forked from ValveSoftware/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathbasecombatcharacter.cpp
More file actions
4958 lines (4213 loc) · 146 KB
/
Copy pathbasecombatcharacter.cpp
File metadata and controls
4958 lines (4213 loc) · 146 KB
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: Base combat character with no AI
//
//=============================================================================//
#include "cbase.h"
#include "basecombatcharacter.h"
#include "basecombatweapon.h"
#include "animation.h"
#include "gib.h"
#include "entitylist.h"
#include "gamerules.h"
#include "ai_basenpc.h"
#include "ai_squadslot.h"
#include "ammodef.h"
#include "ndebugoverlay.h"
#include "player.h"
#include "physics.h"
#include "engine/IEngineSound.h"
#include "tier1/strtools.h"
#include "sendproxy.h"
#include "EntityFlame.h"
#include "CRagdollMagnet.h"
#include "IEffects.h"
#include "iservervehicle.h"
#include "igamesystem.h"
#include "globals.h"
#include "physics_prop_ragdoll.h"
#include "physics_impact_damage.h"
#include "saverestore_utlvector.h"
#include "eventqueue.h"
#include "world.h"
#include "globalstate.h"
#include "items.h"
#include "movevars_shared.h"
#include "RagdollBoogie.h"
#include "rumble_shared.h"
#include "saverestoretypes.h"
#include "nav_mesh.h"
#ifdef NEXT_BOT
#include "NextBot/NextBotManager.h"
#endif
#ifdef HL2_DLL
#include "weapon_physcannon.h"
#include "hl2_gamerules.h"
#endif
#ifdef PORTAL
#include "portal_util_shared.h"
#include "prop_portal_shared.h"
#include "portal_shareddefs.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef HL2_DLL
extern int g_interactionBarnacleVictimReleased;
#endif //HL2_DLL
extern ConVar weapon_showproficiency;
ConVar ai_show_hull_attacks( "ai_show_hull_attacks", "0" );
ConVar ai_force_serverside_ragdoll( "ai_force_serverside_ragdoll", "0" );
ConVar nb_last_area_update_tolerance( "nb_last_area_update_tolerance", "4.0", FCVAR_CHEAT, "Distance a character needs to travel in order to invalidate cached area" ); // 4.0 tested as sweet spot (for wanderers, at least). More resulted in little benefit, less quickly diminished benefit [7/31/2008 tom]
#ifdef MAPBASE
// ShouldUseVisibilityCache() is used as an actual function now
ConVar ai_use_visibility_cache( "ai_use_visibility_cache", "1" );
#else
#ifndef _RETAIL
ConVar ai_use_visibility_cache( "ai_use_visibility_cache", "1" );
#define ShouldUseVisibilityCache() ai_use_visibility_cache.GetBool()
#else
#define ShouldUseVisibilityCache() true
#endif
#endif
BEGIN_DATADESC( CBaseCombatCharacter )
#ifdef INVASION_DLL
DEFINE_FIELD( m_iPowerups, FIELD_INTEGER ),
DEFINE_ARRAY( m_flPowerupAttemptTimes, FIELD_TIME, MAX_POWERUPS ),
DEFINE_ARRAY( m_flPowerupEndTimes, FIELD_TIME, MAX_POWERUPS ),
DEFINE_FIELD( m_flFractionalBoost, FIELD_FLOAT ),
#endif
DEFINE_FIELD( m_flNextAttack, FIELD_TIME ),
DEFINE_FIELD( m_eHull, FIELD_INTEGER ),
DEFINE_FIELD( m_bloodColor, FIELD_INTEGER ),
DEFINE_FIELD( m_iDamageCount, FIELD_INTEGER ),
DEFINE_FIELD( m_flFieldOfView, FIELD_FLOAT ),
DEFINE_FIELD( m_HackedGunPos, FIELD_VECTOR ),
DEFINE_KEYFIELD( m_RelationshipString, FIELD_STRING, "Relationship" ),
DEFINE_FIELD( m_LastHitGroup, FIELD_INTEGER ),
DEFINE_FIELD( m_flDamageAccumulator, FIELD_FLOAT ),
DEFINE_INPUT( m_impactEnergyScale, FIELD_FLOAT, "physdamagescale" ),
DEFINE_FIELD( m_CurrentWeaponProficiency, FIELD_INTEGER),
#ifdef MAPBASE
DEFINE_INPUT( m_ProficiencyOverride, FIELD_INTEGER, "SetProficiencyOverride"),
#endif
DEFINE_UTLVECTOR( m_Relationship, FIELD_EMBEDDED),
DEFINE_AUTO_ARRAY( m_iAmmo, FIELD_INTEGER ),
DEFINE_AUTO_ARRAY( m_hMyWeapons, FIELD_EHANDLE ),
DEFINE_FIELD( m_hActiveWeapon, FIELD_EHANDLE ),
#ifdef MAPBASE
DEFINE_INPUT( m_bForceServerRagdoll, FIELD_BOOLEAN, "SetForceServerRagdoll" ),
#else
DEFINE_FIELD( m_bForceServerRagdoll, FIELD_BOOLEAN ),
#endif
DEFINE_FIELD( m_bPreventWeaponPickup, FIELD_BOOLEAN ),
#ifndef MAPBASE // See CBaseEntity::InputKilledNPC()
DEFINE_INPUTFUNC( FIELD_VOID, "KilledNPC", InputKilledNPC ),
#endif
#ifdef MAPBASE
DEFINE_INPUTFUNC( FIELD_INTEGER, "SetBloodColor", InputSetBloodColor ),
DEFINE_INPUTFUNC( FIELD_STRING, "SetRelationship", InputSetRelationship ),
DEFINE_INPUTFUNC( FIELD_VOID, "HolsterWeapon", InputHolsterWeapon ),
DEFINE_INPUTFUNC( FIELD_VOID, "HolsterAndDestroyWeapon", InputHolsterAndDestroyWeapon ),
DEFINE_INPUTFUNC( FIELD_STRING, "UnholsterWeapon", InputUnholsterWeapon ),
DEFINE_INPUTFUNC( FIELD_STRING, "SwitchToWeapon", InputSwitchToWeapon ),
DEFINE_INPUTFUNC( FIELD_STRING, "GiveWeapon", InputGiveWeapon ),
DEFINE_INPUTFUNC( FIELD_STRING, "DropWeapon", InputDropWeapon ),
DEFINE_INPUTFUNC( FIELD_EHANDLE, "PickupWeaponInstant", InputPickupWeaponInstant ),
DEFINE_OUTPUT( m_OnWeaponEquip, "OnWeaponEquip" ),
DEFINE_OUTPUT( m_OnWeaponDrop, "OnWeaponDrop" ),
DEFINE_OUTPUT( m_OnKilledEnemy, "OnKilledEnemy" ),
DEFINE_OUTPUT( m_OnKilledPlayer, "OnKilledPlayer" ),
DEFINE_OUTPUT( m_OnHealthChanged, "OnHealthChanged" ),
#endif
END_DATADESC()
#ifdef MAPBASE_VSCRIPT
ScriptHook_t CBaseCombatCharacter::g_Hook_RelationshipType;
ScriptHook_t CBaseCombatCharacter::g_Hook_RelationshipPriority;
BEGIN_ENT_SCRIPTDESC( CBaseCombatCharacter, CBaseFlex, "The base class shared by players and NPCs." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetActiveWeapon, "GetActiveWeapon", "Get the character's active weapon entity." )
DEFINE_SCRIPTFUNC( WeaponCount, "Get the number of weapons a character possesses." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetWeapon, "GetWeapon", "Get a specific weapon in the character's inventory." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetWeaponByType, "FindWeapon", "Find a specific weapon in the character's inventory by its classname." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetAllWeapons, "GetAllWeapons", "Get the character's weapon inventory." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetCurrentWeaponProficiency, "GetCurrentWeaponProficiency", "Get the character's current proficiency (accuracy) with their current weapon." )
DEFINE_SCRIPTFUNC_NAMED( Weapon_ShootPosition, "ShootPosition", "Get the character's shoot position." )
DEFINE_SCRIPTFUNC_NAMED( Weapon_DropAll, "DropAllWeapons", "Make the character drop all of its weapons." )
DEFINE_SCRIPTFUNC_NAMED( ScriptEquipWeapon, "EquipWeapon", "Make the character equip the specified weapon entity. If they don't already own the weapon, they will acquire it instantly." )
DEFINE_SCRIPTFUNC_NAMED( ScriptDropWeapon, "DropWeapon", "Make the character drop the specified weapon entity if they own it." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGiveAmmo, "GiveAmmo", "Gives the specified amount of the specified ammo type. The third parameter is whether or not to suppress the ammo pickup sound. Returns the amount of ammo actually given, which is 0 if the player's ammo for this type is already full." )
DEFINE_SCRIPTFUNC_NAMED( ScriptRemoveAmmo, "RemoveAmmo", "Removes the specified amount of the specified ammo type." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetAmmoCount, "GetAmmoCount", "Get the ammo count of the specified ammo type." )
DEFINE_SCRIPTFUNC_NAMED( ScriptSetAmmoCount, "SetAmmoCount", "Set the ammo count of the specified ammo type." )
DEFINE_SCRIPTFUNC( DoMuzzleFlash, "Does a muzzle flash." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetAttackSpread, "GetAttackSpread", "Get the attack spread." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetSpreadBias, "GetSpreadBias", "Get the spread bias." )
DEFINE_SCRIPTFUNC_NAMED( ScriptRelationType, "GetRelationship", "Get a character's relationship to a specific entity." )
DEFINE_SCRIPTFUNC_NAMED( ScriptRelationPriority, "GetRelationPriority", "Get a character's relationship priority for a specific entity." )
DEFINE_SCRIPTFUNC_NAMED( ScriptSetRelationship, "SetRelationship", "Set a character's relationship with a specific entity." )
DEFINE_SCRIPTFUNC_NAMED( ScriptSetClassRelationship, "SetClassRelationship", "Set a character's relationship with a specific Classify() class." )
DEFINE_SCRIPTFUNC_NAMED( ScriptGetVehicleEntity, "GetVehicleEntity", "Get the entity for a character's current vehicle if they're in one." )
DEFINE_SCRIPTFUNC_NAMED( ScriptInViewCone, "InViewCone", "Check if the specified position is in the character's viewcone." )
DEFINE_SCRIPTFUNC_NAMED( ScriptEntInViewCone, "EntInViewCone", "Check if the specified entity is in the character's viewcone." )
DEFINE_SCRIPTFUNC_NAMED( ScriptInAimCone, "InAimCone", "Check if the specified position is in the character's aim cone." )
DEFINE_SCRIPTFUNC_NAMED( ScriptEntInViewCone, "EntInAimCone", "Check if the specified entity is in the character's aim cone." )
DEFINE_SCRIPTFUNC_NAMED( ScriptBodyAngles, "BodyAngles", "Get the body's angles." )
DEFINE_SCRIPTFUNC( BodyDirection2D, "Get the body's 2D direction." )
DEFINE_SCRIPTFUNC( BodyDirection3D, "Get the body's 3D direction." )
DEFINE_SCRIPTFUNC( HeadDirection2D, "Get the head's 2D direction." )
DEFINE_SCRIPTFUNC( HeadDirection3D, "Get the head's 3D direction." )
DEFINE_SCRIPTFUNC( EyeDirection2D, "Get the eyes' 2D direction." )
DEFINE_SCRIPTFUNC( EyeDirection3D, "Get the eyes' 3D direction." )
DEFINE_SCRIPTFUNC( LastHitGroup, "Get the last hitgroup." )
#ifdef GLOWS_ENABLE
DEFINE_SCRIPTFUNC( AddGlowEffect, "" )
DEFINE_SCRIPTFUNC( RemoveGlowEffect, "" )
DEFINE_SCRIPTFUNC( IsGlowEffectActive, "" )
DEFINE_SCRIPTFUNC( SetGlowColor, "" )
#endif
//
// Hooks
//
BEGIN_SCRIPTHOOK( CBaseCombatCharacter::g_Hook_RelationshipType, "RelationshipType", FIELD_INTEGER, "Called when a character's relationship to another entity is requested. Returning a disposition will make the game use that disposition instead of the default relationship. (note: 'default' in this case includes overrides from ai_relationship/SetRelationship)" )
DEFINE_SCRIPTHOOK_PARAM( "entity", FIELD_HSCRIPT )
DEFINE_SCRIPTHOOK_PARAM( "def", FIELD_INTEGER )
END_SCRIPTHOOK()
BEGIN_SCRIPTHOOK( CBaseCombatCharacter::g_Hook_RelationshipPriority, "RelationshipPriority", FIELD_INTEGER, "Called when a character's relationship priority for another entity is requested. Returning a number will make the game use that priority instead of the default priority. (note: 'default' in this case includes overrides from ai_relationship/SetRelationship)" )
DEFINE_SCRIPTHOOK_PARAM( "entity", FIELD_HSCRIPT )
DEFINE_SCRIPTHOOK_PARAM( "def", FIELD_INTEGER )
END_SCRIPTHOOK()
END_SCRIPTDESC();
#endif
BEGIN_SIMPLE_DATADESC( Relationship_t )
DEFINE_FIELD( entity, FIELD_EHANDLE ),
DEFINE_FIELD( classType, FIELD_INTEGER ),
DEFINE_FIELD( disposition, FIELD_INTEGER ),
DEFINE_FIELD( priority, FIELD_INTEGER ),
END_DATADESC()
//-----------------------------------------------------------------------------
// Init static variables
//-----------------------------------------------------------------------------
int CBaseCombatCharacter::m_lastInteraction = 0;
Relationship_t** CBaseCombatCharacter::m_DefaultRelationship = NULL;
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
class CCleanupDefaultRelationShips : public CAutoGameSystem
{
public:
CCleanupDefaultRelationShips( char const *name ) : CAutoGameSystem( name )
{
}
virtual void Shutdown()
{
if ( !CBaseCombatCharacter::m_DefaultRelationship )
return;
for ( int i=0; i<NUM_AI_CLASSES; ++i )
{
delete[] CBaseCombatCharacter::m_DefaultRelationship[ i ];
}
delete[] CBaseCombatCharacter::m_DefaultRelationship;
CBaseCombatCharacter::m_DefaultRelationship = NULL;
}
};
static CCleanupDefaultRelationShips g_CleanupDefaultRelationships( "CCleanupDefaultRelationShips" );
void *SendProxy_SendBaseCombatCharacterLocalDataTable( const SendProp *pProp, const void *pStruct, const void *pVarData, CSendProxyRecipients *pRecipients, int objectID )
{
// Only send to local player if this is a player
pRecipients->ClearAllRecipients();
CBaseCombatCharacter *pBCC = ( CBaseCombatCharacter * )pStruct;
if ( pBCC != NULL)
{
if ( pBCC->IsPlayer() )
{
pRecipients->SetOnly( pBCC->entindex() - 1 );
}
else
{
// If it's a vehicle, send to "driver" (e.g., operator of tf2 manned guns)
IServerVehicle *pVehicle = pBCC->GetServerVehicle();
if ( pVehicle != NULL )
{
CBaseCombatCharacter *pDriver = pVehicle->GetPassenger();
if ( pDriver != NULL )
{
pRecipients->SetOnly( pDriver->entindex() - 1 );
}
}
}
}
return ( void * )pVarData;
}
REGISTER_SEND_PROXY_NON_MODIFIED_POINTER( SendProxy_SendBaseCombatCharacterLocalDataTable );
// Only send active weapon index to local player
BEGIN_SEND_TABLE_NOBASE( CBaseCombatCharacter, DT_BCCLocalPlayerExclusive )
SendPropTime( SENDINFO( m_flNextAttack ) ),
END_SEND_TABLE();
//-----------------------------------------------------------------------------
// This table encodes the CBaseCombatCharacter
//-----------------------------------------------------------------------------
IMPLEMENT_SERVERCLASS_ST(CBaseCombatCharacter, DT_BaseCombatCharacter)
#ifdef GLOWS_ENABLE
SendPropBool( SENDINFO( m_bGlowEnabled ) ),
SendPropVector( SENDINFO( m_GlowColor ), 8, 0, 0, 1 ),
SendPropFloat( SENDINFO( m_GlowAlpha ) ),
#endif // GLOWS_ENABLE
// Data that only gets sent to the local player.
SendPropDataTable( "bcc_localdata", 0, &REFERENCE_SEND_TABLE(DT_BCCLocalPlayerExclusive), SendProxy_SendBaseCombatCharacterLocalDataTable ),
SendPropEHandle( SENDINFO( m_hActiveWeapon ) ),
SendPropArray3( SENDINFO_ARRAY3(m_hMyWeapons), SendPropEHandle( SENDINFO_ARRAY(m_hMyWeapons) ) ),
#ifdef INVASION_DLL
SendPropInt( SENDINFO(m_iPowerups), MAX_POWERUPS, SPROP_UNSIGNED ),
#endif
END_SEND_TABLE()
//-----------------------------------------------------------------------------
// Interactions
//-----------------------------------------------------------------------------
void CBaseCombatCharacter::InitInteractionSystem()
{
// interaction ids continue to go up with every map load, otherwise you get
// collisions if a future map has a different set of NPCs from a current map
}
//-----------------------------------------------------------------------------
// Purpose: Return an interaction ID (so we have no collisions)
//-----------------------------------------------------------------------------
int CBaseCombatCharacter::GetInteractionID(void)
{
m_lastInteraction++;
return (m_lastInteraction);
}
#ifdef MAPBASE
//-----------------------------------------------------------------------------
// Purpose: New method of adding interactions which allows their name to be available (currently used for VScript)
//-----------------------------------------------------------------------------
void CBaseCombatCharacter::AddInteractionWithString( int &interaction, const char *szName )
{
interaction = GetInteractionID();
if (g_pScriptVM)
{
ScriptRegisterConstantNamed( g_pScriptVM, interaction, szName, "An interaction which could be used with HandleInteraction or DispatchInteraction. NOTE: These are usually only initialized by certain types of NPCs when an instance of one spawns in the level for the first time!!! (the fact you're seeing this one means there was an NPC in the level which initialized it)" );
}
}
#endif
// ============================================================================
bool CBaseCombatCharacter::HasHumanGibs( void )
{
#if defined( HL2_DLL )
Class_T myClass = Classify();
if ( myClass == CLASS_CITIZEN_PASSIVE ||
myClass == CLASS_CITIZEN_REBEL ||
myClass == CLASS_COMBINE ||
myClass == CLASS_CONSCRIPT ||
myClass == CLASS_METROPOLICE ||
myClass == CLASS_PLAYER )
return true;
#elif defined( HL1_DLL )
Class_T myClass = Classify();
if ( myClass == CLASS_HUMAN_MILITARY ||
myClass == CLASS_PLAYER_ALLY ||
myClass == CLASS_HUMAN_PASSIVE ||
myClass == CLASS_PLAYER )
{
return true;
}
#elif defined( CSPORT_DLL )
Class_T myClass = Classify();
if ( myClass == CLASS_PLAYER )
{
return true;
}
#endif
return false;
}
bool CBaseCombatCharacter::HasAlienGibs( void )
{
#if defined( HL2_DLL )
Class_T myClass = Classify();
if ( myClass == CLASS_BARNACLE ||
myClass == CLASS_STALKER ||
myClass == CLASS_ZOMBIE ||
myClass == CLASS_VORTIGAUNT ||
myClass == CLASS_HEADCRAB )
{
return true;
}
#elif defined( HL1_DLL )
Class_T myClass = Classify();
if ( myClass == CLASS_ALIEN_MILITARY ||
myClass == CLASS_ALIEN_MONSTER ||
myClass == CLASS_INSECT ||
myClass == CLASS_ALIEN_PREDATOR ||
myClass == CLASS_ALIEN_PREY )
{
return true;
}
#endif
return false;
}
void CBaseCombatCharacter::CorpseFade( void )
{
StopAnimation();
SetAbsVelocity( vec3_origin );
SetMoveType( MOVETYPE_NONE );
SetLocalAngularVelocity( vec3_angle );
m_flAnimTime = gpGlobals->curtime;
IncrementInterpolationFrame();
SUB_StartFadeOut();
}
//-----------------------------------------------------------------------------
// Visibility caching
//-----------------------------------------------------------------------------
struct VisibilityCacheEntry_t
{
CBaseEntity *pEntity1;
CBaseEntity *pEntity2;
EHANDLE pBlocker;
float time;
};
class CVisibilityCacheEntryLess
{
public:
CVisibilityCacheEntryLess( int ) {}
bool operator!() const { return false; }
bool operator()( const VisibilityCacheEntry_t &lhs, const VisibilityCacheEntry_t &rhs ) const
{
return ( memcmp( &lhs, &rhs, offsetof( VisibilityCacheEntry_t, pBlocker ) ) < 0 );
}
};
static CUtlRBTree<VisibilityCacheEntry_t, unsigned short, CVisibilityCacheEntryLess> g_VisibilityCache;
const float VIS_CACHE_ENTRY_LIFE = ( !IsXbox() ) ? .090 : .500;
bool CBaseCombatCharacter::FVisible( CBaseEntity *pEntity, int traceMask, CBaseEntity **ppBlocker )
{
VPROF( "CBaseCombatCharacter::FVisible" );
#ifdef MAPBASE
if ( traceMask != MASK_BLOCKLOS || !ShouldUseVisibilityCache( pEntity ) || pEntity == this || !ai_use_visibility_cache.GetBool()
)
#else
if ( traceMask != MASK_BLOCKLOS || !ShouldUseVisibilityCache() || pEntity == this
#if defined(HL2_DLL)
|| Classify() == CLASS_BULLSEYE || pEntity->Classify() == CLASS_BULLSEYE
#endif
)
#endif
{
return BaseClass::FVisible( pEntity, traceMask, ppBlocker );
}
VisibilityCacheEntry_t cacheEntry;
if ( this < pEntity )
{
cacheEntry.pEntity1 = this;
cacheEntry.pEntity2 = pEntity;
}
else
{
cacheEntry.pEntity1 = pEntity;
cacheEntry.pEntity2 = this;
}
int iCache = g_VisibilityCache.Find( cacheEntry );
if ( iCache != g_VisibilityCache.InvalidIndex() )
{
if ( gpGlobals->curtime - g_VisibilityCache[iCache].time < VIS_CACHE_ENTRY_LIFE )
{
bool bCachedResult = !g_VisibilityCache[iCache].pBlocker.IsValid();
if ( bCachedResult )
{
if ( ppBlocker )
{
*ppBlocker = g_VisibilityCache[iCache].pBlocker;
if ( !*ppBlocker )
{
*ppBlocker = GetWorldEntity();
}
}
}
else
{
if ( ppBlocker )
{
*ppBlocker = NULL;
}
}
return bCachedResult;
}
}
else
{
if ( g_VisibilityCache.Count() != g_VisibilityCache.InvalidIndex() )
{
iCache = g_VisibilityCache.Insert( cacheEntry );
}
else
{
return BaseClass::FVisible( pEntity, traceMask, ppBlocker );
}
}
CBaseEntity *pBlocker = NULL;
if ( ppBlocker == NULL )
{
ppBlocker = &pBlocker;
}
bool bResult = BaseClass::FVisible( pEntity, traceMask, ppBlocker );
if ( !bResult )
{
g_VisibilityCache[iCache].pBlocker = *ppBlocker;
}
else
{
g_VisibilityCache[iCache].pBlocker = NULL;
}
g_VisibilityCache[iCache].time = gpGlobals->curtime;
return bResult;
}
void CBaseCombatCharacter::ResetVisibilityCache( CBaseCombatCharacter *pBCC )
{
VPROF( "CBaseCombatCharacter::ResetVisibilityCache" );
if ( !pBCC )
{
g_VisibilityCache.RemoveAll();
return;
}
int i = g_VisibilityCache.FirstInorder();
CUtlVector<unsigned short> removals;
while ( i != g_VisibilityCache.InvalidIndex() )
{
if ( g_VisibilityCache[i].pEntity1 == pBCC || g_VisibilityCache[i].pEntity2 == pBCC )
{
removals.AddToTail( i );
}
i = g_VisibilityCache.NextInorder( i );
}
for ( i = 0; i < removals.Count(); i++ )
{
g_VisibilityCache.RemoveAt( removals[i] );
}
}
#ifdef MAPBASE
bool CBaseCombatCharacter::ShouldUseVisibilityCache( CBaseEntity *pEntity )
{
#ifdef HL2_DLL
return Classify() != CLASS_BULLSEYE && pEntity->Classify() != CLASS_BULLSEYE;
#else
return true;
#endif
}
#endif
#ifdef PORTAL
bool CBaseCombatCharacter::FVisibleThroughPortal( const CProp_Portal *pPortal, CBaseEntity *pEntity, int traceMask, CBaseEntity **ppBlocker )
{
VPROF( "CBaseCombatCharacter::FVisible" );
if ( pEntity->GetFlags() & FL_NOTARGET )
return false;
#if HL1_DLL
// FIXME: only block LOS through opaque water
// don't look through water
if ((m_nWaterLevel != 3 && pEntity->m_nWaterLevel == 3)
|| (m_nWaterLevel == 3 && pEntity->m_nWaterLevel == 0))
return false;
#endif
Vector vecLookerOrigin = EyePosition();//look through the caller's 'eyes'
Vector vecTargetOrigin = pEntity->EyePosition();
// Use the custom LOS trace filter
CTraceFilterLOS traceFilter( this, COLLISION_GROUP_NONE, pEntity );
Vector vecTranslatedTargetOrigin;
UTIL_Portal_PointTransform( pPortal->m_hLinkedPortal->MatrixThisToLinked(), vecTargetOrigin, vecTranslatedTargetOrigin );
Ray_t ray;
ray.Init( vecLookerOrigin, vecTranslatedTargetOrigin );
trace_t tr;
// If we're doing an opaque search, include NPCs.
if ( traceMask == MASK_BLOCKLOS )
{
traceMask = MASK_BLOCKLOS_AND_NPCS;
}
UTIL_Portal_TraceRay_Bullets( pPortal, ray, traceMask, &traceFilter, &tr );
if (tr.fraction != 1.0 || tr.startsolid )
{
// If we hit the entity we're looking for, it's visible
if ( tr.m_pEnt == pEntity )
return true;
// Got line of sight on the vehicle the player is driving!
if ( pEntity && pEntity->IsPlayer() )
{
CBasePlayer *pPlayer = assert_cast<CBasePlayer*>( pEntity );
if ( tr.m_pEnt == pPlayer->GetVehicleEntity() )
return true;
}
if (ppBlocker)
{
*ppBlocker = tr.m_pEnt;
}
return false;// Line of sight is not established
}
return true;// line of sight is valid.
}
#endif
//-----------------------------------------------------------------------------
//=========================================================
// FInViewCone - returns true is the passed ent is in
// the caller's forward view cone. The dot product is performed
// in 2d, making the view cone infinitely tall.
//=========================================================
bool CBaseCombatCharacter::FInViewCone( CBaseEntity *pEntity )
{
return FInViewCone( pEntity->WorldSpaceCenter() );
}
//=========================================================
// FInViewCone - returns true is the passed Vector is in
// the caller's forward view cone. The dot product is performed
// in 2d, making the view cone infinitely tall.
//=========================================================
bool CBaseCombatCharacter::FInViewCone( const Vector &vecSpot )
{
Vector los = ( vecSpot - EyePosition() );
// do this in 2D
los.z = 0;
VectorNormalize( los );
Vector facingDir = EyeDirection2D( );
float flDot = DotProduct( los, facingDir );
if ( flDot > m_flFieldOfView )
return true;
return false;
}
#ifdef PORTAL
//=========================================================
// FInViewCone - returns true is the passed ent is in
// the caller's forward view cone. The dot product is performed
// in 2d, making the view cone infinitely tall.
//=========================================================
CProp_Portal* CBaseCombatCharacter::FInViewConeThroughPortal( CBaseEntity *pEntity )
{
return FInViewConeThroughPortal( pEntity->WorldSpaceCenter() );
}
//=========================================================
// FInViewCone - returns true is the passed Vector is in
// the caller's forward view cone. The dot product is performed
// in 2d, making the view cone infinitely tall.
//=========================================================
CProp_Portal* CBaseCombatCharacter::FInViewConeThroughPortal( const Vector &vecSpot )
{
int iPortalCount = CProp_Portal_Shared::AllPortals.Count();
if( iPortalCount == 0 )
return NULL;
const Vector ptEyePosition = EyePosition();
float fDistToBeat = 1e20; //arbitrarily high number
CProp_Portal *pBestPortal = NULL;
CProp_Portal **pPortals = CProp_Portal_Shared::AllPortals.Base();
// Check through both portals
for ( int iPortal = 0; iPortal < iPortalCount; ++iPortal )
{
CProp_Portal *pPortal = pPortals[iPortal];
// Check if this portal is active, linked, and in the view cone
if( pPortal->IsActivedAndLinked() && FInViewCone( pPortal ) )
{
// The facing direction is the eye to the portal to set up a proper FOV through the relatively small portal hole
Vector facingDir = pPortal->GetAbsOrigin() - ptEyePosition;
// If the portal isn't facing the eye, bail
if ( facingDir.Dot( pPortal->m_plane_Origin.normal ) > 0.0f )
continue;
// If the point is behind the linked portal, bail
if ( ( vecSpot - pPortal->m_hLinkedPortal->GetAbsOrigin() ).Dot( pPortal->m_hLinkedPortal->m_plane_Origin.normal ) < 0.0f )
continue;
// Remove height from the equation
facingDir.z = 0.0f;
float fPortalDist = VectorNormalize( facingDir );
// Translate the target spot across the portal
Vector vTranslatedVecSpot;
UTIL_Portal_PointTransform( pPortal->m_hLinkedPortal->MatrixThisToLinked(), vecSpot, vTranslatedVecSpot );
// do this in 2D
Vector los = ( vTranslatedVecSpot - ptEyePosition );
los.z = 0.0f;
float fSpotDist = VectorNormalize( los );
if( fSpotDist > fDistToBeat )
continue; //no point in going further, we already have a better portal
// If the target point is closer than the portal (banana juice), bail
// HACK: Extra 32 is a fix for the player who's origin can be on one side of a portal while his center mirrored across is closer than the portal.
if ( fPortalDist > fSpotDist + 32.0f )
continue;
// Get the worst case FOV from the portal's corners
float fFOVThroughPortal = 1.0f;
for ( int i = 0; i < 4; ++i )
{
//Vector vPortalCorner = pPortal->GetAbsOrigin() + vPortalRight * PORTAL_HALF_WIDTH * ( ( i / 2 == 0 ) ? ( 1.0f ) : ( -1.0f ) ) +
// vPortalUp * PORTAL_HALF_HEIGHT * ( ( i % 2 == 0 ) ? ( 1.0f ) : ( -1.0f ) );
Vector vEyeToCorner = pPortal->m_vPortalCorners[i] - ptEyePosition;
vEyeToCorner.z = 0.0f;
VectorNormalize( vEyeToCorner );
float flCornerDot = DotProduct( vEyeToCorner, facingDir );
if ( flCornerDot < fFOVThroughPortal )
fFOVThroughPortal = flCornerDot;
}
float flDot = DotProduct( los, facingDir );
// Use the tougher FOV of either the standard FOV or FOV clipped to the portal hole
if ( flDot > MAX( fFOVThroughPortal, m_flFieldOfView ) )
{
float fActualDist = ptEyePosition.DistToSqr( vTranslatedVecSpot );
if( fActualDist < fDistToBeat )
{
fDistToBeat = fActualDist;
pBestPortal = pPortal;
}
}
}
}
return pBestPortal;
}
#endif
//=========================================================
// FInAimCone - returns true is the passed ent is in
// the caller's forward aim cone. The dot product is performed
// in 2d, making the aim cone infinitely tall.
//=========================================================
bool CBaseCombatCharacter::FInAimCone( CBaseEntity *pEntity )
{
return FInAimCone( pEntity->BodyTarget( EyePosition() ) );
}
//=========================================================
// FInAimCone - returns true is the passed Vector is in
// the caller's forward aim cone. The dot product is performed
// in 2d, making the view cone infinitely tall. By default, the
// callers aim cone is assumed to be very narrow
//=========================================================
bool CBaseCombatCharacter::FInAimCone( const Vector &vecSpot )
{
Vector los = ( vecSpot - GetAbsOrigin() );
// do this in 2D
los.z = 0;
VectorNormalize( los );
Vector facingDir = BodyDirection2D( );
float flDot = DotProduct( los, facingDir );
if ( flDot > 0.994 )//!!!BUGBUG - magic number same as FacingIdeal(), what is this?
return true;
return false;
}
//-----------------------------------------------------------------------------
// Purpose: This is a generic function (to be implemented by sub-classes) to
// handle specific interactions between different types of characters
// (For example the barnacle grabbing an NPC)
// Input : The type of interaction, extra info pointer, and who started it
// Output : true - if sub-class has a response for the interaction
// false - if sub-class has no response
//-----------------------------------------------------------------------------
bool CBaseCombatCharacter::HandleInteraction( int interactionType, void *data, CBaseCombatCharacter* sourceEnt )
{
#ifdef HL2_DLL
if ( interactionType == g_interactionBarnacleVictimReleased )
{
// For now, throw away the NPC and leave the ragdoll.
UTIL_Remove( this );
return true;
}
#endif // HL2_DLL
return false;
}
//-----------------------------------------------------------------------------
// Purpose: Constructor : Initialize some fields
//-----------------------------------------------------------------------------
CBaseCombatCharacter::CBaseCombatCharacter( void )
{
#ifdef _DEBUG
// necessary since in debug, we initialize vectors to NAN for debugging
m_HackedGunPos.Init();
#endif
// Zero the damage accumulator.
m_flDamageAccumulator = 0.0f;
// Init weapon and Ammo data
m_hActiveWeapon = NULL;
// reset all ammo values to 0
RemoveAllAmmo();
// not alive yet
m_aliveTimer.Invalidate();
m_hasBeenInjured = 0;
for( int t=0; t<MAX_DAMAGE_TEAMS; ++t )
{
m_damageHistory[t].team = TEAM_INVALID;
}
// not standing on a nav area yet
m_lastNavArea = NULL;
m_registeredNavTeam = TEAM_INVALID;
for (int i = 0; i < MAX_WEAPONS; i++)
{
m_hMyWeapons.Set( i, NULL );
}
// Default so that spawned entities have this set
m_impactEnergyScale = 1.0f;
m_bForceServerRagdoll = ai_force_serverside_ragdoll.GetBool();
#ifdef GLOWS_ENABLE
m_bGlowEnabled.Set( false );
m_GlowColor.GetForModify().Init( 0.76f, 0.76f, 0.76f );
m_GlowAlpha.Set(1.0f);
#endif // GLOWS_ENABLE
}
//------------------------------------------------------------------------------
// Purpose : Destructor
// Input :
// Output :
//------------------------------------------------------------------------------
CBaseCombatCharacter::~CBaseCombatCharacter( void )
{
ResetVisibilityCache( this );
ClearLastKnownArea();
}
//-----------------------------------------------------------------------------
// Purpose: Put the combat character into the environment
//-----------------------------------------------------------------------------
void CBaseCombatCharacter::Spawn( void )
{
BaseClass::Spawn();
SetBlocksLOS( false );
m_aliveTimer.Start();
m_hasBeenInjured = 0;
for( int t=0; t<MAX_DAMAGE_TEAMS; ++t )
{
m_damageHistory[t].team = TEAM_INVALID;
}
// not standing on a nav area yet
ClearLastKnownArea();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseCombatCharacter::Precache()
{
BaseClass::Precache();
PrecacheScriptSound( "BaseCombatCharacter.CorpseGib" );
PrecacheScriptSound( "BaseCombatCharacter.StopWeaponSounds" );
PrecacheScriptSound( "BaseCombatCharacter.AmmoPickup" );
for ( int i = m_Relationship.Count() - 1; i >= 0 ; i--)
{
if ( !m_Relationship[i].entity && m_Relationship[i].classType == CLASS_NONE )
{
DevMsg( 2, "Removing relationship for lost entity\n" );
m_Relationship.FastRemove( i );
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseCombatCharacter::Restore( IRestore &restore )
{
int status = BaseClass::Restore(restore);
if ( !status )
return 0;
if ( gpGlobals->eLoadType == MapLoad_Transition )
{
DevMsg( 2, "%s (%s) removing class relationships due to level transition\n", STRING( GetEntityName() ), GetClassname() );
for ( int i = m_Relationship.Count() - 1; i >= 0; --i )
{
if ( !m_Relationship[i].entity && m_Relationship[i].classType != CLASS_NONE )
{
m_Relationship.FastRemove( i );
}
}
}
return status;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseCombatCharacter::UpdateOnRemove( void )
{
int i;
// Make sure any weapons I didn't drop get removed.
for (i=0;i<MAX_WEAPONS;i++)
{
if (m_hMyWeapons[i])
{
UTIL_Remove( m_hMyWeapons[i] );
}
}
// tell owner ( if any ) that we're dead.This is mostly for NPCMaker functionality.
CBaseEntity *pOwner = GetOwnerEntity();
if ( pOwner )
{
pOwner->DeathNotice( this );
SetOwnerEntity( NULL );
}
#ifdef GLOWS_ENABLE
RemoveGlowEffect();
#endif // GLOWS_ENABLE