forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseentity_shared.cpp
2574 lines (2203 loc) · 68.7 KB
/
baseentity_shared.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:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "decals.h"
#include "effect_dispatch_data.h"
#include "model_types.h"
#include "gamestringpool.h"
#include "ammodef.h"
#include "takedamageinfo.h"
#include "shot_manipulator.h"
#include "ai_debug_shared.h"
#include "mapentities_shared.h"
#include "debugoverlay_shared.h"
#include "coordsize.h"
#include "vphysics/performance.h"
#ifdef CLIENT_DLL
#include "c_te_effect_dispatch.h"
#else
#include "te_effect_dispatch.h"
#include "soundent.h"
#include "iservervehicle.h"
#include "player_pickup.h"
#include "waterbullet.h"
#include "func_break.h"
#ifdef HL2MP
#include "te_hl2mp_shotgun_shot.h"
#endif
#include "gamestats.h"
#endif
#ifdef HL2_EPISODIC
ConVar hl2_episodic( "hl2_episodic", "1", FCVAR_REPLICATED );
#else
ConVar hl2_episodic( "hl2_episodic", "0", FCVAR_REPLICATED );
#endif//HL2_EPISODIC
#ifdef PORTAL
#include "prop_portal_shared.h"
#endif
#ifdef TF_DLL
#include "tf_gamerules.h"
#include "tf_weaponbase.h"
#endif // TF_DLL
#include "rumble_shared.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef GAME_DLL
ConVar ent_debugkeys( "ent_debugkeys", "" );
extern bool ParseKeyvalue( void *pObject, typedescription_t *pFields, int iNumFields, const char *szKeyName, const char *szValue );
extern bool ExtractKeyvalue( void *pObject, typedescription_t *pFields, int iNumFields, const char *szKeyName, char *szValue, int iMaxLen );
#endif
bool CBaseEntity::m_bAllowPrecache = false;
// Set default max values for entities based on the existing constants from elsewhere
float k_flMaxEntityPosCoord = MAX_COORD_FLOAT;
float k_flMaxEntityEulerAngle = 360.0 * 1000.0f; // really should be restricted to +/-180, but some code doesn't adhere to this. let's just trap NANs, etc
// Sometimes the resulting computed speeds are legitimately above the original
// constants; use bumped up versions for the downstream validation logic to
// account for this.
float k_flMaxEntitySpeed = k_flMaxVelocity * 2.0f;
float k_flMaxEntitySpinRate = k_flMaxAngularVelocity * 10.0f;
ConVar ai_shot_bias_min( "ai_shot_bias_min", "-1.0", FCVAR_REPLICATED );
ConVar ai_shot_bias_max( "ai_shot_bias_max", "1.0", FCVAR_REPLICATED );
ConVar ai_debug_shoot_positions( "ai_debug_shoot_positions", "0", FCVAR_REPLICATED | FCVAR_CHEAT );
// Utility func to throttle rate at which the "reasonable position" spew goes out
static double s_LastEntityReasonableEmitTime;
bool CheckEmitReasonablePhysicsSpew()
{
// Reported recently?
double now = Plat_FloatTime();
if ( now >= s_LastEntityReasonableEmitTime && now < s_LastEntityReasonableEmitTime + 5.0 )
{
// Already reported recently
return false;
}
// Not reported recently. Report it now
s_LastEntityReasonableEmitTime = now;
return true;
}
//-----------------------------------------------------------------------------
// Purpose: Spawn some blood particles
//-----------------------------------------------------------------------------
void SpawnBlood(Vector vecSpot, const Vector &vecDir, int bloodColor, float flDamage)
{
UTIL_BloodDrips( vecSpot, vecDir, bloodColor, (int)flDamage );
}
#if !defined( NO_ENTITY_PREDICTION )
//-----------------------------------------------------------------------------
// The player drives simulation of this entity
//-----------------------------------------------------------------------------
void CBaseEntity::SetPlayerSimulated( CBasePlayer *pOwner )
{
m_bIsPlayerSimulated = true;
pOwner->AddToPlayerSimulationList( this );
m_hPlayerSimulationOwner = pOwner;
}
void CBaseEntity::UnsetPlayerSimulated( void )
{
if ( m_hPlayerSimulationOwner != NULL )
{
m_hPlayerSimulationOwner->RemoveFromPlayerSimulationList( this );
}
m_hPlayerSimulationOwner = NULL;
m_bIsPlayerSimulated = false;
}
#endif
// position of eyes
Vector CBaseEntity::EyePosition( void )
{
return GetAbsOrigin() + GetViewOffset();
}
const QAngle &CBaseEntity::EyeAngles( void )
{
return GetAbsAngles();
}
const QAngle &CBaseEntity::LocalEyeAngles( void )
{
return GetLocalAngles();
}
// position of ears
Vector CBaseEntity::EarPosition( void )
{
return EyePosition();
}
void CBaseEntity::SetViewOffset( const Vector& v )
{
m_vecViewOffset = v;
}
const Vector& CBaseEntity::GetViewOffset() const
{
return m_vecViewOffset;
}
//-----------------------------------------------------------------------------
// center point of entity
//-----------------------------------------------------------------------------
const Vector &CBaseEntity::WorldSpaceCenter( ) const
{
return CollisionProp()->WorldSpaceCenter();
}
#if !defined( CLIENT_DLL )
#define CHANGE_FLAGS(flags,newFlags) { unsigned int old = flags; flags = (newFlags); gEntList.ReportEntityFlagsChanged( this, old, flags ); }
#else
#define CHANGE_FLAGS(flags,newFlags) (flags = (newFlags))
#endif
void CBaseEntity::AddFlag( int flags )
{
CHANGE_FLAGS( m_fFlags, m_fFlags | flags );
}
void CBaseEntity::RemoveFlag( int flagsToRemove )
{
CHANGE_FLAGS( m_fFlags, m_fFlags & ~flagsToRemove );
}
void CBaseEntity::ClearFlags( void )
{
CHANGE_FLAGS( m_fFlags, 0 );
}
void CBaseEntity::ToggleFlag( int flagToToggle )
{
CHANGE_FLAGS( m_fFlags, m_fFlags ^ flagToToggle );
}
void CBaseEntity::SetEffects( int nEffects )
{
if ( nEffects != m_fEffects )
{
#if !defined( CLIENT_DLL )
#ifdef HL2_EPISODIC
// Hack for now, to avoid player emitting radius with his flashlight
if ( !IsPlayer() )
{
if ( (nEffects & (EF_BRIGHTLIGHT|EF_DIMLIGHT)) && !(m_fEffects & (EF_BRIGHTLIGHT|EF_DIMLIGHT)) )
{
AddEntityToDarknessCheck( this );
}
else if ( !(nEffects & (EF_BRIGHTLIGHT|EF_DIMLIGHT)) && (m_fEffects & (EF_BRIGHTLIGHT|EF_DIMLIGHT)) )
{
RemoveEntityFromDarknessCheck( this );
}
}
#endif // HL2_EPISODIC
#endif // !CLIENT_DLL
m_fEffects = nEffects;
#ifndef CLIENT_DLL
DispatchUpdateTransmitState();
#else
UpdateVisibility();
#endif
}
}
void CBaseEntity::AddEffects( int nEffects )
{
#if !defined( CLIENT_DLL )
#ifdef HL2_EPISODIC
if ( (nEffects & (EF_BRIGHTLIGHT|EF_DIMLIGHT)) && !(m_fEffects & (EF_BRIGHTLIGHT|EF_DIMLIGHT)) )
{
// Hack for now, to avoid player emitting radius with his flashlight
if ( !IsPlayer() )
{
AddEntityToDarknessCheck( this );
}
}
#endif // HL2_EPISODIC
#endif // !CLIENT_DLL
m_fEffects |= nEffects;
if ( nEffects & EF_NODRAW)
{
#ifndef CLIENT_DLL
DispatchUpdateTransmitState();
#else
UpdateVisibility();
#endif
}
}
void CBaseEntity::SetBlocksLOS( bool bBlocksLOS )
{
if ( bBlocksLOS )
{
RemoveEFlags( EFL_DONTBLOCKLOS );
}
else
{
AddEFlags( EFL_DONTBLOCKLOS );
}
}
bool CBaseEntity::BlocksLOS( void )
{
return !IsEFlagSet(EFL_DONTBLOCKLOS);
}
void CBaseEntity::SetAIWalkable( bool bBlocksLOS )
{
if ( bBlocksLOS )
{
RemoveEFlags( EFL_DONTWALKON );
}
else
{
AddEFlags( EFL_DONTWALKON );
}
}
bool CBaseEntity::IsAIWalkable( void )
{
return !IsEFlagSet(EFL_DONTWALKON);
}
//-----------------------------------------------------------------------------
// Purpose: Handles keys and outputs from the BSP.
// Input : mapData - Text block of keys and values from the BSP.
//-----------------------------------------------------------------------------
void CBaseEntity::ParseMapData( CEntityMapData *mapData )
{
char keyName[MAPKEY_MAXLENGTH];
char value[MAPKEY_MAXLENGTH];
#ifdef _DEBUG
#ifdef GAME_DLL
ValidateDataDescription();
#endif // GAME_DLL
#endif // _DEBUG
// loop through all keys in the data block and pass the info back into the object
if ( mapData->GetFirstKey(keyName, value) )
{
do
{
KeyValue( keyName, value );
}
while ( mapData->GetNextKey(keyName, value) );
}
}
//-----------------------------------------------------------------------------
// Parse data from a map file
//-----------------------------------------------------------------------------
bool CBaseEntity::KeyValue( const char *szKeyName, const char *szValue )
{
//!! temp hack, until worldcraft is fixed
// strip the # tokens from (duplicate) key names
char *s = (char *)strchr( szKeyName, '#' );
if ( s )
{
*s = '\0';
}
if ( FStrEq( szKeyName, "rendercolor" ) || FStrEq( szKeyName, "rendercolor32" ))
{
color32 tmp;
UTIL_StringToColor32( &tmp, szValue );
SetRenderColor( tmp.r, tmp.g, tmp.b );
// don't copy alpha, legacy support uses renderamt
return true;
}
if ( FStrEq( szKeyName, "renderamt" ) )
{
SetRenderColorA( atoi( szValue ) );
return true;
}
if ( FStrEq( szKeyName, "disableshadows" ))
{
int val = atoi( szValue );
if (val)
{
AddEffects( EF_NOSHADOW );
}
return true;
}
if ( FStrEq( szKeyName, "mins" ))
{
Vector mins;
UTIL_StringToVector( mins.Base(), szValue );
CollisionProp()->SetCollisionBounds( mins, CollisionProp()->OBBMaxs() );
return true;
}
if ( FStrEq( szKeyName, "maxs" ))
{
Vector maxs;
UTIL_StringToVector( maxs.Base(), szValue );
CollisionProp()->SetCollisionBounds( CollisionProp()->OBBMins(), maxs );
return true;
}
if ( FStrEq( szKeyName, "disablereceiveshadows" ))
{
int val = atoi( szValue );
if (val)
{
AddEffects( EF_NORECEIVESHADOW );
}
return true;
}
if ( FStrEq( szKeyName, "nodamageforces" ))
{
int val = atoi( szValue );
if (val)
{
AddEFlags( EFL_NO_DAMAGE_FORCES );
}
return true;
}
// Fix up single angles
if( FStrEq( szKeyName, "angle" ) )
{
static char szBuf[64];
float y = atof( szValue );
if (y >= 0)
{
Q_snprintf( szBuf,sizeof(szBuf), "%f %f %f", GetLocalAngles()[0], y, GetLocalAngles()[2] );
}
else if ((int)y == -1)
{
Q_strncpy( szBuf, "-90 0 0", sizeof(szBuf) );
}
else
{
Q_strncpy( szBuf, "90 0 0", sizeof(szBuf) );
}
// Do this so inherited classes looking for 'angles' don't have to bother with 'angle'
return KeyValue( szKeyName, szBuf );
}
// NOTE: Have to do these separate because they set two values instead of one
if( FStrEq( szKeyName, "angles" ) )
{
QAngle angles;
UTIL_StringToVector( angles.Base(), szValue );
// If you're hitting this assert, it's probably because you're
// calling SetLocalAngles from within a KeyValues method.. use SetAbsAngles instead!
Assert( (GetMoveParent() == NULL) && !IsEFlagSet( EFL_DIRTY_ABSTRANSFORM ) );
SetAbsAngles( angles );
return true;
}
if( FStrEq( szKeyName, "origin" ) )
{
Vector vecOrigin;
UTIL_StringToVector( vecOrigin.Base(), szValue );
// If you're hitting this assert, it's probably because you're
// calling SetLocalOrigin from within a KeyValues method.. use SetAbsOrigin instead!
Assert( (GetMoveParent() == NULL) && !IsEFlagSet( EFL_DIRTY_ABSTRANSFORM ) );
SetAbsOrigin( vecOrigin );
return true;
}
#ifdef GAME_DLL
if ( FStrEq( szKeyName, "targetname" ) )
{
m_iName = AllocPooledString( szValue );
return true;
}
// loop through the data description, and try and place the keys in
if ( !*ent_debugkeys.GetString() )
{
for ( datamap_t *dmap = GetDataDescMap(); dmap != NULL; dmap = dmap->baseMap )
{
if ( ::ParseKeyvalue(this, dmap->dataDesc, dmap->dataNumFields, szKeyName, szValue) )
return true;
}
}
else
{
// debug version - can be used to see what keys have been parsed in
bool printKeyHits = false;
const char *debugName = "";
if ( *ent_debugkeys.GetString() && !Q_stricmp(ent_debugkeys.GetString(), STRING(m_iClassname)) )
{
// Msg( "-- found entity of type %s\n", STRING(m_iClassname) );
printKeyHits = true;
debugName = STRING(m_iClassname);
}
// loop through the data description, and try and place the keys in
for ( datamap_t *dmap = GetDataDescMap(); dmap != NULL; dmap = dmap->baseMap )
{
if ( !printKeyHits && *ent_debugkeys.GetString() && !Q_stricmp(dmap->dataClassName, ent_debugkeys.GetString()) )
{
// Msg( "-- found class of type %s\n", dmap->dataClassName );
printKeyHits = true;
debugName = dmap->dataClassName;
}
if ( ::ParseKeyvalue(this, dmap->dataDesc, dmap->dataNumFields, szKeyName, szValue) )
{
if ( printKeyHits )
Msg( "(%s) key: %-16s value: %s\n", debugName, szKeyName, szValue );
return true;
}
}
if ( printKeyHits )
Msg( "!! (%s) key not handled: \"%s\" \"%s\"\n", STRING(m_iClassname), szKeyName, szValue );
}
#endif
// key hasn't been handled
return false;
}
bool CBaseEntity::KeyValue( const char *szKeyName, float flValue )
{
char string[256];
Q_snprintf(string,sizeof(string), "%f", flValue );
return KeyValue( szKeyName, string );
}
bool CBaseEntity::KeyValue( const char *szKeyName, const Vector &vecValue )
{
char string[256];
Q_snprintf(string,sizeof(string), "%f %f %f", vecValue.x, vecValue.y, vecValue.z );
return KeyValue( szKeyName, string );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
bool CBaseEntity::GetKeyValue( const char *szKeyName, char *szValue, int iMaxLen )
{
if ( FStrEq( szKeyName, "rendercolor" ) || FStrEq( szKeyName, "rendercolor32" ))
{
color32 tmp = GetRenderColor();
Q_snprintf( szValue, iMaxLen, "%d %d %d %d", tmp.r, tmp.g, tmp.b, tmp.a );
return true;
}
if ( FStrEq( szKeyName, "renderamt" ) )
{
color32 tmp = GetRenderColor();
Q_snprintf( szValue, iMaxLen, "%d", tmp.a );
return true;
}
if ( FStrEq( szKeyName, "disableshadows" ))
{
Q_snprintf( szValue, iMaxLen, "%d", IsEffectActive( EF_NOSHADOW ) );
return true;
}
if ( FStrEq( szKeyName, "mins" ))
{
Assert( 0 );
return false;
}
if ( FStrEq( szKeyName, "maxs" ))
{
Assert( 0 );
return false;
}
if ( FStrEq( szKeyName, "disablereceiveshadows" ))
{
Q_snprintf( szValue, iMaxLen, "%d", IsEffectActive( EF_NORECEIVESHADOW ) );
return true;
}
if ( FStrEq( szKeyName, "nodamageforces" ))
{
Q_snprintf( szValue, iMaxLen, "%d", IsEffectActive( EFL_NO_DAMAGE_FORCES ) );
return true;
}
// Fix up single angles
if( FStrEq( szKeyName, "angle" ) )
{
return false;
}
// NOTE: Have to do these separate because they set two values instead of one
if( FStrEq( szKeyName, "angles" ) )
{
QAngle angles = GetAbsAngles();
Q_snprintf( szValue, iMaxLen, "%f %f %f", angles.x, angles.y, angles.z );
return true;
}
if( FStrEq( szKeyName, "origin" ) )
{
Vector vecOrigin = GetAbsOrigin();
Q_snprintf( szValue, iMaxLen, "%f %f %f", vecOrigin.x, vecOrigin.y, vecOrigin.z );
return true;
}
#ifdef GAME_DLL
if ( FStrEq( szKeyName, "targetname" ) )
{
Q_snprintf( szValue, iMaxLen, "%s", STRING( GetEntityName() ) );
return true;
}
if ( FStrEq( szKeyName, "classname" ) )
{
Q_snprintf( szValue, iMaxLen, "%s", GetClassname() );
return true;
}
for ( datamap_t *dmap = GetDataDescMap(); dmap != NULL; dmap = dmap->baseMap )
{
if ( ::ExtractKeyvalue( this, dmap->dataDesc, dmap->dataNumFields, szKeyName, szValue, iMaxLen ) )
return true;
}
#endif
return false;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : collisionGroup -
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool CBaseEntity::ShouldCollide( int collisionGroup, int contentsMask ) const
{
if ( m_CollisionGroup == COLLISION_GROUP_DEBRIS )
{
if ( ! (contentsMask & CONTENTS_DEBRIS) )
return false;
}
return true;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : seed -
//-----------------------------------------------------------------------------
void CBaseEntity::SetPredictionRandomSeed( const CUserCmd *cmd )
{
if ( !cmd )
{
m_nPredictionRandomSeed = -1;
#ifdef GAME_DLL
m_nPredictionRandomSeedServer = -1;
#endif
return;
}
m_nPredictionRandomSeed = ( cmd->random_seed );
#ifdef GAME_DLL
m_nPredictionRandomSeedServer = ( cmd->server_random_seed );
#endif
}
//------------------------------------------------------------------------------
// Purpose : Base implimentation for entity handling decals
//------------------------------------------------------------------------------
void CBaseEntity::DecalTrace( trace_t *pTrace, char const *decalName )
{
int index = decalsystem->GetDecalIndexForName( decalName );
if ( index < 0 )
return;
Assert( pTrace->m_pEnt );
CBroadcastRecipientFilter filter;
te->Decal( filter, 0.0, &pTrace->endpos, &pTrace->startpos,
pTrace->GetEntityIndex(), pTrace->hitbox, index );
}
//-----------------------------------------------------------------------------
// Purpose: Base handling for impacts against entities
//-----------------------------------------------------------------------------
void CBaseEntity::ImpactTrace( trace_t *pTrace, int iDamageType, const char *pCustomImpactName )
{
VPROF( "CBaseEntity::ImpactTrace" );
Assert( pTrace->m_pEnt );
CBaseEntity *pEntity = pTrace->m_pEnt;
// Build the impact data
CEffectData data;
data.m_vOrigin = pTrace->endpos;
data.m_vStart = pTrace->startpos;
data.m_nSurfaceProp = pTrace->surface.surfaceProps;
data.m_nDamageType = iDamageType;
data.m_nHitBox = pTrace->hitbox;
#ifdef CLIENT_DLL
data.m_hEntity = ClientEntityList().EntIndexToHandle( pEntity->entindex() );
#else
data.m_nEntIndex = pEntity->entindex();
#endif
// Send it on its way
if ( !pCustomImpactName )
{
DispatchEffect( "Impact", data );
}
else
{
DispatchEffect( pCustomImpactName, data );
}
}
//-----------------------------------------------------------------------------
// Purpose: returns the damage decal to use, given a damage type
// Input : bitsDamageType - the damage type
// Output : the index of the damage decal to use
//-----------------------------------------------------------------------------
char const *CBaseEntity::DamageDecal( int bitsDamageType, int gameMaterial )
{
if ( m_nRenderMode == kRenderTransAlpha )
return "";
if ( m_nRenderMode != kRenderNormal && gameMaterial == 'G' )
return "BulletProof";
if ( bitsDamageType == DMG_SLASH )
return "ManhackCut";
// This will get translated at a lower layer based on game material
return "Impact.Concrete";
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
int CBaseEntity::GetIndexForThinkContext( const char *pszContext )
{
for ( int i = 0; i < m_aThinkFunctions.Size(); i++ )
{
if ( !Q_strncmp( STRING( m_aThinkFunctions[i].m_iszContext ), pszContext, MAX_CONTEXT_LENGTH ) )
return i;
}
return NO_THINK_CONTEXT;
}
//-----------------------------------------------------------------------------
// Purpose: Get a fresh think context for this entity
//-----------------------------------------------------------------------------
int CBaseEntity::RegisterThinkContext( const char *szContext )
{
int iIndex = GetIndexForThinkContext( szContext );
if ( iIndex != NO_THINK_CONTEXT )
return iIndex;
// Make a new think func
thinkfunc_t sNewFunc;
Q_memset( &sNewFunc, 0, sizeof( sNewFunc ) );
sNewFunc.m_pfnThink = NULL;
sNewFunc.m_nNextThinkTick = 0;
sNewFunc.m_iszContext = AllocPooledString(szContext);
// Insert it into our list
return m_aThinkFunctions.AddToTail( sNewFunc );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
BASEPTR CBaseEntity::ThinkSet( BASEPTR func, float thinkTime, const char *szContext )
{
#if !defined( CLIENT_DLL )
#ifdef _DEBUG
#ifdef GNUC
COMPILE_TIME_ASSERT( sizeof(func) == 8 );
#else
COMPILE_TIME_ASSERT( sizeof(func) == 4 );
#endif
#endif
#endif
// Old system?
if ( !szContext )
{
m_pfnThink = func;
#if !defined( CLIENT_DLL )
#ifdef _DEBUG
FunctionCheck( *(reinterpret_cast<void **>(&m_pfnThink)), "BaseThinkFunc" );
#endif
#endif
return m_pfnThink;
}
// Find the think function in our list, and if we couldn't find it, register it
int iIndex = GetIndexForThinkContext( szContext );
if ( iIndex == NO_THINK_CONTEXT )
{
iIndex = RegisterThinkContext( szContext );
}
m_aThinkFunctions[ iIndex ].m_pfnThink = func;
#if !defined( CLIENT_DLL )
#ifdef _DEBUG
FunctionCheck( *(reinterpret_cast<void **>(&m_aThinkFunctions[ iIndex ].m_pfnThink)), szContext );
#endif
#endif
if ( thinkTime != 0 )
{
int thinkTick = ( thinkTime == TICK_NEVER_THINK ) ? TICK_NEVER_THINK : TIME_TO_TICKS( thinkTime );
m_aThinkFunctions[ iIndex ].m_nNextThinkTick = thinkTick;
CheckHasThinkFunction( thinkTick == TICK_NEVER_THINK ? false : true );
}
return func;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CBaseEntity::SetNextThink( float thinkTime, const char *szContext )
{
int thinkTick = ( thinkTime == TICK_NEVER_THINK ) ? TICK_NEVER_THINK : TIME_TO_TICKS( thinkTime );
// Are we currently in a think function with a context?
int iIndex = 0;
if ( !szContext )
{
#ifdef _DEBUG
if ( m_iCurrentThinkContext != NO_THINK_CONTEXT )
{
Msg( "Warning: Setting base think function within think context %s\n", STRING(m_aThinkFunctions[m_iCurrentThinkContext].m_iszContext) );
}
#endif
// Old system
m_nNextThinkTick = thinkTick;
CheckHasThinkFunction( thinkTick == TICK_NEVER_THINK ? false : true );
return;
}
else
{
// Find the think function in our list, and if we couldn't find it, register it
iIndex = GetIndexForThinkContext( szContext );
if ( iIndex == NO_THINK_CONTEXT )
{
iIndex = RegisterThinkContext( szContext );
}
}
// Old system
m_aThinkFunctions[ iIndex ].m_nNextThinkTick = thinkTick;
CheckHasThinkFunction( thinkTick == TICK_NEVER_THINK ? false : true );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
float CBaseEntity::GetNextThink( const char *szContext )
{
// Are we currently in a think function with a context?
int iIndex = 0;
if ( !szContext )
{
#ifdef _DEBUG
if ( m_iCurrentThinkContext != NO_THINK_CONTEXT )
{
Msg( "Warning: Getting base nextthink time within think context %s\n", STRING(m_aThinkFunctions[m_iCurrentThinkContext].m_iszContext) );
}
#endif
if ( m_nNextThinkTick == TICK_NEVER_THINK )
return TICK_NEVER_THINK;
// Old system
return TICK_INTERVAL * (m_nNextThinkTick );
}
else
{
// Find the think function in our list
iIndex = GetIndexForThinkContext( szContext );
}
if ( iIndex == m_aThinkFunctions.InvalidIndex() )
return TICK_NEVER_THINK;
if ( m_aThinkFunctions[ iIndex ].m_nNextThinkTick == TICK_NEVER_THINK )
{
return TICK_NEVER_THINK;
}
return TICK_INTERVAL * (m_aThinkFunctions[ iIndex ].m_nNextThinkTick );
}
int CBaseEntity::GetNextThinkTick( const char *szContext /*= NULL*/ )
{
// Are we currently in a think function with a context?
int iIndex = 0;
if ( !szContext )
{
#ifdef _DEBUG
if ( m_iCurrentThinkContext != NO_THINK_CONTEXT )
{
Msg( "Warning: Getting base nextthink time within think context %s\n", STRING(m_aThinkFunctions[m_iCurrentThinkContext].m_iszContext) );
}
#endif
if ( m_nNextThinkTick == TICK_NEVER_THINK )
return TICK_NEVER_THINK;
// Old system
return m_nNextThinkTick;
}
else
{
// Find the think function in our list
iIndex = GetIndexForThinkContext( szContext );
// Looking up an invalid think context!
Assert( iIndex != -1 );
}
if ( ( iIndex == -1 ) || ( m_aThinkFunctions[ iIndex ].m_nNextThinkTick == TICK_NEVER_THINK ) )
{
return TICK_NEVER_THINK;
}
return m_aThinkFunctions[ iIndex ].m_nNextThinkTick;
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
float CBaseEntity::GetLastThink( const char *szContext )
{
// Are we currently in a think function with a context?
int iIndex = 0;
if ( !szContext )
{
#ifdef _DEBUG
if ( m_iCurrentThinkContext != NO_THINK_CONTEXT )
{
Msg( "Warning: Getting base lastthink time within think context %s\n", STRING(m_aThinkFunctions[m_iCurrentThinkContext].m_iszContext) );
}
#endif
// Old system
return m_nLastThinkTick * TICK_INTERVAL;
}
else
{
// Find the think function in our list
iIndex = GetIndexForThinkContext( szContext );
}
return m_aThinkFunctions[ iIndex ].m_nLastThinkTick * TICK_INTERVAL;
}
int CBaseEntity::GetLastThinkTick( const char *szContext /*= NULL*/ )
{
// Are we currently in a think function with a context?
int iIndex = 0;
if ( !szContext )
{
#ifdef _DEBUG
if ( m_iCurrentThinkContext != NO_THINK_CONTEXT )
{
Msg( "Warning: Getting base lastthink time within think context %s\n", STRING(m_aThinkFunctions[m_iCurrentThinkContext].m_iszContext) );
}
#endif
// Old system
return m_nLastThinkTick;
}
else
{
// Find the think function in our list
iIndex = GetIndexForThinkContext( szContext );
}
return m_aThinkFunctions[ iIndex ].m_nLastThinkTick;
}
bool CBaseEntity::WillThink()
{
if ( m_nNextThinkTick > 0 )
return true;
for ( int i = 0; i < m_aThinkFunctions.Count(); i++ )
{
if ( m_aThinkFunctions[i].m_nNextThinkTick > 0 )
return true;
}
return false;
}
// returns the first tick the entity will run any think function
// returns TICK_NEVER_THINK if no think functions are scheduled
int CBaseEntity::GetFirstThinkTick()
{
int minTick = TICK_NEVER_THINK;
if ( m_nNextThinkTick > 0 )
{
minTick = m_nNextThinkTick;
}
for ( int i = 0; i < m_aThinkFunctions.Count(); i++ )
{
int next = m_aThinkFunctions[i].m_nNextThinkTick;
if ( next > 0 )
{
if ( next < minTick || minTick == TICK_NEVER_THINK )
{