// Numeric tests for SimNation edge neighbours (sim/neighbors.hpp) — the data model behind the map-edge // "connection to your neighbor" prompt + signposts. Pins: the name table (RT_STRING 548..583), the MISC // dword offsets (438+4·i seed / 439+4·i pop) verified byte-exact against a real city (BABAR), the edge→slot // cascade, the signpost text format, and the new-city Ocean/land generator. #include "sc2k/sim/neighbors.hpp" #include "sc2k/sim/simulation.hpp" // the tick-21 wiring check (Part 8) #include "sc2k/sim/terrain_gen.hpp" #include "sc2k/io/sc2_file.hpp" #include "sc2k/render/asset_loader.hpp" #include #include #include using namespace sc2k; using namespace sc2k::sim; #include "test_harness.hpp" #include "test_paths.hpp" int main() { // ── Part 1: seed → name (verbatim RT_STRING 548..583, seed s → 547+s). Ocean for 0 / out of range. ── CHECK(std::string(neighbor_name_for_seed(1)) == "Oak Creek"); CHECK(std::string(neighbor_name_for_seed(17)) == "Jeromi"); CHECK(std::string(neighbor_name_for_seed(36)) == "Lister"); CHECK(std::string(neighbor_name_for_seed(0)) == "Ocean"); CHECK(std::string(neighbor_name_for_seed(37)) == "Ocean"); // ── Part 2: physical edge→legacy slot cascade; interior −1. ── CHECK(edge_slot(127, 60) == 0); // row max CHECK(edge_slot(60, 127) == 1); // column max CHECK(edge_slot(0, 60) == 2); // row 0 CHECK(edge_slot(60, 0) == 3); // column 0 CHECK(edge_slot(0, 0) == 2); // row-0 priority at the corner CHECK(edge_slot(127, 127) == 1); // column-max priority at the corner CHECK(edge_slot(64, 64) == -1); // interior → no edge // ── Part 3: MISC round-trips the seed/pop; sign text = " ". ── { City c; // zeroed MISC → all Ocean for (int i = 0; i < kNeighborCount; ++i) { CHECK(neighbor_at(c, i).seed == 0); } c.set_misc_be32(neighbor_seed_offset(1), 5); // east neighbour = seed 5 "Mill Valley" c.set_misc_be32(neighbor_pop_offset(1), 4242); CHECK(neighbor_at(c, 1).seed == 5); CHECK(neighbor_at(c, 1).population == 4242); CHECK(neighbor_name(c, 1) == "Mill Valley"); // A marker on the column-max edge at row 62 → pos = 127-62 = 65, digit = '5'+(65%5)='5'. const std::string t = neighbor_sign_text(c, 62, 127); CHECK(t == "Mill Valley 5"); CHECK(neighbor_sign_text(c, 64, 64).empty()); // interior tile has no neighbour sign } // ── Part 4: verified against the real BABAR.SC2 (Jeromi / Sinistrel / Avon / Pioneers). ── { const std::string p = city_file("BABAR.SC2"); if (std::filesystem::exists(p)) { City c; if (io::load_sc2(p, c).ok) { CHECK(neighbor_at(c, 0).seed == 17); CHECK(neighbor_name(c, 0) == "Jeromi"); CHECK(neighbor_at(c, 1).seed == 26); CHECK(neighbor_name(c, 1) == "Sinistrel"); CHECK(neighbor_at(c, 2).seed == 24); CHECK(neighbor_name(c, 2) == "Avon"); CHECK(neighbor_at(c, 3).seed == 14); CHECK(neighbor_name(c, 3) == "Pioneers"); CHECK(neighbor_at(c, 0).population == 1166); } else std::printf(" (BABAR present but failed to load — skipping Part 4)\n"); } else std::printf(" (no BABAR.SC2 — skipping Part 4)\n"); } // ── Part 5: new-city generator — an all-water edge is Ocean, a land edge gets a real name (1..36). ── { City c; // all tiles dry (XBIT 0) … for (int t = 0; t < City::kDim; ++t) c.xbit.at(0, t) |= 0x04; // … except a fully-watered row-0 edge MsRand rng(12345); generate_neighbors(c, rng); CHECK(neighbor_at(c, 2).seed == 0); // row-0 edge (slot 2) → Ocean CHECK(neighbor_name(c, 2) == "Ocean"); for (int s : {0, 1, 3}) { // the three land edges → a named neighbour const int seed = neighbor_at(c, s).seed; CHECK(seed >= 1 && seed <= 36); CHECK(neighbor_at(c, s).population > 0); } } // ── Part 6: stale-save repair — a city whose seeds are ALL zero (pre-feature / uninitialised) but which // has land edges is repaired on load; a correctly-seeded city is left untouched. ── { City c = make_new_city(0x12345678u, 2, 1, 20, 20, 20, 1, 1); // 1 ocean edge + 3 named (verified above) CHECK(!neighbors_uninitialized(c)); // freshly generated → already seeded CHECK(!repair_neighbors_if_uninitialized(c)); // …so repair is a no-op // Simulate a pre-feature save: wipe every seed to 0. Now it looks "all Ocean" despite land edges. int oceanBefore = 0; for (int i = 0; i < kNeighborCount; ++i) { c.set_misc_be32(neighbor_seed_offset(i), 0); c.set_misc_be32(neighbor_pop_offset(i), 0); } CHECK(neighbors_uninitialized(c)); CHECK(repair_neighbors_if_uninitialized(c)); // repair kicks in for (int i = 0; i < kNeighborCount; ++i) { // seeds now match the terrain again const bool water = edge_is_ocean(c, i); const int seed = neighbor_at(c, i).seed; CHECK(water ? (seed == 0) : (seed >= 1 && seed <= 36)); if (!water) ++oceanBefore; // (reusing the counter: count NAMED edges) } CHECK(oceanBefore == 3); // 3 land edges got real names, not "Ocean" // Deterministic: repairing the same terrain twice gives identical names. City d = make_new_city(0x12345678u, 2, 1, 20, 20, 20, 1, 1); for (int i = 0; i < kNeighborCount; ++i) { d.set_misc_be32(neighbor_seed_offset(i), 0); d.set_misc_be32(neighbor_pop_offset(i), 0); } repair_neighbors_if_uninitialized(d); for (int i = 0; i < kNeighborCount; ++i) CHECK(neighbor_at(c, i).seed == neighbor_at(d, i).seed); } // ── Part 7: the monthly SimNation tick — neighbours grow toward the 5M ceiling, hamlets creep, // oceans stay empty, values drift by the fed-rate/wealth balance, and bad years happen. ── { City c; c.set_misc_be32(neighbor_seed_offset(0), 3); c.set_misc_be32(neighbor_pop_offset(0), 120000); // a mid-size neighbour c.set_misc_be32(neighbor_value_offset(0), 60); // tiny value (like the shipped saves') c.set_misc_be32(neighbor_seed_offset(1), 7); c.set_misc_be32(neighbor_pop_offset(1), 6000000); // already past the ceiling c.set_misc_be32(neighbor_seed_offset(2), 9); c.set_misc_be32(neighbor_pop_offset(2), 40); // a hamlet: the step truncates to 0 MsRand rng(7); neighbors_monthly_tick(c, /*trend=*/1, /*fedRate=*/5, rng); // one month first: the hamlet's const long hamlet1 = c.misc_be32(neighbor_pop_offset(2)); // step truncates to 0, so it moves CHECK(hamlet1 == 40 || hamlet1 == 41); // by the coin-flip alone for (int m = 1; m < 12; ++m) neighbors_monthly_tick(c, /*trend=*/1, /*fedRate=*/5, rng); CHECK(c.misc_be32(neighbor_pop_offset(0)) > 120000); // a year of (1 + 0..2)/1200 months CHECK(c.misc_be32(neighbor_pop_offset(1)) < 6000000); // above the ceiling → declining back CHECK(c.misc_be32(neighbor_pop_offset(2)) >= 30); // coin-flips add; one ¾ bad year allowed CHECK(c.misc_be32(neighbor_pop_offset(3)) == 0); // the ocean edge stays empty // Long run: an over-the-ceiling town settles into a band around five million (like LONDON's // shipped neighbours), and a zero value stays exactly zero. for (int m = 0; m < 600; ++m) neighbors_monthly_tick(c, 1, 5, rng); const long settled = c.misc_be32(neighbor_pop_offset(1)); CHECK(settled > 4000000 && settled < 5600000); CHECK(c.misc_be32(neighbor_value_offset(3)) == 0); // Values: a low fed rate grows them, a punitive one shrinks them (drift = 0..4 + coeff − fed − grade). City v; v.set_misc_be32(neighbor_seed_offset(0), 1); v.set_misc_be32(neighbor_pop_offset(0), 1000000); v.set_misc_be32(neighbor_value_offset(0), 100000); City w = v; MsRand r1(3), r2(3); for (int m = 0; m < 240; ++m) neighbors_monthly_tick(v, /*trend=*/0, /*fedRate=*/1, r1); // boom, cheap money for (int m = 0; m < 240; ++m) neighbors_monthly_tick(w, /*trend=*/3, /*fedRate=*/12, r2); // slump, dear money CHECK(v.misc_be32(neighbor_value_offset(0)) > 100000); CHECK(w.misc_be32(neighbor_value_offset(0)) < 100000); // The ~1-in-64 bad year: over a long stretch some month must chop a town by a quarter. City s; for (int i = 0; i < kNeighborCount; ++i) { s.set_misc_be32(neighbor_seed_offset(i), i + 1); s.set_misc_be32(neighbor_pop_offset(i), 4000000); } MsRand r3(99); bool sawBadYear = false; long prev[4] = {4000000, 4000000, 4000000, 4000000}; for (int m = 0; m < 640 && !sawBadYear; ++m) { neighbors_monthly_tick(s, 0, 5, r3); for (int i = 0; i < 4; ++i) { const long p = s.misc_be32(neighbor_pop_offset(i)); if (p < prev[i] - prev[i] / 5) sawBadYear = true; // a >20% single-month drop = the setback prev[i] = p; } } CHECK(sawBadYear); } // ── Part 8: LONDON KAT — the shipped neighbours ride the five-million ceiling; a monthly tick moves // them without knocking them out of the band, and their zero values stay zero. ── if (exists(city_file("LONDON.SC2"))) { City c; CHECK(io::load_sc2(city_file("LONDON.SC2"), c).ok); MsRand rng(1); const long before = c.misc_be32(neighbor_pop_offset(0)); CHECK(before > 3000000); // the shipped save's giants for (int m = 0; m < 12; ++m) neighbors_monthly_tick(c, 2, 7, rng); // its own trend/fed rate for (int i = 0; i < kNeighborCount; ++i) { const long p = c.misc_be32(neighbor_pop_offset(i)); CHECK(p > 3000000 && p < 5600000); // still in the ceiling band CHECK(c.misc_be32(neighbor_value_offset(i)) == 0); // 0 · anything = 0, forever } CHECK(c.misc_be32(neighbor_pop_offset(0)) != before); // …but the figures MOVE now std::printf(" LONDON: neighbours live at the 5M ceiling and tick monthly\n"); // Wiring: the full Simulation's monthly pass (tick 21, finance on) drives the same growth — // the SimNation window's MISC figures change without anyone calling the tick by hand. City w; CHECK(io::load_sc2(city_file("LONDON.SC2"), w).ok); const long wired = w.misc_be32(neighbor_pop_offset(0)); Simulation s(w); s.set_finance_enabled(true); for (int i = 0; i < 25 * 2; ++i) s.tick(); // two game months CHECK(w.misc_be32(neighbor_pop_offset(0)) != wired); std::printf(" LONDON: tick-21 wiring moves the neighbours through Simulation\n"); } if (failures) { std::printf("test_neighbors: %d FAILED\n", failures); return 1; } std::printf("test_neighbors: all passed\n"); return 0; }