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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154// Pure pathfinding KAT (sim/ship_pathfind.hpp) — the global water route used by cargo ships, tested in// isolation on synthetic maps (no ship state machine, no RNG). Validates: A* finds navigable end-to-end// routes through winding channels, never corner-cuts a headland, prefers the channel CENTRE (coast// penalty), reports unreachable when no water path exists, and string-pulls to smooth straight runs.#include "sc2k/sim/ship_pathfind.hpp"#include "sc2k/city.hpp"#include <cstdio>#include <cstdlib>#include <algorithm>
using namespace sc2k;using namespace sc2k::sim;
#include "test_harness.hpp"
// Navigability for the synthetic maps: in-bounds + the surface-water bit (XBIT 0x04). Captureless →// converts to nav::NavFn. (The real ship uses ship_water_at; the algorithm is identical regardless.)static bool nav_water(const City& c, int x, int y) { return unsigned(x) < unsigned(City::kDim) && unsigned(y) < unsigned(City::kDim) && (c.xbit.at(x, y) & 0x04);}static bool eq(nav::Point a, int x, int y) { return a.x == x && a.y == y; } // (braced lists break the CHECK macro)static void water(City& c, int x, int y) { c.xbit.at(x, y) |= 0x04; }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) water(c, x, y);}
// A path is valid iff every tile is navigable, consecutive tiles are 8-adjacent (Chebyshev 1), and no// diagonal step cuts a land corner.static bool path_valid(const City& c, const std::vector<nav::Point>& p) { if (p.empty()) return false; for (auto pt : p) if (!nav_water(c, pt.x, pt.y)) return false; for (std::size_t i = 1; i < p.size(); ++i) { const int dx = p[i].x - p[i - 1].x, dy = p[i].y - p[i - 1].y; if (std::max(std::abs(dx), std::abs(dy)) != 1) return false; // adjacency if (dx && dy && (!nav_water(c, p[i - 1].x + dx, p[i - 1].y) || !nav_water(c, p[i - 1].x, p[i - 1].y + dy))) return false; // corner-cut } return true;}// Average clearance ×100 (integer). The MIN is pinned by fixed near-bank endpoints, so the average is// the honest measure of "how far off the coast does the route run".static int path_avg_clear100(const std::vector<std::uint8_t>& clr, const std::vector<nav::Point>& p) { long s = 0; for (auto pt : p) s += clr[nav::idx_of(pt.x, pt.y)]; return p.empty() ? 0 : int(s * 100 / long(p.size()));}
int main() { // ---- clearance field: against-the-bank = 1, deeper = larger ---- { City c; water_rect(c, 40, 40, 49, 49); // a 10x10 pond auto clr = nav::clearance_field(c, nav_water); CHECK(clr[nav::idx_of(40, 40)] == 1); // corner, touching land CHECK(clr[nav::idx_of(44, 44)] == 5); // 5 tiles in from the nearest edge CHECK(clr[nav::idx_of(20, 20)] == 0); // dry land → 0 }
// ---- straight open channel: a route exists, is valid, ~straight, and smooths to 2 endpoints ---- { City c; water_rect(c, 30, 10, 34, 110); // 5-wide corridor, 100 long auto clr = nav::clearance_field(c, nav_water); auto path = nav::find_path(c, nav_water, {32, 12}, {32, 108}, &clr); CHECK(path_valid(c, path)); CHECK(eq(path.front(), 32, 12)); CHECK(eq(path.back(), 32, 108)); CHECK(path.size() <= 100); // no wandering auto sm = nav::smooth_path(c, nav_water, path, &clr); CHECK(sm.size() == 2); // one straight run std::printf(" straight channel: %zu tiles -> %zu waypoints\n", path.size(), sm.size()); }
// ---- winding S-channel: greedy "head toward the goal" would beach on the land between the arms; // A* threads the corridors. Validate it's navigable end-to-end and reaches the goal. ---- { City c; water_rect(c, 20, 10, 22, 80); // top arm (rows 20-22, cols 10..80) water_rect(c, 20, 78, 90, 80); // right riser (rows 20..90, cols 78-80) water_rect(c, 88, 10, 90, 80); // bottom arm (rows 88-90, cols 10..80) const nav::Point start{21, 12}, goal{89, 12}; auto clr = nav::clearance_field(c, nav_water); auto path = nav::find_path(c, nav_water, start, goal, &clr); CHECK(!path.empty()); CHECK(path_valid(c, path)); CHECK(eq(path.front(), start.x, start.y)); CHECK(eq(path.back(), goal.x, goal.y)); // It must actually go the long way round (through the right riser, col ~79), not teleport across land. bool wentRight = false; for (auto pt : path) if (pt.y >= 78) wentRight = true; CHECK(wentRight); CHECK(path.size() > 150); // a long detour (straight-line is only 68) auto sm = nav::smooth_path(c, nav_water, path, &clr); CHECK(sm.size() >= 3 && sm.size() < path.size()); // a handful of corner waypoints // every smoothed segment is itself a clear straight run for (std::size_t i = 1; i < sm.size(); ++i) CHECK(nav::line_of_sight(c, nav_water, sm[i - 1], sm[i], &clr, 1)); std::printf(" S-channel: %zu tiles -> %zu smoothed waypoints\n", path.size(), sm.size()); }
// ---- coast avoidance: endpoints near the SAME bank. The unpenalised route runs straight along the // bank (shortest); the penalised route bows out toward mid-channel, raising the AVERAGE clearance // (the min is pinned at the near-bank endpoints, so we compare the average). ---- { City c; water_rect(c, 20, 20, 40, 100); // 21-wide, 80 long auto clr = nav::clearance_field(c, nav_water); auto plain = nav::find_path(c, nav_water, {22, 25}, {22, 95}, &clr, /*want*/ 0, /*coastW*/ 0); auto hug = nav::find_path(c, nav_water, {22, 25}, {22, 95}, &clr, /*want*/ 8, /*coastW*/ 8); CHECK(path_valid(c, plain)); CHECK(path_valid(c, hug)); const int ap = path_avg_clear100(clr, plain), ah = path_avg_clear100(clr, hug); CHECK(ah > ap + 50); // bows ≥0.5 tile further off the bank on average std::printf(" coast avoidance: avg clearance plain=%.2f penalised=%.2f\n", ap / 100.0, ah / 100.0); }
// ---- unreachable: two disconnected ponds → no path ---- { City c; water_rect(c, 10, 10, 20, 20); water_rect(c, 90, 90, 100, 100); auto path = nav::find_path(c, nav_water, {15, 15}, {95, 95}); CHECK(path.empty()); }
// ---- no corner-cut: two basins joined ONLY at a diagonal corner-touch (land on both orthogonals) // must read as unreachable — a ship can't squeeze through a corner. ---- { City c; water_rect(c, 40, 40, 44, 44); water_rect(c, 45, 45, 49, 49); // touch only at (44,44)-(45,45) auto path = nav::find_path(c, nav_water, {42, 42}, {47, 47}); CHECK(path.empty()); // corner-cut forbidden }
// ---- nearest_navigable: an inbound goal sitting on the dock (land) snaps to adjacent water ---- { City c; water_rect(c, 50, 50, 60, 60); auto g = nav::nearest_navigable(c, nav_water, 55, 65, 6); // (55,65) is dry, water ends at y=60 CHECK(g.x >= 0 && nav_water(c, g.x, g.y)); CHECK(g.y == 60); // nearest column of water }
if (failures) { std::printf("test_ship_pathfind: %d CHECK(s) FAILED\n", failures); return 1; } std::printf("test_ship_pathfind: all checks passed\n"); return 0;}