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.

218 lines
6.7 KiB

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