EN
games/alis / src / Alis / fn / opcodes_v6a.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/opcodes.c (MIT, Olivier Huguenot, Vadim Kindl): the v6 (version >= 60) functions of// the first half of the file (lines 1-6575, before cwmov). The mixed functions keep their v6 branch// inline in opcodes_a.php; the v6 override table is in opcodes_v6b.php.use App\Alis\OpcodesV6Vars as V6;const V6S = 0x50; // image.h V6_SLOT_STRIDE/** C: #define ENTLIST_END (alis.platform.version >= 60 ? 0 : -1) — terminator of the found-entity list */function ENTLIST_END(): int{    return Alis::$platform->version >= 60 ? 0 : -1;}// v6 dispatch target for cscdumpfunction cscdump_v6(): void{    Img::$numelem = getprot();    readexec_opername();    readexec_opername();    readexec_opername();    readexec_opername();    readexec_opername(); Img::$depx = s16(Alis::$varD7);    readexec_opername(); Img::$depy = s16(Alis::$varD7);    readexec_opername(); Img::$depz = s16(Alis::$varD7);    // TODO: FUN_0000761f — put the type-0x39 credits scroll sprite (render not yet implemented).}// v6 dispatch target for cleavefunction cleave_v6(): void{    if (Alis::$fseq != 0) {        xwrite32(Alis::$script->vram_org - 0x80, Alis::$script->pc - Alis::$script->pc_org);        xwrite16(Alis::$script->vram_org - 0x7c, Alis::$script->vacc_off);    }}// v6 dispatch target for cstopretfunction cstopret_v6(): void{    if (Alis::$fseq == 0) {        cstop();        return;    }    cret();    Alis::$script->running = 0;}// v6 dispatch target for cexitfunction cexit_v6(): void{    if (Alis::$script->vram_org == Alis::$basemain) {        Alis::$cstopret = 1;        Alis::$state = C::eAlisStateStopped;        Alis::$script->running = 0;        return;    }    Alis::$ferase = 1;    killent(xread16(Alis::$script->vram_org + 0x3a) & 0xffff);    Alis::$script->running = 0;}/** * C: static void getprot_x(sV6Prot *p). sV6Prot {s32 elem; u32 limit; s32 aux} → PHP array * ['elem' => , 'limit' => , 'aux' => ] filled through the reference. */function getprot_x(array &$p): void{    $vram = Alis::$script->vram_org;    $ax = script_read16() & 0xffff;    if ($ax == 0xffff) {        $p['elem'] = -1; $p['aux'] = -1; $p['limit'] = 0;        return;    }    if ($ax == 0xfffd) {        $elem = $vram + (script_read16() & 0xffff);        $size = xread16($elem - 4) & 0xffff;        if (s8(xread8($elem - 1)) >= 0) {            Alis::$v6tabcell = 0;            readexec_opername();            if (Alis::$v6tabcell != 0) {                $elem = Alis::$v6tabcell;            }        }        readexec_opername_saveD7();        $aux = (Alis::$varD7 & 0xffff) * V6S;        $p['elem'] = s32($elem);        $p['limit'] = s32($elem + $size - 8);        if ($aux > $size) {            // getprot_x: slot index out of range (perror 0x27)            $aux = 0;        }        $p['aux'] = $aux;        return;    }    if ($ax == 0xfffc) {        $elem = $vram + (script_read16() & 0xffff);        $size = xread16($elem - 4) & 0xffff;        if (s8(xread8($elem - 1)) >= 0) {            Alis::$v6tabcell = 0;            readexec_opername();            if (Alis::$v6tabcell != 0) {                $elem = Alis::$v6tabcell;            }        }        readexec_opername_saveD7();        $desc = adresdes(s16(Alis::$varD7));        $aux = 0;        $count = xread8($elem + 0x33);        for ($i = 0; $i < $count; $i++) {            if (xread32($elem + $aux + 0x3c) == $desc) {                break;            }            $aux += V6S;        }        $p['elem'] = s32($elem);        $p['aux'] = $aux;        $p['limit'] = s32($elem + $size - 8);        return;    }    if ($ax == 0xfffe) {        $p['elem'] = s32($vram);        $p['limit'] = s32($vram + V6S);        $p['aux'] = 0;        return;    }    if ($ax == 0xfffb) {        readexec_opername_saveD7();        $id = s16(Alis::$varD7);        if ($id == 0) {            $p['elem'] = 0; $p['limit'] = 0; $p['aux'] = 0;            return;        }        $elem = xread32(Alis::$atent + ($id & 0xffff));        $p['elem'] = $elem;        $p['limit'] = s32($elem + V6S);        $p['aux'] = 0;        return;    }    if ($ax == 0xfffa) {        $elem = Alis::$basemain + (script_read16() & 0xffff);        $p['elem'] = s32($elem);        $p['limit'] = s32($elem + V6S);        $p['aux'] = 0;        return;    }    if ($ax > 0xff00) {        $p['elem'] = s16($ax);        $p['aux'] = -1; $p['limit'] = 0;        return;    }    $elem = $vram + $ax;    $size = xread16($elem - 4) & 0xffff;    if (s8(xread8($elem - 1)) >= 0) {        Alis::$v6tabcell = 0;        readexec_opername();        if (Alis::$v6tabcell != 0) {            $elem = Alis::$v6tabcell;        }    }    $p['elem'] = s32($elem);    $p['aux'] = -1;    $p['limit'] = s32($elem + $size - 8);}// reads and drops one element selectorfunction v6_skipprot(): void{    $p = [];    getprot_x($p);}function v6_put_entry(int $xflip, int $frommain, int $mzoom, int $at): void{    $v6put = ImageV6Vars::$v6put;    Alis::$flagmain = $frommain;    $inv = xread8(Alis::$script->vram_org + ($frommain ? 0x24 : 0x25));    $v6put->invert = $xflip ? ($inv ^ 1) : $inv;    $v6put->scale = $mzoom ? 0 : 0x100;    $v6put->rotate = 0;    $v6put->rotb1 = 0;    $v6put->rotb2 = 0;    $v6put->ftext = 0;    $p = [];    getprot_x($p);    $v6put->limit = $p['limit'];    $v6put->basesprite = $p['elem'];    readexec_opername_saveD7();    $desc = adresdes(s16(Alis::$varD7));    $v6put->desc = ($desc == 0xf) ? 0 : $desc;    if ($v6put->scale == 0) {        readexec_opername_saveD7();        $v6put->rotate = Alis::$varD7 & 0xffff;        readexec_opername_saveD7();        $v6put->scale = Alis::$varD7 & 0xffff;        readexec_opername_saveD7();        $v6put->rotb1 = Alis::$varD7 & 0xff;        $v6put->rotb2 = ((Alis::$varD7 & 0xffff) >> 8) & 0xff;    }    if ($at) {        readexec_opername_saveD7();        $v6put->depx = s16(Alis::$varD7);        readexec_opername_saveD7();        $v6put->depy = s16(Alis::$varD7);        readexec_opername_saveD7();        $v6put->depz = s16(Alis::$varD7);    } else {        $v6put->depx = 0;        $v6put->depy = 0;        $v6put->depz = 0;    }    $v6put->angx = 0;    $v6put->angy = 0;    $v6put->angz = 0;    if ($p['elem'] > 0) {        put_v6($p['elem']);    }}// Codopname opcode 0x37 — v6 TWEEN/ANIM slot driverfunction canim_v6(): void{    $v6put = ImageV6Vars::$v6put;    $p = [];    getprot_x($p);    $v6put->basesprite = $p['elem'];    $elem = $p['elem'];    $slot = $elem;    $usable = 1;    $count = xread8($elem + 0x33);    if ($count != 0) {        $v6put->numcreated = $count;        $slot = $elem + $count * V6S;        $slot -= V6S;        if (xread8($slot + 0x22) != 0x3a) {            if ($p['limit'] != 0 && ult($p['limit'], s32($slot + 2 * V6S))) {                // canim v6: elem slot array full - anim suppressed                $usable = 0;            } else {                $slot += V6S;                xwrite8($slot + 0x22, 0x3a);                xwrite32($slot + 0x40, -1);                xwrite32($slot + 0x50, 0);                xwrite32($slot + 0x3c, 0);                $v6put->numcreated = ($v6put->numcreated + 1) & 0xff;                xwrite8($elem + 0x33, $count + 1);                if (xread32($slot + 0x34) == 0) {                    $world = get_0x16_screen_id(Alis::$script->vram_org) & 0xffff;                    xwrite16($slot + 0x2c, $world);                    $w = Alis::$basemain + $world;                    $head = xread32($w + 0x38);                    if ($head <= 0 || $head >= Alis::$platform->ram_sz) {                        // canim v6: world has no sprite list - not linking                        goto canim_linked;                    }                    xwrite32($w + 0x38, $slot);                    xwrite32($head + 0x34, $slot);                    xwrite32($slot + 0x34, $w);                    xwrite32($slot + 0x38, $head);                    xwrite32($slot + 0x8, Alis::$script->vram_org);                    xwrite8($slot + 0x23, 0x80);                    canim_linked:                }            }        }    }    readexec_opername_saveD7();    $desc = adresdes(s16(Alis::$varD7));    if ($usable) {        if ($desc != xread32($slot + 0x3c)) {            xwrite32($slot + 0x40, -1);        }        xwrite32($slot + 0x3c, $desc);    }    readexec_opername_saveD7();    if ($usable) {        xwrite32($slot + 0x44, s32(Alis::$varD7));    }    readexec_opername_saveD7();    if ($usable) {        xwrite16($slot + 0x54, Alis::$varD7);    }    readexec_opername_saveD7();    $op4 = s32(Alis::$varD7);    if ($usable) {        xwrite32($slot + 0x48, $op4);    }    if ($usable && xread16($slot + 0x54) != 0) {        $result = v6anim_step($slot);    } elseif ($op4 == -1 && $desc != 0 && xread8($desc + xread32($desc)) == 2) {        $result = xread16($desc + xread32($desc) + 0xa) & 0xffff;    } else {        $result = $usable ? xread32($slot + 0x40) : 0;    }    Alis::$varD7 = $result;    cstore_continue();}function v6_erase_entry(int $ferase): void{    Alis::$ferase = $ferase & 0xff;    $p = [];    getprot_x($p);    if ($p['elem'] < 0) {        killallelem_v6(Alis::$script->vram_org);    } elseif ($p['elem'] > 0) {        eraselem_v6($p['elem']);    }}function cerasen_v6(): void{    $p = [];    getprot_x($p);    if ($p['aux'] != -1) {        $slot = s32($p['elem'] + $p['aux']);        readexec_opername_saveD7();        xadd32($slot + 0xc, s32(Alis::$varD7));        readexec_opername_saveD7();        xadd32($slot + 0x10, s32(Alis::$varD7));        readexec_opername_saveD7();        xadd32($slot + 0x14, s32(Alis::$varD7));        return;    }    readexec_opername_saveD7();    $dx = s16(Alis::$varD7);    readexec_opername_saveD7();    $dy = s16(Alis::$varD7);    readexec_opername_saveD7();    $dz = s16(Alis::$varD7);    $slot = $p['elem'];    $count = xread8($slot + 0x33);    for ($i = 0; $i < $count; $i++) {        if (xread32($slot + 0x8) == Alis::$script->vram_org) {            xadd32($slot + 0xc, $dx);            xadd32($slot + 0x10, $dy);            xadd32($slot + 0x14, $dz);            if (xread8($slot + 0x22) == 0x30) {                xadd16($slot + 0x48, $dx);                xadd16($slot + 0x4a, $dy);                xadd16($slot + 0x4c, $dz);            }            xwrite8($slot + 0x23, xread8($slot + 0x23) | 1);        }        $slot += V6S;    }}// append screen to the end of the ptscreen display chainfunction v6_scadd(int $id): void{    xwrite16(Alis::$basemain + $id + 0x5c, 0);    if (Scr::$ptscreen == 0) {        Scr::$ptscreen = $id;        return;    }    $cur = Scr::$ptscreen;    while (($nxt = xread16(Alis::$basemain + $cur + 0x5c) & 0xffff) != 0) {        $cur = $nxt;    }    xwrite16(Alis::$basemain + $cur + 0x5c, $id);}// unlink screen from the ptscreen chainfunction v6_scdel(int $id): void{    $cur = Scr::$ptscreen;    if ($cur == 0) {        return;    }    if ($cur == $id) {        Scr::$ptscreen = xread16(Alis::$basemain + $id + 0x5c) & 0xffff;        return;    }    do {        $nxt = xread16(Alis::$basemain + $cur + 0x5c) & 0xffff;        if ($nxt == $id) {            xwrite16(Alis::$basemain + $cur + 0x5c, xread16(Alis::$basemain + $id + 0x5c));            return;        }        $cur = $nxt;    } while ($cur != 0);}// refresh the screen's buffer/bounds fieldsfunction v6_screfresh(int $s): void{    xwrite32($s + 0x72, 0);    xwrite16($s + 0x76, 0);    xwrite16($s + 0x78, 0);    $w = s16(Alis::$platform->width);    $h = s16(Alis::$platform->height);    xwrite16($s + 0x7a, $w);    xwrite16($s + 0x7c, $h);    $x1 = s16(xread16($s + 0x66));    $y1 = s16(xread16($s + 0x68));    $x2 = s16(xread16($s + 0x6a));    $y2 = s16(xread16($s + 0x6c));    if ($x1 < 0) {        $x1 = 0;    }    if ($y1 < 0) {        $y1 = 0;    }    if ($x2 > $w - 1) {        $x2 = $w - 1;    }    if ($y2 > $h - 1) {        $y2 = $h - 1;    }    xwrite16($s + 0x66, $x1);    xwrite16($s + 0x68, $y1);    xwrite16($s + 0x6a, $x2);    xwrite16($s + 0x6c, $y2);    xwrite16($s + 0x6e, $x2 - $x1 + 1);    xwrite16($s + 0x70, $y2 - $y1 + 1);}function cscreen_v6x(int $id): void{    xwrite16(Alis::$script->vram_org - 0x64, $id);     // define-reg    $s = Alis::$basemain + $id;    if (xread8($s + 0x5b) != 0) {                      // already defined        return;    }    xwrite8($s + 0x5a, 0);                             // projection mode    xwrite16($s + 0x66, 0);                            // clip x1    xwrite16($s + 0x68, 0);                            // clip y1    $w = s16(Alis::$platform->width);    xwrite16($s + 0x6e, $w);    xwrite16($s + 0x6a, $w - 1);                       // clip x2    $h = s16(Alis::$platform->height);    xwrite16($s + 0x70, $h);    xwrite16($s + 0x6c, $h - 1);                       // clip y2    xwrite32($s + 0xb4, 0);    xwrite8($s + 0x136, 0);    xwrite8($s + 0x137, 0);    xwrite8($s + 0x138, 0);    xwrite32($s + 0x132, 0);    xwrite8($s + 0x139, 0);    xwrite8($s + 0x13a, 0);    xwrite8($s + 0x59, 0);    xwrite8($s + 0x58, 0x40);                          // defined, hidden    xwrite32($s + 0x72, 0);    xwrite16($s + 0x76, 0);    xwrite16($s + 0x78, 0);    xwrite16($s + 0x7a, $w);    xwrite16($s + 0x7c, $h);    xwrite8($s + 0x13b, 0);                            // background colour    v6_scadd($id);    xwrite8($s + 0x5b, 1);                             // defined flag}// v6 dispatch target for cformefunction cforme_v6(): void{    ImageV6Vars::$v6put->fadddes = 1;    cput();    ImageV6Vars::$v6put->fadddes = 0;}// v6 dispatch target for cdelformefunction cdelforme_v6(): void{    ImageV6Vars::$v6put->fadddes = 1;    cputnat();    ImageV6Vars::$v6put->fadddes = 0;}function v6_tst_from_movevec(): void{    $vram = Alis::$script->vram_org;    Alis::$v6tstdx = s16(xread16($vram + 0x26));    Alis::$v6tstdy = s16(xread16($vram + 0x28));    Alis::$v6tstdz = s16(xread16($vram + 0x2a));}function v6_tst_addr_operand(): void{    $p = [];    getprot_x($p);    Alis::$v6tstarg = $p['elem'];}// reset the found stackfunction v6ts_reset(): void{    V6::$v6mat_n = 0;    Alis::$ptrent = 0; // alis.tablent    V6::$v6ts_stop = 0;}function v6ts_push(int $id, int $mat): int{    if (V6::$v6mat_n < V6::V6MAT_MAX - 1) {        Alis::$tablent[V6::$v6mat_n] = s16($id);        V6::$v6mat_id[V6::$v6mat_n] = s32($mat);        V6::$v6mat_n++;    }    if (Alis::$fallent == 0) {        V6::$v6ts_stop = 1;        return 1;    }    return 0;}// terminator at the cursorfunction v6ts_term(): void{    if (V6::$v6mat_n < V6::V6MAT_MAX) {        Alis::$tablent[V6::$v6mat_n] = 0;        V6::$v6mat_id[V6::$v6mat_n] = 0;    }}function v6ts_chainpos(int $parent, int $lx, int $ly, int $lz, array &$out): void{    $out[0] = $lx; $out[1] = $ly; $out[2] = $lz;    for ($e = $parent; $e != 0; $e = xread32($e + 0x8)) {        $out[0] = s32($out[0] + xread32($e + 0x0c));        $out[1] = s32($out[1] + xread32($e + 0x10));        $out[2] = s32($out[2] + xread32($e + 0x14));    }}function v6ts_worldbox(int $parent, int $lx, int $ly, int $lz, int $flip, array $bmin, array $bext): void{    $pos = [0, 0, 0];    v6ts_chainpos($parent, $lx, $ly, $lz, $pos);    if ($flip & 1) {        V6::$v6ts_boxmax[0] = s32($pos[0] - $bmin[0]);        V6::$v6ts_boxmin[0] = s32(V6::$v6ts_boxmax[0] - $bext[0]);    } else {        V6::$v6ts_boxmin[0] = s32($pos[0] + $bmin[0]);        V6::$v6ts_boxmax[0] = s32(V6::$v6ts_boxmin[0] + $bext[0]);    }    V6::$v6ts_boxmin[2] = s32($pos[2] + $bmin[2]);    V6::$v6ts_boxmax[2] = s32(V6::$v6ts_boxmin[2] + $bext[2]);    V6::$v6ts_boxmin[1] = s32($pos[1] + $bmin[1]);    V6::$v6ts_boxmax[1] = s32(V6::$v6ts_boxmin[1] + $bext[1]);}function v6ts_aabb(int $cand): int{    $pos = [0, 0, 0];    v6ts_chainpos(xread32($cand + 0x8), xread32($cand + 0x0c), xread32($cand + 0x10), xread32($cand + 0x14), $pos);    if (xread8($cand + 0x25) & 1) {        $cmax = s32($pos[0] - xread32($cand + 0x40));        $cmin = s32($cmax - xread32($cand + 0x4c));    } else {        $cmin = s32($pos[0] + xread32($cand + 0x40));        $cmax = s32($cmin + xread32($cand + 0x4c));    }    if ($cmin > V6::$v6ts_boxmax[0] || $cmax < V6::$v6ts_boxmin[0]) {        return 0;    }    $cmin = s32($pos[2] + xread32($cand + 0x48));    $cmax = s32($cmin + xread32($cand + 0x54));    if ($cmin > V6::$v6ts_boxmax[2] || $cmax < V6::$v6ts_boxmin[2]) {        return 0;    }    $cmin = s32($pos[1] + xread32($cand + 0x44));    $cmax = s32($cmin + xread32($cand + 0x50));    if ($cmin > V6::$v6ts_boxmax[1] || $cmax < V6::$v6ts_boxmin[1]) {        return 0;    }    return 1;}function v6ts_maptest(int $parent, int $lx, int $ly, int $lz, int $scroff): void{    $scr = Alis::$basemain + s16(xread16(Alis::$script->vram_org - 0x64));    if (xread8($scr + 0x5a) != 5 || xread32($scr + 0x5e) != s32(Alis::$basemain + $scroff)) {        return;    }    $pos = [0, 0, 0];    v6ts_chainpos($parent, $lx, $ly, $lz, $pos);    $mat = 0;    $r = r3do_maptest($scr, $pos[0], $pos[1], V6::$v6ts_boxmin, V6::$v6ts_boxmax, V6::$v6wk_walking, $mat);    if ($r) {        v6ts_push($r & 0xffff, $mat);    }}function v6ts_scan_box(int $parent, int $lx, int $ly, int $lz, int $flip, array $bmin, array $bext, int $selfid, int $scroff): void{    $is3do = Alis::$platform->uid == C::EGameRobinsonsRequiem3DO;    v6ts_worldbox($parent, $lx, $ly, $lz, $flip, $bmin, $bext);    $e = Alis::$basemain + $scroff;    for (;;) {        $e = xread32($e + 0x38);        if ($e == 0 || $e == xread32($e + 0x38)) {            break;        }        if ((Alis::$v6tstmask & xread16($e + 0x28) & 0xffff) == 0) {            continue;        }        if (!$is3do && (xread8($e + 0x24) & 0x40)) {            continue;        }        if (!$is3do && (xread8($e + 0x25) & 0x04)) {            continue;        }        if (xread8($e + 0x22) != 0x35) {            continue;        }        if ((xread16($e + 0x2a) & 0xffff) == $selfid) {            continue;        }        if (!v6ts_aabb($e)) {            continue;        }        $mat = s32((xread16($e + 0x26) & 0xffff) | shl32(xread16($e + 0x28) & 0xffff, 16));        if (v6ts_push(xread16($e + 0x2a) & 0xffff, $mat)) {            return;        }    }    if ($is3do) {        v6ts_maptest($parent, $lx, $ly, $lz, $scroff);    }}function v6ts_scan_sphere3do(int $selfid, int $scroff): void{    for ($e = Alis::$basemain + $scroff; ;) {        $e = xread32($e + 0x38);        if ($e == 0 || $e == xread32($e + 0x38)) {            break;        }        if ((Alis::$v6tstmask & xread16($e + 0x28) & 0xffff) == 0 || (xread16($e + 0x2a) & 0xffff) == $selfid) {            continue;        }        if (xread8($e + 0x22) != 0x33 && xread8($e + 0x22) != 0x30) {            continue;        }        if (v6ts_push(xread16($e + 0x2a) & 0xffff, xread16($e + 0x26) & 0xffff)) {            return;        }    }}function v6ts_scan_shape(): void{    $pr = V6::$v6probe;    if ($pr['type'] == 0x33) {        v6col_sphere_scan($pr['parent'], $pr['srad'], $pr['scen'][0], $pr['scen'][1], $pr['scen'][2], $pr['selfid'], $pr['scroff']);        return;    }    v6ts_scan_box($pr['parent'], $pr['lx'], $pr['ly'], $pr['lz'], $pr['flip'], $pr['bmin'], $pr['bext'], $pr['selfid'], $pr['scroff']);}function v6ts_scan_elem(int $probe): void{    $t = xread8($probe + 0x22);    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO && ($t == 0x33 || $t == 0x30)) {        if ($t == 0x33) {            v6ts_scan_sphere3do(xread16($probe + 0x2a) & 0xffff, xread16($probe + 0x2c) & 0xffff);            return;        }        $t = 0x35;    }    if ($t == 0x33) {        v6col_sphere_scan($probe, xread16($probe + 0x40) & 0xffff, s16(xread16($probe + 0x42)), s16(xread16($probe + 0x44)), s16(xread16($probe + 0x46)), xread16($probe + 0x2a) & 0xffff, xread16($probe + 0x2c) & 0xffff);        return;    }    if ($t == 0x30) {        $res = xread32($probe + 0x3c);        $res = s32($res + xread32($res));        v6col_sphere_scan($probe, xread16($res + 0x9a) & 0xffff, s16(xread16($res + 0x94)), s16(xread16($res + 0x96)), s16(xread16($res + 0x98)), xread16($probe + 0x2a) & 0xffff, xread16($probe + 0x2c) & 0xffff);        return;    }    $bmin = [xread32($probe + 0x40), xread32($probe + 0x44), xread32($probe + 0x48)];    $bext = [xread32($probe + 0x4c), xread32($probe + 0x50), xread32($probe + 0x54)];    v6ts_scan_box(xread32($probe + 0x8), xread32($probe + 0xc), xread32($probe + 0x10), xread32($probe + 0x14), xread8($probe + 0x25), $bmin, $bext, xread16($probe + 0x2a) & 0xffff, xread16($probe + 0x2c) & 0xffff);}function v6ts_scan_ownelems(): void{    $vram = Alis::$script->vram_org;    $head = v6_elemhead($vram);    if ($head == 0) {        return;    }    $e = xread32($head + 4);    if ($e == 0 || $e == xread32($e + 4)) {        return;    }    $count = xread8($e + 0x33);    for ($i = 0; $i < $count; $i++, $e += V6S) {        if (xread8($e + 0x22) == 0x35) {            v6ts_scan_elem($e);            if (V6::$v6ts_stop) {                break;            }        }    }}function v6_polar(int $vram, int $dx, int $dy, int $dz): void{    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        r3do_polar($vram, $dx, $dy, $dz);        return;    }    $c = new sV6Ctx();    v6tf_build($vram, xread32($vram + 0x8), $c);    $v = [$dx, $dy, $dz];    v6tf_apply($v, $c, $v);    xwrite32($vram + 0x0c, $v[0]);    xwrite32($vram + 0x10, $v[1]);    xwrite32($vram + 0x14, $v[2]);}function v6wk_polar_add(int $vram): void{    v6_polar($vram, Alis::$v6tstdx, Alis::$v6tstdy, Alis::$v6tstdz);}function v6ts_driver(int $elemprobe): void{    $vram = Alis::$script->vram_org;    v6ts_reset();    $n = s8(Alis::$v6tstflag);    if ($n > 0) {        for (; $n > 0; $n--) {            $sx = xread32($vram + 0x0c); $sy = xread32($vram + 0x10); $sz = xread32($vram + 0x14);            if (Alis::$v6tstpolar == 0) {                xwrite32($vram + 0x0c, s32($sx + Alis::$v6tstdx));                xwrite32($vram + 0x10, s32($sy + Alis::$v6tstdy));                xwrite32($vram + 0x14, s32($sz + Alis::$v6tstdz));            } else {                v6wk_polar_add($vram);            }            if ($elemprobe == 0) {                v6ts_scan_shape();            } elseif ($elemprobe == V6::V6TS_OWN_ELEMS) {                v6ts_scan_ownelems();            } else {                v6ts_scan_elem($elemprobe);            }            if (V6::$v6mat_n != 0) {                xwrite32($vram + 0x0c, $sx);                xwrite32($vram + 0x10, $sy);                xwrite32($vram + 0x14, $sz);                break;            }        }    } else {        $n = ($n == 0) ? 1 : -$n;        $ox = xread32($vram + 0x0c); $oy = xread32($vram + 0x10); $oz = xread32($vram + 0x14);        for (; $n > 0; $n--) {            if (Alis::$v6tstpolar) {                v6wk_polar_add($vram);            } else {                xwrite32($vram + 0x0c, s32(xread32($vram + 0x0c) + Alis::$v6tstdx));                xwrite32($vram + 0x10, s32(xread32($vram + 0x10) + Alis::$v6tstdy));                xwrite32($vram + 0x14, s32(xread32($vram + 0x14) + Alis::$v6tstdz));            }            if ($elemprobe == 0) {                v6ts_scan_shape();            } elseif ($elemprobe == V6::V6TS_OWN_ELEMS) {                v6ts_scan_ownelems();            } else {                v6ts_scan_elem($elemprobe);            }            if (V6::$v6mat_n != 0) {                break;            }        }        xwrite32($vram + 0x0c, $ox);        xwrite32($vram + 0x10, $oy);        xwrite32($vram + 0x14, $oz);    }    v6ts_term();}function v6ts_shape_and_scan(int $form): void{    $vram = Alis::$script->vram_org;    if ($form < 0) {        V6::$v6wk_cbinner = 1;        v6ts_driver(V6::V6TS_OWN_ELEMS);        Alis::$fallent = 0;        return;    }    $desc = adresdes(s16($form));    $fd = s32($desc + xread32($desc));                   // resolved form data    $pr = &V6::$v6probe;    $pr['parent'] = $vram;    $pr['scroff'] = xread16($vram - 0x16) & 0xffff;     // entity's screen offset    $pr['selfid'] = xread16($vram + 0x3a) & 0xffff;     // entity's elem id    $pr['lx'] = $pr['ly'] = $pr['lz'] = 0;    $pr['flip'] = 0;    $ft = xread8($fd);                                  // form type    if ($ft == 7) {        $pr['type'] = 0x33;        $pr['scen'][0] = s16(xread16($fd + 0x0c));        $pr['scen'][1] = s16(xread16($fd + 0x0e));        $pr['scen'][2] = s16(xread16($fd + 0x10));        $pr['srad'] = xread16($fd + 0x12) & 0xffff;    } elseif ($ft == 8) {        $pr['type'] = 0x33;        $pr['scen'][0] = s16(xread16($fd + 0x94));        $pr['scen'][1] = s16(xread16($fd + 0x96));        $pr['scen'][2] = s16(xread16($fd + 0x98));        $pr['srad'] = xread16($fd + 0x9a) & 0xffff;    } else {        $pr['type'] = 0x35;        $pr['bmin'][0] = s16(xread16($fd + 0x0c));  $pr['bext'][0] = s16(xread16($fd + 0x12));        $pr['bmin'][1] = s16(xread16($fd + 0x0e));  $pr['bext'][1] = s16(xread16($fd + 0x14));        $pr['bmin'][2] = s16(xread16($fd + 0x10));  $pr['bext'][2] = s16(xread16($fd + 0x16));        $pr['flip'] = xread8($vram + 0x24);         // invert bits    }    unset($pr);    V6::$v6wk_cbinner = 0;    v6ts_driver(0);    Alis::$fallent = 0;}function v6wk_atan2(int $a, int $b, int &$angle, int &$mag): int{    if ($a == 0 && $b == 0) {        return 0;    }    $cx = 0; $neg = 0;    if ($a < 0) {        $cx = ($cx + 0x800) & 0xffff;        $a = s32(-$a);        $b = s32(-$b);    }    if ($b < 0) {        $neg = (int)!$neg;        $b = s32(-$b);    }    if ($a < $b) {        $cx = ($cx + 0x400) & 0xffff;        if ($neg) {            $cx = ($cx - 0x800) & 0xffff;        }        $neg = (int)!$neg;        $t = $a; $a = $b; $b = $t;    }    // q = (u32)(((u64)(u32)b << 10) / (u32)a): here 0 <= b <= a, so q <= 1024 (exact in a double too)    $q = PHP_INT_SIZE >= 8 ? intdiv(($b & 0xffffffff) << 10, $a & 0xffffffff)                           : (int)floor(u32v($b) * 1024.0 / u32v($a));    $ax = s16(OpcodesV6Vars::v6atantab[$q > 1024 ? 1024 : $q]);    if ($neg) {        $ax = -$ax;    }    $ax = ($ax + $cx) & 0xffff;    $ax &= 0xfff; $ax <<= 4;    $angle = $ax;    $mag = s32(v6x_isqrt(s32(mul32($a, $a) + mul32($b, $b))));    return 1;}function v6wk_polarize_pitch(int $dx, int $dy, int $dz): int{    $ang = 0; $mag = 0;    if (!v6wk_atan2($dy, $dx, $ang, $mag)) {        $mag = 0;    }    $pitch = 0; $dist = 0;    if (!v6wk_atan2($mag, $dz, $pitch, $dist)) {        $pitch = 0;    }    return $pitch;}function v6x_focangle(int $scr): void{    $dxl = s16(xread16($scr + 0x62)) - s16(xread16($scr + 0x66));    if ($dxl < 0) {        $dxl = -$dxl;    }    $dxr = s16(xread16($scr + 0x66)) + s16(xread16($scr + 0x6e)) - s16(xread16($scr + 0x62));    if ($dxr < 0) {        $dxr = -$dxr;    }    $dx = $dxl > $dxr ? $dxl : $dxr;    $dyt = s16(xread16($scr + 0x64)) - s16(xread16($scr + 0x68));    if ($dyt < 0) {        $dyt = -$dyt;    }    $dyb = s16(xread16($scr + 0x68)) + s16(xread16($scr + 0x70)) - s16(xread16($scr + 0x64));    if ($dyb < 0) {        $dyb = -$dyb;    }    $dy = $dyt > $dyb ? $dyt : $dyb;    $r = v6x_isqrt(s32(mul32($dx & 0xffff, $dx & 0xffff) + mul32($dy & 0xffff, $dy & 0xffff)));    $f = s16(xread16($scr + 0x48)) < s16(xread16($scr + 0x4c)) ? s16(xread16($scr + 0x48)) : s16(xread16($scr + 0x4c));    if ($f < 0) {        $f = -$f;    }    $ang = 0; $mag = 0;    v6wk_atan2($f, s32($r), $ang, $mag);    xwrite16($scr + 0x176, $ang & 0xffff);}// steppers ground-probe callback dispatchfunction v6wk_probe(): void{    if (V6::$v6wk_cbstep == 0) {        v6ts_shape_and_scan(Alis::$v6walkb8);    } else {        V6::$v6wk_cbinner = 0;        if (Alis::$v6walkb8 > 0) {            v6ts_driver(Alis::$v6walkb8);        } else {            v6ts_driver(V6::V6TS_OWN_ELEMS);        }        Alis::$fallent = 0;    }}function v6wk_blocking(): int{    if (V6::$v6mat_n == 0) {        return 0;    }    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        return 0;    }    $w = shr32(V6::$v6mat_id[V6::$v6mat_n - 1], 16) & 0xffff;    return (int)(s16($w & Alis::$v6tstmask) < 0);}function v6wk_stepper(): int{    $vram = Alis::$script->vram_org;    $cc = Alis::$v6walkcc; $c4 = Alis::$v6walkc4;    if ($c4 <= $cc) {        $cc = $c4;    }    $d0 = Alis::$v6walkd0;    $is3do = Alis::$platform->uid == C::EGameRobinsonsRequiem3DO;    $fall = $is3do ? s32(2 * $c4) : $c4;    if ($is3do) {        if (Alis::$v6walkmode != 1) {            xwrite32($vram + 0x14, s32(xread32($vram + 0x14) - $d0));        }        V6::$v6wk_walking = 1;    } else {        switch (Alis::$v6walkmode) {            case 0: xwrite32($vram + 0x14, s32(xread32($vram + 0x14) - $d0)); break;   // body-height bias            case 1: Deus3dVars::$v6x_walkzoff = $d0; break;                              // camera-Z, then walk            case 2: Deus3dVars::$v6x_walkaoff = s16($d0); break;                         // camera-pitch, then walk            case 3: Deus3dVars::$v6x_walkzoff = $d0; return 0;                           // set only, NO probe            case 4: Deus3dVars::$v6x_walkaoff = s16($d0); return 0;                      // set only, NO probe            default: return 0;        }    }    V6::$v6wk_dc[0] = xread32($vram + 0x0c);              // save start position    V6::$v6wk_dc[1] = xread32($vram + 0x10);    V6::$v6wk_dc[2] = xread32($vram + 0x14);    V6::$v6wk_db = 0;    $zt = s16(xread16($vram + 0x2a));                     // move-vector Z    if ($zt <= 0) {        if ($zt == 0) {            $zt = -1;        }        $zt = -$zt; V6::$v6wk_db = 1;                     // descending intent    }    V6::$v6wk_c8 = $zt;                                   // step tolerance >= 1    $d8 = Alis::$v6walkd8 & 0xff;                         // DEC-BYTE loop counter    V6::$v6wk_d9 = s8(Alis::$v6walkd8);    for (;;) {                                            // outer per-step loop        Alis::$v6tstdz = s32(-V6::$v6wk_c8);              // test: move + step down        Alis::$v6tstflag = 0;        v6wk_probe();        if (V6::$v6mat_n != 0) {            // contact within c8 below the moved position            if (v6wk_blocking()) {                break;            }            Alis::$v6tstflag = 1; Alis::$v6tstdz = 0;     // try APPLY flat            v6wk_probe();            if (V6::$v6mat_n != 0) {                      // still blocked -> climb                Alis::$v6tstdz = 0; Alis::$v6tstflag = 0;                for (;;) {                    $up = s32($cc + Alis::$v6tstdz);                    if ($up > $c4) {                        goto out;                         // blocked wall -> stop                    }                    Alis::$v6tstdz = $up;                    v6wk_probe();                    if (V6::$v6mat_n == 0) {                        break;                            // free above                    }                }                V6::$v6wk_bc = Alis::$v6tstdz;            // FREE / HIT bounds                V6::$v6wk_c0 = s32(Alis::$v6tstdz - $cc);                goto bisect;            }            // applied flat -> next step        } else {            // nothing at -c8: coarse-descend            Alis::$v6tstdz = 0; Alis::$v6tstflag = 0;            $ground = 0;            for (;;) {                $depth = s32($cc - Alis::$v6tstdz);       // negative                if ($depth > $fall) {                     // drop exceeds limit                    // fellThrough                    if (V6::$v6wk_db != 1) {                        Alis::$v6tstflag = 1;             // APPLY at deepest free                        v6wk_probe();                    }                    // cursor NOT advanced, next pair cleared                    if (V6::$v6mat_n < V6::V6MAT_MAX - 1) {                        Alis::$tablent[V6::$v6mat_n] = -2;                        V6::$v6mat_id[V6::$v6mat_n] = 0;                        Alis::$tablent[V6::$v6mat_n + 1] = 0;                        V6::$v6mat_id[V6::$v6mat_n + 1] = 0;                    }                    goto out;                }                Alis::$v6tstdz = s32(-$depth);                v6wk_probe();                if (V6::$v6mat_n != 0) {                    $ground = 1;                    break;                }            }            if ($ground) {                if (v6wk_blocking()) {                    break;                }                V6::$v6wk_c0 = Alis::$v6tstdz;            // HIT bound                V6::$v6wk_bc = s32(Alis::$v6tstdz + $cc); // FREE bound                goto bisect;            }        }        step:        $d8 = ($d8 - 1) & 0xff;        if ($d8 != 0) {            continue;                                     // next step this frame        }        break;        bisect:        Alis::$v6tstflag = 0;        for (;;) {            Alis::$v6tstdz = s32(V6::$v6wk_bc + V6::$v6wk_c0) >> 1;   // SAR midpoint            // PORT: C (bc + c0) is s32 arithmetic (wraps); s32() then >> 1 reproduces it            if (s32(Alis::$v6tstdz - V6::$v6wk_c0) <= V6::$v6wk_c8) { // converged                break;            }            v6wk_probe();            if (V6::$v6mat_n == 0) {                V6::$v6wk_bc = Alis::$v6tstdz;            } else {                V6::$v6wk_c0 = Alis::$v6tstdz;            }        }        Alis::$v6tstdz = V6::$v6wk_bc;                    // APPLY at free bound        Alis::$v6tstflag = 1;        v6wk_probe();        if (V6::$v6mat_n == 0) {                          // landed            goto step;        }        break;    }    out:    $ret = 0;    if (V6::$v6wk_d9 <= 0) {                              // test-only walk:        xwrite32($vram + 0x0c, V6::$v6wk_dc[0]);          // restore start position        xwrite32($vram + 0x10, V6::$v6wk_dc[1]);        xwrite32($vram + 0x14, V6::$v6wk_dc[2]);    }    if ($is3do) {        if (Alis::$v6walkmode == 0) {            xwrite32($vram + 0x14, s32(xread32($vram + 0x14) + $d0));        }        V6::$v6wk_walking = 0;        return 0;    }    if (Alis::$v6walkmode == 0) {        xwrite32($vram + 0x14, s32(xread32($vram + 0x14) + $d0));   // undo bias        return 0;    }    $ret = v6wk_polarize_pitch(s32(xread32($vram + 0x0c) - V6::$v6wk_dc[0]), s32(xread32($vram + 0x10) - V6::$v6wk_dc[1]), s32(xread32($vram + 0x14) - V6::$v6wk_dc[2]));    return $ret;                                          // pitch angle}function v6_tstscan(): void{    v6ts_shape_and_scan(Alis::$v6tstarg);}function v6_tstscan_elem(): void{    V6::$v6wk_cbinner = 0;    if (Alis::$v6tstarg > 0) {        v6ts_driver(Alis::$v6tstarg);    } else {        v6ts_driver(V6::V6TS_OWN_ELEMS);    }    Alis::$fallent = 0;}// Codopname no. 084 opcode 0x53 ctstmov (v6)function ctstmov_v6(): void{    readexec_opername_saveD7();    Alis::$v6tstdx = s32(Alis::$varD7);    readexec_opername_saveD7();    Alis::$v6tstdy = s32(Alis::$varD7);    readexec_opername_saveD7();    Alis::$v6tstdz = s32(Alis::$varD7);    readexec_opername_saveD7();    Alis::$v6tstflag = Alis::$varD7 & 0xff;            // DL, byte    readexec_opername_saveD7();    Alis::$v6tstmask = Alis::$varD7 & 0xffff;          // DX, word    readexec_opername_saveD7();    Alis::$v6tstarg = s32(Alis::$varD7);               // scan argument    v6_tstscan();    crstent();}// Codopname no. 085 opcode 0x54 ctstset (v6)function ctstset_v6(): void{    $vram = Alis::$script->vram_org;    readexec_opername_saveD7();    Alis::$v6tstdx = s32(Alis::$varD7 - xread32($vram + 0x0c));    readexec_opername_saveD7();    Alis::$v6tstdy = s32(Alis::$varD7 - xread32($vram + 0x10));    readexec_opername_saveD7();    Alis::$v6tstdz = s32(Alis::$varD7 - xread32($vram + 0x14));    Alis::$v6tstflag = 0;    readexec_opername_saveD7();    Alis::$v6tstmask = Alis::$varD7 & 0xffff;    readexec_opername_saveD7();    Alis::$v6tstarg = s32(Alis::$varD7);    v6_tstscan();    crstent();}// Codopname no. 086 opcode 0x55 cftstmov (v6)function cftstmov_v6(): void{    readexec_opername_saveD7(); Alis::$v6tstdx = s32(Alis::$varD7);    readexec_opername_saveD7(); Alis::$v6tstdy = s32(Alis::$varD7);    readexec_opername_saveD7(); Alis::$v6tstdz = s32(Alis::$varD7);    readexec_opername_saveD7(); Alis::$v6tstflag = Alis::$varD7 & 0xff;    readexec_opername_saveD7(); Alis::$v6tstmask = Alis::$varD7 & 0xffff;    v6_tst_addr_operand();    v6_tstscan_elem();    crstent();}// Codopname no. 087 opcode 0x56 cftstset (v6)function cftstset_v6(): void{    $vram = Alis::$script->vram_org;    readexec_opername_saveD7(); Alis::$v6tstdx = s32(Alis::$varD7 - xread32($vram + 0x0c));    readexec_opername_saveD7(); Alis::$v6tstdy = s32(Alis::$varD7 - xread32($vram + 0x10));    readexec_opername_saveD7(); Alis::$v6tstdz = s32(Alis::$varD7 - xread32($vram + 0x14));    Alis::$v6tstflag = 0;    readexec_opername_saveD7(); Alis::$v6tstmask = Alis::$varD7 & 0xffff;    v6_tst_addr_operand();    v6_tstscan_elem();    crstent();}function near_v6(int $kind): void{    $is3do = Alis::$platform->uid == C::EGameRobinsonsRequiem3DO;    if ($is3do) {        readexec_opername_saveD7();    } else {        readexec_opername();    }    $r2 = u32v(mul32(Alis::$varD7, Alis::$varD7));  // (u32)varD7 * (u32)varD7    $name = '';    $key = 0;    if ($kind == 0) {        readexec_opername_swap();        $name = str_pad(substr(mem_str(Alis::$sd7), 0, 8), 8, "\0"); // strncpy(name, alis.sd7, 8)    } else {        if ($is3do) {            readexec_opername_saveD7();        } else {            readexec_opername();        }        $key = $kind == 1 && Alis::$fallent ? -1 : s32(Alis::$varD7);    }    $src = Alis::$script->vram_org;    $world = s16(xread16($src - 0x16));    $tabidx = 0;    $entidx = s16(alis_first_ent());    do {        $tgt = xread32(Alis::$atent + $entidx);        $match = $tgt != 0 && s16(xread16($tgt - 0x16)) == $world;        if ($match && $kind == 0) {            $match = mem_read($tgt + 0x3c, 8) === $name;        } elseif ($match && $kind == 1) {            $match = $key <= 0 || $entidx == $key;        } elseif ($match) {            $match = s16(xread16($tgt + ($is3do ? 0x48 : 0x46))) == s16($key);        }        if ($match && $kind == 1 && !$is3do) {            $match = $tgt != $src;        }        if ($match) {            // PORT: C sums (s64) squared differences into a u64; here a double, exact while the sum            // stays below 2^53 (any sum >= 2^32 fails the u32 radius test anyway; u64 wrap ignored).            $d2 = 0;            for ($i = 0; $is3do && $i < 3; $i++) {                $d = (float)xread32($tgt + 0xc + $i * 4) - (float)xread32($src + 0xc + $i * 4);                $d2 += $d * $d;            }            if ($d2 <= $r2) {                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 v6_enqueue(int $id, int $val): void{    if ($id <= 0) {        return;    }    $vram = xread32(Alis::$atent + $id);    if ($vram == 0) {                                   // dead entity        return;    }    if (Alis::$platform->kind != C::EPlatform3DO && (xread8(V6_FARSLEEP($vram)) & AlisV6Const::V6_DORMANT)) {        return;                                         // 3DO has no test    }    if ((get_0x24_scan_inter($vram) & 1) != 0) {        return;    }    $qsize = xread16(get_0x14_script_org_offset($vram) + 0x36) & 0xffff;    $tail = s16(get_0x1e_scan_clr($vram));    $next = s16($tail + 4);    if ($next >= -0x40) {        $next = s16($next - $qsize);    }    if ($next != s16(get_0x1c_scan_clr($vram))) {        xwrite32($vram + $tail, $val);        $tail = $next;    }    set_0x1e_scan_clr($vram, $tail);    set_0x24_scan_inter($vram, get_0x24_scan_inter($vram) | 0x80);}// v6 dispatch target for csendfunction csend_v6(): void{    $count = script_read8();    readexec_opername();    $id = s16(Alis::$varD7);    for ($i = 0; $i <= $count; $i++) {        readexec_opername();        v6_enqueue($id, s32(Alis::$varD7));    }}// v6 dispatch target for cscanclrfunction cscanclr_v6(): void{    set_0x1c_scan_clr(Alis::$script->vram_org, get_0x1e_scan_clr(Alis::$script->vram_org));    set_0x24_scan_inter(Alis::$script->vram_org, get_0x24_scan_inter(Alis::$script->vram_org) & 0x7f);}// Deus DOS czap/cexplode/cding/cnoise only read their operandsfunction psg_ops_dos_v6(int $operands): int{    if (Alis::$platform->version < 60 || Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        return 0;    }    while ($operands--) {        readexec_opername();    }    return 1;}// v6 dispatch target for cfreadvfunction cfreadv_v6(): void{    $b = Alis::$fp ? fread(Alis::$fp, 4) : '';    $b = str_pad($b === false ? '' : $b, 4, "\0");      // u8 b[4] = {0}: short reads leave zeros    Alis::$varD7 = s32((ord($b[0]) << 24) | (ord($b[1]) << 16) | (ord($b[2]) << 8) | ord($b[3]));    cstore_continue();}function v6_tabdata(int $cell): int{    if (s8(xread8($cell - 2)) >= 0) {        return $cell;    }    $blk = xread32($cell);    return $blk > 0 ? s32($blk + xread32($blk)) : 0;}// v6 dispatch target for cpicturefunction cpicture_v6(): void{    $world = script_read16() & 0xffff;    if (Alis::$platform->version >= 60) {        $scr = s16(xread16(Alis::$script->vram_org - 0x64));        if ($scr != 0) {            $s = Alis::$basemain + ($scr & 0xffff);            xwrite32($s + 0x5e, Alis::$basemain + $world);            xwrite8($s + 0x58, xread8($s + 0x58) | 0x80);        }    }}// v6 dispatch target for cdefvectfunction cdefvect_v6(): void{    $p = [];    getprot_x($p);    if ($p['aux'] != -1) {        $slot = s32($p['elem'] + $p['aux']);        readexec_opername_saveD7();        xwrite16($slot + 0x18, Alis::$varD7 << 8);        readexec_opername_saveD7();        xwrite16($slot + 0x1a, Alis::$varD7 << 8);        readexec_opername_saveD7();        xwrite16($slot + 0x1c, Alis::$varD7 << 8);        return;    }    readexec_opername_saveD7();    $ax = s16(Alis::$varD7 << 8);    readexec_opername_saveD7();    $bx = s16(Alis::$varD7 << 8);    readexec_opername_saveD7();    $dx = s16(Alis::$varD7 << 8);    $slot = $p['elem'];    $count = xread8($slot + 0x33);    for ($i = 0; $i < $count; $i++) {        if (xread32($slot + 0x8) == Alis::$script->vram_org) {            xwrite16($slot + 0x18, $ax);            xwrite16($slot + 0x1a, $bx);            xwrite16($slot + 0x1c, $dx);            xwrite8($slot + 0x23, xread8($slot + 0x23) | 1);        }        $slot += V6S;    }}// v6 dispatch target for csetvectfunction csetvect_v6(): void{    $p = [];    getprot_x($p);    if ($p['aux'] != -1) {        $slot = s32($p['elem'] + $p['aux']);        readexec_opername_saveD7();        xadd16($slot + 0x18, Alis::$varD7 << 8);        readexec_opername_saveD7();        xadd16($slot + 0x1a, Alis::$varD7 << 8);        readexec_opername_saveD7();        xadd16($slot + 0x1c, Alis::$varD7 << 8);        return;    }    readexec_opername_saveD7();    $ax = s16(Alis::$varD7 << 8);    readexec_opername_saveD7();    $bx = s16(Alis::$varD7 << 8);    readexec_opername_saveD7();    $dx = s16(Alis::$varD7 << 8);    $slot = $p['elem'];    $count = xread8($slot + 0x33);    for ($i = 0; $i < $count; $i++) {        if (xread32($slot + 0x8) == Alis::$script->vram_org) {            xadd16($slot + 0x18, $ax);            xadd16($slot + 0x1a, $bx);            xadd16($slot + 0x1c, $dx);            xwrite8($slot + 0x23, xread8($slot + 0x23) | 1);        }        $slot += V6S;    }}// v6 dispatch target for cvtstmovfunction cvtstmov_v6(): void{    v6_tst_from_movevec();    readexec_opername_saveD7(); Alis::$v6tstflag = Alis::$varD7 & 0xff;    readexec_opername_saveD7(); Alis::$v6tstmask = Alis::$varD7 & 0xffff;    readexec_opername_saveD7(); Alis::$v6tstarg = s32(Alis::$varD7);    v6_tstscan();    crstent();}// v6 dispatch target for cvftstmovfunction cvftstmov_v6(): void{    v6_tst_from_movevec();    readexec_opername_saveD7(); Alis::$v6tstflag = Alis::$varD7 & 0xff;    readexec_opername_saveD7(); Alis::$v6tstmask = Alis::$varD7 & 0xffff;    v6_tst_addr_operand();    v6_tstscan_elem();    crstent();}function v6_worldhead(): int{    return V6::$defworld_list_head;}// savestate restore: the head isn't saved, find the world no other world links tofunction v6_worldhead_rebuild(): void{    $w = [];    $n = 0;    for ($i = 0; $i < 512; $i++) { // MAX_SCRIPTS        $e = Alis::$live_scripts[$i];        $id = $e && $e->vram_org ? get_0x16_screen_id($e->vram_org) & 0xffff : 0;        for ($b = $id ? Alis::$basemain + $id : 0; $b && ult($b, Alis::$finent) && $n < 16; $b = xread32($b + 0xb4)) {            $k = 0;            while ($k < $n && $w[$k] != $b) {                $k++;            }            if ($k < $n) {                break;            }            $w[$n++] = $b;        }    }    V6::$defworld_list_head = 0;    for ($i = 0; $i < $n && !V6::$defworld_list_head; $i++) {        $linked = 0;        for ($j = 0; $j < $n; $j++) {            $linked |= (int)(xread32($w[$j] + 0xb4) == $w[$i]);        }        if (!$linked) {            V6::$defworld_list_head = $w[$i];        }    }}// v6 dispatch target for cfindmatfunction cfindmat_v6(): void{    $world = script_read16() & 0xffff;    $tabidx = 0;    $entidx = s16(alis_first_ent());    do {        $tgt_vram = xread32(Alis::$atent + $entidx);        // PORT: the C reads the world id before testing tgt_vram != 0; read it only for a live entity        // (same result, avoids a negative offset into Alis::$mem).        $w = $tgt_vram != 0 ? (Alis::$platform->version >= 60 ? get_0x16_screen_id($tgt_vram) : get_0x22_cworld($tgt_vram)) & 0xffff : 0;        if ($tgt_vram != 0 && $w == $world) {            if (tabent_full($tabidx)) {                break;            }            Alis::$matent[$tabidx] = 0;            Alis::$tablent[$tabidx] = $entidx;            $tabidx++;            if (Alis::$fallent == 0) {                break;            }        }        $entidx = xread16(Alis::$atent + $entidx + 4);    } while ($entidx != 0);    Alis::$tablent[$tabidx] = ENTLIST_END();    Alis::$fallent = 0;    crstent();}// v6 dispatch target for csetvolumfunction csetvolum_v6(): void{    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        readexec_opername();        Aud::$muvolume = Alis::$varD7 & 0xff;        // *(u8 *)&audio.maxvolume = (u8)varD7: low byte of the u16 (little-endian host)        Aud::$maxvolume = (Aud::$maxvolume & 0xff00) | (Alis::$varD7 & 0xff);        Aud::$muvol = ((Aud::$muvolume >> 1) + 1) & 0xff;        return;    }    // TODO: port the full v6 music-volume routine    readexec_opername();    $vol = s16(Alis::$varD7);    readexec_opername();    readexec_opername();    readexec_opername();    Aud::$muvolume = $vol & 0xff;    Aud::$maxvolume = (Aud::$maxvolume & 0xff00) | ($vol & 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);    }}function v6_sound_tail(int $slot): void{    $v6put = ImageV6Vars::$v6put;    $w = ($slot > 0);    if (!$w) {        for ($i = 0; $i < 7; $i++) {            readexec_opername_saveD7();        }        // v6 sound: elem is a sentinel/null - operands consumed, writes skipped        return;    }    readexec_opername_saveD7();    xwrite16($slot + 0x40, Alis::$varD7);    readexec_opername_saveD7();    xwrite8($slot + 0x44, Alis::$varD7 & 0xff);    xwrite8($slot + 0x45, Alis::$varD7 & 0xff);    readexec_opername_saveD7();    xwrite8($slot + 0x46, Alis::$varD7 & 0xff);    xwrite8($slot + 0x47, Alis::$varD7 & 0xff);    readexec_opername_saveD7();    xwrite16($slot + 0x4c, Alis::$varD7);    readexec_opername_saveD7();    xwrite16($slot + 0x48, Alis::$varD7);    xwrite16($slot + 0x4a, Alis::$varD7);    readexec_opername_saveD7();    xwrite16($slot + 0x42, Alis::$varD7);    readexec_opername_saveD7();    xwrite16($slot + 0x52, Alis::$varD7);    $pan = xread32($slot + 0x0c) >> 2;    if ($pan > 0x7f) {        $pan = 0x7f;    }    if ($pan < -0x7f) {        $pan = -0x7f;    }    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        $pan = 0;    }    xwrite8($slot + 0x51, $pan & 0xff);    if (xread8($slot + 0x22) != 0x20) {        xwrite8($slot + 0x50, 0xff);        Alis::$v6soundseq = s32(Alis::$v6soundseq + 1);        xwrite32($slot + 0x54, Alis::$v6soundseq);    }    if ($v6put->desc != 0) {        $v6put->defstate = 0x20;        $v6put->invert = xread8(Alis::$script->vram_org + 0x25);        $v6put->depx = $v6put->depy = $v6put->depz = 0;        $v6put->angx = $v6put->angy = $v6put->angz = 0;        $v6put->fadddes = 0;        put_v6($slot);        xwrite8($slot + 0x23, xread8($slot + 0x23) | 0x80);    }    if ((xread16($slot + 0x40) & 3) == 0) {        v6snd_step($slot);    }}function v6_sound_form(): void{    readexec_opername_saveD7();    $form = s16(Alis::$varD7);    ImageV6Vars::$v6put->desc = ($form == -1) ? 0 : adresdes($form);}function csound_v6(): void{    $p = [];    getprot_x($p);    ImageV6Vars::$v6put->basesprite = $p['elem'];    ImageV6Vars::$v6put->limit = $p['limit'];    v6_sound_form();    v6_sound_tail($p['elem']);}function cmsound_v6(): void{    $v6put = ImageV6Vars::$v6put;    $p = [];    getprot_x($p);    $v6put->basesprite = $p['elem'];    $v6put->limit = $p['limit'];    readexec_opername_swap();    $found = null;    // strncmp(alis.sd7, mem + data_org + 8, 8) == 0    $key = substr(mem_str(Alis::$sd7) . "\0", 0, 8);    for ($i = 0; $i < Alis::$nbprog && !$found; $i++) {        $d = Alis::$loaded_scripts[$i];        if ($d && strncmp($key, substr(mem_str($d->data_org + 8) . "\0", 0, 8), 8) == 0) {            $found = $d;        }    }    if (!$found) {        for ($i = 0; $i < 8; $i++) {            readexec_opername_saveD7();        }        return;    }    readexec_opername_saveD7();    $form = s16(Alis::$varD7);    $base = $found->data_org;    if ($form == -1) {        $v6put->desc = 0;    } elseif (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO || ($form & 0xffff) < (xread16($base + 0x3e) & 0xffff)) {        $v6put->desc = $base + 0x40 + ($form & 0xffff) * 4;    } else {        // cmsound: form out of range        $v6put->desc = 0xf;    }    v6_sound_tail($p['elem']);}// v6 dispatch target for cdelsoundfunction cdelsound_v6(): void{    $p = [];    getprot_x($p);    readexec_opername_saveD7();    $arg = s16(Alis::$varD7);    if ($arg != -1) {        Alis::$ferase = 0;        if ($p['elem'] > 0) {            v6snd_delsound($p['elem'], $arg & 0xffff);            eraselem_v6($p['elem']);        } else {            // cdelsound: elem is a sentinel/null - skipped        }    } else {        v6snd_stopchan(-1);    }}