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.

258 lines
6.1 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef AI_WAYPOINT_H
  8. #define AI_WAYPOINT_H
  9. #if defined( _WIN32 )
  10. #pragma once
  11. #endif
  12. #include <mempool.h>
  13. // ----------------------------------------------------------------------------
  14. // Forward declarations
  15. // ----------------------------------------------------------------------------
  16. // ----------------------------------------------------------------------------
  17. // Flags used in the flags field in AI_Waypoint_T
  18. // ----------------------------------------------------------------------------
  19. enum WaypointFlags_t
  20. {
  21. // The type of waypoint
  22. bits_WP_TO_DETOUR = 0x01, // move to detour point.
  23. bits_WP_TO_PATHCORNER = 0x02, // move to a path corner
  24. bits_WP_TO_NODE = 0x04, // move to a node
  25. bits_WP_TO_GOAL = 0x08, // move to an arbitrary point
  26. bits_WP_TO_DOOR = 0x10, // move to position to open a door
  27. // Other flags for waypoint
  28. bits_WP_DONT_SIMPLIFY = 0x20, // Don't let the route code simplify this waypoint
  29. };
  30. // ----------------------------------------------------------------------------
  31. // Purpose: Waypoints that make up an NPC's route.
  32. // ----------------------------------------------------------------------------
  33. struct AI_Waypoint_t
  34. {
  35. public:
  36. AI_Waypoint_t();
  37. AI_Waypoint_t( const Vector &vecPosition, float flYaw, Navigation_t navType, int fWaypointFlags, int nNodeID );
  38. AI_Waypoint_t( const AI_Waypoint_t &from )
  39. {
  40. memcpy( this, &from, sizeof(*this) );
  41. flPathDistGoal = -1;
  42. pNext = pPrev = NULL;
  43. }
  44. AI_Waypoint_t &operator=( const AI_Waypoint_t &from )
  45. {
  46. memcpy( this, &from, sizeof(*this) );
  47. flPathDistGoal = -1;
  48. pNext = pPrev = NULL;
  49. return *this;
  50. }
  51. ~AI_Waypoint_t()
  52. {
  53. AssertValid();
  54. if ( pNext )
  55. {
  56. pNext->AssertValid();
  57. pNext->pPrev = pPrev;
  58. }
  59. if ( pPrev )
  60. {
  61. pPrev->AssertValid();
  62. pPrev->pNext = pNext;
  63. }
  64. }
  65. //---------------------------------
  66. void AssertValid() const
  67. {
  68. #ifdef DEBUG
  69. Assert( !pNext || pNext->pPrev == this );
  70. Assert( !pPrev || pPrev->pNext == this );
  71. #endif
  72. }
  73. //---------------------------------
  74. int Flags() const;
  75. Navigation_t NavType() const;
  76. // Flag modification method
  77. void ModifyFlags( int fFlags, bool bEnable );
  78. bool IsReducible() { return (pNext && m_iWPType == pNext->m_iWPType && !(m_fWaypointFlags & (bits_WP_TO_GOAL | bits_WP_TO_PATHCORNER | bits_WP_DONT_SIMPLIFY)) ); }
  79. //---------------------------------
  80. void SetNext( AI_Waypoint_t *p );
  81. AI_Waypoint_t * GetNext() { return pNext; }
  82. const AI_Waypoint_t *GetNext() const { return pNext; }
  83. AI_Waypoint_t * GetPrev() { return pPrev; }
  84. const AI_Waypoint_t *GetPrev() const { return pPrev; }
  85. AI_Waypoint_t * GetLast();
  86. //---------------------------------
  87. const Vector & GetPos() const { return vecLocation; }
  88. void SetPos(const Vector &newPos) { vecLocation = newPos; }
  89. EHANDLE GetEHandleData() { return m_hData; }
  90. //---------------------------------
  91. //
  92. // Basic info
  93. //
  94. Vector vecLocation;
  95. float flYaw; // Waypoint facing dir
  96. int iNodeID; // If waypoint is a node, which one
  97. //---------------------------------
  98. //
  99. // Precalculated distances
  100. //
  101. float flPathDistGoal;
  102. //---------------------------------
  103. //
  104. // If following a designer laid path, the path-corner entity (if any)
  105. //
  106. EHANDLE hPathCorner;
  107. // Data specific to the waypoint type:
  108. //
  109. // PATHCORNER: The path corner entity.
  110. // DOOR: If moving to position to open a door, the handle of the door to open.
  111. EHANDLE m_hData;
  112. private:
  113. int m_fWaypointFlags; // See WaypointFlags_t
  114. Navigation_t m_iWPType; // The type of waypoint
  115. AI_Waypoint_t *pNext;
  116. AI_Waypoint_t *pPrev;
  117. DECLARE_FIXEDSIZE_ALLOCATOR(AI_Waypoint_t);
  118. public:
  119. DECLARE_SIMPLE_DATADESC();
  120. };
  121. // ----------------------------------------------------------------------------
  122. // Inline methods associated with AI_Waypoint_t
  123. // ----------------------------------------------------------------------------
  124. inline int AI_Waypoint_t::Flags() const
  125. {
  126. return m_fWaypointFlags;
  127. }
  128. inline Navigation_t AI_Waypoint_t::NavType() const
  129. {
  130. return m_iWPType;
  131. }
  132. inline void AI_Waypoint_t::ModifyFlags( int fFlags, bool bEnable )
  133. {
  134. if (bEnable)
  135. m_fWaypointFlags |= fFlags;
  136. else
  137. m_fWaypointFlags &= ~fFlags;
  138. }
  139. inline void AI_Waypoint_t::SetNext( AI_Waypoint_t *p )
  140. {
  141. if (pNext)
  142. {
  143. pNext->pPrev = NULL;
  144. }
  145. pNext = p;
  146. if ( pNext )
  147. {
  148. if ( pNext->pPrev )
  149. pNext->pPrev->pNext = NULL;
  150. pNext->pPrev = this;
  151. }
  152. }
  153. // ----------------------------------------------------------------------------
  154. // Purpose: Holds an maintains a chain of waypoints
  155. class CAI_WaypointList
  156. {
  157. public:
  158. CAI_WaypointList()
  159. : m_pFirstWaypoint( NULL )
  160. {
  161. }
  162. CAI_WaypointList( AI_Waypoint_t *pFirstWaypoint)
  163. : m_pFirstWaypoint( pFirstWaypoint )
  164. {
  165. }
  166. void Set(AI_Waypoint_t* route);
  167. void PrependWaypoints( AI_Waypoint_t *pWaypoints );
  168. void PrependWaypoint( const Vector &newPoint, Navigation_t navType, unsigned waypointFlags, float flYaw = 0 );
  169. bool IsEmpty() const { return ( m_pFirstWaypoint == NULL ); }
  170. AI_Waypoint_t * GetFirst() { return m_pFirstWaypoint; }
  171. const AI_Waypoint_t *GetFirst() const { return m_pFirstWaypoint; }
  172. AI_Waypoint_t * GetLast();
  173. const AI_Waypoint_t *GetLast() const;
  174. void RemoveAll();
  175. private:
  176. AI_Waypoint_t* m_pFirstWaypoint; // Linked list of waypoints
  177. };
  178. // ----------------------------------------------------------------------------
  179. #ifdef DEBUG
  180. void AssertRouteValid( AI_Waypoint_t* route );
  181. #else
  182. #define AssertRouteValid( route ) ((void)0)
  183. #endif
  184. // ----------------------------------------------------------------------------
  185. // Utilities
  186. void DeleteAll( AI_Waypoint_t *pWaypointList );
  187. // ------------------------------------
  188. inline void DeleteAll( AI_Waypoint_t **ppWaypointList )
  189. {
  190. DeleteAll( *ppWaypointList );
  191. *ppWaypointList = NULL;
  192. }
  193. // ------------------------------------
  194. void AddWaypointLists(AI_Waypoint_t *pLeft, AI_Waypoint_t *pRight);
  195. // ----------------------------------------------------------------------------
  196. #endif // AI_WAYPOINT_H