Codefix: Documentation comment in IndustryDirectoryWindow (#13059)
[openttd-github.git] / src / timer / timer_game_tick.h
blob02ae2b16ff95b70cd8c3f3614d43e5824de49188
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 /** @file timer_game_tick.h Definition of the tick-based game-timer */
10 #ifndef TIMER_GAME_TICK_H
11 #define TIMER_GAME_TICK_H
13 #include "../gfx_type.h"
15 #include <chrono>
17 /**
18 * Timer that represents the game-ticks. It will pause when the game is paused.
20 * @note Callbacks are executed in the game-thread.
22 class TimerGameTick {
23 public:
24 using Ticks = int32_t; ///< The type to store ticks in
25 using TickCounter = uint64_t; ///< The type that the tick counter is stored in
27 enum Priority {
28 NONE, ///< These timers can be executed in any order; the order is not relevant.
30 /* For all other priorities, the order is important.
31 * For safety, you can only setup a single timer on a single priority. */
32 COMPETITOR_TIMEOUT,
35 struct TPeriod {
36 Priority priority;
37 uint value;
39 TPeriod(Priority priority, uint value) : priority(priority), value(value)
42 bool operator < (const TPeriod &other) const
44 /* Sort by priority before value, such that changes in value for priorities other than NONE do not change the container order */
45 if (this->priority != other.priority) return this->priority < other.priority;
46 return this->value < other.value;
49 bool operator == (const TPeriod &other) const
51 return this->priority == other.priority && this->value == other.value;
55 using TElapsed = uint;
56 struct TStorage {
57 uint elapsed;
60 static TickCounter counter; ///< Monotonic counter, in ticks, since start of game.
63 /**
64 * Storage class for Ticks constants.
66 class Ticks {
67 public:
68 static constexpr TimerGameTick::Ticks INVALID_TICKS = -1; ///< Representation of an invalid number of ticks.
70 /**
71 * 1 day is 74 ticks; TimerGameCalendar::date_fract used to be uint16_t and incremented by 885. On an overflow the new day begun and 65535 / 885 = 74.
72 * 1 tick is approximately 27 ms.
73 * 1 day is thus about 2 seconds (74 * 27 = 1998) on a machine that can run OpenTTD normally
75 static constexpr TimerGameTick::Ticks DAY_TICKS = 74; ///< ticks per day
76 static constexpr TimerGameTick::Ticks TICKS_PER_SECOND = 1000 / MILLISECONDS_PER_TICK; ///< Estimation of how many ticks fit in a single second.
78 static constexpr TimerGameTick::Ticks STATION_RATING_TICKS = 185; ///< Cycle duration for updating station rating.
79 static constexpr TimerGameTick::Ticks STATION_ACCEPTANCE_TICKS = 250; ///< Cycle duration for updating station acceptance.
80 static constexpr TimerGameTick::Ticks STATION_LINKGRAPH_TICKS = 504; ///< Cycle duration for cleaning dead links.
81 static constexpr TimerGameTick::Ticks CARGO_AGING_TICKS = 185; ///< Cycle duration for aging cargo.
82 static constexpr TimerGameTick::Ticks INDUSTRY_PRODUCE_TICKS = 256; ///< Cycle duration for industry production.
83 static constexpr TimerGameTick::Ticks TOWN_GROWTH_TICKS = 70; ///< Cycle duration for towns trying to grow (this originates from the size of the town array in TTD).
84 static constexpr TimerGameTick::Ticks INDUSTRY_CUT_TREE_TICKS = INDUSTRY_PRODUCE_TICKS * 2; ///< Cycle duration for lumber mill's extra action.
87 #endif /* TIMER_GAME_TICK_H */