<?phpnamespace App\Alis;// HD edition (not in the C): the land of Robinson's Requiem (rrq3d.c, DOS back end) drawn a second time at twice// the resolution, by the same renderer, for the host (App\Hd\HdRenderer) to show over the scaled picture.//// affiland() draws the land as the game does (the VM's state, the screen: unchanged), then hd3d_pass():// 1. the VM's state after that frame is kept aside (the Img / Rrq3dVars variables, the sprites, the scene's and// the render context's memory);// 2. the state from before the frame comes back, with the scene's screen made twice as big (viewport, centre,// lens), its buffer, row table, altitude and slope tables replaced by bigger ones (host blocks past the RAM);// 3. the same pipeline runs (calctoy, spritland, calclan0, doland: the land, its sky and its sprites) into the// big buffer; the pixels are taken (Hd3d::$land);// 4. the state after the game's own frame comes back: the game never sees the HD pass.// The host then shows those pixels where the frame still has the land (what the VM drew over it stays)./** Before the game's own land frame: the state the HD pass will start from. */function hd3d_begin(int $scene, int $rc, bool $partial = false): array{ // the sprites the game's own land draws now (zoomtofen()); a partial redraw (landtofi) keeps those of before if (!$partial) { Hd3d::$drawn = []; } Hd3d::$recording = true; $t = microtime(true); return ['scene' => $scene, 'rc' => $rc, 'state' => hd3d_state($scene, $rc), 't' => microtime(true) - $t];}/** After doland(): the land as drawn at the game's resolution (to know which pixels of the screen it is). */function hd3d_low(array &$hd): void{ // the viewport: the scene's own (the clip variables are changed by doland()) $scene = $hd['scene']; $x0 = xread16($scene + 0xe); $y0 = xread16($scene + 0x10); $w = (xread16($scene + 0x12) + 1) & 0xffff; $h = (xread16($scene + 0x14) + 1) & 0xffff; $buffer = xread32(xread32($scene + 0x30)); $low = ''; for ($r = 0; $r < $h; $r++) { $low .= substr(Alis::$mem, $buffer + $x0 + $r * 0x140, $w); } $hd += ['x' => $x0, 'y' => $y0, 'w' => $w, 'h' => $h, 'low' => $low, 'drawn' => Hd3d::$drawn]; Hd3d::$recording = false;}/** The VM's state the land pipeline changes. @return array<string, mixed> */function hd3d_state(int $scene, int $rc): array{ static $img = null, $v = null; $img ??= new \ReflectionClass(Img::class); $v ??= new \ReflectionClass(Rrq3dVars::class); $sprites = []; foreach (Img::$sprites as $k => $s) { $sprites[$k] = get_object_vars($s); } return [ 'img' => $img->getStaticProperties(), 'v' => $v->getStaticProperties(), 'sprites' => $sprites, 'scene' => substr(Alis::$mem, $scene, 0x100), 'rc' => substr(Alis::$mem, $rc - 0x400, 0x400), ];}function hd3d_restore(array $state, int $scene, int $rc): void{ foreach ($state['img'] as $name => $value) { Img::$$name = $value; } foreach ($state['v'] as $name => $value) { Rrq3dVars::$$name = $value; } foreach (Img::$sprites as $k => $s) { if (!isset($state['sprites'][$k])) { unset(Img::$sprites[$k]); // made by the pass continue; } foreach ($state['sprites'][$k] as $p => $value) { $s->$p = $value; } } mem_write($scene, $state['scene']); mem_write($rc - 0x400, $state['rc']);}/** The land again at twice the resolution: Hd3d::$land. */function hd3d_pass(array $hd): void{ $scene = $hd['scene']; $rc = $hd['rc']; if (!isset($hd['low']) || $hd['w'] <= 0 || $hd['h'] <= 0) { return; } // the same land as the last HD one (the VM drew it again, nothing moved): that HD land is still right $last = Hd3d::$land; if ($last !== null && $last['low'] === $hd['low'] && $last['x'] === $hd['x'] && $last['y'] === $hd['y']) { Hd3d::$pending = null; return; } // a computer too slow for this view: the game's own land while it moves, the HD land once it stays still if (empty($hd['deferred']) && Hd3d::isSlow($scene)) { $pending = Hd3d::$pending; if ($pending === null || $pending['low'] !== $hd['low'] || $pending['scene'] !== $scene) { Hd3d::$pendingAt = SysVars::$msec / 1000; } Hd3d::$pending = $hd; // the HD land of before stays: hd3d_shown() lets the game's pixels through where the land changed (a // character walking away), or gives it up when too much changed (the view turned): no HD / game / HD blinking return; } $t0 = microtime(true); $after = hd3d_state($scene, $rc); $check = Hd3d::$debug ? hd3d_everything() : null; hd3d_restore($hd['state'], $scene, $rc); try { $S = Hd3d::$passScale; $w = (xread16($scene + 0x12) + 1) & 0xffff; $h = (xread16($scene + 0x14) + 1) & 0xffff; hd3d_buffers($w * $S, $h * $S, xread16($rc - 0x3cc) & 0xffff); // the screen of the scene, twice as big: its centre, its viewport (its corner at 0, 0 of the big buffer: the // buffer is the viewport), the lens computed for it // the centre of the projection is on the screen (absolute): in the big buffer, from the viewport's corner xwrite16($scene + 0xa, (xread16($scene + 0xa) - xread16($scene + 0xe)) * $S); xwrite16($scene + 0xc, (xread16($scene + 0xc) - xread16($scene + 0x10)) * $S); xwrite16($scene + 0xe, 0); xwrite16($scene + 0x10, 0); xwrite16($scene + 0x12, $w * $S - 1); xwrite16($scene + 0x14, $h * $S - 1); xwrite32($scene + 0x30, Hd3d::$handle); Hd3d::$scale = $S; Hd3d::$lowX = $hd['x']; // the tables the game draws with (made once by openland(), with the lens and camera of that moment: a script // may have changed the lens since, the game keeps them): its altitude table scaled, the same slopes and row // pointers for the big buffer, the projection scales and their copies scaled $le = Alis::$platform->is_little_endian; $rows = $le ? Rrq3dConst::DOS_ALT_ROWS : Rrq3dConst::M68K_ALT_ROWS; $count = $rows * 0x100 + ($le ? Rrq3dConst::DOS_ALT_SENTINEL : Rrq3dConst::M68K_ALT_SENTINEL); $alt = substr(Alis::$mem, Img::$atalti, $count * 2); // the tables change only when the game's do (openland()): made again then, not at every frame $version = crc32($alt) . "|$S|" . Hd3d::$tables; $tables = Hd3d::$tables; Img::$atalti = $tables; Img::$atlpix = $tables + Rrq3dConst::DOS_ATLPIX_OFS; Img::$atalias = Img::$atlpix + 0x1000; if (Hd3d::$tablesFor !== $version) { Hd3d::$tablesFor = $version; $words = unpack($le ? 'v*' : 'n*', $alt); foreach ($words as $k => $v) { if ($v !== 20000) { $words[$k] = (((($v ^ 0x8000) - 0x8000) * $S) & 0xffff); } } mem_write($tables, pack($le ? 'v*' : 'n*', ...$words)); $row = xread32(Hd3d::$handle); $pix = ''; for ($i = 0, $n = Hd3d::rows(); $i < $n; $i++) { $pix .= pack('V', $row + $i * Hd3d::stride()); } mem_write(Img::$atlpix, $le ? $pix : implode('', array_map('strrev', str_split($pix, 4)))); $aw = Hd3d::aliasWidth(); $alias = []; for ($d = 1; $d <= $aw; $d++) { for ($i = 0; $i < $aw; $i++) { $alias[] = \intdiv($i * 0x100, $d) & 0xffff; } } mem_write(Img::$atalias, pack($le ? 'v*' : 'n*', ...$alias)); } foreach ([0x76, 0x7a] as $o) { xwrite16($scene + $o, xread16($scene + $o) * $S); } // (rc-0x262 and rc-0x2c8, which pick the altitude table's row, are depths in the world: unchanged) foreach ([0x3aa, 0x3a6] as $o) { xwrite16($rc - $o, xread16($rc - $o) * $S); } Hd3d::$inPass = true; $drawnNow = Hd3d::$drawn; Hd3d::$drawn = $hd['drawn'] ?? []; // affiland()'s 3D branch, into the big buffer $scene_addr = $scene; Img::$mapscreen = $scene_addr; Img::$wloglarg = Hd3d::stride(); Img::$wlogic = xread32(xread32($scene_addr + 0x30)); Img::$wlogx1 = 0; Img::$wlogy1 = 0; Img::$fenx1 = 0; Img::$landclipy1 = 0; Img::$landclipx2 = s16($w * $S - 1); Img::$feny2 = s16($h * $S - 1); Img::$clipl = $w * $S; Img::$landcliph = $h * $S; Img::$vbarclipx2 = s16(Img::$landclipx2 + 1); Img::$feny1 = 0; Img::$fenx2 = Img::$landclipx2; Img::$clipx1 = 0; Img::$clipy1 = 0; Img::$clipx2 = Img::$landclipx2; Img::$clipy2 = Img::$feny2; Img::$cliph = Img::$landcliph; calctoy($scene_addr, $rc); spritland($scene_addr, xread16($scene_addr + 2)); calclan0($scene_addr, $rc); if (((xread8($scene_addr + 1) & 0x40) == 0) && (xread16($scene_addr + 0xa2) == 0)) { clrvga(); } doland($scene_addr, $rc); $stride = Hd3d::stride(); $big = ''; for ($r = 0, $hh = $h * $S; $r < $hh; $r++) { $big .= substr(Alis::$mem, Img::$wlogic + $r * $stride, $w * $S); } $sl = $GLOBALS['spr']['low'] ?? []; $sh = $GLOBALS['spr']['hd'] ?? []; foreach ($sh as $k => $v) { if (!isset($sl[$k])) { error_log(sprintf('SPR HD only %d: x=%d y=%d w=%d h=%d d=%d (frame %d)', $k, ...$v, ...[SysVars::$frameCount])); } } $GLOBALS['spr'] = []; Hd3d::$land = ['x' => $hd['x'], 'y' => $hd['y'], 'w' => $hd['w'], 'h' => $hd['h'], 'scale' => $S, 'low' => $hd['low'], 'hd' => $big]; if (Trace::on()) { Trace::line(sprintf('pass land=%d,%d %dx%d crc=%08x hd=%08x', $hd['x'], $hd['y'], $hd['w'], $hd['h'], crc32($hd['low']), crc32($big))); } } catch (\Throwable $e) { // the HD picture is a bonus: an accident in it never stops the game Hd3d::$land = null; error_log('ALIS HD 3D: ' . $e->getMessage()); } finally { Hd3d::$scale = 1; Hd3d::$inPass = false; Hd3d::$drawn = $drawnNow ?? Hd3d::$drawn; hd3d_restore($after, $scene, $rc); if ($check !== null) { hd3d_compare($check, hd3d_everything()); } if (empty($hd['deferred'])) { Hd3d::spent($scene, microtime(true) - $t0 + ($hd['t'] ?? 0.0)); } }}/** Debugging (Hd3d::$debug): every static variable of the VM and its memory (without the HD blocks). */function hd3d_everything(): array{ $all = []; foreach (get_declared_classes() as $class) { if (str_starts_with($class, 'App\\Alis\\') && $class !== Hd3d::class && $class !== SysVars::class) { foreach ((new \ReflectionClass($class))->getStaticProperties() as $name => $value) { $all["$class::$name"] = is_object($value) ? serialize(get_object_vars($value)) : (is_array($value) ? md5(serialize(array_map(static fn ($v) => is_object($v) ? get_object_vars($v) : $v, $value))) : $value); } } } $all['mem'] = substr(Alis::$mem, 0, Hd3d::$buffer ?: strlen(Alis::$mem)); return $all;}function hd3d_compare(array $a, array $b): void{ foreach ($a as $k => $v) { if (($b[$k] ?? null) !== $v) { if ($k === 'mem') { $n = min(strlen($v), strlen($b[$k])); for ($i = 0; $i < $n && $v[$i] === $b[$k][$i]; $i++) { } error_log(sprintf('HD 3D changed the memory at 0x%x (scene 0x%x)', $i, Img::$mapscreen)); } else { error_log("HD 3D changed $k"); } } }}/** The big buffer (8 rows of margin above, like openland()'s 4) and the tables, made once per size. */function hd3d_buffers(int $w, int $h, int $strips): void{ $size = "$w x $h"; if (Hd3d::$size === $size && Hd3d::$buffer !== 0 && Hd3d::$buffer < strlen(Alis::$mem)) { return; } $stride = 0x140 * 2; Hd3d::$buffer = host_alloc(($h + 18) * $stride); Hd3d::$handle = host_alloc(4); xwrite32(Hd3d::$handle, Hd3d::$buffer + 8 * $stride); // [altitude 0x16800][row table 0x1000][slopes 128 x 128 words] Hd3d::$tables = host_alloc(Rrq3dConst::DOS_ATLPIX_OFS + 0x1000 + 0x80 * 0x80 * 2); Hd3d::$size = $size;}/** * doland() of the HD pass: the sky, computed at the game's resolution and drawn with its pixels doubled (the * sky is a picture: skytofen() would show it half as big). */function hd3d_sky(int $scene_addr, int $render_context): void{ $S = Hd3d::$scale; $sky_res = xread32($scene_addr + 0xa4); $sky_data = s32(xread32($sky_res) + $sky_res); $hotspot_y = 0; $bitmap = $sky_data; if (xread8($sky_data) == 3) { $sky_res = s32(s16(xread16($sky_data + 2) * 4) + $sky_res); $bitmap = s32(xread32($sky_res) + $sky_res); $hotspot_y = xread16($sky_data + 6); } $stride = Img::$wloglarg; $dst0 = s32(Img::$clipx1 + xread32(Img::$atlpix + s16((Img::$clipy1 - Img::$wlogy1) * 4))); $cl = intdiv(Img::$clipl, $S); $rows = intdiv(Img::$cliph, $S); $put = static function (int $r, string $row) use ($dst0, $stride, $S): void { $wide = $S === 2 ? strtr($row, hd3d_doubler()) : $row; for ($k = 0; $k < $S; $k++) { mem_write($dst0 + ($r * $S + $k) * $stride, $wide); } }; if (xread8($bitmap) == 1) { $c = xread8($bitmap + 1) & 0xf; $row = str_repeat(chr($c | ($c << 4)), $cl); for ($r = 0; $r < $rows; $r++) { $put($r, $row); } return; } // the game's resolution, from the viewport's top (the centre is relative to it in the HD pass) $clipy1 = intdiv(Img::$clipy1, $S); $bh = xread16($bitmap + 4); $skyposy = s16(($hotspot_y + intdiv(xread16($scene_addr + 0xc) + xread16($render_context - 0x352), $S)) - ((($bh + 1) & 0xffff) >> 1)); $tex_width = s16(xread16($bitmap + 2) + 1); $low_x1 = Hd3d::$lowX; // the viewport's left edge on the game's screen (the scroll uses it) $scroll_mod = s16(cmod(s16(s16(shr32(xread16($scene_addr + 0x38) * xread16($scene_addr + 0xa8), 8)) - $low_x1), $tex_width)); $scroll_pos = (-$scroll_mod) & 0xffff; if (0 < $scroll_mod) { $scroll_pos = ($tex_width + $scroll_pos) & 0xffff; } $posx = s16($scroll_pos & 0xfffe); $data_offset = Alis::$platform->bpp == 4 ? 6 : 8; $top = chr(xread8($bitmap + $data_offset)); $bottom = chr(xread8(s32($bitmap + mul32($tex_width & 0xffff, $bh) + $data_offset))); for ($r = 0; $r < $rows; $r++) { $y = $clipy1 + $r - $skyposy; // the row of the sky picture if ($y < 0) { $row = str_repeat($top, $cl); } elseif ($y >= $bh) { $row = str_repeat($bottom, $cl); } else { $line = substr(Alis::$mem, $bitmap + $data_offset + $y * $tex_width, $tex_width); $line = substr($line, $posx) . $line; while (strlen($line) < $cl) { $line .= $line; } $row = substr($line, 0, $cl); } $put($r, $row); }}/** strtr() map: each byte twice. @return array<string, string> */function hd3d_doubler(): array{ static $map = null; if ($map === null) { $map = []; for ($i = 0; $i < 256; $i++) { $map[chr($i)] = chr($i) . chr($i); } } return $map;}/** * sys_render(): the HD land for the host, where the frame still shows it (what the VM drew over the land: not * replaced). Returns [x, y, w, h (game pixels), scale, the HD pixels (8-bit, the frame's palette), the colour * index that marks "not the land" (or -1)], or null. * @return array{0: int, 1: int, 2: int, 3: int, 4: int, 5: string, 6: int}|null */function hd3d_shown(string $pixels, int $fw, int $fh): ?array{ $land = Hd3d::$land; Hd3d::$differ = 0; if ($land === null) { Hd3d::$why = 'none'; return null; } ['x' => $x, 'y' => $y, 'w' => $w, 'h' => $h, 'scale' => $S, 'low' => $low, 'hd' => $hd] = $land; if ($x < 0 || $y < 0 || $x + $w > $fw || $y + $h > $fh) { Hd3d::$why = 'outside'; return null; } $key = -1; $out = $hd; $hw = $w * $S; $differ = 0; $limit = (int) ($w * $h * 0.25); for ($r = 0; $r < $h; $r++) { $frameRow = substr($pixels, ($y + $r) * $fw + $x, $w); $lowRow = substr($low, $r * $w, $w); if ($frameRow === $lowRow) { continue; } if ($key < 0) { // a colour the HD land does not use marks the pixels the VM drew over it $used = count_chars($hd, 1); for ($k = 255; $k >= 0 && isset($used[$k]); $k--) { } if ($k < 0) { Hd3d::$why = 'nokey'; return null; } $key = $k; } $kc = chr($key); for ($i = 0; $i < $w; $i++) { if ($frameRow[$i] !== $lowRow[$i]) { // the screen no longer shows this land (another view, a film, a conversation drawn over it): the // game's own picture, not an HD land that would come and go from one frame to the next if (++$differ > $limit) { Hd3d::$why = 'differ'; Hd3d::$differ = $differ; return null; } for ($k = 0; $k < $S; $k++) { $at = ($r * $S + $k) * $hw + $i * $S; for ($j = 0; $j < $S; $j++) { $out[$at + $j] = $kc; // in place } } } } } Hd3d::$why = 'ok'; Hd3d::$differ = $differ; return [$x, $y, $w, $h, $S, $out, $key];}/** * sys_render(): the pending HD pass of a slow computer (hd3d_pass()), once the land has stayed the same for * Hd3d::STILL: made from the state taken when the land was drawn, the VM's state of now given back after it. */function hd3d_deferred(): void{ $hd = Hd3d::$pending; if ($hd === null || SysVars::$msec / 1000 - Hd3d::$pendingAt < Hd3d::STILL) { return; } Hd3d::$pending = null; $hd['deferred'] = true; hd3d_pass($hd);}