// disasters.cpp — out-of-line bodies for sc2k/sim/disasters.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/disasters.hpp" #include "sc2k/tile_codes.hpp" namespace sc2k::sim::detail { void demolish(City& c, int x, int y, bool lightFire) { const int code = c.xbld.at(x, y); if (code < 6) return; // the demolisher's top guard: bare/rubble/radioactive = no-op if (code >= 63 && code < 67) return; // 0x3F..0x42 — roads/rails not razed by spread // Raze the structure's OWN footprint only. The original walks the NE-corner+BSIZE block; // we flood the connected same-code component but CAP it at the footprint area N×N (BSIZE table), so a // disaster can't over-clear a run of abutting same-code buildings — the bug behind meltdowns/tornadoes // wiping whole neighbourhoods. Same bound the bulldoze tool uses (ui/tools.hpp clear_structure). const int Ns = building_sizes()[code]; // footprint side 1..4 const int budget = Ns * Ns; // ≤16 (4×4) constexpr int kCap = 128; // > 4·budget, so the flood never truncates a 4×4 int stack[kCap][2]; int sp = 0, cleared = 0; stack[sp][0] = x; stack[sp][1] = y; ++sp; while (sp > 0 && cleared < budget) { const int r = stack[--sp][0], cc = stack[sp][1]; if (!inb(r, cc) || c.xbld.at(r, cc) != code) continue; c.xbld.at(r, cc) = rubble_code(c, r, cc); // → Rubble 1..4 (flat) / 0 (slope) c.xzon.at(r, cc) = std::uint8_t(c.xzon.at(r, cc) & 0x0F); // drop building corner bits c.xbit.at(r, cc) = std::uint8_t(c.xbit.at(r, cc) & 0x3D); // clear mirror 0x02 / 0x40 / conductive 0x80 if (c.xtxt.at(r, cc) < 0xC9) c.xtxt.at(r, cc) = 0; // clear sign / sprite-link if (lightFire && !is_water(c, r, cc)) c.xtxt.at(r, cc) = fx::Fire; ++cleared; if (sp + 4 <= kCap) { stack[sp][0] = r + 1; stack[sp][1] = cc; ++sp; stack[sp][0] = r - 1; stack[sp][1] = cc; ++sp; stack[sp][0] = r; stack[sp][1] = cc + 1; ++sp; stack[sp][0] = r; stack[sp][1] = cc - 1; ++sp; } } } } // namespace sc2k::sim::detail namespace sc2k::sim::detail { int ignite(City& c, int x, int y, int force) { if (!inb(x, y)) return 0; if (c.xbit.at(x, y) & 0x04) return 0; // water if (c.xbld.at(x, y) < 6 && !force) return 0; // bare ground, not forced const std::uint8_t v = c.xtxt.at(x, y); if (v == 0 || v < 0x33) { // empty, or a sign index (label-free omitted) c.xtxt.at(x, y) = fx::Fire; c.xtrf.at(x / 2, y / 2) = 0; return 1; } if (v < 0xC9) { // a building/effect sprite already drawn here demolish(c, x, y, /*lightFire=*/true); c.xtxt.at(x, y) = fx::Fire; c.xtrf.at(x / 2, y / 2) = 0; return 1; } if (v < 0xF1) return 0; // 0xC9..0xF0 — a Things sprite link, can't burn // 0xF1..0xFF: an effect/sprite already present. The original tears down a 0xFA network tile here; an // existing 0xFB..0xFF fire/flood tile is left as-is (no re-ignite). We mirror "leave it". return 0; } } // namespace sc2k::sim::detail namespace sc2k::sim::detail { void finalize_burn(City& c, DisasterSim& d, int x, int y) { if (!inb(x, y) || c.xtxt.at(x, y) != fx::Fire) return; c.xtxt.at(x, y) = 0; // fire out at this tile const int code = c.xbld.at(x, y); if (code < 0x3F || code >= 0x43) { // skip the 0x3F..0x42 network range if (code >= 0x61) { // a building/zoned tile if ((c.xzon.at(x, y) & 0xF0) == 0xF0) demolish(c, x, y, /*lightFire=*/false); } else { // 0x00..0x60 — raze, then leave Rubble 1..4 demolish(c, x, y, /*lightFire=*/false); const std::uint8_t rub = std::uint8_t(d.lfsrMod(4) + 1); // burn-out rubble: XBLD = lfsrMod(4)+1 // …but rubble only survives on FLAT ground (deviation, 2026-07-08 — the demolisher's own // flat/slope rule extended to burn-out): on a sloped tile the burn leaves bare ground instead // of parking a flat rubble sprite on the hillside. The LFSR is still drawn so the stream stays // aligned with the original's. c.xbld.at(x, y) = (c.xter.at(x, y) != 0) ? std::uint8_t(0) : rub; } } } } // namespace sc2k::sim::detail namespace sc2k::sim::detail { void flood_flow(City& c, int x, int y, int maxLevel) { if (!inb(x, y)) return; if ((c.altm.at(x, y) & 0x1F) > maxLevel) return; const std::uint8_t ter = c.xter.at(x, y); if (ter >= 0x10 && ter < 0x20) return; // water-surface terrain — no spread const std::uint8_t v = c.xtxt.at(x, y); if (v == 0 || v < 0x33) { c.xtxt.at(x, y) = fx::Flood; c.xtrf.at(x / 2, y / 2) = 0; return; } if (v < 0xC9) { demolish(c, x, y, /*lightFire=*/false); c.xtxt.at(x, y) = fx::Flood; c.xtrf.at(x / 2, y / 2) = 0; } // 0xC9.. (sprite-link / existing effect): leave as-is, matching the original's fall-through. } } // namespace sc2k::sim::detail namespace sc2k::sim::detail { void city_centre(const City& c, int& cx, int& cy) { long sr = 0, sc = 0, n = 0; for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) if (xbld::is_developed(c.xbld.at(r, col))) { sr += r; sc += col; ++n; } if (n == 0) { cx = City::kDim / 2; cy = City::kDim / 2; return; } cx = int(sr / n); cy = int(sc / n); } } // namespace sc2k::sim::detail namespace sc2k::sim { void disaster_epicentre(const City& c, int type, MsRand& rng, int& x, int& y) { auto rnd = [&] { return int(rng.next() % 0x7E) + 1; }; // 1..126, the original's per-axis roll switch (type) { case 6: case 7: case 0x0B: case 0x0E: // quake / tornado / volcano / major flood x = rnd(); y = rnd(); break; default: { int cx, cy; detail::city_centre(c, cx, cy); // wCityCenterX/Y if (type == sim::dtype::kFire) { x = cx + int(rng.next() % 0x28) - 20; y = cy + int(rng.next() % 0x28) - 20; } // fire ±20 else if (type == sim::dtype::kRiot || type == sim::dtype::kMonster) { x = cx + int(rng.next() & 0x1F) - 15; y = cy + int(rng.next() & 0x1F) - 15; } // riot / monster ±15 else { x = cx; y = cy; } // flood / firestorm / fallback → centre exact break; } } x = std::clamp(x, 0, City::kDim - 1); y = std::clamp(y, 0, City::kDim - 1); } } // namespace sc2k::sim namespace sc2k::sim { bool start_fire(City& c, DisasterSim& d, MsRand& rng, int ex, int ey) { static constexpr int dx[4] = {0, 1, 0, -1}; // spread deltas (measured from the original) static constexpr int dy[4] = {-1, 0, 1, 0}; if (ex < 0) disaster_epicentre(c, 1, rng, ex, ey); // menu path: centre ±20 int x = ex, y = ey; int dir = 0, radius = 1, step = 0; for (;;) { x += dx[dir]; y += dy[dir]; if (detail::inb(x, y) && xbld::is_developed(c.xbld.at(x, y)) && detail::ignite(c, x, y, 0)) { d.active = true; d.type = sim::dtype::kFire; return true; } if (++step >= radius) { step = 0; if (dir & 1) ++radius; dir = (dir + 1) & 3; } if (radius >= 64) { // spiral exhausted → random fallback for (int tries = 0; tries < 200; ++tries) { int rx = d.gate(0x7F), ry = d.gate(0x7F); // j_RandomWordLFSRMod128 if (detail::ignite(c, rx, ry, 0)) { d.active = true; d.type = sim::dtype::kFire; return true; } } return false; } } } } // namespace sc2k::sim namespace sc2k::sim { bool start_flood(City& c, DisasterSim& d, MsRand& rng, int ex, int ey) { static constexpr int dx[4] = {0, 1, 0, -1}; // spiral deltas (shared with the fire creator) static constexpr int dy[4] = {-1, 0, 1, 0}; int cx, cy; detail::city_centre(c, cx, cy); if (ex < 0) { ex = cx + int(rng.next() % 0x28) - 20; ey = cy + int(rng.next() % 0x28) - 20; } int x = ex, y = ey; ex = cx; ey = cy; int dir = 0, radius = 1, step = 0; for (int guard = 0; guard < 0x4000; ++guard) { // find the source: first terrain edge/shore near centre x += dx[dir]; y += dy[dir]; if (detail::inb(x, y)) { const int t = c.xter.at(x, y); if (t >= 0x20 && t < 0x30) { ex = x; ey = y; break; } } if (++step >= radius) { step = 0; if (dir & 1) ++radius; dir = (dir + 1) & 3; } if (radius >= 64) break; // none near → fall back to the centre tile } auto seed = [&](int sx, int sy) { // a cross of flood water, water-guarded (XBIT & 4) if (detail::inb(sx, sy) && !detail::is_water(c, sx, sy)) c.xtxt.at(sx, sy) = fx::Flood; }; seed(ex, ey); seed(ex - 1, ey); seed(ex + 1, ey); seed(ex, ey - 1); seed(ex, ey + 1); d.effectTimer = 60; // the 60-frame window the tick spreads under d.active = true; d.type = sim::dtype::kFlood; return true; } } // namespace sc2k::sim namespace sc2k::sim { bool start_riot_once(City& c, MsRand& rng, int ex, int ey) { static constexpr int dx[4] = {0, 1, 0, -1}; // walk deltas static constexpr int dy[4] = {-1, 0, 1, 0}; int x = ex, y = ey, dir = 0, radius = 1, step = 0; for (;;) { x += dx[dir]; y += dy[dir]; if (detail::inb(x, y) && detail::riot_road(c.xbld.at(x, y)) && !(c.xbit.at(x, y) & 4) && c.xtxt.at(x, y) == 0) { c.xtxt.at(x, y) = (rng.next() & 1) ? fx::RiotNW : fx::RiotSE; // (rand&1)-3 = 0xFE/0xFD return true; } if (++step >= radius) { step = 0; if (dir & 1) ++radius; dir = (dir + 1) & 3; } if (radius >= 128) return false; } } } // namespace sc2k::sim namespace sc2k::sim { bool start_riot(City& c, DisasterSim& d, MsRand& rng, int ex, int ey) { if (ex < 0) disaster_epicentre(c, 3, rng, ex, ey); // riot epicentre: centre ±15 (else the scripted epicentre) if (!start_riot_once(c, rng, ex, ey)) return false; start_riot_once(c, rng, ex, ey); start_riot_once(c, rng, ex, ey); d.active = true; d.type = sim::dtype::kRiot; return true; } } // namespace sc2k::sim namespace sc2k::sim { bool start_mass_riots(City& c, DisasterSim& d, MsRand& rng) { int cx, cy; detail::city_centre(c, cx, cy); const int count = int(10L * c.zone_pop(0) / 10000) + 5; // dwCityPopulation/0x2710 + 5 bool any = false; for (int n = 0; n < count; ++n) { const int ex = cx + int(rng.next() & 0x1F) - 16, ey = cy + int(rng.next() & 0x1F) - 16; // ±16 box if (start_riot_once(c, rng, ex, ey)) any = true; } if (!any) return false; d.active = true; d.type = sim::dtype::kMassRiots; return true; } } // namespace sc2k::sim namespace sc2k::sim { bool volcano_can_raise(const City& c, int r, int col, int gen, std::vector& memo) { if (!detail::inb(r, col)) return true; // off-map: not itself a blocker (the original returns 1) const std::size_t i = std::size_t(r) * City::kDim + col; if (memo[i] / 2 == gen) return (memo[i] & 1) != 0; // already evaluated this call memo[i] = gen * 2; // mark visited, provisionally NOT raisable if ((c.xzon.at(r, col) & 0x0F) == 7) return false; if (detail::is_water(c, r, col)) return false; const int a = c.altm.at(r, col) & 0x1F; if (a >= 30) return false; static constexpr int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; // 8-neighbour deltas static constexpr int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; for (int k = 0; k < 8; ++k) { const int nr = r + dx8[k], nc = col + dy8[k]; if (detail::inb(nr, nc) && ((c.xzon.at(nr, nc) & 0x0F) == 7 || detail::is_water(c, nr, nc))) return false; } static constexpr int cdr[4] = {-1, 0, 1, 0}, cdc[4] = {0, -1, 0, 1}; for (int k = 0; k < 4; ++k) { // every strictly-lower cardinal neighbour must raise too const int nr = r + cdr[k], nc = col + cdc[k]; if (detail::inb(nr, nc) && (c.altm.at(nr, nc) & 0x1F) < a && !volcano_can_raise(c, nr, nc, gen, memo)) return false; } memo[i] = gen * 2 + 1; // resolved: raisable return true; } } // namespace sc2k::sim namespace sc2k::sim { bool volcano_can_raise(const City& c, int r, int col) { std::vector memo(std::size_t(City::kDim) * City::kDim, 0); return volcano_can_raise(c, r, col, 1, memo); } } // namespace sc2k::sim namespace sc2k::sim { void volcano_raise(City& c, int r, int col, long& budget, std::vector& seen, int& R0, int& C0, int& R1, int& C1) { if (!detail::inb(r, col)) return; const std::size_t i = std::size_t(r) * City::kDim + col; if (seen[i]) return; seen[i] = 1; const int a = c.altm.cells[i] & 0x1F; if (a >= 30) return; static constexpr int cdr[4] = {-1, 0, 1, 0}, cdc[4] = {0, -1, 0, 1}; for (int k = 0; k < 4; ++k) { const int nr = r + cdr[k], nc = col + cdc[k]; if (detail::inb(nr, nc) && (c.altm.at(nr, nc) & 0x1F) < a) volcano_raise(c, nr, nc, budget, seen, R0, C0, R1, C1); } if (budget < 25) return; // out of budget → leave this tile unraised budget -= 25; c.altm.cells[i] = std::uint16_t((c.altm.cells[i] & ~0x1F) | ((a + 1) & 0x1F)); // The rising rock DESTROYS whatever was on the tile. Two-stage so the demolition is visible as the cone // climbs (per-tick growth): a real thing — building / road / tree (all live in XBLD: trees 0x06..0x0D, // roads 0x1D..0x2C, structures 0x70+) — razes to RUBBLE (1..4) on the step the rock reaches it; the next // step (cone risen further) clears the rubble to bare mountain. So you see "demolition → rubble → gone", // not a dragged-up road/building. (the original only clears XZON because the editor never raises under a // structure; the volcano does, and roads/trees can't burn, so the raise itself must wipe them.) // Fire/firestorm in XTXT is left intact (the eruption's smoke/flames). const int was = c.xbld.cells[i]; if (was >= 6 && building_sizes()[was] > 1) // A MULTI-TILE building: the rock reaching ANY of its tiles razes the WHOLE footprint (the shared // footprint destroyer, as the quake/fire use) — never a half-building stub fused into the cone. It // rubbles every footprint tile; already-set fire/smoke XTXT (≥ 0xC9) is kept, so the eruption reads. detail::demolish(c, r, col, /*lightFire=*/false); else c.xbld.cells[i] = std::uint8_t(was >= 6 ? ((r + col) & 3) + 1 : 0); // 1×1: structure/road/tree → rubble; rubble/bare → cleared c.xzon.cells[i] = 0; R0 = std::min(R0, r); C0 = std::min(C0, col); R1 = std::max(R1, r); C1 = std::max(C1, col); } } // namespace sc2k::sim namespace sc2k::sim { bool start_volcano(City& c, DisasterSim& d, MsRand& rng, int ex, int ey) { d.volcanoX = ex >= 0 ? ex : int(rng.next() % 0x7E) + 1; // random epicentre, rand()%0x7E+1 per axis d.volcanoY = ex >= 0 ? ey : int(rng.next() % 0x7E) + 1; // (a scenario scripts it — e.g. PORTLAND erupts at 60,53) d.volcanoBudget = 25000; // armed; the cone grows over the next ~dozen ticks if (detail::inb(d.volcanoX, d.volcanoY)) c.xtxt.at(d.volcanoX, d.volcanoY) = fx::Fire; // initial vent flame d.active = true; d.type = sim::dtype::kVolcano; return true; } } // namespace sc2k::sim namespace sc2k::sim { void volcano_grow_step(City& c, DisasterSim& d, MsRand& rng) { const int ex = d.volcanoX, ey = d.volcanoY; if (!detail::inb(ex, ey)) { d.volcanoBudget = 0; d.volcanoX = d.volcanoY = -1; return; } const int startAlt = c.altm.at(ex, ey) & 0x1F; std::vector seen(std::size_t(City::kDim) * City::kDim, 0); std::vector craiseMemo(std::size_t(City::kDim) * City::kDim, 0); int gen = 0, guard = 0; int R0 = City::kDim, C0 = City::kDim, R1 = -1, C1 = -1; while (d.volcanoBudget > 0 && (c.altm.at(ex, ey) & 0x1F) == startAlt && guard++ < 300) { const int x = ex + int(rng.next() % 5) - 2; // ±2 cone about the vent const int y = ey + int(rng.next() % 5) - 2; if (!detail::inb(x, y)) continue; c.xtxt.at(x, y) = (rng.next() & 1) == 0 ? fx::ToxicCloud : fx::Fire; if (volcano_can_raise(c, x, y, ++gen, craiseMemo)) { std::fill(seen.begin(), seen.end(), char(0)); volcano_raise(c, x, y, d.volcanoBudget, seen, R0, C0, R1, C1); // cone raise, $25/tile, cap 30 } else { d.volcanoBudget -= 1000; // can't raise here → drain the budget } const int fr = ex + int(rng.next() & 0x1F) - 16; // ±16 fire scatter const int fc = ey + int(rng.next() & 0x1F) - 16; if (detail::inb(fr, fc)) c.xtxt.at(fr, fc) = detail::is_water(c, fr, fc) ? fx::ToxicCloud : fx::Fire; } if (R1 >= 0) sc2k::detail::reslope_box(c, R0, C0, R1, C1, 2); // re-derive XTER slopes over the grown area if (d.volcanoBudget <= 0) d.volcanoX = d.volcanoY = -1; // growth phase complete (fire continues) } } // namespace sc2k::sim namespace sc2k::sim { bool start_earthquake(City& c, DisasterSim& d, MsRand& rng, int cx, int cy) { if (cx < 0) disaster_epicentre(c, 6, rng, cx, cy); // type-6 epicentre is RANDOM, not the city centre d.epiX = cx; d.epiY = cy; // Auto-Goto centres here for (int dr = -32; dr <= 32; ++dr) for (int dc = -32; dc <= 32; ++dc) { if ((rng.next() & 0x3F) != 0) continue; // 1-in-64 per tile const int x = cx + dr, y = cy + dc; if (detail::inb(x, y) && c.xbld.at(x, y) >= 0x0E) { if (rng.next() & 3) detail::demolish(c, x, y, false); // 3/4 — flattened by the shock else detail::ignite(c, x, y, 0); // 1/4 — sparks a fire } } d.active = true; d.type = sim::dtype::kQuake; return true; } } // namespace sc2k::sim namespace sc2k::sim { bool start_meltdown(City& c, DisasterSim& d, MsRand& rng) { using namespace detail; int vx = -1, vy = -1; for (int r = 0; r < City::kDim && vx < 0; ++r) for (int col = 0; col < City::kDim; ++col) if (c.xbld.at(r, col) == 0xCB) { vx = r; vy = col; break; } if (vx < 0) return false; // no reactor → nothing to melt down (the original returns 0) demolish(c, vx, vy, /*lightFire=*/true); for (int dr = -32; dr <= 32; ++dr) for (int dc = -32; dc <= 32; ++dc) { if ((rng.next() & 0x1F) != 0) continue; // ~1/32 of tiles const int x = vx + dr, y = vy + dc; if (!inb(x, y)) continue; if (rng.next() & 3) demolish(c, x, y, (rng.next() & 1) != 0); // 3/4 raze (half alight) else ignite(c, x, y, 1); // 1/4 lava → forced fire } d.active = true; d.type = sim::dtype::kMeltdown; return true; } } // namespace sc2k::sim namespace sc2k::sim { bool start_microwave(City& c, DisasterSim& d, MsRand& rng) { using namespace detail; int mx = -1, my = -1; for (int r = 0; r < City::kDim && mx < 0; ++r) for (int col = 0; col < City::kDim; ++col) if (c.xbld.at(r, col) == 0xCD) { mx = r; my = col; break; } if (mx < 0) return false; // no microwave plant present int x = mx, y = my; for (int step = 39; step > 0 && inb(x, y); --step) { if (c.xbld.at(x, y) != 0xCD) { if (is_water(c, x, y)) c.xtxt.at(x, y) = fx::ToxicCloud; else ignite(c, x, y, 1); } const int dir = rng.next() & 7; x += kDir8X[dir]; y += kDir8Y[dir]; } d.active = true; d.type = sim::dtype::kMicrowave; return true; } } // namespace sc2k::sim namespace sc2k::sim { bool start_hurricane(City& c, DisasterSim& d, MsRand& rng, int sweep) { using namespace detail; d.effectTimer = 60; d.aftershock = 50; // effect + aftershock timers — both faithful sweep &= 3; const bool down = (sweep == 0 || sweep == 1); // scan 127→0 (else 0→127) const bool swapAxes = (sweep == 1 || sweep == 3); // the scanned coordinate is the FIRST index auto bld = [&](int fixed, int scan) { return swapAxes ? c.xbld.at(scan, fixed) : c.xbld.at(fixed, scan); }; auto raze = [&](int fixed, int scan) { if (swapAxes) demolish(c, scan, fixed, false); else demolish(c, fixed, scan, false); }; auto wet = [&](int fixed, int scan) { if (swapAxes) c.xtxt.at(scan, fixed) = fx::Flood; else c.xtxt.at(fixed, scan) = fx::Flood; }; for (int n = 0; n < 20; ++n) { // 20 scan lines → demolish the first building (cold) const int fixed = d.gate(0x7F); int scan = down ? 127 : 0, guard = 0; for (; scan >= 0 && scan <= 127 && guard < 200; ++guard) { if (bld(fixed, scan) >= 0x0D) { if (down ? scan > 0 : scan < 127) raze(fixed, scan); // the original's demolish bound break; } scan += down ? -d.lfsrMod(20) : d.lfsrMod(20); // jump along the line (the original's LFSR mod-20 roll) } } const int floodLines = (sweep == 0 || sweep == 3) ? 50 : 100; // per the original's four cases for (int n = 0; n < floodLines; ++n) { // storm-surge flood on the first building along each line const int fixed = d.gate(0x7F); if (down) { for (int s = 127; s >= 0; --s) if (bld(fixed, s) >= 6) { if (s > 0) wet(fixed, s); break; } } else { for (int s = 0; s <= 127; ++s) if (bld(fixed, s) >= 6) { if (s < 127) wet(fixed, s); break; } } } (void)rng; d.active = true; d.type = sim::dtype::kHurricane; return true; } } // namespace sc2k::sim namespace sc2k::sim { bool start_firestorm(City& c, DisasterSim& d, MsRand& rng, int cx, int cy) { if (cx < 0) detail::city_centre(c, cx, cy); // menu path scatters about the centre; a scenario scripts it const int count = int(10L * c.zone_pop(0) / 10000) + 5; // resPop/10000 + 5, as the original bool any = false; for (int n = 0; n < count; ++n) { const int x = cx + int(rng.next() & 7) - 4, y = cy + int(rng.next() & 7) - 4; // ±4 box if (detail::inb(x, y)) { c.xtxt.at(x, y) = fx::ToxicCloud; any = true; } } if (!any) return false; d.active = true; d.type = sim::dtype::kFirestorm; return true; } } // namespace sc2k::sim namespace sc2k::sim { void dispatch_fortify(City& c, DisasterSim& d, MsRand& rng, int i, int j, std::uint8_t link) { const int idx = link - 0xC9; // XTXT sprite-link → Thing slot (the original's 12·v−2412) const int type = c.xthg[std::size_t(idx) * 12 + 0]; // thg::Type if (type != 7 && type != 8 && type != 14) return; // only police/fire/military units fortify static constexpr int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; // {N,W,S,E} — the original's case order if (type == 8 || type == 14 || (type == 7 && d.gate(0x0F) == 0)) { // douse fire on a random neighbour const int k = rng.next() & 3, nx = i + dx[k], ny = j + dy[k]; if (detail::inb(nx, ny)) detail::finalize_burn(c, d, nx, ny); // no-op unless that neighbour is on fire } if (type == 7 || type == 14) { // disperse a rioter on a random neighbour const int k = rng.next() & 3, nx = i + dx[k], ny = j + dy[k]; // random-walk spread if (detail::inb(nx, ny) && (c.xtxt.at(nx, ny) == fx::RiotSE || c.xtxt.at(nx, ny) == fx::RiotNW)) c.xtxt.at(nx, ny) = 0; } } } // namespace sc2k::sim namespace sc2k::sim { bool disaster_tick(City& c, DisasterSim& d, MsRand& rng) { using namespace detail; if (d.effectTimer > 0) --d.effectTimer; // effect-timer countdown if (d.volcanoBudget > 0) volcano_grow_step(c, d, rng); // an erupting volcano climbs ≈one step this tick bool fire = false, flood = false, riot = false, toxic = false, other = false; for (int i = 0; i < City::kDim; ++i) { for (int j = 0; j < City::kDim; ++j) { const std::uint8_t v = c.xtxt.at(i, j); if (v == fx::Fire) { // case 255 fire = true; if ((rng.next() & 3) != 0) continue; // acts 1 frame in 4 if (is_water(c, i, j)) { c.xtxt.at(i, j) = 0; continue; } switch (rng.next() & 7) { case 0: ignite(c, i - 1, j, 0); break; // spread N case 1: ignite(c, i, j - 1, 0); break; // spread W case 2: ignite(c, i + 1, j, 0); break; // spread S case 3: ignite(c, i, j + 1, 0); break; // spread E case 5: // a building here → it burns if (xbld::is_developed(c.xbld.at(i, j))) { demolish(c, i, j, true); finalize_burn(c, d, i, j); } break; default: { // 4,6,7 — firefighting roll (XFIR coverage) const int resist = c.xfir.at(i / 4, j / 4) + 8; if (resist > (rng.next() & 0xFF)) { demolish(c, i, j, true); finalize_burn(c, d, i, j); } break; } } } else if (v == fx::Flood) { // case 252 — flood: water spreads downhill while the timer runs flood = true; // While the flood timer is running (or, once over, half the time) it advances; else it drains out. if (d.effectTimer || d.gate(1) == 0) { // timer running, or a coin-flip gate if (d.effectTimer >= 52 || (rng.next() & 3) == 0) { if (d.effectTimer < 30 && (rng.next() & 3) == 0) { // late phase → settle a tile if (xbld::is_developed(c.xbld.at(i, j))) demolish(c, i, j, false); c.xtxt.at(i, j) = 0; } if (d.effectTimer > 0) { // spread downhill to one neighbour const int lvl = c.altm.at(i, j) & 0x1F; switch (rng.next() & 3) { case 0: flood_flow(c, i - 1, j, lvl); break; case 1: flood_flow(c, i, j - 1, lvl); break; case 2: flood_flow(c, i + 1, j, lvl); break; case 3: flood_flow(c, i, j + 1, lvl); break; } } } } else { c.xtxt.at(i, j) = 0; // flood over → drain } } else if (v == fx::RiotNW || v == fx::RiotSE) { // cases 254/253 — riot fronts (march along streets) riot = true; if ((rng.next() & 3) != 0) continue; // acts 1 frame in 4 if ((rng.next() & 0xFF) == 0 || is_water(c, i, j)) { c.xtxt.at(i, j) = 0; continue; } // drain c.xtrf.at(i / 2, j / 2) = 0; switch (rng.next() & 0x7F) { // occasional incidental damage to a neighbour case 0: ignite(c, i - 1, j, 0); break; case 1: ignite(c, i, j - 1, 0); break; case 2: ignite(c, i + 1, j, 0); break; case 3: ignite(c, i, j + 1, 0); break; default: break; } // RiotNW (0xFE) marches N/W and hands off to 0xFD; RiotSE (0xFD) marches S/E → 0xFE. const bool nw = (v == fx::RiotNW); const int a = nw ? i - 1 : i + 1, b = nw ? j - 1 : j + 1; int n = 0; if ((nw ? i > 0 : i < 127) && riot_able(c, a, j)) n = 1; if ((nw ? j > 0 : j < 127) && riot_able(c, i, b)) n |= 2; const std::uint8_t spread = nw ? fx::RiotNW : fx::RiotSE; const std::uint8_t settle = nw ? fx::RiotSE : fx::RiotNW; if (n) { c.xtxt.at(i, j) = ((rng.next() & 7) == 0) ? settle : 0; // settle to draining, or recede if (n == 3) n = int(rng.next() & 1) + 1; if (n == 1) riot_spread(c, a, j, spread); else riot_spread(c, i, b, spread); } else { c.xtxt.at(i, j) = settle; // nowhere to go → settle to draining } } else if (v == fx::ToxicCloud) { // case 251 — toxic cloud / firestorm tile: walks downhill → ruin toxic = true; if (rng.next() & 1) continue; // acts half the time if (d.gate(0x3F) == 0 || (is_water(c, i, j) && (rng.next() & 0xF) == 0)) { // 1/64, or water 1/16 c.xtxt.at(i, j) = 0; continue; } const int b = c.xbld.at(i, j); if (xbld::is_developed(b) && b < xbld::kPlantFirst && !kFireImmune[b - xbld::kFirstDeveloped]) demolish(c, i, j, false); // ruin it // advance to the lowest-altitude neighbour (downhill), else a random one static constexpr int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0}; // spread deltas int best = -1, minLevel = c.altm.at(i, j) & 0x1F; for (int k = 0; k < 4; ++k) { const int nx = i + dx[k], ny = j + dy[k]; if (inb(nx, ny)) { const int nl = c.altm.at(nx, ny) & 0x1F; if (nl < minLevel) { best = k; minLevel = nl; } } } if (best < 0) best = int(rng.next() & 3); c.xtxt.at(i, j) = 0; // clear the current tile (single moving front) const int nx = i + dx[best], ny = j + dy[best]; if (inb(nx, ny) && c.xtxt.at(nx, ny) < 0x33) c.xtxt.at(nx, ny) = fx::ToxicCloud; // cloud spread } else if (v >= 0xC9 && v <= 0xF0) { // default case — a Thing sprite-link: a dispatched dispatch_fortify(c, d, rng, i, j, v); // police/fire/military marker fortifies a neighbour } else if (fx::is_effect(v)) { other = true; // any unhandled 0xFA..0xFF tile — keep the disaster live } } } // Re-ignition pass (the tick's tail): while the spread timer runs (the hurricane arms // it at 50), randomly raze a building each frame — the storm's rolling damage. if (d.effectTimer && d.aftershock) { if (d.gate(1) == 0) { // !j_RandomWordLFSRMod2() const int rx = d.gate(0x7F), ry = d.gate(0x7F); // two j_RandomWordLFSRMod128 if (c.xbld.at(rx, ry) > 0x70) demolish(c, rx, ry, false); } --d.aftershock; } // Disaster-Thing keep-alive — the scan at the tail of the original's disaster driver: after // the tick reports its effect-tile result, the original re-arms the active flag while ANY live Thing // is a Monster (type 5), Tornado (15), Explosion (6) or a crashing plane (type 1 in state 7). Without // this, a Thing-based disaster with no effect tiles "subsided" on its very first tick and the Thing // froze mid-map (the tick that moves it only runs while the disaster is active) — the reported stuck // monster/tornado and the air crash whose paper printed while its fire still spread. bool thing = false; for (int t = 1; t < 40 && !thing; ++t) { const std::uint8_t ty = c.xthg[std::size_t(t) * 12 + 0], st = c.xthg[std::size_t(t) * 12 + 2]; thing = (ty == 5 || ty == 15 || ty == 6 || (ty == 1 && st == 7)); } d.active = fire || flood || riot || toxic || other || thing || d.volcanoBudget > 0; // live while anything runs return d.active; } } // namespace sc2k::sim namespace sc2k::sim { bool disaster_focus(const City& c, const DisasterSim& d, int& fr, int& fc) { if (d.volcanoX >= 0) { fr = d.volcanoX; fc = d.volcanoY; return true; } // the erupting vent if (d.type == sim::dtype::kQuake && d.epiX >= 0) { fr = d.epiX; fc = d.epiY; return true; } // quake: the prepared epicentre // (the original centres there BEFORE the shake — its damage // leaves rubble, not effect tiles, so the centroid below // would miss a fire-less quake entirely) long sr = 0, sc = 0, n = 0; for (int r = 0; r < City::kDim; ++r) for (int cc = 0; cc < City::kDim; ++cc) if (fx::is_effect(c.xtxt.at(r, cc))) { sr += r; sc += cc; ++n; } // Moving Things that ARE the disaster — Tornado (XTHG type 15), Monster (5), Explosion (6); exactly // the set things_tick advances as a disaster. Regular traffic (aircraft/ships/trains, types 1-3/9) and // deployed dispatch units live in the SAME XTHG array but are NOT the disaster: averaging them in // dragged Goto off toward mid-traffic, so in a live city full of vehicles the actual fire/tornado // ended up off-screen. (slot 0 reserved; [0]=type, [3]=x, [4]=y) for (int i = 1; i < 40; ++i) { const std::size_t b = std::size_t(i) * 12; const std::uint8_t ttype = c.xthg[b]; if (ttype == 15 || ttype == 5 || ttype == 6 || (ttype == 1 && c.xthg[b + 2] == 7)) // + the crashing plane { sr += c.xthg[b + 3]; sc += c.xthg[b + 4]; ++n; } } if (n == 0) return false; fr = int(sr / n); fc = int(sc / n); return true; } } // namespace sc2k::sim