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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154// sc2k/ui/commentary_history.cpp — deterministic temporal memory implementation.#include "sc2k/ui/commentary_history.hpp"
#include <algorithm>#include <array>#include <limits>
#include "sc2k/ui/commentary_persistence.hpp"
namespace sc2k::ui {namespace {
struct StreakObservation { CommentStreakId id; bool active = false;};
CommentStreakState* find_streak( CommentaryState& state, CommentStreakId id) noexcept { const auto found = std::find_if(state.streaks.begin(), state.streaks.end(), [&](const CommentStreakState& streak) { return streak.id == id; }); return found == state.streaks.end() ? nullptr : &*found;}
std::uint32_t active_streak_length(const CommentaryState& state, CommentStreakId id, std::int32_t currentMonth) noexcept { for (const CommentStreakState& streak : state.streaks) if (streak.id == id && streak.lastObservedMonth == currentMonth) return streak.lengthMonths; return 0;}
void observe_streak(CommentaryState& state, StreakObservation observation, std::int32_t currentMonth, CommentaryHistoryUpdateSummary& summary) { CommentStreakState* streak = find_streak(state, observation.id); if (!observation.active) { if (!streak) return; state.streaks.erase(std::remove_if(state.streaks.begin(), state.streaks.end(), [&](const CommentStreakState& candidate) { return candidate.id == observation.id; }), state.streaks.end()); ++summary.ended; return; }
if (!streak) { if (state.streaks.size() >= kMaximumCommentStreaks) { ++summary.capacitySkipped; return; } state.streaks.push_back({observation.id, currentMonth, currentMonth, 1}); ++summary.started; return; } if (streak->lastObservedMonth == currentMonth) return; if (streak->lastObservedMonth < std::numeric_limits<std::int32_t>::max() && streak->lastObservedMonth + 1 == currentMonth) { streak->lastObservedMonth = currentMonth; streak->lengthMonths = static_cast<std::uint32_t>( std::int64_t(currentMonth) - streak->startMonth + 1); ++summary.advanced; return; } *streak = {observation.id, currentMonth, currentMonth, 1}; ++summary.restarted;}
} // namespace
CommentaryHistoryUpdateSummary update_commentary_history( CommentaryState& state, const CommentaryContext& context) noexcept { CommentaryHistoryUpdateSummary summary; if (context.monthIndex < 0 || context.monthIndex <= state.lastEvaluatedMonth) { summary.ignored = true; return summary; }
const auto scalar = [&](CommentScalar id) { return comment_scalar(context, id); }; const std::int64_t temperature = scalar(CommentScalar::TemperatureF); const std::int64_t rain = scalar(CommentScalar::RainMm); const bool lowFunding = std::min({scalar(CommentScalar::RoadFunding), scalar(CommentScalar::PoliceFunding), scalar(CommentScalar::FireFunding), scalar(CommentScalar::HealthFunding), scalar(CommentScalar::EducationFunding), scalar(CommentScalar::TransitFunding)}) <= 50; const bool prosperous = scalar(CommentScalar::Population) >= 10000 && scalar(CommentScalar::EconomyTrend) >= 3 && scalar(CommentScalar::Treasury) >= 100000 && scalar(CommentScalar::Unemployment) <= 5; const std::array observations{ StreakObservation{comment_streak::kWarmWeather, temperature >= 75 && temperature <= 94}, StreakObservation{comment_streak::kRainyWeather, rain >= 25 && rain <= 39}, StreakObservation{comment_streak::kColdWeather, temperature >= 6 && temperature <= 32}, StreakObservation{comment_streak::kHotWeather, temperature >= 80 && temperature <= 94}, StreakObservation{comment_streak::kOrdinancesActive, scalar(CommentScalar::OrdinanceCount) > 0}, StreakObservation{comment_streak::kLowServiceFunding, lowFunding}, StreakObservation{comment_streak::kProsperity, prosperous}, }; for (const StreakObservation observation : observations) observe_streak(state, observation, context.monthIndex, summary); std::sort(state.streaks.begin(), state.streaks.end(), [](const CommentStreakState& left, const CommentStreakState& right) { return left.id < right.id; }); return summary;}
void apply_commentary_history_context( CommentaryContext& context, const CommentaryState& state) noexcept { std::uint64_t occurrences = 0; std::int32_t lastDisaster = kCommentMonthUnset; bool neverOccurredKnown = true; for (const CommentOccurrenceHistory& history : state.disasterHistory) { neverOccurredKnown = neverOccurredKnown && history.knowledge == CommentHistoryKnowledge::KnownNever; if (history.knowledge != CommentHistoryKnowledge::Occurred) continue; occurrences = std::min<std::uint64_t>( std::numeric_limits<std::int64_t>::max(), occurrences + history.occurrenceCount); lastDisaster = std::max(lastDisaster, history.lastOccurrenceMonth); } const std::int64_t monthsSince = lastDisaster == kCommentMonthUnset ? std::max<std::int64_t>(0, std::int64_t(context.monthIndex) - state.trackingStartMonth) : std::max<std::int64_t>(0, std::int64_t(context.monthIndex) - lastDisaster); set_comment_scalar(context, CommentScalar::DisasterOccurrenceCount, static_cast<std::int64_t>(occurrences)); set_comment_scalar(context, CommentScalar::MonthsSinceLastDisaster, monthsSince); set_comment_scalar(context, CommentScalar::WarmWeatherStreak, active_streak_length(state, comment_streak::kWarmWeather, context.monthIndex)); set_comment_scalar(context, CommentScalar::RainyWeatherStreak, active_streak_length(state, comment_streak::kRainyWeather, context.monthIndex)); set_comment_scalar(context, CommentScalar::ColdWeatherStreak, active_streak_length(state, comment_streak::kColdWeather, context.monthIndex)); set_comment_scalar(context, CommentScalar::HotWeatherStreak, active_streak_length(state, comment_streak::kHotWeather, context.monthIndex)); set_comment_scalar(context, CommentScalar::OrdinanceStreak, active_streak_length(state, comment_streak::kOrdinancesActive, context.monthIndex)); set_comment_scalar(context, CommentScalar::LowFundingStreak, active_streak_length(state, comment_streak::kLowServiceFunding, context.monthIndex)); set_comment_scalar(context, CommentScalar::ProsperityStreak, active_streak_length(state, comment_streak::kProsperity, context.monthIndex)); set_comment_flag(context, CommentFlag::DisasterNeverOccurredKnown, neverOccurredKnown);}
} // namespace sc2k::ui