// Random-disaster scheduler (sim/disaster_sched.hpp) — the monthly roll ported from // the original's monthly disaster roll. Verifies the verifiable, faithful parts numerically: the // difficulty→interval table, the building-presence gate, the "No Disasters" flag, the city-size floor, and // that disasters DO occur over time in a qualifying city (instead of only via the menu). #include "sc2k/sim/disaster_sched.hpp" #include "sc2k/city.hpp" #include "sc2k/rng.hpp" #include using namespace sc2k; using namespace sc2k::sim; #include "test_harness.hpp" // A developed city: difficulty, zone_pop(0) (population = 10×), funds, and a downtown to burn/raze. static City make_city(int difficulty, std::int32_t zonePop0, std::int32_t funds) { City c; c.set_misc_be32(sc2k::misc::kCityClass, difficulty + 1); // MISC 0x1C = 1-based class (Easy 1/Med 2/Hard 3) c.set_misc_be32(sc2k::misc::kZonePops, zonePop0); // zone_pop(0); dwCityPopulation = 10·this c.set_funds(funds); for (int r = 30; r < 100; ++r) for (int cc = 30; cc < 100; ++cc) c.xbld.at(r, cc) = std::uint8_t(0x70 + ((r * 3 + cc * 5) & 0x1F)); // varied downtown return c; } // Run `n` monthly rolls on `c`; clear any effect tiles between months (a real disaster resolves before the // next). Returns how many months started a disaster. static int run_months(City& c, bool enabled, int n, std::uint32_t seed) { MsRand rng(seed); int fired = 0; for (int i = 0; i < n; ++i) { for (int r = 0; r < City::kDim; ++r) for (int cc = 0; cc < City::kDim; ++cc) if (fx::is_effect(c.xtxt.at(r, cc))) c.xtxt.at(r, cc) = 0; DisasterSim d; if (maybe_start_disaster(c, d, rng, enabled)) ++fired; } return fired; } int main() { // ---- difficulty → mean-interval table ---- CHECK(disaster_interval(0) == 100); // easy — rarest CHECK(disaster_interval(1) == 60); // medium CHECK(disaster_interval(2) == 30); // hard — most frequent // ---- building-presence gate ---- { City c; c.xbld.at(10, 10) = 0xCB; // a nuclear plant CHECK(city_has_building(c, 0xCB)); CHECK(!city_has_building(c, 0xCD)); } // ---- "No Disasters": disabled ⇒ NEVER fires, even over 2000 months in a big hard-mode city ---- { City c = make_city(2, 5000, 50000); CHECK(run_months(c, false, 2000, 0xD15A5) == 0); } // ---- city-size floor: a tiny city (pop = 10·8 = 80, /25 = 3 < 30) is safe ---- { City c = make_city(2, 8, 50000); CHECK(run_months(c, true, 3000, 0x5EED) == 0); } // ---- a qualifying city DOES get disasters over time (the occurrence the port was missing) ---- { City c = make_city(2, 5000, 50000); const int fired = run_months(c, true, 3000, 0xBEEF); CHECK(fired > 0); CHECK(fired < 3000); // and NOT every month — the frequency gate works std::printf(" hard/big city: %d disasters over 3000 months (~1 per %d)\n", fired, fired ? 3000 / fired : 0); } // ---- frequency scales with difficulty: hard fires more often than easy (same city size/seed) ---- { City easy = make_city(0, 8000, 90000), hard = make_city(2, 8000, 90000); const int fe = run_months(easy, true, 4000, 0xC0FFEE); const int fh = run_months(hard, true, 4000, 0xC0FFEE); CHECK(fh > fe); // hard (interval 30) > easy (interval 100) std::printf(" easy=%d vs hard=%d disasters over 4000 months\n", fe, fh); } if (failures == 0) { std::printf("disaster_sched: all checks passed\n"); return 0; } std::printf("disaster_sched: %d failure(s)\n", failures); return 1; }