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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421// City Wire progression projector: inventions, rewards, openings, neighbours, and teams.#include "sc2k/sim/growth.hpp"#include "sc2k/ui/notification_projector.hpp"#include <array>#include <cstdint>#include <cstdio>#include <string>#include <string_view>#include <vector>
#include "test_harness.hpp"
using namespace sc2k;
template <typename Payload>static sim::DomainEvent domain_event(Payload payload, std::uint32_t ordinal = 0, sim::EventPhase phase = sim::EventPhase::Progression) { return {{900, phase, ordinal}, sim::DomainEventPayload{payload}};}
static const ui::NotificationParameter* parameter(const ui::Notification& notification, std::string_view name) { for (const ui::NotificationParameter& candidate : notification.parameters) if (candidate.name == name) return &candidate; return nullptr;}
static std::int64_t integer_parameter(const ui::Notification& notification, std::string_view name) { const ui::NotificationParameter* found = parameter(notification, name); const auto* value = found ? std::get_if<std::int64_t>(&found->value) : nullptr; CHECK(value); return value ? *value : 0;}
static std::string_view literal_parameter(const ui::Notification& notification, std::string_view name) { const ui::NotificationParameter* found = parameter(notification, name); const auto* value = found ? std::get_if<ui::NotificationLiteral>(&found->value) : nullptr; CHECK(value); return value ? std::string_view(value->value) : std::string_view{};}
static std::string_view message_parameter(const ui::Notification& notification, std::string_view name) { const ui::NotificationParameter* found = parameter(notification, name); const auto* value = found ? std::get_if<ui::NotificationMessageKey>(&found->value) : nullptr; CHECK(value); return value ? std::string_view(value->value) : std::string_view{};}
static void check_action(const ui::Notification& notification, std::size_t index, std::string_view label, ui::NotificationActionCommand command, std::int64_t argument, int row = -1, int column = -1) { CHECK(index < notification.actionCount); if (index >= notification.actionCount) return; const ui::NotificationAction& action = notification.actions[index]; CHECK(action.labelKey == label); CHECK(action.command == command && action.argument == argument); CHECK(action.row == row && action.column == column && action.closeDialog);}
static void all_inventions_have_stable_copy_actions_and_parameters() { static constexpr std::array<const char*, 17> kSlugs = { "gas", "nuclear", "solar", "wind", "microwave", "fusion", "airports", "highways", "bus_depots", "subways", "water_treatment", "desalination", "plymouth_arcology", "forest_arcology", "darco_arcology", "launch_arcology", "elevated_highways"}; static constexpr std::array<ui::NotificationToolTarget, 17> kTargets = { ui::NotificationToolTarget::PowerPlants, ui::NotificationToolTarget::PowerPlants, ui::NotificationToolTarget::PowerPlants, ui::NotificationToolTarget::PowerPlants, ui::NotificationToolTarget::PowerPlants, ui::NotificationToolTarget::PowerPlants, ui::NotificationToolTarget::Airport, ui::NotificationToolTarget::Highway, ui::NotificationToolTarget::BusDepot, ui::NotificationToolTarget::Subway, ui::NotificationToolTarget::WaterFacilities, ui::NotificationToolTarget::WaterFacilities, ui::NotificationToolTarget::Arcologies, ui::NotificationToolTarget::Arcologies, ui::NotificationToolTarget::Arcologies, ui::NotificationToolTarget::Arcologies, ui::NotificationToolTarget::ElevatedHighway};
ui::NotificationStore store; for (std::size_t index = 0; index < kSlugs.size(); ++index) { const sim::InventionId invention = static_cast<sim::InventionId>(index); const sim::DomainEvent event = domain_event(sim::InventionDiscovered{invention}, static_cast<std::uint32_t>(index), sim::EventPhase::Technology); CHECK(ui::project_notification_event(store, event, {40, "Tech City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationKey key{ui::NotificationKeyDomain::Invention, index}; const ui::Notification* notification = store.find(key); CHECK(notification); if (!notification) continue; const std::string base = std::string("citywire.invention.") + kSlugs[index]; CHECK(notification->sourceEvent == event.stamp); CHECK(notification->titleKey == base + ".title"); CHECK(notification->descriptionKey == base + ".description"); CHECK(notification->category == ui::NotificationCategory::Info); CHECK(notification->lifecycle == ui::NotificationLifecycle::Historical); CHECK(notification->expiryMonths == 24 && !notification->autoOpen); CHECK(integer_parameter(*notification, "invention") == static_cast<std::int64_t>(index)); CHECK(message_parameter(*notification, "what") == sim::invention_label(static_cast<int>(index))); CHECK(notification->actionCount == 1); CHECK(notification->actions[0].command == ui::NotificationActionCommand::SelectTool); CHECK(notification->actions[0].argument == ui::notification_action_argument(kTargets[index])); } CHECK(store.size() == 17);
const ui::NotificationKey gas{ui::NotificationKeyDomain::Invention, 0}; const ui::NotificationId gasId = store.find(gas)->id; CHECK(ui::project_notification_event(store, domain_event(sim::InventionDiscovered{sim::InventionId::NaturalGas}), {41, "Tech City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 17 && store.find(gas) && store.find(gas)->id == gasId); CHECK(store.find(gas) && store.find(gas)->createdMonth == 40 && store.find(gas)->updatedMonth == 41);
CHECK(ui::project_notification_event(store, domain_event(sim::InventionDiscovered{static_cast<sim::InventionId>(255)}), {41, "Tech City"}) == ui::NotificationProjectionOutcome::Ignored); store.prune(64); CHECK(store.size() == 1 && store.find(gas)); store.prune(65); CHECK(store.empty());}
struct RewardExpected { int status; std::uint8_t building; const char* slug; const char* action;};
static constexpr std::array<RewardExpected, 4> kRewards = {{ {1, xbld::kMayorsHouse, "mayors_house", "citywire.action.place_mayors_house"}, {2, xbld::kCityHall, "city_hall", "citywire.action.place_city_hall"}, {3, xbld::kStatue, "statue", "citywire.action.place_statue"}, {5, xbld::kLlamaDome, "llama_dome", "citywire.action.place_llama_dome"},}};
static void earned_rewards_are_protected_and_external_placement_resolves_them() { ui::NotificationStore store; for (const RewardExpected& expected : kRewards) { const int currentMonth = 70 + expected.status * 10000; const sim::DomainEvent earned = domain_event(sim::CityStatusAdvanced{ expected.status - 1, expected.status, 2000LL * expected.status}); CHECK(ui::project_notification_event(store, earned, {currentMonth, "Reward City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationKey key{ui::NotificationKeyDomain::Reward, expected.building}; const ui::Notification* notification = store.find(key); CHECK(notification); if (!notification) continue; const std::string base = std::string("citywire.reward.") + expected.slug; CHECK(notification->titleKey == base + ".title" && notification->descriptionKey == base + ".description"); CHECK(notification->category == ui::NotificationCategory::Reward); CHECK(notification->lifecycle == ui::NotificationLifecycle::RewardBound); CHECK(notification->expiryMonths == 0 && notification->autoOpen); CHECK(literal_parameter(*notification, "city") == "Reward City"); CHECK(integer_parameter(*notification, "status") == expected.status); CHECK(integer_parameter(*notification, "building") == expected.building); check_action(*notification, 0, expected.action, ui::NotificationActionCommand::OpenRewardPlacement, expected.building);
store.prune(currentMonth + 10000); CHECK(store.find(key)); CHECK(ui::project_notification_event(store, domain_event(sim::BuildingPlaced{10, 20, expected.building, 2, 2, 4, sim::MapOperationCause::Player}), {currentMonth + 10000, "Reward City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(store.find(key)); CHECK(ui::project_notification_event(store, domain_event(sim::RewardPlaced{10, 20, expected.building}), {currentMonth + 10000, "Reward City"}) == ui::NotificationProjectionOutcome::Resolved); CHECK(!store.find(key)); } CHECK(store.empty());
CHECK(ui::project_notification_event(store, domain_event(sim::RewardPlaced{0, 0, xbld::kPoliceStation}), {0, "Reward City"}) == ui::NotificationProjectionOutcome::Ignored);}
static void later_population_classes_are_historical() { ui::NotificationStore store; CHECK(ui::project_notification_event(store, domain_event(sim::CityStatusAdvanced{3, 4, 60001}), {80, "Metro"}) == ui::NotificationProjectionOutcome::Ignored);
CHECK(ui::project_notification_event(store, domain_event(sim::CityStatusAdvanced{5, 6, 120001}), {80, "Metro"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationKey arcologies{ui::NotificationKeyDomain::Progression, 6}; const ui::Notification* arcology = store.find(arcologies); CHECK(arcology && arcology->category == ui::NotificationCategory::Reward); CHECK(arcology && arcology->lifecycle == ui::NotificationLifecycle::Historical); CHECK(arcology && arcology->expiryMonths == 24 && arcology->autoOpen); CHECK(arcology && arcology->titleKey == "citywire.progress.arcologies.title"); CHECK(arcology && arcology->actions[0].argument == ui::notification_action_argument(ui::NotificationToolTarget::Arcologies));
for (int status = 7; status <= 10; ++status) { CHECK(ui::project_notification_event(store, domain_event(sim::CityStatusAdvanced{status - 1, status, status * 100000LL}), {80, "Metro"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find({ui::NotificationKeyDomain::Progression, static_cast<std::uint64_t>(status)}); CHECK(notification && notification->category == ui::NotificationCategory::Info); CHECK(notification && notification->titleKey == "citywire.progress.population_class.title"); CHECK(notification && !notification->autoOpen && notification->actionCount == 0); CHECK(notification && integer_parameter(*notification, "population") == status * 100000LL); } CHECK(store.size() == 5);
const std::array invalid = { sim::CityStatusAdvanced{2, 2, 30001}, sim::CityStatusAdvanced{2, 4, 60001}, sim::CityStatusAdvanced{-1, 0, 0}, sim::CityStatusAdvanced{10, 11, 20000000}, sim::CityStatusAdvanced{0, 1, -1}}; for (const auto& status : invalid) CHECK(ui::project_notification_event(store, domain_event(status), {80, "Metro"}) == ui::NotificationProjectionOutcome::Ignored);}
static void every_first_opening_has_copy_focus_and_retention() { static constexpr std::array<const char*, sim::kCityOpeningCount> kSuffixes = { "airport", "seaport", "zoo", "marina", "college", "stadium", "arco", "llama"}; ui::NotificationStore store; for (std::size_t index = 0; index < sim::kCityOpeningCount; ++index) { const sim::CityOpeningKind kind = static_cast<sim::CityOpeningKind>(index); const int row = 10 + static_cast<int>(index); const int column = 30 + static_cast<int>(index); const sim::DomainEvent event = domain_event( sim::FirstBuildingOpened{kind, row, column, static_cast<std::uint8_t>(200 + index)}); CHECK(ui::project_notification_event(store, event, {90, "Open City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find({ui::NotificationKeyDomain::Opening, index}); CHECK(notification); if (!notification) continue; const std::string base = std::string("desk.open.") + kSuffixes[index]; CHECK(notification->titleKey == base + ".head" && notification->descriptionKey == base + ".body"); CHECK(notification->category == (kind == sim::CityOpeningKind::LlamaDome ? ui::NotificationCategory::Comments : ui::NotificationCategory::Info)); const int expiry = kind == sim::CityOpeningKind::Arcology ? 24 : kind == sim::CityOpeningKind::LlamaDome ? 12 : 18; CHECK(notification->expiryMonths == expiry && !notification->autoOpen); CHECK(literal_parameter(*notification, "city") == "Open City"); check_action(*notification, 0, "citywire.action.goto", ui::NotificationActionCommand::GoTo, 0, row, column); } CHECK(store.size() == sim::kCityOpeningCount);
const ui::NotificationKey airport{ui::NotificationKeyDomain::Opening, static_cast<std::uint64_t>(sim::CityOpeningKind::Airport)}; const ui::NotificationId airportId = store.find(airport)->id; CHECK(ui::project_notification_event(store, domain_event(sim::FirstBuildingOpened{sim::CityOpeningKind::Airport, 70, 71, 201}), {91, "Open City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.find(airport) && store.find(airport)->id == airportId && store.find(airport)->updatedMonth == 91); CHECK(store.find(airport) && store.find(airport)->actions[0].row == 70 && store.find(airport)->actions[0].column == 71);
CHECK(ui::project_notification_event(store, domain_event(sim::FirstBuildingOpened{static_cast<sim::CityOpeningKind>(255), 1, 1, 1}), {91, "Open City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::FirstBuildingOpened{sim::CityOpeningKind::Airport, -1, 1, 1}), {91, "Open City"}) == ui::NotificationProjectionOutcome::Ignored);}
static void team_founding_owns_city_data_and_has_a_deterministic_fallback() { 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); const sim::StadiumTeam team = sim::stadium_team(city, 40, 40); CHECK(slot >= 0 && team.valid && !team.team.empty()); const int sport = sim::xmic_u16(city, slot, 4);
ui::NotificationStore store; const sim::DomainEvent event = domain_event( sim::StadiumTeamFounded{40, 40, slot, sport}, 0, sim::EventPhase::Session); CHECK(ui::project_notification_event(store, event, {100, "Sports City", &city}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationKey key{ui::NotificationKeyDomain::Stadium, static_cast<std::uint64_t>(slot)}; const ui::Notification* notification = store.find(key); CHECK(notification && notification->category == ui::NotificationCategory::Comments); CHECK(notification && notification->expiryMonths == 12); CHECK(notification && literal_parameter(*notification, "team") == team.team); CHECK(notification && message_parameter(*notification, "sport") == sim::stadium_sport_name(sport)); CHECK(notification && integer_parameter(*notification, "microsim_slot") == slot); if (notification) check_action(*notification, 0, "citywire.action.goto", ui::NotificationActionCommand::GoTo, 0, 40, 40);
const std::string storedTeam = notification ? std::string(literal_parameter(*notification, "team")) : std::string{}; sim::set_label_text(city, sim::xmic_u16(city, slot, 6), "Changed Later"); CHECK(store.find(key) && literal_parameter(*store.find(key), "team") == storedTeam);
ui::NotificationStore fallback; CHECK(ui::project_notification_event(fallback, event, {100, "Sports City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(fallback.find(key) && message_parameter(*fallback.find(key), "team") == "citywire.team.fallback");
const std::array invalid = { sim::StadiumTeamFounded{-1, 40, slot, sport}, sim::StadiumTeamFounded{40, 40, -1, sport}, sim::StadiumTeamFounded{40, 40, 150, sport}, sim::StadiumTeamFounded{40, 40, slot, 99}}; for (const auto& founded : invalid) CHECK(ui::project_notification_event(fallback, domain_event(founded), {100, "Sports City", &city}) == ui::NotificationProjectionOutcome::Ignored);}
static void neighbor_milestones_use_committed_identity_and_fallback_names() { City city; city.set_misc_be32(sim::neighbor_seed_offset(2), 17); ui::NotificationStore store; const sim::DomainEvent opened = domain_event( sim::NeighborConnectionOpened{0, 40, 2, sim::NeighborConnectionKind::Road, 1000}, 0, sim::EventPhase::Session); CHECK(ui::project_notification_event(store, opened, {110, "Link City", &city}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 1 && !store.records()[0].stableKey); const ui::Notification& connection = store.records()[0]; CHECK(connection.category == ui::NotificationCategory::Info && connection.expiryMonths == 18); CHECK(literal_parameter(connection, "nb") == "Jeromi"); CHECK(integer_parameter(connection, "kind") == static_cast<std::int64_t>(sim::NeighborConnectionKind::Road)); CHECK(integer_parameter(connection, "cost") == 1000); check_action(connection, 0, "citywire.action.goto", ui::NotificationActionCommand::GoTo, 0, 0, 40);
CHECK(ui::project_notification_event(store, opened, {111, "Link City"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 2); CHECK(message_parameter(store.records()[1], "nb") == "desk.link.fallback");
const sim::DomainEvent overtaken = domain_event( sim::NeighborOvertaken{2, 17, 90000, 85000}, 1, sim::EventPhase::Conditions); CHECK(ui::project_notification_event(store, overtaken, {111, "Link City", &city}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification& overtake = store.records()[2]; CHECK(overtake.category == ui::NotificationCategory::Comments && overtake.expiryMonths == 12); CHECK(overtake.actionCount == 0 && literal_parameter(overtake, "nb") == "Jeromi"); CHECK(integer_parameter(overtake, "population") == 90000);
const std::array invalidConnections = { sim::NeighborConnectionOpened{-1, 40, 2, sim::NeighborConnectionKind::Road, 1000}, sim::NeighborConnectionOpened{0, 40, 4, sim::NeighborConnectionKind::Road, 1000}, sim::NeighborConnectionOpened{0, 40, 2, sim::NeighborConnectionKind::Unknown, 1000}}; for (const auto& invalid : invalidConnections) CHECK(ui::project_notification_event(store, domain_event(invalid), {111, "Link City", &city}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::NeighborOvertaken{2, 0, 90000, 85000}), {111, "Link City"}) == ui::NotificationProjectionOutcome::Ignored); CHECK(ui::project_notification_event(store, domain_event(sim::NeighborOvertaken{2, 17, -1, 85000}), {111, "Link City"}) == ui::NotificationProjectionOutcome::Ignored);}
static void mixed_batch_preserves_order_and_resolves_reward_in_place() { ui::NotificationStore store; const std::vector events = { domain_event(sim::InventionDiscovered{sim::InventionId::Airport}, 0, sim::EventPhase::Technology), domain_event(sim::CityStatusAdvanced{0, 1, 2500}, 1), domain_event(sim::FirstBuildingOpened{sim::CityOpeningKind::Airport, 20, 30, xbld::kAirportBldg1}, 2), domain_event(sim::MilitaryBaseOffered{}, 3), domain_event(sim::RewardPlaced{50, 50, xbld::kMayorsHouse}, 4, sim::EventPhase::Session)}; CHECK(ui::project_notification_batch(store, events, {130, "Batch City"}) == (ui::NotificationProjectionSummary{4, 1, 0})); CHECK(store.size() == 3); CHECK(store.records()[0].stableKey == (ui::NotificationKey{ui::NotificationKeyDomain::Invention, static_cast<std::uint64_t>(sim::InventionId::Airport)})); CHECK(store.records()[1].stableKey == (ui::NotificationKey{ui::NotificationKeyDomain::Opening, static_cast<std::uint64_t>(sim::CityOpeningKind::Airport)})); CHECK(store.records()[2].stableKey == (ui::NotificationKey{ui::NotificationKeyDomain::Decision, ui::notification_key_value(ui::NotificationDecisionKey::MilitaryBase)}));}
static void reward_availability_reconstructs_and_reactivates_after_deletion() { City city(MapSize::Small); city.name = "Return Gift"; city.set_city_status(3); ui::NotificationStore store; const ui::NotificationProjectionContext context{200, city.name, &city}; const auto initial = ui::synchronize_reward_notifications(store, city, context); CHECK(initial.upserted == 3 && initial.resolved == 0 && store.size() == 3); const ui::NotificationKey mayorKey{ui::NotificationKeyDomain::Reward, xbld::kMayorsHouse}; CHECK(store.find(mayorKey) && !store.find(mayorKey)->autoOpen);
city.xbld.at(10, 10) = xbld::kMayorsHouse; const auto placed = ui::synchronize_reward_notifications(store, city, context); CHECK(placed.resolved == 1 && !store.find(mayorKey));
city.xbld.at(10, 10) = 0; CHECK(ui::project_notification_event(store, domain_event(sim::BuildingRemoved{10, 10, xbld::kMayorsHouse, 2, 2, 4, sim::MapOperationCause::Player}), context) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.find(mayorKey) && !store.find(mayorKey)->autoOpen); CHECK(integer_parameter(*store.find(mayorKey), "status") == 1);}
int main() { all_inventions_have_stable_copy_actions_and_parameters(); earned_rewards_are_protected_and_external_placement_resolves_them(); later_population_classes_are_historical(); every_first_opening_has_copy_focus_and_retention(); team_founding_owns_city_data_and_has_a_deterministic_fallback(); neighbor_milestones_use_committed_identity_and_fallback_names(); mixed_batch_preserves_order_and_resolves_reward_in_place(); reward_availability_reconstructs_and_reactivates_after_deletion(); if (failures == 0) { std::printf("notification progression: all checks passed\n"); return 0; } std::printf("notification progression: %d failure(s)\n", failures); return 1;}