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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383// City Wire yearly operations: plant lifecycles, stadium seasons, and Exodus.#include "sc2k/ui/notification_projector.hpp"#include "sc2k/ui/notification_selection.hpp"#include <array>#include <cstdint>#include <cstdio>#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::Annual, std::int32_t clock = 1500) { return {{clock, 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 ui::NotificationKey plant_key(int row, int column) { return {ui::NotificationKeyDomain::Plant, ui::notification_plant_key_value(row, column)};}
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 plant_warning_rebuild_and_closure_share_one_truthful_identity() { ui::NotificationStore store; const ui::NotificationProjectionContext context{100, "Power City"}; const ui::NotificationKey key = plant_key(12, 34);
const sim::DomainEvent age49 = domain_event( sim::PlantAgingWarning{12, 34, xbld::kCoalPlant, 49, 49}); CHECK(ui::project_notification_event(store, age49, context) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* warning = store.find(key); CHECK(warning); if (!warning) return; const ui::NotificationId id = warning->id; CHECK(warning->sourceEvent == age49.stamp); CHECK(warning->titleKey == "citywire.plant.aging.title"); CHECK(warning->descriptionKey == "citywire.plant.aging.description"); CHECK(warning->category == ui::NotificationCategory::Warning); CHECK(warning->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(warning->expiryMonths == 0 && !warning->autoOpen); CHECK(message_parameter(*warning, "plant") == "plant.coal"); CHECK(integer_parameter(*warning, "building") == xbld::kCoalPlant); CHECK(integer_parameter(*warning, "age") == 49); CHECK(integer_parameter(*warning, "years") == 49); CHECK(integer_parameter(*warning, "row") == 12); CHECK(integer_parameter(*warning, "column") == 34); CHECK(warning->actionCount == 2); check_action(*warning, 0, "citywire.action.goto", ui::NotificationActionCommand::GoTo, 0, 12, 34); check_action(*warning, 1, "citywire.action.open_power_plants", ui::NotificationActionCommand::SelectTool, ui::notification_action_argument(ui::NotificationToolTarget::PowerPlants));
CHECK(store.acknowledge(id)); const sim::DomainEvent age50 = domain_event( sim::PlantAgingWarning{12, 34, xbld::kCoalPlant, 50, 50}, 1); CHECK(ui::project_notification_event(store, age50, {112, "Power City"}) == ui::NotificationProjectionOutcome::Upserted); warning = store.find(key); CHECK(warning && warning->id == id); CHECK(warning && warning->createdMonth == 100 && warning->updatedMonth == 112); CHECK(warning && warning->unread && integer_parameter(*warning, "age") == 50);
const sim::DomainEvent rebuilt = domain_event( sim::PlantRebuilt{12, 34, xbld::kCoalPlant, 51, 51, 4000}, 2); CHECK(ui::project_notification_event(store, rebuilt, {124, "Power City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* outcome = store.find(key); CHECK(outcome && outcome->id == id); CHECK(outcome && outcome->titleKey == "citywire.plant.rebuilt.title"); CHECK(outcome && outcome->category == ui::NotificationCategory::Info); CHECK(outcome && outcome->lifecycle == ui::NotificationLifecycle::Historical); CHECK(outcome && outcome->expiryMonths == 12 && !outcome->autoOpen); CHECK(outcome && integer_parameter(*outcome, "cost") == 4000); CHECK(outcome && outcome->actionCount == 1); store.prune(135); CHECK(store.find(key)); store.prune(136); CHECK(!store.find(key));
CHECK(ui::project_notification_event(store, domain_event(sim::PlantAgingWarning{12, 34, xbld::kOilPlant, 50, 50}, 3), {200, "Power City"}) == ui::NotificationProjectionOutcome::Upserted); const ui::NotificationId secondId = store.find(key)->id; CHECK(store.mark_auto_opened(secondId)); const sim::DomainEvent closed = domain_event( sim::PlantClosed{12, 34, xbld::kOilPlant, 51, 51}, 4); CHECK(ui::project_notification_event(store, closed, {212, "Power City"}) == ui::NotificationProjectionOutcome::Upserted); outcome = store.find(key); CHECK(outcome && outcome->id == secondId); CHECK(outcome && outcome->titleKey == "citywire.plant.closed.title"); CHECK(outcome && outcome->category == ui::NotificationCategory::Danger); CHECK(outcome && outcome->lifecycle == ui::NotificationLifecycle::Historical); CHECK(outcome && outcome->expiryMonths == 18 && outcome->autoOpen && !outcome->autoOpened); CHECK(outcome && ui::notification_interrupts_existing_dialog(outcome->category)); CHECK(ui::notification_detail_pause_policy() == ui::SimulationPausePolicy::PauseSimulation); CHECK(outcome && message_parameter(*outcome, "plant") == "plant.oil"); CHECK(outcome && outcome->actionCount == 2); CHECK(ui::next_auto_open_notification(store.records()) == secondId);}
static void every_aging_plant_uses_a_localisable_name_and_locations_do_not_collide() { static constexpr std::array<std::uint8_t, 7> kBuildings = { xbld::kGasPlant, xbld::kOilPlant, xbld::kNuclearPlant, xbld::kSolarPlant, xbld::kMicrowavePlant, xbld::kFusionPlant, xbld::kCoalPlant}; static constexpr std::array<const char*, 7> kKeys = { "plant.gas", "plant.oil", "plant.nuclear", "plant.solar", "plant.microwave", "plant.fusion", "plant.coal"};
ui::NotificationStore store; for (std::size_t index = 0; index < kBuildings.size(); ++index) { const int row = static_cast<int>(index); const int column = static_cast<int>(index + 10); CHECK(ui::project_notification_event(store, domain_event(sim::PlantAgingWarning{row, column, kBuildings[index], 49, 49}, static_cast<std::uint32_t>(index)), {0, "Plants"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification* notification = store.find(plant_key(row, column)); CHECK(notification && message_parameter(*notification, "plant") == kKeys[index]); } CHECK(store.size() == kBuildings.size()); CHECK(plant_key(1, 23) != plant_key(12, 3));}
static void decisive_stadium_seasons_use_committed_team_metadata() { City city; constexpr int kSlot = 10; city.xmic[std::size_t(kSlot) * 8] = xbld::kStadium; sim::xmic_set_u16(city, kSlot, 4, 2); sim::xmic_set_u16(city, kSlot, 6, 201); sim::set_label_text(city, 201, "Metro Rockets");
ui::NotificationStore store; const sim::DomainEvent championship = domain_event( sim::StadiumSeasonCompleted{kSlot, 32, 8, 24000}); CHECK(ui::project_notification_event(store, championship, {400, "Sports City", &city}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 1); const ui::Notification& winner = store.records().front(); CHECK(!winner.stableKey); CHECK(winner.titleKey == "desk.sports.champ.head"); CHECK(winner.descriptionKey == "desk.sports.champ.body"); CHECK(winner.category == ui::NotificationCategory::Comments); CHECK(winner.lifecycle == ui::NotificationLifecycle::Historical); CHECK(winner.expiryMonths == 6 && winner.actionCount == 0 && !winner.autoOpen); CHECK(literal_parameter(winner, "team") == "Metro Rockets"); CHECK(message_parameter(winner, "sport") == "sport.soccer"); CHECK(integer_parameter(winner, "sport_index") == 2); CHECK(integer_parameter(winner, "wins") == 32); CHECK(integer_parameter(winner, "losses") == 8); CHECK(integer_parameter(winner, "w") == 32 && integer_parameter(winner, "l") == 8); CHECK(integer_parameter(winner, "attendance") == 24000);
const sim::DomainEvent losing = domain_event( sim::StadiumSeasonCompleted{kSlot, 10, 30, 900}, 1); CHECK(ui::project_notification_event(store, losing, {401, "Sports City", &city}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 2); CHECK(store.records()[1].titleKey == "desk.sports.bust.head");
CHECK(ui::project_notification_event(store, domain_event(sim::StadiumSeasonCompleted{kSlot, 27, 13, 12000}, 2), {402, "Sports City", &city}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.records()[2].titleKey == "desk.sports.win.head");
CHECK(ui::project_notification_event(store, domain_event(sim::StadiumSeasonCompleted{kSlot, 16, 24, 8000}, 3), {403, "Sports City", &city}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.records()[3].titleKey == "desk.sports.loss.head");
CHECK(ui::project_notification_event(store, domain_event(sim::StadiumSeasonCompleted{kSlot, 20, 20, 10000}, 4), {404, "Sports City", &city}) == ui::NotificationProjectionOutcome::Ignored); CHECK(store.size() == 4);
// A game-day win remains a win even when the rolling form is 10-30. // Consumers must use the explicit outcome instead of reinterpreting form. const sim::DomainEvent gameDay = domain_event( sim::StadiumGameDayCompleted{1, 2, 20, 20, kSlot, sim::StadiumEpisodeStartReason::Natural, 50, 50, 56, sim::StadiumGameOutcome::HomeWin, 10, 30, 9000}, 5, sim::EventPhase::Stadium, 56); CHECK(ui::project_notification_event( store, gameDay, {405, "Sports City", &city}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.records().back().titleKey == "desk.sports.win.head"); CHECK(literal_parameter(store.records().back(), "stadium_id") == "1"); CHECK(literal_parameter(store.records().back(), "episode_id") == "2"); CHECK(integer_parameter(store.records().back(), "row") == 20); CHECK(integer_parameter(store.records().back(), "column") == 20);
CHECK(ui::project_notification_event(store, domain_event(sim::StadiumGameDayCompleted{2, 3, 20, 20, kSlot, sim::StadiumEpisodeStartReason::Natural, 75, 75, 81, sim::StadiumGameOutcome::Draw, 20, 20, 9000}, 6, sim::EventPhase::Stadium, 81), {406, "Sports City", &city}) == ui::NotificationProjectionOutcome::Ignored);
ui::NotificationStore fallbackStore; CHECK(ui::project_notification_event(fallbackStore, losing, {500, "No Context"}) == ui::NotificationProjectionOutcome::Upserted); const ui::Notification& fallback = fallbackStore.records().front(); CHECK(message_parameter(fallback, "team") == "citywire.team.fallback"); CHECK(message_parameter(fallback, "sport") == "citywire.sport.fallback");}
static void exodus_is_major_bounded_history_without_an_invented_action() { City city; ui::NotificationStore store; const sim::DomainEvent event = domain_event( sim::ExodusCompleted{301, 30100000, 60, 70}, 0, sim::EventPhase::Session); CHECK(ui::project_notification_event(store, event, {600, "Arcology City", &city}) == ui::NotificationProjectionOutcome::Upserted); CHECK(store.size() == 1); const ui::Notification& notification = store.records().front(); CHECK(notification.sourceEvent == event.stamp); CHECK(notification.titleKey == "citywire.exodus.title"); CHECK(notification.descriptionKey == "citywire.exodus.description"); CHECK(notification.category == ui::NotificationCategory::Info); CHECK(notification.lifecycle == ui::NotificationLifecycle::Historical); CHECK(notification.expiryMonths == 36 && notification.actionCount == 0); CHECK(!notification.autoOpen && !notification.stableKey); CHECK(integer_parameter(notification, "count") == 301); CHECK(integer_parameter(notification, "n") == 301); CHECK(integer_parameter(notification, "refund") == 30100000); CHECK(integer_parameter(notification, "pad_row") == 60); CHECK(integer_parameter(notification, "pad_column") == 70); store.prune(635); CHECK(store.size() == 1); store.prune(636); CHECK(store.empty());}
static void malformed_yearly_events_are_ignored_without_partial_state() { City city; ui::NotificationStore store; const std::array events = { domain_event(sim::PlantAgingWarning{-1, 0, xbld::kCoalPlant, 49, 49}), domain_event(sim::PlantAgingWarning{0, 0, xbld::kWindPlant, 49, 49}), domain_event(sim::PlantAgingWarning{0, 0, xbld::kCoalPlant, 48, 48}), domain_event(sim::PlantRebuilt{0, 0, xbld::kCoalPlant, 50, 50, 4000}), domain_event(sim::PlantRebuilt{0, 0, xbld::kCoalPlant, 51, 51, 0}), domain_event(sim::PlantClosed{0, 0, xbld::kCoalPlant, 50, 50}), domain_event(sim::StadiumSeasonCompleted{-1, 28, 12, 1000}), domain_event(sim::StadiumSeasonCompleted{0, 28, 11, 1000}), domain_event(sim::StadiumSeasonCompleted{0, 28, 12, sim::kStadiumCapacity + 1}), domain_event(sim::ExodusCompleted{0, 0, 0, 0}, 0, sim::EventPhase::Session), domain_event(sim::ExodusCompleted{1, -1, 0, 0}, 0, sim::EventPhase::Session), domain_event(sim::ExodusCompleted{1, 100000, -1, -1}, 0, sim::EventPhase::Session), }; CHECK(ui::project_notification_batch(store, events, {0, "Invalid", &city}) == (ui::NotificationProjectionSummary{0, 0, events.size()})); CHECK(store.empty());}
static void batch_projection_keeps_yearly_producer_order() { ui::NotificationStore store; const std::vector events = { domain_event(sim::StadiumSeasonCompleted{1, 30, 10, 10000}, 0), domain_event(sim::PlantAgingWarning{4, 5, xbld::kGasPlant, 49, 49}, 1), domain_event(sim::ExodusCompleted{301, 30100000, 6, 7}, 2, sim::EventPhase::Session), }; CHECK(ui::project_notification_batch(store, events, {1000, "Ordered"}) == (ui::NotificationProjectionSummary{3, 0, 0})); CHECK(store.size() == 3); CHECK(store.records()[0].titleKey == "desk.sports.win.head"); CHECK(store.records()[1].titleKey == "citywire.plant.aging.title"); CHECK(store.records()[2].titleKey == "citywire.exodus.title"); CHECK(store.records()[0].sourceEvent->ordinal == 0); CHECK(store.records()[1].sourceEvent->ordinal == 1); CHECK(store.records()[2].sourceEvent->ordinal == 2);}
static void aging_plant_warnings_reconstruct_from_microsim_records() { City city(MapSize::Small); constexpr int slot = 1; constexpr int row = 12; constexpr int column = 34; city.xmic[slot * 8] = xbld::kCoalPlant; city.xmic[slot * 8 + 1] = 49; city.xtxt.at(row, column) = slot + 51; city.xbld.at(row, column) = xbld::kCoalPlant;
ui::NotificationStore store; const auto initial = ui::synchronize_plant_notifications( store, city, {600, "Loaded Power", &city}); CHECK(initial.upserted == 1); const ui::Notification* warning = store.find(plant_key(row, column)); CHECK(warning && warning->lifecycle == ui::NotificationLifecycle::ConditionBound); CHECK(warning && integer_parameter(*warning, "age") == 49); CHECK(warning && message_parameter(*warning, "plant") == "plant.coal"); CHECK(warning && warning->actions[0].row == row && warning->actions[0].column == column);
city.xmic[slot * 8 + 1] = 48; const auto cleared = ui::synchronize_plant_notifications( store, city, {601, "Loaded Power", &city}); CHECK(cleared.resolved == 1 && store.empty());}
static void committed_plant_removal_resolves_an_active_warning() { ui::NotificationStore store; CHECK(ui::project_notification_event(store, domain_event(sim::PlantAgingWarning{8, 9, xbld::kGasPlant, 49, 49}), {700, "Demolition"}) == ui::NotificationProjectionOutcome::Upserted); CHECK(ui::project_notification_event(store, domain_event(sim::BuildingRemoved{8, 9, xbld::kGasPlant, 4, 4, 16, sim::MapOperationCause::Player}, 1, sim::EventPhase::Session), {700, "Demolition"}) == ui::NotificationProjectionOutcome::Resolved); CHECK(store.empty());}
int main() { plant_warning_rebuild_and_closure_share_one_truthful_identity(); every_aging_plant_uses_a_localisable_name_and_locations_do_not_collide(); decisive_stadium_seasons_use_committed_team_metadata(); exodus_is_major_bounded_history_without_an_invented_action(); malformed_yearly_events_are_ignored_without_partial_state(); batch_projection_keeps_yearly_producer_order(); aging_plant_warnings_reconstruct_from_microsim_records(); committed_plant_removal_resolves_an_active_warning(); if (failures == 0) { std::printf("notification operations: all checks passed\n"); return 0; } std::printf("notification operations: %d failure(s)\n", failures); return 1;}