// Numeric verification of the bridge system — a faithful port of the original's span builder // (placement) and shared demolisher (removal). // // A surface network laid onto shore water auto-spans a bridge: an entrance RAMP (a raised terrain slope = // the "angle", with a tile of terrain CREATED underneath it by clearing the water bit), interior DECK // spans, and an exit RAMP. Removing the bridge (bulldoze any deck) must restore BOTH ramps to water and // lower their altitude — the "remove the terrain under the angle" rule the user called out. // // Synthetic cities, no assets. We set the water tiles' coast codes directly (the live game derives them // from the water autotiler) so the launch-direction gate + span walk are exercised deterministically. #include "sc2k/ui/tools.hpp" #include using namespace sc2k; namespace br = sc2k::ui::detail; #include "test_harness.hpp" static bool road_family(int x) { return x >= 0x1D && x < 0x2C; } static bool rail_family(int x) { return (x >= 0x2C && x < 0x3F) || (x >= 0x45 && x < 0x49); } static bool power_family(int x) { return x >= 0x0E && x < 0x1D; } // Anchor coast nibble whose allowedDir permits launching in `dir` (kBrAllowedDir: 4→W,1→S,2→E,3→N). static const int kAnchorNib[4] = {4, 1, 2, 3}; // Build a river crossed in direction `dir` for W water tiles from (R,C), the land everywhere else at alt L. // The water is widened ±1 perpendicular so it reads as a real channel; the anchor gets the launch coast code. static void make_channel(City& c, int R, int C, int W, int dir, int L) { // A REAL depression: land plateau at L+2, water ground at L (one below sea), sea level L+1. This makes the // bridge-removal re-shore (terrain_autotile_cell) correctly restore WATER at the ramps — `sea(L+1) > ground(L)` // — the way it does in a real city. (The earlier degenerate channel put water at land height / sea 0, so the // faithful re-shore dried the ramps; that masked the real-city behaviour the user cares about.) Water ground // stays L so bridge_lay still raises the ramps to L+1, preserving the lay-time altitude assertions below. c.set_misc_be32(sc2k::misc::kWaterLevel, L + 1); // global sea level for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) { c.xter.at(r, col) = 0; c.xbld.at(r, col) = 0; c.xzon.at(r, col) = 0; c.xbit.at(r, col) = 0; c.altm.at(r, col) = std::uint16_t(L + 2); // dry plateau above sea } const int dR = br::kBrDRow[dir], dC = br::kBrDCol[dir]; const int pR = (dir & 1) ? 0 : 1, pC = (dir & 1) ? 1 : 0; // perpendicular axis for (int i = 0; i < W; ++i) for (int p = -1; p <= 1; ++p) { const int r = R + i * dR + p * pR, col = C + i * dC + p * pC; if (r < 0 || r >= City::kDim || col < 0 || col >= City::kDim) continue; c.xbit.at(r, col) = 0x04; c.altm.at(r, col) = std::uint16_t(L | ((L + 2) << 5)); // ground L (submerged 1), water-level field L+2 c.xter.at(r, col) = std::uint8_t((i == 0 && p == 0) ? (0x20 + kAnchorNib[dir]) : 0x12); } } int main() { // ---- Style masks: road span-gated {2,3,4}; rail always {1}; power always {0} (verified filters) ---- CHECK(br::bridge_style_mask(ui::Tool::Road, 4) == 0x04); // span 1-4 → suspension only CHECK(br::bridge_style_mask(ui::Tool::Road, 6) == 0x0C); // span 5-6 → +towers CHECK(br::bridge_style_mask(ui::Tool::Road, 8) == 0x1C); // span 7-11 → +reinforced CHECK(br::bridge_style_mask(ui::Tool::Road, 12) == 0x14); // span ≥12 → suspension + reinforced (no towers) CHECK(br::bridge_style_mask(ui::Tool::Rail, 3) == 0x02); // rail = raising, any span CHECK(br::bridge_style_mask(ui::Tool::Rail, 9) == 0x02); CHECK(br::bridge_style_mask(ui::Tool::PowerLine, 3) == 0x01); // power = causeway, any span CHECK(br::bridge_cost(0) == 10 && br::bridge_cost(2) == 25 && br::bridge_cost(3) == 50 && br::bridge_cost(4) == 75); // ---- Direction finder + probe ---- { City c; make_channel(c, 40, 30, 4, /*E*/ 2, 5); CHECK(br::bridge_find_dir(c, 40, 30, /*compass W*/ 0) == 2); // W not allowed → falls to E const auto sp = br::bridge_probe(c, 40, 30, 2); CHECK(sp.status == br::BridgeStatus::Ok); CHECK(sp.span == 4); // 4 water tiles to the far shore // Non-shore anchors: land (NotWater) and a deep-water nibble-0 coast (BadShore). CHECK(br::bridge_probe(c, 40, 20, 2).status == br::BridgeStatus::NotWater); // dry land c.xter.at(40, 30) = 0x20; // nibble 0 → allowedDir 0 CHECK(br::bridge_probe(c, 40, 30, 2).status == br::BridgeStatus::BadShore); } // Off-map: a channel that runs to the edge with no far shore. { City c; make_channel(c, 40, 124, 4, 2, 5); // cols 124..127 water, no land past the edge for (int col = 124; col < 128; ++col) { c.xbit.at(40, col) = 0x04; c.xter.at(40, col) = std::uint8_t(col == 124 ? 0x22 : 0x12); } CHECK(br::bridge_probe(c, 40, 124, 2).status == br::BridgeStatus::OffMap); } // Blocked: a built tile sitting on the water before land. { City c; make_channel(c, 40, 30, 5, 2, 5); c.xbld.at(40, 32) = 0x1D; // a stray road tile mid-water CHECK(br::bridge_probe(c, 40, 30, 2).status == br::BridgeStatus::Blocked); } // ---- ROAD suspension, span 4 (E-W): ramp / deck / ramp layout + terrain-underneath + faithful removal ---- { City c; make_channel(c, 40, 30, 4, 2, 5); const auto sp = br::bridge_probe(c, 40, 30, 2); CHECK(sp.status == br::BridgeStatus::Ok && sp.span == 4); br::bridge_lay(c, sp, /*suspension*/ 2); // entrance ramp at (40,30): slope XTER 4, altitude +1, water bit cleared (terrain underneath), road on it CHECK(c.xter.at(40, 30) == 4); CHECK((c.altm.at(40, 30) & 0x1F) == 6); // L+1 CHECK((c.xbit.at(40, 30) & 0x04) == 0); // TERRAIN created under the angle CHECK(road_family(c.xbld.at(40, 30))); // decks at (40,31)(40,32): suspension code 87, water bit KEPT, no N-S marker (E-W bridge) CHECK(c.xbld.at(40, 31) == 87 && c.xbld.at(40, 32) == 87); CHECK((c.xbit.at(40, 31) & 0x04) != 0 && (c.xbit.at(40, 31) & 0x02) == 0); // exit ramp at (40,33): opposite slope XTER 2, +1, water cleared, road CHECK(c.xter.at(40, 33) == 2); CHECK((c.altm.at(40, 33) & 0x1F) == 6); CHECK((c.xbit.at(40, 33) & 0x04) == 0); CHECK(road_family(c.xbld.at(40, 33))); // Remove by bulldozing a DECK tile → whole span razed, both ramps restored to WATER + altitude lowered. const bool razed = br::bridge_remove(c, 40, 31); CHECK(razed); for (int col = 30; col <= 33; ++col) { CHECK(c.xbld.at(40, col) == 0); // every span tile cleared CHECK((c.xbit.at(40, col) & 0x04) != 0); // back to water } CHECK((c.altm.at(40, 30) & 0x1F) == 5 && (c.altm.at(40, 33) & 0x1F) == 5); // ramps lowered back to L CHECK((c.xbit.at(40, 30) & 0x02) == 0); // raised marker cleared } // ---- RAIL raising bridge, span 3 (E-W): decks 90/91, ramps carry rail ---- { City c; make_channel(c, 50, 40, 3, 2, 4); const auto sp = br::bridge_probe(c, 50, 40, 2); CHECK(sp.status == br::BridgeStatus::Ok && sp.span == 3); br::bridge_lay(c, sp, /*raising*/ 1); CHECK(rail_family(c.xbld.at(50, 40))); // entrance ramp = rail-on-slope CHECK(c.xbld.at(50, 41) == 90 || c.xbld.at(50, 41) == 91); // single deck = raising span CHECK(rail_family(c.xbld.at(50, 42))); // exit ramp CHECK((c.xbit.at(50, 40) & 0x04) == 0 && (c.xbit.at(50, 42) & 0x04) == 0); CHECK(br::bridge_remove(c, 50, 41)); for (int col = 40; col <= 42; ++col) { CHECK(c.xbld.at(50, col) == 0); CHECK((c.xbit.at(50, col) & 0x04) != 0); } } // ---- POWER causeway, span 3 (E-W): decks 92 + conductive bit, ramps carry power ---- { City c; make_channel(c, 60, 50, 3, 2, 6); const auto sp = br::bridge_probe(c, 60, 50, 2); CHECK(sp.status == br::BridgeStatus::Ok && sp.span == 3); br::bridge_lay(c, sp, /*causeway*/ 0); CHECK(power_family(c.xbld.at(60, 50))); // entrance ramp = power-on-slope CHECK(c.xbld.at(60, 51) == 92); // causeway deck CHECK((c.xbit.at(60, 51) & 0x80) != 0); // causeway conductive bit CHECK((c.xbit.at(60, 51) & 0x04) != 0); // deck keeps water CHECK(power_family(c.xbld.at(60, 52))); // exit ramp CHECK(br::bridge_remove(c, 60, 51)); for (int col = 50; col <= 52; ++col) { CHECK(c.xbld.at(60, col) == 0); CHECK((c.xbit.at(60, col) & 0x04) != 0); // razed → open water // CRITICAL: the razed deck/ramp must DROP its conductive+powered bits, else the now-open-water tile // keeps carrying the power flood and a severed island never loses electricity (user-reported on // CapeQuest). The original's deck branch omits this `&= 0x3D` that its sibling branches all apply. CHECK((c.xbit.at(60, col) & 0xC0) == 0); // not conductive, not powered } // The restored RAMPS (cols 50/52) must NOT keep their bridge land-slope XTER: re-shoring a leftover slope // emits the 0x3E WATERFALL code, which renders as a raised block jutting from the water (user-reported // "raised water blocks" at deleted bridge ends). restore_ramp clears XTER before re-shore to prevent it. CHECK(c.xter.at(60, 50) != 0x3e && c.xter.at(60, 52) != 0x3e); } // ---- N-S bridge (dir S), span 4, road: decks carry the XBIT 0x02 axis marker; removal walks ROWS ---- { City c; make_channel(c, 30, 70, 4, /*S*/ 1, 5); const auto sp = br::bridge_probe(c, 30, 70, 1); CHECK(sp.status == br::BridgeStatus::Ok && sp.span == 4); br::bridge_lay(c, sp, 2); CHECK((c.xbit.at(31, 70) & 0x02) != 0 && (c.xbit.at(32, 70) & 0x02) != 0); // N-S decks marked CHECK(c.xbld.at(31, 70) == 87 && c.xbld.at(32, 70) == 87); CHECK(road_family(c.xbld.at(30, 70)) && road_family(c.xbld.at(33, 70))); // ramps CHECK(br::bridge_remove(c, 31, 70)); // click a deck → walks rows (XBIT 0x02) for (int r = 30; r <= 33; ++r) { CHECK(c.xbld.at(r, 70) == 0); CHECK((c.xbit.at(r, 70) & 0x04) != 0); } CHECK((c.altm.at(30, 70) & 0x1F) == 5 && (c.altm.at(33, 70) & 0x1F) == 5); } // ---- W=2: the two angles meet, ZERO deck tiles (faithful "two angles block the gap") ---- { City c; make_channel(c, 40, 30, 2, 2, 5); const auto sp = br::bridge_probe(c, 40, 30, 2); CHECK(sp.status == br::BridgeStatus::Ok && sp.span == 2); br::bridge_lay(c, sp, 2); CHECK(road_family(c.xbld.at(40, 30)) && road_family(c.xbld.at(40, 31))); // both water tiles → ramps int decks = 0; for (int col = 30; col <= 31; ++col) if (c.xbld.at(40, col) >= 81 && c.xbld.at(40, col) < 93) ++decks; CHECK(decks == 0); // no deck spans for a 2-wide gap CHECK((c.xbit.at(40, 30) & 0x04) == 0 && (c.xbit.at(40, 31) & 0x04) == 0); // terrain under both angles } // ---- W=1: a 1-tile gap yields no deck/span structure ("no bridge"); span probes as 1 ---- { City c; make_channel(c, 40, 30, 1, 2, 5); const auto sp = br::bridge_probe(c, 40, 30, 2); CHECK(sp.status == br::BridgeStatus::Ok && sp.span == 1); br::bridge_lay(c, sp, 2); int decks = 0; for (int col = 29; col <= 31; ++col) if (c.xbld.at(40, col) >= 81 && c.xbld.at(40, col) < 93) ++decks; CHECK(decks == 0); // no bridge deck appears for a 1-wide gap CHECK((c.xbit.at(40, 30) & 0x04) == 0); // the single water tile is filled to a ramp } // ---- Issue 1 (2026-07-01): a TERRAIN EDIT over a bridge deck destroys the WHOLE span, not just that tile. // The original re-slope razes any XBLD>=0xD via the shared demolisher (the SAME destroyer the // bulldozer uses), so a partially-affected bridge is fully removed. Libreopolis now routes terrain demolition // through the shared sim::demolish_at (which dispatches to bridge_remove) instead of a raw component flood. { City c; c.set_funds(100000); make_channel(c, 70, 30, 4, 2, 5); const auto sp = br::bridge_probe(c, 70, 30, 2); CHECK(sp.status == br::BridgeStatus::Ok && sp.span == 4); br::bridge_lay(c, sp, /*suspension*/ 2); int decks = 0; for (int col = 31; col <= 32; ++col) if (c.xbld.at(70, col) >= 81 && c.xbld.at(70, col) < 93) ++decks; CHECK(decks == 2); // span built // Raise the terrain on ONE deck tile → the entire span (both decks + both ramps) is destroyed. sc2k::raise_terrain(c, 70, 31); int left = 0; for (int col = 29; col <= 34; ++col) if (c.xbld.at(70, col) >= 81 && c.xbld.at(70, col) < 93) ++left; CHECK(left == 0); // no orphaned deck fragments — bridge_remove ran CHECK((c.xbit.at(70, 32) & 0x04) != 0); // the other deck's tile restored to water } // ---- Issue (2026-07-01): bulldozing the APPROACH RAMP of a bridge removes the WHOLE span (no floating deck). { City c; c.set_funds(100000); make_channel(c, 80, 30, 4, 2, 5); const auto sp = br::bridge_probe(c, 80, 30, 2); CHECK(sp.status == br::BridgeStatus::Ok && sp.span == 4); br::bridge_lay(c, sp, /*suspension*/ 2); CHECK(road_family(c.xbld.at(80, 30))); // entrance ramp (road-on-slope) CHECK(c.xbld.at(80, 31) == 87 && c.xbld.at(80, 32) == 87); // two decks // Bulldoze the ENTRANCE RAMP tile → the whole bridge (decks + far ramp) is razed, all back to water. const auto r = ui::apply_tool(c, ui::Tool::Bulldoze, 80, 30); CHECK(r.ok); for (int col = 30; col <= 33; ++col) CHECK(c.xbld.at(80, col) == 0); // nothing left standing CHECK((c.xbit.at(80, 31) & 0x04) != 0 && (c.xbit.at(80, 32) & 0x04) != 0); // decks back to water } // ---- Issue (2026-07-01): raising terrain NEXT TO a bridge must not PARTIALLY destroy it. A deck sits over // water (altitude below sea), so a nearby edit that merely re-slopes it used to trip the submerge-clear and // wipe the deck's XBLD — orphaning the rest of the span (user-reported "the ramp/deck vanished but the bridge // survived"). The submerge-clear now only clears rubble/trees (XBLD<0xD), never structures. So a nearby edit // leaves the bridge whole; only an edit ON a deck/ramp (in the demolish footprint) removes the span. { City c; c.set_funds(1000000); make_channel(c, 40, 30, 4, 2, 5); for (int r = 38; r <= 42; ++r) { c.altm.at(r, 35) = 7; c.altm.at(r, 36) = 8; c.altm.at(r, 37) = 9; c.altm.at(r, 38) = 10; } for (int pass = 0; pass < 3; ++pass) for (int r = 37; r <= 43; ++r) for (int col = 33; col <= 40; ++col) sc2k::terrain_autotile_cell(c, r, col, std::clamp(c.water_level(), 0, 31)); const auto sp = br::bridge_probe(c, 40, 30, 2); br::bridge_lay(c, sp, /*suspension*/ 2); CHECK(c.xbld.at(40, 31) == 87 && c.xbld.at(40, 32) == 87); // two decks laid const int exitRampXter = c.xter.at(40, 33); // the exit ramp is a cardinal slope CHECK(exitRampXter >= 1 && exitRampXter <= 4); sc2k::raise_terrain(c, 38, 35); // raise a terrace tile 2 tiles off the exit ramp // The bridge must stay WHOLE — no orphaned deck, and the ramp SLOPE must not be flattened by a re-slope // reaching a 2nd ring the demolish never touched (the CapeQuest "slope went flat, deck in thin air" bug). CHECK(c.xbld.at(40, 31) == 87 && c.xbld.at(40, 32) == 87); // both decks survive CHECK(road_family(c.xbld.at(40, 30)) && road_family(c.xbld.at(40, 33))); // both ramps survive CHECK(c.xter.at(40, 33) >= 1 && c.xter.at(40, 33) <= 4); // exit ramp slope PRESERVED (not flattened) } if (failures == 0) std::printf("test_bridge: all checks passed\n"); else std::printf("test_bridge: %d FAILED\n", failures); return failures ? 1 : 0; }