// Aggregate map-command events: one stable report per committed building or neighbour operation. #include "sc2k/sim/growth.hpp" #include "sc2k/sim/map_events.hpp" #include "sc2k/ui/tools.hpp" #include #include #include "test_harness.hpp" using namespace sc2k; template static const Payload* payload_at(std::span events, std::size_t index) { if (index >= events.size()) return nullptr; return std::get_if(&events[index].payload); } static void buildings_report_whole_committed_footprints() { City city; CHECK(construct_building(city, 10, 10, xbld::kPoliceStation, 3, 0)); const auto snapshot = sim::snapshot_developed_building(city, 11, 11); CHECK(snapshot.has_value()); CHECK(snapshot && *snapshot == (sim::BuildingEventSnapshot{10, 10, xbld::kPoliceStation, 3, 3, 9})); sim::EventBuffer events; events.begin_batch(100); CHECK(snapshot && sim::report_building_placed(events, *snapshot)); CHECK(events.view().size() == 1); CHECK(events.view()[0].stamp == (sim::EventStamp{100, sim::EventPhase::Session, 0})); const auto* placed = payload_at(events.view(), 0); CHECK(placed && placed->row == 10 && placed->column == 10); CHECK(placed && placed->building == xbld::kPoliceStation); CHECK(placed && placed->footprintRows == 3 && placed->footprintColumns == 3 && placed->footprintTiles == 9); CHECK(placed && placed->cause == sim::MapOperationCause::Player); CHECK(!sim::snapshot_developed_building(city, -1, 0)); city.xbld.at(30, 30) = xbld::kSmallPark; CHECK(!sim::snapshot_developed_building(city, 30, 30)); } static void rewards_emit_generic_then_specific() { City city; CHECK(construct_building(city, 20, 20, xbld::kMayorsHouse, 2, 0)); const auto snapshot = sim::snapshot_developed_building(city, 21, 21); CHECK(snapshot.has_value()); sim::EventBuffer events; events.begin_batch(125); CHECK(snapshot && sim::report_building_placed(events, *snapshot)); CHECK(events.view().size() == 2); CHECK(events.view()[0].stamp == (sim::EventStamp{125, sim::EventPhase::Session, 0})); CHECK(events.view()[1].stamp == (sim::EventStamp{125, sim::EventPhase::Session, 1})); const auto* placed = payload_at(events.view(), 0); const auto* reward = payload_at(events.view(), 1); CHECK(placed && placed->building == xbld::kMayorsHouse); CHECK(reward && reward->row == 20 && reward->column == 20 && reward->building == xbld::kMayorsHouse); } static void removal_reports_original_identity_only_after_commit() { City city; city.set_funds(1000); CHECK(construct_building(city, 30, 30, xbld::kPoliceStation, 3, 0)); const auto snapshot = sim::snapshot_developed_building(city, 31, 31); CHECK(snapshot.has_value()); sim::EventBuffer events; events.begin_batch(150); CHECK(snapshot && !sim::report_building_removed(city, events, *snapshot)); CHECK(events.view().empty()); CHECK(ui::apply_tool(city, ui::Tool::Bulldoze, 31, 31).ok); CHECK(snapshot && sim::report_building_removed(city, events, *snapshot)); CHECK(events.view().size() == 1); const auto* removed = payload_at(events.view(), 0); CHECK(removed && removed->row == 30 && removed->column == 30); CHECK(removed && removed->building == xbld::kPoliceStation && removed->footprintTiles == 9); CHECK(removed && removed->cause == sim::MapOperationCause::Player); } static void stadium_acceptance_has_one_ordered_batch() { City city; CHECK(construct_building(city, 40, 40, xbld::kStadium, 4, 0)); sim::provision_stadium(city, 40, 40, 1234); const int slot = sim::stadium_slot(city, 40, 40); CHECK(slot >= 0); sim::xmic_set_u16(city, slot, 4, 3); sim::EventBuffer events; events.begin_batch(175); const auto snapshot = sim::snapshot_developed_building(city, 42, 42); CHECK(snapshot && sim::report_building_placed(events, *snapshot)); CHECK(sim::report_stadium_team_founded(city, events, 40, 40)); CHECK(events.view().size() == 2); const auto* placed = payload_at(events.view(), 0); const auto* founded = payload_at(events.view(), 1); CHECK(placed && placed->building == xbld::kStadium && placed->footprintTiles == 16); CHECK(founded && founded->row == 40 && founded->column == 40); CHECK(founded && founded->microsimSlot == slot && founded->sport == 3); // A cancelled dialog never calls either reporter; removing the provisional stadium makes the // acceptance-specific reporter reject it as well. events.begin_batch(176); int tiles[128][2]; const int tileCount = sim::building_footprint(city, 40, 40, tiles, 128); for (int index = 0; index < tileCount; ++index) city.xbld.at(tiles[index][0], tiles[index][1]) = 0; CHECK(!sim::report_stadium_team_founded(city, events, 40, 40)); CHECK(events.view().empty()); } static void neighbor_connections_commit_atomically() { City city; city.set_funds(5000); city.xbld.at(0, 40) = xbld::kRoadFirst; sim::EventBuffer events; events.begin_batch(200); CHECK(sim::commit_neighbor_connection(city, sim::ConnNet::Road, 0, 40, &events)); CHECK(city.funds() == 4000); CHECK(city.xtxt.at(0, 40) == xtxt::kNeighborConnection); const auto* opened = payload_at(events.view(), 0); CHECK(events.view().size() == 1); CHECK(opened && opened->row == 0 && opened->column == 40 && opened->edgeSlot == 2); CHECK(opened && opened->kind == sim::NeighborConnectionKind::Road && opened->cost == 1000); events.begin_batch(201); CHECK(!sim::commit_neighbor_connection(city, sim::ConnNet::Road, 0, 40, &events)); CHECK(city.funds() == 4000 && events.view().empty()); city.xbld.at(20, 20) = xbld::kRoadFirst; CHECK(!sim::commit_neighbor_connection(city, sim::ConnNet::Road, 20, 20, &events)); CHECK(city.funds() == 4000 && events.view().empty()); const auto snapshot = sim::snapshot_neighbor_connection(city, 0, 40); CHECK(snapshot == (sim::NeighborConnectionSnapshot{0, 40, 2, sim::NeighborConnectionKind::Road})); CHECK(snapshot && !sim::report_neighbor_connection_removed(city, events, *snapshot)); CHECK(ui::apply_tool(city, ui::Tool::Bulldoze, 0, 40).ok); CHECK(snapshot && sim::report_neighbor_connection_removed(city, events, *snapshot)); const auto* removed = payload_at(events.view(), 0); CHECK(removed && removed->edgeSlot == 2 && removed->kind == sim::NeighborConnectionKind::Road); } static void highway_snapshot_finds_the_block_marker() { City city; for (int row = 0; row < 2; ++row) for (int column = 60; column < 62; ++column) city.xbld.at(row, column) = xbld::kHighwayLR; city.xtxt.at(0, 61) = xtxt::kNeighborConnection; const auto snapshot = sim::snapshot_neighbor_connection(city, 1, 60); CHECK(snapshot == (sim::NeighborConnectionSnapshot{0, 61, 2, sim::NeighborConnectionKind::Highway})); } int main() { buildings_report_whole_committed_footprints(); rewards_emit_generic_then_specific(); removal_reports_original_identity_only_after_commit(); stadium_acceptance_has_one_ordered_batch(); neighbor_connections_commit_atomically(); highway_snapshot_finds_the_block_marker(); if (failures == 0) { std::printf("map events: all checks passed\n"); return 0; } std::printf("map events: %d failure(s)\n", failures); return 1; }