// Numeric regression for vertical tile placement — the height/rotation fixes ported from the validated // reference compositor (src/sc2k_render.c) and proven by src/cube_check.c. Per feedback-verify-visuals- // numerically we never eyeball these: we drive the real CityRenderer, then for each candidate tile compute // where its sprite SHOULD land straight from the rule (cube top = altitude+1; highway = base 17 + float) — // independently of the renderer's own blit helper — and sample the composited framebuffer at that sprite's // mask to see whether the piece actually drew there. // // • Cube ("rock") lift: a road/network (code<0x6c) on XTER 0x0d sits one level UP on the cube top, else // it sinks behind the rock face and reads as missing. (cube_check.c; sc2k_render.c:152) // • Elevated-highway float: XBLD 0x61-0x6b anchor at base 17 + a downward float (+20 for the 0x62/0x63 // curves, +8 otherwise), not as a generic 2×2 (base 25). (sc2k_render.c:158) // // Structure: (1) a SYNTHETIC clean-room test — one road on one lone cube, no occluders — proves the lift // and its rotation-invariance with a noise-free per-pixel signal, and does an in-process A/B (flip the fix // flag) to prove the test is load-bearing. (2) a REAL city (HILTON) corroborates over hundreds of tiles // (dense road-on-rock + the 0x62/0x63 highway curves) at all four compasses. Skips if no game data. #include "sc2k/io/sc2_file.hpp" #include "sc2k/render/asset_loader.hpp" #include "sc2k/render/city_renderer.hpp" #include "sc2k/render/rotate.hpp" #include "sc2k/sim/building_sizes.hpp" #include #include #include #include using namespace sc2k; using namespace sc2k::render; #include "test_harness.hpp" #include "test_paths.hpp" static const Camera kCam{2100, 420}; static constexpr int kW = 4200, kH = 2600; // Screen altitude of a tile, matching CityRenderer::alt_at: land code (XTER < 0x10) // at its raw ground altitude, water/coast code (>= 0x10) at the per-tile water level. No global-sea clamp. static int alt_at(const City& c, int r, int col) { const std::uint16_t a = c.altm.at(r, col); return (c.xter.at(r, col) < 0x10) ? (int(a) & 0x1F) : ((int(a) >> 5) & 0x1F); } // Fraction of sprite `s`'s opaque pixels that match the composited framebuffer when its TOP-LEFT is placed // at (x0,y0): exactly what blit_sprite wrote if the piece really drew there. Computed from the rule, never // by calling the renderer's projection helper. Returns -1 if nothing of the sprite is on-canvas. static double match_at(const Framebuffer& fb, const Palette& pal, const Sprite& s, int x0, int y0) { long matched = 0, total = 0; for (int yy = 0; yy < s.height; ++yy) for (int xx = 0; xx < s.width; ++xx) { if (!s.mask[std::size_t(yy) * s.width + xx]) continue; const int px = x0 + xx, py = y0 + yy; if (px < 0 || px >= fb.width() || py < 0 || py >= fb.height()) continue; ++total; const Rgba want = pal[s.index[std::size_t(yy) * s.width + xx]]; const Rgba got = fb.at(px, py); if (got.r == want.r && got.g == want.g && got.b == want.b) ++matched; } return total ? double(matched) / double(total) : -1.0; } static double oriented_match_at(const Framebuffer& fb, const Palette& pal, const Sprite& s, int x0, int y0, bool flipped) { long matched = 0, total = 0; for (int yy = 0; yy < s.height; ++yy) for (int xx = 0; xx < s.width; ++xx) { const int source_x = flipped ? s.width - 1 - xx : xx; const std::size_t source = static_cast(yy) * s.width + source_x; if (!s.mask[source]) continue; const int px = x0 + xx, py = y0 + yy; if (px < 0 || px >= fb.width() || py < 0 || py >= fb.height()) continue; ++total; const Rgba want = pal[s.index[source]]; const Rgba got = fb.at(px, py); if (got.r == want.r && got.g == want.g && got.b == want.b) ++matched; } return total ? double(matched) / double(total) : -1.0; } // Place sprite `s` bottom-anchored on tile (r,col) at the given altitude / base / vertical float (the same // geometry blit_sprite_at uses) and return its framebuffer match. static double probe(const Framebuffer& fb, const Palette& pal, const Sprite& s, int r, int col, int alt, int base, int yoff) { const ScreenPos p = tile_to_screen(r, col, alt, 2, kCam.x, kCam.y); return match_at(fb, pal, s, p.x, p.y - (s.height - base) + yoff); } // Match only the bottom `band` rows of the sprite — the strip of a stacked skirt cube that the cube one // level up doesn't overdraw (each level is 12 px above, a same-shape sprite, so it covers all but the // bottom ~12 px). Lets us read which sprite a buried skirt level actually drew. static double probe_band(const Framebuffer& fb, const Palette& pal, const Sprite& s, int r, int col, int alt, int band) { const ScreenPos p = tile_to_screen(r, col, alt, 2, kCam.x, kCam.y); const int x0 = p.x, y0 = p.y - (s.height - 17); long matched = 0, total = 0; for (int yy = std::max(0, s.height - band); yy < s.height; ++yy) for (int xx = 0; xx < s.width; ++xx) { if (!s.mask[std::size_t(yy) * s.width + xx]) continue; const int px = x0 + xx, py = y0 + yy; if (px < 0 || px >= fb.width() || py < 0 || py >= fb.height()) continue; ++total; const Rgba want = pal[s.index[std::size_t(yy) * s.width + xx]]; const Rgba got = fb.at(px, py); if (got.r == want.r && got.g == want.g && got.b == want.b) ++matched; } return total ? double(matched) / double(total) : -1.0; } // Road-on-cube tally over `city` (already rotated to `compass`): each road/network on an XTER 0x0d cube must // match the cube-TOP (alt+1) placement better than the cube-BASE (alt+0). Buried tiles are skipped. struct CubeStat { int top = 0, base = 0, occ = 0; }; static CubeStat cube_tally(const City& city, const Framebuffer& fb, const Palette& pal, const SpriteBank& bank) { const auto& bsize = sim::building_sizes(); CubeStat st; for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) { if (city.xter.at(r, col) != 0x0d) continue; const int code = city.xbld.at(r, col); if (code == 0 || code >= 0x6c || bsize[code] != 1) continue; // road/groundcover/network only const Sprite* s = bank.get(1000 + code); if (!s || s->empty()) continue; const int A = alt_at(city, r, col); const double top = probe(fb, pal, *s, r, col, A + 1, 17, 0); // cube top (the rule) const double bas = probe(fb, pal, *s, r, col, A + 0, 17, 0); // cube base (the bug) if (std::max(top, bas) < 0.30) { ++st.occ; continue; } // buried behind nearer tiles if (top > bas) ++st.top; else ++st.base; } return st; } // ── Part 1: SYNTHETIC clean room. One straight road on one lone cube on a flat empty plateau — no // neighbouring roads or stacks to confound the sample — so the lift gives a crisp top≫base separation we // can assert exactly, at every compass, and flip on/off in-process to prove the test bites. ── static void synthetic(const Assets& a, const CityRenderer& renderer) { City base; // default city: water_level()==0, everything flat at alt 0 const int A = 8; for (int r = 58; r <= 70; ++r) // a flat alt-8 plateau, all bare grass (no occluders) for (int c = 58; c <= 70; ++c) { base.altm.at(r, c) = A; base.xter.at(r, c) = 0; base.xbld.at(r, c) = 0; } base.xter.at(64, 64) = 0x0d; // the lone cube ("rock") base.xbld.at(64, 64) = 0x1d; // a straight road on top of it const Sprite* road = a.bank.get(1000 + 0x1d); CHECK(road != nullptr && !road->empty()); if (!road || road->empty()) return; auto measure = [&](const City& city, int compass, double& top, double& bas) { // the cube is unique, so just find it in the (possibly rotated) layers int cr = -1, cc = -1; for (int r = 0; r < City::kDim && cr < 0; ++r) for (int c = 0; c < City::kDim; ++c) if (city.xter.at(r, c) == 0x0d) { cr = r; cc = c; break; } CHECK(cr >= 0); const int code = city.xbld.at(cr, cc); const Sprite* s = a.bank.get(1000 + code); Framebuffer fb(kW, kH); renderer.render(city, fb, kCam, compass); top = probe(fb, a.palette, *s, cr, cc, A + 1, 17, 0); bas = probe(fb, a.palette, *s, cr, cc, A + 0, 17, 0); }; // The lift holds at every compass: road clearly present at the cube top, clearly absent at the base. for (int q = 0; q <= 3; ++q) { const City view = rotate_city(base, q); double top = 0, bas = 0; measure(view, q, top, bas); std::printf(" synthetic @compass%d: top=%.2f base=%.2f\n", q, top, bas); CHECK(top > 0.85); // the road really sits on the cube top CHECK(bas < 0.50); // and is NOT at the cube base CHECK(top > bas + 0.30); // crisp separation (no ambiguity without occluders) } } static void hydro_orientation(const Assets& assets, const CityRenderer& renderer) { City city; constexpr int row = 64, column = 64; city.xbld.at(row, column) = xbld::kHydroPlant1; city.xzon.at(row, column) = 0xF0; Framebuffer framebuffer(kW, kH); renderer.render(city, framebuffer, kCam); const Sprite* sprite = assets.bank.get(1000 + xbld::kHydroPlant1); CHECK(sprite && !sprite->empty()); if (!sprite || sprite->empty()) return; const ScreenPos position = tile_to_screen(row, column, 0, 2, kCam.x, kCam.y); const int x = position.x; const int y = position.y - (sprite->height - 17); const double corrected = oriented_match_at(framebuffer, assets.palette, *sprite, x, y, true); const double quarterTurned = oriented_match_at(framebuffer, assets.palette, *sprite, x, y, false); std::printf(" hydro orientation: corrected=%.2f quarter-turned=%.2f\n", corrected, quarterTurned); CHECK(corrected > 0.95); CHECK(corrected > quarterTurned + 0.25); } // ── Part 3: map skirt (the original's edge branch). On a coastal edge the side-wall is ROCK (1269) from the // alt-0 baseline up to the sea floor, then WATER (1284) up to the waterline. The old code drew a single // sprite (water all the way down on any watered tile), so the submerged cliff showed water where rock // belongs. Verify each band shows the right sprite on a real coastal city, at compass 0 and rotated. ── static void skirt(const Assets& a, const CityRenderer& renderer) { const std::string path = first_present_city({"BAYVIEW.SC2", "CAPE.SC2", "CAPEQUES.SC2", "HILTON.SC2"}); if (path.empty()) { std::printf(" skirt: SKIP (no coastal city installed)\n"); return; } City city; if (!io::load_sc2(path, city).ok) { CHECK(false); return; } const Sprite* rock = a.bank.get(1269); const Sprite* watc = a.bank.get(1284); CHECK(rock && !rock->empty() && watc && !watc->empty()); if (!rock || !watc || rock->empty() || watc->empty()) return; std::printf(" skirt city = %s\n", path.c_str()); auto run = [&](int q) { const City view = rotate_city(city, q); Framebuffer fb(kW, kH); renderer.render(view, fb, kCam, q); int tiles = 0, rockOK = 0, rockBad = 0, watOK = 0, watBad = 0; auto edge = [&](int r, int c) { const std::uint16_t al = view.altm.at(r, c); const int g = al & 0x1F, wl = (al >> 5) & 0x1F; if (!((view.xbit.at(r, c) & 0x04) && g >= 1 && wl > g)) return; // coastal: both bands present ++tiles; // Read the exposed bottom strip of a representative cube in each band (whole-sprite matching // can't see a buried skirt level). Rock at the top of the rock band (g-1), water at the top // of the water band (wl-1): the renderer must paint 1269 in the rock band, 1284 in the water. const double rkR = probe_band(fb, a.palette, *rock, r, c, g - 1, 12); const double rkW = probe_band(fb, a.palette, *watc, r, c, g - 1, 12); if (std::max(rkR, rkW) >= 0.30) { if (rkR > rkW) ++rockOK; else ++rockBad; } const double wtW = probe_band(fb, a.palette, *watc, r, c, wl - 1, 12); const double wtR = probe_band(fb, a.palette, *rock, r, c, wl - 1, 12); if (std::max(wtW, wtR) >= 0.30) { if (wtW > wtR) ++watOK; else ++watBad; } }; for (int c = 0; c < City::kDim; ++c) edge(City::kDim - 1, c); for (int r = 0; r < City::kDim; ++r) edge(r, City::kDim - 1); std::printf(" skirt @compass%d: coastal=%d rock[ok=%d bad=%d] water[ok=%d bad=%d]\n", q, tiles, rockOK, rockBad, watOK, watBad); CHECK(rockOK > 0); // the submerged cliff really shows rock CHECK(rockBad == 0); // and NEVER water in the rock band (the bug we removed) CHECK(watOK > 0); // the column above the sea floor really shows water }; run(0); run(1); // and the same holds after a quarter-turn (per-tile ALTM/XBIT rotate with the data) } // ── Part 4: near-water cube ("rock") height (the original altitude model). A shoreline XTER 0x0d cube whose // ground sits below the waterline must draw at its RAW ground — its top face one level up lands flush with // the water surface — NOT clamped up to the global sea level (which stuck it a level out of the water). // Find those cubes on a coastal city and verify the terrain sprite (1269) lands at `ground`, not at `sea`. static void cube_flush(const Assets& a, const CityRenderer& renderer) { const std::string path = first_present_city({"BAYVIEW.SC2", "CAPE.SC2", "HILTON.SC2", "RIVER5.SC2"}); if (path.empty()) { std::printf(" cube-flush: SKIP (no coastal city)\n"); return; } City city; if (!io::load_sc2(path, city).ok) { CHECK(false); return; } const int sea = std::clamp(city.water_level(), 0, 31); const Sprite* cube = a.bank.get(1269); CHECK(cube && !cube->empty()); if (!cube || cube->empty()) return; Framebuffer fb(kW, kH); renderer.render(city, fb, kCam); int tested = 0, flushWins = 0, raisedWins = 0, occ = 0; for (int r = 0; r < City::kDim; ++r) for (int c = 0; c < City::kDim; ++c) { if (city.xter.at(r, c) != 0x0d) continue; const int g = int(city.altm.at(r, c)) & 0x1F; if (g >= sea) continue; // only below-waterline shoreline cubes ++tested; const double flush = probe(fb, a.palette, *cube, r, c, g, 17, 0); // the original: raw ground (flush) const double raised = probe(fb, a.palette, *cube, r, c, sea, 17, 0); // old: clamped to sea (stuck up) if (std::max(flush, raised) < 0.30) { ++occ; continue; } if (flush > raised) ++flushWins; else ++raisedWins; } std::printf(" cube-flush (sea=%d): tested=%d flushWins=%d raisedWins=%d occ=%d\n", sea, tested, flushWins, raisedWins, occ); CHECK(tested > 0); // the city really has below-waterline shoreline cubes CHECK(flushWins > 0); // they draw flush at raw ground... CHECK(raisedWins == 0); // ...and none stick up at the clamped sea level (the bug we removed) } // ── Part 6: underground depth. The redesigned view has one lowered working floor. A station therefore // owns the same single-level foundation as the other network endpoints; the old two-level station block // must not survive beneath it. Render a lone XUND station and compare the two candidate sprites at the // lowered terrain surface anchor. static void underground_anchor(const Assets& a, const CityRenderer& renderer) { const Sprite* foundation = a.bank.get(1352); // one-level underground endpoint foundation const Sprite* oldStation = a.bank.get(1353); // obsolete two-level station platform CHECK(foundation && oldStation && !foundation->empty() && !oldStation->empty()); if (!foundation || !oldStation || foundation->empty() || oldStation->empty()) return; City c; const int A = 10; for (int r = 60; r <= 68; ++r) for (int cc = 60; cc <= 68; ++cc) { c.altm.at(r, cc) = A; c.xter.at(r, cc) = 0; } const int sr = 64, sc = 64; c.xund.at(sr, sc) = 0x23; // a lone subway station ViewToggles tg; tg.underground = true; Framebuffer fb(kW, kH); renderer.render(c, fb, kCam, 0, 0, 2, tg); const double oneLevel = probe(fb, a.palette, *foundation, sr, sc, A - 1, 17, 0); const double twoLevel = probe(fb, a.palette, *oldStation, sr, sc, A - 1, 17, 0); std::printf(" ug-depth station: one-level=%.2f two-level=%.2f\n", oneLevel, twoLevel); CHECK(oneLevel > 0.95); CHECK(twoLevel < 0.80); CHECK(oneLevel > twoLevel + 0.20); } // ── Part 7f: SURFACE per-tile draw order. The original draws each tile's terrain THEN its infra in one back-to- // front pass, so a nearer tile occludes the lower part of a feature behind it. The old two-pass // (ALL terrain, then ALL infra) drew every feature over ALL terrain → a tree/building on the far side of a // mountain SHOWED THROUGH it ("see through the map"), and a bridge ramp's underwater edge drew over the water // ("ramp under water"). Synthetic: a tree at the foot of a hill that rises IN FRONT of it (higher row+col). // With per-tile the hill occludes ~half the tree; with the two-pass the tree shows fully through the hill. static void surface_pertile_occlusion(const CityRenderer& renderer) { auto scene = [](bool hill) { // tree at (60,64); hill rises in front (rows 61-68) City c; for (int i = 54; i <= 74; ++i) for (int j = 58; j <= 70; ++j) { c.altm.at(i, j) = 4; c.xter.at(i, j) = 0; } c.xbld.at(60, 64) = 0x08; // "More Trees" if (hill) for (int k = 1; k <= 8; ++k) { c.xter.at(60 + k, 64) = 0x07; c.altm.at(60 + k, 64) = std::uint16_t(4 + k); } return c; }; City flatC; for (int i = 54; i <= 74; ++i) for (int j = 58; j <= 70; ++j) flatC.altm.at(i, j) = 4; Framebuffer flat(kW, kH), treeOnly(kW, kH); renderer.render(flatC, flat, kCam, 0, 0, 2, {}); renderer.render(scene(false), treeOnly, kCam, 0, 0, 2, {}); const int xc = kCam.x + 16 * (60 - 64); // the tree tile's screen column std::vector> treePx; for (int y = 0; y < kH; ++y) for (int x = xc - 20; x <= xc + 20; ++x) { if (x < 0 || x >= kW || y < 0 || y >= kH) continue; const Rgba t = treeOnly.at(x, y), f = flat.at(x, y); if (t.r != f.r || t.g != f.g || t.b != f.b) treePx.push_back({x, y}); } CHECK(!treePx.empty()); auto occluded = [&]() { Framebuffer full(kW, kH); renderer.render(scene(true), full, kCam, 0, 0, 2, {}); long gone = 0; for (auto [x, y] : treePx) { const Rgba w = treeOnly.at(x, y), g = full.at(x, y); if (g.r != w.r || g.g != w.g || g.b != w.b) ++gone; } // the tree pixel is no longer the tree → hill drew over it return gone; }; const long ptGone = occluded(); const double ptPct = 100.0 * ptGone / treePx.size(); std::printf(" surface-occlusion: tree px=%zu per-tile occluded=%.0f%%\n", treePx.size(), ptPct); CHECK(ptPct > 30.0); // the hill in front correctly hides the lower part of the tree behind it } // ── Part 7g: interior cliff fill. SC2K terrain steps one level per tile (each level faced by a slope sprite), // but some saved terrain drops 2-3 levels between adjacent tiles — no slope can face that, so the dark // background shows through ("a yawning chasm"; in the original too). draw_cliff fills the exposed face with // rock cubes. Synthetic: a flat alt-2 field with a 3-level bump (alt 5) — its front face is a chasm without // the fill, solid rock with it. static void surface_cliff_fill(const CityRenderer& renderer) { City c; for (int i = 40; i <= 90; ++i) for (int j = 40; j <= 90; ++j) { c.altm.at(i, j) = 2; c.xter.at(i, j) = 0; } for (int i = 60; i <= 64; ++i) for (int j = 58; j <= 62; ++j) c.altm.at(i, j) = 5; // a 3-level bump Framebuffer on(kW, kH); renderer.render(c, on, kCam, 0, 0, 2, {}); auto isBg = [](Rgba p) { return p.r == kBackground.r && p.g == kBackground.g && p.b == kBackground.b; }; const int xc = kCam.x + 16 * (64 - 62), yc = kCam.y + 8 * (64 + 62) - 12 * 5; // bump front corner long bgOn = 0; for (int y = yc - 20; y <= yc + 90; ++y) for (int x = xc - 90; x <= xc + 90; ++x) { if (x < 0 || x >= kW || y < 0 || y >= kH) continue; if (isBg(on.at(x, y))) ++bgOn; } std::printf(" cliff-fill: chasm background px = %ld\n", bgOn); CHECK(bgOn < 50); // the rock cubes fill the multi-level drop (no bare-background chasm) } // ── Part 7h: a multi-tile BUILDING on a cliff edge must draw OVER the interior cliff fill, not under it. // The cubes belong to the building's front footprint tiles (a higher diagonal than its NE-corner anchor), so // drawn in the terrain pass they'd paint over the building. The fix lays a multi-tile piece's footprint cliff // at its anchor (with the piece), so the building covers it. Guard: the building's own pixels on a cliff // match the building rendered on flat ground (it is NOT eaten by rock cubes). static void surface_building_on_cliff(const CityRenderer& renderer) { const std::uint8_t B = 0x91; // a 2×2 developed building (BSIZE 2) CHECK(sim::building_sizes()[B] == 2); auto mk = [&](bool cliff, bool building) { City c; for (int i = 40; i <= 90; ++i) for (int j = 40; j <= 90; ++j) { c.altm.at(i, j) = cliff ? 2 : 5; c.xter.at(i, j) = 0; } if (cliff) for (int i = 56; i <= 61; ++i) for (int j = 54; j <= 63; ++j) c.altm.at(i, j) = 5; // a plateau… if (building) { for (int i = 60; i <= 61; ++i) for (int j = 61; j <= 62; ++j) c.xbld.at(i, j) = B; // …with the bldg at its edge c.xzon.at(60, 61) = 0x10; // corner bits (rotation 0) — a developed c.xzon.at(61, 61) = 0x20; // building draws only at its 0x80 anchor c.xzon.at(61, 62) = 0x40; // corner, exactly like a real save c.xzon.at(60, 62) = 0x80; } return c; }; Framebuffer bare(kW, kH), flat(kW, kH), cliff(kW, kH); renderer.render(mk(false, false), bare, kCam, 0, 0, 2, {}); // flat alt-5, no building renderer.render(mk(false, true), flat, kCam, 0, 0, 2, {}); // building on flat alt-5 (no cliff) renderer.render(mk(true, true), cliff, kCam, 0, 0, 2, {}); // building on the plateau EDGE (cliff fires) long total = 0, intact = 0; for (int y = 0; y < kH; ++y) for (int x = 0; x < kW; ++x) { const Rgba f = flat.at(x, y), b = bare.at(x, y); if (f.r == b.r && f.g == b.g && f.b == b.b) continue; // not a building pixel (terrain unchanged) ++total; const Rgba c = cliff.at(x, y); if (c.r == f.r && c.g == f.g && c.b == f.b) ++intact; // building pixel survives on the cliff } const double frac = total ? double(intact) / total : 0.0; std::printf(" building-on-cliff: building px=%ld intact on cliff=%.1f%%\n", total, 100.0 * frac); CHECK(total > 500); // the building actually drew CHECK(frac > 0.92); // it draws OVER the cliff fill — not eaten by rock cubes } // ── Part 8: subway/pipe orientation survives rotation. rotate_city remaps the directional XUND codes // so a turn re-orients the tunnel; without it a N-S subway stayed N-S after a 90° turn. // Pure checks on the table + the layer transform (no art needed). static void subway_rotation() { using namespace sc2k::render; const auto& T = xund_rot_cw(); // (a) order-4 permutation: four applications return every code to itself. int moved = 0; for (int code = 0; code < 0x24; ++code) { int x = code; for (int i = 0; i < 4; ++i) x = T[x]; CHECK(x == code); // 4 turns = identity if (T[code] != code) ++moved; } CHECK(moved > 0); // it actually re-orients directional pieces // (b) rotate_city carries XUND through the table AND moves it to the rotated position. City c; c.xund.at(40, 50) = 0x01; // a N-S subway straight const City v = rotate_city(c, 1); int dr, dc; rotate_index_cw(40, 50, dr, dc); CHECK(v.xund.at(dr, dc) == T[0x01]); // re-oriented (0x01 → 0x02) at the moved tile CHECK(v.xund.at(dr, dc) == 0x02); std::printf(" subway-rotation: xund table order-4 ok (%d codes re-orient); rotate_city remaps 0x01→0x%02x\n", moved, v.xund.at(dr, dc)); } int main() { // Pure (no art): subway/pipe rotation table + layer transform — runs even without extracted assets. std::printf("height_placement: subway/pipe rotation (XUND code remap)\n"); subway_rotation(); if (!have_render_assets()) { std::printf("height_placement: SKIP rest (no extracted assets — run extract_assets.py)\n"); return failures ? 1 : 0; } const Assets a = load_assets(); CityRenderer renderer(a.bank, a.palette); std::printf("height_placement: synthetic clean-room cube lift\n"); synthetic(a, renderer); std::printf("height_placement: hydroelectric sprite handedness\n"); hydro_orientation(a, renderer); // ── Part 2: real city. Need one rich in BOTH cube-roads and the 0x62/0x63 highway curves. HILTON is // the canary (compass 0, 89 cube-roads, 84 of the 0x62/0x63 curves). ── const std::string cityPath = first_present_city({"HILTON.SC2", "FOURCITI.SC2", "HALENAPT.SC2", "CAPEQUES.SC2"}); if (cityPath.empty()) { std::printf("height_placement: (synthetic only — no real test city installed)\n"); return failures ? 1 : 0; } City city; const auto lr = io::load_sc2(cityPath, city); CHECK(lr.ok); if (!lr.ok) { std::printf("height_placement: load failed: %s\n", lr.error.c_str()); return 1; } std::printf("height_placement: real city = %s\n", cityPath.c_str()); // Issue 3 & 4: roads on rocks, at every compass. Dense real road networks on cubes can't give a clean // per-pixel verdict (a neighbour road at another altitude bleeds road-coloured pixels into the base // sample on curve tiles), so we assert the cube TOP placement *overwhelmingly dominates* the base — the // population flips from ~all-base to ~all-top exactly when the fix is on (verified by the A/B below). for (int q = 0; q <= 3; ++q) { const City view = rotate_city(city, q); Framebuffer fb(kW, kH); renderer.render(view, fb, kCam, q); const CubeStat st = cube_tally(view, fb, a.palette, a.bank); std::printf(" cube-road @compass%d: top=%d base=%d occ=%d\n", q, st.top, st.base, st.occ); CHECK(st.top >= 15); // plenty of visible cube-roads sit on the cube top CHECK(st.base * 4 <= st.top); // base-placements are a tiny residual (curve-tile noise), not the norm } // Elevated highways (0x61-0x6b) render at the GENERIC 2×2 base 25 — the original draws them at v36 − anchor // + 8 with an H−17 anchor (the same +8 a 2×2 building gets in the original, which // our renderer already places right at base 25). So +8 ≡ base 25. They take NO extra "float": an earlier // port floated the 0x62/0x63 curves a further 12 px DOWN (base 17 + 20), which left them sagging below // the deck and shifted vs the x-ray view (which uses +8). Probe the 0x62/0x63 anchors and confirm the // render sits at base 25, not the sunk base17+20. { Framebuffer fb(kW, kH); renderer.render(city, fb, kCam); std::vector claimed(std::size_t(City::kDim) * City::kDim, 0); int base25Wins = 0, sagWins = 0, occ = 0, tested = 0; for (int r = 0; r < City::kDim; ++r) for (int col = City::kDim - 1; col >= 0; --col) { const int code = city.xbld.at(r, col); if (code != 0x62 && code != 0x63) continue; if (claimed[std::size_t(r) * City::kDim + col]) continue; for (int dr = 0; dr < 2; ++dr) // claim the 2×2 same-code footprint for (int dc = 0; dc < 2; ++dc) { const int rr = r + dr, cc = col - dc; if (rr < City::kDim && cc >= 0 && city.xbld.at(rr, cc) == code) claimed[std::size_t(rr) * City::kDim + cc] = 1; } const Sprite* s = a.bank.get(1000 + code); if (!s || s->empty()) continue; const int A = alt_at(city, r, col); const double base25 = probe(fb, a.palette, *s, r, col, A, 25, 0); // the original (== +8) const double sag = probe(fb, a.palette, *s, r, col, A, 17, 20); // the reverted 12px sag if (std::max(base25, sag) < 0.30) { ++occ; continue; } ++tested; if (base25 > sag) ++base25Wins; else if (sag > base25) ++sagWins; } std::printf(" highway 0x62/63: base25Wins=%d sagWins=%d (occ=%d tested=%d)\n", base25Wins, sagWins, occ, tested); CHECK(base25Wins > 0); // the curves sit at the original base 25... CHECK(sagWins == 0); // ...not 12 px lower at the reverted base17+20 } // Issue 2: the map skirt — rock-then-water layering on coastal edges. std::printf("height_placement: map skirt (coastal rock/water layering)\n"); skirt(a, renderer); // Near-water cube height — the original land=ground / water=waterLevel altitude model (no global-sea clamp). std::printf("height_placement: near-water cube flush (altitude model)\n"); cube_flush(a, renderer); // Underground ground-layer sprites (subway/station/pipe) anchor on the lowered terrain surface. std::printf("height_placement: underground lowered surface anchor (station/subway)\n"); underground_anchor(a, renderer); // Surface per-tile order: a nearer mountain occludes a feature behind it (no "see through the map"). std::printf("height_placement: surface per-tile occlusion (feature behind a mountain)\n"); surface_pertile_occlusion(renderer); // Interior cliff fill: an abrupt multi-level terrain drop is faced with rock, not a background "chasm". std::printf("height_placement: interior cliff fill (multi-level drop \"chasm\")\n"); surface_cliff_fill(renderer); // A multi-tile building on a cliff edge draws OVER the cliff fill (not eaten by the rock cubes). std::printf("height_placement: building on a cliff edge (cliff fill stays behind it)\n"); surface_building_on_cliff(renderer); if (failures == 0) { std::printf("height_placement: all checks passed\n"); return 0; } std::printf("height_placement: %d failure(s)\n", failures); return 1; }