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.

282 lines
10 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "cbase.h"
  7. #include "hud.h"
  8. #include "c_props.h"
  9. #include "iclientvehicle.h"
  10. #include <vgui_controls/Controls.h>
  11. #include <color.h>
  12. #include "vehicle_choreo_generic_shared.h"
  13. #include "vehicle_viewblend_shared.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. extern float RemapAngleRange( float startInterval, float endInterval, float value );
  17. #define ROLL_CURVE_ZERO 5 // roll less than this is clamped to zero
  18. #define ROLL_CURVE_LINEAR 45 // roll greater than this is copied out
  19. #define PITCH_CURVE_ZERO 10 // pitch less than this is clamped to zero
  20. #define PITCH_CURVE_LINEAR 45 // pitch greater than this is copied out
  21. // spline in between
  22. //-----------------------------------------------------------------------------
  23. // Purpose:
  24. //-----------------------------------------------------------------------------
  25. class C_PropVehicleChoreoGeneric : public C_DynamicProp, public IClientVehicle
  26. {
  27. DECLARE_CLASS( C_PropVehicleChoreoGeneric, C_DynamicProp );
  28. public:
  29. DECLARE_CLIENTCLASS();
  30. DECLARE_DATADESC();
  31. C_PropVehicleChoreoGeneric();
  32. void PreDataUpdate( DataUpdateType_t updateType );
  33. void PostDataUpdate( DataUpdateType_t updateType );
  34. public:
  35. // IClientVehicle overrides.
  36. virtual void GetVehicleViewPosition( int nRole, Vector *pOrigin, QAngle *pAngles, float *pFOV = NULL );
  37. virtual void GetVehicleFOV( float &flFOV )
  38. {
  39. flFOV = m_flFOV;
  40. }
  41. virtual void DrawHudElements();
  42. virtual bool IsPassengerUsingStandardWeapons( int nRole = VEHICLE_ROLE_DRIVER ) { return false; }
  43. virtual void UpdateViewAngles( C_BasePlayer *pLocalPlayer, CUserCmd *pCmd );
  44. virtual C_BaseCombatCharacter *GetPassenger( int nRole );
  45. virtual int GetPassengerRole( C_BaseCombatCharacter *pPassenger );
  46. virtual void GetVehicleClipPlanes( float &flZNear, float &flZFar ) const;
  47. virtual int GetPrimaryAmmoType() const { return -1; }
  48. virtual int GetPrimaryAmmoCount() const { return -1; }
  49. virtual int GetPrimaryAmmoClip() const { return -1; }
  50. virtual bool PrimaryAmmoUsesClips() const { return false; }
  51. virtual int GetJoystickResponseCurve() const { return 0; }
  52. public:
  53. // C_BaseEntity overrides.
  54. virtual IClientVehicle* GetClientVehicle() { return this; }
  55. virtual C_BaseEntity *GetVehicleEnt() { return this; }
  56. virtual void SetupMove( C_BasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move ) {}
  57. virtual void ProcessMovement( C_BasePlayer *pPlayer, CMoveData *pMoveData ) {}
  58. virtual void FinishMove( C_BasePlayer *player, CUserCmd *ucmd, CMoveData *move ) {}
  59. virtual bool IsPredicted() const { return false; }
  60. virtual void ItemPostFrame( C_BasePlayer *pPlayer ) {}
  61. virtual bool IsSelfAnimating() { return false; };
  62. private:
  63. void UpdateViewClamps( void );
  64. float m_flPitchMaxCurrent;
  65. float m_flPitchMinCurrent;
  66. float m_flYawMaxCurrent;
  67. float m_flYawMinCurrent;
  68. CHandle<C_BasePlayer> m_hPlayer;
  69. CHandle<C_BasePlayer> m_hPrevPlayer;
  70. bool m_bEnterAnimOn;
  71. bool m_bExitAnimOn;
  72. Vector m_vecEyeExitEndpoint;
  73. bool m_bForceEyesToAttachment;
  74. float m_flFOV; // The current FOV (changes during entry/exit anims).
  75. ViewSmoothingData_t m_ViewSmoothingData;
  76. vehicleview_t m_vehicleView;
  77. };
  78. IMPLEMENT_CLIENTCLASS_DT(C_PropVehicleChoreoGeneric, DT_PropVehicleChoreoGeneric, CPropVehicleChoreoGeneric)
  79. RecvPropEHandle( RECVINFO(m_hPlayer) ),
  80. RecvPropBool( RECVINFO( m_bEnterAnimOn ) ),
  81. RecvPropBool( RECVINFO( m_bExitAnimOn ) ),
  82. RecvPropBool( RECVINFO( m_bForceEyesToAttachment ) ),
  83. RecvPropVector( RECVINFO( m_vecEyeExitEndpoint ) ),
  84. RecvPropBool( RECVINFO( m_vehicleView.bClampEyeAngles ) ),
  85. RecvPropFloat( RECVINFO( m_vehicleView.flPitchCurveZero ) ),
  86. RecvPropFloat( RECVINFO( m_vehicleView.flPitchCurveLinear ) ),
  87. RecvPropFloat( RECVINFO( m_vehicleView.flRollCurveZero ) ),
  88. RecvPropFloat( RECVINFO( m_vehicleView.flRollCurveLinear ) ),
  89. RecvPropFloat( RECVINFO( m_vehicleView.flFOV ) ),
  90. RecvPropFloat( RECVINFO( m_vehicleView.flYawMin ) ),
  91. RecvPropFloat( RECVINFO( m_vehicleView.flYawMax ) ),
  92. RecvPropFloat( RECVINFO( m_vehicleView.flPitchMin ) ),
  93. RecvPropFloat( RECVINFO( m_vehicleView.flPitchMax ) ),
  94. END_RECV_TABLE()
  95. BEGIN_DATADESC( C_PropVehicleChoreoGeneric )
  96. DEFINE_EMBEDDED( m_ViewSmoothingData ),
  97. END_DATADESC()
  98. //-----------------------------------------------------------------------------
  99. // Purpose:
  100. //-----------------------------------------------------------------------------
  101. C_PropVehicleChoreoGeneric::C_PropVehicleChoreoGeneric( void )
  102. {
  103. memset( &m_ViewSmoothingData, 0, sizeof( m_ViewSmoothingData ) );
  104. m_ViewSmoothingData.pVehicle = this;
  105. m_ViewSmoothingData.flPitchCurveZero = PITCH_CURVE_ZERO;
  106. m_ViewSmoothingData.flPitchCurveLinear = PITCH_CURVE_LINEAR;
  107. m_ViewSmoothingData.flRollCurveZero = ROLL_CURVE_ZERO;
  108. m_ViewSmoothingData.flRollCurveLinear = ROLL_CURVE_LINEAR;
  109. m_flFOV = 0;
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Purpose:
  113. // Input : updateType -
  114. //-----------------------------------------------------------------------------
  115. void C_PropVehicleChoreoGeneric::PreDataUpdate( DataUpdateType_t updateType )
  116. {
  117. BaseClass::PreDataUpdate( updateType );
  118. m_hPrevPlayer = m_hPlayer;
  119. }
  120. //-----------------------------------------------------------------------------
  121. // Purpose:
  122. //-----------------------------------------------------------------------------
  123. void C_PropVehicleChoreoGeneric::PostDataUpdate( DataUpdateType_t updateType )
  124. {
  125. BaseClass::PostDataUpdate( updateType );
  126. if ( updateType == DATA_UPDATE_CREATED )
  127. {
  128. m_flPitchMaxCurrent = m_vehicleView.flPitchMax;
  129. m_flPitchMinCurrent = m_vehicleView.flPitchMin;
  130. m_flYawMaxCurrent = m_vehicleView.flYawMax;
  131. m_flYawMinCurrent = m_vehicleView.flYawMin;
  132. }
  133. if ( !m_hPlayer && m_hPrevPlayer )
  134. {
  135. // They have just exited the vehicle.
  136. // Sometimes we never reach the end of our exit anim, such as if the
  137. // animation doesn't have fadeout 0 specified in the QC, so we fail to
  138. // catch it in VehicleViewSmoothing. Catch it here instead.
  139. m_ViewSmoothingData.bWasRunningAnim = false;
  140. //There's no need to "smooth" the view when leaving the vehicle so just set this here so the stair code doesn't get confused.
  141. m_hPrevPlayer->SetOldPlayerZ( m_hPrevPlayer->GetLocalOrigin().z );
  142. }
  143. m_ViewSmoothingData.bClampEyeAngles = m_vehicleView.bClampEyeAngles;
  144. m_ViewSmoothingData.flFOV = m_vehicleView.flFOV;
  145. }
  146. //-----------------------------------------------------------------------------
  147. // Purpose:
  148. //-----------------------------------------------------------------------------
  149. C_BaseCombatCharacter *C_PropVehicleChoreoGeneric::GetPassenger( int nRole )
  150. {
  151. if ( nRole == VEHICLE_ROLE_DRIVER )
  152. return m_hPlayer.Get();
  153. return NULL;
  154. }
  155. //-----------------------------------------------------------------------------
  156. // Returns the role of the passenger
  157. //-----------------------------------------------------------------------------
  158. int C_PropVehicleChoreoGeneric::GetPassengerRole( C_BaseCombatCharacter *pPassenger )
  159. {
  160. if ( m_hPlayer.Get() == pPassenger )
  161. return VEHICLE_ROLE_DRIVER;
  162. return VEHICLE_ROLE_NONE;
  163. }
  164. //-----------------------------------------------------------------------------
  165. // Purpose: Modify the player view/camera while in a vehicle
  166. //-----------------------------------------------------------------------------
  167. void C_PropVehicleChoreoGeneric::GetVehicleViewPosition( int nRole, Vector *pAbsOrigin, QAngle *pAbsAngles, float *pFOV /*=NULL*/ )
  168. {
  169. SharedVehicleViewSmoothing( m_hPlayer,
  170. pAbsOrigin, pAbsAngles,
  171. m_bEnterAnimOn, m_bExitAnimOn,
  172. m_vecEyeExitEndpoint,
  173. &m_ViewSmoothingData,
  174. pFOV, m_bForceEyesToAttachment );
  175. }
  176. //-----------------------------------------------------------------------------
  177. // Purpose:
  178. // Input : pLocalPlayer -
  179. // pCmd -
  180. //-----------------------------------------------------------------------------
  181. void C_PropVehicleChoreoGeneric::UpdateViewAngles( C_BasePlayer *pLocalPlayer, CUserCmd *pCmd )
  182. {
  183. int eyeAttachmentIndex = LookupAttachment( "vehicle_driver_eyes" );
  184. Vector vehicleEyeOrigin;
  185. QAngle vehicleEyeAngles;
  186. GetAttachmentLocal( eyeAttachmentIndex, vehicleEyeOrigin, vehicleEyeAngles );
  187. UpdateViewClamps();
  188. // Limit the yaw.
  189. float flAngleDiff = AngleDiff( pCmd->viewangles.y, vehicleEyeAngles.y );
  190. flAngleDiff = clamp( flAngleDiff, m_flYawMinCurrent, m_flYawMaxCurrent );
  191. pCmd->viewangles.y = vehicleEyeAngles.y + flAngleDiff;
  192. // Limit the pitch -- don't let them look down into the empty pod!
  193. flAngleDiff = AngleDiff( pCmd->viewangles.x, vehicleEyeAngles.x );
  194. flAngleDiff = clamp( flAngleDiff, m_flPitchMinCurrent, m_flPitchMaxCurrent );
  195. pCmd->viewangles.x = vehicleEyeAngles.x + flAngleDiff;
  196. }
  197. //-----------------------------------------------------------------------------
  198. // Futzes with the clip planes
  199. //-----------------------------------------------------------------------------
  200. void C_PropVehicleChoreoGeneric::GetVehicleClipPlanes( float &flZNear, float &flZFar ) const
  201. {
  202. // Pod doesn't need to adjust the clip planes.
  203. //flZNear = 6;
  204. }
  205. //-----------------------------------------------------------------------------
  206. // Renders hud elements
  207. //-----------------------------------------------------------------------------
  208. void C_PropVehicleChoreoGeneric::DrawHudElements( )
  209. {
  210. }
  211. float InterpolateViewClamp( float flValue, float flDesired )
  212. {
  213. if ( CloseEnough ( flValue, flDesired, 1e-3 ) == false )
  214. {
  215. float delta = flDesired - flValue;
  216. delta = delta * ExponentialDecay( 0.2, 0.5, gpGlobals->frametime );
  217. return flDesired - delta;
  218. }
  219. else
  220. {
  221. return flDesired;
  222. }
  223. }
  224. void C_PropVehicleChoreoGeneric::UpdateViewClamps( void )
  225. {
  226. m_flPitchMaxCurrent = InterpolateViewClamp( m_flPitchMaxCurrent, m_vehicleView.flPitchMax );
  227. m_flPitchMinCurrent = InterpolateViewClamp( m_flPitchMinCurrent, m_vehicleView.flPitchMin );
  228. m_flYawMaxCurrent = InterpolateViewClamp( m_flYawMaxCurrent, m_vehicleView.flYawMax );
  229. m_flYawMinCurrent = InterpolateViewClamp( m_flYawMinCurrent, m_vehicleView.flYawMin );
  230. }