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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243// Police/fire SERVICE integration KAT. The existing test_disasters fills XFIR by hand; this one drives// the WHOLE chain a player exercises: build a station → the coverage stencil (coverage.hpp)// fills XPLC/XFIR from it → crime drops / fires get put out. It catches integration breaks that the// hand-filled tests can't (e.g. a built station that never registers as "active"/powered, so coverage// stays 0 and fires rage no matter how many stations you plop).#include "sc2k/city.hpp"#include "sc2k/rng.hpp"#include "sc2k/sim/coverage.hpp"#include "sc2k/sim/crime.hpp"#include "sc2k/sim/growth.hpp" // construct_building — what the build tool calls#include "sc2k/sim/land_value.hpp"#include "sc2k/sim/disasters.hpp"#include "sc2k/sim/things.hpp" // spawn_deploy — the Dispatch tool's deploy-unit markers#include "sc2k/sim/simulation.hpp" // the LIVE driver — phase_fields runs the coverage pass at tick%25==2#include <algorithm>#include <cstdio>
using namespace sc2k;using namespace sc2k::sim;
#include "test_harness.hpp"
static int max_field(const CoverageField& f) { int m = 0; for (auto v : f) m = v > m ? v : m; return m;}
// Place a station exactly as the build tool does. The build tool requires clear land, so first clear the// footprint (as the player would by bulldozing): construct_building(code,size,rot=0), then mark the// footprint powered (XBIT 0x40) — the power sim would normally do this once it's grid-connected.static void build_station(City& c, int r, int col, int code, int size, bool powered) { for (int rr = r; rr < r + size; ++rr) for (int cc = col; cc < col + size; ++cc) { c.xbld.at(rr, cc) = 0; c.xzon.at(rr, cc) = 0; } CHECK(construct_building(c, r, col, code, size, 0)); // construct_building now stamps the optimistic powered bit 0x40 (faithful to the original's 0xE0 stamp, like // RCI growth). To model a genuinely UNPOWERED station — the state the monthly power flood leaves an // unconnected building in — clear 0x40 across the footprint; the powered case sets it explicitly. for (int rr = r; rr < r + size; ++rr) for (int cc = col; cc < col + size; ++cc) c.xbit.at(rr, cc) = std::uint8_t(powered ? (c.xbit.at(rr, cc) | 0x40) : (c.xbit.at(rr, cc) & ~0x40));}
static void fill_downtown(City& c, int r0, int c0, int n) { for (int r = r0; r < r0 + n; ++r) for (int col = c0; col < c0 + n; ++col) c.xbld.at(r, col) = std::uint8_t(0x70 + ((r * 3 + col * 5) & 0x1F));}static int run_to_quiescence(City& c, DisasterSim& d, MsRand& rng, int cap) { for (int t = 1; t <= cap; ++t) if (!disaster_tick(c, d, rng)) return t; return -1;}
int main() { // ───────────────────────── A. A built fire station produces XFIR coverage ───────────────────────── { City c; build_station(c, 62, 62, 0xD3, 3, /*powered=*/true); // 3×3 Fire Station at the map centre police_fire_coverage(c, /*police*/ 10000, /*fire*/ 10000, /*prisonBonus*/ 0); const int peak = max_field(c.xfir.cells); std::printf(" built fire station -> peak XFIR=%d (expect ~250)\n", peak); CHECK(peak > 0); // ★ THE integration check: a station must cover CHECK(std::all_of(c.xplc.cells.begin(), c.xplc.cells.end(), [](std::uint8_t value) { return value == 0; })); } // A built police station produces XPLC coverage AND measurably cuts crime under it. { City c; for (int r = 40; r < 80; ++r) for (int col = 40; col < 80; ++col) // a crime-prone district c.xbld.at(r, col) = 0xB0; // a developed code in 0x70..0xC5, kCrimeTable[0xB0]=4 (max) std::array<std::uint8_t, 64 * 64> lv{}, crmNo{}, crmYes{}; crime_step(c, lv, crmNo); // crime with NO police build_station(c, 58, 58, 0xD2, 3, true); // drop a Police station in the middle police_fire_coverage(c, 10000, 10000, 0); const int peak = max_field(c.xplc.cells); crime_step(c, lv, crmYes); // crime WITH police coverage long sumNo = 0, sumYes = 0; for (int i = 0; i < 64 * 64; ++i) { sumNo += crmNo[i]; sumYes += crmYes[i]; } std::printf(" built police station -> peak XPLC=%d, city crime %ld -> %ld\n", peak, sumNo, sumYes); CHECK(peak > 0); CHECK(sumYes < sumNo); // police demonstrably reduce crime } // Powered vs unpowered: an unconnected station still covers, but at ~half strength (the original halves it when XBIT 0x40 is clear). { City a, b; build_station(a, 62, 62, 0xD3, 3, true); build_station(b, 62, 62, 0xD3, 3, false); police_fire_coverage(a, 10000, 10000, 0); police_fire_coverage(b, 10000, 10000, 0); const int hi = max_field(a.xfir.cells), lo = max_field(b.xfir.cells); std::printf(" fire coverage powered=%d unpowered=%d\n", hi, lo); CHECK(lo > 0 && lo < hi); } // Funding scales coverage: 0% funding → no coverage (a station you refuse to fund is useless). { City c; build_station(c, 62, 62, 0xD3, 3, true); police_fire_coverage(c, 0, 0, 0); std::printf(" fire coverage @0%% funding=%d (expect 0)\n", max_field(c.xfir.cells)); CHECK(max_field(c.xfir.cells) == 0); }
// ───────────────────── B. End-to-end: a funded, powered fire station tames a fire ───────────────────── // Identical downtown + identical ignition seed; the ONLY difference is whether a fire station exists and // feeds XFIR through the real coverage pass. The station must shorten the blaze. { auto trial = [](bool withStation) { City c; DisasterSim d; MsRand rng(0xF1AE); fill_downtown(c, 40, 40, 24); // a 24×24 dense district if (withStation) { build_station(c, 50, 50, 0xD3, 3, true); // a fire house in the middle of it police_fire_coverage(c, 10000, 10000, 0); // the field pass that the live sim runs at tick 2 } for (int r = 46; r < 58; ++r) for (int col = 46; col < 58; ++col) c.xtxt.at(r, col) = fx::Fire; // 12×12 blaze d.active = true; d.type = 1; return run_to_quiescence(c, d, rng, 200000); }; const int withSt = trial(true); const int noSt = trial(false); std::printf(" ticks to extinguish a 12x12 blaze: with fire station=%d without=%d\n", withSt, noSt); CHECK(withSt > 0 && noSt > 0); CHECK(withSt < noSt); // ★ the service must actually help in the live chain }
// ───────────────── C. The LIVE path: Simulation::tick() drives the coverage pass from budget funding ───────────────── // This is the exact chain a player hits: the Simulation seeds funding from the city's budget records, and // phase_fields (tick%25==2) calls police_fire_coverage with that funding. If THIS breaks, a player's // stations produce no coverage no matter what — the failure mode behind "fires rage despite fire houses". { City c; c.set_budget_rate(5, 100); // police funding % (budget rec 5) c.set_budget_rate(6, 100); // fire funding % (budget rec 6) build_station(c, 62, 62, 0xD3, 3, true); // a powered fire house on clear land Simulation sim(c); for (int t = 0; t < 30; ++t) sim.tick(); // ≥1 full pass through tick 2 (phase_fields) const int peak = max_field(c.xfir.cells); // Expect ~125, NOT 250: phase_power (tick 1) runs before phase_fields (tick 2) and clears this lone // station's powered bit (it has no plant/lines), so coverage is HALVED — the faithful "unpowered // station = half strength" rule. A real gameplay lever: stations must be on the power grid for full // fire/police coverage. (>0 alone proves the funding→coverage wiring; the value documents the halving.) std::printf(" LIVE Simulation::tick -> peak XFIR from a built (un-gridded) station = %d (half; power it for 250)\n", peak); CHECK(peak > 0); // ★ the live funding→coverage wiring works }
// A persisted fire strike composes after 50% funding, so the live field receives 10% of nominal // coverage (then the ordinary unpowered-station halving), without affecting police efficiency. { City c; c.set_budget_rate(5, 100); c.set_budget_rate(6, 50); build_station(c, 62, 62, xbld::kFireStation, 3, true); sim::MunicipalDebt debt; sim::OperatingBudgetState operating; sim::ServiceDisruptionState disruptions; disruptions.randomSalt = 123; disruptions.lastProcessedPeriod = 0; disruptions.episodes[static_cast<std::size_t>( sim::ServiceDisruptionKind::Fire)] = { sim::ServiceDisruptionPhase::Active, sim::ServiceDisruptionCause::Underfunding, 0, 0}; Simulation simulation(c, debt, operating, disruptions); CHECK(simulation.public_safety_service_efficiency() == (std::array<std::int32_t, 2>{10000, 1000})); simulation.tick(); simulation.tick(); const int peak = max_field(c.xfir.cells); CHECK(peak > 0 && peak < 50); }
// ───────────────── D. Emergency dispatch DOES fight a disaster (the original's default case) ───────────────── // A deployed unit marker douses fire / disperses rioters on a random neighbour each disaster tick — the // manual's "the marker does the job", active only while a disaster runs (it's driven by disaster_tick, not // the unit's own nullsub handler). Identical seed/layout per pair; the only variable is the dispatched unit. { // Fire: a contained blaze with a hole at the centre (both trials); a FIRE unit (type 8) in that hole // must shorten the blaze vs no unit. auto fireTrial = [](bool dispatch) { City c; DisasterSim d; MsRand rng(0xD00D); fill_downtown(c, 40, 40, 16); for (int r = 46; r < 54; ++r) for (int col = 46; col < 54; ++col) c.xtxt.at(r, col) = fx::Fire; // 8×8 blaze c.xtxt.at(50, 50) = 0; // a hole for the unit (same in both trials) d.active = true; d.type = 1; if (dispatch) CHECK(spawn_deploy(c, 8, 50, 50) != 0); // a fire truck amid the flames return run_to_quiescence(c, d, rng, 200000); }; const int withUnit = fireTrial(true), noUnit = fireTrial(false); std::printf(" fire: ticks to extinguish dispatched-fire-unit=%d none=%d\n", withUnit, noUnit); CHECK(withUnit > 0 && noUnit > 0); CHECK(withUnit < noUnit); // ★ the fire unit demonstrably helps
// Riot: a street lined with rioters; a POLICE unit (type 7) among them must disperse them faster. auto riotTrial = [](bool dispatch) { City c; DisasterSim d; MsRand rng(0xC0FFEE); for (int cc = 30; cc < 98; ++cc) c.xbld.at(64, cc) = 0x1D; // an E–W street through the centre for (int cc = 40; cc < 60; ++cc) c.xtxt.at(64, cc) = fx::RiotNW; // a 20-tile line of rioters on it c.xtxt.at(64, 50) = 0; // a hole for the unit (same in both trials) d.active = true; d.type = 3; if (dispatch) CHECK(spawn_deploy(c, 7, 64, 50) != 0); // a police unit among the mob int steps = 0; for (; steps < 200000; ++steps) if (!disaster_tick(c, d, rng)) break; return steps; }; const int withCop = riotTrial(true), noCop = riotTrial(false); std::printf(" riot: ticks to disperse dispatched-police-unit=%d none=%d\n", withCop, noCop); CHECK(withCop > 0 && noCop > 0); CHECK(withCop < noCop); // ★ the police unit demonstrably helps
// Disaster-end cleanup: a deployed unit is removed when the disaster ends. City c; const int idx = spawn_deploy(c, 8, 40, 40); CHECK(idx != 0 && c.xtxt.at(40, 40) >= 0xC9); // marker placed clear_dispatch_units(c); CHECK(thing_field(c, idx, thg::Type) == 0); // Thing slot freed CHECK(c.xtxt.at(40, 40) == 0); // XTXT marker cleared }
if (failures == 0) std::printf("services: all checks passed\n"); else std::printf("services: %d CHECK(s) FAILED\n", failures); return failures ? 1 : 0;}