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.

209 lines
5.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "cbase.h"
  8. #include "mempool.h"
  9. #include "ai_navtype.h"
  10. #include "ai_node.h"
  11. #include "ai_waypoint.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. #define WAYPOINT_POOL_SIZE 512
  15. //-----------------------------------------------------------------------------
  16. // Init static variables
  17. //-----------------------------------------------------------------------------
  18. DEFINE_FIXEDSIZE_ALLOCATOR( AI_Waypoint_t, WAYPOINT_POOL_SIZE, CUtlMemoryPool::GROW_FAST );
  19. //-------------------------------------
  20. BEGIN_SIMPLE_DATADESC( AI_Waypoint_t )
  21. DEFINE_FIELD( vecLocation, FIELD_POSITION_VECTOR),
  22. DEFINE_FIELD( flYaw, FIELD_FLOAT ),
  23. // iNodeID (not saved, cannot rely on consistent mapping)
  24. // flPathDistGoal (not saved )
  25. DEFINE_FIELD( hPathCorner, FIELD_EHANDLE ),
  26. DEFINE_FIELD( m_hData, FIELD_EHANDLE ),
  27. DEFINE_FIELD( m_fWaypointFlags, FIELD_INTEGER ),
  28. DEFINE_FIELD( m_iWPType, FIELD_INTEGER ),
  29. // pNext
  30. // pPrev
  31. END_DATADESC()
  32. //-------------------------------------
  33. AI_Waypoint_t::AI_Waypoint_t()
  34. {
  35. memset( this, 0, sizeof(*this) );
  36. vecLocation = vec3_invalid;
  37. iNodeID = NO_NODE;
  38. flPathDistGoal = -1;
  39. }
  40. //-------------------------------------
  41. AI_Waypoint_t::AI_Waypoint_t( const Vector &initPosition, float initYaw, Navigation_t initNavType, int initWaypointFlags, int initNodeID )
  42. {
  43. memset( this, 0, sizeof(*this) );
  44. // A Route of length one to the endpoint
  45. vecLocation = initPosition;
  46. flYaw = initYaw;
  47. m_iWPType = initNavType;
  48. m_fWaypointFlags = initWaypointFlags;
  49. iNodeID = initNodeID;
  50. flPathDistGoal = -1;
  51. }
  52. //-------------------------------------
  53. AI_Waypoint_t * AI_Waypoint_t::GetLast()
  54. {
  55. Assert( !pNext || pNext->pPrev == this );
  56. AI_Waypoint_t *pCurr = this;
  57. while (pCurr->GetNext())
  58. {
  59. pCurr = pCurr->GetNext();
  60. }
  61. return pCurr;
  62. }
  63. //-----------------------------------------------------------------------------
  64. void CAI_WaypointList::RemoveAll()
  65. {
  66. DeleteAll( &m_pFirstWaypoint );
  67. Assert( m_pFirstWaypoint == NULL );
  68. }
  69. //-------------------------------------
  70. void CAI_WaypointList::PrependWaypoints( AI_Waypoint_t *pWaypoints )
  71. {
  72. AddWaypointLists( pWaypoints, GetFirst() );
  73. Set( pWaypoints );
  74. }
  75. //-------------------------------------
  76. void CAI_WaypointList::PrependWaypoint( const Vector &newPoint, Navigation_t navType, unsigned waypointFlags, float flYaw )
  77. {
  78. PrependWaypoints( new AI_Waypoint_t( newPoint, flYaw, navType, waypointFlags, NO_NODE ) );
  79. }
  80. //-------------------------------------
  81. void CAI_WaypointList::Set(AI_Waypoint_t* route)
  82. {
  83. m_pFirstWaypoint = route;
  84. }
  85. //-------------------------------------
  86. AI_Waypoint_t *CAI_WaypointList::GetLast()
  87. {
  88. AI_Waypoint_t *p = GetFirst();
  89. if (!p)
  90. return NULL;
  91. while ( p->GetNext() )
  92. p = p->GetNext();
  93. return p;
  94. }
  95. //-------------------------------------
  96. const AI_Waypoint_t *CAI_WaypointList::GetLast() const
  97. {
  98. return const_cast<CAI_WaypointList *>(this)->GetLast();
  99. }
  100. //-------------------------------------
  101. #ifdef DEBUG
  102. void AssertRouteValid( AI_Waypoint_t* route )
  103. {
  104. // Check that the goal wasn't just clobbered
  105. if (route)
  106. {
  107. AI_Waypoint_t* waypoint = route;
  108. while (waypoint)
  109. {
  110. #ifdef _GOALDEBUG
  111. if (!waypoint->GetNext() && !(waypoint->Flags() & (bits_WP_TO_GOAL|bits_WP_TO_PATHCORNER)))
  112. {
  113. DevMsg( "!!ERROR!! Final waypoint is not a goal!\n");
  114. }
  115. #endif
  116. waypoint->AssertValid();
  117. waypoint = waypoint->GetNext();
  118. }
  119. }
  120. }
  121. #endif
  122. //-----------------------------------------------------------------------------
  123. // Purpose: Deletes a waypoint linked list
  124. //-----------------------------------------------------------------------------
  125. void DeleteAll( AI_Waypoint_t *pWaypointList )
  126. {
  127. while ( pWaypointList )
  128. {
  129. AI_Waypoint_t *pPrevWaypoint = pWaypointList;
  130. pWaypointList = pWaypointList->GetNext();
  131. delete pPrevWaypoint;
  132. }
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose: Adds addRoute to the end of oldRoute
  136. //-----------------------------------------------------------------------------
  137. void AddWaypointLists(AI_Waypoint_t *oldRoute, AI_Waypoint_t *addRoute)
  138. {
  139. // Add to the end of the route
  140. AI_Waypoint_t *waypoint = oldRoute;
  141. while (waypoint->GetNext())
  142. {
  143. waypoint = waypoint->GetNext();
  144. }
  145. waypoint->ModifyFlags( bits_WP_TO_GOAL, false );
  146. // Check for duplication, but copy the type
  147. if (waypoint->iNodeID != NO_NODE &&
  148. waypoint->iNodeID == addRoute->iNodeID )
  149. {
  150. // waypoint->iWPType = addRoute->iWPType; <<TODO>> found case where this was bad
  151. AI_Waypoint_t *pNext = addRoute->GetNext();
  152. delete addRoute;
  153. waypoint->SetNext(pNext);
  154. }
  155. else
  156. {
  157. waypoint->SetNext(addRoute);
  158. }
  159. while (waypoint->GetNext())
  160. {
  161. waypoint = waypoint->GetNext();
  162. }
  163. waypoint->ModifyFlags( bits_WP_TO_GOAL, true );
  164. }
  165. //-----------------------------------------------------------------------------