-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMonster.cpp
More file actions
4820 lines (4672 loc) · 220 KB
/
Monster.cpp
File metadata and controls
4820 lines (4672 loc) · 220 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"
extern ui8 monsterMoveInhibit[4];
extern bool neophyteSkills;
const char *MonsterName(MONSTERTYPE mt);
bool IsPlayFileOpen();
const char *TimerName(TIMERTYPE tt)
{
const char *timeFuncName;
char name[20];
switch (tt)
{
case TT_M1: timeFuncName = "TT_M1"; break;
case TT_M2: timeFuncName = "TT_M2"; break;
case TT_31: timeFuncName = "TT_31"; break;
case TT_MONSTER_Agroup: timeFuncName = "TT_MONSTER_Agrpup"; break;
case TT_MONSTER_A0: timeFuncName = "TT_MONSTER_A0"; break;
case TT_MONSTER_A1: timeFuncName = "TT_MONSTER_A1"; break;
case TT_MONSTER_A2: timeFuncName = "TT_MONSTER_A2"; break;
case TT_MONSTER_A3: timeFuncName = "TT_MONSTER_A3"; break;
case TT_MONSTER_Bgroup: timeFuncName = "TT_MONSTER_Bgroup"; break;
case TT_MONSTER_B0: timeFuncName = "TT_MONSTER_B0"; break;
case TT_MONSTER_B1: timeFuncName = "TT_MONSTER_B1"; break;
case TT_MONSTER_B2: timeFuncName = "TT_MONSTER_B2"; break;
case TT_MONSTER_B3: timeFuncName = "TT_MONSTER_B3"; break;
default:
sprintf(name, "%d", tt);
timeFuncName = name;
};
return timeFuncName;
}
#ifdef _DEBUG
i32 timertrace[1000];
i32 timertraceindex=0;
void settrace(i32 n)
{
if (n==-1)
{
while (timertraceindex>0) timertrace[--timertraceindex]=0;
}
else if (n==-2)
{
if (TimerTraceActive)
{
i32 i;
for (i=0; i<timertraceindex; i++)
{
fprintf (GETFILE(TraceFile), "%4d %08x\n",i, timertrace[i]);
};
};
}
else if (timertraceindex>999)
{
return;
}
else
{
timertrace[timertraceindex++] = n;
};
}
#endif
#ifdef _DEBUG
#define TIMERTRACE(n) settrace(n);
#else
#define TIMERTRACE(n)
#endif
void EnsureItem16Available()
{
if ( (d.ITEM16QueLen >= d.MaxITEM16-5)
&& (d.LoadedLevel == d.partyLevel) )
{
if (d.MaxITEM16 < 240)
{
i32 i, newMax;
newMax = d.MaxITEM16 + 50;
d.Item16 = (ITEM16 *)UI_realloc(d.Item16, 16 * newMax,
MALLOC087);
for (i=d.MaxITEM16; i< newMax; i++)
{
memset(&d.Item16[i], 0, sizeof(ITEM16));
d.Item16[i].word0 = -1;
};
d.MaxITEM16 = sw(newMax);
}
};
}
//*********************************************************
//
//*********************************************************
// TAG00b1f0
RN CreateMonster(i32 value, //8
i32 hitPointMultiplier, //10
i32 numMonM1,//12 Number Monsters-1??
i32 facing, //14 P4
i32 mapX, //16
i32 mapY,
bool invisible,
bool drawAsSize4) //18
{// (RN)
dReg D0, D3, D4, D5, D6;
RN objD7;
MONSTERDESC *pmtDesc;
DB4 *DB4A3;
i16 w_12;
i16 w_2;
i32 monsterSize;
i32 alternateGraphic;
MONSTERTYPE mt;
static bool warningIssued = false;
mt = (MONSTERTYPE)(value & 0x1f);
alternateGraphic = (value >> 5) & 3;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//
if (!MonsterAllowedOnLevel(RNnul, d.LoadedLevel, mt)) return(RNnul);
EnsureItem16Available();
if ( (d.ITEM16QueLen >= d.MaxITEM16-5)
&& (d.LoadedLevel == d.partyLevel) ) return (RNnul);
objD7=FindEmptyDB4Entry(false);
if (objD7 == RNnul) return (RNnul);
DB4A3 = GetRecordAddressDB4(objD7);
DB4A3->monsterType(mt); // Item26 index
DB4A3->alternateGraphics(alternateGraphic);
pmtDesc = &d.MonsterDescriptor[DB4A3->monsterType()];
monsterSize = pmtDesc->horizontalSize();
switch (monsterSize)
{
case 0: //Any number of monsters is OK.
break;
case 1: //size two.....worms perhaps?
if (numMonM1 > 1)
{
if (!warningIssued && !IsPlayFileOpen())
UI_MessageBox("Too many monsters of size 2","Fixing...",MESSAGE_OK);
warningIssued = true;
numMonM1 = 1;
};
break;
case 2:
if (numMonM1 > 0)
{
if (!warningIssued && !IsPlayFileOpen())
UI_MessageBox("Too many monsters of size 4","Fixing...",MESSAGE_OK);
warningIssued = true;
numMonM1 = 0;
};
break;
default:
break;
};
DB4A3->possession(RNeof);
D3W = 0;
//30OCT02SETWBITS10_10(DB4A3->word14, uw(0));
DB4A3->important(0);
//30OCT02SETWBITS8_9(DB4A3->word14, P4);
DB4A3->facing(facing);
//30OCT02SETWBITS5_6(DB4A3->word14, numMonM1);
DB4A3->numMonM1(numMonM1);
DB4A3->invisible(invisible);
DB4A3->drawAsSize4(drawAsSize4);
w_2 = sw(numMonM1);
if (w_2 != 0)
{
D4W = 0;// Added to avoid compiler warning. Not needed.
D5W = STRandom0_3(); // Monster position????
}
else
{
D4W = 255;
D5W = 0; //Added to avoid compiler warning. Not needed.
};
ASSERT(DB4A3->monsterType() < 27,"monType");
D6W = pmtDesc->baseHealth09; // uByte8[1];
do
{
w_12 = sw(STRandom() % (D6W/4 + 1));
D0W = uw(D6W * hitPointMultiplier + w_12);
DB4A3->hitPoints6[numMonM1] = D0W;
if (w_2 != 0)
{
D4W = SetMonsterPositionBits(
D4W, //old positions byte
numMonM1, //monster number
D5W++); //monster position
if (pmtDesc->horizontalSize() == 1)
{
D5W++;
};
D5W &= 3;
};
} while ((numMonM1--) != 0);
DB4A3->groupIndex(D4W);
// The following function call fixes DB4A3->uByte5.
D0W = MoveObject(objD7, -1, 0, mapX, mapY, NULL, NULL);
ASSERT(D0W != 0xccc,"D0W");
if (D0W != 0)
{
return (RNnul);
}
else
{
QueueSound(soundTELEPORT, mapX, mapY, 1);
return (objD7);
};
}
//*********************************************************
//
//*********************************************************
// TAG00bc12
void StealFromCharacter(DB4 *pDB4, i32 chIdx)
{//()
dReg D0, D1, D5, D6, D7;
RN objD4;
CHARDESC *pcA2;
i16 w_2;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//
w_2 = 0;
pcA2 = &d.CH16482[chIdx];
D1W = sw(Quickness(pcA2));
D7W = sw(100 - D1W);
D5W = sw(STRandom(8));
while ( (D7W>0) && !IsCharacterLucky(pcA2,D7W) )
{
D6W = d.uByte13126[D5W];
if (D6W == 13)
{
D6W = sw(D6W + STRandom(17));
};
objD4 = pcA2->Possession(D6W);
if (objD4 != RNnul)
{
w_2 = 1;
objD4 = RemoveCharacterPossession(chIdx, D6W);
if (pDB4->possession() == RNeof)
{
pDB4->possession(objD4);
}
else
{
AddObjectToRoom(objD4, pDB4->possession(), -1, 0, NULL);
};
DrawCharacterState(chIdx);
};
D5W = sw((D5W + 1) & 7);
D7W -= 20;
};
D0W = sw(STRandom(8));
//
if ( (STRandom(8)==0) || ((w_2!=0) && (STRandomBool()!=0)) )
{
D0W = sw(STRandom(64) + 20);
ASSERT(pDB4->groupIndex() < d.MaxITEM16,"maxitem16");
d.Item16[pDB4->groupIndex()].uByte5 = D0B;
//30OCT02SETWBITS0_3(pDB4->word14, 5);
pDB4->fear(StateOfFear5);
};
}
//*********************************************************
//
//*********************************************************
// TAG00be5a
bool BlockedTypeA(i32 mapX, i32 mapY)
{ // return true if blocked to passage.
// Certain monsters can go through certain doors.
// Door types 3 & 4 are considered blocked as are
// certain special doors on this level.
// dReg D0;
ui32 doorPosition;
CELLFLAG cfD7;
ROOMTYPE rtD6;
DB0 *pDoor;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cfD7 = d.LevelCellFlags[mapX][mapY];
rtD6 = (ROOMTYPE)(cfD7>>5);
if (rtD6 == roomDOOR)
{
pDoor = FirstObjectRecordAddressDB0(mapX, mapY); //Door
doorPosition = (I16)(cfD7 & 7);
if ( (doorPosition == 3) || (doorPosition == 4) ) //closed or almost closed
{
//SET(D0W, D5W==3); //Almost closed
//if (D0W == 0)
//{
// SET(D0W, D5W==4); //Closed
//};
//if (D0W != 0)
//D0W = d.DoorTOC[pDoor->doorType()];//one of two types on level
//SET(D0W, (D0W & 0x100) == 0);// The high order byte
if (neophyteSkills)
{
return (d.DoorTOC[pDoor->doorType()] & 0x100) == 0; //Blocked if not special
}
else
{
return (d.DoorTOC[pDoor->doorType()] & 0x100) == 0; //Blocked if not special
};
};
//D0W = (I16)(D0W & 1);
return false;
};
// SET(D0W, rtD6==roomSTONE);
// if (D0W == 0)
// {
// SET(D0W, rtD6 == roomFALSEWALL);
// if (D0W != 0)
// {
// SET(D0W, (cfD7 & 4) == 0);
// };
// };
// };
// return D0W != 0;
if (rtD6 == roomSTONE) return true;
if (rtD6 != roomFALSEWALL) return false;
if (cfD7 & 4) return false;
return true;
}
//*********************************************************
//
//*********************************************************
// TAG00bf00
bool StoneOrClosedFalseWall(i32 mapX, i32 mapY)
{
CELLFLAG cf;
ROOMTYPE rt;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
cf = d.LevelCellFlags[mapX][mapY];
rt = (ROOMTYPE)(cf>>5);
if (rt == roomSTONE) return true;
if (rt != roomFALSEWALL) return false;
if (cf & 4) return false;
return true;
}
//*********************************************************
//Generally, we try to follow a straight line between
// the party and the monster by keeping the slope
// of the line as constant as possible. If we run into
// a wall (as determined by the provided function) we
// say it is impossible and return a zero.
// Else we return the distance to target. Orthogonal crow.
//*********************************************************
i16 TestDirectPathOpen(i32 mapX, //8
i32 mapY, //10
i32 partyX, //12
i32 partyY, //14
bool (*function)(i32,i32))
{
dReg D0, D1, D4, D5, D6, D7;
i16 w_12;
bool DIAG_10;
i16 w_8;
i16 w_6;
bool NS_4;
i16 w_2;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
D0W = sw(abs(mapX-partyX));
D1W = sw(abs(mapY-partyY));
if (D0W+D1W <= 1) return 1;
D5W = sw(abs(partyX - mapX));
D4W = sw(abs(partyY - mapY));
NS_4 = D5W < D4W; // Set if north/south
DIAG_10 = D5W == D4W; // // set if perfect diagonal
D5W = sw(partyX);
D7W =(I16)((partyX <= mapX) ? 1 : -1); // Y increment party to monster
D4W = sw(partyY);
D6W = (I16)(partyY <= mapY ? 1 : -1); // Increment party to monster
if (NS_4) // If North/south
{
w_2 = sw(D4W - mapY); // partyY-mapY
if (w_2 != 0)
{
D0W = sw(64 * (D5W - mapX) / w_2);
}
else
{
D0W = 128;
};
}
else
{
w_2 = sw(D5W - mapX);
if (w_2 != 0)
{
D0W = sw(64 * (D4W - mapY)/w_2); //partyY-mapY
}
else
{
D0W = 128;
};
};
w_12 = D0W;
do
{
if (DIAG_10) //perfect diagonal
{
if ( ( ((*function)(sw(D5W+D7W), D4W) != 0)
&& ((*function)(D5W, sw(D4W+D6W)) != 0))
|| ((*function)(D5W=sw(D5W+D7W), D4W=sw(D4W+D6W))!=0) )
{
return 0;
};
}
else
{
if (NS_4) //North/south
{
w_2 = sw(D4W-mapY); //
if (w_2 != 0)
{
D0W = sw(64 * (D5W + D7W - mapX)/w_2);
}
else
{
D0W = 128;
};
}
else
{
w_2 = sw(D5W + D7W - mapX); // one step in x-direction vs mapX
if (w_2 != 0) // if not in adjacent column
{
D0W = sw(64 * (D4W - mapY)/w_2); // new slope to monster
}
else
{
D0W = 128;
};
};
w_2 = sw(D0W - w_12); // new slope minus original slope
w_6 = sw(abs(w_2));
if (NS_4)
{
w_2 = sw(D4W + D6W - mapY);
if (w_2 != 0)
{
D1W = sw(64 * (D5W - mapX)/w_2);
}
else
{
D1W = 128;
};
}
else
{
w_2 = sw(D5W - mapX); //#columns to monster
if (w_2 != 0)
{
D1W = sw(64 * (D4W + D6W - mapY)/w_2); // new slope if we take y-step
}
else
{
D1W = 128;
};
};
w_2 = sw(D1W - w_12);//y-step slope vs original slope
w_8 = sw(abs(w_2));//difference in slope if we take y-step
if (w_6 < w_8)
{
D5W = sw(D5W + D7W); // Assume party moves in x-direction
}
else
{
D4W = sw(D4W + D6W);//Assume party takes the y-step
};
D0W = (*function)(D5W, D4W);
if (D0W != 0) // non-zero if blocked
{
if (w_6 != w_8) return 0;
D0W = (*function)(D5W=sw(D5W+D7W), D4W=sw(D4W-D6W));
if (D0W != 0) return 0;
};
};
D0W = sw(abs(D5W - mapX));
D1W = sw(abs(D4W - mapY));
} while (D1W + D0W > 1);
return DistanceBetweenPoints(mapX, mapY, partyX, partyY);
}
//*********************************************************
//
//*********************************************************
// TAG00c202
i16 Blocked(DB4 *pDB4, i32 individualMonsterIndex, i32 mapX, i32 mapY)
{
dReg D0, D4, D5, D6, D7;
MONSTERDESC *pmtDesc;
i16 w_18;
i16 w_8[4];
i32 i;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
pmtDesc = &d.MonsterDescriptor[pDB4->monsterType()];
if ( (d.Invisible != 0)
&& !pmtDesc->seesInvisible() ) return 0;
if (pmtDesc->See360()) goto tag00c2fe;
//ASSERT(pDB4->uByte5 < d.MaxITEM16);
if (pDB4->groupIndex() < d.MaxITEM16)
{
D4W = d.Item16[pDB4->groupIndex()].facings();
}
else
{
D4W = 0x7ccc; //designed to cause error!
};
if (individualMonsterIndex < 0)
{
D6W = 0;
for (i=pDB4->numMonM1(); i>=0; i--)
{
// D7W = sw((D4W >> (2*P2)) & 3); //Direction this monster facing?
D7W = sw(TwoBitNibble(D4W,i)); //Direction this monster facing?
D5W = D6W;
while (D5W-- != 0)
{
D0W = w_8[D5W];
if (D7W == D0W) break;
};
if (D5W == -1) // if we fell out bottom of loop.
{
w_8[D6W++] = D7W;
};
// for (P2) continue
//
}; // while (P2 >= 0)
}
else
{
// w_8[0] = sw((D4W >> 2*P2) & 3);
w_8[0] = sw(TwoBitNibble(D4W,individualMonsterIndex));
D6W = 1;
};
while (D6W-- != 0)
{
D0W = IsItInThisDirection(
w_8[D6W],
mapX,
mapY,
d.partyX,
d.partyY);
if (D0W == 0) continue;
tag00c2fe: //***
D5W = pmtDesc->sightDistance();
if (!pmtDesc->canSeeInDark())
{
D5W = sw(D5W - d.CurrentPalette/2);
};
w_18 = sw(Larger(1, D5W));
if (d.OrthogonalDistance2Party > w_18) return 0; //Cannot see the party then zero.
D0W = TestDirectPathOpen(
mapX, mapY, d.partyX, d.partyY, BlockedTypeA);
return D0W;
//continue here
}; // while (D6-- != 0)
return 0;
}
//*********************************************************
//
//*********************************************************
// TAG00c36c
i16 GetBestMonsterFacingP1(MONSTERDESC *pmtDesc, i32 mapX, i32 mapY)
{
dReg D0, D6, D7;
i16 w_10;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
D7W = pmtDesc->smellingDistance();
if (D7W == 0) return 0;
if ( ((D7W+1)/2 >= d.OrthogonalDistance2Party)
&& (TestDirectPathOpen(mapX,
mapY,
d.partyX,
d.partyY,
StoneOrClosedFalseWall) != 0) )
{
d.SecondaryDirection = d.SecondaryDirectionToParty;
return sw(d.PrimaryDirectionToParty + 1);
};
D6W = SearchFootprints(mapX, mapY);
if (D6W != 0)
{
w_10 = STRandom0_3();
D0W = sw(w_10 + (UI8)(d.Byte13220[D6W-1]));
if (D0W > 30 - 2*D7W)
{
return sw(GetPrimaryAndSecondaryDirection(
mapX,
mapY,
BITS0_4(d.PartyFootprints[D6W]),
BITS5_9(d.PartyFootprints[D6W]) ) + 1);
};
};
return 0;
}
//*********************************************************
//
//*********************************************************
// TAG00c442
bool PossibleMove(const MONSTERDESC *pmtDesc, //8
i32 mapX, //12
i32 mapY, //14
i32 direction, //16
i16 RFPAW) //RecognizesFalsePitsAndWalls
{ // This function expects d.Obj13020 to be the RN of
// a monster. After loading a new game, d.Obj13020 may
// be zero.
// And even during an extended game play, Obj13020 will
// sometimes point to the wrong monster.
// So what do we do about that? To be compatible
// with the initial Atari version, we should deal with the
// zero. To be correct, we should fix d.Obj13020 so that
// it reflects the current monster. So that is what I
// have done.
dReg D0, D1, D4, D5, D6, D7;
CELLFLAG *pcfA0;
DB0 *DB0A2;
DB1 *DB1A2;
DB15 *pDB15A2;
RN obj_2;
D4L = 0xccccc;
D5L = 0xccccc;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
TIMERTRACE(0xc442);
d.DirectionHasBeenTested[direction] = 1;
d.Obj13040 = RNeof;
d.Word13042 = 0;
d.moveOntoPartyPosition = 0;
if (pmtDesc->timePerMove() == 255) return 0;
RelativeXY(direction, 1, 0, &mapX, &mapY);//One step forward
D7W = sw(mapX);
D6W = sw(mapY);
D0W = 0;
d.Word13038 = 1; // Assume move is impossible.
if (D7W < 0) return false;
if (D7W >= d.width) return false;
if (D6W < 0) return false;
if (D6W >= d.height)return false;
if (monsterMoveInhibit[direction] != 0) return false;
pcfA0 = d.LevelCellFlags[D7W]; // column pointer
D5W = pcfA0[D6W]; // Get this room's CELLFLAG
//D5W = D0W
D4W = sw(D5W >> 5); // The type of room.
if (D4W == roomSTONE) return false;
if (D4W == roomSTAIRS)return false;
if (D4W == roomPIT)
{
if ((D5W & 8) != 0) // if (pit is open)
{
if ( !pmtDesc->levitating()) //Levitate over open pit is ok.
{
if (!((D5W&1)?RFPAW:(D5W&1))) //Can monster walk on fake pit?
{
return false;
};
};
};
};
if (D4W == roomFALSEWALL)
{
if ((D5W&4) == 0) // if (closed)
{
if (!((D5W&1)?RFPAW:(D5W&1))) // Can monster walk through false wall?
{
return false;
};
};
};
d.Word13038 = 0;
if (pmtDesc->invincible())
{
for (obj_2 = FindFirstObject(D7W, D6W);
obj_2 != RNeof;
obj_2 = GetDBRecordLink(obj_2))
{
if (obj_2.dbType() != dbCLOUD) continue;
pDB15A2 = GetRecordAddressDB15(obj_2);
if (pDB15A2->cloudType() != CT_FLUXCAGE) continue;
d.FluxCages[direction] = 1;
d.FluxCageCount++;
return 0;
//
//
};
};
for (;;)
{
ASSERT(D4L != 0xccccc,"D4L");
ASSERT(D5L != 0xccccc,"D5L");
if (D4W != roomTELEPORTER) break;
if ((D5W & 8)==0) break;//break if teleporter inactive
if (pmtDesc->word16_12_15() < 10) break;
DB1A2 = FirstObjectRecordAddressDB1(D7W, D6W);
if (DB1A2->what() == 0) break;//if not monsters
// See comment at top of this function about d.Obj13020. It may
// be zero and it may be wrong. I have patched the Atari code
// ProcessTimer29To41 to always put the current monster
// in Obj13020. This seemed the 'right' thing to do.
if (MonsterAllowedOnLevel(d.LastMonsterMoved, DB1A2->destLevel()))
break;
d.Word13038 = 1;
return 0;
};
d.moveOntoPartyPosition = (d.LoadedLevel == d.partyLevel)
&& (D7W == d.partyX)
&& (D6W == d.partyY);
if (d.moveOntoPartyPosition) return 0;
for (;;)
{
if (D4W != roomDOOR) break;//
DB0A2 = FirstObjectRecordAddressDB0(D7W, D6W);
D0W = (I16)(D5W & 7);
D1W = BITS5_5(DB0A2->mode()); //Up-down or sideways
if (D1W != 0)
{
D1W = pmtDesc->verticalSize();
}
else
{
D1W = 1;
};
if (D0W <= D1W) break;
if ((D5W & 7) == 5) break;
if (pmtDesc->nonMaterial()) break;
d.Word13042 = 1;
return 0;
};
d.Obj13040 = FindFirstMonster(D7W, D6W);
return d.Obj13040 == RNeof;
;
}
//*********************************************************
//
//*********************************************************
// TAG00c70c
i16 FindPossibleMove(const MONSTERDESC *pmtDesc,i32 x,i32 y,i16 P4)
{
dReg D0;
i32 direction;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
for (direction = 0; direction < 4; direction++)
{
if (d.DirectionHasBeenTested[direction] != 0) continue;
D0W = PossibleMove(pmtDesc, x, y, direction, P4);
if (D0W != 0) return sw(direction + 1);
//
//
};
return 0;
}
void CallMonsterMovedFilter()
{
}
//*********************************************************
//
//*********************************************************
// TAG00c756
i16 PossibleDoubleMove(const MONSTERDESC *pmtDesc, //8
i32 mapX, //12
i32 mapY, //14
i32 direction) //16
{
dReg D7;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
D7W = sw(direction);
ASSERT(D7W < 4,"D7W");
if (d.FluxCages[D7W] != 0)
{
return 0;
};
mapX = sw(mapX + d.DeltaX[D7W]);
mapY = sw(mapY + d.DeltaY[D7W]);
return PossibleMove(pmtDesc, mapX, mapY, D7W, 0);
}
//*********************************************************
//
//*********************************************************
// TAG00c7bc
void TurnMonster(ITEM16 *pI16,
i32 preferredDirection,
i32 monsterIndex, // within group
bool P4)
{ //P4 means to treat the monsters a pairs. For example,
//worms, which occupy two corners, must turn together.
dReg D0, D5;
i32 newDir;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
//D7W = preferredDirection;
//D6W = monsterIndex;
D5W = P4;
if (P4)
{
if ( (d.Time == d.Time13130)
&& (pI16 == d.pI1613134) ) return;
};
D5W = pI16->facings(); // Old facing byte
// D0W = sw(D5W >> (2*monsterIndex)); // Current facing
D0W = sw(TwoBitNibble(D5W,monsterIndex)); // Current facing
D0W = (I16)((D0W - preferredDirection) & 3); // relative to preference
if (D0W == 2) // if facing away from party
{
newDir = ((STRandom() & 2) + preferredDirection + 1) & 3;
CallMonsterMovedFilter();
D5W = SetMonsterPositionBits(
D5W, //old facing byte
monsterIndex,
newDir); //
}
else
{
newDir = preferredDirection;
CallMonsterMovedFilter();
D5W = SetMonsterPositionBits(
D5W, //old directions byte
monsterIndex,
newDir); //new direction
};
if (P4)
{
D5W = SetMonsterPositionBits(
D5W, //old directions byte
monsterIndex ^ 1, //monster number
newDir); //facing direction
d.Time13130 = d.Time;
d.pI1613134 = pI16;
};
pI16->facings() = D5B;
}
//*********************************************************
//
//*********************************************************
// TAG00c86c
void TurnMonsterGroup(ITEM16 *pI16,
i32 PreferredDirection,
i32 numMonster,
i32 P4)
{ // P4 means monsters should be treated as pairs.
// This particularly applies to worms that occupy
// two corners of the room. They must turn together.
i32 monsterIndex;
bool bD6;
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
monsterIndex = numMonster;
bD6 = ( (monsterIndex!=0) && (P4!=0) );
if (bD6) monsterIndex--;
do
{
//
if ( (monsterIndex == 0) || (STRandomBool() != 0) )
{
TurnMonster(pI16,
PreferredDirection,
monsterIndex,
bD6);
};
monsterIndex--;
} while (monsterIndex >= 0);
}
i32 RanLiveChar()
{ //Return charIndex of a random live character.
i32 ch[4]; // A list of live characters
i32 i, n;
for (i=n=0; i<d.NumCharacter; i++)
{
if (d.CH16482[i].HP() > 0) ch[n++] = i;
};
if (n == 0) return -1;
return ch[STRandom(n)];
}
struct ATTACK_PARAMETERES {
ui32 monsterID;
ui32 monsterType;
ui32 monsterIndex;
i32 monsterLevel;
i32 monsterX;
i32 monsterY;
i32 monsterPos;
i32 missileOriginPosition;
i32 missileRange;
i32 missileDamage;
i32 missileDecayRate;
i32 directionToParty;
i32 distanceToParty;
ui32 missileType;
i32 monsterShouldLaunchMissile;
i32 monsterShouldSteal;
i32 heroToDamage;
i32 attackSoundOrdinal;
i32 disableTime;
i32 supressPoison;
} attackParameters;
//*********************************************************
//
//*********************************************************
// TAG00c8c0
bool MonsterAttacks(RN monster,
DB4 *pDB4, //8
i32 mapX, //12
i32 mapY, //14
i16 monsterIndex) //16 //MonsterIndex within group
{ //(i16)
//Called to compute last parameter of call
// to NextMonsterUpdateTime. (is attacking?)
//Return false if (no live character or ???).
// else true;
dReg D4;
i32 len;
MONSTERTYPE mtD7;
ITEM16 item16_20;
DB4 *DB4A3;
MONSTERDESC *pmtDesc;
CHARDESC *pch_4;
i32 positionsByte;
i32 rightSideMonster;
ui32 key, *pRecord;
RN missile;
/*
RN missileType;
bool monsterShouldThrowMissile;
bool monsterShouldSteal;
i32 directionToParty;
i32 heroToDamage;
i32 attackSoundOrdinal;
i32 missileOriginX;
i32 missileOriginY;
i32 missileOriginPosition;
i32 missileDirection;
i32 missileRange;
i32 missileDamage;
*/
//;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
if (d.NumCharacter == 0) return false;
DB4A3 = pDB4;
d.LastMonsterAttackTime = d.Time;
ASSERT(DB4A3->groupIndex() < d.MaxITEM16,"maxitem16");
memmove(&item16_20, &d.Item16[DB4A3->groupIndex()], 16);
mtD7 = DB4A3->monsterType();
attackParameters.supressPoison = -1;
attackParameters.monsterType = mtD7;
attackParameters.monsterIndex = monsterIndex;
attackParameters.monsterLevel = d.LoadedLevel;
if (AITraceActive)
{
fprintf(GETFILE(TraceFile),
"Monster AI Entering TAG00c9c0 %d(%02d,%02d) %s \n",
d.partyLevel,mapX,mapY,
MonsterName(mtD7));
};
pmtDesc = &d.MonsterDescriptor[mtD7];
attackParameters.monsterID = monster.ConvertToInteger();
//==================================== directionToParty