// Arcologies (sim/arcologies.hpp): lazy record provisioning, the yearly growth microsim (feeder // pool / quality / tax mood / capacity + shimmer), the double Exodus gate (>300 Launch Arcos AND // >6M residents), and the launch — Launch Arcos razed to clean pads and $100k refunded apiece, // everything else left standing. Also the corner-bit footprint: two towers placed flush stay two // records. Real-save KAT at the end (skips without game data). #include "sc2k/sim/arcologies.hpp" #include "sc2k/sim/terrain_gen.hpp" #include "sc2k/io/sc2_file.hpp" #include "sc2k/ui/tools.hpp" // the shared destroyer's body (sim::demolish_at) #include #include "test_harness.hpp" #include "test_paths.hpp" using namespace sc2k; // Stamp a 4x4 arcology of `code` at (r,c) with the corner bits a real placement carries. static void place_arco(City& c, int r, int col, std::uint8_t code) { for (int dr = 0; dr < 4; ++dr) for (int dc = 0; dc < 4; ++dc) { c.xbld.at(r + dr, col + dc) = code; c.xzon.at(r + dr, col + dc) = 0; c.xbit.at(r + dr, col + dc) |= 0x50; // powered + watered (full quality) } c.xzon.at(r, col) = xzon::kCornerTL; c.xzon.at(r, col + 3) = xzon::kCornerTR; c.xzon.at(r + 3, col) = xzon::kCornerBL; c.xzon.at(r + 3, col + 3) = xzon::kCornerBR; } int main() { City c = make_new_city(0x5150u, 0, 1, 20, 20, 20, 1, 1); place_arco(c, 40, 40, xbld::kLaunchArco); place_arco(c, 40, 44, xbld::kLaunchArco); // flush against the first — must stay a SEPARATE tower place_arco(c, 60, 60, xbld::kPlymouthArco); c.set_misc_be32(sc2k::misc::kCityPopulation, 400000); // the feeder pool: towers recruit FROM the city for (int r = 0; r < 3; ++r) c.set_budget_rate(r, 0); // tax holiday → best mood // Year one: records provisioned lazily at the anchor corner, one per tower (the corner-bit // footprint keeps the flush pair apart), each seeded with capacity/founding year and a live // quality; populations start recruiting. MsRand rng(0x5150u); auto st = sim::arco_annual_tick(c, 1900, rng); CHECK(st.arcos == 3 && st.launchArcos == 2); CHECK(st.totalPop > 0); CHECK(c.xtxt.at(40, 40 + 3) >= 51 && c.xtxt.at(40, 44 + 3) >= 51); // the 0x80 corner carries the slot CHECK(c.xtxt.at(40, 40) != c.xtxt.at(40, 44)); // two towers, two records { const int slot = c.xtxt.at(40, 40); CHECK(sim::xmic_u16(c, slot - 51, 2) == 65); // capacity word, in thousands CHECK(sim::xmic_u16(c, slot - 51, 6) == 1900); // founding year const int q = c.xmic[std::size_t(slot - 51) * 8 + 1]; // quality: fields are all zero here, CHECK(q == 12); // powered + watered → (0-0-0)+12 = 12 } CHECK(c.misc_be32(sc2k::misc::kArcoPopulation) == st.totalPop); // the MISC mirror tracks the records // Populations climb year over year (12-quality towers at zero tax: intake 40·(5·(12+10)−50) is // plenty, so the binding caps are the feeder split and 100·capK) and settle at capacity, where // the 0..63 head-count shimmer keeps them. for (int y = 1901; y <= 1980; ++y) st = sim::arco_annual_tick(c, y, rng); CHECK(st.totalPop >= 2L * 65000 + 55000); CHECK(st.totalPop <= 2L * 65000 + 55000 + 3 * 63); for (int i = 1; i < 150; ++i) { // each record: capacity ≤ pop ≤ capacity+63 const std::uint8_t xb = c.xmic[std::size_t(i) * 8]; if (xb < xbld::kPlymouthArco || xb > xbld::kLaunchArco) continue; const int capK = sim::xmic_u16(c, i, 2); const int pop = sim::xmic_u16(c, i, 4); CHECK(pop >= 1000 * capK && pop <= 1000 * capK + 63); } CHECK(!sim::exodus_due(st)); // three towers are nowhere near the gate // Mood: heavy taxes + a bad neighbourhood DRAIN a tower. Cut power/water (quality 12 → 3) and // tax at 20/20/20 (lift 0): mood = 5·3 − 50 = −35 → intake −1400 a year, more than the 2% // compounding can replace at these populations. { for (int r = 0; r < 3; ++r) c.set_budget_rate(r, 20); for (int dr = 0; dr < 4; ++dr) for (int dc = 0; dc < 4; ++dc) c.xbit.at(40 + dr, 40 + dc) &= ~0x50; const int slot = c.xtxt.at(40, 43); const int before = sim::xmic_u16(c, slot - 51, 4); st = sim::arco_annual_tick(c, 1981, rng); CHECK(c.xmic[std::size_t(slot - 51) * 8 + 1] == 3); // quality re-read from the live tile CHECK(sim::xmic_u16(c, slot - 51, 4) < before); // the tower is emptying for (int r = 0; r < 3; ++r) c.set_budget_rate(r, 0); // restore for the launch below } // The Exodus gate needs BOTH bars: more than 300 Launch Arcos AND more than six million aboard. { sim::ArcoStatus full; full.totalPop = sim::kExodusPopulation + 1; full.launchArcos = sim::kExodusLaunchArcos + 1; CHECK(sim::exodus_due(full)); full.launchArcos = sim::kExodusLaunchArcos; // 300 exactly → not yet CHECK(!sim::exodus_due(full)); full.launchArcos = sim::kExodusLaunchArcos + 1; full.totalPop = sim::kExodusPopulation; // 6M exactly → not yet CHECK(!sim::exodus_due(full)); } // The launch: both Launch Arcos depart — razed, pads left CLEAN (deliberate deviation: the // original burns them), records freed, $100k apiece — and the Plymouth stays. const long fundsBefore = c.funds(); sim::EventBuffer exodusEvents; exodusEvents.begin_batch(300 * 1982); const auto res = sim::launch_arcos(c, &exodusEvents); CHECK(res.launched == 2); CHECK(exodusEvents.view().size() == 1); const auto* exodus = std::get_if(&exodusEvents.view()[0].payload); CHECK(exodus && exodus->launched == 2 && exodus->refund == 2 * sim::kLaunchRefund); CHECK(exodus && exodus->padRow == res.padR && exodus->padColumn == res.padC); CHECK(c.xbld.at(40, 40) != xbld::kLaunchArco && c.xbld.at(40, 44) != xbld::kLaunchArco); CHECK(c.xtxt.at(40, 40) == 0); // no fire, no stale label ref CHECK(c.xbld.at(60, 60) == xbld::kPlymouthArco); // Plymouth doesn't fly CHECK(c.funds() == fundsBefore + 2 * sim::kLaunchRefund); bool launchRecords = false; for (int i = 1; i < 150; ++i) if (c.xmic[std::size_t(i) * 8] == xbld::kLaunchArco) launchRecords = true; CHECK(!launchRecords); // the departed towers' records freed // Nothing left to launch — and the recount repair sums what remains (the Plymouth alone). exodusEvents.begin_batch(300 * 1983); CHECK(sim::launch_arcos(c, &exodusEvents).launched == 0); CHECK(exodusEvents.view().empty()); { const int slot = c.xtxt.at(60, 63); const long plymouth = sim::xmic_u16(c, slot - 51, 4); c.set_misc_be32(sc2k::misc::kArcoPopulation, 0); // simulate a pre-feature save's stale mirror CHECK(sim::recount_arco_population(c) == plymouth); CHECK(c.misc_be32(sc2k::misc::kArcoPopulation) == plymouth); } // Real-save KAT: the shipped mega-city's records already satisfy the model's fixed point — full // towers sit at capacity + 0..63 — and its MISC mirror equals the record sum. One tick must keep // every full tower inside [capacity − drain, capacity + 63] and re-sum the mirror exactly. if (exists(city_file("BIGGESTC.SC2"))) { City b; CHECK(io::load_sc2(city_file("BIGGESTC.SC2"), b).ok); CHECK(sim::recount_arco_population(b) == b.misc_be32(sc2k::misc::kArcoPopulation)); // real saves agree already int before[150] = {}; // pre-tick populations, per record for (int i = 1; i < 150; ++i) if (b.xmic[std::size_t(i) * 8] == xbld::kLaunchArco) before[i] = sim::xmic_u16(b, i, 4); MsRand kat(7); const auto sb = sim::arco_annual_tick(b, 2050, kat); CHECK(sb.totalPop == b.misc_be32(sc2k::misc::kArcoPopulation)); CHECK(!sim::exodus_due(sb)); // dozens of Launch Arcos, not 300 int fullChecked = 0; for (int i = 1; i < 150; ++i) { if (b.xmic[std::size_t(i) * 8] != xbld::kLaunchArco) continue; const int pop = sim::xmic_u16(b, i, 4); const int q = b.xmic[std::size_t(i) * 8 + 1]; CHECK(q >= 0 && q <= 12); // refreshed quality stays in band if (before[i] >= 65000) { // full before the tick (cap + shimmer) CHECK(pop >= 64300 && pop <= 65063); // stays at cap+shimmer, less a worst-case mood drain ++fullChecked; } } CHECK(fullChecked > 10); std::printf("arcologies: BIGGESTC KAT — %d full towers hold capacity+shimmer, mirror=%ld\n", fullChecked, sb.totalPop); } if (failures == 0) { std::printf("arcologies: all checks passed\n"); return 0; } std::printf("arcologies: %d FAILURES\n", failures); return 1; }