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.

274 lines
10 KiB

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