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.

246 lines
7.8 KiB

  1. //========= Copyright � 1996-2005, 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. // Core data management
  72. CAI_Network();
  73. ~CAI_Network();
  74. CAI_Node * AddNode( const Vector &origin, float yaw ); // Returns a new node in the network
  75. CAI_Link * CreateLink( int srcID, int destID, CAI_DynamicLink *pDynamicLink = NULL );
  76. bool IsConnected(int srcID, int destID); // Use during run time
  77. void TestIsConnected(int startID, int endID); // Use only for initialization!
  78. Vector GetNodePosition( CBaseCombatCharacter *pNPC, int nodeID );
  79. Vector GetNodePosition( Hull_t hull, int nodeID );
  80. float GetNodeYaw( int nodeID );
  81. static int FindBSSmallest(CVarBitVec *bitString, float *float_array, int array_size);
  82. int NumNodes() const { return m_iNumNodes; }
  83. CAI_Node* GetNode( int id, bool bHandleError = true ) const
  84. {
  85. if ( id >= 0 &&
  86. id < m_iNumNodes )
  87. {
  88. return m_pAInode[id];
  89. }
  90. if ( bHandleError )
  91. {
  92. static int warningCount = 0;
  93. if ( ++warningCount < 10 )
  94. {
  95. AssertMsg2( 0, "Node (%i) out of range (%i total)\n", id, m_iNumNodes );
  96. }
  97. }
  98. return NULL;
  99. }
  100. CAI_Node** AccessNodes() const { return m_pAInode; }
  101. //////////////////////////////////
  102. // Tools and utility functions
  103. int NearestNodeToPoint( CAI_BaseNPC* pNPC, const Vector &vecOrigin, bool bCheckVisiblity, INearestNodeFilter *pFilter );
  104. int NearestNodeToPoint( CAI_BaseNPC* pNPC, const Vector &vecOrigin, bool bCheckVisiblity = true ) { return NearestNodeToPoint( pNPC, vecOrigin, bCheckVisiblity, NULL ); }
  105. int NearestNodeToPoint(const Vector &vPosition, bool bCheckVisiblity = true );
  106. /** @brief Callback lets you customize FindNodeDistanceAwayFromStart to accept or reject specific nodes based on other criteria
  107. To use, inherit from this and override Validate(). It's like a closure.
  108. */
  109. class IPathingNodeValidator
  110. {
  111. public:
  112. /// FindNodeDistanceAwayFromStart will pass each node it considers acceptable into this function and will reject ones that return false.
  113. /// NOTE: this must be const!
  114. virtual bool Validate( const CAI_Node *pNode, const CAI_Network * const pNetwork ) const { return true; } /// < default impl accepts everything
  115. };
  116. /// Experimental: starting at a given nav-node, walk the network to try to find a node at least
  117. /// mindist away from a point yet no more than maxdist. Returns NULL on failure. Recursive.
  118. /// supply squares of min, max distance.
  119. CAI_Node * FindNodeDistanceAwayFromStart( CAI_Node *pStartNode, const Vector &point, float minDistSq, float maxDistSq, const Hull_t hulltype, const Capability_t movetype, const IPathingNodeValidator &validator = IPathingNodeValidator() ) RESTRICT;
  120. private:
  121. friend class CAI_NetworkManager;
  122. virtual IterationRetval_t EnumElement( IHandleEntity *pHandleEntity );
  123. int GetCachedNearestNode(const Vector &checkPos, CAI_BaseNPC *pNPC, int *pCachePos );
  124. void SetCachedNearestNode(const Vector &checkPos, int nodeID, Hull_t nHull);
  125. int GetCachedNode(const Vector &checkPos, Hull_t nHull, int *pCachePos);
  126. int ListNodesInBox( CNodeList &list, int maxListCount, const Vector &mins, const Vector &maxs, INodeListFilter *pFilter );
  127. //---------------------------------
  128. enum
  129. {
  130. NEARNODE_CACHE_SIZE = 32,
  131. NEARNODE_CACHE_LIFE = 10,
  132. };
  133. struct NearNodeCache_T
  134. {
  135. Vector vTestPosition;
  136. float expiration; // Time tested
  137. int node; // Nearest Node to position
  138. int hull; // Hull type tested (or HULL_NONE is only visibility tested)
  139. };
  140. int m_iNumNodes; // Number of nodes in this network
  141. CAI_Node** m_pAInode; // Array of all nodes in this network
  142. enum
  143. {
  144. PARTITION_NODE = ( 1 << 0 )
  145. };
  146. NearNodeCache_T m_NearestCache[NEARNODE_CACHE_SIZE]; // Cache of nearest nodes
  147. int m_iNearestCacheNext; // Oldest record in the cache
  148. #ifdef AI_NODE_TREE
  149. ISpatialPartition * m_pNodeTree;
  150. CUtlVector<int> m_GatheredNodes;
  151. #endif
  152. };
  153. //-----------------------------------------------------------------------------
  154. // CAI_NetworkEditTools
  155. //
  156. // Purpose: Provides the operations used when building levels, whether in-game
  157. // debugging tools or editor related items.
  158. //
  159. //-----------------------------------------------------------------------------
  160. // ------------------------------------
  161. // Debug overlay bits
  162. enum DebugNetOverlayBits_e
  163. {
  164. bits_debugOverlayNodes = 0x00000001, // show node
  165. bits_debugOverlayNodesLev2 = 0x00000002, // show nodes and text
  166. bits_debugOverlayHulls = 0x00000004, // show hulls
  167. bits_debugOverlayConnections = 0x00000008, // show connections
  168. bits_debugOverlayVisibility = 0x00000010, // show visibility
  169. bits_debugOverlayGraphConnect = 0x00000020, // show graph connectivity
  170. bits_debugOverlayGrid = 0x00000040, // show grid
  171. bits_debugOverlayHints = 0x00000080, // show hints
  172. bits_debugOverlayJumpConnections= 0x00000100, // show jump connections
  173. bits_debugOverlayFlyConnections = 0x00000200, // show fly connections
  174. bits_debugOverlayCrawlConnections = 0x00000400, // show crawl connections
  175. bits_debugNeedRebuild = 0x10000000, // network needs rebuilding
  176. };
  177. // ------------------------------------
  178. // ----------------
  179. //-----------------------------------------------------------------------------
  180. // Useful utility function defined by AI_network.cpp
  181. Vector PointOnLineNearestPoint(const Vector& vStartPos, const Vector& vEndPos, const Vector& vPoint);
  182. //-----------------------------------------------------------------------------
  183. // For now just using one big AI network
  184. extern CAI_NetworkManager * g_pAINetworkManager;
  185. extern CAI_Network * g_pBigAINet;
  186. //=============================================================================
  187. #endif // AI_NETWORK_H