// End-to-end monthly finance boundaries through the fixed-step Simulation scheduler. #include "sc2k/sim/simulation.hpp" #include "test_harness.hpp" #include using namespace sc2k; static void place_service_footprint(City& city) { for (int row = 10; row < 13; ++row) for (int column = 10; column < 13; ++column) city.xbld.at(row, column) = 0xD2; } int main() { // The derived water-system value becomes a real monthly operating request and paid YTD total. { City city{MapSize::Tiny}; city.set_misc_be32(misc::kClockDays, 24); city.set_funds(100); for (int index = 0; index < 60; ++index) city.xbld.cells[static_cast(index)] = xbld::kWaterPump; sim::MunicipalDebt debt; sim::OperatingBudgetState operating; operating.waterFundingPercent = 150; Simulation simulation(city, debt, operating); simulation.set_finance_enabled(true); const Simulation::TickResult boundary = simulation.tick(); CHECK(!boundary.financeDecisionPending); CHECK(operating.waterOperatingValue == 120); CHECK(operating.waterPaidYearToDate == 15); CHECK(city.funds() == 85); CHECK(simulation.operating_funding_snapshot().efficiency_basis_points( sim::kWaterOperationsBudgetRecord) == 11852); } // A native Monthly session receives revenue at tick 0 and records history without annual accrual. { City city; city.set_misc_be32(misc::kClockDays, 24); city.set_funds(0); city.set_budget_rate(0, 75); city.set_budget_value(0, 120); sim::MunicipalDebt debt; sim::OperatingBudgetState operating; Simulation simulation(city, debt, operating); simulation.set_finance_enabled(true); const Simulation::TickResult boundary = simulation.tick(); CHECK(boundary.clock == 25 && !boundary.financeDecisionPending); CHECK(city.funds() == 10); CHECK(debt.lastProcessedPeriod == 1); CHECK(operating.lastProcessedPeriod == 1 && operating.coverageBasisPoints == 10000); CHECK(city.budget_revenue(0) == 0); CHECK(city.budget_value_hist(0, 1) == 120); CHECK(city.budget_rate_hist(0, 1) == 75); CHECK(boundary.events.size() == 2); CHECK(std::get_if(&boundary.events[0].payload)); const auto* settled = std::get_if( &boundary.events[1].payload); CHECK(settled && settled->revenueReceived == 10 && settled->fundsAfter == 10); } // A Shutdown proposal pauses at the boundary for any number of ticks, then commits on acceptance. { City city; city.set_misc_be32(misc::kClockDays, 24); city.set_funds(0); city.set_budget_rate(5, 100); place_service_footprint(city); sim::MunicipalDebt debt; sim::OperatingBudgetState operating; Simulation simulation(city, debt, operating); simulation.set_finance_enabled(true); const Simulation::TickResult boundary = simulation.tick(); CHECK(boundary.clock == 25 && boundary.financeDecisionPending); CHECK(city.funds() == 0 && !debt.lastProcessedPeriod); CHECK(operating.pendingDecisionPeriod == 1 && !operating.lastProcessedPeriod); CHECK(boundary.events.size() == 2); CHECK(std::get_if( &boundary.events[1].payload)); for (int attempt = 0; attempt < 20; ++attempt) { const Simulation::TickResult blocked = simulation.tick(); CHECK(blocked.clock == 25 && blocked.financeDecisionPending && blocked.events.empty()); } const auto accepted = simulation.accept_critical_operating_cuts(); CHECK(accepted.result == sim::MonthlyFinanceCommitResult::Committed); CHECK(city.funds() == 0 && debt.lastProcessedPeriod == 1); CHECK(operating.lastProcessedPeriod == 1 && !operating.pendingDecisionPeriod && operating.coverageBasisPoints == 0 && operating.consecutiveDistressMonths == 1); CHECK(operating.conversionRemainderUnits[5] == -4); CHECK(accepted.events.size() == 1); CHECK(std::get_if( &accepted.events[0].payload)); CHECK(simulation.tick().clock == 26); } // External funding during the pause is re-quoted and can clear the crisis without accepting cuts. { City city; city.set_misc_be32(misc::kClockDays, 24); city.set_funds(0); city.set_budget_rate(5, 100); place_service_footprint(city); sim::MunicipalDebt debt; sim::OperatingBudgetState operating; Simulation simulation(city, debt, operating); simulation.set_finance_enabled(true); CHECK(simulation.tick().financeDecisionPending); city.set_funds(100); const auto recovered = simulation.accept_critical_operating_cuts(); CHECK(recovered.result == sim::MonthlyFinanceCommitResult::Committed); CHECK(city.funds() == 92); CHECK(operating.coverageBasisPoints == 10000 && operating.consecutiveDistressMonths == 0 && !operating.pendingDecisionPeriod); } // A second consecutive shutdown settles, then offers a persisted operating rescue. { City city; city.set_misc_be32(misc::kClockDays, 24); city.set_funds(0); city.set_budget_rate(5, 100); place_service_footprint(city); sim::MunicipalDebt debt; sim::OperatingBudgetState operating; Simulation simulation(city, debt, operating); simulation.set_finance_enabled(true); CHECK(simulation.tick().financeDecisionPending); CHECK(simulation.accept_critical_operating_cuts().result == sim::MonthlyFinanceCommitResult::Committed); Simulation::TickResult second; for (int tick = 0; tick < 25; ++tick) second = simulation.tick(); CHECK(second.clock == 50 && second.financeDecisionPending); CHECK(operating.consecutiveDistressMonths == 2 && operating.lastProcessedPeriod == 2); CHECK(debt.support.pending && debt.support.pending->context == sim::BailoutContext::OperatingDistress && debt.support.pending->aggregatePaymentDue == 0 && debt.support.pending->runwayReserve == 10000 && debt.support.pending->injection == 10000); CHECK(second.events.size() == 3); CHECK(std::get_if( &second.events[1].payload)); CHECK(std::get_if( &second.events[2].payload)); const auto accepted = simulation.accept_government_support(); CHECK(accepted.result == sim::GovernmentSupportCommandResult::Committed); CHECK(city.funds() == 10000 && !debt.support.pending && debt.support.status == sim::FiscalStatus::GovernmentSupervision && debt.support.bailoutEligibleAgainPeriod == 62); CHECK(accepted.events.size() == 1); CHECK(std::get_if( &accepted.events[0].payload)); CHECK(simulation.tick().clock == 51); } // Sustained operating shutdown during active supervision ends the mayoral term. { City city; city.set_misc_be32(misc::kClockDays, 49); city.set_funds(0); city.set_budget_rate(5, 100); place_service_footprint(city); sim::MunicipalDebt debt; debt.lastProcessedPeriod = 1; debt.support.status = sim::FiscalStatus::GovernmentSupervision; debt.support.lastBailoutPeriod = 1; debt.support.bailoutEligibleAgainPeriod = 61; debt.support.lastBailoutAmount = 10000; sim::OperatingBudgetState operating; operating.lastProcessedPeriod = 1; operating.coverageBasisPoints = 0; operating.consecutiveDistressMonths = 1; Simulation simulation(city, debt, operating); simulation.set_finance_enabled(true); const Simulation::TickResult second = simulation.tick(); CHECK(second.clock == 50 && second.terminalOutcome && !second.financeDecisionPending); CHECK(debt.support.status == sim::FiscalStatus::Dismissed && debt.support.dismissalReason == sim::DismissalReason::PersistentOperatingCrisis); CHECK(simulation.city_outcome() == CityOutcome::PersistentOperatingCrisis); CHECK(second.events.size() == 4); CHECK(std::get_if(&second.events[2].payload)); const auto* outcome = std::get_if(&second.events[3].payload); CHECK(outcome && outcome->outcome == CityOutcome::PersistentOperatingCrisis); } // Compatibility constructors retain annual mode until their explicit January activation period. { City city; city.set_misc_be32(misc::kClockDays, 24); Simulation compatibility(city); CHECK(compatibility.operating_budget_state().mode == sim::OperatingBudgetMode::LegacyAnnual); CHECK(compatibility.operating_budget_state().activationPeriod == 12); } // The January cutover settles legacy accrual once, then processes January as a monthly period. { City city; city.set_misc_be32(misc::kClockDays, 299); city.set_funds(0); city.set_budget_rate(0, 75); city.set_budget_value(0, 120); city.set_budget_revenue(0, 9000); sim::MunicipalDebt debt; sim::OperatingBudgetState operating; operating.mode = sim::OperatingBudgetMode::LegacyAnnual; operating.activationPeriod = 12; Simulation simulation(city, debt, operating); simulation.set_finance_enabled(true); const Simulation::TickResult boundary = simulation.tick(); CHECK(boundary.clock == 300 && !boundary.financeDecisionPending); CHECK(city.funds() == 20); CHECK(operating.mode == sim::OperatingBudgetMode::Monthly && !operating.activationPeriod && operating.lastProcessedPeriod == 12); CHECK(city.budget_revenue(0) == 0); CHECK(boundary.events.size() == 4); CHECK(std::get_if(&boundary.events[0].payload)); CHECK(std::get_if(&boundary.events[1].payload)); const auto* annual = std::get_if(&boundary.events[2].payload); CHECK(annual && annual->fundsBefore == 0 && annual->fundsAfter == 10); const auto* monthly = std::get_if( &boundary.events[3].payload); CHECK(monthly && monthly->revenueReceived == 10 && monthly->fundsAfter == 20); } if (failures == 0) std::printf("monthly_finance_simulation: all checks passed\n"); else std::printf("monthly_finance_simulation: %d FAILED\n", failures); return failures ? 1 : 0; }