// Select Power Plant data KAT (ui/power_plants.hpp): the 9 plants' MW/cost, recovered from the // original game, match its canonical values exactly. #include "sc2k/ui/power_plants.hpp" #include "sc2k/ui/tools.hpp" #include "sc2k/sim/ordinances.hpp" // ORD_NUCLEAR_FREE / ORD_ANTI_DRUG bit constants #include "sc2k/city.hpp" #include #include #include "test_harness.hpp" int main() { using sc2k::ui::power_plant; CHECK(sc2k::ui::power_plant_count() == 9); CHECK(std::strcmp(power_plant(0).name, "plant.coal") == 0 && power_plant(0).mw == 200 && power_plant(0).cost == 4000); CHECK(std::strcmp(power_plant(4).name, "plant.nuclear") == 0 && power_plant(4).mw == 500 && power_plant(4).cost == 15000); CHECK(std::strcmp(power_plant(5).name, "plant.wind") == 0 && power_plant(5).mw == 4 && power_plant(5).cost == 100); CHECK(std::strcmp(power_plant(8).name, "plant.fusion") == 0 && power_plant(8).mw == 2500 && power_plant(8).cost == 40000); // Placement plops the SELECTED type's own XBLD code. The old code always wrote 0xC9, so a 0xCB nuclear / // 0xCD microwave tile could NEVER exist — which is why the meltdown/microwave disasters never found one. { using namespace sc2k; City c; CHECK(ui::detail::place_power_plant(c, 10, 10, 4)); // Nuclear (dialog index 4) CHECK(c.xbld.at(10, 10) == 0xCB && c.xbld.at(13, 13) == 0xCB); // a real 4×4 nuclear plant now exists City g; CHECK(ui::detail::place_power_plant(g, 10, 10, 3)); // Natural Gas → 0xC9 CHECK(g.xbld.at(10, 10) == 0xC9); City m; CHECK(ui::detail::place_power_plant(m, 10, 10, 7)); // Microwave → 0xCD CHECK(m.xbld.at(10, 10) == 0xCD); // Nuclear Free Zone ordinance (bit 17, 0x20000) prohibits building the nuclear plant. (bit 9 — the // value the old code wrongly tested — is Anti-Drug Campaign, and must NOT block a nuclear plant.) City n; n.set_ordinances(1u << ORD_NUCLEAR_FREE); CHECK(!ui::detail::place_power_plant(n, 10, 10, 4)); // nuclear blocked CHECK(n.xbld.at(10, 10) == 0); CHECK(ui::detail::place_power_plant(n, 10, 10, 3)); // gas still allowed under the ordinance City ad; ad.set_ordinances(1u << ORD_ANTI_DRUG); // bit 9 ≠ nuclear-free CHECK(ui::detail::place_power_plant(ad, 10, 10, 4)); // nuclear NOT blocked by Anti-Drug std::printf(" placement: nuclear=0xCB / gas=0xC9 / microwave=0xCD; Nuclear Free Zone blocks nuclear\n"); } // Cross-compat: a placed plant carries the XZON building CORNER BITS (+ XBIT 0xE0) like real saved cities — // WITHOUT them the real game renders the plant as a BLANK space (no building anchor). User-reported. { using namespace sc2k; City c; CHECK(ui::detail::place_power_plant(c, 20, 20, 0)); // 4×4 coal CHECK((c.xzon.at(20, 20) & 0xF0) == 0x10); // TL CHECK((c.xzon.at(23, 20) & 0xF0) == 0x20); // BL CHECK((c.xzon.at(23, 23) & 0xF0) == 0x40); // BR CHECK((c.xzon.at(20, 23) & 0xF0) == 0x80); // TR CHECK((c.xzon.at(21, 21) & 0xF0) == 0x00); // interior tile: no corner bits CHECK((c.xbit.at(20, 20) & 0xE0) == 0xE0); // conductive|powered|demands City h; CHECK(ui::detail::place_power_plant(h, 30, 30, 1)); // 1×1 hydro CHECK((h.xzon.at(30, 30) & 0xF0) == 0xF0); // all four corner bits on the one tile // repair_building_corners re-stamps a plant saved WITHOUT corner bits (early port save), idempotently. City s; ui::detail::place_power_plant(s, 50, 50, 0); for (int r = 50; r < 54; r++) for (int cc = 50; cc < 54; cc++) s.xzon.at(r, cc) = std::uint8_t(s.xzon.at(r, cc) & 0x0F); // strip CHECK((s.xzon.at(50, 50) & 0xF0) == 0x00); CHECK(ui::detail::repair_building_corners(s) == 1); // one footprint fixed CHECK((s.xzon.at(50, 50) & 0xF0) == 0x10 && (s.xzon.at(53, 53) & 0xF0) == 0x40); CHECK(ui::detail::repair_building_corners(s) == 0); // already stamped → no-op std::printf(" cross-compat: XZON corner bits stamped (4×4 + 1×1) + repaired on load\n"); } if (failures == 0) { std::printf("powerplants: 9 MW/cost + placement codes + nuclear-free-zone verified\n"); return 0; } std::printf("powerplants: %d failure(s)\n", failures); return 1; }