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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142// Numeric verification of compass rotation (render/rotate.hpp) — NOT by eye. The faithful invariants:// each code table is an order-4 permutation, four quarter-turns of a city return it exactly (incl. the// bridge/ramp mirror-bit cycle), and a tile lands where rotate_index_cw says with its code remapped.#include "sc2k/io/sc2_file.hpp"#include "sc2k/render/rotate.hpp"#include <array>#include <cstdio>#include <string>#include "test_paths.hpp"
using namespace sc2k;using namespace sc2k::render;
#include "test_harness.hpp"
// Compose a 256-entry LUT with itself n times.static std::array<std::uint8_t, 256> compose(const std::array<std::uint8_t, 256>& t, int n) { std::array<std::uint8_t, 256> out{}; for (int i = 0; i < 256; ++i) out[i] = std::uint8_t(i); for (int k = 0; k < n; ++k) { auto cur = out; for (int i = 0; i < 256; ++i) out[i] = t[cur[i]]; } return out;}
int main() { // (1) Each rotation table is an order-4 permutation: composed 4x == identity, and it's a bijection. for (const auto* tbl : {&xbld_rot_cw(), &xter_rot_cw()}) { const auto id4 = compose(*tbl, 4); std::array<int, 256> seen{}; for (int i = 0; i < 256; ++i) { CHECK(id4[i] == i); ++seen[(*tbl)[i]]; } for (int i = 0; i < 256; ++i) CHECK(seen[i] == 1); // bijection }
// (2) rotate_index_cw is order-4 on positions: four turns return every tile to itself, and one // turn is a bijection over the 128x128 grid. { std::array<std::uint8_t, City::kTiles> hit{}; for (int r = 0; r < City::kDim; ++r) for (int c = 0; c < City::kDim; ++c) { int r1, c1; rotate_index_cw(r, c, r1, c1); int r2, c2; rotate_index_cw(r1, c1, r2, c2); int r3, c3; rotate_index_cw(r2, c2, r3, c3); int r4, c4; rotate_index_cw(r3, c3, r4, c4); CHECK(r4 == r && c4 == c); CHECK(r1 >= 0 && r1 < City::kDim && c1 >= 0 && c1 < City::kDim); ++hit[r1 * City::kDim + c1]; } for (auto h : hit) CHECK(h == 1); }
// (3) The bridge/ramp mirror-bit cycle returns to identity after 4 turns. Synthesize a city with // those codes (real saves may have none, so test the hard path explicitly). { City c; // clang-format off std::uint8_t codes[] = {0x51,0x52,0x53,0x55,0x5a,0x5d,0x5e,0x5f,0x60,0x6a,0x6b,0x80,0x1d,0x0d}; // clang-format on int k = 0; for (std::uint8_t code : codes) { c.xbld.at(0, k) = code; c.xbit.at(0, k) = 0x00; // no mirror c.xbld.at(1, k) = code; c.xbit.at(1, k) = 0x02; // mirror set ++k; } const City r4 = rotate_city(c, 4); for (int i = 0; i < k; ++i) for (int row = 0; row <= 1; ++row) { CHECK(r4.xbld.at(row, i) == c.xbld.at(row, i)); CHECK((r4.xbit.at(row, i) & 0x02) == (c.xbit.at(row, i) & 0x02)); } }
// (4) On a real city: four turns are an exact identity across all rendered layers. const std::string cityPath = first_present_city({"TOKYO.SC2", "LONDON.SC2", "BAYVIEW.SC2"}); if (!cityPath.empty()) { City c; if (io::load_sc2(cityPath, c).ok) { const City r4 = rotate_city(c, 4); long diffs = 0; for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) { diffs += (r4.xter.at(r, col) != c.xter.at(r, col)); diffs += (r4.xbld.at(r, col) != c.xbld.at(r, col)); diffs += (r4.xbit.at(r, col) != c.xbit.at(r, col)); diffs += (r4.altm.at(r, col) != c.altm.at(r, col)); } CHECK(diffs == 0);
// One turn relocates tile (10,20) to (127-20, 10) = (107,10) with terrain code remapped. int dr, dc; rotate_index_cw(10, 20, dr, dc); CHECK(dr == 107 && dc == 10); const City r1 = rotate_city(c, 1); CHECK(r1.xter.at(dr, dc) == xter_rot_cw()[c.xter.at(10, 20)]);
std::printf("rotate: 4-turn identity exact on %s; tile (10,20)->(%d,%d)\n", cityPath.c_str(), dr, dc); } } else { std::printf("rotate: (no city file for the 4-turn-identity check; table checks ran)\n"); }
// (5) Visible water-main ownership and hidden imported links rotate independently with their // tiles and survive a complete turn cycle. { City c; c.modernWaterSystem = true; c.waterMains.at(10, 20) = 1; c.legacyWaterLinks.at(11, 20) = 1; const City r1 = rotate_city(c, 1); CHECK(r1.waterMains.at(107, 10) == 1); CHECK(r1.legacyWaterLinks.at(107, 11) == 1); CHECK(rotate_city(c, 4).waterMains.at(10, 20) == 1); CHECK(rotate_city(c, 4).legacyWaterLinks.at(11, 20) == 1); }
// (6) compass_mirror: buildings flip on odd compass only; networks never. CHECK(!compass_mirror(0, 0xC6)); CHECK(compass_mirror(1, 0xC6)); CHECK(!compass_mirror(2, 0xC6)); CHECK(compass_mirror(3, 0xC6)); CHECK(compass_mirror(1, 0x6c)); CHECK(!compass_mirror(1, 0x1d)); // road never flips
if (failures == 0) { std::printf("rotate: all checks passed\n"); return 0; } std::printf("rotate: %d failure(s)\n", failures); return 1;}