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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132// Pins the saved budget-record stride and validates the original's service-funding model.//// The finance pass writes, each budget tick, five service-funding records whose *value* field is a// pure function of the tile histogram (the original's tile counts == our TileCensus):// record 5 value = count[0xD2]/9 record 6 value = count[0xD3]/9// record 7 value = count[0xD1]/9 record 8 value = count[0xD6]/9// record 9 value = count[0xD9]/16// (the /9 and /16 convert 3x3 / 4x4 station tile-counts to building counts). Because the histogram is// deterministic from the saved XBLD layer, the saved record values must reproduce exactly — which also// lets us settle whether the saved record stride is 27 or 28 dwords (the live record is 28 dwords =// 112 bytes per the disasm; our accessor currently assumes a 27-dword save stride).#include "sc2k/sim/budget_categories.hpp"#include "sc2k/sim/municipal_finance.hpp"#include "sc2k/io/sc2_file.hpp"#include <cstdio>#include "test_paths.hpp"
#include "test_harness.hpp"
namespace {// record R value byte in the save = base(record-0 value) + strideBytes*R.int record_value(const sc2k::City& c, int rec, int strideDwords) { return c.misc_be32(sc2k::misc::kBudgetValue + 4 * strideDwords * rec);}// Thin wrapper so the byte-exact check exercises the shipped module function.int maint_expected(const sc2k::TileCensus& cen, int rec) { return sc2k::maintenance_value(cen, rec); }} // namespace
static void check_city(const char* name) { using namespace sc2k; std::string path = (cities_dir()) + name; City c; auto r = io::load_sc2(path, c); if (!r.ok) { std::printf(" %-13s skipped (%s)\n", name, r.error.c_str()); return; }
TileCensus cen; cen.recount(c);
int fund = 0; for (int slot = 0; slot < 5; ++slot) { int expect = service_funding_value(cen, slot); // shipped module function CHECK(record_value(c, kServiceFundRecord[slot], 27) == expect); // byte-exact at the pinned stride fund += (record_value(c, kServiceFundRecord[slot], 27) == expect); } // Maintenance records 11/12/13/15 reproduce byte-exact everywhere. Record 10 reproduces byte-exact // in every real city (incl. ones with 100s–1000s of code-0x23..0x26 road-variant tiles); BIGGESTC, // a synthetic max-grid city, is low by exactly its 4 code-0x23..0x26 tiles, whose saved dwTileCount // is stale vs the XBLD layer — a per-city save artifact (cf. PARIS water), not a formula error (the // rec10 ranges are instruction-level verified). We assert that BIGGESTC's only divergence is exactly // those tiles. int maint = 0; for (int rec : {11, 12, 13, 15}) { CHECK(record_value(c, rec, 27) == maint_expected(cen, rec)); maint += (record_value(c, rec, 27) == maint_expected(cen, rec)); } { int got = record_value(c, 10, 27), exp = maint_expected(cen, 10); int stale = cen.count[0x23] + cen.count[0x24] + cen.count[0x25] + cen.count[0x26]; if (got == exp) ++maint; // real cities: byte-exact else { CHECK(exp - got == stale && stale <= 4); // BIGGESTC: divergence == its 0x23..0x26 tiles std::printf(" rec10 stale-counter artifact: saved=%d recount=%d (=%d stale road-variant tiles)\n", got, exp, stale); } } std::printf(" %-13s funding %d/5 maintenance %d/5 byte-exact\n", name, fund, maint);}
// Known-answer test for operating year-end settlement and the derived legacy-debt Budget row.static void check_year_end() { using namespace sc2k; // (1) An income category (divisor +75): value 1000 @ rate 9, accrued over 12 months, applies as // 12·9·1000 / (12·75) = 108000/900 = +120 to the treasury. { std::array<BudgetRecord, 16> recs{}; recs[0].value = 1000; recs[0].rate = 9; for (int m = 0; m < 12; ++m) accrue_record(recs[0], m); CHECK(recs[0].revenue == 12L * 9 * 1000); CHECK(recs[0].valueHist[5] == 1000 && recs[0].rateHist[11] == 9); long funds = 0; apply_year_end(recs, funds); CHECK(funds == 120); CHECK(recs[0].revenue == 0); // revenue cleared after applying } // (2) Five imported 8% bonds derive the compatibility row exactly, but generic Budget // settlement cannot charge it; scheduled municipal-finance servicing owns that payment. { sim::MunicipalDebt debt; debt.nextId = 6; for (int i = 0; i < 5; ++i) { sim::DebtInstrument instrument; instrument.id = static_cast<sim::DebtId>(i + 1); instrument.kind = sim::DebtKind::LegacyInterestOnly; instrument.origin = sim::DebtOrigin::ClassicImport; instrument.originalPrincipal = 10000; instrument.remainingPrincipal = 10000; instrument.annualRate = 800; instrument.nextDuePeriod = 12; instrument.frequency = sim::PaymentFrequency::Annual; debt.instruments.push_back(instrument); } const auto projection = sim::project_legacy_debt_budget(debt); CHECK(projection); std::array<BudgetRecord, 16> recs{}; recs[4].value = projection ? projection->instrumentCount : 0; recs[4].rate = projection ? projection->aggregateRate : 0; for (int m = 0; m < 12; ++m) accrue_record(recs[4], m); CHECK(recs[4].revenue == 12L * 80000 * 5); long funds = 0; apply_year_end(recs, funds); CHECK(funds == 0); CHECK(recs[4].revenue == 0); }}
int main() { for (const char* n : {"TOKYO.SC2", "BAYVIEW.SC2", "LONDON.SC2", "NYC.SC2", "PARIS.SC2", "BIGGESTC.SC2"}) check_city(n); check_year_end(); if (failures == 0) { std::printf("budget_funding: all checks passed\n"); return 0; } std::printf("budget_funding: %d failure(s)\n", failures); return 1;}