Team Fortress 2 Source Code as on 22/4/2020
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.

150 lines
4.9 KiB

  1. //========= Copyright 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. //-----------------------------------------------------------------------------
  21. // Purpose: Implements prediction in the client .dll
  22. //-----------------------------------------------------------------------------
  23. class CPrediction : public IPrediction
  24. {
  25. // Construction
  26. public:
  27. DECLARE_CLASS_GAMEROOT( CPrediction, IPrediction );
  28. CPrediction( void );
  29. virtual ~CPrediction( void );
  30. virtual void Init( void );
  31. virtual void Shutdown( void );
  32. // Implement IPrediction
  33. public:
  34. virtual void Update
  35. (
  36. int startframe, // World update ( un-modded ) most recently received
  37. bool validframe, // Is frame data valid
  38. int incoming_acknowledged, // Last command acknowledged to have been run by server (un-modded)
  39. int outgoing_command // Last command (most recent) sent to server (un-modded)
  40. );
  41. virtual void OnReceivedUncompressedPacket( void );
  42. virtual void PreEntityPacketReceived( int commands_acknowledged, int current_world_update_packet );
  43. virtual void PostEntityPacketReceived( void );
  44. virtual void PostNetworkDataReceived( int commands_acknowledged );
  45. virtual bool InPrediction( void ) const;
  46. virtual bool IsFirstTimePredicted( void ) const;
  47. #if !defined( NO_ENTITY_PREDICTION )
  48. virtual int GetIncomingPacketNumber( void ) const;
  49. #endif
  50. float GetIdealPitch( void ) const
  51. {
  52. return m_flIdealPitch;
  53. }
  54. // The engine needs to be able to access a few predicted values
  55. virtual void GetViewOrigin( Vector& org );
  56. virtual void SetViewOrigin( Vector& org );
  57. virtual void GetViewAngles( QAngle& ang );
  58. virtual void SetViewAngles( QAngle& ang );
  59. virtual void GetLocalViewAngles( QAngle& ang );
  60. virtual void SetLocalViewAngles( QAngle& ang );
  61. virtual void RunCommand( C_BasePlayer *player, CUserCmd *ucmd, IMoveHelper *moveHelper );
  62. // Internal
  63. protected:
  64. virtual void SetupMove( C_BasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  65. virtual void FinishMove( C_BasePlayer *player, CUserCmd *ucmd, CMoveData *move );
  66. virtual void SetIdealPitch ( C_BasePlayer *player, const Vector& origin, const QAngle& angles, const Vector& viewheight );
  67. void CheckError( int commands_acknowledged );
  68. // Called before and after any movement processing
  69. void StartCommand( C_BasePlayer *player, CUserCmd *cmd );
  70. void FinishCommand( C_BasePlayer *player );
  71. // Helpers to call pre and post think for player, and to call think if a think function is set
  72. void RunPreThink( C_BasePlayer *player );
  73. void RunThink (C_BasePlayer *ent, double frametime );
  74. void RunPostThink( C_BasePlayer *player );
  75. private:
  76. virtual void _Update
  77. (
  78. bool received_new_world_update,
  79. bool validframe, // Is frame data valid
  80. int incoming_acknowledged, // Last command acknowledged to have been run by server (un-modded)
  81. int outgoing_command // Last command (most recent) sent to server (un-modded)
  82. );
  83. // Actually does the prediction work, returns false if an error occurred
  84. bool PerformPrediction( bool received_new_world_update, C_BasePlayer *localPlayer, int incoming_acknowledged, int outgoing_command );
  85. void ShiftIntermediateDataForward( int slots_to_remove, int previous_last_slot );
  86. void RestoreEntityToPredictedFrame( int predicted_frame );
  87. int ComputeFirstCommandToExecute( bool received_new_world_update, int incoming_acknowledged, int outgoing_command );
  88. void DumpEntity( C_BaseEntity *ent, int commands_acknowledged );
  89. void ShutdownPredictables( void );
  90. void ReinitPredictables( void );
  91. void RemoveStalePredictedEntities( int last_command_packet );
  92. void RestoreOriginalEntityState( void );
  93. void RunSimulation( int current_command, float curtime, CUserCmd *cmd, C_BasePlayer *localPlayer );
  94. void Untouch( void );
  95. void StorePredictionResults( int predicted_frame );
  96. bool ShouldDumpEntity( C_BaseEntity *ent );
  97. void SmoothViewOnMovingPlatform( C_BasePlayer *pPlayer, Vector& offset );
  98. #if !defined( NO_ENTITY_PREDICTION )
  99. // Data
  100. protected:
  101. // Last object the player was standing on
  102. CHandle< C_BaseEntity > m_hLastGround;
  103. private:
  104. bool m_bInPrediction;
  105. bool m_bFirstTimePredicted;
  106. bool m_bOldCLPredictValue;
  107. bool m_bEnginePaused;
  108. // Last network origin for local player
  109. int m_nPreviousStartFrame;
  110. int m_nCommandsPredicted;
  111. int m_nServerCommandsAcknowledged;
  112. int m_bPreviousAckHadErrors;
  113. int m_nIncomingPacketNumber;
  114. #endif
  115. float m_flIdealPitch;
  116. };
  117. extern CPrediction *prediction;
  118. #endif // PREDICTION_H