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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174// Tests for the military base: the grant flow + the growth-tick fill// (the nibble-7 branch of SimulationGrowthTick). See docs/ref/re/scout_scenario.md §D + the probe of// real cities (air/army 8×8 of 0xDD runways + 0xE2-0xF6 buildings; navy 0xE0/0xE3/0xF1/0xF2; missile 0xF9).#include "sc2k/sim/military.hpp"#include "sc2k/sim/growth_scan.hpp"#include "sc2k/sim/things.hpp"#include "sc2k/ui/tools.hpp" // bulldoze_protected / apply_tool (military land can't be bulldozed)#include <cstdio>
#include "test_harness.hpp"
using namespace sc2k;
// A flat, empty, uniform-altitude city (every tile buildable & same altitude).static City flat_city(int alt = 5) { City c; for (int t = 0; t < City::kTiles; ++t) { c.xter.cells[t] = 0; // flat land c.altm.cells[t] = std::uint16_t(alt); // uniform altitude c.xbld.cells[t] = 0; c.xzon.cells[t] = 0; c.xbit.cells[t] = 0; } return c;}
int main() { // 1. Grant on flat land → an 8×8 AIR FORCE base (all tiles same altitude), 64 military-zone tiles ---- { City c = flat_city(); MsRand rng(12345); sim::EventBuffer events; events.begin_batch(77); auto r = sim::propose_military_base(c, rng, nullptr, &events); CHECK(r.placed); CHECK(r.type == sim::MilitaryType::AirForce); // flat + uniform altitude ⇒ Air Force CHECK(c.military_base() == int(sim::MilitaryType::AirForce)); int zoned = 0, cleared = 0; for (int t = 0; t < City::kTiles; ++t) if ((c.xzon.cells[t] & 0xF) == 7) { ++zoned; if (c.xbld.cells[t] == 0) ++cleared; } CHECK(zoned == 64); // an 8×8 parcel CHECK(cleared == 64); // tiles cleared to fresh land for the buildings to grow CHECK(sim::has_military_base(c)); CHECK(sim::military_base_count(c) == 1); // Military dispatch now has a provider CHECK(events.view().size() == 1); CHECK(events.view()[0].stamp == (sim::EventStamp{77, sim::EventPhase::Session, 0})); const auto* resolved = std::get_if<sim::MilitaryBaseDecisionResolved>(&events.view()[0].payload); CHECK(resolved && resolved->outcome == sim::MilitaryBaseDecisionOutcome::Placed); CHECK(resolved && resolved->baseType == static_cast<std::uint8_t>(sim::MilitaryType::AirForce)); CHECK(resolved && resolved->centerRow == r.centerRow && resolved->centerColumn == r.centerCol); }
// 2. Grant with no room → declined/failed (no 8×8 buildable, no six 3×3 sites) --------------------- { City c = flat_city(); for (int t = 0; t < City::kTiles; ++t) c.xbld.cells[t] = 0x80; // every tile occupied → nothing buildable MsRand rng(7); sim::EventBuffer events; events.begin_batch(78); auto r = sim::propose_military_base(c, rng, nullptr, &events); CHECK(!r.placed); CHECK(r.type == sim::MilitaryType::Declined); CHECK(!sim::has_military_base(c)); CHECK(sim::military_base_count(c) == 0); CHECK(events.view().size() == 1); const auto* resolved = std::get_if<sim::MilitaryBaseDecisionResolved>(&events.view()[0].payload); CHECK(resolved && resolved->outcome == sim::MilitaryBaseDecisionOutcome::NoSuitableSite); CHECK(resolved && resolved->baseType == static_cast<std::uint8_t>(sim::MilitaryType::Declined)); CHECK(resolved && resolved->centerRow == -1 && resolved->centerColumn == -1); }
// 3. The growth tick develops military buildings on the zone (runways/hangars appear) -------------- { City c = flat_city(); MsRand rng(99); auto r = sim::propose_military_base(c, rng); CHECK(r.placed && r.type == sim::MilitaryType::AirForce); // Run many growth passes (each tile has a 1/4 gate); the zone should fill with air-force buildings. MsRand grng(0xABCD); ZonePops pops; for (int pass = 0; pass < 80; ++pass) for (int a = 0; a < 4; ++a) for (int b = 0; b < 4; ++b) growth_scan_region(c, a, b, grng, 0, pops); int runways = 0, buildings = 0, milTiles = 0; for (int t = 0; t < City::kTiles; ++t) { if ((c.xzon.cells[t] & 0xF) != 7) continue; ++milTiles; const int b = c.xbld.cells[t]; if (b == 0xDD) ++runways; if (b >= 0xDD && b <= 0xF6) ++buildings; // any air-force building code } CHECK(milTiles == 64); CHECK(runways > 0); // the 0xDD runway backbone got laid CHECK(buildings >= 40); // most of the parcel developed }
// 4. Missile (type 5) growth places 0xF9 silos ---------------------------------------------------- { City c = flat_city(); // Hand-stamp a 6×6 military zone and force MISSILE, then grow. for (int r = 10; r < 16; ++r) for (int col = 10; col < 16; ++col) { const int t = r * 128 + col; c.xzon.cells[t] = 7; c.xbld.cells[t] = 0; } c.set_military_base(int(sim::MilitaryType::Missile)); MsRand grng(0x1234); ZonePops pops; for (int pass = 0; pass < 120; ++pass) for (int a = 0; a < 4; ++a) for (int b = 0; b < 4; ++b) growth_scan_region(c, a, b, grng, 0, pops); int silos = 0; for (int t = 0; t < City::kTiles; ++t) if (c.xbld.cells[t] == 0xF9) ++silos; CHECK(silos > 0); // 0xF9 silos placed }
// 5. Military base tiles cannot be bulldozed (the original's bulldozer refuses XZON nibble 7) -------- { City c = flat_city(); MsRand rng(5); auto r = sim::propose_military_base(c, rng); CHECK(r.placed); int mr = -1, mc = -1; // find a military-zone tile for (int rr = 0; rr < 128 && mr < 0; ++rr) for (int cc = 0; cc < 128; ++cc) if ((c.xzon.at(rr, cc) & 0xF) == 7) { mr = rr; mc = cc; break; } CHECK(mr >= 0); CHECK(ui::bulldoze_protected(c, mr, mc)); // military land is protected CHECK(!ui::bulldoze_protected(c, 0, 0)); // ordinary empty land isn't c.set_funds(100000); c.xbld.at(mr, mc) = 0xDD; // a runway on the tile — the bulldozer must still refuse const auto res = ui::apply_tool(c, ui::Tool::Bulldoze, mr, mc); CHECK(!res.ok); CHECK(c.xbld.at(mr, mc) == 0xDD); // building survives; funds untouched CHECK(c.funds() == 100000); }
// 6. Declining commits once and emits only after the persisted outcome changes -------------------- { City c = flat_city(); CHECK(!sim::military_base_offer_pending(c)); c.set_city_status(4); CHECK(sim::military_base_offer_pending(c)); sim::EventBuffer events; events.begin_batch(79); CHECK(sim::decline_military_base(c, &events)); CHECK(c.military_base() == int(sim::MilitaryType::Declined)); CHECK(!sim::military_base_offer_pending(c)); CHECK(events.view().size() == 1); const auto* resolved = std::get_if<sim::MilitaryBaseDecisionResolved>(&events.view()[0].payload); CHECK(resolved && resolved->outcome == sim::MilitaryBaseDecisionOutcome::Declined); CHECK(resolved && resolved->baseType == static_cast<std::uint8_t>(sim::MilitaryType::Declined)); CHECK(!sim::decline_military_base(c, &events)); CHECK(events.view().size() == 1); }
if (failures == 0) std::printf("military: all checks passed\n"); else std::printf("military: %d CHECK(s) FAILED\n", failures); return failures ? 1 : 0;}