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.

219 lines
6.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: A base class that deals with four-wheel vehicles
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef FOUR_WHEEL_VEHICLE_PHYSICS_H
  8. #define FOUR_WHEEL_VEHICLE_PHYSICS_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vphysics/vehicles.h"
  13. #include "vcollide_parse.h"
  14. #include "datamap.h"
  15. #include "vehicle_sounds.h"
  16. // in/sec to miles/hour
  17. #define INS2MPH_SCALE ( 3600 * (1/5280.0f) * (1/12.0f) )
  18. #define INS2MPH(x) ( (x) * INS2MPH_SCALE )
  19. #define MPH2INS(x) ( (x) * (1/INS2MPH_SCALE) )
  20. class CBaseAnimating;
  21. class CFourWheelServerVehicle;
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. //-----------------------------------------------------------------------------
  25. class CFourWheelVehiclePhysics
  26. {
  27. public:
  28. DECLARE_DATADESC();
  29. CFourWheelVehiclePhysics( CBaseAnimating *pOuter );
  30. ~CFourWheelVehiclePhysics ();
  31. // Call Precache + Spawn from the containing entity's Precache + Spawn methods
  32. void Precache();
  33. void Spawn();
  34. void SetOuter( CBaseAnimating *pOuter, CFourWheelServerVehicle *pServerVehicle );
  35. // Initializes the vehicle physics so we can drive it
  36. bool Initialize( const char *pScriptName, unsigned int nVehicleType );
  37. void Teleport( matrix3x4_t& relativeTransform );
  38. bool VPhysicsUpdate( IPhysicsObject *pPhysics );
  39. bool Think();
  40. void PlaceWheelDust( int wheelIndex, bool ignoreSpeed = false );
  41. void DrawDebugGeometryOverlays();
  42. int DrawDebugTextOverlays( int nOffset );
  43. // Updates the controls based on user input
  44. void UpdateDriverControls( CUserCmd *cmd, float flFrameTime );
  45. // Various steering parameters
  46. void SetThrottle( float flThrottle );
  47. void SetMaxThrottle( float flMaxThrottle );
  48. void SetMaxReverseThrottle( float flMaxThrottle );
  49. void SetSteering( float flSteering, float flSteeringRate );
  50. void SetSteeringDegrees( float flDegrees );
  51. void SetAction( float flAction );
  52. void TurnOn( );
  53. void TurnOff();
  54. void ReleaseHandbrake();
  55. void SetHandbrake( bool bBrake );
  56. bool IsOn() const { return m_bIsOn; }
  57. void ResetControls();
  58. void SetBoost( float flBoost );
  59. bool UpdateBooster( void );
  60. void SetHasBrakePedal( bool bHasBrakePedal );
  61. // Engine
  62. void SetDisableEngine( bool bDisable );
  63. bool IsEngineDisabled( void ) { return m_pVehicle->IsEngineDisabled(); }
  64. // Enable/Disable Motion
  65. void EnableMotion( void );
  66. void DisableMotion( void );
  67. // Shared code to compute the vehicle view position
  68. void GetVehicleViewPosition( const char *pViewAttachment, float flPitchFactor, Vector *pAbsPosition, QAngle *pAbsAngles );
  69. IPhysicsObject *GetWheel( int iWheel ) { return m_pWheels[iWheel]; }
  70. int GetSpeed() const;
  71. int GetMaxSpeed() const;
  72. int GetRPM() const;
  73. float GetThrottle() const;
  74. bool HasBoost() const;
  75. int BoostTimeLeft() const;
  76. bool IsBoosting( void );
  77. float GetHLSpeed() const;
  78. float GetSteering() const;
  79. float GetSteeringDegrees() const;
  80. IPhysicsVehicleController* GetVehicle(void) { return m_pVehicle; }
  81. float GetWheelBaseHeight(int wheelIndex) { return m_wheelBaseHeight[wheelIndex]; }
  82. float GetWheelTotalHeight(int wheelIndex) { return m_wheelTotalHeight[wheelIndex]; }
  83. IPhysicsVehicleController *GetVehicleController() { return m_pVehicle; }
  84. const vehicleparams_t &GetVehicleParams( void ) { return m_pVehicle->GetVehicleParams(); }
  85. const vehicle_controlparams_t &GetVehicleControls( void ) { return m_controls; }
  86. const vehicle_operatingparams_t &GetVehicleOperatingParams( void ) { return m_pVehicle->GetOperatingParams(); }
  87. int VPhysicsGetObjectList( IPhysicsObject **pList, int listMax );
  88. private:
  89. // engine sounds
  90. void CalcWheelData( vehicleparams_t &vehicle );
  91. void SteeringRest( float carSpeed, const vehicleparams_t &vehicleData );
  92. void SteeringTurn( float carSpeed, const vehicleparams_t &vehicleData, bool bTurnLeft, bool bBrake, bool bThrottle );
  93. void SteeringTurnAnalog( float carSpeed, const vehicleparams_t &vehicleData, float sidemove );
  94. // A couple wrapper methods to perform common operations
  95. int LookupPoseParameter( const char *szName );
  96. float GetPoseParameter( int iParameter );
  97. float SetPoseParameter( int iParameter, float flValue );
  98. bool GetAttachment ( const char *szName, Vector &origin, QAngle &angles );
  99. void InitializePoseParameters();
  100. bool ParseVehicleScript( const char *pScriptName, solid_t &solid, vehicleparams_t &vehicle );
  101. private:
  102. // This is the entity that contains this class
  103. CHandle<CBaseAnimating> m_pOuter;
  104. CFourWheelServerVehicle *m_pOuterServerVehicle;
  105. vehicle_controlparams_t m_controls;
  106. IPhysicsVehicleController *m_pVehicle;
  107. // Vehicle state info
  108. int m_nSpeed;
  109. int m_nLastSpeed;
  110. int m_nRPM;
  111. float m_fLastBoost;
  112. int m_nBoostTimeLeft;
  113. int m_nHasBoost;
  114. float m_maxThrottle;
  115. float m_flMaxRevThrottle;
  116. float m_flMaxSpeed;
  117. float m_actionSpeed;
  118. IPhysicsObject *m_pWheels[4];
  119. int m_wheelCount;
  120. Vector m_wheelPosition[4];
  121. QAngle m_wheelRotation[4];
  122. float m_wheelBaseHeight[4];
  123. float m_wheelTotalHeight[4];
  124. int m_poseParameters[12];
  125. float m_actionValue;
  126. float m_actionScale;
  127. float m_debugRadius;
  128. float m_throttleRate;
  129. float m_throttleStartTime;
  130. float m_throttleActiveTime;
  131. float m_turboTimer;
  132. float m_flVehicleVolume; // NPC driven vehicles used louder sounds
  133. bool m_bIsOn;
  134. bool m_bLastThrottle;
  135. bool m_bLastBoost;
  136. bool m_bLastSkid;
  137. };
  138. //-----------------------------------------------------------------------------
  139. // Physics state..
  140. //-----------------------------------------------------------------------------
  141. inline int CFourWheelVehiclePhysics::GetSpeed() const
  142. {
  143. return m_nSpeed;
  144. }
  145. inline int CFourWheelVehiclePhysics::GetMaxSpeed() const
  146. {
  147. return INS2MPH(m_pVehicle->GetVehicleParams().engine.maxSpeed);
  148. }
  149. inline int CFourWheelVehiclePhysics::GetRPM() const
  150. {
  151. return m_nRPM;
  152. }
  153. inline float CFourWheelVehiclePhysics::GetThrottle() const
  154. {
  155. return m_controls.throttle;
  156. }
  157. inline bool CFourWheelVehiclePhysics::HasBoost() const
  158. {
  159. return m_nHasBoost != 0;
  160. }
  161. inline int CFourWheelVehiclePhysics::BoostTimeLeft() const
  162. {
  163. return m_nBoostTimeLeft;
  164. }
  165. inline void CFourWheelVehiclePhysics::SetOuter( CBaseAnimating *pOuter, CFourWheelServerVehicle *pServerVehicle )
  166. {
  167. m_pOuter = pOuter;
  168. m_pOuterServerVehicle = pServerVehicle;
  169. }
  170. float RemapAngleRange( float startInterval, float endInterval, float value );
  171. #define ROLL_CURVE_ZERO 5 // roll less than this is clamped to zero
  172. #define ROLL_CURVE_LINEAR 45 // roll greater than this is copied out
  173. #define PITCH_CURVE_ZERO 10 // pitch less than this is clamped to zero
  174. #define PITCH_CURVE_LINEAR 45 // pitch greater than this is copied out
  175. #endif // FOUR_WHEEL_VEHICLE_PHYSICS_H