Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290// Structural validation of the per-tile growth step (the RCI branch of the original's growth tick).// RNG-driven, so we assert behaviour over many seeds rather than bit-exact: a powered tile with a job// route and strong demand grows; an isolated developed tile declines; an unreachable tile does neither.#include "sc2k/sim/growth_scan.hpp"#include "sc2k/sim/region.hpp"#include <array>#include <cstdio>
#include "test_harness.hpp"
namespace {// An empty R zone at (50,50), powered, with a road leading to a C job at (50,56).sc2k::City connected_city() { using namespace sc2k; City c; c.xzon.cells[50 * 128 + 50] = 0x01; // empty R zone (nibble 1) c.xbit.cells[50 * 128 + 50] = 0x40; // powered for (int x = 51; x <= 55; ++x) c.xbld.cells[50 * 128 + x] = 0x1D; // a road run c.xzon.cells[50 * 128 + 56] = 0x03; // a C job past the road return c;}} // namespace
int main() { using namespace sc2k;
// The live growth gate consumes the same typed-border route result and period diagnostics as the // standalone planner; the generic compatibility marker alone cannot make a zone eligible. { auto edge_city = [] { City city; city.xzon.at(64, 6) = 0x81; city.xbld.at(64, 6) = 130; city.xbit.at(64, 6) = 0x40; for (int column = 0; column <= 5; ++column) city.xbld.at(64, column) = 0x1D; city.xtxt.at(64, 0) = 0xFA; return city; }; City untyped = edge_city(); MsRand untypedRng(9); ZonePops untypedPops; CommutePeriodDiagnostics untypedDiagnostics; (void)growth_tile_step(untyped, 64, 6, 1800, untypedRng, 0, untypedPops, nullptr, {}, &untypedDiagnostics); CHECK(untypedDiagnostics.attempts == 1 && untypedDiagnostics.successes == 0);
City typed = edge_city(); const std::array connections = {sim::BorderConnection{64, 0, 3, 64, sim::BorderConnectionKind::Road, sim::BorderConnectionLayer::Surface, 1}}; MsRand typedRng(9); ZonePops typedPops; CommutePeriodDiagnostics typedDiagnostics; (void)growth_tile_step(typed, 64, 6, 1800, typedRng, 0, typedPops, nullptr, connections, &typedDiagnostics); CHECK(typedDiagnostics.attempts == 1 && typedDiagnostics.successes == 1); }
// The growth gate receives the live operating snapshot rather than silently routing at 100%. // The same intact rail-only topology changes only because its service efficiency changes. { auto rail_city = [] { City city; city.xzon.at(64, 64) = 0xF1; city.xbld.at(64, 64) = 112; city.xbit.at(64, 64) = 0x40; city.xbld.at(64, 65) = xbld::kRailDepot; for (int column = 66; column <= 72; ++column) city.xbld.at(64, column) = 0x2C; city.xbld.at(64, 73) = xbld::kRailDepot; city.xzon.at(64, 74) = 3; return city; }; CommuteRouteConfig stopped; stopped.serviceEfficiency.modeBasisPoints[static_cast<std::size_t>( CommuteTravelMode::Rail)] = 0;
City unavailable = rail_city(); MsRand unavailableRng(11); ZonePops unavailablePops; CommutePeriodDiagnostics unavailableDiagnostics; (void)growth_tile_step(unavailable, 64, 64, 1800, unavailableRng, 0, unavailablePops, nullptr, {}, &unavailableDiagnostics, nullptr, stopped); CHECK(unavailableDiagnostics.attempts == 1 && unavailableDiagnostics.successes == 0); CHECK(unavailableDiagnostics.stops[static_cast<std::size_t>( CommuteRouteStop::ServiceUnavailable)] == 1);
City funded = rail_city(); MsRand fundedRng(11); ZonePops fundedPops; CommutePeriodDiagnostics fundedDiagnostics; (void)growth_tile_step(funded, 64, 64, 1800, fundedRng, 0, fundedPops, nullptr, {}, &fundedDiagnostics); CHECK(fundedDiagnostics.attempts == 1 && fundedDiagnostics.successes == 1); }
// (1) Grow: eligible empty R tile + strong R demand → grows for at least some seeds. { int grew = 0; for (int seed = 1; seed <= 30; ++seed) { City c = connected_city(); MsRand rng(seed); ZonePops pops; if (growth_tile_step(c, 50, 50, /*demand*/ 1800, rng, 0, pops) == TileGrowth::Grew) { ++grew; CHECK(c.xbld.cells[50 * 128 + 50] >= 112); // a building was actually placed } } CHECK(grew > 0); std::printf(" grow: %d/30 seeds grew an eligible high-demand R tile\n", grew); }
// (1b) Construction-site UPGRADE: the generic placeholder the grow/merge lays (flag-1 code 136) books NO // population; a later scan must replace it in place with the real zone-class building (flag-0) that DOES. // Without the upgrade, buildings stay as construction sites forever and demand can never move. { CHECK(kBuildingFlag[136 - 0x70] == 1); // 136 IS a construction placeholder… CHECK(kPopPerCode[136 - 0x70] == 0); // …that books zero population int upgraded = 0, censusAfter = 0; for (int seed = 1; seed <= 40; ++seed) { City c = connected_city(); c.xbld.cells[50 * 128 + 50] = 136; // a construction site sitting on the eligible R tile c.xzon.cells[50 * 128 + 50] = 0xF1; // built anchor (all corner bits) + R nibble MsRand rng(seed); // Run the step until the site finishes (or give up) — each eligible tick it may upgrade. for (int t = 0; t < 12; ++t) { ZonePops pops; growth_tile_step(c, 50, 50, /*R demand*/ 1800, rng, 0, pops); const int code = c.xbld.cells[50 * 128 + 50]; if (code >= 112 && kBuildingFlag[code - 0x70] == 0) { // reached a finished building ++upgraded; ZonePops p2; growth_tile_step(c, 50, 50, 1800, rng, 0, p2); if (p2.cat[1] > 0) ++censusAfter; // …which now books population break; } } } CHECK(upgraded > 30); // the vast majority finish within a few ticks CHECK(censusAfter == upgraded); // every finished building books population std::printf(" construction upgrade: %d/40 sites finished, all book population\n", upgraded); }
// (1c) MULTI-TILE construction upgrade — the back half of the 2×2/3×3 growth ladder. The grow/merge // lays a flag-1 site (166-169 = 2×2, 194-195 = 3×3); the scan, gated to the 0x80 anchor corner, // must replace it IN PLACE with the finished zone-class building of the same level, preserving the // exact footprint and the rotation-0 corner bits. (With the corner-bit render anchor, a wrong // footprint or missing bits would draw phantoms or nothing — the FOURCITI report.) { auto stamp = [](City& c, int r0, int c0, int n, int code, int nibble) { for (int r = r0; r < r0 + n; ++r) for (int cc = c0; cc < c0 + n; ++cc) { c.xbld.cells[r * 128 + cc] = std::uint8_t(code); c.xzon.cells[r * 128 + cc] = std::uint8_t(nibble); } c.xzon.cells[r0 * 128 + c0] |= 0x10; c.xzon.cells[(r0 + n - 1) * 128 + c0] |= 0x20; c.xzon.cells[(r0 + n - 1) * 128 + c0 + n - 1] |= 0x40; c.xzon.cells[r0 * 128 + c0 + n - 1] |= 0x80; }; auto complete = [](const City& c, int r0, int c0, int n, int lo, int hi) { const int code = c.xbld.cells[r0 * 128 + c0]; if (code < lo || code > hi) return false; for (int r = r0; r < r0 + n; ++r) for (int cc = c0; cc < c0 + n; ++cc) if (c.xbld.cells[r * 128 + cc] != code) return false; return (c.xzon.cells[r0 * 128 + c0] & 0xF0) == 0x10 && (c.xzon.cells[r0 * 128 + c0 + n - 1] & 0xF0) == 0x80 && (c.xzon.cells[(r0 + n - 1) * 128 + c0] & 0xF0) == 0x20 && (c.xzon.cells[(r0 + n - 1) * 128 + c0 + n - 1] & 0xF0) == 0x40; }; MsRand rng(4321); bool ok22 = false, ok33 = false; { City c; stamp(c, 20, 20, 2, 166, 1); // 2×2 R construction, anchor bit at (20,21) for (int t = 0; t < 200 && !ok22; ++t) { ZonePops pops; growth_tile_step(c, 20, 21, 1800, rng, 0, pops); ok22 = complete(c, 20, 20, 2, 140, 143); // finished R 2×2, same footprint + corners } CHECK(ok22); } { City c; stamp(c, 30, 28, 3, 194, 6); // 3×3 I construction, anchor bit at (30,30) for (int t = 0; t < 200 && !ok33; ++t) { ZonePops pops; growth_tile_step(c, 30, 30, 1800, rng, 0, pops); ok33 = complete(c, 30, 28, 3, 188, 193); // finished I 3×3 (Large Factory range) } CHECK(ok33); } std::printf(" multi-tile upgrade: 2x2 -> finished %s, 3x3 -> finished %s\n", ok22 ? "yes" : "NO", ok33 ? "yes" : "NO"); }
// (2) Decline: an isolated developed R building (no power, no road) abandons at the ~v40/level rate. // (One shared RNG stream so the values vary — the abandon roll is rand() < 4000/32768 ≈ 12%.) { int declined = 0; const int N = 400; MsRand rng(20260620); for (int i = 0; i < N; ++i) { City c; c.xzon.cells[50 * 128 + 50] = 0xF1; // R zone, 1×1 anchor corners c.xbld.cells[50 * 128 + 50] = 112; // an R 1×1 building (level 1) ZonePops pops; TileGrowth a = growth_tile_step(c, 50, 50, /*demand*/ 0, rng, 0, pops); CHECK(pops.cat[1] == 1); // census always books the building first if (a == TileGrowth::Declined) { ++declined; CHECK(c.xbld.cells[50 * 128 + 50] >= 138); // overwritten with a ruin } } CHECK(declined > 0 && declined < N); // abandons happen, but not every tick std::printf(" decline: %d/%d isolated R buildings abandoned (~%.0f%%, expect ~12%%)\n", declined, N, 100.0 * declined / N); }
// (3) Unreachable empty tile (no transit adjacent) → no action, no building placed. { City c; c.xzon.cells[50 * 128 + 50] = 0x01; // zoned R but nothing around it MsRand rng(1); ZonePops pops; CHECK(growth_tile_step(c, 50, 50, 2000, rng, 0, pops) == TileGrowth::Skip); CHECK(c.xbld.cells[50 * 128 + 50] == 0); }
// (4) Region driver: the 16 (a,b) regions together census every developed tile exactly once. { City c; const int spots[][2] = {{10, 10}, {20, 21}, {33, 8}, {41, 44}, {62, 60}}; for (auto& s : spots) { c.xzon.cells[s[0] * 128 + s[1]] = 0xF1; // isolated R 1×1 buildings (level 1) c.xbld.cells[s[0] * 128 + s[1]] = 112; } MsRand rng(99); ZonePops pops; for (int a = 0; a < 4; ++a) for (int b = 0; b < 4; ++b) growth_scan_region(c, a, b, rng, 0, pops); CHECK(pops.cat[1] == 5); // all five R buildings booked once each CHECK(pops.r() == 5); std::printf(" region sweep: censused R=%d (expect 5)\n", pops.cat[1]); }
// (5) building_rating: land value lifts it, pollution/crime cut it (>>5 scale), utilities halve it. { CHECK(building_rating(0xE0, 0, 0, true, true) == 12); // 7+12 → clamp 12 CHECK(building_rating(0, 0xE0, 0xE0, true, true) == 0); // 0−7−7+12 = −2 → clamp 0 CHECK(building_rating(0, 0x60, 0x40, true, true) == 7); // 0−3−2+12 = 7 CHECK(building_rating(0, 0x60, 0x40, false, true) == 3); // unpowered → /2 CHECK(building_rating(0, 0x60, 0x40, false, false) == 1); // also unwatered → /2 again }
// infra_funding_record: maps an infrastructure tile to the budget maintenance record that funds it. { CHECK(infra_funding_record(29) == 10); // a road → roads record CHECK(infra_funding_record(44) == 13); // rail CHECK(infra_funding_record(97) == 11); // power line CHECK(infra_funding_record(81) == 12); CHECK(infra_funding_record(63) == 10); // [63,67) is shadowed by the road range CHECK(infra_funding_record(200) == -1); // a building is not funded infrastructure CHECK(infra_funding_record(0) == -1); // empty terrain } // infra_underfunded_decays: fully funded never decays; 0% always decays; partial is probabilistic. { MsRand rng(2026); int decay0 = 0, decay100 = 0, decay50 = 0; for (int i = 0; i < 400; ++i) { if (infra_underfunded_decays(0, rng)) ++decay0; if (infra_underfunded_decays(100, rng)) ++decay100; if (infra_underfunded_decays(50, rng)) ++decay50; } CHECK(decay0 == 400); // 0% funded → always decays CHECK(decay100 == 0); // 100% funded → never decays CHECK(decay50 > 100 && decay50 < 300); // ~50% std::printf(" infra decay: 0%%->%d/400 100%%->%d/400 50%%->%d/400\n", decay0, decay100, decay50); }
if (failures == 0) { std::printf("growth_scan: structural checks passed\n"); return 0; } std::printf("growth_scan: %d failure(s)\n", failures); return 1;}