OpenTTD Source 20260721-master-g25ec12c62d
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 "../settings_type.h"
23#include "timer.h"
24#include "timer_game_calendar.h"
25#include "../vehicle_base.h"
26
27#include "../safeguards.h"
28
34
40/* static */ TimerGameCalendar::YearMonthDay TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::Date date)
41{
42 /* This wrapper function only exists because economy time sometimes does things differently, when using wallclock units. */
44}
45
54{
55 /* This wrapper function only exists because economy time sometimes does things differently, when using wallclock units. */
57}
58
74
75template <>
76void IntervalTimer<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed trigger)
77{
78 if (trigger == this->period.trigger) {
79 this->callback(1);
80 }
81}
82
83template <>
84void TimeoutTimer<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed trigger)
85{
86 if (this->fired) return;
87
88 if (trigger == this->period.trigger) {
89 this->callback();
90 this->fired = true;
91 }
92}
93
94template <>
95bool TimerManager<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed)
96{
97 if (_game_mode == GameMode::Menu) return false;
98
99 /* If calendar day progress is frozen, don't try to advance time. */
100 if (_settings_game.economy.minutes_per_calendar_year == CalendarTime::FROZEN_MINUTES_PER_YEAR) return false;
101
102 /* If we are using a non-default calendar progression speed, we need to check the sub_date_fract before updating date_fract. */
103 if (_settings_game.economy.minutes_per_calendar_year != CalendarTime::DEF_MINUTES_PER_YEAR) {
105
106 /* Check if we are ready to increment date_fract */
107 const uint16_t threshold = (_settings_game.economy.minutes_per_calendar_year * Ticks::DAY_TICKS) / CalendarTime::DEF_MINUTES_PER_YEAR;
108 if (TimerGameCalendar::sub_date_fract < threshold) return false;
109
111 }
112
114
115 /* Check if we entered a new day. */
119
120 /* Increase day counter. */
122
123 TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
124
125 /* Check if we entered a new month. */
126 bool new_month = ymd.month != TimerGameCalendar::month;
127
128 /* Check if we entered a new year. */
129 bool new_year = ymd.year != TimerGameCalendar::year;
130
131 /* Update internal variables before calling the daily/monthly/yearly loops. */
132 TimerGameCalendar::month = ymd.month;
133 TimerGameCalendar::year = ymd.year;
134
135 /* Make a temporary copy of the timers, as a timer's callback might add/remove other timers. */
137
138 for (auto timer : timers) {
139 timer->Elapsed(TimerGameCalendar::Trigger::Day);
140 }
141
142 if (new_month) {
143 for (auto timer : timers) {
144 timer->Elapsed(TimerGameCalendar::Trigger::Month);
145 }
146 }
147
148 if (new_year) {
149 for (auto timer : timers) {
150 timer->Elapsed(TimerGameCalendar::Trigger::Year);
151 }
152 }
153
154 /* If we reached the maximum year, decrement dates by a year. */
158 TimerGameCalendar::date -= days_this_year;
159 }
160
161 return true;
162}
163
164#ifdef WITH_ASSERT
165template <>
166void TimerManager<TimerGameCalendar>::Validate(TimerGameCalendar::TPeriod period)
167{
168 if (period.priority == TimerGameCalendar::Priority::None) return;
169
170 /* Validate we didn't make a developer error and scheduled more than one
171 * entry on the same priority/trigger. There can only be one timer on
172 * a specific trigger/priority, to ensure we are deterministic. */
173 for (const auto &timer : TimerManager<TimerGameCalendar>::GetTimers()) {
174 if (timer->period.trigger != period.trigger) continue;
175
176 assert(timer->period.priority != period.priority);
177 }
178}
179#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
StrongType::Typedef< int32_t, struct YearTag< struct Calendar >, StrongType::Compare, StrongType::Integer > Year
static YearMonthDay CalendarConvertDateToYMD(Date date)
StrongType::Typedef< int32_t, DateTag< struct Calendar >, StrongType::Compare, StrongType::Integer > 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.
@ Menu
In the main menu.
Definition openttd.h:19
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
Types related to global configuration settings.
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.