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.

1566 lines
48 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "vehicle_base.h"
  9. #include "engine/IEngineSound.h"
  10. #include "in_buttons.h"
  11. #include "ammodef.h"
  12. #include "IEffects.h"
  13. #include "beam_shared.h"
  14. #include "soundenvelope.h"
  15. #include "decals.h"
  16. #include "soundent.h"
  17. #include "te_effect_dispatch.h"
  18. #include "ndebugoverlay.h"
  19. #include "movevars_shared.h"
  20. #include "bone_setup.h"
  21. #include "ai_basenpc.h"
  22. #include "ai_hint.h"
  23. #include "globalstate.h"
  24. // memdbgon must be the last include file in a .cpp file!!!
  25. #include "tier0/memdbgon.h"
  26. #define VEHICLE_HITBOX_DRIVER 1
  27. #define LOCK_SPEED 10
  28. #define JEEP_GUN_YAW "vehicle_weapon_yaw"
  29. #define JEEP_GUN_PITCH "vehicle_weapon_pitch"
  30. #define JEEP_GUN_SPIN "gun_spin"
  31. #define JEEP_GUN_SPIN_RATE 20
  32. #define CANNON_MAX_UP_PITCH 20
  33. #define CANNON_MAX_DOWN_PITCH 20
  34. #define CANNON_MAX_LEFT_YAW 90
  35. #define CANNON_MAX_RIGHT_YAW 90
  36. #define OVERTURNED_EXIT_WAITTIME 2.0f
  37. #define JEEP_AMMOCRATE_HITGROUP 5
  38. #define JEEP_WHEEL_COUNT 4
  39. #define JEEP_AMMO_CRATE_CLOSE_DELAY 2.0f
  40. #define JEEP_DELTA_LENGTH_MAX 12.0f // 1 foot
  41. #define JEEP_FRAMETIME_MIN 1e-6
  42. ConVar hud_jeephint_numentries( "hud_jeephint_numentries", "10", FCVAR_NONE );
  43. ConVar g_jeepexitspeed( "g_jeepexitspeed", "100", FCVAR_CHEAT );
  44. //=============================================================================
  45. //
  46. // Jeep water data.
  47. //
  48. struct JeepWaterData_t
  49. {
  50. bool m_bWheelInWater[JEEP_WHEEL_COUNT];
  51. bool m_bWheelWasInWater[JEEP_WHEEL_COUNT];
  52. Vector m_vecWheelContactPoints[JEEP_WHEEL_COUNT];
  53. float m_flNextRippleTime[JEEP_WHEEL_COUNT];
  54. bool m_bBodyInWater;
  55. bool m_bBodyWasInWater;
  56. DECLARE_SIMPLE_DATADESC();
  57. };
  58. BEGIN_SIMPLE_DATADESC( JeepWaterData_t )
  59. DEFINE_ARRAY( m_bWheelInWater, FIELD_BOOLEAN, JEEP_WHEEL_COUNT ),
  60. DEFINE_ARRAY( m_bWheelWasInWater, FIELD_BOOLEAN, JEEP_WHEEL_COUNT ),
  61. DEFINE_ARRAY( m_vecWheelContactPoints, FIELD_VECTOR, JEEP_WHEEL_COUNT ),
  62. DEFINE_ARRAY( m_flNextRippleTime, FIELD_TIME, JEEP_WHEEL_COUNT ),
  63. DEFINE_FIELD( m_bBodyInWater, FIELD_BOOLEAN ),
  64. DEFINE_FIELD( m_bBodyWasInWater, FIELD_BOOLEAN ),
  65. END_DATADESC()
  66. //-----------------------------------------------------------------------------
  67. // Purpose: Four wheel physics vehicle server vehicle with weaponry
  68. //-----------------------------------------------------------------------------
  69. class CJeepFourWheelServerVehicle : public CFourWheelServerVehicle
  70. {
  71. typedef CFourWheelServerVehicle BaseClass;
  72. // IServerVehicle
  73. public:
  74. bool NPC_HasPrimaryWeapon( void ) { return true; }
  75. void NPC_AimPrimaryWeapon( Vector vecTarget );
  76. int GetExitAnimToUse( Vector &vecEyeExitEndpoint, bool &bAllPointsBlocked );
  77. };
  78. //-----------------------------------------------------------------------------
  79. // Purpose:
  80. //-----------------------------------------------------------------------------
  81. class CPropJeep : public CPropVehicleDriveable
  82. {
  83. DECLARE_CLASS( CPropJeep, CPropVehicleDriveable );
  84. public:
  85. DECLARE_SERVERCLASS();
  86. DECLARE_DATADESC();
  87. CPropJeep( void );
  88. // CPropVehicle
  89. virtual void OnRestore( void );
  90. virtual void ProcessMovement( CBasePlayer *pPlayer, CMoveData *pMoveData );
  91. virtual void DriveVehicle( float flFrameTime, CUserCmd *ucmd, int iButtonsDown, int iButtonsReleased );
  92. virtual void SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  93. virtual void Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
  94. virtual void DampenEyePosition( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles );
  95. virtual bool AllowBlockedExit( CBasePlayer *pPlayer, int nRole ) { return false; }
  96. virtual bool CanExitVehicle( CBaseEntity *pEntity );
  97. virtual bool IsVehicleBodyInWater() { return m_WaterData.m_bBodyInWater; }
  98. // CBaseEntity
  99. void Think(void);
  100. void Precache( void );
  101. void Spawn( void );
  102. virtual void CreateServerVehicle( void );
  103. virtual Vector BodyTarget( const Vector &posSrc, bool bNoisy = true );
  104. virtual void TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator );
  105. virtual int OnTakeDamage( const CTakeDamageInfo &info );
  106. virtual void EnterVehicle( CBasePlayer *pPlayer );
  107. virtual void ExitVehicle( int nRole );
  108. void AimGunAt( Vector *endPos, float flInterval );
  109. bool TauCannonHasBeenCutOff( void ) { return m_bGunHasBeenCutOff; }
  110. // NPC Driving
  111. bool NPC_HasPrimaryWeapon( void ) { return true; }
  112. void NPC_AimPrimaryWeapon( Vector vecTarget );
  113. const char *GetTracerType( void ) { return "AR2Tracer"; }
  114. void DoImpactEffect( trace_t &tr, int nDamageType );
  115. bool HeadlightIsOn( void ) { return m_bHeadlightIsOn; }
  116. void HeadlightTurnOn( void ) { m_bHeadlightIsOn = true; }
  117. void HeadlightTurnOff( void ) { m_bHeadlightIsOn = false; }
  118. private:
  119. void FireCannon( void );
  120. void ChargeCannon( void );
  121. void FireChargedCannon( void );
  122. void DrawBeam( const Vector &startPos, const Vector &endPos, float width );
  123. void StopChargeSound( void );
  124. void GetCannonAim( Vector *resultDir );
  125. void InitWaterData( void );
  126. void HandleWater( void );
  127. bool CheckWater( void );
  128. void CheckWaterLevel( void );
  129. void CreateSplash( const Vector &vecPosition );
  130. void CreateRipple( const Vector &vecPosition );
  131. void UpdateSteeringAngle( void );
  132. void CreateDangerSounds( void );
  133. void ComputePDControllerCoefficients( float *pCoefficientsOut, float flFrequency, float flDampening, float flDeltaTime );
  134. void DampenForwardMotion( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles, float flFrameTime );
  135. void DampenUpMotion( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles, float flFrameTime );
  136. void InputStartRemoveTauCannon( inputdata_t &inputdata );
  137. void InputFinishRemoveTauCannon( inputdata_t &inputdata );
  138. private:
  139. bool m_bGunHasBeenCutOff;
  140. float m_flDangerSoundTime;
  141. int m_nBulletType;
  142. bool m_bCannonCharging;
  143. float m_flCannonTime;
  144. float m_flCannonChargeStartTime;
  145. Vector m_vecGunOrigin;
  146. CSoundPatch *m_sndCannonCharge;
  147. int m_nSpinPos;
  148. float m_aimYaw;
  149. float m_aimPitch;
  150. float m_throttleDisableTime;
  151. float m_flAmmoCrateCloseTime;
  152. // handbrake after the fact to keep vehicles from rolling
  153. float m_flHandbrakeTime;
  154. bool m_bInitialHandbrake;
  155. float m_flOverturnedTime;
  156. Vector m_vecLastEyePos;
  157. Vector m_vecLastEyeTarget;
  158. Vector m_vecEyeSpeed;
  159. Vector m_vecTargetSpeed;
  160. JeepWaterData_t m_WaterData;
  161. int m_iNumberOfEntries;
  162. int m_nAmmoType;
  163. // Seagull perching
  164. float m_flPlayerExitedTime; // Time at which the player last left this vehicle
  165. float m_flLastSawPlayerAt; // Time at which we last saw the player
  166. EHANDLE m_hLastPlayerInVehicle;
  167. bool m_bHasPoop;
  168. CNetworkVar( bool, m_bHeadlightIsOn );
  169. };
  170. BEGIN_DATADESC( CPropJeep )
  171. DEFINE_FIELD( m_bGunHasBeenCutOff, FIELD_BOOLEAN ),
  172. DEFINE_FIELD( m_flDangerSoundTime, FIELD_TIME ),
  173. DEFINE_FIELD( m_nBulletType, FIELD_INTEGER ),
  174. DEFINE_FIELD( m_bCannonCharging, FIELD_BOOLEAN ),
  175. DEFINE_FIELD( m_flCannonTime, FIELD_TIME ),
  176. DEFINE_FIELD( m_flCannonChargeStartTime, FIELD_TIME ),
  177. DEFINE_FIELD( m_vecGunOrigin, FIELD_POSITION_VECTOR ),
  178. DEFINE_SOUNDPATCH( m_sndCannonCharge ),
  179. DEFINE_FIELD( m_nSpinPos, FIELD_INTEGER ),
  180. DEFINE_FIELD( m_aimYaw, FIELD_FLOAT ),
  181. DEFINE_FIELD( m_aimPitch, FIELD_FLOAT ),
  182. DEFINE_FIELD( m_throttleDisableTime, FIELD_TIME ),
  183. DEFINE_FIELD( m_flHandbrakeTime, FIELD_TIME ),
  184. DEFINE_FIELD( m_bInitialHandbrake, FIELD_BOOLEAN ),
  185. DEFINE_FIELD( m_flOverturnedTime, FIELD_TIME ),
  186. DEFINE_FIELD( m_flAmmoCrateCloseTime, FIELD_FLOAT ),
  187. DEFINE_FIELD( m_vecLastEyePos, FIELD_POSITION_VECTOR ),
  188. DEFINE_FIELD( m_vecLastEyeTarget, FIELD_POSITION_VECTOR ),
  189. DEFINE_FIELD( m_vecEyeSpeed, FIELD_POSITION_VECTOR ),
  190. DEFINE_FIELD( m_vecTargetSpeed, FIELD_POSITION_VECTOR ),
  191. DEFINE_FIELD( m_bHeadlightIsOn, FIELD_BOOLEAN ),
  192. DEFINE_EMBEDDED( m_WaterData ),
  193. DEFINE_FIELD( m_iNumberOfEntries, FIELD_INTEGER ),
  194. DEFINE_FIELD( m_nAmmoType, FIELD_INTEGER ),
  195. DEFINE_FIELD( m_flPlayerExitedTime, FIELD_TIME ),
  196. DEFINE_FIELD( m_flLastSawPlayerAt, FIELD_TIME ),
  197. DEFINE_FIELD( m_hLastPlayerInVehicle, FIELD_EHANDLE ),
  198. DEFINE_FIELD( m_bHasPoop, FIELD_BOOLEAN ),
  199. DEFINE_INPUTFUNC( FIELD_VOID, "StartRemoveTauCannon", InputStartRemoveTauCannon ),
  200. DEFINE_INPUTFUNC( FIELD_VOID, "FinishRemoveTauCannon", InputFinishRemoveTauCannon ),
  201. END_DATADESC()
  202. IMPLEMENT_SERVERCLASS_ST( CPropJeep, DT_PropJeep )
  203. SendPropBool( SENDINFO( m_bHeadlightIsOn ) ),
  204. END_SEND_TABLE();
  205. //LINK_ENTITY_TO_CLASS( prop_vehicle_jeep, CPropJeep );
  206. //-----------------------------------------------------------------------------
  207. // Purpose:
  208. //-----------------------------------------------------------------------------
  209. CPropJeep::CPropJeep( void )
  210. {
  211. m_bHasGun = true;
  212. m_bGunHasBeenCutOff = false;
  213. m_bCannonCharging = false;
  214. m_flCannonChargeStartTime = 0;
  215. m_flCannonTime = 0;
  216. m_nBulletType = -1;
  217. m_flOverturnedTime = 0.0f;
  218. m_iNumberOfEntries = 0;
  219. m_vecEyeSpeed.Init();
  220. InitWaterData();
  221. m_bUnableToFire = true;
  222. m_flAmmoCrateCloseTime = 0;
  223. }
  224. //-----------------------------------------------------------------------------
  225. // Purpose:
  226. //-----------------------------------------------------------------------------
  227. void CPropJeep::CreateServerVehicle( void )
  228. {
  229. // Create our armed server vehicle
  230. m_pServerVehicle = new CJeepFourWheelServerVehicle();
  231. m_pServerVehicle->SetVehicle( this );
  232. }
  233. //-----------------------------------------------------------------------------
  234. // Purpose:
  235. //-----------------------------------------------------------------------------
  236. void CPropJeep::Precache( void )
  237. {
  238. UTIL_PrecacheOther( "npc_seagull" );
  239. PrecacheScriptSound( "PropJeep.AmmoClose" );
  240. PrecacheScriptSound( "PropJeep.FireCannon" );
  241. PrecacheScriptSound( "PropJeep.FireChargedCannon" );
  242. PrecacheScriptSound( "PropJeep.AmmoOpen" );
  243. PrecacheScriptSound( "Jeep.GaussCharge" );
  244. // PrecacheModel( GAUSS_BEAM_SPRITE );
  245. BaseClass::Precache();
  246. }
  247. //------------------------------------------------
  248. // Spawn
  249. //------------------------------------------------
  250. void CPropJeep::Spawn( void )
  251. {
  252. // Setup vehicle as a real-wheels car.
  253. SetVehicleType( VEHICLE_TYPE_CAR_WHEELS );
  254. BaseClass::Spawn();
  255. m_flHandbrakeTime = gpGlobals->curtime + 0.1;
  256. m_bInitialHandbrake = false;
  257. m_VehiclePhysics.SetHasBrakePedal( false );
  258. m_flMinimumSpeedToEnterExit = LOCK_SPEED;
  259. m_nBulletType = GetAmmoDef()->Index("GaussEnergy");
  260. if ( m_bHasGun )
  261. {
  262. SetBodygroup( 1, true );
  263. }
  264. else
  265. {
  266. SetBodygroup( 1, false );
  267. }
  268. // Initialize pose parameters
  269. SetPoseParameter( JEEP_GUN_YAW, 0 );
  270. SetPoseParameter( JEEP_GUN_PITCH, 0 );
  271. m_nSpinPos = 0;
  272. SetPoseParameter( JEEP_GUN_SPIN, m_nSpinPos );
  273. m_aimYaw = 0;
  274. m_aimPitch = 0;
  275. AddSolidFlags( FSOLID_NOT_STANDABLE );
  276. CAmmoDef *pAmmoDef = GetAmmoDef();
  277. m_nAmmoType = pAmmoDef->Index("GaussEnergy");
  278. }
  279. //-----------------------------------------------------------------------------
  280. // Purpose:
  281. // Input : &tr -
  282. // nDamageType -
  283. //-----------------------------------------------------------------------------
  284. void CPropJeep::DoImpactEffect( trace_t &tr, int nDamageType )
  285. {
  286. //Draw our beam
  287. DrawBeam( tr.startpos, tr.endpos, 2.4 );
  288. if ( (tr.surface.flags & SURF_SKY) == false )
  289. {
  290. CPVSFilter filter( tr.endpos );
  291. te->GaussExplosion( filter, 0.0f, tr.endpos, tr.plane.normal, 0 );
  292. UTIL_ImpactTrace( &tr, m_nBulletType );
  293. }
  294. }
  295. //-----------------------------------------------------------------------------
  296. // Purpose:
  297. //-----------------------------------------------------------------------------
  298. void CPropJeep::TraceAttack( const CTakeDamageInfo &inputInfo, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator )
  299. {
  300. CTakeDamageInfo info = inputInfo;
  301. if ( ptr->hitbox != VEHICLE_HITBOX_DRIVER )
  302. {
  303. if ( inputInfo.GetDamageType() & DMG_BULLET )
  304. {
  305. info.ScaleDamage( 0.0001 );
  306. }
  307. }
  308. BaseClass::TraceAttack( info, vecDir, ptr, pAccumulator );
  309. }
  310. //-----------------------------------------------------------------------------
  311. // Purpose:
  312. //-----------------------------------------------------------------------------
  313. int CPropJeep::OnTakeDamage( const CTakeDamageInfo &inputInfo )
  314. {
  315. //Do scaled up physics damage to the car
  316. CTakeDamageInfo info = inputInfo;
  317. info.ScaleDamage( 25 );
  318. // HACKHACK: Scale up grenades until we get a better explosion/pressure damage system
  319. if ( inputInfo.GetDamageType() & DMG_BLAST )
  320. {
  321. info.SetDamageForce( inputInfo.GetDamageForce() * 10 );
  322. }
  323. VPhysicsTakeDamage( info );
  324. // reset the damage
  325. info.SetDamage( inputInfo.GetDamage() );
  326. // small amounts of shock damage disrupt the car, but aren't transferred to the player
  327. if ( info.GetDamageType() == DMG_SHOCK )
  328. {
  329. if ( info.GetDamage() <= 10 )
  330. {
  331. // take 10% damage and make the engine stall
  332. info.ScaleDamage( 0.1 );
  333. m_throttleDisableTime = gpGlobals->curtime + 2;
  334. }
  335. }
  336. //Check to do damage to driver
  337. if ( GetDriver() )
  338. {
  339. //Take no damage from physics damages
  340. if ( info.GetDamageType() & DMG_CRUSH )
  341. return 0;
  342. // Take the damage (strip out the DMG_BLAST)
  343. info.SetDamageType( info.GetDamageType() & (~DMG_BLAST) );
  344. GetDriver()->TakeDamage( info );
  345. }
  346. return 0;
  347. }
  348. //-----------------------------------------------------------------------------
  349. // Purpose:
  350. //-----------------------------------------------------------------------------
  351. Vector CPropJeep::BodyTarget( const Vector &posSrc, bool bNoisy )
  352. {
  353. Vector shotPos;
  354. matrix3x4_t matrix;
  355. int eyeAttachmentIndex = LookupAttachment("vehicle_driver_eyes");
  356. GetAttachment( eyeAttachmentIndex, matrix );
  357. MatrixGetColumn( matrix, 3, shotPos );
  358. if ( bNoisy )
  359. {
  360. shotPos[0] += random->RandomFloat( -8.0f, 8.0f );
  361. shotPos[1] += random->RandomFloat( -8.0f, 8.0f );
  362. shotPos[2] += random->RandomFloat( -8.0f, 8.0f );
  363. }
  364. return shotPos;
  365. }
  366. //-----------------------------------------------------------------------------
  367. // Purpose: Aim Gun at a target
  368. //-----------------------------------------------------------------------------
  369. void CPropJeep::AimGunAt( Vector *endPos, float flInterval )
  370. {
  371. Vector aimPos = *endPos;
  372. // See if the gun should be allowed to aim
  373. if ( IsOverturned() || m_bEngineLocked )
  374. {
  375. SetPoseParameter( JEEP_GUN_YAW, 0 );
  376. SetPoseParameter( JEEP_GUN_PITCH, 0 );
  377. SetPoseParameter( JEEP_GUN_SPIN, 0 );
  378. return;
  379. // Make the gun go limp and look "down"
  380. Vector v_forward, v_up;
  381. AngleVectors( GetLocalAngles(), NULL, &v_forward, &v_up );
  382. aimPos = WorldSpaceCenter() + ( v_forward * -32.0f ) - Vector( 0, 0, 128.0f );
  383. }
  384. matrix3x4_t gunMatrix;
  385. GetAttachment( LookupAttachment("gun_ref"), gunMatrix );
  386. // transform the enemy into gun space
  387. Vector localEnemyPosition;
  388. VectorITransform( aimPos, gunMatrix, localEnemyPosition );
  389. // do a look at in gun space (essentially a delta-lookat)
  390. QAngle localEnemyAngles;
  391. VectorAngles( localEnemyPosition, localEnemyAngles );
  392. // convert to +/- 180 degrees
  393. localEnemyAngles.x = UTIL_AngleDiff( localEnemyAngles.x, 0 );
  394. localEnemyAngles.y = UTIL_AngleDiff( localEnemyAngles.y, 0 );
  395. float targetYaw = m_aimYaw + localEnemyAngles.y;
  396. float targetPitch = m_aimPitch + localEnemyAngles.x;
  397. // Constrain our angles
  398. float newTargetYaw = clamp( targetYaw, -CANNON_MAX_LEFT_YAW, CANNON_MAX_RIGHT_YAW );
  399. float newTargetPitch = clamp( targetPitch, -CANNON_MAX_DOWN_PITCH, CANNON_MAX_UP_PITCH );
  400. // If the angles have been clamped, we're looking outside of our valid range
  401. if ( fabs(newTargetYaw-targetYaw) > 1e-4 || fabs(newTargetPitch-targetPitch) > 1e-4 )
  402. {
  403. m_bUnableToFire = true;
  404. }
  405. targetYaw = newTargetYaw;
  406. targetPitch = newTargetPitch;
  407. // Exponentially approach the target
  408. float yawSpeed = 8;
  409. float pitchSpeed = 8;
  410. m_aimYaw = UTIL_Approach( targetYaw, m_aimYaw, yawSpeed );
  411. m_aimPitch = UTIL_Approach( targetPitch, m_aimPitch, pitchSpeed );
  412. SetPoseParameter( JEEP_GUN_YAW, -m_aimYaw);
  413. SetPoseParameter( JEEP_GUN_PITCH, -m_aimPitch );
  414. InvalidateBoneCache();
  415. // read back to avoid drift when hitting limits
  416. // as long as the velocity is less than the delta between the limit and 180, this is fine.
  417. m_aimPitch = -GetPoseParameter( JEEP_GUN_PITCH );
  418. m_aimYaw = -GetPoseParameter( JEEP_GUN_YAW );
  419. // Now draw crosshair for actual aiming point
  420. Vector vecMuzzle, vecMuzzleDir;
  421. QAngle vecMuzzleAng;
  422. GetAttachment( "Muzzle", vecMuzzle, vecMuzzleAng );
  423. AngleVectors( vecMuzzleAng, &vecMuzzleDir );
  424. trace_t tr;
  425. UTIL_TraceLine( vecMuzzle, vecMuzzle + (vecMuzzleDir * MAX_TRACE_LENGTH), MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
  426. // see if we hit something, if so, adjust endPos to hit location
  427. if ( tr.fraction < 1.0 )
  428. {
  429. m_vecGunCrosshair = vecMuzzle + ( vecMuzzleDir * MAX_TRACE_LENGTH * tr.fraction );
  430. }
  431. }
  432. //-----------------------------------------------------------------------------
  433. // Purpose:
  434. //-----------------------------------------------------------------------------
  435. void CPropJeep::InitWaterData( void )
  436. {
  437. m_WaterData.m_bBodyInWater = false;
  438. m_WaterData.m_bBodyWasInWater = false;
  439. for ( int iWheel = 0; iWheel < JEEP_WHEEL_COUNT; ++iWheel )
  440. {
  441. m_WaterData.m_bWheelInWater[iWheel] = false;
  442. m_WaterData.m_bWheelWasInWater[iWheel] = false;
  443. m_WaterData.m_vecWheelContactPoints[iWheel].Init();
  444. m_WaterData.m_flNextRippleTime[iWheel] = 0;
  445. }
  446. }
  447. //-----------------------------------------------------------------------------
  448. // Purpose:
  449. //-----------------------------------------------------------------------------
  450. void CPropJeep::HandleWater( void )
  451. {
  452. // Only check the wheels and engine in water if we have a driver (player).
  453. if ( !GetDriver() )
  454. return;
  455. // Check to see if we are in water.
  456. if ( CheckWater() )
  457. {
  458. for ( int iWheel = 0; iWheel < JEEP_WHEEL_COUNT; ++iWheel )
  459. {
  460. // Create an entry/exit splash!
  461. if ( m_WaterData.m_bWheelInWater[iWheel] != m_WaterData.m_bWheelWasInWater[iWheel] )
  462. {
  463. CreateSplash( m_WaterData.m_vecWheelContactPoints[iWheel] );
  464. CreateRipple( m_WaterData.m_vecWheelContactPoints[iWheel] );
  465. }
  466. // Create ripples.
  467. if ( m_WaterData.m_bWheelInWater[iWheel] && m_WaterData.m_bWheelWasInWater[iWheel] )
  468. {
  469. if ( m_WaterData.m_flNextRippleTime[iWheel] < gpGlobals->curtime )
  470. {
  471. // Stagger ripple times
  472. m_WaterData.m_flNextRippleTime[iWheel] = gpGlobals->curtime + RandomFloat( 0.1, 0.3 );
  473. CreateRipple( m_WaterData.m_vecWheelContactPoints[iWheel] );
  474. }
  475. }
  476. }
  477. }
  478. // Save of data from last think.
  479. for ( int iWheel = 0; iWheel < JEEP_WHEEL_COUNT; ++iWheel )
  480. {
  481. m_WaterData.m_bWheelWasInWater[iWheel] = m_WaterData.m_bWheelInWater[iWheel];
  482. }
  483. }
  484. //-----------------------------------------------------------------------------
  485. // Purpose:
  486. //-----------------------------------------------------------------------------
  487. bool CPropJeep::CheckWater( void )
  488. {
  489. bool bInWater = false;
  490. // Check all four wheels.
  491. for ( int iWheel = 0; iWheel < JEEP_WHEEL_COUNT; ++iWheel )
  492. {
  493. // Get the current wheel and get its contact point.
  494. IPhysicsObject *pWheel = m_VehiclePhysics.GetWheel( iWheel );
  495. if ( !pWheel )
  496. continue;
  497. // Check to see if we hit water.
  498. if ( pWheel->GetContactPoint( &m_WaterData.m_vecWheelContactPoints[iWheel], NULL ) )
  499. {
  500. m_WaterData.m_bWheelInWater[iWheel] = ( UTIL_PointContents( m_WaterData.m_vecWheelContactPoints[iWheel] ) & MASK_WATER ) ? true : false;
  501. if ( m_WaterData.m_bWheelInWater[iWheel] )
  502. {
  503. bInWater = true;
  504. }
  505. }
  506. }
  507. // Check the body and the BONNET.
  508. int iEngine = LookupAttachment( "vehicle_engine" );
  509. Vector vecEnginePoint;
  510. QAngle vecEngineAngles;
  511. GetAttachment( iEngine, vecEnginePoint, vecEngineAngles );
  512. m_WaterData.m_bBodyInWater = ( UTIL_PointContents( vecEnginePoint ) & MASK_WATER ) ? true : false;
  513. if ( m_WaterData.m_bBodyInWater )
  514. {
  515. if ( m_bHasPoop )
  516. {
  517. RemoveAllDecals();
  518. m_bHasPoop = false;
  519. }
  520. if ( !m_VehiclePhysics.IsEngineDisabled() )
  521. {
  522. m_VehiclePhysics.SetDisableEngine( true );
  523. }
  524. }
  525. else
  526. {
  527. if ( m_VehiclePhysics.IsEngineDisabled() )
  528. {
  529. m_VehiclePhysics.SetDisableEngine( false );
  530. }
  531. }
  532. if ( bInWater )
  533. {
  534. // Check the player's water level.
  535. CheckWaterLevel();
  536. }
  537. return bInWater;
  538. }
  539. //-----------------------------------------------------------------------------
  540. // Purpose:
  541. //-----------------------------------------------------------------------------
  542. void CPropJeep::CheckWaterLevel( void )
  543. {
  544. CBaseEntity *pEntity = GetDriver();
  545. if ( pEntity && pEntity->IsPlayer() )
  546. {
  547. CBasePlayer *pPlayer = static_cast<CBasePlayer*>( pEntity );
  548. Vector vecAttachPoint;
  549. QAngle vecAttachAngles;
  550. // Check eyes. (vehicle_driver_eyes point)
  551. int iAttachment = LookupAttachment( "vehicle_driver_eyes" );
  552. GetAttachment( iAttachment, vecAttachPoint, vecAttachAngles );
  553. // Add the jeep's Z view offset
  554. Vector vecUp;
  555. AngleVectors( vecAttachAngles, NULL, NULL, &vecUp );
  556. vecUp.z = clamp( vecUp.z, 0.0f, vecUp.z );
  557. vecAttachPoint.z += r_JeepViewZHeight.GetFloat() * vecUp.z;
  558. bool bEyes = ( UTIL_PointContents( vecAttachPoint ) & MASK_WATER ) ? true : false;
  559. if ( bEyes )
  560. {
  561. pPlayer->SetWaterLevel( WL_Eyes );
  562. return;
  563. }
  564. // Check waist. (vehicle_engine point -- see parent function).
  565. if ( m_WaterData.m_bBodyInWater )
  566. {
  567. pPlayer->SetWaterLevel( WL_Waist );
  568. return;
  569. }
  570. // Check feet. (vehicle_feet_passenger0 point)
  571. iAttachment = LookupAttachment( "vehicle_feet_passenger0" );
  572. GetAttachment( iAttachment, vecAttachPoint, vecAttachAngles );
  573. bool bFeet = ( UTIL_PointContents( vecAttachPoint ) & MASK_WATER ) ? true : false;
  574. if ( bFeet )
  575. {
  576. pPlayer->SetWaterLevel( WL_Feet );
  577. return;
  578. }
  579. // Not in water.
  580. pPlayer->SetWaterLevel( WL_NotInWater );
  581. }
  582. }
  583. //-----------------------------------------------------------------------------
  584. // Purpose:
  585. //-----------------------------------------------------------------------------
  586. void CPropJeep::CreateSplash( const Vector &vecPosition )
  587. {
  588. // Splash data.
  589. CEffectData data;
  590. data.m_fFlags = 0;
  591. data.m_vOrigin = vecPosition;
  592. data.m_vNormal.Init( 0.0f, 0.0f, 1.0f );
  593. VectorAngles( data.m_vNormal, data.m_vAngles );
  594. data.m_flScale = 10.0f + random->RandomFloat( 0, 2 );
  595. // Create the splash..
  596. DispatchEffect( "watersplash", data );
  597. }
  598. //-----------------------------------------------------------------------------
  599. // Purpose:
  600. //-----------------------------------------------------------------------------
  601. void CPropJeep::CreateRipple( const Vector &vecPosition )
  602. {
  603. // Ripple data.
  604. CEffectData data;
  605. data.m_fFlags = 0;
  606. data.m_vOrigin = vecPosition;
  607. data.m_vNormal.Init( 0.0f, 0.0f, 1.0f );
  608. VectorAngles( data.m_vNormal, data.m_vAngles );
  609. data.m_flScale = 10.0f + random->RandomFloat( 0, 2 );
  610. if ( GetWaterType() & CONTENTS_SLIME )
  611. {
  612. data.m_fFlags |= FX_WATER_IN_SLIME;
  613. }
  614. // Create the ripple.
  615. DispatchEffect( "waterripple", data );
  616. }
  617. //-----------------------------------------------------------------------------
  618. // Purpose:
  619. //-----------------------------------------------------------------------------
  620. void CPropJeep::Think(void)
  621. {
  622. BaseClass::Think();
  623. /*
  624. CBasePlayer *pPlayer = UTIL_GetLocalPlayer();
  625. if ( m_bEngineLocked )
  626. {
  627. m_bUnableToFire = true;
  628. if ( pPlayer != NULL )
  629. {
  630. pPlayer->m_Local.m_iHideHUD |= HIDEHUD_VEHICLE_CROSSHAIR;
  631. }
  632. }
  633. else
  634. {
  635. // Start this as false and update it again each frame
  636. m_bUnableToFire = false;
  637. if ( pPlayer != NULL )
  638. {
  639. pPlayer->m_Local.m_iHideHUD &= ~HIDEHUD_VEHICLE_CROSSHAIR;
  640. }
  641. }
  642. */
  643. // Water!?
  644. HandleWater();
  645. SetSimulationTime( gpGlobals->curtime );
  646. SetNextThink( gpGlobals->curtime );
  647. SetAnimatedEveryTick( true );
  648. if ( !m_bInitialHandbrake ) // after initial timer expires, set the handbrake
  649. {
  650. m_bInitialHandbrake = true;
  651. m_VehiclePhysics.SetHandbrake( true );
  652. m_VehiclePhysics.Think();
  653. }
  654. // Check overturned status.
  655. if ( !IsOverturned() )
  656. {
  657. m_flOverturnedTime = 0.0f;
  658. }
  659. else
  660. {
  661. m_flOverturnedTime += gpGlobals->frametime;
  662. }
  663. // spin gun if charging cannon
  664. //FIXME: Don't bother for E3
  665. if ( m_bCannonCharging )
  666. {
  667. m_nSpinPos += JEEP_GUN_SPIN_RATE;
  668. SetPoseParameter( JEEP_GUN_SPIN, m_nSpinPos );
  669. }
  670. // Aim gun based on the player view direction.
  671. if ( m_hPlayer && !m_bExitAnimOn && !m_bEnterAnimOn )
  672. {
  673. Vector vecEyeDir, vecEyePos;
  674. m_hPlayer->EyePositionAndVectors( &vecEyePos, &vecEyeDir, NULL, NULL );
  675. // Trace out from the player's eye point.
  676. Vector vecEndPos = vecEyePos + ( vecEyeDir * MAX_TRACE_LENGTH );
  677. trace_t trace;
  678. UTIL_TraceLine( vecEyePos, vecEndPos, MASK_SHOT, this, COLLISION_GROUP_NONE, &trace );
  679. // See if we hit something, if so, adjust end position to hit location.
  680. if ( trace.fraction < 1.0 )
  681. {
  682. vecEndPos = vecEyePos + ( vecEyeDir * MAX_TRACE_LENGTH * trace.fraction );
  683. }
  684. //m_vecLookCrosshair = vecEndPos;
  685. AimGunAt( &vecEndPos, 0.1f );
  686. }
  687. StudioFrameAdvance();
  688. // If the enter or exit animation has finished, tell the server vehicle
  689. if ( IsSequenceFinished() && (m_bExitAnimOn || m_bEnterAnimOn) )
  690. {
  691. if ( m_bEnterAnimOn )
  692. {
  693. m_VehiclePhysics.ReleaseHandbrake();
  694. StartEngine();
  695. // HACKHACK: This forces the jeep to play a sound when it gets entered underwater
  696. if ( m_VehiclePhysics.IsEngineDisabled() )
  697. {
  698. CBaseServerVehicle *pServerVehicle = dynamic_cast<CBaseServerVehicle *>(GetServerVehicle());
  699. if ( pServerVehicle )
  700. {
  701. pServerVehicle->SoundStartDisabled();
  702. }
  703. }
  704. // The first few time we get into the jeep, print the jeep help
  705. if ( m_iNumberOfEntries < hud_jeephint_numentries.GetInt() )
  706. {
  707. UTIL_HudHintText( m_hPlayer, "#Valve_Hint_JeepKeys" );
  708. m_iNumberOfEntries++;
  709. }
  710. }
  711. // If we're exiting and have had the tau cannon removed, we don't want to reset the animation
  712. GetServerVehicle()->HandleEntryExitFinish( m_bExitAnimOn, !(m_bExitAnimOn && TauCannonHasBeenCutOff()) );
  713. }
  714. // See if the ammo crate needs to close
  715. if ( ( m_flAmmoCrateCloseTime < gpGlobals->curtime ) && ( GetSequence() == LookupSequence( "ammo_open" ) ) )
  716. {
  717. m_flAnimTime = gpGlobals->curtime;
  718. m_flPlaybackRate = 0.0;
  719. SetCycle( 0 );
  720. ResetSequence( LookupSequence( "ammo_close" ) );
  721. }
  722. else if ( ( GetSequence() == LookupSequence( "ammo_close" ) ) && IsSequenceFinished() )
  723. {
  724. m_flAnimTime = gpGlobals->curtime;
  725. m_flPlaybackRate = 0.0;
  726. SetCycle( 0 );
  727. ResetSequence( LookupSequence( "idle" ) );
  728. CPASAttenuationFilter sndFilter( this, "PropJeep.AmmoClose" );
  729. EmitSound( sndFilter, entindex(), "PropJeep.AmmoClose" );
  730. }
  731. }
  732. //-----------------------------------------------------------------------------
  733. // Purpose:
  734. // Input : &startPos -
  735. // &endPos -
  736. // width -
  737. // useMuzzle -
  738. //-----------------------------------------------------------------------------
  739. void CPropJeep::DrawBeam( const Vector &startPos, const Vector &endPos, float width )
  740. {
  741. /*
  742. //Tracer down the middle
  743. UTIL_Tracer( startPos, endPos, 0, TRACER_DONT_USE_ATTACHMENT, 6500, false, "GaussTracer" );
  744. //Draw the main beam shaft
  745. CBeam *pBeam = CBeam::BeamCreate( GAUSS_BEAM_SPRITE, 0.5 );
  746. pBeam->SetStartPos( startPos );
  747. pBeam->PointEntInit( endPos, this );
  748. pBeam->SetEndAttachment( LookupAttachment("Muzzle") );
  749. pBeam->SetWidth( width );
  750. pBeam->SetEndWidth( 0.05f );
  751. pBeam->SetBrightness( 255 );
  752. pBeam->SetColor( 255, 185+random->RandomInt( -16, 16 ), 40 );
  753. pBeam->RelinkBeam();
  754. pBeam->LiveForTime( 0.1f );
  755. //Draw electric bolts along shaft
  756. pBeam = CBeam::BeamCreate( GAUSS_BEAM_SPRITE, 3.0f );
  757. pBeam->SetStartPos( startPos );
  758. pBeam->PointEntInit( endPos, this );
  759. pBeam->SetEndAttachment( LookupAttachment("Muzzle") );
  760. pBeam->SetBrightness( random->RandomInt( 64, 255 ) );
  761. pBeam->SetColor( 255, 255, 150+random->RandomInt( 0, 64 ) );
  762. pBeam->RelinkBeam();
  763. pBeam->LiveForTime( 0.1f );
  764. pBeam->SetNoise( 1.6f );
  765. pBeam->SetEndWidth( 0.1f ); */
  766. }
  767. //-----------------------------------------------------------------------------
  768. // Purpose:
  769. //-----------------------------------------------------------------------------
  770. void CPropJeep::FireCannon( void )
  771. {
  772. //Don't fire again if it's been too soon
  773. if ( m_flCannonTime > gpGlobals->curtime )
  774. return;
  775. if ( m_bUnableToFire )
  776. return;
  777. m_flCannonTime = gpGlobals->curtime + 0.2f;
  778. m_bCannonCharging = false;
  779. //Find the direction the gun is pointing in
  780. Vector aimDir;
  781. GetCannonAim( &aimDir );
  782. FireBulletsInfo_t info( 1, m_vecGunOrigin, aimDir, VECTOR_CONE_1DEGREES, MAX_TRACE_LENGTH, m_nAmmoType );
  783. info.m_nFlags = FIRE_BULLETS_ALLOW_WATER_SURFACE_IMPACTS;
  784. info.m_pAttacker = m_hPlayer;
  785. FireBullets( info );
  786. // Register a muzzleflash for the AI
  787. if ( m_hPlayer )
  788. {
  789. m_hPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5 );
  790. }
  791. CPASAttenuationFilter sndFilter( this, "PropJeep.FireCannon" );
  792. EmitSound( sndFilter, entindex(), "PropJeep.FireCannon" );
  793. // make cylinders of gun spin a bit
  794. m_nSpinPos += JEEP_GUN_SPIN_RATE;
  795. //SetPoseParameter( JEEP_GUN_SPIN, m_nSpinPos ); //FIXME: Don't bother with this for E3, won't look right
  796. }
  797. //-----------------------------------------------------------------------------
  798. // Purpose:
  799. //-----------------------------------------------------------------------------
  800. void CPropJeep::FireChargedCannon( void )
  801. {
  802. /* bool penetrated = false;
  803. m_bCannonCharging = false;
  804. m_flCannonTime = gpGlobals->curtime + 0.5f;
  805. StopChargeSound();
  806. CPASAttenuationFilter sndFilter( this, "PropJeep.FireChargedCannon" );
  807. EmitSound( sndFilter, entindex(), "PropJeep.FireChargedCannon" );
  808. //Find the direction the gun is pointing in
  809. Vector aimDir;
  810. GetCannonAim( &aimDir );
  811. Vector endPos = m_vecGunOrigin + ( aimDir * MAX_TRACE_LENGTH );
  812. //Shoot a shot straight out
  813. trace_t tr;
  814. UTIL_TraceLine( m_vecGunOrigin, endPos, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
  815. ClearMultiDamage();
  816. //Find how much damage to do
  817. float flChargeAmount = ( gpGlobals->curtime - m_flCannonChargeStartTime ) / MAX_GAUSS_CHARGE_TIME;
  818. //Clamp this
  819. if ( flChargeAmount > 1.0f )
  820. {
  821. flChargeAmount = 1.0f;
  822. }
  823. //Determine the damage amount
  824. //FIXME: Use ConVars!
  825. float flDamage = 15 + ( ( 250 - 15 ) * flChargeAmount );
  826. CBaseEntity *pHit = tr.m_pEnt;
  827. //Look for wall penetration
  828. if ( tr.DidHitWorld() && !(tr.surface.flags & SURF_SKY) )
  829. {
  830. //Try wall penetration
  831. UTIL_ImpactTrace( &tr, m_nBulletType, "ImpactJeep" );
  832. UTIL_DecalTrace( &tr, "RedGlowFade" );
  833. CPVSFilter filter( tr.endpos );
  834. te->GaussExplosion( filter, 0.0f, tr.endpos, tr.plane.normal, 0 );
  835. Vector testPos = tr.endpos + ( aimDir * 48.0f );
  836. UTIL_TraceLine( testPos, tr.endpos, MASK_SHOT, GetDriver(), COLLISION_GROUP_NONE, &tr );
  837. if ( tr.allsolid == false )
  838. {
  839. UTIL_DecalTrace( &tr, "RedGlowFade" );
  840. penetrated = true;
  841. }
  842. }
  843. else if ( pHit != NULL )
  844. {
  845. CTakeDamageInfo dmgInfo( this, GetDriver(), flDamage, DMG_SHOCK );
  846. CalculateBulletDamageForce( &dmgInfo, GetAmmoDef()->Index("GaussEnergy"), aimDir, tr.endpos, 1.0f + flChargeAmount * 4.0f );
  847. //Do direct damage to anything in our path
  848. pHit->DispatchTraceAttack( dmgInfo, aimDir, &tr );
  849. }
  850. ApplyMultiDamage();
  851. //Kick up an effect
  852. if ( !(tr.surface.flags & SURF_SKY) )
  853. {
  854. UTIL_ImpactTrace( &tr, m_nBulletType, "ImpactJeep" );
  855. //Do a gauss explosion
  856. CPVSFilter filter( tr.endpos );
  857. te->GaussExplosion( filter, 0.0f, tr.endpos, tr.plane.normal, 0 );
  858. }
  859. //Show the effect
  860. DrawBeam( m_vecGunOrigin, tr.endpos, 9.6 );
  861. // Register a muzzleflash for the AI
  862. if ( m_hPlayer )
  863. {
  864. m_hPlayer->SetMuzzleFlashTime( gpGlobals->curtime + 0.5f );
  865. }
  866. //Rock the car
  867. IPhysicsObject *pObj = VPhysicsGetObject();
  868. if ( pObj != NULL )
  869. {
  870. Vector shoveDir = aimDir * -( flDamage * 500.0f );
  871. pObj->ApplyForceOffset( shoveDir, m_vecGunOrigin );
  872. }
  873. //Do radius damage if we didn't penetrate the wall
  874. if ( penetrated == true )
  875. {
  876. RadiusDamage( CTakeDamageInfo( this, this, flDamage, DMG_SHOCK ), tr.endpos, 200.0f, CLASS_NONE, NULL );
  877. } */
  878. }
  879. //-----------------------------------------------------------------------------
  880. // Purpose:
  881. //-----------------------------------------------------------------------------
  882. void CPropJeep::ChargeCannon( void )
  883. {
  884. //Don't fire again if it's been too soon
  885. if ( m_flCannonTime > gpGlobals->curtime )
  886. return;
  887. //See if we're starting a charge
  888. if ( m_bCannonCharging == false )
  889. {
  890. m_flCannonChargeStartTime = gpGlobals->curtime;
  891. m_bCannonCharging = true;
  892. //Start charging sound
  893. CPASAttenuationFilter filter( this );
  894. m_sndCannonCharge = (CSoundEnvelopeController::GetController()).SoundCreate( filter, entindex(), CHAN_STATIC, "Jeep.GaussCharge", ATTN_NORM );
  895. assert(m_sndCannonCharge!=NULL);
  896. if ( m_sndCannonCharge != NULL )
  897. {
  898. (CSoundEnvelopeController::GetController()).Play( m_sndCannonCharge, 1.0f, 50 );
  899. (CSoundEnvelopeController::GetController()).SoundChangePitch( m_sndCannonCharge, 250, 3.0f );
  900. }
  901. return;
  902. }
  903. //TODO: Add muzzle effect?
  904. //TODO: Check for overcharge and have the weapon simply fire or instead "decharge"?
  905. }
  906. //-----------------------------------------------------------------------------
  907. // Purpose:
  908. //-----------------------------------------------------------------------------
  909. void CPropJeep::StopChargeSound( void )
  910. {
  911. if ( m_sndCannonCharge != NULL )
  912. {
  913. (CSoundEnvelopeController::GetController()).SoundFadeOut( m_sndCannonCharge, 0.1f );
  914. }
  915. }
  916. //-----------------------------------------------------------------------------
  917. // Purpose: Finds the true aiming position of the gun (looks at what player
  918. // is looking at and adjusts)
  919. // Input : &resultDir - direction to be calculated
  920. //-----------------------------------------------------------------------------
  921. void CPropJeep::GetCannonAim( Vector *resultDir )
  922. {
  923. Vector muzzleOrigin;
  924. QAngle muzzleAngles;
  925. GetAttachment( LookupAttachment("gun_ref"), muzzleOrigin, muzzleAngles );
  926. AngleVectors( muzzleAngles, resultDir );
  927. }
  928. //-----------------------------------------------------------------------------
  929. // Purpose: If the player uses the jeep while at the back, he gets ammo from the crate instead
  930. //-----------------------------------------------------------------------------
  931. void CPropJeep::Use( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
  932. {
  933. CBasePlayer *pPlayer = ToBasePlayer( pActivator );
  934. if ( pPlayer == NULL)
  935. return;
  936. // Find out if the player's looking at our ammocrate hitbox
  937. Vector vecForward;
  938. pPlayer->EyeVectors( &vecForward, NULL, NULL );
  939. trace_t tr;
  940. Vector vecStart = pPlayer->EyePosition();
  941. UTIL_TraceLine( vecStart, vecStart + vecForward * 1024, MASK_SOLID | CONTENTS_DEBRIS | CONTENTS_HITBOX, pPlayer, COLLISION_GROUP_NONE, &tr );
  942. if ( tr.m_pEnt == this && tr.hitgroup == JEEP_AMMOCRATE_HITGROUP )
  943. {
  944. // Player's using the crate.
  945. // Fill up his SMG ammo.
  946. pPlayer->GiveAmmo( 300, "SMG1");
  947. if ( ( GetSequence() != LookupSequence( "ammo_open" ) ) && ( GetSequence() != LookupSequence( "ammo_close" ) ) )
  948. {
  949. // Open the crate
  950. m_flAnimTime = gpGlobals->curtime;
  951. m_flPlaybackRate = 0.0;
  952. SetCycle( 0 );
  953. ResetSequence( LookupSequence( "ammo_open" ) );
  954. CPASAttenuationFilter sndFilter( this, "PropJeep.AmmoOpen" );
  955. EmitSound( sndFilter, entindex(), "PropJeep.AmmoOpen" );
  956. }
  957. m_flAmmoCrateCloseTime = gpGlobals->curtime + JEEP_AMMO_CRATE_CLOSE_DELAY;
  958. return;
  959. }
  960. // Fall back and get in the vehicle instead
  961. BaseClass::Use( pActivator, pCaller, useType, value );
  962. }
  963. //-----------------------------------------------------------------------------
  964. // Purpose:
  965. //-----------------------------------------------------------------------------
  966. bool CPropJeep::CanExitVehicle( CBaseEntity *pEntity )
  967. {
  968. return ( !m_bEnterAnimOn && !m_bExitAnimOn && !m_bLocked && (m_nSpeed <= g_jeepexitspeed.GetFloat() ) );
  969. }
  970. //-----------------------------------------------------------------------------
  971. // Purpose:
  972. //-----------------------------------------------------------------------------
  973. void CPropJeep::DampenEyePosition( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles )
  974. {
  975. // Get the frametime. (Check to see if enough time has passed to warrent dampening).
  976. float flFrameTime = gpGlobals->frametime;
  977. if ( flFrameTime < JEEP_FRAMETIME_MIN )
  978. {
  979. vecVehicleEyePos = m_vecLastEyePos;
  980. DampenUpMotion( vecVehicleEyePos, vecVehicleEyeAngles, 0.0f );
  981. return;
  982. }
  983. // Keep static the sideways motion.
  984. // Dampen forward/backward motion.
  985. DampenForwardMotion( vecVehicleEyePos, vecVehicleEyeAngles, flFrameTime );
  986. // Blend up/down motion.
  987. DampenUpMotion( vecVehicleEyePos, vecVehicleEyeAngles, flFrameTime );
  988. }
  989. //-----------------------------------------------------------------------------
  990. // Use the controller as follows:
  991. // speed += ( pCoefficientsOut[0] * ( targetPos - currentPos ) + pCoefficientsOut[1] * ( targetSpeed - currentSpeed ) ) * flDeltaTime;
  992. //-----------------------------------------------------------------------------
  993. void CPropJeep::ComputePDControllerCoefficients( float *pCoefficientsOut,
  994. float flFrequency, float flDampening,
  995. float flDeltaTime )
  996. {
  997. float flKs = 9.0f * flFrequency * flFrequency;
  998. float flKd = 4.5f * flFrequency * flDampening;
  999. float flScale = 1.0f / ( 1.0f + flKd * flDeltaTime + flKs * flDeltaTime * flDeltaTime );
  1000. pCoefficientsOut[0] = flKs * flScale;
  1001. pCoefficientsOut[1] = ( flKd + flKs * flDeltaTime ) * flScale;
  1002. }
  1003. //-----------------------------------------------------------------------------
  1004. // Purpose:
  1005. //-----------------------------------------------------------------------------
  1006. void CPropJeep::DampenForwardMotion( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles, float flFrameTime )
  1007. {
  1008. // Get forward vector.
  1009. Vector vecForward;
  1010. AngleVectors( vecVehicleEyeAngles, &vecForward);
  1011. // Simulate the eye position forward based on the data from last frame
  1012. // (assumes no acceleration - it will get that from the "spring").
  1013. Vector vecCurrentEyePos = m_vecLastEyePos + m_vecEyeSpeed * flFrameTime;
  1014. // Calculate target speed based on the current vehicle eye position and the last vehicle eye position and frametime.
  1015. Vector vecVehicleEyeSpeed = ( vecVehicleEyePos - m_vecLastEyeTarget ) / flFrameTime;
  1016. m_vecLastEyeTarget = vecVehicleEyePos;
  1017. // Calculate the speed and position deltas.
  1018. Vector vecDeltaSpeed = vecVehicleEyeSpeed - m_vecEyeSpeed;
  1019. Vector vecDeltaPos = vecVehicleEyePos - vecCurrentEyePos;
  1020. // Clamp.
  1021. if ( vecDeltaPos.Length() > JEEP_DELTA_LENGTH_MAX )
  1022. {
  1023. float flSign = vecForward.Dot( vecVehicleEyeSpeed ) >= 0.0f ? -1.0f : 1.0f;
  1024. vecVehicleEyePos += flSign * ( vecForward * JEEP_DELTA_LENGTH_MAX );
  1025. m_vecLastEyePos = vecVehicleEyePos;
  1026. m_vecEyeSpeed = vecVehicleEyeSpeed;
  1027. return;
  1028. }
  1029. // Generate an updated (dampening) speed for use in next frames position extrapolation.
  1030. float flCoefficients[2];
  1031. ComputePDControllerCoefficients( flCoefficients, r_JeepViewDampenFreq.GetFloat(), r_JeepViewDampenDamp.GetFloat(), flFrameTime );
  1032. m_vecEyeSpeed += ( ( flCoefficients[0] * vecDeltaPos + flCoefficients[1] * vecDeltaSpeed ) * flFrameTime );
  1033. // Save off data for next frame.
  1034. m_vecLastEyePos = vecCurrentEyePos;
  1035. // Move eye forward/backward.
  1036. Vector vecForwardOffset = vecForward * ( vecForward.Dot( vecDeltaPos ) );
  1037. vecVehicleEyePos -= vecForwardOffset;
  1038. }
  1039. //-----------------------------------------------------------------------------
  1040. // Purpose:
  1041. //-----------------------------------------------------------------------------
  1042. void CPropJeep::DampenUpMotion( Vector &vecVehicleEyePos, QAngle &vecVehicleEyeAngles, float flFrameTime )
  1043. {
  1044. // Get up vector.
  1045. Vector vecUp;
  1046. AngleVectors( vecVehicleEyeAngles, NULL, NULL, &vecUp );
  1047. vecUp.z = clamp( vecUp.z, 0.0f, vecUp.z );
  1048. vecVehicleEyePos.z += r_JeepViewZHeight.GetFloat() * vecUp.z;
  1049. // NOTE: Should probably use some damped equation here.
  1050. }
  1051. //-----------------------------------------------------------------------------
  1052. // Purpose:
  1053. //-----------------------------------------------------------------------------
  1054. void CPropJeep::SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move )
  1055. {
  1056. // If we are overturned and hit any key - leave the vehicle (IN_USE is already handled!).
  1057. if ( m_flOverturnedTime > OVERTURNED_EXIT_WAITTIME )
  1058. {
  1059. if ( (ucmd->buttons & (IN_FORWARD|IN_BACK|IN_MOVELEFT|IN_MOVERIGHT|IN_SPEED|IN_JUMP|IN_ATTACK|IN_ATTACK2) ) && !m_bExitAnimOn )
  1060. {
  1061. // Can't exit yet? We're probably still moving. Swallow the keys.
  1062. if ( !CanExitVehicle(player) )
  1063. return;
  1064. if ( !GetServerVehicle()->HandlePassengerExit( m_hPlayer ) && ( m_hPlayer != NULL ) )
  1065. {
  1066. m_hPlayer->PlayUseDenySound();
  1067. }
  1068. return;
  1069. }
  1070. }
  1071. // If the throttle is disabled or we're upside-down, don't allow throttling (including turbo)
  1072. CUserCmd tmp;
  1073. if ( ( m_throttleDisableTime > gpGlobals->curtime ) || ( IsOverturned() ) )
  1074. {
  1075. m_bUnableToFire = true;
  1076. tmp = (*ucmd);
  1077. tmp.buttons &= ~(IN_FORWARD|IN_BACK|IN_SPEED);
  1078. ucmd = &tmp;
  1079. }
  1080. BaseClass::SetupMove( player, ucmd, pHelper, move );
  1081. }
  1082. //-----------------------------------------------------------------------------
  1083. // Purpose:
  1084. //-----------------------------------------------------------------------------
  1085. void CPropJeep::DriveVehicle( float flFrameTime, CUserCmd *ucmd, int iButtonsDown, int iButtonsReleased )
  1086. {
  1087. int iButtons = ucmd->buttons;
  1088. //Adrian: No headlights on Superfly.
  1089. /* if ( ucmd->impulse == 100 )
  1090. {
  1091. if (HeadlightIsOn())
  1092. {
  1093. HeadlightTurnOff();
  1094. }
  1095. else
  1096. {
  1097. HeadlightTurnOn();
  1098. }
  1099. }*/
  1100. // If we're holding down an attack button, update our state
  1101. if ( IsOverturned() == false )
  1102. {
  1103. if ( iButtons & IN_ATTACK )
  1104. {
  1105. if ( m_bCannonCharging )
  1106. {
  1107. FireChargedCannon();
  1108. }
  1109. else
  1110. {
  1111. FireCannon();
  1112. }
  1113. }
  1114. else if ( iButtons & IN_ATTACK2 )
  1115. {
  1116. ChargeCannon();
  1117. }
  1118. }
  1119. // If we've released our secondary button, fire off our cannon
  1120. if ( ( iButtonsReleased & IN_ATTACK2 ) && ( m_bCannonCharging ) )
  1121. {
  1122. FireChargedCannon();
  1123. }
  1124. BaseClass::DriveVehicle( flFrameTime, ucmd, iButtonsDown, iButtonsReleased );
  1125. }
  1126. //-----------------------------------------------------------------------------
  1127. // Purpose:
  1128. // Input : *pPlayer -
  1129. // *pMoveData -
  1130. //-----------------------------------------------------------------------------
  1131. void CPropJeep::ProcessMovement( CBasePlayer *pPlayer, CMoveData *pMoveData )
  1132. {
  1133. BaseClass::ProcessMovement( pPlayer, pMoveData );
  1134. // Create dangers sounds in front of the vehicle.
  1135. CreateDangerSounds();
  1136. }
  1137. //-----------------------------------------------------------------------------
  1138. // Purpose: Create danger sounds in front of the vehicle.
  1139. //-----------------------------------------------------------------------------
  1140. void CPropJeep::CreateDangerSounds( void )
  1141. {
  1142. QAngle dummy;
  1143. GetAttachment( "Muzzle", m_vecGunOrigin, dummy );
  1144. if ( m_flDangerSoundTime > gpGlobals->curtime )
  1145. return;
  1146. QAngle vehicleAngles = GetLocalAngles();
  1147. Vector vecStart = GetAbsOrigin();
  1148. Vector vecDir, vecRight;
  1149. GetVectors( &vecDir, &vecRight, NULL );
  1150. const float soundDuration = 0.25;
  1151. float speed = m_VehiclePhysics.GetHLSpeed();
  1152. // Make danger sounds ahead of the jeep
  1153. if ( fabs(speed) > 120 )
  1154. {
  1155. Vector vecSpot;
  1156. float steering = m_VehiclePhysics.GetSteering();
  1157. if ( steering != 0 )
  1158. {
  1159. if ( speed > 0 )
  1160. {
  1161. vecDir += vecRight * steering * 0.5;
  1162. }
  1163. else
  1164. {
  1165. vecDir -= vecRight * steering * 0.5;
  1166. }
  1167. VectorNormalize(vecDir);
  1168. }
  1169. const float radius = speed * 0.4;
  1170. // 0.3 seconds ahead of the jeep
  1171. vecSpot = vecStart + vecDir * (speed * 0.3f);
  1172. CSoundEnt::InsertSound( SOUND_DANGER, vecSpot, radius, soundDuration, this, 0 );
  1173. CSoundEnt::InsertSound( SOUND_PHYSICS_DANGER, vecSpot, radius, soundDuration, this, 1 );
  1174. //NDebugOverlay::Box(vecSpot, Vector(-radius,-radius,-radius),Vector(radius,radius,radius), 255, 0, 255, 0, soundDuration);
  1175. #if 0
  1176. trace_t tr;
  1177. // put sounds a bit to left and right but slightly closer to Jeep to make a "cone" of sound
  1178. // in front of it
  1179. vecSpot = vecStart + vecDir * (speed * 0.5f) - vecRight * speed * 0.5;
  1180. UTIL_TraceLine( vecStart, vecSpot, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
  1181. CSoundEnt::InsertSound( SOUND_DANGER, vecSpot, 400, soundDuration, this, 1 );
  1182. vecSpot = vecStart + vecDir * (speed * 0.5f) + vecRight * speed * 0.5;
  1183. UTIL_TraceLine( vecStart, vecSpot, MASK_SHOT, this, COLLISION_GROUP_NONE, &tr );
  1184. CSoundEnt::InsertSound( SOUND_DANGER, vecSpot, 400, soundDuration, this, 2);
  1185. #endif
  1186. }
  1187. m_flDangerSoundTime = gpGlobals->curtime + 0.1;
  1188. }
  1189. //-----------------------------------------------------------------------------
  1190. // Purpose:
  1191. //-----------------------------------------------------------------------------
  1192. void CPropJeep::EnterVehicle( CBasePlayer *pPlayer )
  1193. {
  1194. if ( !pPlayer )
  1195. return;
  1196. CheckWater();
  1197. BaseClass::EnterVehicle( pPlayer );
  1198. // Start looking for seagulls to land
  1199. m_hLastPlayerInVehicle = m_hPlayer;
  1200. }
  1201. //-----------------------------------------------------------------------------
  1202. // Purpose:
  1203. //-----------------------------------------------------------------------------
  1204. void CPropJeep::ExitVehicle( int nRole )
  1205. {
  1206. HeadlightTurnOff();
  1207. BaseClass::ExitVehicle( nRole );
  1208. //If the player has exited, stop charging
  1209. StopChargeSound();
  1210. m_bCannonCharging = false;
  1211. // Remember when we last saw the player
  1212. m_flPlayerExitedTime = gpGlobals->curtime;
  1213. m_flLastSawPlayerAt = gpGlobals->curtime;
  1214. }
  1215. //-----------------------------------------------------------------------------
  1216. // Purpose:
  1217. //-----------------------------------------------------------------------------
  1218. void CPropJeep::InputStartRemoveTauCannon( inputdata_t &inputdata )
  1219. {
  1220. // Start the gun removal animation
  1221. m_flAnimTime = gpGlobals->curtime;
  1222. m_flPlaybackRate = 0.0;
  1223. SetCycle( 0 );
  1224. ResetSequence( LookupSequence( "tau_levitate" ) );
  1225. m_bGunHasBeenCutOff = true;
  1226. }
  1227. //-----------------------------------------------------------------------------
  1228. // Purpose:
  1229. //-----------------------------------------------------------------------------
  1230. void CPropJeep::InputFinishRemoveTauCannon( inputdata_t &inputdata )
  1231. {
  1232. // Remove & hide the gun
  1233. SetBodygroup( 1, false );
  1234. m_bHasGun = false;
  1235. }
  1236. //-----------------------------------------------------------------------------
  1237. // Purpose:
  1238. //-----------------------------------------------------------------------------
  1239. void CPropJeep::OnRestore( void )
  1240. {
  1241. IServerVehicle *pServerVehicle = GetServerVehicle();
  1242. if ( pServerVehicle != NULL )
  1243. {
  1244. // Restore the passenger information we're holding on to
  1245. pServerVehicle->RestorePassengerInfo();
  1246. }
  1247. }
  1248. //========================================================================================================================================
  1249. // JEEP FOUR WHEEL PHYSICS VEHICLE SERVER VEHICLE
  1250. //========================================================================================================================================
  1251. //-----------------------------------------------------------------------------
  1252. // Purpose:
  1253. //-----------------------------------------------------------------------------
  1254. void CJeepFourWheelServerVehicle::NPC_AimPrimaryWeapon( Vector vecTarget )
  1255. {
  1256. ((CPropJeep*)m_pVehicle)->AimGunAt( &vecTarget, 0.1f );
  1257. }
  1258. //-----------------------------------------------------------------------------
  1259. // Purpose:
  1260. // Input : &vecEyeExitEndpoint -
  1261. // Output : int
  1262. //-----------------------------------------------------------------------------
  1263. int CJeepFourWheelServerVehicle::GetExitAnimToUse( Vector &vecEyeExitEndpoint, bool &bAllPointsBlocked )
  1264. {
  1265. bAllPointsBlocked = false;
  1266. if ( !m_bParsedAnimations )
  1267. {
  1268. // Load the entry/exit animations from the vehicle
  1269. ParseEntryExitAnims();
  1270. m_bParsedAnimations = true;
  1271. }
  1272. CBaseAnimating *pAnimating = dynamic_cast<CBaseAnimating *>(m_pVehicle);
  1273. // If we don't have the gun anymore, we want to get out using the "gun-less" animation
  1274. if ( pAnimating && ((CPropJeep*)m_pVehicle)->TauCannonHasBeenCutOff() )
  1275. {
  1276. // HACK: We know the tau-cannon removed exit anim uses the first upright anim's exit details
  1277. trace_t tr;
  1278. // Convert our offset points to worldspace ones
  1279. Vector vehicleExitOrigin = m_ExitAnimations[0].vecExitPointLocal;
  1280. QAngle vehicleExitAngles = m_ExitAnimations[0].vecExitAnglesLocal;
  1281. UTIL_ParentToWorldSpace( pAnimating, vehicleExitOrigin, vehicleExitAngles );
  1282. // Ensure the endpoint is clear by dropping a point down from above
  1283. vehicleExitOrigin -= VEC_VIEW;
  1284. Vector vecMove = Vector(0,0,64);
  1285. Vector vecStart = vehicleExitOrigin + vecMove;
  1286. Vector vecEnd = vehicleExitOrigin - vecMove;
  1287. UTIL_TraceHull( vecStart, vecEnd, VEC_HULL_MIN, VEC_HULL_MAX, MASK_SOLID, NULL, COLLISION_GROUP_NONE, &tr );
  1288. Assert( !tr.startsolid && tr.fraction < 1.0 );
  1289. m_vecCurrentExitEndPoint = vecStart + ((vecEnd - vecStart) * tr.fraction);
  1290. vecEyeExitEndpoint = m_vecCurrentExitEndPoint + VEC_VIEW;
  1291. m_iCurrentExitAnim = 0;
  1292. return pAnimating->LookupSequence( "exit_tauremoved" );
  1293. }
  1294. return BaseClass::GetExitAnimToUse( vecEyeExitEndpoint, bAllPointsBlocked );
  1295. }