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.

201 lines
6.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $Workfile: $
  6. // $Date: $
  7. // $NoKeywords: $
  8. //=============================================================================//
  9. #if !defined( PREDICTION_H )
  10. #define PREDICTION_H
  11. #ifdef _WIN32
  12. #pragma once
  13. #endif
  14. #include "mathlib/vector.h"
  15. #include "iprediction.h"
  16. #include "c_baseplayer.h"
  17. #include "cdll_bounded_cvars.h"
  18. class CMoveData;
  19. class CUserCmd;
  20. class CPDumpPanel;
  21. //-----------------------------------------------------------------------------
  22. // Purpose: Implements prediction in the client .dll
  23. //-----------------------------------------------------------------------------
  24. class CPrediction : public IPrediction
  25. {
  26. // Construction
  27. public:
  28. DECLARE_CLASS_GAMEROOT( CPrediction, IPrediction );
  29. CPrediction( void );
  30. virtual ~CPrediction( void );
  31. virtual void Init( void );
  32. virtual void Shutdown( void );
  33. // Implement IPrediction
  34. public:
  35. virtual void Update
  36. (
  37. int startframe, // World update ( un-modded ) most recently received
  38. bool validframe, // Is frame data valid
  39. int incoming_acknowledged, // Last command acknowledged to have been run by server (un-modded)
  40. int outgoing_command // Last command (most recent) sent to server (un-modded)
  41. );
  42. virtual void OnReceivedUncompressedPacket( void );
  43. virtual void PreEntityPacketReceived( int commands_acknowledged, int current_world_update_packet, int server_ticks_elapsed );
  44. virtual void PostEntityPacketReceived( void );
  45. virtual void PostNetworkDataReceived( int commands_acknowledged );
  46. virtual bool InPrediction( void ) const;
  47. virtual bool IsFirstTimePredicted( void ) const;
  48. virtual int GetLastAcknowledgedCommandNumber( void ) const;
  49. #if defined( KEEP_COMMAND_REPREDICTION_COUNT )
  50. virtual unsigned int GetRepredictionCount( void ) const;
  51. #endif
  52. #if !defined( NO_ENTITY_PREDICTION )
  53. virtual int GetIncomingPacketNumber( void ) const;
  54. #endif
  55. float GetIdealPitch( int nSlot ) const
  56. {
  57. if ( nSlot == -1 )
  58. {
  59. Assert( 0 );
  60. return 0.0f;
  61. }
  62. return m_Split[ nSlot ].m_flIdealPitch;
  63. }
  64. // The engine needs to be able to access a few predicted values
  65. virtual void GetViewOrigin( Vector& org );
  66. virtual void SetViewOrigin( Vector& org );
  67. virtual void GetViewAngles( QAngle& ang );
  68. virtual void SetViewAngles( QAngle& ang );
  69. virtual void GetLocalViewAngles( QAngle& ang );
  70. virtual void SetLocalViewAngles( QAngle& ang );
  71. virtual void CheckMovingGround( C_BasePlayer *player, double frametime );
  72. virtual void RunCommand( C_BasePlayer *player, CUserCmd *ucmd, IMoveHelper *moveHelper );
  73. float GetSavedTime() const;
  74. // Internal
  75. protected:
  76. virtual void SetupMove( C_BasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  77. virtual void FinishMove( C_BasePlayer *player, CUserCmd *ucmd, CMoveData *move );
  78. virtual void SetIdealPitch ( int nSlot, C_BasePlayer *player, const Vector& origin, const QAngle& angles, const Vector& viewheight );
  79. virtual void CheckError( int nSlot, C_BasePlayer *player, int commands_acknowledged );
  80. // Called before and after any movement processing
  81. void StartCommand( C_BasePlayer *player, CUserCmd *cmd );
  82. void FinishCommand( C_BasePlayer *player );
  83. // Helpers to call pre and post think for player, and to call think if a think function is set
  84. void RunPreThink( C_BasePlayer *player );
  85. void RunThink (C_BasePlayer *ent, double frametime );
  86. void RunPostThink( C_BasePlayer *player );
  87. private:
  88. virtual void _Update
  89. (
  90. int nSlot,
  91. bool received_new_world_update,
  92. bool validframe, // Is frame data valid
  93. int incoming_acknowledged, // Last command acknowledged to have been run by server (un-modded)
  94. int outgoing_command // Last command (most recent) sent to server (un-modded)
  95. );
  96. // Actually does the prediction work, returns false if an error occurred
  97. bool PerformPrediction( int nSlot, C_BasePlayer *localPlayer, bool received_new_world_update, int incoming_acknowledged, int outgoing_command );
  98. void ShiftIntermediateDataForward( int nSlot, int slots_to_remove, int previous_last_slot );
  99. void ShiftFirstPredictedIntermediateDataForward( int nSlot, int slots_to_remove );
  100. void RestoreEntityToPredictedFrame( int nSlot, int predicted_frame );
  101. int ComputeFirstCommandToExecute( int nSlot, bool received_new_world_update, int incoming_acknowledged, int outgoing_command );
  102. void DumpEntity( C_BaseEntity *ent, int commands_acknowledged );
  103. void ShutdownPredictables( void );
  104. void ReinitPredictables( void );
  105. void RemoveStalePredictedEntities( int nSlot, int last_command_packet );
  106. void RestoreOriginalEntityState( int nSlot );
  107. void RunSimulation( int current_command, float curtime, CUserCmd *cmd, C_BasePlayer *localPlayer );
  108. void Untouch( int nSlot );
  109. void StorePredictionResults( int nSlot, int predicted_frame );
  110. bool ShouldDumpEntity( C_BaseEntity *ent );
  111. void SmoothViewOnMovingPlatform( C_BasePlayer *pPlayer, Vector& offset );
  112. void ResetSimulationTick();
  113. void ShowPredictionListEntry( int listRow, int showlist, C_BaseEntity *ent, int &totalsize, int &totalsize_intermediate );
  114. void FinishPredictionList( int listRow, int showlist, int totalsize, int totalsize_intermediate );
  115. void CheckPredictConvar();
  116. #if !defined( NO_ENTITY_PREDICTION )
  117. // Data
  118. protected:
  119. // Last object the player was standing on
  120. CHandle< C_BaseEntity > m_hLastGround;
  121. private:
  122. bool m_bInPrediction;
  123. bool m_bOldCLPredictValue;
  124. bool m_bEnginePaused;
  125. int m_nPreviousStartFrame;
  126. int m_nIncomingPacketNumber;
  127. float m_flLastServerWorldTimeStamp;
  128. // Last network origin for local player
  129. struct Split_t
  130. {
  131. Split_t()
  132. {
  133. m_bFirstTimePredicted = false;
  134. m_nCommandsPredicted = 0;
  135. m_nServerCommandsAcknowledged = 0;
  136. m_bPreviousAckHadErrors = false;
  137. m_flIdealPitch = 0.0f;
  138. m_nLastCommandAcknowledged = 0;
  139. m_bPreviousAckErrorTriggersFullLatchReset = false;
  140. }
  141. bool m_bFirstTimePredicted;
  142. int m_nCommandsPredicted;
  143. int m_nServerCommandsAcknowledged;
  144. int m_bPreviousAckHadErrors;
  145. float m_flIdealPitch;
  146. int m_nLastCommandAcknowledged;
  147. bool m_bPreviousAckErrorTriggersFullLatchReset;
  148. CUtlVector< CHandle< CBaseEntity > > m_EntsWithPredictionErrorsInLastAck;
  149. bool m_bPerformedTickShift;
  150. #if defined( KEEP_COMMAND_REPREDICTION_COUNT )
  151. unsigned int m_Debug_RepredictionCount;
  152. #endif
  153. };
  154. Split_t m_Split[ MAX_SPLITSCREEN_PLAYERS ];
  155. #endif
  156. CGlobalVarsBase m_SavedVars;
  157. bool m_bPlayerOriginTypedescriptionSearched;
  158. CUtlVector< const typedescription_t * > m_PlayerOriginTypeDescription; // A vector in cases where the .x, .y, and .z are separately listed
  159. CPDumpPanel *m_pPDumpPanel;
  160. };
  161. extern CPrediction *prediction;
  162. #endif // PREDICTION_H