PL
games/alis / src / Alis / fn / rrq3d_dos.php · commit 9c722cf
<?phpnamespace App\Alis;use App\Alis\Rrq3dVars as V;// Port of alis/src/render/rrq3d_dos.c (MIT, Olivier Huguenot, Vadim Kindl).// Robinson's Requiem 3D terrain renderer, DOS (little-endian, 8-bit VGA) back end.// The shared globals of rrq3d.c (fprectop, adresa, ...) are Rrq3dVars (aliased V here).// The C DRAW_LINE_* macros of bartra_dos() are the rrq_line_*() helpers below.// Hot loops write Alis::$mem through a PHP reference (in place). See fn/rrq3d.php and docs/porting.md.// ---------------------------------------------------------------------------------------------// bartra_dos() scanline macros (C: DRAW_LINE_SOLID_LTR / DRAW_LINE_SOLID_RTL / DRAW_LINE_XP_LTR)//   vm   = v16 & v_mask (u16), h = hbase16 (u16), count = u8 pixel count,//   texel index = (vm & 0xff00) | ((u8)vm | ((h >> 8) & hmask)) == vm | ((h >> 8) & hmask)//   dark = alis.ptrdark + dark_base (address of the 256-byte darkness page)// ---------------------------------------------------------------------------------------------function rrq_line_solid_ltr(int $dst, int $vm, int $h, int $count, int $hstep, int $hmask, int $tex_data, int $dark): void{    $m = &Alis::$mem;    for ($c = $count & 0xff; $c > 0; $c--) {        $m[$dst++] = $m[$dark + \ord($m[$tex_data + ($vm | (($h >> 8) & $hmask))])];        $h = ($h + $hstep) & 0xffff;    }}// dst points one past the last pixel (asm); pre-decrementfunction rrq_line_solid_rtl(int $dst, int $vm, int $h, int $count, int $hstep, int $hmask, int $tex_data, int $dark): void{    $m = &Alis::$mem;    for ($c = $count & 0xff; $c > 0; $c--) {        $m[--$dst] = $m[$dark + \ord($m[$tex_data + ($vm | (($h >> 8) & $hmask))])];        $h = ($h - $hstep) & 0xffff;    }}// transparent (skip texel == 0), LTRfunction rrq_line_xp_ltr(int $dst, int $vm, int $h, int $count, int $hstep, int $hmask, int $tex_data, int $dark): void{    $m = &Alis::$mem;    for ($c = $count & 0xff; $c > 0; $c--, $dst++) {        $texel = \ord($m[$tex_data + ($vm | (($h >> 8) & $hmask))]);        if ($texel) {            $m[$dst] = $m[$dark + $texel];        }        $h = ($h + $hstep) & 0xffff;    }}/** atalias[] slope lookup shared by bartra/tbarland: index already reduced to u16. */function rrq_alias(int $idx): int{    return xread16(Img::$atalias + ($idx & 0xffff) * 2);}// ---------------------------------------------------------------------------------------------// rrq3d_dos.c// ---------------------------------------------------------------------------------------------function vgatofen_dos(): void{    Img::$switchgo = 1;    $w = (Img::$clipx2 - Img::$clipx1) + 1;    $src = Img::$wlogic + (Img::$clipx1 + (Img::$clipy1 - Img::$wlogy1) * Img::$wloglarg);    Img::$vgamodulo = s16(Img::$wloglarg - $w);    Img::$bitmodulo = s16(Img::$loglarg * 2 - $w);    $tgt = Img::$logic + Img::$clipx1 + Img::$clipy1 * Img::$loglarg * 2;    $rows = Img::$clipy2 - Img::$clipy1 + 1;    if ($rows > 0 && $w > 0) {        if (Img::$vgamodulo == 0 && Img::$bitmodulo == 0) {            // contiguous rows on both sides: one block copy            mem_move($tgt, $src, $w * $rows);        } else {            $ts = $w + Img::$bitmodulo;            $ss = $w + Img::$vgamodulo;            for ($y = 0; $y < $rows; $y++, $tgt += $ts, $src += $ss) {                rrq_copy($tgt, $src, $w);            }        }    }    dirty_add(Img::$clipx1, Img::$clipy1, Img::$clipx2 - Img::$clipx1 + 1, Img::$clipy2 - Img::$clipy1 + 1);}function clrvga_dos(): void{    // DOS: fill with 0 using 4-byte writes, width = (fenx2-fenx1)+1    $width = ((Img::$fenx2 - Img::$fenx1) + 1) & 0xffff;    if (Img::$fenx1 + $width > Img::$wloglarg) {        $width = (Img::$wloglarg - Img::$fenx1) & 0xffff;    }    $rows = s16(Img::$feny2 - Img::$feny1);    $w4 = $width >> 2;    $modulo = (Img::$wloglarg - ($w4 << 2)) & 0xffff;    // Rows are relative to the land buffer: rebase by wlogy1 like vgatofen_dos, the atlpix    // table and clrvga_68k (else the clear overruns the openland allocation).    $ptr = Img::$wlogic + Img::$fenx1 + ((Img::$feny1 - Img::$wlogy1) & 0xffff) * Img::$wloglarg;    $n = $w4 << 2;    if ($rows < 0 || $n == 0) {        return;    }    if ($modulo == 0) {        // contiguous rows: one block clear        mem_set($ptr, 0, ($rows + 1) * $n);        return;    }    for ($y = $rows; $y >= 0; $y--) {        rrq_fill($ptr, 0, $n);        $ptr += $n + $modulo;    }}function calclan0_dos(int $scene_addr, int $render_context): void{    $rc = $render_context;    $grid_shift = xread16($rc - 0x3c0) & 0x3f;    // Compute signed axis deltas and their absolute values    Img::$signedx = s16(xread16($rc - 0x378) - xread16($rc - 0x37e));    $abs_dx = Img::$signedx < 0 ? s16(-Img::$signedx) : Img::$signedx;    Img::$signedy = s16(xread16($rc - 0x376) - xread16($rc - 0x37c));    $abs_dy = Img::$signedy < 0 ? s16(-Img::$signedy) : Img::$signedy;    // Determine dominant axis and compute fixed-point stepping    if ($abs_dx < $abs_dy) {        // Y-dominant: step along Y, interpolate X        xwrite16($rc - 0x2b0, $abs_dy);        $minor_span = (xread16($rc - 0x36a) - xread16($rc - 0x370)) & 0xffff;        if ($minor_span & 0x8000) {            $minor_span = (-$minor_span) & 0xffff;        }        xwrite16($rc - 0x2ac, $minor_span);        $step_num = s16(xread16($rc - 0x378) - xread16($rc - 0x37e));        $frac = s16(cdiv(shl32(cmod($step_num, $abs_dy) & 0xffff, 0x10) >> 1, $abs_dy));        xwrite32($rc - 0x2c4, s32(shl32(cdiv($step_num, $abs_dy) & 0xffff, 16) + $frac * 2));        $dir_sign = Img::$signedy < 0 ? -0x10000 : 0x10000;        xwrite32($rc - 0x2c0, $dir_sign);        $cross_corr = s16((s16(xread16($rc - 0x37c) - xread16($rc - 0x370)) * s16(xread32($rc - 0x2c4) >> 2)) >> 0xe);        if (xread32($rc - 0x2c0) < 0) {            $cross_corr = s16(-$cross_corr);        }        $strip_span = ((xread16($rc - 0x372) + $cross_corr) - xread16($rc - 0x37e)) & 0xffff;        if ($strip_span & 0x8000) {            $strip_span = (-$strip_span) & 0xffff;        }        xwrite16($rc - 0x24e, s16($strip_span) >> $grid_shift);    } else {        // X-dominant: step along X, interpolate Y        xwrite16($rc - 0x2b0, $abs_dx);        $minor_span = (xread16($rc - 0x36c) - xread16($rc - 0x372)) & 0xffff;        if ($minor_span & 0x8000) {            $minor_span = (-$minor_span) & 0xffff;        }        xwrite16($rc - 0x2ac, $minor_span);        $step_num = s16(xread16($rc - 0x376) - xread16($rc - 0x37c));        $frac = s16(cdiv(shl32(cmod($step_num, $abs_dx) & 0xffff, 0x10) >> 1, $abs_dx));        xwrite32($rc - 0x2c0, s32(shl32(cdiv($step_num, $abs_dx) & 0xffff, 16) + $frac * 2));        $dir_sign = Img::$signedx < 0 ? -0x10000 : 0x10000;        xwrite32($rc - 0x2c4, $dir_sign);        $cross_corr = s16((s16(xread16($rc - 0x37e) - xread16($rc - 0x372)) * s16(xread32($rc - 0x2c0) >> 2)) >> 0xe);        if (xread32($rc - 0x2c4) < 0) {            $cross_corr = s16(-$cross_corr);        }        $strip_span = ((xread16($rc - 0x370) + $cross_corr) - xread16($rc - 0x37c)) & 0xffff;        if ($strip_span & 0x8000) {            $strip_span = (-$strip_span) & 0xffff;        }        xwrite16($rc - 0x24e, s16($strip_span) >> $grid_shift);    }    // Compute per-strip grid stepping (16.16 fixed-point)    $strip_count = xread16($rc - 0x24e);    $delta_x = s16(xread16($rc - 0x372) - xread16($rc - 0x37e));    $frac_x = s16(cdiv(shl32(cmod($delta_x, $strip_count) & 0xffff, 0x10) >> 1, $strip_count));    xwrite32($rc - 0x2bc, s32(shl32(cdiv($delta_x, $strip_count) & 0xffff, 16) + $frac_x * 2) >> $grid_shift);    $delta_y = s16(xread16($rc - 0x370) - xread16($rc - 0x37c));    $frac_y = s16(cdiv(shl32(cmod($delta_y, $strip_count) & 0xffff, 0x10) >> 1, $strip_count));    xwrite32($rc - 0x2b8, s32(shl32(cdiv($delta_y, $strip_count) & 0xffff, 16) + $frac_y * 2) >> $grid_shift);    // Direction signs for grid stepping    xwrite32($rc - 0x2a0, xread32($rc - 700) < 0 ? -0x10000 : 0x10000);    xwrite32($rc - 0x29c, xread32($rc - 0x2b8) < 0 ? -0x10000 : 0x10000);    // DOS: dimension scaling delta — full 32-bit quotient (no s16 truncation)    xwrite32($rc - 0x2b4, shl32(cdiv((xread16($rc - 0x2ac) - xread16($rc - 0x2b0)) << 8, xread16($rc - 0x24e)), 8) >> $grid_shift);    // Fog/palette interpolation setup    xwrite32($rc - 0x252, 0);    xwrite32($rc - 0x256, shl32(xread16($scene_addr + 0x9e) & 0xffff, 0x10));    if (xread16($scene_addr + 0x98) != 0) {        xwrite32($rc - 0x256, 0x60000);        $fog_level = xread16($scene_addr + 0x9e) & 0xffff;        if (s16($fog_level) < 7) {            xwrite32($rc - 0x256, 0);        }        if (-1 < xread16($scene_addr + 0x98)) {            xwrite32($rc - 0x256, s32(shl32(xread16($scene_addr + 0xa0) & 0xffff, 16) - 1));        }        // DOS: full 32-bit fog stepping delta (no s16 truncation)        xwrite32($rc - 0x252, shl32(cdiv(s32(shl32($fog_level, 16) - xread32($rc - 0x256)) >> 8, s16(xread16($rc - 0x24e) + 1)), 8));    }    // Per-strip interpolation deltas and initial values — DOS: full 32-bit quotients    $strip_count = xread16($rc - 0x24e);    xwrite32($rc - 0x2e4, cdiv(s32(xread32($rc - 0x318) - xread32($rc - 0x330)), $strip_count));    xwrite32($rc - 0x2dc, shl32(cdiv(mul32(s32(xread32($rc - 0x2f0) - xread32($rc - 0x2fc)), 0x100), $strip_count), 8));    xwrite32($rc - 0x2d4, cdiv(s32(xread32($rc - 0x310) - xread32($rc - 0x328)), $strip_count));    xwrite32($rc - 0x2cc, shl32(cdiv(mul32(s32(xread32($rc - 0x2ec) - xread32($rc - 0x2f8)), 0x100), $strip_count), 8));    xwrite32($rc - 0x2e8, xread32($rc - 0x330));    xwrite32($rc - 0x2e0, shl32(xread32($rc - 0x32c), 0x10));    xwrite32($rc - 0x2d8, xread32($rc - 0x328));    xwrite32($rc - 0x2d0, xread32($rc - 0x2f8));}// Draw a textured scanline for DOS 8-bit terrain bars// direction: 1 = left-to-right, -1 = right-to-left// DOS ASM uses OR-based texture addressing://   texel = tex_data[(v_coord & v_mask) | ((h_coord >> 8) & tex_width)]function bartra_dos_line(int $dst, int $start_col, int $end_col, int $v_coord, int $shift, int $tex_width, int $tex_height, int $subtile, int $tex_data, int $dark_page, int $direction): void{    if ($subtile != 0) {        $v_mask = (($tex_height - $subtile) << $shift) & 0xffff;        $tex_base = $tex_data + ($subtile << $shift);    } else {        $v_mask = ($tex_height << $shift) & 0xffff;        $tex_base = $tex_data;    }    $v_masked = $v_coord & $v_mask;    $dark = Alis::$ptrdark + (($dark_page & 0xffff) << 8);    $hstep = Img::$tex_persp_step;    $m = &Alis::$mem;    if ($direction < 0) {        // Right-to-left: ASM adds 0x2000 to tex_hbase, then iterates with h_coord -= tex_hstep        $h_coord = (Img::$tex_hbase + 0x2000) & 0xffff;        for ($col = $end_col; $col >= $start_col; $col--) {            $texel = \ord($m[$tex_base + ($v_masked | (($h_coord >> 8) & $tex_width))]);            if ($subtile == 0 || $texel != 0) {                $m[$dst + $col] = $m[$dark + $texel];            }            $h_coord = ($h_coord - $hstep) & 0xffff;        }    } else {        // Left-to-right: h_coord starts from tex_hbase directly        $h_coord = Img::$tex_hbase & 0xffff;        for ($col = $start_col; $col <= $end_col; $col++) {            $texel = \ord($m[$tex_base + ($v_masked | (($h_coord >> 8) & $tex_width))]);            if ($subtile == 0 || $texel != 0) {                $m[$dst + $col] = $m[$dark + $texel];            }            $h_coord = ($h_coord + $hstep) & 0xffff;        }    }}// DOS bartra: 16-bit DX accumulator, per-pixel DH mask, local tex_hbase.function bartra_dos(int $terrain_cell, int $render_context, int $drawy, int $index, int $barwidth, int $barheight, int $bary): void{    $__m = &Alis::$mem; // xread16 / xwrite16 / s16 written out (the HD pass: millions of calls)    if ($barheight <= 0 || $barwidth <= 0) {        return;    }    // --- Prologue ---    $_precx = Img::$precx;    $stride = Img::$wloglarg & 0xffff;    $width_byte = $barwidth & 0xff;    $tex_hstep = Img::$tex_persp_step & 0xffff;    $shift = ord($__m[$render_context + $index]);    $tex_vstep = ((($tex_hstep >> ((8 - $shift) & 0x1f)) & 0xffff) + 1) & 0xffff;    $sh = $shift & 0x1f;    // Work on a LOCAL copy (DX in asm is a working accumulator, not a global that persists)    $hbase0 = Img::$tex_hbase & 0xffff;    $slope_clip = (((ord($__m[$__a = ($render_context - 0x246)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000); // unaff_BP    $sVar9 = (((($drawy) & 0xffff) ^ 0x8000) - 0x8000);    $slope_height_s = (((($slope_clip - $sVar9) & 0xffff) ^ 0x8000) - 0x8000);    $slope_height = $slope_height_s & 0xffff;    $slope_rows = $slope_height_s & 0xff;    // --- Load texture from type entry ---    $tex_ptr_addr = ((($__t = (ord($__m[$__b = ($render_context + $index + 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);    if ($tex_ptr_addr == 0) {        return;    }    $tex_offset = ((($__t = (ord($__m[$__b = ($tex_ptr_addr)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);    $tex_width_mask = ord($__m[$tex_ptr_addr + $tex_offset + 2]);    $tex_height_raw = (((ord($__m[$__a = ($tex_ptr_addr + $tex_offset + 4)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $subtile = (((ord($__m[$__a = ($render_context + $index + 2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff;    $tex_base = s32($tex_ptr_addr + $tex_offset);    // --- Darkness ---    $cell = (((ord($__m[$__a = ($terrain_cell)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff;    // 8-bit dark page    $dark_page = ((((((Img::$vdarkw >> 8) & 0xff) - ((($cell >> 8) & 0xff) >> 6) * 2) & 0xffff) ^ 0x8000) - 0x8000);    if ($dark_page & 0x80) { // (s16)((u16)dark_page << 8) < 0        $dark_page = 0;    }    $dark = Alis::$ptrdark + (($dark_page & 0xff) << 8);    // --- Shared working vars ---    $v_coord = 0;    $tex_data = 0;    $v_mask = 0;    $remaining = $barheight;    $atlpix = Img::$atlpix;    $wlogy1 = Img::$wlogy1;    // --- Top part selection ---    if ($slope_height_s == 0 || $slope_clip < $sVar9) {        // No slope        $v_mask = ((($tex_height_raw - $subtile) & 0xffff) << $sh) & 0xffff;        $tex_data = $tex_base + ($subtile << $sh) + 8;        $v_coord = 0;        $remaining = $barheight;    } elseif ($subtile == 0) {        // --- No-subtile slope ---        $remaining = (((($barheight - (((($slope_height) & 0xffff) ^ 0x8000) - 0x8000)) & 0xffff) ^ 0x8000) - 0x8000);        if ($barheight < (((($slope_height) & 0xffff) ^ 0x8000) - 0x8000)) {            $slope_rows = (s8($slope_rows) + s8($remaining)) & 0xff;        }        // atalias lookup        $slope_dist = ($slope_clip - $bary) & 0xffff;        if ($slope_dist < 0x41) {            $slope_step = rrq_alias((($slope_dist - 1) * Hd3d::aliasWidth() + ($barwidth & 0xffff)) - 1);        } else {            $slope_dist >>= 2;            if (0x40 < $slope_dist) {                $slope_dist = 0x40;            }            $slope_step = ((((rrq_alias((($slope_dist - 1) * Hd3d::aliasWidth() + (($barwidth & 0xffff) >> 1)) - 1) >> 1) & 0xffff) ^ 0x8000) - 0x8000);        }        $col_width = 1;        $v_coord = 0;        if ($bary < $sVar9) {            $v_coord = mul32(($sVar9 - $bary) & 0xffff, $tex_vstep) & 0xffff;            $col_width = (((mul32(($sVar9 - $bary) & 0xffff, $slope_step & 0xffff) & 0xffff) >> 8) + 1) & 0xff;        }        $v_mask = (($tex_height_raw & 0xffff) << $sh) & 0xffff;        $tex_data = $tex_base + 8;        // direction check        $screen_ptr = ((($__t = (ord($__m[$__b = ($atlpix + (((($sVar9 - $wlogy1) & 0xffff) ^ 0x8000) - 0x8000) * 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $_precx;        $col_frac = ($col_width << 8) & 0xffff;        $rows = $slope_rows;        if ($slope_clip == (((ord($__m[$__a = ($render_context - 0x25c)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) {            // LTR slope (solid)            do {                $__pB = $v_coord & $v_mask; $__pD = $col_frac >> 8; for ($__c = ($__pD) & 0xff, $__d = $screen_ptr, $__vm = $__pB, $__h = $hbase0; $__c > 0; $__c--) { $__m[$__d++] = $__m[$dark + ord($__m[$tex_data + ($__vm | (($__h >> 8) & $tex_width_mask))])]; $__h = ($__h + $tex_hstep) & 0xffff; }                $v_coord = ($v_coord + $tex_vstep) & 0xffff;                $col_frac = ($col_frac + $slope_step) & 0xffff;                $screen_ptr += $stride;                $rows = ($rows - 1) & 0xff;            } while ($rows != 0);        } else {            // RTL slope (solid) - uses +0x2000 hbase tweak locally            $hbase = ($hbase0 + 0x2000) & 0xffff;            $screen_ptr += $width_byte;            do {                $__pB = $v_coord & $v_mask; $__pD = $col_frac >> 8; for ($__c = ($__pD) & 0xff, $__d = $screen_ptr, $__vm = $__pB, $__h = $hbase; $__c > 0; $__c--) { $__m[--$__d] = $__m[$dark + ord($__m[$tex_data + ($__vm | (($__h >> 8) & $tex_width_mask))])]; $__h = ($__h - $tex_hstep) & 0xffff; }                $v_coord = ($v_coord + $tex_vstep) & 0xffff;                $col_frac = ($col_frac + $slope_step) & 0xffff;                $screen_ptr += $stride;                $rows = ($rows - 1) & 0xff;            } while ($rows != 0);        }        if ($remaining < 1) {            return;        }    } else {        // --- Subtile slope ---        $remaining = (((($barheight - (((($slope_height) & 0xffff) ^ 0x8000) - 0x8000)) & 0xffff) ^ 0x8000) - 0x8000);        if ($barheight < (((($slope_height) & 0xffff) ^ 0x8000) - 0x8000)) {            $slope_height = ($slope_height + $remaining) & 0xffff;        }        $slope_h = $slope_height;        if ($slope_clip == (((($bary - V::$bartra_saved_si) & 0xffff) ^ 0x8000) - 0x8000)) {            // PATH A: flat subtile (transparent), LTR            $v_coord = 0;            if ($bary < $sVar9) {                $v_coord = mul32(($sVar9 - $bary) & 0xffff, $tex_vstep) & 0xffff;            }            $v_mask = ((($tex_height_raw - $subtile) & 0xffff) << $sh) & 0xffff;            $subtile_off = $subtile << $sh;            $tex_data = $tex_base + 8;            $screen_ptr = ((($__t = (ord($__m[$__b = ($atlpix + (((($sVar9 - $wlogy1) & 0xffff) ^ 0x8000) - 0x8000) * 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $_precx;            $sub_rows = $slope_h & 0xff;            do {                $__pB = $v_coord & $v_mask; for ($__c = ($width_byte) & 0xff, $__d = $screen_ptr, $__vm = $__pB, $__h = $hbase0; $__c > 0; $__c--, $__d++) { if ($__x = ord($__m[$tex_data + ($__vm | (($__h >> 8) & $tex_width_mask))])) { $__m[$__d] = $__m[$dark + $__x]; } $__h = ($__h + $tex_hstep) & 0xffff; }                $v_coord = ($v_coord + $tex_vstep) & 0xffff;                $screen_ptr += $stride;                $sub_rows = ($sub_rows - 1) & 0xff;            } while ($sub_rows != 0);            if ($remaining < 1) {                return;            }            $tex_data += $subtile_off;            $v_coord = 0;            // NOTE: v_mask stays at (tex_height_raw - subtile) << shift        } else {            // PATH B: perspective subtile            //   slope_dist = slope_clip - ground_base_y (without extra height)            $slope_dist = (($slope_clip - $bary) + V::$bartra_saved_si) & 0xffff;            if ($slope_dist < 0x41) {                $slope_step = rrq_alias(($slope_dist + (($barwidth & 0xffff) - 1) * Hd3d::aliasWidth()) - 1);            } else {                $slope_dist >>= 2;                if (0x40 < $slope_dist) {                    $slope_dist = 0x40;                }                $slope_step = ((((rrq_alias(($slope_dist + (((($barwidth - 1) & 0xffff) * Hd3d::aliasWidth()) >> 1)) - 1) >> 1) & 0xffff) ^ 0x8000) - 0x8000);            }            $slope_step_rows = (($slope_step & 0xffff) >> 8) & 0xff;            $scanline_delta = $slope_step_rows * ((((Img::$wloglarg) & 0xffff) ^ 0x8000) - 0x8000);            $v_start = 0;            if ($bary < $sVar9) {                $v_start = mul32(($sVar9 - $bary) & 0xffff, $tex_vstep) & 0xffff;            }            $v_mask = ((($tex_height_raw - $subtile) & 0xffff) << $sh) & 0xffff;            $subtile_off = $subtile << $sh;            $tex_data = $tex_base + 8;            $drawy_idx = (((($sVar9 - $wlogy1) & 0xffff) ^ 0x8000) - 0x8000);            $end_ptr = ((($__t = (ord($__m[$__b = ($atlpix + ((((($slope_h & 0xff) + $drawy_idx) & 0xffff) ^ 0x8000) - 0x8000) * 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $_precx;            $aH = ($slope_step & 0xff) << 8;   // addv/subv = ((u8)slope_step << 24) | tex_hstep, as 16-bit halves            $aL = $tex_hstep;            $m = &Alis::$mem;            // Direction check            if ($slope_clip == (((ord($__m[$__a = ($render_context - 0x25c)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) {                // LTR perspective (transparent)                $start_ptr = ((($__t = (ord($__m[$__b = ($atlpix + $drawy_idx * 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $_precx;                $hH = 0;            // h_accum = (u32)hbase0                $hL = $hbase0;                $col_count = $width_byte;                do {                    // Mask h byte before accumulation                    $h_masked = ($hL >> 8) & $tex_width_mask;                    $sL = ($h_masked << 8) | ($hL & 0xff);   // h_saved low word (high word = hH)                    $cur = $start_ptr;                    $vc = $v_start;                    while ($cur < $end_ptr) {                        $texel = $m[$tex_data + (($vc & $v_mask) | $h_masked)];                        if ($texel !== "\0") {                            $m[$cur] = $texel;                        }                        $vc = ($vc + $tex_vstep) & 0xffff;                        $cur += $stride;                    }                    // Advance h_accum with carry                    $lo = $sL + $aL;                    $hi = $hH + $aH + ($lo >> 16);                    if ($hi >> 16) {                        $start_ptr += $stride;                    }                    if ($end_ptr <= $start_ptr + $scanline_delta) {                        break;                    }                    $start_ptr += $scanline_delta + 1;                    $hH = $hi & 0xffff;                    $hL = $lo & 0xffff;                    $col_count = ($col_count - 1) & 0xff;                } while ($col_count != 0);            } else {                // RTL perspective (transparent)                $start_ptr = ((($__t = (ord($__m[$__b = ($atlpix + $drawy_idx * 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $_precx + $width_byte;                $hH = 0;                $hL = ($hbase0 + 0x2000) & 0xffff;                $col_count = $width_byte;                do {                    $h_masked = ($hL >> 8) & $tex_width_mask;                    $sL = ($h_masked << 8) | ($hL & 0xff);                    $cur = $start_ptr;                    $vc = $v_start;                    while ($cur < $end_ptr) {                        $texel = $m[$tex_data + (($vc & $v_mask) | $h_masked)];                        if ($texel !== "\0") {                            $m[$cur] = $texel;                        }                        $vc = ($vc + $tex_vstep) & 0xffff;                        $cur += $stride;                    }                    // new_h = h_saved - subv; borrow when h_saved < subv                    $lo = $sL - $aL;                    $hi = $hH - $aH - ($lo < 0 ? 1 : 0);                    if ($hi < 0) {                        $start_ptr += $stride;                    }                    if ($end_ptr <= $start_ptr + $scanline_delta) {                        break;                    }                    $start_ptr += $scanline_delta - 1;                    $hH = $hi & 0xffff;                    $hL = $lo & 0xffff;                    $col_count = ($col_count - 1) & 0xff;                } while ($col_count != 0);            }            unset($m);            if ($remaining < 1) {                return;            }            $tex_data += $subtile_off;            $v_coord = 0;            // NOTE: v_mask stays at (tex_height_raw - subtile) << shift        }    }    // --- Mid/flat section (solid, LTR), matches LAB_0000df53 family ---    $mid_rows = (((($remaining - Img::$vbarbot) & 0xffff) ^ 0x8000) - 0x8000);    if ($mid_rows > 0) {        $mid_drawy = (((($sVar9 + ($barheight - $remaining)) & 0xffff) ^ 0x8000) - 0x8000);        $screen_ptr = ((($__t = (ord($__m[$__b = ($atlpix + (((($mid_drawy - $wlogy1) & 0xffff) ^ 0x8000) - 0x8000) * 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $_precx;        $mr = $mid_rows & 0xff;        do {            $__pB = $v_coord & $v_mask; for ($__c = ($width_byte) & 0xff, $__d = $screen_ptr, $__vm = $__pB, $__h = $hbase0; $__c > 0; $__c--) { $__m[$__d++] = $__m[$dark + ord($__m[$tex_data + ($__vm | (($__h >> 8) & $tex_width_mask))])]; $__h = ($__h + $tex_hstep) & 0xffff; }            $v_coord = ($v_coord + $tex_vstep) & 0xffff;            $screen_ptr += $stride;            $mr = ($mr - 1) & 0xff;        } while ($mr != 0);    }    // --- Bottom section ---    if (Img::$vbarbot > 0) {        $bot_dist = (V::$botalt - V::$bothigh) & 0xffff;        if ($bot_dist != 0 && V::$bothigh <= V::$botalt) {            $bot_width_val = $width_byte;            if ($bot_dist < 0x41) {                $bot_slope_step = rrq_alias((($bot_dist - 1) * Hd3d::aliasWidth() + $bot_width_val) - 1);            } else {                $bot_dist >>= 2;                if (0x40 < $bot_dist) {                    $bot_dist = 0x40;                }                $bot_slope_step = ((((rrq_alias((($bot_dist - 1) * Hd3d::aliasWidth() + ($bot_width_val >> 1)) - 1) >> 1) & 0xffff) ^ 0x8000) - 0x8000);            }            $bot_rows = Img::$vbarbot & 0xff;            $bot_v_coord = 0;            $bot_tex_ptr = ((($__t = (ord($__m[$__b = ($render_context + V::$bottom_type_index + 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);            if ($bot_tex_ptr != 0) {                $bot_tex_off = ((($__t = (ord($__m[$__b = ($bot_tex_ptr)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);                $bot_tex_width_mask = ord($__m[$bot_tex_ptr + $bot_tex_off + 2]);                $bot_shift = ord($__m[$render_context + V::$bottom_type_index]);                $bot_v_mask = ((((ord($__m[$__a = ($bot_tex_ptr + $bot_tex_off + 4)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) << ($bot_shift & 0x1f)) & 0xffff;                $bot_tex_data = $bot_tex_ptr + $bot_tex_off + 8;                $bot_dark = (((Img::$vdarkw >> 8) & 0xff) - ((($cell >> 8) & 0xff) >> 6) * 2) & 0xff;                if ($bot_dark & 0x80) {                    $bot_dark = 0;                }                $bot_dark_addr = Alis::$ptrdark + ($bot_dark << 8);                $bot_screen = ((($__t = (ord($__m[$__b = ($atlpix + ((((V::$bothigh - $wlogy1) & 0xffff) ^ 0x8000) - 0x8000) * 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $_precx;                // col_frac starts at width in high byte, 0xFF low byte                $bot_col_frac = (($width_byte << 8) | 0xff) & 0xffff;                if (V::$botalt == V::$precboti) {                    // RTL bottom (solid)                    $bot_screen += $width_byte;                    $hbase = ($hbase0 + 0x2000) & 0xffff;                    do {                        $__pB = $bot_v_coord & $bot_v_mask; $__pD = $bot_col_frac >> 8; for ($__c = ($__pD) & 0xff, $__d = $bot_screen, $__vm = $__pB, $__h = $hbase; $__c > 0; $__c--) { $__m[--$__d] = $__m[$bot_dark_addr + ord($__m[$bot_tex_data + ($__vm | (($__h >> 8) & $bot_tex_width_mask))])]; $__h = ($__h - $tex_hstep) & 0xffff; }                        $bot_v_coord = ($bot_v_coord + $tex_vstep) & 0xffff;                        $bot_col_frac = ($bot_col_frac - $bot_slope_step) & 0xffff;                        $bot_screen += $stride;                        $bot_rows = ($bot_rows - 1) & 0xff;                    } while ($bot_rows != 0);                } else {                    // LTR bottom (solid)                    do {                        $__pB = $bot_v_coord & $bot_v_mask; $__pD = $bot_col_frac >> 8; for ($__c = ($__pD) & 0xff, $__d = $bot_screen, $__vm = $__pB, $__h = $hbase0; $__c > 0; $__c--) { $__m[$__d++] = $__m[$bot_dark_addr + ord($__m[$bot_tex_data + ($__vm | (($__h >> 8) & $bot_tex_width_mask))])]; $__h = ($__h + $tex_hstep) & 0xffff; }                        $bot_v_coord = ($bot_v_coord + $tex_vstep) & 0xffff;                        $bot_col_frac = ($bot_col_frac - $bot_slope_step) & 0xffff;                        $bot_screen += $stride;                        $bot_rows = ($bot_rows - 1) & 0xff;                    } while ($bot_rows != 0);                }            }        }    }}/** Shared Y clipping of barland_dos/tbarland_dos; false when the bar is entirely clipped. */function rrq_bar_clip_y(int &$drawy, int &$barheight): bool{    $sVar4 = s16(($drawy - Img::$landclipy1) - Img::$landcliph);    if ((Img::$landcliph <= (($drawy - Img::$landclipy1) & 0xffff) && $sVar4 != 0) || (s16(-$barheight - $sVar4) < 0)) {        if (Img::$clipy2 < $drawy) {            return false;        }        $sVar4 = s16($barheight + $drawy - 1);        if ($sVar4 < Img::$landclipy1) {            return false;        }        if ($drawy < Img::$landclipy1) {            $barheight = s16(($barheight - Img::$landclipy1) + $drawy);            $drawy = Img::$landclipy1;        }        if (Img::$clipy2 < $sVar4) {            $barheight = s16(($barheight - $sVar4) + Img::$clipy2);        }    }    return true;}/** Shared ftstpix hit-test of barland_dos/tbarland_dos (after the clip against rc-0x246). */function rrq_bar_hittest(int $terrain_cell, int $render_context, int $step_x, int $step_y, int $drawy, int $barheight, int $vbarlarg, int $packed_coord): void{    if (($drawy <= Img::$ytstpix) && (Img::$ytstpix < s16($drawy + $barheight)) && (Img::$precx <= Img::$xtstpix) && (Img::$xtstpix < s16(Img::$precx + s16($vbarlarg)))) {        // Terrain cell reads (DOS LE: byte0=height, byte1=type)        $cell = xread16($terrain_cell);        Img::$ntstpix = s16(($cell >> 8) & 0xffff);        Img::$cztstpix = $cell & 0xff;        // GS:[0x840] = zoom shift        $bVar3 = xread16($render_context - 0x3c0) & 0x1f;        Img::$cxtstpix = (((s16($packed_coord >> 16) * 2 - $step_x) << $bVar3) & 0xffff) >> 1;        Img::$cytstpix = (((s16($packed_coord) * 2 - $step_y) << $bVar3) & 0xffff) >> 1;        Img::$etstpix = -2; // 0xfffe        // GS:[0x922] = depth/altitude        Img::$dtstpix = xread32hi16($render_context - 0x2e0);    }}// DOS barland: rewritten from Ghidra decompiled rrq-dos.c barland() function.//   bary = initial screen Y, barheight, barx = screen X of bar right edge,//   index = type entry offset into render_context; bartra_saved_si set by doland_dos.function barland_dos(int $terrain_cell, int $render_context, int $step_x, int $step_y, int $bary, int $barheight, int $index, int $barx, int $packed_coord, int $d6): void{    $__m = &Alis::$mem; // xread16 / xwrite16 / s16 written out (the HD pass: millions of calls)    // === Y clipping ===    $drawy = $bary;    if (!rrq_bar_clip_y($drawy, $barheight)) {        return;    }    // === X clipping ===    if (Img::$clipx1 <= $barx) {        $vbarlarg = ($barx - Img::$precx) & 0xffff;        if ($vbarlarg == 0 || $barx < Img::$precx || $vbarlarg >= 0x80) {            return;        }        Img::$vbarx = Img::$precx;        Img::$vbarlarg = $vbarlarg;        // sVar4 = dynamic clip Y from doland_dos        $sVar4 = (((ord($__m[$__a = ($render_context - 0x246)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);        Img::$vbarbot = 0;        // GS:[0x9b6] >= 0 (visibility check)        if (-1 < (((ord($__m[$__a = ($render_context - 0x24e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) {            if (Img::$ftstpix == 0) {                // Normal rendering: GS:[DI+8] texture check                if (ord($__m[$render_context + 8 + $index]) != 0) {                    bartra_dos($terrain_cell, $render_context, $drawy, $index, $vbarlarg, $barheight, $bary);                    return;                }                barlands($drawy, $barheight, $vbarlarg);                return;            }            // === ftstpix hit-test path ===            if ($drawy < $sVar4) {                if ((((($drawy + $barheight) & 0xffff) ^ 0x8000) - 0x8000) <= $sVar4) {                    Img::$vbarbot = 0;                    return;                }                $barheight = ((((($barheight + $sVar4) - $drawy) & 0xffff) ^ 0x8000) - 0x8000);                $drawy = $sVar4;            }            rrq_bar_hittest($terrain_cell, $render_context, $step_x, $step_y, $drawy, $barheight, $vbarlarg, $packed_coord);        }    }}// tbarland_dos: DOS-faithful overhang/ceiling bar renderer (Ghidra tbarland() of rrq-dos.c).//   1. Inline bottom surface rendering when bothigh <= drawy (narrowing textured slope)//   2. DOS-simple X clipping (barx - precx, no vbarclipx2)//   3. bothigh clamped against rc-0x246function tbarland_dos(int $terrain_cell, int $render_context, int $step_x, int $step_y, int $bary, int $barheight, int $index, int $barx, int $packed_coord, int $d6): void{    $__m = &Alis::$mem; // xread16 / xwrite16 / s16 written out (the HD pass: millions of calls)    // ASM: MOV [DAT_0001ab5e], SI where SI=0    V::$bartra_saved_si = 0;    // === Y clipping ===    $drawy = $bary;    if (!rrq_bar_clip_y($drawy, $barheight)) {        return;    }    // === X clipping ===    if (Img::$clipx1 > $barx) {        return;    }    $vbarlarg = ($barx - Img::$precx) & 0xffff;    if ($vbarlarg == 0 || $barx < Img::$precx || $vbarlarg >= 0x80) {        return;    }    Img::$vbarx = Img::$precx;    Img::$vbarlarg = $vbarlarg;    // === Visibility check ===    if ((((ord($__m[$__a = ($render_context - 0x24e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) < 0) {        return;    }    // === Clip boundary = dynamic clip Y from doland_dos ===    $sVar_clip = (((ord($__m[$__a = ($render_context - 0x246)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    if (((((V::$bothigh) & 0xffff) ^ 0x8000) - 0x8000) < $sVar_clip) {        V::$bothigh = $sVar_clip & 0xffff;    }    Img::$vbarbot = 0;    // === fbottom handling ===    if (V::$fbottom != 0) {        V::$bottom_type_index = $index;        if (((((V::$bothigh) & 0xffff) ^ 0x8000) - 0x8000) <= $drawy) {            // === INLINE BOTTOM SURFACE RENDERING ===            // Renders a narrowing textured slope for the overhang bottom surface.            $_precx = Img::$precx;            $stride = Img::$wloglarg & 0xffff;            $shift = ord($__m[$render_context + $index]);            $tex_hstep = Img::$tex_persp_step & 0xffff;            $tex_vstep = (($tex_hstep >> ((8 - $shift) & 0x1f)) + 1) & 0xffff;            // Early exit: barheight <= 0            if ($barheight < 1) {                Img::$vbarbot = 0;                return;            }            // Distance for atalias lookup            $distance = (V::$botalt - V::$bothigh) & 0xffff;            if ($distance == 0 || V::$botalt < ((((V::$bothigh) & 0xffff) ^ 0x8000) - 0x8000)) {                Img::$vbarbot = 0;                return;            }            // Atalias lookup for slope narrowing step            // Use image.vbarlarg (full unclipped width) for correct slope geometry            $slope_width_t = (Img::$vbarlarg > 0 ? Img::$vbarlarg : $vbarlarg) & 0xffff;            if ($distance < 0x41) {                $slope_step = rrq_alias((($distance - 1) * Hd3d::aliasWidth() + $slope_width_t) - 1);            } else {                $distance >>= 2;                if (0x40 < $distance) {                    $distance = 0x40;                }                $slope_step = ((((rrq_alias((($distance - 1) * Hd3d::aliasWidth() + ($slope_width_t >> 1)) - 1) >> 1) & 0xffff) ^ 0x8000) - 0x8000);            }            // Initialize v_coord and col_width            $v_coord = 0;            $col_width = $vbarlarg & 0xff;            if (((((V::$bothigh) & 0xffff) ^ 0x8000) - 0x8000) < $drawy) {                $pre_rows = ($drawy - ((((V::$bothigh) & 0xffff) ^ 0x8000) - 0x8000)) & 0xffff;                $v_coord = ($pre_rows * $tex_vstep) & 0xffff;                $col_width = ($col_width - (((($pre_rows * ($slope_step & 0xffff)) & 0xffff) >> 8) & 0xff)) & 0xff;            }            // Load texture from type entry            $tex_ptr_addr = ((($__t = (ord($__m[$__b = ($render_context + $index + 4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);            if ($tex_ptr_addr == 0) {                return;            }            $tex_offset = ((($__t = (ord($__m[$__b = ($tex_ptr_addr)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);            $tex_width = ord($__m[$tex_ptr_addr + $tex_offset + 2]);            $tex_height_raw = (((ord($__m[$__a = ($tex_ptr_addr + $tex_offset + 4)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff;            $tex_data = $tex_ptr_addr + $tex_offset + 8;            // Compute darkness: fog_byte - 2 * (type_byte >> 6), clamped >= 0            $fog = ((Img::$vdarkw & 0xffff) >> 8) & 0xff;            $type_dark = ((ord($__m[$terrain_cell + 1]) >> 6) * 2) & 0xff;            $dark_page = $fog - $type_dark;            if ($dark_page < 0) {                $dark_page = 0;            }            // Row counter: low byte of barheight            $row_count = $barheight & 0xff;            // Col fraction: (col_width << 8) | 0xFF            $col_frac = (($col_width << 8) | 0xff) & 0xffff;            // Screen pointer for first row            $screen_base = ((($__t = (ord($__m[$__b = (Img::$atlpix + ((((($drawy - Img::$wlogy1) * 4) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $_precx;            // Direction check            if (V::$botalt == V::$precboti) {                // === RTL path: bar narrows from left side ===                do {                    $cols = ($col_frac >> 8) & 0xff;                    if ($cols > 0) {                        $start_col = ($vbarlarg & 0xff) - $cols;                        $end_col = ($vbarlarg & 0xff) - 1;                        bartra_dos_line($screen_base, $start_col, $end_col, $v_coord, $shift, $tex_width, $tex_height_raw, 0, $tex_data, $dark_page, -1);                    }                    $v_coord = ($v_coord + $tex_vstep) & 0xffff;                    $col_frac = ($col_frac - $slope_step) & 0xffff;                    $screen_base += $stride;                    $row_count = ($row_count - 1) & 0xff;                } while ($row_count != 0);            } else {                // === LTR path: bar narrows from right side ===                do {                    $cols = ($col_frac >> 8) & 0xff;                    if ($cols > 0) {                        bartra_dos_line($screen_base, 0, $cols - 1, $v_coord, $shift, $tex_width, $tex_height_raw, 0, $tex_data, $dark_page, 1);                    }                    $v_coord = ($v_coord + $tex_vstep) & 0xffff;                    $col_frac = ($col_frac - $slope_step) & 0xffff;                    $screen_base += $stride;                    $row_count = ($row_count - 1) & 0xff;                } while ($row_count != 0);            }            return;        }        // Not entirely below clip: compute vbarbot        $clip_calc = ((((($barheight + $drawy) - ((((V::$bothigh) & 0xffff) ^ 0x8000) - 0x8000)) & 0xffff) ^ 0x8000) - 0x8000);        if ($clip_calc != 0 && ((((V::$bothigh) & 0xffff) ^ 0x8000) - 0x8000) <= (((($barheight + $drawy) & 0xffff) ^ 0x8000) - 0x8000)) {            Img::$vbarbot = $clip_calc;        }    }    // === Normal dispatch ===    if (Img::$ftstpix == 0) {        if (ord($__m[$render_context + 8 + $index]) != 0) {            bartra_dos($terrain_cell, $render_context, $drawy, $index, $vbarlarg, $barheight, $bary);        } else {            barlands($drawy, $barheight, $vbarlarg);        }        return;    }    // === ftstpix hit-test path ===    if ($drawy < $sVar_clip) {        if ((((($drawy + $barheight) & 0xffff) ^ 0x8000) - 0x8000) <= $sVar_clip) {            return;        }        $barheight = ((((($barheight + $sVar_clip) - $drawy) & 0xffff) ^ 0x8000) - 0x8000);        $drawy = $sVar_clip;    }    rrq_bar_hittest($terrain_cell, $render_context, $step_x, $step_y, $drawy, $barheight, $vbarlarg, $packed_coord);}// =============================================================================// doland_dos: DOS-specific terrain column renderer// (Ghidra-decompiled program flow of rrq-dos.c:18388-18948)//   - tex_hbase computed per row from integer grid coords//   - Perspective shifts use <<8/<<8//   - 179-row altitude table (0xB3 segments)//   - Calls barland_dos()/tbarland_dos()// Port: the render-context words that doland never writes (grid bounds, wrap flag, shift, ...) are// read once into locals; the per-row ones once per row.// =============================================================================function doland_dos(int $scene_addr, int $render_context): void{    $__m = &Alis::$mem; // xread16 / xwrite16 / s16 written out (the HD pass: millions of calls)    $rc = $render_context;    // =========================================================================    // INITIALIZATION    // =========================================================================    // --- Sprite depth sorting setup ---    Img::$spritprof = 0x8000;    $sprite = sprite_var((((ord($__m[$__a = ($scene_addr + 0x2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff);    Img::$spritnext = $sprite->link;    if (Img::$spritnext != 0) {        Img::$spritprof = sprite_var(Img::$spritnext)->newd;    }    // --- Render sky background ---    if ((((ord($__m[$__a = ($scene_addr + 0xa2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) != 0) {        Hd3d::$inPass ? hd3d_sky($scene_addr, $rc) : skytofen($scene_addr, $rc);    }    // --- Terrain data pointers ---    $alt_table = Img::$atalti;    $dos_alt_delta = 0; // 32-bit altitude segment delta    $terrain_grid = Img::$atlland;    // --- Reset overhang interpolation state ---    V::$fprectop = 0;    V::$fprectopa = 0;    V::$adresa = 0;    V::$prectopa = 1000;    V::$prectopb = 1000;    V::$prectopc = 1000;    V::$precbota = 1000;    V::$precbotb = 1000;    V::$precbotc = 1000;    // --- Initialize rendering accumulators ---    $__wa = $rc - 0x25a; $__wv = 0; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);    $__wa = $rc - 0x27c; $__wv = 0; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);    $cam_grid_x = (((ord($__m[$__a = ($rc - 0x3a2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff;    $cam_grid_y = (((ord($__m[$__a = ($rc - 0x3a0)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff;    $__wa = $rc - 0x2a8; $__wv = shl32($cam_grid_x, 0x10); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);    $__wa = $rc - 0x2a4; $__wv = shl32($cam_grid_y, 0x10); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);    // Column step direction in terrain grid (16.16 fixed-point)    // Word-swapped form used for 68k ADDX carry-chain emulation    $col_step_x_raw = ((($__t = (ord($__m[$__b = ($rc - 0x2c4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);    $col_step_x = ((((($__s = ($col_step_x_raw)) & 0xffff) ^ 0x8000) - 0x8000) << 16 | (($__s >> 16) & 0xffff));    $col_step_y_raw = ((($__t = (ord($__m[$__b = ($rc - 0x2c0)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);    $col_step_y = ((((($__s = ($col_step_y_raw)) & 0xffff) ^ 0x8000) - 0x8000) << 16 | (($__s >> 16) & 0xffff));    // Read-only render-context values for the whole call    $cell_shift = (((ord($__m[$__a = ($rc - 0x3c0)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0x3f;    $x294 = (((ord($__m[$__a = ($rc - 0x294)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $u294 = $x294 & 0xffff;    $x292 = (((ord($__m[$__a = ($rc - 0x292)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $u292 = $x292 & 0xffff;    $wrap = ord($__m[$rc - 0x3fe]) == 1;    $x360 = (((ord($__m[$__a = ($rc - 0x360)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x35e = (((ord($__m[$__a = ($rc - 0x35e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x378 = (((ord($__m[$__a = ($rc - 0x378)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x37e = (((ord($__m[$__a = ($rc - 0x37e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x376 = (((ord($__m[$__a = ($rc - 0x376)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x37c = (((ord($__m[$__a = ($rc - 0x37c)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x26c = (((ord($__m[$__a = ($rc - 0x26c)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x26a = (((ord($__m[$__a = ($rc - 0x26a)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x3a4 = (((ord($__m[$__a = ($rc - 0x3a4)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $x3a8 = (((ord($__m[$__a = ($rc - 0x3a8)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);    $col_dir_x = (((($col_step_x_raw >> 0x10) & 0xffff) ^ 0x8000) - 0x8000);    $col_dir_y = (((($col_step_y_raw >> 0x10) & 0xffff) ^ 0x8000) - 0x8000);    // Initial screen Y projection    $__wa = $rc - 0x270; $__wv = ($x26a + (((ord($__m[$__a = ($rc - 0x352)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) & 0xffff; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);    // --- Initial perspective projection ---    $proj_x = ((((($cam_grid_x << $cell_shift) - $x360) & 0xffff) ^ 0x8000) - 0x8000);    $proj_y = ((((($cam_grid_y << $cell_shift) - $x35e) & 0xffff) ^ 0x8000) - 0x8000);    glandtopix($rc, $proj_x, $proj_y, ((((($x378 + $proj_x) - $x37e) & 0xffff) ^ 0x8000) - 0x8000), ((((($x376 + $proj_y) - $x37c) & 0xffff) ^ 0x8000) - 0x8000), (((ord($__m[$__a = ($rc - 0x2e0) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000));    $__wa = $rc - 0x280; $__wv = shl32(($x26c + $proj_x) & 0xffff, 0x10); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);    // DOS: <<8 / <<8 perspective (not <<6 / <<10 like m68k)    $__wa = $rc - 0x27c; $__wv = ((((cdiv((((($proj_y - $proj_x) & 0xffff) ^ 0x8000) - 0x8000) << 8, $x3a4)) & 0xffff) ^ 0x8000) - 0x8000) << 8; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);    // Current traversal position in terrain grid (16.16 fixed-point)    $trav_x = shl32($cam_grid_x, 16);    $trav_y = shl32($cam_grid_y, 16);    // DOS: initialize tex_hbase once before outer loop    // It accumulates +0x2000 per column through the entire doland() call.    Img::$tex_hbase = (((((($trav_x & 0xffff) + ($trav_y & 0xffff)) << 4) & 0xffff) ^ 0x8000) - 0x8000);    // =========================================================================    // OUTER LOOP: Process terrain rows from far to near    // =========================================================================    do {        // ----- Advance row position accumulators -----        $__wa = $rc - 0x2a8; $__wv = s32(((($__t = (ord($__m[$__b = ($rc - 0x2bc)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + ((($__t = (ord($__m[$__b = ($rc - 0x2a8)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t)); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        $__wa = $rc - 0x2a4; $__wv = s32(((($__t = (ord($__m[$__b = ($rc - 0x2b8)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + ((($__t = (ord($__m[$__b = ($rc - 0x2a4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t)); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        $row_start_x = $trav_x;        $row_start_y = s32($trav_y + shl32((((ord($__m[$__a = ($rc - 0x29c) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000), 16));        // ----- Snap column position to terrain grid alignment -----        // Safety bound: break after 512 iterations to prevent infinite loops.        $hi2a8 = (((ord($__m[$__a = ($rc - 0x2a8) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);        $hi2a4 = (((ord($__m[$__a = ($rc - 0x2a4) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);        if ($col_step_x_raw == 0x00010000) {            if ((((ord($__m[$__a = ($rc - 0x2bc) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) < 0) {                for ($_snap = 512; $_snap > 0 && $hi2a8 < (((($row_start_x >> 16) & 0xffff) ^ 0x8000) - 0x8000); $_snap--) {                    $row_start_y = s32($row_start_y - $col_step_y_raw);                    $row_start_x = s32($row_start_x - $col_step_x_raw);                }            } else {                for ($_snap = 512; $_snap > 0 && (((($row_start_x >> 16) & 0xffff) ^ 0x8000) - 0x8000) < $hi2a8; $_snap--) {                    $row_start_y = s32($row_start_y + $col_step_y_raw);                    $row_start_x = s32($row_start_x + $col_step_x_raw);                }            }        } elseif ($col_step_x_raw == -0x10000) { // 0xffff0000            if ((((ord($__m[$__a = ($rc - 0x2bc) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) < 0) {                for ($_snap = 512; $_snap > 0 && $hi2a8 < (((($row_start_x >> 16) & 0xffff) ^ 0x8000) - 0x8000); $_snap--) {                    $row_start_y = s32($row_start_y + $col_step_y_raw);                    $row_start_x = s32($row_start_x + $col_step_x_raw);                }            } else {                for ($_snap = 512; $_snap > 0 && (((($row_start_x >> 16) & 0xffff) ^ 0x8000) - 0x8000) < $hi2a8; $_snap--) {                    $row_start_y = s32($row_start_y - $col_step_y_raw);                    $row_start_x = s32($row_start_x - $col_step_x_raw);                }            }        } else {            // Column step is primarily in Y direction            $row_start_y = $trav_y;            $row_start_x = s32($trav_x + shl32((((ord($__m[$__a = ($rc - 0x2a0) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000), 16));            if ($col_step_y_raw == 0x00010000) {                if ((((ord($__m[$__a = ($rc - 0x2b8) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) < 0) {                    for ($_snap = 512; $_snap > 0 && $hi2a4 < (((($row_start_y >> 16) & 0xffff) ^ 0x8000) - 0x8000); $_snap--) {                        $row_start_y = s32($row_start_y - $col_step_y_raw);                        $row_start_x = s32($row_start_x - $col_step_x_raw);                    }                } else {                    for ($_snap = 512; $_snap > 0 && (((($row_start_y >> 16) & 0xffff) ^ 0x8000) - 0x8000) < $hi2a4; $_snap--) {                        $row_start_y = s32($row_start_y + $col_step_y_raw);                        $row_start_x = s32($row_start_x + $col_step_x_raw);                    }                }            } else {                if ((((ord($__m[$__a = ($rc - 0x2b8) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) < 0) {                    for ($_snap = 512; $_snap > 0 && $hi2a4 < (((($row_start_y >> 16) & 0xffff) ^ 0x8000) - 0x8000); $_snap--) {                        $row_start_y = s32($row_start_y + $col_step_y_raw);                        $row_start_x = s32($row_start_x + $col_step_x_raw);                    }                } else {                    for ($_snap = 512; $_snap > 0 && (((($row_start_y >> 16) & 0xffff) ^ 0x8000) - 0x8000) < $hi2a4; $_snap--) {                        $row_start_y = s32($row_start_y - $col_step_y_raw);                        $row_start_x = s32($row_start_x - $col_step_x_raw);                    }                }            }        }        // ----- Update fog/distance shading -----        $__wa = $rc - 0x256; $__wv = s32(((($__t = (ord($__m[$__b = ($rc - 0x252)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + ((($__t = (ord($__m[$__b = ($rc - 0x256)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t)); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        Img::$vdarkw = ((((((((($__t = (ord($__m[$__b = ($rc - 0x256)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) >> 16) & 0xff) << 8) & 0xffff) ^ 0x8000) - 0x8000);        // ----- Update depth (distance from camera) -----        $depth = s32(((($__t = (ord($__m[$__b = ($rc - 0x2dc)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + ((($__t = (ord($__m[$__b = ($rc - 0x2e0)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t));        $__wa = $rc - 0x2e0; $__wv = $depth; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        if ($depth < 0) {            $__wa = $rc - 0x2e0; $__wv = s32(((($__t = (ord($__m[$__b = ($rc - 0x2e0)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) - ((($__t = (ord($__m[$__b = ($rc - 0x2dc)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t)); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        }        // ----- Update vertical height projection -----        $__wa = $rc - 0x2d8; $__wv = s32(((($__t = (ord($__m[$__b = ($rc - 0x2d4)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + ((($__t = (ord($__m[$__b = ($rc - 0x2d8)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t)); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        // Prepare column rendering: word-swap for 68k fixed-point carry emulation        $raw_step = ((($__t = (ord($__m[$__b = ($rc - 0x27c)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);        $col_x_step_swp = ((((($__s = ($raw_step)) & 0xffff) ^ 0x8000) - 0x8000) << 16 | (($__s >> 16) & 0xffff));        $col_x_pos_swp = ((((($__s = (((($__t = (ord($__m[$__b = ($rc - 0x280)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t))) & 0xffff) ^ 0x8000) - 0x8000) << 16 | (($__s >> 16) & 0xffff));        // DOS: tex_hstep computation (ceiling division)        // ROL(step, 24) extracts middle bytes for divisor        $divisor = ($raw_step >> 8) & 0xffff;        if ($divisor > 0) {            $quotient = \intdiv(0x200000, $divisor);            if (0x200000 % $divisor != 0) {                $quotient++; // ceiling            }            Img::$tex_persp_step = $quotient & 0xffff;        } else {            Img::$tex_persp_step = 0;        }        $__wa = $rc - 0x278; $__wv = ((($__t = (ord($__m[$__b = ($rc - 0x270)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        // ----- Recalculate perspective if row is in front of camera -----        if (-1 < (((ord($__m[$__a = ($rc - 0x24e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) {            $proj_x = ((((((((($row_start_x >> 16) & 0xffff) ^ 0x8000) - 0x8000) << $cell_shift) - $x360) & 0xffff) ^ 0x8000) - 0x8000);            $proj_y = ((((((((($row_start_y >> 16) & 0xffff) ^ 0x8000) - 0x8000) << $cell_shift) - $x35e) & 0xffff) ^ 0x8000) - 0x8000);            glandtopix($rc, $proj_x, $proj_y, ((((($x378 + $proj_x) - $x37e) & 0xffff) ^ 0x8000) - 0x8000), ((((($x376 + $proj_y) - $x37c) & 0xffff) ^ 0x8000) - 0x8000), (((ord($__m[$__a = ($rc - 0x2e0) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000));            $__wa = $rc - 0x280; $__wv = shl32(($x26c + $proj_x) & 0xffff, 0x10); $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);            // DOS: <<8 / <<8 perspective            $__wa = $rc - 0x27c; $__wv = ((((cdiv((((($proj_y - $proj_x) & 0xffff) ^ 0x8000) - 0x8000) << 8, $x3a4)) & 0xffff) ^ 0x8000) - 0x8000) << 8; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);            $hi2e0 = (((ord($__m[$__a = ($rc - 0x2e0) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);            $proj_denom = ($x3a8 + $hi2e0) & 0xffff;            // x86 JLE: a + b <= 0 (signed, with overflow) → 1            if ($proj_denom == 0 || scarry2($x3a8, $hi2e0) != (($proj_denom & 0x8000) ? 1 : 0)) {                $proj_denom = 1;            }            $__wa = $rc - 0x270; $__wv = ($x26a + ((((cdiv(((($__t = (ord($__m[$__b = ($rc - 0x2d8)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t), (((($proj_denom) & 0xffff) ^ 0x8000) - 0x8000))) & 0xffff) ^ 0x8000) - 0x8000)) & 0xffff; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        }        // ----- Render sprites at this depth layer -----        $hi2e0 = (((ord($__m[$__a = ($rc - 0x2e0) + 2]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);        if ($hi2e0 <= Img::$spritprof) {            spritaff($hi2e0);        }        // ----- Select altitude table segment for current distance -----        $alt_table += $dos_alt_delta;        $alt_seg_idx = cdiv(s32(((($__t = (ord($__m[$__b = ($rc - 0x2e0)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) - ((($__t = (ord($__m[$__b = ($rc - 0x2c8)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t)) >> 8, (((ord($__m[$__a = ($rc - 0x262)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) & 0xffff;        // DOS uses 179 segments (0xB3), 512 bytes per segment        $max_seg = Rrq3dConst::DOS_ALT_ROWS;        if ((((ord($__m[$__a = ($rc - 0x24e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) == 1) {            $alt_seg_idx = $max_seg;        }        if ($max_seg < $alt_seg_idx) {            $alt_seg_idx = ($alt_seg_idx & 0x8000) ? 0 : $max_seg;        }        // 32-bit delta to avoid s16 overflow (179 segs * 0x200 = 91648 > 32767)        $dos_alt_delta = (Img::$atalti + $alt_seg_idx * 0x200) - $alt_table;        $alt_next = $alt_table + $dos_alt_delta; // segment used by the pre-scan and the overhangs        // ----- Reset per-row rendering state -----        Img::$precx = ((((Img::$clipx1 - 0x10) & 0xffff) ^ 0x8000) - 0x8000);        $__wa = $rc - 0x260; $__wv = Img::$clipy2; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);        $__wa = $rc - 0x25c; $__wv = Img::$clipy2; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);        V::$fprectop = 0;        V::$fprectopa = 0;        // ----- Decrement row counter -----        $rows_remaining = ((((ord($__m[$__a = ($rc - 0x24e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) - 1) & 0xffff;        $__wa = $rc - 0x24e; $__wv = $rows_remaining; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);        if (($rows_remaining & 0x8000) && ((((ord($__m[$__a = ($rc - 0x24e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) < -3)) {            spritaff(-1);            return;        }        // =====================================================================        // INNER LOOP: Process columns left-to-right within this row        // =====================================================================        $saved_col_x = ((($__t = (ord($__m[$__b = ($rc - 0x280)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);        $scan_x = ((((($__s = ($row_start_x)) & 0xffff) ^ 0x8000) - 0x8000) << 16 | (($__s >> 16) & 0xffff)); // concat22(row_start_x, row_start_x >> 16)        $scan_y = ((((($__s = ($row_start_y)) & 0xffff) ^ 0x8000) - 0x8000) << 16 | (($__s >> 16) & 0xffff));        // per-row constants        $c27c = ((($__t = (ord($__m[$__b = ($rc - 0x27c)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);        $c280 = $saved_col_x;        $lo270 = (((ord($__m[$__a = ($rc - 0x270)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);        $lo278 = (((ord($__m[$__a = ($rc - 0x278)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);        do {            $max_y = (((ord($__m[$__a = ($rc - 0x260)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);            $prev_max_y = (((ord($__m[$__a = ($rc - 0x25e)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);            if ($prev_max_y < $max_y) {                $prev_max_y = $max_y;            }            $bar_screen_x = (((($col_x_pos_swp) & 0xffff) ^ 0x8000) - 0x8000);            $col_target_x = (((($c280 >> 16) & 0xffff) ^ 0x8000) - 0x8000);            // ----- Pre-scan: step through grid cells between columns -----            while ($col_target_x < $bar_screen_x) {                $__wa = $rc - 0x25e; $__wv = $max_y; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                $prescan_height = 0;                // Advance scan position by one column step (with carry)                $__xa = $col_step_x; $__xb = $scan_x; $__lo = ($__xa & 0xffff) + ($__xb & 0xffff); $__hi = (($__xa >> 16) & 0xffff) + (($__xb >> 16) & 0xffff) + ($__lo >> 16); $scan_x = (((($__hi & 0xffff) ^ 0x8000) - 0x8000) << 16) | (($__lo + ($__hi >> 16)) & 0xffff);                $next_scan_x = $scan_x & 0xffff;                $__xa = $col_step_y; $__xb = $scan_y; $__lo = ($__xa & 0xffff) + ($__xb & 0xffff); $__hi = (($__xa >> 16) & 0xffff) + (($__xb >> 16) & 0xffff) + ($__lo >> 16); $scan_y = (((($__hi & 0xffff) ^ 0x8000) - 0x8000) << 16) | (($__lo + ($__hi >> 16)) & 0xffff);                $next_scan_y = $scan_y & 0xffff;                $nsy2 = 2 * (((($next_scan_y) & 0xffff) ^ 0x8000) - 0x8000);                $c280 = s32($c27c + $c280);                $__wa = $rc - 0x280; $__wv = $c280; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);                // Look up terrain cell at (next_scan_x, next_scan_y)                if ($u294 < $next_scan_x) {                    if ($wrap) {                        $wrap_x = 0;                        if ($x294 <= (((($next_scan_x) & 0xffff) ^ 0x8000) - 0x8000)) {                            $wrap_x = (((($x294 * 2) & 0xffff) ^ 0x8000) - 0x8000);                        }                        $strip_ptr = ((($__t = (ord($__m[$__b = ($terrain_grid + ((((($wrap_x - $next_scan_x) * 4) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);                        if ($u292 < $next_scan_y) {                            if ((((($u292) & 0xffff) ^ 0x8000) - 0x8000) <= (((($next_scan_y) & 0xffff) ^ 0x8000) - 0x8000)) {                                $strip_ptr += $x292 << 2;                            }                            V::$adresa = s32($strip_ptr - $nsy2);                        } else {                            V::$adresa = s32($strip_ptr + $nsy2);                        }                        $prescan_height = (((ord($__m[$__a = (V::$adresa)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xff;                    }                } else {                    // X in bounds: as the DOS decompile, only the Y-out-of-bounds case is                    // wrap-gated; an in-bounds cell is always sampled.                    if ($u292 < $next_scan_y) {                        if ($wrap) {                            $strip_ptr = ((($__t = (ord($__m[$__b = ($terrain_grid + (((($next_scan_x * 4) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);                            if ((((($u292) & 0xffff) ^ 0x8000) - 0x8000) <= (((($next_scan_y) & 0xffff) ^ 0x8000) - 0x8000)) {                                $strip_ptr += $x292 << 2;                            }                            V::$adresa = s32($strip_ptr - $nsy2);                            $prescan_height = (((ord($__m[$__a = (V::$adresa)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xff;                        }                        // wrap off: prescan_height stays 0                    } else {                        $strip_ptr = ((($__t = (ord($__m[$__b = ($terrain_grid + (((($next_scan_x * 4) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);                        V::$adresa = s32($strip_ptr + $nsy2);                        $prescan_height = (((ord($__m[$__a = (V::$adresa)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xff;                    }                }                // Convert height to screen Y and track maximum                $max_y = (((($lo270 + (((ord($__m[$__a = ($alt_next + $prescan_height * 2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) & 0xffff) ^ 0x8000) - 0x8000);                $__wa = $rc - 0x260; $__wv = $max_y; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                if ($prev_max_y < $max_y) {                    $prev_max_y = $max_y;                }                $col_target_x = (((($c280 >> 16) & 0xffff) ^ 0x8000) - 0x8000);            }            // =================================================================            // Main terrain lookup at center column position            // =================================================================            $center_grid_x = ($trav_x >> 16) & 0xffff;            $center_grid_y = ($trav_y >> 16) & 0xffff;            $cgy2 = 2 * (((($center_grid_y) & 0xffff) ^ 0x8000) - 0x8000);            $terrain_wrapped = 0;            // C gotos dos_cell_calc_subtract / dos_cell_calc_add: 1 = subtract, 2 = add, 0 = no cell            $cell_mode = 0;            $strip_ptr_main = 0;            $grid_h = 0;            if ($u294 < $center_grid_x) {                if ($wrap) {                    $terrain_wrapped = 1;                    $wrap_x = 0;                    if ($x294 <= (((($center_grid_x) & 0xffff) ^ 0x8000) - 0x8000)) {                        $wrap_x = (((($x294 * 2) & 0xffff) ^ 0x8000) - 0x8000);                    }                    $strip_ptr_main = ((($__t = (ord($__m[$__b = ($terrain_grid + ((((($wrap_x - $center_grid_x) * 4) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);                    $grid_h = $u292;                    $cell_mode = $grid_h < $center_grid_y ? 1 : 2;                }            } elseif ($u292 < $center_grid_y) {                if ($wrap) {                    $terrain_wrapped = 1;                    $strip_ptr_main = ((($__t = (ord($__m[$__b = ($terrain_grid + (((($center_grid_x << 2) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);                    $grid_h = $u292;                    $cell_mode = 1;                }            } else {                $strip_ptr_main = ((($__t = (ord($__m[$__b = ($terrain_grid + (((($center_grid_x << 2) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t);                $cell_mode = 2;            }            if ($cell_mode != 0) {                if ($cell_mode == 1) {                    // dos_cell_calc_subtract                    if ((((($grid_h) & 0xffff) ^ 0x8000) - 0x8000) <= (((($center_grid_y) & 0xffff) ^ 0x8000) - 0x8000)) {                        $strip_ptr_main += $x292 << 2;                    }                    $terrain_cell = s32($strip_ptr_main - $cgy2);                } else {                    // dos_cell_calc_add                    $terrain_cell = s32($strip_ptr_main + $cgy2);                }                // --- Read terrain cell and compute ground screen Y ---                $ground_clip_y = (((ord($__m[$__a = ($rc - 0x25c)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);                $cell_data = (((ord($__m[$__a = ($terrain_cell)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff;                Img::$solh = $cell_data & 0xff;                Img::$solpixy = (((($lo278 + (((ord($__m[$__a = ($alt_table + ((((Img::$solh * 2) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) & 0xffff) ^ 0x8000) - 0x8000);                $__wa = $rc - 0x246; $__wv = Img::$solpixy; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                $__wa = $rc - 0x25c; $__wv = Img::$solpixy; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                if (Img::$solpixy < $ground_clip_y) {                    $__wa = $rc - 0x246; $__wv = $ground_clip_y; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                    $ground_clip_y = Img::$solpixy;                }                // Extract terrain type index                $terrain_type_idx = (((((($cell_data & 0x3f00) >> 3) - 0xc00) & 0xffff) ^ 0x8000) - 0x8000);                // DOS: set terrain fill color for barlands() gap fill                V::$terrain_fill_color = ord($__m[$rc + 8 + $terrain_type_idx]);                // DOS: per-terrain-type extra height adjustment from altitude table                V::$bartra_saved_si = 0;                $type_extra = (((ord($__m[$__a = ($rc + $terrain_type_idx + 0x2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);                if ($type_extra != 0) {                    V::$bartra_saved_si = (((ord($__m[$__a = ($alt_table + $type_extra * 2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);                    $ground_clip_y = (((($ground_clip_y + V::$bartra_saved_si) & 0xffff) ^ 0x8000) - 0x8000);                }                $bar_height = ($prev_max_y - $ground_clip_y) & 0xffff;                // --- Render ground terrain bar ---                if ($bar_height != 0 && sborrow2($prev_max_y, $ground_clip_y) == (($bar_height & 0x8000) ? 1 : 0)) {                    barland_dos($terrain_cell, $rc, $col_dir_x, $col_dir_y, $ground_clip_y, (((($bar_height) & 0xffff) ^ 0x8000) - 0x8000), $terrain_type_idx, $bar_screen_x, ((((($__s = ($trav_x)) & 0xffff) ^ 0x8000) - 0x8000) << 16 | (($__s >> 16) & 0xffff)), $scan_x);                }                // =============================================================                // Check for terrain features: overhangs or billboard sprites                // =============================================================                $feature = ord($__m[$rc + $terrain_type_idx + 0x14]);                if ($feature != 0) {                    // DOS: skip overhangs for wrapped terrain cells                    if ($terrain_wrapped) {                        goto dos_advance_column;                    }                    if (s8($feature) < 0) {                        // =====================================================                        // OVERHANG / BRIDGE RENDERING                        // =====================================================                        V::$notopa = 1;                        $prev_col_grid_x = (((($scan_x >> 0x10) & 0xffff) ^ 0x8000) - 0x8000);                        $oh_packed = concat22($prev_col_grid_x, V::$prectopa);                        $oh_prev_bot = V::$precbota;                        // --- Check previous column for overhang continuity ---                        if (V::$adresa != 0) {                            $adr_cell = (((ord($__m[$__a = (V::$adresa)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);                            V::$solha = $adr_cell & 0xff;                            $prev_type_idx = (((((($adr_cell & 0x3f00) >> 3) - 0xc00) & 0xffff) ^ 0x8000) - 0x8000);                            if (s8(ord($__m[$rc + 0x14 + $prev_type_idx])) < 0) {                                // Previous column also has overhang: compute transition                                $oh_thickness = (((ord($__m[$__a = ($rc + 0x16 + $prev_type_idx)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);                                $oh_ceil_h = (((ord($__m[$__a = (s32(V::$adresa + ((($__t = (ord($__m[$__b = ($rc + 0x10 + $prev_type_idx)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t)))]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xff;                                $oh_delta_h = ($oh_ceil_h - V::$solha) & 0xffff;                                $oh_rem = ($oh_thickness - $oh_delta_h) & 0xffff;                                if (($oh_delta_h != 0 && sborrow2($oh_ceil_h, V::$solha) == (($oh_delta_h & 0x8000) ? 1 : 0)) && ($oh_rem != 0 && sborrow2($oh_thickness, $oh_delta_h) == (($oh_rem & 0x8000) ? 1 : 0))) {                                    $oh_bottom_h = $oh_ceil_h;                                    if (((((V::$solha + $oh_rem) & 0xffff) ^ 0x8000) - 0x8000) < (((($oh_ceil_h) & 0xffff) ^ 0x8000) - 0x8000)) {                                        $oh_bottom_h = ((V::$solha + $oh_rem) - V::$solha) & 0xffff;                                    }                                    // Convert overhang heights to screen Y                                    $oh_top_scr = (((($lo270 + (((ord($__m[$__a = ($alt_next + $oh_ceil_h * 2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) & 0xffff) ^ 0x8000) - 0x8000);                                    $oh_bot_scr = ($lo270 + (((ord($__m[$__a = ($alt_next + ($oh_bottom_h - $oh_ceil_h) * -2)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) & 0xffff;                                    V::$adresa = 0;                                    if (V::$fprectopa == 0) {                                        // First overhang column in this span                                        V::$fprectopa = 1;                                        V::$prectopc = 5000;                                        V::$precbotc = 0xec78;                                        V::$prectopa = $oh_top_scr & 0xffff;                                        V::$precbota = $oh_bot_scr;                                    } else {                                        // Continuing overhang: update interpolation tracking                                        V::$prectopb = V::$prectopc;                                        V::$precbotb = V::$precbotc;                                        if (V::$prectopa < $oh_top_scr) {                                            $oh_packed = concat22($prev_col_grid_x, $oh_top_scr);                                        }                                        $oh_interp_top = (((($oh_packed) & 0xffff) ^ 0x8000) - 0x8000);                                        if ($oh_interp_top < V::$prectopc) {                                            $oh_packed = concat22($oh_packed >> 0x10, V::$prectopc);                                        }                                        $oh_min_bot = V::$precbota;                                        if ((((($oh_bot_scr) & 0xffff) ^ 0x8000) - 0x8000) < ((((V::$precbota) & 0xffff) ^ 0x8000) - 0x8000)) {                                            $oh_min_bot = $oh_bot_scr;                                        }                                        $oh_prev_bot = $oh_min_bot;                                        if (((((V::$precbotc) & 0xffff) ^ 0x8000) - 0x8000) < (((($oh_min_bot) & 0xffff) ^ 0x8000) - 0x8000)) {                                            $oh_prev_bot = V::$precbotc;                                        }                                        V::$prectopa = $oh_top_scr & 0xffff;                                        V::$precbota = $oh_bot_scr;                                        if ($oh_top_scr > ((((V::$prectopc) & 0xffff) ^ 0x8000) - 0x8000)) {                                            V::$prectopc = $oh_top_scr & 0xffff;                                        }                                        if ((((($oh_bot_scr) & 0xffff) ^ 0x8000) - 0x8000) < ((((V::$precbotc) & 0xffff) ^ 0x8000) - 0x8000)) {                                            V::$precbotc = $oh_bot_scr;                                        }                                        if ((((($oh_packed) & 0xffff) ^ 0x8000) - 0x8000) < (((($oh_prev_bot) & 0xffff) ^ 0x8000) - 0x8000)) {                                            V::$notopa = 0;                                        }                                    }                                    goto dos_overhang_render;                                }                            }                            V::$fprectopa = 0;                        }                        dos_overhang_render:                        // --- Look up overhang terrain cell ---                        $terrain_cell = s32(((($__t = (ord($__m[$__b = ($rc + 0x10 + $terrain_type_idx)]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t) + $cgy2 + ((($__t = (ord($__m[$__b = ($terrain_grid + (((($center_grid_x << 2) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__b + 1]) << 8) | (ord($__m[$__b + 2]) << 16) | (ord($__m[$__b + 3]) << 24))) > 0x7fffffff) ? $__t - 0x100000000 : $__t));                        V::$fbottom = 0;                        $oh_cell_data = (((ord($__m[$__a = ($terrain_cell)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff;                        Img::$toph = $oh_cell_data & 0xff;                        $oh_type_idx = (((((($oh_cell_data & 0x3f00) >> 3) - 0xc00) & 0xffff) ^ 0x8000) - 0x8000);                        // Overhang height relative to ground                        $oh_rel_height = (Img::$toph - Img::$solh) & 0xffff;                        $oh_thickness_avail = (((ord($__m[$__a = ($rc + 0x16 + $terrain_type_idx)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000) & 0xffff;                        $oh_thickness_rem = ($oh_thickness_avail - $oh_rel_height) & 0xffff;                        if (($oh_rel_height != 0 && sborrow2(Img::$toph, Img::$solh) == (($oh_rel_height & 0x8000) ? 1 : 0)) && ($oh_thickness_rem != 0 && sborrow2($oh_thickness_avail, $oh_rel_height) == (($oh_thickness_rem & 0x8000) ? 1 : 0))) {                            V::$fbottom = ((((Img::$solh + $oh_thickness_rem) & 0xffff) ^ 0x8000) - 0x8000) < Img::$toph ? 1 : 0;                            if (V::$fbottom) {                                $oh_rel_height = ((Img::$solh + $oh_thickness_rem) - Img::$solh) & 0xffff;                            }                            $oh_bottom_offset = (((($oh_rel_height - Img::$toph) & 0xffff) ^ 0x8000) - 0x8000);                            $oh_top_pix = ($lo278 + (((ord($__m[$__a = ($alt_table + ((((Img::$toph * 2) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) & 0xffff;                            $oh_clip_y = (((ord($__m[$__a = ($rc - 0x246)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);                            Img::$toppixy = (((($oh_top_pix) & 0xffff) ^ 0x8000) - 0x8000);                            $__wa = $rc - 0x246; $__wv = $oh_top_pix; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                            $prev_oh_top = V::$prectopi;                            V::$prectopi = $oh_top_pix;                            if ((((($oh_top_pix) & 0xffff) ^ 0x8000) - 0x8000) < (((($prev_oh_top) & 0xffff) ^ 0x8000) - 0x8000)) {                                $__wa = $rc - 0x246; $__wv = V::$prectopi; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                                $prev_oh_top = V::$prectopi;                                $oh_top_pix = V::$prectopi;                            }                            V::$prectopi = $oh_top_pix;                            if (V::$fbottom != 0) {                                $oh_clip_y = (((($lo278 + (((ord($__m[$__a = ($alt_table + (((($oh_bottom_offset * -2) & 0xffff) ^ 0x8000) - 0x8000))]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000)) & 0xffff) ^ 0x8000) - 0x8000);                            }                            V::$botalt = V::$precboti;                            V::$bothigh = $oh_clip_y & 0xffff;                            if (V::$precboti < $oh_clip_y) {                                V::$bothigh = V::$precboti;                                V::$botalt = $oh_clip_y & 0xffff;                            }                            V::$precboti = $oh_clip_y & 0xffff;                            // --- Render overhang bar ---                            if (V::$fprectop != 0) {                                $oh_bar_h = (V::$botalt - $prev_oh_top) & 0xffff;                                $oh_bar_height = $oh_bar_h;                                if ($oh_bar_h != 0 && sborrow2(V::$botalt, $prev_oh_top) == (($oh_bar_h & 0x8000) ? 1 : 0)) {                                    if (V::$notopa == 0) {                                        $oh_check_top = (((($oh_packed) & 0xffff) ^ 0x8000) - 0x8000);                                        $oh_delta = ($oh_check_top - $prev_oh_top) & 0xffff;                                        $oh_packed = concat22($oh_packed >> 0x10, $oh_delta);                                        if ($oh_delta == 0 || sborrow2($oh_check_top, $prev_oh_top) != (($oh_delta & 0x8000) ? 1 : 0)) {                                            $oh_new_top = (((($prev_oh_top + $oh_bar_h) & 0xffff) ^ 0x8000) - 0x8000);                                            $oh_adj = ($oh_new_top - $oh_prev_bot) & 0xffff;                                            $oh_bar_height = $oh_adj;                                            $prev_oh_top = $oh_prev_bot;                                            if ($oh_adj == 0 || sborrow2($oh_new_top, $oh_prev_bot) != (($oh_adj & 0x8000) ? 1 : 0)) {                                                goto dos_overhang_done;                                            }                                        } elseif ((((($oh_bar_h) & 0xffff) ^ 0x8000) - 0x8000) <= (((($oh_prev_bot - $prev_oh_top) & 0xffff) ^ 0x8000) - 0x8000)) {                                            V::$fbottom = 0;                                            $oh_bar_height = $oh_delta;                                        }                                    }                                    $saved_clip = (((ord($__m[$__a = ($rc - 0x25c)]) | (ord($__m[$__a + 1]) << 8)) ^ 0x8000) - 0x8000);                                    $__wa = $rc - 0x25c; $__wv = V::$prectopi; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                                    tbarland_dos($terrain_cell, $rc, $col_dir_x, (((($col_step_y) & 0xffff) ^ 0x8000) - 0x8000), (((($prev_oh_top) & 0xffff) ^ 0x8000) - 0x8000), (((($oh_bar_height) & 0xffff) ^ 0x8000) - 0x8000), $oh_type_idx, (((($col_x_pos_swp) & 0xffff) ^ 0x8000) - 0x8000), ((((($__s = ($trav_x)) & 0xffff) ^ 0x8000) - 0x8000) << 16 | (($__s >> 16) & 0xffff)), $oh_packed);                                    $__wa = $rc - 0x25c; $__wv = $saved_clip; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff);                                }                            }                            dos_overhang_done:                            V::$fprectop = 1;                        }                        goto dos_advance_column;                    }                    if (s8($feature) != 1) {                        goto dos_advance_column;                    }                    // --- Billboard sprite at this terrain cell ---                    barsprite($rc, $terrain_type_idx, (((($center_grid_x) & 0xffff) ^ 0x8000) - 0x8000), (((($center_grid_y) & 0xffff) ^ 0x8000) - 0x8000), (((($scan_x) & 0xffff) ^ 0x8000) - 0x8000));                }                V::$fprectop = 0;                V::$fprectopa = 0;            }            dos_advance_column:            Img::$tex_hbase = ((((Img::$tex_hbase + 0x2000) & 0xffff) ^ 0x8000) - 0x8000);            Img::$precx = $bar_screen_x;            if (Img::$landclipx2 < $bar_screen_x) {                break;            }            // Step traversal to next column            $trav_x = s32($trav_x + $col_step_x_raw);            $trav_y = s32($trav_y + $col_step_y_raw);            // Advance screen column X (fixed-point with carry emulation)            $__xa = $col_x_step_swp; $__xb = $col_x_pos_swp; $__lo = ($__xa & 0xffff) + ($__xb & 0xffff); $__hi = (($__xa >> 16) & 0xffff) + (($__xb >> 16) & 0xffff) + ($__lo >> 16); $col_x_pos_swp = (((($__hi & 0xffff) ^ 0x8000) - 0x8000) << 16) | (($__lo + ($__hi >> 16)) & 0xffff);        } while (true);        // Restore row state for next iteration        $__wa = $rc - 0x280; $__wv = $saved_col_x; $__m[$__wa] = \chr($__wv & 0xff); $__m[$__wa + 1] = \chr(($__wv >> 8) & 0xff); $__m[$__wa + 2] = \chr(($__wv >> 16) & 0xff); $__m[$__wa + 3] = \chr(($__wv >> 24) & 0xff);        $trav_x = $row_start_x;        $trav_y = $row_start_y;    } while (true);}function landtofi_dos(int $unused, int $scene_id): void{    $scene_addr = s32(Alis::$basemain + s16($scene_id));    // DOS path: only save/restore clipy1 and cliph. clipx1/clipx2/clipl/fen values    // are NOT modified — they remain from the sprite dirty rect.    $save_cliph = Img::$cliph;    $save_clipy1 = Img::$clipy1;    Img::$wloglarg = Hd3d::stride(); // DOS: hardcoded 320-byte rows (the HD pass: 640, fn/hd3d.php)    Img::$wlogic = xread32(xread32($scene_addr + 0x30));    Img::$wlogx1 = xread16($scene_addr + 0xe);    Img::$landclipx2 = s16(Img::$wlogx1 + xread16($scene_addr + 0x12));    Img::$clipy1 = xread16($scene_addr + 0x10); // override to viewport top    Img::$cliph = s16((Img::$clipy2 - Img::$clipy1) + 1);    Img::$wlogy1 = Img::$clipy1;    Img::$mapscreen = $scene_addr;    Img::$landclipy1 = Img::$clipy1;    Img::$landcliph = Img::$cliph;    // Tautology — always runs (Ghidra: if (fdoland==0 || fdoland!=0))    Img::$fdoland = 0;    if ((s16(xread16($scene_addr + 0x12) + 1) == Img::$clipl) && (((xread16($scene_addr + 0x14) + 1) & 0xffff) == (Img::$cliph & 0xffff))) {        Img::$fdoland = 1;    }    $render_context = s32(xread16($scene_addr + 0x42) + xread32(Alis::$atent + xread16($scene_addr + 0x40)));    // HD edition: this partial redraw of the land (under a sprite that went away: Socrate leaving after a talk) changes    // the land on the screen too, so the HD land is made again here, like after affiland() (fn/hd3d.php)    $hd = Hd3d::wanted($scene_addr) ? hd3d_begin($scene_addr, $render_context, true) : null;    calclan0_dos($scene_addr, $render_context);    if (((xread8($scene_addr + 1) & 0x40) == 0) && (xread16($scene_addr + 0xa2) == 0)) {        clrvga_dos();    }    doland_dos($scene_addr, $render_context);    Img::$fdoland = 0;    // Restore only clipy1 and cliph (matching Ghidra)    Img::$clipy1 = $save_clipy1;    Img::$cliph = $save_cliph;    if ($hd !== null) {        hd3d_low($hd);       // the whole land buffer as it is now (the redrawn part and the rest)    }    vgatofen();    if ($hd !== null) {        hd3d_pass($hd);    }    Img::$wlogic = Img::$logic;    Img::$wlogx1 = Img::$logx1;    Img::$wlogy1 = Img::$logy1;    Img::$wloglarg = Img::$loglarg;}