Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
5.2 KiB

  1. //========= Copyright � 1996-2006, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Manages the server moving things back in time to match up to where clients thought they were
  4. // when the client commited an action
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #ifndef _PLAYER_LAG_COMPENSATION_H_
  9. #define _PLAYER_LAG_COMPENSATION_H_
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. #include "igamesystem.h"
  14. #include "ilagcompensationmanager.h"
  15. #include "utllinkedlist.h"
  16. #define MAX_LAYER_RECORDS (CBaseAnimatingOverlay::MAX_OVERLAYS)
  17. struct LayerRecord
  18. {
  19. int m_sequence;
  20. float m_cycle;
  21. float m_weight;
  22. int m_order;
  23. LayerRecord()
  24. {
  25. Clear();
  26. }
  27. LayerRecord( const LayerRecord& src )
  28. {
  29. m_sequence = src.m_sequence;
  30. m_cycle = src.m_cycle;
  31. m_weight = src.m_weight;
  32. m_order = src.m_order;
  33. }
  34. void Clear()
  35. {
  36. m_sequence = 0;
  37. m_cycle = 0;
  38. m_weight = 0;
  39. m_order = 0;
  40. }
  41. };
  42. struct LagRecord
  43. {
  44. public:
  45. LagRecord()
  46. {
  47. Clear();
  48. }
  49. LagRecord( const LagRecord& src )
  50. {
  51. m_fFlags = src.m_fFlags;
  52. m_vecOrigin = src.m_vecOrigin;
  53. m_vecAngles = src.m_vecAngles;
  54. m_vecMins = src.m_vecMins;
  55. m_vecMaxs = src.m_vecMaxs;
  56. m_flSimulationTime = src.m_flSimulationTime;
  57. for( int layerIndex = 0; layerIndex < MAX_LAYER_RECORDS; ++layerIndex )
  58. {
  59. m_layerRecords[layerIndex] = src.m_layerRecords[layerIndex];
  60. }
  61. m_masterSequence = src.m_masterSequence;
  62. m_masterCycle = src.m_masterCycle;
  63. for( int i=0; i<MAXSTUDIOPOSEPARAM; i++ )
  64. {
  65. m_flPoseParameters[i] = src.m_flPoseParameters[i];
  66. }
  67. }
  68. void Clear()
  69. {
  70. m_fFlags = 0;
  71. m_vecOrigin.Init();
  72. m_vecAngles.Init();
  73. m_vecMins.Init();
  74. m_vecMaxs.Init();
  75. m_flSimulationTime = -1;
  76. m_masterSequence = 0;
  77. m_masterCycle = 0;
  78. for( int layerIndex = 0; layerIndex < MAX_LAYER_RECORDS; ++layerIndex )
  79. {
  80. m_layerRecords[layerIndex].Clear();
  81. }
  82. for( int i=0; i<MAXSTUDIOPOSEPARAM; i++ )
  83. {
  84. m_flPoseParameters[i] = 0;
  85. }
  86. }
  87. // Did player die this frame
  88. int m_fFlags;
  89. // Player position, orientation and bbox
  90. Vector m_vecOrigin;
  91. QAngle m_vecAngles;
  92. Vector m_vecMins;
  93. Vector m_vecMaxs;
  94. float m_flSimulationTime;
  95. // Player animation details, so we can get the legs in the right spot.
  96. LayerRecord m_layerRecords[MAX_LAYER_RECORDS];
  97. int m_masterSequence;
  98. float m_masterCycle;
  99. float m_flPoseParameters[MAXSTUDIOPOSEPARAM];
  100. };
  101. typedef CUtlFixedLinkedList< LagRecord > LagRecordList;
  102. //-----------------------------------------------------------------------------
  103. class CLagCompensationManager : public CAutoGameSystemPerFrame, public ILagCompensationManager
  104. {
  105. public:
  106. CLagCompensationManager( char const *name ) :
  107. CAutoGameSystemPerFrame( name ),
  108. m_CompensatedEntities( 0, 0, DefLessFunc( EHANDLE ) ),
  109. m_AdditionalEntities( 0, 0, DefLessFunc( EHANDLE ) )
  110. {
  111. m_bNeedToRestore = false;
  112. m_weaponRange = 0.0f;
  113. m_isCurrentlyDoingCompensation = false;
  114. }
  115. // IServerSystem stuff
  116. virtual void Shutdown()
  117. {
  118. ClearHistory();
  119. }
  120. virtual void LevelShutdownPostEntity()
  121. {
  122. ClearHistory();
  123. }
  124. // called after entities think
  125. virtual void FrameUpdatePostEntityThink();
  126. // ILagCompensationManager stuff
  127. // Called during player movement to set up/restore after lag compensation
  128. void StartLagCompensation( CBasePlayer *player, LagCompensationType lagCompensationType, const Vector& weaponPos = vec3_origin, const QAngle &weaponAngles = vec3_angle, float weaponRange = 0.0f );
  129. void FinishLagCompensation( CBasePlayer *player );
  130. // Mappers can flag certain additional entities to lag compensate, this handles them
  131. virtual void AddAdditionalEntity( CBaseEntity *pEntity );
  132. virtual void RemoveAdditionalEntity( CBaseEntity *pEntity );
  133. void RecordDataIntoTrack( CBaseEntity *entity, LagRecordList *track, bool wantsAnims );
  134. bool BacktrackEntity( CBaseEntity *entity, float flTargetTime, LagRecordList *track, LagRecord *restore, LagRecord *change, bool wantsAnims );
  135. void RestoreEntityFromRecords( CBaseEntity *entity, LagRecord *restore, LagRecord *change, bool wantsAnims );
  136. private:
  137. void ClearHistory()
  138. {
  139. FOR_EACH_MAP( m_CompensatedEntities, i )
  140. {
  141. delete m_CompensatedEntities[ i ];
  142. }
  143. m_CompensatedEntities.Purge();
  144. }
  145. struct EntityLagData
  146. {
  147. EntityLagData() : m_bRestoreEntity( false )
  148. {
  149. }
  150. // True if lag compensation altered entity data
  151. bool m_bRestoreEntity;
  152. // keep a list of lag records for each player
  153. LagRecordList m_LagRecords;
  154. // Entity data before we moved him back
  155. LagRecord m_RestoreData;
  156. // Entity data where we moved him back
  157. LagRecord m_ChangeData;
  158. };
  159. CUtlMap< EHANDLE, EntityLagData * > m_CompensatedEntities;
  160. // True if at least one entity was changed
  161. bool m_bNeedToRestore;
  162. CBasePlayer *m_pCurrentPlayer; // The player we are doing lag compensation for
  163. LagCompensationType m_lagCompensationType;
  164. Vector m_weaponPos;
  165. QAngle m_weaponAngles;
  166. float m_weaponRange;
  167. bool m_isCurrentlyDoingCompensation; // Sentinel to prevent calling StartLagCompensation a second time before a Finish.
  168. // List of additional entities flagged by mappers for lag compensation (shouldn't be more than a few)
  169. CUtlRBTree< EHANDLE > m_AdditionalEntities;
  170. };
  171. #endif