// Repro + regression test for the "Goto a disaster lands it off-screen" report. The centring math is // fine (origin_centering_tile round-trips in test_iso_projection); the real bug was disaster_focus // averaging in EVERY moving Thing — aircraft, ships, trains, dispatch units — not just the disaster, so // in a live city full of traffic the focus was dragged off toward mid-traffic and the fire/tornado // ended up off-screen. This test seeds stray traffic and checks Goto still lands on the disaster. // Pure (no SDL). #include "sc2k/sim/disasters.hpp" #include "sc2k/sim/things.hpp" #include "sc2k/render/iso_projection.hpp" #include "sc2k/render/rotate.hpp" #include "sc2k/city.hpp" #include "sc2k/rng.hpp" #include #include using namespace sc2k; #include "test_harness.hpp" // The true centroid of the on-map disaster effect tiles (fire/flood/riot/toxic), for comparison. static void effect_centroid(const City& c, int& er, int& ec, int& n) { long sr = 0, sc = 0; n = 0; for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) if (sim::fx::is_effect(c.xtxt.at(r, col))) { sr += r; sc += col; ++n; } er = n ? int(sr / n) : -1; ec = n ? int(sc / n) : -1; } // Seed a handful of regular (non-disaster) Things scattered to the map corners — the kind of traffic a // live city always has. These must NOT influence the disaster focus. static void seed_traffic(City& c) { const int spots[3][3] = {{1, 5, 5}, {2, 8, 122}, {3, 120, 8}}; // {type, x, y} for (auto& s : spots) { const int idx = sim::thing_alloc(c); if (!idx) continue; sim::thing_field(c, idx, sim::thg::Type) = std::uint8_t(s[0]); sim::thing_field(c, idx, sim::thg::X) = std::uint8_t(s[1]); sim::thing_field(c, idx, sim::thg::Y) = std::uint8_t(s[2]); } } static City make_city() { City c; for (int r = 0; r < City::kDim; ++r) for (int col = 0; col < City::kDim; ++col) c.altm.at(r, col) = std::uint8_t((r / 8) & 0x1F); // altitude rises N→S for (int r = 40; r < 56; ++r) for (int col = 70; col < 86; ++col) c.xbld.at(r, col) = std::uint8_t(0x70 + ((r * 3 + col * 5) & 0x1F)); // downtown ~(48,78) return c; } // Goto lands tile (cr,cc) dead-centre (mirrors app/main.cpp center_on_tile), so a focus equal to the // disaster location is on-screen. Returns the focus's screen position at rotation 0, zoom 2. static render::ScreenPos goto_screen_pos(const City& c, int cr, int cc, int W, int H) { const int alt = c.altm.at(cr, cc) & 0x1F; // rotation 0 → view == city const render::ScreenPos o = render::origin_centering_tile(cr, cc, alt, 2, W / 2, H / 2); return render::tile_to_screen(cr, cc, alt, 2, o.x, o.y); } int main() { const int W = 1280, H = 800; // ── Fire amid traffic: focus must track the fire, not the traffic ────────────────────────────── { City c = make_city(); MsRand rng(0x1234); sim::DisasterSim d; CHECK(sim::start_fire(c, d, rng)); int er, ec, en; effect_centroid(c, er, ec, en); // where the fire really is seed_traffic(c); // …now add city traffic int fr = -1, fc = -1; CHECK(sim::disaster_focus(c, d, fr, fc)); std::printf("fire: effect centroid=(%d,%d) n=%d disaster_focus=(%d,%d)\n", er, ec, en, fr, fc); CHECK(std::abs(fr - er) <= 1 && std::abs(fc - ec) <= 1); // focus == the fire (not traffic) } // ── Tornado amid traffic: focus must track the tornado Thing ─────────────────────────────────── { City c = make_city(); MsRand rng(0x99); const int tx = 64, ty = 64; CHECK(sim::spawn_tornado(c, rng, tx, ty) != 0); seed_traffic(c); sim::DisasterSim d; d.active = true; d.type = 7; int fr = -1, fc = -1; CHECK(sim::disaster_focus(c, d, fr, fc)); std::printf("tornado: spawned at (%d,%d) disaster_focus=(%d,%d)\n", tx, ty, fr, fc); CHECK(std::abs(fr - tx) <= 1 && std::abs(fc - ty) <= 1); // focus == the tornado const render::ScreenPos p = goto_screen_pos(c, fr, fc, W, H); CHECK(p.x == W / 2 && p.y == H / 2); // …and Goto centres it } if (failures == 0) { std::printf("goto_focus: all checks passed\n"); return 0; } std::printf("goto_focus: %d failure(s)\n", failures); return 1; }