// Validates the power flood-fill against saved cities, byte-for-byte. // // update_power recomputes XBIT 0x40 (powered) from the plants + conductive grid. For a city // with adequate generation, every plant-connected conductive tile is powered, and the result // must equal the powered bits the original game saved. We clear and recompute, then compare to // the snapshot — all 16384 tiles must agree. #include "sc2k/sim/power.hpp" #include "sc2k/io/sc2_file.hpp" #include #include #include #include "test_paths.hpp" #include "test_harness.hpp" static bool check_city(const char* name) { using namespace sc2k; std::string path = (cities_dir()) + name; City c; auto r = io::load_sc2(path, c); if (!r.ok) { std::printf(" %-13s skipped (%s)\n", name, r.error.c_str()); return true; } // snapshot the powered bits the game saved std::vector saved(City::kTiles); for (int t = 0; t < City::kTiles; ++t) saved[t] = (c.xbit.cells[t] & 0x40) != 0; update_power(c, nullptr, /*diagonal=*/false); // faithful 4-connected — byte-exact vs the original's saved XBIT int mismatches = 0, savedOn = 0, mineOn = 0; for (int t = 0; t < City::kTiles; ++t) { bool mine = (c.xbit.cells[t] & 0x40) != 0; if (mine != saved[t]) ++mismatches; if (saved[t]) ++savedOn; if (mine) ++mineOn; } std::printf(" %-13s saved_powered=%d recomputed=%d mismatches=%d\n", name, savedOn, mineOn, mismatches); CHECK(mismatches == 0); return mismatches == 0; } int main() { using namespace sc2k; const char* cities[] = {"BIGGESTC.SC2", "TOKYO.SC2", "PARIS.SC2", "LONDON.SC2", "NYC.SC2"}; for (const char* n : cities) check_city(n); // A freshly-placed / isolated WIND turbine (0xC8) must power its OWN tile on the edit-path recompute // (rng == nullptr) — else it flashes the "no power" bolt (user-reported 2026-07-01). Its deterministic // floor is (ALTM&0x1F)/2, the wind formula's rand()=0 minimum, so a turbine on real terrain self-powers. { City c; c.set_funds(0); const int wr = 40, wc = 40; c.xbld.at(wr, wc) = 0xC8; // a lone wind turbine c.xbit.at(wr, wc) = 0x80; // conductive (place_power_plant stamps this) c.altm.at(wr, wc) = 10; // altitude 10 (as in the report) → floor 5 update_power(c, nullptr, /*diagonal=*/false); // edit-path recompute: no RNG (lone tile — connectivity moot) CHECK((c.xbit.at(wr, wc) & 0x40) != 0); // powered → NO badge CHECK((c.xbit.at(wr, wc) & 0xC0) != 0x80); // not the (conductive & !powered) badge state // At altitude 0 the floor is genuinely 0 (faithful — the formula's true minimum), so the badge there // is not a bug; confirm the fix is altitude-scaled, not a blanket "always ≥1". City c0; c0.xbld.at(wr, wc) = 0xC8; c0.xbit.at(wr, wc) = 0x80; c0.altm.at(wr, wc) = 0; update_power(c0, nullptr, /*diagonal=*/false); CHECK((c0.xbit.at(wr, wc) & 0x40) == 0); // altitude-0 turbine at rand()=0 makes 0 — genuine } // DEVIATION (user-requested): 8-connected flood — a plant powers a conductive tile touching it only at a // CORNER. Faithful (4-connected) leaves it dark; diagonal powers it. (update_power's `diagonal` param.) { City c; c.xbld.at(10, 10) = 0xCB; c.xbit.at(10, 10) = 0x80; // a nuclear plant (ample supply), conductive c.xbld.at(11, 11) = 0x80; c.xbit.at(11, 11) = 0x80; // a building touching it ONLY at the SE corner City o = c; update_power(o, nullptr, /*diagonal=*/false); // faithful: the corner building stays DARK CHECK((o.xbit.at(11, 11) & 0x40) == 0); update_power(c, nullptr, /*diagonal=*/true); // deviation: the corner building is POWERED CHECK((c.xbit.at(11, 11) & 0x40) != 0); } if (failures == 0) { std::printf("power: all checks passed\n"); return 0; } std::printf("power: %d failure(s)\n", failures); return 1; }