// growth.cpp — out-of-line bodies for sc2k/sim/growth.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.hpp" #include "sc2k/tile_codes.hpp" namespace sc2k { int place_building(City& c, int row, int col, int size, int zoneClass, MsRand& rng, int rotation) { auto& xb = c.xbld.cells; auto& xz = c.xzon.cells; auto& xbit = c.xbit.cells; const bool classicWaterLinks = !c.modernWaterSystem || c.waterDistributionModel == WaterDistributionModel::ClassicLinks; const std::uint8_t buildingBits = classicWaterLinks ? 0xE0 : 0xC0; auto at = [&](int r, int cc) { return c.index(r, cc); }; if (size == 1) { int code; if (zoneClass != 0) { // C / I 1×1 const int idx = 4 * zoneClass + 1; code = kZoneCode[idx] + (rng.next() % kZoneRange[idx]); } else { // R 1×1 — level from local land value int lv = c.xval.at(row / 2, col / 2) >> 6; if (lv > 2) lv = 2; code = kZoneCode[1] + (rng.next() & 3) + 4 * lv; // 112 + 0..3 + 4·lvClass } xb[at(row, col)] = static_cast(code); xz[at(row, col)] |= 0xF0; // 1×1 → all corner bits xbit[at(row, col)] |= buildingBits; c.legacyWaterLinks.cells[at(row, col)] = classicWaterLinks ? 1 : 0; return code; } const int idx = size + 4 * zoneClass; if (idx < 1 || idx >= 20) return -1; // guard the 20-entry code/range tables const int code = kZoneCode[idx] + (rng.next() % kZoneRange[idx]); const int half = size / 2; const int c0 = col - half; // footprint columns: c0 .. col if (row < 2 || col < 2 || row + half > c.dimension() - 2 || col > c.dimension() - 2 - half || c0 < 0) return -1; for (int r = row; r <= row + half; ++r) for (int cc = c0; cc <= c0 + half; ++cc) { xb[at(r, cc)] = static_cast(code); xz[at(r, cc)] &= 0x0F; // clear corner bits across the footprint xbit[at(r, cc)] |= buildingBits; c.legacyWaterLinks.cells[at(r, cc)] = classicWaterLinks ? 1 : 0; } const int rb = 4 * rotation; // then set the four corners xz[at(row, c0)] = static_cast(kCornerBits[rb + 0] | (xz[at(row, c0)] & 0xF)); xz[at(row + half, c0)] = static_cast(kCornerBits[rb + 1] | (xz[at(row + half, c0)] & 0xF)); xz[at(row + half, c0 + half)] = static_cast(kCornerBits[rb + 2] | (xz[at(row + half, c0 + half)] & 0xF)); xz[at(row, c0 + half)] = static_cast(kCornerBits[rb + 3] | (xz[at(row, c0 + half)] & 0xF)); return code; } } // namespace sc2k namespace sc2k { bool can_merge_tile(const City& c, int row, int col, int altitude, int nibble, int threshold, const terrain::Model* terrainModel) { if (!c.on_map(row, col)) return false; if (terrain::Query(c, terrainModel).base_level({row, col}) != altitude) return false; if ((c.xzon.cells[c.index(row, col)] & 0xF) != nibble) return false; const int code = c.xbld.cells[c.index(row, col)]; if (code >= threshold) return false; if ((code >= 0x1D && code < 0x2C) || (code >= 0x3F && code < 0x47) || code == 75 || code == 76 || (code >= 0x5D && code < 0x61)) return false; // road / highway return !xbld::is_rail_bed(std::uint8_t(code)); // rail / infrastructure } } // namespace sc2k namespace sc2k { void clear_building(City& c, int row, int col, MsRand& rng, int rotation) { int anchorCol = col, anchorRow = row; // the original's argument order int corner; switch (c.xzon.cells[c.index(row, col)] & 0xF0) { // which corner is this tile? case 0x10: corner = (-rotation) & 3; break; case 0x20: corner = (1 - rotation) & 3; break; case 0x40: corner = (-2 - rotation) & 3; break; case 0x80: corner = (-1 - rotation) & 3; break; default: corner = 3; break; // the original reads uninitialised (clear is only ever } // called on a building corner) — anchor = (row,col) if (corner == 0) { anchorCol = col + 1; } // anchor offset by corner+rotation else if (corner == 1) { anchorCol = col + 1; anchorRow = row - 1; } else if (corner == 2) { anchorRow = row - 1; } // corner == 3: anchor stays (row,col) place_building(c, anchorRow, anchorCol, 1, 4, rng, rotation); // overwrite the 2×2 with ruins place_building(c, anchorRow + 1, anchorCol, 1, 4, rng, rotation); place_building(c, anchorRow + 1, anchorCol - 1, 1, 4, rng, rotation); place_building(c, anchorRow, anchorCol - 1, 1, 4, rng, rotation); } } // namespace sc2k namespace sc2k { int grow_building(City& c, int row, int col, int level, int nibble, MsRand& rng, int rotation, const terrain::Model* terrainModel) { constexpr int kGrownSet = 3; // the original's fixed set id switch (level) { case 0: return place_building(c, row, col, 1, kGrownSet, rng, rotation); case 1: { // try the four 2×2 orientations const int alt = terrain::Query(c, terrainModel).base_level({row, col}); int m = can_merge_tile(c, row + 1, col, alt, nibble, 140, terrainModel) ? 1 : 0; // S if (can_merge_tile(c, row, col + 1, alt, nibble, 140, terrainModel)) m |= 4; // E if (can_merge_tile(c, row + 1, col + 1, alt, nibble, 140, terrainModel)) m |= 2; // SE if ((m & 7) == 7) return place_building(c, row, col + 1, 2, kGrownSet, rng, rotation); if (can_merge_tile(c, row, col - 1, alt, nibble, 140, terrainModel)) m |= 0x10; // W if (can_merge_tile(c, row + 1, col - 1, alt, nibble, 140, terrainModel)) m |= 8; // SW if ((m & 0x19) == 0x19) return place_building(c, row, col, 2, kGrownSet, rng, rotation); if (can_merge_tile(c, row - 1, col, alt, nibble, 140, terrainModel)) m |= 0x40; // N if (can_merge_tile(c, row - 1, col + 1, alt, nibble, 140, terrainModel)) m |= 0x20; // NE if ((m & 0x64) == 0x64) return place_building(c, row - 1, col + 1, 2, kGrownSet, rng, rotation); if (can_merge_tile(c, row - 1, col - 1, alt, nibble, 140, terrainModel)) m |= 0x80; // NW if ((m & 0xD0) == 0xD0) return place_building(c, row - 1, col, 2, kGrownSet, rng, rotation); return -1; } case 2: return place_building(c, row, col, 3, kGrownSet, rng, rotation); case 3: { // level 3 → the largest (size 4) const int alt = terrain::Query(c, terrainModel).base_level({row, col}); for (int o = 0; o < 4; ++o) { // try the four footprint orientations const int r0 = row - (o & 1), c0 = (o / 2) + col; // candidate anchor // the eight perimeter tiles must all be mergeable (threshold 174) if (!can_merge_tile(c, r0, c0, alt, nibble, 174, terrainModel)) continue; if (!can_merge_tile(c, r0, c0 - 1, alt, nibble, 174, terrainModel)) continue; if (!can_merge_tile(c, r0, c0 - 2, alt, nibble, 174, terrainModel)) continue; if (!can_merge_tile(c, r0 + 1, c0 - 2, alt, nibble, 174, terrainModel)) continue; if (!can_merge_tile(c, r0 + 2, c0 - 2, alt, nibble, 174, terrainModel)) continue; if (!can_merge_tile(c, r0 + 2, c0 - 1, alt, nibble, 174, terrainModel)) continue; if (!can_merge_tile(c, r0 + 2, c0, alt, nibble, 174, terrainModel)) continue; if (!can_merge_tile(c, r0 + 1, c0, alt, nibble, 174, terrainModel)) continue; // and a special building (XBLD 35–43, a port/landmark) must abut one of the four sides auto xb = [&](int r, int cc) { return c.on_map(r, cc) ? c.xbld.cells[c.index(r, cc)] : -1; }; int s; bool ok = false; if (r0 > 0 && c0 < c.dimension() - 1) { s = xb(r0 - 1, c0 + 1); ok = s == 35 || s == 39 || s == 40 || s == 43; } if (!ok && r0 > 0 && c0 > 2) { s = xb(r0 - 1, c0 - 3); ok = s == 36 || s == 40 || s == 41 || s == 43; } if (!ok && r0 < c.dimension() - 3 && c0 > 2) { s = xb(r0 + 3, c0 - 3); ok = s == 37 || s == 41 || s == 42 || s == 43; } if (!ok && r0 < c.dimension() - 3 && c0 < c.dimension() - 1) { s = xb(r0 + 3, c0 + 1); ok = s == 38 || s == 42 || s == 39 || s == 43; } if (!ok) continue; auto clr = [&](int r, int cc) { // bulldoze any building on the footprint if (c.xbld.cells[c.index(r, cc)] >= 0x8C) clear_building(c, r, cc, rng, rotation); }; clr(r0, c0); clr(r0, c0 - 1); clr(r0, c0 - 2); clr(r0 + 1, c0 - 2); clr(r0 + 2, c0 - 2); clr(r0 + 2, c0 - 1); clr(r0 + 2, c0); clr(r0 + 1, c0); return place_building(c, r0, c0, 4, kGrownSet, rng, rotation); } return -1; // no orientation fit } default: return -1; } } } // namespace sc2k namespace sc2k { int decline_building(City& c, int row, int col, int level, int split, MsRand& rng, int rotation) { constexpr int kRuinSet = 4; // the original's fixed set id auto ruin = [&](int r, int cc, int size) { return place_building(c, r, cc, size, kRuinSet, rng, rotation); }; switch (level) { case 1: return ruin(row, col, 1); // a single 1×1 ruin case 2: if (split) { // 4 separate 1×1 ruins (the 2×2) ruin(row, col, 1); ruin(row + 1, col, 1); ruin(row + 1, col - 1, 1); return ruin(row, col - 1, 1); } return ruin(row, col, 2); // a 2×2 ruin case 3: return split ? ruin(row, col, 2) : ruin(row, col, 3); // 2×2 or 3×3 ruin case 4: if (split) { // perimeter 1×1 ruins + a 3×3 core ruin(row, col, 1); ruin(row + 1, col, 1); ruin(row + 2, col, 1); ruin(row + 2, col - 1, 1); ruin(row + 2, col - 2, 1); ruin(row + 1, col - 2, 1); ruin(row, col - 2, 1); ruin(row, col - 1, 1); const int roll = static_cast(rng.next()); return ruin(row + (roll & 1), col - (roll & 3) / 2, 3); } return ruin(row, col, 4); // a 4×4 ruin (set-4 size-4: guarded) default: return -1; } } } // namespace sc2k namespace sc2k { bool construct_building(City& c, int row, int col, int code, int size, int rotation, const terrain::Model* terrainModel) { const int ext = size - 1; // footprint = size×size int waterTiles = 0; // marina (248): water cells in its footprint const terrain::Query terrainQuery(c, terrainModel); for (int r = row; r <= row + ext; ++r) // clearance: all buildable terrain for (int cc = col; cc <= col + ext; ++cc) { if (!c.on_map(r, cc)) return false; const int xb = c.xbld.cells[c.index(r, cc)]; if (xb >= 0x1D || xb == 5 || xb == 13) return false; if ((c.xzon.cells[c.index(r, cc)] & 0xF) == 7) return false; const terrain::TileQuery terrainTile = terrainQuery.tile({r, cc}); if (!terrainTile.dry()) { // a water tile… if (code == 248) { ++waterTiles; continue; } // …is allowed (and counted) for a marina return false; // …rejected for any other building } // Buildings sit only on FLAT ground. The original rejects any tile whose terrain-slope layer is // nonzero (the same flat-terrain test roads/on-ramps use — XTER 0 = flat). A slope would embed the // building into the hillside, so it's refused. Marina water tiles already `continue` above. if (!terrainTile.flat() || terrainTile.constructed_support) return false; } // Marina: must straddle the shoreline — rejected with no water (not coastal) or all // water (fully submerged 3×3 = 9 tiles); a valid marina has 1–8 water cells. if (code == 248 && (waterTiles == 0 || waterTiles == 9)) return false; // Per-tile XBIT utility stamp: modern cities receive conductive|powered-optimistic (0xC0), // while imported-link cities also retain their hidden water conduit bit. // The two non-conductive exceptions are code 13 (post-meltdown radioactivity) and 213 (0xD5). // Low result/terrain bits are preserved. WITHOUT the power stamp the special- // zone structures (airport/seaport) and every civic Building-tool plop were left non-conductive, so the // power flood (sim/power.hpp, gated on XBIT 0x80) skipped them entirely — the user's "ports/airports // don't transmit electricity" bug. (Military zone 7 clears these high bits afterwards in the caller.) const bool classicWaterLinks = !c.modernWaterSystem || c.waterDistributionModel == WaterDistributionModel::ClassicLinks; const std::uint8_t hiBits = (code == 213 || code == 13) ? (classicWaterLinks ? 0x20 : 0x00) : (classicWaterLinks ? 0xE0 : 0xC0); for (int r = row; r <= row + ext; ++r) // stamp the footprint for (int cc = col; cc <= col + ext; ++cc) { c.xbld.cells[c.index(r, cc)] = static_cast(code); c.xzon.cells[c.index(r, cc)] &= 0x0F; // clear corner bits first const std::size_t index = c.index(r, cc); c.legacyWaterLinks.cells[index] = classicWaterLinks ? 1 : 0; auto& b = c.xbit.cells[index]; b = static_cast((b & 0x1F) | hiBits); // conductive|powered (or 0x20 only) if (c.waterMains.cells[index]) b |= xbit::kPipe; } if (size == 1) { c.xzon.cells[c.index(row, col)] |= 0xF0; } else { // the four corners (TL,BL,BR,TR) const int rb = 4 * (rotation & 3); auto& z = c.xzon.cells; z[c.index(row, col)] = static_cast(kCornerBits[rb + 0] | (z[c.index(row, col)] & 0xF)); z[c.index(row + ext, col)] = static_cast(kCornerBits[rb + 1] | (z[c.index(row + ext, col)] & 0xF)); z[c.index(row + ext, col + ext)] = static_cast(kCornerBits[rb + 2] | (z[c.index(row + ext, col + ext)] & 0xF)); z[c.index(row, col + ext)] = static_cast(kCornerBits[rb + 3] | (z[c.index(row, col + ext)] & 0xF)); } return true; } } // namespace sc2k namespace sc2k { bool special_zone_place_1x1(City& c, int row, int col, int code, int zoneNibble, int rotation, const terrain::Model* terrainModel) { if (c.xbld.cells[c.index(row, col)] >= 0x0D) return false; // only over low terrain if (!construct_building(c, row, col, code, 1, rotation, terrainModel)) return false; auto& z = c.xzon.cells[c.index(row, col)]; z = static_cast((z & 0xF0) | (zoneNibble & 0xF)); // set the zone nibble if (zoneNibble == 7) c.xbit.cells[c.index(row, col)] &= 0x0F; return true; } } // namespace sc2k namespace sc2k { void bulldoze_building(City& c, int row, int col) { const int code = c.xbld.cells[c.index(row, col)]; if (code < 198) return; auto clear = [&](int r, int cc) { c.xbld.cells[c.index(r, cc)] = 0; c.xbit.cells[c.index(r, cc)] &= 0x3F; c.xzon.cells[c.index(r, cc)] &= 0x0F; }; if (code >= 219 && code <= 234) { clear(row, col); // a 1×1 special building } else { const int r0 = row & ~1, c0 = col & ~1; // the even-aligned 2×2 clear(r0, c0); clear(r0, c0 + 1); clear(r0 + 1, c0); clear(r0 + 1, c0 + 1); } } } // namespace sc2k namespace sc2k { bool special_zone_place_2x2(City& c, int row, int col, int code, int zoneNibble, int rotation, const terrain::Model* terrainModel) { const int r0 = row & ~1, c0 = col & ~1; const int tile[4][2] = {{r0, c0}, {r0 + 1, c0}, {r0, c0 + 1}, {r0 + 1, c0 + 1}}; for (auto& t : tile) { const int xb = c.xbld.cells[c.index(t[0], t[1])]; if (xb == 221 || xb == 222 || xb == 224) return false; // power lines block the build if ((c.xzon.cells[c.index(t[0], t[1])] & 0xF) != zoneNibble) return false; // all the same zone } for (auto& t : tile) if (c.xbld.cells[c.index(t[0], t[1])] >= 0x0D) bulldoze_building(c, t[0], t[1]); construct_building(c, r0, c0, code, 2, rotation, terrainModel); // (no-ops if a tile stayed occupied) for (auto& t : tile) { auto& z = c.xzon.cells[c.index(t[0], t[1])]; z = static_cast((z & 0xF0) | (zoneNibble & 0xF)); if (zoneNibble == 7) c.xbit.cells[c.index(t[0], t[1])] &= 0x0F; } return true; } } // namespace sc2k namespace sc2k { bool special_zone_place_pier(City& c, int row, int col, int zoneNibble, int rotation, const terrain::Model* terrainModel) { int dir = -1; for (int i = 0; i < 4; ++i) { // find a water-adjacent direction const int r = row + kSpecialDirRow[i], cc = col + kSpecialDirCol[i]; if (c.on_map(r, cc) && (c.xbit.cells[c.index(r, cc)] & 0x04)) { dir = i; break; } } if (dir < 0) return false; const int dr = kSpecialDirRow[dir], dc = kSpecialDirCol[dir]; if (dc != 0 && (row & 1)) return false; // parity alignment if (dr != 0 && (col & 1)) return false; int r = row, cc = col; // the five tiles ahead must be clear water for (int step = 0; step < 5; ++step) { r += dr; cc += dc; if (!c.on_map(r, cc)) return false; if (!(c.xbit.cells[c.index(r, cc)] & 0x04)) return false; if (c.xbld.cells[c.index(r, cc)]) return false; } const terrain::Query terrainQuery(c, terrainModel); if (terrainQuery.legacy_water_surface_level({r, cc}) < terrainQuery.base_level({r, cc}) + 2) return false; if (c.xbld.cells[c.index(row, col)] >= 0x0D) bulldoze_building(c, row, col); construct_building(c, row, col, 224, 1, rotation, terrainModel); // the 1×1 land anchor auto& z0 = c.xzon.cells[c.index(row, col)]; z0 = static_cast((z0 & 0xF0) | (zoneNibble & 0xF)); if (zoneNibble == 7) c.xbit.cells[c.index(row, col)] &= 0x0F; const bool flag = (dr == 0) ? (rotation & 1) : !(rotation & 1); // XBIT 0x02 parity int pr = row, pc = col; for (int k = 0; k < 4; ++k) { // four extension tiles (code 223) pr += dr; pc += dc; c.xbld.cells[c.index(pr, pc)] = 223; c.xzon.cells[c.index(pr, pc)] |= 0xF0; if (flag) c.xbit.cells[c.index(pr, pc)] |= 0x02; } return true; } } // namespace sc2k namespace sc2k { bool special_zone_place_powerline(City& c, int row, int col, int zoneNibble, int rotation, int powerlineParity) { int drow = 0, dcol = 0; // choose the run direction if (powerlineParity & 1) { if ((col & 1) == 0) { if ((row & 1) == 0) return false; dcol = 1; } else drow = 1; } else { if (row & 1) dcol = 1; else { if ((col & 1) == 0) return false; drow = 1; } } int r = row, cc = col, count = 0; // need five same-zone tiles ahead while (c.on_map(r, cc)) { if ((c.xzon.cells[c.index(r, cc)] & 0xF) != zoneNibble) return false; const int xb = c.xbld.cells[c.index(r, cc)]; if (xb == 221 || xb == 222) --count; // existing lines don't count r += drow; cc += dcol; ++count; if (count >= 5) break; } if (count < 5) return false; const bool flipped = (drow != 0) ? !(rotation & 1) : (rotation & 1); // orientation flag int pr = row, pc = col, placed = 0; while (c.on_map(pr, pc)) { const int xb = c.xbld.cells[c.index(pr, pc)]; if (xb == 221 || xb == 222) { --placed; if (xb == 221 && ((c.xbit.cells[c.index(pr, pc)] & 2) == 2) != flipped) { // re-lay flipped → 222 c.xbld.cells[c.index(pr, pc)] = 222; c.xzon.cells[c.index(pr, pc)] |= 0xF0; if (zoneNibble != 7) c.xbit.cells[c.index(pr, pc)] |= 0xC0; c.xbit.cells[c.index(pr, pc)] &= 0xFD; } } else { if (xb >= 0x0D) bulldoze_building(c, pr, pc); c.xbld.cells[c.index(pr, pc)] = 221; // a new power line c.xzon.cells[c.index(pr, pc)] |= 0xF0; if (zoneNibble != 7) c.xbit.cells[c.index(pr, pc)] |= 0xC0; if (flipped) c.xbit.cells[c.index(pr, pc)] |= 0x02; } pr += drow; pc += dcol; if (++placed >= 5) return true; } return true; } } // namespace sc2k namespace sc2k { bool special_zone_place_3x3(City& c, int row, int col, int code, int zoneNibble, int rotation, int& subwayCount) { if (row >= 1 && (c.xzon.cells[c.index(row - 1, col)] & 0xF) == zoneNibble) --row; // slide to top-left if (row >= 1 && (c.xzon.cells[c.index(row - 1, col)] & 0xF) == zoneNibble) --row; if (col >= 1 && (c.xzon.cells[c.index(row, col - 1)] & 0xF) == zoneNibble) --col; if (col >= 1 && (c.xzon.cells[c.index(row, col - 1)] & 0xF) == zoneNibble) --col; for (int j = 0; j < 3; ++j) for (int k = 0; k < 3; ++k) { c.xbld.cells[c.index(row + j, col + k)] = static_cast(code); set_underground(c, row + j, col + k, 34, subwayCount); } const int rb = 4 * (rotation & 3); // the four corners (TL,BL,BR,TR) auto& z = c.xzon.cells; z[c.index(row, col)] = static_cast(kCornerBits[rb + 0] | (z[c.index(row, col)] & 0xF)); z[c.index(row + 2, col)] = static_cast(kCornerBits[rb + 1] | (z[c.index(row + 2, col)] & 0xF)); z[c.index(row + 2, col + 2)] = static_cast(kCornerBits[rb + 2] | (z[c.index(row + 2, col + 2)] & 0xF)); z[c.index(row, col + 2)] = static_cast(kCornerBits[rb + 3] | (z[c.index(row, col + 2)] & 0xF)); return true; } } // namespace sc2k