OpenTTD Source: src/timer/timer_game_common.h Source File

OpenTTD Source 14.1

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef TIMER_GAME_COMMON_H
11 #define TIMER_GAME_COMMON_H
12
13 #include "../core/strong_typedef_type.hpp"
14
30 template <class T>
31 class TimerGame {
32 public:
34 template <class ST> struct DateTag;
35 using Date = StrongType::Typedef<int32_t, DateTag<T>, StrongType::Compare, StrongType::Integer>;
36
38 using DateFract = uint16_t;
39
41 template <class ST> struct YearTag;
42 using Year = StrongType::Typedef<int32_t, struct YearTag<T>, StrongType::Compare, StrongType::Integer>;
44 using Month = uint8_t;
46 using Day = uint8_t;
47
52 struct YearMonthDay {
53 Year year;
54 Month month;
55 Day day;
56 };
57
63 static constexpr bool IsLeapYear(Year year)
64 {
65 int32_t year_as_int = year.base();
66 return year_as_int % 4 == 0 && (year_as_int % 100 != 0 || year_as_int % 400 == 0);
67 }
68
69 static YearMonthDay CalendarConvertDateToYMD(Date date);
70 static Date CalendarConvertYMDToDate(Year year, Month month, Day day);
71
77 static constexpr Year DateToYear(Date date)
78 {
79 /* Hardcode the number of days in a year because we can't access CalendarTime from here. */
80 return date.base() / 366;
81 }
82
88 static constexpr Date DateAtStartOfYear(Year year)
89 {
90 int32_t year_as_int = year.base();
91 uint number_of_leap_years = (year == 0) ? 0 : ((year_as_int - 1) / 4 - (year_as_int - 1) / 100 + (year_as_int - 1) / 400 + 1);
92
93 /* Hardcode the number of days in a year because we can't access CalendarTime from here. */
94 return (365 * year_as_int) + number_of_leap_years;
95 }
96
97 enum Trigger {
98 DAY,
99 WEEK,
100 MONTH,
101 QUARTER,
102 YEAR,
103 };
104
105 enum Priority {
106 NONE,
107
108 /* All other may have a Random() call in them, so order is important.
109 * For safety, you can only setup a single timer on a single priority. */
110 COMPANY,
111 DISASTER,
112 ENGINE,
113 INDUSTRY,
114 STATION,
115 SUBSIDY,
116 TOWN,
117 VEHICLE,
118 };
119
120 struct TPeriod {
121 Trigger trigger;
122 Priority priority;
123
124 TPeriod(Trigger trigger, Priority priority) : trigger(trigger), priority(priority)
125 {}
126
127 bool operator < (const TPeriod &other) const
128 {
129 if (this->trigger != other.trigger) return this->trigger < other.trigger;
130 return this->priority < other.priority;
131 }
132
133 bool operator == (const TPeriod &other) const
134 {
135 return this->trigger == other.trigger && this->priority == other.priority;
136 }
137 };
138
139 using TElapsed = uint;
140 struct TStorage {};
141 };
142
146 template <class T>
147 class TimerGameConst {
148 public:
149 static constexpr int DAYS_IN_YEAR = 365;
150 static constexpr int DAYS_IN_LEAP_YEAR = 366;
151 static constexpr int MONTHS_IN_YEAR = 12;
152
153 static constexpr int SECONDS_PER_DAY = 2;
154
155 /*
156 * ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR and DAYS_TILL_ORIGINAL_BASE_YEAR are
157 * primarily used for loading newgrf and savegame data and returning some
158 * newgrf (callback) functions that were in the original (TTD) inherited
159 * format, where 'TimerGame<T>::date == 0' meant that it was 1920-01-01.
160 */
161
163 static constexpr typename TimerGame<T>::Year ORIGINAL_BASE_YEAR = 1920;
165 static constexpr typename TimerGame<T>::Year ORIGINAL_END_YEAR = 2051;
167 static constexpr typename TimerGame<T>::Year ORIGINAL_MAX_YEAR = 2090;
168
173 static constexpr typename TimerGame<T>::Year MAX_YEAR = 5000000;
174
176 static constexpr typename TimerGame<T>::Year MIN_YEAR = 0;
177
179 static constexpr typename TimerGame<T>::Year DEF_START_YEAR = 1950;
181 static constexpr typename TimerGame<T>::Year DEF_END_YEAR = ORIGINAL_END_YEAR - 1;
182
184 static constexpr typename TimerGame<T>::Date DAYS_TILL_ORIGINAL_BASE_YEAR = TimerGame<T>::DateAtStartOfYear(ORIGINAL_BASE_YEAR);
185
187 static constexpr typename TimerGame<T>::Date MAX_DATE = TimerGame<T>::DateAtStartOfYear(MAX_YEAR + 1) - 1;
188
190 static constexpr typename TimerGame<T>::Date MIN_DATE = 0;
191
192 static constexpr typename TimerGame<T>::Year INVALID_YEAR = -1;
193 static constexpr typename TimerGame<T>::Date INVALID_DATE = -1;
194 };
195
196 #endif /* TIMER_GAME_COMMON_H */
TimerGame::YearMonthDay
Data structure to convert between Date and triplet (year, month, and day).
Definition: timer_game_common.h:52
StrongType::Compare
Mix-in which makes the new Typedef comparable with itself and its base type.
Definition: strong_typedef_type.hpp:22
TimerGameConst::DAYS_TILL_ORIGINAL_BASE_YEAR
static constexpr TimerGame< T >::Date DAYS_TILL_ORIGINAL_BASE_YEAR
The date of the first day of the original base year.
Definition: timer_game_common.h:184
TimerGameConst::INVALID_YEAR
static constexpr TimerGame< T >::Year INVALID_YEAR
Representation of an invalid year.
Definition: timer_game_common.h:192
StrongType::Integer
Mix-in which makes the new Typedef behave more like an integer.
Definition: strong_typedef_type.hpp:52
TimerGame
Template class for all TimerGame based timers.
Definition: timer_game_common.h:31
TimerGame::DateTag
The type to store our dates in.
Definition: timer_game_common.h:34
StrongType::Typedef
Templated helper to make a type-safe 'typedef' representing a single POD value.
Definition: strong_typedef_type.hpp:150
TimerGame< struct Economy >::Priority
Priority
Definition: timer_game_common.h:105
TimerGameConst::DEF_END_YEAR
static constexpr TimerGame< T >::Year DEF_END_YEAR
The default scoring end year.
Definition: timer_game_common.h:181
TimerGame::TStorage
Definition: timer_game_common.h:140
TimerGameConst::MAX_DATE
static constexpr TimerGame< T >::Date MAX_DATE
The date of the last day of the max year.
Definition: timer_game_common.h:187
TimerGameConst::ORIGINAL_END_YEAR
static constexpr TimerGame< T >::Year ORIGINAL_END_YEAR
The original ending year.
Definition: timer_game_common.h:165
TimerGameConst
Template class for time constants shared by both Calendar and Economy time.
Definition: timer_game_common.h:147
TimerGameConst::MIN_DATE
static constexpr TimerGame< T >::Date MIN_DATE
The date on January 1, year 0.
Definition: timer_game_common.h:190
TimerGameConst::DAYS_IN_LEAP_YEAR
static constexpr int DAYS_IN_LEAP_YEAR
sometimes, you need one day more...
Definition: timer_game_common.h:150
TimerGame::NONE
@ NONE
These timers can be executed in any order; there is no Random() in them, so order is not relevant.
Definition: timer_game_common.h:106
TimerGame::DateToYear
static constexpr Year DateToYear(Date date)
Calculate the year of a given date.
Definition: timer_game_common.h:77
TimerGameConst::ORIGINAL_MAX_YEAR
static constexpr TimerGame< T >::Year ORIGINAL_MAX_YEAR
The maximum year of the original TTD.
Definition: timer_game_common.h:167
TimerGame::YearMonthDay::month
Month month
Month (0..11)
Definition: timer_game_common.h:54
TimerGame::TPeriod
Definition: timer_game_common.h:120
TimerGameConst::MAX_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,...
Definition: timer_game_common.h:173
TimerGameConst::DEF_START_YEAR
static constexpr TimerGame< T >::Year DEF_START_YEAR
The default starting year.
Definition: timer_game_common.h:179
TimerGame::DateAtStartOfYear
static constexpr Date DateAtStartOfYear(Year year)
Calculate the date of the first day of a given year.
Definition: timer_game_common.h:88
TimerGameConst::MONTHS_IN_YEAR
static constexpr int MONTHS_IN_YEAR
months per year
Definition: timer_game_common.h:151
TimerGame::CalendarConvertYMDToDate
static Date CalendarConvertYMDToDate(Year year, Month month, Day day)
Converts a tuple of Year, Month and Day to a Date.
Definition: timer_game_common.cpp:124
TimerGame::IsLeapYear
static constexpr bool IsLeapYear(Year year)
Checks whether the given year is a leap year or not.
Definition: timer_game_common.h:63
TimerGameConst::INVALID_DATE
static constexpr TimerGame< T >::Date INVALID_DATE
Representation of an invalid date.
Definition: timer_game_common.h:193
TimerGame::YearTag
Type for the year, note: 0 based, i.e.
Definition: timer_game_common.h:41
TimerGameConst::MIN_YEAR
static constexpr TimerGame< T >::Year MIN_YEAR
The absolute minimum year in OTTD.
Definition: timer_game_common.h:176
TimerGame< struct Economy >::DateFract
uint16_t DateFract
The fraction of a date we're in, i.e.
Definition: timer_game_common.h:38
TimerGame< struct Economy >::Day
uint8_t Day
Type for the day of the month, note: 1 based, first day of a month is 1.
Definition: timer_game_common.h:46
TimerGameConst::ORIGINAL_BASE_YEAR
static constexpr TimerGame< T >::Year ORIGINAL_BASE_YEAR
The minimum starting year/base year of the original TTD.
Definition: timer_game_common.h:163
TimerGameConst::SECONDS_PER_DAY
static constexpr int SECONDS_PER_DAY
approximate seconds per day, not for precise calculations
Definition: timer_game_common.h:153
TimerGame::YearMonthDay::day
Day day
Day (1..31)
Definition: timer_game_common.h:55
TimerGame::YearMonthDay::year
Year year
Year (0...)
Definition: timer_game_common.h:53
TimerGameConst::DAYS_IN_YEAR
static constexpr int DAYS_IN_YEAR
days per year
Definition: timer_game_common.h:149
TimerGame::CalendarConvertDateToYMD
static YearMonthDay CalendarConvertDateToYMD(Date date)
Converts a Date to a Year, Month & Day.
Definition: timer_game_common.cpp:66
TimerGame< struct Economy >::Month
uint8_t Month
Type for the month, note: 0 based, i.e.
Definition: timer_game_common.h:44