// sc2k/sim/network_commit.hpp — commit a solved path (sim/network_path.hpp) for all five line networks. // Surface networks adapt terrain and use tile-code crossings; pipes and subways remain underground and // independently owned, so either can occupy a tile without reshaping the surface or overwriting the other. // // Surface and subway geometry use the established connectivity tables. Native water-main geometry is // derived from City::waterMains adjacency at presentation time. #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 subway_family(int code) { return xund::is_subway(std::uint8_t(code)); } // 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) { // Native main shapes are derived by water_main_presentation_code() at render time. Keeping // this hook makes the shared retile-ring callers harmless while XUND remains subway-only. (void)c; (void)r; (void)col; } 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) { set_water_main(c, r, col, true); } 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 return; autotile_ring(c, r, col, autotile_subway); } inline void place_one(City& c, int r, int col, NetType net, int dir, bool adaptCompatibilityTerrain = true) { if (adaptCompatibilityTerrain && 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 = ""; }; // Place a prevalidated path without charging. Canonical construction callers first project their prepared // terrain and disable the old compatibility-only tile adapter; legacy callers retain it. inline bool place_network_path(City& city, const NetworkPath& path, NetType net, bool adaptCompatibilityTerrain = true) { if (!path.valid || path.steps.empty()) return false; const bool underground = net == NetType::Pipe || net == NetType::Subway; bool changed = false; for (const auto& step : path.steps) { const int before = net == NetType::Pipe ? int(has_water_main(city, step.row, step.col)) : (underground ? city.xund.at(step.row, step.col) : city.xbld.at(step.row, step.col)); commitdetail::place_one(city, step.row, step.col, net, step.dir, adaptCompatibilityTerrain); const int after = net == NetType::Pipe ? int(has_water_main(city, step.row, step.col)) : (underground ? city.xund.at(step.row, step.col) : city.xbld.at(step.row, step.col)); if (after != before) changed = true; } return changed; } // Commit a solved path for `net`, charging the same cost reported by network_path_cost. Existing pipe // segments are reusable and free; every other network retains its per-path-tile price. // 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 total = static_cast(path.steps.size()); const int chargeable = network_chargeable_tiles(c, path, net); if (net == NetType::Pipe && chargeable == 0) return {false, 0, 0, "nothing to place"}; const int cost = network_path_cost(c, path, net, perTileCost); 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 changed = place_network_path(c, path, net); if (!changed) return {false, 0, 0, "nothing to place"}; c.set_funds(c.funds() - cost); return {true, cost, total, "ok"}; } } // namespace sc2k::sim