OpenTTD Source 20260107-master-g88a467db19
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
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
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
53{
54 /* This wrapper function only exists because economy time sometimes does things differently, when using wallclock units. */
56}
57
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([[maybe_unused]] TimerGameCalendar::TElapsed delta)
95{
96 assert(delta == 1);
97
98 if (_game_mode == GM_MENU) return false;
99
100 /* If calendar day progress is frozen, don't try to advance time. */
101 if (_settings_game.economy.minutes_per_calendar_year == CalendarTime::FROZEN_MINUTES_PER_YEAR) return false;
102
103 /* If we are using a non-default calendar progression speed, we need to check the sub_date_fract before updating date_fract. */
104 if (_settings_game.economy.minutes_per_calendar_year != CalendarTime::DEF_MINUTES_PER_YEAR) {
106
107 /* Check if we are ready to increment date_fract */
108 const uint16_t threshold = (_settings_game.economy.minutes_per_calendar_year * Ticks::DAY_TICKS) / CalendarTime::DEF_MINUTES_PER_YEAR;
109 if (TimerGameCalendar::sub_date_fract < threshold) return false;
110
112 }
113
115
116 /* Check if we entered a new day. */
120
121 /* Increase day counter. */
123
124 TimerGameCalendar::YearMonthDay ymd = TimerGameCalendar::ConvertDateToYMD(TimerGameCalendar::date);
125
126 /* Check if we entered a new month. */
127 bool new_month = ymd.month != TimerGameCalendar::month;
128
129 /* Check if we entered a new year. */
130 bool new_year = ymd.year != TimerGameCalendar::year;
131
132 /* Update internal variables before calling the daily/monthly/yearly loops. */
133 TimerGameCalendar::month = ymd.month;
134 TimerGameCalendar::year = ymd.year;
135
136 /* Make a temporary copy of the timers, as a timer's callback might add/remove other timers. */
138
139 for (auto timer : timers) {
140 timer->Elapsed(TimerGameCalendar::DAY);
141 }
142
143 if (new_month) {
144 for (auto timer : timers) {
145 timer->Elapsed(TimerGameCalendar::MONTH);
146 }
147 }
148
149 if (new_year) {
150 for (auto timer : timers) {
151 timer->Elapsed(TimerGameCalendar::YEAR);
152 }
153 }
154
155 /* If we reached the maximum year, decrement dates by a year. */
159 TimerGameCalendar::date -= days_this_year;
160 }
161
162 return true;
163}
164
165#ifdef WITH_ASSERT
166template <>
167void TimerManager<TimerGameCalendar>::Validate(TimerGameCalendar::TPeriod period)
168{
169 if (period.priority == TimerGameCalendar::Priority::NONE) return;
170
171 /* Validate we didn't make a developer error and scheduled more than one
172 * entry on the same priority/trigger. There can only be one timer on
173 * a specific trigger/priority, to ensure we are deterministic. */
174 for (const auto &timer : TimerManager<TimerGameCalendar>::GetTimers()) {
175 if (timer->period.trigger != period.trigger) continue;
176
177 assert(timer->period.priority != period.priority);
178 }
179}
180#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.
Timer that is increased every 27ms, and counts towards ticks / days / months / years.
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 int DAYS_IN_YEAR
days per year
static constexpr TimerGame< struct Calendar >::Year MAX_YEAR
MAX_YEAR, nicely rounded value of the number of years that can be encoded in a single 32 bits date,...
static constexpr int DAYS_IN_LEAP_YEAR
sometimes, you need one day more...
static YearMonthDay CalendarConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
uint16_t DateFract
The fraction of a date we're in, i.e.
static Date CalendarConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
uint8_t Day
Type for the day of the month, note: 1 based, first day of a month is 1.
uint8_t Month
Type for the month, note: 0 based, i.e.
static constexpr bool IsLeapYear(Year year)
Checks whether the given year is a leap year or not.
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.
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition settings.cpp:61
uint16_t minutes_per_calendar_year
minutes per calendar year. Special value 0 means that calendar time is frozen.
EconomySettings economy
settings to change the economy
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition of Interval and OneShot timers.
Definition of the game-calendar-timer.