-
Notifications
You must be signed in to change notification settings - Fork 3
/
p_enemy.c
864 lines (696 loc) · 15.2 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
/* P_enemy.c */
#include "doomdef.h"
#include "p_local.h"
/*
===============================================================================
ENEMY THINKING
enemies are allways spawned with targetplayer = -1, threshold = 0
Most monsters are spawned unaware of all players, but some can be made preaware
===============================================================================
*/
/*
================
=
= P_CheckMeleeRange
=
================
*/
boolean P_CheckMeleeRange (mobj_t *actor)
{
mobj_t *pl;
fixed_t dist;
if (! (actor->flags&MF_SEETARGET) )
return false;
if (!actor->target)
return false;
pl = actor->target;
dist = P_AproxDistance (pl->x-actor->x, pl->y-actor->y);
if (dist >= MELEERANGE)
return false;
return true;
}
/*
================
=
= P_CheckMissileRange
=
================
*/
boolean P_CheckMissileRange (mobj_t *actor)
{
fixed_t dist;
if (! (actor->flags & MF_SEETARGET) )
return false;
if (actor->flags & MF_JUSTHIT)
{ /* the target just hit the enemy, so fight back! */
actor->flags &= ~MF_JUSTHIT;
return true;
}
if (actor->reactiontime)
return false; /* don't attack yet */
dist = P_AproxDistance ( actor->x-actor->target->x, actor->y-actor->target->y) - 64*FRACUNIT;
if (!actor->info->meleestate)
dist -= 128*FRACUNIT; /* no melee attack, so fire more */
dist >>= 16;
if (actor->type == MT_SKULL)
dist >>= 1;
if (dist > 200)
dist = 200;
if (P_Random () < dist)
return false;
return true;
}
/*
================
=
= P_Move
=
= Move in the current direction
= returns false if the move is blocked
================
*/
fixed_t xspeed[8] = {FRACUNIT,47000,0,-47000,-FRACUNIT,-47000,0,47000};
fixed_t yspeed[8] = {0,47000,FRACUNIT,47000,0,-47000,-FRACUNIT,-47000};
extern line_t *blockline;
boolean P_Move (mobj_t *actor)
{
fixed_t tryx, tryy;
boolean good;
line_t *blkline;
if (actor->movedir == DI_NODIR)
return false;
tryx = actor->x + actor->info->speed*xspeed[actor->movedir];
tryy = actor->y + actor->info->speed*yspeed[actor->movedir];
if (!P_TryMove (actor, tryx, tryy) )
{ /* open any specials */
if (actor->flags & MF_FLOAT && floatok)
{ /* must adjust height */
if (actor->z < tmfloorz)
actor->z += FLOATSPEED;
else
actor->z -= FLOATSPEED;
actor->flags |= MF_INFLOAT;
return true;
}
blkline = (line_t *)DSPRead (&blockline);
if (!blkline || !blkline->special)
return false;
actor->movedir = DI_NODIR;
good = false;
/* if the special isn't a door that can be opened, return false */
if (P_UseSpecialLine (actor, blkline))
good = true;
return good;
}
else
actor->flags &= ~MF_INFLOAT;
if (! (actor->flags & MF_FLOAT) )
actor->z = actor->floorz;
return true;
}
/*
==================================
=
= TryWalk
=
= Attempts to move actoron 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.
=
==================================
*/
boolean P_TryWalk (mobj_t *actor)
{
if (!P_Move (actor))
return false;
actor->movecount = P_Random()&15;
return true;
}
/*
================
=
= P_NewChaseDir
=
================
*/
dirtype_t opposite[] =
{DI_WEST, DI_SOUTHWEST, DI_SOUTH, DI_SOUTHEAST, DI_EAST, DI_NORTHEAST,
DI_NORTH, DI_NORTHWEST, DI_NODIR};
dirtype_t diags[] = {DI_NORTHWEST,DI_NORTHEAST,DI_SOUTHWEST,DI_SOUTHEAST};
void P_NewChaseDir (mobj_t *actor)
{
fixed_t deltax,deltay;
dirtype_t d[3];
dirtype_t tdir, olddir, turnaround;
if (!actor->target)
I_Error ("P_NewChaseDir: called with no target");
olddir = actor->movedir;
turnaround=opposite[olddir];
deltax = actor->target->x - actor->x;
deltay = actor->target->y - actor->y;
if (deltax>10*FRACUNIT)
d[1]= DI_EAST;
else if (deltax<-10*FRACUNIT)
d[1]= DI_WEST;
else
d[1]=DI_NODIR;
if (deltay<-10*FRACUNIT)
d[2]= DI_SOUTH;
else if (deltay>10*FRACUNIT)
d[2]= DI_NORTH;
else
d[2]=DI_NODIR;
/* try direct route */
if (d[1] != DI_NODIR && d[2] != DI_NODIR)
{
actor->movedir = diags[((deltay<0)<<1)+(deltax>0)];
if (actor->movedir != turnaround && P_TryWalk(actor))
return;
}
/* try other directions */
if (P_Random() > 200 || abs(deltay)>abs(deltax))
{
tdir=d[1];
d[1]=d[2];
d[2]=tdir;
}
if (d[1]==turnaround)
d[1]=DI_NODIR;
if (d[2]==turnaround)
d[2]=DI_NODIR;
if (d[1]!=DI_NODIR)
{
actor->movedir = d[1];
if (P_TryWalk(actor))
return; /*either moved forward or attacked*/
}
if (d[2]!=DI_NODIR)
{
actor->movedir =d[2];
if (P_TryWalk(actor))
return;
}
/* there is no direct path to the player, so pick another direction */
if (olddir!=DI_NODIR)
{
actor->movedir =olddir;
if (P_TryWalk(actor))
return;
}
if (P_Random()&1) /*randomly determine direction of search*/
{
for (tdir=DI_EAST ; tdir<=DI_SOUTHEAST ; tdir++)
{
if (tdir!=turnaround)
{
actor->movedir =tdir;
if ( P_TryWalk(actor) )
return;
}
}
}
else
{
for (tdir=DI_SOUTHEAST ; (int)tdir >= (int)DI_EAST;tdir--)
{
if (tdir!=turnaround)
{
actor->movedir =tdir;
if ( P_TryWalk(actor) )
return;
}
}
}
if (turnaround != DI_NODIR)
{
actor->movedir =turnaround;
if ( P_TryWalk(actor) )
return;
}
actor->movedir = DI_NODIR; /* can't move */
}
/*
================
=
= P_LookForPlayers
=
= If allaround is false, only look 180 degrees in front
= returns true if a player is targeted
================
*/
boolean P_LookForPlayers (mobj_t *actor, boolean allaround)
{
angle_t an;
fixed_t dist;
mobj_t *mo;
if (! (actor->flags & MF_SEETARGET) )
{ /* pick another player as target if possible */
newtarget:
if (playeringame[1] && actor->target == players[0].mo)
actor->target = players[1].mo;
else
actor->target = players[0].mo;
return false;
}
mo = actor->target;
if (!mo || mo->health <= 0)
goto newtarget;
if (actor->subsector->sector->soundtarget == actor->target)
allaround = true; /* ambush guys will turn around on a shot */
if (!allaround)
{
an = R_PointToAngle2 (actor->x, actor->y,
mo->x, mo->y) - actor->angle;
if (an > ANG90 && an < ANG270)
{
dist = P_AproxDistance (mo->x - actor->x,
mo->y - actor->y);
/* if real close, react anyway */
if (dist > MELEERANGE)
return false; /* behind back */
}
}
actor->threshold = 60;
return true;
}
/*
===============================================================================
ACTION ROUTINES
===============================================================================
*/
/*
==============
=
= A_Look
=
= Stay in state until a player is sighted
=
==============
*/
void A_Look (mobj_t *actor)
{
/* mobj_t *targ;
*/
/* if current target is visible, start attacking */
if (!P_LookForPlayers (actor, false) )
return;
#if 0
/* if the sector has a living soundtarget, make that the new target */
actor->threshold = 0; /* any shot will wake up */
targ = actor->subsector->sector->soundtarget;
if (targ && (targ->flags & MF_SHOOTABLE) )
actor->target = targ;
return;
#endif
/* go into chase state */
if (actor->info->seesound)
{
int sound;
switch (actor->info->seesound)
{
case sfx_posit1:
case sfx_posit2:
case sfx_posit3:
sound = sfx_posit1+(P_Random()&1);
break;
case sfx_bgsit1:
case sfx_bgsit2:
sound = sfx_bgsit1+(P_Random()&1);
break;
default:
sound = actor->info->seesound;
break;
}
S_StartSound (actor, sound);
}
P_SetMobjState (actor, actor->info->seestate);
}
/*
==============
=
= A_Chase
=
= Actor has a melee attack, so it tries to close as fast as possible
=
==============
*/
void A_Chase (mobj_t *actor)
{
int delta;
if (actor->reactiontime)
actor->reactiontime--;
/* */
/* modify target threshold */
/* */
if (actor->threshold)
actor->threshold--;
/* */
/* turn towards movement direction if not there yet */
/* */
if (actor->movedir < 8)
{
actor->angle &= (7<<29);
delta = actor->angle - (actor->movedir << 29);
if (delta > 0)
actor->angle -= ANG90/2;
else if (delta < 0)
actor->angle += ANG90/2;
}
if (!actor->target || !(actor->target->flags&MF_SHOOTABLE))
{ /* look for a new target */
if (P_LookForPlayers(actor,true))
return; /* got a new target */
P_SetMobjState (actor, actor->info->spawnstate);
return;
}
/* */
/* don't attack twice in a row */
/* */
if (actor->flags & MF_JUSTATTACKED)
{
actor->flags &= ~MF_JUSTATTACKED;
P_NewChaseDir (actor);
return;
}
/* */
/* check for melee attack */
/* */
if (actor->info->meleestate && P_CheckMeleeRange (actor))
{
if (actor->info->attacksound)
S_StartSound (actor, actor->info->attacksound);
P_SetMobjState (actor, actor->info->meleestate);
return;
}
/* */
/* check for missile attack */
/* */
if ( (gameskill == sk_nightmare || !actor->movecount) && actor->info->missilestate
&& P_CheckMissileRange (actor))
{
P_SetMobjState (actor, actor->info->missilestate);
if (gameskill != sk_nightmare)
actor->flags |= MF_JUSTATTACKED;
return;
}
/* */
/* chase towards player */
/* */
if (--actor->movecount<0 || !P_Move (actor))
P_NewChaseDir (actor);
/* */
/* make active sound */
/* */
if (actor->info->activesound && P_Random () < 3)
S_StartSound (actor, actor->info->activesound);
}
/*============================================================================= */
/*
==============
=
= A_FaceTarget
=
==============
*/
void A_FaceTarget (mobj_t *actor)
{
if (!actor->target)
return;
actor->flags &= ~MF_AMBUSH;
actor->angle = R_PointToAngle2 (actor->x, actor->y
, actor->target->x, actor->target->y);
}
/*
==============
=
= A_PosAttack
=
==============
*/
void A_PosAttack (mobj_t *actor)
{
int angle, damage;
if (!actor->target)
return;
A_FaceTarget (actor);
angle = actor->angle;
S_StartSound (actor, sfx_pistol);
angle += (P_Random()-P_Random())<<20;
damage = ((P_Random()&7)+1)*3;
P_LineAttack (actor, angle, MISSILERANGE, MAXINT, damage);
}
void A_SPosAttack (mobj_t *actor)
{
int i;
int angle, bangle, damage;
if (!actor->target)
return;
S_StartSound (actor, sfx_shotgn);
A_FaceTarget (actor);
bangle = actor->angle;
for (i=0 ; i<3 ; i++)
{
angle = bangle + ((P_Random()-P_Random())<<20);
damage = ((P_Random()&7)+1)*3;
P_LineAttack (actor, angle, MISSILERANGE, MAXINT, damage);
}
}
void A_SpidRefire (mobj_t *actor)
{
/* keep firing unless target got out of sight */
A_FaceTarget (actor);
if (P_Random () < 10)
return;
if (!actor->target || actor->target->health <= 0 || !(actor->flags&MF_SEETARGET) )
P_SetMobjState (actor, actor->info->seestate);
}
/*
==============
=
= A_TroopAttack
=
==============
*/
void A_TroopAttack (mobj_t *actor)
{
int damage;
if (!actor->target)
return;
A_FaceTarget (actor);
if (P_CheckMeleeRange (actor))
{
S_StartSound (actor, sfx_claw);
damage = ((P_Random()&7)+1)*3;
P_DamageMobj (actor->target, actor, actor, damage);
return;
}
/* */
/* launch a missile */
/* */
P_SpawnMissile (actor, actor->target, MT_TROOPSHOT);
}
void A_SargAttack (mobj_t *actor)
{
int damage;
if (!actor->target)
return;
A_FaceTarget (actor);
damage = ((P_Random()&7)+1)*4;
P_LineAttack (actor, actor->angle, MELEERANGE, 0, damage);
}
void A_HeadAttack (mobj_t *actor)
{
int damage;
if (!actor->target)
return;
A_FaceTarget (actor);
if (P_CheckMeleeRange (actor))
{
damage = ((P_Random()&7)+1)*8;
P_DamageMobj (actor->target, actor, actor, damage);
return;
}
/* */
/* launch a missile */
/* */
P_SpawnMissile (actor, actor->target, MT_HEADSHOT);
}
void A_CyberAttack (mobj_t *actor)
{
if (!actor->target)
return;
A_FaceTarget (actor);
P_SpawnMissile (actor, actor->target, MT_ROCKET);
}
void A_BruisAttack (mobj_t *actor)
{
int damage;
if (!actor->target)
return;
if (P_CheckMeleeRange (actor))
{
S_StartSound (actor, sfx_claw);
damage = ((P_Random()&7)+1)*11;
P_DamageMobj (actor->target, actor, actor, damage);
return;
}
/* */
/* launch a missile */
/* */
P_SpawnMissile (actor, actor->target, MT_BRUISERSHOT);
}
/*
==================
=
= SkullAttack
=
= Fly at the player like a missile
=
==================
*/
#define SKULLSPEED (40*FRACUNIT)
void A_SkullAttack (mobj_t *actor)
{
mobj_t *dest;
angle_t an;
int dist;
if (!actor->target)
return;
dest = actor->target;
actor->flags |= MF_SKULLFLY;
S_StartSound (actor, actor->info->attacksound);
A_FaceTarget (actor);
an = actor->angle >> ANGLETOFINESHIFT;
actor->momx = FixedMul (SKULLSPEED, finecosine[an]);
actor->momy = FixedMul (SKULLSPEED, finesine[an]);
dist = P_AproxDistance (dest->x - actor->x, dest->y - actor->y);
dist = dist / SKULLSPEED;
if (dist < 1)
dist = 1;
actor->momz = (dest->z+(dest->height>>1) - actor->z) / dist;
}
void A_Scream (mobj_t *actor)
{
int sound;
switch (actor->info->deathsound)
{
case 0:
return;
case sfx_podth1:
case sfx_podth2:
case sfx_podth3:
sound = sfx_podth1 + (P_Random ()&1);
break;
case sfx_bgdth1:
case sfx_bgdth2:
sound = sfx_bgdth1 + (P_Random ()&1);
break;
default:
sound = actor->info->deathsound;
break;
}
S_StartSound (actor, sound);
}
void A_XScream (mobj_t *actor)
{
S_StartSound (actor, sfx_slop);
}
void A_Pain (mobj_t *actor)
{
if (actor->info->painsound)
S_StartSound (actor, actor->info->painsound);
}
void A_Fall (mobj_t *actor)
{
/* actor is on ground, it can be walked over */
actor->flags &= ~MF_SOLID;
}
/*
================
=
= A_Explode
=
================
*/
void A_Explode (mobj_t *thingy)
{
P_RadiusAttack ( thingy, thingy->target, 128 );
}
/*
================
=
= A_BossDeath
=
= Possibly trigger special effects
================
*/
void A_BossDeath (mobj_t *mo)
{
mobj_t *mo2;
line_t junk;
if (gamemap != 8)
return; /* bruisers apear on other levels */
/* */
/* scan the remaining thinkers to see if all bosses are dead */
/* */
/* FIXME */
for (mo2=mobjhead.next ; mo2 != &mobjhead ; mo2=mo2->next)
{
if (mo2 != mo && mo2->type == mo->type && mo2->health > 0)
return; /* other boss not dead */
}
/* */
/* victory! */
/* */
junk.tag = 666;
EV_DoFloor (&junk, lowerFloorToLowest);
}
void A_Hoof (mobj_t *mo)
{
S_StartSound (mo, sfx_hoof);
A_Chase (mo);
}
void A_Metal (mobj_t *mo)
{
S_StartSound (mo, sfx_metal);
A_Chase (mo);
}
/*============================================================================= */
/* a move in p_base.c crossed a special line */
void L_CrossSpecial (mobj_t *mo)
{
line_t *line;
line = (line_t *)(mo->extradata & ~1);
P_CrossSpecialLine (line, mo);
}
/* a move in p_base.c caused a missile to hit another thing or wall */
void L_MissileHit (mobj_t *mo)
{
int damage;
mobj_t *missilething;
missilething = (mobj_t *)mo->extradata;
if (missilething)
{
damage = ((P_Random()&7)+1)*mo->info->damage;
P_DamageMobj (missilething, mo, mo->target, damage);
}
P_ExplodeMissile (mo);
}
/* a move in p_base.c caused a flying skull to hit another thing or a wall */
void L_SkullBash (mobj_t *mo)
{
int damage;
mobj_t *skullthing;
skullthing = (mobj_t *)mo->extradata;
if (skullthing)
{
damage = ((P_Random()&7)+1)*mo->info->damage;
P_DamageMobj (skullthing, mo, mo, damage);
}
mo->flags &= ~MF_SKULLFLY;
mo->momx = mo->momy = mo->momz = 0;
P_SetMobjState (mo, mo->info->spawnstate);
}
/* */