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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482// Known-answer test for the building-placement primitive.// Verifies that grown buildings get the correct code ranges per zone class/size, the footprint is// stamped, and the XZON corner bits are set.#include "sc2k/sim/growth.hpp"#include <cstdio>
#include "test_harness.hpp"
int main() { using namespace sc2k; MsRand rng(1);
// R 2×2 → kZoneCode[2]=140, range 4 → 140..143 { City c; int code = place_building(c, 10, 10, 2, /*R*/ 0, rng, /*rot*/ 0); CHECK(code >= 140 && code <= 143); // footprint: rows 10..11, cols 9..10 all carry the code for (int r = 10; r <= 11; ++r) for (int cc = 9; cc <= 10; ++cc) CHECK(c.xbld.cells[r * 128 + cc] == code); // corner bits present somewhere on the footprint (rotation 0: 0x10/0x20/0x40/0x80) int cornerMask = 0; for (int r = 10; r <= 11; ++r) for (int cc = 9; cc <= 10; ++cc) cornerMask |= (c.xzon.cells[r * 128 + cc] & 0xF0); CHECK(cornerMask == 0xF0); // all four corners set across the 2×2 } // C 2×2 → kZoneCode[6]=148, range 5 → 148..152 { City c; int code = place_building(c, 20, 20, 2, /*C*/ 1, rng, 0); CHECK(code >= 148 && code <= 152); } // I 2×2 → kZoneCode[10]=158, range 4 → 158..161 { City c; int code = place_building(c, 30, 30, 2, /*I*/ 2, rng, 0); CHECK(code >= 158 && code <= 161); } // R 3×3 → kZoneCode[3]=144, range 4 → 144..147 { City c; int code = place_building(c, 40, 40, 3, /*R*/ 0, rng, 0); CHECK(code >= 144 && code <= 147); } // C 1×1 → kZoneCode[5]=124, range 8 → 124..131 { City c; int code = place_building(c, 50, 50, 1, /*C*/ 1, rng, 0); CHECK(code >= 124 && code <= 131); CHECK((c.xzon.cells[50 * 128 + 50] & 0xF0) == 0xF0); // 1×1 → all corner bits } // R 1×1 with high land value → code 112 + 0..3 + 4·2 = 120..123 { City c; c.xval.cells[(60 / 2) * 64 + (60 / 2)] = 0xC0; // land-value class 3 → capped to 2 int code = place_building(c, 60, 60, 1, /*R*/ 0, rng, 0); CHECK(code >= 120 && code <= 123); }
// growth_decide: demand drives the direction. High demand on a powered/road tile grows and never // abandons; low demand (or an ineligible tile) abandons and never grows. { int grewHi = 0, abandonHi = 0, grewLo = 0, abandonLo = 0, abandonIneligible = 0; MsRand g(42); for (int i = 0; i < 2000; ++i) { switch (growth_decide(2, /*demand*/ 2000, /*eligible*/ true, g)) { case GrowthAction::Grow: ++grewHi; break; case GrowthAction::Abandon: ++abandonHi; break; default: break; } switch (growth_decide(2, /*demand*/ -2000, /*eligible*/ true, g)) { case GrowthAction::Grow: ++grewLo; break; case GrowthAction::Abandon: ++abandonLo; break; default: break; } switch (growth_decide(2, /*demand*/ 2000, /*eligible*/ false, g)) { case GrowthAction::Abandon: ++abandonIneligible; break; default: break; } } std::printf(" growth_decide: hi-demand grow=%d abandon=%d | lo-demand grow=%d abandon=%d | ineligible abandon=%d\n", grewHi, abandonHi, grewLo, abandonLo, abandonIneligible); CHECK(grewHi > 0 && abandonHi == 0); // strong demand → growth, never decline CHECK(grewLo == 0 && abandonLo > 0); // weak demand → decline, never growth CHECK(abandonIneligible > 0); // unpowered/no-road → decline even with demand }
// can_merge_tile: same zone+altitude+low-code buildable land joins; others don't. { City c; c.xzon.cells[40 * 128 + 40] = 0x02; // R-dense zone, altitude 0, empty XBLD CHECK(can_merge_tile(c, 40, 40, /*alt*/ 0, /*nibble*/ 2, /*thr*/ 140)); // empty same-zone → joins c.xzon.cells[40 * 128 + 41] = 0x04; // different zone nibble CHECK(!can_merge_tile(c, 40, 41, 0, 2, 140)); c.xbld.cells[40 * 128 + 42] = 0x1D; c.xzon.cells[40 * 128 + 42] = 0x02; // a road CHECK(!can_merge_tile(c, 40, 42, 0, 2, 140)); c.xbld.cells[40 * 128 + 43] = 150; c.xzon.cells[40 * 128 + 43] = 0x02; // already a big building CHECK(!can_merge_tile(c, 40, 43, 0, 2, 140)); // code >= threshold c.altm.cells[40 * 128 + 44] = 5; c.xzon.cells[40 * 128 + 44] = 0x02; // different altitude CHECK(!can_merge_tile(c, 40, 44, 0, 2, 140)); }
// grow_building: the next size up, using the shared high-density set (a4=3): // level 0 → 1×1 (136-137), level 2 → 3×3 (168-169), level 1 → 2×2 (166-167) when a quad merges. { MsRand g(7); { City c; int code = grow_building(c, 30, 30, /*level*/ 0, /*nibble*/ 2, g, 0); CHECK(code >= 136 && code <= 137); } { City c; int code = grow_building(c, 30, 30, /*level*/ 2, 2, g, 0); CHECK(code >= 168 && code <= 169); CHECK(c.xbld.cells[30 * 128 + 30] >= 168 && c.xbld.cells[30 * 128 + 30] <= 169); } // level 1: a mergeable 2×2 of same-zone/altitude empty tiles → places a 2×2 at (40,41). { City c; for (int r = 40; r <= 41; ++r) for (int cc = 40; cc <= 41; ++cc) c.xzon.cells[r * 128 + cc] = 0x02; int code = grow_building(c, 40, 40, /*level*/ 1, 2, g, 0); CHECK(code >= 166 && code <= 167); int filled = 0; for (int r = 40; r <= 41; ++r) for (int cc = 40; cc <= 41; ++cc) filled += (c.xbld.cells[r * 128 + cc] >= 166 && c.xbld.cells[r * 128 + cc] <= 167); CHECK(filled == 4); } // the whole 2×2 footprint was placed // level 1 with no mergeable neighbours → no growth. { City c; c.xzon.cells[60 * 128 + 60] = 0x02; c.xbld.cells[60 * 128 + 61] = 0x1D; // road blocks merge CHECK(grow_building(c, 60, 60, 1, 2, g, 0) == -1); } // level 1 ABSORBS developed 1×1s: four finished houses (112-139, under the 140 merge // threshold) on a same-zone/altitude quad merge into one 2×2 construction site. { City c; for (int r = 40; r <= 41; ++r) for (int cc = 40; cc <= 41; ++cc) c.xzon.cells[r * 128 + cc] = 0x01; // R zone for (int r = 40; r <= 41; ++r) for (int cc = 40; cc <= 41; ++cc) { const int h = place_building(c, r, cc, 1, 0, g, 0); // a finished 1×1 home CHECK(h >= 112 && h < 140); } const int code = grow_building(c, 40, 40, /*level*/ 1, 1, g, 0); CHECK(code >= 166 && code <= 167); // the quad merged into a 2×2 site int filled = 0; for (int r = 40; r <= 41; ++r) for (int cc = 40; cc <= 41; ++cc) filled += (c.xbld.cells[r * 128 + cc] == code); CHECK(filled == 4); // all four houses absorbed CHECK((c.xzon.cells[40 * 128 + 40] & 0xF0) == 0x10 && (c.xzon.cells[40 * 128 + 41] & 0xF0) == 0x80 && (c.xzon.cells[41 * 128 + 40] & 0xF0) == 0x20 && (c.xzon.cells[41 * 128 + 41] & 0xF0) == 0x40); } // level 3 → the 3×3 (the "size-4" placement): a 2×2 building PLUS 1×1s merge into one 3×3 // construction. Needs all 8 perimeter tiles mergeable (threshold 174 — finished 2×2s 140-165 // and 1×1s both qualify) and a road corner/tee/crossing (XBLD 35-43) diagonally off a footprint // corner; overlapped 2×2s are bulldozed via their corner bits (clear_building) first. { City c; for (int r = 30; r <= 32; ++r) for (int cc = 28; cc <= 30; ++cc) c.xzon.cells[r * 128 + cc] = 0x06; // dense I zone const int c22 = place_building(c, 31, 29, 2, 2, g, 0); // an I 2×2 (158-161) CHECK(c22 >= 158 && c22 <= 161); for (auto [r, cc] : {std::pair{30, 28}, {30, 29}, {30, 30}, {31, 30}, {32, 30}}) { const int h = place_building(c, r, cc, 1, 2, g, 0); // 1×1 industry (132-135) CHECK(h >= 132 && h <= 135); } c.xbld.cells[29 * 128 + 31] = 43; // the crossroad the level-4 grow requires const int code = grow_building(c, 31, 29, /*level*/ 3, 6, g, 0); CHECK(code >= 194 && code <= 195); // a 3×3 construction site int filled = 0; for (int r = 30; r <= 32; ++r) for (int cc = 28; cc <= 30; ++cc) filled += (c.xbld.cells[r * 128 + cc] == code); CHECK(filled == 9); // the 2×2 and the 1×1s were all absorbed CHECK((c.xzon.cells[30 * 128 + 28] & 0xF0) == 0x10 && (c.xzon.cells[30 * 128 + 30] & 0xF0) == 0x80 && (c.xzon.cells[32 * 128 + 28] & 0xF0) == 0x20 && (c.xzon.cells[32 * 128 + 30] & 0xF0) == 0x40); // …and WITHOUT the road junction the same layout refuses to grow (the abutment gate). City d; MsRand g2(11); for (int r = 30; r <= 32; ++r) for (int cc = 28; cc <= 30; ++cc) d.xzon.cells[r * 128 + cc] = 0x06; place_building(d, 31, 29, 2, 2, g2, 0); CHECK(grow_building(d, 31, 29, 3, 6, g2, 0) == -1); } }
// decline_building: overwrites with ruins (set a4=4 → 138/170/172 for size 1/2/3), // and on `split` a 2×2 crumbles into four 1×1 ruins. { MsRand g(3); { City c; CHECK(decline_building(c, 30, 30, /*level*/ 1, /*split*/ 0, g, 0) >= 138 && c.xbld.cells[30 * 128 + 30] >= 138 && c.xbld.cells[30 * 128 + 30] <= 139); } { City c; int code = decline_building(c, 30, 30, /*level*/ 2, /*split*/ 0, g, 0); CHECK(code >= 170 && code <= 171); } // 2×2 ruin { City c; int code = decline_building(c, 30, 30, /*level*/ 3, /*split*/ 0, g, 0); CHECK(code >= 172 && code <= 173); } // 3×3 ruin { City c; decline_building(c, 50, 50, /*level*/ 2, /*split*/ 1, g, 0); // crumble the 2×2 int ruins = 0; for (int rc : {50 * 128 + 50, 51 * 128 + 50, 51 * 128 + 49, 50 * 128 + 49}) ruins += (c.xbld.cells[rc] >= 138 && c.xbld.cells[rc] <= 139); CHECK(ruins == 4); } // four scattered 1×1 ruins }
// clear_building: the 2×2 a tile belongs to is overwritten with 1×1 ruins. { MsRand g(5); City c; c.xzon.cells[50 * 128 + 50] = 0x10; // a corner-bit tile (rotation 0 → anchor at col+1) clear_building(c, 50, 50, g, 0); int ruins = 0; for (int rc : {50 * 128 + 50, 50 * 128 + 51, 51 * 128 + 50, 51 * 128 + 51}) ruins += (c.xbld.cells[rc] >= 138 && c.xbld.cells[rc] <= 139); CHECK(ruins == 4); // the whole 2×2 became ruins } // grow_building level 3 (the original's case 3): with a mergeable footprint AND an adjacent special // building (a port/landmark, XBLD 35), the largest building (set-4 size-4 = 194-195, a 3×3 footprint) // is placed; without the landmark it does not grow. { MsRand g(11); auto setup = [](City& c, bool landmark) { for (int r = 39; r <= 43; ++r) for (int cc = 39; cc <= 43; ++cc) c.xzon.cells[r * 128 + cc] = 0x02; if (landmark) c.xbld.cells[39 * 128 + 43] = 35; // a landmark abutting the footprint }; { City c; setup(c, true); int code = grow_building(c, 40, 42, /*level*/ 3, /*nibble*/ 2, g, 0); CHECK(code >= 194 && code <= 195); int filled = 0; for (int r = 40; r <= 42; ++r) for (int cc = 40; cc <= 42; ++cc) filled += (c.xbld.cells[r * 128 + cc] >= 194 && c.xbld.cells[r * 128 + cc] <= 195); CHECK(filled == 9); } // the full 3×3 footprint was placed { City c; setup(c, false); // no landmark → no level-3 growth CHECK(grow_building(c, 40, 42, 3, 2, g, 0) == -1); } }
// construct_building: clearance-checked placement on buildable terrain only. { { City c; // 1×1 on clear land succeeds CHECK(construct_building(c, 50, 50, /*code*/ 230, /*size*/ 1, 0)); CHECK(c.xbld.cells[50 * 128 + 50] == 230); CHECK((c.xzon.cells[50 * 128 + 50] & 0xF0) == 0xF0); } { City c; c.xbld.cells[50 * 128 + 50] = 0x70; // occupied (a building) → rejected CHECK(!construct_building(c, 50, 50, 230, 1, 0)); } { City c; c.xbld.cells[50 * 128 + 50] = 0x1D; // a road (>= 0x1D) → rejected CHECK(!construct_building(c, 50, 50, 230, 1, 0)); } { City c; c.xbit.cells[50 * 128 + 50] = 0x04; // flagged tile → rejected CHECK(!construct_building(c, 50, 50, 230, 1, 0)); } { City c; // 2×2 on clear land stamps the footprint CHECK(construct_building(c, 60, 60, /*code*/ 240, /*size*/ 2, 0)); int filled = 0, corners = 0; for (int r = 60; r <= 61; ++r) for (int cc = 60; cc <= 61; ++cc) { filled += (c.xbld.cells[r * 128 + cc] == 240); corners |= (c.xzon.cells[r * 128 + cc] & 0xF0); } CHECK(filled == 4); CHECK((corners & 0xF0) == 0xF0); } // all four corner bits across the 2×2 { City c; c.xbld.cells[61 * 128 + 61] = 0x70; // one tile occupied → whole 2×2 rejected CHECK(!construct_building(c, 60, 60, 240, 2, 0)); } }
// special_zone_place_1x1 (the original's cases 225-234): a 1×1 special building on low terrain, // tagged with its zone nibble. { { City c; // on clear terrain → placed + zoned CHECK(special_zone_place_1x1(c, 70, 70, /*code*/ 230, /*zone*/ 8, 0)); CHECK(c.xbld.cells[70 * 128 + 70] == 230); CHECK((c.xzon.cells[70 * 128 + 70] & 0x0F) == 8); CHECK((c.xzon.cells[70 * 128 + 70] & 0xF0) == 0xF0); } { City c; c.xbld.cells[70 * 128 + 70] = 0x70; // already built (>= 0x0D) → rejected CHECK(!special_zone_place_1x1(c, 70, 70, 230, 8, 0)); } { City c; c.xbld.cells[70 * 128 + 70] = 0x1D; // a road (>= 0x0D) → rejected CHECK(!special_zone_place_1x1(c, 70, 70, 230, 8, 0)); } }
// bulldoze_building: clears special buildings (code >= 198) only. { { City c; // a 2×2 special building → 2×2 cleared for (int rc : {50 * 128 + 50, 50 * 128 + 51, 51 * 128 + 50, 51 * 128 + 51}) c.xbld.cells[rc] = 200; bulldoze_building(c, 50, 50); int cleared = 0; for (int rc : {50 * 128 + 50, 50 * 128 + 51, 51 * 128 + 50, 51 * 128 + 51}) cleared += (c.xbld.cells[rc] == 0); CHECK(cleared == 4); } { City c; c.xbld.cells[50 * 128 + 50] = 220; // a 1×1 special building (219-234) → 1 cleared c.xbld.cells[50 * 128 + 51] = 220; bulldoze_building(c, 50, 50); CHECK(c.xbld.cells[50 * 128 + 50] == 0 && c.xbld.cells[50 * 128 + 51] == 220); } { City c; c.xbld.cells[50 * 128 + 50] = 112; // an RCI building (< 198) → untouched bulldoze_building(c, 50, 50); CHECK(c.xbld.cells[50 * 128 + 50] == 112); } } // special_zone_place_2x2 (the original's cases 238-246): a 2×2 special building over a same-zone area. { { City c; // four same-zone tiles → 2×2 placed + zoned for (int r = 60; r <= 61; ++r) for (int cc = 60; cc <= 61; ++cc) c.xzon.cells[r * 128 + cc] = 0x08; CHECK(special_zone_place_2x2(c, 60, 60, /*code*/ 240, /*zone*/ 8, 0)); int filled = 0; for (int r = 60; r <= 61; ++r) for (int cc = 60; cc <= 61; ++cc) { filled += (c.xbld.cells[r * 128 + cc] == 240 && (c.xzon.cells[r * 128 + cc] & 0x0F) == 8); } CHECK(filled == 4); } { City c; // a power line in the footprint → rejected for (int r = 60; r <= 61; ++r) for (int cc = 60; cc <= 61; ++cc) c.xzon.cells[r * 128 + cc] = 0x08; c.xbld.cells[60 * 128 + 60] = 221; CHECK(!special_zone_place_2x2(c, 60, 60, 240, 8, 0)); } { City c; // tiles not all the same zone → rejected c.xzon.cells[60 * 128 + 60] = 0x08; CHECK(!special_zone_place_2x2(c, 60, 60, 240, 8, 0)); } }
// set_underground: writes XUND and tracks the subway-tile count. { City c; int sub = 0; set_underground(c, 50, 50, 34, sub); // 34 is a subway code → count up CHECK(c.xund.cells[50 * 128 + 50] == 34 && sub == 1); set_underground(c, 50, 50, 0, sub); // 0 (non-subway) over a subway tile → count down CHECK(c.xund.cells[50 * 128 + 50] == 0 && sub == 0); c.xzon.cells[51 * 128 + 51] = 0x07; // special zone (nibble 7) → no count change set_underground(c, 51, 51, 34, sub); CHECK(c.xund.cells[51 * 128 + 51] == 34 && sub == 0); } // special_zone_place_3x3 (the original's case 249): a 3×3 special building with XUND-34 underneath. { City c; int sub = 0; for (int r = 70; r <= 72; ++r) for (int cc = 70; cc <= 72; ++cc) c.xzon.cells[r * 128 + cc] = 0x09; CHECK(special_zone_place_3x3(c, 70, 70, /*code*/ 249, /*zone*/ 9, 0, sub)); int bld = 0, und = 0; for (int r = 70; r <= 72; ++r) for (int cc = 70; cc <= 72; ++cc) { bld += (c.xbld.cells[r * 128 + cc] == 249); und += (c.xund.cells[r * 128 + cc] == 34); } CHECK(bld == 9 && und == 9 && sub == 9); // full 3×3 placed, 9 subway tiles counted }
// special_zone_place_pier (the original's case 224): a 1×1 anchor (224) + 4 extension tiles (223) // reaching out over water (XBIT 0x04). { { City c; c.xzon.cells[50 * 128 + 50] = 0x08; // the land anchor, special zone 8 for (int x = 51; x <= 55; ++x) c.xbit.cells[50 * 128 + x] = 0x04; // five water-adjacent tiles east c.altm.cells[50 * 128 + 55] = 0x00A0; // far tile water-level 5 ≥ altitude 0 + 2 CHECK(special_zone_place_pier(c, 50, 50, /*zone*/ 8, 0)); CHECK(c.xbld.cells[50 * 128 + 50] == 224); // the land anchor int ext = 0; for (int x = 51; x <= 54; ++x) ext += (c.xbld.cells[50 * 128 + x] == 223); CHECK(ext == 4); } // four extension tiles over water { City c; // no water adjacent → no pier c.xzon.cells[50 * 128 + 50] = 0x08; CHECK(!special_zone_place_pier(c, 50, 50, 8, 0)); } }
// special_zone_place_powerline (the original's case 221): lay a 5-tile power-line run over a // same-zone strip, with the XBIT power bits set. { { City c; // a horizontal same-zone strip → 5 power lines for (int x = 50; x <= 54; ++x) c.xzon.cells[51 * 128 + x] = 0x09; // row 51 odd → horizontal run CHECK(special_zone_place_powerline(c, 51, 50, /*zone*/ 9, 0, /*parity*/ 0)); int lines = 0, powered = 0; for (int x = 50; x <= 54; ++x) { lines += (c.xbld.cells[51 * 128 + x] == 221); powered += ((c.xbit.cells[51 * 128 + x] & 0xC0) == 0xC0); } CHECK(lines == 5 && powered == 5); } // five conductive+powered power lines { City c; // strip too short (zone breaks) → rejected for (int x = 50; x <= 52; ++x) c.xzon.cells[51 * 128 + x] = 0x09; CHECK(!special_zone_place_powerline(c, 51, 50, 9, 0, 0)); } }
// Runtime map extents: Tiny edge checks reject footprints without touching storage, while // Medium/Large coordinates beyond the classic 127 boundary remain usable. { City tiny(MapSize::Tiny); MsRand g(17); CHECK(!can_merge_tile(tiny, 64, 0, 0, 0, 140)); CHECK(!construct_building_tile_ok(tiny, 0, 64, 230)); CHECK(!construct_building(tiny, 63, 63, 240, 2, 0)); tiny.xbit.at(62, 63) = 0x04; CHECK(!special_zone_place_pier(tiny, 62, 62, 8, 0)); CHECK(!special_zone_place_powerline(tiny, 63, 60, 8, 0, 0));
City medium(MapSize::Medium); medium.xzon.at(128, 128) = 2; CHECK(can_merge_tile(medium, 128, 128, 0, 2, 140)); CHECK(place_building(medium, 128, 128, 1, 1, g, 0) >= 124); for (int col = 130; col < 135; ++col) medium.xzon.at(131, col) = 9; CHECK(special_zone_place_powerline(medium, 131, 130, 9, 0, 0)); CHECK(medium.xbld.at(131, 134) == 221);
City large(MapSize::Large); CHECK(construct_building_tile_ok(large, 255, 255, 230)); CHECK(construct_building(large, 255, 255, 230, 1, 0)); CHECK(large.xbld.at(255, 255) == 230); }
if (failures == 0) { std::printf("growth: place/construct + ALL 5 special-zone cases + grow + decline + clear + bulldoze passed\n"); return 0; } std::printf("growth: %d failure(s)\n", failures); return 1;}