KO
games/alis / src / Alis / fn / image_v6.php · commit 9c722cf
<?phpnamespace App\Alis;// Port of alis/src/image.c (MIT, Olivier Huguenot, Vadim Kindl): the VM v6 parts (Deus, Robinson's// Requiem 3DO, Time Warriors...): the v6 put chain, the v6 dirty-rectangle list, the v6x renderer and// compositor, 3DO CEL images and the true-colour screens, plus the `version >= 60` paths of kept// functions (called from image.php: topalette_v6, putin_v6composite, put_string_v6_align, deptopix_v6,// destofen_v6). The image.h v6 accessors (v6el_*, v6sp_*) are here too.//// TRUE COLOUR (3DO): the C keeps u32 ARGB buffers image.rgblogic / image.rgbphysic. Here they are host// buffers in Alis::$mem (rgb_alloc) with 3 bytes per pixel in the order B, G, R: directly the pixel rows// of a 24-bit BMP (stride = width * 3). Colours are 0xRRGGBB ints, the C alpha byte (0xff000000) is// dropped (never ints >= 2^31). sys_render() outputs a 24-bit BMP from host.pixelbuf.rgb.// ---------------------------------------------------------------------------------------------// image.h v6 accessors (also used by opcodes.c / escnames.c / script.c / deus3d.c)// ---------------------------------------------------------------------------------------------function v6el_prev(int $e): int { return xread32($e + 0x00); }function v6el_next(int $e): int { return xread32($e + 0x04); }function v6el_set_prev(int $e, int $v): void { xwrite32($e + 0x00, $v); }function v6el_set_next(int $e, int $v): void { xwrite32($e + 0x04, $v); }function v6sp_parent(int $sp): int { return xread32($sp + 0x08); }function v6sp_set_parent(int $sp, int $v): void { xwrite32($sp + 0x08, $v); }function v6sp_depx(int $sp): int { return xread32($sp + 0x0c); }function v6sp_depy(int $sp): int { return xread32($sp + 0x10); }function v6sp_depz(int $sp): int { return xread32($sp + 0x14); }function v6sp_set_depx(int $sp, int $v): void { xwrite32($sp + 0x0c, $v); }function v6sp_set_depy(int $sp, int $v): void { xwrite32($sp + 0x10, $v); }function v6sp_set_depz(int $sp, int $v): void { xwrite32($sp + 0x14, $v); }function v6sp_angx(int $sp): int { return xread16($sp + 0x18); }function v6sp_angy(int $sp): int { return xread16($sp + 0x1a); }function v6sp_angz(int $sp): int { return xread16($sp + 0x1c); }function v6sp_set_angx(int $sp, int $v): void { xwrite16($sp + 0x18, $v); }function v6sp_set_angy(int $sp, int $v): void { xwrite16($sp + 0x1a, $v); }function v6sp_set_angz(int $sp, int $v): void { xwrite16($sp + 0x1c, $v); }function v6sp_scale(int $sp): int{    $s = xread16($sp + 0x1e) & 0xffff;    return $s || Alis::$platform->uid != C::EGameRobinsonsRequiem3DO ? $s : 0x100;}function v6sp_set_scale(int $sp, int $v): void { xwrite16($sp + 0x1e, $v); }function v6sp_state(int $sp): int { return xread8($sp + 0x22); }function v6sp_set_state(int $sp, int $v): void { xwrite8($sp + 0x22, $v); }function v6sp_flags(int $sp): int { return xread8($sp + 0x23); }function v6sp_set_flags(int $sp, int $v): void { xwrite8($sp + 0x23, $v); }function v6sp_invert(int $sp): int { return xread8($sp + 0x25); }function v6sp_set_invert(int $sp, int $v): void { xwrite8($sp + 0x25, $v); }function v6sp_width(int $sp): int { return xread16($sp + 0x26) & 0xffff; }function v6sp_height(int $sp): int { return xread16($sp + 0x28) & 0xffff; }function v6sp_set_width(int $sp, int $v): void { xwrite16($sp + 0x26, $v); }function v6sp_set_height(int $sp, int $v): void { xwrite16($sp + 0x28, $v); }function v6sp_screen(int $sp): int { return xread16($sp + 0x2c) & 0xffff; }function v6sp_set_screen(int $sp, int $v): void { xwrite16($sp + 0x2c, $v); }function v6sp_count(int $sp): int { return xread8($sp + 0x33); }function v6sp_set_count(int $sp, int $v): void { xwrite8($sp + 0x33, $v); }function v6sp_prev(int $sp): int { return xread32($sp + 0x34); }function v6sp_next(int $sp): int { return xread32($sp + 0x38); }function v6sp_set_prev(int $sp, int $v): void { xwrite32($sp + 0x34, $v); }function v6sp_set_next(int $sp, int $v): void { xwrite32($sp + 0x38, $v); }function v6sp_desc(int $sp): int { return xread32($sp + 0x3c); }function v6sp_set_desc(int $sp, int $v): void { xwrite32($sp + 0x3c, $v); }// V6_CLINKING(): alis.h macro, defined in alis_v6.php (v6 VM core agent).// ---------------------------------------------------------------------------------------------// Memory helpers of this file (string operations instead of pixel loops)// ---------------------------------------------------------------------------------------------/** * Writes $rows (equal-length byte strings) at $dst, $dst + $pitch, ... of Alis::$mem. Small rectangles * are written byte by byte (in place); big ones with ONE substr_replace of the whole span (the gaps * between rows are copied back unchanged). */function v6_rect_put(int $dst, int $pitch, array $rows): void{    $cnt = count($rows);    if ($cnt == 0) {        return;    }    $n = strlen($rows[0]);    if ($n == 0) {        return;    }    $total = ($cnt - 1) * $pitch + $n;    $m = &Alis::$mem;    if ($cnt * $n <= 2048) {        foreach ($rows as $r) {            for ($i = 0; $i < $n; $i++) {                $m[$dst + $i] = $r[$i];            }            $dst += $pitch;        }        return;    }    $blk = substr($m, $dst, $total);    $out = '';    $gap = $pitch - $n;    $last = $cnt - 1;    foreach ($rows as $i => $r) {        $out .= $r;        if ($i < $last && $gap > 0) {            $out .= substr($blk, $i * $pitch + $n, $gap);        }    }    mem_write($dst, $out); // in place (no copy of the whole memory)}/** 8-bit: $src over $dst, "\0" bytes of $src transparent. */function v6_merge8(string $dst, string $src): string{    $n = strlen($src);    for ($i = 0; $i < $n; $i++) {        $c = $src[$i];        if ($c !== "\0") {            $dst[$i] = $c;        }    }    return $dst;}/** The 3 bytes (B, G, R) of a true-colour pixel 0xRRGGBB. */function rgb_bgr(int $c): string{    return chr($c & 0xff) . chr(($c >> 8) & 0xff) . chr(($c >> 16) & 0xff);}/** Writes one true-colour pixel (3 bytes) at byte offset $o of Alis::$mem. */function rgb_put(int $o, int $c): void{    $m = &Alis::$mem;    $m[$o] = chr($c & 0xff);    $m[$o + 1] = chr(($c >> 8) & 0xff);    $m[$o + 2] = chr(($c >> 16) & 0xff);}/** strtr() map 8-bit index -> 3 bytes of pal_argb(index), cached while image.mpalet does not change. */function v6_pal_bgr_map(): array{    static $pal = null;    static $map = [];    if ($pal !== Img::$mpalet) {        $pal = Img::$mpalet;        $map = [];        for ($i = 0; $i < 256; $i++) {            $map[chr($i)] = rgb_bgr(pal_argb($i));        }    }    return $map;}// ---------------------------------------------------------------------------------------------// version >= 60 paths of kept image.c functions (called from image.php)// ---------------------------------------------------------------------------------------------/** topalette(): the `version >= 60` branch (palette resource: count @8, first @10, RGB24 @0xc). */function topalette_v6(int $paldata): void{    Img::$palc = 0;    $count = swap16($paldata + 8) & 0xffff;    $first = swap16($paldata + 10) & 0xff;    $palptr = $paldata + 0xc;    if ($first + $count > 256) {        $count = (256 - $first) & 0xffff;    }    for ($c = 0; $c < $count; $c++) {        $r = xread8($palptr++);        $g = xread8($palptr++);        $b = xread8($palptr++);        image_pal_write('tpalet', Img::$atpalet + $first + $c, $r, $g, $b);    }}/** putin(): the `version >= 60 && resourcedata[0] == 2` composite branch (putfin() done by the caller). */function putin_v6composite(int $resourcedata, int $x, int $y, int $z): void{    $count = s16(read16($resourcedata + 8));    $entry = $resourcedata + 0xc;    for ($i = 0; $i < $count; $i++) {        $op = xread8($entry);        if ($op != 0 && $op != 1) {            break;        }        $form = s16(read16($entry + 2));        $dx = read32($entry + 4);        $dy = read32($entry + 8);        $dz = read32($entry + 0xc);        $invx = Img::$invert_x;        $muldes = Alis::$fmuldes;        if ($op == 1) {            Img::$invert_x ^= 1;        }        if (Img::$invert_x) {            $dx = s32(-$dx);        }        Img::$depx = s16($x + s16($dx));        Img::$depy = s16($y + s16($dy));        Img::$depz = s16($z + s16($dz));        if ($form >= 0) {            Alis::$fmuldes = 1;            putin($form & 0xffff);        }        Img::$depx = $x;        Img::$depy = $y;        Img::$depz = $z;        Img::$invert_x = $invx;        Alis::$fmuldes = $muldes;        $entry += 16;    }    Alis::$fmuldes = 0;}/** put_string(): the `version >= 60 && charmode == 2` text field / alignment branch. */function put_string_v6_align(): void{    Alis::$ftextfield = 1;    Alis::$textfx = Img::$depx;    Alis::$textfy = Img::$depz;    if (Alis::$talign != 0) {        $w = 0;        $maxw = 0;        for ($p = Alis::$sd7; ($c = ord(Alis::$mem[$p])) != 0; $p++) {            if ($c == 0x0a) {                if ($w > $maxw) {                    $maxw = $w;                }                $w = 0;            } elseif ($c != 0x0d) {                $w = s16($w + Alis::$folarg);            }        }        if ($w > $maxw) {            $maxw = $w;        }        if ((Alis::$talign & 0xff) == 0xff) {            Img::$depx = s16(Img::$depx - $maxw);        } else {            Img::$depx = s16(Img::$depx - ($maxw >> 1));        }    }}/** deptopix(): the `version >= 60` path (from `scene = elemsprvar->screen_id` on). */function deptopix_v6(Sprite $elemsprvar, int $scene): void{    $x = s16($elemsprvar->depx - get_scr_depx($scene));    $y = s16($elemsprvar->depy - get_scr_depy($scene));    $z = s16($elemsprvar->depz - get_scr_depz($scene));    $mode = xread8(Alis::$basemain + $scene + 0x5a);    switch ($mode) {        case 1:            $tmpdepx = $x;            $tmpdepy = s16(-$z);            $tmpdepz = $y;            break;        default:            $tmpdepx = $x;            $tmpdepy = $y;            $tmpdepz = $z;            break;    }    $tmpdepx = s16($tmpdepx + get_scr_unknown0x0a($scene));    $tmpdepy = s16($tmpdepy + get_scr_unknown0x0c($scene));    Img::$newad = s32($elemsprvar->data);   // + offset (0)    $spritedata = s32(Img::$newad + xread32(Img::$newad));    Img::$newl = s16(read16($spritedata + 0x10) - 1);    Img::$newh = s16(read16($spritedata + 0x12) - 1);    $anchorx = s16(read16($spritedata + 0x14));    $anchory = s16(read16($spritedata + 0x16));    Img::$newzoomx = 0;    Img::$newzoomy = 0;    if (Alis::$platform->kind == C::EPlatformMac) {        mac_update_pos($tmpdepx, $tmpdepy);    }    Img::$newx = s16($tmpdepx - $anchorx);    Img::$newy = s16($tmpdepy - $anchory);    Img::$newd = $tmpdepz;}/** destofen(): the `version >= 60` path (descriptor types 1 = bitmap, 5 = solid fill). */function destofen_v6(Sprite $sprite, int $bitmap): void{    $desctype = xread8($bitmap);    if ($desctype != 1 && $desctype != 5) {        return;    }    $v6flip = s8($sprite->newf);    $v6width = $sprite->width + 1;    $v6height = $sprite->height + 1;    $pos = image_rect($sprite->newx, $sprite->newy, $sprite->newx + $sprite->width, $sprite->newy + $sprite->height);    if (Img::$clipx2 < $pos->x1 || Img::$clipy2 < $pos->y1 || $pos->x2 < Img::$clipx1 || $pos->y2 < Img::$clipy1) {        return;    }    Img::$blocx1 = $pos->x1 < Img::$clipx1 ? Img::$clipx1 : $pos->x1;    Img::$blocy1 = $pos->y1 < Img::$clipy1 ? Img::$clipy1 : $pos->y1;    Img::$blocx2 = Img::$clipx2 < $pos->x2 ? Img::$clipx2 : $pos->x2;    Img::$blocy2 = Img::$clipy2 < $pos->y2 ? Img::$clipy2 : $pos->y2;    if ($desctype == 5) {        // solid fill of the clipped rect with the descriptors colour byte        $fillcol = xread8($bitmap + 0x1c);        $w = Host::$pixelbuf->w;        $x1 = Img::$blocx1 < 0 ? 0 : Img::$blocx1;        $y1 = Img::$blocy1 < 0 ? 0 : Img::$blocy1;        $x2 = Img::$blocx2 >= $w ? $w - 1 : Img::$blocx2;        $y2 = Img::$blocy2 >= Host::$pixelbuf->h ? Host::$pixelbuf->h - 1 : Img::$blocy2;        if ($x2 >= $x1 && $y2 >= $y1) {            v6_rect_put(Img::$logic + $x1 + $y1 * $w, $w, array_fill(0, $y2 - $y1 + 1, str_repeat(chr($fillcol), $x2 - $x1 + 1)));        }        return;    }    $bx1 = 0;    $by1 = 0;    $bx2 = $v6width;    $by2 = $v6height;    if (Img::$blocx1 > $pos->x1) {        $bx1 += (Img::$blocx1 - $pos->x1);        $bx2 -= (Img::$blocx1 - $pos->x1);    }    if (Img::$blocx2 <= $pos->x2) {        $bx2 -= ($pos->x2 - Img::$blocx2);    }    if (Img::$blocy1 > $pos->y1) {        $by1 += (Img::$blocy1 - $pos->y1);        $by2 -= (Img::$blocy1 - $pos->y1);    }    if (Img::$blocy2 < $pos->y2) {        $by2 -= ($pos->y2 - Img::$blocy2);    }    $bx2 += $bx1;    $by2 += $by1;    $bmp = image_rect($bx1, $by1, $bx2, $by2);    draw_8bit_0($bitmap + 0x1e, $pos, $bmp, $v6width, $v6flip);}// ---------------------------------------------------------------------------------------------// v6 put chain// ---------------------------------------------------------------------------------------------function v6_elemhead(int $vram): int{    return $vram - 0x78;   // DOS ctx -0x2c (node: prev@0, next@4)}function v6_dirtyelems(int $vram): void{    for ($g = xread32(v6_elemhead($vram) + 4); $g && $g != xread32($g + 4); $g = xread32($g + 4)) {        $e = $g;        for ($n = xread8($g + 0x33); $n; $n--, $e += C::V6_SLOT_STRIDE) {            v6sp_set_flags($e, v6sp_flags($e) | 1);        }    }}function v6_elemtail(int $vram): int{    return $vram - 0x70;   // DOS ctx -0x24}function soundatten_v6(int $sp): void{    if (xread8($sp + 0x25) & 0x40) {        return;    }    $range = 300;    $cl = 0x20;    if (xread8($sp + 0x25) & 0x10) {        $cl |= 0x50;    }    $node = $sp;    $possum = 0;    $first = 1;    for (;;) {        if (!$first) {            if ($possum != 0) {                $cl &= 0xef;                if ($cl == 0) {                    break;                }            }        }        $first = 0;        $st = xread8($node + 0x22);        if ($st == 0x30 || $st == 0x10) {            if ($range <= (xread16($node + 0x40) & 0xffff)) {                $cl &= 0xdf;                if ($cl == 0) {                    break;                }            }            $b = abs(xread16($node + 0x42));            $b += abs(xread16($node + 0x44));            $b += abs(xread16($node + 0x46));            if ($b != 0) {                $cl &= 0xef;                if ($cl == 0) {                    break;                }                if ($range <= $b) {                    $cl &= 0xdf;                    if ($cl == 0) {                        break;                    }                }            }        }        if (xread32($node + 0x18) != 0 || xread16($node + 0x1c) != 0) {            $cl &= 0xaf;            if ($cl == 0) {                break;            }        }        if (xread32($node + 0x8) != 0) {            $v = xread32($node + 0xc);            $possum = s32($v < 0 ? -$v : $v);            $v = xread32($node + 0x10);            $possum = s32($possum + ($v < 0 ? -$v : $v));            $v = xread32($node + 0x14);            $possum = s32($possum + ($v < 0 ? -$v : $v));            if (!ult($possum, $range)) {                $cl &= 0xdf;                if ($cl == 0) {                    break;                }            }        }        $node = xread32($node + 0x8);        if ($node == 0) {            break;        }    }    v6sp_set_flags($sp, v6sp_flags($sp) | $cl);}/** putleaf_v6(): the `common_0x32` block (reached by case 1 and by goto from case 5). */function putleaf_v6_common32(int $sp, int $vram, int $data): void{    $p = ImageV6Vars::$v6put;    xwrite16($sp + 0x4e, xread16(V6_CLINKING($vram)));    v6sp_set_height($sp, xread16($data + 0xa));    v6sp_set_width($sp, xread16($data + 0x8));    xwrite8($sp + 0x51, $p->rotb1);    xwrite8($sp + 0x54, $p->rotb2);    xwrite16($sp + 0x52, $p->rotate);}function putleaf_v6(int $sp): int{    $p = ImageV6Vars::$v6put;    $vram = Alis::$script->vram_org;    $r3do = Alis::$platform->uid == C::EGameRobinsonsRequiem3DO;    if (v6sp_prev($sp) == 0) {        $world = get_0x16_screen_id($vram) & 0xffff;        v6sp_set_screen($sp, $world);        $wbase = Alis::$basemain + $world;        $oldhead = xread32($wbase + 0x38);        xwrite32($wbase + 0x38, $sp);        xwrite32($oldhead + 0x34, $sp);        v6sp_set_prev($sp, $wbase);        v6sp_set_next($sp, $oldhead);        v6sp_set_parent($sp, $p->parent);        v6sp_set_flags($sp, 0x80);    }    xwrite16($sp + 0x2a, xread16($vram + 0x3a));    v6sp_set_depx($sp, $p->depx);    v6sp_set_depy($sp, $p->depy);    v6sp_set_depz($sp, $p->depz);    v6sp_set_angx($sp, $p->angx);    v6sp_set_angy($sp, $p->angy);    v6sp_set_angz($sp, $p->angz);    v6sp_set_invert($sp, $p->invert & 0xff);    if (!$r3do) {        v6sp_set_scale($sp, $p->scale);    }    v6sp_set_desc($sp, $p->desc);    if ($p->desc == 0) {        v6sp_set_state($sp, $p->defstate);    } elseif ($p->ftext) {        v6sp_set_height($sp, 0);        v6sp_set_width($sp, 0);        xwrite16($sp + 0x4e, xread16(V6_CLINKING($vram)));        v6sp_set_state($sp, 0x38);        xwrite8($sp + 0x51, xread8($vram - 0x66));        xwrite8($sp + 0x52, $p->textattr);    } else {        $data = s32($p->desc + xread32($p->desc));        $type = xread8($data);        switch ($type) {            case 1:                v6sp_set_state($sp, 0x32);                if ($r3do) {                    xwrite32($sp + 0x50, 0);                }                putleaf_v6_common32($sp, $vram, $data);                break;            case 5:                v6sp_set_state($sp, 0x34);                v6sp_set_height($sp, xread16($data + 0xa));                v6sp_set_width($sp, xread16($data + 0x8));                xwrite8($sp + 0x50, xread8($data + 0x1c));                xwrite8($sp + 0x51, $p->rotb1);                xwrite8($sp + 0x54, $p->rotb2);                xwrite16($sp + 0x52, $p->rotate);                putleaf_v6_common32($sp, $vram, $data); // goto common_0x32                break;            case 4:                v6sp_set_state($sp, 0x35);                v6sp_set_height($sp, xread16($data + 0xa));                v6sp_set_width($sp, xread16($data + 0x8));                xwrite32($sp + 0x40, xread16($data + 0xc));                xwrite32($sp + 0x44, xread16($data + 0xe));                xwrite32($sp + 0x48, xread16($data + 0x10));                xwrite32($sp + 0x4c, xread16($data + 0x12));                xwrite32($sp + 0x50, xread16($data + 0x14));                xwrite32($sp + 0x54, xread16($data + 0x16));                break;            case 8:                v6sp_set_state($sp, 0x30);                v6sp_set_height($sp, xread16($data + 0xa));                v6sp_set_width($sp, xread16($data + 0x8));                xwrite16($sp + 0x42, xread16($data + 0xc));                xwrite16($sp + 0x44, xread16($data + 0xe));                xwrite16($sp + 0x46, xread16($data + 0x10));                xwrite16($sp + 0x40, xread16($data + 0x12));                if ($r3do && $p->parent != $vram) {                    break;                }                v6sp_set_depx($sp, s32(v6sp_depx($sp) + xread32($data + 0x14)));                v6sp_set_depy($sp, s32(v6sp_depy($sp) + xread32($data + 0x18)));                v6sp_set_depz($sp, s32(v6sp_depz($sp) + xread32($data + 0x1c)));                if ($r3do) {                    break;                }                v6sp_set_angx($sp, v6sp_angx($sp) + xread16($data + 0x8a));                v6sp_set_angy($sp, v6sp_angy($sp) + xread16($data + 0x8e));                v6sp_set_angz($sp, v6sp_angz($sp) + xread16($data + 0x92));                xwrite32($sp + 0x50, $data + 0x88);                xwrite16($sp + 0x48, 0);                xwrite16($sp + 0x4a, 0);                xwrite16($sp + 0x4c, 0);                if ($p->parent == $vram) {                    xwrite16($sp + 0x48, s16($p->depx));                    xwrite16($sp + 0x4a, s16($p->depy));                    xwrite16($sp + 0x4c, s16($p->depz));                }                soundatten_v6($sp);                break;            case 7:                v6sp_set_height($sp, xread16($data + 0xa));                v6sp_set_width($sp, xread16($data + 0x8));                xwrite16($sp + 0x42, xread16($data + 0xc));                xwrite16($sp + 0x44, xread16($data + 0xe));                xwrite16($sp + 0x46, xread16($data + 0x10));                xwrite16($sp + 0x40, xread16($data + 0x12));                $sub = xread8($data + 0x18);                if ($sub == 1 || $sub == 3) {                    xwrite8($sp + 0x56, $sub == 3 ? 1 : 0);                    v6sp_set_state($sp, 0x10);                    xwrite16($sp + 0x48, xread8($data + 0x1b));                    xwrite16($sp + 0x54, xread16($data + 0x38));                    xwrite8($sp + 0x50, xread8($data + 0x34));                    xwrite8($sp + 0x51, xread8($data + 0x35));                    xwrite8($sp + 0x52, xread8($data + 0x36));                    xwrite8($sp + 0x53, xread8($data + 0x37));                } elseif ($sub == 2) {                    v6sp_set_state($sp, 0x13);                } else {                    v6sp_set_state($sp, 0x33);                }                soundatten_v6($sp);                break;            default:                v6sp_set_state($sp, $p->defstate);                break;        }    }    v6sp_set_flags($sp, v6sp_flags($sp) | 1);    $p->numcreated = ($p->numcreated + 1) & 0xff;    return $sp + C::V6_SLOT_STRIDE;}/** Target slot of a putlist entry: 0xff = the entity itself. */function putlist_v6_tgt(int $entry): int{    $base = xread8($entry + 1);    return ($base == 0xff) ? Alis::$script->vram_org : ImageV6Vars::$v6put->basesprite + $base * C::V6_SLOT_STRIDE;}/** $cursor_out: C u32 *cursor_out (NULL = not wanted: pass nothing). */function putlist_v6(int $sp, int $entry, int $count, ?int &$cursor_out = null): int{    $p = ImageV6Vars::$v6put;    while ($count-- != 0 && !$p->aborted) {        $op = xread8($entry);        $flip = 0;        switch ($op) {            case 0x05:                $entry += 16;                goto out;            case 0x0b:                $entry = s32($entry + shl32(xread32($entry + 4), 4) + 16);                goto out;            case 0x03:                $tgt = putlist_v6_tgt($entry);                Alis::$v6kforgx = xread16($tgt + 0x48) & 0xffff;                Alis::$v6kforgy = xread16($tgt + 0x4a) & 0xffff;                Alis::$v6kforgz = xread16($tgt + 0x4c) & 0xffff;                if (v6sp_desc($tgt) == 0 && v6sp_parent($tgt) == Alis::$script->vram_org) {                    $tgt = Alis::$script->vram_org;                }                v6sp_set_depx($tgt, xread32($entry + 0x4));                v6sp_set_depy($tgt, xread32($entry + 0x8));                v6sp_set_depz($tgt, xread32($entry + 0xc));                break;            case 0x04:                $tgt = putlist_v6_tgt($entry);                if (v6sp_desc($tgt) == 0 && v6sp_parent($tgt) == Alis::$script->vram_org) {                    $tgt = Alis::$script->vram_org;                }                v6sp_set_angx($tgt, s16(xread32($entry + 0x4)));                v6sp_set_angy($tgt, s16(xread32($entry + 0x8)));                v6sp_set_angz($tgt, s16(xread32($entry + 0xc)));                break;            case 0x08:                $tgt = putlist_v6_tgt($entry);                v6sp_set_depx($tgt, s32(v6sp_depx($tgt) + xread32($entry + 0x4)));                v6sp_set_depy($tgt, s32(v6sp_depy($tgt) + xread32($entry + 0x8)));                v6sp_set_depz($tgt, s32(v6sp_depz($tgt) + xread32($entry + 0xc)));                break;            case 0x09:                $tgt = putlist_v6_tgt($entry);                v6sp_set_angx($tgt, v6sp_angx($tgt) + s16(xread32($entry + 0x4)));                v6sp_set_angy($tgt, v6sp_angy($tgt) + s16(xread32($entry + 0x8)));                v6sp_set_angz($tgt, v6sp_angz($tgt) + s16(xread32($entry + 0xc)));                break;            case 0x0a:                $base = xread8($entry + 1);                if ($base == 0xff) {                    break;                }                $tgt = $p->basesprite + $base * C::V6_SLOT_STRIDE;                $payload = $entry + 4;                $progress = xread8($sp + 0x40);                $oldkf = xread32($tgt + 0x50);                if ($progress == 0 || $oldkf == 0) {                    xwrite32($tgt + 0x50, $payload);                    Alis::$v6kforgx = xread16($tgt + 0x48) & 0xffff;                    Alis::$v6kforgy = xread16($tgt + 0x4a) & 0xffff;                    Alis::$v6kforgz = xread16($tgt + 0x4c) & 0xffff;                    if (v6sp_desc($tgt) == 0 && v6sp_parent($tgt) == Alis::$script->vram_org) {                        $tgt = Alis::$script->vram_org;                    }                    // PORT: shift counts >= 32 are UB in C; masked to 5 bits like x86 (shl32).                    $sh = (xread8($payload - 1) + 1) & 0xff;                    v6sp_set_depx($tgt, s32(shl32(xread16($payload + 0x0), $sh) + s16(Alis::$v6kforgx)));                    v6sp_set_angx($tgt, xread16($payload + 0x2));                    v6sp_set_depy($tgt, s32(shl32(xread16($payload + 0x4), $sh) + s16(Alis::$v6kforgy)));                    v6sp_set_angy($tgt, xread16($payload + 0x6));                    v6sp_set_depz($tgt, s32(shl32(xread16($payload + 0x8), $sh) + s16(Alis::$v6kforgz)));                    v6sp_set_angz($tgt, xread16($payload + 0xa));                } else {                    Alis::$v6kforgx = xread16($tgt + 0x48) & 0xffff;                    Alis::$v6kforgy = xread16($tgt + 0x4a) & 0xffff;                    Alis::$v6kforgz = xread16($tgt + 0x4c) & 0xffff;                    if (v6sp_desc($tgt) == 0 && v6sp_parent($tgt) == Alis::$script->vram_org) {                        $tgt = Alis::$script->vram_org;                    }                    $osh = (xread8($oldkf - 1) + 1) & 0xff;                    $nsh = (xread8($payload - 1) + 1) & 0xff;                    // V6KF_POS / V6KF_ANG                    $org = [s16(Alis::$v6kforgx), s16(Alis::$v6kforgy), s16(Alis::$v6kforgz)];                    for ($k = 0; $k < 3; $k++) {                        $o = $k * 4;                        $ov = s32(shl32(xread16($oldkf + $o), $osh) + $org[$k]);                        $nv = s32(shl32(xread16($payload + $o), $nsh) + $org[$k]);                        $pos = s32($ov + (mul32(s32($nv - $ov), $progress) >> 8));                        $oa = xread16($oldkf + $o + 2);                        $na = xread16($payload + $o + 2);                        $d = s16($na - $oa);                        $ang = $oa + s16(($d * $progress) >> 8);                        if ($k == 0) {                            v6sp_set_depx($tgt, $pos);                            v6sp_set_angx($tgt, $ang);                        } elseif ($k == 1) {                            v6sp_set_depy($tgt, $pos);                            v6sp_set_angy($tgt, $ang);                        } else {                            v6sp_set_depz($tgt, $pos);                            v6sp_set_angz($tgt, $ang);                        }                    }                }                break;            case 0x01:            default:                $flip = 1;                // fall through            case 0x00:                $sinvert = $p->invert;                $sdepx = $p->depx;                $sdepy = $p->depy;                $sdepz = $p->depz;                $p->parent = Alis::$script->vram_org;                $base = xread8($entry + 1);                if ($base != 0xff) {                    $tgt = $p->basesprite + $base * C::V6_SLOT_STRIDE;                    if ($tgt != $sp) {                        $p->parent = $tgt;                        $p->depx = $p->depy = $p->depz = 0;                        $p->angx = $p->angy = $p->angz = 0;                    }                }                $form = s16(xread16($entry + 2));                if ($form < 0) {                    $p->desc = 0;                    $p->defstate = 0;                } else {                    $p->desc = adresdes($form);                }                $dx = xread32($entry + 4);                if ($p->invert & 1) {                    $dx = s32(-$dx);                }                $p->depx = s32($p->depx + $dx);                $p->depy = s32($p->depy + xread32($entry + 8));                $p->depz = s32($p->depz + xread32($entry + 0xc));                $p->invert = ($p->invert ^ $flip) & 0xffff;                $sp = putin_v6($sp);                $p->depz = $sdepz;                $p->depy = $sdepy;                $p->depx = $sdepx;                $p->invert = $sinvert;                break;        }        $entry += 16;    }    out:    $cursor_out = $entry;    return $sp;}function putin_3do(int $sp, int $data): int{    $p = ImageV6Vars::$v6put;    $type = xread8($data);    if ($type == 2) {        return putlist_v6($sp, $data + 0xc, xread16($data + 8));    }    if ($type != 6) {        $next = putleaf_v6($sp);        if ($type == 1) {            xwrite32($sp + 0x50, 0);        }        return $next;    }    $tab = $p->desc;    $e = $data + 0xc;    $img = $tab + xread16($e + 2) * 4;    if (xread8(s32($img + xread32($img))) != 1) {        return $sp;    }    $sinv = $p->invert & 0xff;   // C: u8 sinv    $p->invert ^= (int) (xread8($e) == 1);    $p->desc = $img;    $next = putleaf_v6($sp);    $hx = s16(xread32($e + 4));    xwrite32($sp + 0x50, $tab);    xwrite16($sp + 0x54, ($p->invert & 1) ? -$hx : $hx);    xwrite16($sp + 0x56, s16(xread32($e + 0xc)));    $p->invert = $sinv;    $p->desc = $tab;    return $next;}function putin_v6(int $sp): int{    $p = ImageV6Vars::$v6put;    if ($p->aborted) {        return $sp;    }    if (!ult($sp, $p->limit)) {        // elem slot array overrun (perror 0x26)        $p->aborted = 1;        return $sp;    }    if ($p->desc == 0) {        return putleaf_v6($sp);    }    if ($p->ftext) {        return putleaf_v6($sp);    }    $data = s32($p->desc + xread32($p->desc));    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        return putin_3do($sp, $data);    }    if (xread8($data) != 2) {        return putleaf_v6($sp);    }    $count = xread16($data + 8) & 0xffff;    return putlist_v6($sp, $data + 0xc, $count);}function v6anim_park(int $entry): void{    for (;;) {        $op = xread8($entry);        if ($op == 0x0a) {            $id = xread8($entry + 1);            if ($id != 0xff) {                xwrite32(ImageV6Vars::$v6put->basesprite + $id * C::V6_SLOT_STRIDE + 0x50, $entry + 4);            }            $entry += 0x10;        } elseif ($op == 0x05) {            return;        } elseif ($op == 0x0b) {            return;        } else {            $entry += 0x11;        }    }}function v6anim_step(int $slot): int{    $target = xread32($slot + 0x48);    if (xread32($slot + 0x40) == -1 || $target == 0 || $target == -1) {        $desc = xread32($slot + 0x3c);        $rec = s32($desc + xread32($desc));        if (xread8($rec) != 2) {            goto epilogue;        }        $total = (xread16($rec + 0xa) & 0xffff) << 8;        xwrite32($slot + 0x4c, $total);        if ($target == -1) {            if (xread32($slot + 0x44) == 0) {                goto epilogue;            }            $target = $total - 0x100;        } elseif (xread32($slot + 0x40) != -1 && xread8($slot + 0x40) != 0) {            $target = ($target & ~0xff) | xread8($slot + 0x40);            xwrite32($slot + 0x48, $target);            xwrite32($slot + 0x40, $target);            xwrite32($slot + 0x50, $rec + 0xc);            goto stepping;        }        $target = s32($target + xread32($slot + 0x44));        xwrite32($slot + 0x48, $target);        xwrite32($slot + 0x40, $target);        $cnt = xread16($rec + 8) & 0xffff;        $esi = $rec + 0xc;        $frames = ($target >> 8) - 1;        if ($frames > 0) {            for (;;) {                $op = xread8($esi);                if ($op == 0x0b) {                    break;                }                $esi += 0x10;                if ($op == 0x05) {                    if (--$frames == 0) {                        break;                    }                } elseif (--$cnt == 0) {                    break;                }            }        }        xwrite32($slot + 0x50, $esi);    }    stepping:    $esi = xread32($slot + 0x50);    for (;;) {        $cur = xread32($slot + 0x40) >> 8;        if ($cur == (xread32($slot + 0x48) >> 8)) {            xwrite32($slot + 0x40, xread32($slot + 0x48));            putlist_v6($slot, $esi, 0x7fffffff, $esi);            if (xread8($slot + 0x40) == 0) {                xwrite32($slot + 0x50, $esi);            }            break;        }        xwrite32($slot + 0x40, shl32($cur + 1, 8));        putlist_v6($slot, $esi, 0x7fffffff, $esi);        xwrite32($slot + 0x50, $esi);        if (xread32($slot + 0x48) == xread32($slot + 0x40)) {            break;        }    }    epilogue:    $acc = s32(xread32($slot + 0x48) + xread32($slot + 0x44));    xwrite32($slot + 0x48, $acc);    $ret = $acc;    if ($acc < 0) {        $ret = xread32($slot + 0x4c);    }    if (ult(xread32($slot + 0x4c), $ret)) {        if (xread8($slot + 0x40) != 0) {            xwrite32($slot + 0x40, $ret);            $cursor = xread32($slot + 0x50);            if ($cursor != 0) {                v6anim_park($cursor);            }        }        $ret = 0;    }    return $ret;}function put_v6(int $elem): void{    $p = ImageV6Vars::$v6put;    $vram = Alis::$script->vram_org;    $p->parent = $vram;    $p->numcreated = 0;    $p->aborted = 0;    if (v6sp_count($elem) != 0) {        $prev = v6el_prev($elem);        $next = v6el_next($elem);        v6el_set_next($prev, $next);        v6el_set_prev($next, $prev);    }    $sp = $elem;    if ($p->fadddes) {        $count = v6sp_count($elem);        $p->numcreated = $count;        $sp += $count * C::V6_SLOT_STRIDE;    }    $sp = putin_v6($sp);    $leftover = s16(v6sp_count($elem) - $p->numcreated);    if ($leftover > 0) {        for ($i = 0; $i < $leftover; $i++) {            killsprite_v6($sp);            $sp += C::V6_SLOT_STRIDE;        }    }    v6sp_set_count($elem, $p->numcreated);    if ($p->numcreated != 0) {        $head = v6_elemhead($vram);        $first = v6el_next($head);        v6el_set_next($head, $elem);        v6el_set_prev($first, $elem);        v6el_set_prev($elem, $head);        v6el_set_next($elem, $first);    }}function killsprite_v6(int $sp): void{    $prev = v6sp_prev($sp);    $next = v6sp_next($sp);    if ($prev != 0) {        xwrite32($prev + 0x38, $next);    }    if ($next != 0) {        xwrite32($next + 0x34, $prev);    }    $state = v6sp_state($sp);    $flags = v6sp_flags($sp);    if (Alis::$ferase && !($flags & 0x80) && ($state == 0x32 || $state == 0x34 || $state == 0x38 || $state == 0x39)) {        $world = Alis::$basemain + v6sp_screen($sp);        for ($id = Scr::$ptscreen & 0xffff; $id != 0; $id = xread16(Alis::$basemain + $id + 0x5c) & 0xffff) {            $s = Alis::$basemain + $id;            if (xread32($s + 0x5e) == $world && !(xread8($s + 0x58) & 0x40)) {                v6dirty_sprite($s, $sp);            }        }    }    xwrite16($sp + 0x2e, 0);    v6sp_set_state($sp, 0);    v6sp_set_prev($sp, 0);    v6sp_set_next($sp, 0);}function eraselem_v6(int $elem): void{    $count = v6sp_count($elem);    if ($count != 0) {        $prev = v6el_prev($elem);        $next = v6el_next($elem);        $ram = Alis::$platform->ram_sz;        if ($prev == 0 || !ult($prev, $ram) || $next == 0 || !ult($next, $ram)) {            // elem has corrupt links: skipping unlink (C debug message)        } else {            v6el_set_next($prev, $next);            v6el_set_prev($next, $prev);        }        $sp = $elem;        for ($i = 0; $i < $count; $i++) {            killsprite_v6($sp);            $sp += C::V6_SLOT_STRIDE;        }        v6sp_set_count($elem, 0);    }}function killallelem_v6(int $vram): void{    $head = v6_elemhead($vram);    $e = $head;    $ram = Alis::$platform->ram_sz;    for ($guard = 0; $guard < C::MAX_SCRIPTS * 64; $guard++) {        $e = v6el_next($e);        if ($e == 0 || !ult($e, $ram)) {            // elem chain left memory: aborting (C debug message)            return;        }        if (v6el_next($e) == $e) {            return;        }        eraselem_v6($e);    }    // elem chain did not terminate: aborting (C debug message)}function rangesprite_v6(int $elemidx1, int $head): void{    $sprite1 = sprite_var($elemidx1);    // unlink elemidx1 from its current position    $p = $sprite1->prev;    $n = $sprite1->link;    if ($p != 0) {        sprite_var($p)->link = $n;    }    if ($n != 0) {        sprite_var($n)->prev = $p;    }    $newd1 = $sprite1->newd;    $cordspr1 = s8($sprite1->cordspr);   // C: s8 cordspr1 = (u8) field; compared with the u8 fields    $numelem1 = s8($sprite1->numelem);    if (Img::$wback != 0 && $elemidx1 != Img::$backsprite && Img::$backprof <= $newd1) {        Img::$pback = 1;    }    // find the insertion point    $previdx = $head;    $sprite3 = sprite_var($head);    while ($sprite3->link != 0) {        $li = $sprite3->link;        $linksprite = sprite_var($li);        $lcord = $linksprite->cordspr & 0xff;        if (-1 < $linksprite->state && $linksprite->newd <= $newd1 &&            ($linksprite->newd < $newd1 ||            ($lcord <= $cordspr1 &&            ($lcord < $cordspr1 ||            ($linksprite->numelem <= $numelem1 &&            ($linksprite->numelem < $numelem1 || -1 == $sprite1->state)))))) {            break;        }        $previdx = $li;        $sprite3 = $linksprite;    }    // insert elemidx1 between previdx and its successor, maintaining prev links    $sprite1->state = 0;    $succ = $sprite3->link;    $sprite1->link = $succ;    $sprite1->prev = $previdx & 0xffff;    $sprite3->link = $elemidx1 & 0xffff;    if ($succ != 0) {        sprite_var($succ)->prev = $elemidx1 & 0xffff;    }}// ---------------------------------------------------------------------------------------------// v6 dirty-rectangle list// ---------------------------------------------------------------------------------------------function v6dirty_reset(): void{    ImageV6Vars::$v6dirty_tail = 0;}function v6dirty_count(): int{    return ImageV6Vars::$v6dirty_tail;}function v6dirty_set_window(int $x1, int $y1, int $x2, int $y2): void{    ImageV6Vars::$v6win_x1 = s16($x1);    ImageV6Vars::$v6win_y1 = s16($y1);    ImageV6Vars::$v6win_x2 = s16($x2);    ImageV6Vars::$v6win_y2 = s16($y2);}function v6clip(int $x1, int $y1, int $x2, int $y2): void{    $x1 = s16($x1);    $y1 = s16($y1);    $x2 = s16($x2);    $y2 = s16($y2);    ImageV6Vars::$v6clip_valid = 0;    if ($x1 > ImageV6Vars::$v6win_x2) {        return;    }    if ($y1 > ImageV6Vars::$v6win_y2) {        return;    }    if ($x2 < ImageV6Vars::$v6win_x1) {        return;    }    if ($y2 < ImageV6Vars::$v6win_y1) {        return;    }    if ($x1 < ImageV6Vars::$v6win_x1) {        $x1 = ImageV6Vars::$v6win_x1;    }    if ($y1 < ImageV6Vars::$v6win_y1) {        $y1 = ImageV6Vars::$v6win_y1;    }    if ($x2 > ImageV6Vars::$v6win_x2) {        $x2 = ImageV6Vars::$v6win_x2;    }    if ($y2 > ImageV6Vars::$v6win_y2) {        $y2 = ImageV6Vars::$v6win_y2;    }    $x1 = s16($x1 & 0xfff0);    $x2 = s16($x2 | 0x000f);    if ($x1 < 0) {        $x1 = 0;    }    if ($x2 >= Alis::$platform->width) {        $x2 = s16(Alis::$platform->width - 1);    }    ImageV6Vars::$v6clip_x1 = $x1;    ImageV6Vars::$v6clip_y1 = $y1;    ImageV6Vars::$v6clip_x2 = $x2;    ImageV6Vars::$v6clip_y2 = $y2;    ImageV6Vars::$v6clip_w = s16(($x2 - $x1) + 1);    ImageV6Vars::$v6clip_h = s16(($y2 - $y1) + 1);    ImageV6Vars::$v6clip_valid = 1;}/** C: (u32)(u16)a * (u16)b (can exceed 2^31: a float on 32-bit PHP, only compared). */function v6dirty_area(int $a, int $b): int|float{    return ($a & 0xffff) * ($b & 0xffff);}function v6dirty_add(int $x1, int $y1, int $x2, int $y2, int $id, int $scene): void{    $id = s16($id);    $scene &= 0xffff;    v6clip($x1, $y1, $x2, $y2);    if (!ImageV6Vars::$v6clip_valid) {        return;    }    $nx1 = ImageV6Vars::$v6clip_x1;    $ny1 = ImageV6Vars::$v6clip_y1;    $nx2 = ImageV6Vars::$v6clip_x2;    $ny2 = ImageV6Vars::$v6clip_y2;    $tail = ImageV6Vars::$v6dirty_tail;    $dl = &ImageV6Vars::$v6dirty;    $target = $tail;    if ($id > 0) {        for ($i = 0; $i < $tail; $i++) {            if ($dl[$i]['id'] == $id) {                $target = $i;                break;            }        }    } elseif ($tail > 0) {        $newarea = v6dirty_area($nx2 - $nx1, $ny2 - $ny1);        $best = 4294967295.0;   // 0xffffffffu        for ($i = 0; $i < $tail; $i++) {            $d = $dl[$i];            if ($d['id'] == ImageV6Const::V6_DIRTY_3D) {                continue;            }            $ux1 = $nx1 < $d['x1'] ? $nx1 : $d['x1'];            $ux2 = $nx2 > $d['x2'] ? $nx2 : $d['x2'];            $uy1 = $ny1 < $d['y1'] ? $ny1 : $d['y1'];            $uy2 = $ny2 > $d['y2'] ? $ny2 : $d['y2'];            $uarea = v6dirty_area($ux2 - $ux1, $uy2 - $uy1);            // PORT: C u32 sum area + newarea wraps; no wrap here (areas of a screen never get near 2^32)            if ($uarea < $best && $uarea <= $d['area'] + $newarea) {                $best = $uarea;                $target = $i;            }        }    }    if ($target != $tail) {        $d = $dl[$target];        if ($d['x1'] < $nx1) {            $nx1 = $d['x1'];        }        if ($d['x2'] > $nx2) {            $nx2 = $d['x2'];        }        if ($d['y1'] < $ny1) {            $ny1 = $d['y1'];        }        if ($d['y2'] > $ny2) {            $ny2 = $d['y2'];        }    }    $dl[$target] = [        'x1' => $nx1, 'y1' => $ny1, 'x2' => $nx2, 'y2' => $ny2,        'area' => v6dirty_area($nx2 - $nx1, $ny2 - $ny1),        'id' => $id, 'scene' => $scene,    ];    if ($target == $tail) {        if ($tail < ImageV6Const::V6_DIRTY_MAX - 1) {            ImageV6Vars::$v6dirty_tail++;        } else {            ImageVars::$v6xdirty = 1;        }    }}// sprite bounds clipped to its screenfunction v6dirty_sprite(int $s, int $sp): void{    v6dirty_set_window(xread16($s + 0x66), xread16($s + 0x68), xread16($s + 0x6a), xread16($s + 0x6c));    v6dirty_add(xread16($sp + 0x44), xread16($sp + 0x46), xread16($sp + 0x48), xread16($sp + 0x4a), xread16($sp + 0x4e), ($s - Alis::$basemain) & 0xffff);}// 3D views add their whole clip, unaligned and never merged intofunction v6dirty_add3d(int $s): void{    if (ImageV6Vars::$v6dirty_tail >= ImageV6Const::V6_DIRTY_MAX - 1) {        ImageVars::$v6xdirty = 1;        return;    }    $x1 = xread16($s + 0x66);    $y1 = xread16($s + 0x68);    $x2 = xread16($s + 0x6a);    $y2 = xread16($s + 0x6c);    ImageV6Vars::$v6dirty[ImageV6Vars::$v6dirty_tail++] = [        'x1' => $x1, 'y1' => $y1, 'x2' => $x2, 'y2' => $y2,        'area' => v6dirty_area($x2 - $x1, $y2 - $y1),        'id' => ImageV6Const::V6_DIRTY_3D,        'scene' => ($s - Alis::$basemain) & 0xffff,    ];}// ---------------------------------------------------------------------------------------------// affiscr_v6 (C: not called anywhere, kept for completeness), v6ips_measure// ---------------------------------------------------------------------------------------------function affiscr_v6(int $scene, int $screenidx): void{    $scene &= 0xffff;    $screenidx &= 0xffff;    $spriteidx = $screenidx;    $prevspidx = 0;    $linkidx = 0;   // u16    if (Alis::$platform->version >= 30 && xread8(Alis::$basemain + $scene + 0x84) != 0) {        folscreen($scene);    }    if ((Img::$fremap != 0) || (s8(get_scr_state($scene)) < 0)) {        depscreen($scene, $screenidx);    }    $draw = false;    if (Alis::$platform->version == 0) {        $draw = Alis::$fswitch == 0;    } elseif ((get_scr_numelem($scene) & 2) == 0) {        $draw = true;    } else {        affiland($scene);        $draw = (get_scr_state($scene) & 0x80) == 0;    }    if ($draw) {        Img::$wback = (int) ((get_scr_numelem($scene) & 4) != 0);        Img::$wpag = 0;        if ((get_scr_state($scene) & 0x20) != 0) {            Img::$wpag = 1;            Img::$spag = s8(Img::$spag - 1);            if (Img::$spag == 0) {                Img::$wpag = -1;            }        }        if ((get_scr_numelem($scene) & 0x10) == 0) {            while (true) {                $prevspidx = $spriteidx;                $spriteidx = sprite_var($spriteidx)->link;                if ($spriteidx == 0) {                    break;                }                $state = sprite_var($spriteidx)->state;                if ($state != 0) {                    Img::$joints = 0;                    Img::$pback = 0;                    if ($state == 2) {                        $linkidx = inilink($spriteidx) & 0xffff;                        if ($linkidx >= 0) { // always true: linkidx is u16 in the C                            $spriteidx = iremplink($scene, $spriteidx, $prevspidx, $screenidx);                            suitlin1($scene, $spriteidx, $prevspidx, $screenidx, $linkidx);                        } else {                            $sprite = sprite_var($spriteidx);                            // isback: only its pback side effect matters                            if (Img::$wback != 0 && Img::$backprof <= $sprite->newd) {                                Img::$pback = 1;                            }                            deptopix($scene, $spriteidx);                            tstjoints($spriteidx);                            sprite_var($prevspidx)->link = $sprite->link;                            if (Img::$joints == 0) {                                fenetre($scene, $spriteidx, $screenidx, $prevspidx);                            }                            image_store_new($sprite);                            $prevspidx = rangesprite($spriteidx, $prevspidx, $screenidx);                        }                    } else {                        $linkidx = inilink($spriteidx) & 0xffff;                        if ($state == -1) {                            $spriteidx = inouvlink($scene, $spriteidx, $prevspidx, $screenidx);                        } else {                            $spriteidx = iefflink($spriteidx, $prevspidx);                        }                        if ($linkidx < 0) { // never true: linkidx is u16 in the C                            Img::$joints = 1;                        } else {                            suitlin1($scene, $spriteidx, $prevspidx, $screenidx, $linkidx);                        }                    }                    fenetre($scene, $spriteidx, $screenidx, $prevspidx);                    $spriteidx = $prevspidx;                }            }        } else {            Img::$fenx1 = get_scr_newx($scene);            Img::$feny1 = get_scr_newy($scene);            Img::$fenx2 = s16(Img::$fenx1 + get_scr_width($scene));            Img::$feny2 = s16(Img::$feny1 + get_scr_height($scene));            Img::$clipl = s16((Img::$fenx2 - Img::$fenx1) + 1);            Img::$cliph = s16((Img::$feny2 - Img::$feny1) + 1);            Img::$clipx1 = Img::$fenx1;            Img::$clipy1 = Img::$feny1;            Img::$clipx2 = Img::$fenx2;            Img::$clipy2 = Img::$feny2;            if ((get_scr_numelem($scene) & 0x40) == 0) {                clrfen();            }            while (true) {                $prevspidx = $spriteidx;                $spriteidx = sprite_var($spriteidx)->link;                if ($spriteidx == 0) {                    break;                }                $psprite = sprite_var($prevspidx);                $csprite = sprite_var($spriteidx);                if ($csprite->state != 0) {                    if ($csprite->state == 1) {                        $psprite->link = $csprite->link;                        $csprite->to_next = Img::$libsprit;                        Img::$libsprit = $spriteidx;                        $spriteidx = $prevspidx;                    } else {                        deptopix($scene, $spriteidx);                        $psprite->link = $csprite->link;                        image_store_new($csprite);                        $spriteidx = rangesprite($spriteidx, $prevspidx, $screenidx);                    }                }            }            image_affiscr_screens(Scr::$ptscreen & 0xffff);            if ((Alis::$fswitch == 0) && (Img::$wpag == 0)) {                fentotv();            }        }    } else {        image_affiscr_screens(get_scr_to_next($scene) & 0xffff);        if ((Alis::$fswitch == 0) && (Img::$wpag == 0)) {            fentotv();        }    }    if (Img::$wpag < 0) {        set_scr_state($scene, get_scr_state($scene) & 0xdf);        Img::$fenx1 = get_scr_newx($scene);        Img::$feny1 = get_scr_newy($scene);        Img::$fenx2 = s16(get_scr_newx($scene) + get_scr_width($scene));        Img::$feny2 = s16(get_scr_newy($scene) + get_scr_height($scene));        scrolpage();    }    // TODO: fix this hack    // following 'if ()' is needed to draw rails under train on minimap in Transarctica    if (Alis::$platform->uid != C::EGameTransarctica) {        set_scr_state($scene, get_scr_state($scene) & 0x7f);    }}function v6ips_measure(): void{    $now = Alis::$timeclock;    $slot = ImageV6Vars::$v6ips_slot;    $el = s32($now - ImageV6Vars::$v6ips_ring[$slot]);    if ($el == 0) {        $el = 1;    }    ImageV6Vars::$v6ips_ring[$slot] = $now;    ImageV6Vars::$v6ips_slot = ($slot + 1) & 7;    if (ImageVars::$v6ips_warm != 0) {        ImageVars::$v6ips_warm--;    } else {        Alis::$v6ips = s32(udiv32(0x3c << 3, $el) + 1);    }}// ---------------------------------------------------------------------------------------------// v6x renderer// ---------------------------------------------------------------------------------------------function v6x_depscreen(int $s): void{    $node = xread32($s + 0x5e);    if ($node == 0) {        return;    }    $sp = xread32($node + 0x38);    while ($sp != 0 && $sp != xread32($sp + 0x38)) {        v6sp_set_flags($sp, v6sp_flags($sp) | 1);        $sp = xread32($sp + 0x38);    }}function v6x_deptopix(int $s, int $sp): void{    ImageV6Vars::$v6xdrawinv = v6sp_invert($sp);    $x = v6sp_depx($sp);    $y = v6sp_depy($sp);    $z = v6sp_depz($sp);    for ($p = v6sp_parent($sp); $p != 0; $p = v6sp_parent($p)) {        $x = s32($x + v6sp_depx($p));        $y = s32($y + v6sp_depy($p));        $z = s32($z + v6sp_depz($p));    }    $x = s32($x - xread32($s + 0x0c));    $y = s32($y - xread32($s + 0x10));    $z = s32($z - xread32($s + 0x14));    $ax = s16($x);    $bx = s16($y);    $cx = s16($z);    switch (xread8($s + 0x5a)) {        case 1:            $t = $cx;            $cx = $bx;            $bx = s16(-$t);            break;        case 2:            $bx = s16(-$bx);            $cx = s16(-$cx);            break;        case 3:            $t = $cx;            $cx = $bx;            $bx = s16(-$t - $bx);            break;        case 4:            $t = $cx;            $cx = s16($ax + $bx);            $ax = s16($ax - $bx);            $bx = s16(-($t + $cx));            break;        default:            break;    }    xwrite16($sp + 0x4c, $cx);    $scale = v6sp_scale($sp);    if ($scale != 0x100) {        $ax = s16(($ax * s16($scale)) >> 8);        $bx = s16(($bx * s16($scale)) >> 8);    }    $ax = s16($ax + xread16($s + 0x62));    $bx = s16($bx + xread16($s + 0x64));    $st = v6sp_state($sp);    if ($st == 0x38) {        xwrite16($sp + 0x44, $ax);        xwrite16($sp + 0x46, $bx);        xwrite16($sp + 0x48, $ax);        xwrite16($sp + 0x4a, $bx);        $th = 0;        $tw = v6x_textwalk($sp, 0, $th);        xwrite16($sp + 0x40, $tw);        xwrite16($sp + 0x42, $th);        $align = xread8($sp + 0x52);        if ($align != 0) {            $ax = s16($ax - (($align == 0xff) ? $tw : s16(intdiv($tw, 2))));            xwrite16($sp + 0x44, $ax);            xwrite16($sp + 0x48, $ax);        }        xwrite16($sp + 0x48, s16($ax + $tw - 1));        xwrite16($sp + 0x4a, s16($bx + $th - 1));        return;    }    if ($st == 0x39 || $st == 0x7f) {        $w = xread16($sp + 0x40);        $h = xread16($sp + 0x42);    } else {        $d = v6sp_desc($sp);        if ($d == 0) {            $w = 0;            $h = 0;            xwrite16($sp + 0x40, 0);            xwrite16($sp + 0x42, 0);        } else {            $descr = s32($d + xread32($d));            if ($scale != 0x100) {                $sc = s16($scale);                $ax = s16($ax - s16((xread16($descr + 0x14) * $sc) >> 8));                $bx = s16($bx - s16((xread16($descr + 0x16) * $sc) >> 8));                $w = s16((xread16($descr + 0x10) * $sc) >> 8);                $h = s16((xread16($descr + 0x12) * $sc) >> 8);            } else {                $ax = s16($ax - xread16($descr + 0x14));                $bx = s16($bx - xread16($descr + 0x16));                $w = xread16($descr + 0x10);                $h = xread16($descr + 0x12);            }            xwrite16($sp + 0x40, $w);            xwrite16($sp + 0x42, $h);        }    }    xwrite16($sp + 0x44, $ax);    xwrite16($sp + 0x48, s16($ax + $w - 1));    xwrite16($sp + 0x46, $bx);    xwrite16($sp + 0x4a, s16($bx + $h - 1));}function v6x_rangesprite(int $s, int $sp): void{    $prev = v6sp_prev($sp);    $next = v6sp_next($sp);    xwrite32($next + 0x34, $prev);    xwrite32($prev + 0x38, $next);    $key = xread16($sp + 0x4c);    $ord = s8(xread8($sp + 0x32));    $cand = xread32($s + 0x5e);    for (;;) {        $before = $cand;        $cand = xread32($cand + 0x38);        if ($cand == xread32($cand + 0x38)) {            break;        }        if (v6sp_flags($cand) & 0x80) {            continue;        }        $ct = v6sp_state($cand);        if ($ct != 0x32 && $ct != 0x38 && $ct != 0x39 && $ct != 0x34 && $ct != 0x7f) {            continue;        }        $ckey = xread16($cand + 0x4c);        if ($key < $ckey) {            continue;        }        if ($key > $ckey) {            break;        }        $cord = s8(xread8($cand + 0x32));        if ($ord < $cord) {            continue;        }        if ($ord > $cord) {            break;        }        if (ult($sp, $cand)) {            continue;        }        break;    }    xwrite32($before + 0x38, $sp);    xwrite32($cand + 0x34, $sp);    v6sp_set_prev($sp, $before);    v6sp_set_next($sp, $cand);}/** C: host.pixelbuf.palette[idx] | 0xff000000 (palette = image.mpalet). Returns 0xRRGGBB (no alpha). */function pal_argb(int $idx): int{    return (Img::$mpalet[$idx & 0xff] ?? 0) & 0xffffff;}/** 3DO 15-bit colour -> 0xRRGGBB (C: | 0xff000000 alpha, dropped). */function rgb555(int $c): int{    $r = ($c >> 10) & 0x1f;    $g = ($c >> 5) & 0x1f;    $b = $c & 0x1f;    return (($r << 3 | $r >> 2) << 16) | (($g << 3 | $g >> 2) << 8) | ($b << 3 | $b >> 2);}function v6x_glyphblit(int $sp, int $glyph, int $x, int $y): void{    $gw = xread16($glyph + 0x10);    $gh = xread16($glyph + 0x12);    if ($gw <= 0 || $gh <= 0) {        return;    }    $W = Alis::$platform->width;    $H = Alis::$platform->height;    $c1 = ImageV6Vars::$v6xclipx1;    $c2 = ImageV6Vars::$v6xclipy1;    $c3 = ImageV6Vars::$v6xclipx2;    $c4 = ImageV6Vars::$v6xclipy2;    $cx1 = $x < $c1 ? $c1 : $x;    $cy1 = $y < $c2 ? $c2 : $y;    $cx2 = ($x + $gw - 1) > $c3 ? $c3 : ($x + $gw - 1);    $cy2 = ($y + $gh - 1) > $c4 ? $c4 : ($y + $gh - 1);    if ($cx1 < 0) {        $cx1 = 0;    }    if ($cy1 < 0) {        $cy1 = 0;    }    if ($cx2 >= $W) {        $cx2 = $W - 1;    }    if ($cy2 >= $H) {        $cy2 = $H - 1;    }    if ($cx1 > $cx2 || $cy1 > $cy2) {        return;    }    $opaque = xread8($glyph + 0x1b) & 1;    $flip = xread8($sp + 0x25) & 1;    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        cel_blit($glyph, $x, $y, $x + $gw - 1, $cx1, $cy1, $cx2, $cy2, $flip);        return;    }    // rows of the glyph: sx = flip ? (x + gw - 1 - xx) : (xx - x) = index (xx - x) of the reversed row    $src = $glyph + 0x20;    $n = $cx2 - $cx1 + 1;    $rows = [];    $rgb = Img::$rgblogic != 0;    $map = $rgb ? v6_pal_bgr_map() : null;    $pitch = $rgb ? $W * 3 : $W;    $dst0 = $rgb ? Img::$rgblogic + ($cy1 * $W + $cx1) * 3 : Img::$logic + $cy1 * $W + $cx1;    for ($yy = $cy1; $yy <= $cy2; $yy++) {        $row = substr(Alis::$mem, $src + ($yy - $y) * $gw, $gw);        if ($flip) {            $row = strrev($row);        }        $seg = substr($row, $cx1 - $x, $n);        $d = $dst0 + ($yy - $cy1) * $pitch;        if ($rgb) {            $px = strtr($seg, $map);            if (!$opaque) {                $cur = substr(Alis::$mem, $d, $n * 3);                for ($i = 0; $i < $n; $i++) {                    if ($seg[$i] !== "\0") {                        $cur[$i * 3] = $px[$i * 3];                        $cur[$i * 3 + 1] = $px[$i * 3 + 1];                        $cur[$i * 3 + 2] = $px[$i * 3 + 2];                    }                }                $px = $cur;            }            $rows[] = $px;        } else {            $rows[] = $opaque ? $seg : v6_merge8(substr(Alis::$mem, $d, $n), $seg);        }    }    v6_rect_put($dst0, $pitch, $rows);}/** $outh: C s16 *outh (NULL = not wanted: pass nothing). */function v6x_textwalk(int $sp, int $draw, ?int &$outh = null): int{    $fontno = xread8($sp + 0x51);    $f = $fontno < 64 ? (Alis::$v6xfonts[$fontno] ?? null) : null;    if ($f === null || empty($f['valid'])) {        $outh = 0;        return 8;    }    $first = $f['foasc'];    $count = $f['count'];    $spacing = $f['spacing'];    $tab = $f['glyphslot'];    $propor = ($f['id'] == 2);    $x0 = xread16($sp + 0x44);    $x = $x0;    $y = xread16($sp + 0x46);    $str = s32($sp + xread32($sp + 0x3c));    for (;;) {        $c = xread8($str++);        if ($c == 0) {            break;        }        if ($c < 0x20) {            continue;        }        if ($c != 0x20) {            $idx = $c - $first;            if ($idx >= 0 && $idx < $count) {                if ($tab == 0) {                    break;                }                $entry = $tab + $idx * 4;                $glyph = s32($entry + xread32($entry));                if (xread8($glyph) == 1 && $draw) {                    v6x_glyphblit($sp, $glyph, $x, $y);                }                if ($propor) {                    $x = s16($x + xread16($glyph + 0x10) - xread8($glyph + 0x1a));                }            }        }        $x = s16($x + s16($spacing));    }    $outh = s16($f['height']);    return s16($x - $x0 + 8);}/** C: v6x_textput(const u8 *str): $str = the string as a PHP string (callers: cvputprint, csputprint). */function v6x_textput(string $str): void{    $p = ImageV6Vars::$v6put;    $elem = $p->basesprite;    if (s32($elem) <= 0) {        return;    }    $limit = $p->limit;    $nul = strpos($str, "\0");    if ($nul !== false) {        $str = substr($str, 0, $nul);    }    $bytes = $str . "\0";    $len = strlen($bytes);    if (xread32($elem + 0x3c) != 0) {        $dst = s32($elem + xread32($elem + 0x3c));        while (xread8($dst) != 0) {            $dst++;        }    } else {        $dst = $elem + 0x53;    }    $room = s32($limit - $dst);    if ($room <= 0) {        // elem buffer full (perror 0x25)        return;    }    if ($room < $len) {        // string truncated (perror 0x25)        $len = $room;    }    for ($i = 0; $i < $len; $i++) {        Alis::$mem[$dst + $i] = $bytes[$i];    }    $p->ftext = 1;    $p->desc = 0x53;    put_v6($elem);    $p->ftext = 0;    $p->desc = 0;}function v6x_affiscr(int $s): void{    xwrite16($s + 0x20, 0);    if (xread8($s + 0x80) != 0) {        v6x_folscreen($s);    }    if (xread8($s + 0x58) & 0x80) {        v6x_depscreen($s);    }    if (xread8($s + 0x5a) == 6) {        v6x_mode6($s);        v6dirty_add3d($s);        xwrite8($s + 0x58, xread8($s + 0x58) & 0x7f);        return;    }    if (xread8($s + 0x5a) == 5 && Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        // empty view is repainted once, not every frame        r3do_land($s);        $empty = r3do_empty() ? 1 : 0;        if (!$empty || !ImageV6Vars::$affiscr_lastempty || $s != ImageV6Vars::$affiscr_lastscr || (xread8($s + 0x58) & 0x80)) {            v6dirty_add3d($s);        }        ImageV6Vars::$affiscr_lastscr = $s;        ImageV6Vars::$affiscr_lastempty = $empty;        xwrite8($s + 0x58, xread8($s + 0x58) & 0x7f);        return;    }    ImageV6Vars::$v6xclipx1 = xread16($s + 0x66);    ImageV6Vars::$v6xclipy1 = xread16($s + 0x68);    ImageV6Vars::$v6xclipx2 = xread16($s + 0x6a);    ImageV6Vars::$v6xclipy2 = xread16($s + 0x6c);    $node = xread32($s + 0x5e);    if ($node != 0) {        $sp = xread32($node + 0x38);        for (;;) {            $cur = $sp;            if ($cur == 0) {                break;            }            $sp = xread32($cur + 0x38);            if ($cur == $sp) {                break;            }            $fl = v6sp_flags($cur);            if (($fl & 0x81) == 0) {                continue;            }            if (v6sp_state($cur) == 0x35) {                continue;            }            if (xread8($cur + 0x24) & 0x40) {                continue;            }            if (v6sp_state($cur) == 0x20) {                v6snd_step($cur);                continue;            }            if (!($fl & 0x80)) {                v6dirty_sprite($s, $cur);            }            v6x_deptopix($s, $cur);            v6x_rangesprite($s, $cur);            v6dirty_sprite($s, $cur);            v6sp_set_flags($cur, v6sp_flags($cur) & 0x7e);        }    }    xwrite8($s + 0x58, xread8($s + 0x58) & 0x7f);}function rgb_fillrect(int $x1, int $y1, int $x2, int $y2, int $c): void{    if ($x2 < $x1 || $y2 < $y1) {        return;    }    $W = Alis::$platform->width;    $row = str_repeat(rgb_bgr($c), $x2 - $x1 + 1);    v6_rect_put(Img::$rgblogic + ($y1 * $W + $x1) * 3, $W * 3, array_fill(0, $y2 - $y1 + 1, $row));}/** * One CEL row (3DO packed: 2-bit type + 6-bit count per run). $p, $end: offsets in Alis::$mem; * $plut: 32 colours or null; writes 0x8000 | colour into $px[$pxoff + x]. */function cel_row(int $p, int $end, ?array $plut, array &$px, int $pxoff, int $w): void{    $m = Alis::$mem;    $x = 0;    while ($x < $w && $p < $end) {        $c = ord($m[$p++]);        $n = ($c & 0x3f) + 1;        $type = $c >> 6;        if ($type == 0) {            return;        }        if ($type == 2) {            $x += $n;            continue;        }        for ($i = 0; $i < $n && $x < $w; $i++) {            $v = $plut !== null ? $plut[ord($m[$p]) & 0x1f] : ((ord($m[$p]) << 8) | ord($m[$p + 1]));            $px[$pxoff + $x++] = 0x8000 | $v;            if ($type == 1 || $i == $n - 1) {                $p += $plut !== null ? 1 : 2;            }        }    }}/** * C: u16 *cel_decode(u32 descr, s32 *pw, s32 *ph): a flat PHP array of w * h u16 pixels (index * y * w + x; 0 = transparent, opaque pixels have bit 0x8000), null when w or h <= 0. */function cel_decode(int $descr, int &$pw, int &$ph): ?array{    $base = $descr;    $w = s16(read16($base + 0x10));    $h = s16(read16($base + 0x12));    $pw = $w;    $ph = $h;    if ($w <= 0 || $h <= 0) {        return null;    }    $flags = xread8($base + 0x1b);    $plut = null;    $row = $base + 0x20;    if ($flags & 2) {        $plut = [];        for ($i = 0; $i < 32; $i++) {            $plut[$i] = (xread8($row + $i * 2) << 8) | xread8($row + $i * 2 + 1);        }        $row += 64;    }    $end = s32($base + read32($base + 4));    $px = array_fill(0, $w * $h, 0);    for ($y = 0; $y < $h && $row < $end; $y++) {        $next = $row + (((((xread8($row) << 8) | xread8($row + 1)) & 0x3ff) + 2) * 4);        cel_row($row + 2, $next, $plut, $px, $y * $w, $w);        $row = $next;    }    return $px;}// decodes only the clipped rows, one at a timefunction cel_blit(int $descr, int $x1, int $y1, int $x2, int $cx1, int $cy1, int $cx2, int $cy2, int $flip): void{    $base = $descr;    $w = s16(read16($base + 0x10));    $h = s16(read16($base + 0x12));    if ($w <= 0 || $h <= 0) {        return;    }    $flags = xread8($base + 0x1b);    $plut = null;    $row = $base + 0x20;    if ($flags & 2) {        $plut = [];        for ($i = 0; $i < 32; $i++) {            $plut[$i] = (xread8($row + $i * 2) << 8) | xread8($row + $i * 2 + 1);        }        $row += 64;    }    $end = s32($base + read32($base + 4));    $W = Alis::$platform->width;    $zero = array_fill(0, $w, 0);    $m = &Alis::$mem;    for ($y = 0; $y < $h && $row < $end && $y1 + $y <= $cy2; $y++) {        $next = $row + (((((xread8($row) << 8) | xread8($row + 1)) & 0x3ff) + 2) * 4);        $yy = $y1 + $y;        if ($yy >= $cy1) {            $rb = $zero;            cel_row($row + 2, $next, $plut, $rb, 0, $w);            $d = Img::$rgblogic + $yy * $W * 3;            for ($xx = $cx1; $xx <= $cx2; $xx++) {                $sx = $flip ? ($x2 - $xx) : ($xx - $x1);                if ($sx >= 0 && $sx < $w && $rb[$sx]) {                    $c = rgb555($rb[$sx] & 0x7fff);                    $o = $d + $xx * 3;                    $m[$o] = chr($c & 0xff);                    $m[$o + 1] = chr(($c >> 8) & 0xff);                    $m[$o + 2] = chr(($c >> 16) & 0xff);                }            }        }        $row = $next;    }}// solid fill of the current clip window in the logic bufferfunction v6x_fill(int $col): void{    $W = Alis::$platform->width;    $H = Alis::$platform->height;    $x1 = ImageV6Vars::$v6xclipx1 < 0 ? 0 : ImageV6Vars::$v6xclipx1;    $y1 = ImageV6Vars::$v6xclipy1 < 0 ? 0 : ImageV6Vars::$v6xclipy1;    $x2 = ImageV6Vars::$v6xclipx2 >= $W ? $W - 1 : ImageV6Vars::$v6xclipx2;    $y2 = ImageV6Vars::$v6xclipy2 >= $H ? $H - 1 : ImageV6Vars::$v6xclipy2;    if (Img::$rgblogic) {        rgb_fillrect($x1, $y1, $x2, $y2, pal_argb($col));        return;    }    if ($x2 < $x1 || $y2 < $y1) {        return;    }    v6_rect_put(Img::$logic + $y1 * $W + $x1, $W, array_fill(0, $y2 - $y1 + 1, str_repeat(chr($col & 0xff), $x2 - $x1 + 1)));}// blit sprite into logicfunction v6x_blit(int $sp): void{    $x1 = xread16($sp + 0x44);    $y1 = xread16($sp + 0x46);    $x2 = xread16($sp + 0x48);    $y2 = xread16($sp + 0x4a);    $k1 = ImageV6Vars::$v6xclipx1;    $k2 = ImageV6Vars::$v6xclipy1;    $k3 = ImageV6Vars::$v6xclipx2;    $k4 = ImageV6Vars::$v6xclipy2;    if ($x1 > $k3 || $y1 > $k4 || $x2 < $k1 || $y2 < $k2) {        return;    }    $W = Alis::$platform->width;    $H = Alis::$platform->height;    $cx1 = $x1 < $k1 ? $k1 : $x1;    $cy1 = $y1 < $k2 ? $k2 : $y1;    $cx2 = $x2 > $k3 ? $k3 : $x2;    $cy2 = $y2 > $k4 ? $k4 : $y2;    if ($cx1 < 0) {        $cx1 = 0;    }    if ($cy1 < 0) {        $cy1 = 0;    }    if ($cx2 >= $W) {        $cx2 = $W - 1;    }    if ($cy2 >= $H) {        $cy2 = $H - 1;    }    if ($cx1 > $cx2 || $cy1 > $cy2) {        return;    }    $st = v6sp_state($sp);    if ($st == 0x34 || $st == 0x39) {        $col = xread8($sp + 0x50);        if (Img::$rgblogic) {            $w = xread16($sp + 0x26) & 0xffff;            rgb_fillrect($cx1, $cy1, $cx2, $cy2, $w == 0x1c20 && Alis::$platform->uid == C::EGameRobinsonsRequiem3DO ? rgb555($w) : pal_argb($col));            return;        }        v6_rect_put(Img::$logic + $cy1 * $W + $cx1, $W, array_fill(0, $cy2 - $cy1 + 1, str_repeat(chr($col), $cx2 - $cx1 + 1)));        return;    }    if ($st == 0x38) {        v6x_textwalk($sp, 1);        return;    }    if ($st == 0x7f) {        // PORT: C "map blit (FUN_00047665) not yet ported" warning: nothing drawn        return;    }    if ($st != 0x32) {        return;    }    $d = v6sp_desc($sp);    if ($d == 0) {        return;    }    $descr = s32($d + xread32($d));    if (v6sp_scale($sp) != 0x100) {        // PORT: C "zoom blit (LAB_0004716a) not yet ported" warning: nothing drawn        return;    }    if (xread8($descr) != 1) {        return;    }    $flip = xread8($sp + 0x25) & 1;    if (Img::$rgblogic) {        cel_blit($descr, $x1, $y1, $x2, $cx1, $cy1, $cx2, $cy2, $flip);        return;    }    $bw = xread16($descr + 0x10);    $opaque = xread8($descr + 0x1b) & 1;    $src = $descr + 0x20;    $n = $cx2 - $cx1 + 1;    $dst0 = Img::$logic + $cy1 * $W + $cx1;    $rows = [];    for ($yy = $cy1; $yy <= $cy2; $yy++) {        $s = $src + ($yy - $y1) * $bw;        if ($flip) {            // s += x2 - cx1; n bytes backwards            $seg = strrev(substr(Alis::$mem, $s + $x2 - $cx1 - $n + 1, $n));        } else {            $seg = substr(Alis::$mem, $s + $cx1 - $x1, $n);        }        if (!$opaque) {            $seg = v6_merge8(substr(Alis::$mem, $dst0 + ($yy - $cy1) * $W, $n), $seg);        }        $rows[] = $seg;    }    v6_rect_put($dst0, $W, $rows);}// repaint one dirty rect, every screen it touches back to frontfunction v6x_repaint(int $rx1, int $ry1, int $rx2, int $ry2, int $own): void{    $r3do = Alis::$platform->uid == C::EGameRobinsonsRequiem3DO;    for ($id = Scr::$ptscreen & 0xffff; $id != 0; $id = xread16(Alis::$basemain + $id + 0x5c) & 0xffff) {        $s = Alis::$basemain + $id;        if (xread8($s + 0x58) & 0x40) {            continue;        }        $cx1 = xread16($s + 0x66);        $cy1 = xread16($s + 0x68);        $cx2 = xread16($s + 0x6a);        $cy2 = xread16($s + 0x6c);        if ($cx1 > $rx2 || $cy1 > $ry2 || $cx2 < $rx1 || $cy2 < $ry1) {            continue;        }        $type = xread8($s + 0x5a);        if ($type == 6 || ($type == 5 && $r3do)) {            if ($own != 0 && $own != $id) {                continue;            }            ImageV6Vars::$v6xclipx1 = $cx1;            ImageV6Vars::$v6xclipy1 = $cy1;            ImageV6Vars::$v6xclipx2 = $cx2;            ImageV6Vars::$v6xclipy2 = $cy2;            if ($type == 5) {                r3do_flush($s);                continue;            }            if (xread16($s + 0x116) != 0) {                v6x_skyfill($s);            } elseif (!(xread8($s + 0x59) & 0x40)) {                v6x_fill(xread8($s + 0x13b));            }            v6x3d_flush($s);            continue;        }        ImageV6Vars::$v6xclipx1 = $cx1 > $rx1 ? $cx1 : $rx1;        ImageV6Vars::$v6xclipy1 = $cy1 > $ry1 ? $cy1 : $ry1;        ImageV6Vars::$v6xclipx2 = $cx2 < $rx2 ? $cx2 : $rx2;        ImageV6Vars::$v6xclipy2 = $cy2 < $ry2 ? $cy2 : $ry2;        if (!(xread8($s + 0x59) & 0x40) && !$r3do) {            v6x_fill(xread8($s + 0x13b));        }        $node = xread32($s + 0x5e);        if ($node == 0) {            continue;        }        for ($sp = xread32($node + 0x38); $sp != 0 && $sp != xread32($sp + 0x38); $sp = xread32($sp + 0x38)) {            if (xread16($sp + 0x4c) >= 0 && !(v6sp_flags($sp) & 0x80) && !(xread8($sp + 0x24) & 0x40)) {                v6x_blit($sp);            }        }    }}/** sV6Box = [x1, y1, x2, y2] (s32). */function v6x_clampbox(array &$b): int{    if ($b[0] < 0) {        $b[0] = 0;    }    if ($b[1] < 0) {        $b[1] = 0;    }    if ($b[2] >= Alis::$platform->width) {        $b[2] = Alis::$platform->width - 1;    }    if ($b[3] >= Alis::$platform->height) {        $b[3] = Alis::$platform->height - 1;    }    return (int) ($b[0] <= $b[2] && $b[1] <= $b[3]);}// logic -> physic for one rectfunction v6x_copy(array $b): void{    if (!v6x_clampbox($b)) {        return;    }    $W = Alis::$platform->width;    $n = $b[2] - $b[0] + 1;    $rows = [];    // 3DO draws and shows only the RGB buffers    if (Img::$rgblogic) {        $pitch = $W * 3;        $src = Img::$rgblogic + ($b[1] * $W + $b[0]) * 3;        $dst = Img::$rgbphysic + ($b[1] * $W + $b[0]) * 3;        $len = $n * 3;    } else {        $pitch = $W;        $src = Img::$logic + $b[1] * $W + $b[0];        $dst = Img::$physic + $b[1] * $W + $b[0];        $len = $n;    }    for ($y = $b[1]; $y <= $b[3]; $y++, $src += $pitch) {        $rows[] = substr(Alis::$mem, $src, $len);    }    v6_rect_put($dst, $pitch, $rows);    $nc = ImageV6Vars::$v6x_newcur;    if ($b[0] <= $nc[2] && $b[2] >= $nc[0] && $b[1] <= $nc[3] && $b[3] >= $nc[1]) {        ImageV6Vars::$v6x_curhit = 1;    }    dirty_add($b[0], $b[1], $b[2] - $b[0] + 1, $b[3] - $b[1] + 1);}function v6x_drawcursor(array $b): void{    $W = Alis::$platform->width;    $mx = $b[0];    $my = $b[1];    $r3do = Alis::$platform->uid == C::EGameRobinsonsRequiem3DO;    $m = &Alis::$mem;    for ($yy = 0; $yy <= $b[3] - $my; $yy++) {        for ($xx = 0; $xx <= $b[2] - $mx; $xx++) {            if ($r3do) {                $c = ImageV6Vars::$v6x_mousergb[$yy * 0x20 + $xx];                if ($c) {                    rgb_put(Img::$rgbphysic + (($my + $yy) * $W + $mx + $xx) * 3, rgb555($c & 0x7fff));                }                continue;            }            $c = ImageV6Vars::$v6x_mousebuf[$yy * 0x20 + $xx];            if ($c == 0) {                continue;            }            if (Img::$rgbphysic) {                rgb_put(Img::$rgbphysic + (($my + $yy) * $W + $mx + $xx) * 3, pal_argb($c));            } else {                $m[Img::$physic + ($my + $yy) * $W + $mx + $xx] = chr($c);            }        }    }    dirty_add($b[0], $b[1], $b[2] - $b[0] + 1, $b[3] - $b[1] + 1);}// repaint the dirty rects in logic, then copy only them to physicfunction v6x_composite(): void{    $W = Alis::$platform->width;    $H = Alis::$platform->height;    ImageV6Vars::$v6x_newcur = [0, 0, -1, -1];    if (Alis::$desmouse != 0 && ImageV6Vars::$v6x_mouseh > 0) {        $mouse = sys_get_mouse();        $mxp = s16($mouse->x);        $myp = s16($mouse->y);        if ($mouse->enabled && $mxp >= 0 && $myp >= 0) {            $nc = [$mxp, $myp, $mxp + ImageV6Vars::$v6x_mousew - 1, $myp + ImageV6Vars::$v6x_mouseh - 1];            if (!v6x_clampbox($nc)) {                $nc = [0, 0, -1, -1];            }            ImageV6Vars::$v6x_newcur = $nc;        }    }    // PORT: sys_vblcursor (native Falcon hi-res draws the cursor at VBL) is always 0 here.    $moved = ImageV6Vars::$v6x_newcur !== ImageV6Vars::$v6x_cur || ImageV6Vars::$v6x_mousegen != ImageV6Vars::$v6x_curgen;    ImageV6Vars::$v6x_curhit = 0;    if (ImageVars::$v6xdirty) {        ImageVars::$v6xdirty = 0;        v6x_repaint(0, 0, $W - 1, $H - 1, 0);        v6x_copy([0, 0, $W - 1, $H - 1]);    } else {        $tail = ImageV6Vars::$v6dirty_tail;        for ($i = 0; $i < $tail; $i++) {            $d = ImageV6Vars::$v6dirty[$i];            v6x_repaint($d['x1'], $d['y1'], $d['x2'], $d['y2'], $d['scene']);        }        for ($i = 0; $i < $tail; $i++) {            $d = ImageV6Vars::$v6dirty[$i];            v6x_copy([$d['x1'], $d['y1'], $d['x2'], $d['y2']]);        }        if ($moved && ImageV6Vars::$v6x_cur[0] <= ImageV6Vars::$v6x_cur[2]) {            v6x_copy(ImageV6Vars::$v6x_cur);        }    }    $nc = ImageV6Vars::$v6x_newcur;    if ($nc[0] <= $nc[2] && ($moved || ImageV6Vars::$v6x_curhit)) {        v6x_drawcursor($nc);    }    ImageV6Vars::$v6x_cur = $nc;    ImageV6Vars::$v6x_curgen = ImageV6Vars::$v6x_mousegen;    ImageV6Vars::$v6dirty_tail = 0;}function v6x_defmouse(int $desc): void{    ImageV6Vars::$v6x_mousegen = (ImageV6Vars::$v6x_mousegen + 1) & 0xffff;    if (Alis::$platform->uid == C::EGameRobinsonsRequiem3DO) {        $w = 0;        $h = 0;        $px = cel_decode($desc, $w, $h);        $buf = array_fill(0, 0x400, 0);        $mw = $w > 0x20 ? 0x20 : ($w < 0 ? 0 : $w);        $mh = $h > 0x20 ? 0x20 : ($h < 0 ? 0 : $h);        ImageV6Vars::$v6x_mousew = $mw;        ImageV6Vars::$v6x_mouseh = $mh;        for ($y = 0; $px !== null && $y < $mh; $y++) {            for ($x = 0; $x < $mw; $x++) {                $buf[$y * 0x20 + $x] = $px[$y * $w + $x];            }        }        ImageV6Vars::$v6x_mousergb = $buf;        return;    }    $buf = array_fill(0, 0x400, 0);    $chunks = read16($desc + 0x10);    $clen = read16($desc + 0x12);    ImageV6Vars::$v6x_mousew = $chunks;    ImageV6Vars::$v6x_mouseh = $clen;    $src = $desc + 0x20;    $n = $chunks > 0x20 ? 0x20 : $chunks;    $l = $clen > 0x20 ? 0x20 : $clen;    for ($i = 0; $i < $n; $i++) {        for ($j = 0; $j < $l; $j++) {            $buf[$i * 0x20 + $j] = xread8($src + $i * $clen + $j);        }    }    ImageV6Vars::$v6x_mousebuf = $buf;}function v6x_draw(): void{    ImageV6Vars::$draw_frame++;    Deus3dVars::$v6x3d_tick = s32(Deus3dVars::$v6x3d_tick + 1);    v6ips_measure();    for ($id = Scr::$ptscreen & 0xffff; $id != 0; $id = xread16(Alis::$basemain + $id + 0x5c) & 0xffff) {        $s = Alis::$basemain + $id;        if (xread8($s + 0x58) & 0x40) {            continue;        }        v6x_affiscr($s);    }    v6x_composite();}function affiscr_v6_record(int $scene, int $head): void{    $scene &= 0xffff;    $head &= 0xffff;    if (s8(get_scr_state($scene)) < 0) {        depscreen($scene, $head);    }    // dirty-rect clip window    $wx1 = get_scr_newx($scene);    $wy1 = get_scr_newy($scene);    v6dirty_set_window($wx1, $wy1, $wx1 + get_scr_width($scene), $wy1 + get_scr_height($scene));    // Walk the z-list tracking predecessor. Unlinking a deleted or moved sprite must use the walks    // predecessor, not the sprite's stored `prev` field    $prevspidx = $head;    $cur = sprite_var($head)->link;    while ($cur != 0) {        $cs = sprite_var($cur);        $st = $cs->state;        if ($st == 0) {            $prevspidx = $cur;            $cur = $cs->link;            continue;        }        $cs->prev = $prevspidx;        if ($st == 1) {            v6dirty_add($cs->newx, $cs->newy, $cs->newx + $cs->width, $cs->newy + $cs->height, -1, $scene);            $n = $cs->link;            sprite_var($prevspidx)->link = $n;            if ($n != 0) {                sprite_var($n)->prev = $prevspidx;            }            $cs->to_next = Img::$libsprit;            Img::$libsprit = $cur;            $cur = $n;        } else {            if ($st != -1) {                v6dirty_add($cs->newx, $cs->newy, $cs->newx + $cs->width, $cs->newy + $cs->height, -1, $scene);            }            deptopix($scene, $cur);            image_store_new($cs);            rangesprite_v6($cur, $head);            v6dirty_add($cs->newx, $cs->newy, $cs->newx + $cs->width, $cs->newy + $cs->height, -1, $scene);            $cur = sprite_var($prevspidx)->link;        }    }    set_scr_state($scene, get_scr_state($scene) & 0x7f);}function compositor_v6(): void{    for ($scn = s16(Scr::$ptscreen); $scn != 0; $scn = s16(get_scr_to_next($scn))) {        if ((get_scr_state($scn) & 0x40) != 0) {            continue;        }        $h = get_scr_screen_id($scn) & 0xffff;        if ($h == 0) {            continue;        }        $sx1 = get_scr_newx($scn);        $sy1 = get_scr_newy($scn);        Img::$clipx1 = $sx1;        Img::$clipy1 = $sy1;        Img::$clipx2 = s16($sx1 + get_scr_width($scn));        Img::$clipy2 = s16($sy1 + get_scr_height($scn));        $sp = $h;        while (($sp = sprite_var($sp)->link) != 0) {            $s = sprite_var($sp);            if ($s->state >= -1 && $s->newf >= -1 && $s->newd >= -1) {                destofen($s);            }        }    }    // publish fully-composited logic frame to the display buffer    Img::$fenx1 = Img::$logx1;    Img::$feny1 = Img::$logy1;    Img::$fenx2 = Img::$logx2;    Img::$feny2 = Img::$logy2;    trsfen(Img::$logic, Img::$physic);}