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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216// Integration for the Budget window + annual popup wiring (the sim↔city budget sync and the year-end// event the app consumes to open the modal budget dialog).//// 1. WRITEBACK: with finance+growth+demand live, the sim publishes its 16 budget records back into the// city each month (in the original the live array IS the saved structure). So the Budget window — which reads// city.budget_* via ui::budget_values — shows real numbers, not the stale all-zero seed.// 2. YEAR-END EVENT: consume_year_end_event() fires exactly at the Dec→Jan rollover, and showBudgetDialog// tracks Auto-Budget (off ⇒ the annual popup opens; on ⇒ it's suppressed — as the original).#include "sc2k/sim/simulation.hpp"#include "sc2k/sim/workforce.hpp"#include "sc2k/ui/data_views.hpp"#include "sc2k/io/sc2_file.hpp"#include <algorithm>#include <cstdio>#include <cstdlib>#include "test_paths.hpp"
using namespace sc2k;
#include "test_harness.hpp"
// Run one city for `months`, returning how many year-end events showed the budget popup and how many// fired at all. Also reports whether the live Budget-window numbers came off zero.static void run_city(const char* name, bool autoBudget) { City c; std::string path = (cities_dir()) + name; auto r = io::load_sc2(path, c); if (!r.ok) { std::printf(" %-12s skipped (%s)\n", name, r.error.c_str()); return; } c.set_auto_budget(autoBudget);
Simulation sim(c); sim.set_finance_enabled(true); sim.set_growth_enabled(true); sim.set_demand_enabled(true);
int events = 0, popups = 0; const long f0 = c.funds(); for (int i = 0; i < 25 * 14; ++i) { // > 1 fiscal year sim.tick(); auto ye = sim.consume_year_end_event(); if (ye.occurred) { ++events; if (ye.showBudgetDialog) ++popups; } }
// The live records reached the city: the Budget window now has non-zero figures somewhere (a built-out // city has both a tax base and infrastructure upkeep). Sample a handful of record fields + the dialog map. long recSum = 0; for (int rec = 0; rec < 16; ++rec) recSum += std::llabs(c.budget_value(rec)) + std::llabs(c.budget_revenue(rec)); auto vals = ui::budget_values(c, sim.year(), sim.month(), c.name.empty() ? "New City" : c.name); const bool dialogNonZero = vals.count(133) && vals[133] != "0\n0\n0";
std::printf(" %-12s auto-budget %s: year-end events=%d popups=%d recSum=%ld funds %ld->%ld dlg[133]=\"%s\"\n", name, autoBudget ? "ON " : "off", events, popups, recSum, f0, static_cast<long>(c.funds()), vals.count(133) ? vals[133].c_str() : "(none)");
CHECK(events >= 1); // a fiscal year rolled over CHECK(popups == (autoBudget ? 0 : events)); // popup shown iff Auto-Budget is off CHECK(recSum > 0); // live records reached the city (not the zero seed) CHECK(dialogNonZero); // the Budget window shows real numbers}
// Budget-window rate spinners (tax % / service funding %) adjust the live budget rates.static void run_spinners() { City c; if (!io::load_sc2((city_file("TOKYO.SC2")), c).ok) return; Simulation sim(c);
// Property tax spans recs 0-2 (±1, 0-20); service policy uses the shared ±5, 0-150 contract. ui::BudgetRateSpin s; CHECK(ui::budget_rate_spin(440, s) && s.count == 3 && s.step == 1 && s.hi == 20); // Property Taxes CHECK(ui::budget_rate_spin(441, s) && s.count == 1 && s.records[0] == 5 && s.step == 5 && s.hi == 150); // Police CHECK(ui::budget_rate_spin(443, s) && s.count == 2 && s.records[1] == 9); // Education = recs 8+9 CHECK(ui::budget_rate_spin(444, s) && s.count == 6 && s.records[5] == 15); // Transit = recs 10-15 CHECK(ui::budget_rate_spin(446, s) && s.count == 4 && s.records[0] == 10 && s.records[3] == 15); // Roads CHECK(ui::budget_rate_spin(447, s) && s.count == 2 && s.records[0] == 13 && s.records[1] == 14); // Mass Transit CHECK(!ui::budget_rate_spin(999, s)); // not a rate scrollbar
// Raise property tax by one step on each of recs 0-2 (clamped 0-20), like clicking the up half. ui::budget_rate_spin(440, s); int before[3] = {sim.budget_rate(0), sim.budget_rate(1), sim.budget_rate(2)}; for (int k = 0; k < s.count; ++k) sim.set_budget_rate(s.records[k], std::min(sim.budget_rate(s.records[k]) + s.step, s.hi)); for (int r = 0; r < 3; ++r) { CHECK(sim.budget_rate(r) == std::min(before[r] + 1, 20)); CHECK(c.budget_rate(r) == sim.budget_rate(r)); // published to the city → dialog reflects it } // Lower police funding one step (rec 5, ±5, floor 0). ui::budget_rate_spin(441, s); int pol = sim.budget_rate(5); sim.set_budget_rate(5, std::max(pol - s.step, s.lo)); CHECK(sim.budget_rate(5) == std::max(pol - 5, 0) && c.budget_rate(5) == sim.budget_rate(5)); std::printf(" spinners: tax %d->%d, police %d->%d OK\n", before[0], sim.budget_rate(0), pol, sim.budget_rate(5));
// Sub-budget window spinners map (dialog id, scrollbar id) → the single record + step/range they drive. int rec, step, lo, hi; CHECK(ui::subbudget_rate_spin(120, 442, rec, step, lo, hi) && rec == 0 && step == 1 && hi == 20); // Taxes: Res CHECK(ui::subbudget_rate_spin(120, 443, rec, step, lo, hi) && rec == 1); // Taxes: Com CHECK(ui::subbudget_rate_spin(120, 444, rec, step, lo, hi) && rec == 2); // Taxes: Ind CHECK(!ui::subbudget_rate_spin(125, 447, rec, step, lo, hi)); // Education is consolidated CHECK(!ui::subbudget_rate_spin(126, 443, rec, step, lo, hi)); // Transit is consolidated CHECK(!ui::subbudget_rate_spin(184, 440, rec, step, lo, hi)); // service window: view-only // Consolidated Education adjusts both records together. ui::budget_rate_spin(443, s); const int sch0 = sim.budget_rate(8), col0 = sim.budget_rate(9); for (int k = 0; k < s.count; ++k) sim.set_budget_rate(s.records[k], std::max(sim.budget_rate(s.records[k]) - s.step, s.lo)); CHECK(sim.budget_rate(8) == std::max(sch0 - 5, 0)); CHECK(sim.budget_rate(9) == std::max(col0 - 5, 0));}
// Imported legacy debt accrues into record 4 and is paid by canonical scheduled servicing.static void run_bonds() { City c; if (!io::load_sc2((city_file("TOKYO.SC2")), c).ok) return; // Tick a full fiscal year (crossing one Dec→Jan) with finance on; returns the net treasury movement. // Takes City& so the record state is observable afterwards. auto run_year = [](City& city) { Simulation sim(city); sim.set_finance_enabled(true); const long f0 = city.funds(); for (int i = 0; i < 25 * 13; ++i) sim.tick(); return city.funds() - f0; }; City base = c, bonded = c; bonded.set_bond_count(3); // 3 bonds at 7% → rateSum 21 → −$100·21 ≈ −$2100/yr for (int i = 0; i < 3; ++i) bonded.set_bond_rate(i, 7);
const long dBase = run_year(base), dBonded = run_year(bonded), extra = dBonded - dBase; std::printf(" bonds: base year=%ld, with 3x7%% bonds=%ld (extra=%ld), rec4 value=%d rate=%d\n", dBase, dBonded, extra, bonded.budget_value(4), bonded.budget_rate(4)); CHECK(bonded.budget_value(4) == 3); // rec 4 value = bond count, as the original CHECK(bonded.budget_rate(4) == 70000); // 10000·rateSum/count = 10000·21/3 CHECK(extra < 0); // bonds drain the treasury CHECK(extra >= -2300 && extra <= -1500); // ≈ −$100·rateSum/yr (partial-year accrual slack)}
// Bankruptcy: a treasury below −$100,000 latches game-over for ANY city (the original's tick// case 22 FailRadio(2) → sc2k ScenarioResult::LoseBankrupt), even a plain non-scenario .SC2.static void run_bankruptcy() { City c; if (!io::load_sc2((city_file("TOKYO.SC2")), c).ok) return; c.set_funds(-200000); // deep in the red Simulation sim(c); // plain city (no scenario, no finance drift) for (int i = 0; i < 25; ++i) sim.tick(); // reach the tick-22 check at least once std::printf(" bankruptcy: funds=%ld → result=%d (3=LoseBankrupt)\n", static_cast<long>(c.funds()), static_cast<int>(sim.city_outcome())); CHECK(sim.city_outcome() == CityOutcome::Insolvent);}
// Education funding has a corresponding effect: the school/college capacity cap (funding·count, as the original)// bounds the +35 EQ boost, so fully-funded education yields a higher Education Quotient than zero-funded.static void run_education_effect() { auto eq_after = [](int eduFund) -> int { City c; if (!io::load_sc2((city_file("TOKYO.SC2")), c).ok) return -1; c.set_budget_rate(8, eduFund); c.set_budget_rate(9, eduFund); Simulation sim(c); sim.set_growth_enabled(true); sim.set_finance_enabled(true); for (int i = 0; i < 25 * 12 * 8; ++i) sim.tick(); // 8 game years return compute_workforce(c).education_quotient; }; const int full = eq_after(100), none = eq_after(0); if (full < 0) return; // city not present → skip std::printf(" education: EQ after 8yr — funded 100%%=%d, 0%%=%d (delta %d)\n", full, none, full - none); CHECK(full > none); // funding education raises the Education Quotient}
// Transit/road funding has a live effect: fully-funded roads never decay, under-funded ones crumble to rubble// (the original's nibble-0 growth-scan decay). Records 10/11/13 (roads/power/rail).static void run_infra_decay() { auto roads = [](const City& c) { int n = 0; for (auto b : c.xbld.cells) if (b >= 29 && b < 44) ++n; return n; }; auto roads_after = [&](int fund) -> int { City c; if (!io::load_sc2((city_file("TOKYO.SC2")), c).ok) return -1; c.set_budget_rate(10, fund); Simulation sim(c); sim.set_growth_enabled(true); sim.set_finance_enabled(true); for (int i = 0; i < 25 * 12 * 12; ++i) sim.tick(); // 12 game years return roads(c); }; City base; if (!io::load_sc2((city_file("TOKYO.SC2")), base).ok) return; const int r0 = roads(base), funded = roads_after(100), starved = roads_after(0); std::printf(" infra decay: TOKYO roads %d → funded-100%%=%d, 0%%=%d\n", r0, funded, starved); CHECK(funded == r0); // 100% funding: nothing decays CHECK(starved < r0); // 0% funding: roads crumble}
int main() { run_city("TOKYO.SC2", false); // Auto-Budget off → annual popup every year run_city("TOKYO.SC2", true); // Auto-Budget on → suppressed run_city("NYC.SC2", false); run_spinners(); run_bonds(); run_bankruptcy(); run_education_effect(); run_infra_decay();
if (failures == 0) { std::printf("budget_dialog: all checks passed\n"); return 0; } std::printf("budget_dialog: %d failure(s)\n", failures); return 1;}