OpenTTD Source 20260206-master-g4d4e37dbf1
timer_game_calendar.cpp
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
19
20#include "../stdafx.h"
21#include "../openttd.h"
22#include "timer.h"
23#include "timer_game_calendar.h"
24#include "../vehicle_base.h"
25
26#include "../safeguards.h"
27
28TimerGameCalendar::Year TimerGameCalendar::year = {};
30TimerGameCalendar::Date TimerGameCalendar::date = {};
33
39/* static */ TimerGameCalendar::YearMonthDay TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::Date date)
40{
41 /* This wrapper function only exists because economy time sometimes does things differently, when using wallclock units. */
43}
44
52/* static */ TimerGameCalendar::Date TimerGameCalendar::ConvertYMDToDate(TimerGameCalendar::Year year, TimerGameCalendar::Month month, TimerGameCalendar::Day day)
53{
54 /* This wrapper function only exists because economy time sometimes does things differently, when using wallclock units. */
56}
57
63/* static */ void TimerGameCalendar::SetDate(TimerGameCalendar::Date date, TimerGameCalendar::DateFract fract)
64{
65 assert(fract < Ticks::DAY_TICKS);
66
69 TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(date);
70 TimerGameCalendar::year = TimerGameCalendar::Year{ymd.year};
71 TimerGameCalendar::month = ymd.month;
72}
73
74template <>
75void IntervalTimer<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed trigger)
76{
77 if (trigger == this->period.trigger) {
78 this->callback(1);
79 }
80}
81
82template <>
83void TimeoutTimer<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed trigger)
84{
85 if (this->fired) return;
86
87 if (trigger == this->period.trigger) {
88 this->callback();
89 this->fired = true;
90 }
91}
92
93template <>
94bool TimerManager<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed)
95{
96 if (_game_mode == GM_MENU) return false;
97
98 /* If calendar day progress is frozen, don't try to advance time. */
99 if (_settings_game.economy.minutes_per_calendar_year == CalendarTime::FROZEN_MINUTES_PER_YEAR) return false;
100
101 /* If we are using a non-default calendar progression speed, we need to check the sub_date_fract before updating date_fract. */
102 if (_settings_game.economy.minutes_per_calendar_year != CalendarTime::DEF_MINUTES_PER_YEAR) {
104
105 /* Check if we are ready to increment date_fract */
106 const uint16_t threshold = (_settings_game.economy.minutes_per_calendar_year * Ticks::DAY_TICKS) / CalendarTime::DEF_MINUTES_PER_YEAR;
107 if (TimerGameCalendar::sub_date_fract < threshold) return false;
108
110 }
111
113
114 /* Check if we entered a new day. */
118
119 /* Increase day counter. */
121
122 TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
123
124 /* Check if we entered a new month. */
125 bool new_month = ymd.month != TimerGameCalendar::month;
126
127 /* Check if we entered a new year. */
128 bool new_year = ymd.year != TimerGameCalendar::year;
129
130 /* Update internal variables before calling the daily/monthly/yearly loops. */
131 TimerGameCalendar::month = ymd.month;
132 TimerGameCalendar::year = ymd.year;
133
134 /* Make a temporary copy of the timers, as a timer's callback might add/remove other timers. */
136
137 for (auto timer : timers) {
138 timer->Elapsed(TimerGameCalendar::Trigger::Day);
139 }
140
141 if (new_month) {
142 for (auto timer : timers) {
143 timer->Elapsed(TimerGameCalendar::Trigger::Month);
144 }
145 }
146
147 if (new_year) {
148 for (auto timer : timers) {
149 timer->Elapsed(TimerGameCalendar::Trigger::Year);
150 }
151 }
152
153 /* If we reached the maximum year, decrement dates by a year. */
157 TimerGameCalendar::date -= days_this_year;
158 }
159
160 return true;
161}
162
163#ifdef WITH_ASSERT
164template <>
165void TimerManager<TimerGameCalendar>::Validate(TimerGameCalendar::TPeriod period)
166{
167 if (period.priority == TimerGameCalendar::Priority::None) return;
168
169 /* Validate we didn't make a developer error and scheduled more than one
170 * entry on the same priority/trigger. There can only be one timer on
171 * a specific trigger/priority, to ensure we are deterministic. */
172 for (const auto &timer : TimerManager<TimerGameCalendar>::GetTimers()) {
173 if (timer->period.trigger != period.trigger) continue;
174
175 assert(timer->period.priority != period.priority);
176 }
177}
178#endif /* WITH_ASSERT */
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
static constexpr TimerGameTick::Ticks DAY_TICKS
1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885.
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
static uint16_t sub_date_fract
Subpart of date_fract that we use when calendar days are slower than economy days.
static Month month
Current month (0..11).
static Date ConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
static YearMonthDay ConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
static void SetDate(Date date, DateFract fract)
Set the date.
static Date date
Current date in days (day counter).
static Year year
Current year, starting at 0.
static DateFract date_fract
Fractional part of the day.
static constexpr TimerGame< struct Calendar >::Year MAX_YEAR
static YearMonthDay CalendarConvertDateToYMD(Date date)
static Date CalendarConvertYMDToDate(Year year, Month month, Day day)
static constexpr bool IsLeapYear(Year year)
The TimerManager manages a single Timer-type.
static std::set< BaseTimer< TTimerType > *, base_timer_sorter > & GetTimers()
Singleton list, to store all the active timers.
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
Some generic types.
A number of safeguards to prevent using unsafe methods.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
Definition of base types and functions in a cross-platform compatible way.
Definition of Interval and OneShot timers.
Definition of the game-calendar-timer.
Base class for all vehicles.