// The national economy (the front of the original's monthly demographic pass): the two-accumulator growth walk, // the health index, the 4-state trend, and the bond-rate drift. Verifies the seeds, one deterministic step, // long-run convergence, and that a live sim drifts the bond rate. #include "sc2k/sim/economy.hpp" #include "sc2k/sim/terrain_gen.hpp" #include "sc2k/sim/simulation.hpp" #include "sc2k/io/sc2_file.hpp" #include #include #include "test_paths.hpp" using namespace sc2k; #include "test_harness.hpp" int main() { // 1. New-city seeds (the original's new-game reset): A = era funds, B = 0.3·era funds, trend = difficulty−1, rate 3. { City easy = make_new_city(0xE1u, 1 /*1950 → era funds 25000*/, 1 /*easy*/, 20, 20, 20, 1, 1); EconomyState e = load_economy(easy); CHECK(e.baseA == 25000); CHECK(e.oscB == 7500); // 3·25000/10 CHECK(e.trend == 0); // easy → trend 0 CHECK(e.bondRate == 3); CHECK(load_economy(make_new_city(0xE1u, 1, 2, 20, 20, 20, 1, 1)).trend == 1); // medium → 1 CHECK(load_economy(make_new_city(0xE1u, 1, 3, 20, 20, 20, 1, 1)).trend == 2); // hard → 2 } // 2. One deterministic step: with trend 2, A grows (step = A·2·k) and B is frozen (coeff[2] = 0); // esi = 100·B/(A+1) computed from the post-walk state. { EconomyState e; e.baseA = 100000; e.oscB = 30000; e.trend = 2; e.bondRate = 3; MsRand rng(999); const int esi = economy_step(e, rng); CHECK(e.baseA > 100000); // A climbed CHECK(e.oscB == 30000); // B unchanged (boom/steady coeff for trend 2 is 0) CHECK(esi == static_cast(static_cast(e.oscB) / static_cast(e.baseA + 1) * 100.0)); std::printf(" step: A 100000->%ld, B=%ld, esi=%d\n", e.baseA, e.oscB, esi); } // 3. Long run (200 years): A climbs toward the 5,000,000 ceiling; trend and rate stay in valid bands. { EconomyState e = load_economy(make_new_city(0x7u, 2 /*2000*/, 2, 20, 20, 20, 1, 1)); MsRand rng(12345); int minRate = 99, maxRate = 0, sawTrend = 0; for (int m = 0; m < 12 * 200; ++m) { economy_step(e, rng); minRate = std::min(minRate, e.bondRate); maxRate = std::max(maxRate, e.bondRate); sawTrend |= (1 << (e.trend & 3)); CHECK(e.trend >= 0 && e.trend <= 3); CHECK(e.bondRate >= 1); } std::printf(" 200yr: A=%ld B=%ld trend=%s rate[%d..%d] trendsSeen=0x%x\n", e.baseA, e.oscB, economy_trend_name(e.trend), minRate, maxRate, sawTrend); CHECK(e.baseA > 1000000 && e.baseA < 6000000); // grew substantially toward the ceiling CHECK(maxRate > minRate); // the rate drifted over two centuries } // 4. Integration: a live sim (finance on) drifts the national bond rate over decades. { City c; if (io::load_sc2((city_file("TOKYO.SC2")), c).ok) { c.set_next_bond_rate(3); Simulation sim(c); sim.set_finance_enabled(true); const int r0 = c.next_bond_rate(); for (int i = 0; i < 25 * 12 * 80; ++i) sim.tick(); // 80 game years std::printf(" sim 80yr: bond rate %d -> %d, trend=%s, A=%ld\n", r0, c.next_bond_rate(), economy_trend_name(sim.economy().trend), sim.economy().baseA); CHECK(c.next_bond_rate() >= 1); CHECK(sim.economy().baseA > 60000); // A grew from the 2000-era seed } } if (failures == 0) { std::printf("economy: all checks passed\n"); return 0; } std::printf("economy: %d failure(s)\n", failures); return 1; }