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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
|
/* 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.
*
*/
#include "kyra/engine/eobcommon.h"
#include "kyra/engine/timer.h"
#include "common/system.h"
#ifdef ENABLE_EOB
namespace Kyra {
#define TimerV2(x) new Common::Functor1Mem<int, void, EoBCoreEngine>(this, &EoBCoreEngine::x)
void EoBCoreEngine::setupTimers() {
_timer->addTimer(0, TimerV2(timerProcessCharacterExchange), 9, false);
_timer->addTimer(1, TimerV2(timerProcessFlyingObjects), 3, true);
_timer->addTimer(0x20, TimerV2(timerProcessMonsters), 20, true);
_timer->addTimer(0x21, TimerV2(timerProcessMonsters), 20, true);
_timer->addTimer(0x22, TimerV2(timerProcessMonsters), 20, true);
_timer->addTimer(0x23, TimerV2(timerProcessMonsters), 20, true);
_timer->setNextRun(0x20, _system->getMillis());
_timer->setNextRun(0x21, _system->getMillis() + 7 * _tickLength);
_timer->setNextRun(0x22, _system->getMillis() + 14 * _tickLength);
_timer->setNextRun(0x23, _system->getMillis() + 14 * _tickLength);
_timer->addTimer(0x30, TimerV2(timerSpecialCharacterUpdate), 50, false);
_timer->addTimer(0x31, TimerV2(timerSpecialCharacterUpdate), 50, false);
_timer->addTimer(0x32, TimerV2(timerSpecialCharacterUpdate), 50, false);
_timer->addTimer(0x33, TimerV2(timerSpecialCharacterUpdate), 50, false);
_timer->addTimer(0x34, TimerV2(timerSpecialCharacterUpdate), 50, false);
_timer->addTimer(0x35, TimerV2(timerSpecialCharacterUpdate), 50, false);
_timer->addTimer(4, TimerV2(timerProcessDoors), 5, true);
_timer->addTimer(5, TimerV2(timerUpdateTeleporters), 10, true);
_timer->addTimer(6, TimerV2(timerUpdateFoodStatus), 1080, true);
_timer->addTimer(7, TimerV2(timerUpdateMonsterIdleAnim), 25, true);
_timer->resetNextRun();
}
void EoBCoreEngine::enableSysTimer(int sysTimer) {
if (sysTimer != 2)
return;
KyraRpgEngine::enableSysTimer(sysTimer);
if (!_disableElapsedTime)
return;
_disableElapsedTime = _system->getMillis() - _disableElapsedTime;
for (int i = 0; i < 6; i++) {
EoBCharacter *c = &_characters[i];
for (int ii = 0; ii < 10; ii++) {
if (c->timers[ii])
c->timers[ii] += _disableElapsedTime;
}
}
setupCharacterTimers();
if (_scriptTimersMode & 1) {
for (int i = 0; i < _scriptTimersCount; i++) {
if (_scriptTimers[i].next) {
_scriptTimers[i].next += _disableElapsedTime;
debugC(3, kDebugLevelTimer, "EoBCoreEngine::enableSysTimer() - CTIME: %08d SCRIPT TIMER[%02d].NEXT: %08d", _system->getMillis(), i, _scriptTimers[i].next);
}
}
}
for (int i = 0; i < 5; i++) {
if (_wallsOfForce[i].block)
_wallsOfForce[i].duration += _disableElapsedTime;
}
_disableElapsedTime = 0;
}
void EoBCoreEngine::disableSysTimer(int sysTimer) {
if (sysTimer != 2)
return;
KyraRpgEngine::disableSysTimer(sysTimer);
if (_disableElapsedTime)
return;
_disableElapsedTime = _system->getMillis();
}
void EoBCoreEngine::setCharEventTimer(int charIndex, uint32 countdown, int evnt, int updateExistingTimer) {
uint32 ntime = _system->getMillis() + countdown * _tickLength;
uint8 timerId = 0x30 | (charIndex & 0x0F);
EoBCharacter *c = &_characters[charIndex];
if (!_timer->isEnabled(timerId)) {
c->timers[0] = ntime;
c->events[0] = evnt;
_timer->setCountdown(timerId, countdown);
enableTimer(timerId);
return;
}
if (ntime < _timer->getNextRun(timerId))
_timer->setNextRun(timerId, ntime);
_timer->resetNextRun();
if (updateExistingTimer) {
bool updated = false;
int d = -1;
for (int i = 0; i < 10 && updated == false; i++) {
if (d == -1 && !c->timers[i])
d = i;
if (!updated && c->events[i] == evnt) {
d = i;
updated = true;
}
}
assert(d != -1);
c->timers[d] = ntime;
c->events[d] = evnt;
} else {
for (int i = 0; i < 10; i++) {
if (c->timers[i])
continue;
c->timers[i] = ntime;
c->events[i] = evnt;
return;
}
}
}
void EoBCoreEngine::deleteCharEventTimer(int charIndex, int evnt) {
EoBCharacter *c = &_characters[charIndex];
for (int i = 0; i < 10; i++) {
if (c->events[i] == evnt) {
c->events[i] = 0;
c->timers[i] = 0;
}
}
setupCharacterTimers();
}
void EoBCoreEngine::setupCharacterTimers() {
for (int i = 0; i < 6; i++) {
EoBCharacter *c = &_characters[i];
if (!testCharacter(i, 1))
continue;
uint32 nextTimer = 0xFFFFFFFF;
for (int ii = 0; ii < 10; ii++) {
if (c->timers[ii] && c->timers[ii] < nextTimer)
nextTimer = c->timers[ii];
}
uint32 ctime = _system->getMillis();
if (nextTimer == 0xFFFFFFFF)
_timer->disable(0x30 | i);
else {
enableTimer(0x30 | i);
_timer->setCountdown(0x30 | i, nextTimer >= ctime ? (nextTimer - ctime) / _tickLength : 1);
}
}
_timer->resetNextRun();
}
void EoBCoreEngine::advanceTimers(uint32 millis) {
uint32 ct = _system->getMillis();
for (int i = 0; i < 6; i++) {
EoBCharacter *c = &_characters[i];
for (int ii = 0; ii < 10; ii++) {
if (c->timers[ii] > ct) {
uint32 chrt = c->timers[ii] - ct;
c->timers[ii] = chrt > millis ? ct + chrt - millis : 1;
} else if (c->timers[ii]) {
c->timers[ii] = 1;
}
}
}
if (_disableElapsedTime)
_disableElapsedTime += (ct - _disableElapsedTime);
setupCharacterTimers();
if (_scriptTimersMode & 1) {
for (int i = 0; i < _scriptTimersCount; i++) {
if (_scriptTimers[i].next > ct) {
uint32 chrt = _scriptTimers[i].next - ct;
_scriptTimers[i].next = chrt > millis ? ct + chrt - millis : 1;
debugC(3, kDebugLevelTimer, "EoBCoreEngine::advanceTimers() - CTIME: %08d SCRIPT TIMER[%02d].NEXT: %08d", ct, i, _scriptTimers[i].next);
} else if (_scriptTimers[i].next) {
_scriptTimers[i].next = 1;
debugC(3, kDebugLevelTimer, "EoBCoreEngine::advanceTimers() - CTIME: %08d SCRIPT TIMER[%02d].NEXT: %08d", ct, i, _scriptTimers[i].next);
}
}
}
for (int i = 0; i < 5; i++) {
if (!_wallsOfForce[i].block)
continue;
if (_wallsOfForce[i].duration > ct) {
uint32 chrt = _wallsOfForce[i].duration - ct;
_wallsOfForce[i].duration = chrt > millis ? ct + chrt - millis : 1;
} else {
_wallsOfForce[i].duration = 1;
}
}
}
void EoBCoreEngine::timerProcessCharacterExchange(int timerNum) {
_charExchangeSwap ^= 1;
if (_charExchangeSwap) {
int index = _exchangeCharacterId;
_exchangeCharacterId = -1;
gui_drawCharPortraitWithStats(index);
_exchangeCharacterId = index;
} else {
gui_drawCharPortraitWithStats(_exchangeCharacterId);
}
}
void EoBCoreEngine::timerProcessFlyingObjects(int timerNum) {
static const uint8 dirPosIndex[] = { 0x82, 0x83, 0x00, 0x01, 0x01, 0x80, 0x03, 0x82, 0x02, 0x03, 0x80, 0x81, 0x81, 0x00, 0x83, 0x02 };
for (int i = 0; i < 10; i++) {
EoBFlyingObject *fo = &_flyingObjects[i];
if (!fo->enable)
continue;
bool endFlight = fo->distance == 0;
uint8 pos = dirPosIndex[(fo->direction << 2) + (fo->curPos & 3)];
uint16 bl = fo->curBlock;
bool newBl = (pos & 0x80) ? true : false;
if (newBl) {
bl = calcNewBlockPosition(fo->curBlock, fo->direction);
pos &= 3;
fo->starting = 0;
}
if (updateObjectFlight(fo, bl, pos)) {
if (newBl)
runLevelScript(bl, 0x10);
if (updateFlyingObjectHitTest(fo, bl, pos))
endFlight = true;
} else {
if (fo->flags & 0x20) {
if (!updateFlyingObjectHitTest(fo, fo->curBlock, fo->curPos))
explodeObject(fo, fo->curBlock, fo->item);
}
endFlight = true;
}
if (endFlight)
endObjectFlight(fo);
_sceneUpdateRequired = true;
}
}
void EoBCoreEngine::timerProcessMonsters(int timerNum) {
updateMonsters(timerNum & 0x0F);
}
void EoBCoreEngine::timerSpecialCharacterUpdate(int timerNum) {
int charIndex = timerNum & 0x0F;
EoBCharacter *c = &_characters[charIndex];
uint32 ctime = _system->getMillis();
for (int i = 0; i < 10; i++) {
if (!c->timers[i])
continue;
if (c->timers[i] > ctime)
continue;
c->timers[i] = 0;
int evt = c->events[i];
if (evt < 0) {
removeCharacterEffect(-evt, charIndex, 1);
continue;
}
int od = _screen->curDimIndex();
Screen::FontId of = _screen->setFont(Screen::FID_6_FNT);
_screen->setScreenDim(7);
switch (evt) {
case 2:
case 3:
setCharEventTimer(charIndex, (c->effectFlags & 0x10000) ? 9 : 36, evt + 2, 1);
// fall through
case 0:
case 1:
case 4:
case 5:
setWeaponSlotStatus(charIndex, evt / 2, evt & 1);
break;
case 6:
c->damageTaken = 0;
gui_drawCharPortraitWithStats(charIndex);
break;
case 7:
_txt->printMessage(_characterStatusStrings7[0], -1, c->name);
c->strengthCur = c->strengthMax;
c->strengthExtCur = c->strengthExtMax;
if (_currentControlMode == 2)
gui_drawCharPortraitWithStats(charIndex);
break;
case 8:
if (c->flags & 2) {
calcAndInflictCharacterDamage(charIndex, 0, 0, 5, 0x400, 5, 3);
setCharEventTimer(charIndex, 546, 8, 1);
} else {
c->flags &= ~2;
gui_drawCharPortraitWithStats(charIndex);
}
break;
case 9:
if (c->flags & 4) {
_txt->printMessage(_characterStatusStrings9[0], -1, c->name);
c->flags &= ~4;
gui_drawCharPortraitWithStats(charIndex);
}
break;
case 11:
if (c->disabledSlots & 4) {
c->disabledSlots &= ~4;
if (_openBookChar == charIndex && _updateFlags)
gui_drawSpellbook();
}
break;
case 12:
c->effectFlags &= ~0x1000;
if (_characterStatusStrings12)
_txt->printMessage(_characterStatusStrings12[0], -1, c->name);
break;
default:
break;
}
_screen->setScreenDim(od);
_screen->setFont(of);
}
uint32 nextTimer = 0xFFFFFFFF;
for (int i = 0; i < 10; i++) {
if (c->timers[i] && c->timers[i] < nextTimer)
nextTimer = c->timers[i];
}
if (nextTimer == 0xFFFFFFFF)
_timer->disable(timerNum);
else
_timer->setCountdown(timerNum, (nextTimer - ctime) / _tickLength);
}
void EoBCoreEngine::timerUpdateTeleporters(int timerNum) {
_teleporterPulse ^= 1;
for (int i = 0; i < 18; i++) {
uint8 w = _visibleBlocks[i]->walls[_sceneDrawVarDown];
if ((w == _teleporterWallId) || (_flags.gameID == GI_EOB2 && w == 74)) {
_sceneUpdateRequired = true;
return;
}
}
}
void EoBCoreEngine::timerUpdateFoodStatus(int timerNum) {
for (int i = 0; i < 6; i++) {
// Ring of Sustenance check
if (checkInventoryForRings(i, 2))
continue;
EoBCharacter *c = &_characters[i];
if (c->food != 0 && c->flags & 1 && c->hitPointsCur > -10) {
c->food--;
gui_drawFoodStatusGraph(i);
}
}
}
void EoBCoreEngine::timerUpdateMonsterIdleAnim(int timerNum) {
for (int i = 0; i < 30; i++) {
EoBMonsterInPlay *m = &_monsters[i];
if (m->mode == 7 || m->mode == 10 || (m->flags & 0x20) || (rollDice(1, 2, 0) != 1))
continue;
m->idleAnimState = (rollDice(1, 2, 0) << 4) | rollDice(1, 2, 0);
checkSceneUpdateNeed(m->block);
}
}
} // End of namespace Kyra
#endif // ENABLE_EOB
|