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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444// Highways — the 2×2-block path solver (sim/highway_path.hpp) + autotiler (sim/highway_autotile.hpp), Phase 5// (docs/ref/re/reimpl/10-highways.md). Numeric, asset-free: a faithful port of the original highway drag engine// These checks pin the flat-ground behaviour the "flat network foundation" delivers —// classify validity, straight/curve/tee/4-way piece selection, crossings over road/rail/power, the block// path solver (count, stop-at-building, parallel-rail block), and the $100/block cost — verified numerically// rather than by eye (the slope/ramp + water-bridge branches are ported but belong to a later phase).#include "sc2k/sim/highway_path.hpp"#include "sc2k/sim/terrain_gen.hpp" // build valid slope terrain (the real autotiler) for the ramp checks#include "sc2k/ui/tools.hpp" // apply_tool (Bulldoze) for the demolish-cleanup check#include <cstdio>
using namespace sc2k;using sim::solve_highway_path;using sim::place_highway_block;namespace hd = sc2k::sim::hwydetail;
#include "test_harness.hpp"
static int xb(const City& c, int r, int col) { return c.xbld.at(r, col); }
int main() { // ---- 1. classify: a flat empty block is open (15); buildings / rail-curves invalidate it; road/rail/power // straights are crossable (still valid). ---- { City c; CHECK(hd::classify(c, 10, 10) == 15); // flat empty → flat-open c.xbld.at(10, 10) = 0x90; CHECK(hd::classify(c, 10, 10) == hd::HWY_INVALID); // building blocks City d; d.xbld.at(20, 20) = 0x30; CHECK(hd::classify(d, 20, 20) == hd::HWY_INVALID); // rail curve (0x30) blocks City e; e.xbld.at(30, 30) = 0x1D; // E-W road straight → crossable CHECK(hd::classify(e, 30, 30) == 15); e.xbld.at(31, 31) = 0x0E; // power straight → crossable CHECK(hd::classify(e, 30, 30) == 15); CHECK(hd::block_alt(c, 10, 10) == 0); // flat sea-level block }
// ---- 2. Isolated straight by drag direction: E/W drag → 0x49 (E-W) on all 4 tiles; S/N drag → 0x4A. ---- { City c; CHECK(place_highway_block(c, 10, 10, /*dir=*/2)); // 2 = East CHECK(xb(c, 10, 10) == 0x49 && xb(c, 10, 11) == 0x49 && xb(c, 11, 10) == 0x49 && xb(c, 11, 11) == 0x49); City d; CHECK(place_highway_block(d, 10, 10, /*dir=*/1)); // 1 = South CHECK(xb(d, 10, 10) == 0x4A && xb(d, 10, 11) == 0x4A && xb(d, 11, 10) == 0x4A && xb(d, 11, 11) == 0x4A); }
// ---- 3. Even-snap: a click anywhere in a 2×2 lays the block at the even anchor. ---- { City c; place_highway_block(c, 11, 13, 2); // snaps to (10,12) CHECK(xb(c, 10, 12) == 0x49 && xb(c, 11, 13) == 0x49); }
// ---- 4. Straight E-W drag of 3 blocks: solver yields 3 even-spaced blocks, all reach, all 0x49. ---- { City c; auto p = solve_highway_path(c, 10, 10, 10, 14); CHECK(p.valid && p.blocks.size() == 3); CHECK(p.blocks[0].col == 10 && p.blocks[1].col == 12 && p.blocks[2].col == 14); CHECK(p.endRow == 10 && p.endCol == 14); const int laid = sim::commit_highway_path(c, p); CHECK(laid == 3); // $100 each = $300 bool ok = true; for (int col = 10; col <= 15; ++col) for (int r = 10; r <= 11; ++r) if (xb(c, r, col) != 0x49) ok = false; CHECK(ok); }
// ---- 5. L-bend corner → a curve piece. Build (10,10)-E-(10,12)-S-(12,12); the corner (10,12) connects // West + South → shape 8 → XBLD 0x65. ---- { City c; place_highway_block(c, 10, 10, 2); // anchor place_highway_block(c, 10, 12, 2); // east of it (E-W straight) place_highway_block(c, 12, 12, 1); // south of the second (re-autotiles the corner) CHECK(xb(c, 10, 12) == 0x65); // W+S corner curve CHECK(xb(c, 10, 10) == 0x49); // tail stays an E-W straight CHECK(xb(c, 12, 12) == 0x4A); // leg is a N-S straight }
// ---- 6. The other three corner orientations (S+E, E+N, W+N) → 0x66 / 0x67 / 0x68. ---- { City c; // S+E corner at (10,10): leg north (8,10) + leg east (10,12) place_highway_block(c, 8, 10, 1); place_highway_block(c, 10, 12, 2); place_highway_block(c, 10, 10, 2); // the corner, connecting N + E // (10,10) connects N(8,10) + E(10,12) → E+N corner → 0x67 CHECK(xb(c, 10, 10) == 0x67); }
// ---- 7. Tee / 4-way: a block with 3+ connections → 0x69. ---- { City c; place_highway_block(c, 10, 10, 2); // centre place_highway_block(c, 10, 8, 2); // W place_highway_block(c, 10, 12, 2); // E place_highway_block(c, 12, 10, 1); // S → centre now W+E+S CHECK(xb(c, 10, 10) == 0x69); }
// ---- 8. Crossing a perpendicular N-S road: the on-road tiles become 0x4B, the rest stay 0x49; the road // orientation forces the highway to run E-W. ---- { City c; c.xbld.at(10, 11) = 0x1E; c.xbld.at(11, 11) = 0x1E; // a N-S road through the block's right column place_highway_block(c, 10, 10, /*dir=*/2); CHECK(xb(c, 10, 11) == 0x4B && xb(c, 11, 11) == 0x4B); // highway × N-S road CHECK(xb(c, 10, 10) == 0x49 && xb(c, 11, 10) == 0x49); // plain E-W straight off the road }
// ---- 9. Crossing rail / power straights → 0x4D / 0x4F (N-S variants). ---- { City c; c.xbld.at(10, 11) = 0x2D; // N-S rail straight place_highway_block(c, 10, 10, 2); CHECK(xb(c, 10, 11) == 0x4D); // highway × N-S rail City d; d.xbld.at(10, 11) = 0x0F; // N-S power straight place_highway_block(d, 10, 10, 2); CHECK(xb(d, 10, 11) == 0x4F); // highway × N-S power }
// ---- 10. Solver stops at a building: an unbuildable block ahead → the path ends short (no partial commit). ---- { City c; c.xbld.at(10, 12) = 0x90; // a building fouls block (10,12) auto p = solve_highway_path(c, 10, 10, 10, 14); CHECK(p.valid && p.blocks.size() == 1); // only the anchor CHECK(!(p.endRow == 10 && p.endCol == 14)); // did NOT reach the requested end }
// ---- 11. Solver refuses to run along a PARALLEL rail (can't sit on top of it) but the start is fine. ---- { City c; c.xbld.at(10, 12) = 0x2C; c.xbld.at(11, 12) = 0x2C; // E-W rail straights in the next block auto p = solve_highway_path(c, 10, 10, 10, 14); // an E-W drag onto parallel E-W rail CHECK(p.blocks.size() == 1); // blocked before stepping onto the rail block }
// ---- 12. An invalid anchor yields an invalid (empty-ish) path that commits nothing. ---- { City c; c.xbld.at(10, 10) = 0x90; // building on the anchor block auto p = solve_highway_path(c, 10, 10, 10, 14); CHECK(!p.valid); CHECK(sim::commit_highway_path(c, p) == 0); }
// ---- 13. Re-laying highway over existing highway is FREE — clicking on highway must not drain funds. ---- { City c; auto p = solve_highway_path(c, 10, 10, 10, 14); CHECK(sim::commit_highway_path(c, p) == 3); // 3 NEW blocks the first time auto again = solve_highway_path(c, 10, 10, 10, 10); // click on an existing highway block CHECK(again.blocks.size() == 1); CHECK(sim::commit_highway_path(c, again) == 0); // already highway → 0 charged blocks (no-op) auto redrag = solve_highway_path(c, 10, 10, 10, 14); // re-drag the whole existing line CHECK(sim::commit_highway_path(c, redrag) == 0); // nothing new → free }
// ---- 14. Parallel-merge: a documented deviation — parallel highways stay separate (the nicer behaviour), // but a DELIBERATE 4-way still interchanges (it uses the same-orientation connect terms). ---- { City c; // default = no parallel merge for (int col = 20; col <= 26; col += 2) place_highway_block(c, 10, col, 2); for (int col = 20; col <= 26; col += 2) place_highway_block(c, 12, col, 2); // parallel, one block south bool any69 = false; for (int col = 20; col <= 27; ++col) if (xb(c, 10, col) == 0x69 || xb(c, 12, col) == 0x69) any69 = true; CHECK(!any69); // default: no parallel merge CHECK(xb(c, 10, 22) == 0x49 && xb(c, 12, 22) == 0x49); // both stay clean E-W straights place_highway_block(c, 40, 40, 2); place_highway_block(c, 40, 38, 2); place_highway_block(c, 40, 42, 2); place_highway_block(c, 38, 40, 1); place_highway_block(c, 42, 40, 1); CHECK(xb(c, 40, 40) == 0x69); // a DELIBERATE 4-way still interchanges }
// ---- 15. Elevated-ramp WRITER, validated vs real Win95 cities (CAPEQUES/HALENAPT/CENTERVL/HILTON): forcing // each ramp shape 4-7 lays XBLD 0x61-0x64 with the EXACT XTER skirt the original tool produces. ---- { auto skirt = [](int shape, int alt) { City c; for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) c.altm.at(50 + dr, 50 + dc) = std::uint16_t(alt); hd::write_block(c, 50, 50, shape); return c; }; { City c = skirt(4, 4); // 0x61 — rows [13,1] (CAPEQUES (6,4)=0x0D / (7,4)=0x01) CHECK(xb(c, 50, 50) == 0x61 && xb(c, 51, 51) == 0x61); CHECK(c.xter.at(50, 50) == 13 && c.xter.at(50, 51) == 13 && c.xter.at(51, 50) == 1 && c.xter.at(51, 51) == 1); } { City c = skirt(5, 4); // 0x62 — cols [13,2] (HALENAPT (18,6)=0x0D / (18,7)=0x02) CHECK(xb(c, 50, 50) == 0x62); CHECK(c.xter.at(50, 50) == 13 && c.xter.at(51, 50) == 13 && c.xter.at(50, 51) == 2 && c.xter.at(51, 51) == 2); } { City c = skirt(6, 4); // 0x63 — rows [3,13] (CENTERVL (6,64)=0x03 / (7,64)=0x0D) CHECK(xb(c, 50, 50) == 0x63); CHECK(c.xter.at(50, 50) == 3 && c.xter.at(50, 51) == 3 && c.xter.at(51, 50) == 13 && c.xter.at(51, 51) == 13); } { City c = skirt(7, 4); // 0x64 — cols [4,13] (HILTON (2,14)=0x04 / (2,15)=0x0D) CHECK(xb(c, 50, 50) == 0x64); CHECK(c.xter.at(50, 50) == 4 && c.xter.at(51, 50) == 4 && c.xter.at(50, 51) == 13 && c.xter.at(51, 51) == 13); } // ALTM block-leveling: a block with one corner below the top is leveled WHOLE to (top−1). { City c; c.altm.at(50, 50) = 5; c.altm.at(50, 51) = 5; c.altm.at(51, 50) = 4; c.altm.at(51, 51) = 5; // top = 5 hd::write_block(c, 50, 50, 4); for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) CHECK((c.altm.at(50 + dr, 50 + dc) & 0x1F) == 4); } }
// ---- 16. SLOPE/RAMP end-to-end: a highway climbing a 1-level-per-2-rows ramp lays the correct DIRECTIONAL // ramp piece — ascending North → 0x61, South → 0x63, East → 0x64, West → 0x62 — with the real // tool's XTER skirt. Terrain built by the actual generator's slope autotiler so it's valid. ---- { auto ramp_city = [](bool nsAxis, bool rising) { sc2k::detail::TerrainGen tg; tg.g_wl = 0; for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) { const int k = nsAxis ? r : cc; int lvl = 4 + (rising ? (60 - k) / 2 : (k - 4) / 2); if (lvl < 4) lvl = 4; if (lvl > 20) lvl = 20; tg.A[r * 128 + cc] = lvl; tg.FL[r * 128 + cc] = 0; tg.XB[r * 128 + cc] = 0; tg.XT[r * 128 + cc] = 0; } for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) tg.autotile_cell(r, cc); City city; for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) { city.altm.at(r, cc) = std::uint16_t(tg.A[r * 128 + cc] & 0x1f); city.xter.at(r, cc) = tg.XT[r * 128 + cc]; } return city; }; auto count = [](const City& c, int code) { int n = 0; for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) if (c.xbld.at(r, cc) == code) ++n; return n; }; struct Dir { bool ns, rising; int ar, ac, er, ec, ramp; }; const Dir dirs[4] = { {true, true, 40, 40, 10, 40, 0x61}, // climb North → 0x61 {true, false, 10, 40, 40, 40, 0x63}, // climb South → 0x63 {false, false, 40, 10, 40, 40, 0x64}, // climb East → 0x64 {false, true, 40, 40, 40, 10, 0x62}, // climb West → 0x62 }; for (const auto& d : dirs) { City c = ramp_city(d.ns, d.rising); auto p = solve_highway_path(c, d.ar, d.ac, d.er, d.ec); CHECK(sim::commit_highway_path(c, p) > 0); CHECK(count(c, d.ramp) > 0); // the correct directional ramp piece appears for (int other : {0x61, 0x62, 0x63, 0x64}) // and NONE of the other three ramp directions do if (other != d.ramp) CHECK(count(c, other) == 0); } // the produced 0x61 ramp carries the ground-truth XTER skirt (rows [13,1]). { City c = ramp_city(true, true); auto p = solve_highway_path(c, 40, 40, 10, 40); sim::commit_highway_path(c, p); bool found = false; for (const auto& b : p.blocks) if (xb(c, b.row, b.col) == 0x61) { CHECK(c.xter.at(b.row, b.col) == 13 && c.xter.at(b.row + 1, b.col) == 1); // [13,1] CHECK((c.altm.at(b.row, b.col) & 0x1f) == (c.altm.at(b.row + 1, b.col) & 0x1f)); // uniform ALTM found = true; break; } CHECK(found); } // DEMOLISH cleanup: bulldozing a ramp clears the highway's XTER terrace caps (0x0D) — the original re-autotiles // the terrain (the slope autotiler) on demolish. (The leveled ALTM stays — terrain edits are permanent.) { City c = ramp_city(true, true); c.set_funds(1000000); auto p = solve_highway_path(c, 40, 40, 10, 40); sim::commit_highway_path(c, p); int capsDuring = 0; for (const auto& b : p.blocks) for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) if (c.xter.at(b.row + dr, b.col + dc) == 0x0D) ++capsDuring; CHECK(capsDuring > 0); // the deck laid terrace caps under itself for (const auto& b : p.blocks) ui::apply_tool(c, ui::Tool::Bulldoze, b.row, b.col); int capsAfter = 0, hwyAfter = 0; for (const auto& b : p.blocks) for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) { if (c.xter.at(b.row + dr, b.col + dc) == 0x0D) ++capsAfter; if (hd::is_highway_code(c.xbld.at(b.row + dr, b.col + dc))) ++hwyAfter; } CHECK(hwyAfter == 0); // all highway gone CHECK(capsAfter == 0); // and NO orphaned terrace caps left behind } }
// ---- 17. Demolishing a highway crossing RESTORES the road/rail/power underneath (not destroyed) — the // deviation the user asked for (the original rubbles the whole 2×2). The crossed net re-knits. ---- { City c; for (int rr = 9; rr <= 12; ++rr) c.xbld.at(rr, 11) = 0x1E; // a N-S road column through the block place_highway_block(c, 10, 10, /*dir=*/2); // E-W highway over it → 0x4B crossings CHECK(xb(c, 10, 11) == 0x4B && xb(c, 11, 11) == 0x4B); sc2k::ui::detail::demolish_highway(c, 10, 11); // bulldoze any tile of the block CHECK(sc2k::ui::detail::is_road(xb(c, 10, 11))); // the road is BACK (not destroyed) CHECK(sc2k::ui::detail::is_road(xb(c, 11, 11))); CHECK(xb(c, 10, 10) >= 1 && xb(c, 10, 10) <= 4); // the plain highway tiles → rubble (flat land) CHECK(xb(c, 11, 10) >= 1 && xb(c, 11, 10) <= 4); CHECK(sc2k::ui::detail::is_road(xb(c, 9, 11)) && sc2k::ui::detail::is_road(xb(c, 12, 11))); // column intact // rail / power restore the same way: City d; d.xbld.at(10, 11) = 0x2D; place_highway_block(d, 10, 10, 2); CHECK(xb(d, 10, 11) == 0x4D); sc2k::ui::detail::demolish_highway(d, 10, 10); CHECK(xb(d, 10, 11) == 0x2C || xb(d, 10, 11) == 0x2D); // rail back (re-autotiled) City e; e.xbld.at(10, 11) = 0x0F; place_highway_block(e, 10, 10, 2); CHECK(xb(e, 10, 11) == 0x4F); sc2k::ui::detail::demolish_highway(e, 10, 10); CHECK(xb(e, 10, 11) >= 0x0E && xb(e, 10, 11) < 0x1D); // power back CHECK(e.xbit.at(10, 11) & 0x80); // and conductive again }
// ---- 18. Demolishing the highway an ON-RAMP feeds removes the orphaned ramp (deviation: the original leaves it). ---- { City c; c.set_funds(10000); place_highway_block(c, 38, 40, /*dir=*/2); // highway block rows 38-39, cols 40-41 c.xbld.at(40, 41) = 0x1D; // a road just SE, so an on-ramp is legal auto r = ui::apply_tool(c, ui::Tool::Onramp, 40, 40); // on-ramp at (40,40), fed by highway (39,40) CHECK(r.ok && xb(c, 40, 40) >= 0x5D && xb(c, 40, 40) <= 0x60); sc2k::ui::detail::demolish_highway(c, 39, 40); // raze the block it's attached to CHECK(xb(c, 39, 40) >= 1 && xb(c, 39, 40) <= 4); // highway gone → rubble (flat land) CHECK(xb(c, 40, 40) >= 1 && xb(c, 40, 40) <= 4); // the orphaned on-ramp swept up → rubble CHECK(sc2k::ui::detail::is_road(xb(c, 40, 41))); // its 0x2B connector reverts to plain road }
// ---- 19. Demolishing a highway whose ramp/deck sat on a leveled FOUNDATION (terrace caps XTER 0x0D): the // demolish re-autotiles that terrain back to flat, and rubble appears on the flattened ground (the // residue is decided AFTER the re-slope — not bare, as it would be if judged from the live cap). ---- { City c; for (int r = 20; r <= 21; ++r) for (int col = 20; col <= 21; ++col) { c.xbld.at(r, col) = 0x61; // an elevated ramp piece (highway) c.xter.at(r, col) = 0x0D; // its leveled-platform terrace cap (foundation) c.altm.at(r, col) = 0; // flat heightfield underneath c.xzon.at(r, col) = 0xF0; } sc2k::ui::detail::demolish_highway(c, 20, 20); for (int r = 20; r <= 21; ++r) for (int col = 20; col <= 21; ++col) { CHECK(c.xter.at(r, col) == 0); // the foundation cap is gone → flat ground CHECK(c.xbld.at(r, col) >= 1 && c.xbld.at(r, col) <= 4); // …and rubble sits where the foundation was } }
// ---- 20. DON'T bury a surface network under a highway foundation (user 2026-07-01, deviation). A highway may // cross a road/rail/power straight only on a clean FLAT block (a grade crossing); a sloped/terraced // block that overlaps a network would foundation/level the terrain over it, so it's un-buildable. ---- { // (a) a FLAT block over a road IS buildable — the legitimate grade crossing (test 8 lays 0x4B there). City flat; flat.xbld.at(10, 11) = 0x1E; flat.xbld.at(11, 11) = 0x1E; // N-S road through the block's right column CHECK(sim::highway_block_buildable_land(flat, 10, 10));
// Build a real (generator-autotiled) slope so classify sees a valid non-flat block. auto slope_city = []() { sc2k::detail::TerrainGen tg; tg.g_wl = 0; for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) { int lvl = 4 + (60 - r) / 2; if (lvl < 4) lvl = 4; if (lvl > 20) lvl = 20; tg.A[r * 128 + cc] = lvl; tg.FL[r * 128 + cc] = 0; tg.XB[r * 128 + cc] = 0; tg.XT[r * 128 + cc] = 0; } for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) tg.autotile_cell(r, cc); City city; for (int r = 0; r < 128; ++r) for (int cc = 0; cc < 128; ++cc) { city.altm.at(r, cc) = std::uint16_t(tg.A[r * 128 + cc] & 0x1f); city.xter.at(r, cc) = tg.XT[r * 128 + cc]; } return city; }; // Find a block on the ramp that's a valid non-flat highway block (classify != INVALID and != 15). City base = slope_city(); int sr = -1, sc = -1; for (int r = 20; r < 60 && sr < 0; r += 2) for (int cc = 20; cc < 24; cc += 2) { const int cls = hd::classify(base, r, cc); if (cls != hd::HWY_INVALID && cls != 15) { sr = r; sc = cc; break; } } CHECK(sr >= 0); // a slope block exists CHECK(sim::highway_block_buildable_land(base, sr, sc)); // …buildable as a ramp with NO network under it
// Plant a road straight on one of its tiles → now it would bury the road under a foundation → refuse. City withRoad = base; withRoad.xbld.at(sr, sc) = 0x1D; // E-W road on the slope block CHECK(hd::classify(withRoad, sr, sc) != hd::HWY_INVALID); // classify still ALLOWS it (the original would bury) CHECK(hd::classify(withRoad, sr, sc) != 15); // and it's a non-flat block CHECK(!sim::highway_block_buildable_land(withRoad, sr, sc)); // …but our gate refuses the bury }
if (failures == 0) std::printf("test_highway: ALL CHECKS PASSED\n"); else std::printf("test_highway: %d CHECK(S) FAILED\n", failures); return failures ? 1 : 0;}