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.

280 lines
11 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef AI_TRACKPATHER_H
  7. #define AI_TRACKPATHER_H
  8. #if defined( _WIN32 )
  9. #pragma once
  10. #endif
  11. #include "ai_basenpc.h"
  12. class CPathTrack;
  13. //------------------------------------------------------------------------------
  14. class CAI_TrackPather : public CAI_BaseNPC
  15. {
  16. DECLARE_CLASS( CAI_TrackPather, CAI_BaseNPC );
  17. DECLARE_DATADESC();
  18. friend class CNPC_CombineAdvisorRoaming;
  19. public:
  20. bool IsOnPathTrack() { return (m_pCurrentPathTarget != NULL); }
  21. protected:
  22. void InitPathingData( float flTrackArrivalTolerance, float flTargetDistance, float flAvoidDistance );
  23. virtual bool GetTrackPatherTarget( Vector *pPos ) { return false; }
  24. virtual CBaseEntity *GetTrackPatherTargetEnt() { return NULL; }
  25. const Vector & GetDesiredPosition() const { return m_vecDesiredPosition; }
  26. void SetDesiredPosition( const Vector &v ) { m_vecDesiredPosition = v; }
  27. const Vector & GetGoalOrientation() const { return m_vecGoalOrientation; }
  28. void SetGoalOrientation( const Vector &v ) { m_vecGoalOrientation = v; }
  29. bool CurPathTargetIsDest() { return ( m_pDestPathTarget == m_pCurrentPathTarget ); }
  30. virtual bool HasReachedTarget( void ) { return (WorldSpaceCenter() - m_vecDesiredPosition).Length() < 128; }
  31. CPathTrack * GetDestPathTarget() { return m_pDestPathTarget; }
  32. bool IsInForcedMove() const { return m_bForcedMove; }
  33. void ClearForcedMove() { m_bForcedMove = false; }
  34. float GetPathMaxSpeed() const { return m_flPathMaxSpeed; }
  35. void OnSave( IEntitySaveUtils *pUtils );
  36. void OnRestore( void );
  37. protected:
  38. enum PauseState_t
  39. {
  40. PAUSE_NO_PAUSE = 0,
  41. PAUSED_AT_POSITION,
  42. PAUSE_AT_NEXT_LOS_POSITION,
  43. PAUSE_FORCE_DWORD = 0xFFFFFFFF,
  44. };
  45. // Sets a track
  46. void SetTrack( string_t strTrackName );
  47. void SetTrack( CBaseEntity *pGoalEnt );
  48. // Fly to a particular track point via the path
  49. virtual void InputFlyToPathTrack( inputdata_t &inputdata );
  50. // Updates the nav target if we've reached it
  51. void UpdateTrackNavigation( void );
  52. // Computes distance + nearest point from the current path..
  53. float ClosestPointToCurrentPath( Vector *pVecPoint ) const;
  54. // Computes a "path" velocity at a particular point along the current path
  55. void ComputePathTangent( float t, Vector *pVecTangent ) const;
  56. // Computes the *normalized* velocity at which the helicopter should approach the final point
  57. void ComputeNormalizedDestVelocity( Vector *pVecVelocity ) const;
  58. // Sets the farthest path distance
  59. void SetFarthestPathDist( float flMaxPathDist );
  60. // Returns the next/previous path along our current path
  61. CPathTrack *NextAlongCurrentPath( CPathTrack *pPath ) const;
  62. CPathTrack *PreviousAlongCurrentPath( CPathTrack *pPath ) const;
  63. // Adjusts a "next"most node based on the current movement direction
  64. CPathTrack *AdjustForMovementDirection( CPathTrack *pPath ) const;
  65. // Enemy visibility check
  66. virtual CBaseEntity *FindTrackBlocker( const Vector &vecViewPoint, const Vector &vecTargetPos );
  67. // Compute a point n units along a path
  68. void ComputePointAlongPath( const Vector &vecStartPoint, float flDistance, Vector *pTarget );
  69. // Are we leading?
  70. bool IsLeading() const { return m_bLeading && !m_bForcedMove; }
  71. // Leading + leading distance
  72. void EnableLeading( bool bEnable );
  73. void SetLeadingDistance( float flLeadDistance );
  74. float GetLeadingDistance( ) const;
  75. // Compute a point n units along the current path from our current position
  76. // (but don't pass the desired target point)
  77. void ComputePointAlongCurrentPath( float flDistance, float flPerpDist, Vector *pTarget );
  78. // Returns the perpendicular distance of the target from the nearest path point
  79. float TargetDistanceToPath() const { return m_flTargetDistFromPath; }
  80. // Returns the speed of the target relative to the path
  81. float TargetSpeedAlongPath() const;
  82. // Returns the speed of the target *across* the path
  83. float TargetSpeedAcrossPath() const;
  84. // Compute a path direction
  85. void ComputePathDirection( CPathTrack *pPath, Vector *pVecPathDir );
  86. // What's the current path direction?
  87. void CurrentPathDirection( Vector *pVecPathDir );
  88. // Returns the max distance we can be from the path
  89. float MaxDistanceFromCurrentPath() const;
  90. // true to use farthest, false for nearest
  91. void UseFarthestPathPoint( bool useFarthest );
  92. // Moves to an explicit track point
  93. void MoveToTrackPoint( CPathTrack *pTrack );
  94. // Sets up a new current path target
  95. void SetupNewCurrentTarget( CPathTrack *pTrack );
  96. virtual void OnNewCurrentTarget( CPathTrack *pTrackNew, CPathTrack *pTrackOld ) {}
  97. // Compute the distance to the leading position
  98. float ComputeDistanceToLeadingPosition();
  99. // Compute the distance to the target position
  100. float ComputeDistanceToTargetPosition();
  101. // Set the pause state.
  102. void SetPauseState( PauseState_t pauseState ) { m_nPauseState = pauseState; }
  103. // Does this path track have LOS to the target?
  104. bool HasLOSToTarget( CPathTrack *pTrack );
  105. // FIXME: Work this back into the base class
  106. virtual bool ShouldUseFixedPatrolLogic() { return false; }
  107. virtual bool ShouldUseAvoidanceWhenTracking() { return false; }
  108. // Deal with teleportation
  109. void Teleported();
  110. private:
  111. CPathTrack *BestPointOnPath( CPathTrack *pPath, const Vector &targetPos, float avoidRadius, bool visible, bool bFarthestPointOnPath );
  112. // Input methods
  113. void InputSetTrack( inputdata_t &inputdata );
  114. void InputChooseFarthestPathPoint( inputdata_t &inputdata );
  115. void InputChooseNearestPathPoint( inputdata_t &inputdata );
  116. void InputStartBreakableMovement( inputdata_t &inputdata );
  117. void InputStopBreakableMovement( inputdata_t &inputdata );
  118. void InputStartPatrol( inputdata_t &inputdata );
  119. void InputStopPatrol( inputdata_t &inputdata );
  120. void InputStartLeading( inputdata_t &inputdata );
  121. void InputStopLeading( inputdata_t &inputdata );
  122. // Obsolete, for backward compatibility
  123. void InputStartPatrolBreakable( inputdata_t &inputdata );
  124. // Flies to a point on a track
  125. void FlyToPathTrack( string_t strTrackName );
  126. // Selects a new destination target
  127. void SelectNewDestTarget();
  128. // Makes sure we've picked the right position along the path if we're chasing an enemy
  129. void UpdateTargetPosition( );
  130. // Moves to the track
  131. void UpdateCurrentTarget();
  132. void UpdateCurrentTargetLeading();
  133. // Track debugging info
  134. void VisualizeDebugInfo( const Vector &vecNearestPoint, const Vector &vecTarget );
  135. // Moves to the closest track point
  136. void MoveToClosestTrackPoint( CPathTrack *pTrack );
  137. // Are the two path tracks connected?
  138. bool IsOnSameTrack( CPathTrack *pPath1, CPathTrack *pPath2 ) const;
  139. // Is pPathTest in "front" of pPath on the same path? (Namely, does GetNext() get us there?)
  140. bool IsForwardAlongPath( CPathTrack *pPath, CPathTrack *pPathTest ) const;
  141. // Purpose:
  142. void UpdateTargetPositionLeading( void );
  143. // Compute a point n units along a path
  144. CPathTrack *ComputeLeadingPointAlongPath( const Vector &vecStartPoint, CPathTrack *pFirstTrack, float flDistance, Vector *pTarget );
  145. // Finds the closest point on the path, returns a signed perpendicular distance
  146. CPathTrack *FindClosestPointOnPath( CPathTrack *pPath, const Vector &targetPos, Vector *pVecClosestPoint, Vector *pVecPathDir, float *pDistanceFromPath );
  147. // Methods to find a signed perp distance from the track
  148. // and to compute a point off the path based on the signed perp distance
  149. float ComputePerpDistanceFromPath( const Vector &vecPointOnPath, const Vector &vecPathDir, const Vector &vecPointOffPath );
  150. void ComputePointFromPerpDistance( const Vector &vecPointOnPath, const Vector &vecPathDir, float flPerpDist, Vector *pResult );
  151. // Returns the direction of the path at the closest point to the target
  152. const Vector &TargetPathDirection() const;
  153. const Vector &TargetPathAcrossDirection() const;
  154. // Returns distance along path to target, returns -1 if there's no path
  155. float ComputePathDistance( CPathTrack *pStart, CPathTrack *pDest, bool bForward ) const;
  156. // Compute the distance to a particular point on the path
  157. float ComputeDistanceAlongPathToPoint( CPathTrack *pStartTrack, CPathTrack *pDestTrack, const Vector &vecDestPosition, bool bMovingForward );
  158. private:
  159. //---------------------------------
  160. Vector m_vecDesiredPosition;
  161. Vector m_vecGoalOrientation; // orientation of the goal entity.
  162. // NOTE: CurrentPathTarget changes meaning based on movement direction
  163. // For this *after* means the "next" (m_pnext) side of the line segment
  164. // and "before" means the "prev" (m_pprevious) side of the line segment
  165. // CurrentPathTarget is *after* the desired point when moving forward,
  166. // and *before* the desired point when moving backward.
  167. // DestPathTarget + TargetNearestPath always represent points
  168. // *after* the desired point.
  169. CHandle<CPathTrack> m_pCurrentPathTarget;
  170. CHandle<CPathTrack> m_pDestPathTarget;
  171. CHandle<CPathTrack> m_pLastPathTarget;
  172. CHandle<CPathTrack> m_pTargetNearestPath; // Used only by leading, it specifies the path point *after* where the target is
  173. string_t m_strCurrentPathName;
  174. string_t m_strDestPathName;
  175. string_t m_strLastPathName;
  176. string_t m_strTargetNearestPathName;
  177. Vector m_vecLastGoalCheckPosition; // Last position checked for moving towards
  178. float m_flEnemyPathUpdateTime; // Next time to update our enemies position
  179. bool m_bForcedMove; // Means the destination point must be reached regardless of enemy position
  180. bool m_bPatrolling; // If set, move back and forth along the current track until we see an enemy
  181. bool m_bPatrolBreakable; // If set, I'll stop patrolling if I see an enemy
  182. bool m_bLeading; // If set, we can lead our enemies
  183. // Derived class pathing data
  184. float m_flTargetDistanceThreshold;// Distance threshold used to determine when a target has moved enough to update our navigation to it
  185. float m_flAvoidDistance; //
  186. float m_flTargetTolerance; // How far from a path track do we need to be before we 'reached' it?
  187. Vector m_vecSegmentStartPoint; // Starting point for the current segment
  188. Vector m_vecSegmentStartSplinePoint; // Used to define a spline which is used to compute path velocity
  189. bool m_bMovingForward;
  190. bool m_bChooseFarthestPoint;
  191. float m_flFarthestPathDist; // How far from a path track do we need to be before we 'reached' it?
  192. float m_flPathMaxSpeed;
  193. float m_flTargetDistFromPath; // How far is the target from the closest point on the path?
  194. float m_flLeadDistance;
  195. Vector m_vecTargetPathDir;
  196. Vector m_vecTargetPathPoint; // What point on the path is closest to the target?
  197. PauseState_t m_nPauseState;
  198. };
  199. //------------------------------------------------------------------------------
  200. #endif // AI_TRACKPATHER_H