-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathTimer.cpp
More file actions
2894 lines (2744 loc) · 80.8 KB
/
Timer.cpp
File metadata and controls
2894 lines (2744 loc) · 80.8 KB
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "UI.h"
#include <stdio.h>
//#include "Objects.h"
#include "Dispatch.h"
#include "CSB.h"
#include "Data.h"
void ProcessPortraitTimer7(RN targetObj, const TIMER *pTimer);
void info(char *, unsigned int);
int MissileEncounterFilter(int type, LOCATIONREL locr, RN missile, RN projectile, i32 index);
extern i32 deleteDuplicateTimers;
extern i32 timerTypeModifier[3];
#ifdef _MSVC_INTEL
#pragma warning(disable:4710)
#endif
void TraceCellflags(i32 x, i32 y, const char *msg)
{
if (TimerTraceActive)
fprintf(GETFILE(TraceFile),"%s in %02x,%02x - new value = %02x\n",
msg, x, y, d.LevelCellFlags[x][y]);
}
//#ifdef _DEBUG
void printHexLocation(FILE *f, i32 lev, i32 x, i32 y)
{
fprintf(f,"%02x(%02x,%02x)",lev,x,y);
}
const char *action(i32 act)
{
if (act == 0)return "Set ";
if (act == 1)return "Clear ";
if (act == 2)return "Toggle";
return "??????";
}
void TraceTimer(TIMER *pTimer, i32 index, const char *msg)
{
char Msg[15];
i32 len;
if (!TimerTraceActive) return;
memset(Msg,' ',14);
Msg[14] = 0;
len=strlen(msg);
if (len>14) len=14;
memcpy(Msg,msg,len);
fprintf(GETFILE(TraceFile),"%08x timer %s %03x %02x "
"DLY=%04x "
"%02x %02x %02x %02x %02x %02x %04x %08x",
d.Time,
//Msg, index, pTimer->timerTime>>24,
Msg, index, pTimer->Level(),
//(pTimer->timerTime & 0xffffff) - d.Time,
pTimer->Time() - d.Time,
pTimer->Function(),pTimer->timerUByte5(),pTimer->timerUByte6(),
pTimer->timerUByte7(),pTimer->timerUByte8(),pTimer->timerUByte9(),
pTimer->TimerSequence(),
d.RandomNumber);
switch (pTimer->Function())
{
case 6:
fprintf(GETFILE(TraceFile)," %s 0x%02x in DB at ",
action(timerTypeModifier[pTimer->timerUByte9()]),1<<pTimer->timerUByte8());
printHexLocation(GETFILE(TraceFile),
//pTimer->timerTime>>24,
pTimer->Level(),
pTimer->timerUByte6(),
pTimer->timerUByte7());
fprintf(GETFILE(TraceFile),"%02x",pTimer->timerUByte8());
break;
case TT_FALSEWALL:
fprintf(GETFILE(TraceFile)," %s CELLFLAG 0x04 at %02x,%02x",
action(timerTypeModifier[pTimer->timerUByte9()]),pTimer->timerUByte6(),pTimer->timerUByte7());
break;
case 8:
fprintf(GETFILE(TraceFile)," %s CELLFLAG 0x08 at %02x,%02x",
action(timerTypeModifier[pTimer->timerUByte9()]),pTimer->timerUByte6(),pTimer->timerUByte7());
break;
};
fprintf(GETFILE(TraceFile),"\n");
}
//#else //_DEBUG
//void TraceTimer(TIMER *, i32 , char *)
//{
//}
//#endif //_DEBUG
void TIMER::swapTimerWord6()
{
*(i16 *)(&m_timerUByte6) = LE16(*(i16 *)(&m_timerUByte6));
}
void TIMER::swapTimerObj8()
{
*(i16 *)(&m_timerUByte8) = LE16(*(i16 *)(&m_timerUByte8));
}
void TIMER::swapTimerObj6()
{
*(i16 *)(&m_timerUByte6) = LE16(*(i16 *)(&m_timerUByte6));
}
void TIMER::swapTimerWord8()
{
*(i16 *)(&m_timerUByte8) = LE16(*(i16 *)(&m_timerUByte8));
}
void TIMER::swapTimerSequence()
{
m_timerSequence = LE16(m_timerSequence);
}
//*********************************************************
//
//*********************************************************
// TAG00e17a
void ProcessObjectFromMissile(RN missile, RN *pDest, i32 mapX, i32 mapY)
{ //()
RN objD6;
RN objD7;
DB14 *DB14A3;
DBCOMMON *pdb_4;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
DB14A3 = GetRecordAddressDB14(missile);
objD6 = DB14A3->flyingObject();
if (!objD6.IsMagicSpell()&&(objD6.dbType() != dbCLOUD))
{
if (pDest != NULL)
{
objD7 = *pDest;
if (objD7 == RNeof)
{
pdb_4 = GetCommonAddress(objD6);
pdb_4->link(RNeof);
*pDest = objD6;
}
else
{
AddObjectToRoom(objD6, objD7, -1, 0, NULL);
};
}
else
{
MoveObject(
RN(objD6).pos(missile.pos()),
-2,
0,
mapX,
mapY,
NULL,
NULL);
};
};
//DB14A3->link(RNnul);
DeleteDBEntry(DB14A3);
}
// While we are processing a Missile Timer, the timer
// entry no longer exists in the time queue. We have
// a copy of it right here. If the MissileFilter want
// manipulate the timer, it must manipulate this copy
// of it.
RN missileFilterObject = RNnul;
TIMER missileFilterTimer;
//*********************************************************
//
//*********************************************************
// TAG00e962
void MissileTimer(TIMER *pTimer)
{//()
// A thrown Screamer Slice passes this way, too.
// A fireball is represented by a timer entry with:
// function = 48 then 49.
// obj6 = Record Name (and position in cell)
// word8 = mapX (bits 0-4)
// mapY (bits 5-9)
// direction of travel (bits 10-11)
// delta energy (bits 12-15)
// The object is in database 0x0e (8 bytes)
//
dReg D0;
RN objD0;
DIRECTION missileDirection;
DIRECTION newMissileDirection;
//TIMER timer;
DB14 *pDBMissile;
i32 origMapY=-999;
i32 origMapX=-999;
i32 curMapY; // Set to current position then moved.
i32 curMapX; // Set to current position then moved.
i32 curPos;
bool leavingThisCell;
//RN objD7;
//RN objMissile;
//i32 nextCellFlags;
i32 deltaEnergy; // From timer entry
i32 newX, newY, newDir;
i32 saveTimerIndex;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
if (missileFilterObject != RNnul)
{
die(0x567c, "MissileFilter");
};
memmove((pnt)&missileFilterTimer, (pnt)pTimer, 16); // 5 words = 10 bytes
missileFilterObject = missileFilterTimer.timerObj6();
//objD7 = objMissile;
ASSERT(missileFilterObject.dbType() == dbMISSILE,"missile");
pDBMissile = GetRecordAddressDB14(missileFilterObject);
saveTimerIndex = pDBMissile->timerIndex();
pDBMissile->timerIndex(0xffff);
origMapX = BITS0_4(missileFilterTimer.timerWord8()); //mapX
origMapY = BITS5_9(missileFilterTimer.timerWord8()); //mapY
curMapX = origMapX;
curMapY = origMapY;
if (missileFilterTimer.Function() == TT_Missile0)
{
missileFilterTimer.Function(TT_Missile);
}
else
{
curPos = missileFilterObject.pos();
if ( (d.LoadedLevel == d.partyLevel)
&& (curMapX == d.partyX)
&& (curMapY == d.partyY) )
{
// Did missile strike someone?
D0W = ProcessMissileEncounter(
-2, // Check party members
curMapX, // party location
curMapY,
curMapX, // missile location
curMapY,
curPos,
missileFilterObject,
NULL);
if (D0W != 0)
{
missileFilterObject = RNnul;
return; // Missile is discarded. Gone!
};
};
objD0 = FindFirstMonster(curMapX, curMapY);
if (objD0 != RNeof)
{
D0W = ProcessMissileEncounter(
-1, // check monsters???
curMapX, // Monster location
curMapY,
curMapX, // Missile location
curMapY,
curPos,
missileFilterObject,
NULL);
if (D0W != 0)
{
missileFilterObject = RNnul;
return;
};
};
deltaEnergy = BITS12_15(missileFilterTimer.timerWord8());
if (pDBMissile->rangeRemaining() <= deltaEnergy)
{
LOCATIONREL missileLocr;
missileLocr.l = d.LoadedLevel;
missileLocr.x = curMapX;
missileLocr.y = curMapY;
missileLocr.p = curPos;
MissileEncounterFilter(5, missileLocr, missileFilterObject, pDBMissile->flyingObject(), -1);
RemoveObjectFromRoom(missileFilterObject, curMapX, curMapY, NULL);
ProcessObjectFromMissile(missileFilterObject, NULL, curMapX, curMapY);
missileFilterObject = RNnul;
return;
};
pDBMissile->rangeRemaining(
pDBMissile->rangeRemaining() - deltaEnergy);
if (pDBMissile->damageRemaining() < deltaEnergy)
{
pDBMissile->damageRemaining(0);
}
else
{
pDBMissile->damageRemaining(
pDBMissile->damageRemaining() - deltaEnergy);
};
};
do
{
missileDirection = BITS10_11(missileFilterTimer.timerWord8());
//objD7 = timer.timerObj6();
curPos = missileFilterObject.pos();
leavingThisCell = (missileDirection==curPos)
|| (((missileDirection+1)&3) == curPos);
curMapX = origMapX;
curMapY = origMapY;
if (leavingThisCell)
{
bool nextIsStairs, origIsStairs;
bool nextIsStoneWall;
bool nextIsFalseWall;
bool falseWallIsSolid;
i32 origCellFlags, nextCellFlags;
ROOMTYPE nextRoomType;
curMapX += d.DeltaX[missileDirection];
curMapY += d.DeltaY[missileDirection];
nextCellFlags = GetCellFlags(curMapX, curMapY);
nextRoomType = (ROOMTYPE)(nextCellFlags >> 5);
if ( (curMapX < 0)
|| (curMapX >= d.width)
|| (curMapY < 0)
|| (curMapY >= d.height)
)
{
nextRoomType = roomEXTERIOR;
};
//Cell directly ahead.
// if ( (nextCellFlags>>5)
// && (((nextCellFlags>>5)!=roomFALSEWALL) || (nextCellFlags&5)) )
// {
// if ((nextCellFlags>>5) != roomSTAIRS) goto tag00eb90;
// if ((d.LevelCellFlags[origMapX][origMapY]>>5) != roomSTAIRS )
// goto tag00eb90;
// };
origCellFlags = d.LevelCellFlags[origMapX][origMapY];
nextIsStoneWall = (nextRoomType == roomSTONE) || (nextRoomType == roomEXTERIOR);
nextIsStairs = nextRoomType == roomSTAIRS;
origIsStairs = (origCellFlags>>5) == roomSTAIRS;
nextIsFalseWall = nextRoomType == roomFALSEWALL;
falseWallIsSolid = (nextCellFlags&5) == 0;
if ( nextIsStoneWall
|| (nextIsFalseWall && falseWallIsSolid)
|| (nextIsStairs && origIsStairs)
)
{
// Will missile strike wall?
D0W = ProcessMissileEncounter(
nextRoomType, //Cell type
origMapX,
origMapY,
origMapX,
origMapY, // missile location
curPos,
missileFilterObject,
NULL);
if (D0W != 0)
{
missileFilterObject = RNnul;
return; // Missile is gone.
};
};
};
newMissileDirection = BITS10_11(missileFilterTimer.timerWord8());
} while (newMissileDirection != missileDirection);
//tag00eb90:
if ((missileDirection&1) == (curPos&1))
{
curPos -= 1;
}
else
{
curPos += 1;
};
//D4W &= 3;
// (Later PRS 12/02) objD7.pos(curPos);
if(leavingThisCell)
{
missileFilterObject.pos(curPos);
MoveObject(missileFilterObject, origMapX, origMapY, curMapX, curMapY, NULL, NULL);
newX = d.NewX & 31; //Set to newX by MoveObject
missileFilterTimer.timerWord8() &= 0xffe0;
missileFilterTimer.timerWord8() |= newX; // New mapX to timer entry
newY = d.NewY & 31; //Set to newY by MoveObject
missileFilterTimer.timerWord8() &= 0xfc1f;
missileFilterTimer.timerWord8() |= newY << 5; // New mapY to timer entry
newDir = d.NewDir & 3; //Set to new Direction by MoveObject
missileFilterTimer.timerWord8() &= 0xf3ff;
missileFilterTimer.timerWord8() |= newDir<<10; //Set new direction in timer entry
missileFilterObject.pos(d.NewPos); //Set new position in object.
//missileFilterTimer.timerTime &= 0xffffff;
//D0L |= uw(d.NewLevel) << 24;
//missileFilterTimer.timerTime |= (d.NewLevel<<24);
missileFilterTimer.Level((ui8)d.NewLevel);
}
else
{
D0W = GetCellFlags(curMapX, curMapY);
if ((D0W >> 5) == roomDOOR) // cell type 4
{
// Did missile strike door
D0W = ProcessMissileEncounter(
4,
curMapX,
curMapY,
curMapX,
curMapY,
curPos,
missileFilterObject,
NULL);
if (D0W != 0)
{
missileFilterObject = RNnul;
return;//Missile is gone.
};
};
missileFilterObject.pos(curPos);
RemoveObjectFromRoom(missileFilterObject, curMapX, curMapY, NULL);
AddObjectToRoom(missileFilterObject, RN(RNempty), curMapX , curMapY, NULL);
};
//missileFilterTimer.timerTime++;
missileFilterTimer.Time(missileFilterTimer.Time() + 1);
missileFilterTimer.timerObj6() = missileFilterObject;
missileFilterObject = RNnul;
pDBMissile->timerIndex(gameTimers.SetTimer(&missileFilterTimer));
}
//*********************************************************
// Timer function 25 (0x19)
//*********************************************************
// TAG00ecca
void ProcessTT_25(TIMER *pTimer)
{ //()
//An open door spell??? Other missiles??
dReg D0, D1, D4, D5, D6, D7;
aReg A0, A1;
DB15 *DB15A2;
TIMER timer; //26
i16 w_16;
MONSTERTYPE mt_14=mon_undefined;
i16 w_14;
ui16 uw_12;
RN obj_10;
MONSTERDESC *pmtDesc=NULL;
ITEM16 *pi16A0;
DB4 *pDB4_4=NULL;
i32 damage;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
D7W = pTimer->timerUByte6(); //x
D6W = pTimer->timerUByte7(); //y
DB15A2 = GetRecordAddressDB15(pTimer->timerObj8());
D4W = D6W;
D4W = (UI8)(d.LevelCellFlags[D7W][D6W]); // Cell flag
D4W = sw(BITS5_15(D4W));
//SET(D0W, d.LoadedLevel==d.partyLevel);
//if (D0B != 0)
//{
//SET(D0W, D7W == d.partyX);
//};
//if (D0B != 0)
//{
//SET(D0W, D6W == d.partyY);
//};
if ( (d.LoadedLevel==d.partyLevel)
&& (D7W==d.partyX)
&& (D6W==d.partyY) ) w_16=1; else w_16=0;
//w_16 = D0W & 1;
obj_10 = FindFirstMonster(D7W, D6W);
if (obj_10 != RNeof)
{
pDB4_4 = GetRecordAddressDB4(obj_10);
mt_14 = pDB4_4->monsterType();
pmtDesc = &d.MonsterDescriptor[mt_14];
};
uw_12 = uw(0xff80 + (DB15A2->cloudType()));
if (uw_12 == 0xff87) //Poison cloud
{
D0L = Smaller(DB15A2->value()/32, 4);
D1L = STRandomBool();
D5W = sw(Larger(1, D0W + D1W));
}
else
{
D5W = sw(DB15A2->value()/2 + 1);
//D0L = STRandom() & 0xffff;
D5W = sw(D5W + STRandom(D5W) + 1);
};
switch (uw_12)
{
case 0xff82:
D5W = sw(D5W / 2);
if (D5W == 0) break;
// Note fall-through.
case 0xff80: //Fireball
if (D4W == 4)
{
HitDoor(D7W, D6W, D5W, 1, 0);
};
break;
case RNDispellMissile:
if (obj_10 != RNeof)
{
if (pmtDesc->nonMaterial())
{
if ( (mt_14==mon_Zytaz) && (d.LoadedLevel==d.partyLevel) )
{
w_14 = sw(D5W/8);
D5W = sw(D5W - w_14);
w_14 = sw(2*w_14 + 1);
D4W = pDB4_4->numMonM1(); //#monsters-1
do
{
D0W = sw(pDB4_4->groupIndex());
ASSERT(D0W < d.MaxITEM16,"maxitem16");
pi16A0 = &d.Item16[D0W];
if (pi16A0->singleMonsterStatus[D4W].TestAttacking())
{
damage = STRandom(w_14);
DamageMonster(
pDB4_4,
D4W,
D7W,
D6W,
sw(damage + STRandom0_3() + D5W),
1,
false,
NULL);
// The following did not work because the debug and release
// versions called the two Random functions in a different order!
//DamageMonster(
// pDB4_4,
// D4W,
// D7W,
// D6W,
// sw(STRandom0_3() + STRandom(w_14) + D5W),
// 1);
};
D4W--;
} while (D4W >= 0);
}
else
{
DamageMonsterGroup(pDB4_4, D7W, D6W, D5W, 1, false, NULL);
};
};
};
break;
case 0xffe4:
//D3W = (DB15A2->type() + 1) & 0x7f;
DB15A2->cloudType((CLOUDTYPE)(DB15A2->cloudType()+1));
QueueSound(5, D7W, D6W,1);
goto tag00f020;
case 0xffa8:
if (DB15A2->value() > 55)
{
//.W = DB15A2->value();
//.W = D0W - 40;
//D3W <<= 8;
DB15A2->value(DB15A2->value()-40);
goto tag00f020;
};
break;
case 0xff87: //Poison Cloud
if (w_16 != 0)
{
DamageAllCharacters(D5W, 0, 0);
}
else
{
if (obj_10 != RNeof)
{
D5W = DeterminePoisonDamage(mt_14, D5W);
if (D5W != 0)
{
MONSTER_DAMAGE_RESULT monsterDamageResult;
monsterDamageResult = DamageMonsterGroup(
pDB4_4,
D7W,
D6W,
D5W,
1,
false,
NULL);
if (monsterDamageResult != ALL_MONSTERS_DIED)
{
if (D5W > 2)
{
ProcessTimers29to41(D7W, D6W, TT_M3, 0);
};
};
};
};
};
if (DB15A2->value() < 6) break;
//D3W = (BITS8_15(dbA2->word(2)) - 3) & 0xff;
//D3W <<= 8;
DB15A2->value(DB15A2->value()-3);
tag00f020:
A1 = (pnt)pTimer;
A0 = (pnt)&timer;
//MoveWords(A0, A1, 4); // 10 bytes
memcpy(A0, A1, sizeof(TIMER));
//timer.timerTime++;
timer.Time(timer.Time() + 1);
gameTimers.SetTimer(&timer);
return;
}; // end of switch statement
RemoveObjectFromRoom(pTimer->timerObj8(),
D7W,
D6W,
NULL);
//DB15A2->link(RNnul);
DeleteDBEntry(DB15A2);
}
//*********************************************************
//
//*********************************************************
// TAG00f132
bool OpenTeleporterPitOrDoor(i32 mapX, i32 mapY)
{
i32 cf;
cf = (GetCellFlags(mapX, mapY) & 255) >> 5;
return (cf == roomOPEN)
||(cf == roomTELEPORTER)
||(cf == roomPIT)
||(cf == roomDOOR);
}
GameTimers::GameTimers()
{
m_maxTimers = 0;
m_searchActive = false;
m_timerSequence = 0;
}
GameTimers::~GameTimers()
{
m_maxTimers = 0;
}
void GameTimers::Allocate(i32 numEnt)
{
i32 i;
if (m_maxTimers >= numEnt) return;
m_timerQueue.resize(numEnt, 0);
m_searchList.resize(numEnt);
m_timers.resize(numEnt, TIMER());
// d.timerQue = (i16 *)m_index;
//d.Timers = (TIMER *)m_timers;
for (i=m_maxTimers; i<numEnt; i++)
m_timers[i].Function(TT_EMPTY);
m_maxTimers = (ui16)numEnt;
//d.MaxTimers = (i16)m_size;
}
void GameTimers::Cleanup()
{
m_timerQueue.clear();
m_maxTimers = 0;
m_timerSequence = 0;
}
TIMER *GameTimers::pTimer(HTIMER hTimer)
{
if (hTimer == 0xffff)
{
if (missileFilterObject == RNnul)
{
die(0x5c5c, "Timer");
}
return &missileFilterTimer;
}
else
{
return &m_timers[hTimer];
};
}
GameTimers gameTimers;
//*********************************************************
//
//*********************************************************
// TAG00fd1c
void GameTimers::InitializeTimers()
{
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Allocate(m_maxTimers);
if (d.gameState != GAMESTATE_ResumeSavedGame)
{
for(unsigned i=0;i<m_maxTimers;i++)
m_timers[i].Function(TT_EMPTY);
//d.numTimer = 0;
//d.FirstAvailTimer = 0;
Clear();
SetWatchdogTimer();
};
}
//void GameTimers::TimerQueue(i32 /*v*/, ui16 /*index*/)
//{
// NotImplemented();
//}
//void GameTimers::DecrementNumTimer()
//{
// NotImplemented();
//}
void EnlargeTimerQueue(i32 numEnt)
{
gameTimers.Allocate(numEnt);
}
//*********************************************************
//
//*********************************************************
bool TIMER::operator < (const TIMER& timer2) const // TAG00fd9e
{
// return 1 if *p1 < *p2
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dReg D1, D2;
//A0 = (pnt)p1;
//A1 = (pnt)p2;
//D0L = 1;
//D2L = timerTime & 0xffffff; //D2=*p1
D2L = m_time; //D2=*p1
D1L = timer2.m_time; //D1=*p2
if (D2L < D1L) return true; // *p1 < *p2
if (D2L > D1L) return false; // *p1 > *p2
// Times are the same. Parameter messages come before everything else.
if (timerFunction == TT_ParameterMessage)
{
if (timer2.timerFunction != TT_ParameterMessage) return true;
// Both are Parameter messages; Compare sequence numbers.
if (m_timerUByte5 < timer2.m_timerUByte5) return true;
if (m_timerUByte5 > timer2.m_timerUByte5) return false;
// Identical entries. The smaller pointer comes first
#ifdef SEQUENCED_TIMERS
if (m_timerSequence < timer2.m_timerSequence) return true;
if (m_timerSequence > timer2.m_timerSequence) return false;
#endif
if (&timer2 >= this) return true;
return false;
}
else
{
if (timer2.timerFunction == TT_ParameterMessage) return false;
};
// Compare the first word.
if (timerFunction > timer2.timerFunction) return true;
if (timerFunction < timer2.timerFunction) return false;
if (m_timerUByte5 > timer2.m_timerUByte5) return true;
if (m_timerUByte5 < timer2.m_timerUByte5) return false;
// Identical entries. The smaller pointer comes first
#ifdef SEQUENCED_TIMERS
if (m_timerSequence < timer2.m_timerSequence) return true;
if (m_timerSequence > timer2.m_timerSequence) return false;
#endif
if (&timer2 >= this) return true;
return 0;
}
// TAG00fdce
i16 GameTimers::FindTimerPosition(HTIMER P1)
{ // Find position of timer entry in timer queue.
for(unsigned i=0;i<m_timerQueue.size();i++)
if(m_timerQueue[i]==P1)
return i;
return 0;
}
// TAG00fdf0
void GameTimers::AdjustTimerQueue(i32 timerQueueIndex)
{ // Current position of timer in queue.
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
dReg D0, D4, D5, D6, D7;
// pnt A3;
TIMER *ptA3, *ptA0;
i16 LOCAL_2;
D7W = sw(timerQueueIndex);
D4L = m_numTimer;
//D4W = D0W;
if (D4W == 1) return;
//D5W = gameTimers.TimerQueue()[D7W];
D5W = m_timerQueue[D7W];
ptA3 = &m_timers[D5W]; // New entry to insert
LOCAL_2 = 0;
while (D7W != 0) //Attempt to find entry
{ // Starting at middle word keep dividing the
// que in half and moving the new entry down
// as long as it is smaller than the one already
// there. Set local_2 if any such swap occurs.
D6W = sw((D7W-1)>>1); // Locate middle item???
//D0W = gameTimers.TimerQueue()[D6W];
D0W = m_timerQueue[D6W];
ptA0 = &m_timers[D0W];
D0W = *ptA3 < *ptA0; // Is new smaller
if (D0W == 0) break;
//D0W = d.timerQue[D6W];
//TimerQueue(D7W, gameTimers.TimerQueue()[D6W]);
m_timerQueue[D7W] = m_timerQueue[D6W];
D7W = D6W; //The new empty spot
LOCAL_2 = 1; //we made a swap
};
if (LOCAL_2 == 0)
{
D4W = sw((D4W-2)/2);
while (D7W <= D4W)
{
D6W = sw(2*D7W + 1);
D0W = sw(D6W+1);
if (D0W < m_numTimer)
{
//D0W = d.Pointer12998[D6W];
//parm2 = &d.Pointer12994[D0W];
//D0W = d.Pointer12998[D6W+1];
//parm1 = &d.Pointer12994[D0W];
//D0W = TestTimerLess(gameTimers.pTimer(gameTimers.TimerQueue()[D6W+1]),
// gameTimers.pTimer(gameTimers.TimerQueue()[D6W]));
D0W = m_timers[m_timerQueue[D6W+1]] < m_timers[m_timerQueue[D6W]];
if (D0W != 0) D6W++;
};
D0W = m_timers[m_timerQueue[D6W]] < *ptA3;
if (D0W == 0) break;
//D0W = gameTimers.TimerQueue()[D6W];
D0W = m_timerQueue[D6W];
//gameTimers.TimerQueue(D7W, D0W);
m_timerQueue[D7W] = D0W;
D7W = D6W;
}; //while
};
//gameTimers.TimerQueue(D7W, D5W);
m_timerQueue[D7W] = D5W;
}
i16 TIMER_SEARCH::FindQPos()
{
if (gameTimers.m_searchList[m_index] == gameTimers.m_timerQueue[m_index])
{
return (ui16)m_index;
}
else
{
return gameTimers.FindTimerPosition(TimerHandle());
};
}
void TIMER_SEARCH::AdjustTimerPriority()
{
gameTimers.AdjustTimerQueue(FindQPos());
}
void GameTimers::AdjustTimerPriority(HTIMER hTimer)
{
AdjustTimerQueue(FindTimerPosition(hTimer));
}
//*********************************************************
//
//*********************************************************
#ifdef _DEBUG
#define ASSERTTimer(x) (ASSERT(x,"TimerCheck"))
#else
#define ASSERTTimer(x)
#endif
bool GameTimers::CheckTimers()
{ // Return true if all is well.
// We check that the queue is properly ordered.
i32 n, N;
N = m_numTimer;
for (i32 i=0; i<N; i++)
{
if (m_timers[m_timerQueue[i]].Function() == 0)
{
return false;
};
for (n=2*i+1; n<=2*i+2; n++)
{
if (n >= N) break;
if (m_timers[m_timerQueue[n]] < m_timers[m_timerQueue[i]])
{
return false;
};
};
};
return true;
}
//*********************************************************
//
//*********************************************************
// TAG00ff4c
void GameTimers::DeleteTimer(HTIMER hTimer, const char *tag)
{
i32 QPos;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ASSERTTimer(CheckTimers());
TraceTimer(&m_timers[hTimer], hTimer, tag==NULL?"Delete":tag);
m_timers[hTimer].Function(TT_EMPTY);// .WB8.bytes[0] = 0;
if (hTimer < m_firstAvailTimer)
{
m_firstAvailTimer=hTimer;
};
m_numTimer--;
//D5W = d.numTimer;
if (m_numTimer != 0)
{
QPos = FindTimerPosition(hTimer);
if (QPos != m_numTimer) // If it was not the last entry
{
//TimerQueue((ui16)QPos, m_timerQueue[m_numTimer]);
m_timerQueue[QPos] = m_timerQueue[m_numTimer];
// Move last entry into vacated position.
AdjustTimerQueue(QPos);
};
};
ASSERTTimer(CheckTimers());
}
//*********************************************************
//
//*********************************************************
// TAG00ffbe
HTIMER GameTimers::SetTimer(TIMER *pNewTimer) //
{
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//i32 D5,D7;
dReg D0, D1, D5, D7;
aReg A0, A1;
//i16 *pwA0;
TIMER *ptA0, *ptA2;
ASSERTTimer(CheckTimers());
//ptA3 = P1;
if (m_numTimer >= m_maxTimers - 5)
{
if (m_maxTimers >= 5000)
{
die(45, "No empty Timer entries");
};
//gameTimers.FirstAvailTimer(gameTimers.MaxTimer());
m_firstAvailTimer = m_maxTimers;
EnlargeTimerQueue((m_maxTimers+10)*5/4);
};
if ( (pNewTimer->Function() >= TT_MAP_FIRST)
&& (pNewTimer->Function() <= TT_MAP_LAST))
{
if (deleteDuplicateTimers != 0)
{
int D7W;
TIMER_SEARCH timerSearch;
//for (D7W=0, ptA2=gameTimers.Timers(); D7W<d.MaxTimer(); D7W++, ptA2++) xxTIMERxx Change to FindNextTimer
while (timerSearch.FindNextTimer())
{
ptA2 = timerSearch.TimerAddr();
if ( (ptA2->Function() >= TT_MAP_FIRST)
&& (ptA2->Function() <= TT_MAP_LAST) )
{
//if (pNewTimer->timerTime != ptA2->timerTime) continue;
if (pNewTimer->Time() != ptA2->Time()) continue;
if (pNewTimer->Level() != ptA2->Level()) continue;
if (pNewTimer->timerWord6() != ptA2->timerWord6()) continue;
if (ptA2->Function() == TT_STONEROOM)
{
if (ptA2->timerUByte8() != pNewTimer->timerUByte8()) continue;
};
ptA2->timerUByte9(pNewTimer->timerUByte9());
TraceTimer(ptA2, timerSearch.TimerHandle(), "Byte9");
ASSERTTimer(CheckTimers());
return (i16)timerSearch.TimerHandle();//goto tag010224;
};
if (ptA2->Function() != TT_1) continue;
//if (pNewTimer->timerTime != ptA2->timerTime) continue;
if (pNewTimer->Time() != ptA2->Time()) continue;
if (pNewTimer->Level() != ptA2->Level()) continue;
if (pNewTimer->timerWord6() != ptA2->timerWord6()) continue;
if (pNewTimer->timerUByte9() == 2)
{
D0W = sw(1 - ptA2->timerUByte9());
pNewTimer->timerUByte9(D0B);
TraceTimer(pNewTimer,0,"Byte9");