Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165#include "sc2k/sim/things.hpp"
#include <array>#include <cstddef>#include <cstdint>#include <cstdio>
#include "test_harness.hpp"
using namespace sc2k;using namespace sc2k::sim;
namespace {
void seed_cargo(City& city, int slot, int x, int y, int destinationX, int destinationY, int state) { thing_field(city, slot, thg::Type) = 3; thing_field(city, slot, thg::State) = static_cast<std::uint8_t>(state); thing_field(city, slot, thg::X) = static_cast<std::uint8_t>(x); thing_field(city, slot, thg::Y) = static_cast<std::uint8_t>(y); thing_field(city, slot, thg::DestX) = static_cast<std::uint8_t>(destinationX); thing_field(city, slot, thg::DestY) = static_cast<std::uint8_t>(destinationY); city.xtxt.at(x, y) = static_cast<std::uint8_t>( static_cast<unsigned>(xtxt::kThingFirst) + slot);}
bool same_navigation(const ShipNav& left, const ShipNav& right) { return left.wp == right.wp && left.cursor == right.cursor && left.rawGoalX == right.rawGoalX && left.rawGoalY == right.rawGoalY && left.failed == right.failed && left.dwell == right.dwell && left.berthX == right.berthX && left.berthY == right.berthY && left.pierX == right.pierX && left.pierY == right.pierY && left.trail == right.trail;}
void test_exact_capture_and_restore() { ship_nav_reset_all(); City city; seed_cargo(city, 5, 10, 11, 20, 21, 3); seed_cargo(city, 17, 40, 41, 42, 43, 0);
ShipNav& first = ship_nav(5); first.wp = {{10, 11}, {15, 16}, {30, 31}}; first.cursor = 2; first.rawGoalX = 20; first.rawGoalY = 21; first.failed = false; first.dwell = 172; first.berthX = 30; first.berthY = 31; first.pierX = 30; first.pierY = 32; first.trail = {{8, 9}, {10, 11}};
ShipNav& second = ship_nav(17); second.rawGoalX = 42; second.rawGoalY = 43; second.failed = true; second.trail = {{39, 40}};
MsRand vehicleRng{0xdeadbeefu}; const auto captured = capture_thing_continuation(city, vehicleRng); CHECK(captured.has_value()); CHECK(captured && captured->vehicleRng.state() == 0xdeadbeefu); CHECK(captured && captured->cargoShips.size() == 2); if (!captured) return; CHECK(captured->cargoShips[0].slot == 5); CHECK(captured->cargoShips[0].waypoints == std::vector<CargoShipWaypoint>({{10, 11}, {15, 16}, {30, 31}})); CHECK(captured->cargoShips[0].cursor == 2); CHECK(captured->cargoShips[0].dwell == 172); CHECK(captured->cargoShips[1].slot == 17); CHECK(captured->cargoShips[1].failed);
ship_nav_reset_all(); ship_nav(5).rawGoalX = 99; ship_nav(7).wp = {{70, 71}}; ship_nav(7).trail = {{72, 73}}; vehicleRng.seed(1); CHECK(restore_thing_continuation(*captured, city, vehicleRng)); CHECK(vehicleRng.state() == 0xdeadbeefu); CHECK(ship_nav(5).wp == std::vector<nav::Point>({{10, 11}, {15, 16}, {30, 31}})); CHECK(ship_nav(5).cursor == 2); CHECK(ship_nav(5).rawGoalX == 20 && ship_nav(5).rawGoalY == 21); CHECK(!ship_nav(5).failed && ship_nav(5).dwell == 172); CHECK(ship_nav(5).berthX == 30 && ship_nav(5).berthY == 31); CHECK(ship_nav(5).pierX == 30 && ship_nav(5).pierY == 32); CHECK(ship_nav(5).trail.empty()); CHECK(ship_nav(17).failed && ship_nav(17).rawGoalX == 42 && ship_nav(17).rawGoalY == 43); CHECK(ship_nav(17).trail.empty()); CHECK(ship_nav(7).wp.empty() && ship_nav(7).trail.empty());
const auto recaptured = capture_thing_continuation(city, vehicleRng); CHECK(recaptured == captured);}
void test_restore_rejection_is_atomic() { ship_nav_reset_all(); City city; seed_cargo(city, 5, 10, 11, 20, 21, 3); ShipNav& navigation = ship_nav(5); navigation.wp = {{10, 11}, {15, 16}}; navigation.cursor = 1; navigation.rawGoalX = 20; navigation.rawGoalY = 21; navigation.dwell = 77; navigation.berthX = 18; navigation.berthY = 20; navigation.pierX = 19; navigation.pierY = 20; navigation.trail = {{9, 10}, {10, 11}}; ship_nav(7).wp = {{70, 71}}; ship_nav(7).cursor = 1; ship_nav(7).trail = {{72, 73}}; MsRand vehicleRng{0xa1b2c3d4u};
const auto valid = capture_thing_continuation(city, vehicleRng); CHECK(valid.has_value()); if (!valid) return; std::array<ShipNav, thg::kCount> before; for (int slot = 0; slot < thg::kCount; ++slot) before[slot] = ship_nav(slot); const std::uint32_t rngBefore = vehicleRng.state();
ThingContinuationState invalid = *valid; invalid.cargoShips[0].cursor = 99; invalid.vehicleRng.seed(7); CHECK(!restore_thing_continuation(invalid, city, vehicleRng)); CHECK(vehicleRng.state() == rngBefore); for (int slot = 0; slot < thg::kCount; ++slot) CHECK(same_navigation(ship_nav(slot), before[slot]));
City mismatched = city; mismatched.xtxt.at(10, 11) = xtxt::kNone; CHECK(!restore_thing_continuation(*valid, mismatched, vehicleRng)); CHECK(vehicleRng.state() == rngBefore); for (int slot = 0; slot < thg::kCount; ++slot) CHECK(same_navigation(ship_nav(slot), before[slot]));
navigation.dwell = -1; const ShipNav invalidBeforeCapture = navigation; CHECK(!capture_thing_continuation(city, vehicleRng)); CHECK(same_navigation(navigation, invalidBeforeCapture)); CHECK(vehicleRng.state() == rngBefore);}
} // namespace
int main() { test_exact_capture_and_restore(); test_restore_rejection_is_atomic(); ship_nav_reset_all();
if (failures == 0) { std::printf("thing continuation runtime: all checks passed\n"); return 0; } std::printf("thing continuation runtime: %d failure(s)\n", failures); return 1;}