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.

217 lines
6.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef AI_NETWORKMANAGER_H
  7. #define AI_NETWORKMANAGER_H
  8. #include "utlvector.h"
  9. #include "bitstring.h"
  10. #include "threadtools.h"
  11. #if defined( _WIN32 )
  12. #pragma once
  13. #endif
  14. class CAI_NetworkEditTools;
  15. class CAI_Network;
  16. class CAI_Node;
  17. class CAI_Link;
  18. class CAI_TestHull;
  19. extern ConVar g_ai_threadedgraphbuild;
  20. //-----------------------------------------------------------------------------
  21. // CAI_NetworkManager
  22. //
  23. // Purpose: The entity in the level responsible for building the network if it
  24. // isn't there, saving & loading of the network, and holding the
  25. // CAI_Network instance.
  26. //
  27. //-----------------------------------------------------------------------------
  28. class CAI_NetworkManager : public CPointEntity
  29. {
  30. public:
  31. static void InitializeAINetworks();
  32. DECLARE_DATADESC();
  33. DECLARE_CLASS( CAI_NetworkManager, CPointEntity );
  34. public:
  35. CAI_NetworkManager(void);
  36. virtual ~CAI_NetworkManager(void);
  37. void Spawn ();
  38. virtual int ObjectCaps( void ) { return BaseClass::ObjectCaps() | FCAP_DONT_SAVE; }
  39. void RebuildNetworkGraph(); // Used in WC edit mode
  40. void StartRebuild(); // Used in WC edit mode
  41. void LoadNetworkGraph();
  42. static bool NetworksLoaded() { return gm_fNetworksLoaded; }
  43. bool IsInitialized() { return m_fInitalized; }
  44. void BuildNetworkGraph();
  45. static void DeleteAllAINetworks();
  46. void FixupHints();
  47. void MarkDontSaveGraph();
  48. bool NeedsRebuild( void ) { return m_bNeedGraphRebuild; }
  49. public:
  50. CAI_NetworkEditTools * GetEditOps() { return m_pEditOps; }
  51. CAI_Network * GetNetwork() { Assert(!m_ThreadedBuild.pBuildingNetwork); return m_pNetwork; }
  52. private:
  53. void DelayedInit();
  54. void ThreadedInit(); ///< experimental
  55. void RebuildThink();
  56. void SaveNetworkGraph( void) ;
  57. static bool IsAIFileCurrent( const char *szMapName );
  58. static bool gm_fNetworksLoaded; // Have AINetworks been loaded
  59. bool m_bNeedGraphRebuild;
  60. CAI_NetworkEditTools * m_pEditOps;
  61. CAI_Network * m_pNetwork;
  62. bool m_fInitalized;
  63. bool m_bDontSaveGraph;
  64. /// experimental -- to be replaced with a real job hopefully.
  65. /// affinity should be the second hardware thread on the server core
  66. struct ThreadedGraphBuildData
  67. {
  68. CThreadMutex mutex;
  69. CAI_Network *pBuildingNetwork;
  70. CInterlockedInt nBuildStage;
  71. ThreadHandle_t job; ///< hopefully will replace w/ real job
  72. enum { BUILD_NOT_STARTED, BUILD_UNDERWAY, BUILD_DONE, };
  73. ThreadedGraphBuildData() : pBuildingNetwork(NULL), nBuildStage(BUILD_NOT_STARTED), job(NULL) {}
  74. } m_ThreadedBuild;
  75. static uintp ThreadedBuildJob( /* (ThreadedGraphBuildData *) */ void *pBuildData );
  76. };
  77. //-----------------------------------------------------------------------------
  78. abstract_class CAI_NetworkBuildHelper : public CLogicalEntity
  79. {
  80. DECLARE_CLASS( CAI_NetworkBuildHelper, CLogicalEntity );
  81. public:
  82. virtual void PostInitNodePosition( CAI_Network *pNetwork, CAI_Node *pNode ) = 0;
  83. };
  84. //-----------------------------------------------------------------------------
  85. class CAI_NetworkBuilder
  86. {
  87. public:
  88. void Build( CAI_Network *pNetwork );
  89. void Rebuild( CAI_Network *pNetwork );
  90. void InitNodePosition( CAI_Network *pNetwork, CAI_Node *pNode );
  91. void InitZones( CAI_Network *pNetwork );
  92. private:
  93. void InitVisibility( CAI_Network *pNetwork, CAI_Node *pNode );
  94. void InitNeighbors( CAI_Network *pNetwork, CAI_Node *pNode );
  95. void InitClimbNodePosition( CAI_Network *pNetwork, CAI_Node *pNode );
  96. void InitGroundNodePosition( CAI_Network *pNetwork, CAI_Node *pNode );
  97. void InitLinks( CAI_Network *pNetwork, CAI_Node *pNode );
  98. void ForceDynamicLinkNeighbors();
  99. void FloodFillZone( CAI_Node **ppNodes, CAI_Node *pNode, int zone );
  100. int ComputeConnection( CAI_Node *pSrcNode, CAI_Node *pDestNode, Hull_t hull );
  101. void BeginBuild();
  102. void EndBuild();
  103. CUtlVector<CVarBitVec> m_NeighborsTable;
  104. CVarBitVec m_DidSetNeighborsTable;
  105. CAI_TestHull * m_pTestHull;
  106. };
  107. extern CAI_NetworkBuilder g_AINetworkBuilder;
  108. //-----------------------------------------------------------------------------
  109. // CAI_NetworkEditTools
  110. //
  111. // Purpose: Bridge class to Hammer node editing functionality
  112. //
  113. //-----------------------------------------------------------------------------
  114. class CAI_NetworkEditTools
  115. {
  116. public:
  117. CAI_NetworkEditTools(CAI_NetworkManager *);
  118. ~CAI_NetworkEditTools();
  119. // ----------------------
  120. // Debug & Edit fields
  121. // ----------------------
  122. static CAI_Node * m_pLastDeletedNode; // For undo in wc edit mode
  123. static int m_iHullDrawNum; // Which hulls to draw
  124. static int m_iVisibilityNode; // Node I'm showing visibility for
  125. static int m_iGConnectivityNode; // Node I'm showing graph connectivity for
  126. static bool m_bAirEditMode; // Editing Air Nodes
  127. static bool m_bLinkEditMode; // Editing Links
  128. static float m_flAirEditDistance; // Distance editing Air Nodes
  129. static void DrawHull(Hull_t eHull);
  130. static void DrawNextHull(const char *ainet_name); // Draws next hull set for the named ai network
  131. static void SetDebugBits(const char *ainet_name,int debug_bit);
  132. static CAI_Node * FindAINodeNearestFacing( const Vector &origin, const Vector &facing, float threshold, int nNodeType);
  133. static CAI_Link * FindAILinkNearestFacing( const Vector &origin, const Vector &facing, float threshold);
  134. //---------------
  135. // WC Editing
  136. //---------------
  137. int m_nNextWCIndex; // Next unused index used by WC
  138. Vector * m_pWCPosition; // Array of vectors only used in wc edit mode
  139. //-----------------
  140. // Debugging Tools
  141. //-----------------
  142. int m_debugNetOverlays; // Which network debug overlays to draw
  143. void DrawAINetworkOverlay(void); // Draw network on the client
  144. void RecalcUsableNodesForHull(void); // Used only for debug drawing
  145. //-----------------
  146. void OnInit();
  147. int GetNodeIdFromWCId( int nWCId );
  148. int GetWCIdFromNodeId( int nNodeId );
  149. int * m_pNodeIndexTable; // Table of WC Id's to Engine Id's
  150. void ClearRebuildFlags();
  151. void SetRebuildFlags();
  152. void DrawEditInfoOverlay();
  153. #ifdef AI_PERF_MON
  154. //----------------------
  155. // Performance stats
  156. //----------------------
  157. static int m_nPerfStatNN;
  158. static int m_nPerfStatPB;
  159. static float m_fNextPerfStatTime;
  160. #endif
  161. CAI_NetworkManager *m_pManager;
  162. CAI_Network * m_pNetwork;
  163. };
  164. //-----------------------------------------------------------------------------
  165. #endif // AI_NETWORKMANAGER_H