OpenTTD Source 20241224-master-gf74b0cf984
timer_game_tick.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 <http://www.gnu.org/licenses/>.
6 */
7
13#include "../stdafx.h"
14#include "timer.h"
15#include "timer_game_tick.h"
16
17#include "../safeguards.h"
18
20
21template<>
22void IntervalTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
23{
24 if (this->period.value == 0) return;
25
26 this->storage.elapsed += delta;
27
28 uint count = 0;
29 while (this->storage.elapsed >= this->period.value) {
30 this->storage.elapsed -= this->period.value;
31 count++;
32 }
33
34 if (count > 0) {
35 this->callback(count);
36 }
37}
38
39template<>
40void TimeoutTimer<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
41{
42 if (this->fired) return;
43 if (this->period.value == 0) return;
44
45 this->storage.elapsed += delta;
46
47 if (this->storage.elapsed >= this->period.value) {
48 this->callback();
49 this->fired = true;
50 }
51}
52
53template<>
54bool TimerManager<TimerGameTick>::Elapsed(TimerGameTick::TElapsed delta)
55{
57
58 for (auto timer : TimerManager<TimerGameTick>::GetTimers()) {
59 timer->Elapsed(delta);
60 }
61
62 return true;
63}
64
65#ifdef WITH_ASSERT
66template<>
68{
69 if (period.priority == TimerGameTick::Priority::NONE) return;
70
71 /* Validate we didn't make a developer error and scheduled more than one
72 * entry on the same priority. There can only be one timer on
73 * a specific priority, to ensure we are deterministic, and to avoid
74 * container sort order invariant issues with timer period saveload. */
75 for (const auto &timer : TimerManager<TimerGameTick>::GetTimers()) {
76 assert(timer->period.priority != period.priority);
77 }
78}
79#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.
void Elapsed(TElapsed count) override
Called by the timer manager to notify the timer that the given amount of time has elapsed.
Timer that represents the game-ticks.
uint64_t TickCounter
The type that the tick counter is stored in.
static TickCounter counter
Monotonic counter, in ticks, since start of game.
@ NONE
These timers can be executed in any order; the order is not relevant.
The TimerManager manages a single Timer-type.
static bool Elapsed(TElapsed value)
Called when time for this timer elapsed.
Definition of Interval and OneShot timers.
Definition of the tick-based game-timer.