// Numeric tests for the three view features wired in 2026-06-23 (verify-visuals-numerically, not by eye): // 1. Show/Hide layer toggles (toolbar 28-32) — // the renderer's ViewToggles gate Buildings (XBLD>=0x70), Infrastructure (0x01..0x6f) and the // zone-tint, and Underground swaps to the grey pipes/subway backdrop and suppresses the surface. // 2. The rotate-then-recentre invariant (Rotate CCW/CW buttons): the tile under // the screen centre stays centred *at its altitude* across a quarter-turn — the height-correct // re-centring the buttons need. Built from the same public primitives the app's rotate_view uses. // 3. The Centering-tool invariant: centring on a clicked tile lands it dead-centre at its altitude. // // Part 2/3 are pure math (always run). Part 1 needs the extracted Win95 art, so it SKIPs without it. #include "sc2k/io/sc2_file.hpp" #include "sc2k/render/asset_loader.hpp" #include "sc2k/render/city_renderer.hpp" #include "sc2k/render/iso_projection.hpp" #include "sc2k/render/rotate.hpp" #include #include #include using namespace sc2k; using namespace sc2k::render; #include "test_harness.hpp" #include "test_paths.hpp" // Count pixels that differ between two framebuffers (same size). static long diff_px(const Framebuffer& a, const Framebuffer& b) { long n = 0; for (int y = 0; y < a.height(); ++y) for (int x = 0; x < a.width(); ++x) { const Rgba p = a.at(x, y), q = b.at(x, y); if (p.r != q.r || p.g != q.g || p.b != q.b) ++n; } return n; } int main() { // ───────────────────────── Part 2: rotate-then-recentre keeps the focus tile centred ───────────── // This is exactly what app/main.cpp's `rotate_view` + `center_on_tile` compose. We replicate that // composition from the public primitives and assert the pivot tile lands on the screen centre at its // altitude after the turn — for every rotation, zoom and a spread of altitudes. (origin_centering_tile // is the exact inverse of tile_to_screen; rotation must carry the altitude with the tile.) const int CX = 640, CY = 400; // an arbitrary screen centre for (int rot = 0; rot < 4; ++rot) for (int zoom = 0; zoom <= 2; ++zoom) for (int alt : {0, 1, 7, 16, 31}) for (auto rc : {std::pair{63, 63}, std::pair{40, 70}, std::pair{0, 0}, std::pair{127, 0}}) { const int cr = rc.first, cc = rc.second; // city tile → its position in the view rotated `rot` quarter-turns CW int vr, vc; rotate_index_cw_n(cr, cc, rot, vr, vc); // app reads the altitude from the *rotated* view; rotation must preserve it per-tile, // so the centred camera = origin_centering_tile(view tile, that altitude). const ScreenPos cam = origin_centering_tile(vr, vc, alt, zoom, CX, CY); const ScreenPos p = tile_to_screen(vr, vc, alt, zoom, cam.x, cam.y); CHECK(p.x == CX && p.y == CY); // the focus tile is dead-centre, altitude included } // Rotation actually moves the altitude with the tile (so the centring above used the right height): // a synthetic city with a few raised tiles, rotated, must carry each altitude to the tile's new index. { City c; // zero-init layers c.altm.at(10, 20) = 7; c.altm.at(63, 63) = 16; c.altm.at(0, 127) = 31; for (int rot = 1; rot <= 3; ++rot) { const City r = rotate_city(c, rot); for (auto rc : {std::pair{10, 20}, std::pair{63, 63}, std::pair{0, 127}}) { int vr, vc; rotate_index_cw_n(rc.first, rc.second, rot, vr, vc); CHECK((r.altm.at(vr, vc) & 0x1F) == (c.altm.at(rc.first, rc.second) & 0x1F)); } } } std::printf("view_toggles: centring/rotation projection invariant — checked all rot/zoom/alt\n"); // ───────────────────────── Part 1: the renderer's Show/Hide layer toggles ───────────────────────── // Needs the extracted Win95 art (sprite bank + palette); SKIPs without it. if (!have_render_assets()) { std::printf("view_toggles: render-gating SKIP (no extracted assets — run extract_assets.py)\n"); if (failures == 0) { std::printf("view_toggles: all (math) checks passed\n"); return 0; } std::printf("view_toggles: %d failure(s)\n", failures); return 1; } const Assets a = load_assets(); CityRenderer renderer(a.bank, a.palette); // A BLANK flat city (all-zero layers = flat grass, alt 0, no water) carrying exactly one of each // toggle-able feature, so flipping a single toggle changes precisely that feature — nothing else on // the map to occlude or confound the diff. City city; int bcode = -1; for (int code = 0x70; code <= 0xFF; ++code) if (sim::building_sizes()[code] == 1 && a.bank.get(1000 + code)) { bcode = code; break; } CHECK(bcode > 0); const int bldR = 68, bldC = 68; // the lone building const int roadR = 66, roadC = 62; // the lone road (infra, XBLD 0x01..0x6f) const int zoneR = 62, zoneC = 66; // a zoned-but-undeveloped tile (→ zone tint) city.xbld.at(bldR, bldC) = std::uint8_t(bcode); city.xbld.at(roadR, roadC) = 0x1D; // a road straight city.xzon.at(zoneR, zoneC) = 0x01; // residential zone nibble, no building → tint Framebuffer fbOn(4200, 2600); const Camera cam{2100, 420}; renderer.render(city, fbOn, cam, 0, 0, 2, ViewToggles{}); // Buildings OFF (both building categories) — only the lone building differs, so the whole diff is that sprite. { Framebuffer fb(4200, 2600); renderer.render(city, fb, cam, 0, 0, 2, ViewToggles{.zonedBuildings = false, .otherBuildings = false}); const long ch = diff_px(fbOn, fb); std::printf("view_toggles: Buildings off — changed=%ld px (the lone structure)\n", ch); CHECK(ch > 50); // the building visibly vanished } // Transportation OFF — the road sprite is gone (terrain shows through), so the image changes. { Framebuffer fb(4200, 2600); renderer.render(city, fb, cam, 0, 0, 2, ViewToggles{.transportation = false}); const long ch = diff_px(fbOn, fb); std::printf("view_toggles: Transportation off — changed=%ld\n", ch); CHECK(ch > 10); // the road vanished } // Zones OFF — the zone-tint tile reverts to terrain, so the image changes there. { Framebuffer fb(4200, 2600); renderer.render(city, fb, cam, 0, 0, 2, ViewToggles{.zones = false}); const long ch = diff_px(fbOn, fb); std::printf("view_toggles: Zones off — changed=%ld\n", ch); CHECK(ch > 5); // the zone tint reverted to bare terrain } // The 4→7 split is real: a road (Transport), power line (Power) and tree (Flora) each hide under // their OWN toggle, independently — proven by the three off-renders differing from one another. { City c3; c3.xbld.at(66, 62) = 0x1D; // road → Transport c3.xbld.at(66, 64) = 0x0E; // power line → Power c3.xbld.at(64, 62) = 0x06; // tree → Flora Framebuffer allOn(4200, 2600), noT(4200, 2600), noP(4200, 2600), noF(4200, 2600); renderer.render(c3, allOn, cam, 0, 0, 2, ViewToggles{}); renderer.render(c3, noT, cam, 0, 0, 2, ViewToggles{.transportation = false}); renderer.render(c3, noP, cam, 0, 0, 2, ViewToggles{.powerLines = false}); renderer.render(c3, noF, cam, 0, 0, 2, ViewToggles{.flora = false}); CHECK(diff_px(allOn, noT) > 0); // each toggle hides its own feature CHECK(diff_px(allOn, noP) > 0); CHECK(diff_px(allOn, noF) > 0); CHECK(diff_px(noT, noP) > 0); // …and they hide DIFFERENT features CHECK(diff_px(noT, noF) > 0); CHECK(diff_px(noP, noF) > 0); std::printf("view_toggles: 7-way split — road/power/flora hide independently\n"); } // Underground ON — grey backdrop, and the whole surface (buildings/infra/zone) is suppressed. { Framebuffer fb(4200, 2600); renderer.render(city, fb, cam, 0, 0, 2, ViewToggles{.underground = true}); // A corner pixel (well outside the diamond) is the underground grey, not the surface dark-brown. const Rgba corner = fb.at(2, 2); CHECK(corner.r == kUnderground.r && corner.g == kUnderground.g && corner.b == kUnderground.b); const long ch = diff_px(fbOn, fb); std::printf("view_toggles: Underground on — corner=(%d,%d,%d) changed=%ld\n", corner.r, corner.g, corner.b, ch); CHECK(ch > 100000); // a whole-view mode switch (bg + suppressed surface) } // ───────────────────────── Part 4: underground (x-ray) view CONTENT ─────────────────────────────── // The x-ray view draws wireframe terrain + water pipes + subway tunnels + // zone-coloured building outlines. Verify each numerically (not by eye): a feature toggled on a // single tile changes precisely that tile vs an otherwise-identical bare render. { const ViewToggles ug{.underground = true}; auto ug_render = [&](auto setup) { // fresh flat city → underground render City c; setup(c); Framebuffer fb(4200, 2600); renderer.render(c, fb, cam, 0, 0, 2, ug); return fb; }; const int pR = 60, pC = 60, sR = 60, sC = 64, tR = 64, tC = 60, rR = 64, rC = 64; // (a) wireframe terrain actually fills the diamond (the view isn't bare grey backdrop). Framebuffer bare = ug_render([](City&) {}); long nonGrey = 0; for (int y = 0; y < bare.height(); ++y) for (int x = 0; x < bare.width(); ++x) { const Rgba p = bare.at(x, y); if (!(p.r == kUnderground.r && p.g == kUnderground.g && p.b == kUnderground.b)) ++nonGrey; } CHECK(nonGrey > 10000); // the wireframe terrain mesh // (b) a pipe (XUND 0x10) and a subway (XUND 0x01) each draw distinct content vs bare, and ≠ each other. Framebuffer fPipe = ug_render([&](City& c) { c.xund.at(pR, pC) = 0x10; }); Framebuffer fSub = ug_render([&](City& c) { c.xund.at(sR, sC) = 0x01; }); CHECK(diff_px(bare, fPipe) > 0); CHECK(diff_px(bare, fSub) > 0); CHECK(diff_px(fPipe, fSub) > 0); // (c) SURFACE features contribute nothing underground — trees, roads, rail, power lines and // elevated highways all vanish (no value in surface infra down here); the only sub-0x70 // pieces that show are underground-network endpoints: a tunnel mouth (Tunnels layer) and a // subway↔rail adapter (Subways layer). for (int code : {0x06, 0x1D, 0x2C, 0x0E, 0x65}) CHECK(diff_px(bare, ug_render([&](City& c) { c.xbld.at(rR, rC) = std::uint8_t(code); })) == 0); Framebuffer fMouth = ug_render([&](City& c) { c.xbld.at(tR, tC) = 0x3F; }); Framebuffer fAdapter = ug_render([&](City& c) { c.xbld.at(rR, rC) = 0x6C; }); CHECK(diff_px(bare, fMouth) > 0); CHECK(diff_px(bare, fAdapter) > 0); // (d) zone-coloured outlines: R/C/I give three DISTINCT outlines, each ≠ bare and // ≠ a no-zone building — the blue=C / green=R / yellow=I / orange=other mapping is wired. auto bld = [&](int znib) { return ug_render([&](City& c) { c.xbld.at(bldR, bldC) = std::uint8_t(bcode); c.xzon.at(bldR, bldC) = std::uint8_t(znib); }); }; Framebuffer fR = bld(1), fC = bld(3), fI = bld(5), f0 = bld(0); CHECK(diff_px(bare, fR) > 0); // an outline appears over the building CHECK(diff_px(fR, fC) > 0); CHECK(diff_px(fR, fI) > 0); CHECK(diff_px(fC, fI) > 0); // R≠C≠I CHECK(diff_px(fR, f0) > 0); // zoned ≠ no-zone (orange) // (e) the outlines ALWAYS draw underground — the above-ground visibility toggles don't apply // down here (their checkboxes grey out), so the layout stays readable regardless. Framebuffer fOff(4200, 2600); { City c; c.xbld.at(bldR, bldC) = std::uint8_t(bcode); c.xzon.at(bldR, bldC) = 1; renderer.render(c, fOff, cam, 0, 0, 2, ViewToggles{.zonedBuildings = false, .otherBuildings = false, .underground = true}); } CHECK(diff_px(fR, fOff) == 0); std::printf("view_toggles: underground content — wireframe nonGrey=%ld; pipe/subway/R/C/I " "outlines all distinct; surface infra hidden; outlines ignore the surface toggles\n", nonGrey); } // ───────────────────────── Part 4b: underground per-network toggles + source legibility ──────────── // The Subways / Water pipes toggles gate their own x-ray network independently, and water-source // buildings stay legible under the Water pipes layer so pipes can be traced to their pumps. { auto ug = [&](ViewToggles t, auto setup) { t.underground = true; City c; setup(c); Framebuffer fb(4200, 2600); renderer.render(c, fb, cam, 0, 0, 2, t); return fb; }; const int pR = 60, pC = 60, sR = 60, sC = 64; auto pipe = [&](City& c) { c.xund.at(pR, pC) = 0x10; }; auto subway = [&](City& c) { c.xund.at(sR, sC) = 0x01; }; // A pipe vanishes with Water pipes off; a subway with Subways off — and each toggle leaves the // OTHER network untouched (independent gating). CHECK(diff_px(ug(ViewToggles{}, pipe), ug(ViewToggles{.waterPipes = false}, pipe)) > 0); CHECK(diff_px(ug(ViewToggles{}, subway), ug(ViewToggles{.subways = false}, subway)) > 0); CHECK(diff_px(ug(ViewToggles{}, subway), ug(ViewToggles{.waterPipes = false}, subway)) == 0); CHECK(diff_px(ug(ViewToggles{}, pipe), ug(ViewToggles{.subways = false}, pipe)) == 0); // A water pump (0xDC, an "Other building") stays visible with Other buildings OFF as long as Water // pipes is ON — so pumps are traceable; it hides only when BOTH are off. auto pump = [&](City& c) { c.xbld.at(pR, pC) = 0xDC; }; Framebuffer bareUG = ug(ViewToggles{}, [](City&) {}); Framebuffer pumpOn = ug(ViewToggles{}, pump); // Water pipes on → real sprite + foundation Framebuffer pumpOff = ug(ViewToggles{.waterPipes = false}, pump); // off → the always-on outline CHECK(diff_px(bareUG, pumpOn) > 0); CHECK(diff_px(bareUG, pumpOff) > 0); CHECK(diff_px(pumpOn, pumpOff) > 0); // sprite+foundation ≠ plain outline // …and a MULTI-TILE source (water treatment) draws its sprite + per-tile foundation the same way. const int tN = sim::building_sizes()[0xF4]; auto plant = [&](City& c) { for (int dr = 0; dr < tN; ++dr) for (int dc = 0; dc < tN; ++dc) c.xbld.at(66 + dr, 66 - dc) = 0xF4; }; CHECK(diff_px(bareUG, ug(ViewToggles{}, plant)) > 0); CHECK(diff_px(ug(ViewToggles{}, plant), ug(ViewToggles{.waterPipes = false}, plant)) > 0); // The Tunnels layer gates the bored road-tunnel body (ALTM depth bits) — and only that: hiding // it leaves the other networks untouched. auto tun = [&](City& c) { c.altm.at(58, 58) = std::uint16_t(c.altm.at(58, 58) | (3 << 10)); }; CHECK(diff_px(ug(ViewToggles{}, tun), ug(ViewToggles{.tunnels = false}, tun)) > 0); CHECK(diff_px(ug(ViewToggles{}, pipe), ug(ViewToggles{.tunnels = false}, pipe)) == 0); // CROSSOVER tiles (XUND 0x1F/0x20 = subway + pipe on one tile) belong to BOTH networks: // something draws there under any single toggle (hiding pipes must NOT cut the subway line, // and vice versa), and the two single-network draws differ from each other. Framebuffer bare = ug(ViewToggles{}, [](City&) {}); auto xoverC = [&](City& c) { c.xund.at(62, 62) = 0x1F; }; Framebuffer xBoth = ug(ViewToggles{}, xoverC); Framebuffer xSub = ug(ViewToggles{.waterPipes = false}, xoverC); Framebuffer xPipe = ug(ViewToggles{.subways = false}, xoverC); CHECK(diff_px(bare, xBoth) > 0); // combined art draws CHECK(diff_px(bare, xSub) > 0); // subway half survives pipes-off CHECK(diff_px(bare, xPipe) > 0); // pipe half survives subways-off CHECK(diff_px(xSub, xPipe) > 0); // …and they are the two DIFFERENT halves std::printf("view_toggles: underground per-network — pipes/subways gate independently; " "water sources stay legible under the Water pipes layer; crossovers belong to both\n"); } // ───────────────────────── Part 4c: flat-terrace step faces are filled ──────────────────────────── // Saved cities carry runs of FLAT tiles stepping one ALTM level each (an original city's hillside). // The lower neighbour is flat, so no slope sprite rises to cover the step's vertical face, and the old // ≥2-level cliff rule left a hollow showing the background. Sample the face band under the upper // tile's bottom vertex: it must NOT be the background colour. { City c; for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) c.altm.at(r, col) = std::uint16_t((r <= 64 && col <= 64) ? 5 : 4); // a flat terrace step Framebuffer fs(4200, 2600); renderer.render(c, fs, cam, 0, 0, 2, ViewToggles{}); const Rgba bgS = fs.at(2, 2); const ScreenPos ps = tile_to_screen(64, 64, 5, 2, cam.x, cam.y); const Rgba faceS = fs.at(ps.x + 16, ps.y + 22); // mid-face between the two surfaces CHECK(!(faceS.r == bgS.r && faceS.g == bgS.g && faceS.b == bgS.b)); std::printf("view_toggles: flat-terrace step face is filled (not background)\n"); } if (failures == 0) { std::printf("view_toggles: all checks passed\n"); return 0; } std::printf("view_toggles: %d failure(s)\n", failures); return 1; }