OpenTTD Source 20261112-master-gbcbec5e63f
timer_game_common.h
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
9
10#ifndef TIMER_GAME_COMMON_H
11#define TIMER_GAME_COMMON_H
12
14
30template <class T>
31class TimerGame {
32public:
34 template <class ST> struct DateTag;
37
39 using DateFract = uint16_t;
40
42 template <class ST> struct YearTag;
46 using Month = uint8_t;
48 using Day = uint8_t;
49
59
65 static constexpr bool IsLeapYear(Year year)
66 {
67 int32_t year_as_int = year.base();
68 return year_as_int % 4 == 0 && (year_as_int % 100 != 0 || year_as_int % 400 == 0);
69 }
70
71 static YearMonthDay CalendarConvertDateToYMD(Date date);
72 static Date CalendarConvertYMDToDate(Year year, Month month, Day day);
73
79 static constexpr Year DateToYear(Date date)
80 {
81 /* Hardcode the number of days in a year because we can't access CalendarTime from here. */
82 return Year{date.base() / 366};
83 }
84
90 static constexpr Date DateAtStartOfYear(Year year)
91 {
92 int32_t year_as_int = year.base();
93 int32_t number_of_leap_years = (year == 0) ? 0 : ((year_as_int - 1) / 4 - (year_as_int - 1) / 100 + (year_as_int - 1) / 400 + 1);
94
95 /* Hardcode the number of days in a year because we can't access CalendarTime from here. */
96 return Date{(365 * year_as_int) + number_of_leap_years};
97 }
98
100 enum class Trigger : uint8_t {
106 };
107
109 enum class Priority : uint8_t {
111
112 /* All other may have a Random() call in them, so order is important.
113 * For safety, you can only setup a single timer on a single priority. */
114
123 };
124
125 struct TPeriod {
126 Trigger trigger;
127 Priority priority;
128
129 TPeriod(Trigger trigger, Priority priority) : trigger(trigger), priority(priority)
130 {}
131
137 constexpr auto operator<=>(const TPeriod &other) const noexcept
138 {
139 if (this->trigger != other.trigger) return this->trigger <=> other.trigger;
140 return this->priority <=> other.priority;
141 }
142
148 constexpr bool operator==(const TPeriod &other) const noexcept
149 {
150 return this->trigger == other.trigger && this->priority == other.priority;
151 }
152 };
153
154 using TElapsed = Trigger;
155 struct TStorage {};
156};
157
161template <class T>
163public:
164 static constexpr int DAYS_IN_YEAR = 365;
165 static constexpr int DAYS_IN_LEAP_YEAR = 366;
166 static constexpr int MONTHS_IN_YEAR = 12;
167
168 static constexpr int SECONDS_PER_DAY = 2;
169
170 /*
171 * ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR and DAYS_TILL_ORIGINAL_BASE_YEAR are
172 * primarily used for loading newgrf and savegame data and returning some
173 * newgrf (callback) functions that were in the original (TTD) inherited
174 * format, where 'TimerGame<T>::date == 0' meant that it was 1920-01-01.
175 */
176
178 static constexpr typename TimerGame<T>::Year ORIGINAL_BASE_YEAR{1920};
180 static constexpr typename TimerGame<T>::Year ORIGINAL_END_YEAR{2051};
182 static constexpr typename TimerGame<T>::Year ORIGINAL_MAX_YEAR{2090};
183
188 static constexpr typename TimerGame<T>::Year MAX_YEAR{5000000};
189
191 static constexpr typename TimerGame<T>::Year MIN_YEAR{0};
192
194 static constexpr typename TimerGame<T>::Year DEF_START_YEAR{1950};
196 static constexpr typename TimerGame<T>::Year DEF_END_YEAR{ORIGINAL_END_YEAR - 1};
197
200
203
205 static constexpr typename TimerGame<T>::Date MIN_DATE{0};
206
207 static constexpr typename TimerGame<T>::Year INVALID_YEAR{-1};
208 static constexpr typename TimerGame<T>::Date INVALID_DATE{-1};
209};
210
211#endif /* TIMER_GAME_COMMON_H */
Template class for time constants shared by both Calendar and Economy time.
static constexpr TimerGame< T >::Year DEF_START_YEAR
The default starting year.
static constexpr TimerGame< T >::Date MIN_DATE
The date on January 1, year 0.
static constexpr TimerGame< T >::Year DEF_END_YEAR
The default scoring end year.
static constexpr TimerGame< T >::Year ORIGINAL_MAX_YEAR
The maximum year of the original TTD.
static constexpr int MONTHS_IN_YEAR
months per year
static constexpr TimerGame< T >::Year ORIGINAL_END_YEAR
The original ending year.
static constexpr TimerGame< T >::Year INVALID_YEAR
Representation of an invalid year.
static constexpr TimerGame< T >::Year MIN_YEAR
The absolute minimum year in OTTD.
static constexpr TimerGame< T >::Date INVALID_DATE
Representation of an invalid date.
static constexpr int SECONDS_PER_DAY
approximate seconds per day, not for precise calculations
static constexpr TimerGame< T >::Date MAX_DATE
The date of the last day of the max year.
static constexpr int DAYS_IN_YEAR
days per year
static constexpr TimerGame< T >::Year ORIGINAL_BASE_YEAR
The minimum starting year/base year of the original TTD.
static constexpr TimerGame< T >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
The date of the first day of the original base year.
static constexpr TimerGame< T >::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...
Template class for all TimerGame based timers.
static constexpr Year DateToYear(Date date)
Calculate the year of a given date.
StrongType::Typedef< int32_t, struct YearTag< T >, StrongType::Compare, StrongType::Integer > Year
Type for the year, note: 0 based, i.e.
static YearMonthDay CalendarConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
static constexpr Date DateAtStartOfYear(Year year)
Calculate the date of the first day of a given year.
Trigger
Trigger reasons for the timer based triggers.
@ Quarter
Triggered every first of January, April, July and October.
@ Week
Triggers every Tuesday.
StrongType::Typedef< int32_t, DateTag< T >, StrongType::Compare, StrongType::Integer > Date
The type to store our dates in.
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.
Priority
Different levels of priority to run the timers in.
@ None
These timers can be executed in any order; there is no Random() in them, so order is not relevant.
@ Disaster
Running disaster logic.
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.
Type (helpers) for making a strong typedef that is a distinct type.
Defines the internal data of a functional industry.
Definition industry.h:64
Station data structure.
Mix-in which makes the new Typedef comparable with itself and its base type.
Mix-in which makes the new Typedef behave more like an integer.
Templated helper to make a type-safe 'typedef' representing a single POD value.
Struct about subsidies, offered and awarded.
The tag for making Date StrongType.
constexpr auto operator<=>(const TPeriod &other) const noexcept
Compare with another instance of this class.
constexpr bool operator==(const TPeriod &other) const noexcept
Compare with another instance of this class.
Data structure to convert between Date and triplet (year, month, and day).
Month month
Month (0..11).
The tag for making Year StrongType.
Town data structure.
Definition town.h:64
Vehicle data structure.