// Service / civic building tools KAT (ui/tools.hpp building_for_command + sim/growth construct_building). // The tile code / footprint / cost were recovered from the original (tile-code, footprint, and // cost tables; footprints cross-checked vs the BSIZE render table). Verify the data + that a plop // stamps the right N×N footprint on buildable land and refuses to overlap — numerically, no SDL. #include "sc2k/city.hpp" #include "sc2k/sim/growth.hpp" #include "sc2k/sim/growth_scan.hpp" // special_zone_develop (port development) #include "sc2k/ui/tools.hpp" #include #include #include "test_harness.hpp" static int count_code(const sc2k::City& c, int r0, int c0, int n, int code) { int k = 0; for (int r = r0; r < r0 + n; ++r) for (int cc = c0; cc < c0 + n; ++cc) if (c.xbld.at(r, cc) == code) ++k; return k; } int main() { using namespace sc2k; // Data table: command id → {code, size, cost} (verified against MENU 136 + the original tables). const ui::BuildingSpec* police = ui::building_for_command(32875); CHECK(police && police->code == 210 && police->size == 3 && police->cost == 500); const ui::BuildingSpec* prison = ui::building_for_command(32878); CHECK(prison && prison->code == 216 && prison->size == 4 && prison->cost == 3000); const ui::BuildingSpec* college = ui::building_for_command(32872); CHECK(college && college->code == 217 && college->size == 4 && college->cost == 1000); const ui::BuildingSpec* statue = ui::building_for_command(32847); CHECK(statue && statue->code == 219 && statue->size == 1); const ui::BuildingSpec* stadium = ui::building_for_command(32882); CHECK(stadium && stadium->code == 215 && stadium->size == 4 && stadium->cost == 5000); const ui::BuildingSpec* park = ui::building_for_command(32879); CHECK(park && park->code == 13 && park->size == 1 && park->cost == 20); const ui::BuildingSpec* pump = ui::building_for_command(32841); CHECK(pump && pump->code == 220 && pump->size == 1); CHECK(ui::building_for_command(99999) == nullptr); // Placement on a default (flat, empty, buildable) city. City c; CHECK(construct_building(c, 10, 10, 210, 3, 0)); // Police: 3×3 CHECK(count_code(c, 10, 10, 3, 210) == 9); CHECK(!construct_building(c, 11, 11, 211, 3, 0)); // can't overlap the police footprint CHECK(construct_building(c, 50, 50, 216, 4, 0)); // Prison: 4×4 CHECK(count_code(c, 50, 50, 4, 216) == 16); CHECK(construct_building(c, 70, 70, 219, 1, 0)); // Statue: 1×1 CHECK(c.xbld.at(70, 70) == 219); // Arcologies (Select Arcology dialog) — codes 251-254, 4×4, costs recovered from the tool table. CHECK(ui::arcology(0)->code == 251 && ui::arcology(0)->cost == 100000 && std::string(ui::arcology(0)->name) == "arco.plymouth"); CHECK(ui::arcology(3)->code == 254 && ui::arcology(3)->cost == 200000 && std::string(ui::arcology(3)->name) == "arco.launch"); CHECK(ui::arcology(4) == nullptr); CHECK(construct_building(c, 90, 90, 251, 4, 0) && count_code(c, 90, 90, 4, 251) == 16); // a Plymouth arcology, 4×4 // --- placement-preview per-tile validity (red/blue footprint hint) --- // construct_building_tile_ok must agree, cell-for-cell, with the clearance loop inside construct_building, // so the preview never paints a tile a colour that disagrees with what plopping actually does. { City p; // fresh flat/empty/buildable map CHECK(construct_building_tile_ok(p, 5, 5, 210)); // bare land: OK (blue) p.xbld.at(5, 6) = 0x1D; // a road tile CHECK(!construct_building_tile_ok(p, 5, 6, 210)); // infrastructure: blocked (red) p.xbit.at(5, 7) = std::uint8_t(p.xbit.at(5, 7) | 0x04); // a water tile CHECK(!construct_building_tile_ok(p, 5, 7, 210)); // water: blocked for a normal building CHECK(construct_building_tile_ok(p, 5, 7, 248)); // …but OK for the marina (248) p.xbld.at(5, 8) = 9; // a tree (6-12) CHECK(construct_building_tile_ok(p, 5, 8, 210)); // trees are overwritten: OK p.xzon.at(5, 9) = 7; // military special zone CHECK(!construct_building_tile_ok(p, 5, 9, 210)); // military: blocked p.xter.at(5, 10) = 1; // a sloped tile CHECK(!construct_building_tile_ok(p, 5, 10, 210)); // slope: blocked (buildings need flat ground) CHECK(!construct_building_tile_ok(p, -1, 0, 210)); // off-map: blocked // Whole-footprint consistency: a 3×3 plop succeeds iff every tile passes the per-tile predicate. auto footprint_all_ok = [&](const City& cc, int r0, int c0, int n, int code) { for (int r = r0; r < r0 + n; ++r) for (int c2 = c0; c2 < c0 + n; ++c2) if (!construct_building_tile_ok(cc, r, c2, code)) return false; return true; }; City q; // clean map: footprint clear ⇒ both agree (placeable) CHECK(footprint_all_ok(q, 20, 20, 3, 210) && construct_building(q, 20, 20, 210, 3, 0)); City r; // a road through the footprint ⇒ both agree (blocked) r.xbld.at(31, 31) = 0x1D; CHECK(!footprint_all_ok(r, 30, 30, 3, 210) && !construct_building(r, 30, 30, 210, 3, 0)); City s; // a slope under the footprint ⇒ both refuse (flat-only) s.xter.at(41, 41) = 1; CHECK(!footprint_all_ok(s, 40, 40, 3, 210) && !construct_building(s, 40, 40, 210, 3, 0)); } { // power_plant_tile_ok: flat dry empty/tree land only. City p; CHECK(ui::detail::power_plant_tile_ok(p, 40, 40)); // bare flat land: OK p.xter.at(40, 41) = 1; // a slope CHECK(!ui::detail::power_plant_tile_ok(p, 40, 41)); // slope: blocked (plants need flat) p.xbit.at(40, 42) = std::uint8_t(p.xbit.at(40, 42) | 0x04); CHECK(!ui::detail::power_plant_tile_ok(p, 40, 42)); // water: blocked p.xbld.at(40, 43) = 0x1D; // a road CHECK(!ui::detail::power_plant_tile_ok(p, 40, 43)); // occupied: blocked p.xbld.at(40, 44) = 9; // a tree CHECK(ui::detail::power_plant_tile_ok(p, 40, 44)); // tree yields: OK } // ---- Ports zone like R/C/I: flat DRY land only (water rejected), but NOT required to touch water ---- { City c; c.set_funds(100000); for (int r = 38; r < 43; ++r) for (int cc = 60; cc < 62; ++cc) c.xbit.at(r, cc) |= 0x04; // a lake CHECK(!ui::apply_tool(c, ui::Tool::Seaport, 40, 60).ok); // ON water → rejected (the only "into water" guard) CHECK(ui::apply_tool(c, ui::Tool::Seaport, 40, 10).ok); // dry land away from water → ALLOWED (faithful) CHECK((c.xzon.at(40, 10) & 0x0F) == 9); // seaport zone nibble CHECK(ui::apply_tool(c, ui::Tool::Seaport, 40, 59).ok); // dry, flat, adjacent to the lake → also OK CHECK(ui::apply_tool(c, ui::Tool::Airport, 30, 30).ok); // airport: same flat-dry zone gate CHECK((c.xzon.at(30, 30) & 0x0F) == 8); // airport zone nibble c.xter.at(41, 59) = 1; // a sloped tile CHECK(!ui::apply_tool(c, ui::Tool::Seaport, 41, 59).ok); // zones need flat ground → rejected } // ---- A zoned port only develops when POWERED (the original gates non-military development on power) ---- { auto count_buildings = [](const City& c) { int n = 0; for (int i = 0; i < City::kTiles; ++i) if (c.xbld.cells[i] >= 221) ++n; return n; }; auto run = [&](City& c) { MsRand rng(0x1234); for (int pass = 0; pass < 300; ++pass) { std::array count{}; for (int i = 0; i < City::kTiles; ++i) ++count[c.xbld.cells[i]]; for (int r = 20; r < 23; ++r) for (int cc = 20; cc < 23; ++cc) sc2k::special_zone_develop(c, r, cc, 9, count, rng, 0); } }; City unpowered; // a flat-dry 3×3 seaport zone, no power for (int r = 20; r < 23; ++r) for (int cc = 20; cc < 23; ++cc) unpowered.xzon.at(r, cc) = 9; run(unpowered); CHECK(count_buildings(unpowered) == 0); // no power → never develops City powered = unpowered; for (int r = 20; r < 23; ++r) for (int cc = 20; cc < 23; ++cc) powered.xbit.at(r, cc) |= 0x40; // power the zone run(powered); CHECK(count_buildings(powered) > 0); // powered → develops its structures } if (failures == 0) { std::printf("building_tools: all checks passed\n"); return 0; } std::printf("building_tools: %d failure(s)\n", failures); return 1; }