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.

278 lines
7.8 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Squad classes
  4. //
  5. //=============================================================================//
  6. #ifndef AI_SQUAD_H
  7. #define AI_SQUAD_H
  8. #include "ai_memory.h"
  9. #include "ai_squadslot.h"
  10. #include "bitstring.h"
  11. class CAI_Squad;
  12. typedef CHandle<CAI_BaseNPC> AIHANDLE;
  13. #define PER_ENEMY_SQUADSLOTS 1
  14. //-----------------------------------------------------------------------------
  15. DECLARE_POINTER_HANDLE(AISquadsIter_t);
  16. DECLARE_POINTER_HANDLE(AISquadIter_t);
  17. #define MAX_SQUAD_MEMBERS 16
  18. #define MAX_SQUAD_DATA_SLOTS 4
  19. //-----------------------------------------------------------------------------
  20. // CAI_SquadManager
  21. //
  22. // Purpose: Manages all the squads in the system
  23. //
  24. //-----------------------------------------------------------------------------
  25. class CAI_SquadManager
  26. {
  27. public:
  28. CAI_SquadManager()
  29. {
  30. m_pSquads = NULL;
  31. }
  32. CAI_Squad * GetFirstSquad( AISquadsIter_t *pIter );
  33. CAI_Squad * GetNextSquad( AISquadsIter_t *pIter );
  34. int NumSquads();
  35. CAI_Squad * FindSquad( string_t squadName ); // Returns squad of the given name
  36. CAI_Squad * CreateSquad( string_t squadName ); // Returns squad of the given name
  37. CAI_Squad * FindCreateSquad( string_t squadName ); // Returns squad of the given name
  38. CAI_Squad * FindCreateSquad( CAI_BaseNPC *pNPC, string_t squadName ); // Returns squad of the given name
  39. void DeleteSquad( CAI_Squad *pSquad );
  40. void DeleteAllSquads(void);
  41. private:
  42. CAI_Squad * m_pSquads; // A linked list of all squads
  43. };
  44. //-------------------------------------
  45. extern CAI_SquadManager g_AI_SquadManager;
  46. //-----------------------------------------------------------------------------
  47. #ifdef PER_ENEMY_SQUADSLOTS
  48. struct AISquadEnemyInfo_t
  49. {
  50. EHANDLE hEnemy;
  51. CBitVec<MAX_SQUADSLOTS> slots; // What squad slots are filled?
  52. DECLARE_SIMPLE_DATADESC();
  53. };
  54. #endif
  55. //-----------------------------------------------------------------------------
  56. // CAI_Squad
  57. //
  58. // Purpose: Tracks enemies, squad slots, squad members
  59. //
  60. //-----------------------------------------------------------------------------
  61. class CAI_Squad
  62. {
  63. public:
  64. const char * GetName() const { return STRING(m_Name); }
  65. void RemoveFromSquad( CAI_BaseNPC *pNPC, bool bDeath = false );
  66. CAI_BaseNPC * GetFirstMember( AISquadIter_t *pIter = NULL, bool bIgnoreSilentMembers = true );
  67. CAI_BaseNPC * GetNextMember( AISquadIter_t *pIter, bool bIgnoreSilentMembers = true );
  68. CAI_BaseNPC * GetAnyMember();
  69. int NumMembers( bool bIgnoreSilentMembers = true );
  70. int GetSquadIndex( CAI_BaseNPC * );
  71. void SquadNewEnemy ( CBaseEntity *pEnemy );
  72. void UpdateEnemyMemory( CAI_BaseNPC *pUpdater, CBaseEntity *pEnemy, const Vector &position );
  73. bool OccupyStrategySlotRange( CBaseEntity *pEnemy, int slotIDStart, int slotIDEnd, int *pSlot );
  74. void VacateStrategySlot( CBaseEntity *pEnemy, int slot);
  75. bool IsStrategySlotRangeOccupied( CBaseEntity *pEnemy, int slotIDStart, int slotIDEnd );
  76. CAI_BaseNPC * SquadMemberInRange( const Vector &vecLocation, float flDist );
  77. CAI_BaseNPC * NearestSquadMember( CAI_BaseNPC *pMember );
  78. int GetVisibleSquadMembers( CAI_BaseNPC *pMember );
  79. CAI_BaseNPC * GetSquadMemberNearestTo( const Vector &vecLocation );
  80. bool SquadIsMember( CBaseEntity *pMember );
  81. bool IsLeader( CAI_BaseNPC *pLeader );
  82. CAI_BaseNPC *GetLeader( void );
  83. Vector ComputeSquadCentroid( bool bIncludeSilentMembers, CBaseCombatCharacter *pExcludeMember );
  84. int BroadcastInteraction( int interactionType, void *data, CBaseCombatCharacter *sender = NULL );
  85. void AddToSquad(CAI_BaseNPC *pNPC);
  86. bool FOkToMakeSound( int soundPriority );
  87. void JustMadeSound( int soundPriority, float time );
  88. float GetSquadSoundWaitTime() const { return m_flSquadSoundWaitTime; }
  89. void SetSquadSoundWaitTime( float time ) { m_flSquadSoundWaitTime = time; }
  90. void SquadRemember( int iMemory );
  91. void SetSquadInflictor( CBaseEntity *pInflictor );
  92. bool IsSquadInflictor( CBaseEntity *pInflictor );
  93. static bool IsSilentMember( const CAI_BaseNPC *pNPC );
  94. template <typename T>
  95. void SetSquadData( unsigned slot, const T &data )
  96. {
  97. Assert( slot < MAX_SQUAD_DATA_SLOTS );
  98. if ( slot < MAX_SQUAD_DATA_SLOTS )
  99. {
  100. m_SquadData[slot] = *((int *)&data);
  101. }
  102. }
  103. template <typename T>
  104. void GetSquadData( unsigned slot, T *pData )
  105. {
  106. Assert( slot < MAX_SQUAD_DATA_SLOTS );
  107. if ( slot < MAX_SQUAD_DATA_SLOTS )
  108. {
  109. *pData = *((T *)&m_SquadData[slot]);
  110. }
  111. }
  112. private:
  113. void OccupySlot( CBaseEntity *pEnemy, int i );
  114. void VacateSlot( CBaseEntity *pEnemy, int i );
  115. bool IsSlotOccupied( CBaseEntity *pEnemy, int i ) const;
  116. private:
  117. friend class CAI_SaveRestoreBlockHandler;
  118. friend class CAI_SquadManager;
  119. CAI_Squad();
  120. CAI_Squad(string_t squadName);
  121. ~CAI_Squad(void);
  122. CAI_Squad* GetNext() { return m_pNextSquad; }
  123. void Init( string_t squadName );
  124. CAI_Squad * m_pNextSquad; // The next squad is list of all squads
  125. string_t m_Name;
  126. CUtlVectorFixed<AIHANDLE, MAX_SQUAD_MEMBERS> m_SquadMembers;
  127. float m_flSquadSoundWaitTime; // Time when I'm allowed to make another sound
  128. int m_nSquadSoundPriority; // if we're still waiting, this is the priority of the current sound
  129. EHANDLE m_hSquadInflictor;
  130. int m_SquadData[MAX_SQUAD_DATA_SLOTS];
  131. #ifdef PER_ENEMY_SQUADSLOTS
  132. AISquadEnemyInfo_t *FindEnemyInfo( CBaseEntity *pEnemy );
  133. const AISquadEnemyInfo_t *FindEnemyInfo( CBaseEntity *pEnemy ) const { return const_cast<CAI_Squad *>(this)->FindEnemyInfo( pEnemy ); }
  134. AISquadEnemyInfo_t * m_pLastFoundEnemyInfo; // Occupy/Vacate need to be reworked to not want this
  135. CUtlVector<AISquadEnemyInfo_t> m_EnemyInfos;
  136. float m_flEnemyInfoCleanupTime;
  137. #else
  138. CVarBitVec m_squadSlotsUsed; // What squad slots are filled?
  139. #endif
  140. //---------------------------------
  141. public:
  142. DECLARE_SIMPLE_DATADESC();
  143. };
  144. //-----------------------------------------------------------------------------
  145. //
  146. // Purpose: CAI_SquadManager inline functions
  147. //
  148. //-----------------------------------------------------------------------------
  149. inline CAI_Squad *CAI_SquadManager::GetFirstSquad( AISquadsIter_t *pIter )
  150. {
  151. *pIter = (AISquadsIter_t)m_pSquads;
  152. return m_pSquads;
  153. }
  154. //-------------------------------------
  155. inline CAI_Squad *CAI_SquadManager::GetNextSquad( AISquadsIter_t *pIter )
  156. {
  157. CAI_Squad *pSquad = (CAI_Squad *)*pIter;
  158. if ( pSquad )
  159. pSquad = pSquad->m_pNextSquad;
  160. *pIter = (AISquadsIter_t)pSquad;
  161. return pSquad;
  162. }
  163. //-------------------------------------
  164. // Purpose: Returns squad of the given name or creates a new squad with the
  165. // given name if none exists and add pNPC to the list of members
  166. //-------------------------------------
  167. inline CAI_Squad *CAI_SquadManager::FindCreateSquad(CAI_BaseNPC *pNPC, string_t squadName)
  168. {
  169. CAI_Squad* pSquad = FindSquad( squadName );
  170. if ( !pSquad )
  171. pSquad = CreateSquad( squadName );
  172. pSquad->AddToSquad( pNPC );
  173. return pSquad;
  174. }
  175. //-----------------------------------------------------------------------------
  176. inline CAI_Squad *CAI_SquadManager::FindCreateSquad(string_t squadName)
  177. {
  178. CAI_Squad* pSquad = FindSquad( squadName );
  179. if ( !pSquad )
  180. pSquad = CreateSquad( squadName );
  181. return pSquad;
  182. }
  183. //-------------------------------------
  184. inline CAI_BaseNPC *CAI_Squad::GetAnyMember()
  185. {
  186. if ( m_SquadMembers.Count() )
  187. return m_SquadMembers[random->RandomInt( 0, m_SquadMembers.Count()-1 )];
  188. return NULL;
  189. }
  190. //-------------------------------------
  191. inline int CAI_Squad::GetSquadIndex( CAI_BaseNPC *pAI )
  192. {
  193. for ( int i = 0; i < m_SquadMembers.Count(); i++ )
  194. {
  195. if ( m_SquadMembers[i] == pAI )
  196. return i;
  197. }
  198. return -1;
  199. }
  200. //-----------------------------------------------------------------------------
  201. #endif // AI_SQUAD_H