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.

6117 lines
188 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "cbase.h"
  7. #include "ai_network.h"
  8. #include "ai_default.h"
  9. #include "ai_schedule.h"
  10. #include "ai_hull.h"
  11. #include "ai_node.h"
  12. #include "ai_task.h"
  13. #include "entitylist.h"
  14. #include "basecombatweapon.h"
  15. #include "soundenvelope.h"
  16. #include "gib.h"
  17. #include "gamerules.h"
  18. #include "ammodef.h"
  19. #include "grenade_homer.h"
  20. #include "cbasehelicopter.h"
  21. #include "engine/IEngineSound.h"
  22. #include "IEffects.h"
  23. #include "globals.h"
  24. #include "explode.h"
  25. #include "movevars_shared.h"
  26. #include "smoke_trail.h"
  27. #include "ar2_explosion.h"
  28. #include "collisionutils.h"
  29. #include "props.h"
  30. #include "EntityFlame.h"
  31. #include "decals.h"
  32. #include "effect_dispatch_data.h"
  33. #include "te_effect_dispatch.h"
  34. #include "ai_spotlight.h"
  35. #include "vphysics/constraints.h"
  36. #include "physics_saverestore.h"
  37. #include "ai_memory.h"
  38. #include "npc_attackchopper.h"
  39. #ifdef HL2_EPISODIC
  40. #include "physics_bone_follower.h"
  41. #endif // HL2_EPISODIC
  42. // memdbgon must be the last include file in a .cpp file!!!
  43. #include "tier0/memdbgon.h"
  44. // -------------------------------------
  45. // Bone controllers
  46. // -------------------------------------
  47. #define CHOPPER_DRONE_NAME "models/combine_helicopter/helicopter_bomb01.mdl"
  48. #define CHOPPER_MODEL_NAME "models/combine_helicopter.mdl"
  49. #define CHOPPER_MODEL_CORPSE_NAME "models/combine_helicopter_broken.mdl"
  50. #define CHOPPER_RED_LIGHT_SPRITE "sprites/redglow1.vmt"
  51. #define CHOPPER_MAX_SMALL_CHUNKS 1
  52. #define CHOPPER_MAX_CHUNKS 3
  53. static const char *s_pChunkModelName[CHOPPER_MAX_CHUNKS] =
  54. {
  55. "models/gibs/helicopter_brokenpiece_01.mdl",
  56. "models/gibs/helicopter_brokenpiece_02.mdl",
  57. "models/gibs/helicopter_brokenpiece_03.mdl",
  58. };
  59. #define BOMB_SKIN_LIGHT_ON 1
  60. #define BOMB_SKIN_LIGHT_OFF 0
  61. #define HELICOPTER_CHUNK_COCKPIT "models/gibs/helicopter_brokenpiece_04_cockpit.mdl"
  62. #define HELICOPTER_CHUNK_TAIL "models/gibs/helicopter_brokenpiece_05_tailfan.mdl"
  63. #define HELICOPTER_CHUNK_BODY "models/gibs/helicopter_brokenpiece_06_body.mdl"
  64. #define CHOPPER_MAX_SPEED (60 * 17.6f)
  65. #define CHOPPER_MAX_FIRING_SPEED 250.0f
  66. #define CHOPPER_MAX_GUN_DIST 2000.0f
  67. #define CHOPPER_ACCEL_RATE 500
  68. #define CHOPPER_ACCEL_RATE_BOOST 1500
  69. #define DEFAULT_FREE_KNOWLEDGE_DURATION 5.0f
  70. // -------------------------------------
  71. // Pathing data
  72. #define CHOPPER_LEAD_DISTANCE 800.0f
  73. #define CHOPPER_MIN_CHASE_DIST_DIFF 128.0f // Distance threshold used to determine when a target has moved enough to update our navigation to it
  74. #define CHOPPER_MIN_AGGRESSIVE_CHASE_DIST_DIFF 64.0f
  75. #define CHOPPER_AVOID_DIST 512.0f
  76. #define CHOPPER_ARRIVE_DIST 128.0f
  77. #define CHOPPER_MAX_CIRCLE_OF_DEATH_FOLLOW_SPEED 450.0f
  78. #define CHOPPER_MIN_CIRCLE_OF_DEATH_RADIUS 150.0f
  79. #define CHOPPER_MAX_CIRCLE_OF_DEATH_RADIUS 350.0f
  80. #define CHOPPER_BOMB_DROP_COUNT 6
  81. // Bullrush
  82. #define CHOPPER_BULLRUSH_MODE_DISTANCE g_helicopter_bullrush_distance.GetFloat()
  83. #define CHOPPER_BULLRUSH_ENEMY_BOMB_DISTANCE g_helicopter_bullrush_bomb_enemy_distance.GetFloat()
  84. #define CHOPPER_BULLRUSH_ENEMY_BOMB_TIME g_helicopter_bullrush_bomb_time.GetFloat()
  85. #define CHOPPER_BULLRUSH_ENEMY_BOMB_SPEED g_helicopter_bullrush_bomb_speed.GetFloat()
  86. #define CHOPPER_BULLRUSH_SHOOTING_VERTICAL_OFFSET g_helicopter_bullrush_shoot_height.GetFloat()
  87. #define CHOPPER_GUN_CHARGE_TIME g_helicopter_chargetime.GetFloat()
  88. #define CHOPPER_GUN_IDLE_TIME g_helicopter_idletime.GetFloat()
  89. #define CHOPPER_GUN_MAX_FIRING_DIST g_helicopter_maxfiringdist.GetFloat()
  90. #define BULLRUSH_IDLE_PLAYER_FIRE_TIME 6.0f
  91. #define DRONE_SPEED sk_helicopter_drone_speed.GetFloat()
  92. #define SF_HELICOPTER_LOUD_ROTOR_SOUND 0x00010000
  93. #define SF_HELICOPTER_ELECTRICAL_DRONE 0x00020000
  94. #define SF_HELICOPTER_LIGHTS 0x00040000
  95. #define SF_HELICOPTER_IGNORE_AVOID_FORCES 0x00080000
  96. #define SF_HELICOPTER_AGGRESSIVE 0x00100000
  97. #define SF_HELICOPTER_LONG_SHADOW 0x00200000
  98. #define CHOPPER_SLOW_BOMB_SPEED 250
  99. #define CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED 250
  100. #define CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED_SQ (CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED * CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED)
  101. #define CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED_2 450
  102. #define CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED_2_SQ (CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED_2 * CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED_2)
  103. // CVars
  104. ConVar sk_helicopter_health( "sk_helicopter_health","5600");
  105. ConVar sk_helicopter_firingcone( "sk_helicopter_firingcone","20.0", 0, "The angle in degrees of the cone in which the shots will be fired" );
  106. ConVar sk_helicopter_burstcount( "sk_helicopter_burstcount","12", 0, "How many shot bursts to fire after charging up. The bigger the number, the longer the firing is" );
  107. ConVar sk_helicopter_roundsperburst( "sk_helicopter_roundsperburst","5", 0, "How many shots to fire in a single burst" );
  108. ConVar sk_helicopter_grenadedamage( "sk_helicopter_grenadedamage","25.0", 0, "The amount of damage the helicopter grenade deals." );
  109. ConVar sk_helicopter_grenaderadius( "sk_helicopter_grenaderadius","275.0", 0, "The damage radius of the helicopter grenade." );
  110. ConVar sk_helicopter_grenadeforce( "sk_helicopter_grenadeforce","55000.0", 0, "The physics force that the helicopter grenade exerts." );
  111. ConVar sk_helicopter_grenade_puntscale( "sk_helicopter_grenade_puntscale","1.5", 0, "When physpunting a chopper's grenade, scale its velocity by this much." );
  112. // Number of bomb hits it takes to kill a chopper on each skill level.
  113. ConVar sk_helicopter_num_bombs1("sk_helicopter_num_bombs1", "3");
  114. ConVar sk_helicopter_num_bombs2("sk_helicopter_num_bombs2", "5");
  115. ConVar sk_helicopter_num_bombs3("sk_helicopter_num_bombs3", "5");
  116. ConVar sk_npc_dmg_helicopter_to_plr( "sk_npc_dmg_helicopter_to_plr","3", 0, "Damage helicopter shots deal to the player" );
  117. ConVar sk_npc_dmg_helicopter( "sk_npc_dmg_helicopter","6", 0, "Damage helicopter shots deal to everything but the player" );
  118. ConVar sk_helicopter_drone_speed( "sk_helicopter_drone_speed","450.0", 0, "How fast does the zapper drone move?" );
  119. ConVar g_helicopter_chargetime( "g_helicopter_chargetime","2.0", 0, "How much time we have to wait (on average) between the time we start hearing the charging sound + the chopper fires" );
  120. ConVar g_helicopter_bullrush_distance("g_helicopter_bullrush_distance", "5000");
  121. ConVar g_helicopter_bullrush_bomb_enemy_distance("g_helicopter_bullrush_bomb_enemy_distance", "0");
  122. ConVar g_helicopter_bullrush_bomb_time("g_helicopter_bullrush_bomb_time", "10");
  123. ConVar g_helicopter_idletime( "g_helicopter_idletime","3.0", 0, "How much time we have to wait (on average) after we fire before we can charge up again" );
  124. ConVar g_helicopter_maxfiringdist( "g_helicopter_maxfiringdist","2500.0", 0, "The maximum distance the player can be from the chopper before it stops firing" );
  125. ConVar g_helicopter_bullrush_bomb_speed( "g_helicopter_bullrush_bomb_speed","850.0", 0, "The maximum distance the player can be from the chopper before it stops firing" );
  126. ConVar g_helicopter_bullrush_shoot_height( "g_helicopter_bullrush_shoot_height","650.0", 0, "The maximum distance the player can be from the chopper before it stops firing" );
  127. ConVar g_helicopter_bullrush_mega_bomb_health( "g_helicopter_bullrush_mega_bomb_health","0.25", 0, "Fraction of the health of the chopper before it mega-bombs" );
  128. ConVar g_helicopter_bomb_danger_radius( "g_helicopter_bomb_danger_radius", "120" );
  129. Activity ACT_HELICOPTER_DROP_BOMB;
  130. Activity ACT_HELICOPTER_CRASHING;
  131. static const char *s_pBlinkLightThinkContext = "BlinkLights";
  132. static const char *s_pSpotlightThinkContext = "SpotlightThink";
  133. static const char *s_pRampSoundContext = "RampSound";
  134. static const char *s_pWarningBlinkerContext = "WarningBlinker";
  135. static const char *s_pAnimateThinkContext = "Animate";
  136. #define CHOPPER_LIGHT_BLINK_TIME 1.0f
  137. #define CHOPPER_LIGHT_BLINK_TIME_SHORT 0.1f
  138. #define BOMB_LIFETIME 2.5f // Don't access this directly. Call GetBombLifetime();
  139. #define BOMB_RAMP_SOUND_TIME 1.0f
  140. enum
  141. {
  142. MAX_HELICOPTER_LIGHTS = 3,
  143. };
  144. enum
  145. {
  146. SF_GRENADE_HELICOPTER_MEGABOMB = 0x1,
  147. };
  148. #define GRENADE_HELICOPTER_MODEL "models/combine_helicopter/helicopter_bomb01.mdl"
  149. LINK_ENTITY_TO_CLASS( info_target_helicopter_crash, CPointEntity );
  150. //------------------------------------------------------------------------------
  151. // Purpose :
  152. //------------------------------------------------------------------------------
  153. static inline float ClampSplineRemapVal( float flValue, float flMinValue, float flMaxValue, float flOutMin, float flOutMax )
  154. {
  155. Assert( flMinValue <= flMaxValue );
  156. float flClampedVal = clamp( flValue, flMinValue, flMaxValue );
  157. return SimpleSplineRemapVal( flClampedVal, flMinValue, flMaxValue, flOutMin, flOutMax );
  158. }
  159. //-----------------------------------------------------------------------------
  160. // The bombs the attack helicopter drops
  161. //-----------------------------------------------------------------------------
  162. enum
  163. {
  164. SKIN_REGULAR,
  165. SKIN_DUD,
  166. };
  167. class CGrenadeHelicopter : public CBaseGrenade
  168. {
  169. DECLARE_DATADESC();
  170. public:
  171. DECLARE_CLASS( CGrenadeHelicopter, CBaseGrenade );
  172. virtual void Precache( );
  173. virtual void Spawn( );
  174. virtual void UpdateOnRemove();
  175. virtual void OnEntityEvent( EntityEvent_t event, void *pEventData );
  176. virtual void PhysicsSimulate( void );
  177. virtual float GetShakeAmplitude( void ) { return 25.0; }
  178. virtual float GetShakeRadius( void ) { return sk_helicopter_grenaderadius.GetFloat() * 2; }
  179. virtual int OnTakeDamage( const CTakeDamageInfo &info );
  180. virtual void VPhysicsCollision( int index, gamevcollisionevent_t *pEvent );
  181. void SetExplodeOnContact( bool bExplode ) { m_bExplodeOnContact = bExplode; }
  182. virtual QAngle PreferredCarryAngles( void ) { return QAngle( -12, 98, 55 ); }
  183. virtual bool HasPreferredCarryAnglesForPlayer( CBasePlayer *pPlayer ) { return true; }
  184. float GetBombLifetime();
  185. #ifdef HL2_EPISODIC
  186. virtual void OnPhysGunPickup(CBasePlayer *pPhysGunUser, PhysGunPickup_t reason );
  187. virtual void OnPhysGunDrop( CBasePlayer *pPhysGunUser, PhysGunDrop_t reason );
  188. virtual Class_T Classify( void ) { return CLASS_MISSILE; }
  189. void SetCollisionObject( CBaseEntity *pEntity ) { m_hCollisionObject = pEntity; }
  190. void SendMissEvent();
  191. bool IsThrownByPlayer();
  192. virtual bool ShouldPuntUseLaunchForces( PhysGunForce_t reason ) { return ( reason == PHYSGUN_FORCE_LAUNCHED ); }
  193. virtual Vector PhysGunLaunchVelocity( const Vector &forward, float flMass );
  194. void InputExplodeIn( inputdata_t &inputdata );
  195. #endif // HL2_EPISODIC
  196. private:
  197. // Pow!
  198. void DoExplosion( const Vector &vecOrigin, const Vector &vecVelocity );
  199. void ExplodeThink();
  200. void RampSoundThink();
  201. void WarningBlinkerThink();
  202. void StopWarningBlinker();
  203. void AnimateThink();
  204. void ExplodeConcussion( CBaseEntity *pOther );
  205. void BecomeActive();
  206. void ResolveFlyCollisionCustom( trace_t &trace, Vector &vecVelocity );
  207. bool m_bActivated;
  208. bool m_bExplodeOnContact;
  209. CSoundPatch *m_pWarnSound;
  210. EHANDLE m_hWarningSprite;
  211. bool m_bBlinkerAtTop;
  212. #ifdef HL2_EPISODIC
  213. float m_flLifetime;
  214. EHANDLE m_hCollisionObject; // Pointer to object we re-enable collisions with when picked up
  215. bool m_bPickedUp;
  216. float m_flBlinkFastTime;
  217. COutputEvent m_OnPhysGunOnlyPickup;
  218. #endif // HL2_EPISODIC
  219. };
  220. //-----------------------------------------------------------------------------
  221. // The bombs the attack helicopter drops
  222. //-----------------------------------------------------------------------------
  223. class CBombDropSensor : public CBaseEntity
  224. {
  225. DECLARE_DATADESC();
  226. public:
  227. DECLARE_CLASS( CBombDropSensor, CBaseEntity );
  228. void Spawn();
  229. // Drop a bomb at a particular location
  230. void InputDropBomb( inputdata_t &inputdata );
  231. void InputDropBombStraightDown( inputdata_t &inputdata );
  232. void InputDropBombAtTarget( inputdata_t &inputdata );
  233. void InputDropBombAtTargetAlways( inputdata_t &inputdata );
  234. void InputDropBombDelay( inputdata_t &inputdata );
  235. };
  236. //-----------------------------------------------------------------------------
  237. // This entity is used to create boxes that the helicopter can't bomb in
  238. //-----------------------------------------------------------------------------
  239. class CBombSuppressor : public CBaseEntity
  240. {
  241. DECLARE_DATADESC();
  242. public:
  243. DECLARE_CLASS( CBombSuppressor, CBaseEntity );
  244. virtual void Spawn( );
  245. virtual void Activate();
  246. virtual void UpdateOnRemove();
  247. static bool CanBomb( const Vector &vecPosition );
  248. private:
  249. typedef CHandle<CBombSuppressor> BombSuppressorHandle_t;
  250. static CUtlVector< BombSuppressorHandle_t > s_BombSuppressors;
  251. };
  252. enum
  253. {
  254. CHUNK_COCKPIT,
  255. CHUNK_BODY,
  256. CHUNK_TAIL
  257. };
  258. //-----------------------------------------------------------------------------
  259. // This entity is used for helicopter gibs with specific properties
  260. //-----------------------------------------------------------------------------
  261. class CHelicopterChunk : public CBaseAnimating
  262. {
  263. DECLARE_DATADESC();
  264. public:
  265. DECLARE_CLASS( CHelicopterChunk, CBaseAnimating );
  266. virtual void Spawn( void );
  267. virtual void VPhysicsCollision( int index, gamevcollisionevent_t *pEvent );
  268. static CHelicopterChunk *CreateHelicopterChunk( const Vector &vecPos, const QAngle &vecAngles, const Vector &vecVelocity, const char *pszModelName, int chunkID );
  269. int m_nChunkID;
  270. CHandle<CHelicopterChunk> m_hMaster;
  271. IPhysicsConstraint *m_pTailConstraint;
  272. IPhysicsConstraint *m_pCockpitConstraint;
  273. protected:
  274. void CollisionCallback( CHelicopterChunk *pCaller );
  275. void FallThink( void );
  276. bool m_bLanded;
  277. };
  278. //-----------------------------------------------------------------------------
  279. // The attack helicopter
  280. //-----------------------------------------------------------------------------
  281. class CNPC_AttackHelicopter : public CBaseHelicopter
  282. {
  283. public:
  284. DECLARE_CLASS( CNPC_AttackHelicopter, CBaseHelicopter );
  285. DECLARE_DATADESC();
  286. DEFINE_CUSTOM_AI;
  287. CNPC_AttackHelicopter();
  288. ~CNPC_AttackHelicopter();
  289. virtual void Precache( void );
  290. virtual void Spawn( void );
  291. virtual void Activate( void );
  292. virtual bool CreateComponents();
  293. virtual int ObjectCaps();
  294. #ifdef HL2_EPISODIC
  295. virtual bool CreateVPhysics( void );
  296. #endif // HL2_EPISODIC
  297. virtual void UpdateOnRemove();
  298. virtual void StopLoopingSounds();
  299. int BloodColor( void ) { return DONT_BLEED; }
  300. Class_T Classify ( void ) { return CLASS_COMBINE_GUNSHIP; }
  301. virtual int OnTakeDamage_Alive( const CTakeDamageInfo &info );
  302. virtual void TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator );
  303. virtual int OnTakeDamage( const CTakeDamageInfo &info );
  304. // Shot spread
  305. virtual Vector GetAttackSpread( CBaseCombatWeapon *pWeapon, CBaseEntity *pTarget );
  306. // More Enemy visibility check
  307. virtual bool FVisible( CBaseEntity *pEntity, int traceMask = MASK_BLOCKLOS, CBaseEntity **ppBlocker = NULL );
  308. // Think!
  309. virtual void PrescheduleThink( void );
  310. // Purpose: Set the gunship's paddles flailing!
  311. virtual void Event_Killed( const CTakeDamageInfo &info );
  312. // Drop a bomb at a particular location
  313. void InputDropBomb( inputdata_t &inputdata );
  314. void InputDropBombStraightDown( inputdata_t &inputdata );
  315. void InputDropBombAtTarget( inputdata_t &inputdata );
  316. void InputDropBombAtTargetAlways( inputdata_t &inputdata );
  317. void InputDropBombAtTargetInternal( inputdata_t &inputdata, bool bCheckFairness );
  318. void InputDropBombDelay( inputdata_t &inputdata );
  319. void InputStartCarpetBombing( inputdata_t &inputdata );
  320. void InputStopCarpetBombing( inputdata_t &inputdata );
  321. virtual void SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways );
  322. virtual const char *GetTracerType( void );
  323. virtual void DoImpactEffect( trace_t &tr, int nDamageType );
  324. virtual void DoMuzzleFlash( void );
  325. // FIXME: Work this back into the base class
  326. virtual bool ShouldUseFixedPatrolLogic() { return true; }
  327. protected:
  328. int m_poseWeapon_Pitch, m_poseWeapon_Yaw, m_poseRudder;
  329. virtual void PopulatePoseParameters( void );
  330. private:
  331. enum GunState_t
  332. {
  333. GUN_STATE_IDLE = 0,
  334. GUN_STATE_CHARGING,
  335. GUN_STATE_FIRING,
  336. };
  337. // Gets the max speed of the helicopter
  338. virtual float GetMaxSpeed();
  339. virtual float GetMaxSpeedFiring();
  340. // Startup the chopper
  341. virtual void Startup();
  342. void InitializeRotorSound( void );
  343. // Weaponry
  344. bool FireGun( void );
  345. // Movement:
  346. virtual void Flight( void );
  347. // Changes the main thinking method of helicopters
  348. virtual void Hunt( void );
  349. // For scripted times where it *has* to shoot
  350. void InputResetIdleTime( inputdata_t &inputdata );
  351. void InputSetHealthFraction( inputdata_t &inputdata );
  352. void InputStartBombExplodeOnContact( inputdata_t &inputdata );
  353. void InputStopBombExplodeOnContact( inputdata_t &inputdata );
  354. void InputEnableAlwaysTransition( inputdata_t &inputdata );
  355. void InputDisableAlwaysTransition( inputdata_t &inputdata );
  356. void InputOutsideTransition( inputdata_t &inputdata );
  357. void InputSetOutsideTransitionTarget( inputdata_t &inputdata );
  358. // Turns off the gun
  359. void InputGunOff( inputdata_t &inputdata );
  360. // Vehicle attack modes
  361. void InputStartBombingVehicle( inputdata_t &inputdata );
  362. void InputStartTrailingVehicle( inputdata_t &inputdata );
  363. void InputStartDefaultBehavior( inputdata_t &inputdata );
  364. void InputStartAlwaysLeadingVehicle( inputdata_t &inputdata );
  365. // Deadly shooting, tex!
  366. void InputEnableDeadlyShooting( inputdata_t &inputdata );
  367. void InputDisableDeadlyShooting( inputdata_t &inputdata );
  368. void InputStartNormalShooting( inputdata_t &inputdata );
  369. void InputStartLongCycleShooting( inputdata_t &inputdata );
  370. void InputStartContinuousShooting( inputdata_t &inputdata );
  371. void InputStartFastShooting( inputdata_t &inputdata );
  372. int GetShootingMode( );
  373. bool IsDeadlyShooting();
  374. // Bombing suppression
  375. void InputEnableBombing( inputdata_t &inputdata );
  376. void InputDisableBombing( inputdata_t &inputdata );
  377. // Visibility tests
  378. void InputDisablePathVisibilityTests( inputdata_t &inputdata );
  379. void InputEnablePathVisibilityTests( inputdata_t &inputdata );
  380. // Death, etc.
  381. void InputSelfDestruct( inputdata_t &inputdata );
  382. // Enemy visibility check
  383. CBaseEntity *FindTrackBlocker( const Vector &vecViewPoint, const Vector &vecTargetPos );
  384. // Special path navigation when we explicitly want to follow a path
  385. void UpdateFollowPathNavigation();
  386. // Find interesting nearby things to shoot
  387. int BuildMissTargetList( int nCount, CBaseEntity **ppMissCandidates );
  388. // Shoot when the player's your enemy :
  389. void ShootAtPlayer( const Vector &vBasePos, const Vector &vGunDir );
  390. // Shoot when the player's your enemy + he's driving a vehicle
  391. void ShootAtVehicle( const Vector &vBasePos, const Vector &vGunDir );
  392. // Shoot where we're facing
  393. void ShootAtFacingDirection( const Vector &vBasePos, const Vector &vGunDir, bool bFirstShotAccurate );
  394. // Updates the facing direction
  395. void UpdateFacingDirection( const Vector &vecActualDesiredPosition );
  396. // Various states of the helicopter firing...
  397. bool PoseGunTowardTargetDirection( const Vector &vTargetDir );
  398. // Compute the position to fire at (vehicle + non-vehicle case)
  399. void ComputeFireAtPosition( Vector *pVecActualTargetPosition );
  400. void ComputeVehicleFireAtPosition( Vector *pVecActualTargetPosition );
  401. // Various states of the helicopter firing...
  402. bool DoGunIdle( const Vector &vecGunDir, const Vector &vTargetDir );
  403. bool DoGunCharging( );
  404. bool DoGunFiring( const Vector &vBasePos, const Vector &vGunDir, const Vector &vecFireAtPosition );
  405. void FireElectricityGun( );
  406. // Chooses a point within the circle of death to fire in
  407. void PickDirectionToCircleOfDeath( const Vector &vBasePos, const Vector &vecFireAtPosition, Vector *pResult );
  408. // Gets a vehicle the enemy is in (if any)
  409. CBaseEntity *GetEnemyVehicle();
  410. // Updates the perpendicular path distance for the chopper
  411. float UpdatePerpPathDistance( float flMaxPathOffset );
  412. // Purpose :
  413. void UpdateEnemyLeading( void );
  414. // Drop those bombs!
  415. void DropBombs( );
  416. // Should we drop those bombs?
  417. bool ShouldDropBombs( void );
  418. // Returns the max firing distance
  419. float GetMaxFiringDistance();
  420. // Make sure we don't hit too many times
  421. void FireBullets( const FireBulletsInfo_t &info );
  422. // Is it "fair" to drop this bomb?
  423. bool IsBombDropFair( const Vector &vecBombStartPos, const Vector &vecVelocity );
  424. // Actually drops the bomb
  425. void CreateBomb( bool bCheckForFairness = true, Vector *pVecVelocity = NULL, bool bMegaBomb = false );
  426. CGrenadeHelicopter *SpawnBombEntity( const Vector &vecPos, const Vector &vecVelocity ); // Spawns the bomb entity and sets it up
  427. // Deliberately aims as close as possible w/o hitting
  428. void AimCloseToTargetButMiss( CBaseEntity *pTarget, float flMinDist, float flMaxDist, const Vector &shootOrigin, Vector *pResult );
  429. // Pops a shot inside the circle of death using the burst rules
  430. void ShootInsideCircleOfDeath( const Vector &vBasePos, const Vector &vecFireAtPosition );
  431. // How easy is the target to hit?
  432. void UpdateTargetHittability();
  433. // Add a smoke trail since we've taken more damage
  434. void AddSmokeTrail( const Vector &vecPos );
  435. // Destroy all smoke trails
  436. void DestroySmokeTrails();
  437. // Creates the breakable husk of an attack chopper
  438. void CreateChopperHusk();
  439. // Pow!
  440. void ExplodeAndThrowChunk( const Vector &vecExplosionPos );
  441. // Drop a corpse!
  442. void DropCorpse( int nDamage );
  443. // Should we trigger a damage effect?
  444. bool ShouldTriggerDamageEffect( int nPrevHealth, int nEffectCount ) const;
  445. // Become indestructible
  446. void InputBecomeIndestructible( inputdata_t &inputdata );
  447. // Purpose :
  448. float CreepTowardEnemy( float flSpeed, float flMinSpeed, float flMaxSpeed, float flMinDist, float flMaxDist );
  449. // Start bullrush
  450. void InputStartBullrushBehavior( inputdata_t &inputdata );
  451. void GetMaxSpeedAndAccel( float *pMaxSpeed, float *pAccelRate );
  452. void ComputeAngularVelocity( const Vector &vecGoalUp, const Vector &vecFacingDirection );
  453. void ComputeVelocity( const Vector &deltaPos, float flAdditionalHeight, float flMinDistFromSegment, float flMaxDistFromSegment, Vector *pVecAccel );
  454. void FlightDirectlyOverhead( void );
  455. // Methods related to computing leading distance
  456. float ComputeBombingLeadingDistance( float flSpeed, float flSpeedAlongPath, bool bEnemyInVehicle );
  457. float ComputeBullrushLeadingDistance( float flSpeed, float flSpeedAlongPath, bool bEnemyInVehicle );
  458. bool IsCarpetBombing() { return m_bIsCarpetBombing == true; }
  459. // Update the bullrush state
  460. void UpdateBullrushState( void );
  461. // Whether to shoot at or bomb an idle player
  462. bool ShouldBombIdlePlayer( void );
  463. // Different bomb-dropping behavior
  464. void BullrushBombs( );
  465. // Switch to idle
  466. void SwitchToBullrushIdle( void );
  467. // Secondary mode
  468. void SetSecondaryMode( int nMode, bool bRetainTime = false );
  469. bool IsInSecondaryMode( int nMode );
  470. float SecondaryModeTime( ) const;
  471. // Should the chopper shoot the idle player?
  472. bool ShouldShootIdlePlayerInBullrush();
  473. // Shutdown shooting during bullrush
  474. void ShutdownGunDuringBullrush( );
  475. // Updates the enemy
  476. virtual float EnemySearchDistance( );
  477. // Prototype zapper
  478. bool IsValidZapTarget( CBaseEntity *pTarget );
  479. void CreateZapBeam( const Vector &vecTargetPos );
  480. void CreateEntityZapEffect( CBaseEntity *pEnt );
  481. // Blink lights
  482. void BlinkLightsThink();
  483. // Spotlights
  484. void SpotlightThink();
  485. void SpotlightStartup();
  486. void SpotlightShutdown();
  487. CBaseEntity *GetCrashPoint() { return m_hCrashPoint.Get(); }
  488. private:
  489. enum
  490. {
  491. ATTACK_MODE_DEFAULT = 0,
  492. ATTACK_MODE_BOMB_VEHICLE,
  493. ATTACK_MODE_TRAIL_VEHICLE,
  494. ATTACK_MODE_ALWAYS_LEAD_VEHICLE,
  495. ATTACK_MODE_BULLRUSH_VEHICLE,
  496. };
  497. enum
  498. {
  499. MAX_SMOKE_TRAILS = 5,
  500. MAX_EXPLOSIONS = 13,
  501. MAX_CORPSES = 2,
  502. };
  503. enum
  504. {
  505. BULLRUSH_MODE_WAIT_FOR_ENEMY = 0,
  506. BULLRUSH_MODE_GET_DISTANCE,
  507. BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED,
  508. BULLRUSH_MODE_DROP_BOMBS_FOLLOW_PLAYER,
  509. BULLRUSH_MODE_SHOOT_GUN,
  510. BULLRUSH_MODE_MEGA_BOMB,
  511. BULLRUSH_MODE_SHOOT_IDLE_PLAYER,
  512. };
  513. enum
  514. {
  515. SHOOT_MODE_DEFAULT = 0,
  516. SHOOT_MODE_LONG_CYCLE,
  517. SHOOT_MODE_CONTINUOUS,
  518. SHOOT_MODE_FAST,
  519. };
  520. #ifdef HL2_EPISODIC
  521. void InitBoneFollowers( void );
  522. CBoneFollowerManager m_BoneFollowerManager;
  523. #endif // HL2_EPISODIC
  524. CAI_Spotlight m_Spotlight;
  525. Vector m_angGun;
  526. QAngle m_vecAngAcceleration;
  527. int m_iAmmoType;
  528. float m_flLastCorpseFall;
  529. GunState_t m_nGunState;
  530. float m_flChargeTime;
  531. float m_flIdleTimeDelay;
  532. int m_nRemainingBursts;
  533. int m_nGrenadeCount;
  534. float m_flPathOffset;
  535. float m_flAcrossTime;
  536. float m_flCurrPathOffset;
  537. int m_nBurstHits;
  538. int m_nMaxBurstHits;
  539. float m_flCircleOfDeathRadius;
  540. int m_nAttackMode;
  541. float m_flInputDropBombTime;
  542. CHandle<CBombDropSensor> m_hSensor;
  543. float m_flAvoidMetric;
  544. AngularImpulse m_vecLastAngVelocity;
  545. CHandle<CBaseEntity> m_hSmokeTrail[MAX_SMOKE_TRAILS];
  546. int m_nSmokeTrailCount;
  547. bool m_bIndestructible;
  548. float m_flGracePeriod;
  549. bool m_bBombsExplodeOnContact;
  550. bool m_bNonCombat;
  551. int m_nNearShots;
  552. int m_nMaxNearShots;
  553. // Bomb dropping attachments
  554. int m_nGunTipAttachment;
  555. int m_nGunBaseAttachment;
  556. int m_nBombAttachment;
  557. int m_nSpotlightAttachment;
  558. float m_flLastFastTime;
  559. // Secondary modes
  560. int m_nSecondaryMode;
  561. float m_flSecondaryModeStartTime;
  562. // Bullrush behavior
  563. bool m_bRushForward;
  564. float m_flBullrushAdditionalHeight;
  565. int m_nBullrushBombMode;
  566. float m_flNextBullrushBombTime;
  567. float m_flNextMegaBombHealth;
  568. // Shooting method
  569. int m_nShootingMode;
  570. bool m_bDeadlyShooting;
  571. // Bombing suppression
  572. bool m_bBombingSuppressed;
  573. // Blinking lights
  574. CHandle<CSprite> m_hLights[MAX_HELICOPTER_LIGHTS];
  575. bool m_bShortBlink;
  576. // Path behavior
  577. bool m_bIgnorePathVisibilityTests;
  578. // Teleport
  579. bool m_bAlwaysTransition;
  580. string_t m_iszTransitionTarget;
  581. // Special attacks
  582. bool m_bIsCarpetBombing;
  583. // Fun damage effects
  584. float m_flGoalRollDmg;
  585. float m_flGoalYawDmg;
  586. // Sounds
  587. CSoundPatch *m_pGunFiringSound;
  588. // Outputs
  589. COutputInt m_OnHealthChanged;
  590. COutputEvent m_OnShotDown;
  591. // Crashing
  592. EHANDLE m_hCrashPoint;
  593. };
  594. #ifdef HL2_EPISODIC
  595. static const char *pFollowerBoneNames[] =
  596. {
  597. "Chopper.Body"
  598. };
  599. #endif // HL2_EPISODIC
  600. LINK_ENTITY_TO_CLASS( npc_helicopter, CNPC_AttackHelicopter );
  601. BEGIN_DATADESC( CNPC_AttackHelicopter )
  602. DEFINE_ENTITYFUNC( FlyTouch ),
  603. DEFINE_EMBEDDED( m_Spotlight ),
  604. #ifdef HL2_EPISODIC
  605. DEFINE_EMBEDDED( m_BoneFollowerManager ),
  606. #endif
  607. DEFINE_FIELD( m_angGun, FIELD_VECTOR ),
  608. DEFINE_FIELD( m_vecAngAcceleration, FIELD_VECTOR ),
  609. DEFINE_FIELD( m_iAmmoType, FIELD_INTEGER ),
  610. DEFINE_FIELD( m_flLastCorpseFall, FIELD_TIME ),
  611. DEFINE_FIELD( m_nGunState, FIELD_INTEGER ),
  612. DEFINE_FIELD( m_flChargeTime, FIELD_TIME ),
  613. DEFINE_FIELD( m_flIdleTimeDelay, FIELD_FLOAT ),
  614. DEFINE_FIELD( m_nRemainingBursts, FIELD_INTEGER ),
  615. DEFINE_FIELD( m_nGrenadeCount, FIELD_INTEGER ),
  616. DEFINE_FIELD( m_flPathOffset, FIELD_FLOAT ),
  617. DEFINE_FIELD( m_flAcrossTime, FIELD_TIME ),
  618. DEFINE_FIELD( m_flCurrPathOffset, FIELD_FLOAT ),
  619. DEFINE_FIELD( m_nBurstHits, FIELD_INTEGER ),
  620. DEFINE_FIELD( m_nMaxBurstHits, FIELD_INTEGER ),
  621. DEFINE_FIELD( m_flCircleOfDeathRadius, FIELD_FLOAT ),
  622. DEFINE_FIELD( m_nAttackMode, FIELD_INTEGER ),
  623. DEFINE_FIELD( m_flInputDropBombTime, FIELD_TIME ),
  624. DEFINE_FIELD( m_hSensor, FIELD_EHANDLE ),
  625. DEFINE_FIELD( m_flAvoidMetric, FIELD_FLOAT ),
  626. DEFINE_FIELD( m_vecLastAngVelocity, FIELD_VECTOR ),
  627. DEFINE_AUTO_ARRAY( m_hSmokeTrail, FIELD_EHANDLE ),
  628. DEFINE_FIELD( m_nSmokeTrailCount, FIELD_INTEGER ),
  629. DEFINE_FIELD( m_nNearShots, FIELD_INTEGER ),
  630. DEFINE_FIELD( m_nMaxNearShots, FIELD_INTEGER ),
  631. // DEFINE_FIELD( m_nGunTipAttachment, FIELD_INTEGER ),
  632. // DEFINE_FIELD( m_nGunBaseAttachment, FIELD_INTEGER ),
  633. // DEFINE_FIELD( m_nBombAttachment, FIELD_INTEGER ),
  634. // DEFINE_FIELD( m_nSpotlightAttachment, FIELD_INTEGER ),
  635. DEFINE_FIELD( m_flLastFastTime, FIELD_TIME ),
  636. DEFINE_FIELD( m_nSecondaryMode, FIELD_INTEGER ),
  637. DEFINE_FIELD( m_flSecondaryModeStartTime, FIELD_TIME ),
  638. DEFINE_FIELD( m_bRushForward, FIELD_BOOLEAN ),
  639. DEFINE_FIELD( m_flBullrushAdditionalHeight, FIELD_FLOAT ),
  640. DEFINE_FIELD( m_nBullrushBombMode, FIELD_INTEGER ),
  641. DEFINE_FIELD( m_flNextBullrushBombTime, FIELD_TIME ),
  642. DEFINE_FIELD( m_flNextMegaBombHealth, FIELD_FLOAT ),
  643. DEFINE_FIELD( m_nShootingMode, FIELD_INTEGER ),
  644. DEFINE_FIELD( m_bDeadlyShooting, FIELD_BOOLEAN ),
  645. DEFINE_FIELD( m_bBombingSuppressed, FIELD_BOOLEAN ),
  646. DEFINE_SOUNDPATCH( m_pGunFiringSound ),
  647. DEFINE_AUTO_ARRAY( m_hLights, FIELD_EHANDLE ),
  648. DEFINE_FIELD( m_bIgnorePathVisibilityTests, FIELD_BOOLEAN ),
  649. DEFINE_FIELD( m_bShortBlink, FIELD_BOOLEAN ),
  650. DEFINE_FIELD( m_bIndestructible, FIELD_BOOLEAN ),
  651. DEFINE_FIELD( m_bBombsExplodeOnContact, FIELD_BOOLEAN ),
  652. DEFINE_KEYFIELD( m_bAlwaysTransition, FIELD_BOOLEAN, "AlwaysTransition" ),
  653. DEFINE_KEYFIELD( m_iszTransitionTarget, FIELD_STRING, "TransitionTarget" ),
  654. DEFINE_FIELD( m_bIsCarpetBombing, FIELD_BOOLEAN ),
  655. DEFINE_INPUTFUNC( FIELD_VOID, "EnableAlwaysTransition", InputEnableAlwaysTransition ),
  656. DEFINE_INPUTFUNC( FIELD_VOID, "DisableAlwaysTransition", InputDisableAlwaysTransition ),
  657. DEFINE_INPUTFUNC( FIELD_VOID, "OutsideTransition", InputOutsideTransition ),
  658. DEFINE_INPUTFUNC( FIELD_STRING, "SetTransitionTarget", InputSetOutsideTransitionTarget ),
  659. DEFINE_KEYFIELD( m_flGracePeriod, FIELD_FLOAT, "GracePeriod" ),
  660. DEFINE_KEYFIELD( m_flMaxSpeed, FIELD_FLOAT, "PatrolSpeed" ),
  661. DEFINE_KEYFIELD( m_bNonCombat, FIELD_BOOLEAN, "NonCombat" ),
  662. DEFINE_FIELD( m_hCrashPoint, FIELD_EHANDLE ),
  663. DEFINE_INPUTFUNC( FIELD_VOID, "ResetIdleTime", InputResetIdleTime ),
  664. DEFINE_INPUTFUNC( FIELD_VOID, "StartAlwaysLeadingVehicle", InputStartAlwaysLeadingVehicle ),
  665. DEFINE_INPUTFUNC( FIELD_VOID, "StartBombingVehicle", InputStartBombingVehicle ),
  666. DEFINE_INPUTFUNC( FIELD_VOID, "StartTrailingVehicle", InputStartTrailingVehicle ),
  667. DEFINE_INPUTFUNC( FIELD_VOID, "StartDefaultBehavior", InputStartDefaultBehavior ),
  668. DEFINE_INPUTFUNC( FIELD_VOID, "StartBullrushBehavior", InputStartBullrushBehavior ),
  669. DEFINE_INPUTFUNC( FIELD_VOID, "DropBomb", InputDropBomb ),
  670. DEFINE_INPUTFUNC( FIELD_VOID, "DropBombStraightDown", InputDropBombStraightDown ),
  671. DEFINE_INPUTFUNC( FIELD_STRING, "DropBombAtTargetAlways", InputDropBombAtTargetAlways ),
  672. DEFINE_INPUTFUNC( FIELD_STRING, "DropBombAtTarget", InputDropBombAtTarget ),
  673. DEFINE_INPUTFUNC( FIELD_FLOAT, "DropBombDelay", InputDropBombDelay ),
  674. DEFINE_INPUTFUNC( FIELD_VOID, "StartCarpetBombing", InputStartCarpetBombing ),
  675. DEFINE_INPUTFUNC( FIELD_VOID, "StopCarpetBombing", InputStopCarpetBombing ),
  676. DEFINE_INPUTFUNC( FIELD_VOID, "BecomeIndestructible", InputBecomeIndestructible ),
  677. DEFINE_INPUTFUNC( FIELD_VOID, "EnableDeadlyShooting", InputEnableDeadlyShooting ),
  678. DEFINE_INPUTFUNC( FIELD_VOID, "DisableDeadlyShooting", InputDisableDeadlyShooting ),
  679. DEFINE_INPUTFUNC( FIELD_VOID, "StartNormalShooting", InputStartNormalShooting ),
  680. DEFINE_INPUTFUNC( FIELD_VOID, "StartLongCycleShooting", InputStartLongCycleShooting ),
  681. DEFINE_INPUTFUNC( FIELD_VOID, "StartContinuousShooting", InputStartContinuousShooting ),
  682. DEFINE_INPUTFUNC( FIELD_VOID, "StartFastShooting", InputStartFastShooting ),
  683. DEFINE_INPUTFUNC( FIELD_VOID, "GunOff", InputGunOff ),
  684. DEFINE_INPUTFUNC( FIELD_FLOAT, "SetHealthFraction", InputSetHealthFraction ),
  685. DEFINE_INPUTFUNC( FIELD_VOID, "StartBombExplodeOnContact", InputStartBombExplodeOnContact ),
  686. DEFINE_INPUTFUNC( FIELD_VOID, "StopBombExplodeOnContact", InputStopBombExplodeOnContact ),
  687. DEFINE_INPUTFUNC( FIELD_VOID, "DisablePathVisibilityTests", InputDisablePathVisibilityTests ),
  688. DEFINE_INPUTFUNC( FIELD_VOID, "EnablePathVisibilityTests", InputEnablePathVisibilityTests ),
  689. DEFINE_INPUTFUNC( FIELD_VOID, "SelfDestruct", InputSelfDestruct ),
  690. DEFINE_THINKFUNC( BlinkLightsThink ),
  691. DEFINE_THINKFUNC( SpotlightThink ),
  692. DEFINE_OUTPUT( m_OnHealthChanged, "OnHealthChanged" ),
  693. DEFINE_OUTPUT( m_OnShotDown, "OnShotDown" ),
  694. END_DATADESC()
  695. //------------------------------------------------------------------------------
  696. // Purpose :
  697. //------------------------------------------------------------------------------
  698. CNPC_AttackHelicopter::CNPC_AttackHelicopter() :
  699. m_bNonCombat( false ),
  700. m_flGracePeriod( 2.0f ),
  701. m_bBombsExplodeOnContact( false )
  702. {
  703. m_flMaxSpeed = 0;
  704. }
  705. CNPC_AttackHelicopter::~CNPC_AttackHelicopter(void)
  706. {
  707. }
  708. //-----------------------------------------------------------------------------
  709. // Purpose: Shuts down looping sounds when we are killed in combat or deleted.
  710. //-----------------------------------------------------------------------------
  711. void CNPC_AttackHelicopter::StopLoopingSounds()
  712. {
  713. BaseClass::StopLoopingSounds();
  714. if ( m_pGunFiringSound )
  715. {
  716. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  717. controller.SoundDestroy( m_pGunFiringSound );
  718. m_pGunFiringSound = NULL;
  719. }
  720. }
  721. //------------------------------------------------------------------------------
  722. // Purpose :
  723. //------------------------------------------------------------------------------
  724. void Chopper_PrecacheChunks( CBaseEntity *pChopper )
  725. {
  726. for ( int i = 0; i < CHOPPER_MAX_CHUNKS; ++i )
  727. {
  728. pChopper->PrecacheModel( s_pChunkModelName[i] );
  729. }
  730. pChopper->PrecacheModel( HELICOPTER_CHUNK_COCKPIT );
  731. pChopper->PrecacheModel( HELICOPTER_CHUNK_TAIL );
  732. pChopper->PrecacheModel( HELICOPTER_CHUNK_BODY );
  733. }
  734. //------------------------------------------------------------------------------
  735. // Purpose :
  736. //------------------------------------------------------------------------------
  737. void CNPC_AttackHelicopter::Precache( void )
  738. {
  739. BaseClass::Precache();
  740. if ( !HasSpawnFlags(SF_HELICOPTER_ELECTRICAL_DRONE) )
  741. {
  742. PrecacheModel( CHOPPER_MODEL_NAME );
  743. }
  744. else
  745. {
  746. PrecacheModel( CHOPPER_DRONE_NAME );
  747. }
  748. PrecacheModel( CHOPPER_RED_LIGHT_SPRITE );
  749. //PrecacheModel( CHOPPER_MODEL_CORPSE_NAME );
  750. // If we're never going to engage in combat, we don't need to load these assets!
  751. if ( m_bNonCombat == false )
  752. {
  753. UTIL_PrecacheOther( "grenade_helicopter" );
  754. UTIL_PrecacheOther( "env_fire_trail" );
  755. Chopper_PrecacheChunks( this );
  756. PrecacheModel("models/combine_soldier.mdl");
  757. }
  758. PrecacheScriptSound("NPC_AttackHelicopter.ChargeGun");
  759. if ( HasSpawnFlags( SF_HELICOPTER_LOUD_ROTOR_SOUND ) )
  760. {
  761. PrecacheScriptSound("NPC_AttackHelicopter.RotorsLoud");
  762. }
  763. else
  764. {
  765. PrecacheScriptSound("NPC_AttackHelicopter.Rotors");
  766. }
  767. PrecacheScriptSound( "NPC_AttackHelicopter.DropMine" );
  768. PrecacheScriptSound( "NPC_AttackHelicopter.BadlyDamagedAlert" );
  769. PrecacheScriptSound( "NPC_AttackHelicopter.CrashingAlarm1" );
  770. PrecacheScriptSound( "NPC_AttackHelicopter.MegabombAlert" );
  771. PrecacheScriptSound( "NPC_AttackHelicopter.RotorBlast" );
  772. PrecacheScriptSound( "NPC_AttackHelicopter.EngineFailure" );
  773. PrecacheScriptSound( "NPC_AttackHelicopter.FireGun" );
  774. PrecacheScriptSound( "NPC_AttackHelicopter.Crash" );
  775. PrecacheScriptSound( "HelicopterBomb.HardImpact" );
  776. PrecacheScriptSound( "ReallyLoudSpark" );
  777. PrecacheScriptSound( "NPC_AttackHelicopterGrenade.Ping" );
  778. }
  779. int CNPC_AttackHelicopter::ObjectCaps()
  780. {
  781. int caps = BaseClass::ObjectCaps();
  782. if ( m_bAlwaysTransition )
  783. caps |= FCAP_NOTIFY_ON_TRANSITION;
  784. return caps;
  785. }
  786. void CNPC_AttackHelicopter::InputOutsideTransition( inputdata_t &inputdata )
  787. {
  788. CBaseEntity *pEnt = gEntList.FindEntityByName( NULL, m_iszTransitionTarget );
  789. if ( pEnt )
  790. {
  791. Vector teleportLocation = pEnt->GetAbsOrigin();
  792. QAngle teleportAngles = pEnt->GetAbsAngles();
  793. Teleport( &teleportLocation, &teleportAngles, &vec3_origin );
  794. Teleported();
  795. }
  796. else
  797. {
  798. DevMsg( 2, "NPC \"%s\" failed to find a suitable transition a point\n", STRING(GetEntityName()) );
  799. }
  800. }
  801. void CNPC_AttackHelicopter::InputSetOutsideTransitionTarget( inputdata_t &inputdata )
  802. {
  803. m_iszTransitionTarget = MAKE_STRING( inputdata.value.String() );
  804. }
  805. //-----------------------------------------------------------------------------
  806. // Create components
  807. //-----------------------------------------------------------------------------
  808. bool CNPC_AttackHelicopter::CreateComponents()
  809. {
  810. if ( !BaseClass::CreateComponents() )
  811. return false;
  812. m_Spotlight.Init( this, AI_SPOTLIGHT_NO_DLIGHTS, 45.0f, 500.0f );
  813. return true;
  814. }
  815. //-----------------------------------------------------------------------------
  816. // Purpose :
  817. //-----------------------------------------------------------------------------
  818. void CNPC_AttackHelicopter::Spawn( void )
  819. {
  820. Precache( );
  821. m_bIndestructible = false;
  822. m_bDeadlyShooting = false;
  823. m_bBombingSuppressed = false;
  824. m_bIgnorePathVisibilityTests = false;
  825. if ( !HasSpawnFlags(SF_HELICOPTER_ELECTRICAL_DRONE) )
  826. {
  827. SetModel( CHOPPER_MODEL_NAME );
  828. }
  829. else
  830. {
  831. SetModel( CHOPPER_DRONE_NAME );
  832. }
  833. ExtractBbox( SelectHeaviestSequence( ACT_IDLE ), m_cullBoxMins, m_cullBoxMaxs );
  834. GetEnemies()->SetFreeKnowledgeDuration( DEFAULT_FREE_KNOWLEDGE_DURATION );
  835. float flLoadedSpeed = m_flMaxSpeed;
  836. BaseClass::Spawn();
  837. float flChaseDist = HasSpawnFlags( SF_HELICOPTER_AGGRESSIVE ) ?
  838. CHOPPER_MIN_AGGRESSIVE_CHASE_DIST_DIFF : CHOPPER_MIN_CHASE_DIST_DIFF;
  839. InitPathingData( CHOPPER_ARRIVE_DIST, flChaseDist, CHOPPER_AVOID_DIST );
  840. SetFarthestPathDist( GetMaxFiringDistance() );
  841. m_takedamage = DAMAGE_YES;
  842. m_nGunState = GUN_STATE_IDLE;
  843. SetHullType( HULL_LARGE_CENTERED );
  844. SetHullSizeNormal();
  845. #ifdef HL2_EPISODIC
  846. CreateVPhysics();
  847. #endif // HL2_EPISODIC
  848. SetPauseState( PAUSE_NO_PAUSE );
  849. m_iMaxHealth = m_iHealth = sk_helicopter_health.GetInt();
  850. m_flMaxSpeed = flLoadedSpeed;
  851. if ( m_flMaxSpeed <= 0 )
  852. {
  853. m_flMaxSpeed = CHOPPER_MAX_SPEED;
  854. }
  855. m_flNextMegaBombHealth = m_iMaxHealth - m_iMaxHealth * g_helicopter_bullrush_mega_bomb_health.GetFloat();
  856. m_nGrenadeCount = CHOPPER_BOMB_DROP_COUNT;
  857. m_flFieldOfView = -1.0; // 360 degrees
  858. m_flIdleTimeDelay = 0.0f;
  859. m_iAmmoType = GetAmmoDef()->Index("HelicopterGun");
  860. InitBoneControllers();
  861. m_fHelicopterFlags = BITS_HELICOPTER_GUN_ON;
  862. m_bSuppressSound = false;
  863. m_flAcrossTime = -1.0f;
  864. m_flPathOffset = 0.0f;
  865. m_flCurrPathOffset = 0.0f;
  866. m_nAttackMode = ATTACK_MODE_DEFAULT;
  867. m_flInputDropBombTime = gpGlobals->curtime;
  868. SetActivity( ACT_IDLE );
  869. int nBombAttachment = LookupAttachment("bomb");
  870. m_hSensor = static_cast<CBombDropSensor*>(CreateEntityByName( "npc_helicoptersensor" ));
  871. m_hSensor->Spawn();
  872. m_hSensor->SetParent( this, nBombAttachment );
  873. m_hSensor->SetLocalOrigin( vec3_origin );
  874. m_hSensor->SetLocalAngles( vec3_angle );
  875. m_hSensor->SetOwnerEntity( this );
  876. AddFlag( FL_AIMTARGET );
  877. m_hCrashPoint.Set( NULL );
  878. }
  879. #ifdef HL2_EPISODIC
  880. //-----------------------------------------------------------------------------
  881. // Purpose:
  882. //-----------------------------------------------------------------------------
  883. bool CNPC_AttackHelicopter::CreateVPhysics( void )
  884. {
  885. InitBoneFollowers();
  886. return BaseClass::CreateVPhysics();
  887. }
  888. #endif // HL2_EPISODIC
  889. //------------------------------------------------------------------------------
  890. // Startup the chopper
  891. //------------------------------------------------------------------------------
  892. void CNPC_AttackHelicopter::Startup()
  893. {
  894. BaseClass::Startup();
  895. if ( HasSpawnFlags( SF_HELICOPTER_LIGHTS ) )
  896. {
  897. for ( int i = 0; i < MAX_HELICOPTER_LIGHTS; ++i )
  898. {
  899. // See if there's an attachment for this smoke trail
  900. char buf[32];
  901. Q_snprintf( buf, 32, "Light_Red%d", i );
  902. int nAttachment = LookupAttachment( buf );
  903. if ( nAttachment == 0 )
  904. {
  905. m_hLights[i] = NULL;
  906. continue;
  907. }
  908. m_hLights[i] = CSprite::SpriteCreate( CHOPPER_RED_LIGHT_SPRITE, vec3_origin, false );
  909. if ( !m_hLights[i] )
  910. continue;
  911. m_hLights[i]->SetParent( this, nAttachment );
  912. m_hLights[i]->SetLocalOrigin( vec3_origin );
  913. m_hLights[i]->SetLocalVelocity( vec3_origin );
  914. m_hLights[i]->SetMoveType( MOVETYPE_NONE );
  915. m_hLights[i]->SetTransparency( kRenderTransAdd, 255, 255, 255, 200, kRenderFxNone );
  916. m_hLights[i]->SetScale( 1.0f );
  917. m_hLights[i]->TurnOn();
  918. }
  919. SetContextThink( &CNPC_AttackHelicopter::BlinkLightsThink, gpGlobals->curtime + CHOPPER_LIGHT_BLINK_TIME_SHORT, s_pBlinkLightThinkContext );
  920. }
  921. }
  922. //------------------------------------------------------------------------------
  923. // Startup the chopper
  924. //------------------------------------------------------------------------------
  925. void CNPC_AttackHelicopter::BlinkLightsThink()
  926. {
  927. bool bIsOn = false;
  928. for ( int i = 0; i < MAX_HELICOPTER_LIGHTS; ++i )
  929. {
  930. if ( !m_hLights[i] )
  931. continue;
  932. if ( m_hLights[i]->GetScale() > 0.1f )
  933. {
  934. m_hLights[i]->SetScale( 0.1f, CHOPPER_LIGHT_BLINK_TIME_SHORT );
  935. }
  936. else
  937. {
  938. m_hLights[i]->SetScale( 0.5f, 0.0f );
  939. bIsOn = true;
  940. }
  941. }
  942. float flTime;
  943. if ( bIsOn )
  944. {
  945. flTime = CHOPPER_LIGHT_BLINK_TIME_SHORT;
  946. }
  947. else
  948. {
  949. flTime = m_bShortBlink ? CHOPPER_LIGHT_BLINK_TIME_SHORT : CHOPPER_LIGHT_BLINK_TIME;
  950. m_bShortBlink = !m_bShortBlink;
  951. }
  952. SetContextThink( &CNPC_AttackHelicopter::BlinkLightsThink, gpGlobals->curtime + flTime, s_pBlinkLightThinkContext );
  953. }
  954. //------------------------------------------------------------------------------
  955. // Start up spotlights
  956. //------------------------------------------------------------------------------
  957. void CNPC_AttackHelicopter::SpotlightStartup()
  958. {
  959. if ( !HasSpawnFlags( SF_HELICOPTER_LIGHTS ) )
  960. return;
  961. Vector vecForward;
  962. Vector vecOrigin;
  963. GetAttachment( m_nSpotlightAttachment, vecOrigin, &vecForward );
  964. m_Spotlight.SpotlightCreate( m_nSpotlightAttachment, vecForward );
  965. SpotlightThink();
  966. }
  967. //------------------------------------------------------------------------------
  968. // Shutdown spotlights
  969. //------------------------------------------------------------------------------
  970. void CNPC_AttackHelicopter::SpotlightShutdown()
  971. {
  972. m_Spotlight.SpotlightDestroy();
  973. SetContextThink( NULL, gpGlobals->curtime, s_pSpotlightThinkContext );
  974. }
  975. //------------------------------------------------------------------------------
  976. // Spotlights
  977. //------------------------------------------------------------------------------
  978. void CNPC_AttackHelicopter::SpotlightThink()
  979. {
  980. // NOTE: This function should deal with all deactivation cases
  981. if ( m_lifeState != LIFE_ALIVE )
  982. {
  983. SpotlightShutdown();
  984. return;
  985. }
  986. switch( m_nAttackMode )
  987. {
  988. case ATTACK_MODE_BULLRUSH_VEHICLE:
  989. {
  990. switch ( m_nSecondaryMode )
  991. {
  992. case BULLRUSH_MODE_SHOOT_GUN:
  993. {
  994. Vector vecForward;
  995. Vector vecOrigin;
  996. GetAttachment( m_nSpotlightAttachment, vecOrigin, &vecForward );
  997. m_Spotlight.SetSpotlightTargetDirection( vecForward );
  998. }
  999. break;
  1000. case BULLRUSH_MODE_SHOOT_IDLE_PLAYER:
  1001. if ( GetEnemy() )
  1002. {
  1003. m_Spotlight.SetSpotlightTargetPos( GetEnemy()->WorldSpaceCenter() );
  1004. }
  1005. break;
  1006. default:
  1007. SpotlightShutdown();
  1008. return;
  1009. }
  1010. }
  1011. break;
  1012. default:
  1013. SpotlightShutdown();
  1014. return;
  1015. }
  1016. m_Spotlight.Update();
  1017. SetContextThink( &CNPC_AttackHelicopter::SpotlightThink, gpGlobals->curtime + TICK_INTERVAL, s_pSpotlightThinkContext );
  1018. }
  1019. //-----------------------------------------------------------------------------
  1020. // Purpose: Always transition along with the player
  1021. //-----------------------------------------------------------------------------
  1022. void CNPC_AttackHelicopter::InputEnableAlwaysTransition( inputdata_t &inputdata )
  1023. {
  1024. m_bAlwaysTransition = true;
  1025. }
  1026. //-----------------------------------------------------------------------------
  1027. // Purpose: Stop always transitioning along with the player
  1028. //-----------------------------------------------------------------------------
  1029. void CNPC_AttackHelicopter::InputDisableAlwaysTransition( inputdata_t &inputdata )
  1030. {
  1031. m_bAlwaysTransition = false;
  1032. }
  1033. //------------------------------------------------------------------------------
  1034. // On Remove
  1035. //------------------------------------------------------------------------------
  1036. void CNPC_AttackHelicopter::UpdateOnRemove()
  1037. {
  1038. BaseClass::UpdateOnRemove();
  1039. StopLoopingSounds();
  1040. UTIL_Remove(m_hSensor);
  1041. DestroySmokeTrails();
  1042. for ( int i = 0; i < MAX_HELICOPTER_LIGHTS; ++i )
  1043. {
  1044. if ( m_hLights[i] )
  1045. {
  1046. UTIL_Remove( m_hLights[i] );
  1047. m_hLights[i] = NULL;
  1048. }
  1049. }
  1050. #ifdef HL2_EPISODIC
  1051. m_BoneFollowerManager.DestroyBoneFollowers();
  1052. #endif // HL2_EPISODIC
  1053. }
  1054. //------------------------------------------------------------------------------
  1055. // Purpose :
  1056. //------------------------------------------------------------------------------
  1057. void CNPC_AttackHelicopter::Activate( void )
  1058. {
  1059. BaseClass::Activate();
  1060. m_nGunBaseAttachment = LookupAttachment("gun");
  1061. m_nGunTipAttachment = LookupAttachment("muzzle");
  1062. m_nBombAttachment = LookupAttachment("bomb");
  1063. m_nSpotlightAttachment = LookupAttachment("spotlight");
  1064. if ( HasSpawnFlags( SF_HELICOPTER_LONG_SHADOW ) )
  1065. {
  1066. SetShadowCastDistance( 2048 );
  1067. }
  1068. }
  1069. //-----------------------------------------------------------------------------
  1070. // Purpose:
  1071. //-----------------------------------------------------------------------------
  1072. const char *CNPC_AttackHelicopter::GetTracerType( void )
  1073. {
  1074. return "HelicopterTracer";
  1075. }
  1076. //-----------------------------------------------------------------------------
  1077. // Allows the shooter to change the impact effect of his bullets
  1078. //-----------------------------------------------------------------------------
  1079. void CNPC_AttackHelicopter::DoImpactEffect( trace_t &tr, int nDamageType )
  1080. {
  1081. UTIL_ImpactTrace( &tr, nDamageType, "HelicopterImpact" );
  1082. }
  1083. //------------------------------------------------------------------------------
  1084. // Purpose : Create our rotor sound
  1085. //------------------------------------------------------------------------------
  1086. void CNPC_AttackHelicopter::InitializeRotorSound( void )
  1087. {
  1088. if ( !m_pRotorSound )
  1089. {
  1090. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  1091. CPASAttenuationFilter filter( this );
  1092. if ( HasSpawnFlags( SF_HELICOPTER_LOUD_ROTOR_SOUND ) )
  1093. {
  1094. m_pRotorSound = controller.SoundCreate( filter, entindex(), "NPC_AttackHelicopter.RotorsLoud" );
  1095. }
  1096. else
  1097. {
  1098. m_pRotorSound = controller.SoundCreate( filter, entindex(), "NPC_AttackHelicopter.Rotors" );
  1099. }
  1100. m_pRotorBlast = controller.SoundCreate( filter, entindex(), "NPC_AttackHelicopter.RotorBlast" );
  1101. m_pGunFiringSound = controller.SoundCreate( filter, entindex(), "NPC_AttackHelicopter.FireGun" );
  1102. controller.Play( m_pGunFiringSound, 0.0, 100 );
  1103. }
  1104. else
  1105. {
  1106. Assert(m_pRotorSound);
  1107. Assert(m_pRotorBlast);
  1108. Assert(m_pGunFiringSound);
  1109. }
  1110. BaseClass::InitializeRotorSound();
  1111. }
  1112. //------------------------------------------------------------------------------
  1113. // Gets the max speed of the helicopter
  1114. //------------------------------------------------------------------------------
  1115. float CNPC_AttackHelicopter::GetMaxSpeed()
  1116. {
  1117. if ( HasSpawnFlags(SF_HELICOPTER_ELECTRICAL_DRONE) )
  1118. return DRONE_SPEED;
  1119. if ( ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE ) && IsInSecondaryMode( BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED ) )
  1120. return CHOPPER_BULLRUSH_ENEMY_BOMB_SPEED;
  1121. if ( !GetEnemyVehicle() )
  1122. return BaseClass::GetMaxSpeed();
  1123. return 3000.0f;
  1124. }
  1125. float CNPC_AttackHelicopter::GetMaxSpeedFiring()
  1126. {
  1127. if ( HasSpawnFlags(SF_HELICOPTER_ELECTRICAL_DRONE) )
  1128. return DRONE_SPEED;
  1129. if ( ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE ) && IsInSecondaryMode( BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED ) )
  1130. return CHOPPER_BULLRUSH_ENEMY_BOMB_SPEED;
  1131. if ( !GetEnemyVehicle() )
  1132. return BaseClass::GetMaxSpeedFiring();
  1133. return 3000.0f;
  1134. }
  1135. //------------------------------------------------------------------------------
  1136. // Returns the max firing distance
  1137. //------------------------------------------------------------------------------
  1138. float CNPC_AttackHelicopter::GetMaxFiringDistance()
  1139. {
  1140. if ( !GetEnemyVehicle() )
  1141. return CHOPPER_GUN_MAX_FIRING_DIST;
  1142. return 8000.0f;
  1143. }
  1144. //------------------------------------------------------------------------------
  1145. // Updates the enemy
  1146. //------------------------------------------------------------------------------
  1147. float CNPC_AttackHelicopter::EnemySearchDistance( )
  1148. {
  1149. return 6000.0f;
  1150. }
  1151. //------------------------------------------------------------------------------
  1152. // Leading behaviors
  1153. //------------------------------------------------------------------------------
  1154. void CNPC_AttackHelicopter::InputStartBombingVehicle( inputdata_t &inputdata )
  1155. {
  1156. m_nAttackMode = ATTACK_MODE_BOMB_VEHICLE;
  1157. SetLeadingDistance( 1500.0f );
  1158. }
  1159. void CNPC_AttackHelicopter::InputStartTrailingVehicle( inputdata_t &inputdata )
  1160. {
  1161. m_nAttackMode = ATTACK_MODE_TRAIL_VEHICLE;
  1162. SetLeadingDistance( -1500.0f );
  1163. }
  1164. void CNPC_AttackHelicopter::InputStartDefaultBehavior( inputdata_t &inputdata )
  1165. {
  1166. m_nAttackMode = ATTACK_MODE_DEFAULT;
  1167. }
  1168. void CNPC_AttackHelicopter::InputStartAlwaysLeadingVehicle( inputdata_t &inputdata )
  1169. {
  1170. m_nAttackMode = ATTACK_MODE_ALWAYS_LEAD_VEHICLE;
  1171. SetLeadingDistance( 0.0f );
  1172. }
  1173. void CNPC_AttackHelicopter::InputStartBullrushBehavior( inputdata_t &inputdata )
  1174. {
  1175. if ( m_nAttackMode != ATTACK_MODE_BULLRUSH_VEHICLE )
  1176. {
  1177. m_nAttackMode = ATTACK_MODE_BULLRUSH_VEHICLE;
  1178. SetSecondaryMode( BULLRUSH_MODE_WAIT_FOR_ENEMY );
  1179. SetLeadingDistance( 0.0f );
  1180. }
  1181. }
  1182. //------------------------------------------------------------------------------
  1183. //------------------------------------------------------------------------------
  1184. void CNPC_AttackHelicopter::InputStartCarpetBombing( inputdata_t &inputdata )
  1185. {
  1186. m_bIsCarpetBombing = true;
  1187. }
  1188. //------------------------------------------------------------------------------
  1189. //------------------------------------------------------------------------------
  1190. void CNPC_AttackHelicopter::InputStopCarpetBombing( inputdata_t &inputdata )
  1191. {
  1192. m_bIsCarpetBombing = false;
  1193. }
  1194. //------------------------------------------------------------------------------
  1195. // Become indestructible
  1196. //------------------------------------------------------------------------------
  1197. void CNPC_AttackHelicopter::InputBecomeIndestructible( inputdata_t &inputdata )
  1198. {
  1199. m_bIndestructible = true;
  1200. }
  1201. //------------------------------------------------------------------------------
  1202. // Deadly shooting, tex!
  1203. //------------------------------------------------------------------------------
  1204. void CNPC_AttackHelicopter::InputEnableDeadlyShooting( inputdata_t &inputdata )
  1205. {
  1206. m_bDeadlyShooting = true;
  1207. }
  1208. void CNPC_AttackHelicopter::InputDisableDeadlyShooting( inputdata_t &inputdata )
  1209. {
  1210. m_bDeadlyShooting = false;
  1211. }
  1212. void CNPC_AttackHelicopter::InputStartNormalShooting( inputdata_t &inputdata )
  1213. {
  1214. m_nShootingMode = SHOOT_MODE_DEFAULT;
  1215. }
  1216. void CNPC_AttackHelicopter::InputStartLongCycleShooting( inputdata_t &inputdata )
  1217. {
  1218. m_nShootingMode = SHOOT_MODE_LONG_CYCLE;
  1219. }
  1220. void CNPC_AttackHelicopter::InputStartContinuousShooting( inputdata_t &inputdata )
  1221. {
  1222. m_nShootingMode = SHOOT_MODE_CONTINUOUS;
  1223. }
  1224. void CNPC_AttackHelicopter::InputStartFastShooting( inputdata_t &inputdata )
  1225. {
  1226. m_nShootingMode = SHOOT_MODE_FAST;
  1227. }
  1228. //------------------------------------------------------------------------------
  1229. // Deadly shooting, tex!
  1230. //------------------------------------------------------------------------------
  1231. bool CNPC_AttackHelicopter::IsDeadlyShooting()
  1232. {
  1233. if ( m_bDeadlyShooting )
  1234. return true;
  1235. if (( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE ) && IsInSecondaryMode( BULLRUSH_MODE_SHOOT_IDLE_PLAYER ) )
  1236. {
  1237. return (!GetEnemyVehicle()) && GetEnemy() && GetEnemy()->IsPlayer();
  1238. }
  1239. return false;
  1240. }
  1241. int CNPC_AttackHelicopter::GetShootingMode( )
  1242. {
  1243. if ( IsDeadlyShooting() )
  1244. return SHOOT_MODE_LONG_CYCLE;
  1245. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  1246. return SHOOT_MODE_CONTINUOUS;
  1247. return m_nShootingMode;
  1248. }
  1249. //-----------------------------------------------------------------------------
  1250. // Bombing suppression
  1251. //-----------------------------------------------------------------------------
  1252. void CNPC_AttackHelicopter::InputEnableBombing( inputdata_t &inputdata )
  1253. {
  1254. m_bBombingSuppressed = false;
  1255. }
  1256. void CNPC_AttackHelicopter::InputDisableBombing( inputdata_t &inputdata )
  1257. {
  1258. m_bBombingSuppressed = true;
  1259. }
  1260. //-----------------------------------------------------------------------------
  1261. // Visibility tests
  1262. //-----------------------------------------------------------------------------
  1263. void CNPC_AttackHelicopter::InputDisablePathVisibilityTests( inputdata_t &inputdata )
  1264. {
  1265. m_bIgnorePathVisibilityTests = true;
  1266. GetEnemies()->SetUnforgettable( GetEnemy(), true );
  1267. }
  1268. void CNPC_AttackHelicopter::InputEnablePathVisibilityTests( inputdata_t &inputdata )
  1269. {
  1270. m_bIgnorePathVisibilityTests = false;
  1271. GetEnemies()->SetUnforgettable( GetEnemy(), false );
  1272. }
  1273. //-----------------------------------------------------------------------------
  1274. //-----------------------------------------------------------------------------
  1275. void CNPC_AttackHelicopter::InputSelfDestruct( inputdata_t &inputdata )
  1276. {
  1277. m_lifeState = LIFE_ALIVE; // Force to die properly.
  1278. CTakeDamageInfo info( this, this, Vector(0, 0, 1), WorldSpaceCenter(), GetMaxHealth(), CLASS_MISSILE );
  1279. TakeDamage( info );
  1280. }
  1281. //-----------------------------------------------------------------------------
  1282. // For scripted times where it *has* to shoot
  1283. //-----------------------------------------------------------------------------
  1284. void CNPC_AttackHelicopter::InputSetHealthFraction( inputdata_t &inputdata )
  1285. {
  1286. // Sets the health fraction, no damage effects
  1287. if ( inputdata.value.Float() > 0 )
  1288. {
  1289. SetHealth( GetMaxHealth() * inputdata.value.Float() * 0.01f );
  1290. }
  1291. }
  1292. //-----------------------------------------------------------------------------
  1293. // Purpose:
  1294. // Input : &inputdata -
  1295. //-----------------------------------------------------------------------------
  1296. void CNPC_AttackHelicopter::InputStartBombExplodeOnContact( inputdata_t &inputdata )
  1297. {
  1298. m_bBombsExplodeOnContact = true;
  1299. }
  1300. //-----------------------------------------------------------------------------
  1301. // Purpose:
  1302. // Input : &inputdata -
  1303. //-----------------------------------------------------------------------------
  1304. void CNPC_AttackHelicopter::InputStopBombExplodeOnContact( inputdata_t &inputdata )
  1305. {
  1306. m_bBombsExplodeOnContact = false;
  1307. }
  1308. //------------------------------------------------------------------------------
  1309. // For scripted times where it *has* to shoot
  1310. //------------------------------------------------------------------------------
  1311. void CNPC_AttackHelicopter::InputResetIdleTime( inputdata_t &inputdata )
  1312. {
  1313. if ( m_nGunState == GUN_STATE_IDLE )
  1314. {
  1315. m_flNextAttack = gpGlobals->curtime;
  1316. }
  1317. }
  1318. //-----------------------------------------------------------------------------
  1319. // This trace filter ignores all breakables + physics props
  1320. //-----------------------------------------------------------------------------
  1321. class CTraceFilterChopper : public CTraceFilterSimple
  1322. {
  1323. DECLARE_CLASS( CTraceFilterChopper, CTraceFilterSimple );
  1324. public:
  1325. CTraceFilterChopper( const IHandleEntity *passentity, int collisionGroup );
  1326. virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask );
  1327. private:
  1328. const IHandleEntity *m_pPassEnt;
  1329. int m_collisionGroup;
  1330. };
  1331. CTraceFilterChopper::CTraceFilterChopper( const IHandleEntity *passentity, int collisionGroup ) :
  1332. CTraceFilterSimple( passentity, collisionGroup )
  1333. {
  1334. }
  1335. bool CTraceFilterChopper::ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
  1336. {
  1337. CBaseEntity *pEnt = static_cast<IServerUnknown*>(pServerEntity)->GetBaseEntity();
  1338. if ( pEnt )
  1339. {
  1340. if ( FClassnameIs( pEnt, "func_breakable" ) ||
  1341. FClassnameIs( pEnt, "func_physbox" ) ||
  1342. FClassnameIs( pEnt, "prop_physics" ) ||
  1343. FClassnameIs( pEnt, "physics_prop" ) )
  1344. {
  1345. return false;
  1346. }
  1347. }
  1348. return BaseClass::ShouldHitEntity( pServerEntity, contentsMask );
  1349. }
  1350. //-----------------------------------------------------------------------------
  1351. // Enemy visibility check
  1352. //-----------------------------------------------------------------------------
  1353. CBaseEntity *CNPC_AttackHelicopter::FindTrackBlocker( const Vector &vecViewPoint, const Vector &vecTargetPos )
  1354. {
  1355. if ( m_bIgnorePathVisibilityTests )
  1356. return NULL;
  1357. CTraceFilterChopper chopperFilter( this, COLLISION_GROUP_NONE );
  1358. trace_t tr;
  1359. AI_TraceHull( vecViewPoint, vecTargetPos, -Vector(4,4,4), Vector(4,4,4), MASK_SHOT, &chopperFilter, &tr );
  1360. if ( tr.fraction != 1.0f )
  1361. {
  1362. Assert( tr.m_pEnt );
  1363. }
  1364. return (tr.fraction != 1.0f) ? tr.m_pEnt : NULL;
  1365. }
  1366. //-----------------------------------------------------------------------------
  1367. // More Enemy visibility check
  1368. //-----------------------------------------------------------------------------
  1369. bool CNPC_AttackHelicopter::FVisible( CBaseEntity *pEntity, int traceMask, CBaseEntity **ppBlocker )
  1370. {
  1371. if ( pEntity->GetFlags() & FL_NOTARGET )
  1372. return false;
  1373. #if 0
  1374. // FIXME: only block LOS through opaque water
  1375. // don't look through water
  1376. if ((m_nWaterLevel != 3 && pEntity->m_nWaterLevel == 3)
  1377. || (m_nWaterLevel == 3 && pEntity->m_nWaterLevel == 0))
  1378. return false;
  1379. #endif
  1380. Vector vecLookerOrigin = EyePosition();//look through the caller's 'eyes'
  1381. Vector vecTargetOrigin = pEntity->EyePosition();
  1382. CTraceFilterChopper chopperFilter( this, COLLISION_GROUP_NONE );
  1383. trace_t tr;
  1384. UTIL_TraceLine(vecLookerOrigin, vecTargetOrigin, traceMask, &chopperFilter, &tr);
  1385. if (tr.fraction != 1.0)
  1386. {
  1387. // Got line of sight!
  1388. if ( tr.m_pEnt == pEntity )
  1389. return true;
  1390. // Got line of sight on the vehicle the player is driving!
  1391. if ( pEntity && pEntity->IsPlayer() )
  1392. {
  1393. CBasePlayer *pPlayer = assert_cast<CBasePlayer*>( pEntity );
  1394. if ( tr.m_pEnt == pPlayer->GetVehicleEntity() )
  1395. return true;
  1396. }
  1397. if (ppBlocker)
  1398. {
  1399. *ppBlocker = tr.m_pEnt;
  1400. }
  1401. return false;// Line of sight is not established
  1402. }
  1403. return true;// line of sight is valid.
  1404. }
  1405. //------------------------------------------------------------------------------
  1406. // Shot spread
  1407. //------------------------------------------------------------------------------
  1408. #define PLAYER_TIGHTEN_FACTOR 0.75f
  1409. Vector CNPC_AttackHelicopter::GetAttackSpread( CBaseCombatWeapon *pWeapon, CBaseEntity *pTarget )
  1410. {
  1411. float flSinConeDegrees = sin( sk_helicopter_firingcone.GetFloat() * PLAYER_TIGHTEN_FACTOR * 0.5f * (3.14f / 180.0f) );
  1412. Vector vecSpread( flSinConeDegrees, flSinConeDegrees, flSinConeDegrees );
  1413. return vecSpread;
  1414. }
  1415. //------------------------------------------------------------------------------
  1416. // Find interesting nearby things to shoot
  1417. //------------------------------------------------------------------------------
  1418. int CNPC_AttackHelicopter::BuildMissTargetList( int nCount, CBaseEntity **ppMissCandidates )
  1419. {
  1420. int numMissCandidates = 0;
  1421. CBaseEntity *pEnts[256];
  1422. Vector radius( 150, 150, 150 );
  1423. const Vector &vecSource = GetEnemy()->WorldSpaceCenter();
  1424. int numEnts = UTIL_EntitiesInBox( pEnts, 256, vecSource - radius, vecSource+radius, 0 );
  1425. for ( int i = 0; i < numEnts; i++ )
  1426. {
  1427. if ( pEnts[i] == NULL )
  1428. continue;
  1429. if ( numMissCandidates >= nCount )
  1430. break;
  1431. // Miss candidates cannot include the player or his vehicle
  1432. if ( pEnts[i] == GetEnemyVehicle() || pEnts[i] == GetEnemy() )
  1433. continue;
  1434. // See if it's a good target candidate
  1435. if ( FClassnameIs( pEnts[i], "prop_dynamic" ) ||
  1436. FClassnameIs( pEnts[i], "prop_physics" ) ||
  1437. FClassnameIs( pEnts[i], "physics_prop" ) )
  1438. {
  1439. ppMissCandidates[numMissCandidates++] = pEnts[i];
  1440. }
  1441. }
  1442. return numMissCandidates;
  1443. }
  1444. //------------------------------------------------------------------------------
  1445. // Gets a vehicle the enemy is in (if any)
  1446. //------------------------------------------------------------------------------
  1447. CBaseEntity *CNPC_AttackHelicopter::GetEnemyVehicle()
  1448. {
  1449. if ( !GetEnemy() )
  1450. return NULL;
  1451. if ( !GetEnemy()->IsPlayer() )
  1452. return NULL;
  1453. return static_cast<CBasePlayer*>(GetEnemy())->GetVehicleEntity();
  1454. }
  1455. //------------------------------------------------------------------------------
  1456. // Purpose :
  1457. //------------------------------------------------------------------------------
  1458. void CNPC_AttackHelicopter::ShootAtPlayer( const Vector &vBasePos, const Vector &vGunDir )
  1459. {
  1460. // Fire one shots per round right at the player, using usual rules
  1461. FireBulletsInfo_t info;
  1462. info.m_vecSrc = vBasePos;
  1463. info.m_vecSpread = VECTOR_CONE_PRECALCULATED;
  1464. info.m_flDistance = MAX_COORD_RANGE;
  1465. info.m_iAmmoType = m_iAmmoType;
  1466. info.m_iTracerFreq = 1;
  1467. info.m_vecDirShooting = GetActualShootTrajectory( vBasePos );
  1468. info.m_nFlags = FIRE_BULLETS_TEMPORARY_DANGER_SOUND;
  1469. DoMuzzleFlash();
  1470. QAngle vGunAng;
  1471. VectorAngles( vGunDir, vGunAng );
  1472. FireBullets( info );
  1473. // Fire the rest of the bullets at objects around the player
  1474. CBaseEntity *ppNearbyTargets[16];
  1475. int nActualTargets = BuildMissTargetList( 16, ppNearbyTargets );
  1476. // Randomly sort it...
  1477. int i;
  1478. for ( i = 0; i < nActualTargets; ++i )
  1479. {
  1480. int nSwap = random->RandomInt( 0, nActualTargets - 1 );
  1481. V_swap( ppNearbyTargets[i], ppNearbyTargets[nSwap] );
  1482. }
  1483. // Just shoot where we're facing
  1484. float flSinConeDegrees = sin( sk_helicopter_firingcone.GetFloat() * 0.5f * (3.14f / 180.0f) );
  1485. Vector vecSpread( flSinConeDegrees, flSinConeDegrees, flSinConeDegrees );
  1486. // How many times should we hit the player this time?
  1487. int nDesiredHitCount = (int)(((float)( m_nMaxBurstHits - m_nBurstHits ) / (float)m_nRemainingBursts) + 0.5f);
  1488. int nNearbyTargetCount = 0;
  1489. int nPlayerShotCount = 0;
  1490. for ( i = sk_helicopter_roundsperburst.GetInt() - 1; --i >= 0; )
  1491. {
  1492. // Find something interesting around the enemy to shoot instead of just missing.
  1493. if ( nActualTargets > nNearbyTargetCount )
  1494. {
  1495. // FIXME: Constrain to the firing cone?
  1496. ppNearbyTargets[nNearbyTargetCount]->CollisionProp()->RandomPointInBounds( Vector(.25, .25, .25), Vector(.75, .75, .75), &info.m_vecDirShooting );
  1497. info.m_vecDirShooting -= vBasePos;
  1498. VectorNormalize( info.m_vecDirShooting );
  1499. info.m_vecSpread = VECTOR_CONE_PRECALCULATED;
  1500. info.m_flDistance = MAX_COORD_RANGE;
  1501. info.m_nFlags = FIRE_BULLETS_TEMPORARY_DANGER_SOUND;
  1502. FireBullets( info );
  1503. ++nNearbyTargetCount;
  1504. continue;
  1505. }
  1506. if ( GetEnemy() && ( nPlayerShotCount < nDesiredHitCount ))
  1507. {
  1508. GetEnemy()->CollisionProp()->RandomPointInBounds( Vector(0, 0, 0), Vector(1, 1, 1), &info.m_vecDirShooting );
  1509. info.m_vecDirShooting -= vBasePos;
  1510. VectorNormalize( info.m_vecDirShooting );
  1511. info.m_vecSpread = VECTOR_CONE_PRECALCULATED;
  1512. info.m_flDistance = MAX_COORD_RANGE;
  1513. info.m_nFlags = FIRE_BULLETS_TEMPORARY_DANGER_SOUND;
  1514. FireBullets( info );
  1515. ++nPlayerShotCount;
  1516. continue;
  1517. }
  1518. // Nothing nearby; just fire randomly...
  1519. info.m_vecDirShooting = vGunDir;
  1520. info.m_vecSpread = vecSpread;
  1521. info.m_flDistance = 8192;
  1522. info.m_nFlags = FIRE_BULLETS_TEMPORARY_DANGER_SOUND;
  1523. FireBullets( info );
  1524. }
  1525. }
  1526. //-----------------------------------------------------------------------------
  1527. // Chooses a point within the circle of death to fire in
  1528. //-----------------------------------------------------------------------------
  1529. void CNPC_AttackHelicopter::PickDirectionToCircleOfDeath( const Vector &vBasePos, const Vector &vecFireAtPosition, Vector *pResult )
  1530. {
  1531. *pResult = vecFireAtPosition;
  1532. float x, y;
  1533. do
  1534. {
  1535. x = random->RandomFloat( -1.0f, 1.0f );
  1536. y = random->RandomFloat( -1.0f, 1.0f );
  1537. } while ( (x * x + y * y) > 1.0f );
  1538. pResult->x += x * m_flCircleOfDeathRadius;
  1539. pResult->y += y * m_flCircleOfDeathRadius;
  1540. *pResult -= vBasePos;
  1541. VectorNormalize( *pResult );
  1542. }
  1543. //-----------------------------------------------------------------------------
  1544. // Deliberately aims as close as possible w/o hitting
  1545. //-----------------------------------------------------------------------------
  1546. void CNPC_AttackHelicopter::AimCloseToTargetButMiss( CBaseEntity *pTarget, float flMinDist, float flMaxDist, const Vector &shootOrigin, Vector *pResult )
  1547. {
  1548. Vector vecDirection;
  1549. VectorSubtract( pTarget->WorldSpaceCenter(), shootOrigin, vecDirection );
  1550. float flDist = VectorNormalize( vecDirection );
  1551. float flRadius = pTarget->BoundingRadius() + random->RandomFloat( flMinDist, flMaxDist );
  1552. float flMinRadius = flRadius;
  1553. if ( flDist > flRadius )
  1554. {
  1555. flMinRadius = flDist * flRadius / sqrt( flDist * flDist - flRadius * flRadius );
  1556. }
  1557. // Choose random points in a plane perpendicular to the shoot origin.
  1558. Vector vecRandomDir;
  1559. vecRandomDir.Random( -1.0f, 1.0f );
  1560. VectorMA( vecRandomDir, -DotProduct( vecDirection, vecRandomDir ), vecDirection, vecRandomDir );
  1561. VectorNormalize( vecRandomDir );
  1562. vecRandomDir *= flMinRadius;
  1563. vecRandomDir += pTarget->WorldSpaceCenter();
  1564. VectorSubtract( vecRandomDir, shootOrigin, *pResult );
  1565. VectorNormalize( *pResult );
  1566. }
  1567. //-----------------------------------------------------------------------------
  1568. // Make sure we don't hit too many times
  1569. //-----------------------------------------------------------------------------
  1570. void CNPC_AttackHelicopter::FireBullets( const FireBulletsInfo_t &info )
  1571. {
  1572. // Use this to count the number of hits in a burst
  1573. bool bIsPlayer = GetEnemy() && GetEnemy()->IsPlayer();
  1574. if ( !bIsPlayer )
  1575. {
  1576. BaseClass::FireBullets( info );
  1577. return;
  1578. }
  1579. if ( !GetEnemyVehicle() && !IsDeadlyShooting() )
  1580. {
  1581. if ( m_nBurstHits >= m_nMaxBurstHits )
  1582. {
  1583. FireBulletsInfo_t actualInfo = info;
  1584. actualInfo.m_pAdditionalIgnoreEnt = GetEnemy();
  1585. BaseClass::FireBullets( actualInfo );
  1586. return;
  1587. }
  1588. }
  1589. CBasePlayer *pPlayer = assert_cast<CBasePlayer*>(GetEnemy());
  1590. int nPrevHealth = pPlayer->GetHealth();
  1591. int nPrevArmor = pPlayer->ArmorValue();
  1592. BaseClass::FireBullets( info );
  1593. if (( pPlayer->GetHealth() < nPrevHealth ) || ( pPlayer->ArmorValue() < nPrevArmor ))
  1594. {
  1595. ++m_nBurstHits;
  1596. }
  1597. }
  1598. //------------------------------------------------------------------------------
  1599. // Purpose :
  1600. //------------------------------------------------------------------------------
  1601. void CNPC_AttackHelicopter::ShootInsideCircleOfDeath( const Vector &vBasePos, const Vector &vecFireAtPosition )
  1602. {
  1603. Vector vecFireDirection;
  1604. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  1605. {
  1606. PickDirectionToCircleOfDeath( vBasePos, vecFireAtPosition, &vecFireDirection );
  1607. }
  1608. else if ( ( m_nNearShots < m_nMaxNearShots ) || !GetEnemyVehicle() )
  1609. {
  1610. if ( ( m_nBurstHits < m_nMaxBurstHits ) || !GetEnemy() )
  1611. {
  1612. ++m_nNearShots;
  1613. PickDirectionToCircleOfDeath( vBasePos, vecFireAtPosition, &vecFireDirection );
  1614. }
  1615. else
  1616. {
  1617. m_nNearShots += 6;
  1618. AimCloseToTargetButMiss( GetEnemy(), 20.0f, 50.0f, vBasePos, &vecFireDirection );
  1619. }
  1620. }
  1621. else
  1622. {
  1623. AimCloseToTargetButMiss( GetEnemyVehicle(), 10.0f, 80.0f, vBasePos, &vecFireDirection );
  1624. }
  1625. FireBulletsInfo_t info( 1, vBasePos, vecFireDirection, VECTOR_CONE_PRECALCULATED, MAX_COORD_RANGE, m_iAmmoType );
  1626. info.m_iTracerFreq = 1;
  1627. info.m_nFlags = FIRE_BULLETS_TEMPORARY_DANGER_SOUND;
  1628. FireBullets( info );
  1629. }
  1630. //-----------------------------------------------------------------------------
  1631. // Purpose:
  1632. //-----------------------------------------------------------------------------
  1633. void CNPC_AttackHelicopter::DoMuzzleFlash( void )
  1634. {
  1635. BaseClass::DoMuzzleFlash();
  1636. CEffectData data;
  1637. data.m_nAttachmentIndex = LookupAttachment( "muzzle" );
  1638. data.m_nEntIndex = entindex();
  1639. DispatchEffect( "ChopperMuzzleFlash", data );
  1640. }
  1641. //------------------------------------------------------------------------------
  1642. // Purpose :
  1643. //------------------------------------------------------------------------------
  1644. #define HIT_VEHICLE_SPEED_MIN 200.0f
  1645. #define HIT_VEHICLE_SPEED_MAX 500.0f
  1646. void CNPC_AttackHelicopter::ShootAtVehicle( const Vector &vBasePos, const Vector &vecFireAtPosition )
  1647. {
  1648. int nShotsRemaining = sk_helicopter_roundsperburst.GetInt();
  1649. DoMuzzleFlash();
  1650. // Do special code against episodic drivers
  1651. if ( hl2_episodic.GetBool() )
  1652. {
  1653. Vector vecVelocity;
  1654. GetEnemyVehicle()->GetVelocity( &vecVelocity, NULL );
  1655. float flSpeed = clamp( vecVelocity.Length(), 0.0f, 400.0f );
  1656. float flRange = RemapVal( flSpeed, 0.0f, 400.0f, 0.05f, 1.0f );
  1657. // Alter each shot's trajectory based on our speed
  1658. for ( int i = 0; i < nShotsRemaining; i++ )
  1659. {
  1660. Vector vecShotDir;
  1661. // If they're at a dead stand-still, just hit them
  1662. if ( flRange <= 0.1f )
  1663. {
  1664. VectorSubtract( GetEnemy()->EyePosition(), vBasePos, vecShotDir );
  1665. Vector vecOffset;
  1666. vecOffset.Random( -40.0f, 40.0f );
  1667. vecShotDir += vecOffset;
  1668. VectorNormalize( vecShotDir );
  1669. }
  1670. else
  1671. {
  1672. // Aim in a cone around them
  1673. AimCloseToTargetButMiss( GetEnemy(), (3*12) * flRange, (10*12) * flRange, vBasePos, &vecShotDir );
  1674. }
  1675. FireBulletsInfo_t info( 1, vBasePos, vecShotDir, VECTOR_CONE_PRECALCULATED, MAX_COORD_RANGE, m_iAmmoType );
  1676. info.m_iTracerFreq = 1;
  1677. FireBullets( info );
  1678. }
  1679. // We opt out of the rest of the function
  1680. // FIXME: Should we emulate the below functionality and have half the bullets attempt to miss admirably? -- jdw
  1681. return;
  1682. }
  1683. // Pop one at the player based on how fast he's going
  1684. if ( m_nBurstHits < m_nMaxBurstHits )
  1685. {
  1686. Vector vecDir;
  1687. VectorSubtract( GetEnemy()->EyePosition(), vBasePos, vecDir );
  1688. Vector vecOffset;
  1689. vecOffset.Random( -5.0f, 5.0f );
  1690. vecDir += vecOffset;
  1691. VectorNormalize( vecDir );
  1692. FireBulletsInfo_t info( 1, vBasePos, vecDir, VECTOR_CONE_PRECALCULATED, MAX_COORD_RANGE, m_iAmmoType );
  1693. info.m_iTracerFreq = 1;
  1694. FireBullets( info );
  1695. --nShotsRemaining;
  1696. }
  1697. // Fire half of the bullets within the circle of death, the other half at interesting things
  1698. int i;
  1699. int nFireInCircle = nShotsRemaining >> 1;
  1700. nShotsRemaining -= nFireInCircle;
  1701. for ( i = 0; i < nFireInCircle; ++i )
  1702. {
  1703. ShootInsideCircleOfDeath( vBasePos, vecFireAtPosition );
  1704. }
  1705. // Fire the rest of the bullets at objects around the enemy
  1706. CBaseEntity *ppNearbyTargets[16];
  1707. int nActualTargets = BuildMissTargetList( 16, ppNearbyTargets );
  1708. // Randomly sort it...
  1709. for ( i = 0; i < nActualTargets; ++i )
  1710. {
  1711. int nSwap = random->RandomInt( 0, nActualTargets - 1 );
  1712. V_swap( ppNearbyTargets[i], ppNearbyTargets[nSwap] );
  1713. }
  1714. // Just shoot where we're facing
  1715. float flSinConeDegrees = sin( sk_helicopter_firingcone.GetFloat() * 0.5f * (3.14f / 180.0f) );
  1716. Vector vecSpread( flSinConeDegrees, flSinConeDegrees, flSinConeDegrees );
  1717. for ( i = nShotsRemaining; --i >= 0; )
  1718. {
  1719. // Find something interesting around the enemy to shoot instead of just missing.
  1720. if ( nActualTargets > i )
  1721. {
  1722. Vector vecFireDirection;
  1723. ppNearbyTargets[i]->CollisionProp()->RandomPointInBounds( Vector(.25, .25, .25), Vector(.75, .75, .75), &vecFireDirection );
  1724. vecFireDirection -= vBasePos;
  1725. VectorNormalize( vecFireDirection );
  1726. // FIXME: Constrain to the firing cone?
  1727. // I put in all the default arguments simply so I could guarantee the first shot of one of the bursts always hits
  1728. FireBulletsInfo_t info( 1, vBasePos, vecFireDirection, VECTOR_CONE_PRECALCULATED, MAX_COORD_RANGE, m_iAmmoType );
  1729. info.m_iTracerFreq = 1;
  1730. FireBullets( info );
  1731. }
  1732. else
  1733. {
  1734. ShootInsideCircleOfDeath( vBasePos, vecFireAtPosition );
  1735. }
  1736. }
  1737. }
  1738. //------------------------------------------------------------------------------
  1739. // Various states of the helicopter firing...
  1740. //------------------------------------------------------------------------------
  1741. bool CNPC_AttackHelicopter::PoseGunTowardTargetDirection( const Vector &vTargetDir )
  1742. {
  1743. Vector vecOut;
  1744. VectorIRotate( vTargetDir, EntityToWorldTransform(), vecOut );
  1745. QAngle angles;
  1746. VectorAngles(vecOut, angles);
  1747. if (angles.y > 180)
  1748. {
  1749. angles.y = angles.y - 360;
  1750. }
  1751. else if (angles.y < -180)
  1752. {
  1753. angles.y = angles.y + 360;
  1754. }
  1755. if (angles.x > 180)
  1756. {
  1757. angles.x = angles.x - 360;
  1758. }
  1759. else if (angles.x < -180)
  1760. {
  1761. angles.x = angles.x + 360;
  1762. }
  1763. if ( ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE ) && !IsInSecondaryMode(BULLRUSH_MODE_SHOOT_IDLE_PLAYER) && GetEnemy())
  1764. {
  1765. if ( GetEnemyVehicle() )
  1766. {
  1767. angles.x = clamp( angles.x, -12.0f, 0.0f );
  1768. angles.y = clamp( angles.y, -10.0f, 10.0f );
  1769. }
  1770. else
  1771. {
  1772. angles.x = clamp( angles.x, -10.0f, 10.0f );
  1773. angles.y = clamp( angles.y, -10.0f, 10.0f );
  1774. }
  1775. }
  1776. if (angles.x > m_angGun.x)
  1777. {
  1778. m_angGun.x = MIN( angles.x, m_angGun.x + 12 );
  1779. }
  1780. if (angles.x < m_angGun.x)
  1781. {
  1782. m_angGun.x = MAX( angles.x, m_angGun.x - 12 );
  1783. }
  1784. if (angles.y > m_angGun.y)
  1785. {
  1786. m_angGun.y = MIN( angles.y, m_angGun.y + 12 );
  1787. }
  1788. if (angles.y < m_angGun.y)
  1789. {
  1790. m_angGun.y = MAX( angles.y, m_angGun.y - 12 );
  1791. }
  1792. SetPoseParameter( m_poseWeapon_Pitch, -m_angGun.x );
  1793. SetPoseParameter( m_poseWeapon_Yaw, m_angGun.y );
  1794. return true;
  1795. }
  1796. //------------------------------------------------------------------------------
  1797. // Compute the enemy position (non-vehicle case)
  1798. //------------------------------------------------------------------------------
  1799. void CNPC_AttackHelicopter::ComputeFireAtPosition( Vector *pVecActualTargetPosition )
  1800. {
  1801. // Deal with various leading behaviors...
  1802. *pVecActualTargetPosition = m_vecTargetPosition;
  1803. }
  1804. //------------------------------------------------------------------------------
  1805. // Compute the enemy position (non-vehicle case)
  1806. //------------------------------------------------------------------------------
  1807. void CNPC_AttackHelicopter::ComputeVehicleFireAtPosition( Vector *pVecActualTargetPosition )
  1808. {
  1809. CBaseEntity *pVehicle = GetEnemyVehicle();
  1810. // Make sure the circle of death doesn't move more than N units
  1811. // This will cause the target to have to maintain a large enough speed
  1812. *pVecActualTargetPosition = pVehicle->BodyTarget( GetAbsOrigin(), false );
  1813. // NDebugOverlay::Box( *pVecActualTargetPosition,
  1814. // Vector(-m_flCircleOfDeathRadius, -m_flCircleOfDeathRadius, 0),
  1815. // Vector(m_flCircleOfDeathRadius, m_flCircleOfDeathRadius, 0),
  1816. // 0, 0, 255, false, 0.1f );
  1817. }
  1818. //------------------------------------------------------------------------------
  1819. // Here's what we do when we're looking for a target
  1820. //------------------------------------------------------------------------------
  1821. bool CNPC_AttackHelicopter::DoGunIdle( const Vector &vGunDir, const Vector &vTargetDir )
  1822. {
  1823. // When bullrushing, skip the idle
  1824. if ( ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE ) &&
  1825. ( IsInSecondaryMode( BULLRUSH_MODE_SHOOT_GUN ) || IsInSecondaryMode(BULLRUSH_MODE_SHOOT_IDLE_PLAYER) ) )
  1826. {
  1827. EmitSound( "NPC_AttackHelicopter.ChargeGun" );
  1828. m_flChargeTime = gpGlobals->curtime + CHOPPER_GUN_CHARGE_TIME;
  1829. m_nGunState = GUN_STATE_CHARGING;
  1830. m_flCircleOfDeathRadius = CHOPPER_MAX_CIRCLE_OF_DEATH_RADIUS;
  1831. return true;
  1832. }
  1833. // Can't continually fire....
  1834. if (m_flNextAttack > gpGlobals->curtime)
  1835. return false;
  1836. // Don't fire if we're too far away, or if the enemy isn't in front of us
  1837. if (!GetEnemy())
  1838. return false;
  1839. float flMaxDistSqr = GetMaxFiringDistance();
  1840. flMaxDistSqr *= flMaxDistSqr;
  1841. float flDistSqr = WorldSpaceCenter().DistToSqr( GetEnemy()->WorldSpaceCenter() );
  1842. if (flDistSqr > flMaxDistSqr)
  1843. return false;
  1844. // If he's mostly within the cone, shoot away!
  1845. float flChargeCone = sk_helicopter_firingcone.GetFloat() * 0.5f;
  1846. if ( flChargeCone < 15.0f )
  1847. {
  1848. flChargeCone = 15.0f;
  1849. }
  1850. float flCosConeDegrees = cos( flChargeCone * (3.14f / 180.0f) );
  1851. float fDotPr = DotProduct( vGunDir, vTargetDir );
  1852. if (fDotPr < flCosConeDegrees)
  1853. return false;
  1854. // Fast shooting doesn't charge up
  1855. if( m_nShootingMode == SHOOT_MODE_FAST )
  1856. {
  1857. m_flChargeTime = gpGlobals->curtime;
  1858. m_nGunState = GUN_STATE_CHARGING;
  1859. m_flAvoidMetric = 0.0f;
  1860. m_vecLastAngVelocity.Init( 0, 0, 0 );
  1861. }
  1862. else
  1863. {
  1864. EmitSound( "NPC_AttackHelicopter.ChargeGun" );
  1865. float flChargeTime = CHOPPER_GUN_CHARGE_TIME;
  1866. float flVariance = flChargeTime * 0.1f;
  1867. m_flChargeTime = gpGlobals->curtime + random->RandomFloat(flChargeTime - flVariance, flChargeTime + flVariance);
  1868. m_nGunState = GUN_STATE_CHARGING;
  1869. m_flAvoidMetric = 0.0f;
  1870. m_vecLastAngVelocity.Init( 0, 0, 0 );
  1871. }
  1872. return true;
  1873. }
  1874. //------------------------------------------------------------------------------
  1875. // How easy is the target to hit?
  1876. //------------------------------------------------------------------------------
  1877. void CNPC_AttackHelicopter::UpdateTargetHittability()
  1878. {
  1879. // This simply is a measure of how much juking is going on.
  1880. // Along with how much steering is happening.
  1881. if ( GetEnemyVehicle() )
  1882. {
  1883. Vector vecVelocity;
  1884. AngularImpulse vecAngVelocity;
  1885. GetEnemyVehicle()->GetVelocity( &vecVelocity, &vecAngVelocity );
  1886. float flDist = fabs( vecAngVelocity.z - m_vecLastAngVelocity.z );
  1887. m_flAvoidMetric += flDist;
  1888. m_vecLastAngVelocity = vecAngVelocity;
  1889. }
  1890. }
  1891. //------------------------------------------------------------------------------
  1892. // Here's what we do when we're getting ready to fire
  1893. //------------------------------------------------------------------------------
  1894. bool CNPC_AttackHelicopter::DoGunCharging( )
  1895. {
  1896. // Update the target hittability, which will indicate how many hits we'll accept.
  1897. UpdateTargetHittability();
  1898. if ( m_flChargeTime > gpGlobals->curtime )
  1899. return false;
  1900. m_nGunState = GUN_STATE_FIRING;
  1901. if ( HasSpawnFlags( SF_HELICOPTER_AGGRESSIVE ) )
  1902. {
  1903. SetPauseState( PAUSE_AT_NEXT_LOS_POSITION );
  1904. }
  1905. int nHitFactor = 1;
  1906. switch( GetShootingMode() )
  1907. {
  1908. case SHOOT_MODE_DEFAULT:
  1909. case SHOOT_MODE_FAST:
  1910. {
  1911. int nBurstCount = sk_helicopter_burstcount.GetInt();
  1912. m_nRemainingBursts = random->RandomInt( nBurstCount, 2.0 * nBurstCount );
  1913. m_flIdleTimeDelay = 0.1f * ( m_nRemainingBursts - nBurstCount );
  1914. }
  1915. break;
  1916. case SHOOT_MODE_LONG_CYCLE:
  1917. {
  1918. m_nRemainingBursts = 60;
  1919. m_flIdleTimeDelay = 0.0f;
  1920. nHitFactor = 2;
  1921. }
  1922. break;
  1923. case SHOOT_MODE_CONTINUOUS:
  1924. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  1925. {
  1926. // We're relying on the special aiming behavior for bullrushing to just randomly deal damage
  1927. m_nRemainingBursts = 1;
  1928. m_flIdleTimeDelay = 0.0f;
  1929. }
  1930. else
  1931. {
  1932. m_nRemainingBursts = 0;
  1933. m_flIdleTimeDelay = 0.0f;
  1934. nHitFactor = 1000;
  1935. }
  1936. break;
  1937. }
  1938. if ( !GetEnemyVehicle() )
  1939. {
  1940. m_nMaxBurstHits = !IsDeadlyShooting() ? random->RandomInt( 6, 9 ) : 200;
  1941. m_nMaxNearShots = 10000;
  1942. }
  1943. else
  1944. {
  1945. Vector vecVelocity;
  1946. GetEnemyVehicle()->GetVelocity( &vecVelocity, NULL );
  1947. float flSpeed = vecVelocity.Length();
  1948. flSpeed = clamp( flSpeed, 150.0f, 600.0f );
  1949. flSpeed = RemapVal( flSpeed, 150.0f, 600.0f, 0.0f, 1.0f );
  1950. float flAvoid = clamp( m_flAvoidMetric, 100.0f, 400.0f );
  1951. flAvoid = RemapVal( flAvoid, 100.0f, 400.0f, 0.0f, 1.0f );
  1952. float flTotal = 0.5f * ( flSpeed + flAvoid );
  1953. int nHitCount = (int)(RemapVal( flTotal, 0.0f, 1.0f, 7, -0.5 ) + 0.5f);
  1954. int nMin = nHitCount >= 1 ? nHitCount - 1 : 0;
  1955. m_nMaxBurstHits = random->RandomInt( nMin, nHitCount + 1 );
  1956. int nNearShots = (int)(RemapVal( flTotal, 0.0f, 1.0f, 70, 5 ) + 0.5f);
  1957. int nMinNearShots = nNearShots >= 5 ? nNearShots - 5 : 0;
  1958. m_nMaxNearShots = random->RandomInt( nMinNearShots, nNearShots + 5 );
  1959. // Set up the circle of death parameters at this point
  1960. m_flCircleOfDeathRadius = SimpleSplineRemapVal( flTotal, 0.0f, 1.0f,
  1961. CHOPPER_MIN_CIRCLE_OF_DEATH_RADIUS, CHOPPER_MAX_CIRCLE_OF_DEATH_RADIUS );
  1962. }
  1963. m_nMaxBurstHits *= nHitFactor;
  1964. m_nMaxNearShots *= nHitFactor;
  1965. m_nBurstHits = 0;
  1966. m_nNearShots = 0;
  1967. return true;
  1968. }
  1969. //------------------------------------------------------------------------------
  1970. // Shoot where we're facing
  1971. //------------------------------------------------------------------------------
  1972. void CNPC_AttackHelicopter::ShootAtFacingDirection( const Vector &vBasePos, const Vector &vGunDir, bool bFirstShotAccurate )
  1973. {
  1974. // Just shoot where we're facing
  1975. float flSinConeDegrees = sin( sk_helicopter_firingcone.GetFloat() * 0.5f * (3.14f / 180.0f) );
  1976. Vector vecSpread( flSinConeDegrees, flSinConeDegrees, flSinConeDegrees );
  1977. int nShotCount = sk_helicopter_roundsperburst.GetInt();
  1978. if ( bFirstShotAccurate && GetEnemy() )
  1979. {
  1980. // Check to see if the enemy is within his firing cone
  1981. if ( GetEnemy() )
  1982. {
  1983. // Find the closest point to the gunDir
  1984. const Vector &vecCenter = GetEnemy()->WorldSpaceCenter();
  1985. float t;
  1986. Vector vNearPoint;
  1987. Vector vEndPoint;
  1988. VectorMA( vBasePos, 1024.0f, vGunDir, vEndPoint );
  1989. CalcClosestPointOnLine( vecCenter, vBasePos, vEndPoint, vNearPoint, &t );
  1990. if ( t > 0.0f )
  1991. {
  1992. Vector vecDelta;
  1993. VectorSubtract( vecCenter, vBasePos, vecDelta );
  1994. float flDist = VectorNormalize( vecDelta );
  1995. float flPerpDist = vecCenter.DistTo( vNearPoint );
  1996. float flSinAngle = flPerpDist / flDist;
  1997. if ( flSinAngle <= flSinConeDegrees )
  1998. {
  1999. FireBulletsInfo_t info( 1, vBasePos, vecDelta, VECTOR_CONE_PRECALCULATED, 8192, m_iAmmoType );
  2000. info.m_iTracerFreq = 1;
  2001. FireBullets( info );
  2002. --nShotCount;
  2003. }
  2004. }
  2005. }
  2006. }
  2007. #ifdef HL2_EPISODIC
  2008. if( GetEnemy() != NULL )
  2009. {
  2010. CSoundEnt::InsertSound( SOUND_DANGER, GetEnemy()->WorldSpaceCenter(), 180.0f, 0.5f, this, SOUNDENT_CHANNEL_REPEATED_DANGER );
  2011. }
  2012. #endif//HL2_EPISODIC
  2013. DoMuzzleFlash();
  2014. FireBulletsInfo_t info( nShotCount, vBasePos, vGunDir, vecSpread, 8192, m_iAmmoType );
  2015. info.m_iTracerFreq = 1;
  2016. FireBullets( info );
  2017. }
  2018. //-----------------------------------------------------------------------------
  2019. // Can we zap it?
  2020. //-----------------------------------------------------------------------------
  2021. bool CNPC_AttackHelicopter::IsValidZapTarget( CBaseEntity *pTarget )
  2022. {
  2023. // Don't use the player or vehicle as a zap target, we'll do that ourselves.
  2024. if ( pTarget->IsPlayer() || pTarget->GetServerVehicle() )
  2025. return false;
  2026. if ( pTarget == this )
  2027. return false;
  2028. if ( !pTarget->IsSolid() )
  2029. return false;
  2030. Assert( pTarget );
  2031. IPhysicsObject *pList[VPHYSICS_MAX_OBJECT_LIST_COUNT];
  2032. int count = pTarget->VPhysicsGetObjectList( pList, ARRAYSIZE(pList) );
  2033. for ( int i = 0; i < count; i++ )
  2034. {
  2035. int material = pList[i]->GetMaterialIndex();
  2036. const surfacedata_t *pSurfaceData = physprops->GetSurfaceData( material );
  2037. // Is flesh or metal? Go for it!
  2038. if ( pSurfaceData->game.material == CHAR_TEX_METAL ||
  2039. pSurfaceData->game.material == CHAR_TEX_FLESH ||
  2040. pSurfaceData->game.material == CHAR_TEX_VENT ||
  2041. pSurfaceData->game.material == CHAR_TEX_GRATE ||
  2042. pSurfaceData->game.material == CHAR_TEX_COMPUTER ||
  2043. pSurfaceData->game.material == CHAR_TEX_BLOODYFLESH ||
  2044. pSurfaceData->game.material == CHAR_TEX_ALIENFLESH )
  2045. {
  2046. return true;
  2047. }
  2048. }
  2049. return false;
  2050. }
  2051. //------------------------------------------------------------------------------
  2052. // Effects
  2053. //------------------------------------------------------------------------------
  2054. void CNPC_AttackHelicopter::CreateZapBeam( const Vector &vecTargetPos )
  2055. {
  2056. CEffectData data;
  2057. data.m_nEntIndex = entindex();
  2058. data.m_nAttachmentIndex = 0; // m_nGunTipAttachment;
  2059. data.m_vOrigin = vecTargetPos;
  2060. data.m_flScale = 5;
  2061. DispatchEffect( "TeslaZap", data );
  2062. }
  2063. void CNPC_AttackHelicopter::CreateEntityZapEffect( CBaseEntity *pEnt )
  2064. {
  2065. CEffectData data;
  2066. data.m_nEntIndex = pEnt->entindex();
  2067. data.m_flMagnitude = 10;
  2068. data.m_flScale = 1.0f;
  2069. DispatchEffect( "TeslaHitboxes", data );
  2070. }
  2071. //------------------------------------------------------------------------------
  2072. // Here's what we do when we *are* firing
  2073. //------------------------------------------------------------------------------
  2074. void CNPC_AttackHelicopter::FireElectricityGun( )
  2075. {
  2076. if ( m_flNextAttack > gpGlobals->curtime )
  2077. return;
  2078. EmitSound( "ReallyLoudSpark" );
  2079. CBaseEntity *ppEnts[256];
  2080. Vector vecCenter = WorldSpaceCenter();
  2081. float flRadius = 500.0f;
  2082. vecCenter.z -= flRadius * 0.8f;
  2083. int nEntCount = UTIL_EntitiesInSphere( ppEnts, 256, vecCenter, flRadius, 0 );
  2084. CBaseEntity *ppCandidates[256];
  2085. int nCandidateCount = 0;
  2086. int i;
  2087. for ( i = 0; i < nEntCount; i++ )
  2088. {
  2089. if ( ppEnts[i] == NULL )
  2090. continue;
  2091. // Zap metal or flesh things.
  2092. if ( !IsValidZapTarget( ppEnts[i] ) )
  2093. continue;
  2094. ppCandidates[ nCandidateCount++ ] = ppEnts[i];
  2095. }
  2096. // First, put a bolt in front of the player, at random
  2097. float flDist = 1024;
  2098. if ( GetEnemy() )
  2099. {
  2100. Vector vecDelta;
  2101. Vector2DSubtract( GetEnemy()->WorldSpaceCenter().AsVector2D(), WorldSpaceCenter().AsVector2D(), vecDelta.AsVector2D() );
  2102. vecDelta.z = 0.0f;
  2103. flDist = VectorNormalize( vecDelta );
  2104. Vector vecPerp( -vecDelta.y, vecDelta.x, 0.0f );
  2105. int nBoltCount = (int)(ClampSplineRemapVal( flDist, 256.0f, 1024.0f, 8, 0 ) + 0.5f);
  2106. for ( i = 0; i < nBoltCount; ++i )
  2107. {
  2108. Vector vecTargetPt = GetEnemy()->WorldSpaceCenter();
  2109. VectorMA( vecTargetPt, random->RandomFloat( flDist + 100, flDist + 500 ), vecDelta, vecTargetPt );
  2110. VectorMA( vecTargetPt, random->RandomFloat( -500, 500 ), vecPerp, vecTargetPt );
  2111. vecTargetPt.z += random->RandomFloat( -500, 500 );
  2112. CreateZapBeam( vecTargetPt );
  2113. }
  2114. }
  2115. // Next, choose the number of bolts...
  2116. int nBoltCount = random->RandomInt( 8, 16 );
  2117. for ( i = 0; i < nBoltCount; ++i )
  2118. {
  2119. if ( (nCandidateCount > 0) && random->RandomFloat( 0.0f, 1.0f ) < 0.6f )
  2120. {
  2121. --nCandidateCount;
  2122. Vector vecTarget;
  2123. ppCandidates[nCandidateCount]->CollisionProp()->RandomPointInBounds( vec3_origin, Vector( 1, 1, 1 ), &vecTarget );
  2124. CreateZapBeam( vecTarget );
  2125. CreateEntityZapEffect( ppCandidates[nCandidateCount] );
  2126. }
  2127. else
  2128. {
  2129. // Select random point *on* sphere
  2130. Vector vecTargetPt;
  2131. float flEffectRadius = random->RandomFloat( flRadius * 1.2, flRadius * 1.5f );
  2132. float flTheta = random->RandomFloat( 0.0f, 2.0f * M_PI );
  2133. float flPhi = random->RandomFloat( -0.5f * M_PI, 0.5f * M_PI );
  2134. vecTargetPt.x = cos(flTheta) * cos(flPhi);
  2135. vecTargetPt.y = sin(flTheta) * cos(flPhi);
  2136. vecTargetPt.z = sin(flPhi);
  2137. vecTargetPt *= flEffectRadius;
  2138. vecTargetPt += vecCenter;
  2139. CreateZapBeam( vecTargetPt );
  2140. }
  2141. }
  2142. // Finally, put a bolt right at the player, at random
  2143. float flHitRatio = ClampSplineRemapVal( flDist, 128.0f, 512.0f, 0.75f, 0.0f );
  2144. if ( random->RandomFloat( 0.0f, 1.0f ) < flHitRatio )
  2145. {
  2146. if ( GetEnemyVehicle() )
  2147. {
  2148. Vector vecTarget;
  2149. GetEnemyVehicle()->CollisionProp()->RandomPointInBounds( vec3_origin, Vector( 1, 1, 1 ), &vecTarget );
  2150. CreateZapBeam( vecTarget );
  2151. CreateEntityZapEffect( GetEnemyVehicle() );
  2152. CTakeDamageInfo info( this, this, 5, DMG_SHOCK );
  2153. GetEnemy()->TakeDamage( info );
  2154. }
  2155. else if ( GetEnemy() )
  2156. {
  2157. Vector vecTarget;
  2158. GetEnemy()->CollisionProp()->RandomPointInBounds( vec3_origin, Vector( 1, 1, 1 ), &vecTarget );
  2159. CreateZapBeam( vecTarget );
  2160. CTakeDamageInfo info( this, this, 5, DMG_SHOCK );
  2161. GetEnemy()->TakeDamage( info );
  2162. }
  2163. }
  2164. m_flNextAttack = gpGlobals->curtime + random->RandomFloat( 0.3f, 1.0f );
  2165. }
  2166. //------------------------------------------------------------------------------
  2167. // Here's what we do when we *are* firing
  2168. //------------------------------------------------------------------------------
  2169. #define INTERVAL_BETWEEN_HITS 4
  2170. bool CNPC_AttackHelicopter::DoGunFiring( const Vector &vBasePos, const Vector &vGunDir, const Vector &vecFireAtPosition )
  2171. {
  2172. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  2173. float flVolume = controller.SoundGetVolume( m_pGunFiringSound );
  2174. if ( flVolume != 1.0f )
  2175. {
  2176. controller.SoundChangeVolume( m_pGunFiringSound, 1.0, 0.01f );
  2177. }
  2178. if ( ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE ) && ( IsInSecondaryMode( BULLRUSH_MODE_SHOOT_GUN ) ) )
  2179. {
  2180. ShootAtFacingDirection( vBasePos, vGunDir, m_nRemainingBursts == 0 );
  2181. }
  2182. else if ( GetEnemyVehicle() )
  2183. {
  2184. ShootAtVehicle( vBasePos, vecFireAtPosition );
  2185. }
  2186. else if ( GetEnemy() && GetEnemy()->IsPlayer() )
  2187. {
  2188. if ( !IsDeadlyShooting() )
  2189. {
  2190. ShootAtPlayer( vBasePos, vGunDir );
  2191. }
  2192. else
  2193. {
  2194. ShootAtFacingDirection( vBasePos, vGunDir, true );
  2195. }
  2196. }
  2197. else
  2198. {
  2199. ShootAtFacingDirection( vBasePos, vGunDir, false );
  2200. }
  2201. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  2202. {
  2203. if ( --m_nRemainingBursts < 0 )
  2204. {
  2205. m_nRemainingBursts = INTERVAL_BETWEEN_HITS;
  2206. }
  2207. return true;
  2208. }
  2209. --m_nRemainingBursts;
  2210. if ( m_nRemainingBursts > 0 )
  2211. return true;
  2212. controller.SoundChangeVolume( m_pGunFiringSound, 0.0, 0.01f );
  2213. float flIdleTime = CHOPPER_GUN_IDLE_TIME;
  2214. float flVariance = flIdleTime * 0.1f;
  2215. m_flNextAttack = gpGlobals->curtime + m_flIdleTimeDelay + random->RandomFloat(flIdleTime - flVariance, flIdleTime + flVariance);
  2216. m_nGunState = GUN_STATE_IDLE;
  2217. SetPauseState( PAUSE_NO_PAUSE );
  2218. return true;
  2219. }
  2220. //------------------------------------------------------------------------------
  2221. // Is it "fair" to drop this bomb?
  2222. //------------------------------------------------------------------------------
  2223. #define MIN_BOMB_DISTANCE_SQR ( 600.0f * 600.0f )
  2224. bool CNPC_AttackHelicopter::IsBombDropFair( const Vector &vecBombStartPos, const Vector &vecBombVelocity )
  2225. {
  2226. if ( (m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE) && IsInSecondaryMode( BULLRUSH_MODE_SHOOT_IDLE_PLAYER ) )
  2227. return true;
  2228. // Can happen if you're noclipping around
  2229. if ( !GetEnemy() )
  2230. return false;
  2231. // If the player is moving slowly, it's fair
  2232. if ( GetEnemy()->GetSmoothedVelocity().LengthSqr() < ( CHOPPER_SLOW_BOMB_SPEED * CHOPPER_SLOW_BOMB_SPEED ) )
  2233. return true;
  2234. // Skip out if we're right above or behind the player.. that's unfair
  2235. if ( GetEnemy() && GetEnemy()->IsPlayer() )
  2236. {
  2237. // How much time will it take to fall?
  2238. // dx = 0.5 * a * t^2
  2239. Vector vecTarget = GetEnemy()->BodyTarget( GetAbsOrigin(), false );
  2240. float dz = vecBombStartPos.z - vecTarget.z;
  2241. float dt = (dz > 0.0f) ? sqrt( 2 * dz / GetCurrentGravity() ) : 0.0f;
  2242. // Where will the enemy be in that time?
  2243. Vector vecEnemyVel = GetEnemy()->GetSmoothedVelocity();
  2244. VectorMA( vecTarget, dt, vecEnemyVel, vecTarget );
  2245. // Where will the bomb be in that time?
  2246. Vector vecBomb;
  2247. VectorMA( vecBombStartPos, dt, vecBombVelocity, vecBomb );
  2248. float flEnemySpeed = vecEnemyVel.LengthSqr();
  2249. flEnemySpeed = clamp( flEnemySpeed, 200.0f, 500.0f );
  2250. float flDistFactorSq = RemapVal( flEnemySpeed, 200.0f, 500.0f, 0.3f, 1.0f );
  2251. flDistFactorSq *= flDistFactorSq;
  2252. // If it's too close, then we're not doing it.
  2253. if ( vecBomb.AsVector2D().DistToSqr( vecTarget.AsVector2D() ) < (flDistFactorSq * MIN_BOMB_DISTANCE_SQR) )
  2254. return false;
  2255. }
  2256. return true;
  2257. }
  2258. //-----------------------------------------------------------------------------
  2259. // Purpose: Create the bomb entity and set it up
  2260. // Input : &vecPos - Position to spawn at
  2261. // &vecVelocity - velocity to spawn with
  2262. //-----------------------------------------------------------------------------
  2263. CGrenadeHelicopter *CNPC_AttackHelicopter::SpawnBombEntity( const Vector &vecPos, const Vector &vecVelocity )
  2264. {
  2265. // Create the grenade and set it up
  2266. CGrenadeHelicopter *pGrenade = static_cast<CGrenadeHelicopter*>(CreateEntityByName( "grenade_helicopter" ));
  2267. pGrenade->SetAbsOrigin( vecPos );
  2268. pGrenade->SetOwnerEntity( this );
  2269. pGrenade->SetThrower( this );
  2270. pGrenade->SetAbsVelocity( vecVelocity );
  2271. DispatchSpawn( pGrenade );
  2272. pGrenade->SetExplodeOnContact( m_bBombsExplodeOnContact );
  2273. #ifdef HL2_EPISODIC
  2274. // Disable collisions with the owner's bone followers while we drop
  2275. physfollower_t *pFollower = m_BoneFollowerManager.GetBoneFollower( 0 );
  2276. if ( pFollower )
  2277. {
  2278. CBaseEntity *pBoneFollower = pFollower->hFollower;
  2279. PhysDisableEntityCollisions( pBoneFollower, pGrenade );
  2280. pGrenade->SetCollisionObject( pBoneFollower );
  2281. }
  2282. #endif // HL2_EPISODIC
  2283. return pGrenade;
  2284. }
  2285. //------------------------------------------------------------------------------
  2286. // Actually drops the bomb
  2287. //------------------------------------------------------------------------------
  2288. void CNPC_AttackHelicopter::CreateBomb( bool bCheckForFairness, Vector *pVecVelocity, bool bMegaBomb )
  2289. {
  2290. if ( m_bBombingSuppressed )
  2291. return;
  2292. Vector vTipPos;
  2293. GetAttachment( m_nBombAttachment, vTipPos );
  2294. if ( !CBombSuppressor::CanBomb( vTipPos ) )
  2295. return;
  2296. // Compute velocity
  2297. Vector vecActualVelocity;
  2298. if ( !pVecVelocity )
  2299. {
  2300. Vector vecAcross;
  2301. vecActualVelocity = GetAbsVelocity();
  2302. CrossProduct( vecActualVelocity, Vector( 0, 0, 1 ), vecAcross );
  2303. VectorNormalize( vecAcross );
  2304. vecAcross *= random->RandomFloat( 10.0f, 30.0f );
  2305. vecAcross *= random->RandomFloat( 0.0f, 1.0f ) < 0.5f ? 1.0f : -1.0f;
  2306. // Blat out z component of velocity if it's moving upward....
  2307. if ( vecActualVelocity.z > 0 )
  2308. {
  2309. vecActualVelocity.z = 0.0f;
  2310. }
  2311. vecActualVelocity += vecAcross;
  2312. }
  2313. else
  2314. {
  2315. vecActualVelocity = *pVecVelocity;
  2316. }
  2317. if ( bCheckForFairness )
  2318. {
  2319. if ( !IsBombDropFair( vTipPos, vecActualVelocity ) )
  2320. return;
  2321. }
  2322. AddGesture( (Activity)ACT_HELICOPTER_DROP_BOMB );
  2323. EmitSound( "NPC_AttackHelicopter.DropMine" );
  2324. // Make the bomb and send it off
  2325. CGrenadeHelicopter *pGrenade = SpawnBombEntity( vTipPos, vecActualVelocity );
  2326. if ( pGrenade && bMegaBomb )
  2327. {
  2328. pGrenade->AddSpawnFlags( SF_GRENADE_HELICOPTER_MEGABOMB );
  2329. }
  2330. }
  2331. //------------------------------------------------------------------------------
  2332. // Drop a bomb at a particular location
  2333. //------------------------------------------------------------------------------
  2334. void CNPC_AttackHelicopter::InputDropBomb( inputdata_t &inputdata )
  2335. {
  2336. if ( m_flInputDropBombTime > gpGlobals->curtime )
  2337. return;
  2338. // Prevent two triggers from being hit the same frame
  2339. m_flInputDropBombTime = gpGlobals->curtime + 0.01f;
  2340. CreateBomb( );
  2341. // If we're in the middle of a bomb dropping schedule, wait to drop another bomb.
  2342. if ( ShouldDropBombs() )
  2343. {
  2344. m_flNextAttack = gpGlobals->curtime + 0.5f + random->RandomFloat( 0.3f, 0.6f );
  2345. }
  2346. }
  2347. //------------------------------------------------------------------------------
  2348. // Drops a bomb straight downwards
  2349. //------------------------------------------------------------------------------
  2350. void CNPC_AttackHelicopter::InputDropBombStraightDown( inputdata_t &inputdata )
  2351. {
  2352. if ( m_flInputDropBombTime > gpGlobals->curtime )
  2353. return;
  2354. // Prevent two triggers from being hit the same frame
  2355. m_flInputDropBombTime = gpGlobals->curtime + 0.01f;
  2356. Vector vTipPos;
  2357. GetAttachment( m_nBombAttachment, vTipPos );
  2358. // Make the bomb drop straight down
  2359. SpawnBombEntity( vTipPos, vec3_origin );
  2360. // If we're in the middle of a bomb dropping schedule, wait to drop another bomb.
  2361. if ( ShouldDropBombs() )
  2362. {
  2363. m_flNextAttack = gpGlobals->curtime + 0.5f + random->RandomFloat( 0.3f, 0.6f );
  2364. }
  2365. }
  2366. //------------------------------------------------------------------------------
  2367. // Drop a bomb at a particular location
  2368. //------------------------------------------------------------------------------
  2369. void CNPC_AttackHelicopter::InputDropBombAtTargetInternal( inputdata_t &inputdata, bool bCheckFairness )
  2370. {
  2371. if ( m_flInputDropBombTime > gpGlobals->curtime )
  2372. return;
  2373. // Prevent two triggers from being hit the same frame
  2374. m_flInputDropBombTime = gpGlobals->curtime + 0.01f;
  2375. // Find our specified target
  2376. string_t strBombTarget = MAKE_STRING( inputdata.value.String() );
  2377. CBaseEntity *pBombEnt = gEntList.FindEntityByName( NULL, strBombTarget );
  2378. if ( pBombEnt == NULL )
  2379. {
  2380. Warning( "%s: Could not find bomb drop target '%s'!\n", GetClassname(), STRING( strBombTarget ) );
  2381. return;
  2382. }
  2383. Vector vTipPos;
  2384. GetAttachment( m_nBombAttachment, vTipPos );
  2385. // Compute the time it would take to fall to the target
  2386. Vector vecTarget = pBombEnt->BodyTarget( GetAbsOrigin(), false );
  2387. float dz = vTipPos.z - vecTarget.z;
  2388. if ( dz <= 0.0f )
  2389. {
  2390. Warning("Bomb target %s is above the chopper!\n", STRING( strBombTarget ) );
  2391. return;
  2392. }
  2393. float dt = sqrt( 2 * dz / GetCurrentGravity() );
  2394. // Compute the velocity that would make it happen
  2395. Vector vecVelocity;
  2396. VectorSubtract( vecTarget, vTipPos, vecVelocity );
  2397. vecVelocity /= dt;
  2398. vecVelocity.z = 0.0f;
  2399. if ( bCheckFairness )
  2400. {
  2401. if ( !IsBombDropFair( vTipPos, vecVelocity ) )
  2402. return;
  2403. }
  2404. // Make the bomb and send it off
  2405. SpawnBombEntity( vTipPos, vecVelocity );
  2406. // If we're in the middle of a bomb dropping schedule, wait to drop another bomb.
  2407. if ( ShouldDropBombs() )
  2408. {
  2409. m_flNextAttack = gpGlobals->curtime + 1.5f + random->RandomFloat( 0.1f, 0.2f );
  2410. }
  2411. }
  2412. //------------------------------------------------------------------------------
  2413. // Drop a bomb at a particular location
  2414. //------------------------------------------------------------------------------
  2415. void CNPC_AttackHelicopter::InputDropBombAtTargetAlways( inputdata_t &inputdata )
  2416. {
  2417. InputDropBombAtTargetInternal( inputdata, false );
  2418. }
  2419. //------------------------------------------------------------------------------
  2420. // Drop a bomb at a particular location
  2421. //------------------------------------------------------------------------------
  2422. void CNPC_AttackHelicopter::InputDropBombAtTarget( inputdata_t &inputdata )
  2423. {
  2424. InputDropBombAtTargetInternal( inputdata, true );
  2425. }
  2426. //------------------------------------------------------------------------------
  2427. // Drop a bomb at a particular location
  2428. //------------------------------------------------------------------------------
  2429. void CNPC_AttackHelicopter::InputDropBombDelay( inputdata_t &inputdata )
  2430. {
  2431. m_flInputDropBombTime = gpGlobals->curtime + inputdata.value.Float();
  2432. if ( ShouldDropBombs() )
  2433. {
  2434. m_flNextAttack = m_flInputDropBombTime;
  2435. }
  2436. }
  2437. //------------------------------------------------------------------------------
  2438. // Drop those bombs!
  2439. //------------------------------------------------------------------------------
  2440. #define MAX_BULLRUSH_BOMB_DISTANCE_SQR ( 3072.0f * 3072.0f )
  2441. void CNPC_AttackHelicopter::DropBombs( )
  2442. {
  2443. // Can't continually fire....
  2444. if (m_flNextAttack > gpGlobals->curtime)
  2445. return;
  2446. // Otherwise, behave as normal.
  2447. if ( m_nAttackMode != ATTACK_MODE_BULLRUSH_VEHICLE )
  2448. {
  2449. if ( GetEnemy() && GetEnemy()->IsPlayer() )
  2450. {
  2451. if ( GetEnemy()->GetSmoothedVelocity().LengthSqr() > ( CHOPPER_SLOW_BOMB_SPEED * CHOPPER_SLOW_BOMB_SPEED ) )
  2452. {
  2453. // Don't drop bombs if you are behind the player, unless the player is moving slowly
  2454. float flLeadingDistSq = GetLeadingDistance() * 0.75f;
  2455. flLeadingDistSq *= flLeadingDistSq;
  2456. Vector vecPoint;
  2457. ClosestPointToCurrentPath( &vecPoint );
  2458. if ( vecPoint.AsVector2D().DistToSqr( GetDesiredPosition().AsVector2D() ) > flLeadingDistSq )
  2459. return;
  2460. }
  2461. }
  2462. }
  2463. else
  2464. {
  2465. // Skip out if we're bullrushing but too far from the player
  2466. if ( GetEnemy() )
  2467. {
  2468. if ( GetEnemy()->GetAbsOrigin().AsVector2D().DistToSqr( GetAbsOrigin().AsVector2D() ) > MAX_BULLRUSH_BOMB_DISTANCE_SQR )
  2469. return;
  2470. }
  2471. }
  2472. CreateBomb( );
  2473. m_flNextAttack = gpGlobals->curtime + 0.5f + random->RandomFloat( 0.3f, 0.6f );
  2474. if ( (m_nAttackMode != ATTACK_MODE_BULLRUSH_VEHICLE) )
  2475. {
  2476. if ( --m_nGrenadeCount <= 0 )
  2477. {
  2478. m_nGrenadeCount = CHOPPER_BOMB_DROP_COUNT;
  2479. m_flNextAttack += random->RandomFloat( 1.5f, 3.0f );
  2480. }
  2481. }
  2482. }
  2483. //------------------------------------------------------------------------------
  2484. // Should we drop those bombs?
  2485. //------------------------------------------------------------------------------
  2486. #define BOMB_GRACE_PERIOD 1.5f
  2487. #define BOMB_MIN_SPEED 150.0
  2488. bool CNPC_AttackHelicopter::ShouldDropBombs( void )
  2489. {
  2490. if ( IsCarpetBombing() )
  2491. return true;
  2492. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  2493. {
  2494. // Distance determines whether or not we should do this
  2495. if ((m_nSecondaryMode == BULLRUSH_MODE_SHOOT_IDLE_PLAYER) && (SecondaryModeTime() >= BULLRUSH_IDLE_PLAYER_FIRE_TIME))
  2496. return ShouldBombIdlePlayer();
  2497. return (( m_nSecondaryMode == BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED ) || ( m_nSecondaryMode == BULLRUSH_MODE_DROP_BOMBS_FOLLOW_PLAYER ));
  2498. }
  2499. if (!IsLeading() || !GetEnemyVehicle())
  2500. return false;
  2501. if (( m_nAttackMode != ATTACK_MODE_BOMB_VEHICLE ) && ( m_nAttackMode != ATTACK_MODE_ALWAYS_LEAD_VEHICLE ))
  2502. return false;
  2503. if ( m_nGunState != GUN_STATE_IDLE )
  2504. return false;
  2505. // This is for bombing. If you get hit, give a grace period to get back to speed
  2506. float flSpeedSqr = GetEnemy()->GetSmoothedVelocity().LengthSqr();
  2507. if ( flSpeedSqr >= BOMB_MIN_SPEED * BOMB_MIN_SPEED )
  2508. {
  2509. m_flLastFastTime = gpGlobals->curtime;
  2510. }
  2511. else
  2512. {
  2513. if ( ( gpGlobals->curtime - m_flLastFastTime ) < BOMB_GRACE_PERIOD )
  2514. return false;
  2515. }
  2516. float flSpeedAlongPath = TargetSpeedAlongPath();
  2517. if ( m_nAttackMode == ATTACK_MODE_BOMB_VEHICLE )
  2518. return ( flSpeedAlongPath > -BOMB_MIN_SPEED );
  2519. // This is for ALWAYS_LEAD
  2520. if ( fabs(flSpeedAlongPath) < 50.0f )
  2521. return false;
  2522. float flLeadingDist = ComputeDistanceToLeadingPosition( );
  2523. flLeadingDist = GetLeadingDistance() - flLeadingDist;
  2524. if ( flSpeedAlongPath < 0.0f )
  2525. {
  2526. return flLeadingDist < 300.0f;
  2527. }
  2528. else
  2529. {
  2530. return flLeadingDist > -300.0f;
  2531. }
  2532. }
  2533. //------------------------------------------------------------------------------
  2534. // Different bomb-dropping behavior
  2535. //------------------------------------------------------------------------------
  2536. void CNPC_AttackHelicopter::BullrushBombs( )
  2537. {
  2538. if ( gpGlobals->curtime < m_flNextBullrushBombTime )
  2539. return;
  2540. if ( m_nBullrushBombMode & 0x1 )
  2541. {
  2542. CreateBomb( false, NULL, true );
  2543. }
  2544. else
  2545. {
  2546. Vector vecAcross;
  2547. Vector vecVelocity = GetAbsVelocity();
  2548. CrossProduct( vecVelocity, Vector( 0, 0, 1 ), vecAcross );
  2549. VectorNormalize( vecAcross );
  2550. vecAcross *= random->RandomFloat( 300.0f, 500.0f );
  2551. // Blat out z component of velocity if it's moving upward....
  2552. if ( vecVelocity.z > 0 )
  2553. {
  2554. vecVelocity.z = 0.0f;
  2555. }
  2556. vecVelocity += vecAcross;
  2557. CreateBomb( false, &vecVelocity, true );
  2558. VectorMA( vecVelocity, -2.0f, vecAcross, vecVelocity );
  2559. CreateBomb( false, &vecVelocity, true );
  2560. }
  2561. m_nBullrushBombMode = !m_nBullrushBombMode;
  2562. m_flNextBullrushBombTime = gpGlobals->curtime + 0.2f;
  2563. }
  2564. //-----------------------------------------------------------------------------
  2565. // Purpose: Turn the gun off
  2566. //-----------------------------------------------------------------------------
  2567. void CNPC_AttackHelicopter::InputGunOff( inputdata_t &inputdata )
  2568. {
  2569. BaseClass::InputGunOff( inputdata );
  2570. if ( m_pGunFiringSound )
  2571. {
  2572. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  2573. controller.SoundChangeVolume( m_pGunFiringSound, 0.0, 0.01f );
  2574. }
  2575. }
  2576. //------------------------------------------------------------------------------
  2577. // Fire that gun baby!
  2578. //------------------------------------------------------------------------------
  2579. bool CNPC_AttackHelicopter::FireGun( void )
  2580. {
  2581. // Do the test electricity gun
  2582. if ( HasSpawnFlags(SF_HELICOPTER_ELECTRICAL_DRONE) )
  2583. {
  2584. FireElectricityGun( );
  2585. return true;
  2586. }
  2587. // HACK: CBaseHelicopter ignores this, and fire forever at the last place it saw the player. Why?
  2588. if (( m_nGunState == GUN_STATE_IDLE ) && ( m_nAttackMode != ATTACK_MODE_BULLRUSH_VEHICLE ) && !IsCarpetBombing() )
  2589. {
  2590. if ( (m_flLastSeen + 1 <= gpGlobals->curtime) || (m_flPrevSeen + m_flGracePeriod > gpGlobals->curtime) )
  2591. return false;
  2592. }
  2593. if ( IsCarpetBombing() )
  2594. {
  2595. BullrushBombs();
  2596. return false;
  2597. }
  2598. if ( ShouldDropBombs() )
  2599. {
  2600. DropBombs( );
  2601. return false;
  2602. }
  2603. // Drop those bullrush bombs when shooting...
  2604. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  2605. {
  2606. if ( IsInSecondaryMode( BULLRUSH_MODE_MEGA_BOMB ) )
  2607. {
  2608. BullrushBombs( );
  2609. return false;
  2610. }
  2611. // Don't fire if we're bullrushing and we're getting distance
  2612. if ( !IsInSecondaryMode( BULLRUSH_MODE_SHOOT_GUN ) && !IsInSecondaryMode(BULLRUSH_MODE_SHOOT_IDLE_PLAYER) )
  2613. return false;
  2614. // If we're in the grace period on this mode, then don't fire
  2615. if ( IsInSecondaryMode( BULLRUSH_MODE_SHOOT_IDLE_PLAYER ) && (SecondaryModeTime() < BULLRUSH_IDLE_PLAYER_FIRE_TIME) )
  2616. {
  2617. // Stop our gun sound
  2618. if ( m_nGunState != GUN_STATE_IDLE )
  2619. {
  2620. ShutdownGunDuringBullrush();
  2621. }
  2622. return false;
  2623. }
  2624. }
  2625. // Get gun attachment points
  2626. Vector vBasePos;
  2627. GetAttachment( m_nGunBaseAttachment, vBasePos );
  2628. // Aim perfectly while idle, but after charging, the gun don't move so fast.
  2629. Vector vecFireAtPosition;
  2630. if ( !GetEnemyVehicle() || (m_nGunState == GUN_STATE_IDLE) )
  2631. {
  2632. ComputeFireAtPosition( &vecFireAtPosition );
  2633. }
  2634. else
  2635. {
  2636. ComputeVehicleFireAtPosition( &vecFireAtPosition );
  2637. }
  2638. Vector vTargetDir = vecFireAtPosition - vBasePos;
  2639. VectorNormalize( vTargetDir );
  2640. // Makes the model of the gun point to where we're aiming.
  2641. if ( !PoseGunTowardTargetDirection( vTargetDir ) )
  2642. return false;
  2643. // Are we charging?
  2644. if ( m_nGunState == GUN_STATE_CHARGING )
  2645. {
  2646. if ( !DoGunCharging( ) )
  2647. return false;
  2648. }
  2649. Vector vTipPos;
  2650. GetAttachment( m_nGunTipAttachment, vTipPos );
  2651. Vector vGunDir = vTipPos - vBasePos;
  2652. VectorNormalize( vGunDir );
  2653. // Are we firing?
  2654. if ( m_nGunState == GUN_STATE_FIRING )
  2655. {
  2656. return DoGunFiring( vTipPos, vGunDir, vecFireAtPosition );
  2657. }
  2658. return DoGunIdle( vGunDir, vTargetDir );
  2659. }
  2660. //-----------------------------------------------------------------------------
  2661. // Should we trigger a damage effect?
  2662. //-----------------------------------------------------------------------------
  2663. inline bool CNPC_AttackHelicopter::ShouldTriggerDamageEffect( int nPrevHealth, int nEffectCount ) const
  2664. {
  2665. int nPrevRange = (int)( ((float)nPrevHealth / (float)GetMaxHealth()) * nEffectCount );
  2666. int nRange = (int)( ((float)GetHealth() / (float)GetMaxHealth()) * nEffectCount );
  2667. return ( nRange != nPrevRange );
  2668. }
  2669. //-----------------------------------------------------------------------------
  2670. // Add a smoke trail since we've taken more damage
  2671. //-----------------------------------------------------------------------------
  2672. void CNPC_AttackHelicopter::AddSmokeTrail( const Vector &vecPos )
  2673. {
  2674. if ( m_nSmokeTrailCount == MAX_SMOKE_TRAILS )
  2675. return;
  2676. // See if there's an attachment for this smoke trail
  2677. int nAttachment = LookupAttachment( UTIL_VarArgs( "damage%d", m_nSmokeTrailCount ) );
  2678. if ( nAttachment == 0 )
  2679. return;
  2680. // The final smoke trail is a flaming engine
  2681. if ( m_nSmokeTrailCount == 0 || m_nSmokeTrailCount % 2 )
  2682. {
  2683. CFireTrail *pFireTrail = CFireTrail::CreateFireTrail();
  2684. if ( pFireTrail == NULL )
  2685. return;
  2686. m_hSmokeTrail[m_nSmokeTrailCount] = pFireTrail;
  2687. pFireTrail->FollowEntity( this, UTIL_VarArgs( "damage%d", m_nSmokeTrailCount ) );
  2688. pFireTrail->SetParent( this, nAttachment );
  2689. pFireTrail->SetLocalOrigin( vec3_origin );
  2690. pFireTrail->SetMoveType( MOVETYPE_NONE );
  2691. pFireTrail->SetLifetime( -1 );
  2692. }
  2693. else
  2694. {
  2695. SmokeTrail *pSmokeTrail = SmokeTrail::CreateSmokeTrail();
  2696. if( !pSmokeTrail )
  2697. return;
  2698. m_hSmokeTrail[m_nSmokeTrailCount] = pSmokeTrail;
  2699. pSmokeTrail->m_SpawnRate = 48;
  2700. pSmokeTrail->m_ParticleLifetime = 0.5f;
  2701. pSmokeTrail->m_StartColor.Init(0.15, 0.15, 0.15);
  2702. pSmokeTrail->m_EndColor.Init(0.0, 0.0, 0.0);
  2703. pSmokeTrail->m_StartSize = 24;
  2704. pSmokeTrail->m_EndSize = 80;
  2705. pSmokeTrail->m_SpawnRadius = 8;
  2706. pSmokeTrail->m_Opacity = 0.2;
  2707. pSmokeTrail->m_MinSpeed = 16;
  2708. pSmokeTrail->m_MaxSpeed = 64;
  2709. pSmokeTrail->SetLifetime(-1);
  2710. pSmokeTrail->SetParent( this, nAttachment );
  2711. pSmokeTrail->SetLocalOrigin( vec3_origin );
  2712. pSmokeTrail->SetMoveType( MOVETYPE_NONE );
  2713. }
  2714. m_nSmokeTrailCount++;
  2715. }
  2716. //-----------------------------------------------------------------------------
  2717. // Destroy all smoke trails
  2718. //-----------------------------------------------------------------------------
  2719. void CNPC_AttackHelicopter::DestroySmokeTrails()
  2720. {
  2721. for ( int i = m_nSmokeTrailCount; --i >= 0; )
  2722. {
  2723. UTIL_Remove( m_hSmokeTrail[i] );
  2724. m_hSmokeTrail[i] = NULL;
  2725. }
  2726. }
  2727. //-----------------------------------------------------------------------------
  2728. // Purpose:
  2729. // Input : &vecChunkPos -
  2730. //-----------------------------------------------------------------------------
  2731. void Chopper_CreateChunk( CBaseEntity *pChopper, const Vector &vecChunkPos, const QAngle &vecChunkAngles, const char *pszChunkName, bool bSmall )
  2732. {
  2733. // Drop a flaming, smoking chunk.
  2734. CGib *pChunk = CREATE_ENTITY( CGib, "gib" );
  2735. pChunk->Spawn( pszChunkName );
  2736. pChunk->SetBloodColor( DONT_BLEED );
  2737. pChunk->SetAbsOrigin( vecChunkPos );
  2738. pChunk->SetAbsAngles( vecChunkAngles );
  2739. pChunk->SetOwnerEntity( pChopper );
  2740. if ( bSmall )
  2741. {
  2742. pChunk->m_lifeTime = random->RandomFloat( 0.5f, 1.0f );
  2743. pChunk->SetSolidFlags( FSOLID_NOT_SOLID );
  2744. pChunk->SetSolid( SOLID_BBOX );
  2745. pChunk->AddEffects( EF_NODRAW );
  2746. pChunk->SetGravity( UTIL_ScaleForGravity( 400 ) );
  2747. }
  2748. else
  2749. {
  2750. pChunk->m_lifeTime = 5.0f;
  2751. }
  2752. pChunk->SetCollisionGroup( COLLISION_GROUP_DEBRIS );
  2753. // Set the velocity
  2754. Vector vecVelocity;
  2755. AngularImpulse angImpulse;
  2756. QAngle angles;
  2757. angles.x = random->RandomFloat( -70, 20 );
  2758. angles.y = random->RandomFloat( 0, 360 );
  2759. angles.z = 0.0f;
  2760. AngleVectors( angles, &vecVelocity );
  2761. vecVelocity *= random->RandomFloat( 550, 800 );
  2762. vecVelocity += pChopper->GetAbsVelocity();
  2763. angImpulse = RandomAngularImpulse( -180, 180 );
  2764. pChunk->SetAbsVelocity( vecVelocity );
  2765. if ( bSmall == false )
  2766. {
  2767. IPhysicsObject *pPhysicsObject = pChunk->VPhysicsInitNormal( SOLID_VPHYSICS, pChunk->GetSolidFlags(), false );
  2768. if ( pPhysicsObject )
  2769. {
  2770. pPhysicsObject->EnableMotion( true );
  2771. pPhysicsObject->SetVelocity(&vecVelocity, &angImpulse );
  2772. }
  2773. }
  2774. CFireTrail *pFireTrail = CFireTrail::CreateFireTrail();
  2775. if ( pFireTrail == NULL )
  2776. return;
  2777. pFireTrail->FollowEntity( pChunk, "" );
  2778. pFireTrail->SetParent( pChunk, 0 );
  2779. pFireTrail->SetLocalOrigin( vec3_origin );
  2780. pFireTrail->SetMoveType( MOVETYPE_NONE );
  2781. pFireTrail->SetLifetime( pChunk->m_lifeTime );
  2782. }
  2783. //------------------------------------------------------------------------------
  2784. // Pow!
  2785. //------------------------------------------------------------------------------
  2786. void CNPC_AttackHelicopter::ExplodeAndThrowChunk( const Vector &vecExplosionPos )
  2787. {
  2788. CEffectData data;
  2789. data.m_vOrigin = vecExplosionPos;
  2790. DispatchEffect( "HelicopterMegaBomb", data );
  2791. EmitSound( "BaseExplosionEffect.Sound" );
  2792. UTIL_ScreenShake( vecExplosionPos, 25.0, 150.0, 1.0, 750.0f, SHAKE_START );
  2793. if(GetCrashPoint() != NULL)
  2794. {
  2795. // Make it clear that I'm done for.
  2796. ExplosionCreate( vecExplosionPos, QAngle(0,0,1), this, 100, 128, false );
  2797. }
  2798. if ( random->RandomInt( 0, 4 ) )
  2799. {
  2800. for ( int i = 0; i < 2; i++ )
  2801. {
  2802. Chopper_CreateChunk( this, vecExplosionPos, RandomAngle(0, 360), g_PropDataSystem.GetRandomChunkModel( "MetalChunks" ), true );
  2803. }
  2804. }
  2805. else
  2806. {
  2807. Chopper_CreateChunk( this, vecExplosionPos, RandomAngle(0, 360), s_pChunkModelName[random->RandomInt( 0, CHOPPER_MAX_SMALL_CHUNKS - 1 )], false );
  2808. }
  2809. }
  2810. //-----------------------------------------------------------------------------
  2811. // Drop a corpse!
  2812. //-----------------------------------------------------------------------------
  2813. void CNPC_AttackHelicopter::DropCorpse( int nDamage )
  2814. {
  2815. // Don't drop another corpse if the next guy's not out on the gun yet
  2816. if ( m_flLastCorpseFall > gpGlobals->curtime )
  2817. return;
  2818. // Clamp damage to prevent ridiculous ragdoll velocity
  2819. if( nDamage > 250.0f )
  2820. nDamage = 250.0f;
  2821. m_flLastCorpseFall = gpGlobals->curtime + 3.0;
  2822. // Spawn a ragdoll combine guard
  2823. float forceScale = nDamage * 75 * 4;
  2824. Vector vecForceVector = RandomVector(-1,1);
  2825. vecForceVector.z = 0.5;
  2826. vecForceVector *= forceScale;
  2827. CBaseEntity *pGib = CreateRagGib( "models/combine_soldier.mdl", GetAbsOrigin(), GetAbsAngles(), vecForceVector );
  2828. if ( pGib )
  2829. {
  2830. pGib->SetOwnerEntity( this );
  2831. }
  2832. }
  2833. //-----------------------------------------------------------------------------
  2834. // Purpose:
  2835. //-----------------------------------------------------------------------------
  2836. void CNPC_AttackHelicopter::TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator )
  2837. {
  2838. // Take no damage from trace attacks unless it's blast damage. RadiusDamage() sometimes calls
  2839. // TraceAttack() as a means for delivering blast damage. Usually when the explosive penetrates
  2840. // the target. (RPG missiles do this sometimes).
  2841. if ( ( info.GetDamageType() & DMG_AIRBOAT ) ||
  2842. ( info.GetInflictor()->Classify() == CLASS_MISSILE ) ||
  2843. ( info.GetAttacker()->Classify() == CLASS_MISSILE ) )
  2844. {
  2845. BaseClass::BaseClass::TraceAttack( info, vecDir, ptr, pAccumulator );
  2846. }
  2847. }
  2848. //-----------------------------------------------------------------------------
  2849. // Purpose:
  2850. //-----------------------------------------------------------------------------
  2851. int CNPC_AttackHelicopter::OnTakeDamage( const CTakeDamageInfo &info )
  2852. {
  2853. // We don't take blast damage from anything but the airboat or missiles (or myself!)
  2854. if( info.GetInflictor() != this )
  2855. {
  2856. if ( ( ( info.GetDamageType() & DMG_AIRBOAT ) == 0 ) &&
  2857. ( info.GetInflictor()->Classify() != CLASS_MISSILE ) &&
  2858. ( info.GetAttacker()->Classify() != CLASS_MISSILE ) )
  2859. return 0;
  2860. }
  2861. if ( m_bIndestructible )
  2862. {
  2863. if ( GetHealth() < info.GetDamage() )
  2864. return 0;
  2865. }
  2866. // helicopter takes extra damage from its own grenades
  2867. CGrenadeHelicopter *pGren = dynamic_cast<CGrenadeHelicopter *>(info.GetInflictor());
  2868. if ( pGren && info.GetAttacker() && info.GetAttacker()->IsPlayer() )
  2869. {
  2870. CTakeDamageInfo fudgedInfo = info;
  2871. float damage;
  2872. if( g_pGameRules->IsSkillLevel(SKILL_EASY) )
  2873. {
  2874. damage = GetMaxHealth() / sk_helicopter_num_bombs1.GetFloat();
  2875. }
  2876. else if( g_pGameRules->IsSkillLevel(SKILL_HARD) )
  2877. {
  2878. damage = GetMaxHealth() / sk_helicopter_num_bombs3.GetFloat();
  2879. }
  2880. else // Medium, or unspecified
  2881. {
  2882. damage = GetMaxHealth() / sk_helicopter_num_bombs2.GetFloat();
  2883. }
  2884. damage = ceilf( damage );
  2885. fudgedInfo.SetDamage( damage );
  2886. fudgedInfo.SetMaxDamage( damage );
  2887. return BaseClass::OnTakeDamage( fudgedInfo );
  2888. }
  2889. return BaseClass::OnTakeDamage( info );
  2890. }
  2891. //-----------------------------------------------------------------------------
  2892. // Purpose: Take damage from trace attacks if they hit the gunner
  2893. //-----------------------------------------------------------------------------
  2894. int CNPC_AttackHelicopter::OnTakeDamage_Alive( const CTakeDamageInfo &info )
  2895. {
  2896. int nPrevHealth = GetHealth();
  2897. if ( ( info.GetInflictor() != NULL ) && ( info.GetInflictor()->GetOwnerEntity() != NULL ) && ( info.GetInflictor()->GetOwnerEntity() == this ) )
  2898. {
  2899. // Don't take damage from my own bombs. (Unless the player grabbed them and threw them back)
  2900. return 0;
  2901. }
  2902. // Chain
  2903. int nRetVal = BaseClass::OnTakeDamage_Alive( info );
  2904. if( info.GetDamageType() & DMG_BLAST )
  2905. {
  2906. // Apply a force push that makes us look like we're reacting to the damage
  2907. Vector damageDir = info.GetDamageForce();
  2908. VectorNormalize( damageDir );
  2909. ApplyAbsVelocityImpulse( damageDir * 500.0f );
  2910. // Knock the helicopter off of the level, too.
  2911. Vector vecRight, vecForce;
  2912. float flDot;
  2913. GetVectors( NULL, &vecRight, NULL );
  2914. vecForce = info.GetDamageForce();
  2915. VectorNormalize( vecForce );
  2916. flDot = DotProduct( vecForce, vecRight );
  2917. m_flGoalRollDmg = random->RandomFloat( 10, 30 );
  2918. if( flDot <= 0.0f )
  2919. {
  2920. // Missile hit the right side.
  2921. m_flGoalRollDmg *= -1;
  2922. }
  2923. }
  2924. // Spawn damage effects
  2925. if ( nPrevHealth != GetHealth() )
  2926. {
  2927. // Give the badly damaged call to say we're going to mega bomb soon
  2928. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  2929. {
  2930. if (( nPrevHealth > m_flNextMegaBombHealth ) && (GetHealth() <= m_flNextMegaBombHealth) )
  2931. {
  2932. EmitSound( "NPC_AttackHelicopter.BadlyDamagedAlert" );
  2933. }
  2934. }
  2935. if ( ShouldTriggerDamageEffect( nPrevHealth, MAX_SMOKE_TRAILS ) )
  2936. {
  2937. AddSmokeTrail( info.GetDamagePosition() );
  2938. }
  2939. if ( ShouldTriggerDamageEffect( nPrevHealth, MAX_CORPSES ) )
  2940. {
  2941. if ( nPrevHealth != GetMaxHealth() )
  2942. {
  2943. DropCorpse( info.GetDamage() );
  2944. }
  2945. }
  2946. if ( ShouldTriggerDamageEffect( nPrevHealth, MAX_EXPLOSIONS ) )
  2947. {
  2948. ExplodeAndThrowChunk( info.GetDamagePosition() );
  2949. }
  2950. int nPrevPercent = (int)(100.0f * nPrevHealth / GetMaxHealth());
  2951. int nCurrPercent = (int)(100.0f * GetHealth() / GetMaxHealth());
  2952. if (( (nPrevPercent + 9) / 10 ) != ( (nCurrPercent + 9) / 10 ))
  2953. {
  2954. m_OnHealthChanged.Set( nCurrPercent, this, this );
  2955. }
  2956. }
  2957. return nRetVal;
  2958. }
  2959. //-----------------------------------------------------------------------------
  2960. // Purpose:
  2961. //-----------------------------------------------------------------------------
  2962. void Chopper_BecomeChunks( CBaseEntity *pChopper )
  2963. {
  2964. QAngle vecChunkAngles = pChopper->GetAbsAngles();
  2965. Vector vecForward, vecUp;
  2966. pChopper->GetVectors( &vecForward, NULL, &vecUp );
  2967. #ifdef HL2_EPISODIC
  2968. CNPC_AttackHelicopter *pAttackHelicopter;
  2969. pAttackHelicopter = dynamic_cast<CNPC_AttackHelicopter*>(pChopper);
  2970. if( pAttackHelicopter != NULL )
  2971. {
  2972. // New for EP2, we may be tailspinning, (crashing) and playing an animation that is spinning
  2973. // our root bone, which means our model is not facing the way our entity is facing. So we have
  2974. // to do some attachment point math to get the proper angles to use for computing the relative
  2975. // positions of the gibs. The attachment points called DAMAGE0 is properly oriented and attached
  2976. // to the chopper body so we can use its angles.
  2977. int iAttach = pAttackHelicopter->LookupAttachment( "damage0" );
  2978. Vector vecAttachPos;
  2979. if( iAttach > -1 )
  2980. {
  2981. pAttackHelicopter->GetAttachment(iAttach, vecAttachPos, vecChunkAngles );
  2982. AngleVectors( vecChunkAngles, &vecForward, NULL, &vecUp );
  2983. }
  2984. }
  2985. #endif//HL2_EPISODIC
  2986. Vector vecChunkPos = pChopper->GetAbsOrigin();
  2987. Vector vecRight(0,0,0);
  2988. if( hl2_episodic.GetBool() )
  2989. {
  2990. // We need to get a right hand vector to toss the cockpit and tail pieces
  2991. // so their motion looks like a continuation of the tailspin animation
  2992. // that the chopper plays before crashing.
  2993. pChopper->GetVectors( NULL, &vecRight, NULL );
  2994. }
  2995. // Body
  2996. CHelicopterChunk *pBodyChunk = CHelicopterChunk::CreateHelicopterChunk( vecChunkPos, vecChunkAngles, pChopper->GetAbsVelocity(), HELICOPTER_CHUNK_BODY, CHUNK_BODY );
  2997. Chopper_CreateChunk( pChopper, vecChunkPos, RandomAngle( 0, 360 ), s_pChunkModelName[random->RandomInt( 0, CHOPPER_MAX_CHUNKS - 1 )], false );
  2998. vecChunkPos = pChopper->GetAbsOrigin() + ( vecForward * 100.0f ) + ( vecUp * -38.0f );
  2999. // Cockpit
  3000. CHelicopterChunk *pCockpitChunk = CHelicopterChunk::CreateHelicopterChunk( vecChunkPos, vecChunkAngles, pChopper->GetAbsVelocity() + vecRight * -800.0f, HELICOPTER_CHUNK_COCKPIT, CHUNK_COCKPIT );
  3001. Chopper_CreateChunk( pChopper, vecChunkPos, RandomAngle( 0, 360 ), s_pChunkModelName[random->RandomInt( 0, CHOPPER_MAX_CHUNKS - 1 )], false );
  3002. pCockpitChunk->m_hMaster = pBodyChunk;
  3003. vecChunkPos = pChopper->GetAbsOrigin() + ( vecForward * -175.0f );
  3004. // Tail
  3005. CHelicopterChunk *pTailChunk = CHelicopterChunk::CreateHelicopterChunk( vecChunkPos, vecChunkAngles, pChopper->GetAbsVelocity() + vecRight * 800.0f, HELICOPTER_CHUNK_TAIL, CHUNK_TAIL );
  3006. Chopper_CreateChunk( pChopper, vecChunkPos, RandomAngle( 0, 360 ), s_pChunkModelName[random->RandomInt( 0, CHOPPER_MAX_CHUNKS - 1 )], false );
  3007. pTailChunk->m_hMaster = pBodyChunk;
  3008. // Constrain all the pieces together loosely
  3009. IPhysicsObject *pBodyObject = pBodyChunk->VPhysicsGetObject();
  3010. Assert( pBodyObject );
  3011. IPhysicsObject *pCockpitObject = pCockpitChunk->VPhysicsGetObject();
  3012. Assert( pCockpitObject );
  3013. IPhysicsObject *pTailObject = pTailChunk->VPhysicsGetObject();
  3014. Assert( pTailObject );
  3015. IPhysicsConstraintGroup *pGroup = NULL;
  3016. // Create the constraint
  3017. constraint_fixedparams_t fixed;
  3018. fixed.Defaults();
  3019. fixed.InitWithCurrentObjectState( pBodyObject, pTailObject );
  3020. fixed.constraint.Defaults();
  3021. pBodyChunk->m_pTailConstraint = physenv->CreateFixedConstraint( pBodyObject, pTailObject, pGroup, fixed );
  3022. fixed.Defaults();
  3023. fixed.InitWithCurrentObjectState( pBodyObject, pCockpitObject );
  3024. fixed.constraint.Defaults();
  3025. pBodyChunk->m_pCockpitConstraint = physenv->CreateFixedConstraint( pBodyObject, pCockpitObject, pGroup, fixed );
  3026. }
  3027. //-----------------------------------------------------------------------------
  3028. // Purpose: Start us crashing
  3029. //-----------------------------------------------------------------------------
  3030. void CNPC_AttackHelicopter::Event_Killed( const CTakeDamageInfo &info )
  3031. {
  3032. if( m_lifeState == LIFE_ALIVE )
  3033. {
  3034. m_OnShotDown.FireOutput( this, this );
  3035. }
  3036. m_lifeState = LIFE_DYING;
  3037. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  3038. controller.SoundChangeVolume( m_pGunFiringSound, 0.0, 0.1f );
  3039. if( GetCrashPoint() == NULL )
  3040. {
  3041. CBaseEntity *pCrashPoint = gEntList.FindEntityByClassname( NULL, "info_target_helicopter_crash" );
  3042. if( pCrashPoint != NULL )
  3043. {
  3044. m_hCrashPoint.Set( pCrashPoint );
  3045. SetDesiredPosition( pCrashPoint->GetAbsOrigin() );
  3046. // Start the failing engine sound
  3047. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  3048. controller.SoundDestroy( m_pRotorSound );
  3049. CPASAttenuationFilter filter( this );
  3050. m_pRotorSound = controller.SoundCreate( filter, entindex(), "NPC_AttackHelicopter.EngineFailure" );
  3051. controller.Play( m_pRotorSound, 1.0, 100 );
  3052. // Tailspin!!
  3053. SetActivity( ACT_HELICOPTER_CRASHING );
  3054. // Intentionally returning with m_lifeState set to LIFE_DYING
  3055. return;
  3056. }
  3057. }
  3058. Chopper_BecomeChunks( this );
  3059. StopLoopingSounds();
  3060. m_lifeState = LIFE_DEAD;
  3061. EmitSound( "NPC_CombineGunship.Explode" );
  3062. SetThink( &CNPC_AttackHelicopter::SUB_Remove );
  3063. SetNextThink( gpGlobals->curtime + 0.1f );
  3064. AddEffects( EF_NODRAW );
  3065. // Makes the slower rotors fade back in
  3066. SetStartupTime( gpGlobals->curtime + 99.0f );
  3067. m_iHealth = 0;
  3068. m_takedamage = DAMAGE_NO;
  3069. m_OnDeath.FireOutput( info.GetAttacker(), this );
  3070. }
  3071. //------------------------------------------------------------------------------
  3072. // Creates the breakable husk of an attack chopper
  3073. //------------------------------------------------------------------------------
  3074. void CNPC_AttackHelicopter::CreateChopperHusk()
  3075. {
  3076. // We're embedded into the ground
  3077. CBaseEntity *pCorpse = CreateEntityByName( "prop_physics" );
  3078. pCorpse->SetAbsOrigin( GetAbsOrigin() );
  3079. pCorpse->SetAbsAngles( GetAbsAngles() );
  3080. pCorpse->SetModel( CHOPPER_MODEL_CORPSE_NAME );
  3081. pCorpse->AddSpawnFlags( SF_PHYSPROP_MOTIONDISABLED );
  3082. pCorpse->Spawn();
  3083. pCorpse->SetMoveType( MOVETYPE_NONE );
  3084. }
  3085. //-----------------------------------------------------------------------------
  3086. // Think!
  3087. //-----------------------------------------------------------------------------
  3088. void CNPC_AttackHelicopter::PrescheduleThink( void )
  3089. {
  3090. if ( m_flGoalRollDmg != 0.0f )
  3091. {
  3092. m_flGoalRollDmg = UTIL_Approach( 0, m_flGoalRollDmg, 2.0f );
  3093. }
  3094. switch( m_lifeState )
  3095. {
  3096. case LIFE_DYING:
  3097. {
  3098. if( GetCrashPoint() != NULL )
  3099. {
  3100. // Stay on this, no matter what.
  3101. SetDesiredPosition( GetCrashPoint()->WorldSpaceCenter() );
  3102. }
  3103. if ( random->RandomInt( 0, 4 ) == 0 )
  3104. {
  3105. Vector explodePoint;
  3106. CollisionProp()->RandomPointInBounds( Vector(0.25,0.25,0.25), Vector(0.75,0.75,0.75), &explodePoint );
  3107. ExplodeAndThrowChunk( explodePoint );
  3108. }
  3109. }
  3110. break;
  3111. }
  3112. BaseClass::PrescheduleThink();
  3113. }
  3114. //-----------------------------------------------------------------------------
  3115. // Purpose:
  3116. //-----------------------------------------------------------------------------
  3117. float CNPC_AttackHelicopter::UpdatePerpPathDistance( float flMaxPathOffset )
  3118. {
  3119. if ( !IsLeading() || !GetEnemy() )
  3120. {
  3121. m_flCurrPathOffset = 0.0f;
  3122. return 0.0f;
  3123. }
  3124. float flNewPathOffset = TargetDistanceToPath();
  3125. // Make bomb dropping more interesting
  3126. if ( ShouldDropBombs() )
  3127. {
  3128. float flSpeedAlongPath = TargetSpeedAlongPath();
  3129. if ( flSpeedAlongPath > 10.0f )
  3130. {
  3131. float flLeadTime = GetLeadingDistance() / flSpeedAlongPath;
  3132. flLeadTime = clamp( flLeadTime, 0.0f, 2.0f );
  3133. flNewPathOffset += 0.25 * flLeadTime * TargetSpeedAcrossPath();
  3134. }
  3135. flSpeedAlongPath = clamp( flSpeedAlongPath, 100.0f, 500.0f );
  3136. float flSinHeight = SimpleSplineRemapVal( flSpeedAlongPath, 100.0f, 500.0f, 0.0f, 200.0f );
  3137. flNewPathOffset += flSinHeight * sin( 2.0f * M_PI * (gpGlobals->curtime / 6.0f) );
  3138. }
  3139. if ( (flMaxPathOffset != 0.0f) && (flNewPathOffset > flMaxPathOffset) )
  3140. {
  3141. flNewPathOffset = flMaxPathOffset;
  3142. }
  3143. float flMaxChange = 1000.0f * (gpGlobals->curtime - GetLastThink());
  3144. if ( fabs( flNewPathOffset - m_flCurrPathOffset ) < flMaxChange )
  3145. {
  3146. m_flCurrPathOffset = flNewPathOffset;
  3147. }
  3148. else
  3149. {
  3150. float flSign = (m_flCurrPathOffset < flNewPathOffset) ? 1.0f : -1.0f;
  3151. m_flCurrPathOffset += flSign * flMaxChange;
  3152. }
  3153. return m_flCurrPathOffset;
  3154. }
  3155. //-----------------------------------------------------------------------------
  3156. // Computes the max speed + acceleration:
  3157. //-----------------------------------------------------------------------------
  3158. void CNPC_AttackHelicopter::GetMaxSpeedAndAccel( float *pMaxSpeed, float *pAccelRate )
  3159. {
  3160. *pAccelRate = CHOPPER_ACCEL_RATE;
  3161. *pMaxSpeed = GetMaxSpeed();
  3162. if ( GetEnemyVehicle() )
  3163. {
  3164. *pAccelRate *= 9.0f;
  3165. }
  3166. }
  3167. //-----------------------------------------------------------------------------
  3168. // Computes the acceleration:
  3169. //-----------------------------------------------------------------------------
  3170. #define HELICOPTER_GRAVITY 384
  3171. #define HELICOPTER_DT 0.1f
  3172. #define HELICOPTER_MIN_DZ_DAMP -500.0f
  3173. #define HELICOPTER_MAX_DZ_DAMP -1000.0f
  3174. #define HELICOPTER_FORCE_BLEND 0.8f
  3175. #define HELICOPTER_FORCE_BLEND_VEHICLE 0.2f
  3176. void CNPC_AttackHelicopter::ComputeVelocity( const Vector &vecTargetPosition,
  3177. float flAdditionalHeight, float flMinDistFromSegment, float flMaxDistFromSegment, Vector *pVecAccel )
  3178. {
  3179. Vector deltaPos;
  3180. VectorSubtract( vecTargetPosition, GetAbsOrigin(), deltaPos );
  3181. // calc goal linear accel to hit deltaPos in dt time.
  3182. // This is solving the equation xf = 0.5 * a * dt^2 + vo * dt + xo
  3183. float dt = 1.0f;
  3184. pVecAccel->x = 2.0f * (deltaPos.x - GetAbsVelocity().x * dt) / (dt * dt);
  3185. pVecAccel->y = 2.0f * (deltaPos.y - GetAbsVelocity().y * dt) / (dt * dt);
  3186. pVecAccel->z = 2.0f * (deltaPos.z - GetAbsVelocity().z * dt) / (dt * dt) + HELICOPTER_GRAVITY;
  3187. float flDistFromPath = 0.0f;
  3188. Vector vecPoint, vecDelta;
  3189. if ( flMaxDistFromSegment != 0.0f )
  3190. {
  3191. // Also, add in a little force to get us closer to our current line segment if we can
  3192. ClosestPointToCurrentPath( &vecPoint );
  3193. if ( flAdditionalHeight != 0.0f )
  3194. {
  3195. Vector vecEndPoint, vecClosest;
  3196. vecEndPoint = vecPoint;
  3197. vecEndPoint.z += flAdditionalHeight;
  3198. CalcClosestPointOnLineSegment( GetAbsOrigin(), vecPoint, vecEndPoint, vecClosest );
  3199. vecPoint = vecClosest;
  3200. }
  3201. VectorSubtract( vecPoint, GetAbsOrigin(), vecDelta );
  3202. flDistFromPath = VectorNormalize( vecDelta );
  3203. if ( flDistFromPath > flMaxDistFromSegment )
  3204. {
  3205. // Strongly constrain to an n unit pipe around the current path
  3206. // by damping out all impulse forces that would push us further from the pipe
  3207. float flAmount = (flDistFromPath - flMaxDistFromSegment) / 200.0f;
  3208. flAmount = clamp( flAmount, 0, 1 );
  3209. VectorMA( *pVecAccel, flAmount * 200.0f, vecDelta, *pVecAccel );
  3210. }
  3211. }
  3212. // Apply avoidance forces
  3213. if ( !HasSpawnFlags( SF_HELICOPTER_IGNORE_AVOID_FORCES ) )
  3214. {
  3215. Vector vecAvoidForce;
  3216. CAvoidSphere::ComputeAvoidanceForces( this, 350.0f, 2.0f, &vecAvoidForce );
  3217. *pVecAccel += vecAvoidForce;
  3218. CAvoidBox::ComputeAvoidanceForces( this, 350.0f, 2.0f, &vecAvoidForce );
  3219. *pVecAccel += vecAvoidForce;
  3220. }
  3221. // don't fall faster than 0.2G or climb faster than 2G
  3222. pVecAccel->z = clamp( pVecAccel->z, HELICOPTER_GRAVITY * 0.2f, HELICOPTER_GRAVITY * 2.0f );
  3223. // The lift factor owing to horizontal movement
  3224. float flHorizLiftFactor = fabs( pVecAccel->x ) * 0.10f + fabs( pVecAccel->y ) * 0.10f;
  3225. // If we're way above the path, dampen horizontal lift factor
  3226. float flNewHorizLiftFactor = clamp( deltaPos.z, HELICOPTER_MAX_DZ_DAMP, HELICOPTER_MIN_DZ_DAMP );
  3227. flNewHorizLiftFactor = SimpleSplineRemapVal( flNewHorizLiftFactor, HELICOPTER_MIN_DZ_DAMP, HELICOPTER_MAX_DZ_DAMP, flHorizLiftFactor, 2.5f * (HELICOPTER_GRAVITY * 0.2) );
  3228. float flDampening = (flNewHorizLiftFactor != 0.0f) ? (flNewHorizLiftFactor / flHorizLiftFactor) : 1.0f;
  3229. if ( flDampening < 1.0f )
  3230. {
  3231. pVecAccel->x *= flDampening;
  3232. pVecAccel->y *= flDampening;
  3233. flHorizLiftFactor = flNewHorizLiftFactor;
  3234. }
  3235. Vector forward, right, up;
  3236. GetVectors( &forward, &right, &up );
  3237. // First, attenuate the current force
  3238. float flForceBlend = GetEnemyVehicle() ? HELICOPTER_FORCE_BLEND_VEHICLE : HELICOPTER_FORCE_BLEND;
  3239. m_flForce *= flForceBlend;
  3240. // Now add force based on our acceleration factors
  3241. m_flForce += ( pVecAccel->z + flHorizLiftFactor ) * HELICOPTER_DT * (1.0f - flForceBlend);
  3242. // The force is always *locally* upward based; we pitch + roll the chopper to get movement
  3243. Vector vecImpulse;
  3244. VectorMultiply( up, m_flForce, vecImpulse );
  3245. // NOTE: These have to be done *before* the additional path distance drag forces are applied below
  3246. ApplySidewaysDrag( right );
  3247. ApplyGeneralDrag();
  3248. // If LIFE_DYING, maintain control as long as we're flying to a crash point.
  3249. if ( m_lifeState != LIFE_DYING || (m_lifeState == LIFE_DYING && GetCrashPoint() != NULL) )
  3250. {
  3251. vecImpulse.z += -HELICOPTER_GRAVITY * HELICOPTER_DT;
  3252. if ( flMinDistFromSegment != 0.0f && ( flDistFromPath > flMinDistFromSegment ) )
  3253. {
  3254. Vector vecVelDir = GetAbsVelocity();
  3255. // Strongly constrain to an n unit pipe around the current path
  3256. // by damping out all impulse forces that would push us further from the pipe
  3257. float flDot = DotProduct( vecImpulse, vecDelta );
  3258. if ( flDot < 0.0f )
  3259. {
  3260. VectorMA( vecImpulse, -flDot * 0.1f, vecDelta, vecImpulse );
  3261. }
  3262. // Also apply an extra impulse to compensate for the current velocity
  3263. flDot = DotProduct( vecVelDir, vecDelta );
  3264. if ( flDot < 0.0f )
  3265. {
  3266. VectorMA( vecImpulse, -flDot * 0.1f, vecDelta, vecImpulse );
  3267. }
  3268. }
  3269. }
  3270. else
  3271. {
  3272. // No more upward lift...
  3273. vecImpulse.z = -HELICOPTER_GRAVITY * HELICOPTER_DT;
  3274. // Damp the horizontal impulses; we should pretty much be falling ballistically
  3275. vecImpulse.x *= 0.1f;
  3276. vecImpulse.y *= 0.1f;
  3277. }
  3278. // Add in our velocity pulse for this frame
  3279. ApplyAbsVelocityImpulse( vecImpulse );
  3280. }
  3281. //-----------------------------------------------------------------------------
  3282. // Computes the max speed + acceleration:
  3283. //-----------------------------------------------------------------------------
  3284. void CNPC_AttackHelicopter::ComputeAngularVelocity( const Vector &vecGoalUp, const Vector &vecFacingDirection )
  3285. {
  3286. QAngle goalAngAccel;
  3287. if ( m_lifeState != LIFE_DYING || (m_lifeState == LIFE_DYING && GetCrashPoint() != NULL) )
  3288. {
  3289. Vector forward, right, up;
  3290. GetVectors( &forward, &right, &up );
  3291. Vector goalUp = vecGoalUp;
  3292. VectorNormalize( goalUp );
  3293. // calc goal orientation to hit linear accel forces
  3294. float goalPitch = RAD2DEG( asin( DotProduct( forward, goalUp ) ) );
  3295. float goalYaw = UTIL_VecToYaw( vecFacingDirection );
  3296. float goalRoll = RAD2DEG( asin( DotProduct( right, goalUp ) ) + m_flGoalRollDmg );
  3297. goalPitch *= 0.75f;
  3298. // clamp goal orientations
  3299. goalPitch = clamp( goalPitch, -30, 45 );
  3300. goalRoll = clamp( goalRoll, -45, 45 );
  3301. // calc angular accel needed to hit goal pitch in dt time.
  3302. float dt = 0.6;
  3303. goalAngAccel.x = 2.0 * (AngleDiff( goalPitch, AngleNormalize( GetAbsAngles().x ) ) - GetLocalAngularVelocity().x * dt) / (dt * dt);
  3304. goalAngAccel.y = 2.0 * (AngleDiff( goalYaw, AngleNormalize( GetAbsAngles().y ) ) - GetLocalAngularVelocity().y * dt) / (dt * dt);
  3305. goalAngAccel.z = 2.0 * (AngleDiff( goalRoll, AngleNormalize( GetAbsAngles().z ) ) - GetLocalAngularVelocity().z * dt) / (dt * dt);
  3306. goalAngAccel.x = clamp( goalAngAccel.x, -300, 300 );
  3307. //goalAngAccel.y = clamp( goalAngAccel.y, -60, 60 );
  3308. goalAngAccel.y = clamp( goalAngAccel.y, -120, 120 );
  3309. goalAngAccel.z = clamp( goalAngAccel.z, -300, 300 );
  3310. }
  3311. else
  3312. {
  3313. goalAngAccel.x = 0;
  3314. goalAngAccel.y = random->RandomFloat( 50, 120 );
  3315. goalAngAccel.z = 0;
  3316. }
  3317. // limit angular accel changes to similate mechanical response times
  3318. QAngle angAccelAccel;
  3319. float dt = 0.1;
  3320. angAccelAccel.x = (goalAngAccel.x - m_vecAngAcceleration.x) / dt;
  3321. angAccelAccel.y = (goalAngAccel.y - m_vecAngAcceleration.y) / dt;
  3322. angAccelAccel.z = (goalAngAccel.z - m_vecAngAcceleration.z) / dt;
  3323. angAccelAccel.x = clamp( angAccelAccel.x, -1000, 1000 );
  3324. angAccelAccel.y = clamp( angAccelAccel.y, -1000, 1000 );
  3325. angAccelAccel.z = clamp( angAccelAccel.z, -1000, 1000 );
  3326. // DevMsg( "pitch %6.1f (%6.1f:%6.1f) ", goalPitch, GetLocalAngles().x, m_vecAngVelocity.x );
  3327. // DevMsg( "roll %6.1f (%6.1f:%6.1f) : ", goalRoll, GetLocalAngles().z, m_vecAngVelocity.z );
  3328. // DevMsg( "%6.1f %6.1f %6.1f : ", goalAngAccel.x, goalAngAccel.y, goalAngAccel.z );
  3329. // DevMsg( "%6.0f %6.0f %6.0f\n", angAccelAccel.x, angAccelAccel.y, angAccelAccel.z );
  3330. m_vecAngAcceleration += angAccelAccel * 0.1;
  3331. QAngle angVel = GetLocalAngularVelocity();
  3332. angVel += m_vecAngAcceleration * 0.1;
  3333. angVel.y = clamp( angVel.y, -120, 120 );
  3334. // Fix up pitch and yaw to tend toward small values
  3335. if ( m_lifeState == LIFE_DYING && GetCrashPoint() == NULL )
  3336. {
  3337. float flPitchDiff = random->RandomFloat( -5, 5 ) - GetAbsAngles().x;
  3338. angVel.x = flPitchDiff * 0.1f;
  3339. float flRollDiff = random->RandomFloat( -5, 5 ) - GetAbsAngles().z;
  3340. angVel.z = flRollDiff * 0.1f;
  3341. }
  3342. SetLocalAngularVelocity( angVel );
  3343. float flAmt = clamp( angVel.y, -30, 30 );
  3344. float flRudderPose = RemapVal( flAmt, -30, 30, 45, -45 );
  3345. SetPoseParameter( "rudder", flRudderPose );
  3346. }
  3347. //-----------------------------------------------------------------------------
  3348. // Purpose:
  3349. //-----------------------------------------------------------------------------
  3350. void CNPC_AttackHelicopter::FlightDirectlyOverhead( void )
  3351. {
  3352. Vector vecTargetPosition = m_vecTargetPosition;
  3353. CBaseEntity *pEnemy = GetEnemy();
  3354. if ( HasEnemy() && FVisible( pEnemy ) )
  3355. {
  3356. if ( GetEnemy()->IsPlayer() )
  3357. {
  3358. CBaseEntity *pEnemyVehicle = assert_cast<CBasePlayer*>(GetEnemy())->GetVehicleEntity();
  3359. if ( pEnemyVehicle )
  3360. {
  3361. Vector vecEnemyVel = pEnemyVehicle->GetSmoothedVelocity();
  3362. Vector vecRelativePosition;
  3363. VectorSubtract( GetAbsOrigin(), pEnemyVehicle->GetAbsOrigin(), vecRelativePosition );
  3364. float flDist = VectorNormalize( vecRelativePosition );
  3365. float flEnemySpeed = VectorNormalize( vecEnemyVel );
  3366. float flDot = DotProduct( vecRelativePosition, vecEnemyVel );
  3367. float flSpeed = GetMaxSpeed() * 0.3f; //GetAbsVelocity().Length();
  3368. float a = flSpeed * flSpeed - flEnemySpeed * flEnemySpeed;
  3369. float b = 2.0f * flEnemySpeed * flDist * flDot;
  3370. float c = - flDist * flDist;
  3371. float flDiscrim = b * b - 4 * a * c;
  3372. if ( flDiscrim >= 0 )
  3373. {
  3374. float t = ( -b + sqrt( flDiscrim ) ) / (2 * a);
  3375. t = clamp( t, 0.0f, 4.0f );
  3376. VectorMA( pEnemyVehicle->GetAbsOrigin(), t * flEnemySpeed, vecEnemyVel, vecTargetPosition );
  3377. }
  3378. }
  3379. }
  3380. }
  3381. // if ( GetCurrentPathTargetPosition() )
  3382. // {
  3383. // vecTargetPosition.z = GetCurrentPathTargetPosition()->z;
  3384. // }
  3385. NDebugOverlay::Cross3D( vecTargetPosition, -Vector(32,32,32), Vector(32,32,32), 0, 0, 255, true, 0.1f );
  3386. UpdateFacingDirection( vecTargetPosition );
  3387. Vector accel;
  3388. ComputeVelocity( vecTargetPosition, 0.0f, 0.0f, 0.0f, &accel );
  3389. ComputeAngularVelocity( accel, m_vecDesiredFaceDir );
  3390. }
  3391. //-----------------------------------------------------------------------------
  3392. // Purpose:
  3393. //-----------------------------------------------------------------------------
  3394. void CNPC_AttackHelicopter::Flight( void )
  3395. {
  3396. if( GetFlags() & FL_ONGROUND )
  3397. {
  3398. // This would be really bad.
  3399. SetGroundEntity( NULL );
  3400. }
  3401. // Determine the distances we must lie from the path
  3402. float flMaxPathOffset = MaxDistanceFromCurrentPath();
  3403. float flPerpDist = UpdatePerpPathDistance( flMaxPathOffset );
  3404. float flMinDistFromSegment, flMaxDistFromSegment;
  3405. if ( !IsLeading() )
  3406. {
  3407. flMinDistFromSegment = 0.0f;
  3408. flMaxDistFromSegment = 0.0f;
  3409. }
  3410. else
  3411. {
  3412. flMinDistFromSegment = fabs(flPerpDist) + 100.0f;
  3413. flMaxDistFromSegment = fabs(flPerpDist) + 200.0f;
  3414. if ( flMaxPathOffset != 0.0 )
  3415. {
  3416. if ( flMaxDistFromSegment > flMaxPathOffset - 100.0f )
  3417. {
  3418. flMaxDistFromSegment = flMaxPathOffset - 100.0f;
  3419. }
  3420. if ( flMinDistFromSegment > flMaxPathOffset - 200.0f )
  3421. {
  3422. flMinDistFromSegment = flMaxPathOffset - 200.0f;
  3423. }
  3424. }
  3425. }
  3426. float maxSpeed, accelRate;
  3427. GetMaxSpeedAndAccel( &maxSpeed, &accelRate );
  3428. Vector vecTargetPosition;
  3429. float flCurrentSpeed = GetAbsVelocity().Length();
  3430. float flDist = MIN( flCurrentSpeed + accelRate, maxSpeed );
  3431. float dt = 1.0f;
  3432. ComputeActualTargetPosition( flDist, dt, flPerpDist, &vecTargetPosition );
  3433. // Raise high in the air when doing the shooting attack
  3434. float flAdditionalHeight = 0.0f;
  3435. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  3436. {
  3437. flAdditionalHeight = clamp( m_flBullrushAdditionalHeight, 0.0f, flMaxPathOffset );
  3438. vecTargetPosition.z += flAdditionalHeight;
  3439. }
  3440. Vector accel;
  3441. UpdateFacingDirection( vecTargetPosition );
  3442. ComputeVelocity( vecTargetPosition, flAdditionalHeight, flMinDistFromSegment, flMaxDistFromSegment, &accel );
  3443. ComputeAngularVelocity( accel, m_vecDesiredFaceDir );
  3444. }
  3445. //------------------------------------------------------------------------------
  3446. // Updates the facing direction
  3447. //------------------------------------------------------------------------------
  3448. void CNPC_AttackHelicopter::UpdateFacingDirection( const Vector &vecActualDesiredPosition )
  3449. {
  3450. bool bIsBullrushing = ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE );
  3451. bool bSeenTargetRecently = HasSpawnFlags( SF_HELICOPTER_AGGRESSIVE ) || ( m_flLastSeen + 5 > gpGlobals->curtime );
  3452. if ( GetEnemy() && !bIsBullrushing )
  3453. {
  3454. if ( !IsLeading() )
  3455. {
  3456. if( IsCarpetBombing() && hl2_episodic.GetBool() )
  3457. {
  3458. m_vecDesiredFaceDir = vecActualDesiredPosition - GetAbsOrigin();
  3459. }
  3460. else if ( !IsCrashing() && bSeenTargetRecently )
  3461. {
  3462. // If we've seen the target recently, face the target.
  3463. m_vecDesiredFaceDir = m_vecTargetPosition - GetAbsOrigin();
  3464. }
  3465. else
  3466. {
  3467. // Remain facing the way you were facing...
  3468. }
  3469. }
  3470. else
  3471. {
  3472. if ( ShouldDropBombs() || IsCarpetBombing() )
  3473. {
  3474. m_vecDesiredFaceDir = vecActualDesiredPosition - GetAbsOrigin();
  3475. }
  3476. else
  3477. {
  3478. m_vecDesiredFaceDir = m_vecTargetPosition - GetAbsOrigin();
  3479. }
  3480. }
  3481. }
  3482. else
  3483. {
  3484. // Face our desired position
  3485. float flDistSqr = vecActualDesiredPosition.AsVector2D().DistToSqr( GetAbsOrigin().AsVector2D() );
  3486. if ( flDistSqr <= 50 * 50 )
  3487. {
  3488. if (( flDistSqr > 1 * 1 ) && bSeenTargetRecently && IsInSecondaryMode( BULLRUSH_MODE_SHOOT_IDLE_PLAYER ) )
  3489. {
  3490. m_vecDesiredFaceDir = m_vecTargetPosition - GetAbsOrigin();
  3491. m_vecDesiredFaceDir.z = 0.0f;
  3492. }
  3493. else
  3494. {
  3495. GetVectors( &m_vecDesiredFaceDir, NULL, NULL );
  3496. }
  3497. }
  3498. else
  3499. {
  3500. m_vecDesiredFaceDir = vecActualDesiredPosition - GetAbsOrigin();
  3501. }
  3502. }
  3503. VectorNormalize( m_vecDesiredFaceDir );
  3504. }
  3505. //------------------------------------------------------------------------------
  3506. // Purpose :
  3507. //------------------------------------------------------------------------------
  3508. #define ENEMY_CREEP_RATE 400
  3509. float CNPC_AttackHelicopter::CreepTowardEnemy( float flSpeed, float flMinSpeed, float flMaxSpeed, float flMinDist, float flMaxDist )
  3510. {
  3511. float dt = gpGlobals->curtime - GetLastThink();
  3512. float flEnemyCreepDist = ENEMY_CREEP_RATE * dt;
  3513. // When the player is slow, creep toward him within a second or two
  3514. float flLeadingDist = ClampSplineRemapVal( flSpeed, flMinSpeed, flMaxSpeed, flMinDist, flMaxDist );
  3515. float flCurrentDist = GetLeadingDistance( );
  3516. if ( fabs(flLeadingDist - flCurrentDist) > flEnemyCreepDist )
  3517. {
  3518. float flSign = ( flLeadingDist < flCurrentDist ) ? -1.0f : 1.0f;
  3519. flLeadingDist = flCurrentDist + flSign * flEnemyCreepDist;
  3520. }
  3521. return flLeadingDist;
  3522. }
  3523. #define MIN_ENEMY_SPEED 300
  3524. //------------------------------------------------------------------------------
  3525. // Computes how far to lead the player when bombing
  3526. //------------------------------------------------------------------------------
  3527. float CNPC_AttackHelicopter::ComputeBombingLeadingDistance( float flSpeed, float flSpeedAlongPath, bool bEnemyInVehicle )
  3528. {
  3529. if ( ( flSpeed <= MIN_ENEMY_SPEED ) && bEnemyInVehicle )
  3530. {
  3531. return CreepTowardEnemy( flSpeed, 0.0f, MIN_ENEMY_SPEED, 0.0f, 1000.0f );
  3532. }
  3533. return ClampSplineRemapVal( flSpeedAlongPath, 200.0f, 600.0f, 1000.0f, 2000.0f );
  3534. }
  3535. //------------------------------------------------------------------------------
  3536. // Computes how far to lead the player when bullrushing
  3537. //------------------------------------------------------------------------------
  3538. float CNPC_AttackHelicopter::ComputeBullrushLeadingDistance( float flSpeed, float flSpeedAlongPath, bool bEnemyInVehicle )
  3539. {
  3540. switch ( m_nSecondaryMode )
  3541. {
  3542. case BULLRUSH_MODE_WAIT_FOR_ENEMY:
  3543. return 0.0f;
  3544. case BULLRUSH_MODE_GET_DISTANCE:
  3545. return m_bRushForward ? -CHOPPER_BULLRUSH_MODE_DISTANCE : CHOPPER_BULLRUSH_MODE_DISTANCE;
  3546. case BULLRUSH_MODE_DROP_BOMBS_FOLLOW_PLAYER:
  3547. // return m_bRushForward ? 1500.0f : -1500.0f;
  3548. return ComputeBombingLeadingDistance( flSpeed, flSpeedAlongPath, bEnemyInVehicle );
  3549. case BULLRUSH_MODE_SHOOT_IDLE_PLAYER:
  3550. return 0.0f;
  3551. case BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED:
  3552. return m_bRushForward ? 7000 : -7000;
  3553. case BULLRUSH_MODE_MEGA_BOMB:
  3554. return m_bRushForward ? CHOPPER_BULLRUSH_MODE_DISTANCE : -CHOPPER_BULLRUSH_MODE_DISTANCE;
  3555. case BULLRUSH_MODE_SHOOT_GUN:
  3556. {
  3557. float flLeadDistance = 1000.f - CHOPPER_BULLRUSH_ENEMY_BOMB_DISTANCE;
  3558. return m_bRushForward ? flLeadDistance : -flLeadDistance;
  3559. }
  3560. }
  3561. Assert(0);
  3562. return 0.0f;
  3563. }
  3564. //------------------------------------------------------------------------------
  3565. // Secondary mode
  3566. //------------------------------------------------------------------------------
  3567. inline void CNPC_AttackHelicopter::SetSecondaryMode( int nMode, bool bRetainTime )
  3568. {
  3569. m_nSecondaryMode = nMode;
  3570. if (!bRetainTime)
  3571. {
  3572. m_flSecondaryModeStartTime = gpGlobals->curtime;
  3573. }
  3574. }
  3575. inline bool CNPC_AttackHelicopter::IsInSecondaryMode( int nMode )
  3576. {
  3577. return m_nSecondaryMode == nMode;
  3578. }
  3579. inline float CNPC_AttackHelicopter::SecondaryModeTime( ) const
  3580. {
  3581. return gpGlobals->curtime - m_flSecondaryModeStartTime;
  3582. }
  3583. //------------------------------------------------------------------------------
  3584. // Switch to idle
  3585. //------------------------------------------------------------------------------
  3586. void CNPC_AttackHelicopter::SwitchToBullrushIdle( void )
  3587. {
  3588. // Put us directly into idle gun state (we're in firing state)
  3589. m_flNextAttack = gpGlobals->curtime;
  3590. m_nGunState = GUN_STATE_IDLE;
  3591. m_nRemainingBursts = 0;
  3592. m_flBullrushAdditionalHeight = 0.0f;
  3593. SetPauseState( PAUSE_NO_PAUSE );
  3594. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  3595. controller.SoundChangeVolume( m_pGunFiringSound, 0.0, 0.1f );
  3596. }
  3597. //------------------------------------------------------------------------------
  3598. // Should the chopper shoot the idle player?
  3599. //------------------------------------------------------------------------------
  3600. bool CNPC_AttackHelicopter::ShouldShootIdlePlayerInBullrush()
  3601. {
  3602. // Once he starts shooting, then don't stop until the player is moving pretty fast
  3603. float flSpeedSqr = IsInSecondaryMode( BULLRUSH_MODE_SHOOT_IDLE_PLAYER ) ? CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED_2_SQ : CHOPPER_BULLRUSH_SLOW_SHOOT_SPEED_SQ;
  3604. return ( GetEnemy() && GetEnemy()->GetSmoothedVelocity().LengthSqr() <= flSpeedSqr );
  3605. }
  3606. //------------------------------------------------------------------------------
  3607. // Shutdown shooting during bullrush
  3608. //------------------------------------------------------------------------------
  3609. void CNPC_AttackHelicopter::ShutdownGunDuringBullrush( )
  3610. {
  3611. // Put us directly into idle gun state (we're in firing state)
  3612. m_flNextAttack = gpGlobals->curtime;
  3613. m_nGunState = GUN_STATE_IDLE;
  3614. m_nRemainingBursts = 0;
  3615. SetPauseState( PAUSE_NO_PAUSE );
  3616. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  3617. controller.SoundChangeVolume( m_pGunFiringSound, 0.0, 0.1f );
  3618. }
  3619. #define HELICOPTER_MIN_IDLE_BOMBING_DIST 350.0f
  3620. #define HELICOPTER_MIN_IDLE_BOMBING_SPEED 350.0f
  3621. //-----------------------------------------------------------------------------
  3622. // Purpose:
  3623. // Output : Returns true on success, false on failure.
  3624. //-----------------------------------------------------------------------------
  3625. bool CNPC_AttackHelicopter::ShouldBombIdlePlayer( void )
  3626. {
  3627. // Must be settled over a position and not moving too quickly to do this
  3628. if ( GetAbsVelocity().LengthSqr() > Square(HELICOPTER_MIN_IDLE_BOMBING_SPEED) )
  3629. return false;
  3630. // Must be within a certain range of the target
  3631. float flDistToTargetSqr = (GetEnemy()->WorldSpaceCenter() - GetAbsOrigin()).Length2DSqr();
  3632. if ( flDistToTargetSqr < Square(HELICOPTER_MIN_IDLE_BOMBING_DIST) )
  3633. return true;
  3634. // Can't bomb this
  3635. return false;
  3636. }
  3637. //------------------------------------------------------------------------------
  3638. // Update the bullrush state
  3639. //------------------------------------------------------------------------------
  3640. #define BULLRUSH_GOAL_TOLERANCE 200
  3641. #define BULLRUSH_BOMB_MAX_DISTANCE 3500
  3642. void CNPC_AttackHelicopter::UpdateBullrushState( void )
  3643. {
  3644. if ( !GetEnemy() || IsInForcedMove() )
  3645. {
  3646. if ( !IsInSecondaryMode( BULLRUSH_MODE_WAIT_FOR_ENEMY ) )
  3647. {
  3648. SwitchToBullrushIdle();
  3649. SetSecondaryMode( BULLRUSH_MODE_WAIT_FOR_ENEMY );
  3650. }
  3651. }
  3652. switch( m_nSecondaryMode )
  3653. {
  3654. case BULLRUSH_MODE_WAIT_FOR_ENEMY:
  3655. {
  3656. m_flBullrushAdditionalHeight = CHOPPER_BULLRUSH_SHOOTING_VERTICAL_OFFSET;
  3657. if ( GetEnemy() && !IsInForcedMove() )
  3658. {
  3659. // This forces us to not start trying checking positions
  3660. // until we have been on the path for a little while
  3661. if ( SecondaryModeTime() > 0.3f )
  3662. {
  3663. float flDistanceToGoal = ComputeDistanceToTargetPosition();
  3664. Vector vecPathDir;
  3665. CurrentPathDirection( &vecPathDir );
  3666. bool bMovingForward = DotProduct2D( GetAbsVelocity().AsVector2D(), vecPathDir.AsVector2D() ) >= 0.0f;
  3667. if ( flDistanceToGoal * (bMovingForward ? 1.0f : -1.0f) > 1000 )
  3668. {
  3669. m_bRushForward = bMovingForward;
  3670. SetSecondaryMode( BULLRUSH_MODE_SHOOT_GUN );
  3671. SpotlightStartup();
  3672. }
  3673. else
  3674. {
  3675. m_bRushForward = !bMovingForward;
  3676. SetSecondaryMode( BULLRUSH_MODE_GET_DISTANCE );
  3677. }
  3678. }
  3679. }
  3680. else
  3681. {
  3682. m_flSecondaryModeStartTime = gpGlobals->curtime;
  3683. }
  3684. }
  3685. break;
  3686. case BULLRUSH_MODE_GET_DISTANCE:
  3687. {
  3688. m_flBullrushAdditionalHeight = CHOPPER_BULLRUSH_SHOOTING_VERTICAL_OFFSET;
  3689. float flDistanceToGoal = ComputeDistanceToTargetPosition();
  3690. if ( m_bRushForward )
  3691. {
  3692. if ( flDistanceToGoal < (CHOPPER_BULLRUSH_MODE_DISTANCE - 1000) )
  3693. break;
  3694. }
  3695. else
  3696. {
  3697. if ( flDistanceToGoal > -(CHOPPER_BULLRUSH_MODE_DISTANCE - 1000) )
  3698. break;
  3699. }
  3700. if ( GetHealth() <= m_flNextMegaBombHealth )
  3701. {
  3702. m_flNextMegaBombHealth -= GetMaxHealth() * g_helicopter_bullrush_mega_bomb_health.GetFloat();
  3703. m_flNextBullrushBombTime = gpGlobals->curtime;
  3704. SetSecondaryMode( BULLRUSH_MODE_MEGA_BOMB );
  3705. EmitSound( "NPC_AttackHelicopter.MegabombAlert" );
  3706. }
  3707. else
  3708. {
  3709. SetSecondaryMode( BULLRUSH_MODE_SHOOT_GUN );
  3710. SpotlightStartup();
  3711. }
  3712. }
  3713. break;
  3714. case BULLRUSH_MODE_MEGA_BOMB:
  3715. {
  3716. m_flBullrushAdditionalHeight = CHOPPER_BULLRUSH_SHOOTING_VERTICAL_OFFSET;
  3717. float flDistanceToGoal = ComputeDistanceToTargetPosition();
  3718. if ( m_bRushForward )
  3719. {
  3720. if ( flDistanceToGoal > -(CHOPPER_BULLRUSH_MODE_DISTANCE - 1000) )
  3721. break;
  3722. }
  3723. else
  3724. {
  3725. if ( flDistanceToGoal < (CHOPPER_BULLRUSH_MODE_DISTANCE - 1000) )
  3726. break;
  3727. }
  3728. m_bRushForward = !m_bRushForward;
  3729. SetSecondaryMode( BULLRUSH_MODE_GET_DISTANCE );
  3730. }
  3731. break;
  3732. case BULLRUSH_MODE_SHOOT_GUN:
  3733. {
  3734. // When shooting, stop when we cross the player's position
  3735. // Then start bombing. Use the fixed speed version if we're too far
  3736. // from the enemy or if he's travelling in the opposite direction.
  3737. // Otherwise, do the standard bombing behavior for a while.
  3738. float flDistanceToGoal = ComputeDistanceToTargetPosition();
  3739. float flShootingHeight = CHOPPER_BULLRUSH_SHOOTING_VERTICAL_OFFSET;
  3740. float flSwitchToBombDist = CHOPPER_BULLRUSH_ENEMY_BOMB_DISTANCE;
  3741. float flDropDownDist = 2000.0f;
  3742. if ( m_bRushForward )
  3743. {
  3744. m_flBullrushAdditionalHeight = ClampSplineRemapVal( flDistanceToGoal,
  3745. flSwitchToBombDist, flSwitchToBombDist + flDropDownDist, 0.0f, flShootingHeight );
  3746. if ( flDistanceToGoal > flSwitchToBombDist )
  3747. break;
  3748. }
  3749. else
  3750. {
  3751. m_flBullrushAdditionalHeight = ClampSplineRemapVal( flDistanceToGoal,
  3752. -flSwitchToBombDist - flDropDownDist, -flSwitchToBombDist, flShootingHeight, 0.0f );
  3753. if ( flDistanceToGoal < -flSwitchToBombDist )
  3754. break;
  3755. }
  3756. if ( ShouldShootIdlePlayerInBullrush() )
  3757. {
  3758. SetSecondaryMode( BULLRUSH_MODE_SHOOT_IDLE_PLAYER );
  3759. }
  3760. else
  3761. {
  3762. ShutdownGunDuringBullrush( );
  3763. SetSecondaryMode( BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED );
  3764. }
  3765. }
  3766. break;
  3767. case BULLRUSH_MODE_SHOOT_IDLE_PLAYER:
  3768. {
  3769. // Shut down our gun if we're switching to bombing
  3770. if ( ShouldBombIdlePlayer() )
  3771. {
  3772. // Must not already be shutdown
  3773. if (( m_nGunState != GUN_STATE_IDLE ) && (SecondaryModeTime() >= BULLRUSH_IDLE_PLAYER_FIRE_TIME))
  3774. {
  3775. ShutdownGunDuringBullrush( );
  3776. }
  3777. }
  3778. // m_nBurstHits = 0;
  3779. m_flCircleOfDeathRadius = ClampSplineRemapVal( SecondaryModeTime(), BULLRUSH_IDLE_PLAYER_FIRE_TIME, BULLRUSH_IDLE_PLAYER_FIRE_TIME + 5.0f, 256.0f, 64.0f );
  3780. m_flBullrushAdditionalHeight = CHOPPER_BULLRUSH_SHOOTING_VERTICAL_OFFSET;
  3781. if ( !ShouldShootIdlePlayerInBullrush() )
  3782. {
  3783. ShutdownGunDuringBullrush( );
  3784. SetSecondaryMode( BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED );
  3785. }
  3786. }
  3787. break;
  3788. case BULLRUSH_MODE_DROP_BOMBS_FOLLOW_PLAYER:
  3789. {
  3790. m_flBullrushAdditionalHeight = 0.0f;
  3791. float flDistanceToGoal = ComputeDistanceToTargetPosition();
  3792. if ( fabs( flDistanceToGoal ) > 2000.0f )
  3793. {
  3794. SetSecondaryMode( BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED, true );
  3795. break;
  3796. }
  3797. }
  3798. // FALL THROUGH!!
  3799. case BULLRUSH_MODE_DROP_BOMBS_FIXED_SPEED:
  3800. {
  3801. float flDistanceToGoal = ComputeDistanceToTargetPosition();
  3802. m_flBullrushAdditionalHeight = 0.0f;
  3803. if (( SecondaryModeTime() >= CHOPPER_BULLRUSH_ENEMY_BOMB_TIME ) || ( flDistanceToGoal > BULLRUSH_BOMB_MAX_DISTANCE ))
  3804. {
  3805. m_bRushForward = !m_bRushForward;
  3806. SetSecondaryMode( BULLRUSH_MODE_GET_DISTANCE );
  3807. }
  3808. }
  3809. break;
  3810. }
  3811. }
  3812. //------------------------------------------------------------------------------
  3813. // Purpose :
  3814. //------------------------------------------------------------------------------
  3815. void CNPC_AttackHelicopter::UpdateEnemyLeading( void )
  3816. {
  3817. bool bEnemyInVehicle = true;
  3818. CBaseEntity *pTarget = GetEnemyVehicle();
  3819. if ( !pTarget )
  3820. {
  3821. bEnemyInVehicle = false;
  3822. if ( (m_nAttackMode == ATTACK_MODE_DEFAULT) || !GetEnemy() )
  3823. {
  3824. EnableLeading( false );
  3825. return;
  3826. }
  3827. pTarget = GetEnemy();
  3828. }
  3829. EnableLeading( true );
  3830. float flLeadingDist = 0.0f;
  3831. float flSpeedAlongPath = TargetSpeedAlongPath();
  3832. float flSpeed = pTarget->GetSmoothedVelocity().Length();
  3833. // Do the test electricity gun
  3834. if ( HasSpawnFlags(SF_HELICOPTER_ELECTRICAL_DRONE) )
  3835. {
  3836. if ( flSpeedAlongPath < 200.0f )
  3837. {
  3838. flLeadingDist = ClampSplineRemapVal( flSpeedAlongPath, 0.0f, 200.0f, 100.0f, -200.0f );
  3839. }
  3840. else
  3841. {
  3842. flLeadingDist = ClampSplineRemapVal( flSpeedAlongPath, 200.0f, 600.0f, -200.0f, -500.0f );
  3843. }
  3844. SetLeadingDistance( flLeadingDist );
  3845. return;
  3846. }
  3847. switch( m_nAttackMode )
  3848. {
  3849. case ATTACK_MODE_BULLRUSH_VEHICLE:
  3850. flLeadingDist = ComputeBullrushLeadingDistance( flSpeed, flSpeedAlongPath, bEnemyInVehicle );
  3851. break;
  3852. case ATTACK_MODE_ALWAYS_LEAD_VEHICLE:
  3853. if (( flSpeed <= MIN_ENEMY_SPEED ) && (bEnemyInVehicle) )
  3854. {
  3855. flLeadingDist = CreepTowardEnemy( flSpeed, 0.0f, MIN_ENEMY_SPEED, 0.0f, 1000.0f );
  3856. }
  3857. else
  3858. {
  3859. if ( flSpeedAlongPath > 0.0f )
  3860. {
  3861. flLeadingDist = ClampSplineRemapVal( flSpeedAlongPath, 200.0f, 600.0f, 1000.0f, 2000.0f );
  3862. }
  3863. else
  3864. {
  3865. flLeadingDist = ClampSplineRemapVal( flSpeedAlongPath, -600.0f, -200.0f, -2000.0f, -1000.0f );
  3866. }
  3867. }
  3868. break;
  3869. case ATTACK_MODE_BOMB_VEHICLE:
  3870. flLeadingDist = ComputeBombingLeadingDistance( flSpeed, flSpeedAlongPath, bEnemyInVehicle );
  3871. break;
  3872. case ATTACK_MODE_DEFAULT:
  3873. case ATTACK_MODE_TRAIL_VEHICLE:
  3874. if (( flSpeed <= MIN_ENEMY_SPEED ) && (bEnemyInVehicle))
  3875. {
  3876. flLeadingDist = CreepTowardEnemy( flSpeed, 150.0f, MIN_ENEMY_SPEED, 500.0f, -1000.0f );
  3877. }
  3878. else
  3879. {
  3880. flLeadingDist = ClampSplineRemapVal( flSpeedAlongPath, -600.0f, -200.0f, -2500.0f, -1000.0f );
  3881. }
  3882. break;
  3883. }
  3884. SetLeadingDistance( flLeadingDist );
  3885. }
  3886. //-----------------------------------------------------------------------------
  3887. // Purpose:
  3888. // Input : *pInfo -
  3889. // bAlways -
  3890. //-----------------------------------------------------------------------------
  3891. void CNPC_AttackHelicopter::SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways )
  3892. {
  3893. // Are we already marked for transmission?
  3894. if ( pInfo->m_pTransmitEdict->Get( entindex() ) )
  3895. return;
  3896. BaseClass::SetTransmit( pInfo, bAlways );
  3897. // Make our smoke trails always come with us
  3898. for ( int i = 0; i < m_nSmokeTrailCount; i++ )
  3899. {
  3900. m_hSmokeTrail[i]->SetTransmit( pInfo, bAlways );
  3901. }
  3902. }
  3903. //------------------------------------------------------------------------------
  3904. // Purpose :
  3905. //------------------------------------------------------------------------------
  3906. void CNPC_AttackHelicopter::Hunt( void )
  3907. {
  3908. if ( m_lifeState == LIFE_DEAD )
  3909. {
  3910. return;
  3911. }
  3912. if ( m_lifeState == LIFE_DYING )
  3913. {
  3914. Flight();
  3915. UpdatePlayerDopplerShift( );
  3916. return;
  3917. }
  3918. // FIXME: Hack to allow us to change the firing distance
  3919. SetFarthestPathDist( GetMaxFiringDistance() );
  3920. UpdateEnemy();
  3921. // Give free knowledge of the enemy position if the chopper is "aggressive"
  3922. if ( HasSpawnFlags( SF_HELICOPTER_AGGRESSIVE ) && GetEnemy() )
  3923. {
  3924. m_vecTargetPosition = GetEnemy()->WorldSpaceCenter();
  3925. }
  3926. // Test for state transitions when in bullrush mode
  3927. if ( m_nAttackMode == ATTACK_MODE_BULLRUSH_VEHICLE )
  3928. {
  3929. UpdateBullrushState();
  3930. }
  3931. UpdateEnemyLeading();
  3932. UpdateTrackNavigation( );
  3933. Flight();
  3934. UpdatePlayerDopplerShift( );
  3935. FireWeapons();
  3936. if ( !(m_fHelicopterFlags & BITS_HELICOPTER_GUN_ON) )
  3937. {
  3938. // !!!HACKHACK This is a fairly unsavoury hack that allows the attack
  3939. // chopper to continue to carpet bomb even with the gun turned off
  3940. // (Normally the chopper will carpet bomb inside FireGun(), but FireGun()
  3941. // doesn't get called by the above call to FireWeapons() if the gun is turned off)
  3942. // Adding this little exception here lets me avoid going into the CBaseHelicopter and
  3943. // making some functions virtual that don't want to be virtual.
  3944. if ( IsCarpetBombing() )
  3945. {
  3946. BullrushBombs();
  3947. }
  3948. }
  3949. #ifdef HL2_EPISODIC
  3950. // Update our bone followers
  3951. m_BoneFollowerManager.UpdateBoneFollowers(this);
  3952. #endif // HL2_EPISODIC
  3953. }
  3954. //-----------------------------------------------------------------------------
  3955. // Purpose: Cache whatever pose parameters we intend to use
  3956. //-----------------------------------------------------------------------------
  3957. void CNPC_AttackHelicopter::PopulatePoseParameters( void )
  3958. {
  3959. m_poseWeapon_Pitch = LookupPoseParameter("weapon_pitch");
  3960. m_poseWeapon_Yaw = LookupPoseParameter("weapon_yaw");
  3961. m_poseRudder = LookupPoseParameter("rudder");
  3962. BaseClass::PopulatePoseParameters();
  3963. }
  3964. #ifdef HL2_EPISODIC
  3965. //-----------------------------------------------------------------------------
  3966. // Purpose:
  3967. //-----------------------------------------------------------------------------
  3968. void CNPC_AttackHelicopter::InitBoneFollowers( void )
  3969. {
  3970. // Don't do this if we're already loaded
  3971. if ( m_BoneFollowerManager.GetNumBoneFollowers() != 0 )
  3972. return;
  3973. // Init our followers
  3974. m_BoneFollowerManager.InitBoneFollowers( this, ARRAYSIZE(pFollowerBoneNames), pFollowerBoneNames );
  3975. }
  3976. #endif // HL2_EPISODIC
  3977. //-----------------------------------------------------------------------------
  3978. // Where are how should we avoid?
  3979. //-----------------------------------------------------------------------------
  3980. AI_BEGIN_CUSTOM_NPC( npc_helicopter, CNPC_AttackHelicopter )
  3981. // DECLARE_TASK( )
  3982. DECLARE_ACTIVITY( ACT_HELICOPTER_DROP_BOMB );
  3983. DECLARE_ACTIVITY( ACT_HELICOPTER_CRASHING );
  3984. // DECLARE_CONDITION( COND_ )
  3985. //=========================================================
  3986. // DEFINE_SCHEDULE
  3987. // (
  3988. // SCHED_DUMMY,
  3989. //
  3990. // " Tasks"
  3991. // " TASK_FACE_ENEMY 0"
  3992. // " "
  3993. // " Interrupts"
  3994. // )
  3995. AI_END_CUSTOM_NPC()
  3996. //------------------------------------------------------------------------------
  3997. //
  3998. // A sensor used to drop bombs only in the correct points
  3999. //
  4000. //------------------------------------------------------------------------------
  4001. LINK_ENTITY_TO_CLASS( npc_helicoptersensor, CBombDropSensor );
  4002. BEGIN_DATADESC( CBombDropSensor )
  4003. DEFINE_INPUTFUNC( FIELD_VOID, "DropBomb", InputDropBomb ),
  4004. DEFINE_INPUTFUNC( FIELD_VOID, "DropBombStraightDown", InputDropBombStraightDown ),
  4005. DEFINE_INPUTFUNC( FIELD_STRING, "DropBombAtTargetAlways", InputDropBombAtTargetAlways ),
  4006. DEFINE_INPUTFUNC( FIELD_STRING, "DropBombAtTarget", InputDropBombAtTarget ),
  4007. DEFINE_INPUTFUNC( FIELD_FLOAT, "DropBombDelay", InputDropBombDelay ),
  4008. END_DATADESC()
  4009. void CBombDropSensor::Spawn()
  4010. {
  4011. BaseClass::Spawn();
  4012. UTIL_SetSize(this, Vector(-30,-30,-30), Vector(30,30,30) );
  4013. SetSolid(SOLID_BBOX);
  4014. // Shots pass through
  4015. SetCollisionGroup( COLLISION_GROUP_PROJECTILE );
  4016. }
  4017. // Drop a bomb at a particular location
  4018. void CBombDropSensor::InputDropBomb( inputdata_t &inputdata )
  4019. {
  4020. inputdata_t myVersion = inputdata;
  4021. myVersion.pActivator = this;
  4022. assert_cast<CNPC_AttackHelicopter*>(GetOwnerEntity())->InputDropBomb( myVersion );
  4023. }
  4024. void CBombDropSensor::InputDropBombStraightDown( inputdata_t &inputdata )
  4025. {
  4026. inputdata_t myVersion = inputdata;
  4027. myVersion.pActivator = this;
  4028. assert_cast<CNPC_AttackHelicopter*>(GetOwnerEntity())->InputDropBombStraightDown( myVersion );
  4029. }
  4030. void CBombDropSensor::InputDropBombAtTarget( inputdata_t &inputdata )
  4031. {
  4032. inputdata_t myVersion = inputdata;
  4033. myVersion.pActivator = this;
  4034. assert_cast<CNPC_AttackHelicopter*>(GetOwnerEntity())->InputDropBombAtTarget( myVersion );
  4035. }
  4036. void CBombDropSensor::InputDropBombAtTargetAlways( inputdata_t &inputdata )
  4037. {
  4038. inputdata_t myVersion = inputdata;
  4039. myVersion.pActivator = this;
  4040. assert_cast<CNPC_AttackHelicopter*>(GetOwnerEntity())->InputDropBombAtTargetAlways( myVersion );
  4041. }
  4042. void CBombDropSensor::InputDropBombDelay( inputdata_t &inputdata )
  4043. {
  4044. inputdata_t myVersion = inputdata;
  4045. myVersion.pActivator = this;
  4046. assert_cast<CNPC_AttackHelicopter*>(GetOwnerEntity())->InputDropBombDelay( myVersion );
  4047. }
  4048. //------------------------------------------------------------------------------
  4049. //
  4050. // The bombs the helicopter drops on the player
  4051. //
  4052. //------------------------------------------------------------------------------
  4053. //------------------------------------------------------------------------------
  4054. // Save/load
  4055. //------------------------------------------------------------------------------
  4056. LINK_ENTITY_TO_CLASS( grenade_helicopter, CGrenadeHelicopter );
  4057. BEGIN_DATADESC( CGrenadeHelicopter )
  4058. DEFINE_FIELD( m_bActivated, FIELD_BOOLEAN ),
  4059. DEFINE_FIELD( m_bExplodeOnContact, FIELD_BOOLEAN ),
  4060. DEFINE_SOUNDPATCH( m_pWarnSound ),
  4061. DEFINE_FIELD( m_hWarningSprite, FIELD_EHANDLE ),
  4062. DEFINE_FIELD( m_bBlinkerAtTop, FIELD_BOOLEAN ),
  4063. #ifdef HL2_EPISODIC
  4064. DEFINE_FIELD( m_flLifetime, FIELD_FLOAT ),
  4065. DEFINE_FIELD( m_hCollisionObject, FIELD_EHANDLE ),
  4066. DEFINE_FIELD( m_bPickedUp, FIELD_BOOLEAN ),
  4067. DEFINE_FIELD( m_flBlinkFastTime, FIELD_TIME ),
  4068. DEFINE_INPUTFUNC( FIELD_FLOAT, "ExplodeIn", InputExplodeIn ),
  4069. DEFINE_OUTPUT( m_OnPhysGunOnlyPickup, "OnPhysGunOnlyPickup" ),
  4070. #endif // HL2_EPISODIC
  4071. DEFINE_THINKFUNC( ExplodeThink ),
  4072. DEFINE_THINKFUNC( AnimateThink ),
  4073. DEFINE_THINKFUNC( RampSoundThink ),
  4074. DEFINE_THINKFUNC( WarningBlinkerThink ),
  4075. DEFINE_ENTITYFUNC( ExplodeConcussion ),
  4076. END_DATADESC()
  4077. #define SF_HELICOPTER_GRENADE_DUD (1<<16) // Will not become active on impact, only when launched via physcannon
  4078. //------------------------------------------------------------------------------
  4079. // Precache
  4080. //------------------------------------------------------------------------------
  4081. void CGrenadeHelicopter::Precache( void )
  4082. {
  4083. BaseClass::Precache( );
  4084. PrecacheModel( GRENADE_HELICOPTER_MODEL );
  4085. PrecacheScriptSound( "ReallyLoudSpark" );
  4086. PrecacheScriptSound( "NPC_AttackHelicopterGrenade.Ping" );
  4087. PrecacheScriptSound( "NPC_AttackHelicopterGrenade.PingCaptured" );
  4088. PrecacheScriptSound( "NPC_AttackHelicopterGrenade.HardImpact" );
  4089. }
  4090. //------------------------------------------------------------------------------
  4091. // Spawn
  4092. //------------------------------------------------------------------------------
  4093. void CGrenadeHelicopter::Spawn( void )
  4094. {
  4095. Precache();
  4096. // point sized, solid, bouncing
  4097. SetCollisionGroup( COLLISION_GROUP_PROJECTILE );
  4098. SetModel( GRENADE_HELICOPTER_MODEL );
  4099. if ( HasSpawnFlags( SF_HELICOPTER_GRENADE_DUD ) )
  4100. {
  4101. m_nSkin = (int)SKIN_DUD;
  4102. }
  4103. if ( !HasSpawnFlags( SF_GRENADE_HELICOPTER_MEGABOMB ) )
  4104. {
  4105. IPhysicsObject *pPhysicsObject = VPhysicsInitNormal( SOLID_VPHYSICS, GetSolidFlags(), false );
  4106. SetMoveType( MOVETYPE_VPHYSICS );
  4107. Vector vecAbsVelocity = GetAbsVelocity();
  4108. pPhysicsObject->AddVelocity( &vecAbsVelocity, NULL );
  4109. }
  4110. else
  4111. {
  4112. SetSolid( SOLID_BBOX );
  4113. SetCollisionBounds( Vector( -12.5, -12.5, -12.5 ), Vector( 12.5, 12.5, 12.5 ) );
  4114. VPhysicsInitShadow( false, false );
  4115. SetMoveType( MOVETYPE_FLYGRAVITY, MOVECOLLIDE_FLY_CUSTOM );
  4116. SetElasticity( 0.5f );
  4117. AddEffects( EF_NOSHADOW );
  4118. }
  4119. // We're always being dropped beneath the helicopter; need to not
  4120. // be affected by the rotor wash
  4121. AddEFlags( EFL_NO_ROTORWASH_PUSH );
  4122. // contact grenades arc lower
  4123. QAngle angles;
  4124. VectorAngles(GetAbsVelocity(), angles );
  4125. SetLocalAngles( angles );
  4126. SetThink( NULL );
  4127. // Tumble in air
  4128. QAngle vecAngVel( random->RandomFloat ( -100, -500 ), 0, 0 );
  4129. SetLocalAngularVelocity( vecAngVel );
  4130. // Explode on contact
  4131. SetTouch( &CGrenadeHelicopter::ExplodeConcussion );
  4132. // use a lower gravity for grenades to make them easier to see
  4133. SetGravity( UTIL_ScaleForGravity( 400 ) );
  4134. #ifdef HL2_EPISODIC
  4135. m_bPickedUp = false;
  4136. m_flLifetime = BOMB_LIFETIME * 2.0;
  4137. #endif // HL2_EPISODIC
  4138. if ( hl2_episodic.GetBool() )
  4139. {
  4140. // Disallow this, we'd rather deal with them as physobjects
  4141. m_takedamage = DAMAGE_NO;
  4142. }
  4143. else
  4144. {
  4145. // Allow player to blow this puppy up in the air
  4146. m_takedamage = DAMAGE_YES;
  4147. }
  4148. m_bActivated = false;
  4149. m_pWarnSound = NULL;
  4150. m_bExplodeOnContact = false;
  4151. m_flDamage = sk_helicopter_grenadedamage.GetFloat();
  4152. g_pNotify->AddEntity( this, this );
  4153. if( hl2_episodic.GetBool() )
  4154. {
  4155. SetContextThink( &CGrenadeHelicopter::AnimateThink, gpGlobals->curtime, s_pAnimateThinkContext );
  4156. }
  4157. }
  4158. //------------------------------------------------------------------------------
  4159. // On Remve
  4160. //------------------------------------------------------------------------------
  4161. void CGrenadeHelicopter::UpdateOnRemove()
  4162. {
  4163. if( m_pWarnSound )
  4164. {
  4165. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  4166. controller.SoundDestroy( m_pWarnSound );
  4167. }
  4168. g_pNotify->ClearEntity( this );
  4169. BaseClass::UpdateOnRemove();
  4170. }
  4171. #ifdef HL2_EPISODIC
  4172. //------------------------------------------------------------------------------
  4173. //------------------------------------------------------------------------------
  4174. void CGrenadeHelicopter::InputExplodeIn( inputdata_t &inputdata )
  4175. {
  4176. m_flLifetime = inputdata.value.Float();
  4177. if ( HasSpawnFlags( SF_HELICOPTER_GRENADE_DUD ) )
  4178. {
  4179. // We are a dud no more!
  4180. RemoveSpawnFlags( SF_HELICOPTER_GRENADE_DUD );
  4181. m_nSkin = (int)SKIN_REGULAR;
  4182. }
  4183. m_bActivated = false;
  4184. BecomeActive();
  4185. }
  4186. #endif
  4187. //------------------------------------------------------------------------------
  4188. // Activate!
  4189. //------------------------------------------------------------------------------
  4190. void CGrenadeHelicopter::BecomeActive()
  4191. {
  4192. if ( m_bActivated )
  4193. return;
  4194. if ( IsMarkedForDeletion() )
  4195. return;
  4196. m_bActivated = true;
  4197. bool bMegaBomb = HasSpawnFlags(SF_GRENADE_HELICOPTER_MEGABOMB);
  4198. SetThink( &CGrenadeHelicopter::ExplodeThink );
  4199. if ( hl2_episodic.GetBool() )
  4200. {
  4201. if ( HasSpawnFlags( SF_HELICOPTER_GRENADE_DUD ) == false )
  4202. {
  4203. SetNextThink( gpGlobals->curtime + GetBombLifetime() );
  4204. }
  4205. else
  4206. {
  4207. // NOTE: A dud will not explode after a set time, only when launched!
  4208. SetThink( NULL );
  4209. return;
  4210. }
  4211. }
  4212. else
  4213. {
  4214. SetNextThink( gpGlobals->curtime + GetBombLifetime() );
  4215. }
  4216. if ( !bMegaBomb )
  4217. {
  4218. SetContextThink( &CGrenadeHelicopter::RampSoundThink, gpGlobals->curtime + GetBombLifetime() - BOMB_RAMP_SOUND_TIME, s_pRampSoundContext );
  4219. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  4220. CReliableBroadcastRecipientFilter filter;
  4221. m_pWarnSound = controller.SoundCreate( filter, entindex(), "NPC_AttackHelicopterGrenade.Ping" );
  4222. controller.Play( m_pWarnSound, 1.0, PITCH_NORM );
  4223. }
  4224. SetContextThink( &CGrenadeHelicopter::WarningBlinkerThink, gpGlobals->curtime + (GetBombLifetime() - 2.0f), s_pWarningBlinkerContext );
  4225. #ifdef HL2_EPISODIC
  4226. m_flBlinkFastTime = gpGlobals->curtime + GetBombLifetime() - 1.0f;
  4227. #endif//HL2_EPISODIC
  4228. }
  4229. //------------------------------------------------------------------------------
  4230. // Pow!
  4231. //------------------------------------------------------------------------------
  4232. void CGrenadeHelicopter::RampSoundThink( )
  4233. {
  4234. if ( m_pWarnSound )
  4235. {
  4236. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  4237. controller.SoundChangePitch( m_pWarnSound, 140, BOMB_RAMP_SOUND_TIME );
  4238. }
  4239. SetContextThink( NULL, gpGlobals->curtime, s_pRampSoundContext );
  4240. }
  4241. //------------------------------------------------------------------------------
  4242. //------------------------------------------------------------------------------
  4243. void CGrenadeHelicopter::WarningBlinkerThink()
  4244. {
  4245. #ifndef HL2_EPISODIC
  4246. return;
  4247. #endif
  4248. /*
  4249. if( !m_hWarningSprite.Get() )
  4250. {
  4251. Vector up;
  4252. GetVectors( NULL, NULL, &up );
  4253. // Light isn't on, so create the sprite.
  4254. m_hWarningSprite = CSprite::SpriteCreate( "sprites/redglow1.vmt", GetAbsOrigin() + up * 10.0f, false );
  4255. CSprite *pSprite = (CSprite *)m_hWarningSprite.Get();
  4256. if( pSprite != NULL )
  4257. {
  4258. pSprite->SetParent( this, LookupAttachment("top") );
  4259. pSprite->SetTransparency( kRenderTransAdd, 255, 255, 255, 255, kRenderFxNone );
  4260. pSprite->SetScale( 0.35, 0.0 );
  4261. }
  4262. m_bBlinkerAtTop = true;
  4263. ResetSequence( LookupActivity( "ACT_ARM" ) );
  4264. }
  4265. else
  4266. */
  4267. {
  4268. // Just flip it to the other attachment.
  4269. if( m_bBlinkerAtTop )
  4270. {
  4271. //m_hWarningSprite->SetParentAttachment( "SetParentAttachment", "bottom", false );
  4272. m_nSkin = (int)SKIN_REGULAR;
  4273. m_bBlinkerAtTop = false;
  4274. }
  4275. else
  4276. {
  4277. //m_hWarningSprite->SetParentAttachment( "SetParentAttachment", "top", false );
  4278. m_nSkin = (int)SKIN_DUD;
  4279. m_bBlinkerAtTop = true;
  4280. }
  4281. }
  4282. // Frighten people
  4283. CSoundEnt::InsertSound ( SOUND_DANGER, WorldSpaceCenter(), g_helicopter_bomb_danger_radius.GetFloat(), 0.2f, this, SOUNDENT_CHANNEL_REPEATED_DANGER );
  4284. #ifdef HL2_EPISODIC
  4285. if( gpGlobals->curtime >= m_flBlinkFastTime )
  4286. {
  4287. SetContextThink( &CGrenadeHelicopter::WarningBlinkerThink, gpGlobals->curtime + 0.1f, s_pWarningBlinkerContext );
  4288. }
  4289. else
  4290. {
  4291. SetContextThink( &CGrenadeHelicopter::WarningBlinkerThink, gpGlobals->curtime + 0.2f, s_pWarningBlinkerContext );
  4292. }
  4293. #endif//HL2_EPISODIC
  4294. }
  4295. //------------------------------------------------------------------------------
  4296. //------------------------------------------------------------------------------
  4297. void CGrenadeHelicopter::StopWarningBlinker()
  4298. {
  4299. if( m_hWarningSprite.Get() )
  4300. {
  4301. UTIL_Remove( m_hWarningSprite.Get() );
  4302. m_hWarningSprite.Set( NULL );
  4303. }
  4304. }
  4305. //------------------------------------------------------------------------------
  4306. //------------------------------------------------------------------------------
  4307. void CGrenadeHelicopter::AnimateThink()
  4308. {
  4309. StudioFrameAdvance();
  4310. SetContextThink( &CGrenadeHelicopter::AnimateThink, gpGlobals->curtime + 0.1f, s_pAnimateThinkContext );
  4311. }
  4312. //------------------------------------------------------------------------------
  4313. // Entity events... these are events targetted to a particular entity
  4314. //------------------------------------------------------------------------------
  4315. void CGrenadeHelicopter::OnEntityEvent( EntityEvent_t event, void *pEventData )
  4316. {
  4317. BaseClass::OnEntityEvent( event, pEventData );
  4318. if ( event == ENTITY_EVENT_WATER_TOUCH )
  4319. {
  4320. BecomeActive();
  4321. }
  4322. }
  4323. //------------------------------------------------------------------------------
  4324. // If we hit water, then stop
  4325. //------------------------------------------------------------------------------
  4326. void CGrenadeHelicopter::PhysicsSimulate( void )
  4327. {
  4328. Vector vecPrevPosition = GetAbsOrigin();
  4329. BaseClass::PhysicsSimulate();
  4330. if (!m_bActivated && (GetMoveType() != MOVETYPE_VPHYSICS))
  4331. {
  4332. if ( GetWaterLevel() > 1 )
  4333. {
  4334. SetAbsVelocity( vec3_origin );
  4335. SetMoveType( MOVETYPE_NONE );
  4336. BecomeActive();
  4337. }
  4338. // Stuck condition, can happen pretty often
  4339. if ( vecPrevPosition == GetAbsOrigin() )
  4340. {
  4341. SetAbsVelocity( vec3_origin );
  4342. SetMoveType( MOVETYPE_NONE );
  4343. BecomeActive();
  4344. }
  4345. }
  4346. }
  4347. //------------------------------------------------------------------------------
  4348. // If we hit something, start the timer
  4349. //------------------------------------------------------------------------------
  4350. void CGrenadeHelicopter::VPhysicsCollision( int index, gamevcollisionevent_t *pEvent )
  4351. {
  4352. BaseClass::VPhysicsCollision( index, pEvent );
  4353. BecomeActive();
  4354. #ifndef HL2_EPISODIC // in ep2, don't do this here, do it in Touch()
  4355. if ( m_bExplodeOnContact )
  4356. {
  4357. Vector vecVelocity;
  4358. GetVelocity( &vecVelocity, NULL );
  4359. DoExplosion( GetAbsOrigin(), vecVelocity );
  4360. }
  4361. #endif
  4362. if( hl2_episodic.GetBool() )
  4363. {
  4364. float flImpactSpeed = pEvent->preVelocity->Length();
  4365. if( flImpactSpeed > 400.0f && pEvent->pEntities[ 1 ]->IsWorld() )
  4366. {
  4367. EmitSound( "NPC_AttackHelicopterGrenade.HardImpact" );
  4368. }
  4369. }
  4370. }
  4371. #if HL2_EPISODIC
  4372. //------------------------------------------------------------------------------
  4373. // double launch velocity for ep2_outland_08
  4374. //------------------------------------------------------------------------------
  4375. Vector CGrenadeHelicopter::PhysGunLaunchVelocity( const Vector &forward, float flMass )
  4376. {
  4377. // return ( striderbuster_shot_velocity.GetFloat() * forward );
  4378. return BaseClass::PhysGunLaunchVelocity(forward,flMass) * sk_helicopter_grenaderadius.GetFloat();
  4379. }
  4380. #endif
  4381. //------------------------------------------------------------------------------
  4382. //------------------------------------------------------------------------------
  4383. float CGrenadeHelicopter::GetBombLifetime()
  4384. {
  4385. #if HL2_EPISODIC
  4386. return m_flLifetime;
  4387. #else
  4388. return BOMB_LIFETIME;
  4389. #endif
  4390. }
  4391. //------------------------------------------------------------------------------
  4392. // Pow!
  4393. //------------------------------------------------------------------------------
  4394. int CGrenadeHelicopter::OnTakeDamage( const CTakeDamageInfo &info )
  4395. {
  4396. // We don't take blast damage
  4397. if ( info.GetDamageType() & DMG_BLAST )
  4398. return 0;
  4399. return BaseClass::OnTakeDamage( info );
  4400. }
  4401. //------------------------------------------------------------------------------
  4402. // Pow!
  4403. //------------------------------------------------------------------------------
  4404. void CGrenadeHelicopter::DoExplosion( const Vector &vecOrigin, const Vector &vecVelocity )
  4405. {
  4406. ExplosionCreate( GetAbsOrigin(), GetAbsAngles(), GetOwnerEntity() ? GetOwnerEntity() : this, sk_helicopter_grenadedamage.GetFloat(),
  4407. sk_helicopter_grenaderadius.GetFloat(), (SF_ENVEXPLOSION_NOSPARKS|SF_ENVEXPLOSION_NODLIGHTS|SF_ENVEXPLOSION_NODECAL|SF_ENVEXPLOSION_NOFIREBALL|SF_ENVEXPLOSION_NOPARTICLES),
  4408. sk_helicopter_grenadeforce.GetFloat(), this );
  4409. if ( GetShakeAmplitude() )
  4410. {
  4411. UTIL_ScreenShake( GetAbsOrigin(), GetShakeAmplitude(), 150.0, 1.0, GetShakeRadius(), SHAKE_START );
  4412. }
  4413. CEffectData data;
  4414. // If we're under water do a water explosion
  4415. if ( GetWaterLevel() != 0 && (GetWaterType() & CONTENTS_WATER) )
  4416. {
  4417. data.m_vOrigin = WorldSpaceCenter();
  4418. data.m_flMagnitude = 128;
  4419. data.m_flScale = 128;
  4420. data.m_fFlags = 0;
  4421. DispatchEffect( "WaterSurfaceExplosion", data );
  4422. }
  4423. else
  4424. {
  4425. // Otherwise do a normal explosion
  4426. data.m_vOrigin = GetAbsOrigin();
  4427. DispatchEffect( "HelicopterMegaBomb", data );
  4428. }
  4429. UTIL_Remove( this );
  4430. }
  4431. //------------------------------------------------------------------------------
  4432. // I think I Pow!
  4433. //------------------------------------------------------------------------------
  4434. void CGrenadeHelicopter::ExplodeThink(void)
  4435. {
  4436. #ifdef HL2_EPISODIC
  4437. // remember if we were thrown by player, we can only determine this prior to explosion
  4438. bool bIsThrownByPlayer = IsThrownByPlayer();
  4439. int iHealthBefore = 0;
  4440. // get the health of the helicopter we came from prior to our explosion
  4441. CNPC_AttackHelicopter *pOwner = dynamic_cast<CNPC_AttackHelicopter *>( GetOriginalThrower() );
  4442. if ( pOwner )
  4443. {
  4444. iHealthBefore = pOwner->GetHealth();
  4445. }
  4446. #endif // HL2_EPISODIC
  4447. Vector vecVelocity;
  4448. GetVelocity( &vecVelocity, NULL );
  4449. DoExplosion( GetAbsOrigin(), vecVelocity );
  4450. #ifdef HL2_EPISODIC
  4451. // if we were thrown by player, look at health of helicopter after explosion and determine if we damaged it
  4452. if ( bIsThrownByPlayer && pOwner && ( iHealthBefore > 0 ) )
  4453. {
  4454. int iHealthAfter = pOwner->GetHealth();
  4455. if ( iHealthAfter == iHealthBefore )
  4456. {
  4457. // The player threw us, we exploded due to timer, and we did not damage the helicopter that fired us. Send a miss event
  4458. SendMissEvent();
  4459. }
  4460. }
  4461. #endif // HL2_EPISODIC
  4462. }
  4463. //------------------------------------------------------------------------------
  4464. // I think I Pow!
  4465. //------------------------------------------------------------------------------
  4466. void CGrenadeHelicopter::ResolveFlyCollisionCustom( trace_t &trace, Vector &vecVelocity )
  4467. {
  4468. ResolveFlyCollisionBounce( trace, vecVelocity, 0.1f );
  4469. }
  4470. //------------------------------------------------------------------------------
  4471. // Contact grenade, explode when it touches something
  4472. //------------------------------------------------------------------------------
  4473. void CGrenadeHelicopter::ExplodeConcussion( CBaseEntity *pOther )
  4474. {
  4475. if ( !pOther->IsSolid() )
  4476. return;
  4477. if ( !m_bExplodeOnContact )
  4478. {
  4479. if ( pOther->IsWorld() )
  4480. return;
  4481. if ( hl2_episodic.GetBool() )
  4482. {
  4483. // Don't hit anything other than vehicles
  4484. if ( pOther->GetCollisionGroup() != COLLISION_GROUP_VEHICLE )
  4485. return;
  4486. }
  4487. }
  4488. #ifdef HL2_EPISODIC
  4489. CBaseEntity *pEntityHit = pOther;
  4490. if ( pEntityHit->ClassMatches( "phys_bone_follower" ) && pEntityHit->GetOwnerEntity() )
  4491. {
  4492. pEntityHit = pEntityHit->GetOwnerEntity();
  4493. }
  4494. if ( ( CLASS_COMBINE_GUNSHIP != pEntityHit->Classify() ) || !pEntityHit->ClassMatches( "npc_helicopter" ) )
  4495. {
  4496. // We hit something other than a helicopter. If the player threw us, send a miss event
  4497. if ( IsThrownByPlayer() )
  4498. {
  4499. SendMissEvent();
  4500. }
  4501. }
  4502. #endif // HL2_EPISODIC
  4503. Vector vecVelocity;
  4504. GetVelocity( &vecVelocity, NULL );
  4505. DoExplosion( GetAbsOrigin(), vecVelocity );
  4506. }
  4507. #ifdef HL2_EPISODIC
  4508. //-----------------------------------------------------------------------------
  4509. // Purpose: The bomb will act differently when picked up by the player
  4510. //-----------------------------------------------------------------------------
  4511. void CGrenadeHelicopter::OnPhysGunPickup(CBasePlayer *pPhysGunUser, PhysGunPickup_t reason )
  4512. {
  4513. if ( reason == PICKED_UP_BY_CANNON )
  4514. {
  4515. if ( !m_bPickedUp )
  4516. {
  4517. if( m_hWarningSprite.Get() != NULL )
  4518. {
  4519. UTIL_Remove( m_hWarningSprite );
  4520. m_hWarningSprite.Set(NULL);
  4521. }
  4522. // Turn on
  4523. BecomeActive();
  4524. // Change the warning sound to a captured sound.
  4525. SetContextThink( &CGrenadeHelicopter::RampSoundThink, gpGlobals->curtime + GetBombLifetime() - BOMB_RAMP_SOUND_TIME, s_pRampSoundContext );
  4526. CSoundEnvelopeController &controller = CSoundEnvelopeController::GetController();
  4527. controller.SoundDestroy( m_pWarnSound );
  4528. CReliableBroadcastRecipientFilter filter;
  4529. m_pWarnSound = controller.SoundCreate( filter, entindex(), "NPC_AttackHelicopterGrenade.PingCaptured" );
  4530. controller.Play( m_pWarnSound, 1.0, PITCH_NORM );
  4531. // Reset our counter so the player has more time
  4532. SetThink( &CGrenadeHelicopter::ExplodeThink );
  4533. SetNextThink( gpGlobals->curtime + GetBombLifetime() );
  4534. SetContextThink( &CGrenadeHelicopter::WarningBlinkerThink, gpGlobals->curtime + GetBombLifetime() - 2.0f, s_pWarningBlinkerContext );
  4535. #ifdef HL2_EPISODIC
  4536. m_nSkin = (int)SKIN_REGULAR;
  4537. m_flBlinkFastTime = gpGlobals->curtime + GetBombLifetime() - 1.0f;
  4538. #endif//HL2_EPISODIC
  4539. // Stop us from sparing damage to the helicopter that dropped us
  4540. SetOwnerEntity( pPhysGunUser );
  4541. PhysEnableEntityCollisions( this, m_hCollisionObject );
  4542. // Don't do this again!
  4543. m_bPickedUp = true;
  4544. m_OnPhysGunOnlyPickup.FireOutput( pPhysGunUser, this );
  4545. }
  4546. }
  4547. BaseClass::OnPhysGunPickup( pPhysGunUser, reason );
  4548. }
  4549. //-----------------------------------------------------------------------------
  4550. // Purpose:
  4551. //-----------------------------------------------------------------------------
  4552. void CGrenadeHelicopter::OnPhysGunDrop( CBasePlayer *pPhysGunUser, PhysGunDrop_t reason )
  4553. {
  4554. if ( reason == LAUNCHED_BY_CANNON )
  4555. {
  4556. // Enable world touches.
  4557. unsigned int flags = VPhysicsGetObject()->GetCallbackFlags();
  4558. VPhysicsGetObject()->SetCallbackFlags( flags | CALLBACK_GLOBAL_TOUCH_STATIC );
  4559. // Explode on contact
  4560. SetTouch( &CGrenadeHelicopter::ExplodeConcussion );
  4561. m_bExplodeOnContact = true;
  4562. }
  4563. BaseClass::OnPhysGunDrop( pPhysGunUser, reason );
  4564. }
  4565. //-----------------------------------------------------------------------------
  4566. // Purpose: Returns if the player threw this grenade w/phys gun
  4567. //-----------------------------------------------------------------------------
  4568. bool CGrenadeHelicopter::IsThrownByPlayer()
  4569. {
  4570. // if player is the owner and we're set to explode on contact, then the player threw this grenade.
  4571. return ( ( GetOwnerEntity() == UTIL_GetLocalPlayer() ) && m_bExplodeOnContact );
  4572. }
  4573. //-----------------------------------------------------------------------------
  4574. // Purpose: If player threw this grenade, sends a miss event
  4575. //-----------------------------------------------------------------------------
  4576. void CGrenadeHelicopter::SendMissEvent()
  4577. {
  4578. // send a miss event
  4579. IGameEvent *event = gameeventmanager->CreateEvent( "helicopter_grenade_punt_miss" );
  4580. if ( event )
  4581. {
  4582. gameeventmanager->FireEvent( event );
  4583. }
  4584. }
  4585. #endif // HL2_EPISODIC
  4586. //-----------------------------------------------------------------------------
  4587. //
  4588. // This entity is used to create little force spheres that the helicopters should avoid.
  4589. //
  4590. //-----------------------------------------------------------------------------
  4591. CUtlVector< CAvoidSphere::AvoidSphereHandle_t > CAvoidSphere::s_AvoidSpheres;
  4592. #define SF_AVOIDSPHERE_AVOID_BELOW 0x00010000
  4593. LINK_ENTITY_TO_CLASS( npc_heli_avoidsphere, CAvoidSphere );
  4594. BEGIN_DATADESC( CAvoidSphere )
  4595. DEFINE_KEYFIELD( m_flRadius, FIELD_FLOAT, "radius" ),
  4596. END_DATADESC()
  4597. //-----------------------------------------------------------------------------
  4598. // Creates an avoidance sphere
  4599. //-----------------------------------------------------------------------------
  4600. CBaseEntity *CreateHelicopterAvoidanceSphere( CBaseEntity *pParent, int nAttachment, float flRadius, bool bAvoidBelow )
  4601. {
  4602. CAvoidSphere *pSphere = static_cast<CAvoidSphere*>(CreateEntityByName( "npc_heli_avoidsphere" ));
  4603. pSphere->Init( flRadius );
  4604. if ( bAvoidBelow )
  4605. {
  4606. pSphere->AddSpawnFlags( SF_AVOIDSPHERE_AVOID_BELOW );
  4607. }
  4608. pSphere->Spawn();
  4609. pSphere->SetParent( pParent, nAttachment );
  4610. pSphere->SetLocalOrigin( vec3_origin );
  4611. pSphere->SetLocalAngles( vec3_angle );
  4612. pSphere->SetOwnerEntity( pParent );
  4613. return pSphere;
  4614. }
  4615. //-----------------------------------------------------------------------------
  4616. // Init
  4617. //-----------------------------------------------------------------------------
  4618. void CAvoidSphere::Init( float flRadius )
  4619. {
  4620. m_flRadius = flRadius;
  4621. }
  4622. //-----------------------------------------------------------------------------
  4623. // Spawn, remove
  4624. //-----------------------------------------------------------------------------
  4625. void CAvoidSphere::Activate( )
  4626. {
  4627. BaseClass::Activate();
  4628. s_AvoidSpheres.AddToTail( this );
  4629. }
  4630. void CAvoidSphere::UpdateOnRemove( )
  4631. {
  4632. s_AvoidSpheres.FindAndRemove( this );
  4633. BaseClass::UpdateOnRemove();
  4634. }
  4635. //-----------------------------------------------------------------------------
  4636. // Where are how should we avoid?
  4637. //-----------------------------------------------------------------------------
  4638. void CAvoidSphere::ComputeAvoidanceForces( CBaseEntity *pEntity, float flEntityRadius,
  4639. float flAvoidTime, Vector *pVecAvoidForce )
  4640. {
  4641. pVecAvoidForce->Init( );
  4642. Vector vecEntityDelta;
  4643. VectorMultiply( pEntity->GetAbsVelocity(), flAvoidTime, vecEntityDelta );
  4644. Vector vecEntityCenter = pEntity->WorldSpaceCenter();
  4645. for ( int i = s_AvoidSpheres.Count(); --i >= 0; )
  4646. {
  4647. CAvoidSphere *pSphere = s_AvoidSpheres[i].Get();
  4648. const Vector &vecAvoidCenter = pSphere->WorldSpaceCenter();
  4649. // NOTE: This test can be thought of sweeping a sphere through space
  4650. // and seeing if it intersects the avoidance sphere
  4651. float flTotalRadius = flEntityRadius + pSphere->m_flRadius;
  4652. float t1, t2;
  4653. if ( !IntersectRayWithSphere( vecEntityCenter, vecEntityDelta,
  4654. vecAvoidCenter, flTotalRadius, &t1, &t2 ) )
  4655. {
  4656. continue;
  4657. }
  4658. // NOTE: The point of closest approach is at the average t value
  4659. Vector vecClosestApproach;
  4660. float flAverageT = (t1 + t2) * 0.5f;
  4661. VectorMA( vecEntityCenter, flAverageT, vecEntityDelta, vecClosestApproach );
  4662. // Add velocity to make it be pushed out away from the sphere center
  4663. // without totally counteracting its velocity.
  4664. Vector vecDir;
  4665. VectorSubtract( vecClosestApproach, vecAvoidCenter, vecDir );
  4666. float flZDist = vecDir.z;
  4667. float flDist = VectorNormalize( vecDir );
  4668. float flDistToTravel;
  4669. if ( flDist < 0.01f )
  4670. {
  4671. flDist = 0.01f;
  4672. vecDir.Init( 0, 0, 1 );
  4673. flDistToTravel = flTotalRadius;
  4674. }
  4675. else
  4676. {
  4677. // make the chopper always avoid *above*
  4678. // That means if a force would be applied to push the chopper down,
  4679. // figure out a new distance to travel that would push the chopper up.
  4680. if ( flZDist < 0.0f && !pSphere->HasSpawnFlags(SF_AVOIDSPHERE_AVOID_BELOW) )
  4681. {
  4682. Vector vecExitPoint;
  4683. vecDir.z = -vecDir.z;
  4684. VectorMA( vecAvoidCenter, flTotalRadius, vecDir, vecExitPoint );
  4685. VectorSubtract( vecExitPoint, vecClosestApproach, vecDir );
  4686. flDistToTravel = VectorNormalize( vecDir );
  4687. }
  4688. else
  4689. {
  4690. Assert( flDist <= flTotalRadius );
  4691. flDistToTravel = flTotalRadius - flDist;
  4692. }
  4693. }
  4694. // The actual force amount is easy to think about:
  4695. // We need to change the position by dx over a time dt, so dv = dx/dt
  4696. // But so it doesn't explode, lets clamp t1 to a not-unreasonable time
  4697. if ( t1 < 0.25f )
  4698. {
  4699. t1 = 0.25f;
  4700. }
  4701. float flForce = 1.25f * flDistToTravel / t1;
  4702. vecDir *= flForce;
  4703. *pVecAvoidForce += vecDir;
  4704. }
  4705. }
  4706. //-----------------------------------------------------------------------------
  4707. //
  4708. // This entity is used to create little force boxes that the helicopters should avoid.
  4709. //
  4710. //-----------------------------------------------------------------------------
  4711. CUtlVector< CAvoidBox::AvoidBoxHandle_t > CAvoidBox::s_AvoidBoxes;
  4712. #define SF_AVOIDBOX_AVOID_BELOW 0x00010000
  4713. LINK_ENTITY_TO_CLASS( npc_heli_avoidbox, CAvoidBox );
  4714. BEGIN_DATADESC( CAvoidBox )
  4715. END_DATADESC()
  4716. //-----------------------------------------------------------------------------
  4717. // Spawn, remove
  4718. //-----------------------------------------------------------------------------
  4719. void CAvoidBox::Spawn( )
  4720. {
  4721. SetModel( STRING( GetModelName() ) );
  4722. SetSolid( SOLID_BSP );
  4723. AddSolidFlags( FSOLID_NOT_SOLID );
  4724. AddEffects( EF_NODRAW );
  4725. }
  4726. void CAvoidBox::Activate( )
  4727. {
  4728. BaseClass::Activate();
  4729. s_AvoidBoxes.AddToTail( this );
  4730. }
  4731. void CAvoidBox::UpdateOnRemove( )
  4732. {
  4733. s_AvoidBoxes.FindAndRemove( this );
  4734. BaseClass::UpdateOnRemove();
  4735. }
  4736. //-----------------------------------------------------------------------------
  4737. // Where are how should we avoid?
  4738. //-----------------------------------------------------------------------------
  4739. void CAvoidBox::ComputeAvoidanceForces( CBaseEntity *pEntity, float flEntityRadius, float flAvoidTime, Vector *pVecAvoidForce )
  4740. {
  4741. pVecAvoidForce->Init( );
  4742. Vector vecEntityDelta, vecEntityEnd;
  4743. VectorMultiply( pEntity->GetAbsVelocity(), flAvoidTime, vecEntityDelta );
  4744. Vector vecEntityCenter = pEntity->WorldSpaceCenter();
  4745. VectorAdd( vecEntityCenter, vecEntityDelta, vecEntityEnd );
  4746. Vector vecVelDir = pEntity->GetAbsVelocity();
  4747. VectorNormalize( vecVelDir );
  4748. for ( int i = s_AvoidBoxes.Count(); --i >= 0; )
  4749. {
  4750. CAvoidBox *pBox = s_AvoidBoxes[i].Get();
  4751. const Vector &vecAvoidCenter = pBox->WorldSpaceCenter();
  4752. // NOTE: This test can be thought of sweeping a sphere through space
  4753. // and seeing if it intersects the avoidance box
  4754. float flTotalRadius = flEntityRadius + pBox->BoundingRadius();
  4755. float t1, t2;
  4756. if ( !IntersectInfiniteRayWithSphere( vecEntityCenter, vecEntityDelta,
  4757. vecAvoidCenter, flTotalRadius, &t1, &t2 ) )
  4758. {
  4759. continue;
  4760. }
  4761. if (( t2 < 0.0f ) || ( t1 > 1.0f ))
  4762. continue;
  4763. // Unlike the avoid spheres, we also need to make sure the ray intersects the box
  4764. Vector vecLocalCenter, vecLocalDelta;
  4765. pBox->CollisionProp()->WorldToCollisionSpace( vecEntityCenter, &vecLocalCenter );
  4766. pBox->CollisionProp()->WorldDirectionToCollisionSpace( vecEntityDelta, &vecLocalDelta );
  4767. Vector vecBoxMin( -flEntityRadius, -flEntityRadius, -flEntityRadius );
  4768. Vector vecBoxMax( flEntityRadius, flEntityRadius, flEntityRadius );
  4769. vecBoxMin += pBox->CollisionProp()->OBBMins();
  4770. vecBoxMax += pBox->CollisionProp()->OBBMaxs();
  4771. trace_t tr;
  4772. if ( !IntersectRayWithBox( vecLocalCenter, vecLocalDelta, vecBoxMin, vecBoxMax, 0.0f, &tr ) )
  4773. continue;
  4774. // NOTE: The point of closest approach is at the average t value
  4775. Vector vecClosestApproach;
  4776. float flAverageT = (t1 + t2) * 0.5f;
  4777. VectorMA( vecEntityCenter, flAverageT, vecEntityDelta, vecClosestApproach );
  4778. // Add velocity to make it be pushed out away from the sphere center
  4779. // without totally counteracting its velocity.
  4780. Vector vecDir;
  4781. VectorSubtract( vecClosestApproach, vecAvoidCenter, vecDir );
  4782. // Limit unnecessary sideways motion
  4783. if ( ( tr.plane.type != 3 ) || ( tr.plane.normal[2] > 0.0f ) )
  4784. {
  4785. vecDir.x *= 0.1f;
  4786. vecDir.y *= 0.1f;
  4787. }
  4788. float flZDist = vecDir.z;
  4789. float flDist = VectorNormalize( vecDir );
  4790. float flDistToTravel;
  4791. if ( flDist < 10.0f )
  4792. {
  4793. flDist = 10.0f;
  4794. vecDir.Init( 0, 0, 1 );
  4795. flDistToTravel = flTotalRadius;
  4796. }
  4797. else
  4798. {
  4799. // make the chopper always avoid *above*
  4800. // That means if a force would be applied to push the chopper down,
  4801. // figure out a new distance to travel that would push the chopper up.
  4802. if ( flZDist < 0.0f && !pBox->HasSpawnFlags(SF_AVOIDSPHERE_AVOID_BELOW) )
  4803. {
  4804. Vector vecExitPoint;
  4805. vecDir.z = -vecDir.z;
  4806. VectorMA( vecAvoidCenter, flTotalRadius, vecDir, vecExitPoint );
  4807. VectorSubtract( vecExitPoint, vecClosestApproach, vecDir );
  4808. flDistToTravel = VectorNormalize( vecDir );
  4809. }
  4810. else
  4811. {
  4812. Assert( flDist <= flTotalRadius );
  4813. flDistToTravel = flTotalRadius - flDist;
  4814. }
  4815. }
  4816. // The actual force amount is easy to think about:
  4817. // We need to change the position by dx over a time dt, so dv = dx/dt
  4818. // But so it doesn't explode, lets clamp t1 to a not-unreasonable time
  4819. if ( t1 < 0.25f )
  4820. {
  4821. t1 = 0.25f;
  4822. }
  4823. float flForce = 1.5f * flDistToTravel / t1;
  4824. vecDir *= flForce;
  4825. *pVecAvoidForce += vecDir;
  4826. }
  4827. }
  4828. //-----------------------------------------------------------------------------
  4829. //
  4830. // This entity is used to create little force boxes that the helicopters should avoid.
  4831. //
  4832. //-----------------------------------------------------------------------------
  4833. CUtlVector< CBombSuppressor::BombSuppressorHandle_t > CBombSuppressor::s_BombSuppressors;
  4834. LINK_ENTITY_TO_CLASS( npc_heli_nobomb, CBombSuppressor );
  4835. BEGIN_DATADESC( CBombSuppressor )
  4836. END_DATADESC()
  4837. //-----------------------------------------------------------------------------
  4838. // Spawn, remove
  4839. //-----------------------------------------------------------------------------
  4840. void CBombSuppressor::Spawn( )
  4841. {
  4842. SetModel( STRING( GetModelName() ) );
  4843. SetSolid( SOLID_BSP );
  4844. AddSolidFlags( FSOLID_NOT_SOLID );
  4845. AddEffects( EF_NODRAW );
  4846. }
  4847. void CBombSuppressor::Activate( )
  4848. {
  4849. BaseClass::Activate();
  4850. s_BombSuppressors.AddToTail( this );
  4851. }
  4852. void CBombSuppressor::UpdateOnRemove( )
  4853. {
  4854. s_BombSuppressors.FindAndRemove( this );
  4855. BaseClass::UpdateOnRemove();
  4856. }
  4857. //-----------------------------------------------------------------------------
  4858. // Where are how should we avoid?
  4859. //-----------------------------------------------------------------------------
  4860. bool CBombSuppressor::CanBomb( const Vector &vecPosition )
  4861. {
  4862. for ( int i = s_BombSuppressors.Count(); --i >= 0; )
  4863. {
  4864. CBombSuppressor *pBox = s_BombSuppressors[i].Get();
  4865. if ( pBox->CollisionProp()->IsPointInBounds( vecPosition ) )
  4866. return false;
  4867. }
  4868. return true;
  4869. }
  4870. LINK_ENTITY_TO_CLASS( helicopter_chunk, CHelicopterChunk );
  4871. BEGIN_DATADESC( CHelicopterChunk )
  4872. DEFINE_THINKFUNC( FallThink ),
  4873. DEFINE_FIELD( m_bLanded, FIELD_BOOLEAN ),
  4874. DEFINE_FIELD( m_hMaster, FIELD_EHANDLE ),
  4875. DEFINE_FIELD( m_nChunkID, FIELD_INTEGER ),
  4876. DEFINE_PHYSPTR( m_pTailConstraint ),
  4877. DEFINE_PHYSPTR( m_pCockpitConstraint ),
  4878. END_DATADESC()
  4879. //-----------------------------------------------------------------------------
  4880. // Purpose:
  4881. //-----------------------------------------------------------------------------
  4882. void CHelicopterChunk::Spawn( void )
  4883. {
  4884. }
  4885. //-----------------------------------------------------------------------------
  4886. // Purpose:
  4887. //-----------------------------------------------------------------------------
  4888. void CHelicopterChunk::FallThink( void )
  4889. {
  4890. if ( m_bLanded )
  4891. {
  4892. SetThink( NULL );
  4893. return;
  4894. }
  4895. if ( random->RandomInt( 0, 8 ) == 0 )
  4896. {
  4897. CEffectData data;
  4898. data.m_vOrigin = GetAbsOrigin() + RandomVector( -64, 64 );
  4899. DispatchEffect( "HelicopterMegaBomb", data );
  4900. EmitSound( "BaseExplosionEffect.Sound" );
  4901. }
  4902. SetNextThink( gpGlobals->curtime + 0.1f );
  4903. }
  4904. //-----------------------------------------------------------------------------
  4905. // Purpose:
  4906. // Input : index -
  4907. // *pEvent -
  4908. //-----------------------------------------------------------------------------
  4909. void CHelicopterChunk::VPhysicsCollision( int index, gamevcollisionevent_t *pEvent )
  4910. {
  4911. BaseClass::VPhysicsCollision( index, pEvent );
  4912. if ( m_bLanded == false )
  4913. {
  4914. int otherIndex = !index;
  4915. CBaseEntity *pOther = pEvent->pEntities[otherIndex];
  4916. if ( !pOther )
  4917. return;
  4918. if ( pOther->IsWorld() )
  4919. {
  4920. CollisionCallback( this );
  4921. m_bLanded = true;
  4922. SetThink( NULL );
  4923. }
  4924. }
  4925. }
  4926. //-----------------------------------------------------------------------------
  4927. // Purpose:
  4928. // Input : *pCaller -
  4929. //-----------------------------------------------------------------------------
  4930. void CHelicopterChunk::CollisionCallback( CHelicopterChunk *pCaller )
  4931. {
  4932. if ( m_bLanded )
  4933. return;
  4934. if ( m_hMaster != NULL )
  4935. {
  4936. m_hMaster->CollisionCallback( this );
  4937. }
  4938. else
  4939. {
  4940. // Break our other constraints
  4941. if ( m_pTailConstraint )
  4942. {
  4943. physenv->DestroyConstraint( m_pTailConstraint );
  4944. m_pTailConstraint = NULL;
  4945. }
  4946. if ( m_pCockpitConstraint )
  4947. {
  4948. physenv->DestroyConstraint( m_pCockpitConstraint );
  4949. m_pCockpitConstraint = NULL;
  4950. }
  4951. // Add a dust cloud
  4952. AR2Explosion *pExplosion = AR2Explosion::CreateAR2Explosion( GetAbsOrigin() );
  4953. if ( pExplosion != NULL )
  4954. {
  4955. pExplosion->SetLifetime( 10 );
  4956. }
  4957. // Make a loud noise
  4958. EmitSound( "NPC_AttackHelicopter.Crash" );
  4959. m_bLanded = true;
  4960. }
  4961. }
  4962. //-----------------------------------------------------------------------------
  4963. // Purpose:
  4964. // Input : &vecPos -
  4965. // &vecAngles -
  4966. // &vecVelocity -
  4967. // *pszModelName -
  4968. // Output : CHelicopterChunk
  4969. //-----------------------------------------------------------------------------
  4970. CHelicopterChunk *CHelicopterChunk::CreateHelicopterChunk( const Vector &vecPos, const QAngle &vecAngles, const Vector &vecVelocity, const char *pszModelName, int chunkID )
  4971. {
  4972. // Drop a flaming, smoking chunk.
  4973. CHelicopterChunk *pChunk = CREATE_ENTITY( CHelicopterChunk, "helicopter_chunk" );
  4974. if ( pChunk == NULL )
  4975. return NULL;
  4976. pChunk->Spawn();
  4977. pChunk->SetAbsOrigin( vecPos );
  4978. pChunk->SetAbsAngles( vecAngles );
  4979. pChunk->SetModel( pszModelName );
  4980. pChunk->m_nChunkID = chunkID;
  4981. pChunk->SetCollisionGroup( COLLISION_GROUP_INTERACTIVE );
  4982. IPhysicsObject *pPhysicsObject = pChunk->VPhysicsInitNormal( SOLID_VPHYSICS, pChunk->GetSolidFlags(), false );
  4983. // Set the velocity
  4984. if ( pPhysicsObject )
  4985. {
  4986. pPhysicsObject->EnableMotion( true );
  4987. Vector vecChunkVelocity;
  4988. AngularImpulse angImpulse;
  4989. vecChunkVelocity = vecVelocity;
  4990. angImpulse = vec3_origin;
  4991. pPhysicsObject->SetVelocity(&vecChunkVelocity, &angImpulse );
  4992. }
  4993. pChunk->SetThink( &CHelicopterChunk::FallThink );
  4994. pChunk->SetNextThink( gpGlobals->curtime + 0.1f );
  4995. pChunk->m_bLanded = false;
  4996. SmokeTrail *pSmokeTrail = SmokeTrail::CreateSmokeTrail();
  4997. pSmokeTrail->FollowEntity( pChunk, "damage" );
  4998. pSmokeTrail->m_SpawnRate = 4;
  4999. pSmokeTrail->m_ParticleLifetime = 2.0f;
  5000. pSmokeTrail->m_StartColor.Init( 0.7f, 0.7f, 0.7f );
  5001. pSmokeTrail->m_EndColor.Init( 0.6, 0.6, 0.6 );
  5002. pSmokeTrail->m_StartSize = 32;
  5003. pSmokeTrail->m_EndSize = 64;
  5004. pSmokeTrail->m_SpawnRadius= 8;
  5005. pSmokeTrail->m_MinSpeed = 0;
  5006. pSmokeTrail->m_MaxSpeed = 8;
  5007. pSmokeTrail->m_Opacity = 0.35f;
  5008. CFireTrail *pFireTrail = CFireTrail::CreateFireTrail();
  5009. if ( pFireTrail == NULL )
  5010. return pChunk;
  5011. pFireTrail->FollowEntity( pChunk, "damage" );
  5012. pFireTrail->SetParent( pChunk, 1 );
  5013. pFireTrail->SetLocalOrigin( vec3_origin );
  5014. pFireTrail->SetMoveType( MOVETYPE_NONE );
  5015. pFireTrail->SetLifetime( 10.0f );
  5016. return pChunk;
  5017. }