// City Map overview KAT (render/minimap.hpp): the per-tile palette index reproduces the original's Map fill, and on // a real city the rendered water matches the source water tiles (numeric, not eyeballed). Skips without // assets/cities. #include "sc2k/io/sc2_file.hpp" #include "sc2k/render/asset_loader.hpp" #include "sc2k/render/minimap.hpp" #include #include #include #include "test_harness.hpp" #include "test_paths.hpp" int main() { using namespace sc2k; using render::minimap_index; // The exact index rules from the original. CHECK(minimap_index(0x20, 0, 0) == 0); // developed building (XBLD>=0x0D) → 0 CHECK(minimap_index(0x0D, 0, 0) == 0); CHECK(minimap_index(0x0C, 0, 0) == 67); // structure (6..12) → 67 CHECK(minimap_index(6, 0, 0) == 67); CHECK(minimap_index(5, 0, 0) == 53); // feature (1..5) → 53 CHECK(minimap_index(1, 0, 0) == 53); CHECK(minimap_index(0, 0x04, 0) == 98); // empty + XBIT&4 → water (98) CHECK(minimap_index(0, 0, 0) == 128); // empty terrain, alt 0 → 128 - 0 CHECK(minimap_index(0, 0, 16) == 116); // alt 16 → 128 - 12 CHECK(minimap_index(0, 0, 20) == 116); // alt (20 & 0x1F = 20) clamps to 16 → 128 - 12 CHECK(minimap_index(0x20, 0x04, 0) == 0); // XBLD wins over XBIT (building, not water) // Data views (Zones, Infrastructure). using render::MapView; CHECK(render::minimap_index_view(MapView::Zones, 0, 1, 0, 0) == 59); // R zone → idx 59 (green) CHECK(render::minimap_index_view(MapView::Zones, 0, 3, 0, 0) == 92); // C zone → 92 CHECK(render::minimap_index_view(MapView::Zones, 0, 5, 0, 0) == 50); // I zone → 50 CHECK(render::minimap_index_view(MapView::Zones, 0x20, 0, 0, 0) == 0); // unzoned building → structures colour CHECK(render::minimap_index_view(MapView::Infrastructure, 0x1d, 0, 0, 0) == 255); // a road → white CHECK(render::minimap_index_view(MapView::Infrastructure, 0x70, 0, 0, 0) == 0); // a building → not highlighted CHECK(render::is_transport_tile(0x1d) && render::is_transport_tile(0x3f) && !render::is_transport_tile(0x70)); CHECK(render::is_rail_tile(0x2c) && render::is_rail_tile(0x3e) && !render::is_rail_tile(0x1d)); CHECK(render::is_power_tile(0x0e) && render::is_power_tile(0x1c) && !render::is_power_tile(0x70)); { // Power view: power tile white, powered building → 50, conductive → 29. City pc; pc.xbld.at(7, 7) = 0x80; pc.xbit.at(7, 7) = 0x40; // a powered building CHECK(render::view_index(pc, render::MapView::Power, 7, 7) == 50); pc.xbld.at(8, 8) = 0x0e; // a power line → white CHECK(render::view_index(pc, render::MapView::Power, 8, 8) == 255); // Water view: a piped/subway underground tile → white. pc.xund.at(9, 9) = 0x12; CHECK(render::view_index(pc, render::MapView::Water, 9, 9) == 255); } CHECK(int(render::MapView::Count) == 12); // 12 data views (+ Growth) { // Growth view: XROG growth rate → declining/neutral/growing City gc; gc.xrog.at(0, 0) = 0x70; CHECK(render::view_index(gc, render::MapView::Growth, 0, 0) == 29); // ≤0x7C decline gc.xrog.at(0, 0) = 0x80; CHECK(render::view_index(gc, render::MapView::Growth, 0, 0) == 128); // neutral → base (empty,alt0) gc.xrog.at(0, 0) = 0x90; CHECK(render::view_index(gc, render::MapView::Growth, 0, 0) == 67); // ≥0x83 growth } // Heat-map views (Traffic etc.): high nibble → (field>>4)-101 grey ramp, else base. { City hc; hc.xtrf.at(5, 5) = 0xF0; // peak traffic at downsampled (5,5) CHECK(render::heatmap_index(hc, render::MapView::Traffic, 10, 10) == std::uint8_t(0xF - 101)); // = 170 hc.xtrf.at(5, 5) = 0; CHECK(render::heatmap_index(hc, render::MapView::Traffic, 10, 10) == 128); // no traffic → terrain base } const std::string adir = render::assets_dir(); const std::string city_path = city_file("LONDON.SC2"); if (!exists(adir + "/PAL_MSTR.BMP") || !exists(city_path)) { std::printf("minimap: index rules OK; SKIP city render (no assets/LONDON)\n"); return failures ? 1 : 0; } City city; CHECK(io::load_sc2(city_path, city).ok); const auto assets = render::load_assets(); const auto mm = render::render_minimap(city, assets.palette); CHECK(mm.width() == 128 && mm.height() == 128); // Rendered water (pure blue, idx 98) must sit on the source water tiles: same centroid. long wx = 0, wy = 0, wn = 0, bx = 0, by = 0, bn = 0; for (int y = 0; y < 128; ++y) for (int x = 0; x < 128; ++x) { const auto p = mm.at(x, y); if (p.b > 200 && p.r < 60 && p.g < 60) { wx += x; wy += y; ++wn; } if (city.xbit.at(x, y) & 4) { bx += x; by += y; ++bn; } } CHECK(wn > 100 && bn > 100); CHECK(wn <= bn); // water pixels ⊆ XBIT&4 (buildings override) CHECK(std::abs(wx / wn - bx / bn) <= 1 && std::abs(wy / wn - by / bn) <= 1); // same centroid std::printf("minimap: LONDON water px=%ld centroid=(%ld,%ld) — matches source (%ld,%ld)\n", wn, wx / wn, wy / wn, bx / bn, by / bn); if (failures == 0) { std::printf("minimap: all checks passed\n"); return 0; } std::printf("minimap: %d failure(s)\n", failures); return 1; }