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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464#include "sc2k/sim/milestone4_runtime.hpp"
#include <array>#include <cstdint>#include <cstdio>#include <utility>#include <vector>
#include "sc2k/sim/growth.hpp"#include "sc2k/sim/microsim_labels.hpp"
#include "test_harness.hpp"
namespace {
using namespace sc2k;
struct RuntimeOwner { sim::DisasterSim disaster; bool effectsRemaining = false; bool thingsRemaining = false; sim::DisasterEventTracker disasterEvents; sim::RuntimeDisasterDispatchOrder disasterDispatch; MsRand vehicleRng{sim::kDefaultVehicleRandomState};
[[nodiscard]] sim::Milestone4RuntimeSource source() const { return {disaster, effectsRemaining, thingsRemaining, disasterEvents, disasterDispatch, vehicleRng}; }
[[nodiscard]] sim::Milestone4RuntimeTarget target() { return {disaster, effectsRemaining, thingsRemaining, disasterEvents, disasterDispatch, vehicleRng}; }};
sim::Milestone4State migrated_state(const City& city) { const sim::Milestone4MigrationResult migrated = sim::make_migrated_milestone4_state(city); CHECK(migrated.ok()); return migrated.state;}
void make_fire_runtime(RuntimeOwner& owner, bool active) { owner.disaster.lfsr.seed(0xBEEF); owner.disaster.rubbleRng.seed(0xC0FFEEu); owner.disaster.effectTimer = active ? 19 : 0; owner.disaster.aftershock = active ? 7 : 0; owner.disaster.active = active; owner.disaster.type = sim::dtype::kFire; owner.disaster.epiX = 10; owner.disaster.epiY = 11; owner.effectsRemaining = active; owner.thingsRemaining = active;}
void observe_disaster_start(RuntimeOwner& owner) { sim::EventBuffer events; events.begin_batch(17); CHECK(owner.disasterEvents.start(events, sim::dtype::kFire, 10, 11) == 1);}
void observe_disaster_end(RuntimeOwner& owner) { sim::EventBuffer events; events.begin_batch(18); CHECK(owner.disasterEvents.end(events));}
void lifecycle_boundaries_round_trip_without_replaying_edges() { using Boundary = sim::PersistedDisasterLifecycleBoundary; struct Fixture { Boundary expected; bool runtimeActive; bool observeStart; bool observeEnd; bool startEdgeObserved; bool endEdgeObserved; bool previousActiveLatch; }; constexpr std::array fixtures{ Fixture{Boundary::UnobservedStart, true, false, false, false, false, false}, Fixture{Boundary::Running, true, true, false, true, false, true}, Fixture{Boundary::UnobservedEnd, false, true, false, true, false, true}, Fixture{Boundary::Settled, false, true, true, true, true, false}, };
for (const Fixture& fixture : fixtures) { sim::ship_nav_reset_all(); City city(MapSize::Tiny); sim::Milestone4State authority = migrated_state(city); RuntimeOwner source; make_fire_runtime(source, fixture.runtimeActive); if (fixture.observeStart) observe_disaster_start(source); if (fixture.observeEnd) observe_disaster_end(source);
const sim::Milestone4RuntimeSnapshotResult snap = sim::snapshot_milestone4_runtime( authority, city, source.source()); CHECK(snap.ok()); if (!snap.ok()) continue;
RuntimeOwner fresh; fresh.disaster.type = sim::dtype::kVolcano; fresh.effectsRemaining = !source.effectsRemaining; fresh.thingsRemaining = !source.thingsRemaining; fresh.disasterDispatch[sim::kFireDispatchThingType] = {39}; fresh.vehicleRng.seed(1); sim::ship_nav(7).rawGoalX = 77;
const sim::Milestone4RuntimeRestoreResult restored = sim::restore_milestone4_runtime( authority, city, fresh.target()); CHECK(restored.ok()); if (!restored.ok()) continue; CHECK(restored.disasterEdges.boundary == fixture.expected); CHECK(restored.disasterEdges.startEdgeObserved == fixture.startEdgeObserved); CHECK(restored.disasterEdges.endEdgeObserved == fixture.endEdgeObserved); CHECK(restored.disasterEdges.previous_active_latch() == fixture.previousActiveLatch); CHECK(restored.disasterEdges.start_pending() == (fixture.expected == Boundary::UnobservedStart)); CHECK(restored.disasterEdges.end_pending() == (fixture.expected == Boundary::UnobservedEnd)); CHECK(fresh.disaster.active == fixture.runtimeActive); CHECK(fresh.disaster.type == sim::dtype::kFire); CHECK(fresh.disaster.lfsr.state() == 0xBEEF); CHECK(fresh.disaster.rubbleRng.state() == 0xC0FFEEu); CHECK(fresh.disaster.effectTimer == (fixture.runtimeActive ? 19 : 0)); CHECK(fresh.disaster.aftershock == (fixture.runtimeActive ? 7 : 0)); CHECK(fresh.disaster.epiX == 10 && fresh.disaster.epiY == 11); CHECK(fresh.effectsRemaining == fixture.runtimeActive); CHECK(fresh.thingsRemaining == fixture.runtimeActive); CHECK(fresh.disasterEvents.continuation_state() == authority.disaster.events); CHECK(fresh.disasterDispatch.empty()); CHECK(sim::ship_nav(7).rawGoalX == -1); }}
City make_scenario_city() { City city(MapSize::Tiny); city.scen.assign(4 + 0x34, 0); city.scen[0] = 0x80; city.scen[4] = 0; city.scen[5] = sim::dtype::kQuake; city.scen[6] = 13; city.scen[7] = 17; city.scen[8] = 0; city.scen[9] = 12; return city;}
void scenario_launch_claim_is_exactly_once_and_rng_neutral() { sim::ship_nav_reset_all(); City city = make_scenario_city(); sim::Milestone4State authority = migrated_state(city); CHECK(authority.disaster.scenarioScriptLaunch == sim::ScenarioScriptLaunchState::Pending);
RuntimeOwner runtime; runtime.disaster.lfsr.seed(0x1357); runtime.disaster.rubbleRng.seed(0x2468ACE0u); runtime.vehicleRng.seed(0xDEADBEEFu); const std::uint16_t lfsrBefore = runtime.disaster.lfsr.state(); const std::uint32_t rubbleBefore = runtime.disaster.rubbleRng.state(); const std::uint32_t vehicleBefore = runtime.vehicleRng.state();
const auto request = sim::claim_milestone4_scenario_script_launch(authority, city); CHECK(request.has_value()); if (request) { CHECK(request->type == sim::dtype::kQuake); CHECK(request->x == 13 && request->y == 17); } CHECK(authority.disaster.scenarioScriptLaunch == sim::ScenarioScriptLaunchState::Attempted); CHECK(!sim::claim_milestone4_scenario_script_launch(authority, city)); CHECK(runtime.disaster.lfsr.state() == lfsrBefore); CHECK(runtime.disaster.rubbleRng.state() == rubbleBefore); CHECK(runtime.vehicleRng.state() == vehicleBefore);
const sim::Milestone4RuntimeSnapshotResult snap = sim::snapshot_milestone4_runtime( authority, city, runtime.source()); CHECK(snap.ok()); CHECK(authority.disaster.scenarioScriptLaunch == sim::ScenarioScriptLaunchState::Attempted); CHECK(runtime.disaster.lfsr.state() == lfsrBefore); CHECK(runtime.disaster.rubbleRng.state() == rubbleBefore); CHECK(runtime.vehicleRng.state() == vehicleBefore);}
void seed_thing(City& city, int slot, int type, int x, int y, int destinationX, int destinationY, int state = 0) { sim::thing_field(city, slot, sim::thg::Type) = static_cast<std::uint8_t>(type); sim::thing_field(city, slot, sim::thg::State) = static_cast<std::uint8_t>(state); sim::thing_field(city, slot, sim::thg::X) = static_cast<std::uint8_t>(x); sim::thing_field(city, slot, sim::thg::Y) = static_cast<std::uint8_t>(y); sim::thing_field(city, slot, sim::thg::DestX) = static_cast<std::uint8_t>(destinationX); sim::thing_field(city, slot, sim::thg::DestY) = static_cast<std::uint8_t>(destinationY); city.xtxt.at(x, y) = static_cast<std::uint8_t>( static_cast<unsigned>(xtxt::kThingFirst) + slot);}
void dispatch_and_cargo_round_trip_through_a_fresh_owner() { sim::ship_nav_reset_all(); City city; seed_thing(city, 5, 3, 10, 11, 20, 21); sim::Milestone4State authority = migrated_state(city);
// Dispatch Things are City authority while their vector order is the relocation FIFO. A deliberately // non-slot-sorted police queue proves that capture does not reconstruct order from XTHG. seed_thing(city, 3, sim::kPoliceDispatchThingType, 30, 31, 30, 31); seed_thing(city, 9, sim::kPoliceDispatchThingType, 32, 33, 32, 33); seed_thing(city, 12, sim::kFireDispatchThingType, 34, 35, 34, 35); seed_thing(city, 20, sim::kMilitaryDispatchThingType, 36, 37, 36, 37);
sim::ShipNav& navigation = sim::ship_nav(5); navigation.wp = {{10, 11}, {15, 16}, {20, 21}}; navigation.cursor = 1; navigation.rawGoalX = 20; navigation.rawGoalY = 21; navigation.dwell = 0; navigation.trail = {{8, 9}, {10, 11}};
RuntimeOwner source; make_fire_runtime(source, true); observe_disaster_start(source); source.disasterDispatch = { {sim::kPoliceDispatchThingType, {9, 3}}, {sim::kFireDispatchThingType, {12}}, {sim::kMilitaryDispatchThingType, {20}}, }; source.vehicleRng.seed(0xA1B2C3D4u); const sim::Milestone4RuntimeSnapshotResult snap = sim::snapshot_milestone4_runtime( authority, city, source.source()); CHECK(snap.ok()); if (!snap.ok()) return; CHECK(authority.disaster.dispatch.police.count == 2); CHECK(authority.disaster.dispatch.police.slots[0] == 9); CHECK(authority.disaster.dispatch.police.slots[1] == 3); CHECK(authority.things.cargoShips.size() == 1);
sim::ship_nav_reset_all(); sim::ship_nav(7).wp = {{7, 7}}; sim::ship_nav(7).cursor = 1; RuntimeOwner fresh; fresh.disaster.type = sim::dtype::kMonster; fresh.effectsRemaining = false; fresh.thingsRemaining = false; fresh.disasterDispatch = {{sim::kFireDispatchThingType, {39}}}; fresh.vehicleRng.seed(1);
const sim::Milestone4RuntimeRestoreResult restored = sim::restore_milestone4_runtime( authority, city, fresh.target()); CHECK(restored.ok()); if (!restored.ok()) return; CHECK(fresh.disasterDispatch == source.disasterDispatch); CHECK(fresh.vehicleRng.state() == 0xA1B2C3D4u); CHECK(sim::ship_nav(5).wp == std::vector<sim::nav::Point>({{10, 11}, {15, 16}, {20, 21}})); CHECK(sim::ship_nav(5).cursor == 1); CHECK(sim::ship_nav(5).rawGoalX == 20 && sim::ship_nav(5).rawGoalY == 21); CHECK(sim::ship_nav(5).trail.empty()); CHECK(sim::ship_nav(7).wp.empty());
sim::Milestone4State recaptured = authority; const sim::Milestone4RuntimeSnapshotResult resnapshot = sim::snapshot_milestone4_runtime( recaptured, city, fresh.source()); CHECK(resnapshot.ok()); CHECK(recaptured == authority);}
void dispatch_round_trips_before_the_start_edge_is_observed() { sim::ship_nav_reset_all(); City city; sim::Milestone4State authority = migrated_state(city); seed_thing(city, 3, sim::kPoliceDispatchThingType, 30, 31, 30, 31);
RuntimeOwner source; make_fire_runtime(source, true); source.disasterDispatch = { {sim::kPoliceDispatchThingType, {3}}, }; CHECK(source.disasterEvents.active_instance_id() == 0); const sim::Milestone4RuntimeSnapshotResult snapshot = sim::snapshot_milestone4_runtime( authority, city, source.source()); CHECK(snapshot.ok()); CHECK(sim::persisted_disaster_edge_observation( authority.disaster) .boundary == sim::PersistedDisasterLifecycleBoundary::UnobservedStart);
RuntimeOwner fresh; const sim::Milestone4RuntimeRestoreResult restored = sim::restore_milestone4_runtime( authority, city, fresh.target()); CHECK(restored.ok()); CHECK(restored.disasterEdges.boundary == sim::PersistedDisasterLifecycleBoundary::UnobservedStart); CHECK(fresh.disasterDispatch == source.disasterDispatch); CHECK(fresh.disasterEvents.active_instance_id() == 0);
// The same queue is still invalid once neither runtime activity nor an open event owns it. source.disaster.active = false; source.disaster.effectTimer = 0; source.disaster.aftershock = 0; source.effectsRemaining = false; source.thingsRemaining = false; const sim::Milestone4State beforeRejected = authority; const sim::Milestone4RuntimeSnapshotResult rejected = sim::snapshot_milestone4_runtime( authority, city, source.source()); CHECK(rejected.error == sim::Milestone4RuntimeBoundaryError::InvalidMilestone4State); CHECK(authority == beforeRejected);}
void stadium_reconciliation_is_part_of_the_atomic_snapshot() { sim::ship_nav_reset_all(); City city; sim::Milestone4State authority = migrated_state(city); RuntimeOwner runtime; CHECK(authority.stadiums.recordCount == 0);
constexpr int row = 20; constexpr int column = 24; CHECK(construct_building(city, row, column, xbld::kStadium, sim::kStadiumLifecycleFootprintSize, 0)); sim::provision_stadium(city, row, column, 1234); const int slot = sim::stadium_slot(city, row, column); CHECK(slot >= 0);
const sim::Milestone4RuntimeSnapshotResult added = sim::snapshot_milestone4_runtime( authority, city, runtime.source()); CHECK(added.ok()); CHECK(authority.stadiums.recordCount == 1); CHECK(authority.stadiums.records[0].structure.row == row); CHECK(authority.stadiums.records[0].structure.column == column); CHECK(authority.stadiums.records[0].structure.microsimSlot == slot); CHECK(sim::stadium_lifecycle_v2_matches_city( city, authority.stadiums));
City missingStadium = city; for (int rowOffset = 0; rowOffset < sim::kStadiumLifecycleFootprintSize; ++rowOffset) for (int columnOffset = 0; columnOffset < sim::kStadiumLifecycleFootprintSize; ++columnOffset) missingStadium.xbld.at( row + rowOffset, column + columnOffset) = 0; RuntimeOwner untouched; untouched.disaster.type = sim::dtype::kQuake; untouched.vehicleRng.seed(0x12345678u); const sim::Milestone4RuntimeRestoreResult mismatched = sim::restore_milestone4_runtime( authority, missingStadium, untouched.target()); CHECK(mismatched.error == sim::Milestone4RuntimeBoundaryError::InvalidMilestone4State); CHECK(mismatched.validationError == sim::Milestone4ValidationError::InvalidStadiumLifecycle); CHECK(untouched.disaster.type == sim::dtype::kQuake); CHECK(untouched.vehicleRng.state() == 0x12345678u);
for (int rowOffset = 0; rowOffset < sim::kStadiumLifecycleFootprintSize; ++rowOffset) for (int columnOffset = 0; columnOffset < sim::kStadiumLifecycleFootprintSize; ++columnOffset) city.xbld.at(row + rowOffset, column + columnOffset) = 0;
const sim::Milestone4RuntimeSnapshotResult removed = sim::snapshot_milestone4_runtime( authority, city, runtime.source()); CHECK(removed.ok()); CHECK(authority.stadiums.recordCount == 0); CHECK(sim::stadium_lifecycle_v2_matches_city( city, authority.stadiums));}
void rejected_boundaries_leave_authority_and_runtime_unchanged() { sim::ship_nav_reset_all(); City city; sim::Milestone4State authority = migrated_state(city); RuntimeOwner source; const sim::Milestone4State beforeSnapshot = authority; source.disasterDispatch[99] = {1}; const sim::Milestone4RuntimeSnapshotResult rejectedSnapshot = sim::snapshot_milestone4_runtime( authority, city, source.source()); CHECK(rejectedSnapshot.error == sim::Milestone4RuntimeBoundaryError::InvalidDisasterDispatch); CHECK(authority == beforeSnapshot);
sim::Milestone4State invalid = authority; invalid.disaster.runtime.lfsrState = 0; RuntimeOwner target; target.disaster.lfsr.seed(0x2468); target.disaster.rubbleRng.seed(0x13572468u); target.disaster.type = sim::dtype::kQuake; target.effectsRemaining = true; target.thingsRemaining = true; target.disasterDispatch = {{sim::kFireDispatchThingType, {4, 2}}}; target.vehicleRng.seed(0xCAFEBABEu); sim::ship_nav(6).rawGoalX = 61;
const sim::Milestone4RuntimeRestoreResult rejectedRestore = sim::restore_milestone4_runtime(invalid, city, target.target()); CHECK(rejectedRestore.error == sim::Milestone4RuntimeBoundaryError::InvalidMilestone4State); CHECK(target.disaster.lfsr.state() == 0x2468); CHECK(target.disaster.rubbleRng.state() == 0x13572468u); CHECK(target.disaster.type == sim::dtype::kQuake); CHECK(target.effectsRemaining && target.thingsRemaining); CHECK(target.disasterDispatch == sim::RuntimeDisasterDispatchOrder( {{sim::kFireDispatchThingType, {4, 2}}})); CHECK(target.vehicleRng.state() == 0xCAFEBABEu); CHECK(sim::ship_nav(6).rawGoalX == 61);}
} // namespace
int main() { lifecycle_boundaries_round_trip_without_replaying_edges(); scenario_launch_claim_is_exactly_once_and_rng_neutral(); dispatch_and_cargo_round_trip_through_a_fresh_owner(); dispatch_round_trips_before_the_start_edge_is_observed(); stadium_reconciliation_is_part_of_the_atomic_snapshot(); rejected_boundaries_leave_authority_and_runtime_unchanged(); sim::ship_nav_reset_all();
if (failures == 0) { std::printf("milestone 4 runtime boundary: all checks passed\n"); return 0; } std::printf("milestone 4 runtime boundary: %d failure(s)\n", failures); return 1;}