// Presentation-free monthly city observation: numeric gathering, history boundaries, and cold rebuild. #include "sc2k/sim/city_observer.hpp" #include #include "test_harness.hpp" using namespace sc2k; static unsigned observed_city_hash(const City& city) { unsigned hash = 2166136261u; const auto add = [&](std::uint8_t value) { hash = (hash ^ value) * 16777619u; }; for (const std::uint8_t value : city.xbld.cells) add(value); for (const std::uint8_t value : city.xbit.cells) add(value); for (const std::uint8_t value : city.xzon.cells) add(value); for (const std::uint8_t value : city.xtxt.cells) add(value); for (const std::uint8_t value : city.xtrf.cells) add(value); for (const std::uint8_t value : city.xfir.cells) add(value); for (const std::uint8_t value : city.misc) add(value); return hash; } static City make_observed_city() { City city(MapSize::Large); const auto developed = [&](int row, int column, std::uint8_t bits, bool fireCovered) { city.xzon.at(row, column) = 1; city.xbld.at(row, column) = xbld::kFirstDeveloped; city.xbit.at(row, column) = bits; city.xfir.at(row >> 2, column >> 2) = fireCovered ? 10 : 0; }; developed(8, 8, xbit::kPowered | xbit::kWatered, true); developed(16, 16, 0, false); developed(24, 24, xbit::kPowered, false); developed(32, 32, xbit::kWatered, true); city.xbld.at(40, 40) = xbld::kWaterPump; city.xbld.at(41, 41) = xbld::kRubbleFirst; city.xbld.at(42, 42) = xbld::kAbandoned1; city.xbld.at(43, 43) = xbld::kZoo; city.xbld.at(44, 44) = xbld::kSchool; city.xbld.at(45, 45) = xbld::kPrison; city.xbld.at(46, 46) = xbld::kHospital; city.xbld.at(47, 47) = xbld::kPoliceStation; city.xbld.at(48, 48) = xbld::kFireStation; city.xbld.at(49, 49) = xbld::kBigPark; city.xbld.at(50, 50) = xbld::kCollege; city.xbld.at(51, 51) = xbld::kMarina; city.xbld.at(52, 52) = xbld::kCoalPlant; city.xbld.at(53, 53) = xbld::kRoadFirst; city.xbld.at(54, 54) = xbld::kCityHall; city.xbld.at(55, 55) = xbld::kPlymouthArco; city.xbld.at(56, 56) = xbld::kAirportBldg1; city.xbld.at(57, 57) = xbld::kPier; city.xzon.at(58, 58) = xzon::kMilitary; city.xtxt.at(255, 80) = xtxt::kNeighborConnection; city.xtxt.at(90, 255) = xtxt::kNeighborConnection; city.xtrf.cells[1] = 24; city.xtrf.cells[2] = 169; city.xtrf.cells[3] = 170; city.set_misc_be32(sc2k::misc::kZonePops, 321); city.set_misc_be32(sc2k::misc::kEducationQuotient, 88); city.set_misc_be32(sc2k::misc::kLifeExpectancy, 71); city.set_misc_be32(sc2k::misc::kCrimeTotal, 1234); city.set_misc_be32(sc2k::misc::kPollutionTotal, 567); city.set_misc_be32(sc2k::misc::kUnemployment, 9); city.set_misc_be32(sc2k::misc::kEconomyTrend, 3); city.set_misc_be32(sc2k::misc::kCityClass, 2); city.set_budget_rate(0, 7); city.set_budget_rate(5, 61); city.set_budget_rate(6, 62); city.set_budget_rate(7, 63); city.set_budget_rate(8, 65); city.set_budget_rate(9, 69); city.set_budget_rate(10, 80); city.set_demand(0, 111); city.set_demand(1, -222); city.set_demand(2, 333); city.set_funds(456789); city.set_ordinances(0x12345u); city.set_next_bond_rate(6); city.set_misc_be32(sim::neighbor_seed_offset(0), 2); city.set_misc_be32(sim::neighbor_pop_offset(0), 500); city.set_misc_be32(sim::neighbor_seed_offset(1), 3); city.set_misc_be32(sim::neighbor_pop_offset(1), 800); return city; } static void gathering_is_numeric_deterministic_and_read_only() { City city = make_observed_city(); const unsigned before = observed_city_hash(city); const sim::CityObservationFacts facts{2050, 6, 101, 47, 42}; const sim::CitySignals first = sim::gather_city_signals(city, {}, facts); const sim::CitySignals second = sim::gather_city_signals(city, {}, facts); CHECK(first == second); CHECK(observed_city_hash(city) == before); CHECK(first.year == 2050 && first.month == 6 && first.monthIndex == 24606); CHECK(first.temperatureF == 101 && first.windMph == 47 && first.rainMm == 42); CHECK(first.zonedDeveloped == 4); CHECK(first.unpowered == 2 && first.unwatered == 2 && first.noFireCover == 2); CHECK(first.waterSources == 1 && first.rubble == 1 && first.abandoned == 1); CHECK(first.openings[0] == 1 && first.openings[1] == 1 && first.openings[2] == 1); CHECK(first.schools == 1 && first.hospitals == 1 && first.policeStations == 1); CHECK(first.fireStations == 1 && first.parks == 1 && first.prisons == 1); CHECK(first.colleges == 1 && first.zoos == 1 && first.marinas == 1); CHECK(first.airports == 1 && first.seaports == 1 && first.powerPlants == 1); CHECK(first.arcologies == 1 && first.rewardBuildings == 1); CHECK(first.transportTiles == 1 && first.militaryTiles == 1); CHECK(first.buildingTiles[xbld::kSchool] == 1); CHECK(first.buildingTiles[xbld::kCoalPlant] == 1); CHECK(first.openingLocations[2] == (sim::CityOpeningLocation{43, 43, xbld::kZoo})); CHECK(first.connectionsByNeighbor[0] == 1 && first.connectionsByNeighbor[1] == 1); CHECK(first.connectionsByNeighbor[2] == 0 && first.connectionsByNeighbor[3] == 0); CHECK(first.trafficCells == 3 && first.congestedCells == 1); CHECK(first.population == 3210 && first.education == 88 && first.lifeExpectancy == 71); CHECK(first.demand == (std::array{111, -222, 333})); CHECK(first.treasury == 456789); CHECK(first.crime == 1234 && first.pollution == 567 && first.unemployment == 9); CHECK(first.taxRate == 7 && first.policeFunding == 61 && first.fireFunding == 62); CHECK(first.healthFunding == 63 && first.educationFunding == 65); CHECK(first.transitFunding == 69 && first.roadFunding == 80); CHECK(first.ordinances == 0x12345u && first.economyTrend == 3 && first.federalRate == 6); CHECK(first.topNeighborPopulation == 800 && first.topNeighborSlot == 1 && first.topNeighborSeed == 3); CHECK(first.topNeighborName == "Fort Verdegris"); } static void gathering_uses_effective_operating_funding_when_supplied() { City city = make_observed_city(); const sim::OperatingFundingSnapshot rationed = sim::make_operating_funding_snapshot( [&city](int record) { return city.budget_rate(record); }, 5000); const sim::CitySignals signals = sim::gather_city_signals( city, {}, {2050, 6, 101, 47, 42}, &rationed); CHECK(signals.taxRate == 7); // tax appropriation is not operating coverage CHECK(signals.policeFunding == 30); CHECK(signals.fireFunding == 31); CHECK(signals.healthFunding == 31); CHECK(signals.educationFunding == 32); CHECK(signals.transitFunding == 34); CHECK(signals.roadFunding == 40); } static void history_advances_after_each_observation() { City city = make_observed_city(); sim::CitySignalHistory history; sim::CityObservationFacts facts{1950, 0, 50, 10, 15}; const sim::CityMonthObservation prime = sim::observe_city_month(history, city, {}, facts); CHECK(!prime.hasHistory); CHECK(history.primed && history.previous == prime.current && history.previousYear == prime.current); city.set_misc_be32(sc2k::misc::kZonePops, 400); facts.month = 1; const sim::CityMonthObservation monthOne = sim::observe_city_month(history, city, {}, facts); CHECK(monthOne.hasHistory); CHECK(monthOne.current.population == 4000 && monthOne.previous.population == 3210); CHECK(monthOne.previousYear == prime.current); CHECK(history.previous == monthOne.current && history.previousYear == prime.current); city.set_misc_be32(sc2k::misc::kZonePops, 500); facts.year = 1951; facts.month = 0; const sim::CityMonthObservation yearOne = sim::observe_city_month(history, city, {}, facts); CHECK(yearOne.previous.population == 4000 && yearOne.previousYear.population == 3210); CHECK(history.previousYear == yearOne.current); facts.month = 1; const sim::CityMonthObservation afterRollover = sim::observe_city_month(history, city, {}, facts); CHECK(afterRollover.previousYear == yearOne.current); } static void top_neighbor_identity_has_a_stable_tie_break() { City city; city.set_misc_be32(sim::neighbor_seed_offset(0), 2); city.set_misc_be32(sim::neighbor_pop_offset(0), 800); city.set_misc_be32(sim::neighbor_seed_offset(1), 3); city.set_misc_be32(sim::neighbor_pop_offset(1), 800); const sim::CitySignals signals = sim::gather_city_signals(city, {}, {1950, 0, 50, 10, 15}); CHECK(signals.topNeighborPopulation == 800); CHECK(signals.topNeighborSlot == 0 && signals.topNeighborSeed == 2); CHECK(signals.topNeighborName == "Denmont"); } static void rating_uses_canonical_principal_instead_of_the_compatibility_count() { City city = make_observed_city(); TileCensus census; census.recount(city); const auto assessed = sim::assessed_value_from_legacy_valuation(city_valuation(census)); CHECK(assessed); const auto emptyCapacity = assessed ? sim::calculate_borrowing_capacity(assessed.value, 0, city.difficulty()) : sim::FinanceCalculation{}; CHECK(emptyCapacity && emptyCapacity.value.limit > 0); sim::MunicipalDebt debt; sim::DebtInstrument instrument; instrument.id = 1; instrument.kind = sim::DebtKind::LegacyInterestOnly; instrument.origin = sim::DebtOrigin::ClassicImport; instrument.originalPrincipal = emptyCapacity.value.limit * 65 / 100; instrument.remainingPrincipal = instrument.originalPrincipal; instrument.annualRate = 500; instrument.nextDuePeriod = 12; instrument.frequency = sim::PaymentFrequency::Annual; debt.instruments.push_back(instrument); debt.nextId = 2; CHECK(sim::validate_municipal_debt(debt) == sim::MunicipalDebtValidationError::None); const sim::CityObservationFacts facts{2050, 6, 50, 10, 15}; const sim::CitySignals empty = sim::gather_city_signals(city, {}, facts); const sim::CitySignals leveraged = sim::gather_city_signals(city, debt, facts); CHECK(city.bond_count() == 0 && empty.loanRating == 0); CHECK(leveraged.loanRating == static_cast(sim::CreditRating::C)); } static void cold_rebuild_primes_without_fabricating_history() { const City reloaded = make_observed_city(); const sim::CityObservationFacts facts{2050, 6, 50, 10, 15}; sim::CitySignalHistory rebuilt; const sim::CityMonthObservation first = sim::observe_city_month(rebuilt, reloaded, {}, facts); CHECK(!first.hasHistory); CHECK(first.current == sim::gather_city_signals(reloaded, {}, facts)); CHECK(rebuilt.previous == first.current && rebuilt.previousYear == first.current); } int main() { gathering_is_numeric_deterministic_and_read_only(); gathering_uses_effective_operating_funding_when_supplied(); history_advances_after_each_observation(); top_neighbor_identity_has_a_stable_tie_break(); rating_uses_canonical_principal_instead_of_the_compatibility_count(); cold_rebuild_primes_without_fabricating_history(); if (failures == 0) { std::printf("city observer: all checks passed\n"); return 0; } std::printf("city observer: %d failure(s)\n", failures); return 1; }