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.

470 lines
15 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef NPC_METROPOLICE_H
  7. #define NPC_METROPOLICE_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "rope.h"
  12. #include "rope_shared.h"
  13. #include "ai_baseactor.h"
  14. #include "ai_basenpc.h"
  15. #include "ai_goal_police.h"
  16. #include "ai_behavior.h"
  17. #include "ai_behavior_standoff.h"
  18. #include "ai_behavior_assault.h"
  19. #include "ai_behavior_functank.h"
  20. #include "ai_behavior_actbusy.h"
  21. #include "ai_behavior_rappel.h"
  22. #include "ai_behavior_police.h"
  23. #include "ai_behavior_follow.h"
  24. #include "ai_sentence.h"
  25. #include "props.h"
  26. class CNPC_MetroPolice;
  27. class CNPC_MetroPolice : public CAI_BaseActor
  28. {
  29. DECLARE_CLASS( CNPC_MetroPolice, CAI_BaseActor );
  30. DECLARE_DATADESC();
  31. public:
  32. CNPC_MetroPolice();
  33. virtual bool CreateComponents();
  34. bool CreateBehaviors();
  35. void Spawn( void );
  36. void Precache( void );
  37. Class_T Classify( void );
  38. Disposition_t IRelationType(CBaseEntity *pTarget);
  39. float MaxYawSpeed( void );
  40. void HandleAnimEvent( animevent_t *pEvent );
  41. Activity NPC_TranslateActivity( Activity newActivity );
  42. Vector EyeDirection3D( void ) { return CAI_BaseHumanoid::EyeDirection3D(); } // cops don't have eyes
  43. virtual void Event_Killed( const CTakeDamageInfo &info );
  44. virtual void OnScheduleChange();
  45. float GetIdealAccel( void ) const;
  46. int ObjectCaps( void ) { return UsableNPCObjectCaps(BaseClass::ObjectCaps()); }
  47. void PrecriminalUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
  48. // These are overridden so that the cop can shove and move a non-criminal player safely
  49. CBaseEntity *CheckTraceHullAttack( float flDist, const Vector &mins, const Vector &maxs, int iDamage, int iDmgType, float forceScale, bool bDamageAnyNPC );
  50. CBaseEntity *CheckTraceHullAttack( const Vector &vStart, const Vector &vEnd, const Vector &mins, const Vector &maxs, int iDamage, int iDmgType, float flForceScale, bool bDamageAnyNPC );
  51. virtual int SelectSchedule( void );
  52. virtual int SelectFailSchedule( int failedSchedule, int failedTask, AI_TaskFailureCode_t taskFailCode );
  53. virtual int TranslateSchedule( int scheduleType );
  54. void StartTask( const Task_t *pTask );
  55. void RunTask( const Task_t *pTask );
  56. virtual Vector GetActualShootTrajectory( const Vector &shootOrigin );
  57. virtual void FireBullets( const FireBulletsInfo_t &info );
  58. virtual bool HandleInteraction(int interactionType, void *data, CBaseCombatCharacter* sourceEnt);
  59. virtual void Weapon_Equip( CBaseCombatWeapon *pWeapon );
  60. //virtual bool OverrideMoveFacing( const AILocalMoveGoal_t &move, float flInterval );
  61. bool OnObstructionPreSteer( AILocalMoveGoal_t *pMoveGoal, float distClear, AIMoveResult_t *pResult );
  62. bool ShouldBruteForceFailedNav() { return false; }
  63. virtual void GatherConditions( void );
  64. virtual bool OverrideMoveFacing( const AILocalMoveGoal_t &move, float flInterval );
  65. // Can't move and shoot when the enemy is an airboat
  66. virtual bool ShouldMoveAndShoot();
  67. // TraceAttack
  68. virtual void TraceAttack( const CTakeDamageInfo &info, const Vector &vecDir, trace_t *ptr, CDmgAccumulator *pAccumulator );
  69. // Speaking
  70. virtual void SpeakSentence( int nSentenceType );
  71. // Set up the shot regulator based on the equipped weapon
  72. virtual void OnUpdateShotRegulator( );
  73. bool ShouldKnockOutTarget( CBaseEntity *pTarget );
  74. void KnockOutTarget( CBaseEntity *pTarget );
  75. void StunnedTarget( CBaseEntity *pTarget );
  76. void AdministerJustice( void );
  77. bool QueryHearSound( CSound *pSound );
  78. void SetBatonState( bool state );
  79. bool BatonActive( void );
  80. CAI_Sentence< CNPC_MetroPolice > *GetSentences() { return &m_Sentences; }
  81. virtual bool AllowedToIgnite( void ) { return true; }
  82. void PlayFlinchGesture( void );
  83. protected:
  84. // Determines the best type of flinch anim to play.
  85. virtual Activity GetFlinchActivity( bool bHeavyDamage, bool bGesture );
  86. // Only move and shoot when attacking
  87. virtual bool OnBeginMoveAndShoot();
  88. virtual void OnEndMoveAndShoot();
  89. private:
  90. bool PlayerIsCriminal( void );
  91. void ReleaseManhack( void );
  92. // Speech-related methods
  93. void AnnounceTakeCoverFromDanger( CSound *pSound );
  94. void AnnounceEnemyType( CBaseEntity *pEnemy );
  95. void AnnounceEnemyKill( CBaseEntity *pEnemy );
  96. void AnnounceHarrassment( );
  97. void AnnounceOutOfAmmo( );
  98. // Behavior-related sentences
  99. void SpeakFuncTankSentence( int nSentenceType );
  100. void SpeakAssaultSentence( int nSentenceType );
  101. void SpeakStandoffSentence( int nSentenceType );
  102. virtual void LostEnemySound( void );
  103. virtual void FoundEnemySound( void );
  104. virtual void AlertSound( void );
  105. virtual void PainSound( const CTakeDamageInfo &info );
  106. virtual void DeathSound( const CTakeDamageInfo &info );
  107. virtual void IdleSound( void );
  108. virtual bool ShouldPlayIdleSound( void );
  109. // Burst mode!
  110. void SetBurstMode( bool bEnable );
  111. int OnTakeDamage_Alive( const CTakeDamageInfo &info );
  112. int GetSoundInterests( void );
  113. void BuildScheduleTestBits( void );
  114. bool CanDeployManhack( void );
  115. bool ShouldHitPlayer( const Vector &targetDir, float targetDist );
  116. void PrescheduleThink( void );
  117. void SetPlayerCriminalDuration( float time );
  118. void IncrementPlayerCriminalStatus( void );
  119. virtual bool UseAttackSquadSlots() { return true; }
  120. WeaponProficiency_t CalcWeaponProficiency( CBaseCombatWeapon *pWeapon );
  121. // Inputs
  122. void InputEnableManhackToss( inputdata_t &inputdata );
  123. void InputSetPoliceGoal( inputdata_t &inputdata );
  124. void InputActivateBaton( inputdata_t &inputdata );
  125. void NotifyDeadFriend ( CBaseEntity* pFriend );
  126. // Stitch aiming!
  127. void AimBurstRandomly( int nMinCount, int nMaxCount, float flMinDelay, float flMaxDelay );
  128. void AimBurstAtEnemy( float flReactionTime );
  129. void AimBurstInFrontOfEnemy( float flReactionTime );
  130. void AimBurstAlongSideOfEnemy( float flFollowTime );
  131. void AimBurstBehindEnemy( float flFollowTime );
  132. void AimBurstTightGrouping( float flShotTime );
  133. // Anim event handlers
  134. void OnAnimEventDeployManhack( animevent_t *pEvent );
  135. void OnAnimEventShove( void );
  136. void OnAnimEventBatonOn( void );
  137. void OnAnimEventBatonOff( void );
  138. void OnAnimEventStartDeployManhack( void );
  139. void OnAnimEventPreDeployManhack( void );
  140. bool HasBaton( void );
  141. // Normal schedule selection
  142. int SelectCombatSchedule();
  143. int SelectScheduleNewEnemy();
  144. int SelectScheduleArrestEnemy();
  145. int SelectRangeAttackSchedule();
  146. int SelectScheduleNoDirectEnemy();
  147. int SelectScheduleInvestigateSound();
  148. int SelectShoveSchedule( void );
  149. bool TryToEnterPistolSlot( int nSquadSlot );
  150. // Airboat schedule selection
  151. int SelectAirboatCombatSchedule();
  152. int SelectAirboatRangeAttackSchedule();
  153. // Handle flinching
  154. bool IsHeavyDamage( const CTakeDamageInfo &info );
  155. // Is my enemy currently in an airboat?
  156. bool IsEnemyInAnAirboat() const;
  157. // Returns the airboat
  158. CBaseEntity *GetEnemyAirboat() const;
  159. // Compute a predicted enemy position n seconds into the future
  160. void PredictShootTargetPosition( float flDeltaTime, float flMinLeadDist, float flAddVelocity, Vector *pVecTarget, Vector *pVecTargetVel );
  161. // Compute a predicted velocity n seconds into the future (given a known acceleration rate)
  162. void PredictShootTargetVelocity( float flDeltaTime, Vector *pVecTargetVel );
  163. // How many shots will I fire in a particular amount of time?
  164. int CountShotsInTime( float flDeltaTime ) const;
  165. float GetTimeForShots( int nShotCount ) const;
  166. // Visualize stitch
  167. void VisualizeStitch( const Vector &vecStart, const Vector &vecEnd );
  168. // Visualize line of death
  169. void VisualizeLineOfDeath( );
  170. // Modify the stitch length
  171. float ComputeDistanceStitchModifier( float flDistanceToTarget ) const;
  172. // Adjusts the burst toward the target as it's being fired.
  173. void SteerBurstTowardTarget( );
  174. // Methods to compute shot trajectory based on burst mode
  175. Vector ComputeBurstLockOnTrajectory( const Vector &shootOrigin );
  176. Vector ComputeBurstDeliberatelyMissTrajectory( const Vector &shootOrigin );
  177. Vector ComputeBurstTrajectory( const Vector &shootOrigin );
  178. Vector ComputeTightBurstTrajectory( const Vector &shootOrigin );
  179. // Are we currently firing a burst?
  180. bool IsCurrentlyFiringBurst() const;
  181. // Which entity are we actually trying to shoot at?
  182. CBaseEntity *GetShootTarget();
  183. // Different burst steering modes
  184. void SteerBurstTowardTargetUseSpeedOnly( const Vector &vecShootAt, const Vector &vecShootAtVelocity, float flPredictTime, int nShotsTillPredict );
  185. void SteerBurstTowardTargetUseVelocity( const Vector &vecShootAt, const Vector &vecShootAtVelocity, int nShotsTillPredict );
  186. void SteerBurstTowardTargetUsePosition( const Vector &vecShootAt, const Vector &vecShootAtVelocity, int nShotsTillPredict );
  187. void SteerBurstTowardPredictedPoint( const Vector &vecShootAt, const Vector &vecShootAtVelocity, int nShotsTillPredict );
  188. void SteerBurstWithinLineOfDeath( );
  189. // Set up the shot regulator
  190. int SetupBurstShotRegulator( float flReactionTime );
  191. // Choose a random vector somewhere between the two specified vectors
  192. void RandomDirectionBetweenVectors( const Vector &vecStart, const Vector &vecEnd, Vector *pResult );
  193. // Stitch selector
  194. float StitchAtWeight( float flDist, float flSpeed, float flDot, float flReactionTime, const Vector &vecTargetToGun );
  195. float StitchAcrossWeight( float flDist, float flSpeed, float flDot, float flReactionTime );
  196. float StitchAlongSideWeight( float flDist, float flSpeed, float flDot );
  197. float StitchBehindWeight( float flDist, float flSpeed, float flDot );
  198. float StitchTightWeight( float flDist, float flSpeed, const Vector &vecTargetToGun, const Vector &vecVelocity );
  199. int SelectStitchSchedule();
  200. // Can me enemy see me?
  201. bool CanEnemySeeMe( );
  202. // Combat schedule selection
  203. int SelectMoveToLedgeSchedule();
  204. // position to shoot at
  205. Vector StitchAimTarget( const Vector &posSrc, bool bNoisy );
  206. // Should we attempt to stitch?
  207. bool ShouldAttemptToStitch();
  208. // Deliberately aims as close as possible w/o hitting
  209. Vector AimCloseToTargetButMiss( CBaseEntity *pTarget, const Vector &shootOrigin );
  210. // Compute the actual reaction time based on distance + speed modifiers
  211. float AimBurstAtReactionTime( float flReactonTime, float flDistToTargetSqr, float flCurrentSpeed );
  212. int AimBurstAtSetupHitCount( float flDistToTargetSqr, float flCurrentSpeed );
  213. // How many squad members are trying to arrest the player?
  214. int SquadArrestCount();
  215. // He's resisting arrest!
  216. void EnemyResistingArrest();
  217. void VPhysicsCollision( int index, gamevcollisionevent_t *pEvent );
  218. // Rappel
  219. virtual bool IsWaitingToRappel( void ) { return m_RappelBehavior.IsWaitingToRappel(); }
  220. void BeginRappel() { m_RappelBehavior.BeginRappel(); }
  221. private:
  222. enum
  223. {
  224. BURST_NOT_ACTIVE = 0,
  225. BURST_ACTIVE,
  226. BURST_LOCK_ON_AFTER_HIT,
  227. BURST_LOCKED_ON,
  228. BURST_DELIBERATELY_MISS,
  229. BURST_TIGHT_GROUPING,
  230. };
  231. enum
  232. {
  233. BURST_STEER_NONE = 0,
  234. BURST_STEER_TOWARD_PREDICTED_POINT,
  235. BURST_STEER_WITHIN_LINE_OF_DEATH,
  236. BURST_STEER_ADJUST_FOR_SPEED_CHANGES,
  237. BURST_STEER_EXACTLY_TOWARD_TARGET,
  238. };
  239. enum
  240. {
  241. COND_METROPOLICE_ON_FIRE = BaseClass::NEXT_CONDITION,
  242. COND_METROPOLICE_ENEMY_RESISTING_ARREST,
  243. COND_METROPOLICE_PLAYER_TOO_CLOSE,
  244. COND_METROPOLICE_CHANGE_BATON_STATE,
  245. COND_METROPOLICE_PHYSOBJECT_ASSAULT,
  246. };
  247. enum
  248. {
  249. SCHED_METROPOLICE_WALK = BaseClass::NEXT_SCHEDULE,
  250. SCHED_METROPOLICE_WAKE_ANGRY,
  251. SCHED_METROPOLICE_HARASS,
  252. SCHED_METROPOLICE_CHASE_ENEMY,
  253. SCHED_METROPOLICE_ESTABLISH_LINE_OF_FIRE,
  254. SCHED_METROPOLICE_DRAW_PISTOL,
  255. SCHED_METROPOLICE_DEPLOY_MANHACK,
  256. SCHED_METROPOLICE_ADVANCE,
  257. SCHED_METROPOLICE_CHARGE,
  258. SCHED_METROPOLICE_BURNING_RUN,
  259. SCHED_METROPOLICE_BURNING_STAND,
  260. SCHED_METROPOLICE_SMG_NORMAL_ATTACK,
  261. SCHED_METROPOLICE_SMG_BURST_ATTACK,
  262. SCHED_METROPOLICE_AIM_STITCH_AT_AIRBOAT,
  263. SCHED_METROPOLICE_AIM_STITCH_IN_FRONT_OF_AIRBOAT,
  264. SCHED_METROPOLICE_AIM_STITCH_TIGHTLY,
  265. SCHED_METROPOLICE_AIM_STITCH_ALONG_SIDE_OF_AIRBOAT,
  266. SCHED_METROPOLICE_AIM_STITCH_BEHIND_AIRBOAT,
  267. SCHED_METROPOLICE_ESTABLISH_STITCH_LINE_OF_FIRE,
  268. SCHED_METROPOLICE_INVESTIGATE_SOUND,
  269. SCHED_METROPOLICE_WARN_AND_ARREST_ENEMY,
  270. SCHED_METROPOLICE_ARREST_ENEMY,
  271. SCHED_METROPOLICE_ENEMY_RESISTING_ARREST,
  272. SCHED_METROPOLICE_WARN_TARGET,
  273. SCHED_METROPOLICE_HARASS_TARGET,
  274. SCHED_METROPOLICE_SUPPRESS_TARGET,
  275. SCHED_METROPOLICE_RETURN_FROM_HARASS,
  276. SCHED_METROPOLICE_SHOVE,
  277. SCHED_METROPOLICE_ACTIVATE_BATON,
  278. SCHED_METROPOLICE_DEACTIVATE_BATON,
  279. SCHED_METROPOLICE_ALERT_FACE_BESTSOUND,
  280. SCHED_METROPOLICE_RETURN_TO_PRECHASE,
  281. SCHED_METROPOLICE_SMASH_PROP,
  282. };
  283. enum
  284. {
  285. TASK_METROPOLICE_HARASS = BaseClass::NEXT_TASK,
  286. TASK_METROPOLICE_DIE_INSTANTLY,
  287. TASK_METROPOLICE_BURST_ATTACK,
  288. TASK_METROPOLICE_STOP_FIRE_BURST,
  289. TASK_METROPOLICE_AIM_STITCH_AT_PLAYER,
  290. TASK_METROPOLICE_AIM_STITCH_AT_AIRBOAT,
  291. TASK_METROPOLICE_AIM_STITCH_TIGHTLY,
  292. TASK_METROPOLICE_AIM_STITCH_IN_FRONT_OF_AIRBOAT,
  293. TASK_METROPOLICE_AIM_STITCH_ALONG_SIDE_OF_AIRBOAT,
  294. TASK_METROPOLICE_AIM_STITCH_BEHIND_AIRBOAT,
  295. TASK_METROPOLICE_RELOAD_FOR_BURST,
  296. TASK_METROPOLICE_GET_PATH_TO_STITCH,
  297. TASK_METROPOLICE_RESET_LEDGE_CHECK_TIME,
  298. TASK_METROPOLICE_GET_PATH_TO_BESTSOUND_LOS,
  299. TASK_METROPOLICE_AIM_WEAPON_AT_ENEMY,
  300. TASK_METROPOLICE_ARREST_ENEMY,
  301. TASK_METROPOLICE_LEAD_ARREST_ENEMY,
  302. TASK_METROPOLICE_SIGNAL_FIRING_TIME,
  303. TASK_METROPOLICE_ACTIVATE_BATON,
  304. TASK_METROPOLICE_WAIT_FOR_SENTENCE,
  305. TASK_METROPOLICE_GET_PATH_TO_PRECHASE,
  306. TASK_METROPOLICE_CLEAR_PRECHASE,
  307. };
  308. private:
  309. int m_iPistolClips; // How many clips the cop has in reserve
  310. int m_iManhacks; // How many manhacks the cop has
  311. bool m_fWeaponDrawn; // Is my weapon drawn? (ready to use)
  312. bool m_bSimpleCops; // The easy version of the cops
  313. int m_LastShootSlot;
  314. CRandSimTimer m_TimeYieldShootSlot;
  315. CSimpleSimTimer m_BatonSwingTimer;
  316. CSimpleSimTimer m_NextChargeTimer;
  317. // All related to burst firing
  318. Vector m_vecBurstTargetPos;
  319. Vector m_vecBurstDelta;
  320. int m_nBurstHits;
  321. int m_nMaxBurstHits;
  322. int m_nBurstReloadCount;
  323. Vector m_vecBurstLineOfDeathDelta;
  324. Vector m_vecBurstLineOfDeathOrigin;
  325. int m_nBurstMode;
  326. int m_nBurstSteerMode;
  327. float m_flBurstSteerDistance;
  328. float m_flBurstPredictTime;
  329. Vector m_vecBurstPredictedVelocityDir;
  330. float m_vecBurstPredictedSpeed;
  331. float m_flValidStitchTime;
  332. float m_flNextLedgeCheckTime;
  333. float m_flTaskCompletionTime;
  334. bool m_bShouldActivateBaton;
  335. float m_flBatonDebounceTime; // Minimum amount of time before turning the baton off
  336. float m_flLastPhysicsFlinchTime;
  337. float m_flLastDamageFlinchTime;
  338. // Sentences
  339. float m_flNextPainSoundTime;
  340. float m_flNextLostSoundTime;
  341. int m_nIdleChatterType;
  342. bool m_bPlayerIsNear;
  343. // Policing state
  344. bool m_bPlayerTooClose;
  345. bool m_bKeepFacingPlayer;
  346. float m_flChasePlayerTime;
  347. Vector m_vecPreChaseOrigin;
  348. float m_flPreChaseYaw;
  349. int m_nNumWarnings;
  350. int m_iNumPlayerHits;
  351. // Outputs
  352. COutputEvent m_OnStunnedPlayer;
  353. COutputEvent m_OnCupCopped;
  354. AIHANDLE m_hManhack;
  355. CHandle<CPhysicsProp> m_hBlockingProp;
  356. CAI_ActBusyBehavior m_ActBusyBehavior;
  357. CAI_StandoffBehavior m_StandoffBehavior;
  358. CAI_AssaultBehavior m_AssaultBehavior;
  359. CAI_FuncTankBehavior m_FuncTankBehavior;
  360. CAI_RappelBehavior m_RappelBehavior;
  361. CAI_PolicingBehavior m_PolicingBehavior;
  362. CAI_FollowBehavior m_FollowBehavior;
  363. CAI_Sentence< CNPC_MetroPolice > m_Sentences;
  364. int m_nRecentDamage;
  365. float m_flRecentDamageTime;
  366. // The last hit direction, measured as a yaw relative to our orientation
  367. float m_flLastHitYaw;
  368. static float gm_flTimeLastSpokePeek;
  369. public:
  370. DEFINE_CUSTOM_AI;
  371. };
  372. #endif // NPC_METROPOLICE_H