Monopoly — Portage moderne 0.1.0
Portage moderne C/C++23 du Monopoly de 1999
Loading...
Searching...
No Matches
Timers.cpp
Go to the documentation of this file.
1#include "Timers.hpp"
2
3#include "UIMessages.hpp"
4
5#include <array>
6#include <chrono>
7
9{
10 namespace
11 {
12 using Clock = std::chrono::steady_clock;
13
14 struct TimerState
15 {
16 std::uint16_t value = 0;
17 std::uint16_t restartCount = 0;
18
19 std::uint8_t speed =
20 static_cast<std::uint8_t>(BasicClockRateHz / 10);
21
22 std::uint8_t remainingTicks = 1;
23
24 bool sendUIMessage = false;
25 };
26
27 std::array<TimerState, MaxTimers> states{};
28
29 std::uint64_t globalTickCount = 0;
30
31 Clock::time_point previousTime{};
32 std::chrono::duration<double> accumulator{};
33
34 bool initialized = false;
35
36 constexpr std::chrono::duration<double> TickDuration{
37 1.0 / static_cast<double>(BasicClockRateHz)
38 };
39
40 void sendExpiration(
41 std::size_t timerIndex,
42 const TimerState& timer)
43 {
44 // L_UIMsg.cpp original abandonne les expirations des timers
45 // periodiques lorsque la file depasse sa demi-capacite.
46 if (timer.restartCount != 0 &&
48 {
49 return;
50 }
51
52 uimsg::Message message{};
53 message.type = uimsg::Type::TimerReachedZero;
54 message.numberA = static_cast<std::int64_t>(timerIndex);
55 message.numberB = static_cast<std::int64_t>(globalTickCount);
56
57 // Une file UI pleine perdait deja les evenements periodiques
58 // non essentiels dans L_UIMsg.cpp. L'echec est donc volontaire.
59 (void)uimsg::send(message);
60 }
61
62 void tickOnce()
63 {
65
66 for (std::size_t index = 0; index < states.size(); ++index)
67 {
68 TimerState& timer = states[index];
69
70 if (timer.value == 0)
71 {
72 continue;
73 }
74
75 if (--timer.remainingTicks != 0)
76 {
77 continue;
78 }
79
80 timer.remainingTicks = timer.speed;
81
82 if (--timer.value == 0)
83 {
84 timer.value = timer.restartCount;
85
86 if (timer.sendUIMessage)
87 {
88 // Monopoly 1999: LI_TIMERS_UpdateTIMERS().
90 }
91 }
92 }
93 }
94 }
95
97 {
98 states = {};
99
100 for (TimerState& timer : states)
101 {
102 timer.speed =
103 static_cast<std::uint8_t>(BasicClockRateHz / 10);
104
105 timer.remainingTicks = 1;
106 }
107
108 globalTickCount = 0;
109 accumulator = {};
110 previousTime = Clock::now();
111 initialized = true;
112
113 return true;
114 }
115
116 void shutdown()
117 {
118 states = {};
119 globalTickCount = 0;
120 accumulator = {};
121 initialized = false;
122 }
123
125 std::size_t index,
126 std::uint8_t speed,
127 std::uint16_t restartCount,
128 bool sendUIMessage,
129 std::uint16_t value)
130 {
131 if (!initialized || index >= MaxTimers)
132 {
133 return false;
134 }
135
136 TimerState& timer = states[index];
137
138 timer.speed = speed;
139 timer.restartCount = restartCount;
140 timer.sendUIMessage = sendUIMessage;
141 timer.value = value;
142 timer.remainingTicks = 1;
143
144 return true;
145 }
146
147 void pump()
148 {
149 if (!initialized)
150 {
151 return;
152 }
153
154 const Clock::time_point now = Clock::now();
155
158
159 std::uint64_t elapsedTicks = 0;
160
161 while (accumulator >= TickDuration)
162 {
164 ++elapsedTicks;
165 }
166
168 }
169
170 void advanceTicks(std::uint64_t numberOfTicks)
171 {
172 if (!initialized)
173 {
174 return;
175 }
176
177 for (std::uint64_t tick = 0; tick < numberOfTicks; ++tick)
178 {
179 tickOnce();
180 }
181 }
182
183 std::uint64_t tickCount()
184 {
185 return globalTickCount;
186 }
187
188}
unsigned index
std::uint16_t value
Definition Timers.cpp:16
bool sendUIMessage
Definition Timers.cpp:24
std::uint8_t remainingTicks
Definition Timers.cpp:22
std::uint8_t speed
Definition Timers.cpp:19
std::uint16_t restartCount
Definition Timers.cpp:17
std::uint64_t tickCount()
Definition Timers.cpp:183
bool configure(std::size_t index, std::uint8_t speed, std::uint16_t restartCount, bool sendUIMessage, std::uint16_t value)
Definition Timers.cpp:124
constexpr std::uint32_t BasicClockRateHz
Definition Timers.hpp:9
constexpr std::size_t MaxTimers
Definition Timers.hpp:8
void advanceTicks(std::uint64_t numberOfTicks)
Definition Timers.cpp:170
bool initialize()
Definition Timers.cpp:96
constexpr std::size_t QueueCapacity
Definition UIMessages.hpp:9
bool send(const Message &message)
std::size_t size()