-
Notifications
You must be signed in to change notification settings - Fork 270
/
Copy pathhl1_npc_hgrunt.cpp
2675 lines (2318 loc) · 69.4 KB
/
hl1_npc_hgrunt.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 Valve Corporation, All rights reserved. ============//
//
// Purpose: Bullseyes act as targets for other NPC's to attack and to trigger
// events
//
// $Workfile: $
// $Date: $
//
//-----------------------------------------------------------------------------
// $Log: $
//
// $NoKeywords: $
//=============================================================================//
#include "ai_condition.h"
#include "cbase.h"
#include "beam_shared.h"
#include "ai_default.h"
#include "ai_task.h"
#include "ai_schedule.h"
#include "ai_node.h"
#include "ai_hull.h"
#include "ai_hint.h"
#include "ai_memory.h"
#include "ai_route.h"
#include "ai_motor.h"
#include "hl1_npc_hgrunt.h"
#include "soundent.h"
#include "game.h"
#include "npcevent.h"
#include "entitylist.h"
#include "activitylist.h"
#include "animation.h"
#include "engine/IEngineSound.h"
#include "ammodef.h"
#include "basecombatweapon.h"
#include "hl1_basegrenade.h"
#include "ai_interactions.h"
#include "scripted.h"
#include "hl1_basegrenade.h"
#include "hl1_grenade_mp5.h"
ConVar sk_hgrunt_health( "sk_hgrunt_health","0");
ConVar sk_hgrunt_kick ( "sk_hgrunt_kick", "0" );
ConVar sk_hgrunt_pellets ( "sk_hgrunt_pellets", "0" );
ConVar sk_hgrunt_gspeed ( "sk_hgrunt_gspeed", "0" );
extern ConVar sk_plr_dmg_grenade;
extern ConVar sk_plr_dmg_mp5_grenade;
#define SF_GRUNT_LEADER ( 1 << 5 )
int g_fGruntQuestion; // true if an idle grunt asked a question. Cleared when someone answers.
int g_iSquadIndex = 0;
#define HGRUNT_GUN_SPREAD 0.08716f
//=========================================================
// monster-specific DEFINE's
//=========================================================
#define GRUNT_CLIP_SIZE 36 // how many bullets in a clip? - NOTE: 3 round burst sound, so keep as 3 * x!
#define GRUNT_VOL 0.35 // volume of grunt sounds
#define GRUNT_SNDLVL SNDLVL_NORM // soundlevel of grunt sentences
#define HGRUNT_LIMP_HEALTH 20
#define HGRUNT_DMG_HEADSHOT ( DMG_BULLET | DMG_CLUB ) // damage types that can kill a grunt with a single headshot.
#define HGRUNT_NUM_HEADS 2 // how many grunt heads are there?
#define HGRUNT_MINIMUM_HEADSHOT_DAMAGE 15 // must do at least this much damage in one shot to head to score a headshot kill
#define HGRUNT_SENTENCE_VOLUME (float)0.35 // volume of grunt sentences
#define HGRUNT_9MMAR ( 1 << 0)
#define HGRUNT_HANDGRENADE ( 1 << 1)
#define HGRUNT_GRENADELAUNCHER ( 1 << 2)
#define HGRUNT_SHOTGUN ( 1 << 3)
#define HEAD_GROUP 1
#define HEAD_GRUNT 0
#define HEAD_COMMANDER 1
#define HEAD_SHOTGUN 2
#define HEAD_M203 3
#define GUN_GROUP 2
#define GUN_MP5 0
#define GUN_SHOTGUN 1
#define GUN_NONE 2
//=========================================================
// Monster's Anim Events Go Here
//=========================================================
#define HGRUNT_AE_RELOAD ( 2 )
#define HGRUNT_AE_KICK ( 3 )
#define HGRUNT_AE_BURST1 ( 4 )
#define HGRUNT_AE_BURST2 ( 5 )
#define HGRUNT_AE_BURST3 ( 6 )
#define HGRUNT_AE_GREN_TOSS ( 7 )
#define HGRUNT_AE_GREN_LAUNCH ( 8 )
#define HGRUNT_AE_GREN_DROP ( 9 )
#define HGRUNT_AE_CAUGHT_ENEMY ( 10) // grunt established sight with an enemy (player only) that had previously eluded the squad.
#define HGRUNT_AE_DROP_GUN ( 11) // grunt (probably dead) is dropping his mp5.
const char *CNPC_HGrunt::pGruntSentences[] =
{
"HG_GREN", // grenade scared grunt
"HG_ALERT", // sees player
"HG_MONSTER", // sees monster
"HG_COVER", // running to cover
"HG_THROW", // about to throw grenade
"HG_CHARGE", // running out to get the enemy
"HG_TAUNT", // say rude things
};
enum HGRUNT_SENTENCE_TYPES
{
HGRUNT_SENT_NONE = -1,
HGRUNT_SENT_GREN = 0,
HGRUNT_SENT_ALERT,
HGRUNT_SENT_MONSTER,
HGRUNT_SENT_COVER,
HGRUNT_SENT_THROW,
HGRUNT_SENT_CHARGE,
HGRUNT_SENT_TAUNT,
} ;
LINK_ENTITY_TO_CLASS( monster_human_grunt, CNPC_HGrunt );
//=========================================================
// monster-specific schedule types
//=========================================================
enum
{
SCHED_GRUNT_FAIL = LAST_SHARED_SCHEDULE,
SCHED_GRUNT_COMBAT_FAIL,
SCHED_GRUNT_VICTORY_DANCE,
SCHED_GRUNT_ESTABLISH_LINE_OF_FIRE,
SCHED_GRUNT_ESTABLISH_LINE_OF_FIRE_RETRY,
SCHED_GRUNT_FOUND_ENEMY,
SCHED_GRUNT_COMBAT_FACE,
SCHED_GRUNT_SIGNAL_SUPPRESS,
SCHED_GRUNT_SUPPRESS,
SCHED_GRUNT_WAIT_IN_COVER,
SCHED_GRUNT_TAKE_COVER,
SCHED_GRUNT_GRENADE_COVER,
SCHED_GRUNT_TOSS_GRENADE_COVER,
SCHED_GRUNT_HIDE_RELOAD,
SCHED_GRUNT_SWEEP,
SCHED_GRUNT_RANGE_ATTACK1A,
SCHED_GRUNT_RANGE_ATTACK1B,
SCHED_GRUNT_RANGE_ATTACK2,
SCHED_GRUNT_REPEL,
SCHED_GRUNT_REPEL_ATTACK,
SCHED_GRUNT_REPEL_LAND,
SCHED_GRUNT_TAKE_COVER_FAILED,
SCHED_GRUNT_RELOAD,
SCHED_GRUNT_TAKE_COVER_FROM_ENEMY,
SCHED_GRUNT_BARNACLE_HIT,
SCHED_GRUNT_BARNACLE_PULL,
SCHED_GRUNT_BARNACLE_CHOMP,
SCHED_GRUNT_BARNACLE_CHEW,
};
//=========================================================
// monster-specific tasks
//=========================================================
enum
{
TASK_GRUNT_FACE_TOSS_DIR = LAST_SHARED_TASK + 1,
TASK_GRUNT_SPEAK_SENTENCE,
TASK_GRUNT_CHECK_FIRE,
};
//=========================================================
// monster-specific conditions
//=========================================================
enum
{
COND_GRUNT_NOFIRE = LAST_SHARED_CONDITION + 1,
};
// -----------------------------------------------
// > Squad slots
// -----------------------------------------------
enum SquadSlot_T
{
SQUAD_SLOT_GRENADE1 = LAST_SHARED_SQUADSLOT,
SQUAD_SLOT_GRENADE2,
SQUAD_SLOT_ENGAGE1,
SQUAD_SLOT_ENGAGE2,
};
int ACT_GRUNT_LAUNCH_GRENADE;
int ACT_GRUNT_TOSS_GRENADE;
int ACT_GRUNT_MP5_STANDING;
int ACT_GRUNT_MP5_CROUCHING;
int ACT_GRUNT_SHOTGUN_STANDING;
int ACT_GRUNT_SHOTGUN_CROUCHING;
//---------------------------------------------------------
// Save/Restore
//---------------------------------------------------------
BEGIN_DATADESC( CNPC_HGrunt )
DEFINE_FIELD( m_flNextGrenadeCheck, FIELD_TIME ),
DEFINE_FIELD( m_flNextPainTime, FIELD_TIME ),
DEFINE_FIELD( m_flCheckAttackTime, FIELD_FLOAT ),
DEFINE_FIELD( m_vecTossVelocity, FIELD_VECTOR ),
DEFINE_FIELD( m_iLastGrenadeCondition, FIELD_INTEGER ),
DEFINE_FIELD( m_fStanding, FIELD_BOOLEAN ),
DEFINE_FIELD( m_fFirstEncounter, FIELD_BOOLEAN ),
DEFINE_FIELD( m_iClipSize, FIELD_INTEGER ),
DEFINE_FIELD( m_voicePitch, FIELD_INTEGER ),
DEFINE_FIELD( m_iSentence, FIELD_INTEGER ),
DEFINE_KEYFIELD( m_iWeapons, FIELD_INTEGER, "weapons" ),
DEFINE_FIELD( m_bInBarnacleMouth, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flLastEnemySightTime, FIELD_TIME ),
DEFINE_FIELD( m_flTalkWaitTime, FIELD_TIME ),
//DEFINE_FIELD( m_iAmmoType, FIELD_INTEGER ),
END_DATADESC()
//=========================================================
// Spawn
//=========================================================
void CNPC_HGrunt::Spawn()
{
Precache( );
SetModel( "models/hgrunt.mdl" );
SetHullType(HULL_HUMAN);
SetHullSizeNormal();
SetSolid( SOLID_BBOX );
AddSolidFlags( FSOLID_NOT_STANDABLE );
SetMoveType( MOVETYPE_STEP );
m_bloodColor = BLOOD_COLOR_RED;
ClearEffects();
m_iHealth = sk_hgrunt_health.GetFloat();
m_flFieldOfView = 0.4;// indicates the width of this monster's forward view cone ( as a dotproduct result )
m_NPCState = NPC_STATE_NONE;
m_flNextGrenadeCheck = gpGlobals->curtime + 1;
m_flNextPainTime = gpGlobals->curtime;
m_iSentence = HGRUNT_SENT_NONE;
CapabilitiesClear();
CapabilitiesAdd ( bits_CAP_SQUAD | bits_CAP_TURN_HEAD | bits_CAP_DOORS_GROUP | bits_CAP_MOVE_GROUND );
CapabilitiesAdd(bits_CAP_INNATE_RANGE_ATTACK1 );
// Innate range attack for grenade
CapabilitiesAdd(bits_CAP_INNATE_RANGE_ATTACK2 );
// Innate range attack for kicking
CapabilitiesAdd(bits_CAP_INNATE_MELEE_ATTACK1 );
m_fFirstEncounter = true;// this is true when the grunt spawns, because he hasn't encountered an enemy yet.
m_HackedGunPos = Vector ( 0, 0, 55 );
if ( m_iWeapons == 0)
{
// initialize to original values
m_iWeapons = HGRUNT_9MMAR | HGRUNT_HANDGRENADE;
// pev->weapons = HGRUNT_SHOTGUN;
// pev->weapons = HGRUNT_9MMAR | HGRUNT_GRENADELAUNCHER;
}
if (FBitSet( m_iWeapons, HGRUNT_SHOTGUN ))
{
SetBodygroup( GUN_GROUP, GUN_SHOTGUN );
m_iClipSize = 8;
}
else
{
m_iClipSize = GRUNT_CLIP_SIZE;
}
m_cAmmoLoaded = m_iClipSize;
if ( random->RandomInt( 0, 99 ) < 80)
m_nSkin = 0; // light skin
else
m_nSkin = 1; // dark skin
if (FBitSet( m_iWeapons, HGRUNT_SHOTGUN ))
{
SetBodygroup( HEAD_GROUP, HEAD_SHOTGUN);
}
else if (FBitSet( m_iWeapons, HGRUNT_GRENADELAUNCHER ))
{
SetBodygroup( HEAD_GROUP, HEAD_M203 );
m_nSkin = 1; // alway dark skin
}
m_flTalkWaitTime = 0;
//HACK
g_iSquadIndex = 0;
BaseClass::Spawn();
NPCInit();
CreateVPhysics();
}
bool CNPC_HGrunt::CreateVPhysics(void)
{
VPhysicsDestroyObject();
CPhysCollide* pModel = PhysCreateBbox(NAI_Hull::Mins(HULL_HUMAN), NAI_Hull::Maxs(HULL_HUMAN));
IPhysicsObject* pPhysics = PhysModelCreateCustom(this, pModel, GetAbsOrigin(), GetAbsAngles(), "barney_hull", false);
VPhysicsSetObject(pPhysics);
if (pPhysics)
{
pPhysics->SetCallbackFlags(CALLBACK_GLOBAL_COLLISION | CALLBACK_SHADOW_COLLISION);
}
PhysAddShadow(this);
return true;
}
int CNPC_HGrunt::IRelationPriority( CBaseEntity *pTarget )
{
//I hate alien grunts more than anything.
if ( pTarget->Classify() == CLASS_ALIEN_MILITARY )
{
if ( FClassnameIs( pTarget, "monster_alien_grunt" ) )
{
return ( BaseClass::IRelationPriority ( pTarget ) + 1 );
}
}
return BaseClass::IRelationPriority( pTarget );
}
//=========================================================
// Precache - precaches all resources this monster needs
//=========================================================
void CNPC_HGrunt::Precache()
{
m_iAmmoType = GetAmmoDef()->Index("9mmRound");
PrecacheModel("models/hgrunt.mdl");
// get voice pitch
if ( random->RandomInt(0,1))
m_voicePitch = 109 + random->RandomInt(0,7);
else
m_voicePitch = 100;
PrecacheScriptSound( "HGrunt.Reload" );
PrecacheScriptSound( "HGrunt.GrenadeLaunch" );
PrecacheScriptSound( "HGrunt.9MM" );
PrecacheScriptSound( "HGrunt.Shotgun" );
PrecacheScriptSound( "HGrunt.Pain" );
PrecacheScriptSound( "HGrunt.Die" );
BaseClass::Precache();
UTIL_PrecacheOther( "grenade_hand" );
UTIL_PrecacheOther( "grenade_mp5" );
}
//=========================================================
// someone else is talking - don't speak
//=========================================================
bool CNPC_HGrunt::FOkToSpeak( void )
{
// if someone else is talking, don't speak
if ( gpGlobals->curtime <= m_flTalkWaitTime )
return FALSE;
if ( m_spawnflags & SF_NPC_GAG )
{
if ( m_NPCState != NPC_STATE_COMBAT )
{
// no talking outside of combat if gagged.
return FALSE;
}
}
return TRUE;
}
//=========================================================
// Speak Sentence - say your cued up sentence.
//
// Some grunt sentences (take cover and charge) rely on actually
// being able to execute the intended action. It's really lame
// when a grunt says 'COVER ME' and then doesn't move. The problem
// is that the sentences were played when the decision to TRY
// to move to cover was made. Now the sentence is played after
// we know for sure that there is a valid path. The schedule
// may still fail but in most cases, well after the grunt has
// started moving.
//=========================================================
void CNPC_HGrunt::SpeakSentence( void )
{
if ( m_iSentence == HGRUNT_SENT_NONE )
{
// no sentence cued up.
return;
}
if ( FOkToSpeak() )
{
SENTENCEG_PlayRndSz( edict(), pGruntSentences[ m_iSentence ], HGRUNT_SENTENCE_VOLUME, GRUNT_SNDLVL, 0, m_voicePitch);
JustSpoke();
}
}
//=========================================================
//=========================================================
void CNPC_HGrunt::JustSpoke( void )
{
m_flTalkWaitTime = gpGlobals->curtime + random->RandomFloat( 1.5f, 2.0f );
m_iSentence = HGRUNT_SENT_NONE;
}
//=========================================================
// PrescheduleThink - this function runs after conditions
// are collected and before scheduling code is run.
//=========================================================
void CNPC_HGrunt::PrescheduleThink ( void )
{
BaseClass::PrescheduleThink();
if ( m_pSquad && GetEnemy() != NULL )
{
if ( m_pSquad->GetLeader() == NULL )
return;
CNPC_HGrunt *pSquadLeader = (CNPC_HGrunt*)m_pSquad->GetLeader()->MyNPCPointer();
if ( pSquadLeader == NULL )
return; //Paranoid, so making sure it's ok.
if ( HasCondition ( COND_SEE_ENEMY ) )
{
// update the squad's last enemy sighting time.
pSquadLeader->m_flLastEnemySightTime = gpGlobals->curtime;
}
else
{
if ( gpGlobals->curtime - pSquadLeader->m_flLastEnemySightTime > 5.0f )
{
// been a while since we've seen the enemy
pSquadLeader->GetEnemies()->MarkAsEluded( GetEnemy() );
}
}
}
}
Class_T CNPC_HGrunt::Classify ( void )
{
return CLASS_HUMAN_MILITARY;
}
//=========================================================
//
// SquadRecruit(), get some monsters of my classification and
// link them as a group. returns the group size
//
//=========================================================
int CNPC_HGrunt::SquadRecruit( int searchRadius, int maxMembers )
{
int squadCount;
int iMyClass = Classify();// cache this monster's class
if ( maxMembers < 2 )
return 0;
// I am my own leader
squadCount = 1;
CBaseEntity *pEntity = NULL;
if ( m_SquadName != NULL_STRING )
{
// I have a netname, so unconditionally recruit everyone else with that name.
pEntity = gEntList.FindEntityByClassname( pEntity, "monster_human_grunt" );
while ( pEntity )
{
CNPC_HGrunt *pRecruit = (CNPC_HGrunt*)pEntity->MyNPCPointer();
if ( pRecruit )
{
if ( !pRecruit->m_pSquad && pRecruit->Classify() == iMyClass && pRecruit != this )
{
// minimum protection here against user error.in worldcraft.
if ( pRecruit->m_SquadName != NULL_STRING && FStrEq( STRING( m_SquadName ), STRING( pRecruit->m_SquadName ) ) )
{
pRecruit->InitSquad();
squadCount++;
}
}
}
pEntity = gEntList.FindEntityByClassname( pEntity, "monster_human_grunt" );
}
return squadCount;
}
else
{
char szSquadName[64];
Q_snprintf( szSquadName, sizeof( szSquadName ), "squad%d\n", g_iSquadIndex );
m_SquadName = AllocPooledString( szSquadName );
while ( ( pEntity = gEntList.FindEntityInSphere( pEntity, GetAbsOrigin(), searchRadius ) ) != NULL )
{
if ( !FClassnameIs ( pEntity, "monster_human_grunt" ) )
continue;
CNPC_HGrunt *pRecruit = (CNPC_HGrunt*)pEntity->MyNPCPointer();
if ( pRecruit && pRecruit != this && pRecruit->IsAlive() && !pRecruit->m_hCine )
{
// Can we recruit this guy?
if ( !pRecruit->m_pSquad && pRecruit->Classify() == iMyClass &&
( (iMyClass != CLASS_ALIEN_MONSTER) || FClassnameIs( this, pRecruit->GetClassname() ) ) &&
!pRecruit->m_SquadName )
{
trace_t tr;
UTIL_TraceLine( GetAbsOrigin() + GetViewOffset(), pRecruit->GetAbsOrigin() + GetViewOffset(), MASK_NPCSOLID_BRUSHONLY, pRecruit, COLLISION_GROUP_NONE, &tr );// try to hit recruit with a traceline.
if ( tr.fraction == 1.0 )
{
//We're ready to recruit people, so start a squad if I don't have one.
if ( !m_pSquad )
{
InitSquad();
}
pRecruit->m_SquadName = m_SquadName;
pRecruit->CapabilitiesAdd ( bits_CAP_SQUAD );
pRecruit->InitSquad();
squadCount++;
}
}
}
}
if ( squadCount > 1 )
{
g_iSquadIndex++;
}
}
return squadCount;
}
void CNPC_HGrunt::StartNPC ( void )
{
if ( !m_pSquad )
{
if ( m_SquadName != NULL_STRING )
{
// if I have a groupname, I can only recruit if I'm flagged as leader
if ( GetSpawnFlags() & SF_GRUNT_LEADER )
{
InitSquad();
// try to form squads now.
int iSquadSize = SquadRecruit( 1024, 4 );
if ( iSquadSize )
{
Msg ( "Squad of %d %s formed\n", iSquadSize, GetClassname() );
}
}
else
{
//Hacky.
//Revisit me later.
const char *pSquadName = STRING( m_SquadName );
m_SquadName = NULL_STRING;
BaseClass::StartNPC();
m_SquadName = MAKE_STRING( pSquadName );
return;
}
}
else
{
int iSquadSize = SquadRecruit( 1024, 4 );
if ( iSquadSize )
{
Msg ( "Squad of %d %s formed\n", iSquadSize, GetClassname() );
}
}
}
BaseClass::StartNPC();
if ( m_pSquad && m_pSquad->IsLeader( this ) )
{
SetBodygroup( 1, 1 ); // UNDONE: truly ugly hack
m_nSkin = 0;
}
}
//=========================================================
// CheckMeleeAttack1
//=========================================================
int CNPC_HGrunt::MeleeAttack1Conditions ( float flDot, float flDist )
{
if (flDist > 64)
return COND_TOO_FAR_TO_ATTACK;
else if (flDot < 0.7)
return COND_NOT_FACING_ATTACK;
return COND_CAN_MELEE_ATTACK1;
}
//=========================================================
// CheckRangeAttack1 - overridden for HGrunt, cause
// FCanCheckAttacks() doesn't disqualify all attacks based
// on whether or not the enemy is occluded because unlike
// the base class, the HGrunt can attack when the enemy is
// occluded (throw grenade over wall, etc). We must
// disqualify the machine gun attack if the enemy is occluded.
//=========================================================
int CNPC_HGrunt::RangeAttack1Conditions ( float flDot, float flDist )
{
if ( !HasCondition( COND_ENEMY_OCCLUDED ) && flDist <= 2048 && flDot >= 0.5 && NoFriendlyFire() )
{
trace_t tr;
if ( !GetEnemy()->IsPlayer() && flDist <= 64 )
{
// kick nonclients, but don't shoot at them.
return COND_NONE;
}
if (GetEnemy())
{
return COND_CAN_RANGE_ATTACK1;
}
Vector vecSrc;
QAngle angAngles;
GetAttachment( "0", vecSrc, angAngles );
//NDebugOverlay::Line( GetAbsOrigin() + GetViewOffset(), GetEnemy()->BodyTarget(GetAbsOrigin() + GetViewOffset()), 255, 0, 0, false, 0.1 );
// verify that a bullet fired from the gun will hit the enemy before the world.
UTIL_TraceLine( GetAbsOrigin() + GetViewOffset(), GetEnemy()->BodyTarget(GetAbsOrigin() + GetViewOffset()), MASK_SHOT, this/*pentIgnore*/, COLLISION_GROUP_NONE, &tr);
if ( tr.fraction == 1.0 || tr.m_pEnt == GetEnemy() )
{
//NDebugOverlay::Line( tr.startpos, tr.endpos, 0, 255, 0, false, 1.0 );
return COND_CAN_RANGE_ATTACK1;
}
//NDebugOverlay::Line( tr.startpos, tr.endpos, 255, 0, 0, false, 1.0 );
}
if ( !NoFriendlyFire() )
return COND_WEAPON_BLOCKED_BY_FRIEND; //err =|
return COND_NONE;
}
int CNPC_HGrunt::RangeAttack2Conditions( float flDot, float flDist )
{
m_iLastGrenadeCondition = GetGrenadeConditions( flDot, flDist );
return m_iLastGrenadeCondition;
}
int CNPC_HGrunt::GetGrenadeConditions( float flDot, float flDist )
{
if ( !FBitSet( m_iWeapons, ( HGRUNT_HANDGRENADE | HGRUNT_GRENADELAUNCHER ) ) )
return COND_NONE;
// assume things haven't changed too much since last time
if (gpGlobals->curtime < m_flNextGrenadeCheck )
return m_iLastGrenadeCondition;
if ( m_flGroundSpeed != 0 )
return COND_NONE;
CBaseEntity *pEnemy = GetEnemy();
if (!pEnemy)
return COND_NONE;
Vector flEnemyLKP = GetEnemyLKP();
if ( !(pEnemy->GetFlags() & FL_ONGROUND) && pEnemy->GetWaterLevel() == 0 && flEnemyLKP.z > (GetAbsOrigin().z + WorldAlignMaxs().z) )
{
//!!!BUGBUG - we should make this check movetype and make sure it isn't FLY? Players who jump a lot are unlikely to
// be grenaded.
// don't throw grenades at anything that isn't on the ground!
return COND_NONE;
}
Vector vecTarget;
if (FBitSet( m_iWeapons, HGRUNT_HANDGRENADE))
{
// find feet
if ( random->RandomInt( 0,1 ) )
{
// magically know where they are
pEnemy->CollisionProp()->NormalizedToWorldSpace( Vector( 0.5f, 0.5f, 0.0f ), &vecTarget );
}
else
{
// toss it to where you last saw them
vecTarget = flEnemyLKP;
}
}
else
{
// find target
// vecTarget = GetEnemy()->BodyTarget( GetAbsOrigin() );
vecTarget = GetEnemy()->GetAbsOrigin() + (GetEnemy()->BodyTarget( GetAbsOrigin() ) - GetEnemy()->GetAbsOrigin());
// estimate position
if ( HasCondition( COND_SEE_ENEMY))
{
vecTarget = vecTarget + ((vecTarget - GetAbsOrigin()).Length() / sk_hgrunt_gspeed.GetFloat()) * GetEnemy()->GetAbsVelocity();
}
}
// are any of my squad members near the intended grenade impact area?
if ( m_pSquad )
{
if ( m_pSquad->SquadMemberInRange( vecTarget, 256 ) )
{
// crap, I might blow my own guy up. Don't throw a grenade and don't check again for a while.
m_flNextGrenadeCheck = gpGlobals->curtime + 1; // one full second.
return COND_NONE;
}
}
if ( ( vecTarget - GetAbsOrigin() ).Length2D() <= 256 )
{
// crap, I don't want to blow myself up
m_flNextGrenadeCheck = gpGlobals->curtime + 1; // one full second.
return COND_NONE;
}
if (FBitSet( m_iWeapons, HGRUNT_HANDGRENADE))
{
Vector vGunPos;
QAngle angGunAngles;
GetAttachment( "0", vGunPos, angGunAngles );
Vector vecToss = VecCheckToss( this, vGunPos, vecTarget, -1, 0.5, false );
if ( vecToss != vec3_origin )
{
m_vecTossVelocity = vecToss;
// don't check again for a while.
m_flNextGrenadeCheck = gpGlobals->curtime + 0.3; // 1/3 second.
return COND_CAN_RANGE_ATTACK2;
}
else
{
// don't check again for a while.
m_flNextGrenadeCheck = gpGlobals->curtime + 1; // one full second.
return COND_NONE;
}
}
else
{
Vector vGunPos;
QAngle angGunAngles;
GetAttachment( "0", vGunPos, angGunAngles );
Vector vecToss = VecCheckThrow( this, vGunPos, vecTarget, sk_hgrunt_gspeed.GetFloat(), 0.5 );
if ( vecToss != vec3_origin )
{
m_vecTossVelocity = vecToss;
// don't check again for a while.
m_flNextGrenadeCheck = gpGlobals->curtime + 0.1; // 1/3 second.
return COND_CAN_RANGE_ATTACK2;
}
else
{
// don't check again for a while.
m_flNextGrenadeCheck = gpGlobals->curtime + 1; // one full second.
return COND_NONE;
}
}
}
//=========================================================
// FCanCheckAttacks - this is overridden for human grunts
// because they can throw/shoot grenades when they can't see their
// target and the base class doesn't check attacks if the monster
// cannot see its enemy.
//
// !!!BUGBUG - this gets called before a 3-round burst is fired
// which means that a friendly can still be hit with up to 2 rounds.
// ALSO, grenades will not be tossed if there is a friendly in front,
// this is a bad bug. Friendly machine gun fire avoidance
// will unecessarily prevent the throwing of a grenade as well.
//=========================================================
bool CNPC_HGrunt::FCanCheckAttacks( void )
{
// This condition set when too close to a grenade to blow it up
if ( !HasCondition( COND_TOO_CLOSE_TO_ATTACK ) )
{
return true;
}
else
{
return false;
}
}
int CNPC_HGrunt::GetSoundInterests( void )
{
return SOUND_WORLD |
SOUND_COMBAT |
SOUND_PLAYER |
SOUND_BULLET_IMPACT |
SOUND_DANGER;
}
//=========================================================
// TraceAttack - make sure we're not taking it in the helmet
//=========================================================
void CNPC_HGrunt::TraceAttack( const CTakeDamageInfo &inputInfo, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator )
{
CTakeDamageInfo info = inputInfo;
// check for helmet shot
if (ptr->hitgroup == 11)
{
// make sure we're wearing one
if ( GetBodygroup( 1 ) == HEAD_GRUNT && (info.GetDamageType() & (DMG_BULLET | DMG_SLASH | DMG_BLAST | DMG_CLUB)))
{
// absorb damage
info.SetDamage( info.GetDamage() - 20 );
if ( info.GetDamage() <= 0 )
info.SetDamage( 0.01 );
}
// it's head shot anyways
ptr->hitgroup = HITGROUP_HEAD;
}
BaseClass::TraceAttack( info, vecDir, ptr, pAccumulator );
}
//=========================================================
// TakeDamage - overridden for the grunt because the grunt
// needs to forget that he is in cover if he's hurt. (Obviously
// not in a safe place anymore).
//=========================================================
int CNPC_HGrunt::OnTakeDamage_Alive( const CTakeDamageInfo &inputInfo )
{
Forget( bits_MEMORY_INCOVER );
return BaseClass::OnTakeDamage_Alive ( inputInfo );
}
//=========================================================
// SetYawSpeed - allows each sequence to have a different
// turn rate associated with it.
//=========================================================
float CNPC_HGrunt::MaxYawSpeed( void )
{
float flYS;
switch ( GetActivity() )
{
case ACT_IDLE:
flYS = 150;
break;
case ACT_RUN:
flYS = 150;
break;
case ACT_WALK:
flYS = 180;
break;
case ACT_RANGE_ATTACK1:
flYS = 120;
break;
case ACT_RANGE_ATTACK2:
flYS = 120;
break;
case ACT_MELEE_ATTACK1:
flYS = 120;
break;
case ACT_MELEE_ATTACK2:
flYS = 120;
break;
case ACT_TURN_LEFT:
case ACT_TURN_RIGHT:
flYS = 180;
break;
case ACT_GLIDE:
case ACT_FLY:
flYS = 30;
break;
default:
flYS = 90;
break;
}
// Yaw speed is handled differently now!
return flYS * 0.5f;
}
void CNPC_HGrunt::IdleSound( void )
{
if (FOkToSpeak() && ( g_fGruntQuestion || random->RandomInt( 0,1 ) ) )
{
if (!g_fGruntQuestion)
{
// ask question or make statement
switch ( random->RandomInt( 0,2 ) )
{
case 0: // check in
SENTENCEG_PlayRndSz( edict(), "HG_CHECK", HGRUNT_SENTENCE_VOLUME, SNDLVL_NORM, 0, m_voicePitch);
g_fGruntQuestion = 1;
break;
case 1: // question
SENTENCEG_PlayRndSz( edict(), "HG_QUEST", HGRUNT_SENTENCE_VOLUME, SNDLVL_NORM, 0, m_voicePitch);
g_fGruntQuestion = 2;
break;
case 2: // statement
SENTENCEG_PlayRndSz( edict(), "HG_IDLE", HGRUNT_SENTENCE_VOLUME, SNDLVL_NORM, 0, m_voicePitch);
break;
}
}
else
{
switch (g_fGruntQuestion)
{
case 1: // check in
SENTENCEG_PlayRndSz( edict(), "HG_CLEAR", HGRUNT_SENTENCE_VOLUME, SNDLVL_NORM, 0, m_voicePitch);
break;
case 2: // question
SENTENCEG_PlayRndSz( edict(), "HG_ANSWER", HGRUNT_SENTENCE_VOLUME, SNDLVL_NORM, 0, m_voicePitch);
break;
}
g_fGruntQuestion = 0;
}
JustSpoke();
}
}
bool CNPC_HGrunt::HandleInteraction(int interactionType, void *data, CBaseCombatCharacter* sourceEnt)
{
if (interactionType == g_interactionBarnacleVictimDangle)
{
// Force choosing of a new schedule
ClearSchedule( "Soldier being eaten by a barnacle" );
m_bInBarnacleMouth = true;
return true;
}
else if ( interactionType == g_interactionBarnacleVictimReleased )
{
SetState ( NPC_STATE_IDLE );
m_bInBarnacleMouth = false;
SetAbsVelocity( vec3_origin );
SetMoveType( MOVETYPE_STEP );
return true;
}
else if ( interactionType == g_interactionBarnacleVictimGrab )
{
if ( GetFlags() & FL_ONGROUND )
{
SetGroundEntity( NULL );
}
//Maybe this will break something else.
if ( GetState() == NPC_STATE_SCRIPT )
{
m_hCine->CancelScript();
ClearSchedule( "Soldier grabbed by a barnacle" );
}
SetState( NPC_STATE_PRONE );