// Integration: with growth ENABLED, ticking a real city evolves its zoned land (develops/abandons) // without corrupting it. Growth is RNG-driven (structural validation), so this checks the live wiring // is sound — change happens, building codes stay valid, the developed count stays sane — not bit-exact. #include "sc2k/sim/simulation.hpp" #include "sc2k/io/sc2_file.hpp" #include "sc2k/io/native_city_file.hpp" #include "sc2k/sim/growth.hpp" #include #include #include #include "test_paths.hpp" using namespace sc2k; #include "test_harness.hpp" static int developed(const City& c) { int n = 0; for (int t = 0; t < City::kTiles; ++t) if (c.xbld.cells[t] >= 112) ++n; // zoned-building codes return n; } int main() { City c; auto r = io::load_sc2((city_file("TOKYO.SC2")), c); if (!r.ok) { std::printf("sim_growth: skipped (%s)\n", r.error.c_str()); return 0; } const int dev0 = developed(c); Simulation sim(c); sim.set_growth_enabled(true); for (int i = 0; i < 25 * 12; ++i) sim.tick(); // a full game year // Diff against a pristine reload: how many XBLD tiles the year of growth changed. City fresh; CHECK(io::load_sc2((city_file("TOKYO.SC2")), fresh).ok); int changed = 0; for (int t = 0; t < City::kTiles; ++t) if (c.xbld.cells[t] != fresh.xbld.cells[t]) ++changed; const int dev1 = developed(c); const CommutePeriodDiagnostics& commute = sim.completed_commute_diagnostics(); const CommuteRouteHistory& routes = sim.visible_commute_routes(); long stopTotal = 0; for (long count : commute.stops) stopTotal += count; std::printf("sim_growth: TOKYO 1 year, developed %d -> %d, XBLD tiles changed=%d; " "commutes %ld/%ld max-work=%d modes=%ld/%ld/%ld/%ld\n", dev0, dev1, changed, commute.successes, commute.attempts, commute.maximumExpandedStates, commute.modeTrips[0], commute.modeTrips[1], commute.modeTrips[2], commute.modeTrips[3]); CHECK(changed > 0); // the scan actually did something CHECK(dev1 > dev0 / 2 && dev1 < City::kTiles); // didn't collapse or explode CHECK(sim.clock() == static_cast(fresh.days()) + 25 * 12); CHECK(commute.attempts > 0 && commute.successes <= commute.attempts); CHECK(stopTotal == commute.attempts); CHECK(commute.maximumExpandedStates <= 4096); CHECK(!routes.accepted().empty()); CHECK(routes.accepted().size() <= CommuteRouteHistory::kMaximumAcceptedRoutes); CHECK(routes.failed().size() <= CommuteRouteHistory::kMaximumFailedRoutes); CHECK(!routes.accepted().front().points.empty()); // Optional local regression for the city reported during route playtesting. Establish a complete // post-load census, then run ten years with the same live systems as the app. The reported failure // halves population and leaves widespread ruins within a few years; this fixture must not do so. const std::string capePath = city_file("CapeQuest.lcity"); if (exists(capePath)) { auto loaded = io::load_native_city_file(capePath); CHECK(loaded.ok()); if (loaded.ok()) { io::NativeCityDocument document = std::move(loaded.value); City cape = document.city; sim::MunicipalDebt debt = document.municipalDebt.value_or(sim::MunicipalDebt{}); sim::OperatingBudgetState operatingBudget = document.operatingBudget; Simulation capeSimulation(cape, debt, operatingBudget); capeSimulation.set_growth_enabled(true); capeSimulation.set_finance_enabled(true); capeSimulation.set_demand_enabled(true); capeSimulation.set_tech_enabled(true); capeSimulation.set_terrain_model(document.active_terrain()); if (document.regionalState) capeSimulation.bind_regional_utilities( &*document.regionalState, &document.utilityContracts); for (int tick = 0; tick < 50; ++tick) capeSimulation.tick(); const long baselinePopulation = 10L * cape.zone_pop(0); long minimumPopulation = baselinePopulation; int maximumAbandoned = 0; for (int month = 0; month < 120; ++month) { for (int tick = 0; tick < 25; ++tick) capeSimulation.tick(); minimumPopulation = std::min(minimumPopulation, 10L * cape.zone_pop(0)); int abandoned = 0; for (std::uint8_t code : cape.xbld.cells) if (is_abandoned_code(code)) ++abandoned; maximumAbandoned = std::max(maximumAbandoned, abandoned); } std::printf("sim_growth: coastal native 10 years, population %ld min=%ld final=%ld, max abandoned=%d\n", baselinePopulation, minimumPopulation, 10L * cape.zone_pop(0), maximumAbandoned); CHECK(baselinePopulation > 0); CHECK(minimumPopulation > baselinePopulation / 2); } } if (failures == 0) { std::printf("sim_growth: all checks passed\n"); return 0; } std::printf("sim_growth: %d failure(s)\n", failures); return 1; }