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.

279 lines
6.5 KiB

  1. //========= Copyright � 1996-2005, 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. bits_WP_PRECISE_MOVEMENT = 0x40, // Precise movement is necessary near this waypoint
  30. };
  31. // ----------------------------------------------------------------------------
  32. // Purpose: Waypoints that make up an NPC's route.
  33. // ----------------------------------------------------------------------------
  34. struct AI_Waypoint_t
  35. {
  36. public:
  37. AI_Waypoint_t();
  38. AI_Waypoint_t( const Vector &vecPosition, float flYaw, Navigation_t navType, int fWaypointFlags, int nNodeID );
  39. AI_Waypoint_t( const AI_Waypoint_t &from )
  40. {
  41. memcpy( this, &from, sizeof(*this) );
  42. flPathDistGoal = -1;
  43. pNext = pPrev = NULL;
  44. }
  45. AI_Waypoint_t &operator=( const AI_Waypoint_t &from )
  46. {
  47. memcpy( this, &from, sizeof(*this) );
  48. flPathDistGoal = -1;
  49. pNext = pPrev = NULL;
  50. return *this;
  51. }
  52. ~AI_Waypoint_t()
  53. {
  54. AssertValid();
  55. if ( pNext )
  56. {
  57. pNext->AssertValid();
  58. pNext->pPrev = pPrev;
  59. }
  60. if ( pPrev )
  61. {
  62. pPrev->AssertValid();
  63. pPrev->pNext = pNext;
  64. }
  65. }
  66. //---------------------------------
  67. void AssertValid() const
  68. {
  69. #ifdef DEBUG
  70. Assert( !pNext || pNext->pPrev == this );
  71. Assert( !pPrev || pPrev->pNext == this );
  72. #endif
  73. }
  74. //---------------------------------
  75. int Flags() const;
  76. Navigation_t NavType() const;
  77. void SetNavType( Navigation_t type ) { m_iWPType = type; }
  78. // Flag modification method
  79. void ModifyFlags( int fFlags, bool bEnable );
  80. bool IsReducible() { return (pNext && m_iWPType == pNext->m_iWPType && !(m_fWaypointFlags & (bits_WP_TO_GOAL | bits_WP_TO_PATHCORNER | bits_WP_DONT_SIMPLIFY)) ); }
  81. //---------------------------------
  82. void SetNext( AI_Waypoint_t *p );
  83. AI_Waypoint_t * GetNext() { return pNext; }
  84. const AI_Waypoint_t *GetNext() const { return pNext; }
  85. void SetPrev( AI_Waypoint_t *p );
  86. AI_Waypoint_t * GetPrev() { return pPrev; }
  87. const AI_Waypoint_t *GetPrev() const { return pPrev; }
  88. AI_Waypoint_t * GetLast();
  89. //---------------------------------
  90. const Vector & GetPos() const { return vecLocation; }
  91. void SetPos(const Vector &newPos) { vecLocation = newPos; }
  92. EHANDLE GetEHandleData() { return m_hData; }
  93. //---------------------------------
  94. //
  95. // Basic info
  96. //
  97. Vector vecLocation;
  98. float flYaw; // Waypoint facing dir
  99. int iNodeID; // If waypoint is a node, which one
  100. //---------------------------------
  101. //
  102. // Precalculated distances
  103. //
  104. float flPathDistGoal;
  105. //---------------------------------
  106. //
  107. // If following a designer laid path, the path-corner entity (if any)
  108. //
  109. EHANDLE hPathCorner;
  110. // Data specific to the waypoint type:
  111. //
  112. // PATHCORNER: The path corner entity.
  113. // DOOR: If moving to position to open a door, the handle of the door to open.
  114. EHANDLE m_hData;
  115. private:
  116. int m_fWaypointFlags; // See WaypointFlags_t
  117. Navigation_t m_iWPType; // The type of waypoint
  118. AI_Waypoint_t *pNext;
  119. AI_Waypoint_t *pPrev;
  120. DECLARE_FIXEDSIZE_ALLOCATOR(AI_Waypoint_t);
  121. public:
  122. DECLARE_SIMPLE_DATADESC();
  123. };
  124. // ----------------------------------------------------------------------------
  125. // Inline methods associated with AI_Waypoint_t
  126. // ----------------------------------------------------------------------------
  127. inline int AI_Waypoint_t::Flags() const
  128. {
  129. return m_fWaypointFlags;
  130. }
  131. inline Navigation_t AI_Waypoint_t::NavType() const
  132. {
  133. return m_iWPType;
  134. }
  135. inline void AI_Waypoint_t::ModifyFlags( int fFlags, bool bEnable )
  136. {
  137. if (bEnable)
  138. m_fWaypointFlags |= fFlags;
  139. else
  140. m_fWaypointFlags &= ~fFlags;
  141. }
  142. inline void AI_Waypoint_t::SetNext( AI_Waypoint_t *p )
  143. {
  144. if (pNext)
  145. {
  146. pNext->pPrev = NULL;
  147. }
  148. pNext = p;
  149. if ( pNext )
  150. {
  151. if ( pNext->pPrev )
  152. pNext->pPrev->pNext = NULL;
  153. pNext->pPrev = this;
  154. }
  155. }
  156. inline void AI_Waypoint_t::SetPrev( AI_Waypoint_t *p )
  157. {
  158. if ( pPrev )
  159. {
  160. pPrev->pNext = NULL;
  161. }
  162. pPrev = p;
  163. if ( pPrev )
  164. {
  165. if ( pPrev->pNext )
  166. pPrev->pNext->pPrev = NULL;
  167. pPrev->pNext = this;
  168. }
  169. }
  170. // ----------------------------------------------------------------------------
  171. // Purpose: Holds an maintains a chain of waypoints
  172. class CAI_WaypointList
  173. {
  174. public:
  175. CAI_WaypointList()
  176. : m_pFirstWaypoint( NULL )
  177. {
  178. }
  179. CAI_WaypointList( AI_Waypoint_t *pFirstWaypoint)
  180. : m_pFirstWaypoint( pFirstWaypoint )
  181. {
  182. }
  183. void Set(AI_Waypoint_t* route);
  184. void PrependWaypoints( AI_Waypoint_t *pWaypoints );
  185. void PrependWaypoint( const Vector &newPoint, Navigation_t navType, unsigned waypointFlags, float flYaw = 0 );
  186. bool IsEmpty() const { return ( m_pFirstWaypoint == NULL ); }
  187. AI_Waypoint_t * GetFirst() { return m_pFirstWaypoint; }
  188. const AI_Waypoint_t *GetFirst() const { return m_pFirstWaypoint; }
  189. AI_Waypoint_t * GetLast();
  190. const AI_Waypoint_t *GetLast() const;
  191. void RemoveAll();
  192. private:
  193. AI_Waypoint_t* m_pFirstWaypoint; // Linked list of waypoints
  194. };
  195. // ----------------------------------------------------------------------------
  196. #ifdef DEBUG
  197. void AssertRouteValid( AI_Waypoint_t* route );
  198. #else
  199. #define AssertRouteValid( route ) ((void)0)
  200. #endif
  201. // ----------------------------------------------------------------------------
  202. // Utilities
  203. void DeleteAll( AI_Waypoint_t *pWaypointList );
  204. // ------------------------------------
  205. inline void DeleteAll( AI_Waypoint_t **ppWaypointList )
  206. {
  207. DeleteAll( *ppWaypointList );
  208. *ppWaypointList = NULL;
  209. }
  210. // ------------------------------------
  211. void AddWaypointLists(AI_Waypoint_t *pLeft, AI_Waypoint_t *pRight);
  212. // ----------------------------------------------------------------------------
  213. #endif // AI_WAYPOINT_H