// Derived electricity accounting from the same component walk that commits powered bits. #include "sc2k/sim/power.hpp" #include "sc2k/sim/power_topology.hpp" #include "test_harness.hpp" #include namespace { void place_demand(sc2k::City& city, int row, int column, std::uint8_t code = 0x70, bool conductive = true) { city.xbld.at(row, column) = code; city.xzon.at(row, column) = 0xF0; city.xbit.at(row, column) = conductive ? sc2k::xbit::kConductive : 0; } const sc2k::PowerComponentDiagnostic* component(const sc2k::PowerDiagnostics& diagnostics, int key) { for (const auto& candidate : diagnostics.components) if (candidate.componentKey == key) return &candidate; return nullptr; } const sc2k::PowerBuildingDiagnostic* building(const sc2k::PowerDiagnostics& diagnostics, int row, int column) { for (const auto& candidate : diagnostics.buildings) if (candidate.anchorRow == row && candidate.anchorColumn == column) return &candidate; return nullptr; } } // namespace int main() { using namespace sc2k; { City city; const int dimension = city.dimension(); city.xbld.at(10, 10) = xbld::kWindPlant; city.xbit.at(10, 10) = xbit::kConductive; city.altm.at(10, 10) = 20; // deterministic floor supply = 10 place_demand(city, 10, 11); place_demand(city, 10, 12); city.xbld.at(20, 20) = 0x0E; // source-free power line and consumer island city.xbit.at(20, 20) = xbit::kConductive; place_demand(city, 20, 21); place_demand(city, 30, 30, 0x70, false); // developed but not connected const PowerDiagnostics diagnostics = update_power_with_diagnostics(city, nullptr, false); const int suppliedKey = 10 * dimension + 10; const int sourceFreeKey = 20 * dimension + 20; const auto* supplied = component(diagnostics, suppliedKey); const auto* sourceFree = component(diagnostics, sourceFreeKey); CHECK(supplied != nullptr); CHECK(sourceFree != nullptr); if (supplied) { CHECK(supplied->localSupply == 10); CHECK(supplied->effectiveSupply == 10); CHECK(supplied->demand == 3); // wind tile plus two consumers CHECK(supplied->delivered == 3); CHECK(supplied->remainingSupply == 7); CHECK(supplied->legacyDemand == 2); } if (sourceFree) { CHECK(sourceFree->localSupply == 0); CHECK(sourceFree->demand == 1); CHECK(sourceFree->delivered == 0); CHECK(sourceFree->unmetDemand == 1); } const auto* disconnected = building(diagnostics, 30, 30); CHECK(disconnected != nullptr); if (disconnected) { CHECK(disconnected->componentKey == kDisconnectedPowerComponent); CHECK(disconnected->demand == 1); CHECK(disconnected->delivered == 0); } CHECK(diagnostics.localSupply == 10); CHECK(diagnostics.effectiveSupply == 10); CHECK(diagnostics.connectedDemand == 4); CHECK(diagnostics.totalDemand == 5); CHECK(diagnostics.delivered == 3); CHECK(diagnostics.unmetDemand == 2); CHECK(diagnostics.remainingSupply == 7); CHECK(diagnostics.legacyUsedPercent == 20); CHECK(diagnostics.componentKeyByTile[static_cast(10 * dimension + 12)] == suppliedKey); CHECK(diagnostics.componentKeyByTile[static_cast(30 * dimension + 30)] == kDisconnectedPowerComponent); } { City city; city.xbld.at(39, 40) = xbld::kWindPlant; city.xbit.at(39, 40) = xbit::kConductive; city.altm.at(39, 40) = 6; // supply 3: one unit for the plant, two for the building constexpr std::uint8_t code = 0x8C; CHECK(sim::building_sizes()[code] == 2); for (int row = 40; row <= 41; ++row) for (int column = 40; column <= 41; ++column) { city.xbld.at(row, column) = code; city.xbit.at(row, column) = xbit::kConductive; } city.xzon.at(40, 40) = xzon::kCornerTL; const PowerDiagnostics first = update_power_with_diagnostics(city, nullptr, false); const auto* footprint = building(first, 40, 40); CHECK(footprint != nullptr); if (footprint) { CHECK(footprint->completeFootprint); CHECK(footprint->demand == 4); CHECK(footprint->delivered == 2); } CHECK(first.components.size() == 1); if (!first.components.empty()) { CHECK(first.components[0].localSupply == 3); CHECK(first.components[0].demand == 5); CHECK(first.components[0].delivered == 3); CHECK(first.components[0].unmetDemand == 2); CHECK(first.components[0].remainingSupply == 0); } // The report is a discardable cache: rebuilding from the same authoritative city and deterministic // source floor produces the identical structured snapshot. const PowerDiagnostics rebuilt = update_power_with_diagnostics(city, nullptr, false); CHECK(rebuilt == first); } { // Zero-output sources stay unpowered, so the historic driver revisits the same island from each // source tile. Diagnostics upsert that repeated traversal into one stable component snapshot. City city; city.xbld.at(50, 50) = xbld::kWindPlant; city.xbld.at(50, 51) = xbld::kWindPlant; city.xbit.at(50, 50) = xbit::kConductive; city.xbit.at(50, 51) = xbit::kConductive; city.altm.at(50, 50) = 0; city.altm.at(50, 51) = 0; const PowerDiagnostics diagnostics = update_power_with_diagnostics(city, nullptr, false); CHECK(diagnostics.components.size() == 1); CHECK(diagnostics.localSupply == 0); CHECK(diagnostics.connectedDemand == 2); CHECK(diagnostics.delivered == 0); CHECK(diagnostics.unmetDemand == 2); } { // Maintenance funding scales local generation only. Nominal plant output and demand remain // inspectable, imports remain exact, and the conservation ordinance applies to funded output. City fixture{MapSize::Tiny}; fixture.xbld.at(10, 10) = xbld::kCoalPlant; fixture.xbit.at(10, 10) = xbit::kConductive; for (int column = 11; column <= 63; ++column) place_demand(fixture, 10, column); fixture.set_ordinances(0x10000u); const int key = fixture.index(10, 10); const std::array imports = {{{key, 7}}}; struct FundedAnswer { int efficiency; int funded; int effective; }; constexpr std::array answers{ FundedAnswer{0, 0, 7}, FundedAnswer{5000, 22, 30}, FundedAnswer{10000, 44, 54}, FundedAnswer{11852, 52, 63}, }; for (const FundedAnswer answer : answers) { City city = fixture; const PowerDiagnostics diagnostics = update_power_with_diagnostics( city, nullptr, false, imports, answer.efficiency); const auto* supplied = component(diagnostics, key); CHECK(supplied != nullptr); if (supplied) { CHECK(supplied->localSupply == 44); CHECK(supplied->fundedLocalSupply == answer.funded); CHECK(supplied->importedSupply == 7); CHECK(supplied->effectiveSupply == answer.effective); CHECK(supplied->demand == 54); CHECK(supplied->delivered == std::min(answer.effective, 54)); CHECK(supplied->unmetDemand == 54 - std::min(answer.effective, 54)); CHECK(supplied->remainingSupply == std::max(0, answer.effective - 54)); } CHECK(diagnostics.localSupply == 44); CHECK(diagnostics.fundedLocalSupply == answer.funded); CHECK(diagnostics.importedSupply == 7); CHECK(diagnostics.effectiveSupply == answer.effective); CHECK(diagnostics.totalDemand == 54); const auto* plant = building(diagnostics, 10, 10); CHECK(plant != nullptr); CHECK(plant && plant->localSupply == 44); CHECK(plant && plant->fundedLocalSupply == answer.funded); } } { // Scaling occurs after the established wind draws. A connected component is resolved once at // non-compatibility funding, so changing funding cannot consume extra random values merely because // zero funded output leaves every plant tile dark. City fixture{MapSize::Tiny}; for (int column = 20; column <= 21; ++column) { fixture.xbld.at(20, column) = xbld::kWindPlant; fixture.xbit.at(20, column) = xbit::kConductive; fixture.altm.at(20, column) = 20; } std::uint32_t expectedRngState = 0; for (const int efficiency : {0, 5000, 10000, 11852}) { City city = fixture; MsRand rng{0x12345678u}; (void)update_power_with_diagnostics( city, &rng, false, {}, efficiency); if (expectedRngState == 0) expectedRngState = rng.state(); else CHECK(rng.state() == expectedRngState); } City implicit = fixture; City explicitFull = fixture; MsRand implicitRng{0x10203040u}; MsRand explicitRng{0x10203040u}; const PowerDiagnostics oldContract = update_power_with_diagnostics(implicit, &implicitRng, false); const PowerDiagnostics explicitContract = update_power_with_diagnostics( explicitFull, &explicitRng, false, {}, 10000); CHECK(oldContract == explicitContract); CHECK(implicit.xbit.cells == explicitFull.xbit.cells); CHECK(implicitRng.state() == explicitRng.state()); } { // A demolished power causeway from an older native document can leave conductive/powered flags // on bare open water. The native compatibility repair must remove that impossible remnant before // the solver runs. A real causeway deck remains conductive because its surface structure exists. City city{MapSize::Tiny}; city.xbld.at(10, 10) = xbld::kWindPlant; city.xbit.at(10, 10) = xbit::kConductive; city.altm.at(10, 10) = 20; city.xbit.at(10, 11) = xbit::kWater | xbit::kConductive | xbit::kPowered; place_demand(city, 10, 12); city.xbld.at(20, 10) = xbld::kWindPlant; city.xbit.at(20, 10) = xbit::kConductive; city.altm.at(20, 10) = 20; city.xbld.at(20, 11) = 92; city.xbit.at(20, 11) = xbit::kWater | xbit::kConductive; place_demand(city, 20, 12); CHECK(sim::repair_demolished_power_causeways(city) == 1); const PowerDiagnostics diagnostics = update_power_with_diagnostics(city, nullptr, false); CHECK((city.xbit.at(10, 11) & xbit::kConductive) == 0); CHECK((city.xbit.at(10, 12) & xbit::kPowered) == 0); CHECK((city.xbit.at(20, 11) & xbit::kConductive) != 0); CHECK((city.xbit.at(20, 12) & xbit::kPowered) != 0); CHECK(diagnostics.components.size() == 3); } if (failures == 0) { std::printf("power diagnostics: all checks passed\n"); return 0; } std::printf("power diagnostics: %d failure(s)\n", failures); return 1; }