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.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586// Power-plant aging (sim/plant_aging.hpp): conventional plants gain a year in their microsim// record's [+1] byte (the original's, so ages round-trip through real saves), warn at 49-50, and// past 50 are rebuilt under the auto flag or demolished with the slot freed. Hydro and wind never age.#include "sc2k/sim/plant_aging.hpp"#include "sc2k/sim/terrain_gen.hpp"#include "sc2k/ui/tools.hpp" // the shared destroyer's body (sim::demolish_at lives in tools_demolish.hpp)#include <cstdio>
#include "test_harness.hpp"
using namespace sc2k;
// Stamp a 4x4 plant of `code` at (r,c) and provision its microsim record. Returns the XMIC index.static int place_plant(City& c, int r, int col, std::uint8_t code, const char* name) { for (int dr = 0; dr < 4; ++dr) for (int dc = 0; dc < 4; ++dc) c.xbld.at(r + dr, col + dc) = code; const int slot = sim::provision_label(c, r, col, name); return slot - 51;}
int main() { City c = make_new_city(0x4242u, 0, 1, 20, 20, 20, 1, 1); const int mi = place_plant(c, 40, 40, xbld::kCoalPlant, "Old Smoky"); CHECK(mi >= 10 && c.xmic[std::size_t(mi) * 8] == xbld::kCoalPlant);
// Years 1..48: silent aging, one year per tick. for (int y = 1; y <= 48; ++y) { const auto ev = sim::plant_aging_tick(c, false); CHECK(ev.empty()); CHECK(int(c.xmic[std::size_t(mi) * 8 + 1]) == y); }
// Years 49 and 50: the warning (the original's story-36 bulletin), plant untouched. for (int y = 49; y <= 50; ++y) { const auto ev = sim::plant_aging_tick(c, false); CHECK(ev.size() == 1 && ev[0].kind == sim::PlantEvent::Warning); CHECK(ev[0].age == y && ev[0].xbld == xbld::kCoalPlant); CHECK(c.xbld.at(40, 40) == xbld::kCoalPlant); }
// Year 51, no auto-rebuild: the plant dies — razed, label cells cleared, record freed. { const auto ev = sim::plant_aging_tick(c, false); CHECK(ev.size() == 1 && ev[0].kind == sim::PlantEvent::Died); CHECK(c.xbld.at(40, 40) != xbld::kCoalPlant); CHECK(c.xtxt.at(40, 40) == 0); CHECK(c.xmic[std::size_t(mi) * 8] == 0); // No zombie events after death. CHECK(sim::plant_aging_tick(c, false).empty()); }
// The rebuild path: at 50, with the auto flag and the funds, the plant is rebuilt in place. { City c2 = make_new_city(0x777u, 0, 1, 20, 20, 20, 1, 1); const int m2 = place_plant(c2, 60, 60, xbld::kNuclearPlant, "Atomville"); c2.xmic[std::size_t(m2) * 8 + 1] = 50; // one year from the brink c2.set_funds(20000); const auto ev = sim::plant_aging_tick(c2, true); CHECK(ev.size() == 1 && ev[0].kind == sim::PlantEvent::Rebuilt); CHECK(ev[0].cost == 15000 && c2.funds() == 5000); // the Nuclear price deducted CHECK(c2.xmic[std::size_t(m2) * 8 + 1] == 0); // good as new CHECK(c2.xbld.at(60, 60) == xbld::kNuclearPlant); // still standing // …but with the treasury short, the same brink is fatal. c2.xmic[std::size_t(m2) * 8 + 1] = 50; c2.set_funds(1000); const auto ev2 = sim::plant_aging_tick(c2, true); CHECK(ev2.size() == 1 && ev2[0].kind == sim::PlantEvent::Died); CHECK(c2.funds() == 1000); // no partial charge }
// Hydro and wind are exempt: their age byte never moves. { City c3 = make_new_city(0x888u, 0, 1, 20, 20, 20, 1, 1); const int m3 = place_plant(c3, 80, 80, xbld::kHydroPlant1, "Falls"); for (int y = 0; y < 60; ++y) CHECK(sim::plant_aging_tick(c3, false).empty()); CHECK(c3.xmic[std::size_t(m3) * 8 + 1] == 0); }
if (failures == 0) { std::printf("plant aging: all checks passed\n"); return 0; } std::printf("plant aging: %d FAILURES\n", failures); return 1;}