// Regression: elevated-highway deck footprint-terrain ordering on a CLEAN slope (deck_terrain_pertile fix). // // A 2×2 elevated highway deck (0x61-0x6b) lays its own footprint terrain (the leveled cube caps + slope // skirts). The original's faithful 2×2 branch draws all four footprint tiles at the deck's ANCHOR diagonal (its // NE/back corner), so the FRONT footprint tiles are painted one diagonal too early — and an external terrain // tile that is genuinely BEHIND them, drawn later at the intermediate diagonal, overpaints the terrace. The // result: a terrace slope/cap rendered IN FRONT of the deck's terrace when it should be behind (and the mirror // on rotation). On flat ground the deck sprite hides it; a clean uniform slope exposes it (worst on the // descending 0x63 side). The fix draws each elevated-deck footprint tile's terrain in normal per-tile order // (so it sorts correctly with its neighbours) and floats the deck on top. // // Numeric check (per verify-tile-rendering — never eyeball iso occlusion): build a clean N→S step ramp, drag a // highway up it, and confirm a 0x63 ramp piece is laid; then, on flat ground, confirm a 2×2 elevated ramp deck // does NOT paint over a flat-highway deck one tile IN FRONT of it (the draw-order bug from a real save). #include "sc2k/render/asset_loader.hpp" #include "sc2k/render/city_renderer.hpp" #include "sc2k/sim/highway_path.hpp" #include "sc2k/sim/terrain_gen.hpp" #include #include #include using namespace sc2k; using namespace sc2k::render; #include "test_harness.hpp" #include "test_paths.hpp" // A clean single-step ramp climbing SOUTH (=> 0x63 ramp pieces, cube cap on the FRONT tile — the worst case): // a low north plain, a one-level step, a high south plain. The generator's slope autotiler shapes the terrain // so it is valid, then a highway is dragged up the step. static City ramp_city() { sc2k::detail::TerrainGen tg; tg.g_wl = 0; for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) { const int lvl = (r <= 30) ? 6 : (r >= 34) ? 8 : 6 + (r - 30) / 2; tg.A[r * 128 + cc] = lvl; tg.FL[r * 128 + cc] = 0; tg.XB[r * 128 + cc] = 0; tg.XT[r * 128 + cc] = 0; } for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) tg.autotile_cell(r, cc); City c; for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) { c.altm.at(r, cc) = std::uint16_t(tg.A[r * 128 + cc] & 0x1f); c.xter.at(r, cc) = tg.XT[r * 128 + cc]; } sim::commit_highway_path(c, sim::solve_highway_path(c, 26, 40, 38, 40)); return c; } int main() { if (!have_render_assets()) { std::printf("highway_deck_terrain: SKIP (no extracted assets — run extract_assets.py)\n"); return 0; } const Assets a = load_assets(); CityRenderer renderer(a.bank, a.palette); const City H = ramp_city(); CHECK(H.xbld.at(31, 41) == 0x63); // a 0x63 ramp piece was actually laid (cube on the front tile) const Camera cam{2100, 200}; auto eq = [](Rgba p, Rgba q) { return p.r == q.r && p.g == q.g && p.b == q.b; }; // ── Deck DRAW-ORDER: a ramp deck must NOT paint over a flat-highway deck IN FRONT of it ────────────── // (the "slope piece appears on top of the flat one leading up to it" bug, from a real save: a 2×2 ramp // block's front footprint tile shares a screen diagonal with a 1×1 flat-highway tile one step in front, // at a lower row, so it is drawn FIRST — flushing the deck at its front tile then overpaints it. Flushing // at the ANCHOR diagonal instead keeps the flat in front.) Build it on FLAT ground (no terrace terrain to // muddy the comparison): a 2×2 elevated deck with a flat 0x49 directly in front. The flat tile's pixels // must survive identically whether or not the ramp is present — i.e. the ramp never covers it. { City J; // flat sea-level terrain, XTER 0 everywhere for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) J.xbld.at(40 + dr, 51 - dc) = 0x62; // 2×2 ramp deck J.xbld.at(40, 52) = 0x49; J.xbld.at(41, 52) = 0x49; // flat-highway straight one tile IN FRONT City Jflat = J; // same, minus the ramp deck for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) Jflat.xbld.at(40 + dr, 51 - dc) = 0; City Jbare = Jflat; Jbare.xbld.at(40, 52) = 0; Jbare.xbld.at(41, 52) = 0; // no highway at all Framebuffer fJ(4200, 2600), fF(4200, 2600), fB(4200, 2600); renderer.render(J, fJ, cam); // ramp + flat renderer.render(Jflat, fF, cam); // flat only renderer.render(Jbare, fB, cam); // bare terrain // The FLAT-ROAD pixels are exactly where the flat-only render differs from bare terrain. Of those, count // how many the ramp+flat render changed — i.e. the ramp painting OVER the flat deck road (the bug). long flatRoad = 0, covered = 0; for (int y = 0; y < 2600; ++y) for (int x = 0; x < 4200; ++x) { if (eq(fF.at(x, y), fB.at(x, y))) continue; // not a flat-road pixel ++flatRoad; if (!eq(fJ.at(x, y), fF.at(x, y))) ++covered; // the ramp changed it → covered the flat road } std::printf(" deck-order: flat-road px=%ld, covered by the ramp deck=%ld\n", flatRoad, covered); CHECK(flatRoad > 200); // the flat deck really is there (test is meaningful) CHECK(covered < 15); // ...and the ramp does NOT paint over it (the bug) // (The matching "no wedge" guarantee — the deck's front-most footprint tile, flushed at the anchor // diagonal, must be drawn UNDER the deck so its raised cube cap can't wedge over the road — is enforced // by is_deck_front in city_renderer.hpp and was verified on the real save tiles (106,45)/(107,36); it has // no clean synthetic pixel test because the cube legitimately shows through the deck sprite's gaps.) } if (failures == 0) std::printf("highway_deck_terrain: ALL CHECKS PASSED\n"); else std::printf("highway_deck_terrain: %d CHECK(S) FAILED\n", failures); return failures ? 1 : 0; }