"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Link = void 0;
const JSONTemplate_1 = require("modloader64_api/JSONTemplate");
const Z64API = __importStar(require("../../API/imports"));
const Z64CORE = __importStar(require("../importsZ64"));
const GameAliases_1 = require("../Common/types/GameAliases");
class Link extends JSONTemplate_1.JSONTemplate {
    emulator;
    math;
    pointer = Z64CORE.Z64.Z64_PLAYER;
    state_addr = GameAliases_1.Z64_PLAYER_STATE;
    state2_addr = GameAliases_1.Z64_PLAYER_STATE2;
    tunic_addr = this.pointer + 0x013c;
    shield_addr = this.pointer + 0x013e;
    boots_addr = this.pointer + 0x013f;
    mask_addr = this.pointer + 0x014f;
    pos_addr = this.pointer + 0x24;
    rot_addr = this.pointer + 0xb4;
    sword_addr = Z64CORE.Z64.Z64_SAVE + 0x0070 + 0x1;
    sound_addr = 0x600000 + 0x88;
    rotation;
    position;
    homePos;
    homeRot;
    actorUUID = 'Link';
    isTransitionActor = false;
    jsonFields = [
        'state',
        'tunic',
        'shield',
        'boots',
        'mask',
        'pos',
        'rot',
        'anim_data',
        'current_sound_id',
    ];
    constructor(emu, math) {
        super();
        this.emulator = emu;
        this.rotation = new Z64CORE.Z64.Rotation(this);
        this.position = new Z64CORE.Z64.Position(this);
        this.homePos = new Z64CORE.Z64.Position(this, 0x8);
        this.homeRot = new Z64CORE.Z64.Rotation(this, 0xC);
        this.math = math;
    }
    getRdramBuffer() {
        return this.emulator.getRdramBuffer();
    }
    bitCount8(value) {
        return this.emulator.bitCount8(value);
    }
    bitCount16(value) {
        return this.emulator.bitCount16(value);
    }
    bitCount32(value) {
        return this.emulator.bitCount32(value);
    }
    bitCountBuffer(buf, off, len) {
        return this.emulator.bitCountBuffer(buf, off, len);
    }
    invalidateCachedCode() {
    }
    get actorID() {
        return this.rdramRead16(0x0);
    }
    get actorType() {
        return this.rdramRead8(0x2);
    }
    get room() {
        return this.rdramRead8(0x3);
    }
    set room(r) {
        this.rdramWrite8(0x3, r);
    }
    get renderingFlags() {
        return this.rdramRead32(0x4);
    }
    set renderingFlags(flags) {
        this.rdramWrite32(0x4, flags);
    }
    get variable() {
        return this.rdramRead16(0x1c);
    }
    get objectTableIndex() {
        return this.rdramRead8(0x1e);
    }
    get soundEffect() {
        return this.rdramRead16(0x20);
    }
    set soundEffect(s) {
        this.rdramWrite16(0x20, s);
    }
    get health() {
        return this.emulator.rdramRead16(GameAliases_1.Z64_SAVE + 0x30);
    }
    set health(h) {
        this.emulator.rdramWrite16(GameAliases_1.Z64_SAVE + 0x30, h);
    }
    get redeadFreeze() {
        return this.rdramReadS16(0x110);
    }
    set redeadFreeze(f) {
        this.rdramWrite16(0x110, f);
    }
    get exists() {
        return this.emulator.rdramRead32(this.pointer) === 0x2ff;
    }
    get rawStateValue() {
        return this.emulator.rdramRead32(this.state_addr);
    }
    destroy() { }
    get state() {
        switch (this.emulator.rdramRead32(this.state_addr)) {
            case 0:
                return 1 /* Z64API.Z64.LinkState.STANDING */;
            case 0x00000008:
                return 1 /* Z64API.Z64.LinkState.STANDING */;
            case 0x20000000:
                return 4 /* Z64API.Z64.LinkState.BUSY */;
            case 0x30000000:
                return 3 /* Z64API.Z64.LinkState.OCARINA */;
            case 0x20000001:
                return 5 /* Z64API.Z64.LinkState.LOADING_ZONE */;
            case 0x80000000:
                return 6 /* Z64API.Z64.LinkState.ENTERING_GROTTO */;
            case 0x00100000:
                return 7 /* Z64API.Z64.LinkState.FIRST_PERSON */;
            case 0x00040000:
                return 8 /* Z64API.Z64.LinkState.JUMPING */;
            case 0x08000000:
                return 2 /* Z64API.Z64.LinkState.SWIMMING */;
            case 0x00004000:
                return 9 /* Z64API.Z64.LinkState.CLIMBING_OUT_OF_WATER */;
            case 0x00002000:
                return 10 /* Z64API.Z64.LinkState.HANGING_FROM_LEDGE */;
            case 0x00800000:
                return 15 /* Z64API.Z64.LinkState.RIDING_EPONA */;
            case 0x00000080:
                return 16 /* Z64API.Z64.LinkState.DYING */;
            case 0x04000000:
                return 17 /* Z64API.Z64.LinkState.TAKING_DAMAGE */;
            case 0x00080000:
                return 18 /* Z64API.Z64.LinkState.FALLING */;
            case 0x00068000:
                return 18 /* Z64API.Z64.LinkState.FALLING */;
            case 0xa0040000:
                return 19 /* Z64API.Z64.LinkState.VOIDING_OUT */;
            case 0x20000c00:
                return 13 /* Z64API.Z64.LinkState.GETTING_ITEM */;
            case 0x20010040:
                return 20 /* Z64API.Z64.LinkState.TALKING */;
            case 0x30010040:
                return 20 /* Z64API.Z64.LinkState.TALKING */; // While presenting item?
            case 0x00018000:
                return 22 /* Z64API.Z64.LinkState.Z_TARGETING */;
            case 0x00028000:
                return 22 /* Z64API.Z64.LinkState.Z_TARGETING */;
            case 0x00000800:
                return 12 /* Z64API.Z64.LinkState.HOLDING_ACTOR */;
        }
        return 0 /* Z64API.Z64.LinkState.UNKNOWN */;
    }
    get state2() {
        let s2 = this.emulator.rdramRead32(this.state2_addr);
        let digits = s2.toString().split('');
        let realDigits = digits.map(Number);
        let idle = realDigits[0];
        let crawlspace = realDigits[3];
        let moving = realDigits[6];
        if (idle === 0x1) {
            return 1 /* Z64API.Z64.LinkState2.IDLE */;
        }
        if (crawlspace === 0x4) {
            return 2 /* Z64API.Z64.LinkState2.CRAWLSPACE */;
        }
        if (moving === 0x2) {
            return 3 /* Z64API.Z64.LinkState2.MOVING_FORWARD */;
        }
        return 0 /* Z64API.Z64.LinkState2.UNKNOWN */;
    }
    get tunic() {
        return this.emulator.rdramRead8(this.tunic_addr);
    }
    set tunic(tunic) {
        this.emulator.rdramWrite8(this.tunic_addr, tunic);
    }
    get shield() {
        return this.emulator.rdramRead8(this.shield_addr);
    }
    set shield(shield) {
        this.emulator.rdramWrite8(this.shield_addr, shield);
    }
    get sword() {
        return this.emulator.rdramRead8(this.sword_addr);
    }
    set sword(sword) {
        this.emulator.rdramWrite8(this.sword_addr, sword);
    }
    get boots() {
        return this.emulator.rdramRead8(this.boots_addr);
    }
    set boots(boots) {
        this.emulator.rdramWrite8(this.boots_addr, boots);
    }
    get mask() {
        return this.emulator.rdramRead8(this.mask_addr);
    }
    set mask(mask) {
        this.emulator.rdramWrite8(this.mask_addr, mask);
    }
    get pos() {
        return this.emulator.rdramReadBuffer(this.pos_addr, 0xc);
    }
    set pos(pos) {
        this.emulator.rdramWriteBuffer(this.pos_addr, pos);
    }
    get rot() {
        return this.emulator.rdramReadBuffer(this.rot_addr, 0x8);
    }
    set rot(rot) {
        this.emulator.rdramWriteBuffer(this.rot_addr, rot);
    }
    get anim_data() {
        return this.emulator.rdramReadBuffer(this.pointer + 0x01F0, 0x86);
    }
    set anim_data(buf) {
        this.emulator.rdramWriteBuffer(this.pointer + 0x01F0, buf);
    }
    get current_sound_id() {
        if (this.emulator.rdramRead16(this.sound_addr) > 0) {
            return this.emulator.rdramRead16(this.sound_addr);
        }
        /*         if (this.emulator.rdramRead16(this.sound_addr2) > 0){
                    return this.emulator.rdramRead16(this.sound_addr2);
                } */
        return 0;
    }
    set current_sound_id(s) {
        this.emulator.rdramWrite16(this.sound_addr, s);
    }
    get_anim_id() {
        return this.emulator.rdramRead16(this.pointer + 0x1ae);
    }
    get_anim_frame() {
        return this.emulator.rdramRead16(this.pointer + 0x1f4);
    }
    // Give ILink a complete IMemory implementation for shortcuts.
    rdramRead8(addr) {
        return this.emulator.rdramRead8(this.pointer + addr);
    }
    rdramWrite8(addr, value) {
        this.emulator.rdramWrite8(this.pointer + addr, value);
    }
    rdramRead16(addr) {
        return this.emulator.rdramRead16(this.pointer + addr);
    }
    rdramWrite16(addr, value) {
        this.emulator.rdramWrite16(this.pointer + addr, value);
    }
    rdramWrite32(addr, value) {
        this.emulator.rdramWrite32(this.pointer + addr, value);
    }
    rdramRead32(addr) {
        return this.emulator.rdramRead32(this.pointer + addr);
    }
    rdramReadBuffer(addr, size) {
        return this.emulator.rdramReadBuffer(this.pointer + addr, size);
    }
    rdramWriteBuffer(addr, buf) {
        this.emulator.rdramWriteBuffer(this.pointer + addr, buf);
    }
    dereferencePointer(addr) {
        return this.emulator.dereferencePointer(this.pointer + addr);
    }
    rdramReadS8(addr) {
        return this.emulator.rdramReadS8(this.pointer + addr);
    }
    rdramReadS16(addr) {
        return this.emulator.rdramReadS16(this.pointer + addr);
    }
    rdramReadS32(addr) {
        return this.emulator.rdramReadS32(this.pointer + addr);
    }
    rdramReadBitsBuffer(addr, bytes) {
        return this.emulator.rdramReadBitsBuffer(this.pointer + addr, bytes);
    }
    rdramReadBits8(addr) {
        return this.emulator.rdramReadBits8(this.pointer + addr);
    }
    rdramReadBit8(addr, bitoffset) {
        return this.emulator.rdramReadBit8(this.pointer + addr, bitoffset);
    }
    rdramWriteBitsBuffer(addr, buf) {
        this.emulator.rdramWriteBitsBuffer(this.pointer + addr, buf);
    }
    rdramWriteBits8(addr, buf) {
        this.emulator.rdramWriteBits8(this.pointer + addr, buf);
    }
    rdramWriteBit8(addr, bitoffset, bit) {
        this.emulator.rdramWriteBit8(this.pointer + addr, bitoffset, bit);
    }
    rdramReadPtr8(addr, offset) {
        return this.emulator.rdramReadPtr8(this.pointer + addr, offset);
    }
    rdramWritePtr8(addr, offset, value) {
        this.emulator.rdramWritePtr8(this.pointer + addr, offset, value);
    }
    rdramReadPtr16(addr, offset) {
        return this.emulator.rdramReadPtr16(this.pointer + addr, offset);
    }
    rdramWritePtr16(addr, offset, value) {
        this.emulator.rdramWritePtr16(this.pointer + addr, offset, value);
    }
    rdramWritePtr32(addr, offset, value) {
        this.emulator.rdramWritePtr32(this.pointer + addr, offset, value);
    }
    rdramReadPtr32(addr, offset) {
        return this.emulator.rdramReadPtr32(this.pointer + addr, offset);
    }
    rdramReadPtrBuffer(addr, offset, size) {
        return this.emulator.rdramReadPtrBuffer(this.pointer + addr, offset, size);
    }
    rdramWritePtrBuffer(addr, offset, buf) {
        this.emulator.rdramWritePtrBuffer(this.pointer + addr, offset, buf);
    }
    rdramReadPtrS8(addr, offset) {
        return this.emulator.rdramReadPtrS8(this.pointer + addr, offset);
    }
    rdramReadPtrS16(addr, offset) {
        return this.emulator.rdramReadPtrS16(this.pointer + addr, offset);
    }
    rdramReadPtrS32(addr, offset) {
        return this.emulator.rdramReadPtrS32(this.pointer + addr, offset);
    }
    rdramReadPtrBitsBuffer(addr, offset, bytes) {
        return this.emulator.rdramReadPtrBitsBuffer(this.pointer + addr, offset, bytes);
    }
    rdramReadPtrBits8(addr, offset) {
        return this.emulator.rdramReadPtrBits8(this.pointer + addr, offset);
    }
    rdramReadPtrBit8(addr, offset, bitoffset) {
        return this.emulator.rdramReadPtrBit8(this.pointer + addr, offset, bitoffset);
    }
    rdramWritePtrBitsBuffer(addr, offset, buf) {
        this.emulator.rdramWritePtrBitsBuffer(this.pointer + addr, offset, buf);
    }
    rdramWritePtrBits8(addr, offset, buf) {
        this.emulator.rdramWritePtrBits8(this.pointer + addr, offset, buf);
    }
    rdramWritePtrBit8(addr, offset, bitoffset, bit) {
        this.emulator.rdramWritePtrBit8(this.pointer + addr, offset, bitoffset, bit);
    }
    rdramReadF32(addr) {
        return this.emulator.rdramReadF32(this.pointer + addr);
    }
    rdramReadPtrF32(addr, offset) {
        return this.emulator.rdramReadPtrF32(this.pointer + addr, offset);
    }
    rdramWriteF32(addr, value) {
        this.emulator.rdramWriteF32(this.pointer + addr, value);
    }
    rdramWritePtrF32(addr, offset, value) {
        this.emulator.rdramWritePtrF32(this.pointer + addr, offset, value);
    }
    memoryDebugLogger(bool) { }
    rdramRead64(addr) {
        return this.emulator.rdramRead64(this.pointer + addr);
    }
    rdramReadS64(addr) {
        return this.emulator.rdramReadS64(this.pointer + addr);
    }
    rdramReadF64(addr) {
        return this.emulator.rdramReadF64(this.pointer + addr);
    }
    rdramWrite64(addr, val) {
        this.emulator.rdramWrite64(this.pointer + addr, val);
    }
    rdramWriteF64(addr, val) {
        this.emulator.rdramWriteF64(this.pointer + addr, val);
    }
    rdramReadBigInt64(addr) {
        return this.emulator.rdramReadBigInt64(this.pointer + addr);
    }
    rdramReadBigIntS64(addr) {
        return this.emulator.rdramReadBigIntS64(this.pointer + addr);
    }
    rdramWriteBigInt64(addr, val) {
        this.emulator.rdramWriteBigInt64(this.pointer + addr, val);
    }
    get projected_position() {
        return this.math.rdramReadV3(this.pointer + 0xE4);
    }
    set projected_position(vec) {
        this.math.rdramWriteV3(this.pointer + 0xE4, vec);
    }
    get iframes() {
        return this.emulator.rdramRead8(this.pointer + 0xA68);
    }
    set iframes(f) {
        this.emulator.rdramWrite8(this.pointer + 0xA68, f);
    }
    u8;
    u16;
    u32;
    u64;
    s8;
    s16;
    s32;
    s64;
    f32;
    f64;
}
exports.Link = Link;
//# sourceMappingURL=Link.js.map