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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361// Numeric verification of the map-editor terrain primitives measured from the original game (spec// Raise/Lower Sea Level (sim/sea_level.hpp reflood),// Stretch (sim/terrain_edit.hpp stretch_terrain), and Place Stream (ui/tools.hpp// place_stream, the downhill tracer). Synthetic cities, no assets.#include "sc2k/io/sc2_file.hpp"#include "sc2k/sim/sea_level.hpp"#include "sc2k/sim/terrain_edit.hpp"#include "sc2k/sim/terrain_gen.hpp" // make_new_city / start_game_on_map#include "sc2k/ui/tools.hpp"#include <cstdio>#include <cstring>#include <fstream>#include "test_paths.hpp"
using namespace sc2k;
#include "test_harness.hpp"
static void flatten(City& c, int alt) { for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) c.altm.at(r, col) = std::uint16_t(alt);}static bool is_water(const City& c, int r, int col) { return (c.xbit.at(r, col) & 0x04) != 0; }
int main() { // ---- Raise / Lower Sea Level: a low basin floods when the sea rises over it, dries when it drops ---- { City c; flatten(c, 5); for (int r = 40; r < 50; ++r) for (int col = 40; col < 50; ++col) c.altm.at(r, col) = 2; // a basin c.set_misc_be32(sc2k::misc::kWaterLevel, 1); sim::reflood_to_level(c, 1); // sea level 1: basin (alt 2) is dry land CHECK(!is_water(c, 45, 45)); CHECK(c.water_level() == 1);
CHECK(sim::raise_sea_level(c) == 2); // 1 -> 2 : still above the basin floor CHECK(!is_water(c, 45, 45)); CHECK(sim::raise_sea_level(c) == 3); // 2 -> 3 : now over the alt-2 basin CHECK(is_water(c, 45, 45)); // basin flooded CHECK(c.xter.at(45, 45) >= 0x10); // submerged XTER variant CHECK(!is_water(c, 10, 10)); // the alt-5 plain stays dry
CHECK(sim::lower_sea_level(c) == 2); // 3 -> 2 : basin drains again CHECK(!is_water(c, 45, 45)); CHECK(c.xter.at(45, 45) == 0); // back to flat dry land } // ---- Sea level clamps: cannot drop below 0 or rise above 31 ---- { City c; flatten(c, 5); c.set_misc_be32(sc2k::misc::kWaterLevel, 0); CHECK(sim::lower_sea_level(c) == 0); // already 0 → stays c.set_misc_be32(sc2k::misc::kWaterLevel, 31); CHECK(sim::raise_sea_level(c) == 31); // already 31 → stays } // ---- Stretch: pulls a SMOOTH cone (neighbour-smoothing), not a lone pillar ---- { City c; flatten(c, 5); CHECK(sc2k::stretch_terrain(c, 64, 64, /*newAlt=*/15, /*startAlt=*/5)); const int peak = c.altm.at(64, 64) & 0x1F; CHECK(peak >= 13); // the clicked tile rose toward 15 (smoother may settle ±1) int raised = 0; // the area around it was pulled up, not left a pillar for (auto [dr, dc] : {std::pair{-1, 0}, {1, 0}, {0, -1}, {0, 1}}) if ((c.altm.at(64 + dr, 64 + dc) & 0x1F) > 6) ++raised; CHECK(raised == 4); // …and the result is 1-Lipschitz (no cliffs): every cardinal step differs by ≤ 1 over the cone. int maxStep = 0; for (int r = 50; r < 79; ++r) for (int col = 50; col < 79; ++col) { const int a = c.altm.at(r, col) & 0x1F; maxStep = std::max(maxStep, std::abs(a - (c.altm.at(r, col + 1) & 0x1F))); maxStep = std::max(maxStep, std::abs(a - (c.altm.at(r + 1, col) & 0x1F))); } CHECK(maxStep <= 1); // smooth mountain, no cliff faces // gates: above 30 rejected; more than 10 below the drag-start rejected CHECK(!sc2k::stretch_terrain(c, 20, 20, /*newAlt=*/31, /*startAlt=*/5)); CHECK(!sc2k::stretch_terrain(c, 20, 20, /*newAlt=*/3, /*startAlt=*/15)); // 3 < 15-10 } // ---- Lowering a tile below sea level makes WATER with its surface-level field set (not a floor hole) ---- { City c; flatten(c, 6); c.set_misc_be32(sc2k::misc::kWaterLevel, 4); // sea level 4 c.altm.at(60, 60) = 2; // a tile dug below the sea sc2k::terrain_autotile_cell(c, 60, 60, 4); CHECK(c.xbit.at(60, 60) & 0x04); // flagged as water CHECK(((c.altm.at(60, 60) >> 5) & 0x1F) == 4); // water-LEVEL field = sea level (else it draws at the floor) } // ---- Place Stream: from the top of a downhill ramp, carves a run of surface water that descends ---- { City c; flatten(c, 20); for (int r = 0; r < City::kDim; ++r) // a monotonic N→S downhill ramp for (int col = 0; col < City::kDim; ++col) c.altm.at(r, col) = std::uint16_t(std::clamp(20 - r / 4, 1, 31)); int before = 0; for (int i = 0; i < City::kTiles; ++i) before += (c.xbit.cells[i] & 0x04) ? 1 : 0; ui::detail::place_stream(c, 5, 64, 100); CHECK(is_water(c, 5, 64)); // the source tile became water int after = 0; for (int i = 0; i < City::kTiles; ++i) after += (c.xbit.cells[i] & 0x04) ? 1 : 0; CHECK(after - before >= 4); // it traced a multi-tile stream downhill }
// ---- Editor "Done" → start a game ON the edited map: keep terrain + water level, reset the economy ---- { City c = make_new_city(0x4242, 0, 1, 20, 20, 20, 1, 1); // a "sculpted" map (terrain + a sea level) c.set_funds(999999); // mess up the economy as if edited const auto savedAltm = c.altm.cells; // remember the terrain const auto savedXter = c.xter.cells; const int savedWl = c.water_level(); start_game_on_map(c, 0x4242, 2, 2); // era 2000 / medium CHECK(c.altm.cells == savedAltm); // terrain preserved CHECK(c.xter.cells == savedXter); CHECK(c.water_level() == savedWl); // edited sea level preserved CHECK(c.start_year() == 2000); // new economy applied CHECK(c.funds() == 10000); // medium difficulty treasury (reset) CHECK(c.auto_goto() && !c.auto_budget() && !c.no_disasters()); }
// ---- Edit New Map from a LOADED CITY: wiped down to terrain/water/trees (the original's editor bring-up) ---- { City c = make_new_city(0x1234, 1, 1, 20, 10, 25, 0, 1); // river map with trees c.name = "Metropolis"; // Build a little city on it: zones, buildings, networks, underground, signs, coverage, a Thing. c.xbld.at(10, 10) = 0x70; c.xzon.at(10, 10) = 0x19; // a building on a zoned tile c.xbld.at(10, 11) = 0x1D; c.xbit.at(10, 11) |= 0xF8; // a road, fully serviced bits c.xbld.at(10, 12) = 0x01; // rubble c.xbld.at(10, 13) = 0x05; // radioactive waste c.xund.at(20, 20) = 0x02; // a pipe underground c.altm.at(30, 30) |= 0x2C00; // stray ALTM high flag bits c.xtxt.at(40, 40) = 3; c.xlab[3 * 25] = 5; // a sign with label text std::memcpy(&c.xlab[3 * 25 + 1], "Hello", 5); c.xtrf.at(5, 5) = 99; c.xplt.at(5, 5) = 99; c.xval.at(5, 5) = 99; c.xcrm.at(5, 5) = 99; c.xplc.at(2, 2) = 88; c.xfir.at(2, 2) = 88; c.xpop.at(2, 2) = 88; c.xrog.at(2, 2) = 3; c.xthg[1 * 12 + 0] = 1; // an airborne Thing in slot 1 c.xmic[0] = 0xEC; const auto savedXter = c.xter.cells; // the landscape that must survive const int savedWl = c.water_level(); int treesBefore = 0, waterBefore = 0; for (int i = 0; i < City::kTiles; ++i) { treesBefore += (c.xbld.cells[i] >= 0x06 && c.xbld.cells[i] <= 0x0C) ? 1 : 0; waterBefore += (c.xbit.cells[i] & 0x04) ? 1 : 0; }
wipe_city_to_terrain(c);
CHECK(c.xter.cells == savedXter); // terrain slopes untouched CHECK(c.water_level() == savedWl); // sea level untouched int treesAfter = 0, waterAfter = 0; bool devLeft = false; for (int i = 0; i < City::kTiles; ++i) { const std::uint8_t b = c.xbld.cells[i]; treesAfter += (b >= 0x06 && b <= 0x0C) ? 1 : 0; waterAfter += (c.xbit.cells[i] & 0x04) ? 1 : 0; if ((b != 0 && (b < 0x06 || b >= 0x0D)) || c.xzon.cells[i] || c.xund.cells[i] || c.xtxt.cells[i] || (c.xbit.cells[i] & ~0x05) || (c.altm.cells[i] & ~0x3FF)) devLeft = true; } CHECK(treesAfter == treesBefore); // every tree survives… CHECK(treesAfter > 0); CHECK(waterAfter == waterBefore); // …and every water tile CHECK(!devLeft); // NOTHING built survives, above or below ground CHECK(c.xbld.at(10, 12) == 0 && c.xbld.at(10, 13) == 0); // rubble + radioactive cleared too bool minimapLeft = false; for (int i = 0; i < 64 * 64; ++i) if (c.xtrf.cells[i] || c.xplt.cells[i] || c.xval.cells[i] || c.xcrm.cells[i]) minimapLeft = true; for (int i = 0; i < 32 * 32; ++i) if (c.xplc.cells[i] || c.xfir.cells[i] || c.xpop.cells[i] || c.xrog.cells[i] != 127) minimapLeft = true; CHECK(!minimapLeft); // sim fields cleared, XROG at the 127 baseline bool arraysLeft = false; for (std::uint8_t v : c.xlab) if (v) arraysLeft = true; // signs' text gone for (std::uint8_t v : c.xmic) if (v) arraysLeft = true; // microsims gone for (std::uint8_t v : c.xthg) if (v) arraysLeft = true; // Things (planes/ships/…) gone CHECK(!arraysLeft); CHECK(c.misc_be32(sc2k::misc::kTileHistogram) == 0x4000); // dwTileCount histogram reset CHECK(c.name.empty()); // the original's reset empties the name }
// ---- Edit New Map from a city WITH BRIDGES: the ramp wedges are cleared too (deviation) ---- { using namespace sc2k::ui::detail; // A river channel (land plateau L+2, water ground L, sea L+1) crossed E-W by a span-4 road bridge. auto build_bridged = [](City& c) { const int L = 5, R = 40, C = 30, W = 4, dir = 2; // dir 2 = East const int anchorNib[4] = {4, 1, 2, 3}; c.set_misc_be32(sc2k::misc::kWaterLevel, L + 1); for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) { c.xter.at(r, col) = 0; c.xbld.at(r, col) = 0; c.xbit.at(r, col) = 0; c.altm.at(r, col) = std::uint16_t(L + 2); } const int dR = kBrDRow[dir], dC = kBrDCol[dir]; const int pR = (dir & 1) ? 0 : 1, pC = (dir & 1) ? 1 : 0; for (int i = 0; i < W; ++i) for (int p = -1; p <= 1; ++p) { const int r = R + i * dR + p * pR, col = C + i * dC + p * pC; c.xbit.at(r, col) = 0x04; c.altm.at(r, col) = std::uint16_t(L | ((L + 2) << 5)); c.xter.at(r, col) = std::uint8_t((i == 0 && p == 0) ? (0x20 + anchorNib[dir]) : 0x12); } const auto sp = bridge_probe(c, R, C, dir); bridge_lay(c, sp, /*suspension*/ 2); return sp; };
City c; const auto sp = build_bridged(c); CHECK(sp.status == BridgeStatus::Ok && sp.span == 4); // Post-lay: entrance ramp (40,30) + exit ramp (40,33) are raised DRY land on a slope; decks between. CHECK((c.xbit.at(40, 30) & 0x04) == 0 && c.xter.at(40, 30) >= 1 && c.xter.at(40, 30) <= 4); CHECK((c.xbit.at(40, 33) & 0x04) == 0 && c.xter.at(40, 33) >= 1 && c.xter.at(40, 33) <= 4); CHECK(c.xbld.at(40, 31) != 0 && c.xbld.at(40, 32) != 0); // deck art present
wipe_city_to_terrain(c); // default: strip the ramps
CHECK((c.xbit.at(40, 30) & 0x04) != 0); // entrance ramp → water CHECK((c.xbit.at(40, 33) & 0x04) != 0); // exit ramp → water CHECK((c.xbit.at(40, 31) & 0x04) != 0); // deck tiles → water (bit kept, XBLD cleared) bool wedgeLeft = false; // no dry network-slope wedge anywhere on the span for (int col = 29; col <= 34; ++col) { const int x = c.xbld.at(40, col); if (x >= 0x0E && x <= 0x3A) wedgeLeft = true; // a leftover road/rail/power piece if (!(c.xbit.at(40, col) & 0x04) && c.xter.at(40, col) >= 1 && c.xter.at(40, col) <= 4 && col >= 30 && col <= 33) wedgeLeft = true; // a dry raised slope where the bridge was } CHECK(!wedgeLeft);
// ---- strictly-faithful wipe (stripRamps=false): LEAVES the ramp wedges ---- City c2; build_bridged(c2); wipe_city_to_terrain(c2, /*stripRamps=*/false); CHECK((c2.xbit.at(40, 30) & 0x04) == 0); // entrance ramp stays dry raised land… CHECK(c2.xter.at(40, 30) >= 1 && c2.xter.at(40, 30) <= 4); // …still a slope (the faithful artifact) CHECK(c2.xbld.at(40, 30) == 0); // (its network was still cleared by the wipe) }
// ---- Ramp shapes as they appear in REAL saves: a bare-XBLD line ramp + a highway ramp ---- { // (a) An original-game-style line-bridge ramp: a raised SLOPE with NO network (XBLD 0), water-cleared, at the // shore of a water body with a deck spanning off it (mirrors CAPEQUES (20,56)). The ramp faces the // water on its span side (lower), so it is a genuine shore ramp — not a symmetric pit — and must // revert to water. City c; c.set_misc_be32(sc2k::misc::kWaterLevel, 6); // sea level 6 for (int r = 0; r < City::kDim; ++r) // land to the NORTH (rows 0..19)… for (int col = 0; col < City::kDim; ++col) { c.altm.at(r, col) = 6; c.xter.at(r, col) = 0; c.xbit.at(r, col) = 0; } for (int r = 20; r < City::kDim; ++r) // …open water to the south (rows 20+) for (int col = 0; col < City::kDim; ++col) { c.xbit.at(r, col) = 0x04; c.altm.at(r, col) = std::uint16_t(5 | (6 << 5)); c.xter.at(r, col) = 0x11; } c.xbld.at(21, 50) = 0x5C; c.xbit.at(21, 50) |= 0x02; // a bridge deck out over the water c.xbld.at(20, 50) = 0x00; c.xter.at(20, 50) = 0x03; // the BARE ramp (no network) at the shore, c.xbit.at(20, 50) &= ~0x04; c.altm.at(20, 50) = 6; // dry & raised, water on 3 sides — the original's shape wipe_city_to_terrain(c); CHECK((c.xbit.at(20, 50) & 0x04) != 0); // bare ramp → water CHECK(c.xter.at(20, 50) >= 0x10); // …with a water/coast XTER, not a slope
// (b) A highway on-ramp: XBLD 0x61 with a faked slope/cube XTER on FLAT ground (mirrors CAPEQUES (47,4)). City h; h.set_misc_be32(sc2k::misc::kWaterLevel, 6); for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) { h.altm.at(r, col) = 6; h.xter.at(r, col) = 0; h.xbit.at(r, col) = 0; } h.xbld.at(47, 4) = 0x61; h.xter.at(47, 4) = 0x01; // slope-code artifact on flat land h.xbld.at(46, 4) = 0x61; h.xter.at(46, 4) = 0x0D; // cube-code artifact on flat land wipe_city_to_terrain(h); CHECK(h.xter.at(47, 4) == 0 && h.xbld.at(47, 4) == 0); // collapses back to flat land CHECK(h.xter.at(46, 4) == 0 && h.xbld.at(46, 4) == 0); }
// ---- Regression on the real save the user cited: CAPEQUES bridge + highway ramps all clear ---- { const std::string path = city_file("CAPEQUES.SC2"); std::ifstream probe(path); if (probe.good()) { City c; if (io::load_sc2(path, c).ok) { wipe_city_to_terrain(c); auto not_slope = [&](int r, int col) { return c.xter.at(r, col) < 1 || c.xter.at(r, col) > 4; }; for (auto [r, col] : {std::pair{20, 56}, {31, 56}, {108, 107}, {115, 107}}) // road-bridge ramps CHECK(not_slope(r, col) && ((c.xbit.at(r, col) & 0x04) != 0)); // → water, no wedge for (auto [r, col] : {std::pair{47, 4}, {47, 5}, {46, 4}, {46, 5}}) // highway ramps CHECK(not_slope(r, col) && c.xbld.at(r, col) == 0); // → flat land } } else { std::printf("test_map_editor: (CAPEQUES not present — skipped real-save ramp check)\n"); } }
// ---- terrain_generate keeps the sea level in lockstep with the water it lays (else edits punch holes) ---- { // The map-editor "Generate" bug: enter with one water slider, Generate with another. terrain_generate // must leave water_level() == the surface field it wrote to every water tile, so the raise/lower/level // autotiler (which resets each edited tile's surface to water_level()) keeps the water FLUSH — not // collapsed into a dry pit. City c = make_new_city(0x1111, 0, 1, 8, /*water*/ 5, 5, 1, 1); // enter the editor with a low water level const int wl = terrain_generate(c, 0x2222, 8, /*water*/ 45, 5, 1, 1); // Generate a mostly-water map CHECK(c.water_level() == wl); // sea level tracks the regenerated water
// Every water tile's surface field equals the sea level (a consistent single global water level). int R = -1, C = -1; for (int r = 5; r < 123 && R < 0; ++r) for (int col = 5; col < 123; ++col) { if (!(c.xbit.at(r, col) & 0x04)) continue; CHECK(((c.altm.at(r, col) >> 5) & 0x1F) == wl); // no raised/sunken water surfaces int wn = 0; // find an OPEN-water tile to edit for (int dr = -2; dr <= 2; ++dr) for (int dc = -2; dc <= 2; ++dc) if (c.xbit.at(r + dr, col + dc) & 0x04) ++wn; if (R < 0 && wn == 25) { R = r; C = col; } } // Lowering open water keeps its surface flush at the sea level (the bug dropped it to a stale level → hole). if (R >= 0) { const int surfBefore = (c.altm.at(R, C) >> 5) & 0x1F; for (int i = 0; i < 5; ++i) sc2k::lower_terrain(c, R, C); CHECK((c.xbit.at(R, C) & 0x04) != 0); // still water… CHECK(((c.altm.at(R, C) >> 5) & 0x1F) == surfBefore); // …at the SAME surface level (no collapse) } }
if (failures == 0) std::printf("test_map_editor: all checks passed\n"); else std::printf("test_map_editor: %d FAILURES\n", failures); return failures ? 1 : 0;}