Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / timer / timer_game_economy.cpp
blobcd78aa74af5dad02121fa4474f6f21ed683198c4
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 */
8 /**
9 * @file timer_game_economy.cpp
10 * This file implements the timer logic for the game-economy-timer.
13 /**
14 * Economy time is used for the regular pace of the game, including:
15 * - Industry and house production/consumption
16 * - Industry production changes, closure, and spawning
17 * - Town growth
18 * - Company age and financial statistics
19 * - Vehicle financial statistics
20 * - Vehicle aging, depreciation, reliability, and renewal
21 * - Payment intervals for running and maintenance costs, loan interest, etc.
22 * - Cargo payment "time" calculation
23 * - Local authority and station ratings change intervals
26 #include "../stdafx.h"
27 #include "../openttd.h"
28 #include "timer.h"
29 #include "timer_game_economy.h"
30 #include "timer_game_tick.h"
31 #include "../vehicle_base.h"
32 #include "../linkgraph/linkgraph.h"
34 #include "../safeguards.h"
36 TimerGameEconomy::Year TimerGameEconomy::year = {};
37 TimerGameEconomy::Month TimerGameEconomy::month = {};
38 TimerGameEconomy::Date TimerGameEconomy::date = {};
39 TimerGameEconomy::DateFract TimerGameEconomy::date_fract = {};
41 /**
42 * Converts a Date to a Year, Month & Day.
43 * @param date the date to convert from
44 * @returns YearMonthDay representation of the Date.
46 /* static */ TimerGameEconomy::YearMonthDay TimerGameEconomy::ConvertDateToYMD(TimerGameEconomy::Date date)
48 /* If we're not using wallclock units, we keep the economy date in sync with the calendar. */
49 if (!UsingWallclockUnits()) return CalendarConvertDateToYMD(date);
51 /* If we're using wallclock units, economy months have 30 days and an economy year has 360 days. */
52 TimerGameEconomy::YearMonthDay ymd;
53 ymd.year = date.base() / EconomyTime::DAYS_IN_ECONOMY_YEAR;
54 ymd.month = (date.base() % EconomyTime::DAYS_IN_ECONOMY_YEAR) / EconomyTime::DAYS_IN_ECONOMY_MONTH;
55 ymd.day = (date.base() % EconomyTime::DAYS_IN_ECONOMY_MONTH) + 1;
56 return ymd;
59 /**
60 * Converts a tuple of Year, Month and Day to a Date.
61 * @param year is a number between 0..MAX_YEAR
62 * @param month is a number between 0..11
63 * @param day is a number between 1..31
64 * @returns The equivalent date.
66 /* static */ TimerGameEconomy::Date TimerGameEconomy::ConvertYMDToDate(TimerGameEconomy::Year year, TimerGameEconomy::Month month, TimerGameEconomy::Day day)
68 /* If we're not using wallclock units, we keep the economy date in sync with the calendar. */
69 if (!UsingWallclockUnits()) return CalendarConvertYMDToDate(year, month, day);
71 /* If we're using wallclock units, economy months have 30 days and an economy year has 360 days. */
72 const int total_months = (year.base() * EconomyTime::MONTHS_IN_YEAR) + month;
73 return (total_months * EconomyTime::DAYS_IN_ECONOMY_MONTH) + day - 1; // Day is 1-indexed but Date is 0-indexed, hence the - 1.
76 /**
77 * Set the date.
78 * @param date The new date
79 * @param fract The number of ticks that have passed on this date.
81 /* static */ void TimerGameEconomy::SetDate(TimerGameEconomy::Date date, TimerGameEconomy::DateFract fract)
83 assert(fract < Ticks::DAY_TICKS);
85 TimerGameEconomy::date = date;
86 TimerGameEconomy::date_fract = fract;
87 TimerGameEconomy::YearMonthDay ymd = TimerGameEconomy::ConvertDateToYMD(date);
88 TimerGameEconomy::year = ymd.year;
89 TimerGameEconomy::month = ymd.month;
92 /**
93 * Check if we are using wallclock units.
94 * @param newgame Should we check the settings for a new game (since we are in the main menu)?
95 * @return True if the game is using wallclock units, or false if the game is using calendar units.
97 /* static */ bool TimerGameEconomy::UsingWallclockUnits(bool newgame)
99 if (newgame) return (_settings_newgame.economy.timekeeping_units == TKU_WALLCLOCK);
101 return (_settings_game.economy.timekeeping_units == TKU_WALLCLOCK);
104 template<>
105 void IntervalTimer<TimerGameEconomy>::Elapsed(TimerGameEconomy::TElapsed trigger)
107 if (trigger == this->period.trigger) {
108 this->callback(1);
112 template<>
113 void TimeoutTimer<TimerGameEconomy>::Elapsed(TimerGameEconomy::TElapsed trigger)
115 if (this->fired) return;
117 if (trigger == this->period.trigger) {
118 this->callback();
119 this->fired = true;
123 template<>
124 bool TimerManager<TimerGameEconomy>::Elapsed([[maybe_unused]] TimerGameEconomy::TElapsed delta)
126 assert(delta == 1);
128 if (_game_mode == GM_MENU) return false;
130 TimerGameEconomy::date_fract++;
131 if (TimerGameEconomy::date_fract < Ticks::DAY_TICKS) return true;
132 TimerGameEconomy::date_fract = 0;
134 /* increase day counter */
135 TimerGameEconomy::date++;
137 TimerGameEconomy::YearMonthDay ymd = TimerGameEconomy::ConvertDateToYMD(TimerGameEconomy::date);
139 /* check if we entered a new month? */
140 bool new_month = ymd.month != TimerGameEconomy::month;
142 /* check if we entered a new year? */
143 bool new_year = ymd.year != TimerGameEconomy::year;
145 /* update internal variables before calling the daily/monthly/yearly loops */
146 TimerGameEconomy::month = ymd.month;
147 TimerGameEconomy::year = ymd.year;
149 /* Make a temporary copy of the timers, as a timer's callback might add/remove other timers. */
150 auto timers = TimerManager<TimerGameEconomy>::GetTimers();
152 for (auto timer : timers) {
153 timer->Elapsed(TimerGameEconomy::DAY);
156 if ((TimerGameEconomy::date.base() % 7) == 3) {
157 for (auto timer : timers) {
158 timer->Elapsed(TimerGameEconomy::WEEK);
162 if (new_month) {
163 for (auto timer : timers) {
164 timer->Elapsed(TimerGameEconomy::MONTH);
167 if ((TimerGameEconomy::month % 3) == 0) {
168 for (auto timer : timers) {
169 timer->Elapsed(TimerGameEconomy::QUARTER);
174 if (new_year) {
175 for (auto timer : timers) {
176 timer->Elapsed(TimerGameEconomy::YEAR);
180 /* check if we reached the maximum year, decrement dates by a year */
181 if (TimerGameEconomy::year == EconomyTime::MAX_YEAR + 1) {
182 int days_this_year;
184 TimerGameEconomy::year--;
185 days_this_year = TimerGameEconomy::IsLeapYear(TimerGameEconomy::year) ? EconomyTime::DAYS_IN_LEAP_YEAR : EconomyTime::DAYS_IN_YEAR;
186 TimerGameEconomy::date -= days_this_year;
187 for (Vehicle *v : Vehicle::Iterate()) v->ShiftDates(-days_this_year);
188 for (LinkGraph *lg : LinkGraph::Iterate()) lg->ShiftDates(-days_this_year);
191 return true;
194 #ifdef WITH_ASSERT
195 template<>
196 void TimerManager<TimerGameEconomy>::Validate(TimerGameEconomy::TPeriod period)
198 if (period.priority == TimerGameEconomy::Priority::NONE) return;
200 /* Validate we didn't make a developer error and scheduled more than one
201 * entry on the same priority/trigger. There can only be one timer on
202 * a specific trigger/priority, to ensure we are deterministic. */
203 for (const auto &timer : TimerManager<TimerGameEconomy>::GetTimers()) {
204 if (timer->period.trigger != period.trigger) continue;
206 assert(timer->period.priority != period.priority);
209 #endif /* WITH_ASSERT */