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.

379 lines
12 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "c_prop_vehicle.h"
  9. #include "hud.h"
  10. #include <vgui_controls/Controls.h>
  11. #include <color.h>
  12. #include "view.h"
  13. #include "engine/ivdebugoverlay.h"
  14. #include "movevars_shared.h"
  15. #include "iviewrender.h"
  16. // memdbgon must be the last include file in a .cpp file!!!
  17. #include "tier0/memdbgon.h"
  18. int ScreenTransform( const Vector& point, Vector& screen );
  19. extern ConVar default_fov;
  20. extern ConVar joy_response_move_vehicle;
  21. IMPLEMENT_CLIENTCLASS_DT(C_PropVehicleDriveable, DT_PropVehicleDriveable, CPropVehicleDriveable)
  22. RecvPropEHandle( RECVINFO(m_hPlayer) ),
  23. RecvPropInt( RECVINFO( m_nSpeed ) ),
  24. RecvPropInt( RECVINFO( m_nRPM ) ),
  25. RecvPropFloat( RECVINFO( m_flThrottle ) ),
  26. RecvPropInt( RECVINFO( m_nBoostTimeLeft ) ),
  27. RecvPropInt( RECVINFO( m_nHasBoost ) ),
  28. RecvPropInt( RECVINFO( m_nScannerDisabledWeapons ) ),
  29. RecvPropInt( RECVINFO( m_nScannerDisabledVehicle ) ),
  30. RecvPropInt( RECVINFO( m_bEnterAnimOn ) ),
  31. RecvPropInt( RECVINFO( m_bExitAnimOn ) ),
  32. RecvPropInt( RECVINFO( m_bUnableToFire ) ),
  33. RecvPropVector( RECVINFO( m_vecEyeExitEndpoint ) ),
  34. RecvPropBool( RECVINFO( m_bHasGun ) ),
  35. RecvPropVector( RECVINFO( m_vecGunCrosshair ) ),
  36. END_RECV_TABLE()
  37. BEGIN_DATADESC( C_PropVehicleDriveable )
  38. DEFINE_EMBEDDED( m_ViewSmoothingData ),
  39. END_DATADESC()
  40. ConVar r_VehicleViewClamp( "r_VehicleViewClamp", "1", FCVAR_CHEAT );
  41. #define ROLL_CURVE_ZERO 20 // roll less than this is clamped to zero
  42. #define ROLL_CURVE_LINEAR 90 // roll greater than this is copied out
  43. #define PITCH_CURVE_ZERO 10 // pitch less than this is clamped to zero
  44. #define PITCH_CURVE_LINEAR 45 // pitch greater than this is copied out
  45. // spline in between
  46. //-----------------------------------------------------------------------------
  47. // Purpose: Constructor
  48. //-----------------------------------------------------------------------------
  49. C_PropVehicleDriveable::C_PropVehicleDriveable() :
  50. m_iv_vecGunCrosshair( "C_PropVehicleDriveable::m_iv_vecGunCrosshair" )
  51. {
  52. m_hPrevPlayer = NULL;
  53. memset( &m_ViewSmoothingData, 0, sizeof( m_ViewSmoothingData ) );
  54. m_ViewSmoothingData.pVehicle = this;
  55. m_ViewSmoothingData.bClampEyeAngles = true;
  56. m_ViewSmoothingData.bDampenEyePosition = true;
  57. m_ViewSmoothingData.flPitchCurveZero = PITCH_CURVE_ZERO;
  58. m_ViewSmoothingData.flPitchCurveLinear = PITCH_CURVE_LINEAR;
  59. m_ViewSmoothingData.flRollCurveZero = ROLL_CURVE_ZERO;
  60. m_ViewSmoothingData.flRollCurveLinear = ROLL_CURVE_LINEAR;
  61. m_ViewSmoothingData.flFOV = m_flFOV = default_fov.GetFloat();
  62. AddVar( &m_vecGunCrosshair, &m_iv_vecGunCrosshair, LATCH_SIMULATION_VAR );
  63. }
  64. //-----------------------------------------------------------------------------
  65. // Purpose: De-constructor
  66. //-----------------------------------------------------------------------------
  67. C_PropVehicleDriveable::~C_PropVehicleDriveable()
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. // By default all driveable vehicles use the curve defined by the convar.
  72. //-----------------------------------------------------------------------------
  73. int C_PropVehicleDriveable::GetJoystickResponseCurve() const
  74. {
  75. return joy_response_move_vehicle.GetInt();
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Purpose:
  79. //-----------------------------------------------------------------------------
  80. C_BaseCombatCharacter *C_PropVehicleDriveable::GetPassenger( int nRole )
  81. {
  82. if ( nRole == VEHICLE_ROLE_DRIVER )
  83. return m_hPlayer.Get();
  84. return NULL;
  85. }
  86. //-----------------------------------------------------------------------------
  87. // Returns the role of the passenger
  88. //-----------------------------------------------------------------------------
  89. int C_PropVehicleDriveable::GetPassengerRole( C_BaseCombatCharacter *pPassenger )
  90. {
  91. if ( m_hPlayer.Get() == pPassenger )
  92. return VEHICLE_ROLE_DRIVER;
  93. return VEHICLE_ROLE_NONE;
  94. }
  95. //-----------------------------------------------------------------------------
  96. // Purpose:
  97. // Input : updateType -
  98. //-----------------------------------------------------------------------------
  99. void C_PropVehicleDriveable::OnPreDataChanged( DataUpdateType_t updateType )
  100. {
  101. BaseClass::OnPreDataChanged( updateType );
  102. m_hPrevPlayer = m_hPlayer;
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Purpose:
  106. //-----------------------------------------------------------------------------
  107. void C_PropVehicleDriveable::OnDataChanged( DataUpdateType_t updateType )
  108. {
  109. BaseClass::OnDataChanged( updateType );
  110. if ( m_hPlayer && !m_hPrevPlayer )
  111. {
  112. OnEnteredVehicle( m_hPlayer );
  113. SetNextClientThink( CLIENT_THINK_ALWAYS );
  114. }
  115. else if ( !m_hPlayer && m_hPrevPlayer )
  116. {
  117. // NVNT notify haptics system of navigation exit
  118. OnExitedVehicle( m_hPrevPlayer );
  119. // They have just exited the vehicle.
  120. // Sometimes we never reach the end of our exit anim, such as if the
  121. // animation doesn't have fadeout 0 specified in the QC, so we fail to
  122. // catch it in VehicleViewSmoothing. Catch it here instead.
  123. m_ViewSmoothingData.bWasRunningAnim = false;
  124. SetNextClientThink( CLIENT_THINK_NEVER );
  125. }
  126. }
  127. //-----------------------------------------------------------------------------
  128. // Should this object cast render-to-texture shadows?
  129. //-----------------------------------------------------------------------------
  130. ShadowType_t C_PropVehicleDriveable::ShadowCastType()
  131. {
  132. CStudioHdr *pStudioHdr = GetModelPtr();
  133. if ( !pStudioHdr )
  134. return SHADOWS_NONE;
  135. if ( IsEffectActive(EF_NODRAW | EF_NOSHADOW) )
  136. return SHADOWS_NONE;
  137. // Always use render-to-texture. We'll always the dirty bits in our think function
  138. return SHADOWS_RENDER_TO_TEXTURE;
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Mark the shadow as dirty while the vehicle is being driven
  142. //-----------------------------------------------------------------------------
  143. void C_PropVehicleDriveable::ClientThink( void )
  144. {
  145. // The vehicle is always dirty owing to pose parameters while it's being driven.
  146. g_pClientShadowMgr->MarkRenderToTextureShadowDirty( GetShadowHandle() );
  147. }
  148. //-----------------------------------------------------------------------------
  149. // Purpose:
  150. //-----------------------------------------------------------------------------
  151. void C_PropVehicleDriveable::DampenEyePosition( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles )
  152. {
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: Modify the player view/camera while in a vehicle
  156. //-----------------------------------------------------------------------------
  157. void C_PropVehicleDriveable::GetVehicleViewPosition( int nRole, Vector *pAbsOrigin, QAngle *pAbsAngles, float *pFOV /*=NULL*/ )
  158. {
  159. SharedVehicleViewSmoothing( m_hPlayer,
  160. pAbsOrigin, pAbsAngles,
  161. m_bEnterAnimOn, m_bExitAnimOn,
  162. m_vecEyeExitEndpoint,
  163. &m_ViewSmoothingData,
  164. pFOV );
  165. }
  166. //-----------------------------------------------------------------------------
  167. // Futzes with the clip planes
  168. //-----------------------------------------------------------------------------
  169. void C_PropVehicleDriveable::GetVehicleClipPlanes( float &flZNear, float &flZFar ) const
  170. {
  171. // FIXME: Need something a better long-term, this fixes the buggy.
  172. flZNear = 6;
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Renders hud elements
  176. //-----------------------------------------------------------------------------
  177. //-----------------------------------------------------------------------------
  178. // Simply used to return intensity value based upon current timer passed in
  179. //-----------------------------------------------------------------------------
  180. int GetFlashColorIntensity( int LowIntensity, int HighIntensity, bool Dimming, int Increment, int Timer )
  181. {
  182. if ( Dimming )
  183. return ( HighIntensity - Timer * Increment );
  184. else
  185. return ( LowIntensity + Timer * Increment );
  186. }
  187. #define TRIANGULATED_CROSSHAIR 1
  188. void C_PropVehicleDriveable::DrawHudElements( )
  189. {
  190. CHudTexture *pIcon;
  191. int iIconX, iIconY;
  192. if (m_bHasGun)
  193. {
  194. // draw crosshairs for vehicle gun
  195. pIcon = HudIcons().GetIcon( "gunhair" );
  196. if ( pIcon != NULL )
  197. {
  198. float x, y;
  199. Vector screen;
  200. x = ScreenWidth()/2;
  201. y = ScreenHeight()/2;
  202. #if TRIANGULATED_CROSSHAIR
  203. ScreenTransform( m_vecGunCrosshair, screen );
  204. x += 0.5 * screen[0] * ScreenWidth() + 0.5;
  205. y -= 0.5 * screen[1] * ScreenHeight() + 0.5;
  206. #endif
  207. x -= pIcon->Width() / 2;
  208. y -= pIcon->Height() / 2;
  209. Color clr = ( m_bUnableToFire ) ? GetHud().m_clrCaution : GetHud().m_clrNormal;
  210. pIcon->DrawSelf( x, y, clr );
  211. }
  212. if ( m_nScannerDisabledWeapons )
  213. {
  214. // Draw icons for scanners "weps disabled"
  215. pIcon = HudIcons().GetIcon( "dmg_bio" );
  216. if ( pIcon )
  217. {
  218. iIconY = 467 - pIcon->Height() / 2;
  219. iIconX = 385;
  220. if ( !m_bScannerWepIcon )
  221. {
  222. pIcon->DrawSelf( XRES(iIconX), YRES(iIconY), Color( 0, 0, 255, 255 ) );
  223. m_bScannerWepIcon = true;
  224. m_iScannerWepFlashTimer = 0;
  225. m_bScannerWepDim = true;
  226. }
  227. else
  228. {
  229. pIcon->DrawSelf( XRES(iIconX), YRES(iIconY), Color( 0, 0, GetFlashColorIntensity(55, 255, m_bScannerWepDim, 10, m_iScannerWepFlashTimer), 255 ) );
  230. m_iScannerWepFlashTimer++;
  231. m_iScannerWepFlashTimer %= 20;
  232. if(!m_iScannerWepFlashTimer)
  233. m_bScannerWepDim ^= 1;
  234. }
  235. }
  236. }
  237. }
  238. if ( m_nScannerDisabledVehicle )
  239. {
  240. // Draw icons for scanners "vehicle disabled"
  241. pIcon = HudIcons().GetIcon( "dmg_bio" );
  242. if ( pIcon )
  243. {
  244. iIconY = 467 - pIcon->Height() / 2;
  245. iIconX = 410;
  246. if ( !m_bScannerVehicleIcon )
  247. {
  248. pIcon->DrawSelf( XRES(iIconX), YRES(iIconY), Color( 0, 0, 255, 255 ) );
  249. m_bScannerVehicleIcon = true;
  250. m_iScannerVehicleFlashTimer = 0;
  251. m_bScannerVehicleDim = true;
  252. }
  253. else
  254. {
  255. pIcon->DrawSelf( XRES(iIconX), YRES(iIconY), Color( 0, 0, GetFlashColorIntensity(55, 255, m_bScannerVehicleDim, 10, m_iScannerVehicleFlashTimer), 255 ) );
  256. m_iScannerVehicleFlashTimer++;
  257. m_iScannerVehicleFlashTimer %= 20;
  258. if(!m_iScannerVehicleFlashTimer)
  259. m_bScannerVehicleDim ^= 1;
  260. }
  261. }
  262. }
  263. }
  264. //-----------------------------------------------------------------------------
  265. // Purpose:
  266. //-----------------------------------------------------------------------------
  267. void C_PropVehicleDriveable::RestrictView( float *pYawBounds, float *pPitchBounds,
  268. float *pRollBounds, QAngle &vecViewAngles )
  269. {
  270. int eyeAttachmentIndex = LookupAttachment( "vehicle_driver_eyes" );
  271. Vector vehicleEyeOrigin;
  272. QAngle vehicleEyeAngles;
  273. GetAttachmentLocal( eyeAttachmentIndex, vehicleEyeOrigin, vehicleEyeAngles );
  274. // Limit the yaw.
  275. if ( pYawBounds )
  276. {
  277. float flAngleDiff = AngleDiff( vecViewAngles.y, vehicleEyeAngles.y );
  278. flAngleDiff = clamp( flAngleDiff, pYawBounds[0], pYawBounds[1] );
  279. vecViewAngles.y = vehicleEyeAngles.y + flAngleDiff;
  280. }
  281. // Limit the pitch.
  282. if ( pPitchBounds )
  283. {
  284. float flAngleDiff = AngleDiff( vecViewAngles.x, vehicleEyeAngles.x );
  285. flAngleDiff = clamp( flAngleDiff, pPitchBounds[0], pPitchBounds[1] );
  286. vecViewAngles.x = vehicleEyeAngles.x + flAngleDiff;
  287. }
  288. // Limit the roll.
  289. if ( pRollBounds )
  290. {
  291. float flAngleDiff = AngleDiff( vecViewAngles.z, vehicleEyeAngles.z );
  292. flAngleDiff = clamp( flAngleDiff, pRollBounds[0], pRollBounds[1] );
  293. vecViewAngles.z = vehicleEyeAngles.z + flAngleDiff;
  294. }
  295. }
  296. //-----------------------------------------------------------------------------
  297. // Purpose:
  298. //-----------------------------------------------------------------------------
  299. void C_PropVehicleDriveable::UpdateViewAngles( C_BasePlayer *pLocalPlayer, CUserCmd *pCmd )
  300. {
  301. if ( r_VehicleViewClamp.GetInt() )
  302. {
  303. float pitchBounds[2] = { -85.0f, 25.0f };
  304. RestrictView( NULL, pitchBounds, NULL, pCmd->viewangles );
  305. }
  306. }
  307. //-----------------------------------------------------------------------------
  308. // Purpose:
  309. //-----------------------------------------------------------------------------
  310. void C_PropVehicleDriveable::OnEnteredVehicle( C_BaseCombatCharacter *pPassenger )
  311. {
  312. }
  313. // NVNT - added function
  314. void C_PropVehicleDriveable::OnExitedVehicle( C_BaseCombatCharacter *pPassenger )
  315. {
  316. }