-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai.cpp
2495 lines (2171 loc) · 54.7 KB
/
ai.cpp
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
/*
* PROPRIETARY INFORMATION. This software is proprietary to POWDER
* Development, and is not to be reproduced, transmitted, or disclosed
* in any way without written permission.
*
* Produced by: Jeff Lait
*
* POWDER Development
*
* NAME: ai.cpp ( POWDER Library, C++ )
*
* COMMENTS:
* ai.cpp contains all the AI for MOBs in POWDER.
*
* The hook for all these routines is doAI(), which is called when
* MOBs get an action phase. This will call a series of utility
* functions with the "ai" prefix to perform what would be the
* best action for this MOB at this time.
*
* Actually performing the action is done by invoking the correct
* "action" prefixed method. These ai methods thus *do* nothing,
* they merely determine what should be done, and then call action*
* to do it.
*/
#include "mygba.h"
#include <stdio.h>
#include <ctype.h>
#include "assert.h"
#include "creature.h"
#include "glbdef.h"
#include "rand.h"
#include "map.h"
#include "item.h"
#include "itemstack.h"
#include "intrinsic.h"
#include "control.h"
#include "sramstream.h"
struct MOB_MEMORY
{
// Who remembers this fact
MOBREF id;
// How many turns it will be remembered for.
u8 turns;
// What element this consists of.
u8 element;
};
PTRSTACK<MOB_MEMORY *> glbMobMemory;
void
ai_init()
{
ai_reset();
}
void
ai_reset()
{
int idx;
for (idx = 0; idx < glbMobMemory.entries(); idx++)
{
delete glbMobMemory(idx);
}
glbMobMemory.clear();
}
void
ai_save(SRAMSTREAM &os)
{
int idx;
// FIrst, collapse list so no zeros
glbMobMemory.collapse();
os.write(glbMobMemory.entries(), 32);
for (idx = 0; idx < glbMobMemory.entries(); idx++)
{
glbMobMemory(idx)->id.save(os);
os.write(glbMobMemory(idx)->turns, 8);
os.write(glbMobMemory(idx)->element, 8);
}
}
void
ai_load(SRAMSTREAM &is)
{
// Reset global list
ai_reset();
int val, i, count;
is.uread(count, 32);
for (i = 0; i < count; i++)
{
MOB_MEMORY *mem;
mem = new MOB_MEMORY;
mem->id.load(is);
is.uread(val, 8);
mem->turns = val;
is.uread(val, 8);
mem->element = val;
}
}
static int
sign(int d)
{
if (d == 0)
return 0;
else if (d < 0)
return -1;
else
return 1;
}
bool
mob_shouldCast(int rarity = 0)
{
return !rand_choice(3+rarity);
}
//
// doAI: This runs the AI script for any MOB. It should handle all
// MOB types.
//
void
MOB::doAI()
{
int diffx, diffy;
int dx, dy;
int distx, disty;
int range;
bool skipmovecheck;
int ai = glb_mobdefs[myDefinition].ai;
bool targetinfov = false;
MOB *killtarget;
MOB *aitarget;
MOB *avatar = MOB::getAvatar();
MOB *owner = getMaster();
// Determine if our FSM is currently on the just-born
// delay state. If so, revert to standard command.
if (myAIFSM == AI_FSM_JUSTBORN)
{
myAIFSM = AI_FSM_NORMAL;
return;
}
if (hasIntrinsic(INTRINSIC_BRAINDEAD))
{
// The AI has been deactivated, likely due to having
// possessed another creature.
return;
}
// Specific types of AI have specific types of actions...
if (getAI() == AI_MOUSE)
{
if (aiDoMouse())
return;
}
// These are the highest priority moves. Stuff like healing, etc.
if (aiDoUrgentMoves())
return;
// Check to see if we can see our enemy...
killtarget = 0;
// Process our AI state.
switch (myAIFSM)
{
case AI_FSM_JUSTBORN:
myAIFSM = AI_FSM_NORMAL;
break;
case AI_FSM_NORMAL:
if (owner)
{
myAIFSM = AI_FSM_GUARD;
}
break;
case AI_FSM_ATTACK:
// If our target is dead, return to guarding.
aitarget = getAITarget();
if (!aitarget || aitarget->hasIntrinsic(INTRINSIC_DEAD))
{
// Return to guarding.
myAIFSM = AI_FSM_GUARD;
}
break;
case AI_FSM_GUARD:
if (!owner)
myAIFSM = AI_FSM_NORMAL;
else
{
// Look at our owner's ai target. We do NOT target ourselves,
// even if our owner did.
// We also refuse to target any pets, or fellow pets.
// (They should have never set their ai target anyways,
// but life is full of surprises.)
aitarget = owner->getAITarget();
if (aitarget && aitarget != this &&
!aitarget->hasCommonMaster(this))
{
// Convert to kill!
myAIFSM = AI_FSM_ATTACK;
setAITarget(aitarget);
}
}
break;
case AI_FSM_STAY:
if (!owner)
myAIFSM = AI_FSM_NORMAL;
// Always stay!
break;
}
aitarget = getAITarget();
if (aitarget)
{
// Double check our aitarget lives. It may have died
// and we just a have a corpse reference.
if (aitarget->hasIntrinsic(INTRINSIC_DEAD))
{
clearAITarget();
aitarget = 0;
}
else
{
if (canSense(aitarget))
killtarget = aitarget;
}
}
// If our ai type attacks the avatar, check that...
if (!killtarget && avatar && (avatar != owner))
{
// Check to see if we are an avatar hunter.
// ie, the hostile flag is set.
if (getAttitude(avatar) == ATTITUDE_HOSTILE)
{
// If we are in the avatar's FOV, we will charge...
if (canSense(avatar))
{
// One final test if the avatar is part of the same
// chain as we are. This way imps summoned by demons
// won't target you.
if (!hasCommonMaster(avatar))
killtarget = avatar;
}
}
}
if (killtarget)
{
// Determine shortest direction...
diffx = killtarget->getX() - getX();
diffy = killtarget->getY() - getY();
dx = sign(diffx);
dy = sign(diffy);
distx = abs(diffx);
disty = abs(diffy);
range = MAX(distx, disty);
targetinfov = true;
// If the kill target is the avatar,
// check to see if we have a pure LOS to the avatar,
// ie, if we are in the FOV. If not, we want to use the smell
// gradient.
if (killtarget->isAvatar() && !glbCurLevel->hasFOV(getX(), getY()))
{
glbCurLevel->getAvatarSmellGradient(this, getX(), getY(), dx, dy);
targetinfov = false;
}
}
// If so, we try to kill it.
if (killtarget)
{
// We only get here if we know where the target is, so don't
// need to recheck LOS.
// We now know the location of our enemy, so mark it.
if (glb_aidefs[getAI()].markattackpos)
{
myAIState &= ~AI_LAST_HIT_LOC;
myAIState |= killtarget->getX() | killtarget->getY() * MAP_WIDTH;
}
// Check if we have a straight line.
if (!dx || !dy || (distx == disty) )
{
if (targetinfov && aiDoRangedAttack(dx, dy, range))
return;
}
// Check to see if any way is passable...
// Because we have someone we hate, we try not to make
// any new enemies.
skipmovecheck = false;
// If we are one distance away, don't need to check.
// This allows us to attack people on unmoveable terrain.
if (((distx == 1) && (disty == 0)) ||
((distx == 0) && (disty == 1)))
{
skipmovecheck = true;;
}
// Grid bugs are allowed to skip if both are one.
if (canMoveDiabolically() &&
distx == 1 && disty == 1)
{
skipmovecheck = true;
}
// If we are 2x2 in size, we can attack with certain
// other combinations.
if (getSize() >= SIZE_GARGANTUAN)
{
// Walk east.
if (dx == 1 && distx == 2 && disty <= 1 && (dy == 0 || dy == 1))
{
skipmovecheck = true;
dx = 1;
dy = 0;
}
// Walk west:
if (dx == -1 && distx == 1 && disty <= 1 && (dy == 0 || dy == 1))
{
skipmovecheck = true;
dx = -1;
dy = 0;
}
// Walk south:
if (dy == 1 && disty == 2 && distx <= 1 && (dx == 0 || dx == 1))
{
skipmovecheck = true;
dx = 0;
dy = 1;
}
// Walk north:
if (dy == -1 && disty == 1 && distx <= 1 && (dx == 0 || dx == 1))
{
skipmovecheck = true;
dx = 0;
dy = -1;
}
}
// If we haven't set skipmove check, we'll be trying to close
// in. We, however, don't want to do this if we have battle
// prep stuff (Buffs, etc.)
if (!skipmovecheck)
if (aiDoBattlePrep(diffx, diffy))
return;
// If we are currently in melee, we may be wanting to flee.
if (skipmovecheck)
{
if ( (killtarget->getExpLevel() > getExpLevel() + 2) ||
(getHP() < 5 && getMaxHP() > 20) )
{
if (aiDoFleeMelee(killtarget))
return;
}
// Continue good old melee.
actionBump(dx, dy);
return;
}
// Walk in the dx/dy direction.
if (aiMoveInDirection(dx, dy, distx, disty, killtarget))
return;
}
if (!killtarget &&
aitarget && glb_aidefs[getAI()].markattackpos &&
(myAIState & AI_LAST_HIT_LOC))
{
// Try to move where the attack came from.
dx = sign( (myAIState & 31) - getX() );
dy = sign( ((myAIState >> 5) & 31) - getY() );
distx = abs( (myAIState & 31) - getX() );
disty = abs( ((myAIState >> 5) & 31) - getY() );
range = MAX(distx, disty);
if (!dx && !dy)
{
// We got to the loc, but found no one! Clear out
// the loc with 30% chance.
// This will have the mob become bored of the loc eventually.
if (rand_chance(30))
myAIState &= ~AI_LAST_HIT_LOC;
}
else
{
MOBREF thismobref;
thismobref.setMob(this);
if (aiMoveInDirection(dx, dy, distx, disty, aitarget))
{
// We *may* now know the location of our enemy.
// By double checking here a mob chasing you can see you
// at the end of its turn and thus catch up.
MOB *thismob;
thismob = thismobref.getMob();
if (thismob && glb_aidefs[thismob->getAI()].markattackpos)
{
aitarget = thismob->getAITarget();
if (aitarget && thismob->canSense(aitarget))
{
thismob->myAIState &= ~AI_LAST_HIT_LOC;
thismob->myAIState |= aitarget->getX() |
aitarget->getY() * MAP_WIDTH;
}
}
return;
}
}
}
if (aiUseInventory())
return;
if (aiEatStuff())
return;
if (aiBePackrat())
return;
// Pets told to stay will always stay in place.
if (myAIFSM == AI_FSM_STAY)
return;
// Follow our master...
if (owner)
{
#if 0
dx = sign(owner->getX() - getX());
dy = sign(owner->getY() - getY());
distx = abs(owner->getX() - getX());
disty = abs(owner->getY() - getY());
range = MAX(distx, disty);
// If we are beside our owner, no need to follow. That would
// just cause us to swap places, which would be annoying.
if (range > 1)
{
// Cancel out invalid follows.
if (dx)
{
if (!canMove(getX()+dx, getY(), true,
aiWillOpenDoors()) ||
glbCurLevel->getMob(getX()+dx, getY()))
{
dx = 0;
}
}
if (dy)
{
if (!canMove(getX(), getY()+dy, true,
aiWillOpenDoors()) ||
glbCurLevel->getMob(getX(), getY()+dy))
{
dy = 0;
}
}
// If both set, unset one of them.
if (dx && dy && !canMoveDiabolically())
{
if (rand_choice(2))
dx = 0;
else
dy = 0;
}
// Move in the chosen direction, if it exists.
if (dx || dy)
{
actionBump(dx, dy);
return;
}
}
#else
distx = abs(owner->getX() - getX());
disty = abs(owner->getY() - getY());
range = MAX(distx, disty);
if (range > 1)
{
if (owner->isAvatar() &&
glbCurLevel->getAvatarSmellGradient(this, getX(), getY(), dx, dy))
{
// Target is 0 because we don't want to swap
// with the avatar.
if (aiMoveInDirection(dx, dy, distx, disty, 0))
{
// Only return if we successful use the hint.
return;
}
}
#if 0
// This code works, but is SLOW.
if (glbCurLevel->findPath(
getX(), getY(),
owner->getX(), owner->getY(),
(MOVE_NAMES) getMoveType(),
false, false,
dx, dy))
{
if (dx || dy)
{
actionBump(dx, dy);
return;
}
}
#endif
}
#endif
}
// if we are the avatar and are standing on downstairs, dive deeper!
if (isAvatar())
{
SQUARE_NAMES tile;
tile = glbCurLevel->getTile(getX(), getY());
if (tile == SQUARE_LADDERDOWN)
{
if (actionClimb())
return;
}
// TODO: it would be nice if we tried something a bit more
// interesting here.
// Like, find nearest unexplored square and go to it.
}
// Random movement...
// We have a 25% chance of just waiting.
// After all, we have no hurry. This allows the avatar
// to position himself, and doesn't guarantee a flee/attack
// in narrow corridors.
if (!rand_choice(4))
return;
findRandomValidMove(dx, dy, owner);
actionBump(dx, dy);
}
bool
MOB::aiMoveInDirection(int dx, int dy, int distx, int disty,
MOB *target)
{
bool skipmovecheck = false;
MOB *blocker;
int delta;
// Grid bugs need to do a move check on the diagonal.
// If diagonal works, skipmovecheck.
if (canMoveDiabolically() && dx && dy)
{
if (canMoveDelta(dx, dy, true, aiWillOpenDoors(), true))
{
// Check to see if we hit someone other than
// our target...
blocker = glbCurLevel->getMob(getX() + dx, getY() + dy);
if (!blocker || (blocker == target))
{
// dx+dy is a valid move...
skipmovecheck = true;
}
}
}
if (!skipmovecheck && dx != 0)
{
if (canMove(getX() + dx, getY(), true, aiWillOpenDoors(), true))
{
delta = dx;
if (getSize() >= SIZE_GARGANTUAN)
{
if (delta > 0)
delta *= 2;
}
// Check to see if we hit someone other than
// our target...
blocker = glbCurLevel->getMob(getX() + delta, getY());
if (!blocker || (blocker == target))
{
// dx maybe a valid move.
if (getSize() >= SIZE_GARGANTUAN)
{
blocker = glbCurLevel->getMob(getX() + delta, getY()+1);
if (blocker && blocker != target)
dx = 0;
}
}
else
dx = 0;
}
else
dx = 0;
}
if (!skipmovecheck && dy != 0)
{
if (canMove(getX(), getY() + dy, true, aiWillOpenDoors(), true))
{
delta = dy;
if (getSize() >= SIZE_GARGANTUAN)
{
if (delta > 0)
delta *= 2;
}
// Check to see if we hit someone other than
// our target...
blocker = glbCurLevel->getMob(getX(), getY() + delta);
if (!blocker || (blocker == target))
{
// dy maybe a valid move...
if (getSize() >= SIZE_GARGANTUAN)
{
blocker = glbCurLevel->getMob(getX()+1, getY() + delta);
if (blocker && blocker != target)
dy = 0;
}
}
else
dy = 0;
}
else
dy = 0;
}
// If both dx & dy are set, zero out one of them at random.
// Grid bugs are the exception, of course. However, if they did
// not skip the movecheck, the diagonal was an invalid direction.
if (dx && dy && (!canMoveDiabolically() || !skipmovecheck))
{
if (rand_choice(2))
dx = 0;
else
dy = 0;
}
// If either is set, bump!
if (dx || dy)
{
// Holy fuck does this get complicated quickly.
// I never meant to create this monstrosity of a conditional!
// (Those who were just grepping for swear words: Shame!)
if (( (distx > 2 && dx) || (disty > 2 && dy) ) &&
hasIntrinsic(INTRINSIC_JUMP) &&
canMoveDelta(dx * 2, dy * 2, true, false, true) &&
!hasIntrinsic(INTRINSIC_BLIND) &&
glbCurLevel->isLit(getX() + dx * 2, getY() + dy * 2) &&
(!glbCurLevel->getMob(getX() + dx * 2, getY() + dy * 2) ||
(glbCurLevel->getMob(getX() + dx * 2, getY() + dy * 2) == this)))
{
int tile;
// Verify the ground is solid enough to jump.
tile = glbCurLevel->getTile(getX(), getY());
if (!(glb_squaredefs[tile].movetype & MOVE_WALK) &&
(!hasIntrinsic(INTRINSIC_WATERWALK) ||
!(glb_squaredefs[tile].movetype & MOVE_SWIM)))
{
// The ground isn't solid enough!
return actionBump(dx, dy);
}
else
{
// Note we will still try and jump from buried or
// space walk situations, but these problems aren't
// any better off with bumping, so no need to special
// case.
// Jump to close quickly.
return actionJump(dx, dy);
}
}
else
return actionBump(dx, dy);
}
return false;
}
bool
MOB::aiCanTargetResist(MOB *target, ELEMENT_NAMES element) const
{
// We only track info on avatar, so early exit if invalid
if (!target || !target->isAvatar())
return false;
MOB *master;
master = getMaster();
if (master)
{
// Check master's knowledge base.
if (master->aiCanTargetResist(target, element))
return true;
// We may know locally as well, so still check our own setting.
}
// Search through the memory list.
int idx;
for (idx = 0; idx < glbMobMemory.entries(); idx++)
{
if (!glbMobMemory(idx))
continue;
if (glbMobMemory(idx)->id.getMob() == this &&
glbMobMemory(idx)->element == element)
{
// We have a match!
return true;
}
}
// Not found, fail
return false;
}
void
MOB::aiDecayKnowledgeBase()
{
// Currently only avatar has such a table
if (!isAvatar())
return;
int idx;
for (idx = 0; idx < glbMobMemory.entries(); idx++)
{
if (glbMobMemory(idx))
{
if (!glbMobMemory(idx)->id.getMob() ||
!glbMobMemory(idx)->turns)
{
delete glbMobMemory(idx);
glbMobMemory.set(idx, 0);
}
else
glbMobMemory(idx)->turns--;
}
}
// Collpase the list
glbMobMemory.collapse();
}
void
MOB::aiNoteThatTargetHasIntrinsic(MOB *target, INTRINSIC_NAMES intrinsic)
{
switch (intrinsic)
{
case INTRINSIC_RESISTFIRE:
aiNoteThatTargetCanResist(target, ELEMENT_FIRE);
break;
case INTRINSIC_RESISTCOLD:
aiNoteThatTargetCanResist(target, ELEMENT_COLD);
break;
case INTRINSIC_RESISTACID:
aiNoteThatTargetCanResist(target, ELEMENT_ACID);
break;
case INTRINSIC_RESISTSHOCK:
aiNoteThatTargetCanResist(target, ELEMENT_SHOCK);
break;
case INTRINSIC_BLIND:
aiNoteThatTargetCanResist(target, ELEMENT_LIGHT);
break;
case INTRINSIC_RESISTPHYSICAL:
aiNoteThatTargetCanResist(target, ELEMENT_PHYSICAL);
break;
// Fake elements:
case INTRINSIC_RESISTPOISON:
aiNoteThatTargetCanResist(target, ELEMENT_POISON);
break;
case INTRINSIC_REFLECTION:
aiNoteThatTargetCanResist(target, ELEMENT_REFLECTIVITY);
break;
case INTRINSIC_RESISTSLEEP:
aiNoteThatTargetCanResist(target, ELEMENT_SLEEP);
break;
default:
// Everything else is ignored.
break;
}
}
void
MOB::aiNoteThatTargetCanResist(MOB *target, ELEMENT_NAMES element)
{
// If the target isn't the avatar, we don't track
if (!target || !target->isAvatar())
return;
// 50% chance of noting resistance
if (!rand_chance(50))
return;
aiTrulyNoteThatTargetCanResist(target, element);
}
void
MOB::aiTrulyNoteThatTargetCanResist(MOB *target, ELEMENT_NAMES element)
{
// Pass on to our master, if any.
MOB *master;
master = getMaster();
if (master)
{
master->aiTrulyNoteThatTargetCanResist(target, element);
}
// Verify we are not the avatar, as we don't care about our own
// references :>
if (isAvatar())
return;
// Check the avatar resist list for this mob.
int idx;
for (idx = 0; idx < glbMobMemory.entries(); idx++)
{
if (glbMobMemory(idx))
{
if (glbMobMemory(idx)->id.getMob() == this &&
glbMobMemory(idx)->element == element)
{
// Restore the counter.
glbMobMemory(idx)->turns = 100;
return;
}
}
}
// Failed to find on list, append.
MOB_MEMORY *mem = new MOB_MEMORY;
mem->id.setMob(this);
mem->element = element;
mem->turns = 100;
glbMobMemory.append(mem);
}
bool
MOB::aiDoMouse()
{
int lastdir, dir, newdir;
int dx, dy;
bool found = false;
// Least two bits track our last direction.
lastdir = myAIState & 3;
// We want to try and track a wall. We want this wall to
// be on our right hand side.
// Mob looking right:
// ?@ <- turn south to keep wall.
// X.
//
// ?@. <= Bad state, no old wall! Keep going straight.
// ..?
//
// ?@. <= Go straight
// ?X?
//
// ?.?
// ?@X <= Turn and head north.
// XX?
//
// ?X?
// .@X <= Turn around.
// ?X?
// Is there a wall to the right?
getDirection((lastdir + 1) & 3, dx, dy);
if (!canMoveDelta(dx, dy, true, aiWillOpenDoors()) ||
glbCurLevel->getMob(getX() + dx, getY() + dy))
{
// Got a wall to the right. Try first available direction.
dir = lastdir;
while (1)
{
getDirection(dir, dx, dy);
if (canMoveDelta(dx, dy, true, aiWillOpenDoors()) &&
!glbCurLevel->getMob(getX() + dx, getY() + dy))
{
newdir = dir;
break;
}
// Keeping wall on right means turning left first!
dir--;
dir &= 3;
if (dir == lastdir)
{
newdir = -1;
break;
}
}
}
else
{
// No wall to the right. If there is a wall to the right &
// behind us, we want to turn right.
// Otherwise, we're in a bad state and keep going.
int bx, by;
getDirection( (lastdir + 2) & 3, bx, by);
// This neat trick gets us the back right wall:
bx |= dx;
by |= dy;
if (!canMoveDelta(bx, by, true, aiWillOpenDoors()) ||
glbCurLevel->getMob(getX() + bx, getY() + by))
{
// There is a wall, want to turn right.
newdir = lastdir + 1;
newdir &= 3;
}
else
{
// Go straight.
newdir = lastdir;
}
}
if (newdir == -1)
newdir = lastdir;
else
found = true;
// Store our last direction.
myAIState &= ~3;
myAIState |= newdir;
if (found)
{
return actionBump(dx, dy);
}
// Mouse is cornered! Return to normal AI.
return false;
}
bool
MOB::aiDoUrgentMoves()
{
// This determines if there is any condition that should
// trump everything else.
// 0) If turning to stone, stop it.
if (hasIntrinsic(INTRINSIC_STONING))
{
if (aiDoStopStoningSelf())
return true;
}
// 1) If health is low, heal.
int safelevel;
safelevel = (getMaxHP() >> 3) + 1;
if ((safelevel < getMaxHP()) && (getHP() <= safelevel))
{
if (aiDoHealSelf())
return true;
}
// 2) If poisoned, cure it!
if (aiIsPoisoned())
{
if (aiDoCurePoisonSelf())
return true;
}