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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109// Dynamic reactivity of the utility grids.//// test_power.cpp proves the flood matches the saved bits for a *static* city, and test_sim_tick.cpp// proves a stable city drifts 0 bits per tick. Neither proves the grid REACTS when its sources change// — yet that reaction (cut the supply → buildings go dark → the no-power bolt appears; pumps lose power// → the city dries up) is the whole point of the network sim, and was the symptom behind "I destroyed// power stations and nothing reacted". This test razes the sources the way the bulldozer does and// asserts the grids collapse the way the live game now recomputes them on every edit.//// The no-power badge the renderer draws keys on (XBIT & 0xC0) == 0x80 — conductive AND un-powered — so// a correct collapse must drive that count UP (every still-conductive building wants power and has none).#include "sc2k/sim/power.hpp"#include "sc2k/sim/water.hpp"#include "sc2k/io/sc2_file.hpp"#include <cstdio>#include <string>#include "test_paths.hpp"
#include "test_harness.hpp"
using namespace sc2k;
struct Counts { int powered = 0, badge = 0, watered = 0, plants = 0, pumps = 0;};static Counts count(const City& c) { Counts n; for (int t = 0; t < City::kTiles; ++t) { const std::uint8_t b = c.xbit.cells[t], x = c.xbld.cells[t]; if (b & 0x40) ++n.powered; if ((b & 0xC0) == 0x80) ++n.badge; // conductive + UN-powered = the lightning-bolt badge if (b & 0x10) ++n.watered; if (is_power_plant(x)) ++n.plants; if (x == 0xDC || x == 0xFA) ++n.pumps; // pump / desalination = the powered water sources } return n;}
// Raze every tile matching `pred` exactly as sim::clear_structure / the bulldozer does: rubble + drop// the high XBIT bits (conductive / powered), via the same &= 0x3D mask the demolish path uses.template <class Pred>static void raze(City& c, Pred pred) { for (int t = 0; t < City::kTiles; ++t) if (pred(c.xbld.cells[t])) { c.xbld.cells[t] = 1; // Rubble 1 c.xbit.cells[t] = std::uint8_t(c.xbit.cells[t] & 0x3D); // clear conductive (0x80) / powered (0x40) }}
static void check_city(const char* name) { std::string path = (cities_dir()) + name; City c; auto r = io::load_sc2(path, c); if (!r.ok) { std::printf(" %-12s skipped (%s)\n", name, r.error.c_str()); return; }
update_power(c); update_water(c); // live baseline const Counts base = count(c); std::printf(" %-12s baseline: powered=%-6d badge=%-5d watered=%-6d (plants=%d pumps=%d)\n", name, base.powered, base.badge, base.watered, base.plants, base.pumps); if (base.plants == 0 || base.powered == 0) return; // nothing to test on a plantless city
// 1) Raze every power plant → the grid must go fully dark, every still-conductive building must now // want power (badge jumps), and water cascades off power (pumps need it) so it collapses too. { City np = c; raze(np, [](std::uint8_t x) { return is_power_plant(x); }); update_power(np); update_water(np); const Counts a = count(np); std::printf(" %-12s no plants: powered=%-6d badge=%-5d watered=%-6d\n", name, a.powered, a.badge, a.watered); CHECK(a.powered == 0); // no source → nothing powered CHECK(a.badge > base.badge + 100); // the bolt now flags the dark buildings CHECK(a.badge >= base.powered - base.plants - 50); // ~every formerly-powered building wants power now CHECK(a.watered == 0); // pumps unpowered → water network collapses }
// 2) Raze every water source → the city dries out. (We pull pumps AND desalination, so no supply can // remain.) Pump tiles are themselves conductive — the original's saves carry XBIT 0x80 on them, so // they conduct as well as consume — hence razing them also dims the power grid downstream; that // coupling is faithful, so we only assert the water side here. if (base.pumps > 0 && base.watered > 0) { City nw = c; raze(nw, [](std::uint8_t x) { return x == 0xDC || x == 0xFA; }); update_power(nw); update_water(nw); const Counts d = count(nw); std::printf(" %-12s no pumps: powered=%-6d badge=%-5d watered=%-6d\n", name, d.powered, d.badge, d.watered); CHECK(d.watered == 0); // no source left → nothing watered CHECK(d.watered < base.watered); // …and it strictly dropped }}
int main() { const char* cities[] = {"TOKYO.SC2", "PARIS.SC2", "NYC.SC2", "LONDON.SC2", "BIGGESTC.SC2"}; for (const char* n : cities) check_city(n); if (failures == 0) { std::printf("power_react: all checks passed\n"); return 0; } std::printf("power_react: %d failure(s)\n", failures); return 1;}