// XTHG "Things" engine KAT (sim/things.hpp): the universal sub-pixel mover, slot alloc/ // despawn, and the Tornado (spawn + handler). Verified numerically — the funnel // moves across tiles owning exactly one occupancy cell, demolishes what it crosses, and dissipates. #include "sc2k/sim/things.hpp" #include "sc2k/render/rotate.hpp" #include "sc2k/render/asset_loader.hpp" // monster render check (skipped without the art) #include "sc2k/render/city_renderer.hpp" #include "sc2k/render/iso_projection.hpp" #include "sc2k/city.hpp" #include "sc2k/rng.hpp" #include #include #include using namespace sc2k; using namespace sc2k::sim; #include "test_harness.hpp" #include "test_paths.hpp" int main() { // ---- slot allocator: 1..39, slot 0 reserved, 0 when full ---- { City c; CHECK(thing_alloc(c) == 1); // first free thing_field(c, 1, thg::Type) = thg::kTornado; CHECK(thing_alloc(c) == 2); // 1 taken → next free for (int i = 1; i < thg::kCount; ++i) thing_field(c, i, thg::Type) = 1; CHECK(thing_alloc(c) == 0); // full → 0 } // ---- the mover: slides east, owning one cell, restoring its trail ---- { City c; const int idx = 1; thing_field(c, idx, thg::Type) = thg::kTornado; thing_field(c, idx, thg::X) = 40; thing_field(c, idx, thg::Y) = 40; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::SavedTile) = 0; c.xtxt.at(40, 40) = std::uint8_t(idx - 55); // it owns its cell (== idx+0xC9) CHECK(c.xtxt.at(40, 40) == 0xCA); // 1 + 0xC9 // dir 2 = east (dx=+1,dy=0), speed 8: every 2 ticks crosses one tile boundary. for (int t = 0; t < 8; ++t) CHECK(thing_move(c, thg::kTornado, idx, 2)); CHECK(thing_field(c, idx, thg::X) == 44); // advanced 4 tiles east CHECK(thing_field(c, idx, thg::Y) == 40); CHECK(c.xtxt.at(44, 40) == std::uint8_t(idx - 55)); // owns the new cell CHECK(c.xtxt.at(40, 40) == 0); // old cell restored (savedTile was 0) // exactly one occupancy cell across the whole row int owned = 0; for (int x = 40; x <= 44; ++x) if (c.xtxt.at(x, 40) >= 0xC9) ++owned; CHECK(owned == 1); } // ---- despawn: clears type + occupancy; running off the map auto-despawns ---- { City c; const int idx = 1; thing_field(c, idx, thg::Type) = thg::kTornado; thing_field(c, idx, thg::X) = 126; thing_field(c, idx, thg::Y) = 40; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; c.xtxt.at(126, 40) = std::uint8_t(idx - 55); bool alive = true; for (int t = 0; t < 30 && alive; ++t) alive = thing_move(c, thg::kTornado, idx, 2); // east → off edge CHECK(!alive); // despawned at the boundary CHECK(thing_field(c, idx, thg::Type) == 0); } // ---- Tornado disaster: spawns once, wanders demolishing, and eventually dissipates ---- { City c; MsRand rng(0xBEEF); DisasterSim d; for (int r = 20; r < 110; ++r) for (int cc = 20; cc < 110; ++cc) c.xbld.at(r, cc) = 0x80; // dense city const int before = [&] { int n=0; for (int i=0;i=0x70) ++n; return n; }(); const int idx = spawn_tornado(c, rng, 64, 64); CHECK(idx >= 1); CHECK(spawn_tornado(c, rng, 30, 30) == 0); // only one tornado at a time CHECK(thing_field(c, idx, thg::Type) == thg::kTornado); int steps = 0; for (; steps < 100000; ++steps) if (things_tick(c, rng, d) == 0) break; // run to dissipation CHECK(steps < 100000); // it ended (left the map or fizzled) const int after = [&] { int n=0; for (int i=0;i=0x70) ++n; return n; }(); CHECK(after < before); // it carved a path of destruction // no orphan occupancy cells left behind int owners = 0; for (int i = 0; i < City::kDim; ++i) for (int j = 0; j < City::kDim; ++j) if (c.xtxt.at(i, j) >= 0xC9 && c.xtxt.at(i, j) < 0xF1) ++owners; CHECK(owners == 0); std::printf(" tornado destroyed %d buildings over %d ticks, then dissipated\n", before - after, steps); } // ---- Monster: spawns once, rises → rampages destroying the city, and eventually leaves ---- { City c; MsRand rng(0x4242); DisasterSim d; for (int r = 16; r < 112; ++r) for (int cc = 16; cc < 112; ++cc) c.xbld.at(r, cc) = std::uint8_t(0x70 + ((r + cc) & 0x1F)); const int before = [&] { int n=0; for (int i=0;i=0x70) ++n; return n; }(); const int idx = spawn_monster(c, rng, 64, 64); CHECK(idx >= 1); CHECK(spawn_monster(c, rng, 30, 30) == 0); // one monster at a time CHECK(thing_field(c, idx, thg::Type) == 5); CHECK(thing_field(c, idx, thg::State) == 0); // starts in "rise" int steps = 0; bool rampaged = false; for (; steps < 200000; ++steps) { const bool fx = disaster_tick(c, d, rng); // both ticks each frame, exactly as the app does const int things = things_tick(c, rng, d); // (so the monster's fires actually spread + burn) if (thing_field(c, idx, thg::State) == 1) rampaged = true; if (things == 0 && !fx) break; } CHECK(rampaged); // it reached the rampage state CHECK(steps < 200000); // it left, and its fires burned out const int after = [&] { int n=0; for (int i=0;i=0x70) ++n; return n; }(); CHECK(after < before); // it trampled buildings int owners = 0; for (int i = 0; i < City::kDim; ++i) for (int j = 0; j < City::kDim; ++j) if (c.xtxt.at(i, j) >= 0xC9 && c.xtxt.at(i, j) < 0xF1) ++owners; CHECK(owners == 0); // no orphan occupancy after it left std::printf(" monster trampled %d buildings over %d ticks, then left\n", before - after, steps); } // ---- Explosion: 2-tick fireball, then despawns and chain-reacts to fire ---- { City c; DisasterSim d; for (int r = 50; r < 70; ++r) for (int cc = 50; cc < 70; ++cc) c.xbld.at(r, cc) = 0x80; // buildings const int idx = spawn_explosion(c, 60, 60, 5, /*subtype=*/1); CHECK(idx >= 1 && thing_field(c, idx, thg::Type) == 6); CHECK(explosion_step(c, d, idx)); // fireball frame 0→1 CHECK(explosion_step(c, d, idx)); // fireball frame 1→2 CHECK(!explosion_step(c, d, idx)); // frame 2 → detonate + despawn CHECK(thing_field(c, idx, thg::Type) == 0); int fires = 0; for (int i = 56; i < 64; ++i) for (int j = 56; j < 64; ++j) if (c.xtxt.at(i, j) == fx::Fire) ++fires; CHECK(fires >= 1); // the chain-reaction lit nearby tiles } // ---- Air Crash (start_air_crash): a PLANE spawns falling (state 7) at a random mid-map // tile (32..95), spirals down 16 frames, then explodes into chain fire — no instant same-spot explosion // (the old port invented one at the first building out from the centre; user: "kept happening in almost // the exact same place, and there was no spiralling airplane"). ---- { City c; DisasterSim d; MsRand rng(0xCA5); for (int r = 20; r < 108; ++r) for (int cc = 20; cc < 108; ++cc) c.xbld.at(r, cc) = 0x80; CHECK(start_air_crash(c, d, rng)); CHECK(d.active && d.type == 0x12); // 0x12 = plane crash (spec §Disaster Type) int plane = 0, booms = 0; for (int i = 1; i < thg::kCount; ++i) { if (thing_field(c, i, thg::Type) == 1 && thing_field(c, i, thg::State) == 7) plane = i; if (thing_field(c, i, thg::Type) == 6) ++booms; } CHECK(plane >= 1 && booms == 0); // a falling plane, not an explosion const int px0 = thing_field(c, plane, thg::X), py0 = thing_field(c, plane, thg::Y); CHECK(px0 >= 32 && px0 <= 95 && py0 >= 32 && py0 <= 95); // LFSRMod64+32 spawn window CHECK(thing_field(c, plane, thg::Frame) == 16); // 16 frames of sky to fall through CHECK(disaster_tick(c, d, rng) && d.active); // keep-alive: the crashing plane holds it open int t = 0; bool exploded = false; for (; t < 60 && !exploded; ++t) { // the app's plane tick drives the spiral ambient_things_tick(c, rng, d); exploded = thing_field(c, plane, thg::Type) == 6; } CHECK(exploded && t >= 16); // it fell the full 16 frames, then detonated std::printf(" air crash: plane spawned (%d,%d), spiralled %d ticks, then exploded\n", px0, py0, t); } // Runtime map extents: crash and incoming-aircraft placement fit Tiny, Large incoming aircraft // can use edge 255, and coordinate 128 is real water/obstacle state rather than "off map". { City tiny(MapSize::Tiny); DisasterSim d; MsRand rng(0xCA5); CHECK(start_air_crash(tiny, d, rng)); CHECK(tiny.on_map(d.epiX, d.epiY)); for (unsigned seed = 1; seed < 100; ++seed) { City candidate(MapSize::Tiny); MsRand incoming(seed); const int idx = spawn_airplane(candidate, incoming, 32, 32); if (idx) { CHECK(candidate.on_map(thing_field(candidate, idx, thg::X), thing_field(candidate, idx, thg::Y))); } } bool sawLargeEdge = false; for (unsigned seed = 1; seed < 200 && !sawLargeEdge; ++seed) { City large(MapSize::Large); MsRand incoming(seed); const int idx = spawn_airplane(large, incoming, 128, 128); if (!idx) continue; const int x = thing_field(large, idx, thg::X), y = thing_field(large, idx, thg::Y); CHECK(large.on_map(x, y)); sawLargeEdge = x == 255 || y == 255; } CHECK(sawLargeEdge); City large(MapSize::Large); const int boat = thing_alloc(large); thing_field(large, boat, thg::Type) = 9; thing_field(large, boat, thg::X) = 127; thing_field(large, boat, thg::Y) = 127; large.xbld.at(128, 127) = 0xDF; CHECK(!sailboat_can_go(large, boat, 1)); // row 128 is on-map and the pier blocks it thing_field(large, boat, thg::X) = 0; thing_field(large, boat, thg::Y) = 0; CHECK(sailboat_can_go(large, boat, 0)); // actual off-map remains passable toward despawn } // ---- Ambient crash into an arcology must NOT leave a permanent explosion (the CENTERVL bug) ---- // A plane that hits a tall arco (XBLD ≥ 0xFB) turns its slot into a type-6 fireball. The disaster Thing // tick (things_tick) only runs WHILE a disaster is active, so the every-frame AMBIENT tick must advance // that orphaned fireball too — otherwise it never despawns and the explosion sprite lingers forever, // blooming a fixed cluster over the city centre as plane after plane crashes. { City c; DisasterSim d; MsRand rng(7); c.xbld.at(64, 64) = 0xFF; // a Braun Llama Dome arcology tile const int idx = thing_alloc(c); thing_field(c, idx, thg::Type) = 1; // a passenger plane sitting... thing_field(c, idx, thg::X) = 64; thing_field(c, idx, thg::Y) = 64; // ...right over the arco bool sawFireball = false; for (int t = 0; t < 20; ++t) { // run the ambient tick with NO disaster active ambient_things_tick(c, rng, d); if (thing_field(c, idx, thg::Type) == 6) sawFireball = true; } CHECK(sawFireball); // it did crash into a fireball int lingering = 0; for (int i = 1; i < thg::kCount; ++i) if (thing_field(c, i, thg::Type) == 6) ++lingering; CHECK(lingering == 0); // ...and every fireball despawned — none left behind } // ---- Under "No Disasters", a crash blast is a COSMETIC fireball only — no spreading fire, as the original ---- { City c; DisasterSim d; d.disastersEnabled = false; // "No Disasters" ON const int idx = spawn_explosion(c, 64, 64, /*state*/ 5, /*subtype=*/1); // a would-be chain-fire blast CHECK(idx >= 1); for (int t = 0; t < 6; ++t) explosion_step(c, d, idx); // run the fireball out CHECK(thing_field(c, idx, thg::Type) == 0); // it still despawned (the fireball is ungated) int fires = 0; for (int i = 60; i < 68; ++i) for (int j = 60; j < 68; ++j) if (c.xtxt.at(i, j) == fx::Fire) ++fires; CHECK(fires == 0); // ...but NO fire spread, and... CHECK(!d.active); // ...no disaster was promoted } // ---- Gradual steering: turn ONE octant toward target, never snap ---- { // thing_steer holds when aligned, else ±1 the short way around the ring. CHECK(thing_steer(0, 0, 0, 5, 0) == 1); // facing N, target due-E(2) → bank to NE(1) CHECK(thing_steer(1, 0, 0, 5, 0) == 2); // NE → E (now aligned) CHECK(thing_steer(2, 0, 0, 5, 0) == 2); // E, already facing the target → HOLD (no jitter) // thing_step_toward_dir ALWAYS turns (even when aligned — the ship reroute sweeps on it). CHECK(thing_step_toward_dir(0, 2) == 1); CHECK(thing_step_toward_dir(2, 2) == 3); // aligned but still steps CHECK(thing_step_toward_dir(0, 6) == 7); // shortest way: N→W via NW(7), not the long way // The crux of the "spinning" fix: the OLD port snapped dir = thing_bearing each tick, so a target // that's just *behind* flips the heading 180° in one tick. thing_steer caps any turn at one octant. CHECK(thing_bearing(64, 64, 63, 64) == 6); // snap would jump E(2)→W(6): |Δ|=4 const int sd = thing_steer(2, 64, 64, 63, 64); // …the steer turns at most one CHECK((std::abs(sd - 2) > 4 ? 8 - std::abs(sd - 2) : std::abs(sd - 2)) <= 1); } // ---- Helicopter patrol banks GRADUALLY (≤1 octant/tick) and still homes — no orbit/spin ---- { City c; MsRand rng(0x1234); const int idx = thing_alloc(c); thing_field(c, idx, thg::Type) = 2; // helicopter thing_field(c, idx, thg::State) = 2; // straight into patrol (skip the rotor spin-up) thing_field(c, idx, thg::Dir) = 0; // facing N thing_field(c, idx, thg::X) = 64; thing_field(c, idx, thg::Y) = 64; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::DestX) = 100; thing_field(c, idx, thg::DestY) = 64; c.xtxt.at(64, 64) = std::uint8_t(idx - 55); int prevDir = thing_field(c, idx, thg::Dir), maxTurn = 0; bool arrived = false; for (int t = 0; t < 300 && thing_field(c, idx, thg::Type) == 2; ++t) { const int dx0 = thing_field(c, idx, thg::DestX), dy0 = thing_field(c, idx, thg::DestY); helicopter_step(c, rng, idx); if (thing_field(c, idx, thg::Type) != 2) break; const int d = thing_field(c, idx, thg::Dir); int turn = std::abs(d - prevDir); if (turn > 4) turn = 8 - turn; // shortest ring distance if (turn > maxTurn) maxTurn = turn; prevDir = d; if (std::abs(int(thing_field(c, idx, thg::X)) - dx0) + std::abs(int(thing_field(c, idx, thg::Y)) - dy0) < 2) arrived = true; } CHECK(maxTurn <= 1); // never more than one octant per tick — the anti-spin invariant CHECK(arrived); // …and it actually reaches its destination (homes, doesn't orbit) std::printf(" heli patrol: maxTurn/tick=%d (<=1 = no spin), reached dest=%d\n", maxTurn, arrived); } // ---- Sub-tile glide: a moving Thing visits intermediate sub-pixel positions (so the renderer slides // it between tiles instead of teleporting a whole tile/tick — the "too fast / spazz" fix) ---- { City c; const int idx = thing_alloc(c); thing_field(c, idx, thg::Type) = 2; // heli (step 8 on the 16-grid) thing_field(c, idx, thg::X) = 10; thing_field(c, idx, thg::Y) = 64; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; c.xtxt.at(10, 64) = std::uint8_t(idx - 55); bool sawHalf = false; for (int t = 0; t < 12; ++t) { thing_move(c, 2, idx, 2); if (thing_field(c, idx, thg::Xsub) == 16) sawHalf = true; } CHECK(sawHalf); // the half-tile waypoint (xsub 16) → renders at +8px, mid-tile CHECK(thing_field(c, idx, thg::X) == 16); // 12 ticks × 8/16 = 6 tiles east (10→16) // The cargo ship runs its OWN 12-grid mover: step 4 cardinal ⇒ xsub cycles 8→12→4. City s; const int j = thing_alloc(s); thing_field(s, j, thg::Type) = 3; thing_field(s, j, thg::X) = 10; thing_field(s, j, thg::Y) = 64; thing_field(s, j, thg::Xsub) = 8; thing_field(s, j, thg::Ysub) = 8; s.xtxt.at(10, 64) = std::uint8_t(j - 55); bool saw12 = false, saw4 = false; for (int t = 0; t < 9; ++t) { ship_move(s, j, 2); const int xs = thing_field(s, j, thg::Xsub); if (xs == 12) saw12 = true; if (xs == 4) saw4 = true; } CHECK(saw12 && saw4); // three waypoints per tile (8,12,4) → smooth glide CHECK(thing_field(s, j, thg::X) - 10 == 3); // 9 ticks × 4/12 = 3 tiles east } // ---- Rotation: a Thing's heading + position rotate TOGETHER, so the sprite faces its motion (not // "backwards"). rotate_once turns the heading +2 octants/quarter for the directional vehicles ---- { City c; const int idx = thing_alloc(c); thing_field(c, idx, thg::Type) = 1; // airplane facing E(2) thing_field(c, idx, thg::Dir) = 2; thing_field(c, idx, thg::X) = 10; thing_field(c, idx, thg::Y) = 20; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; const City v = render::rotate_once(c); CHECK(v.xthg[std::size_t(idx) * 12 + 3] == 107); // pos rotated CW: (10,20)→(127−20,10) CHECK(v.xthg[std::size_t(idx) * 12 + 4] == 10); CHECK(v.xthg[std::size_t(idx) * 12 + 1] == 4); // heading E(2) → S(4): +2 octants CHECK(v.xthg[std::size_t(idx) * 12 + 6] == 8); // rest sub-offset (8,8) is a fixed point CHECK(v.xthg[std::size_t(idx) * 12 + 7] == 8); const City v4 = render::rotate_city(c, 4); // four quarters = identity CHECK(v4.xthg[std::size_t(idx) * 12 + 1] == 2); CHECK(v4.xthg[std::size_t(idx) * 12 + 3] == 10 && v4.xthg[std::size_t(idx) * 12 + 4] == 20); // The load-bearing invariant: for EVERY heading, the rotated heading's tile-vector equals the // rotated motion vector of the old heading — i.e. the sprite always points the way it travels. for (int d = 0; d < 8; ++d) { City t; const int k = thing_alloc(t); thing_field(t, k, thg::Type) = 2; thing_field(t, k, thg::Dir) = std::uint8_t(d); thing_field(t, k, thg::X) = 40; thing_field(t, k, thg::Y) = 40; thing_field(t, k, thg::Xsub) = 8; thing_field(t, k, thg::Ysub) = 8; const City r = render::rotate_once(t); const int nd = r.xthg[std::size_t(k) * 12 + 1]; const int rmx = -kThingDY[d], rmy = kThingDX[d]; // motion (DX,DY) under one CW quarter → (−DY,DX) CHECK(kThingDX[nd] == rmx && kThingDY[nd] == rmy); // heading matches motion ⇒ never "backwards" } // Non-directional types keep their heading byte (explosion +1 = a frame timer, not a facing). City e; const int m = thing_alloc(e); thing_field(e, m, thg::Type) = 6; thing_field(e, m, thg::Dir) = 1; // explosion, "dir" = fireball frame thing_field(e, m, thg::X) = 30; thing_field(e, m, thg::Y) = 30; const City re = render::rotate_once(e); CHECK(re.xthg[std::size_t(m) * 12 + 1] == 1); // untouched } // ---- Ship navigability: water bit + water TERRAIN (XTER 0x10-0x1f) + open water / a // bridge span the ship sails under; not shallows, shore, marina, or a building on water ---- { City c; c.xter.at(10, 10) = 0x10; c.xbit.at(10, 10) = 0x04; c.xbld.at(10, 10) = 0x00; CHECK(ship_can_enter(c, 10, 10)); // open deep water → navigable c.xbld.at(10, 10) = 0x51; CHECK(ship_can_enter(c, 10, 10)); // a suspension-bridge span → sails under it c.xbld.at(10, 10) = 0x6b; CHECK(ship_can_enter(c, 10, 10)); // …another whitelisted span c.xbld.at(10, 10) = 0xF8; CHECK(!ship_can_enter(c, 10, 10)); // marina water → off-limits c.xbld.at(10, 10) = 0x70; CHECK(!ship_can_enter(c, 10, 10)); // a building on the water → blocked c.xbld.at(10, 10) = 0x00; c.xter.at(10, 10) = 0x05; CHECK(!ship_can_enter(c, 10, 10)); // land terrain (even with the water bit) → no c.xter.at(10, 10) = 0x25; CHECK(!ship_can_enter(c, 10, 10)); // shoreline slope → no c.xter.at(10, 10) = 0x10; c.xbit.at(10, 10) = 0x00; CHECK(!ship_can_enter(c, 10, 10)); // no water bit → no } // ---- Pathfinding: a departing ship in CLEAR water REACHES the map edge and leaves — it does not // orbit an unreachable point forever (the user's "stuck, can't reach a clear edge"). ---- { City c; MsRand rng(0x5A1L); for (auto& v : c.xter.cells) v = 0x10; // a whole-map deep-water ocean, for (auto& v : c.xbit.cells) v = 0x04; // navigable everywhere const int idx = thing_alloc(c); ship_nav_reset(idx); // clean nav state (the game resets on spawn) thing_field(c, idx, thg::Type) = 3; // a cargo ship mid-ocean, thing_field(c, idx, thg::X) = 64; thing_field(c, idx, thg::Y) = 64; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::DestX) = 64; thing_field(c, idx, thg::DestY) = 4; // …departing to the west edge thing_field(c, idx, thg::Dir) = std::uint8_t(thing_bearing(64, 64, 64, 4)); thing_field(c, idx, thg::State) = 0x80; // State 0x80 = outbound leg c.xtxt.at(64, 64) = std::uint8_t(idx - 55); int ticks = 0; bool left = false; for (; ticks < 3000; ++ticks) { cargoship_step(c, rng, idx); if (thing_field(c, idx, thg::Type) != 3) { left = true; break; } } CHECK(left); // it reached the edge and despawned — NOT stuck CHECK(thing_field(c, idx, thg::Type) == 0); // …cleanly (despawn, not a wreck/explosion) CHECK(ticks < 1500); // and within a sane time (≈ straight-line sail) std::printf(" cargo ship reached the edge in %d ticks (not stuck)\n", ticks); } // ---- …and a genuinely boxed-in ship DESPAWNS rather than oscillating forever (the original's boxed-in case) ---- { City c; MsRand rng(0x0B0); const int idx = thing_alloc(c); ship_nav_reset(idx); c.xter.at(64, 64) = 0x10; c.xbit.at(64, 64) = 0x04; // a single water tile, surrounded by land thing_field(c, idx, thg::Type) = 3; thing_field(c, idx, thg::X) = 64; thing_field(c, idx, thg::Y) = 64; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::DestX) = 64; thing_field(c, idx, thg::DestY) = 4; thing_field(c, idx, thg::State) = 0; thing_field(c, idx, thg::Link) = 0; c.xtxt.at(64, 64) = std::uint8_t(idx - 55); int ticks = 0; bool gone = false; for (; ticks < 200; ++ticks) { cargoship_step(c, rng, idx); if (thing_field(c, idx, thg::Type) != 3) { gone = true; break; } } CHECK(gone); // boxed in → it gives up and despawns CHECK(thing_field(c, idx, thg::Type) == 0); // …without running aground (it never left the water tile) CHECK(ticks < 30); // quickly (a few reroute/search ticks) } // ---- Spawn requires a navigable PATH from a map edge to the seaport: a land-locked harbour (e.g. // FourCities' enclosed bay) gets NO ship, so a ship can't spawn only to run aground (fireball spam) ---- { City ocean; for (auto& v : ocean.xter.cells) v = 0x10; for (auto& v : ocean.xbit.cells) v = 0x04; int ex = -1, ey = -1; MsRand rng(11); CHECK(ship_entry_from_seaport(ocean, 64, 64, rng, ex, ey)); // open ocean → a connected edge entry CHECK(ex == 2 || ex == City::kDim - 3 || ey == 2 || ey == City::kDim - 3); // …and it sits on a map edge City pond; // water only in an interior 12×12 block for (int r = 58; r <= 69; ++r) for (int cc = 58; cc <= 69; ++cc) { pond.xter.at(r, cc) = 0x10; pond.xbit.at(r, cc) |= 0x04; } int px, py; CHECK(!ship_entry_from_seaport(pond, 63, 63, rng, px, py)); // land-locked → no entry → no spawn } // ---- A ship NEVER runs aground/explodes from normal sailing — it reroutes off a shore (no fireballs) ---- { City c; MsRand rng(3); for (int cc = 0; cc < City::kDim; ++cc) // a straight 3-tile-wide ocean channel west for (int r = 63; r <= 65; ++r) { c.xter.at(r, cc) = 0x10; c.xbit.at(r, cc) |= 0x04; } c.xter.at(64, 70) = 0; c.xbit.at(64, 70) &= ~0x04; // a shoal mid-channel it must steer around const int idx = thing_alloc(c); ship_nav_reset(idx); thing_field(c, idx, thg::Type) = 3; thing_field(c, idx, thg::X) = 64; thing_field(c, idx, thg::Y) = 74; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::DestX) = 64; thing_field(c, idx, thg::DestY) = 4; thing_field(c, idx, thg::State) = 0x80; thing_field(c, idx, thg::Dir) = std::uint8_t(thing_bearing(64, 74, 64, 4)); c.xtxt.at(64, 74) = std::uint8_t(idx - 55); bool wrecked = false, left = false; for (int t = 0; t < 4000; ++t) { cargoship_step(c, rng, idx); const int ty = thing_field(c, idx, thg::Type); if (ty == 6) { wrecked = true; break; } if (ty == 0) { left = true; break; } } CHECK(!wrecked); // it never grounded into an explosion CHECK(left); // and sailed out the west edge cleanly } // ---- Sailboat (yacht): actually MOVES (its own 4-way mover, not the speed-0 universal one) and sails // STRAIGHT most ticks — re-orienting only ~1/4 of ticks. The old port called the universal mover // with the type-9 speed (0 in the speed table → it never moved) AND turned every tick = the "spazz". ---- { City c; MsRand rng(0x5A11); for (auto& v : c.xbit.cells) v = 0x04; // open water everywhere (XBIT bit 2) const int idx = thing_alloc(c); thing_field(c, idx, thg::Type) = 9; thing_field(c, idx, thg::X) = 64; thing_field(c, idx, thg::Y) = 64; thing_field(c, idx, thg::Xsub) = 8; thing_field(c, idx, thg::Ysub) = 8; thing_field(c, idx, thg::Dir) = 1; c.xtxt.at(64, 64) = std::uint8_t(idx - 55); int moves = 0, turns = 0, ticks = 0, prevDir = 1; for (; ticks < 150 && thing_field(c, idx, thg::Type) == 9; ++ticks) { const int px = thing_field(c, idx, thg::X), py = thing_field(c, idx, thg::Y); sailboat_step(c, rng, idx); if (thing_field(c, idx, thg::Type) != 9) break; if (thing_field(c, idx, thg::X) != px || thing_field(c, idx, thg::Y) != py) ++moves; const int d = thing_field(c, idx, thg::Dir); if (d != prevDir) ++turns; prevDir = d; } CHECK(moves > 15); // it actually sailed (broken port: 0 moves) CHECK(turns * 3 < ticks); // …and held a heading most ticks (no every-tick spazz) std::printf(" sailboat: %d moves / %d turns over %d ticks\n", moves, turns, ticks); } // ---- Monster RENDER (numeric — verify-visuals-numerically): the 13-piece draw // is (a) present, (b) horizontally centred over its own tile column — the same (r−c) screen column its // rampage fires appear in at (x+1,y+1) (user: "isn't centered over where it's causing fires") — and // (c) ANIMATED: different leg-joint bits (+8/+9) must change pixels (user: "limbs don't move at all"). { using namespace sc2k::render; if (!have_render_assets()) { std::printf(" (skip monster render — no art)\n"); } else { City c; MsRand rng(0x1F10); const int idx = spawn_monster(c, rng, 64, 64); CHECK(idx >= 1); thing_field(c, idx, thg::Frame) = 8; // fully risen Assets a = load_assets(); CityRenderer renderer(a.bank, a.palette); const int W = 640, H = 640; const int alt = int(c.altm.at(64, 64)) & 0x1F; const ScreenPos o = origin_centering_tile(64, 64, alt, 2, W / 2, H / 2); auto shot = [&](std::uint8_t legs1, std::uint8_t legs2) { thing_field(c, idx, thg::DestX) = legs1; // the leg-joint bitfields (+8/+9) thing_field(c, idx, thg::DestY) = legs2; Framebuffer fb(W, H); renderer.render(c, fb, Camera{o.x, o.y}, 0, 0, 2, {}); return fb; }; const Framebuffer base = [&] { // the scene WITHOUT the monster const std::uint8_t t = thing_field(c, idx, thg::Type); thing_field(c, idx, thg::Type) = 0; Framebuffer fb(W, H); renderer.render(c, fb, Camera{o.x, o.y}, 0, 0, 2, {}); thing_field(c, idx, thg::Type) = t; return fb; }(); auto diff_stats = [&](const Framebuffer& fa, const Framebuffer& fb2, long& n, long& sumX) { n = 0; sumX = 0; for (int y = 0; y < H; ++y) for (int x = 0; x < W; ++x) { const Rgba p = fa.at(x, y), q = fb2.at(x, y); if (p.r != q.r || p.g != q.g || p.b != q.b) { ++n; sumX += x; } } }; const Framebuffer mA = shot(0x00, 0x00), mB = shot(0x3F, 0x3F); long nA, sxA, nB, sxB, nAB, sxAB; diff_stats(base, mA, nA, sxA); // monster vs empty scene = its pixels diff_stats(base, mB, nB, sxB); diff_stats(mA, mB, nAB, sxAB); // pose A vs pose B = the moving limbs CHECK(nA > 1500 && nB > 1500); // the 13 pieces really draw (~a big sprite) CHECK(nAB > 200); // the joint bits MOVE pixels — limbs animate const long cxA = nA ? sxA / nA : 0; CHECK(std::abs(cxA - W / 2) < 48); // centred over its tile column (= its fire column) std::printf(" monster render: %ld px, centroid x=%ld (screen centre %d), pose diff %ld px\n", nA, cxA, W / 2, nAB); } } if (failures == 0) { std::printf("things: all checks passed\n"); return 0; } std::printf("things: %d failure(s)\n", failures); return 1; }