Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
9.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef AI_BEHAVIOR_FOLLOW_H
  7. #define AI_BEHAVIOR_FOLLOW_H
  8. #include "simtimer.h"
  9. #include "ai_behavior.h"
  10. #include "ai_goalentity.h"
  11. #include "ai_utils.h"
  12. #include "ai_moveshoot.h"
  13. #include "tier0/platform.h"
  14. #ifdef HL2_EPISODIC
  15. #include "hl2_gamerules.h"
  16. #endif
  17. #if defined( _WIN32 )
  18. #pragma once
  19. #endif
  20. //-----------------------------------------------------------------------------
  21. // NOTE: these must correspond with the AI_FollowFormation_t array in AI_Behavior_Follow.cpp!!
  22. //-----------------------------------------------------------------------------
  23. enum AI_Formations_t
  24. {
  25. AIF_SIMPLE,
  26. AIF_WIDE,
  27. AIF_ANTLION,
  28. AIF_COMMANDER,
  29. AIF_TIGHT,
  30. AIF_MEDIUM,
  31. AIF_SIDEKICK,
  32. AIF_HUNTER,
  33. AIF_VORTIGAUNT,
  34. AIF_TOPDOWN_TIGHT,
  35. };
  36. enum AI_FollowFormationFlags_t
  37. {
  38. AIFF_DEFAULT = 0,
  39. AIFF_USE_FOLLOW_POINTS = 0x01,
  40. AIFF_REQUIRE_LOS_OUTSIDE_COMBAT = 0x02,
  41. };
  42. //-----------------------------------------------------------------------------
  43. //
  44. // CAI_FollowGoal
  45. //
  46. // Purpose: A level tool to control the follow behavior. Use is not required
  47. // in order to use behavior.
  48. //
  49. //-----------------------------------------------------------------------------
  50. class CAI_FollowGoal : public CAI_GoalEntity
  51. {
  52. DECLARE_CLASS( CAI_FollowGoal, CAI_GoalEntity );
  53. public:
  54. virtual void EnableGoal( CAI_BaseNPC *pAI );
  55. virtual void DisableGoal( CAI_BaseNPC *pAI );
  56. #ifdef HL2_EPISODIC
  57. virtual void InputOutsideTransition( inputdata_t &inputdata );
  58. #endif
  59. int m_iFormation;
  60. DECLARE_DATADESC();
  61. };
  62. //-----------------------------------------------------------------------------
  63. int AIGetNumFollowers( CBaseEntity *pEntity, string_t iszClassname = NULL_STRING );
  64. //-----------------------------------------------------------------------------
  65. struct AI_FollowNavInfo_t
  66. {
  67. int flags;
  68. Vector position;
  69. float range;
  70. float Zrange;
  71. float tolerance;
  72. float followPointTolerance;
  73. float targetMoveTolerance;
  74. float repathOnRouteTolerance;
  75. float walkTolerance;
  76. float coverTolerance;
  77. float enemyLOSTolerance;
  78. float chaseEnemyTolerance;
  79. DECLARE_SIMPLE_DATADESC();
  80. };
  81. struct AI_FollowGroup_t;
  82. struct AI_FollowManagerInfoHandle_t
  83. {
  84. AI_FollowGroup_t *m_pGroup;
  85. intp m_hFollower;
  86. };
  87. //-------------------------------------
  88. struct AI_FollowParams_t
  89. {
  90. AI_FollowParams_t( AI_Formations_t formation = AIF_SIMPLE, bool bNormalMemoryDiscard = false )
  91. : formation(formation),
  92. bNormalMemoryDiscard( bNormalMemoryDiscard )
  93. {
  94. }
  95. AI_Formations_t formation;
  96. bool bNormalMemoryDiscard;
  97. DECLARE_SIMPLE_DATADESC();
  98. };
  99. //-------------------------------------
  100. class CAI_FollowBehavior : public CAI_SimpleBehavior
  101. {
  102. DECLARE_CLASS( CAI_FollowBehavior, CAI_SimpleBehavior );
  103. public:
  104. CAI_FollowBehavior( const AI_FollowParams_t &params = AIF_SIMPLE );
  105. ~CAI_FollowBehavior();
  106. virtual int DrawDebugTextOverlays( int text_offset );
  107. virtual void DrawDebugGeometryOverlays();
  108. // Returns true if the NPC is actively following a target.
  109. bool IsActive( void );
  110. void SetParameters( const AI_FollowParams_t &params );
  111. virtual const char *GetName() { return "Follow"; }
  112. AI_Formations_t GetFormation() const { return m_params.formation; }
  113. virtual bool CanSelectSchedule();
  114. const AI_FollowNavInfo_t &GetFollowGoalInfo();
  115. CBaseEntity * GetFollowTarget();
  116. void SetFollowTarget( CBaseEntity *pLeader, bool fFinishCurSchedule = false );
  117. CAI_FollowGoal *GetFollowGoal() { return m_hFollowGoalEnt; } // if any
  118. bool SetFollowGoal( CAI_FollowGoal *pGoal, bool fFinishCurSchedule = false );
  119. void ClearFollowGoal( CAI_FollowGoal *pGoal );
  120. void SetFollowGoalDirect( CAI_FollowGoal *pGoal );
  121. virtual bool FarFromFollowTarget() { return ( m_hFollowTarget && (GetAbsOrigin() - m_hFollowTarget->GetAbsOrigin()).LengthSqr() > (75*12)*(75*12) ); }
  122. virtual bool TargetIsUnreachable() { return m_bTargetUnreachable; }
  123. int GetNumFailedFollowAttempts() { return m_nFailedFollowAttempts; }
  124. float GetTimeFailFollowStarted() { return m_flTimeFailFollowStarted; }
  125. bool FollowTargetVisible() { return HasCondition( COND_FOLLOW_TARGET_VISIBLE ); };
  126. bool IsMovingToFollowTarget();
  127. float GetGoalRange();
  128. float GetGoalZRange();
  129. virtual Activity NPC_TranslateActivity( Activity activity );
  130. virtual int TranslateSchedule( int scheduleType );
  131. virtual void StartTask( const Task_t *pTask );
  132. virtual int SelectFailSchedule( int failedSchedule, int failedTask, AI_TaskFailureCode_t taskFailCode );
  133. virtual void TaskComplete( bool fIgnoreSetFailedCondition = false );
  134. virtual void GatherConditions();
  135. protected:
  136. const Vector &GetGoalPosition();
  137. virtual bool ShouldFollow();
  138. friend class CAI_FollowManager;
  139. virtual void BeginScheduleSelection();
  140. virtual void EndScheduleSelection();
  141. virtual void CleanupOnDeath( CBaseEntity *pCulprit, bool bFireDeathOutput );
  142. virtual void Precache();
  143. virtual int SelectSchedule();
  144. virtual int FollowCallBaseSelectSchedule() { return BaseClass::SelectSchedule(); }
  145. virtual void OnStartSchedule( int scheduleType );
  146. virtual void RunTask( const Task_t *pTask );
  147. void BuildScheduleTestBits();
  148. bool IsCurScheduleFollowSchedule();
  149. virtual bool IsCurTaskContinuousMove();
  150. virtual void OnMovementFailed();
  151. virtual void OnMovementComplete();
  152. virtual bool FValidateHintType( CAI_Hint *pHint );
  153. bool IsValidCover( const Vector &vLocation, CAI_Hint const *pHint );
  154. bool IsValidShootPosition( const Vector &vLocation, CAI_Node *pNode, CAI_Hint const *pHint );
  155. bool FindCoverFromEnemyAtFollowTarget( float coverRadius, Vector *pResult );
  156. bool ShouldAlwaysThink();
  157. bool ShouldMoveToFollowTarget();
  158. int SelectScheduleManagePosition();
  159. int SelectScheduleFollowPoints();
  160. int SelectScheduleMoveToFormation();
  161. void GetFollowTargetViewLoc( Vector *pResult);
  162. bool ValidateFaceTarget( Vector *pFaceTarget );
  163. //----------------------------
  164. bool ShouldUseFollowPoints();
  165. bool HasFollowPoint();
  166. void SetFollowPoint( CAI_Hint *pHintNode );
  167. void ClearFollowPoint();
  168. const Vector & GetFollowPoint();
  169. CAI_Hint * FindFollowPoint();
  170. bool IsFollowPointInRange();
  171. bool ShouldIgnoreFollowPointFacing();
  172. //----------------------------
  173. bool UpdateFollowPosition();
  174. const int GetGoalFlags();
  175. float GetGoalTolerance();
  176. bool PlayerIsPushing();
  177. bool IsFollowTargetInRange( float rangeMultiplier = 1.0 );
  178. bool IsFollowGoalInRange( float tolerance, float zTolerance, int flags );
  179. virtual bool IsChaseGoalInRange();
  180. void NoteFailedFollow();
  181. void NoteSuccessfulFollow();
  182. //----------------------------
  183. protected:
  184. enum
  185. {
  186. SCHED_FOLLOWER_MOVE_AWAY_FAIL = BaseClass::NEXT_SCHEDULE, // Turn back toward player
  187. SCHED_FOLLOWER_MOVE_AWAY_END,
  188. SCHED_FOLLOW,
  189. SCHED_FOLLOWER_IDLE_STAND,
  190. SCHED_MOVE_TO_FACE_FOLLOW_TARGET,
  191. SCHED_FACE_FOLLOW_TARGET,
  192. SCHED_FOLLOWER_GO_TO_WAIT_POINT,
  193. SCHED_FOLLOWER_GO_TO_WAIT_POINT_FAIL,
  194. SCHED_FOLLOWER_STAND_AT_WAIT_POINT,
  195. SCHED_FOLLOWER_COMBAT_FACE,
  196. NEXT_SCHEDULE,
  197. TASK_CANT_FOLLOW = BaseClass::NEXT_TASK,
  198. TASK_FACE_FOLLOW_TARGET,
  199. TASK_MOVE_TO_FOLLOW_POSITION,
  200. TASK_GET_PATH_TO_FOLLOW_POSITION,
  201. TASK_SET_FOLLOW_TARGET_MARK,
  202. TASK_FOLLOWER_FACE_TACTICAL,
  203. TASK_SET_FOLLOW_DELAY,
  204. TASK_GET_PATH_TO_FOLLOW_POINT,
  205. TASK_ARRIVE_AT_FOLLOW_POINT,
  206. TASK_SET_FOLLOW_POINT_STAND_SCHEDULE,
  207. TASK_BEGIN_STAND_AT_WAIT_POINT,
  208. NEXT_TASK,
  209. COND_TARGET_MOVED_FROM_MARK = BaseClass::NEXT_CONDITION,
  210. COND_FOUND_WAIT_POINT,
  211. COND_FOLLOW_DELAY_EXPIRED,
  212. COND_FOLLOW_TARGET_VISIBLE,
  213. COND_FOLLOW_TARGET_NOT_VISIBLE,
  214. COND_FOLLOW_WAIT_POINT_INVALID,
  215. COND_FOLLOW_PLAYER_IS_LIT,
  216. COND_FOLLOW_PLAYER_IS_NOT_LIT,
  217. NEXT_CONDITION,
  218. };
  219. DEFINE_CUSTOM_SCHEDULE_PROVIDER;
  220. protected:
  221. //----------------------------
  222. EHANDLE m_hFollowTarget;
  223. AI_FollowNavInfo_t m_FollowNavGoal;
  224. float m_flTimeUpdatedFollowPosition;
  225. bool m_bFirstFacing;
  226. float m_flTimeFollowTargetVisible;
  227. CAI_MoveMonitor m_TargetMonitor;
  228. bool m_bTargetUnreachable;
  229. bool m_bFollowNavFailed; // Set when pathfinding fails to limit impact of m_FollowDelay on ShouldFollow
  230. int m_nFailedFollowAttempts;
  231. float m_flTimeFailFollowStarted;
  232. Vector m_vFollowMoveAnchor;
  233. bool m_bMovingToCover;
  234. float m_flOriginalEnemyDiscardTime;
  235. float m_SavedDistTooFar;
  236. CRandStopwatch m_FollowDelay;
  237. CSimpleSimTimer m_RepathOnFollowTimer;
  238. //---------------------------------
  239. Activity m_CurrentFollowActivity;
  240. //---------------------------------
  241. CRandSimTimer m_TimeBlockUseWaitPoint;
  242. CSimTimer m_TimeCheckForWaitPoint;
  243. CAI_Hint * m_pInterruptWaitPoint;
  244. //---------------------------------
  245. CRandSimTimer m_TimeBeforeSpreadFacing;
  246. CRandSimTimer m_TimeNextSpreadFacing;
  247. //---------------------------------
  248. AI_FollowManagerInfoHandle_t m_hFollowManagerInfo;
  249. AI_FollowParams_t m_params;
  250. //---------------------------------
  251. CHandle<CAI_FollowGoal> m_hFollowGoalEnt;
  252. //---------------------------------
  253. DECLARE_DATADESC();
  254. };
  255. //-------------------------------------
  256. inline const AI_FollowNavInfo_t &CAI_FollowBehavior::GetFollowGoalInfo()
  257. {
  258. return m_FollowNavGoal;
  259. }
  260. //-------------------------------------
  261. inline const int CAI_FollowBehavior::GetGoalFlags()
  262. {
  263. return m_FollowNavGoal.flags;
  264. }
  265. //-------------------------------------
  266. inline const Vector &CAI_FollowBehavior::GetGoalPosition()
  267. {
  268. return m_FollowNavGoal.position;
  269. }
  270. //-------------------------------------
  271. inline float CAI_FollowBehavior::GetGoalTolerance()
  272. {
  273. return m_FollowNavGoal.tolerance;
  274. }
  275. //-------------------------------------
  276. inline float CAI_FollowBehavior::GetGoalRange()
  277. {
  278. return m_FollowNavGoal.range;
  279. }
  280. //-------------------------------------
  281. inline float CAI_FollowBehavior::GetGoalZRange()
  282. {
  283. return m_FollowNavGoal.Zrange;
  284. }
  285. //-----------------------------------------------------------------------------
  286. #endif // AI_BEHAVIOR_FOLLOW_H