// The network-drawing PATH SOLVER (sim/network_path.hpp) — Phase 1 of the network-drawing reimplementation // (docs/ref/re/reimpl/08-network-drawing.md). Numeric, asset-free: a faithful port of the original's shared drag // engine (walk init + stepper + direction choosers). These checks pin the // flat-terrain L-bend behaviour, the clip box, every "stop" rule (occupied / water / military / off-map), // automatic perpendicular crossings, and the slope-direction (build-along-the-gradient) constraint. #include "sc2k/sim/network_path.hpp" #include using namespace sc2k; using sim::NetType; using sim::solve_network_path; #include "test_harness.hpp" // Is the step list gapless (consecutive tiles are 8-neighbours, never repeating) and does each step reduce // the Manhattan distance to the end by exactly 1 (the primary/minor chooser always advances toward the end)? static bool gapless_monotone(const sim::NetworkPath& p, int er, int ec) { for (std::size_t i = 1; i < p.steps.size(); ++i) { const auto& a = p.steps[i - 1]; const auto& b = p.steps[i]; const int dr = std::abs(a.row - b.row), dc = std::abs(a.col - b.col); if (dr > 1 || dc > 1 || (dr == 0 && dc == 0)) return false; // 8-connected, no repeat const int ma = std::abs(a.row - er) + std::abs(a.col - ec); const int mb = std::abs(b.row - er) + std::abs(b.col - ec); if (mb != ma - 1) return false; // strictly toward the end } return true; } int main() { // ---- 1. Straight horizontal: a flat E-ward drag lays one tile per column, all heading East. ---- { City c; auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); CHECK(p.valid); CHECK(p.steps.size() == 11); // Manhattan 10 + 1 CHECK(p.endRow == 10 && p.endCol == 20); bool rowOk = true, dirOk = true; for (int i = 0; i < (int)p.steps.size(); ++i) { if (p.steps[i].row != 10 || p.steps[i].col != 10 + i) rowOk = false; if (p.steps[i].dir != 2) dirOk = false; // 2 = East } CHECK(rowOk); CHECK(dirOk); } // ---- 2. Straight vertical: a flat S-ward drag steps down one row per tile. ---- { City c; auto p = solve_network_path(c, 10, 10, 20, 10, NetType::Road); CHECK(p.valid && p.steps.size() == 11 && p.endRow == 20 && p.endCol == 10); bool ok = true; for (int i = 0; i < (int)p.steps.size(); ++i) if (p.steps[i].row != 10 + i || p.steps[i].col != 10 || p.steps[i].dir != 1) ok = false; // 1 = South CHECK(ok); } // ---- 3. L-bend: a diagonal drag lays a gapless Manhattan path inside the anchor→end rectangle. ---- { City c; auto p = solve_network_path(c, 10, 10, 15, 20, NetType::Road); CHECK(p.valid); CHECK(p.steps.size() == 16); // Manhattan 5+10 + 1 CHECK(p.endRow == 15 && p.endCol == 20); CHECK(gapless_monotone(p, 15, 20)); bool inBox = true; for (auto& s : p.steps) if (s.row < 10 || s.row > 15 || s.col < 10 || s.col > 20) inBox = false; CHECK(inBox); // confined to the clip box } // ---- 4. A developed building STOPS the drag (no bulldoze) — it reaches the tile before it. ---- { City c; c.xbld.at(10, 15) = 0x80; // a building (>=0x0D, not a crossing) auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); CHECK(p.valid); CHECK(p.endRow == 10 && p.endCol == 14); // stopped one short of the building CHECK(p.steps.size() == 5); // (10,10)..(10,14) } // ---- 5. A perpendicular road is CROSSED (automatic intersection), so the drag reaches the end. ---- { City c; c.xbld.at(10, 15) = 0x1E; // road N-S straight: (dir&1)+0x1E = 30 → crossing OK auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); CHECK(p.valid && p.endRow == 10 && p.endCol == 20); // passes straight through the crossing CHECK(p.steps.size() == 11); } // ---- 6. Open water stops a surface network (the bridge path is separate). ---- { City c; c.xbit.at(10, 15) = 0x04; // water bit; XBLD 0 < 0x40 auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); CHECK(p.valid && p.endCol == 14); } // ---- 7. A military tile (XZON nibble 7) is impassable. ---- { City c; c.xzon.at(10, 15) = 0x07; auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); CHECK(p.valid && p.endCol == 14); } // ---- 8. Slope direction: a slope may only be built ALONG its gradient. ---- { // Shape 1 blocks W/E (shape 1 = [1,0,1,0]); an E-ward drag from it collapses to one tile… City c; c.xter.at(10, 10) = 0x01; auto pe = solve_network_path(c, 10, 10, 10, 20, NetType::Road); CHECK(pe.valid && pe.steps.size() == 1 && pe.endRow == 10 && pe.endCol == 10); // …but an S-ward drag (along the gradient) proceeds normally. City d; d.xter.at(10, 10) = 0x01; auto ps = solve_network_path(d, 10, 10, 20, 10, NetType::Road); CHECK(ps.valid && ps.steps.size() == 11 && ps.endRow == 20 && ps.endCol == 10); } // ---- 9. An invalid anchor (XBLD == 13) rejects the whole drag. ---- { City c; c.xbld.at(10, 10) = 13; auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); CHECK(!p.valid && p.steps.empty()); } // ---- 10. Cost model: terrace-tile count = tiles whose XTER slope shape needs terracing. ---- { City c; sim::NetworkPath path; // a hand-built 3-tile path path.steps = {{10, 10, 2}, {10, 11, 2}, {10, 12, 2}}; CHECK(sim::path_terrace_tiles(c, path) == 0); // flat ⇒ no surcharge c.xter.at(10, 11) = 0x06; // shape 6 ∈ {5..12} ⇒ needs terracing CHECK(sim::path_terrace_tiles(c, path) == 1); } if (failures == 0) { std::printf("network_path: all checks passed\n"); return 0; } std::printf("network_path: %d failure(s)\n", failures); return 1; }