From 724266616d7dd7af8b298d329cd736fa719a711c Mon Sep 17 00:00:00 2001 From: Tyler Trahan Date: Sun, 21 Jan 2024 10:54:17 -0500 Subject: [PATCH] Change: Only run vehicle calendar day proc when TimerGameCalendar::date_fract has advanced --- src/openttd.cpp | 5 +++-- src/timer/timer_game_calendar.cpp | 12 +++++++----- src/timer/timer_game_economy.cpp | 8 +++++--- src/timer/timer_game_realtime.cpp | 4 +++- src/timer/timer_game_tick.cpp | 4 +++- src/timer/timer_manager.h | 3 ++- src/timer/timer_window.cpp | 4 +++- 7 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/openttd.cpp b/src/openttd.cpp index 9a6db6259d..200b9d3d87 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1472,11 +1472,12 @@ void StateGameLoop() BasePersistentStorageArray::SwitchMode(PSM_ENTER_GAMELOOP); AnimateAnimatedTiles(); - TimerManager::Elapsed(1); + if (TimerManager::Elapsed(1)) { + RunVehicleCalendarDayProc(); + } TimerManager::Elapsed(1); TimerManager::Elapsed(1); RunTileLoop(); - RunVehicleCalendarDayProc(); CallVehicleTicks(); CallLandscapeTick(); BasePersistentStorageArray::SwitchMode(PSM_LEAVE_GAMELOOP); diff --git a/src/timer/timer_game_calendar.cpp b/src/timer/timer_game_calendar.cpp index a004a77f15..3b7bc870a8 100644 --- a/src/timer/timer_game_calendar.cpp +++ b/src/timer/timer_game_calendar.cpp @@ -94,27 +94,27 @@ void TimeoutTimer::Elapsed(TimerGameCalendar::TElapsed trigge } template<> -void TimerManager::Elapsed([[maybe_unused]] TimerGameCalendar::TElapsed delta) +bool TimerManager::Elapsed([[maybe_unused]] TimerGameCalendar::TElapsed delta) { assert(delta == 1); - if (_game_mode == GM_MENU) return; + if (_game_mode == GM_MENU) return false; /* If calendar day progress is frozen, don't try to advance time. */ - if (_settings_game.economy.minutes_per_calendar_year == CalendarTime::FROZEN_MINUTES_PER_YEAR) return; + if (_settings_game.economy.minutes_per_calendar_year == CalendarTime::FROZEN_MINUTES_PER_YEAR) return false; /* If we are using a non-default calendar progression speed, we need to check the sub_date_fract before updating date_fract. */ if (_settings_game.economy.minutes_per_calendar_year != CalendarTime::DEF_MINUTES_PER_YEAR) { TimerGameCalendar::sub_date_fract++; /* Check if we are ready to increment date_fract */ - if (TimerGameCalendar::sub_date_fract < (Ticks::DAY_TICKS * _settings_game.economy.minutes_per_calendar_year) / CalendarTime::DEF_MINUTES_PER_YEAR) return; + if (TimerGameCalendar::sub_date_fract < (Ticks::DAY_TICKS * _settings_game.economy.minutes_per_calendar_year) / CalendarTime::DEF_MINUTES_PER_YEAR) return false; } TimerGameCalendar::date_fract++; /* Check if we entered a new day. */ - if (TimerGameCalendar::date_fract < Ticks::DAY_TICKS) return; + if (TimerGameCalendar::date_fract < Ticks::DAY_TICKS) return true; TimerGameCalendar::date_fract = 0; TimerGameCalendar::sub_date_fract = 0; @@ -160,6 +160,8 @@ void TimerManager::Elapsed([[maybe_unused]] TimerGameCalendar days_this_year = TimerGameCalendar::IsLeapYear(TimerGameCalendar::year) ? CalendarTime::DAYS_IN_LEAP_YEAR : CalendarTime::DAYS_IN_YEAR; TimerGameCalendar::date -= days_this_year; } + + return true; } #ifdef WITH_ASSERT diff --git a/src/timer/timer_game_economy.cpp b/src/timer/timer_game_economy.cpp index 474fcfd461..62f6806213 100644 --- a/src/timer/timer_game_economy.cpp +++ b/src/timer/timer_game_economy.cpp @@ -121,14 +121,14 @@ void TimeoutTimer::Elapsed(TimerGameEconomy::TElapsed trigger) } template<> -void TimerManager::Elapsed([[maybe_unused]] TimerGameEconomy::TElapsed delta) +bool TimerManager::Elapsed([[maybe_unused]] TimerGameEconomy::TElapsed delta) { assert(delta == 1); - if (_game_mode == GM_MENU) return; + if (_game_mode == GM_MENU) return false; TimerGameEconomy::date_fract++; - if (TimerGameEconomy::date_fract < Ticks::DAY_TICKS) return; + if (TimerGameEconomy::date_fract < Ticks::DAY_TICKS) return true; TimerGameEconomy::date_fract = 0; /* increase day counter */ @@ -187,6 +187,8 @@ void TimerManager::Elapsed([[maybe_unused]] TimerGameEconomy:: for (Vehicle *v : Vehicle::Iterate()) v->ShiftDates(-days_this_year); for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(-days_this_year); } + + return true; } #ifdef WITH_ASSERT diff --git a/src/timer/timer_game_realtime.cpp b/src/timer/timer_game_realtime.cpp index 62b10f9b05..600e24f317 100644 --- a/src/timer/timer_game_realtime.cpp +++ b/src/timer/timer_game_realtime.cpp @@ -54,11 +54,13 @@ void TimeoutTimer::Elapsed(TimerGameRealtime::TElapsed delta) } template<> -void TimerManager::Elapsed(TimerGameRealtime::TElapsed delta) +bool TimerManager::Elapsed(TimerGameRealtime::TElapsed delta) { for (auto timer : TimerManager::GetTimers()) { timer->Elapsed(delta); } + + return true; } #ifdef WITH_ASSERT diff --git a/src/timer/timer_game_tick.cpp b/src/timer/timer_game_tick.cpp index 225204b668..92399a4e63 100644 --- a/src/timer/timer_game_tick.cpp +++ b/src/timer/timer_game_tick.cpp @@ -51,13 +51,15 @@ void TimeoutTimer::Elapsed(TimerGameTick::TElapsed delta) } template<> -void TimerManager::Elapsed(TimerGameTick::TElapsed delta) +bool TimerManager::Elapsed(TimerGameTick::TElapsed delta) { TimerGameTick::counter++; for (auto timer : TimerManager::GetTimers()) { timer->Elapsed(delta); } + + return true; } #ifdef WITH_ASSERT diff --git a/src/timer/timer_manager.h b/src/timer/timer_manager.h index ca87ec4edc..c3b45a73bd 100644 --- a/src/timer/timer_manager.h +++ b/src/timer/timer_manager.h @@ -78,8 +78,9 @@ public: * Call the Elapsed() method of all active timers. * * @param value The amount of time that has elapsed. + * @return True iff time has progressed. */ - static void Elapsed(TElapsed value); + static bool Elapsed(TElapsed value); private: /** diff --git a/src/timer/timer_window.cpp b/src/timer/timer_window.cpp index bc6a10530d..906d5e0a9f 100644 --- a/src/timer/timer_window.cpp +++ b/src/timer/timer_window.cpp @@ -49,7 +49,7 @@ void TimeoutTimer::Elapsed(TimerWindow::TElapsed delta) } template<> -void TimerManager::Elapsed(TimerWindow::TElapsed delta) +bool TimerManager::Elapsed(TimerWindow::TElapsed delta) { /* Make a temporary copy of the timers, as a timer's callback might add/remove other timers. */ auto timers = TimerManager::GetTimers(); @@ -57,6 +57,8 @@ void TimerManager::Elapsed(TimerWindow::TElapsed delta) for (auto timer : timers) { timer->Elapsed(delta); } + + return true; } #ifdef WITH_ASSERT