// sc2k/sim/network_commit.hpp — Phase 2 of the network-drawing reimplementation: COMMIT a solved path // (sim/network_path.hpp) to the City, faithfully, for all five line networks. Per tile it runs the original's // surface terrain adapter (road/rail/power only) → lay base/crossing (+dezone/+conductive bit) → autotile // self+neighbours, then charges the live cost model. Pipe/subway remain wholly underground: they must never // reshape the landscape above them. This is the path the live preview (Phase 3) commits on mouse-up; it // replaces the naive per-tile apply_tool drag for networks. // // Ported from the original's commit chain. Each network // shares one autotile model — shape = connectShape[connectivityMask] + base, or slopeShape[slope] + base on // a ramp — differing only in base code, the "what counts as a neighbour" test, the crossing map, the layer // (XBLD surface / XUND underground), dezoning, and a conductive bit. The terrain adapter, the write // helpers, and each network's connect/autotile pair are all individually verified against the original. #pragma once #include #include #include "sc2k/city.hpp" #include "sc2k/tile_codes.hpp" #include "sc2k/sim/network_path.hpp" namespace sc2k::sim { namespace commitdetail { // Connectivity mask (N=8 E=4 S=2 W=1) → shape; shared by every network. code = shape + base. inline constexpr int CONNECT_SHAPE[16] = {0, 0, 1, 6, 0, 0, 7, 11, 1, 9, 1, 10, 8, 13, 12, 14}; // Slope shape by XTER. Cardinal slopes 1-4 → {2,3,4,5}. Shapes 9-12 read past the table in the // original (cosmetic, unrecoverable) → treated as 0 (flat) here. inline constexpr int SLOPE_SHAPE[16] = {0, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // The W/E and N/S per-neighbour-XTER connection gates (a slope only connects along // its gradient). Indexed by the NEIGHBOUR's XTER; out of range ⇒ no connection. // clang-format off inline constexpr std::uint8_t GATE_WE[72] = { 1,0,1,0,1,1,1,1, 1,1,1,1,1,1,1,0, 1,0,1,0,1,1,1,1, 1,1,1,1,1,1,1,0, 1,0,1,0,1,1,1,1, 1,1,1,1,1,1,1,0, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,0,0,0,0, }; inline constexpr std::uint8_t GATE_NS[80] = { 1,1,0,1,0,1,1,1, 1,1,1,1,1,1,1,0, 1,1,0,1,0,1,1,1, 1,1,1,1,1,1,1,0, 1,1,0,1,0,1,1,1, 1,1,1,1,1,1,1,0, 1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1, 1,1,1,1,0,0,0,0, 0,0,0,0,0,0,0,0, }; // clang-format on inline bool gate_we(int xter) { return xter >= 0 && xter < 72 && GATE_WE[xter]; } inline bool gate_ns(int xter) { return xter >= 0 && xter < 80 && GATE_NS[xter]; } // Neighbour "family" tests (which adjacent tiles a network's autotiler connects to). inline bool road_family(int code) { return (code >= 0x1D && code < 0x2C) || (code >= 0x3F && code < 0x47) || code == 75 || code == 76 || (code >= 0x5D && code < 0x61); } inline bool rail_family(int code) { return xbld::is_rail_bed(std::uint8_t(code)); } inline bool pipe_family(int xund) { return (xund >= 0x10 && xund < 0x1F) || xund == 31 || xund == 32; } inline bool subway_family(int xund) { return (xund != 0 && xund < 0x10) || xund == 35 || xund == 31 || xund == 32 || xund == 34; } // WriteXBLD / WriteXUND: write the code. (The original also keeps a live tile-type // histogram; Libreopolis recomputes census from the map scan, so we omit it — spec §Write helper.) inline void write_xbld(City& c, int r, int col, int code) { c.xbld.at(r, col) = std::uint8_t(code); } inline void write_xund(City& c, int r, int col, int code) { c.xund.at(r, col) = std::uint8_t(code); } // Build the connectivity mask N=8 E=4 S=2 W=1: a side counts iff the neighbour passes `nbr(r,c)` AND the // per-XTER step gate allows it; XTXT 0xFA adds the border-direction bits (neighbor-connection edge). template inline int connect_mask(const City& c, int r, int col, NbrFn nbr) { int m = 0; if (r > 0 && nbr(r - 1, col) && gate_ns(c.xter.at(r - 1, col))) m |= 8; if (r < c.dimension() - 1 && nbr(r + 1, col) && gate_ns(c.xter.at(r + 1, col))) m |= 2; if (col > 0 && nbr(r, col - 1) && gate_we(c.xter.at(r, col - 1))) m |= 1; if (col < c.dimension() - 1 && nbr(r, col + 1) && gate_we(c.xter.at(r, col + 1))) m |= 4; if (c.xtxt.at(r, col) == 0xFA) { if (r == 0) m |= 8; if (r == c.dimension() - 1) m |= 2; if (col == 0) m |= 1; if (col == c.dimension() - 1) m |= 4; } return m; } // The corner-slope remap for the single-corner slopes 9-12 (indexed [(shape-9)*4 + dir]): the CARDINAL // ramp the corner tile becomes when a network crosses it heading `dir`, so the piece ramps along its travel // axis and CONNECTS (instead of a flat tile floating on a corner). Verbatim from the original. // clang-format off inline constexpr int CORNER_RAMP[16] = {2,1,2,1, 2,3,2,3, 4,3,4,3, 4,1,4,1}; // clang-format on // Terrain adapter: reshape the slope under a network tile so the piece sits flush & connects. // Shapes 5-8 flatten to 0x0D (clear water); single-corner slopes 9-12 convert to a direction-aligned cardinal // ramp (CORNER_RAMP) — the fix for "roads won't join across a corner slope"; cardinal 1-4 / saddle 13 / flat // are left as-is (the piece rides them directly). inline void terrain_adapt(City& c, int r, int col, int dir) { const int xt = c.xter.at(r, col); if (xt >= 0x30) return; const int shape = xt & 0x0F; if (!netdetail::TERRACE[shape]) return; // only shapes 5-12 adapt if (netdetail::RAMP[shape]) { // 9-12 → cardinal ramp aligned to the road direction const int nx = CORNER_RAMP[(shape - 9) * 4 + (dir & 3)]; c.xter.at(r, col) = std::uint8_t(xt < 0x10 ? nx : nx + 16); return; } if (xt < 0x10) c.xter.at(r, col) = 0x0D; // 5-8 → flatten + clear water else { c.xter.at(r, col) = 0x1D; if (xt >= 0x20) c.xter.at(r, col) = 0x0D; } c.xbit.at(r, col) = std::uint8_t(c.xbit.at(r, col) & ~0x04); } // ---- autotilers (pick the shape of an already-laid tile from slope or connectivity) ---- inline void autotile_road(City& c, int r, int col) { const int xb = c.xbld.at(r, col); if (xb < 0x1D || xb >= 0x2C) return; int xterCode = c.xter.at(r, col); if (xterCode >= 0x30) xterCode = 0; const int shape = netdetail::RAMP[xterCode & 0x0F] ? SLOPE_SHAPE[xterCode & 0x0F] : CONNECT_SHAPE[connect_mask(c, r, col, [&](int rr, int cc) { return road_family(c.xbld.at(rr, cc)); })]; write_xbld(c, r, col, shape + 0x1D); } inline void autotile_rail(City& c, int r, int col) { const int xb = c.xbld.at(r, col); if (xb < 0x2C || xb >= 0x3B) return; // rail straights/corners/tees/cross 0x2C-0x3A int xterCode = c.xter.at(r, col); if (xterCode >= 0x30) xterCode = 0; if (netdetail::RAMP[xterCode & 0x0F]) { write_xbld(c, r, col, SLOPE_SHAPE[xterCode & 0x0F] + 0x2C); return; } // Slope-transition: a flat rail abutting a slope-rail neighbour (0x2E-0x31) takes a ramp-base piece. if (r > 0 && c.xbld.at(r - 1, col) == 0x2E) { write_xbld(c, r, col, 0x3B); return; } if (r < c.dimension() - 1 && c.xbld.at(r + 1, col) == 0x30) { write_xbld(c, r, col, 0x3D); return; } if (col > 0 && c.xbld.at(r, col - 1) == 0x2F) { write_xbld(c, r, col, 0x3C); return; } if (col < c.dimension() - 1 && c.xbld.at(r, col + 1) == 0x31) { write_xbld(c, r, col, 0x3E); return; } const int m = connect_mask(c, r, col, [&](int rr, int cc) { return rail_family(c.xbld.at(rr, cc)); }); write_xbld(c, r, col, CONNECT_SHAPE[m] + 0x2C); } inline void autotile_power(City& c, int r, int col) { const int xb = c.xbld.at(r, col); if (xb < 0x0E || xb >= 0x1D) return; // power-line codes 0x0E-0x1C int xterCode = c.xter.at(r, col); if (xterCode >= 0x30) xterCode = 0; const int shape = netdetail::RAMP[xterCode & 0x0F] ? SLOPE_SHAPE[xterCode & 0x0F] : CONNECT_SHAPE[connect_mask(c, r, col, [&](int rr, int cc) { return (c.xbit.at(rr, cc) & 0x80) != 0; })]; write_xbld(c, r, col, shape + 0x0E); } inline void autotile_pipe(City& c, int r, int col) { const int xu = c.xund.at(r, col); if (xu < 0x10 || xu >= 0x1F) return; // pipe codes XUND 16-30 int xterCode = c.xter.at(r, col); if (xterCode > 0x30) xterCode = 0; if (netdetail::RAMP[xterCode & 0x0F]) { write_xund(c, r, col, SLOPE_SHAPE[xterCode & 0x0F] + 16); return; } int m = connect_mask(c, r, col, [&](int rr, int cc) { return pipe_family(c.xund.at(rr, cc)); }); if (!m) m = 15; // isolated pipe → the cross/cap shape write_xund(c, r, col, CONNECT_SHAPE[m] + 16); } inline void autotile_subway(City& c, int r, int col) { const int xu = c.xund.at(r, col); if (xu == 0 || xu >= 0x10) return; // subway codes XUND 1-15 int xterCode = c.xter.at(r, col); if (xterCode > 0x30) xterCode = 0; const int shape = netdetail::RAMP[xterCode & 0x0F] ? SLOPE_SHAPE[xterCode & 0x0F] : CONNECT_SHAPE[connect_mask(c, r, col, [&](int rr, int cc) { return subway_family(c.xund.at(rr, cc)); })]; write_xund(c, r, col, shape + 1); } // Autotile a tile and its 4 orthogonal neighbours with `fn` (the original's post-place sweep). template inline void autotile_ring(City& c, int r, int col, Fn fn) { fn(c, r, col); if (r > 0) fn(c, r - 1, col); if (r < c.dimension() - 1) fn(c, r + 1, col); if (col > 0) fn(c, r, col - 1); if (col < c.dimension() - 1) fn(c, r, col + 1); } inline void dezone(City& c, int r, int col) { if ((c.xzon.at(r, col) & 0x0F) != 7) c.xzon.at(r, col) = std::uint8_t(c.xzon.at(r, col) & 0xF0); } // ---- connect (lay base / crossing, side effects, autotile) ---- inline void connect_road(City& c, int r, int col) { const int xb = c.xbld.at(r, col); const bool fam = (xb >= 29 && xb < 44) || (xb >= 63 && xb < 71) || xb == 75 || xb == 76 || (xb >= 93 && xb < 97); if (!fam) { if (xb < 14) write_xbld(c, r, col, 29); else switch (xb) { case 14: write_xbld(c, r, col, 68); break; case 15: write_xbld(c, r, col, 67); break; // ×power case 44: write_xbld(c, r, col, 70); break; case 45: write_xbld(c, r, col, 69); break; // ×rail case 73: write_xbld(c, r, col, 75); break; case 74: write_xbld(c, r, col, 76); break; // ×highway default: return; } dezone(c, r, col); } autotile_ring(c, r, col, autotile_road); } inline void connect_rail(City& c, int r, int col) { const int xb = c.xbld.at(r, col); const bool fam = (xb >= 44 && xb < 63) || (xb >= 69 && xb < 73) || (xb >= 108 && xb < 112) || xb == 77 || xb == 78; if (!fam) { if (xb < 14) write_xbld(c, r, col, 44); else switch (xb) { case 14: write_xbld(c, r, col, 72); break; case 15: write_xbld(c, r, col, 71); break; // ×power case 29: write_xbld(c, r, col, 69); break; case 30: write_xbld(c, r, col, 70); break; // ×road case 73: write_xbld(c, r, col, 77); break; case 74: write_xbld(c, r, col, 78); break; // ×highway default: return; } dezone(c, r, col); } autotile_ring(c, r, col, autotile_rail); } inline void connect_power(City& c, int r, int col) { const int xb = c.xbld.at(r, col); if (xb < 14) { write_xbld(c, r, col, 14); } else switch (xb) { // power lines do NOT dezone (faithful) case 29: write_xbld(c, r, col, 67); break; case 30: write_xbld(c, r, col, 68); break; // ×road case 44: write_xbld(c, r, col, 71); break; case 45: write_xbld(c, r, col, 72); break; // ×rail case 73: write_xbld(c, r, col, 79); break; case 74: write_xbld(c, r, col, 80); break; // ×highway default: return; // already power-family / not crossable } c.xbit.at(r, col) = std::uint8_t(c.xbit.at(r, col) | 0x80); // conductive autotile_ring(c, r, col, autotile_power); } inline void connect_pipe(City& c, int r, int col) { const int xu = c.xund.at(r, col); if (pipe_family(xu)) { autotile_ring(c, r, col, autotile_pipe); return; } if (xu == 0) write_xund(c, r, col, 16); else if (xu == 1) write_xund(c, r, col, 31); else if (xu == 2) write_xund(c, r, col, 32); else return; c.xbit.at(r, col) = std::uint8_t(c.xbit.at(r, col) | 0x20); // water-conductive autotile_ring(c, r, col, autotile_pipe); } inline void connect_subway(City& c, int r, int col) { const int xu = c.xund.at(r, col); if (subway_family(xu)) { autotile_ring(c, r, col, autotile_subway); return; } if (xu == 0) write_xund(c, r, col, 1); else if (xu == 16) write_xund(c, r, col, 32); else if (xu == 17) write_xund(c, r, col, 31); else return; autotile_ring(c, r, col, autotile_subway); } inline void place_one(City& c, int r, int col, NetType net, int dir) { if (net != NetType::Pipe && net != NetType::Subway) terrain_adapt(c, r, col, dir); switch (net) { case NetType::Road: connect_road(c, r, col); break; case NetType::Rail: connect_rail(c, r, col); break; case NetType::Power: connect_power(c, r, col); break; case NetType::Pipe: connect_pipe(c, r, col); break; case NetType::Subway: connect_subway(c, r, col); break; } } } // namespace commitdetail struct CommitResult { bool ok = false; int spent = 0; int tiles = 0; const char* msg = ""; }; // Commit a solved path for `net`, charging 25·(terrace tiles) + perTileCost·(total tiles) from city funds. // Returns ok=false (and changes nothing) if the path is invalid or funds are short. inline CommitResult commit_network_path(City& c, const NetworkPath& path, NetType net, int perTileCost) { if (!path.valid || path.steps.empty()) return {false, 0, 0, "invalid path"}; const int terrace = network_terrace_tiles(c, path, net); const int total = static_cast(path.steps.size()); const int cost = 25 * terrace + perTileCost * total; if (c.funds() < cost) return {false, 0, 0, "insufficient funds"}; // Place every tile, but only charge if at least one actually CHANGED — otherwise a click on an existing // tile of the same network (e.g. a subway on a subway station) would silently drain funds for a no-op. const bool underground = (net == NetType::Pipe || net == NetType::Subway); bool changed = false; for (const auto& s : path.steps) { const int before = underground ? c.xund.at(s.row, s.col) : c.xbld.at(s.row, s.col); commitdetail::place_one(c, s.row, s.col, net, s.dir); if ((underground ? c.xund.at(s.row, s.col) : c.xbld.at(s.row, s.col)) != before) changed = true; } if (!changed) return {false, 0, 0, "nothing to place"}; c.set_funds(c.funds() - cost); return {true, cost, total, "ok"}; } } // namespace sc2k::sim