-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathg_ai.cpp
1540 lines (1271 loc) · 45.6 KB
/
g_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
// Copyright (c) ZeniMax Media Inc.
// Licensed under the GNU General Public License 2.0.
// g_ai.c
#include "g_local.h"
bool FindTarget(gentity_t *self);
bool ai_checkattack(gentity_t *self, float dist);
bool enemy_vis;
bool enemy_infront;
float enemy_yaw;
constexpr float MAX_SIDESTEP = 8.0f;
//============================================================================
/*
=================
AI_GetSightClient
For a given monster, check active players to see
who we can see. We don't care who we see, as long
as it's something we can shoot.
=================
*/
gentity_t *AI_GetSightClient(gentity_t *self) {
if (level.intermission_time)
return nullptr;
gentity_t **visible_players = (gentity_t **)alloca(sizeof(gentity_t *) * game.maxclients);
size_t num_visible = 0;
for (auto player : active_clients()) {
if (player->health <= 0 || player->deadflag || !player->solid)
continue;
else if (player->flags & (FL_NOTARGET | FL_DISGUISED))
continue;
// if we're touching them, allow to pass through
if (!boxes_intersect(self->absmin, self->absmax, player->absmin, player->absmax)) {
if ((!(self->monsterinfo.aiflags & AI_THIRD_EYE) && !infront(self, player)) || !visible(self, player))
continue;
}
visible_players[num_visible++] = player; // got one
}
if (!num_visible)
return nullptr;
return visible_players[irandom(num_visible)];
}
//============================================================================
/*
=============
ai_move
Move the specified distance at current facing.
This replaces the QC functions: ai_forward, ai_back, ai_pain, and ai_painforward
==============
*/
void ai_move(gentity_t *self, float dist) {
M_walkmove(self, self->s.angles[YAW], dist);
}
/*
=============
ai_stand
Used for standing around and looking for players
Distance is for slight position adjustments needed by the animations
==============
*/
void ai_stand(gentity_t *self, float dist) {
vec3_t v;
bool retval;
if (dist || (self->monsterinfo.aiflags & AI_ALTERNATE_FLY))
M_walkmove(self, self->s.angles[YAW], dist);
if (self->monsterinfo.aiflags & AI_STAND_GROUND) {
// [Paril-KEX] check if we've been pushed out of our point_combat
if (!(self->monsterinfo.aiflags & AI_TEMP_STAND_GROUND) &&
self->movetarget && self->movetarget->classname && !strcmp(self->movetarget->classname, "point_combat")) {
if (!boxes_intersect(self->absmin, self->absmax, self->movetarget->absmin, self->movetarget->absmax)) {
self->monsterinfo.aiflags &= ~AI_STAND_GROUND;
self->monsterinfo.aiflags |= AI_COMBAT_POINT;
self->goalentity = self->movetarget;
self->monsterinfo.run(self);
return;
}
}
if (self->enemy && !(self->enemy->classname && !strcmp(self->enemy->classname, "player_noise"))) {
v = self->enemy->s.origin - self->s.origin;
self->ideal_yaw = vectoyaw(v);
if (!FacingIdeal(self) && (self->monsterinfo.aiflags & AI_TEMP_STAND_GROUND)) {
self->monsterinfo.aiflags &= ~(AI_STAND_GROUND | AI_TEMP_STAND_GROUND);
self->monsterinfo.run(self);
}
if (!(self->monsterinfo.aiflags & AI_MANUAL_STEERING))
M_ChangeYaw(self);
// find out if we're going to be shooting
retval = ai_checkattack(self, 0);
// record sightings of player
if ((self->enemy) && (self->enemy->inuse)) {
if (visible(self, self->enemy)) {
self->monsterinfo.aiflags &= ~AI_LOST_SIGHT;
self->monsterinfo.last_sighting = self->monsterinfo.saved_goal = self->enemy->s.origin;
self->monsterinfo.blind_fire_target = self->monsterinfo.last_sighting + (self->enemy->velocity * -0.1f);
self->monsterinfo.trail_time = level.time;
self->monsterinfo.blind_fire_delay = 0_ms;
} else {
if (FindTarget(self))
return;
self->monsterinfo.aiflags |= AI_LOST_SIGHT;
}
// Paril: fixes rare cases of a stand ground monster being stuck
// aiming at a sound target that they can still see
if ((self->monsterinfo.aiflags & AI_SOUND_TARGET) && !retval) {
if (FindTarget(self))
return;
}
}
// check retval to make sure we're not blindfiring
else if (!retval) {
FindTarget(self);
return;
}
} else
FindTarget(self);
return;
}
// Paril: this fixes a bug somewhere else that sometimes causes
// a monster to be given an enemy without ever calling HuntTarget.
if (self->enemy && !(self->monsterinfo.aiflags & AI_SOUND_TARGET)) {
HuntTarget(self);
return;
}
if (FindTarget(self))
return;
if (level.time > self->monsterinfo.pausetime) {
self->monsterinfo.walk(self);
return;
}
if (!(self->spawnflags & SPAWNFLAG_MONSTER_AMBUSH) && (self->monsterinfo.idle) &&
(level.time > self->monsterinfo.idle_time)) {
if (self->monsterinfo.idle_time) {
self->monsterinfo.idle(self);
self->monsterinfo.idle_time = level.time + random_time(15_sec, 30_sec);
} else {
self->monsterinfo.idle_time = level.time + random_time(15_sec);
}
}
}
/*
=============
ai_walk
The monster is walking it's beat
=============
*/
void ai_walk(gentity_t *self, float dist) {
gentity_t *temp_goal = nullptr;
if (!self->goalentity && (self->monsterinfo.aiflags & AI_GOOD_GUY)) {
vec3_t fwd;
AngleVectors(self->s.angles, fwd, nullptr, nullptr);
temp_goal = G_Spawn();
temp_goal->s.origin = self->s.origin + fwd * 64;
self->goalentity = temp_goal;
}
M_MoveToGoal(self, dist);
if (temp_goal) {
G_FreeEntity(temp_goal);
self->goalentity = nullptr;
}
// check for noticing a player
if (FindTarget(self))
return;
if ((self->monsterinfo.search) && (level.time > self->monsterinfo.idle_time)) {
if (self->monsterinfo.idle_time) {
self->monsterinfo.search(self);
self->monsterinfo.idle_time = level.time + random_time(15_sec, 30_sec);
} else {
self->monsterinfo.idle_time = level.time + random_time(15_sec);
}
}
}
/*
=============
ai_charge
Turns towards target and advances
Use this call with a distance of 0 to replace ai_face
==============
*/
void ai_charge(gentity_t *self, float dist) {
vec3_t v;
float ofs;
// PMM - made AI_MANUAL_STEERING affect things differently here .. they turn, but
// don't set the ideal_yaw
// This is put in there so monsters won't move towards the origin after killing
// a tesla. This could be problematic, so keep an eye on it.
if (!self->enemy || !self->enemy->inuse)
return;
// save blindfire target
if (visible(self, self->enemy))
self->monsterinfo.blind_fire_target = self->enemy->s.origin + (self->enemy->velocity * -0.1f);
if (!(self->monsterinfo.aiflags & AI_MANUAL_STEERING)) {
v = self->enemy->s.origin - self->s.origin;
self->ideal_yaw = vectoyaw(v);
}
M_ChangeYaw(self);
if (dist || (self->monsterinfo.aiflags & AI_ALTERNATE_FLY)) {
if (self->monsterinfo.aiflags & AI_CHARGING) {
M_MoveToGoal(self, dist);
return;
}
// circle strafe support
if (self->monsterinfo.attack_state == AS_SLIDING) {
// if we're fighting a tesla, NEVER circle strafe
if ((self->enemy) && (self->enemy->classname) && (!strcmp(self->enemy->classname, "tesla_mine")))
ofs = 0;
else if (self->monsterinfo.lefty)
ofs = 90;
else
ofs = -90;
dist *= self->monsterinfo.active_move->sidestep_scale;
if (M_walkmove(self, self->ideal_yaw + ofs, dist))
return;
self->monsterinfo.lefty = !self->monsterinfo.lefty;
M_walkmove(self, self->ideal_yaw - ofs, dist);
} else
M_walkmove(self, self->s.angles[YAW], dist);
}
// [Paril-KEX] if our enemy is literally right next to us, give
// us more rotational speed so we don't get circled
if (range_to(self, self->enemy) <= RANGE_MELEE * 2.5f)
M_ChangeYaw(self);
}
/*
=============
ai_turn
don't move, but turn towards ideal_yaw
Distance is for slight position adjustments needed by the animations
=============
*/
void ai_turn(gentity_t *self, float dist) {
if (dist || (self->monsterinfo.aiflags & AI_ALTERNATE_FLY))
M_walkmove(self, self->s.angles[YAW], dist);
if (FindTarget(self))
return;
if (!(self->monsterinfo.aiflags & AI_MANUAL_STEERING))
M_ChangeYaw(self);
}
/*
.enemy
Will be world if not currently angry at anyone.
.movetarget
The next path spot to walk toward. If .enemy, ignore .movetarget.
When an enemy is killed, the monster will try to return to it's path.
.hunt_time
Set to time + something when the player is in sight, but movement straight for
him is blocked. This causes the monster to use wall following code for
movement direction instead of sighting on the player.
.ideal_yaw
A yaw angle of the intended direction, which will be turned towards at up
to 45 deg / state. If the enemy is in view and hunt_time is not active,
this will be the exact line towards the enemy.
.pausetime
A monster will leave it's stand state and head towards it's .movetarget when
time > .pausetime.
walkmove(angle, speed) primitive is all or nothing
*/
/*
=============
range_to
returns the distance of an entity relative to self.
in general, the results determine how an AI reacts:
melee melee range, will become hostile even if back is turned
near visibility and infront, or visibility and show hostile
mid infront and show hostile
> mid only triggered by damage
=============
*/
float range_to(gentity_t *self, gentity_t *other) {
return distance_between_boxes(self->absmin, self->absmax, other->absmin, other->absmax);
}
/*
=============
visible
returns 1 if the entity is visible to self, even if not infront ()
=============
*/
bool visible(gentity_t *self, gentity_t *other, bool through_glass) {
// never visible
if (other->flags & FL_NOVISIBLE)
return false;
// [Paril-KEX] bit of a hack, but we'll tweak monster-player visibility
// if they have the invisibility powerup.
if (other->client) {
// always visible in rtest
if (self->hackflags & HACKFLAG_ATTACK_PLAYER)
return self->inuse;
// fix intermission
if (!other->solid)
return false;
if (ClientIsPredator(other->client) || other->client->pu_time_invisibility > level.time) {
// can't see us at all after this time
if (other->client->invisibility_fade_time <= level.time)
return false;
// otherwise, throw in some randomness
if (frandom() > other->s.alpha)
return false;
}
}
vec3_t spot1;
vec3_t spot2;
trace_t trace;
spot1 = self->s.origin;
spot1[2] += self->viewheight;
spot2 = other->s.origin;
spot2[2] += other->viewheight;
contents_t mask = MASK_OPAQUE;
if (!through_glass)
mask |= CONTENTS_WINDOW;
trace = gi.traceline(spot1, spot2, self, mask);
return trace.fraction == 1.0f || trace.ent == other; // PGM
}
/*
=============
infront
returns 1 if the entity is in front (in sight) of self
=============
*/
bool infront(gentity_t *self, gentity_t *other) {
vec3_t vec;
float dot;
vec3_t forward;
AngleVectors(self->s.angles, forward, nullptr, nullptr);
vec = other->s.origin - self->s.origin;
vec.normalize();
dot = vec.dot(forward);
// [Paril-KEX] if we're an ambush monster, reduce our cone of
// vision to not ruin surprises, unless we already had an enemy.
if (self->spawnflags.has(SPAWNFLAG_MONSTER_AMBUSH) && !self->monsterinfo.trail_time && !self->enemy)
return dot > 0.15f;
return dot > -0.30f;
}
//============================================================================
void HuntTarget(gentity_t *self, bool animate_state) {
vec3_t vec;
self->goalentity = self->enemy;
if (animate_state) {
if (self->monsterinfo.aiflags & AI_STAND_GROUND)
self->monsterinfo.stand(self);
else
self->monsterinfo.run(self);
}
vec = self->enemy->s.origin - self->s.origin;
self->ideal_yaw = vectoyaw(vec);
}
void FoundTarget(gentity_t *self) {
// let other monsters see this monster for a while
if (self->enemy->client) {
if (self->enemy->flags & FL_DISGUISED)
self->enemy->flags &= ~FL_DISGUISED;
self->enemy->client->sight_entity = self;
self->enemy->client->sight_entity_time = level.time;
self->enemy->show_hostile = level.time + 1_sec; // wake up other monsters
}
// [Paril-KEX] the first time we spot something, give us a bit of a grace
// period on firing
if (!self->monsterinfo.trail_time)
self->monsterinfo.attack_finished = level.time + 600_ms;
// give easy/medium a little more reaction time
self->monsterinfo.attack_finished += skill->integer == 0 ? 400_ms : skill->integer == 1 ? 200_ms : 0_ms;
self->monsterinfo.last_sighting = self->monsterinfo.saved_goal = self->enemy->s.origin;
self->monsterinfo.trail_time = level.time;
self->monsterinfo.blind_fire_target = self->monsterinfo.last_sighting + (self->enemy->velocity * -0.1f);
self->monsterinfo.blind_fire_delay = 0_ms;
// [Paril-KEX] for alternate fly, pick a new position immediately
self->monsterinfo.fly_position_time = 0_ms;
self->monsterinfo.aiflags &= ~AI_THIRD_EYE;
// Paril: if we're heading to a combat point/path corner, don't
// hunt the new target yet.
if (self->monsterinfo.aiflags & AI_COMBAT_POINT)
return;
if (!self->combattarget) {
HuntTarget(self);
return;
}
self->goalentity = self->movetarget = G_PickTarget(self->combattarget);
if (!self->movetarget) {
self->goalentity = self->movetarget = self->enemy;
HuntTarget(self);
gi.Com_PrintFmt("{}: combattarget {} not found\n", *self, self->combattarget);
return;
}
// clear out our combattarget, these are a one shot deal
self->combattarget = nullptr;
self->monsterinfo.aiflags |= AI_COMBAT_POINT;
// clear the targetname, that point is ours!
// [Paril-KEX] not any more, we can re-use them
//self->movetarget->targetname = nullptr;
self->monsterinfo.pausetime = 0_ms;
// run for it
self->monsterinfo.run(self);
}
// [Paril-KEX] monsters that were alerted by players will
// be temporarily stored on player entities, so we can
// check them & get mad at them even around corners
static gentity_t *AI_GetMonsterAlertedByPlayers(gentity_t *self) {
for (auto player : active_clients()) {
// dead
if (player->health <= 0 || player->deadflag || !player->solid)
continue;
// we didn't alert any other monster, or it wasn't recently
if (!player->client->sight_entity || !(player->client->sight_entity_time >= (level.time - FRAME_TIME_S)))
continue;
// if we can't see the monster, don't bother
if (!visible(self, player->client->sight_entity))
continue;
// probably good
return player->client->sight_entity;
}
return nullptr;
}
// [Paril-KEX] per-player sounds
static gentity_t *AI_GetSoundClient(gentity_t *self, bool direct) {
gentity_t *best_sound = nullptr;
float best_distance = std::numeric_limits<float>::max();
for (auto player : active_clients()) {
// dead
if (player->health <= 0 || player->deadflag || !player->solid)
continue;
gentity_t *sound = direct ? player->client->sound_entity : player->client->sound2_entity;
if (!sound)
continue;
// too late
gtime_t &time = direct ? player->client->sound_entity_time : player->client->sound2_entity_time;
if (!(time >= (level.time - FRAME_TIME_S)))
continue;
// prefer the closest one we heard
float dist = (self->s.origin - sound->s.origin).length();
if (!best_sound || dist < best_distance) {
best_distance = dist;
best_sound = sound;
}
}
return best_sound;
}
bool G_MonsterSourceVisible(gentity_t *self, gentity_t *client) {
// this is where we would check invisibility
float r = range_to(self, client);
if (r > RANGE_MID)
return false;
// Paril: revised so that monsters can be woken up
// by players 'seen' and attacked at by other monsters
// if they are close enough. they don't have to be visible.
bool is_visible =
((r <= RANGE_NEAR && client->show_hostile >= level.time && !(self->spawnflags & SPAWNFLAG_MONSTER_AMBUSH)) ||
(visible(self, client) && (r <= RANGE_MELEE || (self->monsterinfo.aiflags & AI_THIRD_EYE) || infront(self, client))));
return is_visible;
}
/*
===========
FindTarget
Self is currently not attacking anything, so try to find a target
Returns TRUE if an enemy was sighted
When a player fires a missile, the point of impact becomes a fakeplayer so
that monsters that see the impact will respond as if they had seen the
player.
To avoid spending too much time, only a single client (or fakeclient) is
checked each frame. This means multi player games will have slightly
slower noticing monsters.
============
*/
bool FindTarget(gentity_t *self) {
gentity_t *client = nullptr;
bool heardit;
bool ignore_sight_sound = false;
// [Paril-KEX] if we're in a level transition, don't worry about enemies
if (globals.server_flags & SERVER_FLAG_LOADING)
return false;
// N64 cutscene behavior
if (self->hackflags & HACKFLAG_END_CUTSCENE)
return false;
if (self->monsterinfo.aiflags & AI_GOOD_GUY) {
if (self->goalentity && self->goalentity->inuse && self->goalentity->classname) {
if (strcmp(self->goalentity->classname, "target_actor") == 0)
return false;
}
// FIXME look for monsters?
return false;
}
// if we're going to a combat point, just proceed
if (self->monsterinfo.aiflags & AI_COMBAT_POINT)
return false;
// if the first spawnflag bit is set, the monster will only wake up on
// really seeing the player, not another monster getting angry or hearing
// something
// revised behavior so they will wake up if they "see" a player make a noise
// but not weapon impact/explosion noises
heardit = false;
// Paril: revised so that monsters will first try to consider
// the current sight client immediately if they can see it.
// this fixes them dancing in front of you if you fire every frame.
if ((client = AI_GetSightClient(self))) {
if (client == self->enemy) {
return false;
}
}
// check indirect sources
if (!client) {
// check monsters that were alerted by players; we can only be alerted if we
// can see them
if (!(self->spawnflags & SPAWNFLAG_MONSTER_AMBUSH) && (client = AI_GetMonsterAlertedByPlayers(self))) {
// KEX_FIXME: when does this happen?
// [Paril-KEX] adjusted to clear the client
// so we can try other things
if (client->enemy == self->enemy ||
!G_MonsterSourceVisible(self, client))
client = nullptr;
}
if (client == nullptr) {
if (level.disguise_violation_time > level.time) {
client = level.disguise_violator;
} else if ((client = AI_GetSoundClient(self, true))) {
heardit = true;
} else if (!(self->enemy) && !(self->spawnflags & SPAWNFLAG_MONSTER_AMBUSH) &&
(client = AI_GetSoundClient(self, false))) {
heardit = true;
}
}
}
if (!client)
return false; // no clients to get mad at
// if the entity went away, forget it
if (!client->inuse)
return false;
if (client == self->enemy) {
bool skip_found = true;
// [Paril-KEX] slight special behavior if we are currently going to a sound
// and we hear a new one; because player noises are re-used, this can leave
// us with the "same" enemy even though it's a different noise.
if (heardit && (self->monsterinfo.aiflags & AI_SOUND_TARGET)) {
vec3_t temp = client->s.origin - self->s.origin;
self->ideal_yaw = vectoyaw(temp);
if (!FacingIdeal(self))
skip_found = false;
else if (!G_CloseEnough(self, client, 8.f))
skip_found = false;
if (!skip_found && (self->monsterinfo.aiflags & AI_TEMP_STAND_GROUND)) {
self->monsterinfo.aiflags &= ~(AI_STAND_GROUND | AI_TEMP_STAND_GROUND);
}
}
if (skip_found)
return true; // JDC false;
}
// hintpath coop fix
if ((self->monsterinfo.aiflags & AI_HINT_PATH) && InCoopStyle())
heardit = false;
if (client->svflags & SVF_MONSTER) {
if (!client->enemy)
return false;
if (client->enemy->flags & FL_NOTARGET)
return false;
} else if (heardit) {
// pgm - a little more paranoia won't hurt....
if ((client->owner) && (client->owner->flags & FL_NOTARGET))
return false;
} else if (!client->client)
return false;
if (!heardit) {
// this is where we would check invisibility
float r = range_to(self, client);
if (r > RANGE_MID)
return false;
// Paril: revised so that monsters can be woken up
// by players 'seen' and attacked at by other monsters
// if they are close enough. they don't have to be visible.
bool is_visible =
((r <= RANGE_NEAR && client->show_hostile >= level.time && !(self->spawnflags & SPAWNFLAG_MONSTER_AMBUSH)) ||
(visible(self, client) && (r <= RANGE_MELEE || (self->monsterinfo.aiflags & AI_THIRD_EYE) || infront(self, client))));
if (!is_visible)
return false;
self->enemy = client;
if (strcmp(self->enemy->classname, "player_noise") != 0) {
self->monsterinfo.aiflags &= ~AI_SOUND_TARGET;
if (!self->enemy->client) {
self->enemy = self->enemy->enemy;
if (!self->enemy->client) {
self->enemy = nullptr;
return false;
}
}
}
if (self->enemy->client && (ClientIsPredator(self->enemy->client) || self->enemy->client->pu_time_invisibility > level.time) && self->enemy->client->invisibility_fade_time <= level.time) {
self->enemy = nullptr;
return false;
}
if (self->monsterinfo.close_sight_tripped)
ignore_sight_sound = true;
else
self->monsterinfo.close_sight_tripped = true;
} else // heardit
{
vec3_t temp;
if (self->spawnflags.has(SPAWNFLAG_MONSTER_AMBUSH)) {
if (!visible(self, client))
return false;
} else {
if (!gi.inPHS(self->s.origin, client->s.origin, true))
return false;
}
temp = client->s.origin - self->s.origin;
if (temp.length() > 1000) // too far to hear
return false;
// check area portals - if they are different and not connected then we can't hear it
if (client->areanum != self->areanum)
if (!gi.AreasConnected(self->areanum, client->areanum))
return false;
self->ideal_yaw = vectoyaw(temp);
if (!(self->monsterinfo.aiflags & AI_MANUAL_STEERING))
M_ChangeYaw(self);
// hunt the sound for a bit; hopefully find the real player
self->monsterinfo.aiflags |= AI_SOUND_TARGET;
self->enemy = client;
}
//
// got one
//
// if we got an enemy, we need to bail out of hint paths, so take over here
if (self->monsterinfo.aiflags & AI_HINT_PATH)
hintpath_stop(self); // this calls foundtarget for us
else
FoundTarget(self);
if (!(self->monsterinfo.aiflags & AI_SOUND_TARGET) && (self->monsterinfo.sight) &&
// Paril: adjust to prevent monsters getting stuck in sight loops
!ignore_sight_sound)
self->monsterinfo.sight(self, self->enemy);
return true;
}
//=============================================================================
/*
============
FacingIdeal
============
*/
bool FacingIdeal(gentity_t *self) {
float delta = anglemod(self->s.angles[YAW] - self->ideal_yaw);
if (self->monsterinfo.aiflags & AI_PATHING)
return !(delta > 5 && delta < 355);
return !(delta > 45 && delta < 315);
}
//=============================================================================
// [Paril-KEX] split this out so we can use it for the other bosses
bool M_CheckAttack_Base(gentity_t *self, float stand_ground_chance, float melee_chance, float near_chance, float mid_chance, float far_chance, float strafe_scalar) {
vec3_t spot1, spot2;
float chance;
trace_t tr;
if (self->enemy->flags & FL_NOVISIBLE)
return false;
if (self->enemy->health > 0) {
if (self->enemy->client) {
if (ClientIsPredator(self->enemy->client) || self->enemy->client->pu_time_invisibility > level.time) {
// can't see us at all after this time
if (self->enemy->client->invisibility_fade_time <= level.time)
return false;
}
}
spot1 = self->s.origin;
spot1[2] += self->viewheight;
// see if any entities are in the way of the shot
if (!self->enemy->client || self->enemy->solid) {
spot2 = self->enemy->s.origin;
spot2[2] += self->enemy->viewheight;
tr = gi.traceline(spot1, spot2, self,
MASK_SOLID | CONTENTS_MONSTER | CONTENTS_PLAYER | CONTENTS_SLIME | CONTENTS_LAVA);
} else {
tr.ent = world;
tr.fraction = 0;
}
// do we have a clear shot?
if (!(self->hackflags & HACKFLAG_ATTACK_PLAYER) && tr.ent != self->enemy && !(tr.ent->svflags & SVF_PLAYER)) {
// we want them to go ahead and shoot at info_notnulls if they can.
if (self->enemy->solid != SOLID_NOT || tr.fraction < 1.0f) {
// if we can't see our target, and we're not blocked by a monster, go into blind fire if available
// Paril - *and* we have at least seen them once
if (!(tr.ent->svflags & SVF_MONSTER) && !visible(self, self->enemy) && self->monsterinfo.had_visibility) {
if (self->monsterinfo.blindfire && (self->monsterinfo.blind_fire_delay <= 20_sec)) {
if (level.time < self->monsterinfo.attack_finished) {
return false;
}
if (level.time < (self->monsterinfo.trail_time + self->monsterinfo.blind_fire_delay)) {
// wait for our time
return false;
} else {
// make sure we're not going to shoot a monster
tr = gi.traceline(spot1, self->monsterinfo.blind_fire_target, self,
CONTENTS_MONSTER);
if (tr.allsolid || tr.startsolid || ((tr.fraction < 1.0f) && (tr.ent != self->enemy)))
return false;
self->monsterinfo.attack_state = AS_BLIND;
return true;
}
}
}
return false;
}
}
}
float enemy_range = range_to(self, self->enemy);
// melee attack
if (enemy_range <= RANGE_MELEE) {
if (self->monsterinfo.melee && self->monsterinfo.melee_debounce_time <= level.time)
self->monsterinfo.attack_state = AS_MELEE;
else
self->monsterinfo.attack_state = AS_MISSILE;
return true;
}
// if we were in melee just before this but we're too far away, get out of melee state now
if (self->monsterinfo.attack_state == AS_MELEE && self->monsterinfo.melee_debounce_time > level.time)
self->monsterinfo.attack_state = AS_MISSILE;
// missile attack
if (!self->monsterinfo.attack) {
// fix for melee only monsters & strafing
self->monsterinfo.attack_state = AS_STRAIGHT;
return false;
}
if (level.time < self->monsterinfo.attack_finished)
return false;
if (self->monsterinfo.aiflags & AI_STAND_GROUND) {
chance = stand_ground_chance;
} else if (enemy_range <= RANGE_MELEE) {
chance = melee_chance;
} else if (enemy_range <= RANGE_NEAR) {
chance = near_chance;
} else if (enemy_range <= RANGE_MID) {
chance = mid_chance;
} else {
chance = far_chance;
}
// go ahead and shoot every time if it's a info_notnull
if ((!self->enemy->client && self->enemy->solid == SOLID_NOT) || (frandom() < chance)) {
self->monsterinfo.attack_state = AS_MISSILE;
self->monsterinfo.attack_finished = level.time;
return true;
}
// daedalus should strafe more .. this can be done here or in a customized
// check_attack code for the hover.
if (self->flags & FL_FLY) {
if (self->monsterinfo.strafe_check_time <= level.time) {
// originally, just 0.3
float strafe_chance;
if (!(strcmp(self->classname, "monster_daedalus")))
strafe_chance = 0.8f;
else
strafe_chance = 0.6f;
// if enemy is tesla, never strafe
if ((self->enemy) && (self->enemy->classname) && (!strcmp(self->enemy->classname, "tesla_mine")))
strafe_chance = 0;
else
strafe_chance *= strafe_scalar;
if (strafe_chance) {
monster_attack_state_t new_state = AS_STRAIGHT;
if (frandom() < strafe_chance)
new_state = AS_SLIDING;
if (new_state != self->monsterinfo.attack_state) {
self->monsterinfo.strafe_check_time = level.time + random_time(1_sec, 3_sec);
self->monsterinfo.attack_state = new_state;
}
}
}
}
// do we want the monsters strafing?
// [Paril-KEX] no, we don't
// [Paril-KEX] if we're pathing, don't immediately reset us to
// straight; this allows us to turn to fire and not jerk back and
// forth.
else if (!(self->monsterinfo.aiflags & AI_PATHING))
self->monsterinfo.attack_state = AS_STRAIGHT;
return false;
}
MONSTERINFO_CHECKATTACK(M_CheckAttack) (gentity_t *self) -> bool {
return M_CheckAttack_Base(self, 0.7f, 0.4f, 0.25f, 0.06f, 0.f, 1.0f);
}
/*
=============
ai_run_melee
Turn and close until within an angle to launch a melee attack
=============
*/
static void ai_run_melee(gentity_t *self) {
self->ideal_yaw = enemy_yaw;
if (!(self->monsterinfo.aiflags & AI_MANUAL_STEERING))
M_ChangeYaw(self);
if (FacingIdeal(self)) {
self->monsterinfo.melee(self);
self->monsterinfo.attack_state = AS_STRAIGHT;
}
}
/*
=============
ai_run_missile
Turn in place until within an angle to launch a missile attack
=============
*/
static void ai_run_missile(gentity_t *self) {
self->ideal_yaw = enemy_yaw;
if (!(self->monsterinfo.aiflags & AI_MANUAL_STEERING))
M_ChangeYaw(self);
if (FacingIdeal(self)) {
if (self->monsterinfo.attack) {
self->monsterinfo.attack(self);
self->monsterinfo.attack_finished = level.time + random_time(1.0_sec, 2.0_sec);
}
if ((self->monsterinfo.attack_state == AS_MISSILE) || (self->monsterinfo.attack_state == AS_BLIND))
self->monsterinfo.attack_state = AS_STRAIGHT;
}
};
/*
=============
ai_run_slide
Strafe sideways, but stay at aproximately the same range
=============
*/
static void ai_run_slide(gentity_t *self, float distance) {
float ofs;
float angle;