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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320// Persisted temporal commentary memory: streaks, disaster provenance, and expiry.#include "sc2k/ui/commentary_history.hpp"
#include <algorithm>#include <array>#include <cstdio>
#include "sc2k/ui/commentary_catalogue.hpp"#include "sc2k/ui/commentary_director.hpp"#include "sc2k/ui/commentary_persistence.hpp"#include "test_harness.hpp"
using namespace sc2k;
static ui::CommentaryContext ordinary_context(std::int32_t month) { ui::CommentaryContext context; context.monthIndex = month; ui::set_comment_scalar(context, ui::CommentScalar::Population, 20000); ui::set_comment_scalar(context, ui::CommentScalar::TemperatureF, 50); ui::set_comment_scalar(context, ui::CommentScalar::RainMm, 15); ui::set_comment_scalar(context, ui::CommentScalar::RoadFunding, 100); ui::set_comment_scalar(context, ui::CommentScalar::PoliceFunding, 100); ui::set_comment_scalar(context, ui::CommentScalar::FireFunding, 100); ui::set_comment_scalar(context, ui::CommentScalar::HealthFunding, 100); ui::set_comment_scalar(context, ui::CommentScalar::EducationFunding, 100); ui::set_comment_scalar(context, ui::CommentScalar::TransitFunding, 100); ui::set_comment_scalar(context, ui::CommentScalar::EconomyTrend, 2); ui::set_comment_scalar(context, ui::CommentScalar::Treasury, 50000); ui::set_comment_scalar(context, ui::CommentScalar::Unemployment, 6); return context;}
static const ui::CommentStreakState* find_streak( const ui::CommentaryState& state, ui::CommentStreakId id) { const auto found = std::find_if(state.streaks.begin(), state.streaks.end(), [&](const ui::CommentStreakState& streak) { return streak.id == id; }); return found == state.streaks.end() ? nullptr : &*found;}
static void streaks_advance_end_and_restart_conservatively() { ui::CommentaryState state = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 0, 1); state.streaks.push_back({{65000}, 0, 0, 1});
ui::CommentaryContext month10 = ordinary_context(10); ui::set_comment_scalar(month10, ui::CommentScalar::TemperatureF, 80); auto summary = ui::update_commentary_history(state, month10); CHECK(summary.started == 2 && summary.advanced == 0 && summary.restarted == 0); CHECK(find_streak(state, ui::comment_streak::kWarmWeather)->lengthMonths == 1); CHECK(find_streak(state, ui::comment_streak::kHotWeather)->lengthMonths == 1); CHECK(find_streak(state, {65000}) != nullptr); state.lastEvaluatedMonth = 10;
ui::CommentaryContext month11 = ordinary_context(11); ui::set_comment_scalar(month11, ui::CommentScalar::TemperatureF, 80); summary = ui::update_commentary_history(state, month11); CHECK(summary.advanced == 2 && summary.restarted == 0); CHECK(find_streak(state, ui::comment_streak::kWarmWeather)->lengthMonths == 2); state.lastEvaluatedMonth = 11;
ui::CommentaryContext month13 = ordinary_context(13); ui::set_comment_scalar(month13, ui::CommentScalar::TemperatureF, 80); summary = ui::update_commentary_history(state, month13); CHECK(summary.restarted == 2 && summary.advanced == 0); CHECK(find_streak(state, ui::comment_streak::kWarmWeather)->startMonth == 13); state.lastEvaluatedMonth = 13;
ui::CommentaryContext month14 = ordinary_context(14); summary = ui::update_commentary_history(state, month14); CHECK(summary.ended == 2); CHECK(find_streak(state, ui::comment_streak::kWarmWeather) == nullptr); CHECK(find_streak(state, ui::comment_streak::kHotWeather) == nullptr); CHECK(find_streak(state, {65000}) != nullptr); state.lastEvaluatedMonth = 14;
const ui::CommentaryState before = state; CHECK(ui::update_commentary_history(state, month13).ignored); CHECK(state == before);}
static void unknown_streak_capacity_remains_saveable() { ui::CommentaryState state = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 0, 40); for (std::size_t index = 0; index < ui::kMaximumCommentStreaks; ++index) state.streaks.push_back({{static_cast<std::uint16_t>(1000 + index)}, 0, 0, 1}); ui::CommentaryContext context = ordinary_context(1); ui::set_comment_scalar(context, ui::CommentScalar::TemperatureF, 75); const auto summary = ui::update_commentary_history(state, context); CHECK(summary.capacitySkipped == 1 && summary.started == 0); CHECK(state.streaks.size() == ui::kMaximumCommentStreaks); CHECK(ui::validate_commentary_state(state, 1) == ui::CommentaryStateIssue::None);}
static void temporal_context_respects_disaster_provenance_and_active_months() { ui::CommentaryState fresh = ui::make_commentary_state( ui::CommentaryTrackingProvenance::FromCreation, 0, 2); ui::CommentaryContext context = ordinary_context(60); ui::apply_commentary_history_context(context, fresh); CHECK(ui::comment_scalar(context, ui::CommentScalar::DisasterOccurrenceCount) == 0); CHECK(ui::comment_scalar(context, ui::CommentScalar::MonthsSinceLastDisaster) == 60); CHECK(ui::comment_flag(context, ui::CommentFlag::DisasterNeverOccurredKnown));
ui::CommentaryState imported = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 40, 3); context = ordinary_context(64); ui::apply_commentary_history_context(context, imported); CHECK(ui::comment_scalar(context, ui::CommentScalar::MonthsSinceLastDisaster) == 24); CHECK(!ui::comment_flag(context, ui::CommentFlag::DisasterNeverOccurredKnown));
imported.disasterHistory[0] = { ui::CommentHistoryKnowledge::Occurred, 2, 58}; imported.disasterHistory[3] = { ui::CommentHistoryKnowledge::Occurred, 1, 55}; imported.streaks = { {ui::comment_streak::kRainyWeather, 62, 64, 3}, {ui::comment_streak::kOrdinancesActive, 53, 64, 12}, {ui::comment_streak::kProsperity, 52, 63, 12}, }; context = ordinary_context(64); ui::apply_commentary_history_context(context, imported); CHECK(ui::comment_scalar(context, ui::CommentScalar::DisasterOccurrenceCount) == 3); CHECK(ui::comment_scalar(context, ui::CommentScalar::MonthsSinceLastDisaster) == 6); CHECK(ui::comment_scalar(context, ui::CommentScalar::RainyWeatherStreak) == 3); CHECK(ui::comment_scalar(context, ui::CommentScalar::OrdinanceStreak) == 12); CHECK(ui::comment_scalar(context, ui::CommentScalar::ProsperityStreak) == 0); CHECK(!ui::comment_flag(context, ui::CommentFlag::DisasterNeverOccurredKnown));}
static bool observation_activates( ui::CommentaryContext context, ui::CommentStreakId id) { ui::CommentaryState state = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 0, 30); ui::update_commentary_history(state, context); return find_streak(state, id) != nullptr;}
static void snapshot_streak_predicates_have_exact_edges() { ui::CommentaryContext context = ordinary_context(1); ui::set_comment_scalar(context, ui::CommentScalar::TemperatureF, 74); CHECK(!observation_activates(context, ui::comment_streak::kWarmWeather)); ui::set_comment_scalar(context, ui::CommentScalar::TemperatureF, 75); CHECK(observation_activates(context, ui::comment_streak::kWarmWeather));
context = ordinary_context(1); ui::set_comment_scalar(context, ui::CommentScalar::RainMm, 24); CHECK(!observation_activates(context, ui::comment_streak::kRainyWeather)); ui::set_comment_scalar(context, ui::CommentScalar::RainMm, 25); CHECK(observation_activates(context, ui::comment_streak::kRainyWeather));
context = ordinary_context(1); ui::set_comment_scalar(context, ui::CommentScalar::TemperatureF, 33); CHECK(!observation_activates(context, ui::comment_streak::kColdWeather)); ui::set_comment_scalar(context, ui::CommentScalar::TemperatureF, 32); CHECK(observation_activates(context, ui::comment_streak::kColdWeather)); ui::set_comment_scalar(context, ui::CommentScalar::TemperatureF, 79); CHECK(!observation_activates(context, ui::comment_streak::kHotWeather)); ui::set_comment_scalar(context, ui::CommentScalar::TemperatureF, 80); CHECK(observation_activates(context, ui::comment_streak::kHotWeather));
context = ordinary_context(1); CHECK(!observation_activates(context, ui::comment_streak::kOrdinancesActive)); ui::set_comment_scalar(context, ui::CommentScalar::OrdinanceCount, 1); CHECK(observation_activates(context, ui::comment_streak::kOrdinancesActive));
context = ordinary_context(1); ui::set_comment_scalar(context, ui::CommentScalar::RoadFunding, 51); CHECK(!observation_activates(context, ui::comment_streak::kLowServiceFunding)); ui::set_comment_scalar(context, ui::CommentScalar::RoadFunding, 50); CHECK(observation_activates(context, ui::comment_streak::kLowServiceFunding));
context = ordinary_context(1); ui::set_comment_scalar(context, ui::CommentScalar::Population, 10000); ui::set_comment_scalar(context, ui::CommentScalar::EconomyTrend, 3); ui::set_comment_scalar(context, ui::CommentScalar::Treasury, 100000); ui::set_comment_scalar(context, ui::CommentScalar::Unemployment, 5); CHECK(observation_activates(context, ui::comment_streak::kProsperity)); ui::set_comment_scalar(context, ui::CommentScalar::Treasury, 99999); CHECK(!observation_activates(context, ui::comment_streak::kProsperity));}
static bool temporal_rule_is_eligible(std::uint16_t id, const ui::CommentaryState& state, std::int32_t month) { const auto rules = ui::commentary_rules(); const auto found = std::find_if(rules.begin(), rules.end(), [&](const ui::CommentRuleDefinition& rule) { return rule.id.value == id; }); if (found == rules.end()) return false; ui::CommentaryContext context = ordinary_context(month); ui::set_comment_scalar(context, ui::CommentScalar::TrackedMonths, std::max<std::int64_t>(0, std::int64_t(month) - state.trackingStartMonth)); ui::apply_commentary_history_context(context, state); const std::array oneRule{*found}; return ui::evaluate_commentary(state, context, oneRule).selection.has_value();}
static void disaster_story_windows_expire_at_exact_months() { ui::CommentaryState occurred = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 0, 31); occurred.disasterHistory[0] = { ui::CommentHistoryKnowledge::Occurred, 1, 10}; CHECK(temporal_rule_is_eligible(198, occurred, 12)); CHECK(!temporal_rule_is_eligible(198, occurred, 13)); CHECK(temporal_rule_is_eligible(199, occurred, 14)); CHECK(temporal_rule_is_eligible(199, occurred, 18)); CHECK(!temporal_rule_is_eligible(199, occurred, 19));
occurred.disasterHistory[0] = { ui::CommentHistoryKnowledge::Occurred, 3, 0}; CHECK(temporal_rule_is_eligible(202, occurred, 12)); CHECK(!temporal_rule_is_eligible(202, occurred, 13));
ui::CommentaryState imported = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 0, 32); CHECK(temporal_rule_is_eligible(200, imported, 24)); imported.disasterHistory[0] = { ui::CommentHistoryKnowledge::Occurred, 1, 1}; CHECK(!temporal_rule_is_eligible(200, imported, 24));
const ui::CommentaryState fresh = ui::make_commentary_state( ui::CommentaryTrackingProvenance::FromCreation, 0, 33); CHECK(temporal_rule_is_eligible(201, fresh, 60)); CHECK(!temporal_rule_is_eligible(201, ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 0, 34), 60));}
static void premise_expiry_blocks_temporal_copy_immediately() { const auto rules = ui::commentary_rules(); const auto temporal = std::find_if(rules.begin(), rules.end(), [](const ui::CommentRuleDefinition& rule) { return rule.id.value == 203; }); CHECK(temporal != rules.end()); if (temporal == rules.end()) return;
ui::CommentaryState state = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 0, 4); state.streaks.push_back({ui::comment_streak::kWarmWeather, 7, 10, 4}); ui::CommentaryContext context = ordinary_context(10); ui::apply_commentary_history_context(context, state); const std::array oneRule{*temporal}; CHECK(ui::evaluate_commentary(state, context, oneRule).selection.has_value());
state.lastEvaluatedMonth = 10; context = ordinary_context(11); CHECK(ui::update_commentary_history(state, context).ended == 1); ui::apply_commentary_history_context(context, state); CHECK(ui::comment_scalar(context, ui::CommentScalar::WarmWeatherStreak) == 0); CHECK(!ui::evaluate_commentary(state, context, oneRule).selection.has_value());}
static void save_reload_continues_the_same_streak() { ui::CommentaryState uninterrupted = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 10, 5); ui::CommentaryContext context = ordinary_context(10); ui::set_comment_scalar(context, ui::CommentScalar::RainMm, 30); ui::update_commentary_history(uninterrupted, context); uninterrupted.lastEvaluatedMonth = 10; context.monthIndex = 11; ui::update_commentary_history(uninterrupted, context); uninterrupted.lastEvaluatedMonth = 11;
const auto bytes = ui::encode_commentary_state(uninterrupted, 11); const auto restored = ui::restore_commentary_state(bytes, 11, 99); CHECK(restored.status == ui::CommentaryRestoreStatus::Restored); ui::CommentaryState resumed = restored.state; context.monthIndex = 12; ui::update_commentary_history(uninterrupted, context); ui::update_commentary_history(resumed, context); CHECK(resumed == uninterrupted); CHECK(find_streak(resumed, ui::comment_streak::kRainyWeather)->lengthMonths == 3);}
static void director_updates_history_even_while_emergency_defers_copy() { ui::CommentaryState state = ui::make_commentary_state( ui::CommentaryTrackingProvenance::Unknown, 0, 6); state.lastEvaluatedMonth = 3; state.nextAmbientMonth = 4; state.streaks = { {ui::comment_streak::kWarmWeather, 1, 3, 3}, {ui::comment_streak::kHotWeather, 1, 3, 3}, }; sim::CitySignals signals; signals.monthIndex = 4; signals.population = 20000; signals.temperatureF = 80; signals.roadFunding = signals.policeFunding = signals.fireFunding = 100; signals.healthFunding = signals.educationFunding = signals.transitFunding = 100; ui::NotificationStore store; const auto emitted = ui::publish_commentary_month(state, store, ui::make_commentary_context(signals, state, false)); CHECK(emitted.decision.selection.has_value()); CHECK(emitted.decision.selection && (emitted.decision.selection->rule.value == 203 || emitted.decision.selection->rule.value == 206)); CHECK(find_streak(state, ui::comment_streak::kWarmWeather)->lengthMonths == 4);
signals.monthIndex = 5; const auto deferred = ui::publish_commentary_month(state, store, ui::make_commentary_context(signals, state, true)); CHECK(deferred.decision.blockReason == ui::CommentaryBlockReason::EmergencyActive); CHECK(!deferred.notification); CHECK(find_streak(state, ui::comment_streak::kWarmWeather)->lengthMonths == 5);}
int main() { streaks_advance_end_and_restart_conservatively(); unknown_streak_capacity_remains_saveable(); temporal_context_respects_disaster_provenance_and_active_months(); snapshot_streak_predicates_have_exact_edges(); disaster_story_windows_expire_at_exact_months(); premise_expiry_blocks_temporal_copy_immediately(); save_reload_continues_the_same_streak(); director_updates_history_even_while_emergency_defers_copy(); if (failures == 0) { std::printf("commentary history: all checks passed\n"); return 0; } std::printf("commentary history: %d failure(s)\n", failures); return 1;}