/
npc_BaseZombie.cpp
3023 lines (2497 loc) · 84 KB
/
npc_BaseZombie.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: Implements the zombie, a horrific once-human headcrab victim.
//
// The zombie has two main states: Full and Torso.
//
// In Full state, the zombie is whole and walks upright as he did in Half-Life.
// He will try to claw the player and swat physics items at him.
//
// In Torso state, the zombie has been blasted or cut in half, and the Torso will
// drag itself along the ground with its arms. It will try to claw the player.
//
// In either state, a severely injured Zombie will release its headcrab, which
// will immediately go after the player. The Zombie will then die (ragdoll).
//
//=============================================================================//
#include "cbase.h"
#include "npc_BaseZombie.h"
#include "player.h"
#include "game.h"
#include "ai_network.h"
#include "ai_navigator.h"
#include "ai_motor.h"
#include "ai_default.h"
#include "ai_schedule.h"
#include "ai_hull.h"
#include "ai_node.h"
#include "ai_memory.h"
#include "ai_senses.h"
#include "bitstring.h"
#include "EntityFlame.h"
#include "hl2_shareddefs.h"
#include "npcevent.h"
#include "activitylist.h"
#include "entitylist.h"
#include "gib.h"
#include "soundenvelope.h"
#include "ndebugoverlay.h"
#include "rope.h"
#include "rope_shared.h"
#include "igamesystem.h"
#include "vstdlib/random.h"
#include "engine/IEngineSound.h"
#include "props.h"
#include "hl2_gamerules.h"
#include "weapon_physcannon.h"
#include "ammodef.h"
#include "vehicle_base.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
extern ConVar sk_npc_head;
#define ZOMBIE_BULLET_DAMAGE_SCALE 0.5f
int g_interactionZombieMeleeWarning;
envelopePoint_t envDefaultZombieMoanVolumeFast[] =
{
{ 1.0f, 1.0f,
0.1f, 0.1f,
},
{ 0.0f, 0.0f,
0.2f, 0.3f,
},
};
envelopePoint_t envDefaultZombieMoanVolume[] =
{
{ 1.0f, 0.1f,
0.1f, 0.1f,
},
{ 1.0f, 1.0f,
0.2f, 0.2f,
},
{ 0.0f, 0.0f,
0.3f, 0.4f,
},
};
// if the zombie doesn't find anything closer than this, it doesn't swat.
#define ZOMBIE_FARTHEST_PHYSICS_OBJECT 40.0*12.0
#define ZOMBIE_PHYSICS_SEARCH_DEPTH 100
// Don't swat objects unless player is closer than this.
#define ZOMBIE_PLAYER_MAX_SWAT_DIST 1000
//
// How much health a Zombie torso gets when a whole zombie is broken
// It's whole zombie's MAX Health * this value
#define ZOMBIE_TORSO_HEALTH_FACTOR 0.5
//
// When the zombie has health < m_iMaxHealth * this value, it will
// try to release its headcrab.
#define ZOMBIE_RELEASE_HEALTH_FACTOR 0.5
//
// The heaviest physics object that a zombie should try to swat. (kg)
#define ZOMBIE_MAX_PHYSOBJ_MASS 60
//
// Zombie tries to get this close to a physics object's origin to swat it
#define ZOMBIE_PHYSOBJ_SWATDIST 80
//
// Because movement code sometimes doesn't get us QUITE where we
// want to go, the zombie tries to get this close to a physics object
// Zombie will end up somewhere between PHYSOBJ_MOVE_TO_DIST & PHYSOBJ_SWATDIST
#define ZOMBIE_PHYSOBJ_MOVE_TO_DIST 48
//
// How long between physics swat attacks (in seconds).
#define ZOMBIE_SWAT_DELAY 5
//
// After taking damage, ignore further damage for n seconds. This keeps the zombie
// from being interrupted while.
//
#define ZOMBIE_FLINCH_DELAY 3
#define ZOMBIE_BURN_TIME 10 // If ignited, burn for this many seconds
#define ZOMBIE_BURN_TIME_NOISE 2 // Give or take this many seconds.
//=========================================================
// private activities
//=========================================================
int CNPC_BaseZombie::ACT_ZOM_SWATLEFTMID;
int CNPC_BaseZombie::ACT_ZOM_SWATRIGHTMID;
int CNPC_BaseZombie::ACT_ZOM_SWATLEFTLOW;
int CNPC_BaseZombie::ACT_ZOM_SWATRIGHTLOW;
int CNPC_BaseZombie::ACT_ZOM_RELEASECRAB;
int CNPC_BaseZombie::ACT_ZOM_FALL;
ConVar sk_zombie_dmg_one_slash( "sk_zombie_dmg_one_slash","0");
ConVar sk_zombie_dmg_both_slash( "sk_zombie_dmg_both_slash","0");
// When a zombie spawns, he will select a 'base' pitch value
// that's somewhere between basepitchmin & basepitchmax
ConVar zombie_basemin( "zombie_basemin", "100" );
ConVar zombie_basemax( "zombie_basemax", "100" );
ConVar zombie_changemin( "zombie_changemin", "0" );
ConVar zombie_changemax( "zombie_changemax", "0" );
// play a sound once in every zombie_stepfreq steps
ConVar zombie_stepfreq( "zombie_stepfreq", "4" );
ConVar zombie_moanfreq( "zombie_moanfreq", "1" );
ConVar zombie_decaymin( "zombie_decaymin", "0.1" );
ConVar zombie_decaymax( "zombie_decaymax", "0.4" );
ConVar zombie_ambushdist( "zombie_ambushdist", "16000" );
//=========================================================
// For a couple of reasons, we keep a running count of how
// many zombies in the world are angry at any given time.
//=========================================================
static int s_iAngryZombies = 0;
//=========================================================
//=========================================================
class CAngryZombieCounter : public CAutoGameSystem
{
public:
CAngryZombieCounter( char const *name ) : CAutoGameSystem( name )
{
}
// Level init, shutdown
virtual void LevelInitPreEntity()
{
s_iAngryZombies = 0;
}
};
CAngryZombieCounter AngryZombieCounter( "CAngryZombieCounter" );
int AE_ZOMBIE_ATTACK_RIGHT;
int AE_ZOMBIE_ATTACK_LEFT;
int AE_ZOMBIE_ATTACK_BOTH;
int AE_ZOMBIE_SWATITEM;
int AE_ZOMBIE_STARTSWAT;
int AE_ZOMBIE_STEP_LEFT;
int AE_ZOMBIE_STEP_RIGHT;
int AE_ZOMBIE_SCUFF_LEFT;
int AE_ZOMBIE_SCUFF_RIGHT;
int AE_ZOMBIE_ATTACK_SCREAM;
int AE_ZOMBIE_GET_UP;
int AE_ZOMBIE_POUND;
int AE_ZOMBIE_ALERTSOUND;
int AE_ZOMBIE_POPHEADCRAB;
//=========================================================
//=========================================================
BEGIN_DATADESC( CNPC_BaseZombie )
DEFINE_SOUNDPATCH( m_pMoanSound ),
DEFINE_FIELD( m_fIsTorso, FIELD_BOOLEAN ),
DEFINE_FIELD( m_fIsHeadless, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flNextFlinch, FIELD_TIME ),
DEFINE_FIELD( m_bHeadShot, FIELD_BOOLEAN ),
DEFINE_FIELD( m_flBurnDamage, FIELD_FLOAT ),
DEFINE_FIELD( m_flBurnDamageResetTime, FIELD_TIME ),
DEFINE_FIELD( m_hPhysicsEnt, FIELD_EHANDLE ),
DEFINE_FIELD( m_flNextMoanSound, FIELD_TIME ),
DEFINE_FIELD( m_flNextSwat, FIELD_TIME ),
DEFINE_FIELD( m_flNextSwatScan, FIELD_TIME ),
DEFINE_FIELD( m_crabHealth, FIELD_FLOAT ),
DEFINE_FIELD( m_flMoanPitch, FIELD_FLOAT ),
DEFINE_FIELD( m_iMoanSound, FIELD_INTEGER ),
DEFINE_FIELD( m_hObstructor, FIELD_EHANDLE ),
DEFINE_FIELD( m_bIsSlumped, FIELD_BOOLEAN ),
END_DATADESC()
//LINK_ENTITY_TO_CLASS( base_zombie, CNPC_BaseZombie );
//---------------------------------------------------------
//---------------------------------------------------------
int CNPC_BaseZombie::g_numZombies = 0;
//---------------------------------------------------------
//---------------------------------------------------------
CNPC_BaseZombie::CNPC_BaseZombie()
{
// Gotta select which sound we're going to play, right here!
// Because everyone's constructed before they spawn.
//
// Assign moan sounds in order, over and over.
// This means if 3 or so zombies spawn near each
// other, they will definitely not pick the same
// moan loop.
m_iMoanSound = g_numZombies;
g_numZombies++;
}
//---------------------------------------------------------
//---------------------------------------------------------
CNPC_BaseZombie::~CNPC_BaseZombie()
{
g_numZombies--;
}
//---------------------------------------------------------
// The closest physics object is chosen that is:
// <= MaxMass in Mass
// Between the zombie and the enemy
// not too far from a direct line to the enemy.
//---------------------------------------------------------
bool CNPC_BaseZombie::FindNearestPhysicsObject( int iMaxMass )
{
CBaseEntity *pList[ ZOMBIE_PHYSICS_SEARCH_DEPTH ];
CBaseEntity *pNearest = NULL;
float flDist;
IPhysicsObject *pPhysObj;
int i;
Vector vecDirToEnemy;
Vector vecDirToObject;
if ( !CanSwatPhysicsObjects() || !GetEnemy() )
{
// Can't swat, or no enemy, so no swat.
m_hPhysicsEnt = NULL;
return false;
}
vecDirToEnemy = GetEnemy()->GetAbsOrigin() - GetAbsOrigin();
float dist = VectorNormalize(vecDirToEnemy);
vecDirToEnemy.z = 0;
if( dist > ZOMBIE_PLAYER_MAX_SWAT_DIST )
{
// Player is too far away. Don't bother
// trying to swat anything at them until
// they are closer.
return false;
}
float flNearestDist = MIN( dist, ZOMBIE_FARTHEST_PHYSICS_OBJECT * 0.5 );
Vector vecDelta( flNearestDist, flNearestDist, GetHullHeight() * 2.0 );
class CZombieSwatEntitiesEnum : public CFlaggedEntitiesEnum
{
public:
CZombieSwatEntitiesEnum( CBaseEntity **pList, int listMax, int iMaxMass )
: CFlaggedEntitiesEnum( pList, listMax, 0 ),
m_iMaxMass( iMaxMass )
{
}
virtual IterationRetval_t EnumElement( IHandleEntity *pHandleEntity )
{
CBaseEntity *pEntity = gEntList.GetBaseEntity( pHandleEntity->GetRefEHandle() );
if ( pEntity &&
pEntity->VPhysicsGetObject() &&
pEntity->VPhysicsGetObject()->GetMass() <= m_iMaxMass &&
pEntity->VPhysicsGetObject()->IsAsleep() &&
pEntity->VPhysicsGetObject()->IsMoveable() )
{
return CFlaggedEntitiesEnum::EnumElement( pHandleEntity );
}
return ITERATION_CONTINUE;
}
int m_iMaxMass;
};
CZombieSwatEntitiesEnum swatEnum( pList, ZOMBIE_PHYSICS_SEARCH_DEPTH, iMaxMass );
int count = UTIL_EntitiesInBox( GetAbsOrigin() - vecDelta, GetAbsOrigin() + vecDelta, &swatEnum );
// magically know where they are
Vector vecZombieKnees;
CollisionProp()->NormalizedToWorldSpace( Vector( 0.5f, 0.5f, 0.25f ), &vecZombieKnees );
for( i = 0 ; i < count ; i++ )
{
pPhysObj = pList[ i ]->VPhysicsGetObject();
Assert( !( !pPhysObj || pPhysObj->GetMass() > iMaxMass || !pPhysObj->IsAsleep() ) );
Vector center = pList[ i ]->WorldSpaceCenter();
flDist = UTIL_DistApprox2D( GetAbsOrigin(), center );
if( flDist >= flNearestDist )
continue;
// This object is closer... but is it between the player and the zombie?
vecDirToObject = pList[ i ]->WorldSpaceCenter() - GetAbsOrigin();
VectorNormalize(vecDirToObject);
vecDirToObject.z = 0;
if( DotProduct( vecDirToEnemy, vecDirToObject ) < 0.8 )
continue;
if( flDist >= UTIL_DistApprox2D( center, GetEnemy()->GetAbsOrigin() ) )
continue;
// don't swat things where the highest point is under my knees
// NOTE: This is a rough test; a more exact test is going to occur below
if ( (center.z + pList[i]->BoundingRadius()) < vecZombieKnees.z )
continue;
// don't swat things that are over my head.
if( center.z > EyePosition().z )
continue;
vcollide_t *pCollide = modelinfo->GetVCollide( pList[i]->GetModelIndex() );
Vector objMins, objMaxs;
physcollision->CollideGetAABB( &objMins, &objMaxs, pCollide->solids[0], pList[i]->GetAbsOrigin(), pList[i]->GetAbsAngles() );
if ( objMaxs.z < vecZombieKnees.z )
continue;
if ( !FVisible( pList[i] ) )
continue;
if ( hl2_episodic.GetBool() )
{
// Skip things that the enemy can't see. Do we want this as a general thing?
// The case for this feature is that zombies who are pursuing the player will
// stop along the way to swat objects at the player who is around the corner or
// otherwise not in a place that the object has a hope of hitting. This diversion
// makes the zombies very late (in a random fashion) getting where they are going. (sjb 1/2/06)
if( !GetEnemy()->FVisible( pList[i] ) )
continue;
}
// Make this the last check, since it makes a string.
// Don't swat server ragdolls!
if ( FClassnameIs( pList[ i ], "physics_prop_ragdoll" ) )
continue;
if ( FClassnameIs( pList[ i ], "prop_ragdoll" ) )
continue;
// The object must also be closer to the zombie than it is to the enemy
pNearest = pList[ i ];
flNearestDist = flDist;
}
m_hPhysicsEnt = pNearest;
if( m_hPhysicsEnt == NULL )
{
return false;
}
else
{
return true;
}
}
//-----------------------------------------------------------------------------
// Purpose: Returns this monster's place in the relationship table.
//-----------------------------------------------------------------------------
Class_T CNPC_BaseZombie::Classify( void )
{
if ( IsSlumped() )
return CLASS_NONE;
return( CLASS_ZOMBIE );
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
Disposition_t CNPC_BaseZombie::IRelationType( CBaseEntity *pTarget )
{
// Slumping should not affect Zombie's opinion of others
if ( IsSlumped() )
{
m_bIsSlumped = false;
Disposition_t result = BaseClass::IRelationType( pTarget );
m_bIsSlumped = true;
return result;
}
return BaseClass::IRelationType( pTarget );
}
//-----------------------------------------------------------------------------
// Purpose: Returns the maximum yaw speed based on the monster's current activity.
//-----------------------------------------------------------------------------
float CNPC_BaseZombie::MaxYawSpeed( void )
{
if( m_fIsTorso )
{
return( 60 );
}
else if (IsMoving() && HasPoseParameter( GetSequence(), m_poseMove_Yaw ))
{
return( 15 );
}
else
{
switch( GetActivity() )
{
case ACT_TURN_LEFT:
case ACT_TURN_RIGHT:
return 100;
break;
case ACT_RUN:
return 15;
break;
case ACT_WALK:
case ACT_IDLE:
return 25;
break;
case ACT_RANGE_ATTACK1:
case ACT_RANGE_ATTACK2:
case ACT_MELEE_ATTACK1:
case ACT_MELEE_ATTACK2:
return 120;
default:
return 90;
break;
}
}
}
//-----------------------------------------------------------------------------
// Purpose: turn in the direction of movement
// Output :
//-----------------------------------------------------------------------------
bool CNPC_BaseZombie::OverrideMoveFacing( const AILocalMoveGoal_t &move, float flInterval )
{
if (!HasPoseParameter( GetSequence(), m_poseMove_Yaw ))
{
return BaseClass::OverrideMoveFacing( move, flInterval );
}
// required movement direction
float flMoveYaw = UTIL_VecToYaw( move.dir );
float idealYaw = UTIL_AngleMod( flMoveYaw );
if (GetEnemy())
{
float flEDist = UTIL_DistApprox2D( WorldSpaceCenter(), GetEnemy()->WorldSpaceCenter() );
if (flEDist < 256.0)
{
float flEYaw = UTIL_VecToYaw( GetEnemy()->WorldSpaceCenter() - WorldSpaceCenter() );
if (flEDist < 128.0)
{
idealYaw = flEYaw;
}
else
{
idealYaw = flMoveYaw + UTIL_AngleDiff( flEYaw, flMoveYaw ) * (2 - flEDist / 128.0);
}
//DevMsg("was %.0f now %.0f\n", flMoveYaw, idealYaw );
}
}
GetMotor()->SetIdealYawAndUpdate( idealYaw );
// find movement direction to compensate for not being turned far enough
float fSequenceMoveYaw = GetSequenceMoveYaw( GetSequence() );
float flDiff = UTIL_AngleDiff( flMoveYaw, GetLocalAngles().y + fSequenceMoveYaw );
SetPoseParameter( m_poseMove_Yaw, GetPoseParameter( m_poseMove_Yaw ) + flDiff );
return true;
}
//-----------------------------------------------------------------------------
// Purpose: For innate melee attack
// Input :
// Output :
//-----------------------------------------------------------------------------
int CNPC_BaseZombie::MeleeAttack1Conditions ( float flDot, float flDist )
{
float range = GetClawAttackRange();
if (flDist > range )
{
// Translate a hit vehicle into its passenger if found
if ( GetEnemy() != NULL )
{
#if defined(HL2_DLL) && !defined(HL2MP)
// If the player is holding an object, knock it down.
if( GetEnemy()->IsPlayer() )
{
CBasePlayer *pPlayer = ToBasePlayer( GetEnemy() );
Assert( pPlayer != NULL );
// Is the player carrying something?
CBaseEntity *pObject = GetPlayerHeldEntity(pPlayer);
if( !pObject )
{
pObject = PhysCannonGetHeldEntity( pPlayer->GetActiveWeapon() );
}
if( pObject )
{
float flDist = pObject->WorldSpaceCenter().DistTo( WorldSpaceCenter() );
if( flDist <= GetClawAttackRange() )
return COND_CAN_MELEE_ATTACK1;
}
}
#endif
}
return COND_TOO_FAR_TO_ATTACK;
}
if (flDot < 0.7)
{
return COND_NOT_FACING_ATTACK;
}
// Build a cube-shaped hull, the same hull that ClawAttack() is going to use.
Vector vecMins = GetHullMins();
Vector vecMaxs = GetHullMaxs();
vecMins.z = vecMins.x;
vecMaxs.z = vecMaxs.x;
Vector forward;
GetVectors( &forward, NULL, NULL );
trace_t tr;
CTraceFilterNav traceFilter( this, false, this, COLLISION_GROUP_NONE );
AI_TraceHull( WorldSpaceCenter(), WorldSpaceCenter() + forward * GetClawAttackRange(), vecMins, vecMaxs, MASK_NPCSOLID, &traceFilter, &tr );
if( tr.fraction == 1.0 || !tr.m_pEnt )
{
#ifdef HL2_EPISODIC
// If our trace was unobstructed but we were shooting
if ( GetEnemy() && GetEnemy()->Classify() == CLASS_BULLSEYE )
return COND_CAN_MELEE_ATTACK1;
#endif // HL2_EPISODIC
// This attack would miss completely. Trick the zombie into moving around some more.
return COND_TOO_FAR_TO_ATTACK;
}
if( tr.m_pEnt == GetEnemy() ||
tr.m_pEnt->IsNPC() ||
( tr.m_pEnt->m_takedamage == DAMAGE_YES && (dynamic_cast<CBreakableProp*>(tr.m_pEnt) ) ) )
{
// -Let the zombie swipe at his enemy if he's going to hit them.
// -Also let him swipe at NPC's that happen to be between the zombie and the enemy.
// This makes mobs of zombies seem more rowdy since it doesn't leave guys in the back row standing around.
// -Also let him swipe at things that takedamage, under the assumptions that they can be broken.
return COND_CAN_MELEE_ATTACK1;
}
Vector vecTrace = tr.endpos - tr.startpos;
float lenTraceSq = vecTrace.Length2DSqr();
if ( GetEnemy() && GetEnemy()->MyCombatCharacterPointer() && tr.m_pEnt == static_cast<CBaseCombatCharacter *>(GetEnemy())->GetVehicleEntity() )
{
if ( lenTraceSq < Square( GetClawAttackRange() * 0.75f ) )
{
return COND_CAN_MELEE_ATTACK1;
}
}
if( tr.m_pEnt->IsBSPModel() )
{
// The trace hit something solid, but it's not the enemy. If this item is closer to the zombie than
// the enemy is, treat this as an obstruction.
Vector vecToEnemy = GetEnemy()->WorldSpaceCenter() - WorldSpaceCenter();
if( lenTraceSq < vecToEnemy.Length2DSqr() )
{
return COND_ZOMBIE_LOCAL_MELEE_OBSTRUCTION;
}
}
#ifdef HL2_EPISODIC
if ( !tr.m_pEnt->IsWorld() && GetEnemy() && GetEnemy()->GetGroundEntity() == tr.m_pEnt )
{
//Try to swat whatever the player is standing on instead of acting like a dill.
return COND_CAN_MELEE_ATTACK1;
}
// Bullseyes are given some grace on if they can be hit
if ( GetEnemy() && GetEnemy()->Classify() == CLASS_BULLSEYE )
return COND_CAN_MELEE_ATTACK1;
#endif // HL2_EPISODIC
// Move around some more
return COND_TOO_FAR_TO_ATTACK;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#define ZOMBIE_BUCKSHOT_TRIPLE_DAMAGE_DIST 96.0f // Triple damage from buckshot at 8 feet (headshot only)
float CNPC_BaseZombie::GetHitgroupDamageMultiplier( int iHitGroup, const CTakeDamageInfo &info )
{
switch( iHitGroup )
{
case HITGROUP_HEAD:
{
if( info.GetDamageType() & DMG_BUCKSHOT )
{
float flDist = FLT_MAX;
if( info.GetAttacker() )
{
flDist = ( GetAbsOrigin() - info.GetAttacker()->GetAbsOrigin() ).Length();
}
if( flDist <= ZOMBIE_BUCKSHOT_TRIPLE_DAMAGE_DIST )
{
return 3.0f;
}
}
else
{
return 2.0f;
}
}
}
return BaseClass::GetHitgroupDamageMultiplier( iHitGroup, info );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CNPC_BaseZombie::TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator )
{
CTakeDamageInfo infoCopy = info;
// Keep track of headshots so we can determine whether to pop off our headcrab.
if (ptr->hitgroup == HITGROUP_HEAD)
{
m_bHeadShot = true;
}
if( infoCopy.GetDamageType() & DMG_BUCKSHOT )
{
// Zombie gets across-the-board damage reduction for buckshot. This compensates for the recent changes which
// make the shotgun much more powerful, and returns the zombies to a level that has been playtested extensively.(sjb)
// This normalizes the buckshot damage to what it used to be on normal (5 dmg per pellet. Now it's 8 dmg per pellet).
infoCopy.ScaleDamage( 0.625 );
}
BaseClass::TraceAttack( infoCopy, vecDir, ptr, pAccumulator );
}
//-----------------------------------------------------------------------------
// Purpose: A zombie has taken damage. Determine whether he should split in half
// Input :
// Output : bool, true if yes.
//-----------------------------------------------------------------------------
bool CNPC_BaseZombie::ShouldBecomeTorso( const CTakeDamageInfo &info, float flDamageThreshold )
{
if ( info.GetDamageType() & DMG_REMOVENORAGDOLL )
return false;
if ( m_fIsTorso )
{
// Already split.
return false;
}
// Not if we're in a dss
if ( IsRunningDynamicInteraction() )
return false;
// Break in half IF:
//
// Take half or more of max health in DMG_BLAST
if( (info.GetDamageType() & DMG_BLAST) && flDamageThreshold >= 0.5 )
{
return true;
}
if ( hl2_episodic.GetBool() )
{
// Always split after a cannon hit
if ( info.GetAmmoType() == GetAmmoDef()->Index("CombineHeavyCannon") )
return true;
}
#if 0
if( info.GetDamageType() & DMG_BUCKSHOT )
{
if( m_iHealth <= 0 || flDamageThreshold >= 0.5 )
{
return true;
}
}
#endif
return false;
}
//-----------------------------------------------------------------------------
// Purpose: A zombie has taken damage. Determine whether he release his headcrab.
// Output : YES, IMMEDIATE, or SCHEDULED (see HeadcrabRelease_t)
//-----------------------------------------------------------------------------
HeadcrabRelease_t CNPC_BaseZombie::ShouldReleaseHeadcrab( const CTakeDamageInfo &info, float flDamageThreshold )
{
if ( m_iHealth <= 0 )
{
if ( info.GetDamageType() & DMG_REMOVENORAGDOLL )
return RELEASE_NO;
if ( info.GetDamageType() & DMG_SNIPER )
return RELEASE_RAGDOLL;
// If I was killed by a bullet...
if ( info.GetDamageType() & DMG_BULLET )
{
if( m_bHeadShot )
{
if( flDamageThreshold > 0.25 )
{
// Enough force to kill the crab.
return RELEASE_RAGDOLL;
}
}
else
{
// Killed by a shot to body or something. Crab is ok!
return RELEASE_IMMEDIATE;
}
}
// If I was killed by an explosion, release the crab.
if ( info.GetDamageType() & DMG_BLAST )
{
return RELEASE_RAGDOLL;
}
if ( m_fIsTorso && IsChopped( info ) )
{
return RELEASE_RAGDOLL_SLICED_OFF;
}
}
return RELEASE_NO;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : pInflictor -
// pAttacker -
// flDamage -
// bitsDamageType -
// Output : int
//-----------------------------------------------------------------------------
#define ZOMBIE_SCORCH_RATE 8
#define ZOMBIE_MIN_RENDERCOLOR 50
int CNPC_BaseZombie::OnTakeDamage_Alive( const CTakeDamageInfo &inputInfo )
{
CTakeDamageInfo info = inputInfo;
if( inputInfo.GetDamageType() & DMG_BURN )
{
// If a zombie is on fire it only takes damage from the fire that's attached to it. (DMG_DIRECT)
// This is to stop zombies from burning to death 10x faster when they're standing around
// 10 fire entities.
if( IsOnFire() && !(inputInfo.GetDamageType() & DMG_DIRECT) )
{
return 0;
}
Scorch( ZOMBIE_SCORCH_RATE, ZOMBIE_MIN_RENDERCOLOR );
}
// Take some percentage of damage from bullets (unless hit in the crab). Always take full buckshot & sniper damage
if ( !m_bHeadShot && (info.GetDamageType() & DMG_BULLET) && !(info.GetDamageType() & (DMG_BUCKSHOT|DMG_SNIPER)) )
{
info.ScaleDamage( ZOMBIE_BULLET_DAMAGE_SCALE );
}
if ( ShouldIgnite( info ) )
{
Ignite( 100.0f );
}
int tookDamage = BaseClass::OnTakeDamage_Alive( info );
// flDamageThreshold is what percentage of the creature's max health
// this amount of damage represents. (clips at 1.0)
float flDamageThreshold = MIN( 1, info.GetDamage() / m_iMaxHealth );
// Being chopped up by a sharp physics object is a pretty special case
// so we handle it with some special code. Mainly for
// Ravenholm's helicopter traps right now (sjb).
bool bChopped = IsChopped(info);
bool bSquashed = IsSquashed(info);
bool bKilledByVehicle = ( ( info.GetDamageType() & DMG_VEHICLE ) != 0 );
if( !m_fIsTorso && (bChopped || bSquashed) && !bKilledByVehicle && !(info.GetDamageType() & DMG_REMOVENORAGDOLL) )
{
if( bChopped )
{
EmitSound( "E3_Phystown.Slicer" );
}
DieChopped( info );
}
else
{
HeadcrabRelease_t release = ShouldReleaseHeadcrab( info, flDamageThreshold );
switch( release )
{
case RELEASE_IMMEDIATE:
ReleaseHeadcrab( EyePosition(), vec3_origin, true, true );
break;
case RELEASE_RAGDOLL:
// Go a little easy on headcrab ragdoll force. They're light!
ReleaseHeadcrab( EyePosition(), inputInfo.GetDamageForce() * 0.25, true, false, true );
break;
case RELEASE_RAGDOLL_SLICED_OFF:
{
EmitSound( "E3_Phystown.Slicer" );
Vector vecForce = inputInfo.GetDamageForce() * 0.1;
vecForce += Vector( 0, 0, 2000.0 );
ReleaseHeadcrab( EyePosition(), vecForce, true, false, true );
}
break;
case RELEASE_VAPORIZE:
RemoveHead();
break;
case RELEASE_SCHEDULED:
SetCondition( COND_ZOMBIE_RELEASECRAB );
break;
}
if( ShouldBecomeTorso( info, flDamageThreshold ) )
{
bool bHitByCombineCannon = (inputInfo.GetAmmoType() == GetAmmoDef()->Index("CombineHeavyCannon"));
if ( CanBecomeLiveTorso() )
{
BecomeTorso( vec3_origin, inputInfo.GetDamageForce() * 0.50 );
if ( ( info.GetDamageType() & DMG_BLAST) && random->RandomInt( 0, 1 ) == 0 )
{
Ignite( 5.0 + random->RandomFloat( 0.0, 5.0 ) );
}
// For Combine cannon impacts
if ( hl2_episodic.GetBool() )
{
if ( bHitByCombineCannon )
{
// Catch on fire.
Ignite( 5.0f + random->RandomFloat( 0.0f, 5.0f ) );
}
}
if (flDamageThreshold >= 1.0)
{
m_iHealth = 0;
BecomeRagdollOnClient( info.GetDamageForce() );
}
}
else if ( random->RandomInt(1, 3) == 1 )
DieChopped( info );
}
}
if( tookDamage > 0 && (info.GetDamageType() & (DMG_BURN|DMG_DIRECT)) && m_ActBusyBehavior.IsActive() )
{
//!!!HACKHACK- Stuff a light_damage condition if an actbusying zombie takes direct burn damage. This will cause an
// ignited zombie to 'wake up' and rise out of its actbusy slump. (sjb)
SetCondition( COND_LIGHT_DAMAGE );
}
// IMPORTANT: always clear the headshot flag after applying damage. No early outs!
m_bHeadShot = false;
return tookDamage;
}
//-----------------------------------------------------------------------------
// Purpose: make a sound Alyx can hear when in darkness mode
// Input : volume (radius) of the sound.
// Output :
//-----------------------------------------------------------------------------
void CNPC_BaseZombie::MakeAISpookySound( float volume, float duration )
{
#ifdef HL2_EPISODIC
if ( HL2GameRules()->IsAlyxInDarknessMode() )
{
CSoundEnt::InsertSound( SOUND_COMBAT, EyePosition(), volume, duration, this, SOUNDENT_CHANNEL_SPOOKY_NOISE );
}
#endif // HL2_EPISODIC
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
bool CNPC_BaseZombie::CanPlayMoanSound()
{
if( HasSpawnFlags( SF_NPC_GAG ) )
return false;
// Burning zombies play their moan loop at full volume for as long as they're
// burning. Don't let a moan envelope play cause it will turn the volume down when done.
if( IsOnFire() )
return false;
// Members of a small group of zombies can vocalize whenever they want
if( s_iAngryZombies <= 4 )
return true;
// This serves to limit the number of zombies that can moan at one time when there are a lot.
if( random->RandomInt( 1, zombie_moanfreq.GetInt() * (s_iAngryZombies/2) ) == 1 )
{
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Open a window and let a little bit of the looping moan sound
// come through.
//-----------------------------------------------------------------------------
void CNPC_BaseZombie::MoanSound( envelopePoint_t *pEnvelope, int iEnvelopeSize )
{
if( HasSpawnFlags( SF_NPC_GAG ) )
{
// Not yet!
return;
}
if( !m_pMoanSound )
{
// Don't set this up until the code calls for it.
const char *pszSound = GetMoanSound( m_iMoanSound );