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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174// Tests the deterministic parts of the original's commute path-tracer: the seed scan and the four// movement/acceptance tables. The RNG-driven backtracking DFS is validated on growth-scan integration.#include "sc2k/sim/commute.hpp"#include <cstdio>
#include "test_harness.hpp"
int main() { using namespace sc2k;
// Directions are W,S,E,N and the no-U-turn masks exclude the reverse of each. CHECK(kCommuteDirRow[0] == 0 && kCommuteDirCol[0] == -1); // W CHECK(kCommuteDirRow[1] == 1 && kCommuteDirCol[1] == 0); // S CHECK(kCommuteDirRow[2] == 0 && kCommuteDirCol[2] == 1); // E CHECK(kCommuteDirRow[3] == -1 && kCommuteDirCol[3] == 0); // N const int reverse[4] = {2, 3, 0, 1}; // W↔E, S↔N for (int d = 0; d < 4; ++d) { CHECK((kCommuteNoUturn[d] & (1 << reverse[d])) == 0); // can't immediately reverse CHECK((kCommuteNoUturn[d] & (1 << d)) != 0); // can continue straight }
// Accept masks: a residential source (zone 1) reaches commercial/industrial nibbles (≥3) but not // another residential tile; commercial (zone 3) reaches R and I. CHECK((kCommuteAcceptMask[1] & (1 << 3)) != 0); // R → C (a job) CHECK((kCommuteAcceptMask[1] & (1 << 5)) != 0); // R → I (a job) CHECK((kCommuteAcceptMask[1] & (1 << 1)) == 0); // R → R (not a destination) CHECK((kCommuteAcceptMask[1] & (1 << 0)) == 0); // R → unzoned (not a destination) CHECK((kCommuteAcceptMask[3] & (1 << 1)) != 0); // C → R CHECK((kCommuteAcceptMask[3] & (1 << 5)) != 0); // C → I
// Seed scan: the first-scanned neighbour (offset (0,+1)) determines the start mode. { City c; c.xbld.cells[50 * 128 + 51] = 0x1D; // a road at (50,51) CHECK(commute_seed(c, 50, 50) == 0); // road → mode 0 } { City c; c.xbld.cells[50 * 128 + 51] = 0xEC; CHECK(commute_seed(c, 50, 50) == 8); } // 236 → 8 { City c; c.xbld.cells[50 * 128 + 51] = 0xED; CHECK(commute_seed(c, 50, 50) == 10); } // 237 → 10 { City c; c.xbld.cells[50 * 128 + 51] = 0xE9; CHECK(commute_seed(c, 50, 50) == 11); } // 233 → 11 { City c; CHECK(commute_seed(c, 50, 50) == -1); } // nothing adjacent // A building tile (non-transit) confers no seed. { City c; c.xbld.cells[50 * 128 + 51] = 0x80; CHECK(commute_seed(c, 50, 50) == -1); }
// commute_transition: representative deterministic steps for each leg-mode (zone 0 = no job; a C // neighbour (nibble 3) with an R source (zone 1) is a job for the modes that test it). // clang-format off auto chk = [&](CommuteStep s, bool found, bool moved, int mode, int cost, int line) { bool ok = s.found == found && s.moved == moved && (!moved || found || (s.mode == mode && s.cost == cost)); if (!ok) { std::printf(" FAIL transition (line %d): found=%d moved=%d mode=%d cost=%d\n", line, s.found, s.moved, s.mode, s.cost); ++failures; } }; #define T(mode,xbld,zn,xund,altm,src) commute_transition(mode,xbld,zn,xund,altm,src) // clang-format on // mode 0 (road): job → success; the special ranges divert to modes 2/3/1 before the plain-road case. chk(T(0, 0x1D, 0, 0, 0, 1), false, true, 0, 3, __LINE__); // road → stay road, cost 3 chk(T(0, 0x00, 3, 0, 0, 1), true, true, 0, 0, __LINE__); // neighbour is a C job → FOUND chk(T(0, 0x40, 0, 0, 0, 1), false, true, 2, 3, __LINE__); // [0x3F,0x43) → mode 2 chk(T(0, 0x52, 0, 0, 0, 1), false, true, 3, 3, __LINE__); // [0x51,0x5D) → mode 3 chk(T(0, 0x5E, 0, 0, 0, 1), false, true, 1, 2, __LINE__); // [0x5D,0x61) → mode 1 chk(T(0, 0x43, 0, 0, 0, 1), false, true, 0, 3, __LINE__); // 0x43 stays road (not stolen by mode 2) chk(T(0, 236, 0, 0, 0, 1), false, true, 8, 4, __LINE__); // 236 → rail mode 8 chk(T(0, 0xFB, 0, 0, 0, 1), true, true, 0, 0, __LINE__); // >0xFA → FOUND chk(T(0, 0x80, 0, 0, 0, 1), false, false, -1, 0, __LINE__); // a building → dead direction // mode 4 (priority road): cheaper, diverts to 6/7/5, rail→9. chk(T(4, 0x1D, 0, 0, 0, 1), false, true, 4, 2, __LINE__); chk(T(4, 236, 0, 0, 0, 1), false, true, 9, 4, __LINE__); // mode 2 (bridge/tunnel): ALTM flag bits keep the leg; a road surfaces to mode 0. chk(T(2, 0x00, 0, 0, 0x04, 1), false, true, 2, 3, __LINE__); chk(T(2, 0x1D, 0, 0, 0x00, 1), false, true, 0, 3, __LINE__); // mode 8 (rail): stays on 236, or transfers to road as mode 4. chk(T(8, 236, 0, 0, 0, 1), false, true, 8, 4, __LINE__); chk(T(8, 0x1D, 0, 0, 0, 1), false, true, 4, 2, __LINE__); // mode 10/12 rail-bed + mode 11/13 subway. chk(T(10, 237, 0, 0, 0, 1), false, true, 10, 4, __LINE__); chk(T(10, 0x2C, 0, 0, 0, 1), false, true, 12, 1, __LINE__); // rail-bed → mode 12 chk(T(11, 0x00, 0, 1, 0, 1), false, true, 13, 1, __LINE__); // subway tile (XUND) → mode 13 chk(T(12, 0x2D, 0, 0, 0, 1), false, true, 12, 1, __LINE__); chk(T(13, 0xE9, 0, 0, 0, 1), false, true, 9, 4, __LINE__); // subway surfaces to rail 233#undef T
// CommutePathRing: a backtracking stack (LIFO via head), peek non-destructive, drop-oldest on full. { CommutePathRing q; int r = -1, cc = -1; q.init(5, 7); CHECK(!q.empty()); q.peek(r, cc); CHECK(r == 5 && cc == 7); q.peek(r, cc); CHECK(r == 5 && cc == 7); // peek doesn't consume q.push(8, 9); q.pop(r, cc); CHECK(r == 8 && cc == 9); // LIFO: last in, first out q.pop(r, cc); CHECK(r == 5 && cc == 7); CHECK(q.empty()); // head wrapped back to tail } { CommutePathRing q; // overflow stays bounded (drop oldest) q.init(0, 0); for (int i = 1; i < 600; ++i) q.push(i, i); // > 512 pushes CHECK(((q.head - q.tail) & CommutePathRing::kMask) <= 512); int r = -1, cc = -1; q.peek(r, cc); CHECK(r == 599 && cc == 599); // most-recent push is on top }
// Full driver (commute_trace), structural validation: an R tile that reaches a C job along a road // must succeed (for every RNG seed — a real path is found regardless of the random walk) and stamp // XTRF on the road; the same road leading nowhere must fail. { auto build = [](City& c, bool withJob) { c.xzon.cells[50 * 128 + 50] = 0x01; // R zone source for (int x = 51; x <= 55; ++x) c.xbld.cells[50 * 128 + x] = 0x1D; // a road run if (withJob) c.xzon.cells[50 * 128 + 56] = 0x03; // a C job past the road }; int reached = 0; for (int seed = 1; seed <= 8; ++seed) { City c; build(c, true); MsRand rng(seed); if (commute_trace(c, 50, 50, /*R*/ 1, /*weight*/ 10, /*maxCost*/ 100, rng)) { ++reached; long xtrf = 0; for (auto t : c.xtrf.cells) xtrf += t; CHECK(xtrf > 0); // road legs were stamped } } CHECK(reached == 8); // reachable for every seed // Disconnected: no job anywhere → the walk exhausts and fails. int failed = 0; for (int seed = 1; seed <= 8; ++seed) { City c; build(c, false); MsRand rng(seed); if (!commute_trace(c, 50, 50, 1, 10, 100, rng)) ++failed; } CHECK(failed == 8); // never spuriously succeeds // No transit adjacent at all → immediate failure. { City c; MsRand rng(1); CHECK(!commute_trace(c, 10, 10, 1, 10, 100, rng)); } std::printf(" driver: reached %d/8 (connected), failed %d/8 (disconnected)\n", reached, failed); }
if (failures == 0) { std::printf("commute: seed + tables + transition + ring + driver checks passed\n"); return 0; } std::printf("commute: %d failure(s)\n", failures); return 1;}