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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138// Read-out dialog data-binding KAT (ui/data_views.hpp): the Budget (102) and Ordinances (119) value// maps reproduce the original's owner-draw fills. We don't eyeball renders — we check the numeric// invariants of the binding against a real saved city (the project rule: verify numerically). Skips if// the assets / cities aren't present.#include "sc2k/io/sc2_file.hpp"#include "sc2k/render/asset_loader.hpp"#include "sc2k/ui/data_views.hpp"#include <cstdio>#include <fstream>#include <string>
#include "test_harness.hpp"#include "test_paths.hpp"
// Values are locale-formatted money; retain only the sign and digits before parsing.static int parse_grouped(const std::string& s) { std::string t; for (char c : s) if ((c >= '0' && c <= '9') || c == '-') t += c; return std::atoi(t.c_str());}static int geti(const std::map<int, std::string>& m, int id) { auto it = m.find(id); return it == m.end() ? 0 : parse_grouped(it->second);}static bool has(const std::map<int, std::string>& m, int id) { return m.count(id) != 0; }static std::string nth_line(const std::string& s, int n) { // 0-based line of a \n-joined value std::size_t a = 0; for (int i = 0; i < n; ++i) { a = s.find('\n', a); if (a == std::string::npos) return ""; ++a; } const std::size_t b = s.find('\n', a); return s.substr(a, b == std::string::npos ? b : b - a);}
int main() { using namespace sc2k; CHECK(ui::demand_bar_geometry(2000).top == 1 && ui::demand_bar_geometry(2000).height == 18); CHECK(ui::demand_bar_geometry(-2000).top == 20 && ui::demand_bar_geometry(-2000).height == 18); CHECK(ui::demand_bar_geometry(0).top == 19 && ui::demand_bar_geometry(0).height == 0); const std::string city_path = city_file("LONDON.SC2"); if (!exists(city_path)) { std::printf("data_views: SKIP (need CITIES/LONDON.SC2)\n"); return 0; }
City city; if (const auto lr = io::load_sc2(city_path, city); !lr.ok) { std::printf("data_views: load failed: %s\n", lr.error.c_str()); return 1; } // ── Budget (102) ──────────────────────────────────────────────────────────────────────────── { const auto v = ui::budget_values(city, 1950, 5, "London"); const auto summary = ui::budget_summary(city, 1950, 5, "London"); CHECK(summary.rows.size() == 8); CHECK(summary.incomeRows.size() == 4 && summary.expenditureRows.size() == 9); CHECK(summary.compactHeader == i18n::trf("dialog.budget.compact_header", {{"mon", i18n::month_short(5)}, {"year", "1950"}})); CHECK(std::string(summary.incomeRows[0].labelKey) == "dialog.budget.residential_tax"); CHECK(std::string(summary.incomeRows[3].labelKey) == "dialog.budget.ordinances"); CHECK(std::string(summary.expenditureRows[0].labelKey) == "dialog.budget.bonds"); CHECK(std::string(summary.expenditureRows[1].labelKey) == "dialog.budget.ordinances"); CHECK(std::string(summary.expenditureRows[5].labelKey) == "dialog.budget.education"); CHECK(std::string(summary.expenditureRows[6].labelKey) == "dialog.budget.roads"); CHECK(std::string(summary.expenditureRows[7].labelKey) == "dialog.budget.mass_transit"); CHECK(std::string(summary.expenditureRows[8].labelKey) == "dialog.budget.water"); CHECK(summary.incomeRows[0].rate == city.budget_rate(0)); CHECK(summary.incomeRows[1].rate == city.budget_rate(1)); CHECK(summary.incomeRows[2].rate == city.budget_rate(2)); CHECK(std::string(summary.rows[0].labelKey) == "dialog.budget.property" && summary.rows[0].adjustable); CHECK(std::string(summary.rows[1].labelKey) == "dialog.budget.ordinances" && !summary.rows[1].adjustable); CHECK(summary.rows[0].toDate == v.at(200) && summary.rows[7].yearEnd == v.at(217)); CHECK(summary.funds == nth_line(v.at(133), 2) && summary.endOfYear == v.at(132)); // Every row's two columns + the summary controls are present. for (int id : {101, 305, 306, 200, 207, 210, 217, 102, 104, 108, 132, 133}) CHECK(has(v, id)); const int funds = city.funds(); // Summary box line 3 = current funds; End-of-Year (132) = funds + estimated cashflow (line 2). CHECK(parse_grouped(nth_line(v.at(133), 2)) == funds); const int est = parse_grouped(nth_line(v.at(133), 1)); CHECK(geti(v, 132) == funds + est); CHECK(summary.endOfYearValue == funds + est); const auto forecast = sim::forecast_budget_cashflow( sim::gather_operating_forecast_input(city, 5)); CHECK(forecast && summary.estimatedCashflowValue == forecast.value.net); // Property ToDate (200) folds records 0-2 exactly (the binary's grouping). static const int kDiv[16] = {75, 75, 75, 75, -100, -1, -1, -2, -4, -1, -1000, -500, -400, -250, -250, -250}; int pTo = 0; for (int r = 0; r < 3; ++r) pTo += city.budget_revenue(r) / (12 * kDiv[r]); CHECK(geti(v, 200) == pTo); // YTD cashflow (133 line 1) = (Σ_16 rev/div)/12 — the per-row/summary consistency. int ytdSum = 0; for (int r = 0; r < 16; ++r) ytdSum += city.budget_revenue(r) / kDiv[r]; CHECK(parse_grouped(nth_line(v.at(133), 0)) == ytdSum / 12); std::printf("data_views: Budget London — funds %d, EOY %d, YTD cashflow %s\n", funds, geti(v, 132), nth_line(v.at(133), 0).c_str()); }
// ── Compact finance-detail model (Taxes; Education and Transit are consolidated above) ── { const auto taxes = ui::finance_detail(city, 120, 5); CHECK(taxes.rowCount == 3); CHECK(taxes.funding[0].record == 0 && taxes.funding[0].step == 1 && taxes.funding[0].hi == 20); CHECK(taxes.rows[0].toDate.find('$') != std::string::npos && taxes.rows[0].yearEnd.find('$') != std::string::npos); CHECK(ui::finance_detail(city, 125, 5).rowCount == 0); CHECK(ui::finance_detail(city, 126, 5).rowCount == 0); const auto retired = ui::finance_detail(city, 184, 5); CHECK(retired.rowCount == 0); }
// ── Ordinances (119): semantic model, independent of retired dialog geometry ─────────────── { const auto summary = ui::ordinance_summary(city); CHECK(std::string(summary.categories[0].labelKey) == "ord.group.finance"); CHECK(std::string(summary.categories[4].items[1].labelKey) == "ord.nuclear_free"); CHECK(summary.categories[0].items[0].enabled == ((city.ordinances() & 1u) != 0)); const std::uint32_t ord = city.ordinances(); const int r10 = city.budget_value(0), c10 = city.budget_value(1), i10 = city.budget_value(2); int cat[5] = {0, 0, 0, 0, 0}; for (int bit = 0; bit < 20; ++bit) if (ord & (1u << bit)) cat[bit / 4] += ordinance_cost(bit, r10, c10, i10, r10, 0); for (int category = 0; category < 5; ++category) CHECK(summary.categories[category].monthlyTotal == cat[category] / 75); int monthlyNet = 0; for (const auto& category : summary.categories) monthlyNet += category.monthlyTotal; CHECK(std::abs(summary.monthlyNet - monthlyNet) <= 4); // per-category integer division may differ by <1 each std::printf("data_views: Ordinances London — Finance %d, net %d\n", cat[0] / 75, summary.monthlyNet); }
if (failures == 0) { std::printf("data_views: all checks passed\n"); return 0; } std::printf("data_views: %d failure(s)\n", failures); return 1;}