KO
games/alis / src / Alis / fn / opcodes_a.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/opcodes.c (MIT, Olivier Huguenot, Vadim Kindl).// First half: lines 1-6575 (everything before cwmov). The second half is opcodes_b.php.// v6+ code (Deus / 3DO / RRQ 3D) is dropped; dropped functions still referenced by the opcode tables// are stubs calling unsupported().use App\Alis\OpcodesConst as K;use App\Alis\OpcodesVars as V;// ============================================================================// MARK: - Additions// ============================================================================// Found-entity table full (keeps a slot for the terminator); overflow would hit atentfunction tabent_full(int $idx): int{    if ($idx < 256 - 1) {        return 0;    }    return 1;}// ============================================================================// MARK: - Stub routines// ============================================================================// cnul is put in place of the non-implemented opcodes in tcodop and tcodesc1; pnul (address 0x0000)// in place of the non-implemented functions of toper, tstore and tadd. Both stop the VM.// Codopname no. 001 opcode 0x00 -> cnul// Codopname no. 056 opcode 0x37 -> cnul// Codopname no. 140 opcode 0x8b -> cnul// Codesc1name no. 01 opcode 0x00 -> cnulfunction cnul(): void{    Alis::$state = C::eAlisStateStopped;}// Opername no. 31 opcode 0x3c -> pnul// Opername no. 32 opcode 0x3e -> pnul// Storename no. 01..03 opcodes 0x00, 0x02, 0x04 -> pnul// Addname no. 01..03 opcodes 0x00, 0x02, 0x04 -> pnulfunction pnul(): void{    Alis::$state = C::eAlisStateStopped;}// Ishar 3 Korean (IBM PC): cdefmap, csetmap, cputmap, ctexmap, cwalkmap, catstmap, cchartmap => map_cnulfunction map_cnul(): void{    Alis::$state = C::eAlisStateStopped;}// ============================================================================// MARK: - Codesc1, Codesc2, Codesc3 routines// ============================================================================// Codopname no. 002 opcode 0x01 cesc1function cesc1(): void{    if (Alis::$platform->version == 10) {        return;    }    readexec_codesc1name();}// ============================================================================// MARK: - Restored non-implemented [N/I] codops (opcodes)// ============================================================================// Codopname no. 003 opcode 0x02 cesc2// Not yet found in ALIS interpreters, restored for a reason.function cesc2(): void{    if (Alis::$platform->version == 10 || Alis::$platform->version >= 60) {        return;    }    readexec_codesc2name();}// Codopname no. 004 opcode 0x03 cesc3// Not yet found in ALIS interpreters, restored for a reason.function cesc3(): void{    // v3 DOS VM (v30/v31): the original handler at 0x38f9 is a bare ret, nothing more is read.    if (Alis::$platform->version == 10 || (Alis::$platform->version >= 30 && Alis::$platform->version < 40) || Alis::$platform->version >= 60) {        return;    }    readexec_codesc3name();}// ============================================================================// MARK: - Non-implemented [N/I] codops (opcodes)// ============================================================================// In the original interpreters, these opcodes are empty (their functions contain only retn).// Codopname no. 005 opcode 0x04 cbreakpt// [N/I]: Robinson's Requiem (IBM PC)function cbreakpt(): void{}// Codopname no. 012 opcode 0x0b cjsrabs// [N/I]: Robinson's Requiem (IBM PC)function cjsrabs(): void{}// Codopname no. 013 opcode 0x0c cjmpabs// [N/I]: Robinson's Requiem (IBM PC)function cjmpabs(): void{}// Codopname no. 014 opcode 0x0d cjsrind16// [N/I]: Robinson's Requiem (IBM PC)function cjsrind16(): void{}// Codopname no. 015 opcode 0x0e cjsrind24// [N/I]: Robinson's Requiem (IBM PC). In the DOS VM these indirect ops are bare RET (no-ops), so reaching// one normally means a previous opcode consumed the wrong number of script bytes.function cjsrind24(): void{}// Codopname no. 016 opcode 0x0f cjmpind16// [N/I]: Robinson's Requiem (IBM PC)function cjmpind16(): void{}// Codopname no. 017 opcode 0x10 cjmpind24// [N/I]: Robinson's Requiem (IBM PC)function cjmpind24(): void{}// Codopname no. 035 opcode 0x22 cmul// [N/I]: Robinson's Requiem (IBM PC)function cmul(): void{}// Codopname no. 036 opcode 0x23 cdiv// [N/I]: Robinson's Requiem (IBM PC)// PORT: renamed op_cdiv: the C name cdiv collides with the runtime.php helper cdiv(int $a, int $b)// (C integer division); the opcode table (opcodes_b.php) references op_cdiv for 0x23.function op_cdiv(): void{}// Codopname no. 202 opcode 0xc9 cscdump// [N/I]: Robinson's Requiem (IBM PC)function cscdump(): void{}// Codopname no. 213 opcode 0xd4 cautobase// [N/I]: Robinson's Requiem (IBM PC)function cautobase(): void{}// Codopname no. 236 opcode 0xeb cscantab// [N/I]: Robinson's Requiem (IBM PC)function cscantab(): void{}// Codopname no. 237 opcode 0xec cneartab// [N/I]: Robinson's Requiem (IBM PC)function cneartab(): void{}// ============================================================================// MARK: - Opcode / Codop (Code-op) routines// ============================================================================// Codopname no. 031 opcode 0x1e cstorefunction cstore(): void{    readexec_opername_saveD7();    cstore_continue();}// Codopname no. 032 opcode 0x1f cevalfunction ceval(): void{    readexec_opername_saveD7();}// Codopname no. 033 opcode 0x20 caddfunction cadd(): void{    readexec_opername_saveD7();    readexec_addname_swap();}// Codopname no. 034 opcode 0x21 csubfunction csub(): void{    readexec_opername_saveD7();    Alis::$varD7 = s32(-Alis::$varD7);    readexec_addname_swap();}// Codopname no. 037 opcode 0x24 cvprintfunction cvprint(): void{    Alis::$charmode = 0;    readexec_opername_saveD7();    putval(s16(Alis::$varD7));}// Codopname no. 038 opcode 0x25 csprintifunction csprinti(): void{    Alis::$charmode = 0;    for ($c = script_read8(); $c != 0; $c = script_read8()) {        put_char($c);    }}// Codopname no. 039 opcode 0x26 csprintafunction csprinta(): void{    if (Alis::$platform->version == 10) {        return;    }    Alis::$charmode = 0;    readexec_opername_saveD7();    put_string();}// Codopname no. 040 opcode 0x27 clocatefunction clocate(): void{    Alis::$charmode = 0;    readexec_opername_saveD7();    readexec_opername_saveD6();    Alis::$xlocate = Alis::$varD7 & 0xff;    Alis::$ylocate = Alis::$varD6 & 0xff;}// Codopname no. 041 opcode 0x28 ctabfunction ctab(): void{    Alis::$charmode = 0;    readexec_opername_saveD7();    Alis::$xlocate = Alis::$varD7 & 0xff;}// Codopname no. 042 opcode 0x29 cdimfunction cdim(): void{    $offset = s16(script_read16());    $counter = script_read8();    $byte2 = script_read8();    $offset = s16($offset - 1);    xwrite8(Alis::$script->vram_org + $offset, $counter);    $offset = s16($offset - 1);    xwrite8(Alis::$script->vram_org + $offset, $byte2);    if (Alis::$platform->version >= 60) {        $offset = s16($offset - 2);        xwrite16(Alis::$script->vram_org + $offset, script_read16());        for ($i = 0; $i < s8($counter) + 1; $i++) {            $offset = s16($offset - 4);            xwrite32(Alis::$script->vram_org + $offset, script_read32());        }    } elseif (Alis::$platform->version >= 30) {        $counter16 = $counter & 0xf;        if (-1 < s8($counter)) {            while ($counter16-- > 0) {                $offset = s16($offset - 2);                xwrite16(Alis::$script->vram_org + $offset, script_read16());            }            $offset = s16($offset - 4);            xwrite32(Alis::$script->vram_org + $offset, script_read32());        } else {            while ($counter16-- > 0) {                $offset = s16($offset - 4);                xwrite32(Alis::$script->vram_org + $offset, script_read32());            }            $offset = s16($offset - 4);            xwrite32(Alis::$script->vram_org + $offset, script_read32());        }    } else {        // loop w/ counter, read words, store backwards        while ($counter--) {            $offset = s16($offset - 2);            xwrite16(Alis::$script->vram_org + $offset, script_read16());        }    }}// Codopname no. 043 opcode 0x2a crandomfunction crandom(): void{    if (is_rrq_dos_v3()) {        // DOS v3: D6 = D7 first, the seed is stored as is (0 included, no special case)        readexec_opername_saveD7();        Alis::$random_number = Alis::$varD7 & 0xffff;        return;    }    readexec_opername();    Alis::$random_number = Alis::$varD7 & 0xffff;    if (Alis::$random_number == 0) {        Alis::$random_number = sys_random() & 0xffff;        // test key/joy input, if zero, random is set to $64 ???    }}/** Robinson's Requiem, MS-DOS v3 VM (rrq_dos32): some opcodes are stubs there. */function is_rrq_dos_v3(): int{    $pl = Alis::$platform;    return $pl->kind == C::EPlatformPC && $pl->version >= 30 && $pl->version < 60        && ($pl->uid == C::EGameRobinsonsRequiem0 || $pl->uid == C::EGameRobinsonsRequiem1) ? 1 : 0;}function read_off24(): int{    if (Alis::$platform->version >= 60) {        return read_off24_v6();    }    if (Alis::$platform->is_little_endian) {        $o = s16(script_read16());        script_read8();        return $o;    }    $hi = s8(script_read8());    return s32(shl32($hi, 16) | script_read16());}function cloop(int $offset): void{    // NOTE: looks like the only code called is adir* and aloc*    // we set zero flag using last called instructions there    V::$save_loop_pc = Alis::$script->pc;    Alis::$varD7 = -1;    readexec_addname_swap();    if (!(Alis::$sr['zero'] ?? 0)) {        Alis::$script->pc = V::$save_loop_pc;        Alis::$script->pc += $offset;    }}// Codopname no. 044 opcode 0x2b cloop8function cloop8(): void{    cloop(s8(script_read8()));}// Codopname no. 045 opcode 0x2c cloop16function cloop16(): void{    cloop(s16(script_read16()));}// Codopname no. 046 opcode 0x2d cloop24function cloop24(): void{    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        $o = s16(script_read16());        script_read8();        cloop($o);        return;    }    cloop(read_off24());}// Codopname no. 047 opcode 0x2e cswitch1function cswitch1(): void{    readexec_opername();    $val = s16(Alis::$varD7);    $test = 0;    $addition = script_read8();    if ((Alis::$script->pc & 1) != 0) {        Alis::$script->pc++;    }    do {        $test = s16(script_read16());        if ($val == $test) {            Alis::$script->pc += script_read16();            return;        }        Alis::$script->pc += 2;    } while ($val > $test && (--$addition) > -1);    if ($addition > 0) {        Alis::$script->pc += ($addition * 4);    }}// Codopname no. 048 opcode 0x2f cswitch2function cswitch2(): void{    readexec_opername();    $addition = script_read8();    if ((Alis::$script->pc & 1) != 0) {        Alis::$script->pc++;    }    $val = s16(Alis::$varD7 + script_read16());    Alis::$varD7 = $val;    if ($val >= 0 && $val <= $addition) {        Alis::$varD7 = $val * 2;        Alis::$script->pc += Alis::$varD7;        Alis::$script->pc += script_read16();    } else {        Alis::$script->pc += ($addition * 2 + 2);    }}// Codopname no. 052 opcode 0x33 cleavefunction cleave(): void{    if (Alis::$fseq == 0) {        $vacc_offset = get_0x0c_vacc_offset(Alis::$script->vram_org);        if ($vacc_offset != 0) {            set_0x0c_vacc_offset(Alis::$script->vram_org, 0);            set_0x08_script_ret_offset(Alis::$script->vram_org, xread32(Alis::$script->vram_org + $vacc_offset));            set_0x0a_vacc_offset(Alis::$script->vram_org, $vacc_offset + 4);            return;        }    } else {        Alis::$script->vacc_off = get_0x0c_vacc_offset(Alis::$script->vram_org);        if (Alis::$script->vacc_off != 0) {            set_0x0c_vacc_offset(Alis::$script->vram_org, 0);            Alis::$script->pc = Alis::$script->pc_org + xpop32();            return;        }    }    cret();}// Codopname no. 053 opcode 0x34 cprotectfunction cprotect(): void{    readexec_opername();    Alis::$vprotect = s8(Alis::$varD7);}// Codopname no. 054 opcode 0x35 casleepfunction casleep(): void{    readexec_opername();    if (-1 < Alis::$varD7) {        $vram = xread32(Alis::$atent + Alis::$varD7);        if ($vram) {            set_0x04_cstart_csleep($vram, 0);        }    }}// Codopname no. 057 opcode 0x38 cscmovfunction cscmov(): void{    readexec_opername_saveD7();    $x = s16(Alis::$varD7);    readexec_opername_saveD7();    $y = s16(Alis::$varD7);    readexec_opername_saveD7();    $z = s16(Alis::$varD7);    $scridx = get_0x16_screen_id(Alis::$script->vram_org);    if (Alis::$platform->version < 30) {        set_scr_unknown0x2e($scridx, $z);        set_scr_unknown0x2c($scridx, $y);        set_scr_unknown0x2a($scridx, $x);        set_scr_state($scridx, get_scr_state($scridx) | 0x80);    } else {        if ($z != 0) {            set_scr_depz($scridx, get_scr_depz($scridx) + $z);            set_scr_state($scridx, get_scr_state($scridx) | 0x80);        }        if ($y != 0) {            set_scr_depy($scridx, get_scr_depy($scridx) + $y);            set_scr_state($scridx, get_scr_state($scridx) | 0x80);        }        if ($x != 0) {            set_scr_depx($scridx, get_scr_depx($scridx) + $x);            set_scr_state($scridx, get_scr_state($scridx) | 0x80);        }    }}// Codopname no. 058 opcode 0x39 cscsetfunction cscset(): void{    readexec_opername();    $x = s16(Alis::$varD7);    readexec_opername();    $y = s16(Alis::$varD7);    readexec_opername();    $z = s16(Alis::$varD7);    $scridx = get_0x16_screen_id(Alis::$script->vram_org);    if (Alis::$platform->version < 30) {        set_scr_unknown0x2e($scridx, $z - get_scr_depz($scridx));        set_scr_unknown0x2c($scridx, $y - get_scr_depy($scridx));        set_scr_unknown0x2a($scridx, $x - get_scr_depx($scridx));        set_scr_state($scridx, get_scr_state($scridx) | 0x80);    } else {        if ($z != get_scr_depz($scridx)) {            set_scr_depz($scridx, $z);            set_scr_state($scridx, get_scr_state($scridx) | 0x80);        }        if ($y != get_scr_depy($scridx)) {            set_scr_depy($scridx, $y);            set_scr_state($scridx, get_scr_state($scridx) | 0x80);        }        if ($x != get_scr_depx($scridx)) {            set_scr_depx($scridx, $x);            set_scr_state($scridx, get_scr_state($scridx) | 0x80);        }    }}// Codopname no. 059 opcode 0x3a cclippingfunction cclipping(): void{    // The v3 DOS original only clears an inert byte (nothing reads it differently):    // keep fswitch as set by switching, or the port's renderer takes its no-display path.    if (Alis::$platform->version >= 30) {        return;    }    Alis::$fswitch = 0;}// Codopname no. 060 opcode 0x3b cswitching// [N/I]: Robinson's Requiem, Storm Master, Ishar 3 Korean (IBM PC)// TODO: the PC original never sets fswitch, but this port must or nothing is shown.function cswitching(): void{    Alis::$fswitch = 1;}function get_vector(int &$x, int &$y, int &$z): void{    readexec_opername();    $index = s16(Alis::$varD7);    $frmidx = adresform($index);    $test = xread8($frmidx);    if ($test == 0) {        $x = s16(xread8($frmidx + 4) + (xread8($frmidx + 7) >> 1));        $z = s16(xread8($frmidx + 5) + (xread8($frmidx + 8) >> 1));        $y = s16(xread8($frmidx + 6) + (xread8($frmidx + 9) >> 1));    } elseif (s8($test) < 0) {        Alis::$wcx = 0;        Alis::$wcy = 0;        Alis::$wcz = 0;        return;    } else {        if ($test == 1) {            $x = s16(xread16($frmidx + 4) + (xread16($frmidx + 0xa) >> 1));            $z = s16(xread16($frmidx + 6) + (xread16($frmidx + 0xc) >> 1));            $y = s16(xread16($frmidx + 8) + (xread16($frmidx + 0xe) >> 1));        } else {            $x = xread16($frmidx + 2);            $z = xread16($frmidx + 4);            $y = xread16($frmidx + 6);        }    }    if (0x14 < get_0x2e_script_header_word_2(Alis::$script->vram_org) && get_0x03_xinv(Alis::$script->vram_org) != 0) {        $x = s16(-$x);    }}// Codopname no. 061 opcode 0x3c cwlivefunction cwlive(): void{    get_vector(Alis::$wcx, Alis::$wcy, Alis::$wcz);    Alis::$varD7 = -1;    clivin();}// Codopname no. 062 opcode 0x3d cunloadfunction cunload(): void{    if (Alis::$platform->version >= 60) {        readexec_opername_swap();        $name = substr(mem_str(Alis::$sd7), 0, 15); // strncpy(name, alis.sd7, sizeof(name) - 1)        readexec_opername();        v6_unloadprog($name, Alis::$platform->kind == C::EPlatform3DO ? 0 : s32(Alis::$varD7));        return;    }    cunload_id(s16(script_read16()));}/** cunload() of versions < 60 once the script id is read (the compiled scripts call it with the id as a constant) */function cunload_id(int $id): void{    if ($id == -1 || $id == get_0x10_script_id(Alis::$script->vram_org)) {        return;    }    $index = s16(debprotf($id));    if ($index == -1) {        return;    }    $data_org = K::ATPROG($index);    $data_end = 0xfffffff;    for ($i = 0; $i < Alis::$nbprog; $i++) {        if (ult($data_org, K::ATPROG($i)) && ult(K::ATPROG($i), $data_end)) {            $data_end = K::ATPROG($i);        }    }    if ($data_end == 0xfffffff) {        $data_end = Alis::$finprog;    }    $length = s32($data_end - $data_org);    if (Alis::$platform->kind == C::EPlatformPC && Alis::$platform->version >= 30) {        // DOS v3 (0xb6c5 -> 0x108e4): the sample-stop routine is called with the script's length        // in AX, an original bug with an audible effect: bit 7 of the low byte set stops every        // channel, a length below the channel count stops that channel.        // (A negative 16-bit length with bit 7 clear writes past the channel table: ignored.)        $ax = s16($length & 0xffff);        if ($length & 0x80) {            offsound();        } elseif ($ax >= 0 && $ax < 4) {            channel_off($ax);        }    }    // NOTE: interuptable delay to make everything work more like on original platforms    sys_sleep_interactive(Alis::$unload_delay, Alis::$unload_delay - 100);    shrinkprog($data_org, $length, $id & 0xffff);}// Codopname no. 063 opcode 0x3e cwakeupfunction cwakeup(): void{    readexec_opername();    if (-1 < Alis::$varD7) {        $vram = xread32(Alis::$atent + Alis::$varD7);        if ($vram) {            set_0x04_cstart_csleep($vram, 1);        }    }}// Codopname no. 067 opcode 0x42 cstopfunction cstop(): void{    // in real program, adds 4 to real stack pointer    Alis::$script->running = 0;}// Codopname no. 064 opcode 0x3f csleepfunction csleep(): void{    set_0x04_cstart_csleep(Alis::$script->vram_org, 0);    if (Alis::$fseq) {        cstop();    }}// Codopname no. 065 opcode 0x40 clivefunction clive(): void{    Alis::$wcx = 0;    Alis::$wcy = 0;    Alis::$wcz = 0;    Alis::$varD7 = -1;    clivin();}function clivin(): void{    if (Alis::$platform->version >= 60) {        readexec_opername_swap();        $go = clivin_prog(node_prog(node_find(mem_str(Alis::$sd7))));    } else {        $go = clivin_id(s16(script_read16()));    }    if ($go) {        cstore_continue();    }}/** * clivin() of versions < 60 once the script id is read (the compiled scripts call it with the id as a constant): * true when the store target follows (the VM then runs cstore_continue()), false when the original returns before it. */function clivin_id(int $id): bool{    $prog = null;    if ($id == -1) {        Alis::$varD7 = -1;    } else {        $index = s16(debprotf($id));        if ($index != -1) {            $prog = Alis::$loaded_scripts[$index];        }    }    return clivin_prog($prog);}/** the rest of clivin(): starts the script $prog (null: none); false when the store target is skipped */function clivin_prog(?ScriptData $prog): bool{    if ($prog) {        $script = script_live($prog);        if ($script === null) {            Alis::$varD7 = 0;            return false;        }        $prev_vram = Alis::$script->vram_org;        $next = $script->vram_org;        if (Alis::$platform->version < 60) {        if (Alis::$platform->version >= 30) {            mem_move($next + 0x00, $prev_vram + 0x00, 4);            mem_move($next + 0x08, $prev_vram + 0x08, 4);            mem_move($next + 0x10, $prev_vram + 0x10, 4);            mem_move($next + 0x30, $prev_vram + 0x30, 2);            mem_move($next + K::ALIS_SCR_WCX2(), $prev_vram + K::ALIS_SCR_WCX2(), 2);            mem_move($next + K::ALIS_SCR_WCY2(), $prev_vram + K::ALIS_SCR_WCY2(), 2);            mem_move($next + K::ALIS_SCR_WCZ2(), $prev_vram + K::ALIS_SCR_WCZ2(), 2);            mem_move($next + K::ALIS_SCR_WCAX(), $prev_vram + K::ALIS_SCR_WCAX(), 4);            mem_move($next + K::ALIS_SCR_WCAY(), $prev_vram + K::ALIS_SCR_WCAY(), 4);            mem_move($next + K::ALIS_SCR_WCAZ(), $prev_vram + K::ALIS_SCR_WCAZ(), 4);            mem_move($next + 0x40, $prev_vram + 0x40, 2);            mem_move($next + 0x44, $prev_vram + 0x44, 2);            mem_move($next + 0x48, $prev_vram + 0x48, 2);        } elseif (Alis::$platform->version == 10) {            // position and velocity (DOS skips bytes 6-7), byte 8 is not inherited            mem_move($next, $prev_vram, Alis::$platform->kind == C::EPlatformPC ? 6 : 8);            mem_move($next + 9, $prev_vram + 9, 3);        } else {            mem_move($next, $prev_vram, 0xb);        }        xadd16($script->vram_org + C::ALIS_SCR_WCX, Alis::$wcx);        xadd16($script->vram_org + K::ALIS_SCR_WCY(), Alis::$wcz);        xadd16($script->vram_org + K::ALIS_SCR_WCZ(), Alis::$wcy);        mem_move($next - 0x28, $prev_vram - 0xe, 2);        mem_move($next - 0x16, $prev_vram - 0x16, 2);        mem_move($next - 0x22, $prev_vram - 0x22, 2);        mem_move($next - 0x2a, $next - 0xe, 2);        } // version < 60        // (the C lists the running scripts here, debug output only)        if (Alis::$maxent < Alis::$nbent) {            // ERROR: Exceeded number of scripts slots!            return false;        }    } elseif (Alis::$platform->version >= 60) {        Alis::$varD7 = 0;    }    return true;}// Codopname no. 066 opcode 0x41 ckillfunction ckill(): void{    readexec_opername_saveD7();    if (Alis::$platform->version >= 60) {        if (Alis::$varD7 <= 0) {            return;        }        Alis::$ferase = 1;    } elseif (Alis::$platform->version >= 30 ? s16(Alis::$varD7) <= 0 : Alis::$varD7 < 0) {        // v3 (0x41a9: test dx,dx / js / jz): entity 0 (the main script) and negative values are ignored        return;    }    killent(Alis::$varD7 & 0xffff);}// Codopname no. 068 opcode 0x43 cstopretfunction cstopret(): void{    if (Alis::$fseq == 0) {        cstop();        // v1.0 and v3 (0x41cb: jz 0x41c0) just stop: no return address popped, vram 0x0c kept        if (Alis::$platform->version == 10 || (Alis::$platform->version >= 30 && Alis::$platform->version < 60)) {            return;        }    }    cret();    Alis::$script->running = 0;}// Codopname no. 069 opcode 0x44 cexitfunction cexit(): void{    if (Alis::$varD5 == 0) {        // Quit: unwind to main(), which tears down.        Alis::$cstopret = 1;        Alis::$state = C::eAlisStateStopped;        Alis::$script->running = 0;        return;    }    killent(Alis::$varD5);    Alis::$script->running = 0;}// Codopname no. 070 opcode 0x45 cloadfunction cload(): void{    // NOTE: interuptable delay to make everything work more like on original platforms    sys_sleep_interactive(Alis::$load_delay, Alis::$load_delay - 100);    if (Alis::$platform->version >= 60) {        readexec_opername_swap();        $name = substr(mem_str(Alis::$sd7), 0, 20); // strncpy(name, alis.sd7, sizeof(name) - 4)        $node = node_find($name);        if ($node == 0) {            $name .= '.' . Alis::$platform->ext;            $path = Alis::$platform->path . $name;            $loaded = script_load(strlower($path));            $node = $loaded ? $loaded->node : 0;            is_delay_script($name);        }        v6_wakeall($node);        return;    }    // get script ID    $id = script_read16();    if ($id != 0) {        // not main script, unpack and load into vm        // script_read_until_zero(name)        $name = '';        while (($c = script_read8()) != 0) {            $name .= chr($c);        }        cload_name($name);    } else {        // NOTE: load main script and start game loop        // we are dooing it elsewhere, shouldnt ever be reached;        if (Alis::$platform->version == 10) {            while (script_read8() != 0) {            }        } else {            readexec_opername_swap();        }    }}/** cload() of versions < 60, a script other than the main one, once its name is read (the compiled scripts call it) */function cload_name(string $name): void{    $path = Alis::$platform->path;    // strcpy(strrchr(name, '.') + 1, alis.platform.ext);    $dot = strrpos($name, '.');    $name = substr($name, 0, $dot === false ? 0 : $dot + 1) . Alis::$platform->ext;    $path .= $name;    $loaded = script_load($path);    if (!$loaded && AlisVars::$alis_fatal) {   // C: const char *alis_fatal (alis.c)        // Out of memory: the original stops with a fatal error too.        Alis::$cstopret = 1;        Alis::$state = C::eAlisStateStopped;        Alis::$script->running = 0;        return;    }    is_delay_script($name);}function cdefsc(): void{    if (Alis::$platform->version >= 60) {        $scene = s16(xread16(Alis::$script->vram_org - 0x64));        readexec_opername();        xwrite8(Alis::$basemain + ($scene & 0xffff) + 0x5a, Alis::$varD7 & 0xff);        return;    }    $scridx = s16(script_read16());    $numelem = script_read8();    $length = Alis::$platform->version == 10 ? 26 : 32;    $bytes = substr(Alis::$mem, Alis::$script->pc, $length);    Alis::$script->pc += $length;    cdefsc_core($scridx, $numelem, $bytes);}/** cdefsc() (versions < 60) once its operands are read: the screen index, the element and the 26 / 32 bytes of its header */function cdefsc_core(int $scridx, int $numelem, string $bytes): void{    set_scr_state($scridx, get_scr_state($scridx) | 0x40);    set_scr_numelem($scridx, $numelem);    set_scr_screen_id($scridx, Img::$libsprit);    $ptr = Alis::$basemain + $scridx + 6;    for ($i = 0, $n = strlen($bytes); $i < $n; $i++, $ptr++) {        Alis::$mem[$ptr] = $bytes[$i];    }    $sprite = sprite_var(Img::$libsprit);    $sprite->link = 0;    $sprite->numelem = get_scr_numelem($scridx);    $x = get_scr_newx($scridx);    $y = get_scr_newy($scridx);    $w = get_scr_width($scridx);    $h = get_scr_height($scridx);    if (Alis::$platform->kind == C::EPlatformMac) {        mac_update_pos($x, $y);        mac_update_pos($w, $h);    }    $sprite->newx = $x;    $sprite->newy = $y;    $sprite->newd = 0x7fff;    $sprite->depx = s16($x + $w);    $sprite->depy = s16($y + $h);    Img::$libsprit = $sprite->to_next;    set_scr_to_next($scridx, 0);    scdosprite($scridx);    if (Alis::$platform->version != 10) {        set_scr_unknown0x2a($scridx, 0);        set_scr_unknown0x2c($scridx, 0);        set_scr_unknown0x2e($scridx, 0);    }    Img::$libsprit = $sprite->to_next;    scadd($scridx);    vectoriel($scridx);}// Codopname no. 072 opcode 0x47 cscreenfunction cscreen(): void{    $screen_id = script_read16();    if (Alis::$platform->version >= 60) {        cscreen_v6x($screen_id);        return;    }    if ($screen_id != get_0x16_screen_id(Alis::$script->vram_org)) {        set_0x16_screen_id(Alis::$script->vram_org, $screen_id);    }}// Codopname no. 073 opcode 0x48 cputfunction cput(): void{    if (Alis::$platform->version >= 60) {        v6_put_entry(0, 0, 0, 0);       // DOS OPCODE_CPUT 00006ecc        return;    }    Alis::$flagmain = 0;    Img::$invert_x = get_0x03_xinv(Alis::$script->vram_org);    $elem = 0;    readexec_opername();    Img::$depx = 0;    Img::$depy = 0;    Img::$depz = 0;    Img::$numelem = $elem;    $idx = Alis::$varD7 & 0xffff;    put($idx);}// Codopname no. 074 opcode 0x49 cputnatfunction cputnat(): void{    if (Alis::$platform->version >= 60) {        v6_put_entry(0, 0, 0, 1);       // DOS OPCODE_CPUTNAT 00006f8d        return;    }    Alis::$flagmain = 0;    Img::$invert_x = get_0x03_xinv(Alis::$script->vram_org);    readexec_opername_saveD7();    Img::$depx = s16(Alis::$varD7);    readexec_opername_saveD7();    Img::$depy = s16(Alis::$varD7);    Img::$depz = 0;    if (Alis::$platform->version != 10) {        readexec_opername_saveD7();        Img::$depz = s16(Alis::$varD7);    }    readexec_opername_saveD7();    readexec_opername_saveD6();    Img::$numelem = s8(Alis::$varD6);    $idx = Alis::$varD7 & 0xffff;    put($idx);}function getprot(): int{    $ax = s16(script_read16());    $uax = $ax & 0xffff;    if ($uax == 0xffff || $uax == 0xfffe) {        return $ax;    }    if ($uax == 0xfffa) {        return s16(script_read16());    }    if ($uax == 0xfffb) {        readexec_opername();        return $ax;    }    if ($uax == 0xfffd || $uax == 0xfffc) {        $w = s16(script_read16());        $ctx = Alis::$script->vram_org + ($w & 0xffff);        if (s8(xread8($ctx - 1)) >= 0) {            readexec_opername();        }        readexec_opername();        return $ax;    }    if ($uax > 0xff00) {        return $ax;    }    $ctx = Alis::$script->vram_org + $uax;    if (s8(xread8($ctx - 1)) >= 0) {        readexec_opername();    }    return $ax;}// Codopname no. 056 opcode 0x37: v6 tween driver, no-op for older games.function canim(): void{    if (Alis::$platform->version >= 60) {        canim_v6();        return;    }    cnul();}// Codopname no. 075 opcode 0x4a cerasefunction cerase(): void{    if (Alis::$platform->version >= 60) {        v6_erase_entry(1);        return;    }    $curidx = 0;    $previdx = 0;    if (searchtete($curidx, $previdx)) {        $screenid = s16(get_0x16_screen_id(Alis::$script->vram_org));        do {            killelem($curidx, $previdx);            if ($curidx == 0) {                Alis::$ferase = 0;                return;            }        } while ($screenid == sprite_var($curidx)->screen_id);    }    Alis::$ferase = 0;}// Codopname no. 076 opcode 0x4b cerasenfunction cerasen(): void{    readexec_opername_saveD7();    Img::$numelem = s8(Alis::$varD7);    $curidx = 0;    $previdx = 0;    while (searchelem($curidx, $previdx)) {        killelem($curidx, $previdx);    }    Alis::$ferase = 0;}// Codopname no. 077 opcode 0x4c csetfunction cset(): void{    if (Alis::$platform->version >= 60) {        readexec_opername();        xwrite32(Alis::$script->vram_org + 0x0c, s32(Alis::$varD7));        readexec_opername();        xwrite32(Alis::$script->vram_org + 0x10, s32(Alis::$varD7));        readexec_opername();        xwrite32(Alis::$script->vram_org + 0x14, s32(Alis::$varD7));        xwrite8(Alis::$script->vram_org + 0x45, 1);        return;    }    readexec_opername();    xwrite16(Alis::$script->vram_org + C::ALIS_SCR_WCX, Alis::$varD7);    readexec_opername();    xwrite16(Alis::$script->vram_org + K::ALIS_SCR_WCY(), Alis::$varD7);    readexec_opername();    xwrite16(Alis::$script->vram_org + K::ALIS_SCR_WCZ(), Alis::$varD7);}// Codopname no. 078 opcode 0x4d cmovfunction cmov(): void{    if (Alis::$platform->version >= 60) {        readexec_opername();        xadd32(Alis::$script->vram_org + 0x0c, s32(Alis::$varD7));        readexec_opername();        xadd32(Alis::$script->vram_org + 0x10, s32(Alis::$varD7));        readexec_opername();        xadd32(Alis::$script->vram_org + 0x14, s32(Alis::$varD7));        xwrite8(Alis::$script->vram_org + 0x45, 1);        return;    }    readexec_opername();    xadd16(Alis::$script->vram_org + C::ALIS_SCR_WCX, Alis::$varD7);    readexec_opername();    xadd16(Alis::$script->vram_org + K::ALIS_SCR_WCY(), Alis::$varD7);    readexec_opername();    xadd16(Alis::$script->vram_org + K::ALIS_SCR_WCZ(), Alis::$varD7);}// Codopname no. 079 opcode 0x4e copenscfunction copensc(): void{    if (Alis::$platform->version >= 60) {        $id = script_read16();        $s = Alis::$basemain + $id;        xwrite8($s + 0x58, xread8($s + 0x58) & 0xbf);        xwrite8($s + 0x58, xread8($s + 0x58) | 0x80);        v6_scdel($id);        v6_scadd($id);        $mode = xread8($s + 0x5a);        if ($mode == 5) {            // copensc v6: mode-5 open hook (FUN_000438f0) not yet ported (debug message only)        } elseif ($mode == 6) {            v6x3d_open($s);            ImageVars::$v6ips_warm = 8;        }        return;    }    $id = script_read16();    Alis::$mem[Alis::$basemain + $id] = chr(ord(Alis::$mem[Alis::$basemain + $id]) & 0xbf);    Alis::$mem[Alis::$basemain + $id] = chr(ord(Alis::$mem[Alis::$basemain + $id]) | 0x80);    scbreak($id);    scadd($id);    if (Alis::$platform->version >= 31) {        if ((xread8(Alis::$basemain + 1 + $id) & 2) != 0) {            openland($id);        }    }}// Codopname no. 080 opcode 0x4f cclosescfunction cclosesc(): void{    $id = script_read16();    if (Alis::$platform->version >= 60) {        $s = Alis::$basemain + $id;        xwrite8($s + 0x58, xread8($s + 0x58) | 0x40); // OR 0x40        return;    }    Alis::$mem[Alis::$basemain + $id] = chr(ord(Alis::$mem[Alis::$basemain + $id]) | 0x40);}// Codopname no. 081 opcode 0x50 cerasallfunction cerasall(): void{    $tmpidx = 0;    $curidx = get_0x18_unknown(Alis::$script->vram_org);    while ($curidx) {        killelem($curidx, $tmpidx);    }    Alis::$ferase = 0;}// Codopname no. 082 opcode 0x51 cformefunction cforme(): void{    readexec_opername();    set_0x1a_cforme(Alis::$script->vram_org, Alis::$varD7);}// Codopname no. 083 opcode 0x52 cdelformefunction cdelforme(): void{    set_0x1a_cforme(Alis::$script->vram_org, -1);}function formptr(int $base, int $idx2): int{    $off = xread16($base + $idx2);    return s32($base + (Alis::$platform->kind == C::EPlatformPC && Alis::$platform->version < 30 ? ($off & 0xffff) : $off));}function formkind(int $hdr): int{    if (Alis::$platform->uid == C::EGameLeFeticheMaya) {        return (int) (xread16($hdr) != 0);    }    if (Alis::$platform->kind != C::EPlatformPC && xread16($hdr) == 0) {        return 0;    }    $b = xread8($hdr);    return Alis::$platform->kind != C::EPlatformPC && $b == 0 ? -1 : $b;}function multifirm(int $ent_vram, int $ent_formedata, int $formedata, int $a4): int{    $length = xread8($ent_formedata - 1);    for ($i = 0; $i < $length; $i++, $ent_formedata += 2) {        $forme = xread16($ent_formedata);        if (-1 < $forme) {            if (!traitfirm($ent_vram, $formedata, $a4, $forme + $forme)) {                // Hit!                return 0;            }        }    }    // No hits!    return 1;}function traitfirm(int $ent_vram, int $formedata, int $ent_baseform, int $forme): int{    $ent_formedata = formptr($ent_baseform, $forme);    $test = Alis::$platform->kind == C::EPlatformPC ? s8(xread8($ent_formedata)) : xread16($ent_formedata);    if ($test < 0) {        return multifirm($ent_vram, $ent_formedata + 2, $formedata, $ent_baseform);    }    return monofirm($ent_vram, $ent_formedata + 2);}function monofirm(int $ent_vram, int $ent_formedata): int{    $result = (xread16($ent_formedata) & Alis::$matmask) & 0xffff;    if ($result == 0) {        // Masked        return 1;    }    $bits = formkind($ent_formedata - 2);    if ($bits == 0) {        $tmpx1 = s8(xread8($ent_formedata + 2));        $tmpy1 = s8(xread8($ent_formedata + 3));        $tmpz1 = s8(xread8($ent_formedata + 4));        $tmpx2 = s8(xread8($ent_formedata + 5));        $tmpy2 = s8(xread8($ent_formedata + 6));        $tmpz2 = s8(xread8($ent_formedata + 7));    } elseif ($bits == 1) {        $tmpx1 = xread16($ent_formedata + 2);        $tmpy1 = xread16($ent_formedata + 4);        $tmpz1 = xread16($ent_formedata + 6);        $tmpx2 = xread16($ent_formedata + 0x8);        $tmpy2 = xread16($ent_formedata + 0xa);        $tmpz2 = xread16($ent_formedata + 0xc);    } else {        // Form type: no hit        return 1;    }    Alis::$goodmat = xread16($ent_formedata) & 0xffff;    if (get_0x03_xinv($ent_vram) != 0) {        $tmpx1 = s16(-$tmpx1);        $tmpx2 = s16(-$tmpx2);    }    $tmpx1 = s16($tmpx1 + xread16($ent_vram + C::ALIS_SCR_WCX));    $tmpy1 = s16($tmpy1 + xread16($ent_vram + K::ALIS_SCR_WCY()));    $tmpz1 = s16($tmpz1 + xread16($ent_vram + K::ALIS_SCR_WCZ()));    $tmpx2 = s16($tmpx2 + $tmpx1);    $tmpy2 = s16($tmpy2 + $tmpy1);    $tmpz2 = s16($tmpz2 + $tmpz1);    $tmp = $tmpy1;    if ($tmpy2 <= $tmpy1) {        $tmp = $tmpy2;        $tmpy2 = $tmpy1;    }    if ((V::$py1 <= $tmpy2) && ($tmp <= V::$py2)) {        $tmp = $tmpz1;        if ($tmpz2 <= $tmpz1) {            $tmp = $tmpz2;            $tmpz2 = $tmpz1;        }        if ((V::$pz1 <= $tmpz2) && ($tmp <= V::$pz2)) {            $tmp = $tmpx1;            if ($tmpx2 <= $tmpx1) {                $tmp = $tmpx2;                $tmpx2 = $tmpx1;            }            if ((V::$px1 <= $tmpx2) && ($tmp <= V::$px2)) {                return 0;            }        }    }    return 1;}function traitform(int $ent_vram, int $ent_formedata, int $ent_baseform, int $ent_wforme2x): int{    $formedata = formptr(Alis::$baseform, $ent_wforme2x);    $test = Alis::$platform->kind == C::EPlatformPC ? s8(xread16($formedata)) : xread16($formedata);    if ($test < 0) {        return multiform($ent_vram, $ent_formedata, $formedata + 2, $ent_baseform);    }    return monoform($ent_vram, $ent_formedata, $formedata + 2, $ent_baseform, $ent_wforme2x);}function multiform(int $ent_vram, int $ent_formedata, int $formedata, int $ent_baseform): int{    $length = s8(xread8($formedata - 1));    for ($i = 0; $i < $length; $i++, $formedata += 2) {        $wforme = xread16($formedata);        if (-1 < $wforme) {            if (!traitform($ent_vram, $ent_formedata, $ent_baseform, $wforme + $wforme)) {                // Hit!                return 0;            }        }    }    // No hits!    return 1;}function monoform(int $ent_vram, int $ent_formedata, int $formedata, int $ent_baseform, int $ent_wforme2x): int{    $bits = formkind($formedata - 2);    if ($bits == 0) {        $frmx = s8(xread8($formedata + 2));        $frmy = s8(xread8($formedata + 3));        $frmz = s8(xread8($formedata + 4));        V::$px2 = s8(xread8($formedata + 5));        V::$py2 = s8(xread8($formedata + 6));        V::$pz2 = s8(xread8($formedata + 7));        $formedata += 8;    } elseif ($bits == 1) {        $frmx = xread16($formedata + 2);        $frmy = xread16($formedata + 4);        $frmz = xread16($formedata + 6);        V::$px2 = xread16($formedata + 0x8);        V::$py2 = xread16($formedata + 0xa);        V::$pz2 = xread16($formedata + 0xc);        $formedata += 0xe;    } else {        // Form type: no hit        return 1;    }    if (get_0x03_xinv(Alis::$script->vram_org) != 0) {        $frmx = s16(-$frmx);        V::$px2 = s16(-V::$px2);    }    $frmx = s16($frmx + Alis::$wcx);    $frmy = s16($frmy + Alis::$wcy);    $frmz = s16($frmz + Alis::$wcz);    V::$px2 = s16(V::$px2 + $frmx);    V::$py2 = s16(V::$py2 + $frmy);    V::$pz2 = s16(V::$pz2 + $frmz);    V::$py1 = $frmy;    if (V::$py2 <= $frmy) {        V::$py1 = V::$py2;        V::$py2 = $frmy;    }    V::$pz1 = $frmz;    if (V::$pz2 <= $frmz) {        V::$pz1 = V::$pz2;        V::$pz2 = $frmz;    }    V::$px1 = $frmx;    if (V::$px2 <= $frmx) {        V::$px1 = V::$px2;        V::$px2 = $frmx;    }    $test = Alis::$platform->kind == C::EPlatformPC ? s8(xread16($ent_formedata)) : xread16($ent_formedata);    $ent_formedata += 2;    if ($test < 0) {        return multifirm($ent_vram, $ent_formedata, $formedata, $ent_baseform);    }    return monofirm($ent_vram, $ent_formedata);}// v1.0 forms: header byte 0 < 0 composite (byte 1 = count, then sub-form indices),// 0 = box of 6 signed bytes, > 0 = box of 6 words (x, y, z, dx, dy, dz)function formbase_v10(int $vram): int{    $addr = get_0x14_script_org_offset($vram);    if (Alis::$platform->kind == C::EPlatformPC) {        $addr += xread16($addr + 0xe) & 0xffff;        $addr += xread16($addr + 6) & 0xffff;    } else {        $addr = s32($addr + xread32($addr + 0xe));        $addr = s32($addr + xread32($addr + 6));    }    return $addr;}function formaddr_v10(int $base, int $idx): int{    return $base + xread16($base + $idx * 2);}// 68k tests the whole header word, x86 only its first bytefunction formhdr_v10(int $at): int{    return Alis::$platform->kind == C::EPlatformPC ? s8(xread8($at)) : xread16($at);}function formbox_v10(int $at, int $hdr, int $vram, int $px, int $py, int $pz, int $bug, array &$b): void{    if ($hdr == 0) {        $x1 = s8(xread8($at + 2));        $y1 = s8(xread8($at + 3));        $z1 = s8(xread8($at + 4));        $x2 = s8(xread8($at + 5));        $y2 = s8(xread8($at + 6));        $z2 = s8(xread8($at + 7));    } else {        $x1 = xread16($at + 2);        $y1 = xread16($at + 4);        $z1 = xread16($at + 6);        $x2 = xread16($at + 8);        $y2 = xread16($at + 10);        $z2 = xread16($at + 12);    }    $inv = (int) (get_0x03_xinv($vram) != 0);    if ($inv) {        $x1 = s16(-$x1);    }    $x1 = s16($x1 + $px);    $y1 = s16($y1 + $py);    $z1 = s16($z1 + $pz);    if ($inv) {        // original 68k bug: word box negates x1 again instead of dx        if ($bug && $hdr != 0) {            $x1 = s16(-$x1);        } else {            $x2 = s16(-$x2);        }    }    $x2 = s16($x2 + $x1);    $y2 = s16($y2 + $y1);    $z2 = s16($z2 + $z1);    if ($y2 <= $y1) {        $t = $y1;        $y1 = $y2;        $y2 = $t;    }    if ($z2 <= $z1) {        $t = $z1;        $z1 = $z2;        $z2 = $t;    }    if ($x2 <= $x1) {        $t = $x1;        $x1 = $x2;        $x2 = $t;    }    $b = [$x1, $y1, $z1, $x2, $y2, $z2];}function formhit_ent_v10(int $at, int $base, int $vram, array $own, int $depth): int{    $hdr = formhdr_v10($at);    if ($hdr < 0) {        if ($depth > 16) {            return 0;        }        $cnt = xread8($at + 1);        for ($i = 0; $i < $cnt; $i++) {            $idx = xread16($at + 2 + $i * 2);            if ($idx >= 0 && formhit_ent_v10(formaddr_v10($base, $idx), $base, $vram, $own, $depth + 1)) {                return 1;            }        }        return 0;    }    $b = [];    formbox_v10($at, $hdr, $vram, xread16($vram + 0), xread16($vram + 2), xread16($vram + 4), 0, $b);    return (int) !($b[4] < $own[1] || $b[1] > $own[4] || $b[5] < $own[2] || $b[2] > $own[5] || $b[3] < $own[0] || $b[0] > $own[3]);}function formhit_own_v10(int $at, int $base, int $ent_form, int $ent_base, int $ent_vram, int $depth): int{    $hdr = formhdr_v10($at);    if ($hdr < 0) {        if ($depth > 16) {            return 0;        }        $cnt = xread8($at + 1);        for ($i = 0; $i < $cnt; $i++) {            $idx = xread16($at + 2 + $i * 2);            if ($idx >= 0 && formhit_own_v10(formaddr_v10($base, $idx), $base, $ent_form, $ent_base, $ent_vram, $depth + 1)) {                return 1;            }        }        return 0;    }    $own = [];    formbox_v10($at, $hdr, Alis::$script->vram_org, Alis::$wcx, Alis::$wcy, Alis::$wcz, (int) (Alis::$platform->kind != C::EPlatformPC), $own);    return formhit_ent_v10($ent_form, $ent_base, $ent_vram, $own, 0);}function clipform_v10(): void{    Alis::$ptrent = 0;    if (Alis::$wforme >= 0) {        $vram = Alis::$script->vram_org;        $base = formbase_v10($vram);        $form = formaddr_v10($base, Alis::$wforme);        $entidx = 0;        do {            $ent_vram = xread32(Alis::$atent + $entidx);            $ent_forme = get_0x1a_cforme($ent_vram);            if ((xread8($ent_vram + 0xc) & (Alis::$matmask & 0xff)) && xread16($ent_vram + 6) == xread16($vram + 6) && $ent_forme >= 0 && $ent_vram != $vram) {                $ent_base = formbase_v10($ent_vram);                if (formhit_own_v10($form, $base, formaddr_v10($ent_base, $ent_forme), $ent_base, $ent_vram, 0)) {                    $index = Alis::$ptrent;                    if ($index >= 127) {                        break;                    }                    Alis::$matent[$index] = 0;                    Alis::$tablent[$index] = $entidx;                    Alis::$ptrent++;                    if (Alis::$fallent == 0) {                        break;                    }                }            }            $entidx = xread16(Alis::$atent + 4 + $entidx);        } while ($entidx != 0);    }    $index = Alis::$ptrent;    Alis::$matent[$index] = 0;    Alis::$tablent[$index] = -1;    Alis::$ptrent++;}// check for intersected objectfunction clipform(): void{    if (Alis::$platform->version == 10) {        clipform_v10();        return;    }    Alis::$ptrent = 0;    if (-1 < Alis::$wforme) {        $val = get_0x14_script_org_offset(Alis::$script->vram_org);        $addr = s32(xread32($val + 0xe) + $val);        Alis::$baseform = s32(xread32($addr + 6) + $addr);        $formedata = formptr(Alis::$baseform, s32(Alis::$wforme * 2));        $entidx = s16(alis_first_ent());        // form index past the script's form count (+0xa)        // perror 0x10 in the Falcon builds, skipped here        $range = (int) (Alis::$wforme < (xread16($addr + 0xa) & 0xffff));        do {            $ent_vram = xread32(Alis::$atent + $entidx);            if ($range && xread16($ent_vram + K::ALIS_SCR_GROUP()) == xread16(Alis::$script->vram_org + K::ALIS_SCR_GROUP()) && -1 < get_0x1a_cforme($ent_vram) && Alis::$script->vram_org != $ent_vram) {                $ent_val = get_0x14_script_org_offset($ent_vram);                $ent_addr = s32(xread32($ent_val + 0xe) + $ent_val);                $ent_baseform = s32(xread32($ent_addr + 6) + $ent_addr);                if (get_0x1a_cforme($ent_vram) >= (xread16($ent_addr + 0xa) & 0xffff)) {                    // form out of range                    $entidx = xread16(Alis::$atent + 4 + $entidx);                    continue;   // to the while test                }                $ent_wforme2x = get_0x1a_cforme($ent_vram) * 2;                $ent_formedata = formptr($ent_baseform, $ent_wforme2x);                $result = 0;                $test = Alis::$platform->kind == C::EPlatformPC ? s8(xread16($formedata)) : xread16($formedata);                if ($test < 0) {                    $result = multiform($ent_vram, $ent_formedata, $formedata + 2, $ent_baseform);                } else {                    $result = monoform($ent_vram, $ent_formedata, $formedata + 2, $ent_baseform, $ent_wforme2x);                }                if (!$result) {                    if (Alis::$witmov != 0) {                        Alis::$fmitmov = 1;                        return;                    }                    $index = Alis::$ptrent;                    if (tabent_full($index)) {                        break;                    }                    Alis::$matent[$index] = s16(Alis::$goodmat);                    Alis::$tablent[$index] = $entidx;                    Alis::$ptrent++;                    if (Alis::$fallent == 0) {                        break;                    }                }            }            $entidx = xread16(Alis::$atent + 4 + $entidx);        } while ($entidx != 0);    }    if (Alis::$witmov == 0) {        // NOTE: matent!!!        $index = Alis::$ptrent;        Alis::$matent[$index] = 0;        Alis::$tablent[$index] = ENTLIST_END();        Alis::$ptrent++;    }}// Codopname no. 088 opcode 0x57 csuccentfunction csuccent(): void{    Alis::$varD7 = Alis::$tablent[Alis::$ptrent];    if (Alis::$platform->version >= 60 ? (Alis::$tablent[Alis::$ptrent] != 0) : (-1 < Alis::$tablent[Alis::$ptrent])) {        Alis::$ptrent++;    }    cstore_continue();}// Codopname no. 097 opcode 0x60 crstentfunction crstent(): void{    Alis::$ptrent = 0;    csuccent();}// Codopname no. 084 opcode 0x53 ctstmovfunction ctstmov(): void{    readexec_opername();    Alis::$wcx = s16(xread16(Alis::$script->vram_org + C::ALIS_SCR_WCX) + Alis::$varD7);    readexec_opername();    Alis::$wcy = s16(xread16(Alis::$script->vram_org + K::ALIS_SCR_WCY()) + Alis::$varD7);    readexec_opername();    Alis::$wcz = s16(xread16(Alis::$script->vram_org + K::ALIS_SCR_WCZ()) + Alis::$varD7);    readexec_opername();    Alis::$matmask = s16(Alis::$varD7);    Alis::$wforme = get_0x1a_cforme(Alis::$script->vram_org);    clipform();    crstent();}// Codopname no. 085 opcode 0x54 ctstsetfunction ctstset(): void{    if (Alis::$platform->version == 10) {        readexec_opername();        Alis::$wcx = s16(Alis::$varD7);        readexec_opername();        Alis::$wcy = s16(Alis::$varD7);        readexec_opername();        Alis::$wcz = s16(Alis::$varD7);        readexec_opername();        Alis::$matmask = s16(Alis::$varD7);        Alis::$wforme = get_0x1a_cforme(Alis::$script->vram_org);        clipform();        crstent();        return;    }    // MISSING: ctstset}// Codopname no. 086 opcode 0x55 cftstmovfunction cftstmov(): void{    readexec_opername();    Alis::$wcx = s16(xread16(Alis::$script->vram_org + C::ALIS_SCR_WCX) + Alis::$varD7);    readexec_opername();    Alis::$wcy = s16(xread16(Alis::$script->vram_org + K::ALIS_SCR_WCY()) + Alis::$varD7);    readexec_opername();    Alis::$wcz = s16(xread16(Alis::$script->vram_org + K::ALIS_SCR_WCZ()) + Alis::$varD7);    readexec_opername();    Alis::$matmask = s16(Alis::$varD7);    readexec_opername();    Alis::$wforme = s16(Alis::$varD7);    clipform();    crstent();}// Codopname no. 087 opcode 0x56 cftstsetfunction cftstset(): void{    if (Alis::$platform->version == 10) {        // v1.0 handler is relative, same as cftstmov        cftstmov();        return;    }    readexec_opername();    Alis::$wcx = s16(Alis::$varD7);    readexec_opername();    Alis::$wcy = s16(Alis::$varD7);    readexec_opername();    Alis::$wcz = s16(Alis::$varD7);    readexec_opername();    Alis::$matmask = s16(Alis::$varD7);    readexec_opername();    Alis::$wforme = s16(Alis::$varD7);    clipform();    crstent();}// Codopname no. 089 opcode 0x58 cpredentfunction cpredent(): void{    if (Alis::$ptrent != 0) {        Alis::$ptrent--;        Alis::$varD7 = Alis::$tablent[Alis::$ptrent];    } else {        Alis::$varD7 = -1;    }    cstore_continue();}function calcnear(int $source, int $target, int &$val): int{    $wcx = xread16($target + C::ALIS_SCR_WCX) - xread16($source + C::ALIS_SCR_WCX);    $wcy = xread16($target + K::ALIS_SCR_WCY()) - xread16($source + K::ALIS_SCR_WCY());    $wcz = xread16($target + K::ALIS_SCR_WCZ()) - xread16($source + K::ALIS_SCR_WCZ());    if (Alis::$platform->version == 10) {        // v1.0: 16-bit deltas; view drops the y term (68k adds it to d7) and truncates the dot to 16 bits        $dx = s16($wcx);        $dy = s16($wcy);        $dz = s16($wcz);        $val = s32($dx * $dx + $dy * $dy + $dz * $dz);        if ($val > Alis::$valnorme) {            return 1;        }        if (Alis::$fview == 0) {            return 0;        }        Alis::$varD7 = s32(Alis::$varD7 + s8(xread8($source + K::ALIS_SCR_WCY2())) * $dy);        $dot = s16(s8(xread8($source + K::ALIS_SCR_WCX2())) * $dx + s8(xread8($source + K::ALIS_SCR_WCZ2())) * $dz);        return $val <= $dot * Alis::$valchamp ? 0 : 1;    }    $val = s32(s32($wcx * $wcx) + s32($wcy * $wcy) + s32($wcz * $wcz));    // *val (s32) <= alis.valnorme (u32): unsigned compare in C    return (ule($val, Alis::$valnorme) && (Alis::$fview == 0 || $val <= mul32(s8(xread8($source + K::ALIS_SCR_WCX2())) * $wcx + s8(xread8($source + K::ALIS_SCR_WCY2())) * $wcy + s8(xread8($source + K::ALIS_SCR_WCZ2())) * $wcz, Alis::$valchamp))) ? 0 : 1;}function trinorme(int $bufidx): void{    if ($bufidx > 0) {        $result = false;        do {            $length = s16($bufidx);            // PORT: the C never resets bufidx/result, so after a swap it spins forever on length 0;            // stop instead of hanging the frame (a pass of length 0 changes nothing).            if ($length <= 0) {                break;            }            for ($i = 0; $i < $length; $i++, $bufidx--) {                // PORT: index bufidx - 2 reaches -1 on the last step (C reads before alis.buffer): read as 0.                $bufvalue = V::$buffer32[$bufidx - 1] ?? 0;                if ($bufvalue < (V::$buffer32[$bufidx - 2] ?? 0)) {                    V::$buffer32[$bufidx - 1] = V::$buffer32[$bufidx - 2] ?? 0;                    V::$buffer32[$bufidx - 2] = $bufvalue;                    $tblvalue = Alis::$tablent[$bufidx - 1] ?? 0;                    Alis::$tablent[$bufidx - 1] = Alis::$tablent[$bufidx - 2] ?? 0;                    Alis::$tablent[$bufidx - 2] = s16($tblvalue);                    $result = true;                }            }        } while ($result);    }}function findent(int $bytyp, int $near): void{    $src_vram = Alis::$script->vram_org;    $tabidx = 0;    $entidx = 0;    $value = 0;    do {        $tgt_vram = xread32(Alis::$atent + $entidx);        if (($bytyp ? Alis::$varD7 == s16(get_0x10_script_id($tgt_vram)) : (xread8($tgt_vram + 0xc) & (Alis::$matmask & 0xff)) != 0)            && (!$near || xread16($tgt_vram + K::ALIS_SCR_GROUP()) == xread16($src_vram + K::ALIS_SCR_GROUP())) && $src_vram != $tgt_vram            && (!$near || !calcnear($src_vram, $tgt_vram, $value)) && $tabidx < 127) {            Alis::$tablent[$tabidx++] = $entidx;            if (Alis::$fallent == 0) {                break;            }        }    } while (($entidx = xread16(Alis::$atent + 4 + $entidx)));    Alis::$tablent[$tabidx] = -1;    Alis::$fallent = 0;    crstent();}function sviewtyp(): void{    if (Alis::$platform->version == 10) {        findent(1, 1);        return;    }    sviewtyp_list();    crstent();}/** sviewtyp() of versions > 10 before crstent(): the list of the nearest entities of type D7 (Alis::$tablent) */function sviewtyp_list(): void{    $id = s16(Alis::$varD7);    if ($id < 0) {        $id &= 0xff;    }    $bufidx = 0;    if (Alis::$fallent == 0) {        V::$buffer32[$bufidx] = 0x7fffffff;        Alis::$tablent[$bufidx] = -1;   // 0xffff        $bufidx = 1;    }    $veradd = Alis::$platform->version >= 30 ? 0x30 : 0x6;    $entidx = s16(alis_first_ent());    $src_vram = Alis::$script->vram_org;    do {        $tgt_vram = xread32(Alis::$atent + $entidx);        if ($id == get_0x10_script_id($tgt_vram) && xread16($tgt_vram + $veradd) == xread16($src_vram + $veradd) && $src_vram != $tgt_vram) {            $value = 0;            $result = calcnear($src_vram, $tgt_vram, $value);            if (!$result) {                if (Alis::$fallent || V::$buffer32[$bufidx - 1] > $value) {                    if (Alis::$fallent == 0) {                        $bufidx--;                    }                    if (tabent_full($bufidx)) {                        break;                    }                    V::$buffer32[$bufidx] = $value;                    Alis::$tablent[$bufidx] = $entidx;                    $bufidx++;                }            }        }    } while (($entidx = xread16(Alis::$atent + 4 + $entidx)));    Alis::$tablent[$bufidx] = ENTLIST_END();    if (Alis::$fallent != 0) {        trinorme($bufidx);    }    Alis::$fallent = 0;}function multimat(int $formeaddr, int $baseform, int $wforme): int{    $length = xread8($formeaddr - 2);    if ($length == 0) {        return $wforme;    }    for ($i = 0; $i < $length; $i++) {        $wforme = xread16($formeaddr + $i * 2);        if (-1 < $wforme) {            $wforme = s16(traitmat($baseform, $wforme));            if ($wforme) {                return $wforme;            }        }    }    return $wforme;}function traitmat(int $baseform, int $wforme): int{    $wforme *= 2;    // PORT: the C stores this address into an s16 (truncation kept as is); a negative result would    // read outside the memory.    $formeaddr = s16(xread32($baseform + $wforme) + $baseform);    if (xread16($formeaddr) < 0) {        return multimat($formeaddr + 2, $baseform, s16($wforme));    } else {        return monomat($formeaddr + 2);    }}function monomat(int $formeaddr): int{    Alis::$goodmat = xread16($formeaddr) & 0xffff;    return s16(Alis::$matmask & Alis::$goodmat);}function clipmat(int $src_vram, int $tgt_vram): int{    $wforme = get_0x1a_cforme($tgt_vram);    if (-1 < $wforme && $src_vram != $tgt_vram) {        $addr = s32(xread32(get_0x14_script_org_offset($tgt_vram) + 0xe) + get_0x14_script_org_offset($tgt_vram));        $baseform = s32(xread32($addr + 6) + $addr);        $formeaddr = xread16($baseform + $wforme * 2) + $baseform;        if (xread16($formeaddr) < 0) {            return multimat($formeaddr + 2, $baseform, $wforme);        } else {            return monomat($formeaddr + 2);        }    }    return $wforme;}function sviewmat(): void{    if (Alis::$platform->version == 10) {        findent(0, 1);        return;    }    $bufidx = 0;    if (Alis::$fallent == 0) {        V::$buffer32[$bufidx] = 0x7fffffff;        Alis::$tablent[$bufidx] = -1;   // 0xffff        $bufidx = 1;    }    $veradd = Alis::$platform->version >= 30 ? 0x30 : 0x6;    $entidx = s16(alis_first_ent());    $src_vram = Alis::$script->vram_org;    $tgt_vram = 0;    do {        $tgt_vram = xread32(Alis::$atent + $entidx);        if (xread16($src_vram + $veradd) == xread16($tgt_vram + $veradd)) {            $result = clipmat($src_vram, $tgt_vram);            if ($result) {                $value = 0;                $result = calcnear($src_vram, $tgt_vram, $value);                if (!$result) {                    if (Alis::$fallent == 0) {                        if (V::$buffer32[$bufidx - 1] <= $value) {                            goto LAB_00015c8a;                        }                        $bufidx--;                    } elseif (tabent_full($bufidx)) {                        goto LAB_00015c8a;                    }                    V::$buffer32[$bufidx] = $value;                    Alis::$tablent[$bufidx] = $entidx;                    $bufidx++;                }            }        }        LAB_00015c8a:        $entidx = xread16(Alis::$atent + 4 + $entidx);        if ($entidx == 0) {            Alis::$tablent[$bufidx] = ENTLIST_END();            if (Alis::$fallent != 0) {                trinorme($bufidx);            }            Alis::$fallent = 0;            crstent();            return;        }    } while (true);}function sviewent(): void{    if (Alis::$platform->version >= 30) {        sviewent_list();        crstent();    } else {        $entidx = s16(Alis::$varD7);        $tabidx = 0;        $src_vram = Alis::$script->vram_org;        $tgt_vram = xread32(Alis::$atent + $entidx);        if (xread16($tgt_vram + 6) == xread16($src_vram + 6)) {            $tmp = 0;            $result = calcnear($src_vram, $tgt_vram, $tmp);            if (!$result) {                Alis::$tablent[$tabidx++] = $entidx;            }        }        Alis::$tablent[$tabidx] = ENTLIST_END();        Alis::$fallent = 0;        crstent();    }}/** sviewent() of versions >= 30 before crstent(): the list of the nearest entities (Alis::$tablent); the compiled scripts call it */function sviewent_list(): void{    {        if (Alis::$fallent != 0) {            Alis::$varD7 = -1;        }        $bufidx = 0;        if (Alis::$fallent == 0) {            V::$buffer32[$bufidx] = 0x7fffffff;            Alis::$tablent[$bufidx] = -1;   // 0xffff            $bufidx = 1;        }        $entidx = s16(alis_first_ent());        $src_vram = Alis::$script->vram_org;        do {            $tgt_vram = xread32(Alis::$atent + $entidx);            if ((Alis::$varD7 < 0 || Alis::$varD7 == get_0x0e_script_ent($tgt_vram)) && xread16($tgt_vram + 0x30) == xread16($src_vram + 0x30) && $src_vram != $tgt_vram) {                $value = 0;                $result = calcnear($src_vram, $tgt_vram, $value);                if (!$result) {                    if (Alis::$fallent == 0) {                        if (V::$buffer32[$bufidx - 1] <= $value) {                            goto cnearen2;                        }                        $bufidx--;                    } elseif (tabent_full($bufidx)) {                        goto cnearen2;                    }                    V::$buffer32[$bufidx] = $value;                    Alis::$tablent[$bufidx] = $entidx;                    $bufidx++;                }            }            cnearen2:            $entidx = xread16(Alis::$atent + 4 + $entidx);            if ($entidx == 0) {                Alis::$tablent[$bufidx] = ENTLIST_END();                if (Alis::$fallent != 0) {                    trinorme($bufidx);                }                Alis::$fallent = 0;                return;            }        } while (true);    }}// Codopname no. 090 opcode 0x59 cnearentfunction cnearent(): void{    if (Alis::$platform->version >= 60) {        near_v6(1);        return;    }    Alis::$fview = 0;    readexec_opername();    Alis::$valnorme = mul32(Alis::$varD7, Alis::$varD7);    readexec_opername();    sviewent();}// Codopname no. 091 opcode 0x5a cneartypfunction cneartyp(): void{    if (Alis::$platform->version >= 60) {        near_v6(0);        return;    }    Alis::$fview = 0;    readexec_opername();    Alis::$valnorme = mul32(Alis::$varD7, Alis::$varD7);    if (Alis::$platform->kind == C::EPlatformPC) {        Alis::$varD7 = script_read16();    } else {        readexec_opername();    }    sviewtyp();}// Codopname no. 092 opcode 0x5b cnearmatfunction cnearmat(): void{    if (Alis::$platform->version >= 60) {        near_v6(2);        return;    }    Alis::$fview = 0;    readexec_opername();    Alis::$valnorme = mul32(Alis::$varD7, Alis::$varD7);    readexec_opername();    Alis::$matmask = s16(Alis::$varD7);    sviewmat();}// Codopname no. 093 opcode 0x5c cviewentfunction cviewent(): void{    if (Alis::$platform->version >= 60) {        near_v6(1);        return;    }    Alis::$fview = 1;    readexec_opername();    Alis::$valnorme = mul32(Alis::$varD7, Alis::$varD7);    readexec_opername();    Alis::$valchamp = s16(Alis::$varD7);    readexec_opername();    sviewent();}// Codopname no. 094 opcode 0x5d cviewtypfunction cviewtyp(): void{    if (Alis::$platform->version >= 60) {        near_v6(0);        return;    }    Alis::$fview = 1;    readexec_opername();    Alis::$valnorme = mul32(Alis::$varD7, Alis::$varD7);    readexec_opername();    Alis::$valchamp = s16(Alis::$varD7);    if (Alis::$platform->kind == C::EPlatformPC) {        Alis::$varD7 = script_read16();    } else {        readexec_opername();    }    sviewtyp();}// Codopname no. 095 opcode 0x5e cviewmatfunction cviewmat(): void{    if (Alis::$platform->version >= 60) {        near_v6(2);        return;    }    Alis::$fview = 1;    readexec_opername();    Alis::$valnorme = mul32(Alis::$varD7, Alis::$varD7);    readexec_opername();    Alis::$valchamp = s16(Alis::$varD7);    readexec_opername();    Alis::$matmask = s16(Alis::$varD7);    sviewmat();}// Codopname no. 096 opcode 0x5f corientfunction corient(): void{    if (Alis::$platform->version >= 60) {        return;    }    readexec_opername();    Alis::$varD7 = s32(Alis::$varD7 * 3);    $addr = get_0x20_set_vect(Alis::$script->vram_org) == 0 ? Alis::$vstandard + 1 : get_0x20_set_vect(Alis::$script->vram_org) + get_0x14_script_org_offset(Alis::$script->vram_org) + 1;    xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCX2(), xread8($addr + 0 + Alis::$varD7));    xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCY2(), xread8($addr + 1 + Alis::$varD7));    xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCZ2(), xread8($addr + 2 + Alis::$varD7));}// Codopname no. 098 opcode 0x61 csendfunction csend(): void{    $length = script_read8();    do {        readexec_opername();        $entry = s16(Alis::$varD7);        if ($entry == -1) {            break;        }        $vram = xread32(Alis::$atent + $entry);        if ($vram == 0) {            break;        }        if ((get_0x24_scan_inter($vram) & 1) != 0) {            break;        }        $limit = Alis::$platform->version >= 30 ? -0x3f : -0x35;        $offset = xread16(get_0x14_script_org_offset($vram) + 0x16);        $scanclr1 = get_0x1c_scan_clr($vram);        $scanclr2 = 0;        while (true) {            readexec_opername();            $scanclr2 = s16($scanclr1 + 2);            if ($limit < $scanclr2) {                $scanclr2 = s16($scanclr2 - $offset);            }            if ($scanclr2 == get_0x1e_scan_clr($vram)) {                $length--;                if (Alis::$platform->version == 10) {                    $scanclr2 = get_0x1c_scan_clr($vram);                }                break;            }            xwrite16($vram + $scanclr1, Alis::$varD7);            $scanclr1 = $scanclr2;            if ((--$length) == -1) {                set_0x1c_scan_clr($vram, $scanclr2);                set_0x24_scan_inter($vram, get_0x24_scan_inter($vram) | 0x80);                return;            }        }        set_0x1c_scan_clr($vram, $scanclr2);    } while (false);    while (($length--) != -1) {        readexec_opername();    }}// Codopname no. 103 opcode 0x66 cscanclrfunction cscanclr(): void{    set_0x1e_scan_clr(Alis::$script->vram_org, get_0x1c_scan_clr(Alis::$script->vram_org));    if (Alis::$platform->version != 10) {        set_0x24_scan_inter(Alis::$script->vram_org, get_0x24_scan_inter(Alis::$script->vram_org) & 0x7f);    }}// Codopname no. 099 opcode 0x62 cscanonfunction cscanon(): void{    set_0x24_scan_inter(Alis::$script->vram_org, get_0x24_scan_inter(Alis::$script->vram_org) & 0xfe);}// Codopname no. 100 opcode 0x63 cscanofffunction cscanoff(): void{    set_0x24_scan_inter(Alis::$script->vram_org, get_0x24_scan_inter(Alis::$script->vram_org) | 1);    if (Alis::$platform->version != 10) {        cscanclr();    }}// Codopname no. 101 opcode 0x64 cinteronfunction cinteron(): void{    set_0x24_scan_inter(Alis::$script->vram_org, get_0x24_scan_inter(Alis::$script->vram_org) & 0xfd);}// Codopname no. 102 opcode 0x65 cinterofffunction cinteroff(): void{    set_0x24_scan_inter(Alis::$script->vram_org, get_0x24_scan_inter(Alis::$script->vram_org) | 2);}// Codopname no. 104 opcode 0x67 callentityfunction callentity(): void{    Alis::$fallent = 1;}// Codopname no. 105 opcode 0x68 cpalettefunction cpalette(): void{    Alis::$flagmain = 0;    readexec_opername();    $palidx = s16(Alis::$varD7);    if (Alis::$platform->version == 10) {        // keeps the BIOS CGA palette        $paldata = adresdes($palidx);        if (Alis::$platform->kind != C::EPlatformPC && xread8($paldata) == 0xfe) {            topalette($paldata, 0);        }    } elseif (dos_pal_active()) {        $addr = $palidx < 0 ? 0 : adresdes($palidx);        dos_cpalette($palidx, $palidx < 0 ? 0 : s32($addr + xread32($addr)));   // 0 = NULL    } elseif ($palidx < 0) {        if (Alis::$platform->version < 60 || $palidx > -3) {            restorepal($palidx, 0);        }    } else {        $addr = adresdes($palidx);        $paldata = s32($addr + xread32($addr));        if (Alis::$platform->version >= 60 && xread8($paldata) != 3) {            return;        }        topalette($paldata, 0);    }}// Codopname no. 106 opcode 0x69 cdefcolorfunction cdefcolor(): void{    readexec_opername();    readexec_opername_saveD6();    // big-endian color word, as RGB9/RGB12 expect    $c0 = (Alis::$varD6 >> 8) & 0xff;    $c1 = Alis::$varD6 & 0xff;    $index = s16(Alis::$varD7);    if (dos_pal_active()) {        dos_cdefcolor($index, Alis::$varD6 & 0xffff);        return;    }    if (Alis::$platform->kind == C::EPlatformAmiga || Alis::$platform->kind == C::EPlatformAmigaAGA) {        // RGB12        $r = (($c0 & 0x0f) << 4) & 0xff;        $g = (($c1 >> 4) << 4) & 0xff;        $b = (($c1 & 0x0f) << 4) & 0xff;    } else {        // RGB9        $r = (($c0 & 0x07) << 5) & 0xff;        $g = (($c1 >> 4) << 5) & 0xff;        $b = (($c1 & 0x07) << 5) & 0xff;    }    // PAL_WRITE_RGB_AT(image.mpalet, index, ...)    Img::$mpalet[$index] = ($r << 16) | ($g << 8) | $b;    setmpalet();    set_update_cursor();}// Codopname no. 107 opcode 0x6a ctimingfunction ctiming(): void{    readexec_opername();    Alis::$ctiming = Alis::$varD7 & 0xff;}function ymsfx(int $type, int $vol, int $zap): void{    $pereson = get_0x0e_script_ent(Alis::$script->vram_org) & 0xff;    readexec_opername();    $priorson = Alis::$varD7 & 0xff;    readexec_opername();    $freqson = Alis::$varD7 & 0xffff;    readexec_opername();    $longson = Alis::$varD7 & 0xffff;    $dfreqson = 0;    if ($zap) {        readexec_opername();        $dfreqson = s16(Alis::$varD7);    }    if ($longson == 0) {        return;    }    $volson = s16(($vol & 0xff) << 8);    $dvolson = 0;    if ($dfreqson == 0) {        $dvolson = s16(intdiv($volson, $longson));        if ($dvolson == 0) {            $dvolson = 1;        }    }    runson($type, $pereson, $priorson, $volson, $freqson, $longson, s16(-$dvolson), $dfreqson);}// Codopname no. 108 opcode 0x6b czapfunction czap(): void{    if (psg_ops_dos_v6(5)) {        return;    }    if (Alis::$platform->version == 10) {        ymsfx(C::eChannelTypeDingZap, 0xf, 1);        return;    }    $pereson = get_0x0e_script_ent(Alis::$script->vram_org) & 0xff;    readexec_opername();    $priorson = Alis::$varD7 & 0xff;    readexec_opername();    $volson = s16(Alis::$varD7 << 8);    readexec_opername();    $freqson = Alis::$varD7 & 0xffff;    readexec_opername();    $longson = Alis::$varD7 & 0xffff;    if ($longson == 0) {        return;    }    readexec_opername();    $dfreqson = Alis::$varD7 & 0xffff;    runson(C::eChannelTypeDingZap, $pereson, $priorson, $volson, $freqson, $longson, 0, s16($dfreqson));}// Codopname no. 109 opcode 0x6c cexplodefunction cexplode(): void{    if (psg_ops_dos_v6(4)) {        return;    }    if (Alis::$platform->version == 10) {        ymsfx(C::eChannelTypeExplode, 0xf, 0);        return;    }    $pereson = get_0x0e_script_ent(Alis::$script->vram_org) & 0xff;    readexec_opername();    $priorson = Alis::$varD7 & 0xff;    readexec_opername();    $volson = s16(Alis::$varD7 << 8);    readexec_opername();    $freqson = Alis::$varD7 & 0xffff;    readexec_opername();    $longson = Alis::$varD7 & 0xffff;    if ($longson == 0) {        return;    }    $dvolson = udiv32($volson, $longson);    if ($dvolson == 0) {        $dvolson = 1;    }    runson(C::eChannelTypeExplode, $pereson, $priorson, $volson, $freqson, $longson, s16(-s16($dvolson)), 0);}// Codopname no. 110 opcode 0x6d cdingfunction cding(): void{    if (psg_ops_dos_v6(4)) {        return;    }    if (is_rrq_dos_v3()) {        // DOS v3: the player (0x922f) is a bare RET, the operands are read and nothing is played        for ($i = 0; $i < 4; $i++) {            readexec_opername();        }        return;    }    if (Alis::$platform->version == 10) {        ymsfx(C::eChannelTypeDingZap, 0xf, 0);        return;    }    $pereson = get_0x0e_script_ent(Alis::$script->vram_org) & 0xff;    readexec_opername();    $priorson = Alis::$varD7 & 0xff;    readexec_opername();    $volson = s16(Alis::$varD7 << 8);    readexec_opername();    $freqson = (Alis::$varD7 >> 1) & 0xffff;    readexec_opername();    $longson = Alis::$varD7 & 0xffff;    if ($longson == 0) {        return;    }    $dvolson = udiv32($volson, $longson);    if ($dvolson == 0) {        $dvolson = 1;    }    runson(C::eChannelTypeDingZap, $pereson, $priorson, $volson, $freqson, $longson, s16(-s16($dvolson)), 0);}// Codopname no. 111 opcode 0x6e cnoisefunction cnoise(): void{    if (psg_ops_dos_v6(4)) {        return;    }    if (Alis::$platform->version == 10) {        readexec_opername();        readexec_opername();        readexec_opername();        return;    }    $pereson = get_0x0e_script_ent(Alis::$script->vram_org) & 0xff;    readexec_opername();    $priorson = Alis::$varD7 & 0xff;    readexec_opername();    $volson = s16(Alis::$varD7 << 8);    readexec_opername();    $freqson = Alis::$varD7 & 0xffff;    readexec_opername();    $longson = Alis::$varD7 & 0xffff;    if ($longson == 0) {        return;    }    $dvolson = udiv32($volson, $longson);    if ($dvolson == 0) {        $dvolson = 1;    }    runson(C::eChannelTypeNoise, $pereson, $priorson, $volson, $freqson, $longson, s16(-s16($dvolson)), 0);}// Codopname no. 112 opcode 0x6f cinitabfunction cinitab(): void{    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        $off = s16(script_read16());        script_read16();        xwrite8(Alis::$script->vram_org + $off, script_read8());        return;    }    if (Alis::$platform->version >= 60) {        $off = s16(script_read16());        $count = s16(script_read16());        for ($i = 0; $i < $count - 1; $i++) {            xwrite8(Alis::$script->vram_org + $off + $i, script_read8());        }        return;    }    if (Alis::$platform->version == 10) {        // copy inline bytes into the script variables (DOS copies one byte less)        $offset = s16(script_read16());        $length = script_read16();        if (Alis::$platform->kind == C::EPlatformPC && $length) {            $length--;        }        for ($i = 0; $i < $length; $i++) {            xwrite8(Alis::$script->vram_org + $offset + $i, script_read8());        }        return;    }    // MISSING: cinitab}// Codopname no. 113 opcode 0x70 cfopenfunction cfopen(): void{    $path = Alis::$platform->path;    if (ord(Alis::$mem[Alis::$script->pc]) == 0xff) {        Alis::$script->pc += 1;        readexec_opername_swap();        readexec_opername();        $path .= mem_str(Alis::$sd7);        $mode = Alis::$varD7 & 0xffff;    } else {        // script_read_until_zero(path + strlen(alis.platform.path))        while (($c = script_read8()) != 0) {            $path .= chr($c);        }        $mode = script_read16();    }    afopen($path, $mode);    if (Alis::$fp === null || Alis::$fp === false) {        alis_error(C::ALIS_ERR_FOPEN, $path);    }}// Codopname no. 114 opcode 0x71 cfclosefunction cfclose(): void{    if (sys_fclose(Alis::$fp) < 0) {        alis_error(C::ALIS_ERR_FCLOSE);    }    Alis::$fp = null;}// Codopname no. 115 opcode 0x72 cfcreatfunction cfcreat(): void{    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        $name = '';        while (($c = script_read8()) != 0) {            $name .= chr($c);        }        script_read16();        $f = sys_fopen(Alis::$platform->path . $name, 'ab');        if ($f) {            sys_fclose($f);        }        return;    }    if (Alis::$platform->version >= 60) {        Alis::$script->pc++;        readexec_opername_swap();        readexec_opername();        $f = sys_fopen(Alis::$platform->path . mem_str(Alis::$sd7), 'wb');        if ($f) {            sys_fclose($f);        }        return;    }    if (Alis::$platform->version == 10) {        // name, then a word        while (script_read8() != 0) {        }        script_read16();        return;    }    // MISSING: cfcreat}// Codopname no. 116 opcode 0x73 cfdelfunction cfdel(): void{    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        if (xread8(Alis::$script->pc) == 0xff) {            Alis::$script->pc++;            readexec_opername_swap();            $name = mem_str(Alis::$sd7);        } else {            $name = '';            while (($c = script_read8()) != 0) {                $name .= chr($c);            }        }        sys_fdelete(Alis::$platform->path . $name);        return;    }    if (Alis::$platform->version >= 60) {        Alis::$script->pc++;        readexec_opername_swap();        sys_fdelete(Alis::$platform->path . mem_str(Alis::$sd7));        return;    }    if (Alis::$platform->version == 10) {        // name        while (script_read8() != 0) {        }        return;    }    // MISSING: cfdel}// Codopname no. 117 opcode 0x74 cfreadvfunction cfreadv(): void{    $b = fread(Alis::$fp, 2);    $b = str_pad($b === false ? '' : $b, 2, "\0");    // xswap16be(*(s16 *)alis.buffer): the 2 bytes are big endian    Alis::$buffer[0] = ord($b[0]);    Alis::$buffer[1] = ord($b[1]);    Alis::$varD7 = s16((ord($b[0]) << 8) | ord($b[1]));    cstore_continue();}// Codopname no. 118 opcode 0x75 cfwritevfunction cfwritev(): void{    readexec_opername();    if (Alis::$platform->version >= 60) {        $v = Alis::$varD7;        fwrite(Alis::$fp, chr(($v >> 24) & 0xff) . chr(($v >> 16) & 0xff) . chr(($v >> 8) & 0xff) . chr($v & 0xff));        return;    }    // val = xswap16be(varD7) stored host order: the file gets the big-endian word    $v = Alis::$varD7 & 0xffff;    fwrite(Alis::$fp, chr($v >> 8) . chr($v & 0xff));}// Codopname no. 119 opcode 0x76 cfwriteifunction cfwritei(): void{    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        // cfwritei 3DO: name ignored (00034b28 not ported)        while (script_read8() != 0) {        }        return;    }    if (Alis::$platform->version >= 60) {        $str = Alis::$script->pc;        $len = mem_strlen($str) + 1;        Alis::$script->pc += $len;        if (Alis::$fp) {            fwrite(Alis::$fp, mem_read($str, $len));        }        return;    }    if (Alis::$platform->version == 10) {        // name        while (script_read8() != 0) {        }        return;    }    // MISSING: cfwritei}function unmixword(int $tgt_addr, int $tgt_len): void{    $tmp_offset_lsr4 = 0;    $bufpackend = shr32($tgt_len, 5);    $len = (shr32($tgt_len, 5) + 4) & 0xffff;    $tmp_buffer = array_fill(0, max($len, 1), 0);    $half_longpack = shr32($tgt_len, 1);    $tmp_offset2 = 1;    $tmp_buffer[0] = 1;    do {        $tmp_byte = xread8($tgt_addr + $tmp_offset2);        $tmp_offset = $tmp_offset2;        while (true) {            $tmp_xchg = $tmp_byte;            if ($half_longpack <= $tmp_offset) {                do {                    $tmp_offset = ($tmp_offset - $half_longpack) * 2 + 1;                    $tmp_xchg = xread8($tgt_addr + $tmp_offset);                    xwrite8($tgt_addr + $tmp_offset, $tmp_byte);                    $tmp_byte = $tmp_xchg;                } while ($half_longpack <= $tmp_offset);                $tmp_offset_lsr4 = $tmp_offset >> 4;                $tmp_buffer[$tmp_offset_lsr4] = (($tmp_buffer[$tmp_offset_lsr4] ?? 0) | (1 << ((($tmp_offset >> 1) & 0xffff) & 7))) & 0xff;                if ($tmp_offset == $tmp_offset2) {                    break;                }            }            $tmp_offset *= 2;            $tmp_byte = xread8($tgt_addr + $tmp_offset);            xwrite8($tgt_addr + $tmp_offset, $tmp_xchg);        }        while (($tmp_buffer[$tmp_offset_lsr4] ?? 0) == 0xff) {            $tmp_offset_lsr4++;            if ($bufpackend <= $tmp_offset_lsr4) {                return;            }        }        $tgt_byte = $tmp_buffer[$tmp_offset_lsr4] ?? 0;        $tmp_offset_tune = -1;        do {            $tmp_offset_tune++;            $bitcheck = $tgt_byte & 1;            $tgt_byte >>= 1;        } while ($bitcheck != 0);        $tmp_offset2 = ($tmp_offset_tune + $tmp_offset_lsr4 * 8) * 2 + 1;    } while ($tmp_offset2 < $half_longpack);}// s16 offset from vram; 0 means "next s16 is from basemain" (DOS 000068b2, 3DO 0001e4d8)function read_vaddr(): int{    $base = Alis::$script->vram_org;    $offset = s16(script_read16());    if ($offset == 0) {        $offset = s16(script_read16());        $base = Alis::$basemain;    }    return $base + $offset;}// .fic data is big-endian: on PC swap u16 cells (flag 2)function fic_cellsize(int $cell): int{    if (Alis::$platform->kind != C::EPlatformPC) {        return 0;    }    $flag = xread8($cell - 2);    if (Alis::$platform->version >= 60) {        return $flag & 0xf;    }    return $flag == 2 ? 2 : 0;}// swaps the bytes of each size-byte cell of the memory block at pfunction fic_swap(int $p, int $length, int $size): void{    if ($size != 2 && $size != 4) {        return;    }    for ($i = 0; $i + $size <= $length; $i += $size) {        for ($l = 0, $r = $size - 1; $l < $r; $l++, $r--) {            $t = Alis::$mem[$p + $i + $l];            Alis::$mem[$p + $i + $l] = Alis::$mem[$p + $i + $r];            Alis::$mem[$p + $i + $r] = $t;        }    }}// Codopname no. 120 opcode 0x77 cfreadbfunction cfreadb(): void{    $cell = read_vaddr();    if (Alis::$platform->version >= 30 && Alis::$platform->version < 60) {        // (the original returns before reading the length when the table is empty: its bytes then run as code)        $addr = cfreadb_addr($cell);        if ($addr !== null) {            cfreadb_at($cell, $addr, script_read32());        }        return;    }    $addr = $cell;    $length = 0;    if (Alis::$platform->version >= 30) {        if (Alis::$platform->version >= 60) {            if (($addr = v6_tabdata($cell)) == 0) {                return;            }        } elseif (s8(xread8($addr - 1)) < 0) {            $addr = xread32($addr);            $addr = xread32($addr);            if ($addr < 1) {                return;            }        }        $length = script_read32();        if (Alis::$typepack == 0) {            $data = $length > 0 ? fread(Alis::$fp, $length) : '';            mem_write($addr, $data === false ? '' : $data);        } else {            $unpacked_buffer = $addr;            unpack_script_fp(Alis::$fp, $unpacked_buffer, $length);            if (Alis::$wordpack != 0) {                unmixword($addr, $length);            }        }    } else {        $length = script_read16();        $data = $length > 0 ? fread(Alis::$fp, $length) : '';        mem_write($addr, $data === false ? '' : $data);    }    fic_swap($addr, $length, fic_cellsize($cell));}/** cfreadb() of versions 30 .. 59: where to read into, null when the original returns before reading the length */function cfreadb_addr(int $cell): ?int{    $addr = $cell;    if (s8(xread8($addr - 1)) < 0) {        $addr = xread32($addr);        $addr = xread32($addr);        if ($addr < 1) {            return null;        }    }    return $addr;}/** cfreadb() of versions 30 .. 59 once its operands are read (the compiled scripts call it with them as constants) */function cfreadb_at(int $cell, int $addr, int $length): void{    if (Alis::$typepack == 0) {        $data = $length > 0 ? fread(Alis::$fp, $length) : '';        mem_write($addr, $data === false ? '' : $data);    } else {        $unpacked_buffer = $addr;        unpack_script_fp(Alis::$fp, $unpacked_buffer, $length);        if (Alis::$wordpack != 0) {            unmixword($addr, $length);        }    }    fic_swap($addr, $length, fic_cellsize($cell));}/** cfwriteb() of versions 30 .. 59 once its operands are read */function cfwriteb_at(int $cell, int $length): void{    if (-1 < s8(xread8($cell - 1))) {        writeswap($cell, $length, $cell);    } else {        $addr1 = xread32($cell);        $addr2 = xread32($addr1);        if ($addr2 != 0) {   // 0 < (u32)addr2            writeswap($addr2, $length, $cell);        }    }}function writeswap(int $addr, int $length, int $cell): void{    $size = fic_cellsize($cell);    if ($size == 2 || $size == 4) {        // the C swaps a malloc'ed copy: swap in place, write, swap back        fic_swap($addr, $length, $size);        fwrite(Alis::$fp, mem_read($addr, $length));        fic_swap($addr, $length, $size);        return;    }    fwrite(Alis::$fp, mem_read($addr, $length));}// Codopname no. 121 opcode 0x78 cfwritebfunction cfwriteb(): void{    $cell = read_vaddr();    if (Alis::$platform->version >= 60) {        $data = v6_tabdata($cell);        if ($data == 0) {            return;        }        $length = script_read32();        writeswap($data, $length, $cell);    } elseif (Alis::$platform->version >= 30) {        $length = script_read32();        if (-1 < s8(xread8($cell - 1))) {            writeswap($cell, $length, $cell);        } else {            $addr1 = xread32($cell);            $addr2 = xread32($addr1);            if ($addr2 != 0) {   // 0 < (u32)addr2                writeswap($addr2, $length, $cell);            }        }    } else {        $length = script_read16();        writeswap($cell, $length, $cell);    }}function SYS_PutPixel(): void{    V::$adptsin[0] = Alis::$poldx;    V::$adptsin[1] = Alis::$poldy;    draw_pixel(Alis::$poldx, Alis::$poldy);}function pen_ops_nodraw(): int{    return (int) (Alis::$platform->version >= 60 || (Alis::$platform->kind == C::EPlatformPC && Alis::$platform->version >= 30));}// Codopname no. 122 opcode 0x79 cplotfunction cplot(): void{    if (pen_ops_nodraw()) {        readexec_opername();        readexec_opername();        return;    }    readexec_opername();    readexec_opername_saveD6();    Alis::$poldx = s16(Alis::$varD7);    Alis::$poldy = s16(Alis::$varD6);    SYS_PutPixel();}// Codopname no. 123 opcode 0x7a cdrawfunction cdraw(): void{    readexec_opername();    readexec_opername_saveD6();    if (pen_ops_nodraw()) {        return;    }    $oldx = Alis::$poldx;    $oldy = Alis::$poldy;    Alis::$poldx = s16(Alis::$poldx + Alis::$varD7);    Alis::$poldy = s16(Alis::$poldy + Alis::$varD6);    draw_line($oldx, $oldy, Alis::$poldx, Alis::$poldy);}// Codopname no. 124 opcode 0x7b cboxfunction cbox(): void{    readexec_opername();    readexec_opername_saveD6();    if (pen_ops_nodraw()) {        return;    }    $oldx = Alis::$poldx;    $oldy = Alis::$poldy;    Alis::$poldx = s16(Alis::$poldx + Alis::$varD7);    Alis::$poldy = s16(Alis::$poldy + Alis::$varD6);    draw_box($oldx, $oldy, Alis::$poldx, Alis::$poldy);}// Codopname no. 125 opcode 0x7c cboxffunction cboxf(): void{    readexec_opername();    readexec_opername_saveD6();    if (pen_ops_nodraw()) {        return;    }    $oldx = Alis::$poldx;    $oldy = Alis::$poldy;    Alis::$poldx = s16(Alis::$poldx + Alis::$varD7);    Alis::$poldy = s16(Alis::$poldy + Alis::$varD6);    draw_boxf($oldx, $oldy, Alis::$poldx, Alis::$poldy);}// Codopname no. 126 opcode 0x7d cinkfunction cink(): void{    readexec_opername();    Img::$inkcolor = Alis::$varD7 & 0xffff;}// Codopname no. 127 opcode 0x7e cpsetfunction cpset(): void{    if (Alis::$platform->version >= 60) {        readexec_opername();        readexec_opername();        return;    }    // pre-v6 pen position    readexec_opername();    Alis::$poldx = s16(Alis::$varD7);    readexec_opername();    Alis::$poldy = s16(Alis::$varD7);}// Codopname no. 128 opcode 0x7f cpmovefunction cpmove(): void{    if (Alis::$platform->version >= 60) {        readexec_opername();        readexec_opername();        return;    }    // pre-v6 pen position    readexec_opername();    Alis::$poldx = s16(Alis::$poldx + Alis::$varD7);    readexec_opername();    Alis::$poldy = s16(Alis::$poldy + Alis::$varD7);}// Codopname no. 129 opcode 0x80 cpmodefunction cpmode(): void{    readexec_opername();    Img::$line_a_mode = Alis::$varD7 & 0xffff;}// Codopname no. 130 opcode 0x81 cpicturefunction cpicture(): void{    if (Alis::$platform->version == 10) {        Alis::$flagmain = 0;        readexec_opername();        Img::$depx = 0;        Img::$depy = 0;        Img::$depz = 0;        Img::$invert_x = 0;        picture_v10(Alis::$varD7);        return;    }    // MISSING: cpicture}// Codopname no. 131 opcode 0x82 cxyscrollfunction cxyscroll(): void{    readexec_opername();    readexec_opername();}// Codopname no. 132 opcode 0x83 clinkingfunction clinking(): void{    readexec_opername();    if (Alis::$platform->version >= 60) {        xwrite16(V6_CLINKING(Alis::$script->vram_org), Alis::$varD7);    } else {        set_0x2a_clinking(Alis::$script->vram_org, Alis::$varD7);    }}// Codopname no. 133 opcode 0x84 cmousonfunction cmouson(): void{    sys_enable_mouse(1);}// Codopname no. 134 opcode 0x85 cmousofffunction cmousoff(): void{    sys_enable_mouse(0);}// Codopname no. 135 opcode 0x86 cmouse// 0x86 - 14d62function cmouse(): void{    // Consume (not just read) so the sticky lb_clicked/rb_clicked latches are cleared.    $mouse = sys_consume_mouse();    $mx = $mouse->x;    $my = $mouse->y;    if (sys_is_dos_v3()) {        // 0x2876: raw position + the csetmouse offset, clamped to the screen        $mx = max(0, min(Alis::$platform->width - 1, $mx + SysVars::$mouseOffX));        $my = max(0, min(Alis::$platform->height - 1, $my + SysVars::$mouseOffY));    }    if (Alis::$platform->kind == C::EPlatformMac) {        $mx = (int) ($mx / 1.5);        $my = (int) ($my / 1.5);    }    Alis::$varD7 = $mx;    cstore_continue();    Alis::$varD7 = $my;    cstore_continue();    // Quick clicks (down+up landed between two script polls) show up    // here through the *_clicked latches consumed above.    $lb_seen = $mouse->lb || $mouse->lb_clicked;    $rb_seen = $mouse->rb || $mouse->rb_clicked;    if (sys_is_dos_v3()) {        // 0x280d: the driver's button bits & 3 (both buttons held = 3)        Alis::$varD7 = ($lb_seen ? 1 : 0) | ($rb_seen ? 2 : 0);    } else {        Alis::$varD7 = $lb_seen ? 1 : ($rb_seen ? 2 : 0);    }    cstore_continue();}// Codopname no. 136 opcode 0x87 cdefmousefunction cdefmouse(): void{    Alis::$flagmain = 0;    readexec_opername();    cdefmouse_core();}/** cdefmouse() once its operand is in D7 */function cdefmouse_core(): void{    if (Alis::$platform->version >= 60) {        $addr = adresdes(Alis::$varD7);        if ($addr != 0xf) {            $desc = s32($addr + xread32($addr));            Alis::$desmouse = $desc;            v6x_defmouse($desc);        }        return;    }    if (Alis::$platform->kind != C::EPlatformMac) {        $addr = adresdes(Alis::$varD7);        $ptr = s32($addr + xread32($addr));        $b = xread8($ptr);        if ($b == 0 || $b == 0x10 || $b == 0x14) {            Alis::$desmouse = $ptr;            set_update_cursor();        }    }}// Codopname no. 137 opcode 0x88 csetmousefunction csetmouse(): void{    readexec_opername();    $x = Alis::$varD7 & 0xffff;    if (Alis::$platform->version == 10 && Alis::$platform->kind == C::EPlatformPC) {        return;    }    if (sys_is_dos_v3()) {        // 0x9a22: the second expression is y (0x9d17), then 0x28f1 sets an offset cmouse adds        readexec_opername();        sys_set_mouse_offset($x, Alis::$varD7 & 0xffff);        return;    }    readexec_opername();    $y = Alis::$varD6 & 0xffff;   // (sic) the C reads varD6    sys_set_mouse($x, $y);}// Codopname no. 138 opcode 0x89 cdefvectfunction cdefvect(): void{    $offset = script_read8();    $offset += $offset * 2;    $offset += 3;    Alis::$script->pc += $offset;}// Codopname no. 139 opcode 0x8a csetvectfunction csetvect(): void{    $value = s16(script_read16());    set_0x20_set_vect(Alis::$script->vram_org, $value);}// returns the address (in Alis::$mem) of the first vector after the count bytefunction deb_approach(int $offset, int &$wcx, int &$wcy, int &$wcz): int{    $offset = xread32(Alis::$atent + $offset);    $wcx = s16(xread16($offset + C::ALIS_SCR_WCX) - xread16(Alis::$script->vram_org + C::ALIS_SCR_WCX));    $wcy = s16(xread16($offset + K::ALIS_SCR_WCY()) - xread16(Alis::$script->vram_org + K::ALIS_SCR_WCY()));    $wcz = s16(xread16($offset + K::ALIS_SCR_WCZ()) - xread16(Alis::$script->vram_org + K::ALIS_SCR_WCZ()));    $address = get_0x20_set_vect(Alis::$script->vram_org) == 0 ? Alis::$vstandard : get_0x20_set_vect(Alis::$script->vram_org) + get_0x14_script_org_offset(Alis::$script->vram_org);    Alis::$varD7 = xread8($address);    xwrite8(Alis::$script->vram_org + K::ALIS_SCR_ADDR(), xread8($address));    return $address + 1;}// address: s8[3] vector in Alis::$memfunction calscal(int $address, int $wcx, int $wcy, int $wcz): int{    $result = 0;    $a0 = s8(xread8($address));    $a1 = s8(xread8($address + 1));    $a2 = s8(xread8($address + 2));    if ($a0 != 0) {        $result += $a0 * $wcx;    }    if ($a1 != 0) {        $result += $a1 * $wcy;    }    if ($a2 != 0) {        $result += $a2 * $wcz;    }    return s32($result);}// Codopname no. 141 opcode 0x8c capproachfunction capproach(): void{    if (Alis::$platform->version >= 60) {        return;    }    readexec_opername();    if (Alis::$varD7 != -1) {        $wcx = 0;        $wcy = 0;        $wcz = 0;        $addr0 = deb_approach(Alis::$varD7, $wcx, $wcy, $wcz);        $addr1 = 0;        $limit = -0x7fffffff - 1;   // 0x80000000        $index = s8(Alis::$varD6);        do {            $test = calscal($addr0, $wcx, $wcy, $wcz);            if ($limit < $test) {                $index = s8(Alis::$varD7);                $limit = $test;                $addr1 = $addr0;            }            $addr0 += 3;            Alis::$varD7 = s32(Alis::$varD7 - 1);        } while (Alis::$varD7 != -1);        xwrite8(Alis::$script->vram_org + K::ALIS_SCR_ADDR(), xread8(Alis::$script->vram_org + K::ALIS_SCR_ADDR()) - $index);        xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCX2(), xread8($addr1 + 0));        xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCY2(), xread8($addr1 + 1));        xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCZ2(), xread8($addr1 + 2));    }}// Codopname no. 142 opcode 0x8d cescapefunction cescape(): void{    if (Alis::$platform->version >= 60) {        return;    }    readexec_opername();    if (Alis::$varD7 != -1) {        $wcx = 0;        $wcy = 0;        $wcz = 0;        $addr0 = deb_approach(Alis::$varD7, $wcx, $wcy, $wcz);        $addr1 = 0;        $limit = 0x7fffffff;        $index = s8(Alis::$varD6);        do {            $test = s16(calscal($addr0, $wcx, $wcy, $wcz));            if ($test < $limit) {                $index = s8(Alis::$varD7);                $limit = $test;                $addr1 = $addr0;            }            $addr0 += 3;            Alis::$varD7 = s32(Alis::$varD7 - 1);        } while (Alis::$varD7 != -1);        xwrite8(Alis::$script->vram_org + K::ALIS_SCR_ADDR(), xread8(Alis::$script->vram_org + K::ALIS_SCR_ADDR()) - $index);        xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCX2(), xread8($addr1 + 0));        xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCY2(), xread8($addr1 + 1));        xwrite8(Alis::$script->vram_org + K::ALIS_SCR_WCZ2(), xread8($addr1 + 2));    }}// Codopname no. 143 opcode 0x8e cvtstmovfunction cvtstmov(): void{    Alis::$wcx = s16(s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCX2())) + xread16(Alis::$script->vram_org + C::ALIS_SCR_WCX));    Alis::$wcy = s16(s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCY2())) + xread16(Alis::$script->vram_org + K::ALIS_SCR_WCY()));    Alis::$wcz = s16(s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCZ2())) + xread16(Alis::$script->vram_org + K::ALIS_SCR_WCZ()));    readexec_opername();    Alis::$matmask = s16(Alis::$varD7);    Alis::$wforme = get_0x1a_cforme(Alis::$script->vram_org);    clipform();    crstent();}// Codopname no. 144 opcode 0x8f cvftstmovfunction cvftstmov(): void{    Alis::$wcx = s16(s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCX2())) + xread16(Alis::$script->vram_org + C::ALIS_SCR_WCX));    Alis::$wcy = s16(s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCY2())) + xread16(Alis::$script->vram_org + K::ALIS_SCR_WCY()));    Alis::$wcz = s16(s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCZ2())) + xread16(Alis::$script->vram_org + K::ALIS_SCR_WCZ()));    readexec_opername();    Alis::$matmask = s16(Alis::$varD7);    readexec_opername();    Alis::$wforme = s16(Alis::$varD7);    clipform();    crstent();}// Codopname no. 145 opcode 0x90 cvmovfunction cvmov(): void{    if (Alis::$platform->version >= 60) {        $v = Alis::$script->vram_org;        xwrite32($v + 0x0c, s32(xread32($v + 0x0c) + s16(xread16($v + 0x26))));        xwrite32($v + 0x10, s32(xread32($v + 0x10) + s16(xread16($v + 0x28))));        xwrite32($v + 0x14, s32(xread32($v + 0x14) + s16(xread16($v + 0x2a))));        xwrite8($v + 0x45, 1);        return;    }    xadd16(Alis::$script->vram_org + C::ALIS_SCR_WCX, s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCX2())));    xadd16(Alis::$script->vram_org + K::ALIS_SCR_WCY(), s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCY2())));    xadd16(Alis::$script->vram_org + K::ALIS_SCR_WCZ(), s8(xread8(Alis::$script->vram_org + K::ALIS_SCR_WCZ2())));}// Codopname no. 146 opcode 0x91 cdefworldfunction cdefworld(): void{    $offset = s16(script_read16());    if (Alis::$platform->version >= 60) {        $base = Alis::$script->vram_org + ($offset & 0xffff);        set_0x16_screen_id(Alis::$script->vram_org, $offset & 0xffff);        // insert into the global world list + initialise the struct fields        xwrite32($base + 0xb4, OpcodesV6Vars::$defworld_list_head);        OpcodesV6Vars::$defworld_list_head = $base;        xwrite32($base + 0xb0, 0);        xwrite32($base + 0x38, $base + 0x58);        xwrite32($base + 0x34, 0);        xwrite8($base + 0x22, 0);        xwrite32($base + 0x8c, $base);        xwrite32($base + 0x90, $base + 0x58);        xwrite8($base + 0x7a, 0);        // TODO: verify destination base + 0x58 + offset against a non-empty world        $count = script_read16() & 0xffff;        $dst = $base + 0x58 + ($offset & 0xffff);        while ($count--) {            xwrite16($dst, script_read16());            $dst += 2;        }        return;    }    $counter = Alis::$platform->version == 10 && Alis::$platform->kind == C::EPlatformPC ? 5 : 6;    while ($counter--) {        xwrite8(Alis::$script->vram_org + $offset, script_read8());    }}// Codopname no. 147 opcode 0x92 cworldfunction cworld(): void{    if (Alis::$platform->version >= 60) {        $world = script_read16() & 0xffff;        set_0x16_screen_id(Alis::$script->vram_org, $world);        return;    }    // (u32 vram_org + 0xffde: a forward offset in the C, kept as is)    xwrite8(Alis::$script->vram_org + 0xffde, script_read8());    xwrite8(Alis::$script->vram_org + 0xffdf, script_read8());}// Codopname no. 148 opcode 0x93 cfindmatfunction cfindmat(): void{    if (Alis::$platform->version == 10) {        readexec_opername();        Alis::$matmask = s16(Alis::$varD7);        findent(0, 0);        return;    }    // MISSING: cfindmat}// Codopname no. 149 opcode 0x94 cfindtypfunction cfindtyp(): void{    if (Alis::$platform->version >= 60) {        readexec_opername_swap();        $name = str_pad(substr(mem_str(Alis::$sd7), 0, 8), 8, "\0"); // strncpy(name, alis.sd7, 8)        $tabidx = 0;        $entidx = s16(alis_first_ent());        do {            $tgt_vram = xread32(Alis::$atent + $entidx);            if ($tgt_vram != 0 && $tgt_vram != Alis::$script->vram_org && mem_read($tgt_vram + 0x3c, 8) === $name) {                if (tabent_full($tabidx)) {                    break;                }                Alis::$matent[$tabidx] = 0;                Alis::$tablent[$tabidx] = $entidx;                $tabidx++;                if (Alis::$fallent == 0) {                    break;                }            }            $entidx = xread16(Alis::$atent + 4 + $entidx);        } while ($entidx != 0);        Alis::$tablent[$tabidx] = ENTLIST_END();        Alis::$fallent = 0;        crstent();        return;    }    if (Alis::$platform->kind == C::EPlatformPC) {        Alis::$varD7 = script_read16();    } else {        readexec_opername();    }    if (Alis::$platform->version == 10 && Alis::$platform->kind != C::EPlatformPC) {        findent(1, 0);        return;    }    if (Alis::$varD7 < 0) {        Alis::$varD7 &= 0xff;    }    $id = s16(Alis::$varD7);    $entidx = s16(alis_first_ent());    $tabidx = 0;    do {        $tgt_vram = xread32(Alis::$atent + $entidx);        if ($id == get_0x10_script_id($tgt_vram) && Alis::$script->vram_org != $tgt_vram) {            if (tabent_full($tabidx)) {                break;            }            Alis::$matent[$tabidx] = 0;            Alis::$tablent[$tabidx] = $entidx;            $tabidx++;            if (Alis::$fallent == 0) {                break;            }        }        $entidx = xread16(Alis::$atent + 4 + $entidx);    } while ($entidx != 0);    Alis::$tablent[$tabidx] = ENTLIST_END();    Alis::$fallent = 0;    crstent();}function music(): void{    if (Alis::$platform->version <= 10) {        readexec_opername();        $idx = s16(Alis::$varD7);        $addr = ($idx < 0 || Alis::$platform->kind == C::EPlatformPC) ? 0 : adresmus($idx);        if ($idx < 0) {            mv0_offmusic();        }        $op = [0, 0, 0, 0];        for ($i = 0; $i < 4; $i++) {            readexec_opername();            $op[$i] = s16(Alis::$varD7);        }        if ($addr && xread8($addr) == 0) {            mv0_gomusic($addr, $op[0], $op[1], $op[2], $op[3]);        }        return;    }    $scriptId = Alis::$flagmain ? Alis::$main->data->header->id : Alis::$script->data->header->id;    Aud::$musicId = $scriptId & 0xffff;    readexec_opername();    $idx = s16(Alis::$varD7);    $addr = adresmus($idx);    $type = xread8($addr);    if ($type == 0 || $type == 3) {        Aud::$muvolume = 0;        Aud::$mupnote = $addr + 6;        readexec_opername();        Aud::$maxvolume = (Alis::$varD7 << 8) & 0xffff;        readexec_opername();        Aud::$mutempo = Alis::$varD7 & 0xff;        readexec_opername();        Aud::$muattac = (Alis::$varD7 + 1) & 0xffff;        readexec_opername();        Aud::$muduree = s16(Alis::$varD7 + 1);        readexec_opername();        Aud::$muchute = (Alis::$varD7 + 2) & 0xffff;        if (Aud::$muattac != 0) {            Aud::$dattac = intdiv(Aud::$maxvolume, Aud::$muattac);        }        if (Aud::$muchute != 0) {            Aud::$dchute = intdiv(Aud::$maxvolume, Aud::$muchute);        }        $type = xread8(Aud::$mupnote - 6);        if ($type == 0) {            mv1_gomusic();        }        // else: STUBBED        Aud::$mustate = 1;    } elseif ($type == 4) {        Aud::$muvolume = 0;        Aud::$mupnote = $addr + 6;        readexec_opername();        Aud::$maxvolume = (Alis::$varD7 << 8) & 0xffff;        readexec_opername();        Aud::$mutempo = Alis::$varD7 & 0xff;        readexec_opername();        Aud::$muattac = (Alis::$varD7 + 1) & 0xffff;        readexec_opername();        Aud::$muduree = s16(Alis::$varD7);        readexec_opername();        Aud::$muchute = (Alis::$varD7 + 1) & 0xffff;        Aud::$dattac = 0;        Aud::$dchute = 0;        if (Aud::$muattac != 0) {            Aud::$dattac = intdiv(Aud::$maxvolume, Aud::$muattac);        }        if (Aud::$muchute != 0) {            Aud::$dchute = intdiv(Aud::$maxvolume, Aud::$muchute);        }        Aud::$muvolume = (Aud::$maxvolume >> 8) & 0xff;        if (Aud::$fmusic) {            mv2_gomusic();        }        Aud::$mustate = 1;    } else {        readexec_opername();        readexec_opername();        readexec_opername();        readexec_opername();        readexec_opername();        // STUBBED    }}// Codopname no. 150 opcode 0x95 cmusicfunction cmusic(): void{    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        for ($i = 0; $i < 6; $i++) {            readexec_opername();        }        return;    }    Alis::$flagmain = 0;    music();}// Codopname no. 151 opcode 0x96 cdelmusicfunction cdelmusic(): void{    if (Alis::$platform->version <= 10) {        mv0_offmusic();        return;    }    readexec_opername();    // Stop the engine that plays: version-21 games (Boston Bomb Club, Bunny Bricks) still use MV1.    // PORT: C compares the function pointer audio.soundrout with mv1_soundrout; Aud has no soundrout    // property yet. Assumed here: Aud::$soundrout holds the callable name string 'App\Alis\mv1_soundrout'.    if ((Aud::$soundrout ?? null) === __NAMESPACE__ . '\\mv1_soundrout') {        mv1_offmusic(Alis::$varD7);    } else {        mv2_offmusic(Alis::$varD7);    }}// Codopname no. 152 opcode 0x97 ccadencefunction ccadence(): void{    readexec_opername();    if (Alis::$platform->version == 10) {        mv0_cadence(Alis::$varD7);        return;    }    if (Alis::$varD7 == 0) {        Alis::$varD7 = 1;    }    Aud::$mutempo = Alis::$varD7 & 0xff;    Aud::$mutemp = intdiv(Aud::$mutempo * 6, 0x20) & 0xff;}// Codopname no. 153 opcode 0x98 csetvolumfunction csetvolum(): void{    readexec_opername();    if (Alis::$platform->version == 10) {        mv0_volume(Alis::$varD7);        return;    }    Aud::$muvolume = Alis::$varD7 & 0xff;    // *(u8 *)&audio.maxvolume = varD7: low byte of the u16 (little-endian host)    Aud::$maxvolume = (Aud::$maxvolume & 0xff00) | (Alis::$varD7 & 0xff);    Aud::$muvol = ((Aud::$muvolume >> 1) + 1) & 0xff;    if (Aud::$muattac != 0) {        Aud::$dattac = intdiv(Aud::$maxvolume, Aud::$muattac);    }    if (Aud::$muchute != 0) {        Aud::$dchute = intdiv(Aud::$maxvolume, Aud::$muchute);    }}// Codopname no. 154 opcode 0x99 cxinvfunction cxinv(): void{    $xinv = get_0x03_xinv(Alis::$script->vram_org);    $xinv ^= 1;   // BIT_CHG(xinv, 0)    set_0x03_xinv(Alis::$script->vram_org, $xinv & 0xff);}// Codopname no. 155 opcode 0x9a cxinvonfunction cxinvon(): void{    set_0x03_xinv(Alis::$script->vram_org, 1);}// Codopname no. 156 opcode 0x9b cxinvofffunction cxinvoff(): void{    set_0x03_xinv(Alis::$script->vram_org, 0);}// Codopname no. 157 opcode 0x9c clistentfunction clistent(): void{    $entidx = s16(alis_first_ent());    $tabidx = 0;    if (Alis::$platform->version == 10) {        // v1.0 includes entity 0 and keeps fallent        do {            Alis::$tablent[$tabidx++] = $entidx;        } while (($entidx = xread16(Alis::$atent + 4 + $entidx)) != 0 && $tabidx < 127);        Alis::$tablent[$tabidx] = -1;        crstent();        return;    }    while (($entidx = xread16(Alis::$atent + 4 + $entidx)) != 0 && !tabent_full($tabidx)) {        Alis::$matent[$tabidx] = 0;        Alis::$tablent[$tabidx] = $entidx;        $tabidx++;    }    Alis::$tablent[$tabidx] = ENTLIST_END();    Alis::$fallent = 0;    crstent();}function sound(): void{    // v1.0: priority (0 = 4, odd = interruptible), index; one digi channel at ~8 kHz    if (Alis::$platform->version == 10) {        readexec_opername();        $prio = Alis::$varD7 & 0xff;        readexec_opername();        if (Alis::$platform->kind == C::EPlatformPC) {            return;        }        $addr = adresmus(Alis::$varD7);        $ch = Aud::$channels[3];        if (xread8($addr) != 1 || ($ch->type == C::eChannelTypeSample && !($ch->curson & 1))) {            return;        }        $len = (xread8($addr + 1) << 8) | xread8($addr + 2);        if ($len < 2) {            return;        }        $ch->type = C::eChannelTypeNone;        $ch->address = $addr + 3;        $ch->length = $len - 1;        $ch->freq = 7979;        $ch->volume = 0xff;        $ch->loop = 0;        $ch->forever = false;        $ch->played = 0;        $ch->curson = s8($prio ? $prio : 4);        $ch->type = C::eChannelTypeSample;        return;    }    readexec_opername();    $index = Alis::$varD7 & 0xff;    readexec_opername();    $priorson = Alis::$varD7 & 0xff;    readexec_opername();    $volson = Alis::$varD7 & 0xff;    readexec_opername();    $loopsam = Alis::$varD7 & 0xffff;    readexec_opername();    $speedsam = Alis::$varD7 & 0xff;    $addr = adresmus($index);    $type = s8(xread8($addr));    if ($type == 1 || $type == 2) {        if ($speedsam == 0) {            $speedsam = xread8($addr + 1);        }        $longsam = s32(xread32($addr + 2) - 0x10);        if (Alis::$platform->kind == C::EPlatformPC && (Alis::$platform->uid == C::EGameColorado || Alis::$platform->uid == C::EGameWindsurfWilly || Alis::$platform->uid == C::EGameMadShow || Alis::$platform->uid == C::EGameLeFeticheMaya)) {            $longsam = s32(xread32be($addr + 2) - 0x10);        }        $startsam = $addr + 0x10;        playsample(C::eChannelTypeSample, $startsam, $speedsam, $volson, $longsam, $loopsam, $priorson, s16(get_0x0e_script_ent(Alis::$script->vram_org)));    }}// Codopname no. 158 opcode 0x9d csoundfunction csound(): void{    if (Alis::$platform->version >= 60) {        csound_v6();        return;    }    Alis::$flagmain = 0;    sound();}// Codopname no. 159 opcode 0x9e cmsoundfunction cmsound(): void{    if (Alis::$platform->version >= 60) {        cmsound_v6();        return;    }    Alis::$flagmain = 1;    sound();}// Codopname no. 160 opcode 0x9f credonfunction credon(): void{    // v1.0: cding with volume    if (Alis::$platform->version == 10) {        readexec_opername();        ymsfx(C::eChannelTypeDingZap, Alis::$varD7 & 0xff, 0);        return;    }    set_0x25_credon_credoff(Alis::$script->vram_org, 0x0);}// Codopname no. 161 opcode 0xa0 credofffunction credoff(): void{    // v1.0: cexplode with volume    if (Alis::$platform->version == 10) {        readexec_opername();        ymsfx(C::eChannelTypeExplode, Alis::$varD7 & 0xff, 0);        return;    }    set_0x25_credon_credoff(Alis::$script->vram_org, Alis::$platform->version < 30 ? 0xff : 0x80);}// Codopname no. 162 opcode 0xa1 cdelsoundfunction cdelsound(): void{    offsound();}