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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407// Cargo-ship navigation: the ship follows a global A* route (coast-averse, smoothed) with the original's// gradual local steer. Drives the real cargoship_step and checks it ARRIVES through a deterministic// synthetic winding channel AND the real FOURCITI harbour, then exercises docking/mooring, dock// accessibility, ship-size berth rules, and port-destroyed-mid-approach recovery. Verified numerically.#include "sc2k/sim/things.hpp"#include "sc2k/sim/ship_pathfind.hpp"#include "sc2k/io/sc2_file.hpp"#include "sc2k/city.hpp"#include "sc2k/rng.hpp"#include <cstdio>#include <cstdlib>#include <cmath>#include <algorithm>#include <string>#include "test_paths.hpp"
using namespace sc2k;using namespace sc2k::sim;
#include "test_harness.hpp"
static bool nav_water(const City& c, int x, int y) { return unsigned(x) < unsigned(City::kDim) && unsigned(y) < unsigned(City::kDim) && ship_water_at(c, x, y);}static void water_rect(City& c, int x0, int y0, int x1, int y1) { for (int x = x0; x <= x1; ++x) for (int y = y0; y <= y1; ++y) { c.xter.at(x, y) = 0x10; c.xbit.at(x, y) |= 0x04; // deep-water terrain + surface-water bit }}static int octant_diff(int a, int b) { int d = std::abs((a & 7) - (b & 7)); return std::min(d, 8 - d);}
struct Metrics { bool arrived = false; int ticks = 0, moves = 0, reversals = 0, turns = 0; double avgClear = 0, effic = 0;};
// Run one ship from `start` toward `dest` on city `c` (a COPY, so each mode starts clean). leg: 0=inbound// (dock), 0x80=outbound (leave). `usePathfind` selects the steering. Returns the chaos metrics.static Metrics run_ship(City c, nav::Point start, nav::Point dest, int leg, int budget) { const int idx = 1; ship_nav_reset(idx); thing_field(c, idx, thg::Type) = 3; thing_field(c, idx, thg::X) = std::uint8_t(start.x); thing_field(c, idx, thg::Y) = std::uint8_t(start.y); thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::DestX) = std::uint8_t(dest.x); thing_field(c, idx, thg::DestY) = std::uint8_t(dest.y); thing_field(c, idx, thg::Dir) = std::uint8_t(thing_bearing(start.x, start.y, dest.x, dest.y)); thing_field(c, idx, thg::State) = std::uint8_t(leg); thing_field(c, idx, thg::Frame) = std::uint8_t(start.x); thing_field(c, idx, thg::Link) = std::uint8_t(start.y); c.xtxt.at(start.x, start.y) = std::uint8_t(idx - 55); MsRand rng(0xC0FFEEL); const auto clr = nav::clearance_field(c, &nav_water);
Metrics m; int prevDir = thing_field(c, idx, thg::Dir), px = start.x, py = start.y; long clrSum = 0; int clrN = 0; for (m.ticks = 0; m.ticks < budget; ++m.ticks) { cargoship_step(c, rng, idx); const int t = thing_field(c, idx, thg::Type); if (t != 3) { // despawned: arrival (near dest) vs gave-up/wreck const int lastDist = std::abs(px - dest.x) + std::abs(py - dest.y); m.arrived = (t == 0 && lastDist < 4); // type 6 = wreck (never, by design) break; } const int x = thing_field(c, idx, thg::X), y = thing_field(c, idx, thg::Y), d = thing_field(c, idx, thg::Dir); if (x != px || y != py) { ++m.moves; clrSum += clr[nav::idx_of(x, y)]; ++clrN; } if (d != prevDir) { ++m.turns; if (octant_diff(d, prevDir) >= 3) ++m.reversals; } if (leg == 0 && (thing_field(c, idx, thg::State) & 0x7F) == 3) { m.arrived = true; ++m.ticks; break; } // docked prevDir = d; px = x; py = y; } const int sl = std::max(1, std::abs(start.x - dest.x) + std::abs(start.y - dest.y)); // straight-line (Manhattan) m.effic = double(m.moves) / sl; m.avgClear = clrN ? double(clrSum) / clrN : 0; return m;}static void report(const char* tag, const Metrics& a) { std::printf(" %-12s | A* PATH arrived=%d ticks=%4d moves=%4d reversals=%3d turns=%3d effic=%.2f avgClear=%.2f\n", tag, a.arrived, a.ticks, a.moves, a.reversals, a.turns, a.effic, a.avgClear);}
int main() { // ───────── 1. Deterministic tight SERPENTINE (boustrophedon): four 2-wide lanes joined alternately // at the ends. The goal is "due south" of the start, so greedy keeps pointing into the lane walls and // ricochets back and forth at every switchback; A* follows the snake. (A reproducible FOURCITI.) ───── { City c; const int lane[4] = {20, 35, 50, 65}; for (int L = 0; L < 4; ++L) water_rect(c, lane[L], 20, lane[L] + 1, 100); // 4 horizontal lanes water_rect(c, lane[0], 99, lane[1] + 1, 100); // join 0↔1 at the right water_rect(c, lane[1], 20, lane[2] + 1, 21); // join 1↔2 at the left water_rect(c, lane[2], 99, lane[3] + 1, 100); // join 2↔3 at the right const nav::Point start{lane[0], 22}, dest{lane[3], 22}; Metrics a = run_ship(c, start, dest, 0x80, 8000); report("serpentine", a); CHECK(a.arrived); // A* sails the snake to the far end }
// ───────── 2. The user's real case: FOURCITI's winding N-edge→seaport channel (memory: "defeats the // greedy local steering — ships sail it visibly then leave"). Inbound from the connected edge to the // dock; A* should actually DOCK (state 3). ───── { const std::string cand[] = { original_cd_root() + "/WIN31/CITIES/FOURCITI.SC2", original_cd_root() + "/DOS/CITIES/FOURCITI.SC2", }; City c; bool loaded = false; for (const auto& p : cand) if (io::load_sc2(p, c).ok) { loaded = true; break; } if (!loaded) { std::printf(" (FOURCITI not found — skipping the real-city A/B)\n"); } else { // seaport (0xE0) centre + a connected entry edge (the same machinery the spawner uses). int spx = -1, spy = -1; for (int x = 0; x < 128 && spx < 0; ++x) for (int y = 0; y < 128; ++y) if (c.xbld.at(x, y) == 0xE0) { spx = x; spy = y; break; } MsRand rng(7); int ex = -1, ey = -1; const bool conn = ship_entry_from_seaport(c, spx, spy, rng, ex, ey); CHECK(conn); // harbour reaches the edge (lenient navigability) if (conn) { const nav::Point entry{ex, ey}, dock{spx, spy}; Metrics a = run_ship(c, entry, dock, 0, 8000); report("FOURCITI", a); CHECK(a.arrived); // A* docks at the seaport } } }
// ───────── 3. Docking: a ship pulls ALONGSIDE a pier and MOORS there for a good while (user: "it // doesn't stop at a port for long; should pull up alongside"), instead of the old ~6 s flit. ───── { City c; water_rect(c, 60, 62, 68, 75); // open basin EAST of the dock for (int r = 63; r <= 65; ++r) { // a pier wall (0xDF) on the water edge, jutting into the basin c.xter.at(r, 61) = 0x10; c.xbit.at(r, 61) |= 0x04; c.xbld.at(r, 61) = 0xDF; for (int col = 58; col <= 60; ++col) c.xbld.at(r, col) = 0xE0; // 3×3 seaport behind, on (dry) land } const int idx = 1; ship_nav_reset(idx); thing_field(c, idx, thg::Type) = 3; thing_field(c, idx, thg::X) = 64; thing_field(c, idx, thg::Y) = 73; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::DestX) = 64; thing_field(c, idx, thg::DestY) = 59; // inbound to the seaport thing_field(c, idx, thg::Dir) = std::uint8_t(thing_bearing(64, 73, 64, 59)); thing_field(c, idx, thg::State) = 0; thing_field(c, idx, thg::Frame) = 64; thing_field(c, idx, thg::Link) = 73; // entry edge (for the return) c.xtxt.at(64, 73) = std::uint8_t(idx - 55); MsRand rng(42); auto dock_dist = [&](int x, int y) { // Manhattan distance to the nearest pier OR seaport tile int best = 99; for (int dx = -4; dx <= 4; ++dx) for (int dy = -4; dy <= 4; ++dy) { const int ax = x + dx, ay = y + dy; if (ax >= 0 && ay >= 0 && ax < 128 && ay < 128 && (c.xbld.at(ax, ay) == 0xDF || c.xbld.at(ax, ay) == 0xE0)) best = std::min(best, std::abs(dx) + std::abs(dy)); } return best; }; int tDock = -1, mooredRun = 0, bestMooredRun = 0, tDepart = -1; int mooredDist = 99; int lastX = 64, lastY = 70, stillRun = 0, bestStill = 0; for (int t = 0; t < 1200; ++t) { cargoship_step(c, rng, idx); const int st = thing_field(c, idx, thg::State); const int x = thing_field(c, idx, thg::X), y = thing_field(c, idx, thg::Y); if ((st & 0x7F) == 3 && !(st & 0x80)) { // moored (docked, not yet outbound) if (tDock < 0) tDock = t; mooredRun++; bestMooredRun = std::max(bestMooredRun, mooredRun); mooredDist = std::min(mooredDist, dock_dist(x, y)); // distance to the nearest dock tile (pier/seaport) if (x == lastX && y == lastY) { stillRun++; bestStill = std::max(bestStill, stillRun); } else stillRun = 0; } else { mooredRun = 0; stillRun = 0; if (tDock >= 0 && tDepart < 0 && (st & 0x80)) tDepart = t; } lastX = x; lastY = y; if (thing_field(c, idx, thg::Type) != 3) break; // left the map after departing } std::printf(" docking: docked@%d mooredRun=%d pierDist=%d heldStill=%d departed@%d\n", tDock, bestMooredRun, mooredDist, bestStill, tDepart); CHECK(tDock >= 0); // it docked CHECK(mooredDist <= 1); // pulled ALONGSIDE the pier (adjacent, not the 2-tile dock trigger) CHECK(bestMooredRun >= 120); // moored a good while (≥24 s; old flit was ~6 s avg) CHECK(bestStill >= 100); // and held STILL alongside (didn't mill around as the old code did) CHECK(tDepart > tDock); // then cast off and left }
// ───────── 4. Dock ACCESSIBILITY (user: "several docks are inaccessible — the overlay missed them"): a slip // sealed off from the open sea is flagged inaccessible, and the ship won't route a berth there. ───── { City c; water_rect(c, 56, 2, 72, 84); // main basin, open to the W edge for (int r = 59; r <= 61; ++r) for (int col = 80; col <= 82; ++col) c.xbld.at(r, col) = 0xE0; // seaport A for (int col = 74; col <= 79; ++col) c.xbld.at(62, col) = 0xDF; // A's pier (accessible) water_rect(c, 40, 40, 48, 48); // a SEALED pond — navigable water, no edge path for (int r = 42; r <= 44; ++r) for (int col = 49; col <= 51; ++col) c.xbld.at(r, col) = 0xE0; // seaport B for (int col = 44; col <= 48; ++col) c.xbld.at(45, col) = 0xDF; // B's pier (sealed) const auto reach = ship_edge_reachable(c); CHECK(seaport_reachable_berth(c, 60, 81, reach)); // A opens to the sea → usable (overlay green) CHECK(!seaport_reachable_berth(c, 43, 50, reach)); // B is sealed → inaccessible (overlay red) int bx, by, px, py; CHECK(find_berth(c, 43, 45, 43, 50, bx, by, px, py, nullptr)); // B HAS a berth (ignoring reachability), CHECK(!find_berth(c, 43, 45, 43, 50, bx, by, px, py, &reach)); // …but none REACHABLE → the ship won't route there CHECK(find_berth(c, 64, 40, 60, 81, bx, by, px, py, &reach)); // A has a reachable berth CHECK(c.xbld.at(px, py) == 0xDF); std::printf(" accessibility: A usable, B sealed→flagged, ship berths only at the reachable dock\n"); }
// ───────── 5. Ship SIZE (user: "considering the size of a ship"): a 1-tile-wide slip between abutting // fingers is too narrow — a ship is bigger than a tile and can't fit/turn. Only berths in ≥2-wide water // count, so a finger sandwiched by two 1-wide slips is unusable. ───── { City c; water_rect(c, 48, 2, 72, 88); // basin to the W edge for (int col = 78; col <= 84; ++col) { // fingers 56/58/60 (1-wide slips 57,59) + 63 (2-wide slip 61-62) c.xbld.at(56, col) = 0xDF; c.xbld.at(58, col) = 0xDF; c.xbld.at(60, col) = 0xDF; c.xbld.at(63, col) = 0xDF; } const auto reach = ship_edge_reachable(c); CHECK(!pier_has_usable_berth(c, 58, 80, reach)); // sandwiched by two 1-wide slips → no ship fits CHECK(pier_has_usable_berth(c, 56, 80, reach)); // open-water side → usable CHECK(pier_has_usable_berth(c, 63, 80, reach)); // a ≥2-wide slip → usable int bx, by, px, py; CHECK(find_berth(c, 64, 80, 60, 84, bx, by, px, py, &reach)); // pick a berth near the fingers CHECK(ship_water_at(c, bx + (bx - px), by + (by - py))); // …its far side is water → ≥2-wide, not a 1-wide slip std::printf(" ship-size: 1-wide slip rejected; berth chosen in ≥2-wide water at (%d,%d)\n", bx, by); }
// ───────── 6. Port DESTROYED mid-approach (user: "incoming ship gets confused if the target pier is // removed"): recalculate — retarget a surviving berth, or give up and sail home; never sail to a phantom. ─ auto place_inbound = [](City& c, int idx) { ship_nav_reset(idx); thing_field(c, idx, thg::Type) = 3; thing_field(c, idx, thg::X) = 64; thing_field(c, idx, thg::Y) = 4; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::DestX) = 60; thing_field(c, idx, thg::DestY) = 81; thing_field(c, idx, thg::Dir) = std::uint8_t(thing_bearing(64, 4, 60, 81)); thing_field(c, idx, thg::Frame) = 64; thing_field(c, idx, thg::Link) = 4; c.xtxt.at(64, 4) = std::uint8_t(idx - 55); }; { // (a) one of two piers bulldozed → retarget the survivor and still dock City c; water_rect(c, 56, 2, 72, 84); for (int r = 59; r <= 61; ++r) for (int col = 80; col <= 82; ++col) c.xbld.at(r, col) = 0xE0; // seaport for (int col = 74; col <= 79; ++col) { c.xbld.at(58, col) = 0xDF; c.xbld.at(64, col) = 0xDF; } // two finger piers const int idx = 1; place_inbound(c, idx); MsRand rng(3); for (int t = 0; t < 60; ++t) cargoship_step(c, rng, idx); // get en route + pick a berth const int tgt = thing_field(c, idx, 0) == 3 ? ship_nav(idx).pierX : -1; CHECK(tgt >= 0); for (int col = 74; col <= 79; ++col) c.xbld.at(tgt, col) = 0; // BULLDOZE the whole targeted finger bool docked = false; for (int t = 0; t < 1000 && !docked; ++t) { cargoship_step(c, rng, idx); if ((thing_field(c, idx, thg::State) & 0x7F) == 3 && !(thing_field(c, idx, thg::State) & 0x80)) docked = true; if (thing_field(c, idx, thg::Type) != 3) break; } CHECK(docked); // retargeted a survivor and docked CHECK(ship_nav(idx).pierX != tgt); // …NOT the bulldozed pier std::printf(" destroyed pier: retargeted to pier row %d (was %d), docked\n", ship_nav(idx).pierX, tgt); } { // (b) the WHOLE port razed → give up, turn around, leave (don't get stuck at a phantom dock) City c; water_rect(c, 56, 2, 72, 84); for (int r = 59; r <= 61; ++r) for (int col = 80; col <= 82; ++col) c.xbld.at(r, col) = 0xE0; for (int col = 74; col <= 79; ++col) c.xbld.at(62, col) = 0xDF; const int idx = 1; place_inbound(c, idx); MsRand rng(5); for (int t = 0; t < 60; ++t) cargoship_step(c, rng, idx); for (int r = 59; r <= 61; ++r) for (int col = 80; col <= 82; ++col) { c.xbld.at(r, col) = 0; c.xter.at(r, col) = 0x10; c.xbit.at(r, col) |= 4; } for (int col = 74; col <= 79; ++col) c.xbld.at(62, col) = 0; // raze seaport + pier → open water bool turned = false, left = false; for (int t = 0; t < 1500; ++t) { cargoship_step(c, rng, idx); if (thing_field(c, idx, thg::State) & 0x80) turned = true; // went outbound if (thing_field(c, idx, thg::Type) != 3) { left = true; break; } } CHECK(turned); CHECK(left); // gave up, turned around, sailed off std::printf(" razed port: ship gave up and left (not stuck)\n"); }
{ // (c) the pier bulldozed WHILE the ship is moored/loading → cast off and leave (user: "gets stuck if // the port is demolished while it's loading"), don't sit out the dwell in empty water City c; water_rect(c, 56, 2, 72, 84); for (int r = 59; r <= 61; ++r) for (int col = 80; col <= 82; ++col) c.xbld.at(r, col) = 0xE0; for (int col = 74; col <= 79; ++col) c.xbld.at(62, col) = 0xDF; const int idx = 1; place_inbound(c, idx); MsRand rng(9); bool docked = false; for (int t = 0; t < 700 && !docked; ++t) { cargoship_step(c, rng, idx); if ((thing_field(c, idx, thg::State) & 0x7F) == 3 && !(thing_field(c, idx, thg::State) & 0x80)) docked = true; } CHECK(docked); for (int col = 74; col <= 79; ++col) c.xbld.at(62, col) = 0; // BULLDOZE the pier while moored bool turned = false, left = false; for (int t = 0; t < 1500; ++t) { cargoship_step(c, rng, idx); if (thing_field(c, idx, thg::State) & 0x80) turned = true; if (thing_field(c, idx, thg::Type) != 3) { left = true; break; } } CHECK(turned); CHECK(left); std::printf(" demolished while docked: cast off and left\n"); }
// ───────── 7. SHIP SIZE — a tight slip's DEAD END is inaccessible even though the water reaches it: a ship // can only poke a few tiles in from the open harbour (user: inner berths show green but a ship can't reach). ─ { City c; water_rect(c, 64, 2, 90, 80); // open harbour to the W edge for (int r = 53; r <= 63; ++r) { c.xter.at(r, 63) = 0x10; c.xbit.at(r, 63) |= 4; // a 2-wide slip (cols 63-64), c.xter.at(r, 64) = 0x10; c.xbit.at(r, 64) |= 4; // closed at the wharf (row 53) c.xbld.at(r, 62) = 0xDF; c.xbld.at(r, 65) = 0xDF; } // fingers either side const auto acc = ship_accessible_water(c); CHECK(acc[std::size_t(63) * City::kDim + 63]); // slip mouth (by the harbour) → accessible CHECK(!acc[std::size_t(54) * City::kDim + 63]); // dead end (by the wharf) → NOT std::printf(" deep slip: mouth accessible, dead end not\n"); }
if (failures) { std::printf("test_ship_nav: %d CHECK(s) FAILED\n", failures); return 1; } std::printf("test_ship_nav: all checks passed\n"); return 0;}