// test_thread_local — the ship-nav thread_local must not leak a route across a city load. // // ShipNav (sim/things.hpp) is per-thread scratch, NOT stored in the save, so loading a new city cannot // overwrite it the way the tile layers get overwritten. Without an explicit hook a ship in city B could // inherit city A's planned route / berth / goal. reset_ambient_things (the city-load hook in main.cpp) // drops the saved ships AND calls ship_nav_reset_all, so every slot is fresh before city B's ships sail. #include "sc2k/sim/things.hpp" #include #include "test_harness.hpp" using namespace sc2k; using namespace sc2k::sim; int main() { // City A: give a few ship slots a fully-planned route + berth, as cargoship_step would leave them. for (int slot : {1, 5, 30}) { ShipNav& r = ship_nav(slot); r.rawGoalX = 40; r.rawGoalY = 50; r.berthX = 41; r.berthY = 51; r.pierX = 42; r.pierY = 52; r.failed = true; r.dwell = 7; r.cursor = 3; r.wp.push_back({10, 10}); r.wp.push_back({11, 12}); r.trail.push_back({10, 10}); } CHECK(ship_nav(5).berthX == 41 && !ship_nav(5).wp.empty()); // the route really is set // Load city B: reset_ambient_things is the city-(re)load hook — it must clear every nav slot. City cityB; reset_ambient_things(cityB); for (int slot : {1, 5, 30}) { const ShipNav& r = ship_nav(slot); CHECK(r.rawGoalX == -1 && r.rawGoalY == -1); // no stale goal CHECK(r.berthX == -1 && r.berthY == -1 && r.pierX == -1 && r.pierY == -1); // no stale berth/pier CHECK(!r.failed && r.dwell == 0 && r.cursor == 0); CHECK(r.wp.empty() && r.trail.empty()); // no stale route / trail } // ship_nav_reset_all clears all kCount slots directly too (the reset hook itself). ship_nav(2).berthX = 99; ship_nav_reset_all(); for (int i = 0; i < thg::kCount; ++i) CHECK(ship_nav(i).berthX == -1); if (failures == 0) std::printf("test_thread_local: ship nav resets across a city load — OK\n"); else std::printf("test_thread_local: %d CHECK(S) FAILED\n", failures); return failures ? 1 : 0; }