// Validates the water flood-fill against saved cities, byte-for-byte. // // update_water recomputes XBIT 0x10 (watered) from the pumps + pipe network + supply model. // Water is genuinely supply-limited, so this also exercises the brownout cutoff on // supply-constrained cities (BAYVIEW). The recomputed bits must equal the saved ones. // // PARIS is a special, instructive case: with its *saved* rainfall (27) the recompute differs by // exactly 4 tiles, all in supply-limited pump networks. The faithful pump base is // `5*waterLevel + rainfall>>1`; PARIS needs `rainfall>>1 == 12` (rain 24/25), so the saved water // bits were computed with a slightly lower rainfall than the saved weather field — water is // recomputed every tick / on placement, so the bits lag the weather field. The test below // confirms PARIS becomes byte-exact once that rainfall is supplied, isolating the lag from the // formula (which is faithful). #include "sc2k/sim/water.hpp" #include "sc2k/io/sc2_file.hpp" #include #include #include "test_paths.hpp" #include "test_harness.hpp" // Returns the number of mismatching tiles (or -1 if the city couldn't be loaded). static int water_mismatches(const char* name, int rainOverride, bool logDiffs) { 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 -1; } std::vector saved(City::kTiles); for (int t = 0; t < City::kTiles; ++t) saved[t] = (c.xbit.cells[t] & 0x10) != 0; update_water(c, rainOverride); int mismatches = 0, savedOn = 0, mineOn = 0; for (int t = 0; t < City::kTiles; ++t) { bool mine = (c.xbit.cells[t] & 0x10) != 0; if (mine != saved[t]) ++mismatches; if (saved[t]) ++savedOn; if (mine) ++mineOn; } std::printf(" %-13s%s saved_watered=%d recomputed=%d mismatches=%d\n", name, rainOverride >= 0 ? " (rain*)" : " ", savedOn, mineOn, mismatches); (void)logDiffs; return mismatches; } int main() { using namespace sc2k; // Cities whose saved water bits the faithful recompute reproduces exactly. const char* exact[] = {"BIGGESTC.SC2", "TOKYO.SC2", "LONDON.SC2", "NYC.SC2", "BAYVIEW.SC2"}; for (const char* n : exact) { int m = water_mismatches(n, -1, false); if (m >= 0) CHECK(m == 0); } // PARIS with its saved rainfall: a documented ~4-tile lag (see header). int parisSaved = water_mismatches("PARIS.SC2", -1, true); if (parisSaved >= 0) { // Confirm the discrepancy is the rainfall value, not the algorithm: with the rainfall the // water bits were actually computed with (rain>>1 == 12), PARIS is byte-exact. int p24 = water_mismatches("PARIS.SC2", 24, false); int p25 = water_mismatches("PARIS.SC2", 25, false); CHECK(p24 == 0 || p25 == 0); // the faithful formula reproduces PARIS exactly at the right rainfall } // Native plumbing: only source-connected mains radiate coverage, using the observed seven-cell // square radius. A disconnected main inside an already watered area cannot relay that water. { City city; sim::initialize_modern_water_network(city); city.xbld.at(20, 20) = xbld::kWaterPump; city.xbit.at(20, 20) |= xbit::kPowered; city.xbit.at(19, 19) |= xbit::kWater; // ten units of pump supply sim::set_water_main(city, 20, 20, true); city.xbld.at(27, 27) = xbld::kHomesFirst; // Chebyshev distance 7 city.xbld.at(28, 20) = xbld::kHomesFirst; // axial distance 8 sim::set_water_main(city, 20, 28, true); // disconnected despite lying near watered ground city.xbld.at(20, 35) = xbld::kHomesFirst; // only the disconnected main could reach this update_water(city); CHECK(city.xbit.at(27, 27) & xbit::kWatered); CHECK(!(city.xbit.at(28, 20) & xbit::kWatered)); CHECK(!(city.xbit.at(20, 28) & xbit::kWatered)); CHECK(!(city.xbit.at(20, 35) & xbit::kWatered)); } // Imported cities retain their exact hidden building-to-building water links. The same distant // demand is served through those links in the compatibility profile, but not by a native city's // radial model when its only visible main is at the source. { City imported; imported.waterDistributionModel = WaterDistributionModel::ClassicLinks; sim::initialize_modern_water_network(imported); imported.set_misc_be32(misc::kWeatherRain, 100); // ample supply for this small network imported.xbld.at(20, 20) = xbld::kWaterPump; imported.xbit.at(20, 20) |= xbit::kPowered; sim::set_water_main(imported, 20, 20, true); for (int column = 21; column <= 40; ++column) { imported.xbld.at(20, column) = xbld::kHomesFirst; imported.legacyWaterLinks.at(20, column) = 1; sim::sync_water_pipe_bit(imported, imported.index(20, column)); } update_water(imported); CHECK(imported.xbit.at(20, 40) & xbit::kWatered); // twenty tiles from the visible main City native = imported; native.waterDistributionModel = WaterDistributionModel::RadialMains; for (int index = 0; index < native.tile_count(); ++index) sim::sync_water_pipe_bit(native, static_cast(index)); update_water(native); CHECK(!(native.xbit.at(20, 40) & xbit::kWatered)); } // Capacity-limited native plumbing serves demand in hydraulic-distance order, not map-storage // order. This main runs west from its source; ten units must reach the ten closest homes even // though the farther homes have smaller column indices and therefore appear first row-major. { City city; sim::initialize_modern_water_network(city); city.xbld.at(60, 60) = xbld::kWaterPump; city.xbit.at(60, 60) |= xbit::kPowered; city.xbit.at(61, 61) |= xbit::kWater; // exactly ten units of supply for (int column = 30; column <= 60; ++column) sim::set_water_main(city, 60, column, true); for (int column = 31; column < 60; ++column) city.xbld.at(59, column) = xbld::kHomesFirst; update_water(city); for (int column = 31; column < 50; ++column) CHECK(!(city.xbit.at(59, column) & xbit::kWatered)); for (int column = 50; column < 60; ++column) CHECK(city.xbit.at(59, column) & xbit::kWatered); } // Equal hydraulic distances use row-major coordinates as the final stable tie-breaker. { City city; sim::initialize_modern_water_network(city); city.set_misc_be32(misc::kWeatherRain, 2); // one unit of pump supply city.xbld.at(40, 40) = xbld::kWaterPump; city.xbit.at(40, 40) |= xbit::kPowered; sim::set_water_main(city, 40, 40, true); city.xbld.at(39, 40) = xbld::kHomesFirst; city.xbld.at(40, 39) = xbld::kHomesFirst; update_water(city); CHECK(city.xbit.at(39, 40) & xbit::kWatered); CHECK(!(city.xbit.at(40, 39) & xbit::kWatered)); } // A building directly above a main still consumes capacity; main ownership is not free service. { City city; sim::initialize_modern_water_network(city); city.set_misc_be32(misc::kWeatherRain, 2); city.xbld.at(70, 70) = xbld::kWaterPump; city.xbit.at(70, 70) |= xbit::kPowered; for (int column = 68; column <= 70; ++column) sim::set_water_main(city, 70, column, true); city.xbld.at(70, 69) = xbld::kHomesFirst; city.xbld.at(70, 68) = xbld::kHomesFirst; update_water(city); CHECK(city.xbit.at(70, 69) & xbit::kWatered); CHECK(!(city.xbit.at(70, 68) & xbit::kWatered)); } if (failures == 0) { std::printf("water: all checks passed\n"); return 0; } std::printf("water: %d failure(s)\n", failures); return 1; }