UK
games/alis / src / Alis / fn / deus3d.php · commit 9c722cf
<?phpnamespace App\Alis;use App\Alis\Deus3dVars as V;// Port of alis/src/render/deus3d.c (MIT, Olivier Huguenot, Vadim Kindl).// Deus (VM v6) 3D renderer: shading LUTs, transform contexts, sphere collision, the face pipeline// (projection, clipping, depth slots) and the span rasterisers writing 8-bit pixels into image.logic.//// PORT: 64-bit C products (s64) — on 32-bit PHP an int product that overflows becomes a float, exact up// to 2^53, which covers every s16 x s32 sum of products here. d3_sar() / d3_div64() take int|float and// return the C (s32) cast. s32 x s32 products that may exceed 2^53 go through d3_mul64_sar() (exact).// ---------------------------------------------------------------------------------------------// 64-bit helpers (fixmath.h: fx_mul64 / fx_div64)// ---------------------------------------------------------------------------------------------/** (s32)(v >> n) of a C s64 value (int, or float above 2^31 on 32-bit PHP). */function d3_sar(int|float $v, int $n): int{    if (is_int($v)) {        return s32($v >> $n);    }    return s32(floor($v / (1 << $n)));}/** (s32)(num / den) of a C s64 numerator (truncation toward zero; 0 when den == 0). */function d3_div64(int|float $num, int $den): int{    if ($den === 0) {        return 0;    }    if (is_int($num)) {        if ($num === PHP_INT_MIN && $den === -1) {            return s32($num);        }        return s32(intdiv($num, $den));    }    $q = $num / $den;    return s32($q < 0 ? ceil($q) : floor($q));}/** (s32)(((s64)a * b) >> n), exact for any s32 a, b and n >= 16. */function d3_mul64_sar(int $a, int $b, int $n): int{    if (PHP_INT_SIZE === 8) {        return s32(($a * $b) >> $n);    }    $r = $a * $b;    if (is_int($r)) {        return $r >> $n;    }    $bl = $b & 0xffff;    $bh = $b >> 16;    // a*b = a*bh*2^16 + a*bl; both partial products < 2^47 (exact as floats)    $hi = $a * $bh + floor(($a * $bl) / 65536);    return s32(floor($hi / (1 << ($n - 16))));}/** C s64 product of two s32 (int on 64-bit; float, exact below 2^53, on 32-bit). */function d3_mul64(int $a, int $b): int|float{    return $a * $b;}/** V6COS(i) */function V6COS(int $i): int{    return Deus3dConst::v6costab[$i & 0xfff];}/** V6SIN(i) */function V6SIN(int $i): int{    return Deus3dConst::v6costab[($i + 0xc00) & 0xfff];}// ---------------------------------------------------------------------------------------------// Shading / fog LUTs// ---------------------------------------------------------------------------------------------// weighted nearest-palette-colour searchfunction v6shad_nearest(int $pal, int $r, int $g, int $b, int $lo, int $hi): int{    $tr = $r + 1;    if ($tr > 255) {        $tr = 255;    }    $tr &= ~3;    $tg = $g + 1;    if ($tg > 255) {        $tg = 255;    }    $tg &= ~3;    $tb = $b + 1;    if ($tb > 255) {        $tb = 255;    }    $tb &= ~3;    $m = Alis::$mem;    $best = PHP_INT_MAX; // C: 0xffffffff (d never reaches it)    $idx = $lo;    for ($i = $lo; $i <= $hi; $i++) {        $d = 0;        $o = $pal + $i * 3;        $dv = ord($m[$o]) - $tr;        if ($dv) {            $d += ($dv * $dv) * 0x11;        }        $dv = ord($m[$o + 1]) - $tg;        if ($dv) {            $d += ($dv * $dv) * 0x18;        }        $dv = ord($m[$o + 2]) - $tb;        if ($dv) {            $d += ($dv * $dv) * 0x0a;        }        if ($d < $best) {            $best = $d;            $idx = $i & 0xff;            if ($d == 0) {                break;            }        }    }    return $idx;}// build 256x256 colour-mix LUTfunction v6shad_mixlut(int $pal, int $dst, int $pre, int $lo, int $hi): void{    for ($row = 0; $row < 256; $row++) {        $rowR = xread8($pal + $row * 3 + 0);        $rowG = xread8($pal + $row * 3 + 1);        $rowB = xread8($pal + $row * 3 + 2);        if ($pre && $row == 0) {            $pre++;        }        Alis::$mem[$dst + $row * 256] = chr($row);        for ($c = 1; $c < 256; $c++) {            $al = 0;            $compute = 1;            if ($c < $row) {                $al = ord(Alis::$mem[$dst + $c * 256 + $row]);                $compute = 0;            } elseif ($pre) {                $al = ord(Alis::$mem[$pre++]);                if ($al >= $lo && $al <= $hi) {                    $compute = 0;                }            }            if ($compute) {                $r = (xread8($pal + $c * 3 + 0) + $rowR) >> 1;                $g = (xread8($pal + $c * 3 + 1) + $rowG) >> 1;                $b = (xread8($pal + $c * 3 + 2) + $rowB) >> 1;                $al = v6shad_nearest($pal, $r, $g, $b, $lo, $hi);            }            Alis::$mem[$dst + $row * 256 + $c] = chr($al);        }    }}// build fog/darkness remap rowsfunction v6shad_fogrows(int $scr, int $pal, int $dst, int $width, int $start): void{    $start = s16($start);    $lo = xread8($scr + 0xb8);    if ($lo == 0) {        $lo = 1;    }    $hi = xread8($scr + 0xba);    $mode = xread16($scr + 0x94) & 0xffff;    $curR = $start;    $curG = $start;    $curB = $start;    // static s16 stepR, stepG, stepB    $stepR = V::$fog_stepR;    $stepG = V::$fog_stepG;    $stepB = V::$fog_stepB;    $fogR = 0;    $fogG = 0;    $fogB = 0;    $shad = V::$v6shad;    $shad->solidband = -1;    $shad->identband = $width & 0xff;    $shad->bands = s32($width - 1);    if (($mode & 0xff) == 0) {        if (!($mode & 0x100)) {            xwrite8($scr + 0x13b, 0);        }        if ($start == 0) {            $shad->solidband = 0;            $shad->solidflag = 1;        }        if ($mode & 0x400) {            $d32 = xread32($scr + 0xa2) >> 7;            $d = s16($d32);            if ($d32) {                $num = s16(0x100 - $curR);                $stepR = s16(cdiv($num, $d));                $curR = s16($curR + cmod($num, $d));            } else {                $curR = 0x100;                $stepR = s16(cdiv(xread16($scr + 0x9a), $width));            }            if ($d32) {                $num = s16(0x100 - $curG);                $stepG = s16(cdiv($num, $d));                $curG = s16($curG + cmod($num, $d));            } else {                $curG = 0x100;                $stepG = s16(cdiv(xread16($scr + 0x9c), $width));            }            if ($d32) {                $num = s16(0x100 - $curB);                $stepB = s16(cdiv($num, $d));                $curB = s16($curB + cmod($num, $d));            } else {                $curB = 0x100;                $stepB = s16(cdiv(xread16($scr + 0x9e), $width));            }        } else {            $stepR = s16(cdiv(xread16($scr + 0x9a), $width));            $stepG = s16(cdiv(xread16($scr + 0x9c), $width));            $stepB = s16(cdiv(xread16($scr + 0x9e), $width));        }        $pivot = $width;    } else {        $shad->solidband = $shad->bands;        $shad->solidflag = 0;        $fogidx = v6shad_nearest($pal, xread16($scr + 0x9a), xread16($scr + 0x9c), xread16($scr + 0x9e), $lo, $hi);        if (!($mode & 0x100)) {            xwrite8($scr + 0x13b, $fogidx);        }        $fogR = xread8($pal + $fogidx * 3 + 0) << 8;        $fogG = xread8($pal + $fogidx * 3 + 1) << 8;        $fogB = xread8($pal + $fogidx * 3 + 2) << 8;        $pivot = 0;        if (($mode & 0xff) != 2) {            if ($mode & 0x400) {                $d32 = xread32($scr + 0xa2) >> 7;                $d = s16($d32);                if ($d32) {                    $num = s16(0x100 - $curR);                    $stepR = s16(cdiv($num, $d));                    $curR = s16($curR + cmod($num, $d));                } else {                    $curR = 0x100;                    $stepR = s16(cdiv(s16(xread16($scr + 0x9a) + 0x100 - $curR), $width));                }                if ($d32) {                    $num = s16(0x100 - $curG);                    $stepG = s16(cdiv($num, $d));                    $curG = s16($curG + cmod($num, $d));                } else {                    $curG = 0x100;                    $stepG = s16(cdiv(s16(xread16($scr + 0x9c) + 0x100 - $curG), $width));                }                if ($d32) {                    $num = s16(0x100 - $curB);                    $stepB = s16(cdiv($num, $d));                    $curB = s16($curB + cmod($num, $d));                } else {                    $curB = 0x100;                    $stepB = s16(cdiv(s16(xread16($scr + 0x9e) + 0x100 - $curB), $width));                }            } else {                $stepR = s16(cdiv(s16(xread16($scr + 0x9a) + 0x100 - $curR), $width));                $stepG = s16(cdiv(s16(xread16($scr + 0x9c) + 0x100 - $curG), $width));                $stepB = s16(cdiv(s16(xread16($scr + 0x9e) + 0x100 - $curB), $width));            }            $pivot = $width >> 1;        }    }    for ($row = 0; $row < $width; $row++) {        for ($c = 0; $c < 256; $c++) {            if ($c == 0) {                Alis::$mem[$dst + $row * 256] = "\0";                continue;            }            if ($c < $lo || $c > $hi) {                continue;            }            if ($row < $pivot) {                $cr = $curR < 0 ? 0 : ($curR & 0xffff);                $cg = $curG < 0 ? 0 : ($curG & 0xffff);                $cb = $curB < 0 ? 0 : ($curB & 0xffff);                $tr = s16((xread8($pal + $c * 3 + 0) * $cr) >> 8);                $tg = s16((xread8($pal + $c * 3 + 1) * $cg) >> 8);                $tb = s16((xread8($pal + $c * 3 + 2) * $cb) >> 8);            } else {                $stepR = $stepG = $stepB = 0;                $den = $width - $pivot;                $v = xread8($pal + $c * 3 + 0) * ($curR & 0xffff);                $q = d3_div64(($v - $fogR) * ($row - $pivot), $den);                $tr = s16(($v - $q) >> 8);                $v = xread8($pal + $c * 3 + 1) * ($curG & 0xffff);                $q = d3_div64(($v - $fogG) * ($row - $pivot), $den);                $tg = s16(($v - $q) >> 8);                $v = xread8($pal + $c * 3 + 2) * ($curB & 0xffff);                $q = d3_div64(($v - $fogB) * ($row - $pivot), $den);                $tb = s16(($v - $q) >> 8);            }            Alis::$mem[$dst + $row * 256 + $c] = chr(v6shad_nearest($pal, $tr, $tg, $tb, $lo, $hi));        }        $curR = s16($curR + $stepR);        $curG = s16($curG + $stepG);        $curB = s16($curB + $stepB);    }    V::$fog_stepR = $stepR;    V::$fog_stepG = $stepG;    V::$fog_stepB = $stepB;    for ($i = 0; $i < 256; $i++) {        Alis::$mem[$dst + $width * 256 + $i] = chr($i);    }}function v6shad_build3(int $scr, int $desc): void{    $width = xread16($scr + 0x96) & 0xffff;    if (xread32($scr + 0xb4) == 0) {        $node = Alis::$platform->kind != C::EPlatform3DO ? (node_take(0xb) & 0xffff) : 0;        xwrite32($scr + 0xb4, io_malloc((($width + 1) << 8) + 0x10000));        if ($node) {            scrnode_set($scr, 0xb4, $node);        }    }    $hbuf = xread32($scr + 0xb4);    if ($hbuf == 0) {        return;    }    $buf = xread32($hbuf);    $pal = $desc + 0xc;    $pre = (xread8($desc + 0xb) & 1) ? $desc + 0x30c : 0;    v6shad_mixlut($pal, $buf, $pre, xread8($scr + 0xb8), xread8($scr + 0xba));    v6shad_fogrows($scr, $pal, $buf + 0x10000, $width, xread16($scr + 0x98));}// ---------------------------------------------------------------------------------------------// Transform contexts// ---------------------------------------------------------------------------------------------/** The sV6Ctx stored at byte offset $off of the C static u8 v6tf_pool[0x8020] (created zeroed). */function v6tf_poolctx(int $off): sV6Ctx{    return V::$v6tf_pool[$off] ??= new sV6Ctx();}function v6tf_identity(sV6Ctx $c): void{    $c->m = [0x7fff, 0, 0, 0, 0x7fff, 0, 0, 0, 0x7fff];    $c->t = [0, 0, 0];}// V6ROTL(lo, hi): a = lo*C - hi*S; b = lo*S + hi*C; lo = a >> 15; hi = b >> 15function v6tf_rotx_left(sV6Ctx $c, int $angle): void{    $i = ($angle & 0xffff) >> 4;    if (!$i) {        return;    }    $C = V6COS($i);    $S = V6SIN($i);    $m = &$c->m;    for ($col = 0; $col < 3; $col++) {        $lo = $m[3 + $col];        $hi = $m[6 + $col];        $m[3 + $col] = s16(($lo * $C - $hi * $S) >> 15);        $m[6 + $col] = s16(($lo * $S + $hi * $C) >> 15);    }    $lo = $c->t[1];    $hi = $c->t[2];    $c->t[1] = d3_sar($lo * $C - $hi * $S, 15);    $c->t[2] = d3_sar($lo * $S + $hi * $C, 15);}function v6tf_roty_left(sV6Ctx $c, int $angle): void{    $i = ($angle & 0xffff) >> 4;    if (!$i) {        return;    }    $C = V6COS($i);    $S = V6SIN($i);    $m = &$c->m;    for ($col = 0; $col < 3; $col++) {        $lo = $m[6 + $col];        $hi = $m[$col];        $m[6 + $col] = s16(($lo * $C - $hi * $S) >> 15);        $m[$col] = s16(($lo * $S + $hi * $C) >> 15);    }    $lo = $c->t[2];    $hi = $c->t[0];    $c->t[2] = d3_sar($lo * $C - $hi * $S, 15);    $c->t[0] = d3_sar($lo * $S + $hi * $C, 15);}function v6tf_rotz_left(sV6Ctx $c, int $angle): void{    $i = ($angle & 0xffff) >> 4;    if (!$i) {        return;    }    $C = V6COS($i);    $S = V6SIN($i);    $m = &$c->m;    for ($col = 0; $col < 3; $col++) {        $lo = $m[$col];        $hi = $m[3 + $col];        $m[$col] = s16(($lo * $C - $hi * $S) >> 15);        $m[3 + $col] = s16(($lo * $S + $hi * $C) >> 15);    }    $lo = $c->t[0];    $hi = $c->t[1];    $c->t[0] = d3_sar($lo * $C - $hi * $S, 15);    $c->t[1] = d3_sar($lo * $S + $hi * $C, 15);}// V6ROTR(lo, hi): a = lo*C + hi*S; b = -lo*S + hi*Cfunction v6tf_rotx_right(sV6Ctx $c, int $angle): void{    $i = ($angle & 0xffff) >> 4;    if (!$i) {        return;    }    $C = V6COS($i);    $S = V6SIN($i);    $m = &$c->m;    for ($r = 0; $r < 9; $r += 3) {        $lo = $m[$r + 1];        $hi = $m[$r + 2];        $m[$r + 1] = s16(($lo * $C + $hi * $S) >> 15);        $m[$r + 2] = s16(($hi * $C - $lo * $S) >> 15);    }}function v6tf_roty_right(sV6Ctx $c, int $angle): void{    $i = ($angle & 0xffff) >> 4;    if (!$i) {        return;    }    $C = V6COS($i);    $S = V6SIN($i);    $m = &$c->m;    for ($r = 0; $r < 9; $r += 3) {        $lo = $m[$r + 2];        $hi = $m[$r];        $m[$r + 2] = s16(($lo * $C + $hi * $S) >> 15);        $m[$r] = s16(($hi * $C - $lo * $S) >> 15);    }}function v6tf_rotz_right(sV6Ctx $c, int $angle): void{    $i = ($angle & 0xffff) >> 4;    if (!$i) {        return;    }    $C = V6COS($i);    $S = V6SIN($i);    $m = &$c->m;    for ($r = 0; $r < 9; $r += 3) {        $lo = $m[$r];        $hi = $m[$r + 1];        $m[$r] = s16(($lo * $C + $hi * $S) >> 15);        $m[$r + 1] = s16(($hi * $C - $lo * $S) >> 15);    }}/** C v6tf_apply(v, c, out): out may be the same array as v. */function v6tf_apply(array $v, sV6Ctx $c, array &$out): void{    $m = $c->m;    $t = $c->t;    $x = s32(d3_sar($m[0] * $v[0] + $m[1] * $v[1] + $m[2] * $v[2], 15) + $t[0]);    $y = s32(d3_sar($m[3] * $v[0] + $m[4] * $v[1] + $m[5] * $v[2], 15) + $t[1]);    $z = s32(d3_sar($m[6] * $v[0] + $m[7] * $v[1] + $m[8] * $v[2], 15) + $t[2]);    $out[2] = $z;    $out[1] = $y;    $out[0] = $x;}function v6tf_combine(sV6Ctx $D, sV6Ctx $A, sV6Ctx $out): void{    $pool0 = v6tf_poolctx(0);    if ($D === $pool0) {        $out->copy20($A);        return;    }    if ($A === $pool0) {        $out->copy20($D);        return;    }    // computed into locals first: covers D == out / A == out (C goes through v6tf_scratch)    $d = $D->m;    $a = $A->m;    $m = [];    for ($r = 0; $r < 9; $r += 3) {        for ($col = 0; $col < 3; $col++) {            $m[$r + $col] = s16(d3_sar($d[$r] * $a[$col] + $d[$r + 1] * $a[3 + $col] + $d[$r + 2] * $a[6 + $col], 15));        }    }    $at = $A->t;    $dt = $D->t;    $t = [];    for ($r = 0; $r < 3; $r++) {        $t[$r] = s32($dt[$r] + d3_sar($d[$r * 3] * $at[0] + $d[$r * 3 + 1] * $at[1] + $d[$r * 3 + 2] * $at[2], 15));    }    $o = ($D === $out || $A === $out) ? V::$v6tf_scratch : $out;    $o->m = $m;    $o->t = $t;    if ($o === V::$v6tf_scratch) {        $out->copy20($o);    }}// cache primitivesfunction v6tf_probe_src(int $e): ?sV6Ctx{    if ($e == 0) {        return V::$v6tf_w2c;    }    $w = xread16($e + 0x20) & 0xffff;    if (!$w || !($w & 1)) {        return null;    }    V::$v6tf_hits = s32(V::$v6tf_hits + 1);    return v6tf_poolctx($w & ~3);}function v6tf_probe_dst(int $e): ?sV6Ctx{    if ($e == 0) {        return V::$v6tf_c2w;    }    $w = xread16($e + 0x20) & 0xffff;    if (!$w || !($w & 2)) {        return null;    }    V::$v6tf_hits = s32(V::$v6tf_hits + 1);    return v6tf_poolctx(($w & ~3) + 0x20);}function v6tf_store(int $e, sV6Ctx $c, int $bit, int $half): void{    $off = xread16($e + 0x20) & 0xffff;    if (!$off) {        if (V::$v6tf_poolnext >= V::$v6tf_poolend) {            return;        }        $off = V::$v6tf_poolnext;        xwrite16($e + 0x20, $off);        V::$v6tf_poolnext += 0x40;        V::$v6tf_allocs = s32(V::$v6tf_allocs + 1);    }    xwrite16($e + 0x20, xread16($e + 0x20) | $bit);    v6tf_poolctx(($off & ~3) + $half)->copy20($c);}// apply src entitys local transformfunction v6tf_apply_src_local(sV6Ctx $c, int $e): void{    $px = xread32($e + 0x0c);    $py = xread32($e + 0x10);    $pz = xread32($e + 0x14);    $m = $c->m;    for ($r = 0; $r < 3; $r++) {        $c->t[$r] = s32($c->t[$r] + d3_sar($m[$r * 3] * $px + $m[$r * 3 + 1] * $py + $m[$r * 3 + 2] * $pz, 15));    }    v6tf_rotz_right($c, xread16($e + 0x1c));    v6tf_roty_right($c, xread16($e + 0x1a));    v6tf_rotx_right($c, xread16($e + 0x18));}// apply dest entity's inverse local transformfunction v6tf_apply_dst_inverse(sV6Ctx $c, int $e): void{    $c->t[0] = s32($c->t[0] - xread32($e + 0x0c));    $c->t[1] = s32($c->t[1] - xread32($e + 0x10));    $c->t[2] = s32($c->t[2] - xread32($e + 0x14));    if (!(V::$v6tf_srcflags & 0x8)) {        v6tf_rotz_left($c, -xread16($e + 0x1c));    }    v6tf_roty_left($c, -xread16($e + 0x1a));    if (!(V::$v6tf_srcflags & 0x2)) {        v6tf_rotx_left($c, -xread16($e + 0x18));    }}function v6tf_walk_src(int $e): sV6Ctx{    $hit = v6tf_probe_src($e);    if ($hit) {        return $hit;    }    $acc = v6tf_walk_src(xread32($e + 0x8));    if ($acc !== V::$v6tf_out) {        V::$v6tf_out->copy20($acc);        $acc = V::$v6tf_out;    }    v6tf_apply_src_local($acc, $e);    v6tf_store($e, $acc, 1, 0);    return $acc;}function v6tf_walk_dst(int $e): sV6Ctx{    $hit = v6tf_probe_dst($e);    if ($hit) {        return $hit;    }    $acc = v6tf_walk_dst(xread32($e + 0x8));    if ($acc !== V::$v6tf_destacc) {        V::$v6tf_destacc->copy20($acc);        $acc = V::$v6tf_destacc;    }    v6tf_apply_dst_inverse($acc, $e);    v6tf_store($e, $acc, 2, 0x20);    return $acc;}function v6tf_cached(int $src, int $dst): void{    $A = v6tf_walk_src($src);    if ($dst != V::$v6tf_camera) {        $D = v6tf_walk_dst($dst);        if ($src != V::$v6tf_camera) {            v6tf_combine($D, $A, V::$v6tf_out);            return;        }        $A = $D;    }    if ($A !== V::$v6tf_out) {        V::$v6tf_out->copy20($A);    }}// full rebuild dest sidefunction v6tf_full_dst(int $src, int $d, sV6Ctx $c): void{    if ($d == 0) {        return;    }    v6tf_full_dst($src, xread32($d + 0x8), $c);    if ($src != 0 && (xread8($d + 0x25) & 0x40)) {        v6tf_identity($c);        $c->scale = $src ? xread16($src + 0x1e) : 0x100;    }    $c->t[0] = s32($c->t[0] - xread32($d + 0x0c));    $c->t[1] = s32($c->t[1] - xread32($d + 0x10));    $c->t[2] = s32($c->t[2] - xread32($d + 0x14));    if (!($c->flags & 0x8)) {        v6tf_rotz_left($c, -xread16($d + 0x1c));    }    v6tf_roty_left($c, -xread16($d + 0x1a));    if (!($c->flags & 0x2)) {        v6tf_rotx_left($c, -xread16($d + 0x18));    }}function v6tf_full(int $src, int $dst, sV6Ctx $c): void{    $c->flags = $src ? xread8($src + 0x25) : 0;    v6tf_identity($c);    $c->scale = $src ? xread16($src + 0x1e) : 0x100;    for ($e = $src; $e != 0; $e = xread32($e + 0x8)) {        if (xread8($e + 0x25) & 0x40) {            $c->scale = xread16($e + 0x1e);            $x = xread32($e + 0x0c);            $y = xread32($e + 0x10);            $z = xread32($e + 0x14);            for ($p = xread32($e + 0x8); $p != 0; $p = xread32($p + 0x8)) {                $x = s32($x + xread32($p + 0x0c));                $y = s32($y + xread32($p + 0x10));                $z = s32($z + xread32($p + 0x14));            }            $c->t[0] = s32($c->t[0] + $x);            $c->t[1] = s32($c->t[1] + $y);            $c->t[2] = s32($c->t[2] + $z);            return;        }        if (!($c->flags & 0x2)) {            v6tf_rotx_left($c, xread16($e + 0x18));        }        v6tf_roty_left($c, xread16($e + 0x1a));        if (!($c->flags & 0x8)) {            v6tf_rotz_left($c, xread16($e + 0x1c));        }        $c->t[0] = s32($c->t[0] + xread32($e + 0x0c));        $c->t[1] = s32($c->t[1] + xread32($e + 0x10));        $c->t[2] = s32($c->t[2] + xread32($e + 0x14));        if (xread8($e + 0x25) & 0x20) {            return;        }    }    v6tf_full_dst($src, $dst, $c);}function v6tf_build(int $src, int $dst, sV6Ctx $out): void{    if (!V::$v6tf_cacheon) {        v6tf_full($src, $dst, $out);        return;    }    V::$v6tf_out = $out;    V::$v6tf_srcflags = 0;    V::$v6tf_srcscale = 0x100;    if ($src != 0) {        if ((xread8($src + 0x25) & 0xea) || ($dst != 0 && (xread8($dst + 0x25) & 0xea))) {            v6tf_full($src, $dst, $out);            return;        }        V::$v6tf_srcflags = xread8($src + 0x25);        V::$v6tf_srcscale = xread16($src + 0x1e) & 0xffff;    }    v6tf_cached($src, $dst);}// per-frame transform setupfunction v6tf_frame_setup(int $s): void{    V::$v6tf_hits = V::$v6tf_allocs = 0;    V::$v6tf_cacheon = 0;    V::$v6tf_camera = $s;    v6tf_build(0, $s, V::$v6tf_w2c);    v6tf_build($s, 0, V::$v6tf_c2w);    V::$v6tf_eye[0] = xread16($s + 0x42);    V::$v6tf_eye[1] = xread16($s + 0x44);    V::$v6tf_eye[2] = xread16($s + 0x46);    V::$v6tf_eye[3] = xread16($s + 0x40) & 0xffff;    v6tf_apply(V::$v6tf_eye, V::$v6tf_c2w, V::$v6tf_eye);    V::$v6tf_cacheon = 1;    V::$v6tf_poolnext = 0x20;    V::$v6tf_poolend = 0x20 + 0x8000;    $head = xread32($s + 0x5e);    if ($head != 0) {        for ($e = xread32($head + 0x38); $e != 0 && $e != xread32($e + 0x38); $e = xread32($e + 0x38)) {            xwrite16($e + 0x20, 0);        }    }}// integer square rootfunction v6x_isqrt(int $n): int{    if ($n == 0) {        return 0;    }    $n = u32v(s32($n)); // u32 (float above 2^31 on 32-bit PHP, exact)    $b = $n;    $c = 0;    if ($b >= 0x10000) {        $c += 16;        $b = (int) floor($b / 65536);    }    if ($b >= 0x100) {        $c += 8;        $b >>= 8;    }    if ($b >= 0x10) {        $c += 4;        $b >>= 4;    }    if ($b >= 4) {        $c += 2;        $b >>= 2;    }    if ($b >= 2) {        $c += 1;        $b >>= 1;    }    $c >>= 1;    $x = (int) floor($n / (1 << $c));    $x = (int) floor((floor($n / $x) + $x) / 2);    $x = (int) floor((floor($n / $x) + $x) / 2);    $x = (int) floor((floor($n / $x) + $x) / 2);    return $x;}// sphere-vs-view-sphere testfunction v6x3d_cull(int $elem): int{    return v6x3d_cull_vs(V::$v6tf_eye, $elem);}function v6x3d_cull_vs(array $sph, int $elem): int{    $b23 = xread8($elem + 0x23);    if ($b23 & 0x10) {        $P0 = xread32($elem + 0x0c);        $P1 = xread32($elem + 0x10);        $P2 = xread32($elem + 0x14);    } elseif ($b23 & 0x40) {        $P0 = xread16($elem + 0x42);        $P1 = xread16($elem + 0x44);        $P2 = xread16($elem + 0x46);        for ($e2 = $elem; $e2 != 0; $e2 = xread32($e2 + 0x8)) {            $P0 = s32($P0 + xread32($e2 + 0x0c));            $P1 = s32($P1 + xread32($e2 + 0x10));            $P2 = s32($P2 + xread32($e2 + 0x14));        }    } else {        $e2 = $elem;        while (xread32($e2 + 0x8) != 0) {            $e2 = xread32($e2 + 0x8);        }        $P0 = xread32($e2 + 0x0c);        $P1 = xread32($e2 + 0x10);        $P2 = xread32($e2 + 0x14);    }    $P = [$P0, $P1, $P2];    $q = 0; // u32, sum of three squares < 3 * 46340^2 (C wraps mod 2^32)    for ($i = 0; $i < 3; $i++) {        $d = s32($P[$i] - $sph[$i]) >> 1;        if ($d > 46340 || $d < -46340) {            return 0;        }        $q += $d * $d;    }    $R = ((xread16($elem + 0x40) & 0xffff) + $sph[3]) >> 1; // sph[3] is a u16 radius    return ult(s32($q), mul32($R, $R)) ? 1 : 0;}// probe sphere -> world spacefunction v6col_probe_world(int $chain, int $srad, int $cx, int $cy, int $cz): void{    $c = new sV6Ctx();    v6tf_build($chain, 0, $c);    $pw = [s16($cx), s16($cy), s16($cz), 0];    v6tf_apply($pw, $c, $pw);    $r = $srad & 0xffff;    if (ult($r, Alis::$v6focstep)) {        $r = Alis::$v6focstep;    }    $pw[3] = s32($r);    V::$v6col_pw = $pw;}// candidate sphere vs the probefunction v6col_test_sphere(int $cand, int $prad): int{    if (xread8($cand + 0x23) & 0x70) {        if (!v6x3d_cull_vs(V::$v6col_pw, $cand)) {            return 0;        }    }    if (xread8($cand + 0x25) & 4) {        return 0;    }    $rsum = ((xread16($cand + 0x40) & 0xffff) + ($prad & 0xffff)) & 0xffff;    $c = new sV6Ctx();    v6tf_build($cand, 0, $c);    $wc = [xread16($cand + 0x42), xread16($cand + 0x44), xread16($cand + 0x46)];    v6tf_apply($wc, $c, $wc);    $sum = 0;    $pw = V::$v6col_pw;    for ($i = 0; $i < 3; $i++) {        $d = s32($wc[$i] - $pw[$i]);        $pp = $d * $d; // s64        if ($pp >= 4294967296.0 || $pp < 0) {            return 0;        }        $sum += $pp;    }    // u32 sum (wraps mod 2^32) < rsum^2    $sum = s32($sum);    return ult($sum, $rsum * $rsum) ? 1 : 0;}// ---------------------------------------------------------------------------------------------// Sphere collision// ---------------------------------------------------------------------------------------------function v6col_intri(int $face, int $second, array $F): int{    $vb = V::$v6col_verts;    $P = $vb + 12 * (xread16($face + 0) & 0xffff);    $R = $vb + 12 * (xread16($face + ($second ? 6 : 2)) & 0xffff);    $Q = $vb + 12 * (xread16($face + 4) & 0xffff);    $p = [];    $q = [];    $r3 = [];    for ($i = 0; $i < 3; $i++) {        $p[$i] = xread16($P + $i * 2);        $q[$i] = xread16($Q + $i * 2);        $r3[$i] = xread16($R + $i * 2);    }    $pc = V::$v6col_pc;    $r2 = V::$v6col_r2;    for ($k = 0; $k < 3; $k++) {        $v = $k == 0 ? $p : ($k == 1 ? $q : $r3);        $d2 = 0;        for ($i = 0; $i < 3; $i++) {            $d = s32($v[$i] - $pc[$i]);            $d2 = s32($d2 + mul32($d, $d));        }        if (ule($d2, $r2)) {            return 1;        }    }    for ($k = 0; $k < 3; $k++) {        $a = $k == 0 ? $p : ($k == 1 ? $p : $q);        $b = $k == 0 ? $q : $r3;        $d2 = 0;        for ($i = 0; $i < 3; $i++) {            $d = s32((($a[$i] + $b[$i]) >> 1) - $pc[$i]);            $d2 = s32($d2 + mul32($d, $d));        }        if (ule($d2, $r2)) {            return 1;        }    }    $c0 = s32(($p[0] - $r3[0]) * ($q[1] - $r3[1]) - ($p[1] - $r3[1]) * ($q[0] - $r3[0]));    $denom = $c0;    $mag = $c0 < 0 ? -$c0 : $c0;    $axis = 0;    $c1 = s32(($p[0] - $r3[0]) * ($q[2] - $r3[2]) - ($p[2] - $r3[2]) * ($q[0] - $r3[0]));    $m1 = $c1 < 0 ? -$c1 : $c1;    if ($m1 >= $mag) {        $denom = $c1;        $mag = $m1;        $axis = 1;    }    $c2 = s32(($p[1] - $r3[1]) * ($q[2] - $r3[2]) - ($p[2] - $r3[2]) * ($q[1] - $r3[1]));    $m2 = $c2 < 0 ? -$c2 : $c2;    if ($m2 >= $mag) {        $denom = $c2;        $mag = $m2;        $axis = 2;    }    if ($denom == 0) {        return 0;    }    $ia = $axis == 2 ? 1 : 0;    $ib = $axis == 0 ? 1 : 2;    $n1 = s32(mul32($F[$ia] - $r3[$ia], $q[$ib] - $r3[$ib]) - mul32($F[$ib] - $r3[$ib], $q[$ia] - $r3[$ia]));    $n2 = s32(mul32($F[$ib] - $r3[$ib], $p[$ia] - $r3[$ia]) - mul32($F[$ia] - $r3[$ia], $p[$ib] - $r3[$ib]));    if ($denom < 0) {        $n1 = s32(-$n1);        $denom = s32(-$denom);        $n2 = s32(-$n2);    }    if (ult($denom, $n2)) {        return 0;    }    if (ult($denom, $n1)) {        return 0;    }    if (ult($denom, s32($n1 + $n2))) {        return 0;    }    return 1;}function v6col_matgate(int $face): int{    $id = 0;    $mat = V::$v6col_mats + xread8($face + 0xb) * 0x20;    if (xread8($mat + 0xa) == 0xff) {        return 0;    }    if (xread32($mat) != 0) {        $m16 = xread16($mat + 0x16) & 0xffff;        if ($m16 != 0 && (Alis::$v6tstmask & $m16) == 0) {            return 0;        }        $id = xread32($mat + 0x14);        if ($id == 0) {            $ext = $mat + xread32($mat);            $e16 = xread16($ext + 0xa) & 0xffff;            if ($e16 != 0 && (Alis::$v6tstmask & $e16) == 0) {                return 0;            }            $id = xread32($ext + 0x8);        }    }    V::$v6col_matout = $id;    return 1;}// sphere vs facefunction v6col_face(int $face): int{    $flags = xread16($face + 0xc) & 0xffff;    $vb = V::$v6col_verts;    $pc = V::$v6col_pc;    $r7 = V::$v6col_r7;    $r2 = V::$v6col_r2;    if ($flags & 0x200) {        $vA = $vb + 12 * (xread16($face + 0) & 0xffff);        $vB = $vb + 12 * (xread16($face + 2) & 0xffff);        $dx = (xread16($vA) - xread16($vB)) >> 2;        $half = $dx + ($dx >> 1);        $mx = ((xread16($vA + 0) + xread16($vB + 0)) >> 1) - $pc[0];        $my = ((xread16($vA + 2) + xread16($vB + 2)) >> 1) - $pc[1];        $d2xy = s32(mul32($my, $my) + mul32($mx, $mx));        $lim = s32(($r7 >> 7) + $half);        if (ult(mul32($lim, $lim), $d2xy)) {            return 0;        }        $z01 = shl32(((xread16($vA + 4) + xread16($vB + 4)) >> 1) - $pc[2], 7);        if ($z01 > $r7) {            return 0;        }        $vC = $vb + 12 * (xread16($face + 4) & 0xffff);        $vD = $vb + 12 * (xread16($face + 6) & 0xffff);        $z23 = shl32(-((((xread16($vC + 4) + xread16($vD + 4)) >> 1) - $pc[2])), 7);        if ($z23 > $r7) {            return 0;        }        return v6col_matgate($face);    }    $v0 = $vb + 12 * (xread16($face + 0) & 0xffff);    $reach = s32((xread8($v0 + 7) << 6) + V::$v6col_r);    $d2 = 0;    for ($i = 0; $i < 3; $i++) {        $d = s32(xread16($v0 + $i * 2) - $pc[$i]);        if (ult($reach, $d < 0 ? -$d : $d)) {            return 0;        }        $d2 = s32($d2 + mul32($d, $d));    }    $inside = 0;    if (ule($d2, $r2)) {        $inside = 1;    }    if (!$inside) {        $rr = xread8($v0 + 7) << 6;        $R2 = mul32($rr, $rr);        if (s32($d2 - $R2) >= $r2) {            return 0;        }        $nv = ($flags & 0x8000) ? 3 : 2;        for ($k = 1; $k <= $nv && !$inside; $k++) {            $vk = $vb + 12 * (xread16($face + $k * 2) & 0xffff);            $dk = 0;            for ($i = 0; $i < 3; $i++) {                $d = s32(xread16($vk + $i * 2) - $pc[$i]);                $dk = s32($dk + mul32($d, $d));            }            if (ule($dk, $r2)) {                $inside = 1;            }        }    }    if (!$inside) {        $dot = s32(mul32(s8(xread8($face + 8)), xread16($v0 + 0) - $pc[0])            + mul32(s8(xread8($face + 9)), xread16($v0 + 2) - $pc[1])            + mul32(s8(xread8($face + 10)), xread16($v0 + 4) - $pc[2]));        $ad = $dot < 0 ? s32(-$dot) : $dot;        if (ult($r7, $ad)) {            return 0;        }        $F = [];        $F[0] = s32((mul32(s8(xread8($face + 8)), $dot) >> 14) + $pc[0]);        $F[1] = s32((mul32(s8(xread8($face + 9)), $dot) >> 14) + $pc[1]);        $F[2] = s32((mul32(s8(xread8($face + 10)), $dot) >> 14) + $pc[2]);        if (!v6col_intri($face, 0, $F)) {            if (!($flags & 0x8000)) {                return 0;            }            if (!v6col_intri($face, 1, $F)) {                return 0;            }        }    }    return v6col_matgate($face);}function v6col_gridwalk(int $model, int $prad): int{    V::$v6col_facesA = $model + xread32($model + 0x3c);    V::$v6col_verts = $model + xread32($model + 0x38);    V::$v6col_mats = $model + xread32($model + 0x44);    $r = $prad & 0xffff;    $lo = [0, 0, 0];    $hi = [0, 0, 0];    static $orgo = [0x50, 0x58, 0x60], $maxo = [0x54, 0x5c, 0x64];    static $sho = [0x6d, 0x6e, 0x6f], $dimo = [0x70, 0x74, 0x78];    $pc = V::$v6col_pc;    for ($a = 0; $a < 3; $a++) {        $org = xread32($model + $orgo[$a]);        $mx = xread32($model + $maxo[$a]);        $sh = xread8($model + $sho[$a]) & 31;        $dim = xread32($model + $dimo[$a]);        $c = $pc[$a];        if ($c - $r > $mx) {            return 0;        }        $lo[$a] = s32($c - $r - $org) >> $sh;        if ($lo[$a] < 1) {            $lo[$a] = 0;        }        if ($c + $r < $org) {            return 0;        }        $hi[$a] = s32($c + $r - $org) >> $sh;        if ($hi[$a] >= $dim) {            $hi[$a] = $dim - 1;        }    }    $nfb = (((((xread16($model + 0x2e) + xread16($model + 0x30)) & 0xffff) >> 5) + 1) * 4);    if ($nfb > 0x2000) {        $nfb = 0x2000;    }    $fbits = &V::$v6col_fbits;    for ($i = 0; $i < $nfb; $i++) {        $fbits[$i] = 0;    }    $base = $model + xread32($model + 0x68);    $dim0 = xread32($model + 0x70);    $dim1 = xread32($model + 0x74);    $plane = mul32(mul32($dim1, $dim0), 4);    $rowst = mul32($dim0, 4);    for ($z = $lo[2]; $z <= $hi[2]; $z++) {        for ($y = $lo[1]; $y <= $hi[1]; $y++) {            $cp = s32($base + mul32($plane, $z) + mul32($rowst, $y) + $lo[0] * 4);            for ($x = $lo[0]; $x <= $hi[0]; $x++, $cp += 4) {                $off = xread32($cp);                if ($off == 0) {                    continue;                }                $fl = $model + $off;                $cnt = xread16($fl) & 0xffff;                $fl += 2;                for ($i = 0; $i < $cnt; $i++, $fl += 2) {                    $idx = xread16($fl) & 0xffff;                    if ($idx == 0xffff) {                        continue;                    }                    if (($idx >> 3) >= 0x2000) {                        continue;                    }                    if ($fbits[$idx >> 3] & (1 << ($idx & 7))) {                        continue;                    }                    $fbits[$idx >> 3] |= (1 << ($idx & 7));                    if (v6col_face(V::$v6col_facesA + $idx * 0x18)) {                        return 1;                    }                }            }        }    }    return 0;}function v6col_test_mesh(int $chain, int $prad, int $pcx, int $pcy, int $pcz, int $cand, int &$matout): int{    $matout = 0;    if (xread8($cand + 0x23) & 0x70) {        if (!v6x3d_cull_vs(V::$v6col_pw, $cand)) {            return 0;        }    }    $c = new sV6Ctx();    v6tf_build($chain, $cand, $c);    $pc = [s16($pcx), s16($pcy), s16($pcz)];    v6tf_apply($pc, $c, $pc);    V::$v6col_pc = $pc;    $d2 = 0;    for ($i = 0; $i < 3; $i++) {        $d = s32(xread16($cand + 0x42 + $i * 2) - $pc[$i]);        $d2 = s32($d2 + mul32($d, $d));    }    $prad &= 0xffff;    $rsum = s32((xread16($cand + 0x40) & 0xffff) + $prad);    if (mul32($rsum, $rsum) < $d2) {        return 0;    }    if (xread8($cand + 0x25) & 4) {        return 0;    }    V::$v6col_r = $prad;    V::$v6col_r7 = $prad << 7;    V::$v6col_r2 = $prad * $prad;    $res = xread32($cand + 0x3c);    $res = s32($res + xread32($res));    $hit = 0;    switch (xread8($res + 0x6c)) {        case 0:            V::$v6col_verts = $res + xread32($res + 0x38);            V::$v6col_mats = $res + xread32($res + 0x44);            $cnt = (xread16($res + 0x2e) + xread16($res + 0x30)) & 0xffff;            $face = $res + xread32($res + 0x3c);            for ($i = 0; $i < $cnt && !$hit; $i++, $face += 0x18) {                $hit = v6col_face($face);            }            break;        case 1:            $hit = v6col_gridwalk($res, $prad);            break;        default:            // portal-cell walk (FUN_00010854/104a9) not yet ported in the C either: no portal-mesh collision            break;    }    if (!$hit) {        return 0;    }    $matout = V::$v6col_matout;    return 1;}// sphere-probe world scanfunction v6col_sphere_scan(int $chain, int $srad, int $scx, int $scy, int $scz, int $selfid, int $scroff): void{    V::$v6tf_cacheon = 0;    v6col_probe_world($chain, $srad, $scx, $scy, $scz);    $selfid &= 0xffff;    $e = Alis::$basemain + ($scroff & 0xffff);    for (;;) {        $e = xread32($e + 0x38);        if ($e == 0 || $e == xread32($e + 0x38)) {            break;        }        if ((Alis::$v6tstmask & (xread16($e + 0x28) & 0xffff)) == 0) {            continue;        }        if (xread8($e + 0x24) & 0x40) {            continue;        }        if (xread8($e + 0x25) & 0x04) {            continue;        }        if ((xread16($e + 0x2a) & 0xffff) == $selfid) {            continue;        }        $t = xread8($e + 0x22);        if ($t == 0x33) {            if (!v6col_test_sphere($e, $srad)) {                continue;            }            $mat = xread32($e + 0x26);        } elseif ($t == 0x30) {            $fm = 0;            if (!v6col_test_mesh($chain, $srad, $scx, $scy, $scz, $e, $fm)) {                continue;            }            $mat = $fm ? $fm : xread32($e + 0x26);        } else {            continue;        }        if (v6ts_push(xread16($e + 0x2a) & 0xffff, $mat)) { // a112-a139            break;        }    }}// ---------------------------------------------------------------------------------------------// Depth-slot face lists, lights// ---------------------------------------------------------------------------------------------/** C (s64)num / den truncated toward zero, kept 64-bit (int, or float on 32-bit PHP). */function d3_tdiv(int|float $num, int|float $den): int|float{    if ($den == 0) {        return 0;    }    if (is_int($num) && is_int($den)) {        if ($num === PHP_INT_MIN && $den === -1) {            return $num;        }        return intdiv($num, $den);    }    $q = $num / $den;    return $q < 0 ? ceil($q) : floor($q);}function v6x3d_slotinsert(int $depth): int{    $off = $depth & ~3;    if (V::$v6x3d_arenacur >= V::$v6x3d_arenalim) {        // face arena overflow (perror 0xa): reusing last record        $rec = V::$v6x3d_arenacur - 0x50;    } else {        $rec = V::$v6x3d_arenacur;        V::$v6x3d_arenacur += 0x50;    }    xwrite32($rec, V::$v6x3d_slothead[$off >> 2]);    V::$v6x3d_slothead[$off >> 2] = $rec;    V::$v6x3d_slotdirty[$off >> 6] = 1;    V::$v6x3d_cntfaces++;    return $rec;}function v6x3d_lights_build(int $s): void{    V::$v6x3d_nlights = 0;    $fog = V::$v6x3dfog;    $fog->bands = xread16($s + 0x96) & 0xffff;    $fog->bandshift = $fog->bands <= 0x10 ? 4 : 5;    $fog->bandclamp = xread16($s + 0xae) & 0xffff;    $fog->mode = xread16($s + 0x94) & 0xffff;    $fog->ambbase = xread32($s + 0x146);    $fog->fogslope = xread32($s + 0xa6);    $fog->attslope = xread32($s + 0xaa);    $fog->fogstart = xread32($s + 0x146) >> 2;    $fog->baselevel = xread32($s + 0xa2);    $head = xread32($s + 0x5e);    if ($head == 0) {        return;    }    for ($e = xread32($head + 0x38); $e != 0 && $e != xread32($e + 0x38); $e = xread32($e + 0x38)) {        if (xread8($e + 0x22) != 0x10) {            continue;        }        if ((xread8($e + 0x23) & 0x70) && !v6x3d_cull($e)) {            continue;        }        $rec = V::$v6x3d_light[V::$v6x3d_nlights];        v6tf_build($e, $s, $rec->ctx);        $c = [xread16($e + 0x42), xread16($e + 0x44), xread16($e + 0x46)];        v6tf_apply($c, $rec->ctx, $c);        if ((xread16($e + 0x40) & 0xffff) != 0xffff) {            $dx = xread16($s + 0x42) - $c[0];            $dy = xread16($s + 0x44) - $c[1];            $dz = xread16($s + 0x46) - $c[2];            $d2 = $dx * $dx + $dy * $dy + $dz * $dz; // s64            if ($d2 >= 4294967296.0 || $d2 < 0) { // (u64)d2 >> 32                continue;            }            $lim = (xread16($s + 0x40) & 0xffff) + (xread16($e + 0x40) & 0xffff);            $lim = mul32($lim, $lim);            if (s32($d2) > $lim) {                continue;            }        }        $rec->elem = $e;        $rec->type = xread8($e + 0x22);        $rec->kind = 0;        $rec->range = xread16($e + 0x40) & 0xffff;        if (xread8($e + 0x56) == 1) {            v6tf_build($e, 0, V::$v6tf_camobj);            $rec->pos = [xread16($e + 0x42), xread16($e + 0x44), xread16($e + 0x46)];            v6tf_apply($rec->pos, V::$v6tf_camobj, $rec->pos);            $p = $rec->pos;            $m = $p[0] < 0 ? -$p[0] : $p[0];            $t = $p[1] < 0 ? -$p[1] : $p[1];            if ($t > $m) {                $m = $t;            }            $t = $p[2] < 0 ? -$p[2] : $p[2];            if ($t > $m) {                $m = $t;            }            if ($m == 0) {                $m = 1;            }            $m = s32(-$m);            $rec->dir = [cdiv(shl32($p[0], 7), $m), cdiv(shl32($p[1], 7), $m), cdiv(shl32($p[2], 7), $m)];            $rec->slope = 0;            if ($fog->mode != 0) {                $rec->slope = $fog->ambbase >> 1;            }            $rec->kind = 1;            $rec->spot = 0;            $rec->intensity = mul32(xread16($e + 0x48), $fog->bands);            v6tf_build(0, $s, $rec->ctx);            $t0 = [0, 0, 0];            v6tf_apply([0, 0, 0], $rec->ctx, $t0);            $cd = [0, 0, 0];            v6tf_apply($rec->dir, $rec->ctx, $cd);            $rec->camdir = [s32($cd[0] - $t0[0]), s32($cd[1] - $t0[1]), s32($cd[2] - $t0[2])];        } else {            $rec->pos = [xread16($e + 0x42), xread16($e + 0x44), xread16($e + 0x46)];            $rec->dir = [s8(xread8($e + 0x50)), s8(xread8($e + 0x51)), s8(xread8($e + 0x52))];            $rec->spot = 0;            $cone = xread8($e + 0x53);            if ($cone < 0x7f) {                $rec->spot = 1;                $rec->cone = Deus3dConst::v6costab[($cone >> 1) << 4] >> 8;            }            $rec->intensity = mul32(xread16($e + 0x48), $fog->bands);            $rec->fstart = $rec->range;            $fs = xread16($e + 0x54) & 0xffff;            if ($fs < 0xc8) {                $fs = 0xc8;            }            if ($fs < $rec->range) {                $rec->fstart = $fs;                $span = $rec->range - $fs;                $rec->int8fp = shl32($rec->intensity, 8);                $rec->slope = s32(-cdiv($rec->int8fp, $span));            }            v6tf_apply($rec->pos, $rec->ctx, $rec->campos);            if ($rec->spot) {                v6tf_apply($rec->dir, $rec->ctx, $rec->camdir);            }        }        if (++V::$v6x3d_nlights >= 0x40) {            break;        }    }}function v6x3d_framereset(int $s): void{    V::$v6x3d_cntfaces = V::$v6x3d_cntverts = 0;    $h = xread32($s + 0xbc);    if ($h != 0) {        $a = xread32($h);        V::$v6x3d_arena = $a;        V::$v6x3d_arenalim = $a + 0x1d4c0;        V::$v6x3d_shl16 = $a + 0x1d4c0;    } else {        V::$v6x3d_arena = 0;    }    $head = &V::$v6x3d_slothead;    $dirty = &V::$v6x3d_slotdirty;    for ($g = 0; $g < 0x200; $g++) {        if ($dirty[$g]) {            for ($i = $g * 16, $e = $i + 16; $i < $e; $i++) {                $head[$i] = 0;            }            $dirty[$g] = 0;        }    }    V::$v6x3d_arenacur = V::$v6x3d_arena;    v6tf_frame_setup($s);    v6x3d_lights_build($s);}// ---------------------------------------------------------------------------------------------// Rasteriser: edges, rows (C sV6Row v6x3d_rows[500] as one PHP array per field), spans// ---------------------------------------------------------------------------------------------// face-record vertex-node copy accessorsfunction v6nd_x(int $n): int{    return xread16($n + 0);}function v6nd_d(int $n): int{    return xread16($n + 2) & 0xffff;}function v6nd_y(int $n): int{    return xread16($n + 4);}// write the -1 X markers into a rowfunction v6x3d_rowterm(int $ri): void{    if ($ri >= 0 && $ri < 500) {        V::$row_lx[$ri] = -1;        V::$row_rx[$ri] = -1;    }}function v6x3d_edgebuild(int $side, int $na, int $nb): int{    $E = V::$ws_e[$side];    $dy = s16(v6nd_y($nb) - v6nd_y($na));    if ($dy < 0) {        $tn = $na;        $na = $nb;        $nb = $tn;        $dy = s16(-$dy);    }    $E->lower = $nb;    $E->rows = $dy;    if ($dy == 0) {        return 1;    }    $E->fresh = 1;    $xa = v6nd_x($na);    $E->x = shl32($xa, 16);    $E->dx = cdiv(shl32(v6nd_x($nb) - $xa, 16), $dy);    if (!(V::$v6x3d_ae98 & 0x80)) {        $ua = xread8($na + 6);        $va = xread8($na + 7);        $E->u = ($ua << 8) & 0xffff;        $E->du = s16(cdiv((xread8($nb + 6) - $ua) << 8, $dy));        $E->v = ($va << 8) & 0xffff;        $E->dv = s16(cdiv((xread8($nb + 7) - $va) << 8, $dy));    } else {        $E->i = 0;        $uu = xread8($na + 6);        $du = s16(xread8($nb + 6) - $uu);        $vv = xread8($na + 7);        $dv = s16(xread8($nb + 7) - $vv);        if (V::$v6x3d_ae5f) {            $uu = s16($uu * 2);            $vv = s16($vv * 2);            $du = s16($du * 2);            $dv = s16($dv * 2);        }        $E->u15 = shl32($uu, 15);        $E->du4 = $du << 4;        $E->v15 = shl32($vv, 15);        $E->dv4 = $dv << 4;        $za = v6nd_d($na);        $zb = v6nd_d($nb);        $E->z15 = shl32($za, 15);        $E->dz4 = ($zb - $za) << 4;        $zdiv = $za ? $za : 1;        $r = intdiv($zb << 11, $zdiv);        $E->rdy = mul32($r, $dy);        $E->c800r = s32(0x800 - $r);    }    $ba = xread8($na + 0xb);    $db = s16(cdiv((xread8($nb + 0xb) - $ba) << 8, $dy));    if ($db < 0) {        $db = s16($db + 2);    }    $db = s16($db & ~1);    $E->band = ($ba << 8) & 0xffff;    $E->dband = $db;    return 0;}function v6x3d_segment(): int{    $e0 = V::$ws_e[0];    $e1 = V::$ws_e[1];    $n = $e0->rows < $e1->rows ? $e0->rows : $e1->rows;    $e0->rows -= $n;    $e1->rows -= $n;    if ($n <= 0) {        return 1;    }    V::$ws_n = $n;    $y = V::$ws_y;    if (s16($y) > ImageV6Vars::$v6xclipy2) {        return 1;    }    $cy1 = ImageV6Vars::$v6xclipy1;    if (s16($y + $n) >= $cy1) {        $over = ($y + $n) - ImageV6Vars::$v6xclipy2;        if ($over > 0) {            V::$ws_n -= ($over - 1);        }        $skip = $cy1 - $y;        if ($skip <= 0) {            goto rows;        }    } else {        $skip = $n;    }    $persp = V::$v6x3d_ae98 & 0x80;    foreach ([$e0, $e1] as $E) {        $E->x = s32($E->x + mul32($E->dx, $skip));        $E->band = ($E->band + $E->dband * $skip) & 0xffff;        if (!$persp) {            $E->u = ($E->u + $E->du * $skip) & 0xffff;            $E->v = ($E->v + $E->dv * $skip) & 0xffff;        } else {            $E->i += $skip;        }    }    if (s16(V::$ws_y + V::$ws_n) < $cy1) {        return 1;    }    V::$ws_n -= $skip;    V::$ws_y += $skip;    rows:    V::$ws_cur = V::$ws_y;    V::$ws_end = V::$ws_y + V::$ws_n;    if (V::$ws_first == -1) {        V::$ws_first = V::$ws_cur;    }    return 0;}function v6x3d_orderfix(): void{    $n = V::$ws_n;    $e0 = V::$ws_e[0];    $e1 = V::$ws_e[1];    $doswap = 0;    if ($e1->x < $e0->x) {        $doswap = 1;    } elseif ($e1->x == $e0->x && ($e1->x + $e1->dx * $n) < ($e0->x + $e0->dx * $n)) {        $doswap = 1;    }    if ($doswap) {        V::$ws_e = [$e1, $e0];    }    if (V::$v6x3d_ae98 & 0x10) {        return;    }    $persp = V::$v6x3d_ae98 & 0x80;    for ($s = 0; $s < 2; $s++) {        $E = V::$ws_e[$s];        if (!$E->fresh) {            continue;        }        $E->fresh = 0;        $E->x = s32($E->x + 0x10000);        $step = ($s == 0) ? ($E->dx < 0) : ($E->dx >= 0);        if ($step) {            $E->x = s32($E->x + $E->dx);            $E->band = ($E->band + $E->dband) & 0xffff;            if (!$persp) {                $E->u = ($E->u + $E->du) & 0xffff;                $E->v = ($E->v + $E->dv) & 0xffff;            } else {                $E->i++;            }        }    }}function v6x3d_emit(): void{    $fl = V::$v6x3d_ae98;    $uvlin = ($fl & 6) != 0 && !($fl & 0x80);    $persp = ($fl & 0x80) != 0;    $ri = V::$ws_cur;    if ($ri < 0 || V::$ws_end > 499) {        if ($ri < 0) {            $ri = 0;        }        if (V::$ws_end > 499) {            V::$ws_end = 499;        }    }    $e0 = V::$ws_e[0];    $e1 = V::$ws_e[1];    $end = V::$ws_end;    $bandstep = !(V::$v6x3d_ae9c == 0 || V::$v6x3d_ae9c == 2 || V::$v6x3d_ae9c == 8);    for (; $ri < $end; $ri++) {        V::$row_lx[$ri] = $e0->x;        V::$row_rx[$ri] = $e1->x;        V::$row_lband[$ri] = $e0->band;        V::$row_rband[$ri] = $e1->band;        if ($uvlin) {            V::$row_lv[$ri] = $e0->v;            V::$row_lu[$ri] = $e0->u;            V::$row_rv[$ri] = $e1->v;            V::$row_ru[$ri] = $e1->u;        } elseif ($persp) {            for ($s = 0; $s < 2; $s++) {                $E = $s ? $e1 : $e0;                $den = $E->i * $E->c800r + $E->rdy; // s64                if ($den <= 0) {                    $den = 1;                }                // i < 512 and den < 2^31: (i << 22) / den < 2^31, so divs.l needs no saturation                if ($E->i >= 0 && $E->i < 512 && $den <= 0x7fffffff) {                    $t = d3_div64($E->i << 22, (int) $den);                } else {                    $q = d3_tdiv($E->i * 4194304, $den);                    $t = $q > 0x7fffffff ? 0x7fffffff : s32($q);                }                // only the low 32 bits are kept, 32-bit products are exact                $vv = s32($E->v15 + mul32($t, $E->dv4));                $uu = s32($E->u15 + mul32($t, $E->du4));                $zz = s32($E->z15 + mul32($t, $E->dz4));                if ($s == 0) {                    V::$row_lv15[$ri] = $vv;                    V::$row_lu15[$ri] = $uu;                    V::$row_lz15[$ri] = $zz;                } else {                    V::$row_rv15[$ri] = $vv;                    V::$row_ru15[$ri] = $uu;                    V::$row_rz15[$ri] = $zz;                }            }        }        foreach ([$e0, $e1] as $E) {            $E->x = s32($E->x + $E->dx);            if ($bandstep) {                $E->band = ($E->band + $E->dband) & 0xffff;            }            if ($uvlin) {                $E->u = ($E->u + $E->du) & 0xffff;                $E->v = ($E->v + $E->dv) & 0xffff;            }            if ($persp) {                $E->i++;            }        }    }    v6x3d_rowterm($ri);    V::$ws_cur = $ri;}function v6x3d_span_flat(int $first, int $end): void{    $fl = V::$v6x3d_ae98;    $fog = V::$v6x3d_fogbase;    $colour = xread8(V::$v6x3d_res + 0x1c);    $mix = ($fl & 0x10) ? $fog - 0x10000 : 0;    $cx1 = ImageV6Vars::$v6xclipx1;    $cx2 = ImageV6Vars::$v6xclipx2;    $logic = Img::$logic;    $pitch = Alis::$platform->width;    $m = &Alis::$mem;    for ($ri = $first; $ri < $end; $ri++) {        $lx = s16(V::$row_lx[$ri] >> 16);        $rx = s16(V::$row_rx[$ri] >> 16);        if ($lx > $cx2 || $rx < $cx1) {            continue;        }        $len = $rx - $lx;        if ($len <= 0) {            continue;        }        $dband = s16(cdiv(s16(V::$row_rband[$ri]) - s16(V::$row_lband[$ri]), $len));        $band = V::$row_lband[$ri];        if ($lx < $cx1) {            $dx = $cx1 - $lx;            $band = ($band + $dband * $dx) & 0xffff;            $lx = $cx1;        }        if ($rx >= $cx2) {            $rx = s16($cx2 + 1);        }        $len = $rx - $lx;        if ($len <= 0) {            continue;        }        $c = $colour;        if ($fl & 0x20) {            $lx = s16($lx & ~1);            $units = (($rx - $lx + 1) & 0xffff) >> 1;            $si = s16($dband * 2);            $dst = $logic + V::$row_rowoff[$ri] * 2 + $lx;            for ($k = 0; $k < $units; $k++, $dst += 2) {                $out = $m[$fog + (($band & 0xff00) | $c)];                $band = ($band + $si) & 0xffff;                if ($mix) {                    $out = $m[$mix + ((ord($m[$dst]) << 8) | ord($out))];                }                $m[$dst] = $out;                $m[$dst + 1] = $out;                $m[$dst + $pitch] = $out;                $m[$dst + $pitch + 1] = $out;            }            continue;        }        if (!($fl & 4)) {            $c = ord($m[$fog + (($band & 0xff00) | $c)]);        }        $dst = $logic + V::$row_rowoff[$ri] + $lx;        if ($fl & 0x40) {            $units = $len >> 1;            $si = s16($dband * 2);            $a = "\0";            for ($k = 0; $k < $units; $k++, $dst += 2) {                $out = ord($m[$fog + (($band & 0xff00) | $c)]);                $band = ($band + $si) & 0xffff;                $a = $m[$mix + ((ord($m[$dst]) << 8) | $out)];                $m[$dst] = $m[$mix + ((ord($a) << 8) | ord($m[$dst - 1]))];                $m[$dst + 1] = $a;            }            if ($len & 1) {                $m[$dst] = $a;            }            continue;        }        if ($mix) {            for ($nn = $len; $nn > 0; $nn--, $dst++) {                $out = ord($m[$fog + (($band & 0xff00) | $c)]);                $m[$dst] = $m[$mix + ((ord($m[$dst]) << 8) | $out)];                $band = ($band + $dband) & 0xffff;            }        } else {            for ($nn = $len; $nn > 0; $nn--, $dst++) {                $m[$dst] = $m[$fog + (($band & 0xff00) | $c)];                $band = ($band + $dband) & 0xffff;            }        }    }}/** * C v6x3d_run(): plain textured run, n >= 1 pixels, keyed skips texel 0. * PORT: the C u32 uvb (band 8.8 << 16 | v 8.8, masked by 0xfffe00ff | vmask) is split in two 16-bit * halves, $uh (band) and $ul (v): the mask clears bit 16 and the band step is even, so no carry ever * crosses between them (identical results, no u32 overflow on 32-bit PHP). Returns $uh (C: uvb >> 16). */function v6x3d_run(int $dst, int $tex, int $fog, int $uh, int $ul, int $sh, int $sl, int $uS, int $duS, int $lmask, int $W, int $n, int $keyed): int{    $m = &Alis::$mem;    $rs = 16 - $W;    if ($keyed) {        for (; $n > 0; $n--, $dst++) {            $texel = ord($m[$tex + (((($ul >> 8) & 0xff) << $W) | ($uS >> $rs))]);            if ($texel) {                $m[$dst] = $m[$fog + (($uh & 0xff00) | $texel)];            }            $uh = ($uh + $sh) & 0xfffe;            $ul = ($ul + $sl) & $lmask;            $uS = ($uS + $duS) & 0xffff;        }    } else {        for (; $n > 0; $n--, $dst++) {            $m[$dst] = $m[$fog + (($uh & 0xff00) | ord($m[$tex + (((($ul >> 8) & 0xff) << $W) | ($uS >> $rs))]))];            $uh = ($uh + $sh) & 0xfffe;            $ul = ($ul + $sl) & $lmask;            $uS = ($uS + $duS) & 0xffff;        }    }    return $uh;}function v6x3d_span_tex(int $first, int $end): void{    $fl = V::$v6x3d_ae98;    $W = V::$v6x3d_texw;    $H = V::$v6x3d_texh;    if ($W < 1 || $W > 8 || $H < 1 || $H > 8) {        return; // bad texture log2 dims    }    $tex = V::$v6x3d_res + 0x20;    $fog = V::$v6x3d_fogbase;    $lut = $fog - 0x10000;    $mix = ($fl & 0x10) ? $lut : 0;    $keyed = ($fl & 1) == 0 && !$mix ? 1 : 0;    $vmaskw = ((1 << $H) - 1) << 8;    $lmask = 0x00ff | $vmaskw; // C mask = 0xfffe00ff | vmaskw: high half 0xfffe    $cx1 = ImageV6Vars::$v6xclipx1;    $cx2 = ImageV6Vars::$v6xclipx2;    $ae5f = V::$v6x3d_ae5f;    $logic = Img::$logic;    $pitch = Alis::$platform->width;    $rs = 16 - $W;    $m = &Alis::$mem;    for ($ri = $first; $ri < $end; $ri++) {        $lx = s16(V::$row_lx[$ri] >> 16);        $rx = s16(V::$row_rx[$ri] >> 16);        if ($lx > $cx2 || $rx < $cx1) {            continue;        }        $len = $rx - $lx;        if ($len <= 0) {            continue;        }        $dur = V::$row_ru[$ri] - V::$row_lu[$ri];        $dvr = V::$row_rv[$ri] - V::$row_lv[$ri];        if ($ae5f) {            $dur *= 2;            $dvr *= 2;        }        $du = s16(intdiv($dur, $len));        $dv = s16(intdiv($dvr, $len));        $dband = 0;        if ($fl & 4) {            $bd = s16(V::$row_rband[$ri]) - s16(V::$row_lband[$ri]);            $mag = intdiv($bd < 0 ? -$bd : $bd, $len) & 0xfffe;            $dband = s16($bd < 0 ? -$mag : $mag);        }        $u0 = V::$row_lu[$ri];        $v0 = V::$row_lv[$ri];        $band = V::$row_lband[$ri];        if ($lx < $cx1) {            $dx = $cx1 - $lx;            $duc = $ae5f ? ($du >> 1) : $du;            $dvc = $ae5f ? ($dv >> 1) : $dv;            $u0 = ($u0 + $duc * $dx) & 0xffff;            $v0 = ($v0 + $dvc * $dx) & 0xffff;            $band = ($band + $dband * $dx) & 0xffff;            $lx = $cx1;        }        if ($rx >= $cx2) {            $rx = s16($cx2 + 1);        }        $len = $rx - $lx;        if ($len <= 0) {            continue;        }        if ($ae5f) {            $u0 = ($u0 << 1) & 0xffff;            $v0 = ($v0 << 1) & 0xffff;        }        $blk = ($fl & 0x20) ? 1 : 0;        $half = !$blk && ($fl & 0x40);        $interp = $fl & 4;        $units = $len;        if ($blk) {            $lx = s16($lx & ~1);            $units = ($rx - $lx + ($interp ? 1 : 0)) >> 1;            $du = s16($du * 2);            $dv = s16($dv * 2);            $dband = s16($dband * 2);            $dst = $logic + V::$row_rowoff[$ri] * 2 + $lx;        } elseif ($half) {            $units = $len >> 1;            if ($units == 0) {                continue;            }            $du = s16($du * 2);            $dv = s16($dv * 2);            $dband = s16($dband * 2);            $dst = $logic + V::$row_rowoff[$ri] + $lx;        } else {            $dst = $logic + V::$row_rowoff[$ri] + $lx;        }        $uS = ($u0 << (8 - $W)) & 0xffff;        $duS = ($du << (8 - $W)) & 0xffff;        $uh = $band & 0xfffe;            // uvb >> 16        $ul = $v0 & $lmask;              // uvb & 0xffff        $sh = $dband & 0xffff;           // step >> 16        $sl = $dv & 0xffff;              // step & 0xffff        $bconst = ($band >> 8) & 0xff;        if (!$half && !$blk && !$mix) {            if ($units > 0) {                v6x3d_run($dst, $tex, $fog, $uh, $ul, $interp ? $sh : 0, $sl, $uS, $duS, $lmask, $W, $units, $keyed);            }            continue;        }        $al = 0;        $ah = 0;        $idx = 0;        for ($nn = $units; $nn > 0; $nn--) {            $idx = ((($ul >> 8) & 0xff) << $W) | ($uS >> $rs);            $texel = ord($m[$tex + $idx]);            $bi = $interp ? ($uh >> 8) : $bconst;            $out = ord($m[$fog + (($bi << 8) | $texel)]);            if ($half) {                if ($mix) {                    $a = ord($m[$mix + ((ord($m[$dst]) << 8) | $out)]);                    $m[$dst] = $m[$mix + (($a << 8) | ord($m[$dst - 1]))];                    $m[$dst + 1] = chr($a);                    $ah = $a;                } elseif ($interp && $keyed) {                    if ($texel) {                        $al = ord($m[$lut + (($out << 8) | ord($m[$dst - 1]))]);                        $m[$dst + 2] = chr($al);                        $m[$dst + 3] = chr($out);                        $ah = $out;                    } else {                        $al = $idx & 0xff;                        $ah = ($idx >> 8) & 0xff;                    }                } elseif (!$keyed || $out) {                    $m[$dst] = $m[$lut + (($out << 8) | ord($m[$dst - 1]))];                    $m[$dst + 1] = chr($out);                    $ah = $out;                }                $dst += 2;            } elseif ($blk) {                if ($mix) {                    $mm = $m[$mix + ((ord($m[$dst + ($interp ? 1 : 0)]) << 8) | $out)];                    $m[$dst] = $mm;                    $m[$dst + 1] = $mm;                    $m[$dst + $pitch] = $mm;                    $m[$dst + $pitch + 1] = $mm;                } elseif (!$keyed || $texel != 0) {                    $o = chr($out);                    $m[$dst] = $o;                    $m[$dst + 1] = $o;                    $m[$dst + $pitch] = $o;                    $m[$dst + $pitch + 1] = $o;                }                $dst += 2;            } else {                if (!$keyed || $texel != 0) {                    $m[$dst] = $mix ? $m[$mix + ((ord($m[$dst]) << 8) | $out)] : chr($out);                }                $dst++;            }            $uh = ($uh + $sh) & 0xfffe;            $ul = ($ul + $sl) & $lmask;            $uS = ($uS + $duS) & 0xffff;        }        if ($half && ($len & 1)) {            if (!$interp) {                $idxn = ((($ul >> 8) & 0xff) << $W) | ($uS >> $rs);                $al = $idxn & 0xff;                $ah = ($idxn >> 8) & 0xff;            }            if ($mix) {                $test = $interp ? $ah : $al;            } elseif ($keyed) {                $test = $al;            } else {                $test = 1;            }            if ($test) {                $m[$dst] = chr($ah);            }        }    }}function v6p_at(int $aL, int $aR, int $d, int $len, int $zL, int $zR): int{    $t = $d * $zL;                 // s64    $den = ($len - $d) * $zR + $t; // s64    if ($den <= 0) {        $den = 1;    }    if ($den <= 0x7fffffff && $t >= 0 && $t <= $den) {        // PORT: t * (aR - aL) as a float on 32-bit PHP (exact below 2^53)        return s32($aL + d3_div64(s32($t) * ($aR - $aL), (int) $den));    }    return s32($aL + d3_tdiv($t * ($aR - $aL), $den));}function v6p_step(int $delta, int $cw): int{    if ($cw == 16) {        return s16($delta >> 4);    }    $mag = ($delta < 0 ? -$delta : $delta) & 0xffff;    return s16($delta < 0 ? -intdiv($mag, $cw) : intdiv($mag, $cw));}function v6x3d_span_persp(int $first, int $end): void{    $fl = V::$v6x3d_ae98;    $W = V::$v6x3d_texw;    $H = V::$v6x3d_texh;    if ($W < 1 || $W > 8 || $H < 1 || $H > 8) {        return; // bad texture log2 dims    }    $tex = V::$v6x3d_res + 0x20;    $fog = V::$v6x3d_fogbase;    $lut = $fog - 0x10000;    $mix = ($fl & 0x10) ? $lut : 0;    $keyed = ($fl & 1) == 0 && !$mix ? 1 : 0;    $blk = ($fl & 0x20) != 0;    $half = !$blk && ($fl & 0x40);    $vmaskw = ((1 << $H) - 1) << 8;    $lmask = 0x00ff | $vmaskw;    $pitch = Alis::$platform->width;    $cx1 = ImageV6Vars::$v6xclipx1;    $cx2 = ImageV6Vars::$v6xclipx2;    $logic = Img::$logic;    $rs = 16 - $W;    $ustep = &V::$v6p_ustep;    $m = &Alis::$mem;    for ($ri = $first; $ri < $end; $ri++) {        $rawlx = s16(V::$row_lx[$ri] >> 16);        $rawrx = s16(V::$row_rx[$ri] >> 16);        if ($rawlx > $cx2 || $rawrx < $cx1) {            continue;        }        $len = $rawrx - $rawlx;        if ($len <= 0) {            continue;        }        $bd = s16(V::$row_rband[$ri]) - s16(V::$row_lband[$ri]);        $mag = intdiv($bd < 0 ? -$bd : $bd, $len) & 0xfffe;        $dband = s16($bd < 0 ? -$mag : $mag);        $band = V::$row_lband[$ri];        $lx = $rawlx;        $rx = $rawrx;        if ($lx < $cx1) {            $band = ($band + $dband * ($cx1 - $lx)) & 0xffff;            $lx = $cx1;        }        if ($rx > $cx2) {            $rx = s16($cx2 + 1);        }        $count = $rx - $lx;        if ($count <= 0) {            continue;        }        if ($blk) {            $lx = s16($lx & ~1);            $units = (($rx - $lx + 1) & 0xffff) >> 1;            $dst = $logic + V::$row_rowoff[$ri] * 2 + $lx;        } elseif ($half) {            $units = $count >> 1;            if ($units == 0) {                continue;            }            $dst = $logic + V::$row_rowoff[$ri] + $lx;        } else {            $units = $count;            $dst = $logic + V::$row_rowoff[$ri] + $lx;        }        if ($blk || $half) {            $dband = s16($dband * 2);        }        if ($blk || $half) {            $l2 = $rawlx & ~1;            $done = shr32($lx - $l2, 1);            $lenE = shr32($rawrx - $l2, 1);        } else {            $done = $lx - $rawlx;            $lenE = $len;        }        if ($lenE == $done) {            $lenE = $done + 1;        }        $uL = shr32(V::$row_lu15[$ri], 7);        $uR = shr32(V::$row_ru15[$ri], 7);        $vL = shr32(V::$row_lv15[$ri], 7);        $vR = shr32(V::$row_rv15[$ri], 7);        $zL = V::$row_lz15[$ri] >> 8;        $zR = V::$row_rz15[$ri] >> 8;        $u0 = v6p_at($uL, $uR, $done, $lenE, $zL, $zR);        $v0 = v6p_at($vL, $vR, $done, $lenE, $zL, $zR);        $chunk = 0;        for ($left = $units; $left > 0; $chunk++) {            $cw = $left & 0xf;            if ($cw == 0) {                $cw = 16;            }            $u1 = v6p_at($uL, $uR, $done + $cw, $lenE, $zL, $zR);            $v1 = v6p_at($vL, $vR, $done + $cw, $lenE, $zL, $zR);            $du = v6p_step(s32($u1 - $u0), $cw);            $dv = v6p_step(s32($v1 - $v0), $cw);            if ($chunk < 64) {                $ustep[$chunk] = $du & 0xffff;            }            $uS = (($u0 & 0xffff) << (8 - $W)) & 0xffff;            $duS = ($du << (8 - $W)) & 0xffff;            $uh = $band & 0xfffe;            $ul = $v0 & $lmask;            $sh = $dband & 0xffff;            $sl = $dv & 0xffff;            if (!$half && !$blk && !$mix) {                $uh = v6x3d_run($dst, $tex, $fog, $uh, $ul, $sh, $sl, $uS, $duS, $lmask, $W, $cw, $keyed);                $dst += $cw;                $band = $uh;                $u0 = $u1;                $v0 = $v1;                $done += $cw;                $left -= $cw;                continue;            }            for ($nn = $cw; $nn > 0; $nn--) {                $texel = ord($m[$tex + (((($ul >> 8) & 0xff) << $W) | ($uS >> $rs))]);                $out = ord($m[$fog + (($uh & 0xff00) | $texel)]);                if ($half) {                    if ($mix) {                        $m1 = ord($m[$mix + ((ord($m[$dst]) << 8) | $out)]);                        $m[$dst] = $m[$mix + (($m1 << 8) | ord($m[$dst - 1]))];                        $m[$dst + 1] = chr($m1);                    } elseif ($keyed) {                        if ($texel) {                            $m[$dst + 2] = $m[$lut + (($out << 8) | ord($m[$dst - 1]))];                            $m[$dst + 3] = chr($out);                        }                    } else {                        $m[$dst] = $m[$lut + (($out << 8) | ord($m[$dst - 1]))];                        $m[$dst + 1] = chr($out);                    }                    $dst += 2;                } elseif ($blk) {                    if ($mix) {                        $mm = $m[$mix + ((ord($m[$dst + 1]) << 8) | $out)];                        $m[$dst] = $mm;                        $m[$dst + 1] = $mm;                        $m[$dst + $pitch] = $mm;                        $m[$dst + $pitch + 1] = $mm;                    } elseif (!$keyed || $texel != 0) {                        $o = chr($out);                        $m[$dst] = $o;                        $m[$dst + 1] = $o;                        $m[$dst + $pitch] = $o;                        $m[$dst + $pitch + 1] = $o;                    }                    $dst += 2;                } else {                    if (!$keyed || $texel != 0) {                        $m[$dst] = $mix ? $m[$mix + ((ord($m[$dst]) << 8) | $out)] : chr($out);                    }                    $dst++;                }                $uh = ($uh + $sh) & 0xfffe;                $ul = ($ul + $sl) & $lmask;                $uS = ($uS + $duS) & 0xffff;            }            $band = $uh;            $u0 = $u1;            $v0 = $v1;            $done += $cw;            $left -= $cw;        }        if ($half && ($count & 1)) {            $ax = $chunk < 64 ? (($ustep[$chunk] << (8 - $W)) & 0xffff) : 0;            $al = $ax & 0xff;            $ah = $ax >> 8;            $test = $mix ? $ah : ($keyed ? $al : 1);            if ($test) {                $m[$dst] = chr($ah);            }        }    }}// span drawer dispatchfunction v6x3d_spans(int $first, int $end): void{    $fl = V::$v6x3d_ae98;    if (V::$v6x3d_pick) {        V::$v6x3d_pickhit = 0;        $pr = V::$v6x3d_picky;        if ($fl & 0x20) {            $pr >>= 1;        }        if ($pr < $first || $pr > $end) {            return;        }        $lx = s16((V::$row_lx[$pr] ?? 0) >> 16);        $rx = s16((V::$row_rx[$pr] ?? 0) >> 16);        if ($lx == $rx) {            return;        }        $xmin = $lx;        $xmax = $rx;        if ($xmin > $xmax) {            $t = $xmin;            $xmin = $xmax;            $xmax = $t;        }        $px = V::$v6x3d_pickx;        if ($xmin > $px || $xmax < $px) {            return;        }        if (($fl & 1) || !($fl & 2)) {            V::$v6x3d_pickhit = 1;            return;        }        $len = $xmax - $xmin;        $frac = cdiv(($px - $xmin) << 8, $len);        // IDIV (len > 0)        $lu = V::$row_lu[$pr];        $lv = V::$row_lv[$pr];        $uu = (((((V::$row_ru[$pr] - $lu) * $frac) >> 8) + $lu) >> 8);        $vv = (((((V::$row_rv[$pr] - $lv) * $frac) >> 8) + $lv) >> 8);        $idx = s32(shl32($vv, V::$v6x3d_texw) + $uu);        V::$v6x3d_pickhit = xread8(V::$v6x3d_res + 0x20 + $idx) != 0 ? 1 : 0;        return;    }    if (V::$v6x3d_fogbase == 0) {        return; // no SHAD fog LUT (screen+0xb4): spans dropped    }    if ($fl & 0x08) {        return; // bit3 reserved bank reached: dropped    }    if (($fl & 6) == 0 && ($fl & 0x70) == 0x40) {        return; // flat pair bank is unreachable in DOS: spans dropped    }    if (($fl & 2) == 0) {        v6x3d_span_flat($first, $end);    } elseif ($fl & 0x80) {        v6x3d_span_persp($first, $end);    } else {        v6x3d_span_tex($first, $end);    }}function v6x3d_tridrv(int $rec): void{    $A = $rec + 0x08;    $B = $rec + 0x14;    $C = $rec + 0x20;    V::$ws_y = v6nd_y($A);    V::$ws_n = 0;    V::$ws_first = -1;    V::$ws_cur = 0;    V::$ws_end = 0;    v6x3d_edgebuild(0, $A, $B);    v6x3d_edgebuild(1, $A, $C);    if (!v6x3d_segment()) {        v6x3d_orderfix();        v6x3d_emit();    }    V::$ws_y += V::$ws_n;    if (s16(V::$ws_y) <= ImageV6Vars::$v6xclipy2) {        if (V::$ws_e[0]->rows > V::$ws_e[1]->rows) {            v6x3d_edgebuild(1, $C, $B);        } else {            v6x3d_edgebuild(0, $B, $C);        }        if (!v6x3d_segment()) {            v6x3d_orderfix();            v6x3d_emit();        }    }    if (V::$ws_first != -1 && V::$ws_first < V::$ws_end) {        v6x3d_spans(V::$ws_first, V::$ws_end);    }}// QUAD driverfunction v6x3d_quaddrv(int $rec): void{    $A = $rec + 0x08;    $B = $rec + 0x14;    $C = $rec + 0x20;    $D = $rec + 0x2c;    $ebx = $B;    $ecx = $C;    $edx = $D;    if (!(v6nd_y($ebx) > v6nd_y($edx))) {        $t = $ebx;        $ebx = $edx;        $edx = $t;    }    if (!(v6nd_y($ebx) > v6nd_y($ecx))) {        $t = $ebx;        $ebx = $ecx;        $ecx = $t;    }    $ymaxn = $ebx;    if (v6nd_y($ecx) > v6nd_y($edx)) {        $swap_cd = 0;    } elseif (v6nd_y($ecx) != v6nd_y($edx)) {        $swap_cd = 1;    } elseif (v6nd_y($ecx) != v6nd_y($A)) {        $swap_cd = 1;    } elseif (!(v6nd_x($A) < v6nd_x($ecx))) {        $swap_cd = 0;    } else {        $swap_cd = (v6nd_x($ecx) < v6nd_x($edx)) ? 1 : 0;    }    if ($swap_cd) {        $t = $ecx;        $ecx = $edx;        $edx = $t;    }    $mid = $ecx;    V::$ws_y = v6nd_y($A);    V::$ws_n = 0;    V::$ws_first = -1;    V::$ws_cur = 0;    V::$ws_end = 0;    v6x3d_edgebuild(0, $A, $B);    v6x3d_edgebuild(1, $A, $D);    if (!v6x3d_segment()) {        v6x3d_orderfix();        v6x3d_emit();    }    V::$ws_y += V::$ws_n;    if (s16(V::$ws_y) > ImageV6Vars::$v6xclipy2) {        goto draw;    }    if (V::$ws_e[1]->rows == 0) {        v6x3d_edgebuild(1, V::$ws_e[1]->lower, $C);    }    if (V::$ws_e[0]->rows == 0) {        v6x3d_edgebuild(0, V::$ws_e[0]->lower, $C);    }    if (!v6x3d_segment()) {        v6x3d_orderfix();        v6x3d_emit();    }    v6x3d_rowterm(V::$ws_cur + 1);    v6x3d_rowterm(V::$ws_cur + 2);    V::$ws_y += V::$ws_n;    if (s16(V::$ws_y) > ImageV6Vars::$v6xclipy2) {        goto draw;    }    if (V::$ws_e[1]->rows == 0) {        if (V::$ws_e[0]->rows == 0) {            goto draw;        }        v6x3d_edgebuild(1, $mid, $ymaxn);    }    if (V::$ws_e[0]->rows == 0) {        v6x3d_edgebuild(0, $mid, $ymaxn);    }    if (!v6x3d_segment()) {        v6x3d_orderfix();        v6x3d_emit();    }    draw:    if (V::$ws_first != -1 && V::$ws_first < V::$ws_end) {        v6x3d_spans(V::$ws_first, V::$ws_end);    }}function v6x3d_drawface(int $rec): void{    $fl = xread8($rec + 0x3b);    V::$v6x3d_ae5f = 0;    $m = xread32($rec + 0x40);    if (xread32($m) != 0) {        V::$v6x3d_ae5f = xread8($m + 0x09);        $b0a = xread8($m + 0x0a);        if ($b0a != 0) {            $fl |= 0x10;            if ($b0a == 0xff) {                return;            }        }    }    $t = xread32($rec + 0x44);    V::$v6x3d_res = $t;    V::$v6x3d_texw = xread8($t + 0x18);    V::$v6x3d_texw8 = (8 - V::$v6x3d_texw) & 0xff;    V::$v6x3d_texh = xread8($t + 0x19);    if (xread8($t + 0x00) == 1) {        $fl |= 1;        if (!(xread8($t + 0x1b) & 1)) {            $fl = ($fl & ~1) | 2;        }    } else {        $fl &= ~2;    }    if (xread8($t + 0x0f)) {        $fl |= 0x10;    }    $sy1 = 0;    $sy2 = 0;    if ($fl & 0x20) {        $sy1 = ImageV6Vars::$v6xclipy1;        $sy2 = ImageV6Vars::$v6xclipy2;        ImageV6Vars::$v6xclipy1 = s16(ImageV6Vars::$v6xclipy1 >> 1);        ImageV6Vars::$v6xclipy2 = s16(ImageV6Vars::$v6xclipy2 >> 1);        for ($k = 0; $k < 4; $k++) {            $o = $rec + 0x0c + $k * 12;            xwrite16($o, xread16($o) >> 1);        }    }    V::$v6x3d_ae98 = $fl;    V::$v6x3d_ae9c = ($fl & 6) | (($fl & 0x80) ? 8 : 0);    if (xread8($rec + 0x3a) == 0) {        v6x3d_tridrv($rec);    } else {        v6x3d_quaddrv($rec);    }    if (V::$v6x3d_ae98 & 0x20) {        for ($k = 0; $k < 4; $k++) {            $o = $rec + 0x0c + $k * 12;            xwrite16($o, xread16($o) << 1);        }        ImageV6Vars::$v6xclipy2 = $sy2;        ImageV6Vars::$v6xclipy1 = $sy1;    }}function v6x3d_flush(int $scr): void{    V::$v6tf_cacheon = 0;    $hbuf = xread32($scr + 0xb4);    V::$v6x3d_fogbase = $hbuf ? xread32($hbuf) + 0x10000 : 0;    V::$v6x3d_bands = xread16($scr + 0x96) & 0xff;    for ($g = 0x1ff; $g >= 0; $g--) {        if (!V::$v6x3d_slotdirty[$g]) {            continue;        }        for ($i = 15; $i >= 0; $i--) {            for ($rec = V::$v6x3d_slothead[$g * 16 + $i]; ult(1, $rec); $rec = xread32($rec)) {                v6x3d_drawface($rec);            }        }    }}function v6x3d_pickrecur(int $rec, int $zmin): int{    if (ule($rec, 1)) {        return 0;    }    if (xread32($rec) != 0) {        if (v6x3d_pickrecur(xread32($rec), $zmin)) {            return 1;        }    }    V::$v6x3d_pickhit = 0;    if (ult(xread16($rec + 0x38) & 0xffff, $zmin)) {        return 0;    }    $px = s16(V::$v6x3d_pickx);    $py = s16(V::$v6x3d_picky);    if ($px < xread16($rec + 0x48)) {        return 0;    }    if ($px > xread16($rec + 0x4c)) {        return 0;    }    if ($py < xread16($rec + 0x4a)) {        return 0;    }    if ($py > xread16($rec + 0x4e)) {        return 0;    }    xwrite8($rec + 0x3b, xread8($rec + 0x3b) & 0x7f);    v6x3d_drawface($rec);    if (V::$v6x3d_pickhit) {        V::$v6x3d_pickrec = $rec;        return 1;    }    return 0;}function v6x3d_pickwalk(int $scr, int $px, int $py, int $zmin, array &$w3, int &$elem, int &$tag, int &$depth): int{    V::$v6x3d_pick = 1;    V::$v6x3d_pickx = $px;    V::$v6x3d_picky = $py;    $sx1 = ImageV6Vars::$v6xclipx1;    $sx2 = ImageV6Vars::$v6xclipx2;    $sy1 = ImageV6Vars::$v6xclipy1;    $sy2 = ImageV6Vars::$v6xclipy2;    ImageV6Vars::$v6xclipx1 = ImageV6Vars::$v6xclipx2 = s16($px);    ImageV6Vars::$v6xclipy1 = ImageV6Vars::$v6xclipy2 = s16($py);    $hit = 0;    for ($g = 0; $g < 0x200 && !$hit; $g++) {        if (!V::$v6x3d_slotdirty[$g]) {            continue;        }        for ($i = 0; $i < 16 && !$hit; $i++) {            $head = V::$v6x3d_slothead[$g * 16 + $i];            if ($head == 0) {                continue;            }            if (v6x3d_pickrecur($head, $zmin)) {                $hit = 1;            }        }    }    ImageV6Vars::$v6xclipx1 = $sx1;    ImageV6Vars::$v6xclipx2 = $sx2;    ImageV6Vars::$v6xclipy1 = $sy1;    ImageV6Vars::$v6xclipy2 = $sy2;    V::$v6x3d_pick = 0;    if (!$hit) {        return 0;    }    $rec = V::$v6x3d_pickrec;    $d16 = xread16($rec + 0x38) & 0xffff;             // hit depth    $depth = $d16;    $elem = xread32($rec + 0x3c);                     // owner elem    $tg = 0;                                          // pick tag    $mm = xread32($rec + 0x40);    if (xread32($mm) != 0) {                          // mat has texture        $tg = xread32($mm + 0x14);                    // explicit tag    }    if ($tg == 0) {        $tg = xread32(xread32($rec + 0x44) + 8);      // texres+8    }    $tag = $tg;    $num = xread16($scr + 0x4e) + $d16;                                   // persp numerator    $bx = s16($px - xread16($scr + 0x62));                                // 16-bit    $camx = d3_div64($num * $bx, xread16($scr + 0x48));                   // sub+sext    $by = s16(0 - (($py - xread16($scr + 0x64)) & 0xffff));    $camy = d3_div64($num * $by, xread16($scr + 0x4c));                   // Y flipped    $w3 = [$camx, $d16, $camy];                                           // (x, depth, y-up)    v6tf_build($scr, 0, V::$v6tf_camobj);    v6tf_apply($w3, V::$v6tf_camobj, $w3);                                // WORLD point    return 1;}function v6x_skyfill(int $s): void{    $cx1 = ImageV6Vars::$v6xclipx1;    $cy1 = ImageV6Vars::$v6xclipy1;    $cy2 = ImageV6Vars::$v6xclipy2;    $blitw = ImageV6Vars::$v6xclipx2 - $cx1 + 1;    $blith = $cy2 - $cy1 + 1;    if ($blitw <= 0 || $blith <= 0) {        return;    }    $entry = xread32($s + 0x118);    if ($entry == 0) {        return;    }    $form = $entry + xread32($entry);    $W = xread16($form + 0x10) & 0xffff;    $H = xread16($form + 0x12) & 0xffff;    if ($W <= 0) {        return;    }    $sx = xread32($s + 0x0c);    $sy = xread32($s + 0x10);    $syaw = xread16($s + 0x1c);    $sroll = xread16($s + 0x1a);    V::$v6tf_cacheon = 0;    xwrite16($s + 0x1c, 0);    xwrite16($s + 0x1a, 0);    xwrite32($s + 0x0c, 0);    xwrite32($s + 0x10, 0);    $v = [0, xread32($s + 0x11c), 0];    v6tf_build(0, $s, V::$v6tf_camobj);    v6tf_apply($v, V::$v6tf_camobj, $v);    xwrite16($s + 0x1a, $sroll);    xwrite16($s + 0x1c, $syaw);    xwrite32($s + 0x10, $sy);    xwrite32($s + 0x0c, $sx);    $depth = $v[1];    if ($depth == 0) {        $depth = 1;    }    $py = s32(-d3_div64($v[2] * xread16($s + 0x4c), $depth) + xread16($s + 0x64));    if ($py > 0x5dc0) {        $py = 0x5dc0;    }    if ($py < -0x5dc0) {        $py = -0x5dc0;    }    $skytop = s16($py - xread16($form + 0x16));    $ush = (($syaw >> 4) * xread16($s + 0x120)) >> 8;    $t = $ush - xread16($form + 0x14) - $cx1;    $u0 = -($t % $W);    if ($u0 < 0) {        $u0 += $W;    }    $span1 = $W - $u0;    if ($span1 > $blitw) {        $span1 = $blitw;    }    $pitch = Alis::$platform->width;    $dst = Img::$logic + $cy1 * $pitch + $cx1;    $m = &Alis::$mem;    $ntop = $skytop - $cy1;    if ($ntop > 0) {        if ($ntop > $blith) {            $ntop = $blith;        }        $fill = str_repeat($m[$form + 0x20], $blitw);        for ($r = 0; $r < $ntop; $r++, $dst += $pitch) {            d3_putstr($dst, $fill);        }    }    $row0 = $skytop > $cy1 ? $skytop : $cy1;    $rowend = $skytop + $H;    if ($rowend > $cy2) {        $rowend = $cy2;    }    $ntex = $rowend - $row0;    if ($ntex > 0) {        $skip = $cy1 - $skytop;        if ($skip < 0) {            $skip = 0;        }        $src = $form + 0x20 + $skip * $W + $u0;        for ($r = 0; $r < $ntex; $r++, $dst += $pitch, $src += $W) {            $line = $span1 > 0 ? substr($m, $src, $span1) : '';            $rest = $blitw - $span1;            if ($rest > 0) {                $line .= substr($m, $src + $span1 - $W, $rest);            }            d3_putstr($dst, $line);        }    }    $nbot = $cy2 - $skytop - $H - 1;    if ($nbot > 0) {        if ($nbot > $blith) {            $nbot = $blith;        }        $fill = str_repeat($m[$form + 0x20 + $W * $H], $blitw);        for ($r = 0; $r < $nbot; $r++, $dst += $pitch) {            d3_putstr($dst, $fill);        }    }}/** memcpy of a PHP string into Alis::$mem, in place (byte loop: substr_replace would copy the whole memory). */function d3_putstr(int $dst, string $s): void{    $m = &Alis::$mem;    $n = strlen($s);    for ($i = 0; $i < $n; $i++) {        $m[$dst + $i] = $s[$i];    }}function v6x3d_open(int $scr): void{    if (xread32($scr + 0xbc) == 0) {        $node = Alis::$platform->kind != C::EPlatform3DO ? (node_take(0xb) & 0xffff) : 0;        xwrite32($scr + 0xc0, 0x1d8c0);        xwrite32($scr + 0xbc, io_malloc(0x1d8c0));        if ($node) {            scrnode_set($scr, 0xbc, $node);        }    }    $h = xread32($scr + 0xbc);    if ($h == 0) {        return; // face arena alloc failed    }    $a = xread32($h);    V::$v6x3d_arena = $a;    V::$v6x3d_arenalim = $a + 0x1d4c0;    V::$v6x3d_shl16 = $a + 0x1d4c0;    for ($i = 0; $i < 256; $i++) {        xwrite32(V::$v6x3d_shl16 + $i * 4, $i << 16);    }    $w = Alis::$platform->width;    for ($y = 0; $y < 500; $y++) {        V::$row_rowoff[$y] = $y * $w;    }    V::$v6x3d_slotdirty = array_fill(0, 0x200, 1);    v6x3d_framereset($scr);}// ---------------------------------------------------------------------------------------------// Vertex pipeline. C sV6Slot v6x3d_vcache[0x4000] = one PHP array per field (V::$sl_*), a slot is an// index; the clip nodes sV6CNode v6x3d_cnode[5] (a slot + camx/rawd/camy, 2 slots wide) are slots// V::CN + 2 * k, the centre node v6x3d_cnode_ctr slot V::CNCTR (V6CNODE_IX(n) = n - V::CN).// ---------------------------------------------------------------------------------------------function v6x3d_wave(int $mv): void{    $f6 = xread8($mv + 6);    if ($f6 & 2) {        $ph = shl32(s32(shl32(V::$v6x3d_tick, V::$v6x3d_ae59) + xread16($mv) + xread16($mv + 2)), V::$v6x3d_ae5a);        xwrite16($mv + 4, xread16($mv + 4) + (Deus3dConst::v6costab[$ph & 0xfff] >> (15 - V::$v6x3d_ae58)));    }    if ($f6 & 4) {        $ph = shl32(s32(shl32(V::$v6x3d_tick, V::$v6x3d_ae5d) + 2 * xread16($mv + 4)), V::$v6x3d_ae5e);        xwrite16($mv + 4, xread16($mv + 4) + (Deus3dConst::v6costab[$ph & 0xfff] >> (15 - V::$v6x3d_ae5c)));    }}// per-vertex light + fog band -> slot->bandfunction v6x3d_lightvtx(int $mv, int $sl): void{    if (V::$v6x3d_ae92) {        V::$sl_b[$sl] = V::$v6shad->identband & 0xff;        return;    }    if (V::$v6x3d_scale4) {        V::$v6x3d_ae94 = V::$sl_d[$sl];        V::$sl_d[$sl] = s16((V::$sl_d[$sl] & 0xffff) >> 2);    }    $fog = V::$v6x3dfog;    $f6 = xread8($mv + 6);    $acc = s32($fog->baselevel + (s8(xread8($mv + 0xb)) << $fog->bandshift));    $n0 = V::$sl_n0[$sl];    $n1 = V::$sl_n1[$sl];    $n2 = V::$sl_n2[$sl];    $nl = V::$v6x3d_nlights;    for ($li = 0; $li < $nl; $li++) {        $rec = V::$v6x3d_light[$li];        if (!$rec->active) {            continue;        }        $camset = ($f6 & 0x10) != 0;        if ($rec->kind == 1) {            $dir = $camset ? $rec->camdir : $rec->objdir;            $d = s32($n0 * $dir[0] + $n1 * $dir[1] + $n2 * $dir[2]);            if ($f6 & 8) {                $d = $d < 0 ? -$d : $d;            } else {                $d = -$d;            }            $acc = s32($acc + d3_sar($d * $rec->intensity, 14));            continue;        }        $pos = $camset ? $rec->campos : $rec->objpos;        $dx = s32(xread16($mv + 0) - $pos[0]);        $dy = s32(xread16($mv + 2) - $pos[1]);        $dz = s32(xread16($mv + 4) - $pos[2]);        $manh = s32(($dx < 0 ? -$dx : $dx) + ($dy < 0 ? -$dy : $dy) + ($dz < 0 ? -$dz : $dz));        if ($manh >= $rec->range) {            continue;        }        if ($rec->spot) {            $pdir = $camset ? $rec->camdir : $rec->objdir;            $ppos = $camset ? $rec->campos : $rec->objpos;            $ax = s32($pdir[0] - $ppos[0]);            $ay = s32($pdir[1] - $ppos[1]);            $az = s32($pdir[2] - $ppos[2]);            $a = s32(mul32($ax, $dx) + mul32($ay, $dy) + mul32($az, $dz));            if ($a <= 0) {                continue;            }            if ($a <= mul32((s32(5 * $manh - $rec->range) >> 2), $rec->cone)) {                continue;            }        }        $nd = s32($n0 * $dx + $n1 * $dy);        $ndz = s32($n2 * $dz);        switch ($f6 & 0x38) {            case 0x10:            case 0x18:                if ($manh <= 200) {                    $manh += 1;                    $num = $manh << 7;                    break;                }                // fall through            default:                $nd = s32($nd + $ndz);                $manh += 0x32;                $nd -= 0x1900;                if ($nd >= 0) {                    continue 2; // nextlight                }                $num = -$nd;                break;            case 0x08:            case 0x28:                $manh += 0x32;                $num = s32($nd - 0x1900 + $ndz);                if ($num < 0) {                    $num = -$num;                }                break;            case 0x30:            case 0x38:                $manh += 1;                $num = $manh << 4;                break;        }        if ($manh <= $rec->fstart) {            $acc = s32($acc + d3_sar(d3_tdiv($rec->intensity * $num, $manh), 7));        } else {            $k = s32(mul32($manh - $rec->fstart, $rec->slope) + $rec->int8fp) >> 8;            $acc = s32($acc + d3_sar(d3_tdiv($k * $num, $manh), 7));        }    }    $d = s32(V::$sl_d[$sl] - $fog->fogstart);    if ($fog->fogslope != 0 && $d >= 0) {        if ($fog->attslope != 0) {            $lim = shl32($fog->bandclamp, 7);            if ($acc > $lim) {                $acc = $lim;            }            $acc = s32($acc - (mul32(mul32($d, $fog->attslope) >> 8, $acc) >> 8));        }        $acc = s32($acc + (mul32($d, $fog->fogslope) >> 9));        $band = $acc >> 7;        if ($band < 0 || $band >= $fog->bands) {            $band = $band < 0 ? 0 : $fog->bands - 1;        }    } else {        $band = $acc >> 7;        if ($band < 0 || $band >= $fog->bandclamp) {            $band = $band < 0 ? 0 : $fog->bandclamp - 1;        }    }    V::$sl_b[$sl] = $band & 0xff;    if (V::$v6x3d_scale4) {        V::$sl_d[$sl] = s16(V::$v6x3d_ae94);    }}function v6x3d_project(int $mv, int $out, int &$clippable, sV6Ctx $base): int{    V::$v6x3d_cntverts++;    $f6 = xread8($mv + 6);    $zsave = xread16($mv + 4);    if ($f6 & 6) {        v6x3d_wave($mv);    }    $vx = xread16($mv + 0);    $vy = xread16($mv + 2);    $vz = xread16($mv + 4);    $c = (($f6 & 1) && $base === V::$v6tf_objcam) ? V::$v6tf_parentctx : $base;    $ret = 1;    $clip = 0;    $cm = $c->m;    $ct = $c->t;    $zf = s32($cm[3] * $vx + $cm[4] * $vy + $cm[5] * $vz + shl32($ct[1] + $c->params[1], 15));    $c->params[8] = $zf;    $zi = $zf >> 15;    if ($zi > $c->params2[1]) {        goto done;    }    if ($zi < -(xread8($mv + 7) << 6)) {        goto done;    }    V::$sl_d[$out] = s16($zi > 32000 ? 32000 : ($zi < -32000 ? -32000 : $zi));    if ($c->flags & 0x80) {        if ($c->flags & 0x40) {            if ($zi < 0) {                goto done;            }            V::$sl_sx[$out] = s16((mul32($vx + $ct[0], $c->scale) >> 8) + $c->params[6]);            V::$sl_sy[$out] = s16(-(mul32($vz + $ct[2], $c->scale) >> 8) + $c->params[7]);            $ret = 0;            goto done;        }        if ($zi < 0) {            goto done;        }        if ($zi == 0) {            $zi = 1;        }        $c->params[8] = $zi;        V::$sl_sx[$out] = s16(cdiv(mul32($ct[0], $c->params[0]), $zi) + $c->params[6] + (mul32($vx, $c->scale) >> 8));        V::$sl_sy[$out] = s16(-cdiv(mul32($ct[2], $c->params[0]), $zi) + $c->params[7] - (mul32($vz, $c->scale) >> 8));        $ret = 0;        goto done;    }    if ($zi <= V::$v6x3d_near) {        $clip = 1;        goto done;    }    $xf = s32($cm[0] * $vx + $cm[1] * $vy + $cm[2] * $vz + shl32($ct[0], 15));    $sx = d3_div64($xf * $c->params[0], $c->params[8]);    if ($sx > 16000 || $sx < -16000) {        goto done;    }    $sx += $c->params[6];    V::$sl_sx[$out] = s16($sx > 24000 ? 24000 : ($sx < -24000 ? -24000 : $sx));    $yf = s32($cm[6] * $vx + $cm[7] * $vy + $cm[8] * $vz + shl32($ct[2], 15));    $sy = s32(-d3_tdiv($yf * $c->params[2], $c->params[8]));    if ($sy > 16000 || $sy < -16000) {        goto done;    }    $sy += $c->params[7];    V::$sl_sy[$out] = s16($sy > 24000 ? 24000 : ($sy < -24000 ? -24000 : $sy));    $ret = 0;    done:    if ($f6 & 6) {        xwrite16($mv + 4, $zsave);    }    $clippable = $clip;    return $ret;}/** Face record (C sV6Face) as a PHP array: i[4], n[3], mat, flags, uv[8], vmrec. */function v6x3d_face(int $r, int $flags): array{    $uv = [];    for ($k = 0; $k < 8; $k++) {        $uv[$k] = xread8($r + 0x10 + $k);    }    return [        'i' => [xread16($r + 0) & 0xffff, xread16($r + 2) & 0xffff, xread16($r + 4) & 0xffff, xread16($r + 6) & 0xffff],        'n' => [s8(xread8($r + 8)), s8(xread8($r + 9)), s8(xread8($r + 10))],        'mat' => xread8($r + 0xb),        'flags' => $flags & 0xffff,        'uv' => $uv,        'vmrec' => $r,    ];}/** Writes the face-record vertex-node copies of slots $ns at rec + 8. */function v6x3d_putnodes(int $rec, array $ns): void{    foreach ($ns as $k => $s) {        $o = $rec + 0x08 + $k * 12;        xwrite16($o + 0, V::$sl_sx[$s]);        xwrite16($o + 2, V::$sl_d[$s]);        xwrite16($o + 4, V::$sl_sy[$s]);        xwrite8($o + 6, V::$sl_u[$s]);        xwrite8($o + 7, V::$sl_v[$s]);        xwrite8($o + 8, V::$sl_n0[$s]);        xwrite8($o + 9, V::$sl_n1[$s]);        xwrite8($o + 10, V::$sl_n2[$s]);        xwrite8($o + 11, V::$sl_b[$s]);    }}/** The render-flags byte rec+0x3b shared by tri_insert / quad_insert. */function v6x3d_blflags(int $rf, int $ae88, int $flags): int{    $bl = $rf;    if (!ult($ae88, V::$lat_ae48)) {        $bl &= ~2;    }    if ($ae88 <= V::$lat_ae4c) {        $bl |= 0x40;    }    if ($ae88 <= V::$lat_ae50) {        $bl |= 0x20;    }    if ($flags & 0x5000) {        if ($ae88 <= V::$lat_ae40 && ($flags & 0x4000)) {            $bl |= 0x80;        }        if ($ae88 <= V::$lat_ae44 && ($flags & 0x1000)) {            $bl |= 0x80;        }    }    if ($ae88 <= V::$lat_ae54) {        $bl |= 0x80;    }    return $bl & 0xff;}function v6x3d_tri_insert(array $fr, int $slotbase): int{    $s0 = $slotbase + $fr['i'][0];    $s1 = $slotbase + $fr['i'][1];    $s2 = $slotbase + $fr['i'][2];    $SB = &V::$sl_b;    $SY = &V::$sl_sy;    $SX = &V::$sl_sx;    $SD = &V::$sl_d;    $rf = 6;    if ($SB[$s0] == $SB[$s1] && $SB[$s1] == $SB[$s2]) {        $rf &= ~4;        if ($SB[$s0] == (V::$v6shad->solidband & 0xff) && V::$v6shad->solidflag) {            $rf &= ~6;        }    }    $uv = $fr['uv'];    V::$sl_u[$s0] = $uv[0];    V::$sl_v[$s0] = $uv[1];    V::$sl_u[$s1] = $uv[2];    V::$sl_v[$s1] = $uv[3];    V::$sl_u[$s2] = $uv[4];    V::$sl_v[$s2] = $uv[5];    $p0 = $s0;    $p1 = $s1;    $p2 = $s2;    if ($SY[$p1] < $SY[$p0]) {        $t = $p0;        $p0 = $p1;        $p1 = $t;    }    if ($SY[$p2] < $SY[$p0]) {        $t = $p0;        $p0 = $p2;        $p2 = $t;    }    if ($SY[$p2] < $SY[$p1]) {        $t = $p1;        $p1 = $p2;        $p2 = $t;    }    if ($SY[$p0] > ImageV6Vars::$v6xclipy2) {        return 0;    }    $my = $SY[$p1] > $SY[$p2] ? $SY[$p1] : $SY[$p2];    if ($my < ImageV6Vars::$v6xclipy1) {        return 0;    }    $key = (($SD[$p0] + $SD[$p1]) & 0xffff) >> 1;    $key = (($key + $SD[$p2]) & 0xffff) >> 1;    if ($key > V::$lat_ae3c) {        return 0;    }    $ae88 = $key;    $flags = $fr['flags'];    if ($flags & 0x2010) {        if ($flags & 0x10) {            $key = $SD[$p0];            if ($SD[$p1] > $key) {                $key = $SD[$p1];            }            if ($SD[$p2] > $key) {                $key = $SD[$p2];            }            if ($flags & 0x2000) {                $key += 2 * V::$v6x3d_b54c;            }        } else {            $key -= V::$v6x3d_b54c;            if ($key <= 0) {                $key = 1;            }        }    }    $x1 = $SX[$s0];    $x2 = $SX[$s0];    if ($SX[$s1] < $x1) {        $x1 = $SX[$s1];    }    if ($SX[$s2] < $x1) {        $x1 = $SX[$s2];    }    if ($SX[$s1] > $x2) {        $x2 = $SX[$s1];    }    if ($SX[$s2] > $x2) {        $x2 = $SX[$s2];    }    if ($x2 < ImageV6Vars::$v6xclipx1) {        return 0;    }    if ($x1 > ImageV6Vars::$v6xclipx2) {        return 0;    }    $m = V::$v6x3d_matbase + $fr['mat'] * 0x20;    if (xread32($m) == -1) {        return 0;    }    if ($key < 0) {        $key = 0;    }    if ($key > 0x7fff) {        $key = 0x7fff;    }    $rec = v6x3d_slotinsert($key);    xwrite32($rec + 0x04, $fr['vmrec']);    xwrite16($rec + 0x48, $x1);    xwrite16($rec + 0x4a, $SY[$p0]);    xwrite16($rec + 0x4c, $x2);    xwrite16($rec + 0x4e, $my);    xwrite16($rec + 0x38, $ae88);    xwrite32($rec + 0x3c, V::$v6x3d_b530);    xwrite32($rec + 0x40, $m);    xwrite32($rec + 0x44, $m + xread32($m));    xwrite8($rec + 0x3b, v6x3d_blflags($rf, $ae88, $flags));    xwrite8($rec + 0x3a, 0);    v6x3d_putnodes($rec, [$p0, $p1, $p2]);    return 1;}/** Fills normals/band of a freshly projected cache slot and lights it (shared by pipeA / pipeB). */function v6x3d_slotlight(int $r, int $mv, int $sl, int $flags, int $fresh): void{    if ($fresh) {        $o = ($flags & 8) ? $r : $mv;        V::$sl_n0[$sl] = s8(xread8($o + 8));        V::$sl_n1[$sl] = s8(xread8($o + 9));        V::$sl_n2[$sl] = s8(xread8($o + 10));        V::$sl_b[$sl] = xread8($o + 0xb);    } else {        V::$sl_n0[$sl] = s8(xread8($r + 8));        V::$sl_n1[$sl] = s8(xread8($r + 9));        V::$sl_n2[$sl] = s8(xread8($r + 10));    }    v6x3d_lightvtx($mv, $sl);}/** Wave parameters of face flag 0x400 (pipeA / pipeB). */function v6x3d_facesetup(int $r, int $flags): void{    if ($flags & 0x400) {        $mb = xread8(V::$v6x3d_matbase + xread8($r + 0xb) * 0x20 + 0xb);        V::$v6x3d_ae58 = V::$v6x3d_ae5c = ($mb & 7) * 2;        V::$v6x3d_ae59 = V::$v6x3d_ae5d = (($mb >> 3) & 7) * 2;        V::$v6x3d_ae5a = V::$v6x3d_ae5e = ($mb >> 6) & 3;    }}/** Back-face dot and the adaptive near distance (pipeA / pipeB). Returns 0 to cull. */function v6x3d_nearset(int $r, int $flags, int $i0): int{    $v0 = V::$v6x3d_vertbase + $i0 * 0xC;    $b660 = V::$v6x3d_b660;    $dot = s32(s8(xread8($r + 8)) * (xread16($v0 + 0) - $b660[0]) + s8(xread8($r + 9)) * (xread16($v0 + 2) - $b660[1]) + s8(xread8($r + 10)) * (xread16($v0 + 4) - $b660[2]));    if (!($flags & 0xc04)) {        $mag = s32(-$dot);        if ($mag < 0) {            return 0;        }    } else {        $mag = $dot < 0 ? s32(-$dot) : $dot;    }    if ($flags & 0x1400) {        V::$v6x3d_near = V::$v6tf_objcam->params2[0];    } else {        V::$v6x3d_near = shr32(mul32(V::$lat_fogsel & 0xffff, $mag), 22); // u32 product >> 22        if (V::$v6x3d_near == 0) {            V::$v6x3d_near = 1;        }        if (V::$v6x3d_near > V::$v6tf_objcam->params2[0]) {            V::$v6x3d_near = V::$v6tf_objcam->params2[0];        }    }    return 1;}function v6x3d_pipeA(int $r): int{    $flags = xread16($r + 0xc) & 0xffff;    if ($flags & 0x80) {        return 0;    }    v6x3d_facesetup($r, $flags);    $i0 = xread16($r + 0) & 0xffff;    $i1 = xread16($r + 2) & 0xffff;    $i2 = xread16($r + 4) & 0xffff;    if ($i0 >= 0x4000 || $i1 >= 0x4000 || $i2 >= 0x4000) {        return 0;    }    if (!v6x3d_nearset($r, $flags, $i0)) {        return 0;    }    $vbits = &V::$v6x3d_vbits;    foreach ([$i0, $i1, $i2] as $ix) {        $mv = V::$v6x3d_vertbase + $ix * 0xC;        if (!($vbits[$ix >> 3] & (1 << ($ix & 7)))) {            $vbits[$ix >> 3] |= (1 << ($ix & 7));            $clippable = 0;            if (v6x3d_project($mv, $ix, $clippable, V::$v6tf_objcam)) {                $vbits[$ix >> 3] &= ~(1 << ($ix & 7));                if ($clippable) {                    v6x3d_cliptri($r, $flags);                }                return 0;            }            v6x3d_slotlight($r, $mv, $ix, $flags, 1);        } elseif ($flags & 8) {            v6x3d_slotlight($r, $mv, $ix, $flags, 0);        }    }    if (($flags & 0x404) && !($flags & 0x800)) {        $SX = V::$sl_sx;        $SY = V::$sl_sy;        $cross = s16($SX[$i1] - $SX[$i0]) * s16($SY[$i2] - $SY[$i0]) - s16($SX[$i2] - $SX[$i0]) * s16($SY[$i1] - $SY[$i0]);        if ($cross > 0) {            return 0;        }    }    return v6x3d_tri_insert(v6x3d_face($r, $flags), 0);}function v6x3d_quad_insert(array $fr, int $slotbase): int{    $A = $slotbase + $fr['i'][0];    $B = $slotbase + $fr['i'][1];    $C = $slotbase + $fr['i'][2];    $D = $slotbase + $fr['i'][3];    $SB = &V::$sl_b;    $SY = &V::$sl_sy;    $SX = &V::$sl_sx;    $SD = &V::$sl_d;    $rf = 6;    if ($SB[$A] == $SB[$B] && $SB[$B] == $SB[$C] && $SB[$C] == $SB[$D]) {        $rf &= ~4;        if ($SB[$A] == (V::$v6shad->solidband & 0xff) && V::$v6shad->solidflag) {            $rf &= ~6;        }    }    $y = $SY[$A];    if (!($y > $SY[$B])) {        $y = $SY[$B];    }    if (!($y > $SY[$C])) {        $y = $SY[$C];    }    if (!($y > $SY[$D])) {        $y = $SY[$D];    }    if ($y < ImageV6Vars::$v6xclipy1) {        return 0;    }    $maxy = $y;    $m = $A;    $y = $SY[$A];    if (!($y < $SY[$B])) {        $y = $SY[$B];        $m = $B;    }    if (!($y < $SY[$C])) {        $y = $SY[$C];        $m = $C;    }    if (!($y < $SY[$D])) {        $y = $SY[$D];        $m = $D;    }    if ($y > ImageV6Vars::$v6xclipy2) {        return 0;    }    $miny = $y;    $uv = $fr['uv'];    V::$sl_u[$A] = $uv[0];    V::$sl_v[$A] = $uv[1];    V::$sl_u[$B] = $uv[2];    V::$sl_v[$B] = $uv[3];    V::$sl_u[$C] = $uv[4];    V::$sl_v[$C] = $uv[5];    V::$sl_u[$D] = $uv[6];    V::$sl_v[$D] = $uv[7];    $n0 = $A;    $n1 = $B;    $n2 = $C;    $n3 = $D;    if ($m == $B) {        $n0 = $B;        $n1 = $C;        $n2 = $D;        $n3 = $A;    } elseif ($m == $C) {        $n0 = $C;        $n1 = $D;        $n2 = $A;        $n3 = $B;    } elseif ($m == $D) {        $n0 = $D;        $n1 = $A;        $n2 = $B;        $n3 = $C;    }    $avg = ($SD[$n0] + $SD[$n1] + $SD[$n2] + $SD[$n3]) >> 2;    if ($avg > V::$lat_ae3c) {        return 0;    }    $ae88 = $avg & 0xffff;    $bp = $avg & 0xffff;    $flags = $fr['flags'];    if ($flags & 0x2010) {        if ($flags & 0x10) {            $mx = $SD[$n0];            if (!($mx > $SD[$n1])) {                $mx = $SD[$n1];            }            if (!($mx > $SD[$n2])) {                $mx = $SD[$n2];            }            if (!($mx > $SD[$n3])) {                $mx = $SD[$n3];            }            $bp = $mx & 0xffff;            if ($flags & 0x2000) {                $bp = ($bp + 2 * (V::$v6x3d_b54c & 0xffff)) & 0xffff;            }        } else {            $bp = ($bp - (V::$v6x3d_b54c & 0xffff)) & 0xffff;            if (!(s16($bp) > 0)) {                $bp = 1;            }        }    }    $xd = $SX[$n3];    $xc = $SX[$n2];    $xb = $SX[$n1];    $xa = $SX[$n0];    if (!($xd > $xc)) {        $xt = $xc;        $xc = $xd;        $xd = $xt;    }    if (!($xd > $xb)) {        $xt = $xb;        $xb = $xd;        $xd = $xt;    }    if (!($xd > $xa)) {        $xt = $xa;        $xa = $xd;        $xd = $xt;    }    if ($xd < ImageV6Vars::$v6xclipx1) {        return 0;    }    if (!($xc < $xb)) {        $xt = $xb;        $xb = $xc;        $xc = $xt;    }    if (!($xc < $xa)) {        $xt = $xa;        $xa = $xc;        $xc = $xt;    }    if ($xc > ImageV6Vars::$v6xclipx2) {        return 0;    }    $mmat = V::$v6x3d_matbase + $fr['mat'] * 0x20;    if (xread32($mmat) == -1) {        return 0;    }    $key = $bp;    if ($key > 0x7fff) {        $key = 0x7fff;    }    $rec = v6x3d_slotinsert($key);    xwrite32($rec + 0x04, $fr['vmrec']);    xwrite16($rec + 0x48, $xc);                       // bbox minX    xwrite16($rec + 0x4c, $xd);                       // bbox maxX    xwrite16($rec + 0x4a, $miny);    xwrite16($rec + 0x4e, $maxy);    xwrite16($rec + 0x38, $ae88);                     // UNBIASED depth    xwrite32($rec + 0x3c, V::$v6x3d_b530);            // element    xwrite32($rec + 0x40, $mmat);                     // material    xwrite32($rec + 0x44, $mmat + xread32($mmat));    // texture resource    xwrite8($rec + 0x3b, v6x3d_blflags($rf, $ae88, $flags));    v6x3d_putnodes($rec, [$n0, $n1, $n2, $n3]);    xwrite8($rec + 0x3a, 1);    return 1;}function v6x3d_xform_full(int $mv, int $n): void{    $f6 = xread8($mv + 6);    $zsave = xread16($mv + 4);    if ($f6 & 6) {        v6x3d_wave($mv);    }    $vx = xread16($mv + 0);    $vy = xread16($mv + 2);    $vz = xread16($mv + 4);    $c = ($f6 & 1) ? V::$v6tf_parentctx : V::$v6tf_objcam;    $cm = $c->m;    $ct = $c->t;    $rawd = s32($cm[3] * $vx + $cm[4] * $vy + $cm[5] * $vz + shl32($ct[1] + $c->params[1], 15));    V::$cn_rawd[$n] = $rawd;    $d = $rawd >> 15;    V::$sl_d[$n] = s16($d > 32000 ? 32000 : ($d < -32000 ? -32000 : $d));    $camx = s32($cm[0] * $vx + $cm[1] * $vy + $cm[2] * $vz + shl32($ct[0], 15));    V::$cn_camx[$n] = $camx;    V::$sl_sx[$n] = s16($camx >> 15);    $camy = s32($cm[6] * $vx + $cm[7] * $vy + $cm[8] * $vz + shl32($ct[2], 15));    V::$cn_camy[$n] = $camy;    V::$sl_sy[$n] = s16($camy >> 15);    if ($f6 & 6) {        xwrite16($mv + 4, $zsave);    }}function v6x3d_clipedge(int $in, int $out, int $o): void{    $din = V::$sl_d[$in];    $den = V::$sl_d[$out] - $din;    if ($den == 0) {        $den = 1;    }    $b6d4 = V::$v6x3d_b6d4;    $t = cdiv(shl32($b6d4 - $din, 16), $den);    $lx = V::$cn_camx[$in] + d3_mul64_sar(s32(V::$cn_camx[$out] - V::$cn_camx[$in]), $t, 16); // s64    $ly = V::$cn_camy[$in] + d3_mul64_sar(s32(V::$cn_camy[$out] - V::$cn_camy[$in]), $t, 16);    $dd = $b6d4 * 32768; // (s64)b6d4 << 15    $sx = s32(s32(d3_tdiv($lx * V::$v6x3d_b6cc, $dd)) + V::$v6x3d_b6dc);    $sy = s32(-s32(d3_tdiv($ly * V::$v6x3d_b6d0, $dd)) + V::$v6x3d_b6e0);    $du = mul32(V::$sl_u[$out] - V::$sl_u[$in], $t) >> 16;    $dv = mul32(V::$sl_v[$out] - V::$sl_v[$in], $t) >> 16;    $db = mul32(V::$sl_b[$out] - V::$sl_b[$in], $t) >> 16;    $iu = V::$sl_u[$in];    $iv = V::$sl_v[$in];    $ib = V::$sl_b[$in];    $nn0 = V::$sl_n0[$in];    $nn1 = V::$sl_n1[$in];    $nn2 = V::$sl_n2[$in];    V::$sl_d[$o] = s16($b6d4);    V::$sl_sx[$o] = s16($sx > 24000 ? 24000 : ($sx < -24000 ? -24000 : $sx));    V::$sl_sy[$o] = s16($sy > 24000 ? 24000 : ($sy < -24000 ? -24000 : $sy));    V::$sl_u[$o] = ($iu + $du) & 0xff;    V::$sl_v[$o] = ($iv + $dv) & 0xff;    V::$sl_n0[$o] = $nn0;    V::$sl_n1[$o] = $nn1;    V::$sl_n2[$o] = $nn2;    V::$sl_b[$o] = ($ib + $db) & 0xff;}function v6x3d_projnode(int $n): void{    $sx = s32(s32(d3_tdiv(V::$cn_camx[$n] * V::$v6x3d_b6cc, V::$cn_rawd[$n])) + V::$v6x3d_b6dc);    $sy = s32(-s32(d3_tdiv(V::$cn_camy[$n] * V::$v6x3d_b6d0, V::$cn_rawd[$n])) + V::$v6x3d_b6e0);    V::$sl_sx[$n] = s16($sx > 24000 ? 24000 : ($sx < -24000 ? -24000 : $sx));    V::$sl_sy[$n] = s16($sy > 24000 ? 24000 : ($sy < -24000 ? -24000 : $sy));}/** Transforms + lights the first $nv corners of face $r into the clip nodes (clipquad / cliptri). */function v6x3d_clipsetup(int $r, int $flags, int $nv): void{    $oc = V::$v6tf_objcam;    V::$v6x3d_b6cc = $oc->params[0];    V::$v6x3d_b6d0 = $oc->params[2];    V::$v6x3d_b6d4 = V::$v6x3d_near;    V::$v6x3d_b6dc = $oc->params[6];    V::$v6x3d_b6e0 = $oc->params[7];    for ($i = 0; $i < $nv; $i++) {        $ix = xread16($r + $i * 2) & 0xffff;        $mv = V::$v6x3d_vertbase + $ix * 0xC;        $n = V::CN + 2 * $i;        v6x3d_xform_full($mv, $n);        V::$sl_u[$n] = xread8($r + 0x10 + $i * 2);        V::$sl_v[$n] = xread8($r + 0x11 + $i * 2);        $o = ($flags & 8) ? $r : $mv;        V::$sl_n0[$n] = s8(xread8($o + 8));        V::$sl_n1[$n] = s8(xread8($o + 9));        V::$sl_n2[$n] = s8(xread8($o + 10));        V::$sl_b[$n] = xread8($mv + 0xb);        v6x3d_lightvtx($mv, $n);    }}/** sV6Face of a clipped polygon: corners are clip-node slots (V6CNODE_IX), uv taken from them. */function v6x3d_clipface(int $r, int $flags, array $nodes): array{    $i = [0, 0, 0, 0];    $uv = [0, 0, 0, 0, 0, 0, 0, 0];    foreach ($nodes as $k => $n) {        $i[$k] = ($n - V::CN) & 0xffff;        $uv[$k * 2] = V::$sl_u[$n];        $uv[$k * 2 + 1] = V::$sl_v[$n];    }    return [        'i' => $i,        'n' => [s8(xread8($r + 8)), s8(xread8($r + 9)), s8(xread8($r + 10))],        'mat' => xread8($r + 0xb),        'flags' => $flags & 0xffff,        'uv' => $uv,        'vmrec' => 0,    ];}function v6x3d_clipquad(int $r, int $flags): void{    v6x3d_clipsetup($r, $flags, 4);    $a = V::CN;    $b = V::CN + 2;    $c = V::CN + 4;    $d = V::CN + 6;    $plane = s16(V::$v6x3d_b6d4);    $SD = &V::$sl_d;    // V6ROT(): t = d; d = c; c = b; b = a; a = t    $found = 0;    $one = 0;    if ($plane < $SD[$a]) {        $found = 1;    }    if (!$found) {        $t = $d; $d = $c; $c = $b; $b = $a; $a = $t;        if ($plane < $SD[$a]) {            $found = 1;        }    }    if (!$found) {        $t = $d; $d = $c; $c = $b; $b = $a; $a = $t;        if ($plane < $SD[$a]) {            $found = 1;        }    }    if (!$found) {        $t = $d; $d = $c; $c = $b; $b = $a; $a = $t;        if ($plane < $SD[$a]) {            $found = 1;            $one = 1;        }    }    if (!$found) {        return;    }    if (!$one) {        if ($plane < $SD[$d]) {            $t = $d; $d = $c; $c = $b; $b = $a; $a = $t;        } elseif (!($plane < $SD[$b])) {            $one = 1;        }        if (!$one) {            if ($plane < $SD[$d]) {                $t = $d; $d = $c; $c = $b; $b = $a; $a = $t;                goto three;            }            if ($plane < $SD[$c]) {                goto three;            }            goto two;        }    }    v6x3d_clipedge($a, $b, $b);    v6x3d_clipedge($a, $d, $d);    v6x3d_projnode($a);    v6x3d_tri_insert(v6x3d_clipface($r, $flags, [$a, $b, $d]), V::CN);    return;    two:    v6x3d_clipedge($a, $d, $d);    v6x3d_projnode($a);    v6x3d_clipedge($b, $c, $c);    v6x3d_projnode($b);    v6x3d_quad_insert(v6x3d_clipface($r, $flags, [$a, $b, $c, $d]), V::CN);    return;    three:    $ex = V::CN + 8;    v6x3d_clipedge($c, $d, $ex);    v6x3d_projnode($c);    v6x3d_clipedge($a, $d, $d);    v6x3d_projnode($a);    v6x3d_projnode($b);    $qrec = v6x3d_clipface($r, $flags, [$a, $b, $c, $d]);    $trec = v6x3d_clipface($r, $flags, [$c, $d, $ex]);    v6x3d_tri_insert($trec, V::CN);    v6x3d_quad_insert($qrec, V::CN);}function v6x3d_cliptri(int $r, int $flags): void{    v6x3d_clipsetup($r, $flags, 3);    $a = V::CN;    $b = V::CN + 2;    $c = V::CN + 4;    $plane = s16(V::$v6x3d_b6d4);    $SD = &V::$sl_d;    if (!($plane < $SD[$a])) {        $t = $a; $a = $b; $b = $c; $c = $t;        if (!($plane < $SD[$a])) {            $t = $a; $a = $b; $b = $c; $c = $t;            if (!($plane < $SD[$a])) {                return;            }            goto one;        }    }    if ($plane < $SD[$b]) {        if ($plane < $SD[$c]) {            return;        }        $in1 = $a;        $in2 = $b;        $out = $c;        goto two;    }    if (!($plane < $SD[$c])) {        goto one;    }    $in1 = $a;    $in2 = $c;    $out = $b;    goto two;    one:    v6x3d_clipedge($a, $b, $b);    v6x3d_clipedge($a, $c, $c);    v6x3d_projnode($a);    v6x3d_tri_insert(v6x3d_clipface($r, $flags, [$c, $b, $a]), V::CN);    return;    two:    $n3 = V::CN + 6;    v6x3d_clipedge($out, $in2, $n3);    v6x3d_projnode($in2);    v6x3d_clipedge($out, $in1, $out);    v6x3d_projnode($in1);    v6x3d_quad_insert(v6x3d_clipface($r, $flags, [$in2, $in1, $out, $n3]), V::CN);}function v6x3d_pipeB_centered(int $r, int $flags): void{    if (V::$v6x3d_cscratch == 0) {        $h = io_malloc(0x20);        if ($h == 0) {            return;        }        V::$v6x3d_cscratch = xread32($h);    }    $ctr = V::$v6x3d_cscratch;    $tmp = V::$v6x3d_cscratch + 0x10;    $mat = V::$v6x3d_matbase + xread8($r + 0xb) * 0x20;    if (xread8($mat + 8) & 0x10) {        $flags |= 8;        xwrite16($r + 0xc, $flags);    }    V::$v6x3d_near = 1;    $vb = V::$v6x3d_vertbase;    $vA = $vb + (xread16($r + 0) & 0xffff) * 0xC;    $vB = $vb + (xread16($r + 2) & 0xffff) * 0xC;    $cx = s16((xread16($vA + 0) + xread16($vB + 0)) >> 1);    if (V::$v6x3d_b544 & 0x40) {        $vC = $vb + (xread16($r + 4) & 0xffff) * 0xC;        $cz = s16((xread16($vA + 4) + xread16($vC + 4)) >> 1);    } else {        $cz = xread16($vA + 4);    }    $cy = xread16($vA + 2);    xwrite16($ctr + 0, $cx);    xwrite16($ctr + 2, $cy);    xwrite16($ctr + 4, $cz);    xwrite16($ctr + 6, 0);    $n = V::CNCTR;    v6x3d_xform_full($ctr, $n);    V::$sl_d[$n] = s16(V::$sl_d[$n] - s16(V::$v6tf_objcam->params[1]));    $lt = V::$v6tf_light;    $lt->t[0] = V::$sl_sx[$n];    $lt->t[1] = V::$sl_d[$n];    $lt->t[2] = V::$sl_sy[$n];    $b548 = ($lt->flags & 0xc0) ? 0x100 : $lt->scale;    for ($i = 0; $i < 4; $i++) {        $ix = xread16($r + $i * 2) & 0xffff;        if ($ix >= 0x4000) {            return;        }        $v = $vb + $ix * 0xC;        $sl = $ix;        xwrite16($tmp + 0, (mul32(xread16($v + 0), $b548) >> 8) - $cx);        xwrite16($tmp + 2, xread16($v + 2) - $cy);        xwrite16($tmp + 4, (mul32(xread16($v + 4), $b548) >> 8) - $cz);        xwrite16($tmp + 6, xread16($v + 6));        $clippable = 0;        if (v6x3d_project($tmp, $sl, $clippable, $lt)) {            return;        }        xwrite8($tmp + 8, xread8($v + 8));        xwrite8($tmp + 9, xread8($v + 9));        xwrite8($tmp + 10, xread8($v + 10));        xwrite8($tmp + 11, xread8($v + 11));        V::$sl_n0[$sl] = s8(xread8($v + 8));        V::$sl_n1[$sl] = s8(xread8($v + 9));        V::$sl_n2[$sl] = s8(xread8($v + 10));        V::$sl_b[$sl] = xread8($v + 11);        $f6 = (xread8($tmp + 6) | 0x10) & 0xff;        if ($flags & 8) {            $f6 |= 0x20;        }        xwrite8($tmp + 6, $f6);        xwrite16($tmp + 0, V::$sl_sx[$n]);        xwrite16($tmp + 2, V::$sl_d[$n]);        xwrite16($tmp + 4, V::$sl_sy[$n]);        v6x3d_lightvtx($tmp, $sl);    }    v6x3d_quad_insert(v6x3d_face($r, $flags), 0);}function v6x3d_pipeB(int $r): int{    $flags = xread16($r + 0xc) & 0xffff;    if (!($flags & 0x8000)) {        return v6x3d_pipeA($r);    }    if ($flags & 0x200) {        v6x3d_pipeB_centered($r, $flags);        return 0;    }    if ($flags & 0x80) {        return 0;    }    v6x3d_facesetup($r, $flags);    $i0 = xread16($r + 0) & 0xffff;    $i1 = xread16($r + 2) & 0xffff;    $i2 = xread16($r + 4) & 0xffff;    $i3 = xread16($r + 6) & 0xffff;    if ($i0 >= 0x4000 || $i1 >= 0x4000 || $i2 >= 0x4000 || $i3 >= 0x4000) {        return 0;    }    if (!v6x3d_nearset($r, $flags, $i0)) {        return 0;    }    $vbits = &V::$v6x3d_vbits;    $idx = [$i0, $i1, $i2, $i3];    for ($k = 0; $k < 4; $k++) {        if ($k == 3 && ($flags & 0x404) && !($flags & 0x800)) {            $SX = V::$sl_sx;            $SY = V::$sl_sy;            $cross = s16($SX[$i1] - $SX[$i0]) * s16($SY[$i2] - $SY[$i0]) - s16($SY[$i1] - $SY[$i0]) * s16($SX[$i2] - $SX[$i0]);            if ($cross > 0) {                return 0;            }        }        $ix = $idx[$k];        $mv = V::$v6x3d_vertbase + $ix * 0xC;        if (!($vbits[$ix >> 3] & (1 << ($ix & 7)))) {            $vbits[$ix >> 3] |= (1 << ($ix & 7));            $clippable = 0;            if (v6x3d_project($mv, $ix, $clippable, V::$v6tf_objcam)) {                $vbits[$ix >> 3] &= ~(1 << ($ix & 7));                if ($clippable) {                    v6x3d_clipquad($r, $flags);                }                return 0;            }            v6x3d_slotlight($r, $mv, $ix, $flags, 1);        } elseif ($flags & 8) {            v6x3d_slotlight($r, $mv, $ix, $flags, 0);        }    }    return v6x3d_quad_insert(v6x3d_face($r, $flags), 0);}// ---------------------------------------------------------------------------------------------// Objects, cell-grid land, billboards, the mode-6 frame// ---------------------------------------------------------------------------------------------function v6x3d_lights_activate(int $e, int $mflags): void{    V::$v6x3d_ae92 = ($mflags & 0x80) ? 1 : 0;    $nl = V::$v6x3d_nlights;    for ($li = 0; $li < $nl; $li++) {        $rec = V::$v6x3d_light[$li];        $rec->active = 0;        if ($mflags & 0x80) {            continue;        }        if ($rec->kind == 1) {            if ($mflags & 0x10) {                continue;            }            v6tf_build(0, $e, $rec->ctx);            $t = [0, 0, 0];            v6tf_apply($t, $rec->ctx, $t);            $od = [0, 0, 0];            v6tf_apply($rec->dir, $rec->ctx, $od);            $rec->objdir = [s32($od[0] - $t[0]), s32($od[1] - $t[1]), s32($od[2] - $t[2])];            $rec->active = 1;        } else {            if ($mflags & 0x20) {                continue;            }            v6tf_build($rec->elem, $e, $rec->ctx);            v6tf_apply($rec->pos, $rec->ctx, $rec->objpos);            if ($rec->spot) {                v6tf_apply($rec->dir, $rec->ctx, $rec->objdir);            }            $d2 = 0;            for ($i = 0; $i < 3; $i++) {                $d = s32(xread16($e + 0x42 + $i * 2) - $rec->objpos[$i]);                $d2 = s32($d2 + s32($d * $d));            }            $r = s32(xread16($e + 0x40) + $rec->range);            if (ule($d2, mul32($r, $r))) {                $rec->active = 1;            }        }    }}/** The camera projection parameters copied into a context (repeated block of v6x3d_object / gridland). */function v6x3d_ctxparams(sV6Ctx $c, int $s, int $far): void{    $c->params[0] = xread16($s + 0x48);    $c->params[1] = xread16($s + 0x4a);    $c->params[2] = xread16($s + 0x4c);    $c->params[5] = xread16($s + 0x52);    $c->params2[0] = xread32($s + 0x142);    $c->params2[1] = $far;    $c->params[6] = xread16($s + 0x62);    $c->params[7] = xread16($s + 0x64);}function v6x3d_object(int $e, int $s): void{    if (xread8($e + 0x23) & 0x70) {        if (!v6x3d_cull($e)) {            return;        }    }    $mref = xread32($e + 0x3c);    if ($mref == 0) {        return;    }    $model = $mref + xread32($mref);    $mflags = xread32($model + 0x28);    V::$v6x3d_scale4 = ($mflags & 8) ? 1 : 0;    $sh = V::$v6x3d_scale4 ? 2 : 0;    v6tf_build($e, $s, V::$v6tf_objcam);    $c = [xread16($e + 0x42), xread16($e + 0x44), xread16($e + 0x46)];    v6tf_apply($c, V::$v6tf_objcam, $c);    $dx = xread16($s + 0x42) - $c[0];    $dy = (xread16($s + 0x44) << $sh) - $c[1];    $dz = xread16($s + 0x46) - $c[2];    $d2 = $dx * $dx + $dy * $dy + $dz * $dz; // s64    $lim = (xread16($e + 0x40) & 0xffff) + ((xread16($s + 0x40) & 0xffff) << $sh);    $lim = $lim * $lim;    if ($d2 > $lim) {        return;    }    V::$lat_fogsel = Deus3dConst::v6costab[(xread16($s + 0x176) & 0xffff) >> 4];    V::$lat_ae48 = shl32(xread32($s + 0x16a), $sh);    V::$lat_ae50 = shl32(xread32($model + 0x68) != 0 ? xread32($s + 0x15a) : xread32($s + 0x152), $sh);    V::$lat_ae4c = shl32(xread32($s + 0x156), $sh);    V::$lat_ae3c = shl32(xread32($s + 0x146), $sh);    V::$lat_ae44 = shl32(xread32($s + 0x14a), $sh);    V::$lat_ae40 = shl32(xread32($s + 0x162), $sh);    V::$lat_ae54 = shl32(xread32($s + 0x166), $sh);    if (($mflags & 0x40) && (xread8($e + 0x25) & 0x40)) {        V::$lat_ae54 = V::$lat_ae44 = V::$lat_ae40 = V::$lat_ae50 = V::$lat_ae4c = -1;    }    if (V::$v6x3d_lighton == 0) {        V::$lat_ae50 = -1;    }    V::$v6x3d_b54c = xread16($model + 0x26);    V::$v6x3d_ae92 = ($mflags & 0x80) ? 1 : 0;    if (xread32($model + 0x68) != 0) {        if (xread8($model + 0x6c) == 1) {            v6x3d_gridland($e, $s, $model, $mflags);        }        // else: portal renderer (FUN_0000fc6d) not yet ported in the C either        return;    }    v6tf_build($s, $e, V::$v6tf_camobj);    $sv = [xread32($s + 0x00), xread32($s + 0x04), xread32($s + 0x08)];    v6tf_apply($sv, V::$v6tf_camobj, V::$v6x3d_b660);    v6x3d_ctxparams(V::$v6tf_objcam, $s, s32(xread32($s + 0x146) + 0x1f4));    if ($mflags & 2) {        $f = xread8($e + 0x25);        xwrite8($e + 0x25, $f | 0x8 | (($mflags & 0x40) ? 0x2 : 0));        v6tf_build($e, $s, V::$v6tf_light);        xwrite8($e + 0x25, $f);        v6x3d_ctxparams(V::$v6tf_light, $s, s32(xread32($s + 0x146) + 0x1f4));    }    if ($mflags & 1) {        v6tf_build(xread32($e + 0x8), $s, V::$v6tf_parentctx);        v6x3d_ctxparams(V::$v6tf_parentctx, $s, s32(xread32($s + 0x146) + 0x1f4));    }    v6x3d_lights_activate($e, $mflags);    V::$v6x3d_vertbase = $model + xread32($model + 0x38);    V::$v6x3d_matbase = $model + xread32($model + 0x44);    V::$v6x3d_b54c = xread16($model + 0x26);    V::$v6x3d_b544 = $mflags;    V::$v6x3d_ae92 = ($mflags & 0x80) ? 1 : 0;    $nverts = xread16($model + 0x2c) & 0xffff;    $nbytes = ((($nverts >> 5) + 1) * 4);    if ($nbytes > 0x2000) {        $nbytes = 0x2000;    }    $vbits = &V::$v6x3d_vbits;    for ($i = 0; $i < $nbytes; $i++) {        $vbits[$i] = 0;    }    $na = xread16($model + 0x2e) & 0xffff;    $recA = $model + xread32($model + 0x3c);    for ($i = 0; $i < $na; $i++, $recA += 0x18) {        v6x3d_pipeA($recA);    }    $nb = xread16($model + 0x30) & 0xffff;    $recB = $model + xread32($model + 0x40);    for ($i = 0; $i < $nb; $i++, $recB += 0x18) {        v6x3d_pipeB($recB);    }}function v6x3d_gridland(int $e, int $s, int $model, int $mflags): void{    $listA = $model + xread32($model + 0x3c);    $listB = $model + xread32($model + 0x40);    V::$v6x3d_vertbase = $model + xread32($model + 0x38);    V::$v6x3d_matbase = $model + xread32($model + 0x44);    V::$v6x3d_b544 = $mflags;    v6tf_build($e, $s, V::$v6tf_objcam);    $far146 = xread32($s + 0x146);    if (V::$v6x3d_scale4) {        $far146 = shl32($far146, 2);    }    v6x3d_ctxparams(V::$v6tf_objcam, $s, s32($far146 + 500));    v6tf_build($s, $e, V::$v6tf_camobj);    $C = [xread16($s + 0x42), xread16($s + 0x44), xread16($s + 0x46)];    if ($mflags & 8) {        $C[1] <<= 2;    }    v6tf_apply($C, V::$v6tf_camobj, $C);    $sv = [xread32($s + 0x00), xread32($s + 0x04), xread32($s + 0x08)];    v6tf_apply($sv, V::$v6tf_camobj, V::$v6x3d_b660);    $R = xread16($s + 0x40) & 0xffff;    if ($mflags & 8) {        $R <<= 2;    }    $lo = [0, 0, 0];    $hi = [0, 0, 0];    static $orgo = [0x50, 0x58, 0x60], $maxo = [0x54, 0x5c, 0x64];    static $sho = [0x6d, 0x6e, 0x6f], $dimo = [0x70, 0x74, 0x78];    for ($a = 0; $a < 3; $a++) {        $org = xread32($model + $orgo[$a]);        $mx = xread32($model + $maxo[$a]);        $sh = xread8($model + $sho[$a]) & 31;        $dim = xread32($model + $dimo[$a]);        if ($C[$a] - $R > $mx) {            return;        }        $lo[$a] = s32($C[$a] - $R - $org) >> $sh;        if ($lo[$a] < 1) {            $lo[$a] = 0;        }        if ($C[$a] + $R < $org) {            return;        }        if ($lo[$a] >= $dim) {            $lo[$a] = $dim - 1;        }        $hi[$a] = s32($C[$a] + $R - $org) >> $sh;        if ($hi[$a] >= $dim) {            $hi[$a] = $dim - 1;        }        if ($hi[$a] < 0) {            $hi[$a] = 0;        }    }    $nA = xread16($model + 0x2e) & 0xffff;    $nB = xread16($model + 0x30) & 0xffff;    $nfb = (((($nA + $nB) & 0xffff) >> 5) + 1) * 4;    if ($nfb > 0x2000) {        $nfb = 0x2000;    }    $fbits = &V::$v6x3d_fbits;    for ($i = 0; $i < $nfb; $i++) {        $fbits[$i] = 0;    }    $nvb = (((xread16($model + 0x2c) & 0xffff) >> 5) + 1) * 4;    if ($nvb > 0x2000) {        $nvb = 0x2000;    }    $vbits = &V::$v6x3d_vbits;    for ($i = 0; $i < $nvb; $i++) {        $vbits[$i] = 0;    }    if ($mflags & 2) {        $f = xread8($e + 0x25);        xwrite8($e + 0x25, $f | 0x8 | (($mflags & 0x40) ? 0x2 : 0));        v6tf_build($e, $s, V::$v6tf_light);        xwrite8($e + 0x25, $f);        v6x3d_ctxparams(V::$v6tf_light, $s, s32(xread32($s + 0x146) + 500));    }    if ($mflags & 1) {        // model flag 1 parent frame hits the DOS stale-EDI bug: using the 13b58-correct form        v6tf_build(xread32($e + 0x8), $s, V::$v6tf_parentctx);        v6x3d_ctxparams(V::$v6tf_parentctx, $s, s32(xread32($s + 0x146) + 500));    }    v6x3d_lights_activate($e, $mflags);    $base = $model + xread32($model + 0x68);    $dim0 = xread32($model + 0x70);    $dim1 = xread32($model + 0x74);    $plane = mul32(mul32($dim1, $dim0), 4);    $rowst = mul32($dim0, 4);    for ($z = $lo[2]; $z <= $hi[2]; $z++) {        for ($y = $lo[1]; $y <= $hi[1]; $y++) {            $cp = s32($base + mul32($plane, $z) + mul32($rowst, $y) + $lo[0] * 4);            for ($x = $lo[0]; $x <= $hi[0]; $x++, $cp += 4) {                $off = xread32($cp);                if ($off == 0) {                    continue;                }                $fl = $model + $off;                $n = xread16($fl) & 0xffff;                $fl += 2;                for ($i = 0; $i < $n; $i++, $fl += 2) {                    $f = xread16($fl) & 0xffff;                    if ($f == 0xffff) {                        continue;                    }                    if ($fbits[$f >> 3] & (1 << ($f & 7))) {                        continue;                    }                    $fbits[$f >> 3] |= (1 << ($f & 7));                    if ($f < $nA) {                        v6x3d_pipeA($listA + $f * 0x18);                    } else {                        v6x3d_pipeB($listB + (($f - $nA) & 0xffff) * 0x18);                    }                }            }        }    }}function v6x3d_billboard(int $e, int $s): void{    if (V::$v6x3d_fake == 0) {        $hf = io_malloc(0x200);        if ($hf == 0) {            return;        }        V::$v6x3d_fake = xread32($hf);        $F0 = V::$v6x3d_fake;        $M0 = $F0 + 0x80;        $FR0 = $F0 + 0x11c;        $V00 = $F0 + 0x1b4;        xwrite16($M0 + 0x2c, 4);                     // verts        xwrite16($M0 + 0x2e, 0);                     // list A count        xwrite16($M0 + 0x30, 1);                     // list B: ONE quad        xwrite16($M0 + 0x32, 1);                     // texture slots        xwrite32($M0 + 0x38, 0x134);                 // self-rel -> verts        xwrite32($M0 + 0x40, 0x9c);                  // self-rel -> face rec        xwrite32($M0 + 0x44, 0xb4);                  // self-rel -> materials        xwrite32($M0 + 0x68, 0);                     // no cell grid        xwrite8($M0 + 0x6c, 0);        xwrite32($M0 + 0x28, 0);                     // model flags        xwrite16($FR0 + 0, 0);        xwrite16($FR0 + 2, 1);                       // quad 0,1,2,3        xwrite16($FR0 + 4, 2);        xwrite16($FR0 + 6, 3);        xwrite8($FR0 + 8, 0);        xwrite8($FR0 + 9, 0x80);                     // normal (0,-128,0): -Y        xwrite8($FR0 + 10, 0);        xwrite8($FR0 + 0xb, 0);                      // material 0        xwrite16($FR0 + 0xc, 0x8802);                // default flags        static $vn = [[-72, -72, 72], [72, -72, 72], [72, -72, -72], [-72, -72, -72]];        for ($vi = 0; $vi < 4; $vi++) {            $vv = $V00 + $vi * 0xC;            xwrite8($vv + 6, 8);                     // vertex flag: two-sided light            xwrite8($vv + 0xb, 0);            xwrite8($vv + 8, $vn[$vi][0]);            xwrite8($vv + 9, $vn[$vi][1]);            xwrite8($vv + 10, $vn[$vi][2]);        }    }    $F = V::$v6x3d_fake;            // fake elem    $M = $F + 0x80;                 // model hdr    $FR = $F + 0x11c;               // face record    $TS = $F + 0x134;               // texture slot    $V0 = $F + 0x1b4;               // 4 verts    $res = xread32($e + 0x3c);    if ($res == 0) {        return;    }    $res = s32($res + xread32($res));    $w = xread16($res + 0x10);    $hh = xread16($res + 0x12);    $hx = xread16($res + 0x14);    $hy = xread16($res + 0x16);    $rad = v6x_isqrt(s32($w * $w + $hh * $hh)) >> 1;    xwrite16($M + 0x12, $rad);                       // bounding radius    xwrite16($M + 0x0c, cdiv($w, 2) - $hx);          // centre offset    xwrite16($M + 0x0e, 0);    xwrite16($M + 0x10, cdiv($hh, 2) - $hy);    xwrite32($TS, $res - $TS);                       // self-rel res ptr    xwrite32($TS + 8, 1);                            // texture slot valid    $flip = xread8($e + 0x52);                       // b0 flipX, b1 flipY    static $cs = [[0, 0], [1, 0], [1, 1], [0, 1]];   // corner select    for ($i = 0; $i < 4; $i++) {        $X = $cs[$i][0] ? ($w - 1 - $hx) : -$hx;        $Y = $cs[$i][1] ? -($hh - 1 - $hy) : $hy;        if ($flip & 1) {            $X = -$X;        }        if ($flip & 2) {            $Y = -$Y;        }        $v = $V0 + $i * 0xC;        xwrite16($v + 0, $X);        xwrite16($v + 2, 0);        xwrite16($v + 4, $Y);        xwrite8($v + 0xb, xread8($e + 0x54));    }    xwrite8($FR + 0x10, 0);    xwrite8($FR + 0x11, 0);    xwrite8($FR + 0x12, $w - 1);    xwrite8($FR + 0x13, 0);    xwrite8($FR + 0x14, $w - 1);    xwrite8($FR + 0x15, $hh - 1);    xwrite8($FR + 0x16, 0);    xwrite8($FR + 0x17, $hh - 1);    xwrite16($FR + 0xc, 0x8202 | (($flip & 0x20) ? 8 : 0));    for ($i = 0; $i < 0x58; $i += 4) {        xwrite32($F + $i, xread32($e + $i));    }    xwrite32($F + 0x3c, $M - 4);    xwrite32($M - 4, 4);    xwrite16($F + 0x40, $rad);    xwrite16($F + 0x42, xread16($M + 0x0c));    xwrite16($F + 0x44, xread16($M + 0x0e));    xwrite16($F + 0x46, xread16($M + 0x10));    $mf = 0x42;    if ($flip & 0x08) {        $mf |= 0x20;    }    if ($flip & 0x04) {        $mf |= 0x10;    }    if ($flip & 0x10) {        $mf |= 0x80;    }    xwrite32($M + 0x28, $mf);    xwrite16($M + 0x26, 0);    v6x3d_object($F, $s);    xwrite32($TS, 0);}function v6x_mode6(int $s): void{    $sav_x2 = xread16($s + 0x6a);                    // clip x2    $sav_y2 = xread16($s + 0x6c);                    // clip y2    $sav_w = xread16($s + 0x7a);                     // buffer logical width    $sav_cx = xread16($s + 0x62);                    // projection center X    $sav_cy = xread16($s + 0x64);                    // projection center Y    if (xread8($s + 0x59) & 0x20) {                  // detail reduction        $clx = xread8($s + 0x136) & 31;        ImageV6Vars::$v6xclipx2 = s16(ImageV6Vars::$v6xclipx2 - s16(((ImageV6Vars::$v6xclipx2 - ImageV6Vars::$v6xclipx1 + 1) & 0xffff) >> $clx));        $w = (xread16($s + 0x6a) - xread16($s + 0x66) + 1) & 0xffff;        xwrite16($s + 0x6a, xread16($s + 0x6a) - ($w >> $clx));        xwrite16($s + 0x7a, (xread16($s + 0x7a) & 0xffff) >> $clx);        $pcx = xread16($s + 0x62);        xwrite16($s + 0x62, $pcx - (s16($pcx - xread16($s + 0x66)) >> $clx));        $cly = xread8($s + 0x137) & 31;        $gdy = ((ImageV6Vars::$v6xclipy2 - ImageV6Vars::$v6xclipy1 + 1) & 0xffff) >> $cly;        ImageV6Vars::$v6xclipy2 = s16(ImageV6Vars::$v6xclipy2 - $gdy);        $h = (xread16($s + 0x6c) - xread16($s + 0x68) + 1) & 0xffff;        xwrite16($s + 0x6c, xread16($s + 0x6c) - ($h >> $cly));        $pcy = xread16($s + 0x64);        xwrite16($s + 0x64, $pcy - (s16($pcy - xread16($s + 0x68)) >> $cly));    }    ImageV6Vars::$v6xclipx1 = xread16($s + 0x66);    ImageV6Vars::$v6xclipx2 = xread16($s + 0x6a);    ImageV6Vars::$v6xclipy1 = xread16($s + 0x68);    ImageV6Vars::$v6xclipy2 = xread16($s + 0x6c);    $frnode = Alis::$platform->kind != C::EPlatform3DO ? (node_take(0xb) & 0xffff) : 0;    v6x3d_framereset($s);    V::$v6x3d_heapmoved = 0;    $head = xread32($s + 0x5e);    if ($head != 0) {        for ($e = xread32($head + 0x38); $e != 0; $e = xread32($e + 0x38)) {            if ($e == xread32($e + 0x38)) {                break;            }            if (xread8($e + 0x24) & 0x40) {                continue;            }            if (xread8($e + 0x25) & 0x04) {                continue;            }            $st = v6sp_state($e);            if ($st == 0x30) {                V::$v6x3d_b530 = $e;                v6x3d_object($e, $s);            } elseif ($st == 0x32) {                V::$v6x3d_b530 = $e;                v6x3d_billboard($e, $s);            } elseif ($st == 0x20) {                if (xread16($e + 0x40) & 1) {                    v6tf_build($e, $s, V::$v6tf_objcam);                    $X = V::$v6tf_objcam->t[0];                    $Y = V::$v6tf_objcam->t[1];                    $Z = V::$v6tf_objcam->t[2];                    $d2 = $X * $X + $Y * $Y + $Z * $Z; // s64                    if ($d2 >= 4294967296.0) {           // (u64)d2 >> 32                        xwrite8($e + 0x47, 0);                        xwrite8($e + 0x45, 0);                        xwrite8($e + 0x51, 0);                        v6snd_step($e);                    } else {                        $dist = v6x_isqrt(s32($d2));                        $shift = ((xread16($e + 0x42) & 0xffff) >> 4) & 0xf;                        if ($shift) {                            $dd = $dist;                            if ($dd == 0) {                                $dd = 1;                            }                            $old = xread16($e + 0x4e) & 0xffff;                            xwrite16($e + 0x4e, $dd);                            if ($old == 0) {                                $freq = xread16($e + 0x48) & 0xffff;                            } else {                                $ab = $dd < 0 ? -$dd : $dd;                                $p = ($old - $dd) * $ab;                 // s64                                $v = d3_sar($p, 16 - $shift);                                $v = s32($v + 0x100);                                $f = $v * (xread16($e + 0x48) & 0xffff); // s64                                $freq = d3_sar($f, 8);                                if ($freq <= 0x64) {                                    $freq = 0x64;                                }                                if ($freq >= 0x7530) {                                    $freq = 0x7530;                                }                            }                            xwrite16($e + 0x4a, $freq);                        }                        $fade = xread32($s + 0x172);                        $vol1 = 0;                        $vol2 = 0;                        $A = s32((xread16($e + 0x52) & 0xffff) + $fade - $dist);                        if ($A > 0) {                            $vol1 = s8(xread8($e + 0x44));                            $vol2 = s8(xread8($e + 0x46));                            if ($A < $fade) {                                $vol2 = d3_div64($A * $vol2, $fade);                                $vol1 = d3_div64($A * $vol1, $fade);                            }                        }                        xwrite8($e + 0x47, $vol2);                        if ($vol1 > 0x7f) {                            $vol1 = 0x7f;                        }                        xwrite8($e + 0x45, $vol1);                        $dead = xread16($e + 0x52) >> 2;                        $pan = 0;                        $lat = $X;                        if ($lat < 0) {                            $lat = s32($lat + $dead);                            $crossed = ($lat >= 0);                        } else {                            $lat = s32($lat - $dead);                            $crossed = ($lat < 0);                        }                        if (!$crossed) {                            $p = $lat * xread16($s + 0x48); // s64                            $dep = $Y;                            if ($dep < 0) {                                $dep = s32(-$dep);                            }                            $dep = s32($dep + 1);                            $pan = d3_div64($p, $dep);                            $pan >>= 2;                            if ($pan <= -0x7f) {                                $pan = -0x7f;                            }                            if ($pan >= 0x7f) {                                $pan = 0x7f;                            }                        }                        xwrite8($e + 0x51, $pan);                        v6snd_step($e);                    }                }            }        }    }    node_free($frnode);    xwrite16($s + 0x64, $sav_cy);    xwrite16($s + 0x62, $sav_cx);    xwrite16($s + 0x7a, $sav_w);    xwrite16($s + 0x6c, $sav_y2);    xwrite16($s + 0x6a, $sav_x2);}function v6x_folscreen(int $s): void{    $id = xread16($s + 0x7e);    if ($id <= 0) {        return;    }    $vram = xread32(Alis::$atent + ($id & 0xffff));    if ($vram == 0) {        return;    }    $fl = xread8($s + 0x80);    if ($fl & 1) {        $v = s32(xread32($vram + 0x0c) + xread32($s + 0x82));        if ($v != xread32($s + 0x0c)) {            xwrite32($s + 0x0c, $v);            xwrite8($s + 0x58, xread8($s + 0x58) | 0x80);        }        $v = s32(xread32($vram + 0x10) + xread32($s + 0x86));        if ($v != xread32($s + 0x10)) {            xwrite32($s + 0x10, $v);            xwrite8($s + 0x58, xread8($s + 0x58) | 0x80);        }        $v = s32(xread32($vram + 0x14) + xread32($s + 0x8a) + V::$v6x_walkzoff);        if ($v != xread32($s + 0x14)) {            xwrite32($s + 0x14, $v);            xwrite8($s + 0x58, xread8($s + 0x58) | 0x80);        }    }    if ($fl & 2) {        $a = s16(xread16($vram + 0x18) + xread16($s + 0x8e) + V::$v6x_walkaoff);        if ($a != xread16($s + 0x18)) {            xwrite16($s + 0x18, $a);            xwrite8($s + 0x58, xread8($s + 0x58) | 0x80);        }        $a = s16(xread16($vram + 0x1a) + xread16($s + 0x90));        if ($a != xread16($s + 0x1a)) {            xwrite16($s + 0x1a, $a);            xwrite8($s + 0x58, xread8($s + 0x58) | 0x80);        }        $a = s16(xread16($vram + 0x1c) + xread16($s + 0x92));        if ($a != xread16($s + 0x1c)) {            xwrite16($s + 0x1c, $a);            xwrite8($s + 0x58, xread8($s + 0x58) | 0x80);        }    }}