forked from ValveSoftware/source-sdk-2013
-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathprediction.cpp
More file actions
1952 lines (1637 loc) · 55.1 KB
/
prediction.cpp
File metadata and controls
1952 lines (1637 loc) · 55.1 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:
//
// $NoKeywords: $
//=============================================================================//
#include "cbase.h"
#include "prediction.h"
#include "igamemovement.h"
#include "prediction_private.h"
#include "ivrenderview.h"
#include "iinput.h"
#include "usercmd.h"
#include <vgui_controls/Controls.h>
#include <vgui/ISurface.h>
#include <vgui/IScheme.h>
#include "hud.h"
#include "iclientvehicle.h"
#include "in_buttons.h"
#include "con_nprint.h"
#include "hud_pdump.h"
#include "datacache/imdlcache.h"
#ifdef HL2_CLIENT_DLL
#include "c_basehlplayer.h"
#endif
#include "tier0/vprof.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#ifdef MAPBASE
// This turned out to be causing major issues with VPhysics collision.
// It's deactivated until a fix is found.
// See player_command.cpp as well.
//#define PLAYER_COMMAND_FIX 1
#endif
IPredictionSystem *IPredictionSystem::g_pPredictionSystems = NULL;
#if !defined( NO_ENTITY_PREDICTION )
ConVar cl_predictweapons ( "cl_predictweapons","1", FCVAR_USERINFO | FCVAR_NOT_CONNECTED, "Perform client side prediction of weapon effects." );
ConVar cl_lagcompensation ( "cl_lagcompensation","1", FCVAR_USERINFO | FCVAR_NOT_CONNECTED, "Perform server side lag compensation of weapon firing events." );
ConVar cl_showerror ( "cl_showerror", "0", 0, "Show prediction errors, 2 for above plus detailed field deltas." );
static ConVar cl_idealpitchscale ( "cl_idealpitchscale", "0.8", FCVAR_ARCHIVE );
static ConVar cl_predictionlist ( "cl_predictionlist", "0", FCVAR_CHEAT, "Show which entities are predicting\n" );
static ConVar cl_predictionentitydump( "cl_pdump", "-1", FCVAR_CHEAT, "Dump info about this entity to screen." );
static ConVar cl_predictionentitydumpbyclass( "cl_pclass", "", FCVAR_CHEAT, "Dump entity by prediction classname." );
static ConVar cl_pred_optimize( "cl_pred_optimize", "2", 0, "Optimize for not copying data if didn't receive a network update (1), and also for not repredicting if there were no errors (2)." );
static ConVar cl_pred_doresetlatch( "cl_pred_doresetlatch", "1", 0 );
#endif
extern IGameMovement *g_pGameMovement;
extern CMoveData *g_pMoveData;
void COM_Log( char *pszFile, const char *fmt, ...);
typedescription_t *FindFieldByName( const char *fieldname, datamap_t *dmap );
#if !defined( NO_ENTITY_PREDICTION )
//-----------------------------------------------------------------------------
// Purpose: For debugging, find predictable by classname
// Input : *classname -
// Output : static C_BaseEntity
//-----------------------------------------------------------------------------
static C_BaseEntity *FindPredictableByGameClass( const char *classname )
{
// Walk backward due to deletion from UtlVector
int c = predictables->GetPredictableCount();
int i;
for ( i = 0; i < c; i++ )
{
C_BaseEntity *ent = predictables->GetPredictable( i );
if ( !ent )
continue;
// Don't do anything to truly predicted things (like player and weapons )
if ( !FClassnameIs( ent, classname ) )
continue;
return ent;
}
return NULL;
}
#endif
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CPrediction::CPrediction( void )
{
#if !defined( NO_ENTITY_PREDICTION )
m_bInPrediction = false;
m_bFirstTimePredicted = false;
m_nIncomingPacketNumber = 0;
m_flIdealPitch = 0.0f;
m_nPreviousStartFrame = -1;
m_nCommandsPredicted = 0;
m_nServerCommandsAcknowledged = 0;
m_bPreviousAckHadErrors = false;
m_bPreviousAckErrorTriggersFullLatchReset = false;
#endif
}
CPrediction::~CPrediction( void )
{
}
void CPrediction::Init( void )
{
#if !defined( NO_ENTITY_PREDICTION )
m_bOldCLPredictValue = cl_predict->GetInt();
#endif
}
void CPrediction::Shutdown( void )
{
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPrediction::CheckError( int commands_acknowledged )
{
#if !defined( NO_ENTITY_PREDICTION )
C_BasePlayer *player;
Vector origin;
Vector delta;
float len;
static int pos = 0;
// Not in the game yet
if ( !engine->IsInGame() )
return;
// Not running prediction
if ( !cl_predict->GetInt() )
return;
player = C_BasePlayer::GetLocalPlayer();
if ( !player )
return;
// Not predictable yet (flush entity packet?)
if ( !player->IsIntermediateDataAllocated() )
return;
origin = player->GetNetworkOrigin();
const void *slot = player->GetPredictedFrame( commands_acknowledged - 1 );
if ( !slot )
return;
// Find the origin field in the database
typedescription_t *td = FindFieldByName( "m_vecNetworkOrigin", player->GetPredDescMap() );
Assert( td );
if ( !td )
return;
Vector predicted_origin;
memcpy( (Vector *)&predicted_origin, (Vector *)( (byte *)slot + td->fieldOffset[ PC_DATA_PACKED ] ), sizeof( Vector ) );
// Compare what the server returned with what we had predicted it to be
VectorSubtract ( predicted_origin, origin, delta );
len = VectorLength( delta );
if (len > MAX_PREDICTION_ERROR )
{
// A teleport or something, clear out error
len = 0;
}
else
{
if ( len > MIN_PREDICTION_EPSILON )
{
player->NotePredictionError( delta );
if ( cl_showerror.GetInt() >= 1 )
{
con_nprint_t np;
np.fixed_width_font = true;
np.color[0] = 1.0f;
np.color[1] = 0.95f;
np.color[2] = 0.7f;
np.index = 20 + ( ++pos % 20 );
np.time_to_live = 2.0f;
engine->Con_NXPrintf( &np, "pred error %6.3f units (%6.3f %6.3f %6.3f)", len, delta.x, delta.y, delta.z );
}
}
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPrediction::ShutdownPredictables( void )
{
#if !defined( NO_ENTITY_PREDICTION )
// Transfer intermediate data from other predictables
int c = predictables->GetPredictableCount();
int i;
int shutdown_count = 0;
int release_count = 0;
for ( i = c - 1; i >= 0 ; i-- )
{
C_BaseEntity *ent = predictables->GetPredictable( i );
if ( !ent )
continue;
// Shutdown predictables
if ( ent->GetPredictable() )
{
ent->ShutdownPredictable();
shutdown_count++;
}
// Otherwise, release client created entities
else
{
ent->Release();
release_count++;
}
}
if ( ( release_count > 0 ) ||
( shutdown_count > 0 ) )
{
Msg( "Shutdown %i predictable entities and %i client-created entities\n",
shutdown_count,
release_count );
}
// All gone now...
Assert( predictables->GetPredictableCount() == 0 );
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPrediction::ReinitPredictables( void )
{
#if !defined( NO_ENTITY_PREDICTION )
// Go through all entities and init any eligible ones
int i;
int c = ClientEntityList().GetHighestEntityIndex();
for ( i = 0; i <= c; i++ )
{
C_BaseEntity *e = ClientEntityList().GetBaseEntity( i );
if ( !e )
continue;
if ( e->GetPredictable() )
continue;
e->CheckInitPredictable( "ReinitPredictables" );
}
Msg( "Reinitialized %i predictable entities\n",
predictables->GetPredictableCount() );
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CPrediction::OnReceivedUncompressedPacket( void )
{
#if !defined( NO_ENTITY_PREDICTION )
m_nCommandsPredicted = 0;
m_nServerCommandsAcknowledged = 0;
m_nPreviousStartFrame = -1;
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : commands_acknowledged -
// current_world_update_packet -
// Output : void CPrediction::PreEntityPacketReceived
//-----------------------------------------------------------------------------
void CPrediction::PreEntityPacketReceived ( int commands_acknowledged, int current_world_update_packet )
{
#if !defined( NO_ENTITY_PREDICTION )
#if defined( _DEBUG )
char sz[ 32 ];
Q_snprintf( sz, sizeof( sz ), "preentitypacket%d", commands_acknowledged );
PREDICTION_TRACKVALUECHANGESCOPE( sz );
#endif
VPROF( "CPrediction::PreEntityPacketReceived" );
// Cache off incoming packet #
m_nIncomingPacketNumber = current_world_update_packet;
// Don't screw up memory of current player from history buffers if not filling in history buffers
// during prediction!!!
if ( !cl_predict->GetInt() )
{
ShutdownPredictables();
return;
}
C_BasePlayer *current = C_BasePlayer::GetLocalPlayer();
// No local player object?
if ( !current )
return;
// Transfer intermediate data from other predictables
int c = predictables->GetPredictableCount();
int i;
for ( i = 0; i < c; i++ )
{
C_BaseEntity *ent = predictables->GetPredictable( i );
if ( !ent )
continue;
if ( !ent->GetPredictable() )
continue;
ent->PreEntityPacketReceived( commands_acknowledged );
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Called for every packet received( could be multiple times per frame)
//-----------------------------------------------------------------------------
void CPrediction::PostEntityPacketReceived( void )
{
#if !defined( NO_ENTITY_PREDICTION )
PREDICTION_TRACKVALUECHANGESCOPE( "postentitypacket" );
VPROF( "CPrediction::PostEntityPacketReceived" );
// Don't screw up memory of current player from history buffers if not filling in history buffers
// during prediction!!!
if ( !cl_predict->GetInt() )
return;
C_BasePlayer *current = C_BasePlayer::GetLocalPlayer();
// No local player object?
if ( !current )
return;
// Transfer intermediate data from other predictables
int c = predictables->GetPredictableCount();
int i;
for ( i = 0; i < c; i++ )
{
C_BaseEntity *ent = predictables->GetPredictable( i );
if ( !ent )
continue;
if ( !ent->GetPredictable() )
continue;
ent->PostEntityPacketReceived();
}
#endif
}
//-----------------------------------------------------------------------------
// Purpose:
// Input : *ent -
// Output : static bool
//-----------------------------------------------------------------------------
bool CPrediction::ShouldDumpEntity( C_BaseEntity *ent )
{
#if !defined( NO_ENTITY_PREDICTION )
int dump_entity = cl_predictionentitydump.GetInt();
if ( dump_entity != -1 )
{
bool dump = false;
if ( ent->entindex() == -1 )
{
dump = ( dump_entity == ent->entindex() ) ? true : false;
}
else
{
dump = ( ent->entindex() == dump_entity ) ? true : false;
}
if ( !dump )
{
return false;
}
}
else
{
if ( cl_predictionentitydumpbyclass.GetString()[ 0 ] == 0 )
return false;
if ( !FClassnameIs( ent, cl_predictionentitydumpbyclass.GetString() ) )
return false;
}
return true;
#else
return false;
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Called at the end of the frame if any packets were received
// Input : error_check -
// last_predicted -
//-----------------------------------------------------------------------------
void CPrediction::PostNetworkDataReceived( int commands_acknowledged )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::PostNetworkDataReceived" );
bool error_check = ( commands_acknowledged > 0 ) ? true : false;
#if defined( _DEBUG )
char szDebug[32];
Q_snprintf( szDebug, sizeof( szDebug ), "postnetworkdata%d", commands_acknowledged );
PREDICTION_TRACKVALUECHANGESCOPE( szDebug );
#endif
#ifndef _XBOX
CPDumpPanel *dump = GetPDumpPanel();
#endif
//Msg( "%i/%i ack %i commands/slot\n",
// gpGlobals->framecount,
// gpGlobals->tickcount,
// commands_acknowledged - 1 );
m_nServerCommandsAcknowledged += commands_acknowledged;
m_bPreviousAckHadErrors = false;
m_bPreviousAckErrorTriggersFullLatchReset = false;
m_EntsWithPredictionErrorsInLastAck.RemoveAll();
bool entityDumped = false;
C_BasePlayer *current = C_BasePlayer::GetLocalPlayer();
// No local player object?
if ( !current )
return;
// Don't screw up memory of current player from history buffers if not filling in history buffers
// during prediction!!!
if ( cl_predict->GetInt() )
{
int showlist = cl_predictionlist.GetInt();
int totalsize = 0;
int totalsize_intermediate = 0;
con_nprint_t np;
np.fixed_width_font = true;
np.color[0] = 0.8f;
np.color[1] = 1.0f;
np.color[2] = 1.0f;
np.time_to_live = 2.0f;
// Transfer intermediate data from other predictables
int c = predictables->GetPredictableCount();
int i;
for ( i = 0; i < c; i++ )
{
C_BaseEntity *ent = predictables->GetPredictable( i );
if ( !ent )
continue;
if ( ent->GetPredictable() )
{
if ( ent->PostNetworkDataReceived( m_nServerCommandsAcknowledged ) )
{
m_bPreviousAckHadErrors = true;
m_bPreviousAckErrorTriggersFullLatchReset |= ent->PredictionErrorShouldResetLatchedForAllPredictables() ? 1 : 0;
m_EntsWithPredictionErrorsInLastAck.AddToTail( ent );
}
}
if ( showlist )
{
char sz[32];
if ( ent->entindex() == -1 )
{
Q_snprintf( sz, sizeof( sz ), "handle %u", (unsigned int)ent->GetClientHandle().ToInt() );
}
else
{
Q_snprintf( sz, sizeof( sz ), "%i", ent->entindex() );
}
np.index = i;
if ( showlist >= 2 )
{
int size = GetClassMap().GetClassSize( ent->GetClassname() );
int intermediate_size = ent->GetIntermediateDataSize() * ( MULTIPLAYER_BACKUP + 1 );
engine->Con_NXPrintf( &np, "%15s %30s (%5i / %5i bytes): %15s",
sz,
ent->GetClassname(),
size,
intermediate_size,
ent->GetPredictable() ? "predicted" : "client created" );
totalsize += size;
totalsize_intermediate += intermediate_size;
}
else
{
engine->Con_NXPrintf( &np, "%15s %30s: %15s",
sz,
ent->GetClassname(),
ent->GetPredictable() ? "predicted" : "client created" );
}
}
#ifndef _XBOX
if ( error_check &&
!entityDumped &&
dump &&
ShouldDumpEntity( ent ) )
{
entityDumped = true;
dump->DumpEntity( ent, m_nServerCommandsAcknowledged );
}
#endif
}
if ( showlist >= 2 )
{
np.index = i++;
char sz1[32];
char sz2[32];
Q_strncpy( sz1, Q_pretifymem( (float)totalsize ), sizeof( sz1 ) );
Q_strncpy( sz2, Q_pretifymem( (float)totalsize_intermediate ), sizeof( sz2 ) );
engine->Con_NXPrintf( &np, "%15s %27s (%s / %s) %14s",
"totals:",
"",
sz1,
sz2,
"" );
}
// Zero out rest of list
if ( showlist )
{
while ( i < 20 )
{
engine->Con_NPrintf( i, "" );
i++;
}
}
if ( error_check )
{
CheckError( m_nServerCommandsAcknowledged );
}
}
// Can also look at regular entities
#ifndef _XBOX
int dumpentindex = cl_predictionentitydump.GetInt();
if ( dump && error_check && !entityDumped && dumpentindex != -1 )
{
int last_entity = ClientEntityList().GetHighestEntityIndex();
if ( dumpentindex >= 0 && dumpentindex <= last_entity )
{
C_BaseEntity *ent = ClientEntityList().GetBaseEntity( dumpentindex );
if ( ent )
{
dump->DumpEntity( ent, m_nServerCommandsAcknowledged );
entityDumped = true;
}
}
}
#endif
if ( cl_predict->GetBool() != m_bOldCLPredictValue )
{
if ( !m_bOldCLPredictValue )
{
ReinitPredictables();
}
m_nCommandsPredicted = 0;
m_nServerCommandsAcknowledged = 0;
m_nPreviousStartFrame = -1;
}
m_bOldCLPredictValue = cl_predict->GetInt();
#ifndef _XBOX
if ( dump && error_check && !entityDumped )
{
dump->Clear();
}
#endif
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Prepare for running prediction code
// Input : *ucmd -
// *from -
// *pHelper -
// &moveInput -
//-----------------------------------------------------------------------------
void CPrediction::SetupMove( C_BasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::SetupMove" );
move->m_bFirstRunOfFunctions = IsFirstTimePredicted();
move->m_bGameCodeMovedPlayer = false;
if ( player->GetPreviouslyPredictedOrigin() != player->GetNetworkOrigin() )
{
move->m_bGameCodeMovedPlayer = true;
}
move->m_nPlayerHandle = player->GetClientHandle();
move->m_vecVelocity = player->GetAbsVelocity();
move->SetAbsOrigin( player->GetNetworkOrigin() );
move->m_vecOldAngles = move->m_vecAngles;
move->m_nOldButtons = player->m_Local.m_nOldButtons;
move->m_flOldForwardMove = player->m_Local.m_flOldForwardMove;
move->m_flClientMaxSpeed = player->m_flMaxspeed;
move->m_vecAngles = ucmd->viewangles;
move->m_vecViewAngles = ucmd->viewangles;
move->m_nImpulseCommand = ucmd->impulse;
move->m_nButtons = ucmd->buttons;
CBaseEntity *pMoveParent = player->GetMoveParent();
if (!pMoveParent)
{
move->m_vecAbsViewAngles = move->m_vecViewAngles;
}
else
{
matrix3x4_t viewToParent, viewToWorld;
AngleMatrix( move->m_vecViewAngles, viewToParent );
ConcatTransforms( pMoveParent->EntityToWorldTransform(), viewToParent, viewToWorld );
MatrixAngles( viewToWorld, move->m_vecAbsViewAngles );
}
// Ingore buttons for movement if at controls
if (player->GetFlags() & FL_ATCONTROLS)
{
move->m_flForwardMove = 0;
move->m_flSideMove = 0;
move->m_flUpMove = 0;
}
else
{
move->m_flForwardMove = ucmd->forwardmove;
move->m_flSideMove = ucmd->sidemove;
move->m_flUpMove = ucmd->upmove;
}
IClientVehicle *pVehicle = player->GetVehicle();
if (pVehicle)
{
pVehicle->SetupMove( player, ucmd, pHelper, move );
}
// Copy constraint information
if ( player->m_hConstraintEntity )
move->m_vecConstraintCenter = player->m_hConstraintEntity->GetAbsOrigin();
else
move->m_vecConstraintCenter = player->m_vecConstraintCenter;
move->m_flConstraintRadius = player->m_flConstraintRadius;
move->m_flConstraintWidth = player->m_flConstraintWidth;
move->m_flConstraintSpeedFactor = player->m_flConstraintSpeedFactor;
#ifdef HL2_CLIENT_DLL
// Convert to HL2 data.
C_BaseHLPlayer *pHLPlayer = static_cast<C_BaseHLPlayer*>( player );
Assert( pHLPlayer );
CHLMoveData *pHLMove = static_cast<CHLMoveData*>( move );
Assert( pHLMove );
pHLMove->m_bIsSprinting = pHLPlayer->IsSprinting();
#endif
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Finish running prediction code
// Input : &move -
// *to -
//-----------------------------------------------------------------------------
void CPrediction::FinishMove( C_BasePlayer *player, CUserCmd *ucmd, CMoveData *move )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::FinishMove" );
player->m_RefEHandle = move->m_nPlayerHandle;
// misyl: was player->m_vecVelocity = move->m_vecVelocity;
// but that's totally WRONG !!! We need to update AbsVelocity here
// like in CPlayerMove as other code could use the abs and not local
// velocity and then overwrite it and cause a cascade of PRED errors!
player->SetAbsVelocity( move->m_vecVelocity );
player->m_vecNetworkOrigin = move->GetAbsOrigin();
player->SetPreviouslyPredictedOrigin( move->GetAbsOrigin() );
player->m_Local.m_nOldButtons = move->m_nButtons;
// NOTE: Don't copy this. the movement code modifies its local copy but is not expecting to be authoritative
//player->m_flMaxspeed = move->m_flClientMaxSpeed;
m_hLastGround = player->GetGroundEntity();
player->SetLocalOrigin( move->GetAbsOrigin() );
IClientVehicle *pVehicle = player->GetVehicle();
if (pVehicle)
{
pVehicle->FinishMove( player, ucmd, move );
}
// Sanity checks
if ( player->m_hConstraintEntity )
Assert( move->m_vecConstraintCenter == player->m_hConstraintEntity->GetAbsOrigin() );
else
Assert( move->m_vecConstraintCenter == player->m_vecConstraintCenter );
Assert( move->m_flConstraintRadius == player->m_flConstraintRadius );
Assert( move->m_flConstraintWidth == player->m_flConstraintWidth );
Assert( move->m_flConstraintSpeedFactor == player->m_flConstraintSpeedFactor );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Called before any movement processing
// Input : *player -
// *cmd -
//-----------------------------------------------------------------------------
void CPrediction::StartCommand( C_BasePlayer *player, CUserCmd *cmd )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::StartCommand" );
CPredictableId::ResetInstanceCounters();
player->m_pCurrentCommand = cmd;
C_BaseEntity::SetPredictionRandomSeed( cmd );
C_BaseEntity::SetPredictionPlayer( player );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Called after any movement processing
// Input : *player -
//-----------------------------------------------------------------------------
void CPrediction::FinishCommand( C_BasePlayer *player )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::FinishCommand" );
player->m_pCurrentCommand = NULL;
C_BaseEntity::SetPredictionRandomSeed( NULL );
C_BaseEntity::SetPredictionPlayer( NULL );
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Called before player thinks
// Input : *player -
// thinktime -
//-----------------------------------------------------------------------------
void CPrediction::RunPreThink( C_BasePlayer *player )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::RunPreThink" );
// Run think functions on the player
if ( !player->PhysicsRunThink() )
return;
// Called every frame to let game rules do any specific think logic for the player
// FIXME: Do we need to set up a client side version of the gamerules???
// g_pGameRules->PlayerThink( player );
player->PreThink();
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Runs the PLAYER's thinking code if time. There is some play in the exact time the think
// function will be called, because it is called before any movement is done
// in a frame. Not used for pushmove objects, because they must be exact.
// Returns false if the entity removed itself.
// Input : *ent -
// frametime -
// clienttimebase -
// Output : void CPlayerMove::RunThink
//-----------------------------------------------------------------------------
void CPrediction::RunThink (C_BasePlayer *player, double frametime )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::RunThink" );
int thinktick = player->GetNextThinkTick();
if ( thinktick <= 0 || thinktick > player->m_nTickBase )
return;
player->SetNextThink( TICK_NEVER_THINK );
// Think
player->Think();
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Called after player movement
// Input : *player -
// thinktime -
// frametime -
//-----------------------------------------------------------------------------
void CPrediction::RunPostThink( C_BasePlayer *player )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::RunPostThink" );
// Run post-think
player->PostThink();
#endif
}
//-----------------------------------------------------------------------------
// Purpose: Checks if the player is standing on a moving entity and adjusts velocity and
// basevelocity appropriately
// Input : *player -
// frametime -
//-----------------------------------------------------------------------------
void CPrediction::CheckMovingGround( C_BasePlayer *player, double frametime )
{
#if 0
CBaseEntity *groundentity;
if ( player->GetFlags() & FL_ONGROUND )
{
groundentity = player->GetGroundEntity();
if ( groundentity && ( groundentity->GetFlags() & FL_CONVEYOR) )
{
Vector vecNewVelocity;
groundentity->GetGroundVelocityToApply( vecNewVelocity );
if ( player->GetFlags() & FL_BASEVELOCITY )
{
vecNewVelocity += player->GetBaseVelocity();
}
player->SetBaseVelocity( vecNewVelocity );
player->AddFlag( FL_BASEVELOCITY );
}
}
#endif
if ( !( player->GetFlags() & FL_BASEVELOCITY ) )
{
// Apply momentum (add in half of the previous frame of velocity first)
player->ApplyAbsVelocityImpulse( (1.0 + ( frametime * 0.5 )) * player->GetBaseVelocity() );
player->SetBaseVelocity( vec3_origin );
}
player->RemoveFlag( FL_BASEVELOCITY );
}
//-----------------------------------------------------------------------------
// Purpose: Predicts a single movement command for player
// Input : *moveHelper -
// *player -
// *u -
//-----------------------------------------------------------------------------
void CPrediction::RunCommand( C_BasePlayer *player, CUserCmd *ucmd, IMoveHelper *moveHelper )
{
#if !defined( NO_ENTITY_PREDICTION )
VPROF( "CPrediction::RunCommand" );
#if defined( _DEBUG )
char sz[ 32 ];
Q_snprintf( sz, sizeof( sz ), "runcommand%04d", ucmd->command_number );
PREDICTION_TRACKVALUECHANGESCOPE( sz );
#endif
StartCommand( player, ucmd );
// Set globals appropriately
gpGlobals->curtime = player->m_nTickBase * TICK_INTERVAL;
gpGlobals->frametime = m_bEnginePaused ? 0 : TICK_INTERVAL;
#ifdef MAPBASE_MP
// Add and subtract buttons we're forcing on the player
ucmd->buttons |= player->m_afButtonForced;
ucmd->buttons &= ~player->m_afButtonDisabled;
#endif
g_pGameMovement->StartTrackPredictionErrors( player );
// TODO
// TODO: Check for impulse predicted?
// Do weapon selection
if ( ucmd->weaponselect != 0 )
{
C_BaseCombatWeapon *weapon = dynamic_cast< C_BaseCombatWeapon * >( CBaseEntity::Instance( ucmd->weaponselect ) );
if ( weapon )
{
player->SelectItem( weapon->GetName(), ucmd->weaponsubtype );
}
}
// Latch in impulse.
IClientVehicle *pVehicle = player->GetVehicle();
if ( ucmd->impulse )
{
// Discard impulse commands unless the vehicle allows them.
// FIXME: UsingStandardWeapons seems like a bad filter for this.
// The flashlight is an impulse command, for example.
if ( !pVehicle || player->UsingStandardWeaponsInVehicle() )
{
player->m_nImpulse = ucmd->impulse;
}
}
// Get button states
player->UpdateButtonState( ucmd->buttons );
// TODO
CheckMovingGround( player, gpGlobals->frametime );
// TODO
// g_pMoveData->m_vecOldAngles = player->pl.v_angle;
// Copy from command to player unless game .dll has set angle using fixangle
// if ( !player->pl.fixangle )
{
player->SetLocalViewAngles( ucmd->viewangles );
}
// Call standard client pre-think
RunPreThink( player );
// Call Think if one is set
RunThink( player, TICK_INTERVAL );
// Setup input.
{
SetupMove( player, ucmd, moveHelper, g_pMoveData );
}
// RUN MOVEMENT
if ( !pVehicle )
{
Assert( g_pGameMovement );
g_pGameMovement->ProcessMovement( player, g_pMoveData );
}
else
{
pVehicle->ProcessMovement( player, g_pMoveData );
}
#ifdef PLAYER_COMMAND_FIX
RunPostThink( player );
FinishMove( player, ucmd, g_pMoveData );
#else
FinishMove( player, ucmd, g_pMoveData );
VPROF_SCOPE_BEGIN( "moveHelper->ProcessImpacts(cl)" );
moveHelper->ProcessImpacts();
VPROF_SCOPE_END();
RunPostThink( player );
#endif
g_pGameMovement->FinishTrackPredictionErrors( player );
FinishCommand( player );
if ( gpGlobals->frametime > 0 )
{
player->m_nTickBase++;
}
#endif
}