<?phpnamespace App\Alis;// Port of alis/src/render/rrq3d_3do.c (MIT, Olivier Huguenot, Vadim Kindl).// Robinson's Requiem 3DO renderer: land tiles, billboards and sprites as 3DO cels, drawn into the// true-colour logic buffer (Img::$rgblogic: 3 bytes per pixel B, G, R, stride width * 3, see image_v6.php).// Struct layouts: see R3doVars. Every static function of the C file is prefixed with r3do_ (no collision// with rrq3d.c / deus3d.c names).// PORT: colours are 0xRRGGBB (the C ORs an opaque alpha 0xff000000 in: dropped).// PORT: the C float (single precision) math runs in PHP doubles: sub-pixel differences possible.const R3DO_CELMAX = 0x140;const R3DO_TILEMAX = 0x400;const R3DO_FARMUL = 2;const R3DO_CELCAP = R3DO_CELMAX * R3DO_FARMUL * R3DO_FARMUL;const R3DO_TILECAP = R3DO_TILEMAX * R3DO_FARMUL * R3DO_FARMUL;const R3DO_BUCKETS = 0x280;const R3DO_TEXMAX = 128;const R3DO_TEXBYTES = 0x80000;// '--far' doubles the view distance, 3DOs cel and tile limits grow with the areafunction r3do_viewfar(int $scr): int{ return mul32(xread32($scr + 0x146), Img::$ddrawdist ? R3DO_FARMUL : 1);}function r3do_celmax(): int{ return Img::$ddrawdist ? R3DO_CELCAP : R3DO_CELMAX;}function r3do_tilemax(): int{ return Img::$ddrawdist ? R3DO_TILECAP : R3DO_TILEMAX;}// C mulw(a, b) = (s32)((u32)a * (u32)b) -> mul32(); C sdiv(d, n) = d ? n / d : 0 -> cdiv(n, d)function r3do_tables(): int{ if (R3doVars::$tables_done) { return 1; } R3doVars::$tables_done = 1; // rotation recurrence: cos() is an fcos the 68040/060 lack (Line-F under plain TOS) $costab = []; $c = 1.0; $s = 0.0; $C = 0.99999882345170187925; $S = 0.00153398018628476550; // cos, sin(pi / 2048) for ($i = 0; $i < 0x1000; $i++) { $costab[$i] = (int) (4096 * $c); $t = $c * $C - $s * $S; $s = $s * $C + $c * $S; $c = $t; } $costab[2048] = -4095; $costab[2532] = -3017; $recip = [0 => 0]; for ($i = 1; $i < 0x8000; $i++) { $recip[$i] = intdiv(0x40000, $i); } R3doVars::$costab = $costab; R3doVars::$recip = $recip; return 1;}function r3do_rot(array &$m, int $a, int $b, int $ang): void{ if ($ang == 0) { return; } $c = R3doVars::$costab[$ang & 0xfff]; $s = R3doVars::$costab[($ang + 0xc00) & 0xfff]; for ($i = 0; $i < 3; $i++) { $x = $m[$a * 3 + $i]; $y = $m[$b * 3 + $i]; $m[$a * 3 + $i] = s16(($x * $c - $y * $s) >> 12); $m[$b * 3 + $i] = s16(($y * $c + $x * $s) >> 12); } $x = $m[9 + $a]; $y = $m[9 + $b]; $m[9 + $a] = s32(mul32($x, $c) - mul32($y, $s)) >> 12; $m[9 + $b] = s32(mul32($y, $c) + mul32($x, $s)) >> 12;}function r3do_cammat(int $scr, int $inv): array{ $m = [0x1000, 0, 0, 0, 0x1000, 0, 0, 0, 0x1000, 0, 0, 0]; $ax = xread16($scr + 0x18); $az = xread16($scr + 0x1c); if (!$inv) { r3do_rot($m, 1, 2, $ax >> 4); r3do_rot($m, 0, 1, $az >> 4); for ($i = 0; $i < 3; $i++) { $m[9 + $i] = s32($m[9 + $i] + xread32($scr + 0xc + $i * 4)); } return $m; } for ($i = 0; $i < 3; $i++) { $m[9 + $i] = s32($m[9 + $i] - xread32($scr + 0xc + $i * 4)); } r3do_rot($m, 0, 1, (-$az) >> 4); r3do_rot($m, 1, 2, (-$ax) >> 4); return $m;}// s16 * s32 products are exact in a double (32-bit PHP) and in 64-bit ints: s32() of the sum wraps like Cfunction r3do_xform(array $m, array &$v): void{ $x = $v[0]; $y = $v[1]; $z = $v[2]; for ($r = 0; $r < 3; $r++) { $v[$r] = s32($m[9 + $r] + (s32($m[$r * 3] * $x + $m[$r * 3 + 1] * $y + $m[$r * 3 + 2] * $z) >> 12)); }}/** Returns the pt3d [x, z, y, valid]. */function r3do_project(array $v, int $x, int $y, int $z): array{ $d = s32($v[10] + (s32($x * $v[3] + $y * $v[4] + $z * $v[5]) >> 12)); $px = s32($v[9] + (s32($x * $v[0] + $y * $v[1] + $z * $v[2]) >> 12)); $py = s32($v[11] + (s32($x * $v[6] + $y * $v[7] + $z * $v[8]) >> 12)); if ($d >= $v[16]) { $r = R3doVars::$recip[$d < 0x8000 ? $d : 0x7fff] ?? 0; return [ s32($v[14] + (mul32(mul32($r, $v[12]), $px) >> 18)), $d, s32($v[15] - (mul32(mul32($r, $v[13]), $py) >> 18)), 1, ]; } $div = $d < 1 ? $v[16] : $d; $ox = s32($v[14] + cdiv(mul32($v[12], $px), $div)); $oy = s32($v[15] - cdiv(mul32($v[13], $py), $div)); if ($oy > 300) { $oy = 300; } if ($oy < -0x32) { $oy = -0x32; } if ($ox > 0x280) { $ox = 0x280; } if ($ox < -0x140) { $ox = -0x140; } return [$ox, $d, $oy, 1];}function r3do_fogpixc(int $scr, int $depth, int $shade): int{ $fade = 0x100; $mode = xread16($scr + 0x94) & 0xffff; if ($mode != 100 && $mode != 101) { return 0x05000500; } $fog = Alis::$fog3do > 0xff ? 0xff : Alis::$fog3do; $far = r3do_viewfar($scr); // u32 if (ult($far, $depth)) { $depth = $far; } $f = shr32(mul32($depth, R3doVars::$recip[ult($far, 0x8000) ? $far : 0x7fff]), 4); $f = shr32(mul32($f, $f), 5); $nf = s32(0x800000 - $f); $g = shr32(mul32($fade, s32(shr32(mul32($f, $fog), 23) + (mul32(0x28 - $shade, $nf) >> 23))), 8); if (ult(0xff, $g)) { $g = 0xff; } $k = mul32($fade, shr32($nf, 15)); $kk = shr32($k, 8); $pt = R3doVars::pixctab; if (ule($kk, 0x80)) { $pix = $pt[shr32($k, 12)]; } elseif (ule($kk, 0x100)) { $pix = $pt[(($kk - 0x80) >> 5) + 8]; } elseif (ult($kk, 0x140)) { $pix = $pt[13]; } else { $pix = $pt[14]; } return (($g >> 3) << 1) | (($g >> 3) << 17) | $pix | 0x400040;}/** C newcel(): PORT: the corners p and an accw override are passed in (the C callers fill them after). */function r3do_newcel(int $b, int $img, int $pixc, int $depth, int $owner, int $aux, array $p, int $accw = -1): void{ R3doVars::$ncel++; if ($b > R3doVars::$maxbucket) { R3doVars::$maxbucket = $b; } R3doVars::$bucket[$b][] = [ 'img' => $img, 'p' => $p, 'pixc' => $pixc, 'accw' => $accw >= 0 ? $accw : (xread8($img + 0x1b) & 8), 'depth' => $depth, 'owner' => $owner, 'aux' => $aux, ];}function r3do_billboard(int $img, array $p, int $scale, int $scr, int $yoff, int $tag): void{ if (R3doVars::$ncel >= r3do_celmax()) { return; } if ($scale > 0x280000) { $scale = 0x200000; } $b = $p[1] >> 5; if ($b < 0 || $b >= R3DO_BUCKETS) { return; } $yoff = s16($yoff); $pixc = (xread8($img + 0x1b) & 4) ? 0x05000500 : r3do_fogpixc($scr, $b << 5, 0); $w = mul32($scale, xread16($img + 0x10) & 0xffff) >> 19; $top = mul32($scale, (xread16($img + 0x12) & 0xffff) + $yoff) >> 18; $bot = mul32($scale, $yoff) >> 18; $x = $p[0]; $y = $p[2]; r3do_newcel($b, $img, $pixc, $p[1], $tag, $yoff, [ s32($x - $w), s32($y - $top), s32($x + $w), s32($y - $top), s32($x + $w), s32($y - $bot), s32($x - $w), s32($y - $bot), ]);}function r3do_texquad(array $a, array $b, array $c, array $d, int $img, int $shade, int $scr, int $minh): void{ if (R3doVars::$ncel >= r3do_celmax()) { return; } $z = $a[1]; if ($z < $b[1]) { $z = $b[1]; } if ($z < $c[1]) { $z = $c[1]; } if ($z < $d[1]) { $z = $d[1]; } if ($z < 0 || ($z >> 5) >= R3DO_BUCKETS) { return; } $pixc = (xread8($img + 0x1b) & 4) ? 0x05000500 : r3do_fogpixc($scr, ($z >> 5) << 5, $shade); r3do_newcel($z >> 5, $img, $pixc, $z, $scr, ($minh & 0xff) << 16, [$a[0], $a[2], $d[0], $d[2], $c[0], $c[2], $b[0], $b[2]]);}function r3do_mirror(int $i, int $n): int{ return $i < 0 ? -$i : ($i >= $n ? 2 * $n - 2 - $i : $i);}function r3do_sprcel(int $img, array $p, int $scale, int $scr, int $e, int $hx, int $hy): void{ if (R3doVars::$ncel >= r3do_celmax()) { return; } if ($scale > 0x280000) { $scale = 0x200000; } $b = $p[1] >> 5; if ($b < 0 || $b >= R3DO_BUCKETS) { return; } $pixc = (xread8($img + 0x1b) & 4) ? 0x05000500 : r3do_fogpixc($scr, $b << 5, 0); $ox = xread16($img + 0x14) - $hx; $oy = xread16($img + 0x16) - $hy; $w = xread16($img + 0x10) & 0xffff; $h = xread16($img + 0x12) & 0xffff; $l = s32($p[0] - (mul32($scale, $ox) >> 18)); $r = s32($p[0] + (mul32($scale, $w - $ox) >> 18)); $t = s32($p[2] - (mul32($scale, $oy) >> 18)); $bt = s32($p[2] + (mul32($scale, $h - $oy) >> 18)); $accw = -1; if (xread8($e + 0x25) & 1) { $k = $l; $l = $r; $r = $k; $accw = 1; } r3do_newcel($b, $img, $pixc, $p[1], $e, 0, [$l, $t, $r, $t, $r, $bt, $l, $bt], $accw);}function r3do_mat7778(int $e0, int $cam): array{ $m = [0x1000, 0, 0, 0, 0x1000, 0, 0, 0, 0x1000, 0, 0, 0]; for ($e = $e0; $e; $e = xread32($e + 8)) { $f = xread8($e + 0x25); r3do_rot($m, 1, 2, xread16((($f & 2) && $cam ? $cam : $e) + 0x18) >> 4); r3do_rot($m, 2, 0, xread16((($f & 4) && $cam ? $cam : $e) + 0x1a) >> 4); r3do_rot($m, 0, 1, xread16((($f & 8) && $cam ? $cam : $e) + 0x1c) >> 4); for ($i = 0; $i < 3; $i++) { $m[9 + $i] = s32($m[9 + $i] + xread32($e + 0xc + $i * 4)); } } $chain = []; $n = 0; for ($c = $cam; $c && $n < 256; $c = xread32($c + 8)) { $chain[$n++] = $c; } $f = $e0 ? xread8($e0 + 0x25) : 0; while ($n--) { $c = $chain[$n]; if (!($f & 0x30)) { for ($i = 0; $i < 3; $i++) { $m[9 + $i] = s32($m[9 + $i] - xread32($c + 0xc + $i * 4)); } r3do_rot($m, 0, 1, (-xread16($c + 0x1c)) >> 4); } if (!($f & 0x20)) { r3do_rot($m, 2, 0, (-xread16($c + 0x1a)) >> 4); } if (!($f & 0x10)) { r3do_rot($m, 1, 2, (-xread16($c + 0x18)) >> 4); } } return $m;}function r3do_elemmat(int $e): array{ return r3do_mat7778($e, 0);}// move an entity by d turned by itd own anglesfunction r3do_polar(int $vram, int $dx, int $dy, int $dz): void{ if (!r3do_tables()) { return; } $m = r3do_mat7778($vram, xread32($vram + 8)); for ($r = 0; $r < 3; $r++) { $v = s32($m[$r * 3] * $dx + $m[$r * 3 + 1] * $dy + $m[$r * 3 + 2] * $dz) >> 12; xwrite32($vram + 0xc + $r * 4, s32($m[9 + $r] + $v + ($v < 0 ? 1 : 0))); }}function r3do_landmap(int $scr, int &$L, int &$H): int{ $owner = xread16($scr + 0xc4); $vram = $owner ? xread32(Alis::$atent + ($owner & 0xffff)) : 0; if (!$vram) { return 0; } $L = s32($vram + xread16($scr + 0xc6)); if (xread32($L - 0xc00) == 0) { return 0; } $H = $L; if (xread8($L - 2) & 0x80) { $H = s32(xread32($L) + xread32(xread32($L))); } return 1;}// probe box against the land of mode-5 screenfunction r3do_maptest(int $scr, int $X, int $Y, array $mn, array $mx, int $walking, int &$mat): int{ $L = 0; $H = 0; if (!r3do_landmap($scr, $L, $H)) { return 0; } $S = $L - 0xc00; $xs = xread16($L - 0x3c0); $ys = xread16($L - 0x3be); $hs = xread16($L - 0x3b8); $nx = xread16($L - 0x3cc); $ny = xread16($L - 0x3ca); $ix = $X >> $xs; $iy = $Y >> $ys; $cx = [$ix, $ix + 1, $ix + 1, $ix]; $cy = [$iy, $iy, $iy + 1, $iy + 1]; $c = []; $avg = 0; for ($i = 0; $i < 4; $i++) { $c[$i] = xread16($H + (r3do_mirror($cx[$i], $nx) * $ny + r3do_mirror($cy[$i], $ny)) * 2) & 0xffff; $avg += $c[$i] & 0xff; } $avg >>= 2; if ($walking) { $slot = $S + (($c[0] >> 8) & 0x3f) * 0x14; $img = s32($slot + xread32($slot)); $x0 = mul32($ix, 1 << $xs); $x1 = mul32($ix + 1, 1 << $xs); $y0 = mul32($iy, 1 << $xs); $y1 = mul32($iy + 1, 1 << $xs); if (($x0 < $mx[0] && $x1 > $mn[0] && $y0 < $mx[1] && $y1 > $mn[1] && $mx[2] > 0 && mul32($avg, 1 << $hs) > $mn[2]) || $mx[2] < 0 || $mn[2] < 0) { $mat = (xread16($img + 8) & 0xffff) | shl32(xread16($img + 0xa) & 0xffff, 16); return -3; } } for ($i = 0; $i < 4; $i++) { $slot = $S + (($c[$i] >> 8) & 0x3f) * 0x14; if (!xread32($slot + 0xc) || xread8($slot + 9) != 2) { continue; } $obj = s32($slot + 0xc + xread32($slot + 0xc)); $r = xread16($obj + 0x10) & 0xffff; $ht = xread16($obj + 0x12) & 0xffff; $ox = mul32($cx[$i], 1 << $xs); $oy = mul32($cy[$i], 1 << $xs); $oz = mul32($c[$i] & 0xff, 1 << $hs); if ($ox - 2 * $r <= $mx[0] && $ox + 2 * $r >= $mn[0] && $oy - 2 * $r <= $mx[1] && $oy + 2 * $r >= $mn[1] && $oz <= $mx[2] && $oz + $ht >= $mn[2]) { $mat = (xread16($obj + 8) & 0xffff) | shl32(xread16($obj + 0xa) & 0xffff, 16); return xread32($slot + 0x10); } } return 0;}function r3do_cellat(int $vram, int $dx, int $dy, int $dz, int $L, int $mode): int{ $H = $L; if (xread8($L - 2) & 0x80) { $H = s32(xread32($L) + xread32(xread32($L))); } $xs = xread16($L - 0x3c0); $ys = xread16($L - 0x3be); $nx = xread16($L - 0x3cc); $ny = xread16($L - 0x3ca); r3do_polar($vram, $dx, $dy, $dz); $X = 0; $Y = 0; for ($e = $vram; $e; $e = xread32($e + 8)) { $X = s32($X + xread32($e + 0xc)); $Y = s32($Y + xread32($e + 0x10)); } $ix = $X >> $xs; $iy = $Y >> $ys; $m0 = r3do_mirror($ix, $nx); $n0 = r3do_mirror($iy, $ny); $m1 = r3do_mirror($ix + 1, $nx); $n1 = r3do_mirror($iy + 1, $ny); $best = xread16($H + ($m0 * $ny + $n0) * 2) & 0xffff; if ($mode == 2) { return $best; } $k = [ xread16($H + ($m1 * $ny + $n0) * 2) & 0xffff, xread16($H + ($m1 * $ny + $n1) * 2) & 0xffff, xread16($H + ($m0 * $ny + $n1) * 2) & 0xffff, ]; for ($i = 0; $i < 3; $i++) { if ($mode == 0 ? ($k[$i] & 0xff) > ($best & 0xff) : ($k[$i] & 0xff) < ($best & 0xff)) { $best = $k[$i]; } } return $best;}function r3do_sprites(int $scr, array $v, int $x0, int $x1, int $y0, int $y1): void{ $node = xread32($scr + 0x5e); if (!$node) { return; } for ($e = xread32($node + 0x38); $e;) { $next = xread32($e + 0x38); if (xread8($e + 0x22) == 0x32 && !(xread8($e + 0x24) & 0x40)) { $m = r3do_elemmat($e); if ($m[9] < $x1 && $x0 < $m[9] && $m[10] < $y1 && $y0 < $m[10]) { $p = r3do_project($v, $m[9], $m[10], $m[11]); $az = 0; for ($k = $e; $k; $k = xread32($k + 8)) { $az = s16($az + xread16($k + 0x1c)); } $n = s8(xread8($e + 0x2e)); $vi = 0; if ($n) { $vi = ((($n * (((($az - xread16($scr + 0x1c)) & 0xffff) + 0x8000) & 0xffff) + 0x8000) >> 16) * 4) & 0xffff; if ($vi >= $n * 4) { $vi = 0; } } if (xread16($e + 0x1e) == 0) { xwrite16($e + 0x1e, 0x100); } $img = 0; $mv = xread32($e + 0x50); $hx = 0; $hy = 0; $skip = false; if (!$mv) { $r = s32(xread32($e + 0x3c) + $vi); $img = s32($r + xread32($r)); } else { $r = s32($mv + $vi); $t6 = s32($r + xread32($r)); if (!ult($t6, s32(Alis::$platform->ram_sz - 0x20)) || xread8($t6) != 6) { $skip = true; } else { $k = xread16($t6 + 0xe); $img = s32($r + $k * 4 + xread32($r + $k * 4)); xwrite8($e + 0x25, xread8($t6 + 0xc) == 1 ? xread8($e + 0x25) | 1 : xread8($e + 0x25) & 0xfe); xwrite16($e + 0x54, xread16($t6 + 0x12)); xwrite16($e + 0x56, xread16($t6 + 0x1a)); $hx = xread16($e + 0x54); $hy = xread16($e + 0x56); } } if (!$skip && $img) { r3do_sprcel($img, $p, cdiv(mul32(mul32(xread16($e + 0x1e), $v[13]), 0x400), $p[1]), $scr, $e, $hx, $hy); } } } // skip: if ($next == $e) { break; } $e = $next; }}function r3do_land(int $scr): void{ if (!r3do_tables()) { return; } R3doVars::$bucket = []; R3doVars::$ncel = 0; R3doVars::$maxbucket = -1; R3doVars::$drewland = 0; $owner = xread16($scr + 0xc4); if ($owner == 0) { return; } $vram = xread32(Alis::$atent + ($owner & 0xffff)); if ($vram == 0) { return; } $L = s32($vram + xread16($scr + 0xc6)); $slots = $L - 0xc00; if (xread32($slots) == 0) { return; } $H = $L; if (xread8($L - 2) & 0x80) { $H = s32(xread32($L) + xread32(xread32($L))); } $sh = s8(xread8($L - 0x3bf)); $cc = xread16($L - 0x3cc); $rc = xread16($L - 0x3ca); $tilemax = r3do_tilemax(); $cache = []; // pt3d per tile corner (C: valid = 0 for every entry) $tocam = r3do_cammat($scr, 0); $v = r3do_cammat($scr, 1); $v[12] = xread16($scr + 0x48); // fx $v[13] = xread16($scr + 0x4c); // fy $v[14] = xread16($scr + 0x62) & 0xffff; // cx $v[15] = xread16($scr + 0x64) & 0xffff; // cy $v[16] = xread32($scr + 0x142); // near $far = r3do_viewfar($scr); // view triangle (eye, far right, far left) in world space $half = cdiv(mul32($v[14], $far), $v[12]); $p1 = [$half, $far, 0]; $p2 = [s32(-$half), $far, 0]; $p3 = [0, 0, 0]; r3do_xform($tocam, $p1); r3do_xform($tocam, $p2); r3do_xform($tocam, $p3); $ymin = min($p1[1], $p2[1], $p3[1]); $ymax = max($p1[1], $p2[1], $p3[1]); $xmin = min($p1[0], $p2[0], $p3[0]); $xmax = max($p1[0], $p2[0], $p3[0]); $d1x = s32($p1[0] - $p3[0]); $d1y = s32($p1[1] - $p3[1]); $d2x = s32($p2[0] - $p3[0]); $d2y = s32($p2[1] - $p3[1]); $d3x = s32($p2[0] - $p1[0]); $d3y = s32($p2[1] - $p1[1]); // per tile row, columns triangle crosses at the sampled lines $r0 = $ymin >> $sh; $r1 = $ymax >> $sh; $eyerow = $p3[1] >> $sh; $e4 = $xmin >> $sh; $e8 = $xmax >> $sh; if ($r1 - $r0 + 1 > $tilemax) { return; } $spans = []; $ex1 = 0; $ex2 = 0; $ex3 = 0; $prevlo = 0; $prevhi = 0; $first = 1; for ($r = $r0; $r <= $r1; $r++) { for ($pass = ($r == $eyerow) ? 0 : 1; $pass < 2; $pass++) { $y = $pass == 0 ? $p3[1] : ($r == $r1 ? shl32($r, $sh) : shl32($r + 1, $sh)); if ($d1y) { $ex1 = s32(cdiv(mul32($d1x, s32($y - $p1[1])), $d1y) + $p1[0]); } if ($d2y) { $ex2 = s32(cdiv(mul32($d2x, s32($y - $p2[1])), $d2y) + $p2[0]); } if ($d3y) { $ex3 = s32(cdiv(mul32($d3x, s32($y - $p2[1])), $d3y) + $p2[0]); } $ok1 = $ex1 >= $xmin && $ex1 <= $xmax && $d1y; $ok2 = $ex2 >= $xmin && $ex2 <= $xmax && $d2y; $ok3 = $ex3 >= $xmin && $ex3 <= $xmax && $d3y; $a = $ex2; $b = $ex3; if ($ok1 && !$ok2) { $a = $ex1; } elseif ($ok1) { $a = $ex1; $b = $ex2; } $lo = $a < $b ? $a : $b; $hi = $a < $b ? $b : $a; if ($ok1 && $ok2 && $ok3) { if ($ex3 < $lo) { $lo = $ex3; } if ($ex3 > $hi) { $hi = $ex3; } } $lo >>= $sh; $hi >>= $sh; if ($lo == $hi) { $hi++; $lo--; } $rawlo = $lo; $rawhi = $hi; if (!$first) { if ($hi < $prevhi) { $hi = $prevhi; } if ($prevlo < $lo) { $lo = $prevlo; } } $first = 0; $spans[$r - $r0] = [$lo, $hi]; if ($lo < $e4) { $e4 = $lo; } if ($hi > $e8) { $e8 = $hi; } $prevlo = $rawlo; $prevhi = $rawhi; } } $f0 = $e8 - $e4 + 2; if ($f0 * ($r1 + 2 - $r0) > $tilemax) { return; } R3doVars::$drewland = 1; r3do_sprites($scr, $v, shl32($e4 - 1, $sh), shl32($e8 + 1, $sh), shl32($r0 - 1, $sh), shl32($r1 + 1, $sh)); $camx = xread32($scr + 0xc); $camy = xread32($scr + 0x10); $camz = xread32($scr + 0x14); $clx1 = xread16($scr + 0x66) & 0xffff; $cly1 = xread16($scr + 0x68) & 0xffff; $clx2 = xread16($scr + 0x6a) & 0xffff; $cly2 = xread16($scr + 0x6c) & 0xffff; $bscale = mul32(xread16($scr + 0x4c), 0x2a800); for ($r = $r0; $r <= $r1; $r++) { $rowa = $f0 * ($r - $r0); $rowb = $f0 * ($r + 1 - $r0); $rm0 = r3do_mirror($r, $rc); $rm1 = r3do_mirror($r + 1, $rc); $y0 = shl32($r, $sh); $y1 = shl32($r + 1, $sh); [$clo, $chi] = $spans[$r - $r0]; for ($col = $clo; $col <= $chi; $col++) { $i00 = $col - $e4 + $rowa + 1; $i10 = $col + 1 - $e4 + $rowa + 1; $i11 = $col + 1 - $e4 + $rowb + 1; $i01 = $col - $e4 + $rowb + 1; $cm0 = r3do_mirror($col, $cc); $cm1 = r3do_mirror($col + 1, $cc); $c00 = xread16($H + ($rc * $cm0 + $rm0) * 2) & 0xffff; $c10 = xread16($H + ($rc * $cm1 + $rm0) * 2) & 0xffff; $c11 = xread16($H + ($rc * $cm1 + $rm1) * 2) & 0xffff; $c01 = xread16($H + ($rc * $cm0 + $rm1) * 2) & 0xffff; $h00 = $c00 & 0xff; $h10 = $c10 & 0xff; $h11 = $c11 & 0xff; $h01 = $c01 & 0xff; $x0 = shl32($col, $sh); $x1 = shl32($col + 1, $sh); $slot = $slots + (($c00 >> 8) & 0x3f) * 0x14; $obj = xread32($slot + 0xc) ? s32($slot + 0xc + xread32($slot + 0xc)) : 0; $front = s32(mul32(s32($y0 - $camy), $h00 - $h01) + mul32($h00 - $h10, s32($x0 - $camx)) + shl32(s32($h00 - $camz), $sh)) < 0 || s32(mul32($h01 - $h11, s32($x1 - $camx)) + mul32($h10 - $h11, s32($y1 - $camy)) + shl32(s32($h11 - $camz), $sh)) < 0; $q00 = $cache[$i00] ?? ($cache[$i00] = r3do_project($v, $x0, $y0, $h00)); if (!$front) { if ($obj) { r3do_billboard($obj, $q00, cdiv($bscale, $q00[1]), $scr, xread16($slot + 0xa), xread32($slot + 0x10)); } continue; } $q10 = $cache[$i10] ?? ($cache[$i10] = r3do_project($v, $x1, $y0, $h10)); $q11 = $cache[$i11] ?? ($cache[$i11] = r3do_project($v, $x1, $y1, $h11)); $q01 = $cache[$i01] ?? ($cache[$i01] = r3do_project($v, $x0, $y1, $h01)); if ($obj) { r3do_billboard($obj, $q00, cdiv($bscale, $q00[1]), $scr, xread16($slot + 0xa), xread32($slot + 0x10)); } if (($q00[2] < $cly1 && $q10[2] < $cly1 && $q11[2] < $cly1 && $q01[2] < $cly1) || ($q00[2] > $cly2 && $q10[2] > $cly2 && $q11[2] > $cly2 && $q01[2] > $cly2) || ($q00[0] < $clx1 && $q10[0] < $clx1 && $q11[0] < $clx1 && $q01[0] < $clx1) || ($q00[0] > $clx2 && $q10[0] > $clx2 && $q11[0] > $clx2 && $q01[0] > $clx2)) { continue; } if ($q00[0] == $q10[0] && $q11[0] == $q01[0] && $q00[2] == $q10[2] && $q11[2] == $q01[2]) { continue; } if (xread32($slot)) { $shade = (($c00 >> 14) + ($c10 >> 14) + ($c11 >> 14) + ($c01 >> 14)) >> 1; $minh = min($h00, $h10, $h11, $h01); r3do_texquad($q00, $q10, $q11, $q01, s32($slot + xread32($slot)), $shade * 10 - 0x14, $scr, $minh); } } }}function r3do_texsig(int $img, int $len): string{ if (ult($len, 16) || ult(Alis::$platform->ram_sz, s32($img + $len))) { $len = 16; } return substr(Alis::$mem, $img, 32) . substr(Alis::$mem, $img + $len - 16, 16);}function r3do_texflush(): void{ R3doVars::$tex = []; R3doVars::$ntex = 0; R3doVars::$texbytes = 0;}/** Decoded cel pixels (u16 int array, index y * w + x) or null. */function r3do_texget(int $img, int &$w, int &$h): ?array{ $len = read32($img + 4); $sig = r3do_texsig($img, $len); $slot = -1; $ntex = R3doVars::$ntex; for ($i = 0; $i < $ntex; $i++) { $t = R3doVars::$tex[$i]; if ($t['img'] != $img) { continue; } if ($t['len'] == $len && $t['sig'] === $sig) { $w = $t['w']; $h = $t['h']; return $t['px']; } // stale: same address, different cel R3doVars::$texbytes -= $t['w'] * $t['h'] * 2; R3doVars::$tex[$i]['px'] = null; $slot = $i; break; } $px = cel_decode($img, $w, $h); if ($px === null) { if ($slot >= 0) { R3doVars::$ntex--; R3doVars::$tex[$slot] = R3doVars::$tex[R3doVars::$ntex]; unset(R3doVars::$tex[R3doVars::$ntex]); } return null; } $sz = $w * $h * 2; if ($slot < 0 && (R3doVars::$ntex >= R3DO_TEXMAX || R3doVars::$texbytes + $sz > R3DO_TEXBYTES)) { r3do_texflush(); } if ($slot < 0) { $slot = R3doVars::$ntex++; } R3doVars::$tex[$slot] = ['img' => $img, 'len' => $len, 'sig' => $sig, 'w' => $w, 'h' => $h, 'px' => $px]; R3doVars::$texbytes += $sz; return $px;}function r3do_rgb5(int $r, int $g, int $b): int{ return ((($r << 3) | ($r >> 2)) << 16) | ((($g << 3) | ($g >> 2)) << 8) | (($b << 3) | ($b >> 2));}function r3do_edge(array $p, int $a, int $b): float{ return hypot((float) ($p[$b] - $p[$a]), (float) ($p[$b + 1] - $p[$a + 1]));}// MapCel corners as p = a + e*u + f*v + g*u*v, plus the ~1.5 pixel overlap per texture axis// (to keep neighbouring quads from showing seams)function r3do_celsetup(array $c): array{ $p = $c['p']; $ax = (float) $p[0]; $ay = (float) $p[1]; $ex = $p[2] - $ax; $ey = $p[3] - $ay; $fx = $p[6] - $ax; $fy = $p[7] - $ay; $gx = $ax - $p[2] + $p[4] - $p[6]; $gy = $ay - $p[3] + $p[5] - $p[7]; return [ 'ax' => $ax, 'ay' => $ay, 'ex' => $ex, 'ey' => $ey, 'fx' => $fx, 'fy' => $fy, 'gx' => $gx, 'gy' => $gy, 'k2' => $gx * $fy - $gy * $fx, 'eu' => 1.5 / max(min(r3do_edge($p, 0, 2), r3do_edge($p, 6, 4)), 1.0), 'ev' => 1.5 / max(min(r3do_edge($p, 0, 6), r3do_edge($p, 2, 4)), 1.0), ];}function r3do_celbox(array $c, int &$x1, int &$y1, int &$x2, int &$y2): void{ $p = $c['p']; $x1 = $x2 = $p[0]; $y1 = $y2 = $p[1]; for ($i = 2; $i < 8; $i += 2) { if ($p[$i] < $x1) { $x1 = $p[$i]; } if ($p[$i] > $x2) { $x2 = $p[$i]; } if ($p[$i + 1] < $y1) { $y1 = $p[$i + 1]; } if ($p[$i + 1] > $y2) { $y2 = $p[$i + 1]; } }}/** C (s32) of a float: truncation, 0 for NaN / out of range (C: undefined). */function r3do_ftoi(float $f): int{ if (is_nan($f) || $f >= 2147483647.0 || $f <= -2147483648.0) { return $f > 0 ? 0x7fffffff : ($f < 0 ? -0x7fffffff - 1 : 0); } return (int) $f;}// texel index under pixel (xx, yy), or -1function r3do_texel(float $u, float $v, int $w, int $h): int{ $tx = r3do_ftoi($u * $w); $ty = r3do_ftoi($v * $h); $tx = $tx < 0 ? 0 : ($tx >= $w ? $w - 1 : $tx); $ty = $ty < 0 ? 0 : ($ty >= $h ? $h - 1 : $ty); return $ty * $w + $tx;}// barycentric weights of (px,py) for b and c in triangle abc (points = offsets ia, ib, ic into p)function r3do_bary(float $px, float $py, array $p, int $ia, int $ib, int $ic, float &$lb, float &$lc): float{ $a0 = $p[$ia]; $a1 = $p[$ia + 1]; $b0 = $p[$ib]; $b1 = $p[$ib + 1]; $c0 = $p[$ic]; $c1 = $p[$ic + 1]; $d = (float) ($b0 - $a0) * ($c1 - $a1) - (float) ($c0 - $a0) * ($b1 - $a1); if (abs($d) < 1e-6) { return 0.0; } $lb = (($px - $a0) * ($c1 - $a1) - ($c0 - $a0) * ($py - $a1)) / $d; $lc = (($b0 - $a0) * ($py - $a1) - ($px - $a0) * ($b1 - $a1)) / $d; return $d;}// pixel within e screen pixels of triangle abc (weights lb, lc; d = signed area x2)function r3do_intri(array $p, int $ia, int $ib, int $ic, float $d, float $lb, float $lc, float $e): bool{ $k = $e / abs($d); $la = 1 - $lb - $lc; return $la >= -$k * hypot($p[$ic] - $p[$ib], $p[$ic + 1] - $p[$ib + 1]) && $lb >= -$k * hypot($p[$ic] - $p[$ia], $p[$ic + 1] - $p[$ia + 1]) && $lc >= -$k * hypot($p[$ib] - $p[$ia], $p[$ib + 1] - $p[$ia + 1]);}// non-convex quad leaves pixels with no bilinear solution, which 3DOs forward mapping still covers: map those through the quad's two triangles insteadfunction r3do_celtri(array $c, float $px, float $py, int $w, int $h, float $e): int{ $p = $c['p']; $lb = 0.0; $lc = 0.0; $d = r3do_bary($px, $py, $p, 0, 2, 4, $lb, $lc); if ($d != 0 && ($d > 0 || $c['accw']) && r3do_intri($p, 0, 2, 4, $d, $lb, $lc, $e)) { return r3do_texel($lb + $lc, $lc, $w, $h); } $d = r3do_bary($px, $py, $p, 0, 4, 6, $lb, $lc); if ($d != 0 && ($d > 0 || $c['accw']) && r3do_intri($p, 0, 4, 6, $d, $lb, $lc, $e)) { return r3do_texel($lb, $lb + $lc, $w, $h); } return -1;}// texel index under pixel (xx, yy), or -1function r3do_celtexel(array $c, array $m, int $w, int $h, int $xx, int $yy): int{ $hx = $xx + 0.5 - $m['ax']; $hy = $yy + 0.5 - $m['ay']; $k1 = $m['ex'] * $m['fy'] - $m['ey'] * $m['fx'] + $hx * $m['gy'] - $hy * $m['gx']; $k0 = $hx * $m['ey'] - $hy * $m['ex']; if (abs($m['k2']) < 1e-4) { if (abs($k1) < 1e-6) { return r3do_celtri($c, $xx + 0.5, $yy + 0.5, $w, $h, 1.5); } $v = -$k0 / $k1; } else { $disc = $k1 * $k1 - 4 * $k0 * $m['k2']; if ($disc < 0) { return r3do_celtri($c, $xx + 0.5, $yy + 0.5, $w, $h, 1.5); } $disc = sqrt($disc); $v = (-$k1 - $disc) / (2 * $m['k2']); if ($v < -$m['ev'] || $v > 1 + $m['ev']) { $v = (-$k1 + $disc) / (2 * $m['k2']); } } $dx = $m['ex'] + $m['gx'] * $v; $dy = $m['ey'] + $m['gy'] * $v; if (abs($dx) > abs($dy)) { $u = $dx != 0 ? ($hx - $m['fx'] * $v) / $dx : NAN; } else { $u = $dy != 0 ? ($hy - $m['fy'] * $v) / $dy : NAN; // C: float division by 0 = inf / nan } if (!($u >= -$m['eu'] && $u <= 1 + $m['eu'] && $v >= -$m['ev'] && $v <= 1 + $m['ev'])) { return r3do_celtri($c, $xx + 0.5, $yy + 0.5, $w, $h, 1.5); } $wind = ($m['ex'] + $m['gx'] * $v) * ($m['fy'] + $m['gy'] * $u) - ($m['ey'] + $m['gy'] * $v) * ($m['fx'] + $m['gx'] * $u); if ($wind < 0 && !$c['accw']) { return -1; } return r3do_texel($u, $v, $w, $h);}// 3DO PIXC per channel: min(31, c * mf / df + av) (halved if bit 0); df is a power of twofunction r3do_pixc_lut(int $pixc): void{ if ($pixc == R3doVars::$pixlutc) { return; } static $dsh = [4, 1, 2, 3]; R3doVars::$pixlutc = $pixc; R3doVars::$pixstr = []; $mf = (($pixc >> 10) & 7) + 1; $sh = $dsh[($pixc >> 8) & 3]; $av = (($pixc >> 6) & 3) == 1 ? ($pixc >> 1) & 0x1f : 0; $lut = []; for ($i = 0; $i < 32; $i++) { $v = (($i * $mf) >> $sh) + $av; if ($pixc & 1) { $v >>= 1; } if ($v > 31) { $v = 31; } $lut[$i] = ($v << 3) | ($v >> 2); // C: pixlut[0] = e << 16 (red), [1] = e << 8, [2] = e } R3doVars::$pixlut = $lut;}/** The 3 bytes B, G, R of texel t through the current pixlut (cached in R3doVars::$pixstr). */function r3do_pixstr(int $t): string{ $lut = R3doVars::$pixlut; return R3doVars::$pixstr[$t] = chr($lut[$t & 0x1f]) . chr($lut[($t >> 5) & 0x1f]) . chr($lut[($t >> 10) & 0x1f]);}// one quad edge stepped per scanline: x in 24.8, u/v in texels 16.16 (celedge = [y0, y1, x, dx, u, du, v, dv])function r3do_celedge_set(array &$e, int $ax, int $ay, int $bx, int $by, int $ua, int $va, int $ub, int $vb, int $ystart): int{ if ($ay == $by) { return 0; } if ($ay > $by) { [$ax, $bx] = [$bx, $ax]; [$ay, $by] = [$by, $ay]; [$ua, $ub] = [$ub, $ua]; [$va, $vb] = [$vb, $va]; } $dy = $by - $ay; $e[0] = $ay; $e[1] = $by; // C: (s32)(((s64)(b0 - a0) << 8) / dy): a float on 32-bit PHP if it overflows, truncated like C $num = ($bx - $ax) * 256; $dx = s32(is_int($num) ? intdiv($num, $dy) : (int) ($num / $dy)); $e[3] = $dx; $du = intdiv(s32($ub - $ua), $dy); $dv = intdiv(s32($vb - $va), $dy); $e[5] = $du; $e[7] = $dv; // first scanline centre at or below the top vertex, or the clip top $y = $ystart > $ay ? $ystart : $ay; $k = $y - $ay; $e[2] = s32(shl32($ax, 8) + $dx * $k + intdiv($dx, 2)); $e[4] = s32($ua + $du * $k + intdiv($du, 2)); $e[6] = s32($va + $dv * $k + intdiv($dv, 2)); return 1;}// one textured span [xs, xe) on row y, clipped to [cx1, cx2]; the ends (overlap pixels) are// clamped, the rest runs unclamped// PORT: celpix() is inlined (clamped texel fetch, then the 3 bytes B,G,R written in place).function r3do_celspan(array $px, int $w, int $h, int $y, int $xs, int $xe, int $u, int $v, int $du, int $dv, int $cx1, int $cx2): void{ $U = $w << 16; $V = $h << 16; if ($xs < $cx1) { $u = s32($u + $du * ($cx1 - $xs)); $v = s32($v + $dv * ($cx1 - $xs)); $xs = $cx1; } if ($xe > $cx2 + 1) { $xe = $cx2 + 1; } $n = $xe - $xs; if ($n <= 0) { return; } $d = Img::$rgblogic + ($y * Alis::$platform->width + $xs) * 3; $m = &Alis::$mem; $map = &R3doVars::$pixstr; $wm1 = $w - 1; $hm1 = $h - 1; // clamped ends for ($k = 0; $k < 2 && $n > 0 && ($u < 0 || $u >= $U || $v < 0 || $v >= $V); $k++, $n--, $d += 3, $u = s32($u + $du), $v = s32($v + $dv)) { $tu = $u < 0 ? 0 : ($u >= $U ? $wm1 : $u >> 16); $tv = $v < 0 ? 0 : ($v >= $V ? $hm1 : $v >> 16); $t = $px[$tv * $w + $tu]; if ($t) { $s = $map[$t] ?? r3do_pixstr($t); $m[$d] = $s[0]; $m[$d + 1] = $s[1]; $m[$d + 2] = $s[2]; } } for ($k = 0; $k < 2 && $n > 0; $k++, $n--) { $ue = s32($u + $du * ($n - 1)); $ve = s32($v + $dv * ($n - 1)); if (!($ue < 0 || $ue >= $U || $ve < 0 || $ve >= $V)) { break; } $tu = $ue < 0 ? 0 : ($ue >= $U ? $wm1 : $ue >> 16); $tv = $ve < 0 ? 0 : ($ve >= $V ? $hm1 : $ve >> 16); $t = $px[$tv * $w + $tu]; if ($t) { $s = $map[$t] ?? r3do_pixstr($t); $o = $d + ($n - 1) * 3; $m[$o] = $s[0]; $m[$o + 1] = $s[1]; $m[$o + 2] = $s[2]; } } if ($n <= 0) { return; } $ue = s32($u + $du * ($n - 1)); $ve = s32($v + $dv * ($n - 1)); if ($u < 0 || $u >= $U || $v < 0 || $v >= $V || $ue < 0 || $ue >= $U || $ve < 0 || $ve >= $V) { for (; $n > 0; $n--, $d += 3, $u = s32($u + $du), $v = s32($v + $dv)) { $tu = $u < 0 ? 0 : ($u >= $U ? $wm1 : $u >> 16); $tv = $v < 0 ? 0 : ($v >= $V ? $hm1 : $v >> 16); $t = $px[$tv * $w + $tu]; if ($t) { $s = $map[$t] ?? r3do_pixstr($t); $m[$d] = $s[0]; $m[$d + 1] = $s[1]; $m[$d + 2] = $s[2]; } } return; } // both ends inside: every u/v of the span stays in [0, U) / [0, V), no wrap for (; $n > 0; $n--, $d += 3, $u += $du, $v += $dv) { $t = $px[($v >> 16) * $w + ($u >> 16)]; if ($t) { $s = $map[$t] ?? r3do_pixstr($t); $m[$d] = $s[0]; $m[$d + 1] = $s[1]; $m[$d + 2] = $s[2]; } }}// span between edges l and r (any order) on row y, plus the overlap row above/below the celfunction r3do_celrow(array $l, array $r, int $y): void{ if ($l[2] > $r[2]) { [$l, $r] = [$r, $l]; } // 1 pixel overlap all round hides T-junction cracks between terrain quads (old 1.5 px margin) $xs = (($l[2] - 128 + 255) >> 8) - 1; $xe = (($r[2] - 128 + 255) >> 8) + 1; $span = ($r[2] - $l[2]) >> 4; $du = $span > 0 ? intdiv(shl32(s32($r[4] - $l[4]), 4), $span) : 0; $dv = $span > 0 ? intdiv(shl32(s32($r[6] - $l[6]), 4), $span) : 0; $f = ($xs << 8) + 128 - $l[2]; $u = s32($l[4] + ($du >> 8) * $f); $v = s32($l[6] + ($dv >> 8) * $f); $px = R3doVars::$cc_px; $w = R3doVars::$cc_w; $h = R3doVars::$cc_h; $cx1 = R3doVars::$cc_cx1; $cx2 = R3doVars::$cc_cx2; r3do_celspan($px, $w, $h, $y, $xs, $xe, $u, $v, $du, $dv, $cx1, $cx2); if ($y == R3doVars::$cc_ymin && $y - 1 >= R3doVars::$cc_cy1 && R3doVars::$cc_ymin == R3doVars::$cc_ytop) { r3do_celspan($px, $w, $h, $y - 1, $xs, $xe, $u, $v, $du, $dv, $cx1, $cx2); } if ($y == R3doVars::$cc_ymax - 1 && $y + 1 <= R3doVars::$cc_cy2 && R3doVars::$cc_ymax == R3doVars::$cc_ybot) { r3do_celspan($px, $w, $h, $y + 1, $xs, $xe, $u, $v, $du, $dv, $cx1, $cx2); }}function r3do_celstep(array &$e): void{ $e[2] = s32($e[2] + $e[3]); $e[4] = s32($e[4] + $e[5]); $e[6] = s32($e[6] + $e[7]);}// edge from corner i to corner n, starting at row y; 0 if horizontalfunction r3do_celedge_at(array &$e, array $p, int $i, int $n, int $y): int{ static $celcu = [0, 1, 1, 0], $celcv = [0, 0, 1, 1]; $U = (R3doVars::$cc_w << 16) - 1; $V = (R3doVars::$cc_h << 16) - 1; return r3do_celedge_set($e, $p[$i * 2], $p[$i * 2 + 1], $p[$n * 2], $p[$n * 2 + 1], $celcu[$i] ? $U : 0, $celcv[$i] ? $V : 0, $celcu[$n] ? $U : 0, $celcv[$n] ? $V : 0, $y);}// convex quad: one left and one right chain from the top cornerfunction r3do_celconvex(array $p): void{ $top = 0; for ($i = 1; $i < 4; $i++) { if ($p[$i * 2 + 1] < $p[$top * 2 + 1]) { $top = $i; } } $a = [0, 0, 0, 0, 0, 0, 0, 0]; $b = [0, 0, 0, 0, 0, 0, 0, 0]; $ai = $top; $bi = $top; $y = R3doVars::$cc_ymin; $ymax = R3doVars::$cc_ymax; $a[1] = $b[1] = $y - 0x8000; while ($y < $ymax) { // next edge on each chain once the current one ends (horizontal ones are skipped) for ($k = 0; $k < 4 && $y >= $a[1]; $k++) { $n = ($ai + 3) & 3; if (!r3do_celedge_at($a, $p, $ai, $n, $y) || $a[1] <= $y) { $a[1] = $y - 0x8000; } $ai = $n; } for ($k = 0; $k < 4 && $y >= $b[1]; $k++) { $n = ($bi + 1) & 3; if (!r3do_celedge_at($b, $p, $bi, $n, $y) || $b[1] <= $y) { $b[1] = $y - 0x8000; } $bi = $n; } if ($y >= $a[1] || $y >= $b[1]) { return; } $end = $a[1] < $b[1] ? $a[1] : $b[1]; if ($end > $ymax) { $end = $ymax; } for (; $y < $end; $y++) { r3do_celrow($a, $b, $y); r3do_celstep($a); r3do_celstep($b); } }}// concave or bowtie quad: even-odd over all four edgesfunction r3do_celgeneral(array $p): void{ $e = []; $ne = 0; $ymin = R3doVars::$cc_ymin; for ($i = 0; $i < 4; $i++) { $t = [0, 0, 0, 0, 0, 0, 0, 0]; if (r3do_celedge_at($t, $p, $i, ($i + 1) & 3, $ymin)) { $e[$ne++] = $t; } } for ($y = $ymin; $y < R3doVars::$cc_ymax; $y++) { $x = []; $nx = 0; for ($i = 0; $i < $ne; $i++) { if ($y < $e[$i][0] || $y >= $e[$i][1]) { continue; } $k = $nx++; while ($k > 0 && $e[$x[$k - 1]][2] > $e[$i][2]) { $x[$k] = $x[$k - 1]; $k--; } $x[$k] = $i; } for ($k = 0; $k + 1 < $nx; $k += 2) { r3do_celrow($e[$x[$k]], $e[$x[$k + 1]], $y); } for ($i = 0; $i < $ne; $i++) { if ($y >= $e[$i][0] && $y < $e[$i][1]) { r3do_celstep($e[$i]); } } }}// 3DO MapCel quad, scanline filled; u/v exact on the edges, linear across each span. Pixel// centres decide coverage, so shared edges leave no seams.function r3do_drawcel(array $c, int $cx1, int $cy1, int $cx2, int $cy2): void{ $w = 0; $h = 0; $px = r3do_texget($c['img'], $w, $h); if ($px === null) { return; } $p = $c['p']; $area = 0; $ymin = $p[1]; $ymax = $p[1]; $pos = 0; $neg = 0; for ($i = 0; $i < 4; $i++) { $a = $i * 2; $b = (($i + 1) & 3) * 2; $d = (($i + 2) & 3) * 2; $area += $p[$a] * $p[$b + 1] - $p[$b] * $p[$a + 1]; $z = ($p[$b] - $p[$a]) * ($p[$d + 1] - $p[$b + 1]) - ($p[$b + 1] - $p[$a + 1]) * ($p[$d] - $p[$b]); $pos |= (int) ($z > 0); $neg |= (int) ($z < 0); if ($p[$a + 1] < $ymin) { $ymin = $p[$a + 1]; } if ($p[$a + 1] > $ymax) { $ymax = $p[$a + 1]; } } // PORT: area is a C s32 sum (wraps); here exact (int or float): only its sign / zero test matters if ($area == 0 || ($area < 0 && !$c['accw'])) { return; } R3doVars::$cc_ytop = $ymin; R3doVars::$cc_ybot = $ymax; R3doVars::$cc_ymin = $ymin < $cy1 ? $cy1 : $ymin; R3doVars::$cc_ymax = $ymax > $cy2 + 1 ? $cy2 + 1 : $ymax; if (R3doVars::$cc_ymin >= R3doVars::$cc_ymax) { return; } R3doVars::$cc_px = $px; R3doVars::$cc_w = $w; R3doVars::$cc_h = $h; R3doVars::$cc_cx1 = $cx1; R3doVars::$cc_cy1 = $cy1; R3doVars::$cc_cx2 = $cx2; R3doVars::$cc_cy2 = $cy2; r3do_pixc_lut($c['pixc']); // (celrows[]: row pointers for the C asm span loop, not needed here) if ($pos && $neg) { r3do_celgeneral($p); } else { r3do_celconvex($p); } R3doVars::$cc_px = [];}/** C r3do_pick(scr, x, y, s32 w3[3], u32 *aux, u32 *img): returns the owner of the cel under (x, y). */function r3do_pick(int $scr, int $x, int $y, array &$w3, int &$aux, int &$img): int{ $best = null; $bw = 0x7fffffff; $bh = 0x7fffffff; $hit = false; for ($b = 0; $b <= R3doVars::$maxbucket && !$hit; $b++) { $list = R3doVars::$bucket[$b] ?? []; for ($i = count($list) - 1; $i >= 0; $i--) { $c = $list[$i]; $x1 = $y1 = $x2 = $y2 = 0; r3do_celbox($c, $x1, $y1, $x2, $y2); if ($x < $x1 || $x > $x2 || $y < $y1 || $y > $y2) { continue; } $w = 0; $h = 0; $px = r3do_texget($c['img'], $w, $h); $m = r3do_celsetup($c); $t = $px !== null ? r3do_celtexel($c, $m, $w, $h, $x, $y) : -1; $hit = $t >= 0 && $px[$t]; if ($hit && ($best === null || $c['depth'] < $best['depth'] || ($c['depth'] == $best['depth'] && ($x2 - $x1 < $bw || $y2 - $y1 < $bh)))) { $best = $c; $bw = $x2 - $x1; $bh = $y2 - $y1; } } } if ($best === null) { return 0; } $d = $best['depth']; $v = [ cdiv(mul32($d, $x - (xread16($scr + 0x62) & 0xffff)), xread16($scr + 0x48)), $d, cdiv(mul32($d, (xread16($scr + 0x64) & 0xffff) - $y), xread16($scr + 0x4c)), ]; $m = r3do_cammat($scr, 0); r3do_xform($m, $v); if ($best['aux'] >> 16) { $v[2] = $best['aux'] >> 16; } $w3 = $v; $aux = $best['aux']; $img = $best['img']; return $best['owner'];}// nothing to draw since the last r3do_landfunction r3do_empty(): int{ return (int) (!R3doVars::$drewland && R3doVars::$maxbucket < 0);}// PORT: one write of the whole block (rows of fill + the untouched bytes between them): mem_write// copies the memory string once instead of once per row.function r3do_fogfill(int $col, int $x1, int $y1, int $x2, int $y2): void{ if ($x2 < $x1 || $y2 < $y1) { return; } $W3 = Alis::$platform->width * 3; $n3 = ($x2 - $x1 + 1) * 3; $fill = str_repeat(chr($col & 0xff) . chr(($col >> 8) & 0xff) . chr(($col >> 16) & 0xff), $x2 - $x1 + 1); $start = Img::$rgblogic + $y1 * $W3 + $x1 * 3; $gap = $W3 - $n3; $s = ''; for ($yy = $y1; $yy <= $y2; $yy++) { $s .= $fill; if ($yy < $y2 && $gap > 0) { $s .= substr(Alis::$mem, $start + ($yy - $y1) * $W3 + $n3, $gap); } } mem_write($start, $s);}function r3do_flush(int $scr): void{ if (!Img::$rgblogic || !r3do_tables()) { return; } $W = Alis::$platform->width; $H = Alis::$platform->height; $x1 = xread16($scr + 0x66); $y1 = xread16($scr + 0x68); $x2 = xread16($scr + 0x6a); $y2 = xread16($scr + 0x6c); if ($x1 < 0) { $x1 = 0; } if ($y1 < 0) { $y1 = 0; } if ($x2 >= $W) { $x2 = $W - 1; } if ($y2 >= $H) { $y2 = $H - 1; } if (R3doVars::$drewland) { $g = (Alis::$fog3do > 0xff ? 0xff : Alis::$fog3do) >> 3; r3do_fogfill(r3do_rgb5($g, $g, $g), $x1, $y1, $x2, $y2); } for ($b = R3doVars::$maxbucket; $b >= 0; $b--) { $list = R3doVars::$bucket[$b] ?? []; for ($i = count($list) - 1; $i >= 0; $i--) { r3do_drawcel($list[$i], $x1, $y1, $x2, $y2); } }}