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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204// test_compass_bits — saves store two per-tile bit sets in the convention of the compass they were// SAVED at (the original rewrites both on every view rotation, so a save freezes them at its view):// • the XBIT mirror bit (0x02) of building-family tiles (XBLD >= 0x6c): INVERTED on odd-compass saves.// Rule derived from the real Win95 CD cities via the strip-axis oracle: a runway (0xDD) / pier deck// (0xDF) STRIP runs along its own tiles, so at the compass-0 convention a strip along COLUMNS must// store mirror=0 and along ROWS mirror=1 (verified against the real game under Wine: CAPEQUES.SC2).// • the XZON corner bits (high nibble): rotated one cycle step 0x10→0x20→0x40→0x80 per compass step.// Rule derived from ~80k complete multi-tile building blocks across every CD city (unanimous per// compass): the storage-TL tile carries 0x10/0x20/0x40/0x80 at saved compass 0/1/2/3.// normalize_save_compass (io/sc2_file.hpp, run inside load_sc2) folds both to the compass-0 convention.// This test locks:// 1. the synthetic normalization semantics (mirror: odd toggles >=0x6c only; corners: un-rotated at// every compass, zone nibble preserved; 0x08 -> 0),// 2. both data rules across every CD city: after normalization the pooled agreement is near-total;// with the saved convention reconstructed (denormalize below) the rotated saves drag it way down// (sensitivity),// 3. FOURCITI ground truth: a compass-2 save loads with rotation-0 corner bits (and remembers its// saved compass for the view restore).#include <cstdio>#include <cstdlib>#include <filesystem>#include <string>#include "sc2k/io/sc2_file.hpp"#include "sc2k/sim/building_sizes.hpp"#include "test_paths.hpp"
using sc2k::City;
static int fails = 0;#define CHECK(cond, msg) \ do { \ if (!(cond)) { \ std::printf("FAIL: %s\n", msg); \ ++fails; \ } else { \ std::printf("ok: %s\n", msg); \ } \ } while (0)
static std::uint8_t rotl_corners(std::uint8_t bits, int k) { // one cycle step 0x10→0x20→0x40→0x80 per k int hi = bits >> 4; hi = ((hi << k) | (hi >> (4 - k))) & 0xF; return std::uint8_t(hi << 4);}
// Reconstruct the AS-STORED convention from a (load-normalized) city — the inverse of// normalize_save_compass, using the remembered City::savedCompass.static void denormalize(City& c) { const int k = c.savedCompass & 3; if (!k) return; for (int r = 0; r < 128; ++r) for (int col = 0; col < 128; ++col) { if ((k & 1) && sc2k::xbld::is_building(c.xbld.at(r, col))) c.xbit.at(r, col) ^= 0x02; const std::uint8_t z = c.xzon.at(r, col); if (z & 0xF0) c.xzon.at(r, col) = std::uint8_t(rotl_corners(z, k) | (z & 0x0F)); } c.set_misc_be32(sc2k::misc::kViewRotation, k);}
// Axis of the same-code strip through (r,c): 'R' rows, 'C' cols, 'B' both, 'N' none.static char strip_axis(const City& c, int r, int col, std::uint8_t code) { const bool rowAx = (r > 0 && c.xbld.at(r - 1, col) == code) || (r < 127 && c.xbld.at(r + 1, col) == code); const bool colAx = (col > 0 && c.xbld.at(r, col - 1) == code) || (col < 127 && c.xbld.at(r, col + 1) == code); return rowAx && colAx ? 'B' : rowAx ? 'R' : colAx ? 'C' : 'N';}
// Pooled axis→bit agreement (%) over every runway/pier strip tile of every city; `raw` reconstructs the// as-stored convention first.static double rule_agreement(const std::string& dir, bool raw, long& nTiles) { long agree = 0, total = 0; for (const auto& e : std::filesystem::directory_iterator(dir)) { if (e.path().extension() != ".SC2" && e.path().extension() != ".sc2") continue; City c; if (!sc2k::io::load_sc2(e.path().string(), c).ok) continue; if (raw) denormalize(c); for (int r = 0; r < 128; ++r) for (int col = 0; col < 128; ++col) { const std::uint8_t b = c.xbld.at(r, col); if (b != 0xDD && b != 0xDF) continue; const char ax = strip_axis(c, r, col, b); if (ax != 'R' && ax != 'C') continue; // unambiguous strip tiles only const bool mirror = (c.xbit.at(r, col) & 0x02) != 0; agree += (ax == 'C') == !mirror; // compass-0 rule: col ⇒ 0, row ⇒ 1 ++total; } } nTiles = total; return total ? 100.0 * double(agree) / double(total) : 0.0;}
// Pooled corner-bit agreement (%) over every COMPLETE multi-tile building footprint of every city:// at the compass-0 convention the storage-TL tile carries 0x10 (and TR/BL/BR carry 0x80/0x20/0x40).static double corner_agreement(const std::string& dir, bool raw, long& nBlocks) { long agree = 0, total = 0; for (const auto& e : std::filesystem::directory_iterator(dir)) { if (e.path().extension() != ".SC2" && e.path().extension() != ".sc2") continue; City c; if (!sc2k::io::load_sc2(e.path().string(), c).ok) continue; if (raw) denormalize(c); for (int r = 0; r < 128; ++r) for (int col = 0; col < 128; ++col) { const std::uint8_t xb = c.xbld.at(r, col); if (xb < 0x6c) continue; const int n = sc2k::sim::building_sizes()[xb]; if (n < 2 || r + n > 128 || col + n > 128) continue; if ((r > 0 && c.xbld.at(r - 1, col) == xb) || (col > 0 && c.xbld.at(r, col - 1) == xb)) continue; bool solid = true; // complete footprints only for (int dr = 0; solid && dr < n; ++dr) for (int dc = 0; dc < n; ++dc) if (c.xbld.at(r + dr, col + dc) != xb) solid = false; if (!solid) continue; ++total; agree += (c.xzon.at(r, col) & 0xF0) == 0x10 && (c.xzon.at(r, col + n - 1) & 0xF0) == 0x80 && (c.xzon.at(r + n - 1, col) & 0xF0) == 0x20 && (c.xzon.at(r + n - 1, col + n - 1) & 0xF0) == 0x40; } } nBlocks = total; return total ? 100.0 * double(agree) / double(total) : 0.0;}
int main() { // 1) synthetic semantics — mirror bit { City c; c.set_misc_be32(sc2k::misc::kViewRotation, 3); // an odd-compass save c.xbld.at(10, 10) = 0xDF; // pier deck (building family), bit clear c.xbld.at(12, 12) = 0x70; c.xbit.at(12, 12) |= 0x02; // building, bit set c.xbld.at(14, 14) = 0x1D; c.xbit.at(14, 14) |= 0x02; // road (< 0x6c) — must NOT toggle CHECK(sc2k::normalize_save_compass(c) == 3, "odd save reports its compass"); CHECK(c.misc_be32(sc2k::misc::kViewRotation) == 0, "compass stamped back to 0"); CHECK((c.xbit.at(10, 10) & 0x02) != 0, "odd save: building-family bit toggled on"); CHECK((c.xbit.at(12, 12) & 0x02) == 0, "odd save: building-family bit toggled off"); CHECK((c.xbit.at(14, 14) & 0x02) != 0, "odd save: network bit untouched"); CHECK(sc2k::normalize_save_compass(c) == 0, "already-normalized save is a no-op"); } // 1b) synthetic semantics — corner bits: a 2×2 stamped in the saved-compass convention normalizes // to the rotation-0 pattern (TL/TR/BL/BR = 0x10/0x80/0x20/0x40) at every compass, nibble kept. for (int k = 0; k < 4; ++k) { City c; c.set_misc_be32(sc2k::misc::kViewRotation, k); for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) c.xbld.at(20 + dr, 20 + dc) = 0x8C; // a 2×2 apartment block c.xzon.at(20, 20) = std::uint8_t(rotl_corners(0x10, k) | 1); // TL, zone nibble 1 c.xzon.at(21, 20) = std::uint8_t(rotl_corners(0x20, k) | 1); // BL c.xzon.at(21, 21) = std::uint8_t(rotl_corners(0x40, k) | 1); // BR c.xzon.at(20, 21) = std::uint8_t(rotl_corners(0x80, k) | 1); // TR sc2k::normalize_save_compass(c); char msg[64]; std::snprintf(msg, sizeof msg, "compass-%d save: corner bits un-rotated, nibble kept", k); CHECK(c.xzon.at(20, 20) == 0x11 && c.xzon.at(20, 21) == 0x81 && c.xzon.at(21, 20) == 0x21 && c.xzon.at(21, 21) == 0x41, msg); }
// 2) the data rules over the real CD cities (the oracle both rules were derived from) const char* root = std::getenv("SC2K_DATA_DIR"); const std::string dir = (root && *root ? std::string(root) : test_root()) + "/CITIES"; if (std::filesystem::exists(dir)) { long nNorm = 0, nRaw = 0; const double norm = rule_agreement(dir, false, nNorm); const double raw = rule_agreement(dir, true, nRaw); std::printf("axis->bit agreement over %ld strip tiles: normalized %.1f%%, raw %.1f%%\n", nNorm, norm, raw); CHECK(nNorm > 1000, "enough runway/pier strip tiles to be meaningful"); CHECK(norm >= 98.0, "normalized saves obey the compass-0 axis->mirror rule"); CHECK(raw <= 90.0, "sensitivity: as-stored, the odd-compass saves break the mirror rule");
long cNorm = 0, cRaw = 0; const double cn = corner_agreement(dir, false, cNorm); const double cr = corner_agreement(dir, true, cRaw); std::printf("corner-bit agreement over %ld complete blocks: normalized %.1f%%, raw %.1f%%\n", cNorm, cn, cr); CHECK(cNorm > 1000, "enough complete multi-tile blocks to be meaningful"); CHECK(cn >= 99.0, "normalized saves carry rotation-0 corner bits"); CHECK(cr <= 60.0, "sensitivity: as-stored, the rotated saves break the corner rule");
// 3) FOURCITI ground truth (compass-2 save; the user-reported demolish/render breaker): // load_sc2 normalizes, remembers the compass, and the 2×2 at (93,123) reads rotation-0. const std::string four = original_cd_root() + "/WIN31/CITIES/FOURCITI.SC2"; if (std::filesystem::exists(four)) { City c; CHECK(sc2k::io::load_sc2(four, c).ok, "FOURCITI loads"); CHECK(c.savedCompass == 2, "FOURCITI remembers its saved compass (2)"); CHECK(c.misc_be32(sc2k::misc::kViewRotation) == 0, "FOURCITI: 0x08 stamped to 0"); CHECK(c.xbld.at(93, 123) == 0x9F && c.xbld.at(94, 124) == 0x9F, "FOURCITI: the 2x2 at (93,123)"); CHECK((c.xzon.at(93, 123) & 0xF0) == 0x10 && (c.xzon.at(93, 124) & 0xF0) == 0x80 && (c.xzon.at(94, 123) & 0xF0) == 0x20 && (c.xzon.at(94, 124) & 0xF0) == 0x40, "FOURCITI: its corner bits read rotation-0 after load"); CHECK((c.xzon.at(93, 125) & 0xF0) == 0x00, "FOURCITI: the lone Large Factory tile stays cornerless"); } } else { std::printf("skip: %s not present\n", dir.c_str()); }
if (fails) { std::printf("%d FAILURES\n", fails); return 1; } std::printf("ALL TESTS PASSED\n"); return 0;}