forked from JACoders/OpenJK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNPC_behavior.c
1769 lines (1543 loc) · 51.7 KB
/
NPC_behavior.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
/*
===========================================================================
Copyright (C) 2000 - 2013, Raven Software, Inc.
Copyright (C) 2001 - 2013, Activision, Inc.
Copyright (C) 2013 - 2015, OpenJK contributors
This file is part of the OpenJK source code.
OpenJK is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program 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 this program; if not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/
//NPC_behavior.cpp
/*
FIXME - MCG:
These all need to make use of the snapshots. Write something that can look for only specific
things in a snapshot or just go through the snapshot every frame and save the info in case
we need it...
*/
#include "b_local.h"
#include "g_nav.h"
#include "icarus/Q3_Interface.h"
extern qboolean showBBoxes;
extern vec3_t NPCDEBUG_BLUE;
extern void G_Cube( vec3_t mins, vec3_t maxs, vec3_t color, float alpha );
extern void NPC_CheckGetNewWeapon( void );
extern qboolean PM_InKnockDown( playerState_t *ps );
extern void NPC_AimAdjust( int change );
extern qboolean NPC_SomeoneLookingAtMe(gentity_t *ent);
/*
void NPC_BSAdvanceFight (void)
Advance towards your captureGoal and shoot anyone you can along the way.
*/
void NPC_BSAdvanceFight (void)
{//FIXME: IMPLEMENT
//Head to Goal if I can
//Make sure we're still headed where we want to capture
if ( NPCS.NPCInfo->captureGoal )
{//FIXME: if no captureGoal, what do we do?
//VectorCopy( NPCInfo->captureGoal->r.currentOrigin, NPCInfo->tempGoal->r.currentOrigin );
//NPCInfo->goalEntity = NPCInfo->tempGoal;
NPC_SetMoveGoal( NPCS.NPC, NPCS.NPCInfo->captureGoal->r.currentOrigin, 16, qtrue, -1, NULL );
// NAV_ClearLastRoute(NPC);
NPCS.NPCInfo->goalTime = level.time + 100000;
}
// NPC_BSRun();
NPC_CheckEnemy(qtrue, qfalse, qtrue);
//FIXME: Need melee code
if( NPCS.NPC->enemy )
{//See if we can shoot him
vec3_t delta, forward;
vec3_t angleToEnemy;
vec3_t hitspot, muzzle, diff, enemy_org, enemy_head;
float distanceToEnemy;
qboolean attack_ok = qfalse;
qboolean dead_on = qfalse;
float attack_scale = 1.0;
float aim_off;
float max_aim_off = 64;
//Yaw to enemy
VectorMA(NPCS.NPC->enemy->r.absmin, 0.5, NPCS.NPC->enemy->r.maxs, enemy_org);
CalcEntitySpot( NPCS.NPC, SPOT_WEAPON, muzzle );
VectorSubtract (enemy_org, muzzle, delta);
vectoangles ( delta, angleToEnemy );
distanceToEnemy = VectorNormalize(delta);
if(!NPC_EnemyTooFar(NPCS.NPC->enemy, distanceToEnemy*distanceToEnemy, qtrue))
{
attack_ok = qtrue;
}
if(attack_ok)
{
NPC_UpdateShootAngles(angleToEnemy, qfalse, qtrue);
NPCS.NPCInfo->enemyLastVisibility = NPCS.enemyVisibility;
NPCS.enemyVisibility = NPC_CheckVisibility ( NPCS.NPC->enemy, CHECK_FOV);//CHECK_360|//CHECK_PVS|
if(NPCS.enemyVisibility == VIS_FOV)
{//He's in our FOV
attack_ok = qtrue;
CalcEntitySpot( NPCS.NPC->enemy, SPOT_HEAD, enemy_head);
if(attack_ok)
{
trace_t tr;
gentity_t *traceEnt;
//are we gonna hit him if we shoot at his center?
trap->Trace ( &tr, muzzle, NULL, NULL, enemy_org, NPCS.NPC->s.number, MASK_SHOT, qfalse, 0, 0 );
traceEnt = &g_entities[tr.entityNum];
if( traceEnt != NPCS.NPC->enemy &&
(!traceEnt || !traceEnt->client || !NPCS.NPC->client->enemyTeam || NPCS.NPC->client->enemyTeam != traceEnt->client->playerTeam) )
{//no, so shoot for the head
attack_scale *= 0.75;
trap->Trace ( &tr, muzzle, NULL, NULL, enemy_head, NPCS.NPC->s.number, MASK_SHOT, qfalse, 0, 0 );
traceEnt = &g_entities[tr.entityNum];
}
VectorCopy( tr.endpos, hitspot );
if( traceEnt == NPCS.NPC->enemy || (traceEnt->client && NPCS.NPC->client->enemyTeam && NPCS.NPC->client->enemyTeam == traceEnt->client->playerTeam) )
{
dead_on = qtrue;
}
else
{
attack_scale *= 0.5;
if(NPCS.NPC->client->playerTeam)
{
if(traceEnt && traceEnt->client && traceEnt->client->playerTeam)
{
if(NPCS.NPC->client->playerTeam == traceEnt->client->playerTeam)
{//Don't shoot our own team
attack_ok = qfalse;
}
}
}
}
}
if( attack_ok )
{
//ok, now adjust pitch aim
VectorSubtract (hitspot, muzzle, delta);
vectoangles ( delta, angleToEnemy );
NPCS.NPCInfo->desiredPitch = angleToEnemy[PITCH];
NPC_UpdateShootAngles(angleToEnemy, qtrue, qfalse);
if( !dead_on )
{//We're not going to hit him directly, try a suppressing fire
//see if where we're going to shoot is too far from his origin
AngleVectors (NPCS.NPCInfo->shootAngles, forward, NULL, NULL);
VectorMA ( muzzle, distanceToEnemy, forward, hitspot);
VectorSubtract(hitspot, enemy_org, diff);
aim_off = VectorLength(diff);
if(aim_off > Q_flrand(0.0f, 1.0f) * max_aim_off)//FIXME: use aim value to allow poor aim?
{
attack_scale *= 0.75;
//see if where we're going to shoot is too far from his head
VectorSubtract(hitspot, enemy_head, diff);
aim_off = VectorLength(diff);
if(aim_off > Q_flrand(0.0f, 1.0f) * max_aim_off)
{
attack_ok = qfalse;
}
}
attack_scale *= (max_aim_off - aim_off + 1)/max_aim_off;
}
}
}
}
if( attack_ok )
{
if( NPC_CheckAttack( attack_scale ))
{//check aggression to decide if we should shoot
NPCS.enemyVisibility = VIS_SHOOT;
WeaponThink(qtrue);
}
}
//Don't do this- only for when stationary and trying to shoot an enemy
// else
// NPC->cantHitEnemyCounter++;
}
else
{//FIXME:
NPC_UpdateShootAngles(NPCS.NPC->client->ps.viewangles, qtrue, qtrue);
}
if(!NPCS.ucmd.forwardmove && !NPCS.ucmd.rightmove)
{//We reached our captureGoal
if(trap->ICARUS_IsInitialized(NPCS.NPC->s.number))
{
trap->ICARUS_TaskIDComplete( (sharedEntity_t *)NPCS.NPC, TID_BSTATE );
}
}
}
void Disappear(gentity_t *self)
{
// ClientDisconnect(self);
self->s.eFlags |= EF_NODRAW;
self->think = 0;
self->nextthink = -1;
}
void MakeOwnerInvis (gentity_t *self);
void BeamOut (gentity_t *self)
{
// gentity_t *tent = G_Spawn();
/*
tent->owner = self;
tent->think = MakeOwnerInvis;
tent->nextthink = level.time + 1800;
//G_AddEvent( ent, EV_PLAYER_TELEPORT, 0 );
tent = G_TempEntity( self->client->pcurrentOrigin, EV_PLAYER_TELEPORT );
*/
//fixme: doesn't actually go away!
self->nextthink = level.time + 1500;
self->think = Disappear;
self->client->squadname = NULL;
self->client->playerTeam = self->s.teamowner = TEAM_FREE;
//self->r.svFlags |= SVF_BEAMING; //this appears unused in SP as well
}
void NPC_BSCinematic( void )
{
if( NPCS.NPCInfo->scriptFlags & SCF_FIRE_WEAPON )
{
WeaponThink( qtrue );
}
if ( UpdateGoal() )
{//have a goalEntity
//move toward goal, should also face that goal
NPC_MoveToGoal( qtrue );
}
if ( NPCS.NPCInfo->watchTarget )
{//have an entity which we want to keep facing
//NOTE: this will override any angles set by NPC_MoveToGoal
vec3_t eyes, viewSpot, viewvec, viewangles;
CalcEntitySpot( NPCS.NPC, SPOT_HEAD_LEAN, eyes );
CalcEntitySpot( NPCS.NPCInfo->watchTarget, SPOT_HEAD_LEAN, viewSpot );
VectorSubtract( viewSpot, eyes, viewvec );
vectoangles( viewvec, viewangles );
NPCS.NPCInfo->lockedDesiredYaw = NPCS.NPCInfo->desiredYaw = viewangles[YAW];
NPCS.NPCInfo->lockedDesiredPitch = NPCS.NPCInfo->desiredPitch = viewangles[PITCH];
}
NPC_UpdateAngles( qtrue, qtrue );
}
void NPC_BSWait( void )
{
NPC_UpdateAngles( qtrue, qtrue );
}
void NPC_BSInvestigate (void)
{
/*
//FIXME: maybe allow this to be set as a tempBState in a script? Just specify the
//investigateGoal, investigateDebounceTime and investigateCount? (Needs a macro)
vec3_t invDir, invAngles, spot;
gentity_t *saveGoal;
//BS_INVESTIGATE would turn toward goal, maybe take a couple steps towards it,
//look for enemies, then turn away after your investigate counter was down-
//investigate counter goes up every time you set it...
if(level.time > NPCInfo->enemyCheckDebounceTime)
{
NPCInfo->enemyCheckDebounceTime = level.time + (NPCInfo->stats.vigilance * 1000);
NPC_CheckEnemy(qtrue, qfalse);
if(NPC->enemy)
{//FIXME: do anger script
NPCInfo->goalEntity = NPC->enemy;
// NAV_ClearLastRoute(NPC);
NPCInfo->behaviorState = BS_RUN_AND_SHOOT;
NPCInfo->tempBehavior = BS_DEFAULT;
NPC_AngerSound();
return;
}
}
NPC_SetAnim( NPC, SETANIM_TORSO, TORSO_WEAPONREADY3, SETANIM_FLAG_NORMAL );
if(NPCInfo->stats.vigilance <= 1.0 && NPCInfo->eventOwner)
{
VectorCopy(NPCInfo->eventOwner->r.currentOrigin, NPCInfo->investigateGoal);
}
saveGoal = NPCInfo->goalEntity;
if( level.time > NPCInfo->walkDebounceTime )
{
vec3_t vec;
VectorSubtract(NPCInfo->investigateGoal, NPC->r.currentOrigin, vec);
vec[2] = 0;
if(VectorLength(vec) > 64)
{
if(Q_irand(0, 100) < NPCInfo->investigateCount)
{//take a full step
//NPCInfo->walkDebounceTime = level.time + 1400;
//actually finds length of my BOTH_WALK anim
NPCInfo->walkDebounceTime = PM_AnimLength( NPC->client->clientInfo.animFileIndex, BOTH_WALK1 );
}
}
}
if( level.time < NPCInfo->walkDebounceTime )
{//walk toward investigateGoal
/*
NPCInfo->goalEntity = NPCInfo->tempGoal;
// NAV_ClearLastRoute(NPC);
VectorCopy(NPCInfo->investigateGoal, NPCInfo->tempGoal->r.currentOrigin);
*/
/* NPC_SetMoveGoal( NPC, NPCInfo->investigateGoal, 16, qtrue );
NPC_MoveToGoal( qtrue );
//FIXME: walk2?
NPC_SetAnim(NPC,SETANIM_LEGS,BOTH_WALK1,SETANIM_FLAG_NORMAL);
ucmd.buttons |= BUTTON_WALKING;
}
else
{
NPC_SetAnim(NPC,SETANIM_LEGS,BOTH_STAND1,SETANIM_FLAG_NORMAL);
if(NPCInfo->hlookCount > 30)
{
if(Q_irand(0, 10) > 7)
{
NPCInfo->hlookCount = 0;
}
}
else if(NPCInfo->hlookCount < -30)
{
if(Q_irand(0, 10) > 7)
{
NPCInfo->hlookCount = 0;
}
}
else if(NPCInfo->hlookCount == 0)
{
NPCInfo->hlookCount = Q_irand(-1, 1);
}
else if(Q_irand(0, 10) > 7)
{
if(NPCInfo->hlookCount > 0)
{
NPCInfo->hlookCount++;
}
else//lookCount < 0
{
NPCInfo->hlookCount--;
}
}
if(NPCInfo->vlookCount >= 15)
{
if(Q_irand(0, 10) > 7)
{
NPCInfo->vlookCount = 0;
}
}
else if(NPCInfo->vlookCount <= -15)
{
if(Q_irand(0, 10) > 7)
{
NPCInfo->vlookCount = 0;
}
}
else if(NPCInfo->vlookCount == 0)
{
NPCInfo->vlookCount = Q_irand(-1, 1);
}
else if(Q_irand(0, 10) > 8)
{
if(NPCInfo->vlookCount > 0)
{
NPCInfo->vlookCount++;
}
else//lookCount < 0
{
NPCInfo->vlookCount--;
}
}
//turn toward investigateGoal
CalcEntitySpot( NPC, SPOT_HEAD, spot );
VectorSubtract(NPCInfo->investigateGoal, spot, invDir);
VectorNormalize(invDir);
vectoangles(invDir, invAngles);
NPCInfo->desiredYaw = AngleNormalize360(invAngles[YAW] + NPCInfo->hlookCount);
NPCInfo->desiredPitch = AngleNormalize360(invAngles[PITCH] + NPCInfo->hlookCount);
}
NPC_UpdateAngles(qtrue, qtrue);
NPCInfo->goalEntity = saveGoal;
// NAV_ClearLastRoute(NPC);
if(level.time > NPCInfo->investigateDebounceTime)
{
NPCInfo->tempBehavior = BS_DEFAULT;
}
NPC_CheckSoundEvents();
*/
}
qboolean NPC_CheckInvestigate( int alertEventNum )
{
gentity_t *owner = level.alertEvents[alertEventNum].owner;
int invAdd = level.alertEvents[alertEventNum].level;
vec3_t soundPos;
float soundRad = level.alertEvents[alertEventNum].radius;
float earshot = NPCS.NPCInfo->stats.earshot;
VectorCopy( level.alertEvents[alertEventNum].position, soundPos );
//NOTE: Trying to preserve previous investigation behavior
if ( !owner )
{
return qfalse;
}
if ( owner->s.eType != ET_PLAYER && owner->s.eType != ET_NPC && owner == NPCS.NPCInfo->goalEntity )
{
return qfalse;
}
if ( owner->s.eFlags & EF_NODRAW )
{
return qfalse;
}
if ( owner->flags & FL_NOTARGET )
{
return qfalse;
}
if ( soundRad < earshot )
{
return qfalse;
}
//if(!trap->InPVSIgnorePortals(ent->r.currentOrigin, NPC->r.currentOrigin))//should we be able to hear through areaportals?
if ( !trap->InPVS( soundPos, NPCS.NPC->r.currentOrigin ) )
{//can hear through doors?
return qfalse;
}
if ( owner->client && owner->client->playerTeam && NPCS.NPC->client->playerTeam && owner->client->playerTeam != NPCS.NPC->client->playerTeam )
{
if( (float)NPCS.NPCInfo->investigateCount >= (NPCS.NPCInfo->stats.vigilance*200) && owner )
{//If investigateCount == 10, just take it as enemy and go
if ( ValidEnemy( owner ) )
{//FIXME: run angerscript
G_SetEnemy( NPCS.NPC, owner );
NPCS.NPCInfo->goalEntity = NPCS.NPC->enemy;
NPCS.NPCInfo->goalRadius = 12;
NPCS.NPCInfo->behaviorState = BS_HUNT_AND_KILL;
return qtrue;
}
}
else
{
NPCS.NPCInfo->investigateCount += invAdd;
}
//run awakescript
G_ActivateBehavior(NPCS.NPC, BSET_AWAKE);
/*
if ( Q_irand(0, 10) > 7 )
{
NPC_AngerSound();
}
*/
//NPCInfo->hlookCount = NPCInfo->vlookCount = 0;
NPCS.NPCInfo->eventOwner = owner;
VectorCopy( soundPos, NPCS.NPCInfo->investigateGoal );
if ( NPCS.NPCInfo->investigateCount > 20 )
{
NPCS.NPCInfo->investigateDebounceTime = level.time + 10000;
}
else
{
NPCS.NPCInfo->investigateDebounceTime = level.time + (NPCS.NPCInfo->investigateCount*500);
}
NPCS.NPCInfo->tempBehavior = BS_INVESTIGATE;
return qtrue;
}
return qfalse;
}
/*
void NPC_BSSleep( void )
*/
void NPC_BSSleep( void )
{
int alertEvent = NPC_CheckAlertEvents( qtrue, qfalse, -1, qfalse, AEL_MINOR );
//There is an event to look at
if ( alertEvent >= 0 )
{
G_ActivateBehavior(NPCS.NPC, BSET_AWAKE);
return;
}
/*
if ( level.time > NPCInfo->enemyCheckDebounceTime )
{
if ( NPC_CheckSoundEvents() != -1 )
{//only 1 alert per second per 0.1 of vigilance
NPCInfo->enemyCheckDebounceTime = level.time + (NPCInfo->stats.vigilance * 10000);
G_ActivateBehavior(NPC, BSET_AWAKE);
}
}
*/
}
extern qboolean NPC_MoveDirClear( int forwardmove, int rightmove, qboolean reset );
void NPC_BSFollowLeader (void)
{
vec3_t vec;
float leaderDist;
visibility_t leaderVis;
int curAnim;
if ( !NPCS.NPC->client->leader )
{//ok, stand guard until we find an enemy
if( NPCS.NPCInfo->tempBehavior == BS_HUNT_AND_KILL )
{
NPCS.NPCInfo->tempBehavior = BS_DEFAULT;
}
else
{
NPCS.NPCInfo->tempBehavior = BS_STAND_GUARD;
NPC_BSStandGuard();
}
return;
}
if ( !NPCS.NPC->enemy )
{//no enemy, find one
NPC_CheckEnemy( NPCS.NPCInfo->confusionTime<level.time, qfalse, qtrue );//don't find new enemy if this is tempbehav
if ( NPCS.NPC->enemy )
{//just found one
NPCS.NPCInfo->enemyCheckDebounceTime = level.time + Q_irand( 3000, 10000 );
}
else
{
if ( !(NPCS.NPCInfo->scriptFlags&SCF_IGNORE_ALERTS) )
{
int eventID = NPC_CheckAlertEvents( qtrue, qtrue, -1, qfalse, AEL_MINOR );
if ( level.alertEvents[eventID].level >= AEL_SUSPICIOUS && (NPCS.NPCInfo->scriptFlags&SCF_LOOK_FOR_ENEMIES) )
{
NPCS.NPCInfo->lastAlertID = level.alertEvents[eventID].ID;
if ( !level.alertEvents[eventID].owner ||
!level.alertEvents[eventID].owner->client ||
level.alertEvents[eventID].owner->health <= 0 ||
level.alertEvents[eventID].owner->client->playerTeam != NPCS.NPC->client->enemyTeam )
{//not an enemy
}
else
{
//FIXME: what if can't actually see enemy, don't know where he is... should we make them just become very alert and start looking for him? Or just let combat AI handle this... (act as if you lost him)
G_SetEnemy( NPCS.NPC, level.alertEvents[eventID].owner );
NPCS.NPCInfo->enemyCheckDebounceTime = level.time + Q_irand( 3000, 10000 );
NPCS.NPCInfo->enemyLastSeenTime = level.time;
TIMER_Set( NPCS.NPC, "attackDelay", Q_irand( 500, 1000 ) );
}
}
}
}
if ( !NPCS.NPC->enemy )
{
if ( NPCS.NPC->client->leader
&& NPCS.NPC->client->leader->enemy
&& NPCS.NPC->client->leader->enemy != NPCS.NPC
&& ( (NPCS.NPC->client->leader->enemy->client&&NPCS.NPC->client->leader->enemy->client->playerTeam==NPCS.NPC->client->enemyTeam)
||(/*NPC->client->leader->enemy->r.svFlags&SVF_NONNPC_ENEMY*/0&&NPCS.NPC->client->leader->enemy->alliedTeam==NPCS.NPC->client->enemyTeam) )
&& NPCS.NPC->client->leader->enemy->health > 0 )
{ //rwwFIXMEFIXME: use SVF_NONNPC_ENEMY?
G_SetEnemy( NPCS.NPC, NPCS.NPC->client->leader->enemy );
NPCS.NPCInfo->enemyCheckDebounceTime = level.time + Q_irand( 3000, 10000 );
NPCS.NPCInfo->enemyLastSeenTime = level.time;
}
}
}
else
{
if ( NPCS.NPC->enemy->health <= 0 || (NPCS.NPC->enemy->flags&FL_NOTARGET) )
{
G_ClearEnemy( NPCS.NPC );
if ( NPCS.NPCInfo->enemyCheckDebounceTime > level.time + 1000 )
{
NPCS.NPCInfo->enemyCheckDebounceTime = level.time + Q_irand( 1000, 2000 );
}
}
else if ( NPCS.NPC->client->ps.weapon && NPCS.NPCInfo->enemyCheckDebounceTime < level.time )
{
NPC_CheckEnemy( (NPCS.NPCInfo->confusionTime<level.time||NPCS.NPCInfo->tempBehavior!=BS_FOLLOW_LEADER), qfalse, qtrue );//don't find new enemy if this is tempbehav
}
}
if ( NPCS.NPC->enemy && NPCS.NPC->client->ps.weapon )
{//If have an enemy, face him and fire
if ( NPCS.NPC->client->ps.weapon == WP_SABER )//|| NPCInfo->confusionTime>level.time )
{//lightsaber user or charmed enemy
if ( NPCS.NPCInfo->tempBehavior != BS_FOLLOW_LEADER )
{//not already in a temp bState
//go after the guy
NPCS.NPCInfo->tempBehavior = BS_HUNT_AND_KILL;
NPC_UpdateAngles(qtrue, qtrue);
return;
}
}
NPCS.enemyVisibility = NPC_CheckVisibility ( NPCS.NPC->enemy, CHECK_FOV|CHECK_SHOOT );//CHECK_360|CHECK_PVS|
if ( NPCS.enemyVisibility > VIS_PVS )
{//face
vec3_t enemy_org, muzzle, delta, angleToEnemy;
CalcEntitySpot( NPCS.NPC->enemy, SPOT_HEAD, enemy_org );
NPC_AimWiggle( enemy_org );
CalcEntitySpot( NPCS.NPC, SPOT_WEAPON, muzzle );
VectorSubtract( enemy_org, muzzle, delta);
vectoangles( delta, angleToEnemy );
NPCS.NPCInfo->desiredYaw = angleToEnemy[YAW];
NPCS.NPCInfo->desiredPitch = angleToEnemy[PITCH];
NPC_UpdateFiringAngles( qtrue, qtrue );
if ( NPCS.enemyVisibility >= VIS_SHOOT )
{//shoot
NPC_AimAdjust( 2 );
if ( NPC_GetHFOVPercentage( NPCS.NPC->enemy->r.currentOrigin, NPCS.NPC->r.currentOrigin, NPCS.NPC->client->ps.viewangles, NPCS.NPCInfo->stats.hfov ) > 0.6f
&& NPC_GetHFOVPercentage( NPCS.NPC->enemy->r.currentOrigin, NPCS.NPC->r.currentOrigin, NPCS.NPC->client->ps.viewangles, NPCS.NPCInfo->stats.vfov ) > 0.5f )
{//actually withing our front cone
WeaponThink( qtrue );
}
}
else
{
NPC_AimAdjust( 1 );
}
//NPC_CheckCanAttack(1.0, qfalse);
}
else
{
NPC_AimAdjust( -1 );
}
}
else
{//FIXME: combine with vector calc below
vec3_t head, leaderHead, delta, angleToLeader;
CalcEntitySpot( NPCS.NPC->client->leader, SPOT_HEAD, leaderHead );
CalcEntitySpot( NPCS.NPC, SPOT_HEAD, head );
VectorSubtract (leaderHead, head, delta);
vectoangles ( delta, angleToLeader );
VectorNormalize(delta);
NPCS.NPC->NPC->desiredYaw = angleToLeader[YAW];
NPCS.NPC->NPC->desiredPitch = angleToLeader[PITCH];
NPC_UpdateAngles(qtrue, qtrue);
}
//leader visible?
leaderVis = NPC_CheckVisibility( NPCS.NPC->client->leader, CHECK_PVS|CHECK_360|CHECK_SHOOT );// ent->e_UseFunc = useF_NULL;
//Follow leader, stay within visibility and a certain distance, maintain a distance from.
curAnim = NPCS.NPC->client->ps.legsAnim;
if ( curAnim != BOTH_ATTACK1 && curAnim != BOTH_ATTACK2 && curAnim != BOTH_ATTACK3 && curAnim != BOTH_MELEE1 && curAnim != BOTH_MELEE2 )
{//Don't move toward leader if we're in a full-body attack anim
//FIXME, use IdealDistance to determine if we need to close distance
float followDist = 96.0f;//FIXME: If there are enmies, make this larger?
float backupdist, walkdist, minrundist;
float leaderHDist;
if ( NPCS.NPCInfo->followDist )
{
followDist = NPCS.NPCInfo->followDist;
}
backupdist = followDist/2.0f;
walkdist = followDist*0.83;
minrundist = followDist*1.33;
VectorSubtract(NPCS.NPC->client->leader->r.currentOrigin, NPCS.NPC->r.currentOrigin, vec);
leaderDist = VectorLength( vec );//FIXME: make this just nav distance?
//never get within their radius horizontally
vec[2] = 0;
leaderHDist = VectorLength( vec );
if( leaderHDist > backupdist && (leaderVis != VIS_SHOOT || leaderDist > walkdist) )
{//We should close in?
NPCS.NPCInfo->goalEntity = NPCS.NPC->client->leader;
NPC_SlideMoveToGoal();
if ( leaderVis == VIS_SHOOT && leaderDist < minrundist )
{
NPCS.ucmd.buttons |= BUTTON_WALKING;
}
}
else if ( leaderDist < backupdist )
{//We should back off?
NPCS.NPCInfo->goalEntity = NPCS.NPC->client->leader;
NPC_SlideMoveToGoal();
//reversing direction
NPCS.ucmd.forwardmove = -NPCS.ucmd.forwardmove;
NPCS.ucmd.rightmove = -NPCS.ucmd.rightmove;
VectorScale( NPCS.NPC->client->ps.moveDir, -1, NPCS.NPC->client->ps.moveDir );
}//otherwise, stay where we are
//check for do not enter and stop if there's one there...
if ( NPCS.ucmd.forwardmove || NPCS.ucmd.rightmove || VectorCompare( vec3_origin, NPCS.NPC->client->ps.moveDir ) )
{
NPC_MoveDirClear( NPCS.ucmd.forwardmove, NPCS.ucmd.rightmove, qtrue );
}
}
}
#define APEX_HEIGHT 200.0f
#define PARA_WIDTH (sqrt(APEX_HEIGHT)+sqrt(APEX_HEIGHT))
#define JUMP_SPEED 200.0f
void NPC_BSJump (void)
{
vec3_t dir, angles, p1, p2, apex;
float time, height, forward, z, xy, dist, yawError, apexHeight;
if( !NPCS.NPCInfo->goalEntity )
{//Should have task completed the navgoal
return;
}
if ( NPCS.NPCInfo->jumpState != JS_JUMPING && NPCS.NPCInfo->jumpState != JS_LANDING )
{
//Face navgoal
VectorSubtract(NPCS.NPCInfo->goalEntity->r.currentOrigin, NPCS.NPC->r.currentOrigin, dir);
vectoangles(dir, angles);
NPCS.NPCInfo->desiredPitch = NPCS.NPCInfo->lockedDesiredPitch = AngleNormalize360(angles[PITCH]);
NPCS.NPCInfo->desiredYaw = NPCS.NPCInfo->lockedDesiredYaw = AngleNormalize360(angles[YAW]);
}
NPC_UpdateAngles ( qtrue, qtrue );
yawError = AngleDelta ( NPCS.NPC->client->ps.viewangles[YAW], NPCS.NPCInfo->desiredYaw );
//We don't really care about pitch here
switch ( NPCS.NPCInfo->jumpState )
{
case JS_FACING:
if ( yawError < MIN_ANGLE_ERROR )
{//Facing it, Start crouching
NPC_SetAnim(NPCS.NPC, SETANIM_LEGS, BOTH_CROUCH1, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD);
NPCS.NPCInfo->jumpState = JS_CROUCHING;
}
break;
case JS_CROUCHING:
if ( NPCS.NPC->client->ps.legsTimer > 0 )
{//Still playing crouching anim
return;
}
//Create a parabola
if ( NPCS.NPC->r.currentOrigin[2] > NPCS.NPCInfo->goalEntity->r.currentOrigin[2] )
{
VectorCopy( NPCS.NPC->r.currentOrigin, p1 );
VectorCopy( NPCS.NPCInfo->goalEntity->r.currentOrigin, p2 );
}
else if ( NPCS.NPC->r.currentOrigin[2] < NPCS.NPCInfo->goalEntity->r.currentOrigin[2] )
{
VectorCopy( NPCS.NPCInfo->goalEntity->r.currentOrigin, p1 );
VectorCopy( NPCS.NPC->r.currentOrigin, p2 );
}
else
{
VectorCopy( NPCS.NPC->r.currentOrigin, p1 );
VectorCopy( NPCS.NPCInfo->goalEntity->r.currentOrigin, p2 );
}
//z = xy*xy
VectorSubtract( p2, p1, dir );
dir[2] = 0;
//Get xy and z diffs
xy = VectorNormalize( dir );
z = p1[2] - p2[2];
apexHeight = APEX_HEIGHT/2;
/*
//Determine most desirable apex height
apexHeight = (APEX_HEIGHT * PARA_WIDTH/xy) + (APEX_HEIGHT * z/128);
if ( apexHeight < APEX_HEIGHT * 0.5 )
{
apexHeight = APEX_HEIGHT*0.5;
}
else if ( apexHeight > APEX_HEIGHT * 2 )
{
apexHeight = APEX_HEIGHT*2;
}
*/
//FIXME: length of xy will change curve of parabola, need to account for this
//somewhere... PARA_WIDTH
z = (sqrt(apexHeight + z) - sqrt(apexHeight));
assert(z >= 0);
// Com_Printf("apex is %4.2f percent from p1: ", (xy-z)*0.5/xy*100.0f);
// Don't need to set apex xy if NPC is jumping directly up.
if ( xy > 0.0f )
{
xy -= z;
xy *= 0.5;
assert(xy > 0);
}
VectorMA (p1, xy, dir, apex);
apex[2] += apexHeight;
VectorCopy(apex, NPCS.NPC->pos1);
//Now we have the apex, aim for it
height = apex[2] - NPCS.NPC->r.currentOrigin[2];
time = sqrt( height / ( .5 * NPCS.NPC->client->ps.gravity ) );
if ( !time )
{
// Com_Printf("ERROR no time in jump\n");
return;
}
// set s.origin2 to the push velocity
VectorSubtract ( apex, NPCS.NPC->r.currentOrigin, NPCS.NPC->client->ps.velocity );
NPCS.NPC->client->ps.velocity[2] = 0;
dist = VectorNormalize( NPCS.NPC->client->ps.velocity );
forward = dist / time;
VectorScale( NPCS.NPC->client->ps.velocity, forward, NPCS.NPC->client->ps.velocity );
NPCS.NPC->client->ps.velocity[2] = time * NPCS.NPC->client->ps.gravity;
// Com_Printf( "%s jumping %s, gravity at %4.0f percent\n", NPC->targetname, vtos(NPC->client->ps.velocity), NPC->client->ps.gravity/8.0f );
NPCS.NPC->flags |= FL_NO_KNOCKBACK;
NPCS.NPCInfo->jumpState = JS_JUMPING;
//FIXME: jumpsound?
break;
case JS_JUMPING:
if ( showBBoxes )
{
VectorAdd(NPCS.NPC->r.mins, NPCS.NPC->pos1, p1);
VectorAdd(NPCS.NPC->r.maxs, NPCS.NPC->pos1, p2);
G_Cube( p1, p2, NPCDEBUG_BLUE, 0.5 );
}
if ( NPCS.NPC->s.groundEntityNum != ENTITYNUM_NONE)
{//Landed, start landing anim
//FIXME: if the
VectorClear(NPCS.NPC->client->ps.velocity);
NPC_SetAnim(NPCS.NPC, SETANIM_BOTH, BOTH_LAND1, SETANIM_FLAG_OVERRIDE|SETANIM_FLAG_HOLD);
NPCS.NPCInfo->jumpState = JS_LANDING;
//FIXME: landsound?
}
else if ( NPCS.NPC->client->ps.legsTimer > 0 )
{//Still playing jumping anim
//FIXME: apply jump velocity here, a couple frames after start, not right away
return;
}
else
{//still in air, but done with jump anim, play inair anim
NPC_SetAnim(NPCS.NPC, SETANIM_BOTH, BOTH_INAIR1, SETANIM_FLAG_OVERRIDE);
}
break;
case JS_LANDING:
if ( NPCS.NPC->client->ps.legsTimer > 0 )
{//Still playing landing anim
return;
}
else
{
NPCS.NPCInfo->jumpState = JS_WAITING;
//task complete no matter what...
NPC_ClearGoal();
NPCS.NPCInfo->goalTime = level.time;
NPCS.NPCInfo->aiFlags &= ~NPCAI_MOVING;
NPCS.ucmd.forwardmove = 0;
NPCS.NPC->flags &= ~FL_NO_KNOCKBACK;
//Return that the goal was reached
trap->ICARUS_TaskIDComplete( (sharedEntity_t *)NPCS.NPC, TID_MOVE_NAV );
//Or should we keep jumping until reached goal?
/*
NPCInfo->goalEntity = UpdateGoal();
if ( !NPCInfo->goalEntity )
{
NPC->flags &= ~FL_NO_KNOCKBACK;
Q3_TaskIDComplete( NPC, TID_MOVE_NAV );
}
*/
}
break;
case JS_WAITING:
default:
NPCS.NPCInfo->jumpState = JS_FACING;
break;
}
}
void NPC_BSRemove (void)
{
NPC_UpdateAngles ( qtrue, qtrue );
//OJKFIXME: clientnum 0
if( !trap->InPVS( NPCS.NPC->r.currentOrigin, g_entities[0].r.currentOrigin ) )//FIXME: use cg.vieworg?
{ //rwwFIXMEFIXME: Care about all clients instead of just 0?
G_UseTargets2( NPCS.NPC, NPCS.NPC, NPCS.NPC->target3 );
NPCS.NPC->s.eFlags |= EF_NODRAW;
NPCS.NPC->s.eType = ET_INVISIBLE;
NPCS.NPC->r.contents = 0;
NPCS.NPC->health = 0;
NPCS.NPC->targetname = NULL;
//Disappear in half a second
NPCS.NPC->think = G_FreeEntity;
NPCS.NPC->nextthink = level.time + FRAMETIME;
}//FIXME: else allow for out of FOV???
}
void NPC_BSSearch (void)
{
NPC_CheckEnemy(qtrue, qfalse, qtrue);
//Look for enemies, if find one:
if ( NPCS.NPC->enemy )
{
if( NPCS.NPCInfo->tempBehavior == BS_SEARCH )
{//if tempbehavior, set tempbehavior to default
NPCS.NPCInfo->tempBehavior = BS_DEFAULT;
}
else
{//if bState, change to run and shoot
NPCS.NPCInfo->behaviorState = BS_HUNT_AND_KILL;
NPC_BSRunAndShoot();
}
return;
}
//FIXME: what if our goalEntity is not NULL and NOT our tempGoal - they must
//want us to do something else? If tempBehavior, just default, else set
//to run and shoot...?
//FIXME: Reimplement
if ( !NPCS.NPCInfo->investigateDebounceTime )
{//On our way to a tempGoal
float minGoalReachedDistSquared = 32*32;
vec3_t vec;
//Keep moving toward our tempGoal
NPCS.NPCInfo->goalEntity = NPCS.NPCInfo->tempGoal;
VectorSubtract ( NPCS.NPCInfo->tempGoal->r.currentOrigin, NPCS.NPC->r.currentOrigin, vec);
if ( vec[2] < 24 )
{
vec[2] = 0;
}
if ( NPCS.NPCInfo->tempGoal->waypoint != WAYPOINT_NONE )
{
/*