// 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 #include using namespace sc2k; #include "test_harness.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 } if (failures == 0) { std::printf("dispatch: all checks passed\n"); return 0; } std::printf("dispatch: %d failure(s)\n", failures); return 1; }