// commute.cpp — out-of-line bodies for sc2k/sim/commute.hpp (see the header for the RE notes + contracts). // Split out of the header so editing an implementation recompiles just this TU (Task 5). #include "sc2k/sim/commute.hpp" #include "sc2k/tile_codes.hpp" namespace sc2k { CommuteStep commute_transition(int mode, int xbld, int zoneNibble, int xund, int altmHi, int sourceZone) { CommuteStep s; const bool job = ((1u << (zoneNibble & 0xF)) & kCommuteAcceptMask[sourceZone & 0xF]) != 0; auto step = [&](int m, int c) { s.moved = true; s.mode = m; s.cost = c; }; switch (mode) { case 0: // surface road, low-priority network if (job) { s.found = s.moved = true; } else if (xbld >= 0x3F && xbld < 0x43) step(2, 3); else if ((xbld >= 0x51 && xbld < 0x5D) || xbld == 106 || xbld == 107) step(3, 3); else if (xbld >= 0x5D && xbld < 0x61) step(1, 2); else if (is_commute_road(xbld)) step(0, 3); else if (xbld == 0xEC) step(8, 4); else if (xbld == sc2k::xbld::kRailDepot) step(10, 4); else if (xbld == sc2k::xbld::kSubwayStation) step(11, 4); else if (xbld > 0xFA) { s.found = s.moved = true; } break; case 1: // leg off a road onto a connector if (sc2k::xbld::is_highway(std::uint8_t(xbld))) step(1, 1); else if (xbld >= 0x5D && xbld < 0x61) step(0, 1); break; case 2: // bridge/tunnel leg (ALTM flag bits) if ((altmHi & 0x7C) != 0) step(2, 3); else if (is_commute_road(xbld)) step(0, 3); break; case 3: // ramp/connector leg back to road if ((xbld >= 0x51 && xbld < 0x5D) || xbld == 106 || xbld == 107) step(3, 3); else if (is_commute_road(xbld)) step(0, 3); break; case 4: // high-priority road network (cheaper) if (job) { s.found = s.moved = true; } else if (xbld >= 0x3F && xbld < 0x43) step(6, 2); else if ((xbld >= 0x51 && xbld < 0x5D) || xbld == 106 || xbld == 107) step(7, 2); else if (xbld >= 0x5D && xbld < 0x61) step(5, 2); else if (is_commute_road(xbld)) step(4, 2); else if (xbld == 0xEC) step(9, 4); else if (xbld == sc2k::xbld::kRailDepot) step(10, 4); else if (xbld == sc2k::xbld::kSubwayStation) step(11, 4); else if (xbld > 0xFA) { s.found = s.moved = true; } break; case 5: if (sc2k::xbld::is_highway(std::uint8_t(xbld))) step(5, 1); else if (xbld >= 0x5D && xbld < 0x61) step(4, 1); break; case 6: if ((altmHi & 0x7C) != 0) step(6, 2); else if (is_commute_road(xbld)) step(4, 2); break; case 7: if ((xbld >= 0x51 && xbld < 0x5D) || xbld == 106 || xbld == 107) step(7, 2); else if (is_commute_road(xbld)) step(4, 2); break; case 8: // rail leg (code 236) if (job) { s.found = s.moved = true; } else if (xbld == 236) step(8, 4); else if (is_commute_road(xbld)) step(4, 2); break; case 9: // rail leg (codes 236/237) if (job) { s.found = s.moved = true; } else if (xbld == 236 || xbld == 237) step(9, 4); else if (is_commute_road(xbld)) step(0, 3); break; case 10: // rail along the rail-bed (code 237) if (xbld == 237) step(10, 4); else if (is_commute_railbed(xbld)) step(12, 1); break; case 11: // subway leg (XUND) if (is_commute_subway(xund)) step(13, 1); break; case 12: // rail-bed surface leg if (xbld == 237) step(9, 4); else if (is_commute_railbed(xbld)) step(12, 1); else if (xbld > 0xFA) { s.found = s.moved = true; } break; case 13: // subway leg, may surface to rail (233) if (xbld == sc2k::xbld::kSubwayStation) step(9, 4); else if (is_commute_subway(xund)) step(13, 1); break; default: break; } return s; } } // namespace sc2k namespace sc2k { bool commute_trace(City& c, int row, int col, int sourceZone, int weight, int maxCost, MsRand& rng) { const int dim = c.dimension(); // Seed: first adjacent transit tile + its start mode (the original's 24-iteration scan). int curR = -1, curC = -1, mode = -1; for (const auto& d : kNeighborScan) { unsigned r = static_cast(row + d[0]), cc = static_cast(col + d[1]); if (r >= static_cast(dim) || cc >= static_cast(dim)) continue; int code = c.xbld.cells[c.index(r, cc)]; if (is_commute_road(code)) { mode = 0; curR = r; curC = cc; break; } if (code == 0xEC) { mode = 8; curR = r; curC = cc; break; } if (code == 0xED) { mode = 10; curR = r; curC = cc; break; } if (code == sc2k::xbld::kSubwayStation) { mode = 11; curR = r; curC = cc; break; } } if (mode < 0) return false; // no adjacent transit std::uint8_t buf[1536] = {}; // the original's two adjacent buffers CommutePathRing ring; ring.init(curR, curC); buf[0] = static_cast(mode); // mode ring: slot 0 = seed mode buf[512] = 15; // avail ring: slot 0 = 15 int depth = 1, cost = 0, avail = 15; const int turnBias = 2 * (static_cast(rng.next()) & 1) + 1; // 1 or 3 if (weight == 1) maxCost -= maxCost >> 2; // weight 1 → −25%, as the original bool found = false; while (cost < maxCost) { bool moved = false; int dirStart = static_cast(rng.next()) & 3, usedDir = 0, nr = 0, nc = 0; for (int j = 0; j < 4 && !moved; ++j) { int dir = (dirStart + turnBias) & 3; dirStart = dir; if (!(avail & (1 << dir))) continue; avail &= ~(1 << dir); usedDir = dir; nr = curR + kCommuteDirRow[dir]; nc = curC + kCommuteDirCol[dir]; if (!c.on_map(nr, nc)) { // Stepping OFF the map from a neighbour-city connection tile (XTXT 0xFA) reaches an // off-map job → success (the original: when the neighbour coord is >=0x80 it // tests dwMapXTXT[cur]==0xFA and sets the found flag). Without this, residents next to a // road/rail link to a neighbour city can't "commute out", so edge zones near a connection // wrongly fail job-access and abandon. The marker is the same XTXT 0xFA the demand update // counts as conn1000/conn1500. if (c.xtxt.cells[c.index(curR, curC)] == 0xFA) { found = moved = true; } continue; // dir consumed; off-map = no in-map step } const std::size_t next = c.index(nr, nc); int xb = c.xbld.cells[next]; int zn = c.xzon.cells[next] & 0xF; int xu = c.xund.cells[next]; int altmHi = (c.altm.cells[next] >> 8) & 0xFF; CommuteStep s = commute_transition(mode, xb, zn, xu, altmHi, sourceZone); cost += s.cost; if (s.found) { found = moved = true; } else if (s.moved) { moved = true; mode = s.mode; } } if (found) break; if (moved) { const int old = depth; // advance to a new node buf[old + 511] = static_cast(avail); // W1: untried dirs left at `old` ring.push(nr, nc); curR = nr; curC = nc; int newAvail; if (mode == 3 || mode == 7) newAvail = 1 << usedDir; // locked to the entry direction else if (mode == 11) newAvail = 15; // free else newAvail = kCommuteNoUturn[usedDir]; avail = newAvail; ++depth; buf[512 + old] = static_cast(newAvail); // W2 buf[1024 + old] = static_cast(cost); // W3: cost on arrival at new node buf[old] = static_cast(mode); // W4: mode at new node } else { do { // backtrack if (--depth > 0) { int rr, cc2; ring.pop(rr, cc2); ring.peek(curR, curC); mode = buf[depth - 1]; // R1: parent mode cost = buf[1023 + depth]; // R2: parent cost-on-arrival avail = buf[depth + 511]; // R3: this frame's untried dirs } } while (avail == 0 && depth > 0); if (depth == 0) break; // stack exhausted → give up } } if (found && weight > 0 && !ring.empty()) { // walk the path back, stamp XTRF while (!ring.empty()) { int rr, cc2; ring.pop(rr, cc2); int m = buf[--depth]; if (m < 2 || m == 3) { // road/highway legs carry traffic std::uint8_t& t = c.xtrf.at(rr / 2, cc2 / 2); int v = t + weight; t = static_cast(v > 255 ? 255 : v); } } } return found; } } // namespace sc2k namespace sc2k { bool commute_reachable(const City& c, int row, int col, int sourceZone, int maxCost) { const int dim = c.dimension(); int sr = -1, sc = -1, seedMode = -1; // seed at the first adjacent transit tile for (const auto& d : kNeighborScan) { unsigned r = static_cast(row + d[0]), cc = static_cast(col + d[1]); if (r >= static_cast(dim) || cc >= static_cast(dim)) continue; int code = c.xbld.cells[c.index(r, cc)]; if (is_commute_road(code)) { seedMode = 0; sr = r; sc = cc; break; } if (code == 0xEC) { seedMode = 8; sr = r; sc = cc; break; } if (code == 0xED) { seedMode = 10; sr = r; sc = cc; break; } if (code == sc2k::xbld::kSubwayStation) { seedMode = 11; sr = r; sc = cc; break; } } if (seedMode < 0) return false; // no adjacent transit → cannot commute // Stamped best-cost table over (tile, mode) states — reused across calls (a generation counter avoids // re-zeroing the runtime-sized table per call. CONTRACT: `best[sid]` is only read when // `stamp[sid] == gen`, and `gen` is bumped once per call, so any value left by a previous call — or a // previous city — is ignored (its stamp is an older generation). No reset hook is needed across a city // load; the only theoretical hazard is `gen` overflowing INT_MAX (~2·10⁹ commute calls), far past any run. const std::size_t states = std::size_t(c.tile_count()) * 16; static thread_local std::vector stamp, best; static thread_local int gen = 0; if (stamp.size() != states) { stamp.assign(states, 0); best.resize(states); gen = 0; } ++gen; auto sid = [dim](int r, int cc, int m) { return (r * dim + cc) * 16 + m; }; using Node = std::pair; // (cost, stateId) — min-heap by cost std::priority_queue, std::greater> pq; const int s0 = sid(sr, sc, seedMode); stamp[s0] = gen; best[s0] = 0; pq.push({0, s0}); while (!pq.empty()) { const auto [cost, st] = pq.top(); pq.pop(); if (cost > maxCost) break; // every remaining state is over budget if (stamp[st] == gen && best[st] < cost) continue; // a cheaper route to this state was found const int tile = st >> 4, m = st & 15, r = tile / dim, cc = tile % dim; for (int dir = 0; dir < 4; ++dir) { const int nr = r + kCommuteDirRow[dir], nc = cc + kCommuteDirCol[dir]; if (!c.on_map(nr, nc)) { if (c.xtxt.cells[c.index(r, cc)] == 0xFA) return true; // step off-map at a neighbour-city link = a job continue; } const std::size_t next = c.index(nr, nc); const int xb = c.xbld.cells[next], zn = c.xzon.cells[next] & 0xF; const int xu = c.xund.cells[next], ah = (c.altm.cells[next] >> 8) & 0xFF; const CommuteStep s = commute_transition(m, xb, zn, xu, ah, sourceZone); if (s.found) return true; // reached a job zone within budget if (!s.moved) continue; const int ncost = cost + s.cost; if (ncost > maxCost) continue; const int ns = sid(nr, nc, s.mode); if (stamp[ns] != gen || best[ns] > ncost) { stamp[ns] = gen; best[ns] = ncost; pq.push({ncost, ns}); } } } return false; } } // namespace sc2k