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.

221 lines
6.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef AI_NETWORK_H
  8. #define AI_NETWORK_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "ispatialpartition.h"
  13. #include "utlpriorityqueue.h"
  14. // ------------------------------------
  15. class CAI_Node;
  16. class CVarBitVec;
  17. class INodeListFilter;
  18. struct AI_Waypoint_t;
  19. class CAI_BaseNPC;
  20. class CAI_Link;
  21. class CAI_DynamicLink;
  22. //-----------------------------------------------------------------------------
  23. class CAI_NetworkManager;
  24. //-----------------------------------------------------------------------------
  25. #define AI_MAX_NODE_LINKS 30
  26. #define MAX_NODES 1500
  27. //-----------------------------------------------------------------------------
  28. //
  29. // Utility classes used by CAI_Network
  30. //
  31. //-----------------------------------------------------------------------------
  32. abstract_class INearestNodeFilter
  33. {
  34. public:
  35. virtual bool IsValid( CAI_Node *pNode ) = 0;
  36. virtual bool ShouldContinue() = 0;
  37. };
  38. //-------------------------------------
  39. struct AI_NearNode_t
  40. {
  41. AI_NearNode_t() {}
  42. AI_NearNode_t( int index, float nodedist ) { dist = nodedist; nodeIndex = index; }
  43. float dist;
  44. int nodeIndex;
  45. };
  46. //-------------------------------------
  47. class CNodeList : public CUtlPriorityQueue<AI_NearNode_t>
  48. {
  49. public:
  50. static bool IsLowerPriority( const AI_NearNode_t &node1, const AI_NearNode_t &node2 )
  51. {
  52. // nodes with greater distance are lower priority
  53. return node1.dist > node2.dist;
  54. }
  55. static bool RevIsLowerPriority( const AI_NearNode_t &node1, const AI_NearNode_t &node2 )
  56. {
  57. // nodes with lower distance are lower priority
  58. return node2.dist > node1.dist;
  59. }
  60. CNodeList( int growSize = 0, int initSize = 0 ) : CUtlPriorityQueue<AI_NearNode_t>( growSize, initSize, IsLowerPriority ) {}
  61. CNodeList( AI_NearNode_t *pMemory, int count ) : CUtlPriorityQueue<AI_NearNode_t>( pMemory, count, IsLowerPriority ) {}
  62. };
  63. //-----------------------------------------------------------------------------
  64. // CAI_Network
  65. //
  66. // Purpose: Stores a node graph through which an AI may pathfind
  67. //-----------------------------------------------------------------------------
  68. class CAI_Network : public IPartitionEnumerator
  69. {
  70. public:
  71. CAI_Network();
  72. ~CAI_Network();
  73. CAI_Node * AddNode( const Vector &origin, float yaw ); // Returns a new node in the network
  74. CAI_Link * CreateLink( int srcID, int destID, CAI_DynamicLink *pDynamicLink = NULL );
  75. bool IsConnected(int srcID, int destID); // Use during run time
  76. void TestIsConnected(int startID, int endID); // Use only for initialization!
  77. Vector GetNodePosition( CBaseCombatCharacter *pNPC, int nodeID );
  78. Vector GetNodePosition( Hull_t hull, int nodeID );
  79. float GetNodeYaw( int nodeID );
  80. static int FindBSSmallest(CVarBitVec *bitString, float *float_array, int array_size);
  81. int NearestNodeToPoint( CAI_BaseNPC* pNPC, const Vector &vecOrigin, bool bCheckVisiblity, INearestNodeFilter *pFilter );
  82. int NearestNodeToPoint( CAI_BaseNPC* pNPC, const Vector &vecOrigin, bool bCheckVisiblity = true ) { return NearestNodeToPoint( pNPC, vecOrigin, bCheckVisiblity, NULL ); }
  83. int NearestNodeToPoint(const Vector &vPosition, bool bCheckVisiblity = true );
  84. int NumNodes() const { return m_iNumNodes; }
  85. CAI_Node* GetNode( int id, bool bHandleError = true )
  86. {
  87. if ( id >= 0 &&
  88. id < m_iNumNodes )
  89. {
  90. return m_pAInode[id];
  91. }
  92. if ( bHandleError )
  93. {
  94. static int warningCount = 0;
  95. if ( ++warningCount < 10 )
  96. {
  97. AssertMsg2( 0, "Node (%i) out of range (%i total)\n", id, m_iNumNodes );
  98. }
  99. }
  100. return NULL;
  101. }
  102. CAI_Node** AccessNodes() const { return m_pAInode; }
  103. private:
  104. friend class CAI_NetworkManager;
  105. virtual IterationRetval_t EnumElement( IHandleEntity *pHandleEntity );
  106. int GetCachedNearestNode(const Vector &checkPos, CAI_BaseNPC *pNPC, int *pCachePos );
  107. void SetCachedNearestNode(const Vector &checkPos, int nodeID, Hull_t nHull);
  108. int GetCachedNode(const Vector &checkPos, Hull_t nHull, int *pCachePos);
  109. int ListNodesInBox( CNodeList &list, int maxListCount, const Vector &mins, const Vector &maxs, INodeListFilter *pFilter );
  110. //---------------------------------
  111. enum
  112. {
  113. NEARNODE_CACHE_SIZE = 32,
  114. NEARNODE_CACHE_LIFE = 10,
  115. };
  116. struct NearNodeCache_T
  117. {
  118. Vector vTestPosition;
  119. float expiration; // Time tested
  120. int node; // Nearest Node to position
  121. int hull; // Hull type tested (or HULL_NONE is only visibility tested)
  122. };
  123. int m_iNumNodes; // Number of nodes in this network
  124. CAI_Node** m_pAInode; // Array of all nodes in this network
  125. enum
  126. {
  127. PARTITION_NODE = ( 1 << 0 )
  128. };
  129. NearNodeCache_T m_NearestCache[NEARNODE_CACHE_SIZE]; // Cache of nearest nodes
  130. int m_iNearestCacheNext; // Oldest record in the cache
  131. #ifdef AI_NODE_TREE
  132. ISpatialPartition * m_pNodeTree;
  133. CUtlVector<int> m_GatheredNodes;
  134. #endif
  135. };
  136. //-----------------------------------------------------------------------------
  137. // CAI_NetworkEditTools
  138. //
  139. // Purpose: Provides the operations used when building levels, whether in-game
  140. // debugging tools or editor related items.
  141. //
  142. //-----------------------------------------------------------------------------
  143. // ------------------------------------
  144. // Debug overlay bits
  145. enum DebugNetOverlayBits_e
  146. {
  147. bits_debugOverlayNodes = 0x00000001, // show node
  148. bits_debugOverlayNodesLev2 = 0x00000002, // show nodes and text
  149. bits_debugOverlayHulls = 0x00000004, // show hulls
  150. bits_debugOverlayConnections = 0x00000008, // show connections
  151. bits_debugOverlayVisibility = 0x00000010, // show visibility
  152. bits_debugOverlayGraphConnect = 0x00000020, // show graph connectivity
  153. bits_debugOverlayGrid = 0x00000040, // show grid
  154. bits_debugOverlayHints = 0x00000080, // show hints
  155. bits_debugOverlayJumpConnections= 0x00000100, // show jump connections
  156. bits_debugOverlayFlyConnections = 0x00000200, // show fly connections
  157. bits_debugNeedRebuild = 0x10000000, // network needs rebuilding
  158. };
  159. // ------------------------------------
  160. // ----------------
  161. //-----------------------------------------------------------------------------
  162. // Useful utility function defined by AI_network.cpp
  163. Vector PointOnLineNearestPoint(const Vector& vStartPos, const Vector& vEndPos, const Vector& vPoint);
  164. //-----------------------------------------------------------------------------
  165. // For now just using one big AI network
  166. extern CAI_NetworkManager * g_pAINetworkManager;
  167. extern CAI_Network * g_pBigAINet;
  168. //=============================================================================
  169. #endif // AI_NETWORK_H