forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc_baseentity.cpp
6509 lines (5440 loc) · 181 KB
/
c_baseentity.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 "c_baseentity.h"
#include "prediction.h"
#include "model_types.h"
#include "iviewrender_beams.h"
#include "dlight.h"
#include "iviewrender.h"
#include "view.h"
#include "iefx.h"
#include "c_team.h"
#include "clientmode.h"
#include "usercmd.h"
#include "engine/IEngineSound.h"
#include "engine/IEngineTrace.h"
#include "engine/ivmodelinfo.h"
#include "tier0/vprof.h"
#include "fx_line.h"
#include "interface.h"
#include "materialsystem/imaterialsystem.h"
#include "soundinfo.h"
#include "mathlib/vmatrix.h"
#include "isaverestore.h"
#include "interval.h"
#include "engine/ivdebugoverlay.h"
#include "c_ai_basenpc.h"
#include "apparent_velocity_helper.h"
#include "c_baseanimatingoverlay.h"
#include "tier1/KeyValues.h"
#include "hltvcamera.h"
#include "datacache/imdlcache.h"
#include "toolframework/itoolframework.h"
#include "toolframework_client.h"
#include "decals.h"
#include "cdll_bounded_cvars.h"
#include "inetchannelinfo.h"
#include "proto_version.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef INTERPOLATEDVAR_PARANOID_MEASUREMENT
int g_nInterpolatedVarsChanged = 0;
bool g_bRestoreInterpolatedVarValues = false;
#endif
static bool g_bWasSkipping = (bool)-1;
static bool g_bWasThreaded =(bool)-1;
static int g_nThreadModeTicks = 0;
void cc_cl_interp_all_changed( IConVar *pConVar, const char *pOldString, float flOldValue )
{
ConVarRef var( pConVar );
if ( var.GetInt() )
{
C_BaseEntityIterator iterator;
C_BaseEntity *pEnt;
while ( (pEnt = iterator.Next()) != NULL )
{
if ( pEnt->ShouldInterpolate() )
{
pEnt->AddToInterpolationList();
}
}
}
}
static ConVar cl_extrapolate( "cl_extrapolate", "1", FCVAR_CHEAT, "Enable/disable extrapolation if interpolation history runs out." );
static ConVar cl_interp_npcs( "cl_interp_npcs", "0.0", FCVAR_USERINFO, "Interpolate NPC positions starting this many seconds in past (or cl_interp, if greater)" );
static ConVar cl_interp_all( "cl_interp_all", "0", 0, "Disable interpolation list optimizations.", 0, 0, 0, 0, cc_cl_interp_all_changed );
ConVar r_drawmodeldecals( "r_drawmodeldecals", "1" );
extern ConVar cl_showerror;
int C_BaseEntity::m_nPredictionRandomSeed = -1;
C_BasePlayer *C_BaseEntity::m_pPredictionPlayer = NULL;
bool C_BaseEntity::s_bAbsQueriesValid = true;
bool C_BaseEntity::s_bAbsRecomputationEnabled = true;
bool C_BaseEntity::s_bInterpolate = true;
bool C_BaseEntity::sm_bDisableTouchFuncs = false; // Disables PhysicsTouch and PhysicsStartTouch function calls
static ConVar r_drawrenderboxes( "r_drawrenderboxes", "0", FCVAR_CHEAT );
static bool g_bAbsRecomputationStack[8];
static unsigned short g_iAbsRecomputationStackPos = 0;
// All the entities that want Interpolate() called on them.
static CUtlLinkedList<C_BaseEntity*, unsigned short> g_InterpolationList;
static CUtlLinkedList<C_BaseEntity*, unsigned short> g_TeleportList;
#if !defined( NO_ENTITY_PREDICTION )
//-----------------------------------------------------------------------------
// Purpose: Maintains a list of predicted or client created entities
//-----------------------------------------------------------------------------
class CPredictableList : public IPredictableList
{
public:
virtual C_BaseEntity *GetPredictable( int slot );
virtual int GetPredictableCount( void );
protected:
void AddToPredictableList( ClientEntityHandle_t add );
void RemoveFromPredictablesList( ClientEntityHandle_t remove );
private:
CUtlVector< ClientEntityHandle_t > m_Predictables;
friend class C_BaseEntity;
};
// Create singleton
static CPredictableList g_Predictables;
IPredictableList *predictables = &g_Predictables;
//-----------------------------------------------------------------------------
// Purpose: Add entity to list
// Input : add -
// Output : int
//-----------------------------------------------------------------------------
void CPredictableList::AddToPredictableList( ClientEntityHandle_t add )
{
// This is a hack to remap slot to index
if ( m_Predictables.Find( add ) != m_Predictables.InvalidIndex() )
{
return;
}
// Add to general list
m_Predictables.AddToTail( add );
// Maintain sort order by entindex
int count = m_Predictables.Size();
if ( count < 2 )
return;
int i, j;
for ( i = 0; i < count; i++ )
{
for ( j = i + 1; j < count; j++ )
{
ClientEntityHandle_t h1 = m_Predictables[ i ];
ClientEntityHandle_t h2 = m_Predictables[ j ];
C_BaseEntity *p1 = cl_entitylist->GetBaseEntityFromHandle( h1 );
C_BaseEntity *p2 = cl_entitylist->GetBaseEntityFromHandle( h2 );
if ( !p1 || !p2 )
{
Assert( 0 );
continue;
}
if ( p1->entindex() != -1 &&
p2->entindex() != -1 )
{
if ( p1->entindex() < p2->entindex() )
continue;
}
if ( p2->entindex() == -1 )
continue;
m_Predictables[ i ] = h2;
m_Predictables[ j ] = h1;
}
}
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : remove -
//-----------------------------------------------------------------------------
void CPredictableList::RemoveFromPredictablesList( ClientEntityHandle_t remove )
{
m_Predictables.FindAndRemove( remove );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : slot -
// Output : C_BaseEntity
//-----------------------------------------------------------------------------
C_BaseEntity *CPredictableList::GetPredictable( int slot )
{
return cl_entitylist->GetBaseEntityFromHandle( m_Predictables[ slot ] );
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int
//-----------------------------------------------------------------------------
int CPredictableList::GetPredictableCount( void )
{
return m_Predictables.Count();
}
//-----------------------------------------------------------------------------
// Purpose: Searc predictables for previously created entity (by testId)
// Input : testId -
// Output : static C_BaseEntity
//-----------------------------------------------------------------------------
static C_BaseEntity *FindPreviouslyCreatedEntity( CPredictableId& testId )
{
int c = predictables->GetPredictableCount();
int i;
for ( i = 0; i < c; i++ )
{
C_BaseEntity *e = predictables->GetPredictable( i );
if ( !e || !e->IsClientCreated() )
continue;
// Found it, note use of operator ==
if ( testId == e->m_PredictableID )
{
return e;
}
}
return NULL;
}
#endif
abstract_class IRecordingList
{
public:
virtual ~IRecordingList() {};
virtual void AddToList( ClientEntityHandle_t add ) = 0;
virtual void RemoveFromList( ClientEntityHandle_t remove ) = 0;
virtual int Count() = 0;
virtual IClientRenderable *Get( int index ) = 0;
};
class CRecordingList : public IRecordingList
{
public:
virtual void AddToList( ClientEntityHandle_t add );
virtual void RemoveFromList( ClientEntityHandle_t remove );
virtual int Count();
IClientRenderable *Get( int index );
private:
CUtlVector< ClientEntityHandle_t > m_Recording;
};
static CRecordingList g_RecordingList;
IRecordingList *recordinglist = &g_RecordingList;
//-----------------------------------------------------------------------------
// Purpose: Add entity to list
// Input : add -
// Output : int
//-----------------------------------------------------------------------------
void CRecordingList::AddToList( ClientEntityHandle_t add )
{
// This is a hack to remap slot to index
if ( m_Recording.Find( add ) != m_Recording.InvalidIndex() )
{
return;
}
// Add to general list
m_Recording.AddToTail( add );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : remove -
//-----------------------------------------------------------------------------
void CRecordingList::RemoveFromList( ClientEntityHandle_t remove )
{
m_Recording.FindAndRemove( remove );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : slot -
// Output : IClientRenderable
//-----------------------------------------------------------------------------
IClientRenderable *CRecordingList::Get( int index )
{
return cl_entitylist->GetClientRenderableFromHandle( m_Recording[ index ] );
}
//-----------------------------------------------------------------------------
// Purpose:
// Output : int
//-----------------------------------------------------------------------------
int CRecordingList::Count()
{
return m_Recording.Count();
}
// Should these be somewhere else?
#define PITCH 0
//-----------------------------------------------------------------------------
// Purpose: Decodes animtime and notes when it changes
// Input : *pStruct - ( C_BaseEntity * ) used to flag animtime is changine
// *pVarData -
// *pIn -
// objectID -
//-----------------------------------------------------------------------------
void RecvProxy_AnimTime( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
C_BaseEntity *pEntity = ( C_BaseEntity * )pStruct;
Assert( pOut == &pEntity->m_flAnimTime );
int t;
int tickbase;
int addt;
// Unpack the data.
addt = pData->m_Value.m_Int;
// Note, this needs to be encoded relative to packet timestamp, not raw client clock
tickbase = gpGlobals->GetNetworkBase( gpGlobals->tickcount, pEntity->entindex() );
t = tickbase;
// and then go back to floating point time.
t += addt; // Add in an additional up to 256 100ths from the server
// center m_flAnimTime around current time.
while (t < gpGlobals->tickcount - 127)
t += 256;
while (t > gpGlobals->tickcount + 127)
t -= 256;
pEntity->m_flAnimTime = ( t * TICK_INTERVAL );
}
void RecvProxy_SimulationTime( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
C_BaseEntity *pEntity = ( C_BaseEntity * )pStruct;
Assert( pOut == &pEntity->m_flSimulationTime );
int t;
int tickbase;
int addt;
// Unpack the data.
addt = pData->m_Value.m_Int;
// Note, this needs to be encoded relative to packet timestamp, not raw client clock
tickbase = gpGlobals->GetNetworkBase( gpGlobals->tickcount, pEntity->entindex() );
t = tickbase;
// and then go back to floating point time.
t += addt; // Add in an additional up to 256 100ths from the server
// center m_flSimulationTime around current time.
while (t < gpGlobals->tickcount - 127)
t += 256;
while (t > gpGlobals->tickcount + 127)
t -= 256;
pEntity->m_flSimulationTime = ( t * TICK_INTERVAL );
}
void RecvProxy_LocalVelocity( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
CBaseEntity *pEnt = (CBaseEntity *)pStruct;
Vector vecVelocity;
vecVelocity.x = pData->m_Value.m_Vector[0];
vecVelocity.y = pData->m_Value.m_Vector[1];
vecVelocity.z = pData->m_Value.m_Vector[2];
// SetLocalVelocity checks to see if the value has changed
pEnt->SetLocalVelocity( vecVelocity );
}
void RecvProxy_ToolRecording( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
if ( !ToolsEnabled() )
return;
CBaseEntity *pEnt = (CBaseEntity *)pStruct;
pEnt->SetToolRecording( pData->m_Value.m_Int != 0 );
}
// Expose it to the engine.
IMPLEMENT_CLIENTCLASS(C_BaseEntity, DT_BaseEntity, CBaseEntity);
static void RecvProxy_MoveType( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
((C_BaseEntity*)pStruct)->SetMoveType( (MoveType_t)(pData->m_Value.m_Int) );
}
static void RecvProxy_MoveCollide( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
((C_BaseEntity*)pStruct)->SetMoveCollide( (MoveCollide_t)(pData->m_Value.m_Int) );
}
static void RecvProxy_Solid( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
((C_BaseEntity*)pStruct)->SetSolid( (SolidType_t)pData->m_Value.m_Int );
}
static void RecvProxy_SolidFlags( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
((C_BaseEntity*)pStruct)->SetSolidFlags( pData->m_Value.m_Int );
}
void RecvProxy_EffectFlags( const CRecvProxyData *pData, void *pStruct, void *pOut )
{
((C_BaseEntity*)pStruct)->SetEffects( pData->m_Value.m_Int );
}
BEGIN_RECV_TABLE_NOBASE( C_BaseEntity, DT_AnimTimeMustBeFirst )
RecvPropInt( RECVINFO(m_flAnimTime), 0, RecvProxy_AnimTime ),
END_RECV_TABLE()
#ifndef NO_ENTITY_PREDICTION
BEGIN_RECV_TABLE_NOBASE( C_BaseEntity, DT_PredictableId )
RecvPropPredictableId( RECVINFO( m_PredictableID ) ),
RecvPropInt( RECVINFO( m_bIsPlayerSimulated ) ),
END_RECV_TABLE()
#endif
BEGIN_RECV_TABLE_NOBASE(C_BaseEntity, DT_BaseEntity)
RecvPropDataTable( "AnimTimeMustBeFirst", 0, 0, &REFERENCE_RECV_TABLE(DT_AnimTimeMustBeFirst) ),
RecvPropInt( RECVINFO(m_flSimulationTime), 0, RecvProxy_SimulationTime ),
RecvPropInt( RECVINFO( m_ubInterpolationFrame ) ),
RecvPropVector( RECVINFO_NAME( m_vecNetworkOrigin, m_vecOrigin ) ),
#if PREDICTION_ERROR_CHECK_LEVEL > 1
RecvPropVector( RECVINFO_NAME( m_angNetworkAngles, m_angRotation ) ),
#else
RecvPropQAngles( RECVINFO_NAME( m_angNetworkAngles, m_angRotation ) ),
#endif
#ifdef DEMO_BACKWARDCOMPATABILITY
RecvPropInt( RECVINFO(m_nModelIndex), 0, RecvProxy_IntToModelIndex16_BackCompatible ),
#else
RecvPropInt( RECVINFO(m_nModelIndex) ),
#endif
RecvPropInt(RECVINFO(m_fEffects), 0, RecvProxy_EffectFlags ),
RecvPropInt(RECVINFO(m_nRenderMode)),
RecvPropInt(RECVINFO(m_nRenderFX)),
RecvPropInt(RECVINFO(m_clrRender)),
RecvPropInt(RECVINFO(m_iTeamNum)),
RecvPropInt(RECVINFO(m_CollisionGroup)),
RecvPropFloat(RECVINFO(m_flElasticity)),
RecvPropFloat(RECVINFO(m_flShadowCastDistance)),
RecvPropEHandle( RECVINFO(m_hOwnerEntity) ),
RecvPropEHandle( RECVINFO(m_hEffectEntity) ),
RecvPropInt( RECVINFO_NAME(m_hNetworkMoveParent, moveparent), 0, RecvProxy_IntToMoveParent ),
RecvPropInt( RECVINFO( m_iParentAttachment ) ),
RecvPropInt( "movetype", 0, SIZEOF_IGNORE, 0, RecvProxy_MoveType ),
RecvPropInt( "movecollide", 0, SIZEOF_IGNORE, 0, RecvProxy_MoveCollide ),
RecvPropDataTable( RECVINFO_DT( m_Collision ), 0, &REFERENCE_RECV_TABLE(DT_CollisionProperty) ),
RecvPropInt( RECVINFO ( m_iTextureFrameIndex ) ),
#if !defined( NO_ENTITY_PREDICTION )
RecvPropDataTable( "predictable_id", 0, 0, &REFERENCE_RECV_TABLE( DT_PredictableId ) ),
#endif
RecvPropInt ( RECVINFO( m_bSimulatedEveryTick ), 0, RecvProxy_InterpolationAmountChanged ),
RecvPropInt ( RECVINFO( m_bAnimatedEveryTick ), 0, RecvProxy_InterpolationAmountChanged ),
RecvPropBool ( RECVINFO( m_bAlternateSorting ) ),
#ifdef TF_CLIENT_DLL
RecvPropArray3( RECVINFO_ARRAY(m_nModelIndexOverrides), RecvPropInt( RECVINFO(m_nModelIndexOverrides[0]) ) ),
#endif
END_RECV_TABLE()
const float coordTolerance = 2.0f / (float)( 1 << COORD_FRACTIONAL_BITS );
BEGIN_PREDICTION_DATA_NO_BASE( C_BaseEntity )
// These have a special proxy to handle send/receive
DEFINE_PRED_TYPEDESCRIPTION( m_Collision, CCollisionProperty ),
DEFINE_PRED_FIELD( m_MoveType, FIELD_CHARACTER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_MoveCollide, FIELD_CHARACTER, FTYPEDESC_INSENDTABLE ),
DEFINE_FIELD( m_vecAbsVelocity, FIELD_VECTOR ),
DEFINE_PRED_FIELD_TOL( m_vecVelocity, FIELD_VECTOR, FTYPEDESC_INSENDTABLE, 0.5f ),
// DEFINE_PRED_FIELD( m_fEffects, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_nRenderMode, FIELD_CHARACTER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_nRenderFX, FIELD_CHARACTER, FTYPEDESC_INSENDTABLE ),
// DEFINE_PRED_FIELD( m_flAnimTime, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
// DEFINE_PRED_FIELD( m_flSimulationTime, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_fFlags, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD_TOL( m_vecViewOffset, FIELD_VECTOR, FTYPEDESC_INSENDTABLE, 0.25f ),
DEFINE_PRED_FIELD( m_nModelIndex, FIELD_SHORT, FTYPEDESC_INSENDTABLE | FTYPEDESC_MODELINDEX ),
DEFINE_PRED_FIELD( m_flFriction, FIELD_FLOAT, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_iTeamNum, FIELD_INTEGER, FTYPEDESC_INSENDTABLE ),
DEFINE_PRED_FIELD( m_hOwnerEntity, FIELD_EHANDLE, FTYPEDESC_INSENDTABLE ),
// DEFINE_FIELD( m_nSimulationTick, FIELD_INTEGER ),
DEFINE_PRED_FIELD( m_hNetworkMoveParent, FIELD_EHANDLE, FTYPEDESC_INSENDTABLE ),
// DEFINE_PRED_FIELD( m_pMoveParent, FIELD_EHANDLE ),
// DEFINE_PRED_FIELD( m_pMoveChild, FIELD_EHANDLE ),
// DEFINE_PRED_FIELD( m_pMovePeer, FIELD_EHANDLE ),
// DEFINE_PRED_FIELD( m_pMovePrevPeer, FIELD_EHANDLE ),
DEFINE_PRED_FIELD_TOL( m_vecNetworkOrigin, FIELD_VECTOR, FTYPEDESC_INSENDTABLE, coordTolerance ),
DEFINE_PRED_FIELD( m_angNetworkAngles, FIELD_VECTOR, FTYPEDESC_INSENDTABLE | FTYPEDESC_NOERRORCHECK ),
DEFINE_FIELD( m_vecAbsOrigin, FIELD_VECTOR ),
DEFINE_FIELD( m_angAbsRotation, FIELD_VECTOR ),
DEFINE_FIELD( m_vecOrigin, FIELD_VECTOR ),
DEFINE_FIELD( m_angRotation, FIELD_VECTOR ),
// DEFINE_FIELD( m_hGroundEntity, FIELD_EHANDLE ),
DEFINE_FIELD( m_nWaterLevel, FIELD_CHARACTER ),
DEFINE_FIELD( m_nWaterType, FIELD_CHARACTER ),
DEFINE_FIELD( m_vecAngVelocity, FIELD_VECTOR ),
// DEFINE_FIELD( m_vecAbsAngVelocity, FIELD_VECTOR ),
// DEFINE_FIELD( model, FIELD_INTEGER ), // writing pointer literally
// DEFINE_FIELD( index, FIELD_INTEGER ),
// DEFINE_FIELD( m_ClientHandle, FIELD_SHORT ),
// DEFINE_FIELD( m_Partition, FIELD_SHORT ),
// DEFINE_FIELD( m_hRender, FIELD_SHORT ),
DEFINE_FIELD( m_bDormant, FIELD_BOOLEAN ),
// DEFINE_FIELD( current_position, FIELD_INTEGER ),
// DEFINE_FIELD( m_flLastMessageTime, FIELD_FLOAT ),
DEFINE_FIELD( m_vecBaseVelocity, FIELD_VECTOR ),
DEFINE_FIELD( m_iEFlags, FIELD_INTEGER ),
DEFINE_FIELD( m_flGravity, FIELD_FLOAT ),
// DEFINE_FIELD( m_ModelInstance, FIELD_SHORT ),
DEFINE_FIELD( m_flProxyRandomValue, FIELD_FLOAT ),
// DEFINE_FIELD( m_PredictableID, FIELD_INTEGER ),
// DEFINE_FIELD( m_pPredictionContext, FIELD_POINTER ),
// Stuff specific to rendering and therefore not to be copied back and forth
// DEFINE_PRED_FIELD( m_clrRender, color32, FTYPEDESC_INSENDTABLE ),
// DEFINE_FIELD( m_bReadyToDraw, FIELD_BOOLEAN ),
// DEFINE_FIELD( anim, CLatchedAnim ),
// DEFINE_FIELD( mouth, CMouthInfo ),
// DEFINE_FIELD( GetAbsOrigin(), FIELD_VECTOR ),
// DEFINE_FIELD( GetAbsAngles(), FIELD_VECTOR ),
// DEFINE_FIELD( m_nNumAttachments, FIELD_SHORT ),
// DEFINE_FIELD( m_pAttachmentAngles, FIELD_VECTOR ),
// DEFINE_FIELD( m_pAttachmentOrigin, FIELD_VECTOR ),
// DEFINE_FIELD( m_listentry, CSerialEntity ),
// DEFINE_FIELD( m_ShadowHandle, ClientShadowHandle_t ),
// DEFINE_FIELD( m_hThink, ClientThinkHandle_t ),
// Definitely private and not copied around
// DEFINE_FIELD( m_bPredictable, FIELD_BOOLEAN ),
// DEFINE_FIELD( m_CollisionGroup, FIELD_INTEGER ),
// DEFINE_FIELD( m_DataChangeEventRef, FIELD_INTEGER ),
#if !defined( CLIENT_DLL )
// DEFINE_FIELD( m_bPredictionEligible, FIELD_BOOLEAN ),
#endif
END_PREDICTION_DATA()
//-----------------------------------------------------------------------------
// Helper functions.
//-----------------------------------------------------------------------------
void SpewInterpolatedVar( CInterpolatedVar< Vector > *pVar )
{
Msg( "--------------------------------------------------\n" );
int i = pVar->GetHead();
Vector v0(0, 0, 0);
CApparentVelocity<Vector> apparent(v0);
float prevtime = 0.0f;
while ( 1 )
{
float changetime;
Vector *pVal = pVar->GetHistoryValue( i, changetime );
if ( !pVal )
break;
float vel = apparent.AddSample( changetime, *pVal );
Msg( "%6.6f: (%.2f %.2f %.2f), vel: %.2f [dt %.1f]\n", changetime, VectorExpand( *pVal ), vel, prevtime == 0.0f ? 0.0f : 1000.0f * ( changetime - prevtime ) );
i = pVar->GetNext( i );
prevtime = changetime;
}
Msg( "--------------------------------------------------\n" );
}
void SpewInterpolatedVar( CInterpolatedVar< Vector > *pVar, float flNow, float flInterpAmount, bool bSpewAllEntries = true )
{
float target = flNow - flInterpAmount;
Msg( "--------------------------------------------------\n" );
int i = pVar->GetHead();
Vector v0(0, 0, 0);
CApparentVelocity<Vector> apparent(v0);
float newtime = 999999.0f;
Vector newVec( 0, 0, 0 );
bool bSpew = true;
while ( 1 )
{
float changetime;
Vector *pVal = pVar->GetHistoryValue( i, changetime );
if ( !pVal )
break;
if ( bSpew && target >= changetime )
{
Vector o;
pVar->DebugInterpolate( &o, flNow );
bool bInterp = newtime != 999999.0f;
float frac = 0.0f;
char desc[ 32 ];
if ( bInterp )
{
frac = ( target - changetime ) / ( newtime - changetime );
Q_snprintf( desc, sizeof( desc ), "interpolated [%.2f]", frac );
}
else
{
bSpew = true;
int savei = i;
i = pVar->GetNext( i );
float oldtertime = 0.0f;
pVar->GetHistoryValue( i, oldtertime );
if ( changetime != oldtertime )
{
frac = ( target - changetime ) / ( changetime - oldtertime );
}
Q_snprintf( desc, sizeof( desc ), "extrapolated [%.2f]", frac );
i = savei;
}
if ( bSpew )
{
Msg( " > %6.6f: (%.2f %.2f %.2f) %s for %.1f msec\n",
target,
VectorExpand( o ),
desc,
1000.0f * ( target - changetime ) );
bSpew = false;
}
}
float vel = apparent.AddSample( changetime, *pVal );
if ( bSpewAllEntries )
{
Msg( " %6.6f: (%.2f %.2f %.2f), vel: %.2f [dt %.1f]\n", changetime, VectorExpand( *pVal ), vel, newtime == 999999.0f ? 0.0f : 1000.0f * ( newtime - changetime ) );
}
i = pVar->GetNext( i );
newtime = changetime;
newVec = *pVal;
}
Msg( "--------------------------------------------------\n" );
}
void SpewInterpolatedVar( CInterpolatedVar< float > *pVar )
{
Msg( "--------------------------------------------------\n" );
int i = pVar->GetHead();
CApparentVelocity<float> apparent(0.0f);
while ( 1 )
{
float changetime;
float *pVal = pVar->GetHistoryValue( i, changetime );
if ( !pVal )
break;
float vel = apparent.AddSample( changetime, *pVal );
Msg( "%6.6f: (%.2f), vel: %.2f\n", changetime, *pVal, vel );
i = pVar->GetNext( i );
}
Msg( "--------------------------------------------------\n" );
}
template<class T>
void GetInterpolatedVarTimeRange( CInterpolatedVar<T> *pVar, float &flMin, float &flMax )
{
flMin = 1e23;
flMax = -1e23;
int i = pVar->GetHead();
Vector v0(0, 0, 0);
CApparentVelocity<Vector> apparent(v0);
while ( 1 )
{
float changetime;
if ( !pVar->GetHistoryValue( i, changetime ) )
return;
flMin = MIN( flMin, changetime );
flMax = MAX( flMax, changetime );
i = pVar->GetNext( i );
}
}
//-----------------------------------------------------------------------------
// Global methods related to when abs data is correct
//-----------------------------------------------------------------------------
void C_BaseEntity::SetAbsQueriesValid( bool bValid )
{
// @MULTICORE: Always allow in worker threads, assume higher level code is handling correctly
if ( !ThreadInMainThread() )
return;
if ( !bValid )
{
s_bAbsQueriesValid = false;
}
else
{
s_bAbsQueriesValid = true;
}
}
bool C_BaseEntity::IsAbsQueriesValid( void )
{
if ( !ThreadInMainThread() )
return true;
return s_bAbsQueriesValid;
}
void C_BaseEntity::PushEnableAbsRecomputations( bool bEnable )
{
if ( !ThreadInMainThread() )
return;
if ( g_iAbsRecomputationStackPos < ARRAYSIZE( g_bAbsRecomputationStack ) )
{
g_bAbsRecomputationStack[g_iAbsRecomputationStackPos] = s_bAbsRecomputationEnabled;
++g_iAbsRecomputationStackPos;
s_bAbsRecomputationEnabled = bEnable;
}
else
{
Assert( false );
}
}
void C_BaseEntity::PopEnableAbsRecomputations()
{
if ( !ThreadInMainThread() )
return;
if ( g_iAbsRecomputationStackPos > 0 )
{
--g_iAbsRecomputationStackPos;
s_bAbsRecomputationEnabled = g_bAbsRecomputationStack[g_iAbsRecomputationStackPos];
}
else
{
Assert( false );
}
}
void C_BaseEntity::EnableAbsRecomputations( bool bEnable )
{
if ( !ThreadInMainThread() )
return;
// This should only be called at the frame level. Use PushEnableAbsRecomputations
// if you're blocking out a section of code.
Assert( g_iAbsRecomputationStackPos == 0 );
s_bAbsRecomputationEnabled = bEnable;
}
bool C_BaseEntity::IsAbsRecomputationsEnabled()
{
if ( !ThreadInMainThread() )
return true;
return s_bAbsRecomputationEnabled;
}
int C_BaseEntity::GetTextureFrameIndex( void )
{
return m_iTextureFrameIndex;
}
void C_BaseEntity::SetTextureFrameIndex( int iIndex )
{
m_iTextureFrameIndex = iIndex;
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *map -
//-----------------------------------------------------------------------------
void C_BaseEntity::Interp_SetupMappings( VarMapping_t *map )
{
if( !map )
return;
int c = map->m_Entries.Count();
for ( int i = 0; i < c; i++ )
{
VarMapEntry_t *e = &map->m_Entries[ i ];
IInterpolatedVar *watcher = e->watcher;
void *data = e->data;
int type = e->type;
watcher->Setup( data, type );
watcher->SetInterpolationAmount( GetInterpolationAmount( watcher->GetType() ) );
}
}
void C_BaseEntity::Interp_RestoreToLastNetworked( VarMapping_t *map )
{
VPROF( "C_BaseEntity::Interp_RestoreToLastNetworked" );
PREDICTION_TRACKVALUECHANGESCOPE_ENTITY( this, "restoretolastnetworked" );
Vector oldOrigin = GetLocalOrigin();
QAngle oldAngles = GetLocalAngles();
Vector oldVel = GetLocalVelocity();
int c = map->m_Entries.Count();
for ( int i = 0; i < c; i++ )
{
VarMapEntry_t *e = &map->m_Entries[ i ];
IInterpolatedVar *watcher = e->watcher;
watcher->RestoreToLastNetworked();
}
BaseInterpolatePart2( oldOrigin, oldAngles, oldVel, 0 );
}
void C_BaseEntity::Interp_UpdateInterpolationAmounts( VarMapping_t *map )
{
if( !map )
return;
int c = map->m_Entries.Count();
for ( int i = 0; i < c; i++ )
{
VarMapEntry_t *e = &map->m_Entries[ i ];
IInterpolatedVar *watcher = e->watcher;
watcher->SetInterpolationAmount( GetInterpolationAmount( watcher->GetType() ) );
}
}
void C_BaseEntity::Interp_HierarchyUpdateInterpolationAmounts()
{
Interp_UpdateInterpolationAmounts( GetVarMapping() );
for ( C_BaseEntity *pChild = FirstMoveChild(); pChild; pChild = pChild->NextMovePeer() )
{
pChild->Interp_HierarchyUpdateInterpolationAmounts();
}
}
inline int C_BaseEntity::Interp_Interpolate( VarMapping_t *map, float currentTime )
{
int bNoMoreChanges = 1;
if ( currentTime < map->m_lastInterpolationTime )
{
for ( int i = 0; i < map->m_nInterpolatedEntries; i++ )
{
VarMapEntry_t *e = &map->m_Entries[ i ];
e->m_bNeedsToInterpolate = true;
}
}
map->m_lastInterpolationTime = currentTime;
for ( int i = 0; i < map->m_nInterpolatedEntries; i++ )
{
VarMapEntry_t *e = &map->m_Entries[ i ];
if ( !e->m_bNeedsToInterpolate )
continue;
IInterpolatedVar *watcher = e->watcher;
Assert( !( watcher->GetType() & EXCLUDE_AUTO_INTERPOLATE ) );
if ( watcher->Interpolate( currentTime ) )
e->m_bNeedsToInterpolate = false;
else
bNoMoreChanges = 0;
}
return bNoMoreChanges;
}
//-----------------------------------------------------------------------------
// Functions.
//-----------------------------------------------------------------------------
C_BaseEntity::C_BaseEntity() :
m_iv_vecOrigin( "C_BaseEntity::m_iv_vecOrigin" ),
m_iv_angRotation( "C_BaseEntity::m_iv_angRotation" ),
m_iv_vecVelocity( "C_BaseEntity::m_iv_vecVelocity" )
{
m_pAttributes = NULL;
AddVar( &m_vecOrigin, &m_iv_vecOrigin, LATCH_SIMULATION_VAR );
AddVar( &m_angRotation, &m_iv_angRotation, LATCH_SIMULATION_VAR );
// Removing this until we figure out why velocity introduces view hitching.
// One possible fix is removing the player->ResetLatched() call in CGameMovement::FinishDuck(),
// but that re-introduces a third-person hitching bug. One possible cause is the abrupt change
// in player size/position that occurs when ducking, and how prediction tries to work through that.
//
// AddVar( &m_vecVelocity, &m_iv_vecVelocity, LATCH_SIMULATION_VAR );
m_DataChangeEventRef = -1;
m_EntClientFlags = 0;
m_iParentAttachment = 0;
m_nRenderFXBlend = 255;
SetPredictionEligible( false );
m_bPredictable = false;
m_bSimulatedEveryTick = false;
m_bAnimatedEveryTick = false;
m_pPhysicsObject = NULL;
#ifdef _DEBUG
m_vecAbsOrigin = vec3_origin;
m_angAbsRotation = vec3_angle;
m_vecNetworkOrigin.Init();
m_angNetworkAngles.Init();
m_vecAbsOrigin.Init();
// m_vecAbsAngVelocity.Init();
m_vecVelocity.Init();
m_vecAbsVelocity.Init();
m_vecViewOffset.Init();
m_vecBaseVelocity.Init();
m_iCurrentThinkContext = NO_THINK_CONTEXT;
#endif
m_nSimulationTick = -1;
// Assume drawing everything
m_bReadyToDraw = true;
m_flProxyRandomValue = 0.0f;
m_fBBoxVisFlags = 0;
#if !defined( NO_ENTITY_PREDICTION )
m_pPredictionContext = NULL;
#endif
//NOTE: not virtual! we are in the constructor!
C_BaseEntity::Clear();
m_InterpolationListEntry = 0xFFFF;
m_TeleportListEntry = 0xFFFF;
#ifndef NO_TOOLFRAMEWORK
m_bEnabledInToolView = true;
m_bToolRecording = false;
m_ToolHandle = 0;
m_nLastRecordedFrame = -1;
m_bRecordInTools = true;
#endif
#ifdef TF_CLIENT_DLL
m_bValidatedOwner = false;
m_bDeemedInvalid = false;
m_bWasDeemedInvalid = false;
#endif
ParticleProp()->Init( this );
}
//-----------------------------------------------------------------------------
// Purpose:
// Input :
// Output :
//-----------------------------------------------------------------------------
C_BaseEntity::~C_BaseEntity()
{
Term();
ClearDataChangedEvent( m_DataChangeEventRef );
#if !defined( NO_ENTITY_PREDICTION )
delete m_pPredictionContext;
#endif
RemoveFromInterpolationList();
RemoveFromTeleportList();
}
void C_BaseEntity::Clear( void )
{
m_bDormant = true;
m_nCreationTick = -1;
m_RefEHandle.Term();
m_ModelInstance = MODEL_INSTANCE_INVALID;
m_ShadowHandle = CLIENTSHADOW_INVALID_HANDLE;
m_hRender = INVALID_CLIENT_RENDER_HANDLE;
m_hThink = INVALID_THINK_HANDLE;
m_AimEntsListHandle = INVALID_AIMENTS_LIST_HANDLE;