// growth_scan.cpp — out-of-line bodies for sc2k/sim/growth_scan.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/growth_scan.hpp" namespace sc2k { int infra_funding_record(int code) { if ((code >= 29 && code < 44) || (code >= 63 && code < 71) || code == 75 || code == 76 || (code >= 93 && code < 97)) return 10; // roads if ((code >= 44 && code < 63) || (code >= 69 && code < 73) || (code >= 108 && code < 112) || code == 77 || code == 78) return 13; // rail if ((code >= 81 && code < 93) || code == 106 || code == 107) return 12; if ((code >= 97 && code < 108) || (code >= 73 && code < 81)) return 11; // power lines if (code >= 63 && code < 67) return 15; return -1; } } // namespace sc2k namespace sc2k { TileGrowth growth_tile_step(City& c, int row, int col, int demand, MsRand& rng, int rotation, ZonePops& pops) { const int t = row * 128 + col; const int xz = c.xzon.cells[t]; const int nibble = xz & 0xF; if (nibble < 1 || nibble > 6) return TileGrowth::Skip; // not an RCI zone const int code = c.xbld.cells[t]; int level; if (code >= 112) { // a built tile if ((xz & 0xF0 & kAnchorMask[rotation & 3]) == 0) return TileGrowth::Skip; // not the anchor const int bi = code - 112; level = (bi < 88) ? kBuildingLevel[bi] : 0; } else { // an empty tile if (code >= 29 || !has_transit_access(c, row, col)) return TileGrowth::Skip; level = 0; } int v33, v40; // demand pressure // Eligibility = powered ∧ can-reach-a-job (as the original). The powered gate short- // circuits the commute (faithful). We ALWAYS run the stochastic commute_trace when powered — it stamps // the XTRF traffic field and consumes the RNG stream the rest of the scan expects — but when the // deterministic-commute deviation is enabled we take the eligibility VERDICT from the exhaustive // reachability instead, so a well-connected building isn't abandoned by an unlucky single trace (which // otherwise death-spirals the whole city at max demand — see commute.hpp). bool eligible = false; if (is_growth_powered(c, row, col)) { (void)commute_trace(c, row, col, nibble, level, 100, rng); // walk it (draws the sim RNG — kept for stream parity) eligible = commute_reachable(c, row, col, nibble, 100); // ...but the growth verdict is deterministic (death-spiral fix) } if (eligible) { v33 = demand + 2000; v40 = 4000 - v33; } else { v33 = 0; v40 = 4000; } // Built-tile branch, keyed by the code's development flag (the original's growth tick): // 0 finished → census to its zone + abandon roll; // 1 construction site → UPGRADE in place to the real zone-class building (the step that finishes it); // 2 ruin → census to the special category + redevelop roll (never grows). TileGrowth outcome = TileGrowth::Skip; const int bi = (code >= 112) ? code - 112 : -1; const int flag = (bi >= 0 && bi < 88) ? kBuildingFlag[bi] : 0; if (level > 0 && bi >= 0) { // Census: a finished building books to its zone nibble, a ruin to the special cat 7 (both via // kPopPerCode/kPopToSpecialCat); a construction site books kPopPerCode == 0, i.e. nothing yet. const int pop = kPopPerCode[bi]; if (pop > 0) { pops.cat[kPopToSpecialCat[bi] ? 7 : nibble] += pop; outcome = TileGrowth::Census; } if (flag == 1) { // CONSTRUCTION SITE → finish it // Replace the generic placeholder with the real building with probability 0x4000/level (as the // original). We skip its rare special-building sub-path; // the common path is place_building(row,col,level,(nibble-1)/2) — the zone-class building that // actually books population. WITHOUT this the site never finishes and the demand loop deadlocks. // On a failed roll it falls through and instead tries to grow one size larger (also faithful). if (static_cast(rng.next()) < 0x4000 / level) { place_building(c, row, col, level, (nibble - 1) / 2, rng, rotation); return TileGrowth::Grew; } } else if (flag == 2) { // RUIN → maybe redevelop; never grows // v33 is 0 unless eligible (powered ∧ job-reachable), so a disconnected ruin never redevelops // (persists, drags land value); an eligible one rebuilds fast. if (15 * v33 / level > static_cast(rng.next())) { place_building(c, row, col, level, (nibble - 1) / 2, rng, rotation); return TileGrowth::Grew; } return outcome; // stay a ruin } else { // FINISHED building → abandon roll if (static_cast(rng.next()) < v40 / level) { decline_building(c, row, col, level, static_cast(rng.next()) & 1, rng, rotation); return TileGrowth::Declined; } } } // grow: not max level, dense zone or empty tile, and the R land-value gates (I zones, nibble≥5, skip). const int lv = c.xval.cells[(row / 2) * 64 + (col / 2)]; const bool lvOk = nibble >= 5 || ((level != 1 || lv >= 0x20) && (level != 2 || lv >= 0x60) && (level != 3 || lv >= 0xC0)); if (level != 4 && ((nibble & 1) == 0 || level <= 0) && lvOk) { if (3 * v33 / (level + 1) > static_cast(rng.next())) { grow_building(c, row, col, level, nibble, rng, rotation); return TileGrowth::Grew; } } return outcome; } } // namespace sc2k namespace sc2k { void special_zone_develop(City& c, int row, int col, int nibble, const std::array& count, MsRand& rng, int rotation) { if ((rng.next() & 3) != 0) return; // 3/4: idle (the original animates cranes/planes here) // The original's special-zone placer gates non-military development on power: it returns 0 (places // nothing) unless the tile or an orthogonal neighbour is powered. So an // unpowered airport/seaport never develops — exactly what the player observes. if (!is_growth_powered(c, row, col)) return; int code; if (nibble == 8) { // ----- AIRPORT ----- const int T = (count[221] + count[222]) / 5; if (count[238] / 4 >= T) code = 221; // parking plentiful → grow a runway else if (2 * count[225] < T) code = 225; else if (2 * count[234] < T) code = 234; // radar else if (count[230] < T) code = 230; // tarmac else if (count[228] / 2 < T) code = 228; // hangar else if (count[229] / 2 < T) code = 229; // hangar else code = (count[246] / 4 >= T) ? 238 : 246; // civ-parking / big hangar } else { // ----- SEAPORT (nibble 9) ----- const int T = count[224]; if (count[242] / 4 >= T) code = 224; // cargo yards plentiful → grow a crane/pier else if (count[240] / 4 < T) code = 240; // loading bay else code = (count[227] / 3 >= T) ? 242 : 227; // cargo yard / warehouse } bool ok; if (code == 221) ok = special_zone_place_powerline(c, row, col, nibble, rotation, count[221] & 1); else if (code == 224) ok = special_zone_place_pier(c, row, col, nibble, rotation); else if (code >= 225 && code <= 234) ok = special_zone_place_1x1(c, row, col, code, nibble, rotation); else ok = special_zone_place_2x2(c, row, col, code, nibble, rotation); if (!ok && nibble == 9) special_zone_place_1x1(c, row, col, 227, nibble, rotation); // seaport fallback: Warehouse } } // namespace sc2k namespace sc2k { bool military_place(City& c, int row, int col, int code, int size) { if (size == 2) { row &= ~1; col &= ~1; } // 2×2 blocks are even-aligned const int ext = size - 1; if (row < 0 || col < 0 || row + ext >= 128 || col + ext >= 128) return false; for (int r = row; r <= row + ext; ++r) for (int cc = col; cc <= col + ext; ++cc) if ((c.xzon.cells[std::size_t(r) * 128 + cc] & 0xF) != 7) return false; // must stay in the zone for (int r = row; r <= row + ext; ++r) for (int cc = col; cc <= col + ext; ++cc) c.xzon.cells[std::size_t(r) * 128 + cc] &= 0xF0; // drop tag → construct passes const bool ok = construct_building(c, row, col, code, size, 0); for (int r = row; r <= row + ext; ++r) for (int cc = col; cc <= col + ext; ++cc) { auto& z = c.xzon.cells[std::size_t(r) * 128 + cc]; z = static_cast((z & 0xF0) | 7); // re-stamp military c.xbit.cells[std::size_t(r) * 128 + cc] &= 0x0F; } return ok; } } // namespace sc2k namespace sc2k { void military_zone_develop(City& c, int row, int col, const std::array& count, MsRand& rng) { if ((rng.next() & 3) != 0) return; // 1/4 per tile ((rand()&3)==0, as the original) if (c.xbld.cells[std::size_t(row) * 128 + col] >= 0x0D) return; // this tile already has a building // A deficit-greedy toward the observed real-city building mix (the original's exact stat ladder reads the // military histogram; this reproduces the SHARES). `fill` = the 1×1 code that // fills a gap where a 2×2 hangar can't even-align to the base's parity (as the real 8×8 bases do). int code, size = 1, fill = 0; switch (c.military_base()) { case 2: { // ARMY — 0xE8 barracks (1×1) + 0xEF HQ (2×2) const int tot = count[0xE8] + count[0xEF] + 1; code = (count[0xEF] * 100 < 20 * tot) ? 0xEF : 0xE8; fill = 0xE8; break; } case 3: { // AIR FORCE — 0xDD runway-dominant + support + hangars const int tot = count[0xDD] + count[0xE2] + count[0xE4] + count[0xE5] + count[0xE7] + count[0xEA] + count[0xEF] + count[0xF6] + 1; if (count[0xDD] * 100 < 34 * tot) code = 0xDD; // runway strips (dominant) else if (count[0xE4] * 100 < 14 * tot) code = 0xE4; else if (count[0xE5] * 100 < 14 * tot) code = 0xE5; else if (count[0xF6] * 100 < 14 * tot) code = 0xF6; // 2×2 big hangar else if (count[0xE7] * 100 < 7 * tot) code = 0xE7; else if (count[0xEF] * 100 < 7 * tot) code = 0xEF; // 2×2 hangar else if (count[0xEA] * 100 < 5 * tot) code = 0xEA; else code = 0xE2; fill = 0xE4; break; } case 4: { // NAVY (DOS imports) — 0xE3 docks + 0xE0/0xF1/0xF2 const int tot = count[0xE0] + count[0xE3] + count[0xF1] + count[0xF2] + 1; if (count[0xE3] * 100 < 53 * tot) code = 0xE3; // docks (dominant) else if (count[0xF1] * 100 < 20 * tot) code = 0xF1; // 2×2 else if (count[0xF2] * 100 < 20 * tot) code = 0xF2; // 2×2 else code = 0xE0; fill = 0xE3; break; } case 5: // MISSILE — 0xF9 silo (3×3), no 1×1 fill code = 0xF9; size = 3; break; default: return; // 0/1 = no base } if (code == 0xEF || code == 0xF1 || code == 0xF2 || code == 0xF6) size = 2; // the 2×2 codes if (!military_place(c, row, col, code, size) && size >= 2 && fill) military_place(c, row, col, fill, 1); // 2×2 didn't fit here → a 1×1 fills the gap } } // namespace sc2k namespace sc2k { void growth_scan_region(City& c, int a, int b, MsRand& rng, int rotation, ZonePops& pops) { int demand[3] = {c.demand(0), c.demand(1), c.demand(2)}; // R/C/I sector demand std::array count{}; // live per-XBLD-code census (port cascades) for (int i = 0; i < 128 * 128; ++i) ++count[c.xbld.cells[std::size_t(i)]]; for (int row = a; row < 128; row += 4) for (int col = b; col < 128; col += 4) { const int nibble = c.xzon.cells[row * 128 + col] & 0xF; if (nibble >= 1 && nibble <= 6) growth_tile_step(c, row, col, demand[(nibble - 1) / 2], rng, rotation, pops); else if (nibble == 7) military_zone_develop(c, row, col, count, rng); // military base buildings grow here else if (nibble == 8 || nibble == 9) special_zone_develop(c, row, col, nibble, count, rng, rotation); } } } // namespace sc2k namespace sc2k { void infra_decay_region(City& c, int a, int b, MsRand& rng) { for (int row = a; row < 128; row += 4) for (int col = b; col < 128; col += 4) { const std::size_t t = std::size_t(row) * 128 + col; if ((c.xzon.cells[t] & 0xF) != 0) continue; // only nibble-0 (infrastructure / no zone) const int code = c.xbld.cells[t]; if (code < 29) continue; // terrain / trees / rubble / small — skip if (rng.next() % 128 != 0) continue; // 1/128 pre-gate (the original's LFSR mod-128 roll) const int rec = infra_funding_record(code); if (rec != 10 && rec != 11 && rec != 13) continue; // bridges(12) / tunnels(15) deferred (see above) if (!infra_underfunded_decays(c.budget_rate(rec), rng)) continue; // funded / passed the roll → survives c.xbld.cells[t] = static_cast((rng.next() & 3) + 1); // decay → Rubble 1-4 c.xbit.cells[t] &= ~0x80; // no longer conductive (the original clears XBIT 0x80) } } } // namespace sc2k