forked from bradharding/doomretro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp_enemy.c
2983 lines (2404 loc) · 85.5 KB
/
p_enemy.c
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
/*
==============================================================================
DOOM Retro
The classic, refined DOOM source port. For Windows PC.
==============================================================================
Copyright © 1993-2024 by id Software LLC, a ZeniMax Media company.
Copyright © 2013-2024 by Brad Harding <mailto:[email protected]>.
This file is a part of DOOM Retro.
DOOM Retro 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 3 of the license, or (at your
option) any later version.
DOOM Retro 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 DOOM Retro. If not, see <https://www.gnu.org/licenses/>.
DOOM is a registered trademark of id Software LLC, a ZeniMax Media
company, in the US and/or other countries, and is used without
permission. All other trademarks are the property of their respective
holders. DOOM Retro is in no way affiliated with nor endorsed by
id Software.
==============================================================================
*/
#include "c_cmds.h"
#include "c_console.h"
#include "d_deh.h"
#include "doomstat.h"
#include "g_game.h"
#include "i_controller.h"
#include "i_timer.h"
#include "m_bbox.h"
#include "m_config.h"
#include "m_misc.h"
#include "m_random.h"
#include "p_inter.h"
#include "p_local.h"
#include "p_setup.h"
#include "p_tick.h"
#include "s_sound.h"
#define DISTFRIEND (128 * FRACUNIT) // distance friends tend to move towards players
#define BARRELRANGE (512 * FRACUNIT)
uint64_t shake = 0;
int shakeduration = 0;
void A_Fall(mobj_t *actor, player_t *player, pspdef_t *psp);
//
// ENEMY THINKING
// Enemies are always spawned
// with targetplayer = -1, threshold = 0
// Most monsters are spawned unaware of all players,
// but some can be made preaware
//
//
// P_RecursiveSound
// Called by P_NoiseAlert.
// Recursively traverse adjacent sectors,
// sound blocking lines cut off traversal.
//
// killough 05/05/98: reformatted, cleaned up
//
static void P_RecursiveSound(sector_t *sec, const int soundblocks, mobj_t *soundtarget)
{
// wake up all monsters in this sector
if (sec->validcount == validcount && sec->soundtraversed <= soundblocks + 1)
return; // already flooded
sec->validcount = validcount;
sec->soundtraversed = soundblocks + 1;
P_SetTarget(&sec->soundtarget, soundtarget);
for (int i = 0; i < sec->linecount; i++)
{
line_t *line = sec->lines[i];
const int flags = line->flags;
if (!(flags & ML_TWOSIDED))
continue;
P_LineOpening(line);
if (openrange <= 0)
continue; // closed door
if (!(flags & ML_SOUNDBLOCK))
P_RecursiveSound(sides[line->sidenum[(sides[line->sidenum[0]].sector == sec)]].sector, soundblocks, soundtarget);
else if (!soundblocks)
P_RecursiveSound(sides[line->sidenum[(sides[line->sidenum[0]].sector == sec)]].sector, 1, soundtarget);
}
}
//
// P_NoiseAlert
// If a monster yells at the player,
// it will alert other monsters to the player.
//
void P_NoiseAlert(mobj_t *target)
{
// [BH] don't alert if notarget CCMD is enabled
if (target->player && (viewplayer->cheats & CF_NOTARGET))
return;
validcount++;
P_RecursiveSound(target->subsector->sector, 0, target);
}
//
// P_CheckRange
//
static bool P_CheckRange(mobj_t *actor, const fixed_t range)
{
mobj_t *target = actor->target;
// killough 07/18/98: friendly monsters don't attack other friends
if (actor->flags & target->flags & MF_FRIEND)
return false;
if (P_ApproxDistance(target->x - actor->x, target->y - actor->y) >= range)
return false;
// [BH] check difference in height as well
if (!infiniteheight && !compat_nopassover
&& (target->z > actor->z + actor->height || actor->z > target->z + target->height))
return false;
if (!P_CheckSight(actor, target))
return false;
return true;
}
//
// P_CheckMeleeRange
//
// MBF21: add meleerange property
//
bool P_CheckMeleeRange(mobj_t *actor)
{
mobj_t *target = actor->target;
if (!target)
return false;
return P_CheckRange(actor, actor->info->meleerange + target->info->radius - 20 * FRACUNIT);
}
//
// P_HitFriend
//
// killough 12/98
// This function tries to prevent shooting at friends
//
static bool P_HitFriend(mobj_t *actor)
{
mobj_t *target;
if (!(actor->flags & MF_FRIEND) || !(target = actor->target))
return false;
P_AimLineAttack(actor, R_PointToAngle2(actor->x, actor->y, target->x, target->y),
P_ApproxDistance(actor->x - target->x, actor->y - target->y), 0);
return (linetarget && linetarget != target && !((linetarget->flags ^ actor->flags) & MF_FRIEND));
}
//
// P_CheckMissileRange
//
static bool P_CheckMissileRange(mobj_t *actor)
{
fixed_t dist;
mobj_t *target = actor->target;
if (!P_CheckSight(actor, target))
return false;
if (actor->flags & MF_JUSTHIT)
{
// the target just hit the enemy, so fight back!
actor->flags &= ~MF_JUSTHIT;
// killough 07/18/98: no friendly fire at corpses
// killough 11/98: prevent too much infighting among friends
return (!(actor->flags & MF_FRIEND)
|| (target->health > 0
&& (!(target->flags & MF_FRIEND)
|| (target->player ? M_Random() > 128 : (!(target->flags & MF_JUSTHIT) && M_Random() > 128)))));
}
// killough 07/18/98: friendly monsters don't attack other friendly
// monsters or players (except when attacked, and then only once)
if (actor->flags & target->flags & MF_FRIEND)
return false;
if (actor->reactiontime)
return false; // do not attack yet
dist = (P_ApproxDistance(actor->x - target->x, actor->y - target->y) >> FRACBITS) - 64;
if (actor->info->meleestate == S_NULL)
dist -= 128; // no melee attack, so fire more
if ((actor->mbf21flags & MF_MBF21_SHORTMRANGE) && dist > 14 * 64)
return false; // too far away
if ((actor->mbf21flags & MF_MBF21_LONGMELEE) && dist < 196)
return false; // close for fist attack
if (actor->mbf21flags & MF_MBF21_RANGEHALF)
dist >>= 1;
if (dist > 200)
dist = 200;
if ((actor->mbf21flags & MF_MBF21_HIGHERMPROB) && dist > 160)
dist = 160;
if (M_Random() < dist)
return false;
if ((actor->flags & MF_FRIEND) && P_HitFriend(actor))
return false;
return true;
}
//
// P_IsUnderDamage
//
// killough 09/09/98:
//
// Returns nonzero if the object is under damage based on
// their current position. Returns 1 if the damage is moderate,
// -1 if it is serious. Used for AI.
//
static int P_IsUnderDamage(const mobj_t *actor)
{
int direction = 0;
for (const struct msecnode_s *seclist = actor->touching_sectorlist; seclist; seclist = seclist->m_tnext)
{
const ceiling_t *ceiling = seclist->m_sector->ceilingdata; // Crushing ceiling
if (ceiling && ceiling->thinker.function == &T_MoveCeiling)
direction |= ceiling->direction;
}
return direction;
}
//
// P_Move
// Move in the current direction,
// returns false if the move is blocked.
//
static const fixed_t xspeed[] = { FRACUNIT, 47000, 0, -47000, -FRACUNIT, -47000, 0, 47000 };
static const fixed_t yspeed[] = { 0, 47000, FRACUNIT, 47000, 0, -47000, -FRACUNIT, -47000 };
static bool P_Move(mobj_t *actor, const int dropoff) // killough 09/12/98
{
fixed_t tryx, tryy;
fixed_t deltax, deltay;
fixed_t origx, origy;
int movefactor;
int friction = ORIG_FRICTION;
int speed;
if (actor->movedir == DI_NODIR)
return false;
// killough 10/98: make monsters get affected by ice and sludge too:
movefactor = P_GetMoveFactor(actor, &friction);
speed = actor->info->speed;
if (friction < ORIG_FRICTION // sludge
&& !(speed = ((ORIG_FRICTION_FACTOR - (ORIG_FRICTION_FACTOR - movefactor) / 2) * speed) / ORIG_FRICTION_FACTOR))
speed = 1; // always give the monster a little bit of speed
tryx = (origx = actor->x) + (deltax = speed * xspeed[actor->movedir]);
tryy = (origy = actor->y) + (deltay = speed * yspeed[actor->movedir]);
if (!P_TryMove(actor, tryx, tryy, dropoff))
{
// open any specials
int good;
if ((actor->flags & MF_FLOAT) && floatok)
{
actor->z += (actor->z < tmfloorz ? FLOATSPEED : -FLOATSPEED); // must adjust height
actor->flags |= MF_INFLOAT;
return true;
}
if (!numspechit)
return false;
actor->movedir = DI_NODIR;
// if the special is not a door that can be opened, return false
//
// killough 08/09/98: this is what caused monsters to get stuck in
// doortracks, because it thought that the monster freed itself
// by opening a door, even if it was moving towards the doortrack,
// and not the door itself.
//
// killough 09/09/98: If a line blocking the monster is activated,
// return true 90% of the time. If a line blocking the monster is
// not activated, but some other line is, return false 90% of the
// time. A bit of randomness is needed to ensure it's free from
// lockups, but for most cases, it returns the correct result.
//
// Do NOT simply return false 1/4th of the time (causes monsters to
// back out when they shouldn't, and creates secondary stickiness).
for (good = 0; numspechit--; )
if (P_UseSpecialLine(actor, spechit[numspechit], 0, false))
good |= (spechit[numspechit] == blockline ? 1 : 2);
return (good && ((M_Random() >= 230) ^ (good & 1)));
}
else
{
actor->flags &= ~MF_INFLOAT;
// killough 10/98:
// Let normal momentum carry them, instead of steptoeing them across ice.
if (friction > ORIG_FRICTION)
{
actor->x = origx;
actor->y = origy;
movefactor *= FRACUNIT / ORIG_FRICTION_FACTOR / 4;
actor->momx += FixedMul(deltax, movefactor);
actor->momy += FixedMul(deltay, movefactor);
}
}
// killough 11/98: fall more slowly, under gravity, if felldown == true
if (!(actor->flags & MF_FLOAT) && !felldown)
actor->z = actor->floorz;
return true;
}
//
// P_SmartMove
//
// killough 09/12/98: Same as P_Move(), except smarter
//
static bool P_SmartMove(mobj_t *actor)
{
mobj_t *target = actor->target;
int dropoff = 0;
int underdamage = P_IsUnderDamage(actor);
// killough 09/12/98: stay on a lift if target is on one
bool onlift = (target && target->health > 0
&& target->subsector->sector->tag == actor->subsector->sector->tag
&& actor->subsector->sector->islift);
// killough 10/98: allow dogs to drop off of taller ledges sometimes.
// dropoff == 1 means always allow it, dropoff == 2 means only up to 128 high,
// and only if the target is immediately on the other side of the line.
if (actor->type == MT_DOGS
&& target && !((target->flags ^ actor->flags) & MF_FRIEND)
&& (target->player || P_ApproxDistance(actor->x - target->x, actor->y - target->y) < 144 * FRACUNIT)
&& M_Random() < 235)
dropoff = (target->player ? 1 : 2);
if (!P_Move(actor, dropoff))
return false;
// killough 09/09/98: avoid crushing ceilings or other damaging areas
if ((onlift && M_Random() < 230 // stay on lift
&& !actor->subsector->sector->islift)
|| (!underdamage // get away from damage
&& (underdamage = P_IsUnderDamage(actor))
&& (underdamage < 0 || M_Random() < 200)))
actor->movedir = DI_NODIR; // avoid the area (most of the time anyway)
return true;
}
//
// TryWalk
// Attempts to move actor on
// in its current (ob->moveangle) direction.
// If blocked by either a wall or an actor
// returns FALSE
// If move is either clear or blocked only by a door,
// returns true and sets...
// If a door is in the way,
// an OpenDoor call is made to start it opening.
//
static bool P_TryWalk(mobj_t *actor)
{
if (!P_SmartMove(actor))
return false;
actor->movecount = (M_Random() & 15);
return true;
}
//
// P_DoNewChaseDir
//
// killough 09/08/98:
//
// Most of P_NewChaseDir(), except for what
// determines the new direction to take
//
static void P_DoNewChaseDir(mobj_t *actor, const fixed_t deltax, const fixed_t deltay)
{
const dirtype_t opposite[] =
{
DI_WEST,
DI_SOUTHWEST,
DI_SOUTH,
DI_SOUTHEAST,
DI_EAST,
DI_NORTHEAST,
DI_NORTH,
DI_NORTHWEST,
DI_NODIR
};
const dirtype_t diags[] =
{
DI_NORTHWEST,
DI_NORTHEAST,
DI_SOUTHWEST,
DI_SOUTHEAST
};
dirtype_t xdir, ydir;
const dirtype_t olddir = actor->movedir;
const dirtype_t turnaround = opposite[olddir];
bool attempts[NUMDIRS - 1] = { false };
xdir = (deltax > 10 * FRACUNIT ? DI_EAST : (deltax < -10 * FRACUNIT ? DI_WEST : DI_NODIR));
ydir = (deltay < -10 * FRACUNIT ? DI_SOUTH : (deltay > 10 * FRACUNIT ? DI_NORTH : DI_NODIR));
// try direct route
if (xdir != DI_NODIR && ydir != DI_NODIR)
{
actor->movedir = diags[((deltay < 0) << 1) + (deltax > 0)];
if (actor->movedir != turnaround)
{
attempts[actor->movedir] = true;
if (P_TryWalk(actor))
return;
}
}
// try other directions
if (M_Random() > 200 || ABS(deltay) > ABS(deltax))
SWAP(xdir, ydir);
if (xdir == turnaround)
xdir = DI_NODIR;
if (ydir == turnaround)
ydir = DI_NODIR;
if (xdir != DI_NODIR && !attempts[xdir])
{
actor->movedir = xdir;
attempts[xdir] = true;
if (P_TryWalk(actor))
return; // either moved forward or attacked
}
if (ydir != DI_NODIR && !attempts[ydir])
{
actor->movedir = ydir;
attempts[ydir] = true;
if (P_TryWalk(actor))
return;
}
// there is no direct path to the player, so pick another direction.
if (olddir != DI_NODIR && !attempts[olddir])
{
actor->movedir = olddir;
attempts[olddir] = true;
if (P_TryWalk(actor))
return;
}
// randomly determine direction of search
if (M_Random() & 1)
{
for (int tdir = DI_EAST; tdir <= DI_SOUTHEAST; tdir++)
if (tdir != turnaround && !attempts[tdir])
{
actor->movedir = tdir;
attempts[tdir] = true;
if (P_TryWalk(actor))
return;
}
}
else
for (int tdir = DI_SOUTHEAST; tdir >= DI_EAST; tdir--)
if (tdir != turnaround && !attempts[tdir])
{
actor->movedir = tdir;
attempts[tdir] = true;
if (P_TryWalk(actor))
return;
}
if (turnaround != DI_NODIR && !attempts[turnaround])
{
actor->movedir = turnaround;
if (P_TryWalk(actor))
return;
}
actor->movedir = DI_NODIR; // cannot move
}
//
// killough 11/98:
//
// Monsters try to move away from tall dropoffs.
//
// In DOOM, they were never allowed to hang over dropoffs,
// and would remain stuck if involuntarily forced over one.
// This logic, combined with p_map.c (P_TryMove), allows
// monsters to free themselves without making them tend to
// hang over dropoffs.
//
static fixed_t dropoff_deltax;
static fixed_t dropoff_deltay;
static fixed_t floorz;
static bool PIT_AvoidDropoff(line_t *line)
{
if (line->backsector // ignore one-sided linedefs
&& tmbbox[BOXRIGHT] > line->bbox[BOXLEFT]
&& tmbbox[BOXLEFT] < line->bbox[BOXRIGHT]
&& tmbbox[BOXTOP] > line->bbox[BOXBOTTOM] // linedef must be contacted
&& tmbbox[BOXBOTTOM] < line->bbox[BOXTOP]
&& P_BoxOnLineSide(tmbbox, line) == -1)
{
const fixed_t front = line->frontsector->floorheight;
const fixed_t back = line->backsector->floorheight;
angle_t angle;
// The monster must contact one of the two floors,
// and the other must be a tall dropoff (more than 24).
if (back == floorz && front < floorz - 24 * FRACUNIT)
angle = R_PointToAngle2(0, 0, line->dx, line->dy) >> ANGLETOFINESHIFT; // front side dropoff
else if (front == floorz && back < floorz - 24 * FRACUNIT)
angle = R_PointToAngle2(line->dx, line->dy, 0, 0) >> ANGLETOFINESHIFT; // back side dropoff
else
return true;
// Move away from dropoff at a standard speed.
// Multiple contacted linedefs are cumulative (e.g. hanging over corner)
dropoff_deltax -= finesine[angle] * 32;
dropoff_deltay += finecosine[angle] * 32;
}
return true;
}
//
// Driver for above
//
static bool P_AvoidDropoff(const mobj_t *actor)
{
const int xh = P_GetSafeBlockX((tmbbox[BOXRIGHT] = actor->x + actor->radius) - bmaporgx);
const int xl = P_GetSafeBlockX((tmbbox[BOXLEFT] = actor->x - actor->radius) - bmaporgx);
const int yh = P_GetSafeBlockY((tmbbox[BOXTOP] = actor->y + actor->radius) - bmaporgy);
const int yl = P_GetSafeBlockY((tmbbox[BOXBOTTOM] = actor->y - actor->radius) - bmaporgy);
floorz = actor->z; // remember floor height
dropoff_deltax = 0;
dropoff_deltay = 0;
// check lines
validcount++;
for (int bx = xl; bx <= xh; bx++)
for (int by = yl; by <= yh; by++)
P_BlockLinesIterator(bx, by, &PIT_AvoidDropoff); // all contacted lines
return (dropoff_deltax || dropoff_deltay); // false if movement prescribed
}
//
// P_NewChaseDir
//
// killough 09/08/98: Split into two functions
//
static void P_NewChaseDir(mobj_t *actor)
{
mobj_t *target;
fixed_t deltax, deltay;
// Move away from dropoff
if (actor->floorz - actor->dropoffz > 24 * FRACUNIT
&& actor->z <= actor->floorz
&& !(actor->flags & (MF_DROPOFF | MF_FLOAT))
&& P_AvoidDropoff(actor))
{
P_DoNewChaseDir(actor, dropoff_deltax, dropoff_deltay);
// If moving away from dropoff, set movecount to 1 so that
// small steps are taken to get monster away from dropoff.
actor->movecount = 1;
return;
}
target = actor->target;
deltax = target->x - actor->x;
deltay = target->y - actor->y;
// Move away from friends when too close, except in certain situations (e.g. a crowded lift)
if ((actor->flags & target->flags & MF_FRIEND)
&& !actor->subsector->sector->islift
&& P_ApproxDistance(deltax, deltay) < DISTFRIEND
&& !P_IsUnderDamage(actor))
{
deltax = -deltax;
deltay = -deltay;
}
P_DoNewChaseDir(actor, deltax, deltay);
}
static bool P_LookForMonsters(mobj_t *actor)
{
// Remember last enemy
if (actor->lastenemy && actor->lastenemy->health > 0
&& !(actor->lastenemy->flags & actor->flags & MF_FRIEND)) // not friends
{
P_SetTarget(&actor->target, actor->lastenemy);
P_SetTarget(&actor->lastenemy, NULL);
return true;
}
for (thinker_t *th = thinkers[th_mobj].cnext; th != &thinkers[th_mobj]; th = th->cnext)
{
mobj_t *mo = (mobj_t *)th;
mobj_t *target;
thinker_t *cap;
if (!(mo->flags & MF_COUNTKILL) || mo == actor || mo->health <= 0)
continue; // not a valid monster
if (!((mo->flags ^ actor->flags) & MF_FRIEND) && !infight)
continue; // don't attack other friends
// If the monster is already engaged in a one-on-one attack
// with a healthy friend, don't attack around 60% the time
if ((target = mo->target) && target->target == mo && M_Random() > 100
&& ((target->flags ^ mo->flags) & MF_FRIEND)
&& target->health * 2 >= target->info->spawnhealth)
continue;
if (P_ApproxDistance(actor->x - mo->x, actor->y - mo->y) > 32 * 64 * FRACUNIT)
continue; // out of range
if (!P_CheckSight(actor, mo))
continue; // out of sight
// Found a target monster
P_SetTarget(&actor->lastenemy, actor->target);
P_SetTarget(&actor->target, mo);
// Move the selected monster to the end of the
// list, so that it gets searched last next time.
cap = &thinkers[th_mobj];
(mo->thinker.cprev->cnext = mo->thinker.cnext)->cprev = mo->thinker.cprev;
(mo->thinker.cprev = cap->cprev)->cnext = &mo->thinker;
(mo->thinker.cnext = cap)->cprev = &mo->thinker;
return true;
}
return false;
}
//
// P_LookForPlayer
// If allaround is false, only look 180 degrees in front.
// Returns true if the player is targeted.
//
static bool P_LookForPlayer(mobj_t *actor, const bool allaround)
{
mobj_t *mo = viewplayer->mo;
// the player is dead, if near the player then look for other monsters
if (infight && P_CheckSight(actor, mo))
return P_LookForMonsters(actor);
if (viewplayer->cheats & CF_NOTARGET)
return false;
// killough 09/09/98: friendly monsters go about players differently
if (actor->flags & MF_FRIEND)
{
// Go back to the player, no matter whether they're visible or not
if (viewplayer->playerstate == PST_LIVE)
{
P_SetTarget(&actor->target, mo);
// killough 12/98:
// get out of refiring loop, to avoid hitting player accidentally
if (actor->info->missilestate != S_NULL)
{
P_SetMobjState(actor, actor->info->seestate);
actor->flags &= ~MF_JUSTHIT;
}
return true;
}
return false;
}
if (viewplayer->health <= 0 || !P_CheckSight(actor, mo))
{
// Use last known enemy if no players sighted -- killough 02/15/98
if (actor->lastenemy && actor->lastenemy->health > 0)
{
P_SetTarget(&actor->target, actor->lastenemy);
P_SetTarget(&actor->lastenemy, NULL);
return true;
}
return false;
}
if (!allaround)
{
const angle_t an = R_PointToAngle2(actor->x, actor->y, mo->x, mo->y) - actor->angle;
if (an > ANG90 && an < ANG270)
// if real close, react anyway
if (P_ApproxDistance(mo->x - actor->x, mo->y - actor->y) > WAKEUPRANGE)
{
// Use last known enemy if no players sighted -- killough 02/15/98
if (actor->lastenemy && actor->lastenemy->health > 0)
{
P_SetTarget(&actor->target, actor->lastenemy);
P_SetTarget(&actor->lastenemy, NULL);
return true;
}
return false;
}
}
if (mo->flags & MF_FUZZ)
{
// player is invisible
if (P_ApproxDistance(mo->x - actor->x, mo->y - actor->y) > 2 * MELEERANGE
&& P_ApproxDistance(mo->momx, mo->momy) < 5 * FRACUNIT)
return false; // player is sneaking - can't detect
if (M_Random() < 225)
return false; // player isn't sneaking, but still didn't detect
}
P_SetTarget(&actor->target, mo);
// killough 09/09/98: give monsters a threshold towards getting players
// (we don't want it to be too easy for a player with dogs :)
actor->threshold = 60;
return true;
}
static bool P_LookForTargets(mobj_t *actor, int allaround)
{
if ((actor->flags & MF_FRIEND) && P_LookForMonsters(actor))
return true;
return P_LookForPlayer(actor, allaround);
}
static void P_ShakeOnExplode(const mobj_t *actor)
{
if (actor->type == MT_BARREL && r_shake_barrels)
{
const mobj_t *mo = viewplayer->mo;
if (mo->z <= mo->floorz && P_ApproxDistance(actor->x - mo->x, actor->y - mo->y) < BARRELRANGE)
{
shakeduration = EXPLODINGBARREL;
shake = I_GetTimeMS() + shakeduration;
if (joy_rumble_barrels)
{
const int strength = 20000 * joy_rumble_barrels / 100;
I_ControllerRumble(strength, strength);
barrelrumbletics = TICRATE;
}
}
}
}
static void P_SpawnBloodOnMelee(mobj_t *target, const int damage)
{
if (!r_blood_melee || r_blood == r_blood_none)
return;
if (target->player)
{
if (!viewplayer->powers[pw_invulnerability] && !(viewplayer->cheats & CF_GODMODE))
{
const unsigned int an = viewangle >> ANGLETOFINESHIFT;
P_SpawnBlood(viewx + 20 * finecosine[an], viewy + 20 * finesine[an], viewz, 0, damage, target);
}
}
else if (!(target->flags & MF_NOBLOOD))
P_SpawnBlood(target->x, target->y, target->z + M_RandomInt(4, 16) * FRACUNIT, 0, damage, target);
}
//
// ACTION ROUTINES
//
//
// A_Look
// Stay in state until the player is sighted.
//
void A_Look(mobj_t *actor, player_t *player, pspdef_t *psp)
{
mobj_t *target;
const int flags = actor->flags;
const bool friendly = (flags & MF_FRIEND);
actor->threshold = 0; // any shot will wake up
// killough 07/18/98:
// Friendly monsters go after other monsters first, but
// also return to player, without attacking them, if they
// cannot find any targets. A marine's best friend :)
actor->pursuecount = 0;
if (!(friendly
&& P_LookForTargets(actor, false))
&& !((target = actor->subsector->sector->soundtarget)
&& (target->flags & MF_SHOOTABLE)
&& (P_SetTarget(&actor->target, target), (!(flags & MF_AMBUSH) || P_CheckSight(actor, target))))
&& (friendly || !P_LookForTargets(actor, false)))
return;
// go into chase state
if (actor->info->seesound)
{
switch (actor->info->seesound)
{
case sfx_posit1:
case sfx_posit2:
case sfx_posit3:
S_StartSound(actor, sfx_posit1 + M_Random() % 3);
break;
case sfx_bgsit1:
case sfx_bgsit2:
S_StartSound(actor, sfx_bgsit1 + M_Random() % 2);
break;
default:
S_StartSound(((actor->mbf21flags & (MF_MBF21_BOSS | MF_MBF21_FULLVOLSOUNDS)) ? NULL : actor), actor->info->seesound);
break;
}
// [crispy] make seesounds uninterruptible
S_UnlinkSound(actor);
}
P_SetMobjState(actor, actor->info->seestate);
}
//
// A_FaceTarget
//
void A_FaceTarget(mobj_t *actor, player_t *player, pspdef_t *psp)
{
const mobj_t *target = actor->target;
if (!target)
return;
actor->flags &= ~MF_AMBUSH;
actor->angle = R_PointToAngle2(actor->x, actor->y, target->x, target->y);
if (target->flags & MF_FUZZ)
actor->angle += (M_SubRandom() << 21);
}
//
// A_Chase
// Actor has a melee attack,
// so it tries to close as fast as possible
//
void A_Chase(mobj_t *actor, player_t *player, pspdef_t *psp)
{
mobj_t *target = actor->target;
const mobjinfo_t *info = actor->info;
if (actor->reactiontime)
actor->reactiontime--;
// modify target threshold
if (actor->threshold)
{
if (!target || target->health <= 0)
actor->threshold = 0;
else
actor->threshold--;
}
// turn towards movement direction if not there yet
if (actor->movedir < 8)
{
const int delta = (actor->angle &= (7u << 29)) - (actor->movedir << 29);
if (delta > 0)
actor->angle -= ANG90 / 2;
else if (delta < 0)
actor->angle += ANG90 / 2;
}
if (!target || !(target->flags & MF_SHOOTABLE))
{
// look for a new target
if (!P_LookForPlayer(actor, true))
P_SetMobjState(actor, info->spawnstate); // no new target
return;
}
// do not attack twice in a row
if (actor->flags & MF_JUSTATTACKED)
{
actor->flags &= ~MF_JUSTATTACKED;
if (gameskill != sk_nightmare && !fastparm)
P_NewChaseDir(actor);
return;
}
// check for melee attack
if (info->meleestate != S_NULL && P_CheckMeleeRange(actor))
{
if (info->attacksound)
S_StartSound(actor, info->attacksound);
P_SetMobjState(actor, info->meleestate);
// killough 08/98: remember an attack
if (info->missilestate == S_NULL)
actor->flags |= MF_JUSTHIT;
return;
}
// check for missile attack
if (info->missilestate != S_NULL)
if (!(gameskill < sk_nightmare && !fastparm && actor->movecount))
if (P_CheckMissileRange(actor))
{
P_SetMobjState(actor, info->missilestate);
actor->flags |= MF_JUSTATTACKED;
return;
}