// Pure test of the Emergency-dispatch limit + recycle rules (sim/things.hpp), faithful to the manual // (p.60): one unit per provider station, and once at the cap a further click RELOCATES the oldest-placed // unit (FIFO rotation) rather than adding a new one. Military has no provider yet → always unavailable. #include "sc2k/sim/things.hpp" #include "sc2k/io/sc2_file.hpp" #include #include using namespace sc2k; #include "test_harness.hpp" #include "test_paths.hpp" // A 3x3 station of XBLD `code` at (r,c) — 9 tiles, the police/fire footprint. static void place_station(City& c, int code, int r, int cc) { for (int dr = 0; dr < 3; ++dr) for (int dc = 0; dc < 3; ++dc) c.xbld.at(r + dr, cc + dc) = std::uint8_t(code); } static int count_units(const City& c, int type) { int n = 0; for (int i = 1; i < sim::thg::kCount; ++i) if (sim::thing_field(c, i, sim::thg::Type) == type) ++n; return n; } int main() { // Provider counts: one allowed unit per station; military is 0 until bases exist. { City c; CHECK(sim::dispatch_provider_count(c, 7) == 0); CHECK(sim::dispatch_provider_count(c, 8) == 0); CHECK(sim::dispatch_provider_count(c, 14) == 0); place_station(c, 0xD3, 10, 10); // fire station CHECK(sim::dispatch_provider_count(c, 8) == 1); place_station(c, 0xD3, 20, 20); CHECK(sim::dispatch_provider_count(c, 8) == 2); place_station(c, 0xD2, 30, 30); // police station CHECK(sim::dispatch_provider_count(c, 7) == 1); CHECK(sim::dispatch_provider_count(c, 8) == 2); // independent of police CHECK(sim::dispatch_provider_count(c, 14) == 0); // still no military base } // No station → no dispatch. { City c; std::vector order; CHECK(sim::dispatch_unit(c, 8, 40, 40, order) == 0); CHECK(order.empty()); CHECK(count_units(c, 8) == 0); } // One fire station: place one unit; a second click RELOCATES the same unit (cap reached). { City c; place_station(c, 0xD3, 10, 10); std::vector order; const int s1 = sim::dispatch_unit(c, 8, 40, 40, order); CHECK(s1 != 0 && order.size() == 1 && order[0] == s1); CHECK(sim::thing_field(c, s1, sim::thg::Type) == 8); CHECK(sim::thing_field(c, s1, sim::thg::X) == 40 && sim::thing_field(c, s1, sim::thg::Y) == 40); CHECK(c.xtxt.at(40, 40) == std::uint8_t(s1 - 55)); // sprite-link marker placed const int s2 = sim::dispatch_unit(c, 8, 50, 50, order); CHECK(s2 == s1); // SAME unit moved, not a new one CHECK(order.size() == 1); CHECK(count_units(c, 8) == 1); // still exactly one fire unit CHECK(c.xtxt.at(40, 40) == 0); // old cell restored CHECK(c.xtxt.at(50, 50) == std::uint8_t(s1 - 55)); // marker now at the new cell CHECK(sim::thing_field(c, s1, sim::thg::X) == 50 && sim::thing_field(c, s1, sim::thg::Y) == 50); } // Two fire stations: place two, then relocate the OLDEST (FIFO), rotating it to newest. { City c; place_station(c, 0xD3, 10, 10); place_station(c, 0xD3, 20, 20); std::vector order; const int a = sim::dispatch_unit(c, 8, 40, 40, order); const int b = sim::dispatch_unit(c, 8, 41, 41, order); CHECK(a && b && a != b && order.size() == 2 && order[0] == a && order[1] == b); CHECK(count_units(c, 8) == 2); const int third = sim::dispatch_unit(c, 8, 42, 42, order); // at cap → move oldest (A) CHECK(third == a); CHECK(order.size() == 2 && order[0] == b && order[1] == a); // rotated: B is now oldest CHECK(sim::thing_field(c, a, sim::thg::X) == 42); // A moved CHECK(sim::thing_field(c, b, sim::thg::X) == 41); // B untouched const int fourth = sim::dispatch_unit(c, 8, 43, 43, order); // now moves B CHECK(fourth == b); CHECK(sim::thing_field(c, b, sim::thg::X) == 43); CHECK(count_units(c, 8) == 2); // never exceeds the cap } // move_deploy: saves/restores the underlying cell, and refuses to stack on another unit. { City c; place_station(c, 0xD3, 10, 10); place_station(c, 0xD3, 20, 20); std::vector order; const int a = sim::dispatch_unit(c, 8, 40, 40, order); const int b = sim::dispatch_unit(c, 8, 41, 41, order); CHECK(b != 0 && sim::thing_field(c, b, sim::thg::X) == 41); // B occupies (41,41) CHECK(!sim::move_deploy(c, a, 41, 41)); // B is there → refuse to stack CHECK(sim::thing_field(c, a, sim::thg::X) == 40); // A unmoved after the refusal c.xtxt.at(60, 60) = 9; // a (fake) sign index under the target cell CHECK(sim::move_deploy(c, a, 60, 60)); CHECK(sim::thing_field(c, a, sim::thg::SavedTile) == 9); // saved what it covered CHECK(c.xtxt.at(40, 40) == 0); // old cell restored } // A Thing is live only while its XTHG record and XTXT owner link agree. Document-boundary repair // retains valid units, removes orphan records, and clears dangling links without erasing signs. { City c; place_station(c, 0xD3, 10, 10); std::vector order; const int valid = sim::dispatch_unit(c, 8, 40, 40, order); CHECK(valid != 0); constexpr int orphan = 13; sim::thing_field(c, orphan, sim::thg::Type) = 8; sim::thing_field(c, orphan, sim::thg::X) = 7; sim::thing_field(c, orphan, sim::thg::Y) = 68; c.xtxt.at(50, 50) = xtxt::kThingFirst; // reserved slot-0 link: always dangling c.xtxt.at(60, 60) = 3; // an unrelated sign survives CHECK(sim::repair_thing_ownership(c) == 2); CHECK(sim::thing_field(c, valid, sim::thg::Type) == 8); CHECK(c.xtxt.at(40, 40) == static_cast(valid - 55)); for (int field = 0; field < sim::thg::kStride; ++field) CHECK(sim::thing_field(c, orphan, field) == 0); CHECK(c.xtxt.at(50, 50) == 0); CHECK(c.xtxt.at(60, 60) == 3); } // Real classic regression: this fixture carries an old type-8 record whose ownership link is // already gone. It must not consume a slot or appear in the world after import normalization. { City c; if (io::load_sc2(city_file("CAPEQUES.SC2"), c).ok) { constexpr int orphan = 13; CHECK(sim::thing_field(c, orphan, sim::thg::Type) == 8); CHECK(c.xtxt.at(7, 68) == 0); CHECK(sim::repair_thing_ownership(c) >= 1); CHECK(sim::thing_field(c, orphan, sim::thg::Type) == 0); CHECK(sim::thing_field(c, 1, sim::thg::Type) == 3); // valid linked cargo ship retained } else { std::puts("dispatch: CAPEQUES.SC2 unavailable; orphan-Thing regression skipped"); } } if (failures == 0) { std::printf("dispatch: all checks passed\n"); return 0; } std::printf("dispatch: %d failure(s)\n", failures); return 1; }