// The network-drawing COMMIT pipeline (sim/network_commit.hpp) — Phase 2. Drives the faithful per-tile place // (terrain adapter / road connect / autotiler) from a solved path. Numeric, // asset-free: pins the laid road codes, automatic crossings (road×rail), dezoning, slope-road shapes, the // terrain flatten, and the 25·terrace + perTile·total cost model. #include "sc2k/sim/network_commit.hpp" #include "sc2k/sim/network_path.hpp" #include "sc2k/sim/terrain_edit.hpp" // terrain_autotile_cell (natural slope from altitudes) #include "sc2k/ui/tools.hpp" // apply_tool (Bulldoze) for the terrain-restore check #include using namespace sc2k; using sim::NetType; using sim::solve_network_path; using sim::commit_network_path; #include "test_harness.hpp" static bool is_road(int code) { return code >= 0x1D && code <= 0x2B; } int main() { // ---- 1. A straight E-W road: every tile is the E-W straight 0x1D; cost = perTile·tiles. ---- { City c; c.set_funds(1000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); auto r = commit_network_path(c, p, NetType::Road, 10); CHECK(r.ok && r.tiles == 11 && r.spent == 110); CHECK(c.funds() == 890); bool allStraight = true; for (int col = 10; col <= 20; ++col) if (c.xbld.at(10, col) != 0x1D) allStraight = false; CHECK(allStraight); } // ---- 2. An L-bend: every laid tile is a road; the turn produces a curve piece (0x23-0x26). ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 14, 16, NetType::Road); auto r = commit_network_path(c, p, NetType::Road, 10); CHECK(r.ok); bool allRoad = true, sawCurve = false; for (auto& s : p.steps) { const int code = c.xbld.at(s.row, s.col); if (!is_road(code)) allRoad = false; if (code >= 0x23 && code <= 0x26) sawCurve = true; // a corner piece somewhere on the staircase } CHECK(allRoad); CHECK(sawCurve); } // ---- 3. Crossing a perpendicular RAIL straight makes the combined road-over-rail sprite (0x45). ---- { City c; c.set_funds(100000); c.xbld.at(10, 15) = 0x2D; // rail N-S straight auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); auto r = commit_network_path(c, p, NetType::Road, 10); CHECK(r.ok && p.endCol == 20); // the drag crosses and reaches the end CHECK(c.xbld.at(10, 15) == 0x45); // road-over-rail crossing tile CHECK(is_road(c.xbld.at(10, 14)) && is_road(c.xbld.at(10, 16))); } // ---- 4. Drawing a road through an empty zoned tile DEZONES it (clears the XZON nibble). ---- { City c; c.set_funds(100000); c.xzon.at(10, 15) = 0x01; // zoned residential, undeveloped auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); commit_network_path(c, p, NetType::Road, 10); CHECK(is_road(c.xbld.at(10, 15))); CHECK((c.xzon.at(10, 15) & 0x0F) == 0); // dezoned } // ---- 5. Terrain adapter: a "needs-terrace" slope (shapes 5-8) flattens to 0x0D and loses its water bit. ---- { City c; c.xter.at(5, 5) = 0x05; c.xbit.at(5, 5) = 0x04; sim::commitdetail::terrain_adapt(c, 5, 5, 2); CHECK(c.xter.at(5, 5) == 0x0D && (c.xbit.at(5, 5) & 0x04) == 0); // Cardinal slopes 1-4 are NOT terraced (a road rides them directly). c.xter.at(6, 6) = 0x02; sim::commitdetail::terrain_adapt(c, 6, 6, 2); CHECK(c.xter.at(6, 6) == 0x02); // Single-corner slope 9 → a direction-aligned cardinal ramp (E heading → CORNER_RAMP[2] = slope 2). c.xter.at(7, 7) = 0x09; sim::commitdetail::terrain_adapt(c, 7, 7, 2); CHECK(c.xter.at(7, 7) == 2); } // ---- 5b. A road over a CORNER slope converts the tile to a cardinal ramp + lays a SLOPED-road sprite // (0x1F-0x22) that connects — not a flat 0x1D floating on the corner (the reported join bug). ---- { City c; c.xter.at(5, 5) = 0x09; // single-corner slope sim::commitdetail::place_one(c, 5, 5, NetType::Road, 2); // road heading East across it CHECK(c.xter.at(5, 5) == 2); // corner → E-W cardinal ramp CHECK(c.xbld.at(5, 5) >= 0x1F && c.xbld.at(5, 5) <= 0x22);// a sloped-road piece (road-family ⇒ connects) } // ---- 6. Slope-road shape: a road on a cardinal slope takes the sloped-road code (0x1F-0x22). ---- { City c; c.xbld.at(5, 5) = 0x1D; c.xter.at(5, 5) = 0x01; // road on slope shape 1 sim::commitdetail::autotile_road(c, 5, 5); CHECK(c.xbld.at(5, 5) == 0x1F); // 29 + slope-variant 2 } // ---- 7. Insufficient funds: nothing is laid and funds are untouched. ---- { City c; c.set_funds(50); // < 11·10 auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Road); auto r = commit_network_path(c, p, NetType::Road, 10); CHECK(!r.ok); CHECK(c.funds() == 50); CHECK(c.xbld.at(10, 10) == 0 && c.xbld.at(10, 15) == 0); // map unchanged } // ---- 8. Rail: a straight run lays the E-W rail straight (0x2C). ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Rail); auto r = commit_network_path(c, p, NetType::Rail, 25); CHECK(r.ok && r.spent == 25 * 11); bool ok = true; for (int col = 10; col <= 20; ++col) if (c.xbld.at(10, col) != 0x2C) ok = false; CHECK(ok); } // ---- 9. Power line: straight run lays 0x0E and marks every tile conductive (XBIT 0x80). ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Power); commit_network_path(c, p, NetType::Power, 2); bool ok = true; for (int col = 10; col <= 20; ++col) if (c.xbld.at(10, col) != 0x0E || !(c.xbit.at(10, col) & 0x80)) ok = false; CHECK(ok); } // ---- 10. Power lines do NOT dezone (a power line can sit on a zone that still develops). ---- { City c; c.set_funds(100000); c.xzon.at(10, 15) = 0x01; auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Power); commit_network_path(c, p, NetType::Power, 2); CHECK(c.xbld.at(10, 15) == 0x0E); CHECK((c.xzon.at(10, 15) & 0x0F) == 1); // zone survives (unlike road/rail) } // ---- 11. Power crossing a perpendicular road (N-S straight) → power-over-road sprite 0x44. ---- { City c; c.set_funds(100000); c.xbld.at(10, 15) = 0x1E; // road N-S straight (perpendicular to E-W power) auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Power); auto r = commit_network_path(c, p, NetType::Power, 2); CHECK(r.ok && p.endCol == 20); CHECK(c.xbld.at(10, 15) == 0x44); } // ---- 12. Pipe: laid in the XUND layer (16-30) and flagged water-conductive (XBIT 0x20). ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Pipe); commit_network_path(c, p, NetType::Pipe, 3); bool ok = true; for (int col = 10; col <= 20; ++col) { const int u = c.xund.at(10, col); if (u < 0x10 || u >= 0x1F || !(c.xbit.at(10, col) & 0x20)) ok = false; } CHECK(ok); CHECK(c.xund.at(10, 15) == 16); // E-W straight pipe } // ---- 13. Subway: laid in the XUND layer (1-15), distinct from the pipe range. ---- { City c; c.set_funds(100000); auto p = solve_network_path(c, 10, 10, 10, 20, NetType::Subway); commit_network_path(c, p, NetType::Subway, 100); bool ok = true; for (int col = 10; col <= 20; ++col) { const int u = c.xund.at(10, col); if (u == 0 || u >= 0x10) ok = false; } CHECK(ok); CHECK(c.xund.at(10, 15) == 1); // E-W straight subway } // ---- 13b. Underground networks never terrace or otherwise mutate the surface above them. ---- // Shape 5 is the important regression: the shared network adapter used to turn it into XTER 0x0D // (a dry empty cube), clear its water bit, and charge an unrelated $25 surface-terraform surcharge. for (const NetType net : {NetType::Pipe, NetType::Subway}) { City c; c.set_funds(100000); c.xter.at(10, 10) = 0x05; c.xbit.at(10, 10) = 0x04; c.altm.at(10, 10) = 0x1234; const auto beforeXter = c.xter.cells; const auto beforeAltm = c.altm.cells; const auto p = solve_network_path(c, 10, 10, 10, 10, net); const auto r = commit_network_path(c, p, net, net == NetType::Pipe ? 3 : 100); CHECK(r.ok); CHECK(r.spent == (net == NetType::Pipe ? 3 : 100)); CHECK(c.xter.cells == beforeXter); CHECK(c.altm.cells == beforeAltm); CHECK((c.xbit.at(10, 10) & 0x04) != 0); // surface water state survives } // ---- 14. A no-op placement (the same network already on the tile) does NOT charge (subway-station spam). ---- { City c; c.set_funds(100000); auto r1 = commit_network_path(c, solve_network_path(c, 10, 10, 10, 10, NetType::Subway), NetType::Subway, 100); CHECK(r1.ok && c.funds() == 99900); // first subway tile charges $100 auto r2 = commit_network_path(c, solve_network_path(c, 10, 10, 10, 10, NetType::Subway), NetType::Subway, 100); CHECK(!r2.ok && c.funds() == 99900); // re-placing on the same tile: no change, no charge } // ---- 15. Bulldozing a network RESTORES the natural ground slope from the (untouched) altitudes. ---- { City c; c.set_funds(100000); c.altm.at(4, 4) = 1; // NW neighbour higher → (5,5) is a corner slope (shape 9) sc2k::terrain_autotile_cell(c, 5, 5, 0); CHECK(c.xter.at(5, 5) == 9); // the natural terrain sim::commitdetail::place_one(c, 5, 5, NetType::Road, 2); CHECK(c.xter.at(5, 5) == 2); // road terraces it to a cardinal ramp CHECK(c.xbld.at(5, 5) >= 0x1F && c.xbld.at(5, 5) <= 0x22); ui::apply_tool(c, ui::Tool::Bulldoze, 5, 5); // delete the road… CHECK(!(c.xbld.at(5, 5) >= 0x1D && c.xbld.at(5, 5) <= 0x2B)); // …road gone CHECK(c.xter.at(5, 5) == 9); // …and the original corner slope is back } if (failures == 0) { std::printf("network_commit: all checks passed\n"); return 0; } std::printf("network_commit: %d failure(s)\n", failures); return 1; }