1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program 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; either version 2
* of the License, or (at your option) any later version.
* This program 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. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "kyra/kyra.h"
#include "kyra/timer.h"
#include "common/func.h"
#include "common/stream.h"
namespace Kyra {
namespace {
struct TimerResync : public Common::UnaryFunction<TimerEntry&, void> {
uint32 _tickLength, _curTime;
TimerResync(KyraEngine *vm, uint32 curTime) : _tickLength(vm->tickLength()), _curTime(curTime) {}
void operator()(TimerEntry &entry) const {
if (entry.lastUpdate < 0) {
if ((uint32)(ABS(entry.lastUpdate)) >= entry.countdown * _tickLength)
entry.nextRun = 0;
else
entry.nextRun = _curTime + entry.lastUpdate + entry.countdown * _tickLength;
} else {
uint32 nextRun = entry.lastUpdate + entry.countdown * _tickLength;
if (_curTime < nextRun)
nextRun = 0;
entry.nextRun = nextRun;
}
}
};
struct TimerEqual : public Common::UnaryFunction<const TimerEntry&, bool> {
uint8 _id;
TimerEqual(uint8 id) : _id(id) {}
bool operator()(const TimerEntry &entry) const {
return entry.id == _id;
}
};
} // end of anonymous namespace
void TimerManager::reset() {
for (Iterator pos = _timers.begin(); pos != _timers.end(); ++pos) {
delete pos->func;
}
_timers.clear();
}
void TimerManager::addTimer(uint8 id, TimerFunc *func, int countdown, bool enabled) {
debugC(9, kDebugLevelTimer, "TimerManager::addTimer(%d, %p, %d, %d)", id, (const void*)func, countdown, enabled);
Iterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end()) {
warning("Adding allready existing timer %d", id);
return;
}
TimerEntry newTimer;
newTimer.id = id;
newTimer.countdown = countdown;
newTimer.enabled = enabled ? 1 : 0;
newTimer.lastUpdate = newTimer.nextRun = 0;
newTimer.func = func;
_timers.push_back(newTimer);
}
void TimerManager::update() {
debugC(9, kDebugLevelTimer, "TimerManager::update()");
if (_system->getMillis() < _nextRun)
return;
_nextRun += 99999;
for (Iterator pos = _timers.begin(); pos != _timers.end(); ++pos) {
if (pos->enabled && pos->countdown >= 0 && pos->nextRun <= _system->getMillis()) {
if (pos->func && pos->func->isValid())
(*pos->func)(pos->id);
uint32 curTime = _system->getMillis();
pos->lastUpdate = curTime;
pos->nextRun = curTime + pos->countdown * _vm->tickLength();
_nextRun = MIN(_nextRun, pos->nextRun);
}
}
}
void TimerManager::resync() {
debugC(9, kDebugLevelTimer, "TimerManager::resync()");
_nextRun = 0; // force rerun
Common::for_each(_timers.begin(), _timers.end(), TimerResync(_vm, _system->getMillis()));
}
void TimerManager::resetNextRun() {
debugC(9, kDebugLevelTimer, "TimerManager::resetNextRun()");
_nextRun = 0;
}
void TimerManager::setCountdown(uint8 id, int32 countdown) {
debugC(9, kDebugLevelTimer, "TimerManager::setCountdown(%d, %d)", id, countdown);
Iterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end()) {
timer->countdown = countdown;
if (countdown >= 0) {
uint32 curTime = _system->getMillis();
timer->lastUpdate = curTime;
timer->nextRun = curTime + countdown * _vm->tickLength();
_nextRun = MIN(_nextRun, timer->nextRun);
}
} else {
warning("TimerManager::setCountdown: No timer %d", id);
}
}
void TimerManager::setDelay(uint8 id, int32 countdown) {
debugC(9, kDebugLevelTimer, "TimerManager::setDelay(%d, %d)", id, countdown);
Iterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end())
timer->countdown = countdown;
else
warning("TimerManager::setDelay: No timer %d", id);
}
int32 TimerManager::getDelay(uint8 id) const {
debugC(9, kDebugLevelTimer, "TimerManager::getDelay(%d)", id);
CIterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end())
return timer->countdown;
warning("TimerManager::getDelay: No timer %d", id);
return -1;
}
void TimerManager::setNextRun(uint8 id, uint32 nextRun) {
debugC(9, kDebugLevelTimer, "TimerManager::setNextRun(%d, %u)", id, nextRun);
Iterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end()) {
timer->nextRun = nextRun;
return;
}
warning("TimerManager::setNextRun: No timer %d", id);
}
uint32 TimerManager::getNextRun(uint8 id) const {
debugC(9, kDebugLevelTimer, "TimerManager::getNextRun(%d)", id);
CIterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end())
return timer->nextRun;
warning("TimerManager::getNextRun: No timer %d", id);
return 0xFFFFFFFF;
}
bool TimerManager::isEnabled(uint8 id) const {
debugC(9, kDebugLevelTimer, "TimerManager::isEnabled(%d)", id);
CIterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end())
return (timer->enabled == 1);
warning("TimerManager::isEnabled: No timer %d", id);
return false;
}
void TimerManager::enable(uint8 id) {
debugC(9, kDebugLevelTimer, "TimerManager::enable(%d)", id);
Iterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end())
timer->enabled = 1;
else
warning("TimerManager::enable: No timer %d", id);
}
void TimerManager::disable(uint8 id) {
debugC(9, kDebugLevelTimer, "TimerManager::disable(%d)", id);
Iterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end())
timer->enabled = 0;
else
warning("TimerManager::disable: No timer %d", id);
}
void TimerManager::loadDataFromFile(Common::SeekableReadStream &file, int version) {
debugC(9, kDebugLevelTimer, "TimerManager::loadDataFromFile(%p, %d)", (const void*)&file, version);
if (version <= 7) {
_nextRun = 0;
for (int i = 0; i < 32; ++i) {
uint8 enabled = file.readByte();
int32 countdown = file.readSint32BE();
uint32 nextRun = file.readUint32BE();
Iterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(i));
if (timer != _timers.end()) {
timer->enabled = enabled;
timer->countdown = countdown;
if (nextRun) {
timer->nextRun = nextRun + _system->getMillis();
timer->lastUpdate = timer->nextRun - countdown * _vm->tickLength();
} else {
uint32 curTime = _system->getMillis();
timer->nextRun = curTime;
timer->lastUpdate = curTime - countdown * _vm->tickLength();
}
} else {
warning("Loading timer data for non existing timer %d", i);
}
}
} else {
int entries = file.readByte();
for (int i = 0; i < entries; ++i) {
uint8 id = file.readByte();
Iterator timer = Common::find_if(_timers.begin(), _timers.end(), TimerEqual(id));
if (timer != _timers.end()) {
timer->enabled = file.readByte();
timer->countdown = file.readSint32BE();
timer->lastUpdate = file.readSint32BE();
} else {
warning("Loading timer data for non existing timer %d", id);
file.seek(7, SEEK_CUR);
}
}
resync();
}
}
void TimerManager::saveDataToFile(Common::WriteStream &file) const {
debugC(9, kDebugLevelTimer, "TimerManager::saveDataToFile(%p)", (const void*)&file);
file.writeByte(count());
for (CIterator pos = _timers.begin(); pos != _timers.end(); ++pos) {
file.writeByte(pos->id);
file.writeByte(pos->enabled);
file.writeSint32BE(pos->countdown);
file.writeSint32BE(pos->lastUpdate - _system->getMillis());
}
}
} // end of namespace Kyra
|