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.

211 lines
5.6 KiB

  1. // NextBotManager.h
  2. // Author: Michael Booth, May 2006
  3. //========= Copyright Valve Corporation, All rights reserved. ============//
  4. #ifndef _NEXT_BOT_MANAGER_H_
  5. #define _NEXT_BOT_MANAGER_H_
  6. #include "NextBotInterface.h"
  7. class CTerrorPlayer;
  8. //----------------------------------------------------------------------------------------------------------------
  9. /**
  10. * The NextBotManager manager
  11. */
  12. class NextBotManager
  13. {
  14. public:
  15. NextBotManager( void );
  16. virtual ~NextBotManager();
  17. void Reset( void ); // reset to initial state
  18. virtual void Update( void );
  19. bool ShouldUpdate( INextBot *bot );
  20. void NotifyBeginUpdate( INextBot *bot );
  21. void NotifyEndUpdate( INextBot *bot );
  22. int GetNextBotCount( void ) const; // How many nextbots are alive right now?
  23. /**
  24. * Populate given vector with all bots in the system
  25. */
  26. void CollectAllBots( CUtlVector< INextBot * > *botVector );
  27. /**
  28. * DEPRECATED: Use CollectAllBots().
  29. * Execute functor for each NextBot in the system.
  30. * If a functor returns false, stop iteration early
  31. * and return false.
  32. */
  33. template < typename Functor >
  34. bool ForEachBot( Functor &func )
  35. {
  36. for( int i=m_botList.Head(); i != m_botList.InvalidIndex(); i = m_botList.Next( i ) )
  37. {
  38. if ( !func( m_botList[i] ) )
  39. {
  40. return false;
  41. }
  42. }
  43. return true;
  44. }
  45. /**
  46. * DEPRECATED: Use CollectAllBots().
  47. * Execute functor for each NextBot in the system as
  48. * a CBaseCombatCharacter.
  49. * If a functor returns false, stop iteration early
  50. * and return false.
  51. */
  52. template < typename Functor >
  53. bool ForEachCombatCharacter( Functor &func )
  54. {
  55. for( int i=m_botList.Head(); i != m_botList.InvalidIndex(); i = m_botList.Next( i ) )
  56. {
  57. if ( !func( m_botList[i]->GetEntity() ) )
  58. {
  59. return false;
  60. }
  61. }
  62. return true;
  63. }
  64. /**
  65. * Return closest bot to given point that passes the given filter
  66. */
  67. template < typename Filter >
  68. INextBot *GetClosestBot( const Vector &pos, Filter &filter )
  69. {
  70. INextBot *close = NULL;
  71. float closeRangeSq = FLT_MAX;
  72. for( int i=m_botList.Head(); i != m_botList.InvalidIndex(); i = m_botList.Next( i ) )
  73. {
  74. float rangeSq = ( m_botList[i]->GetEntity()->GetAbsOrigin() - pos ).LengthSqr();
  75. if ( rangeSq < closeRangeSq && filter( m_botList[i] ) )
  76. {
  77. closeRangeSq = rangeSq;
  78. close = m_botList[i];
  79. }
  80. }
  81. return close;
  82. }
  83. /**
  84. * Event propagators
  85. */
  86. virtual void OnMapLoaded( void ); // when the server has changed maps
  87. virtual void OnRoundRestart( void ); // when the scenario restarts
  88. virtual void OnBeginChangeLevel( void ); // when the server is about to change maps
  89. virtual void OnKilled( CBaseCombatCharacter *victim, const CTakeDamageInfo &info ); // when an actor is killed
  90. virtual void OnSound( CBaseEntity *source, const Vector &pos, KeyValues *keys ); // when an entity emits a sound
  91. virtual void OnSpokeConcept( CBaseCombatCharacter *who, AIConcept_t concept, AI_Response *response ); // when an Actor speaks a concept
  92. virtual void OnWeaponFired( CBaseCombatCharacter *whoFired, CBaseCombatWeapon *weapon ); // when someone fires a weapon
  93. /**
  94. * Debugging
  95. */
  96. bool IsDebugging( unsigned int type ) const; // return true if debugging system is on for the given type(s)
  97. void SetDebugTypes( NextBotDebugType type ); // start displaying debug info of the given type(s)
  98. void DebugFilterAdd( int index ); // add given entindex to the debug filter
  99. void DebugFilterAdd( const char *name ); // add given name to the debug filter
  100. void DebugFilterRemove( int index ); // remove given entindex from the debug filter
  101. void DebugFilterRemove( const char *name ); // remove given name from the debug filter
  102. void DebugFilterClear( void ); // clear the debug filter (remove all entries)
  103. bool IsDebugFilterMatch( const INextBot *bot ) const; // return true if the given bot matches the debug filter
  104. void Select( INextBot *bot ); // mark bot as selected for further operations
  105. void DeselectAll( void );
  106. INextBot *GetSelected( void ) const;
  107. INextBot *GetBotUnderCrosshair( CBasePlayer *picker ); // Get the bot under the given player's crosshair
  108. //
  109. // Put these in a derived class
  110. //
  111. void OnSurvivorVomitedUpon( CTerrorPlayer *victim ); // when a Survivor has been hit by Boomer Vomit
  112. static void SetInstance( NextBotManager *pInstance ) { sInstance = pInstance; };
  113. static NextBotManager* GetInstance() { return sInstance; }
  114. protected:
  115. static NextBotManager* sInstance;
  116. friend class INextBot;
  117. int Register( INextBot *bot );
  118. void UnRegister( INextBot *bot );
  119. CUtlLinkedList< INextBot * > m_botList; // list of all active NextBots
  120. int m_iUpdateTickrate;
  121. double m_CurUpdateStartTime;
  122. double m_SumFrameTime;
  123. unsigned int m_debugType; // debug flags
  124. struct DebugFilter
  125. {
  126. int index; // entindex
  127. enum { MAX_DEBUG_NAME_SIZE = 128 };
  128. char name[ MAX_DEBUG_NAME_SIZE ];
  129. };
  130. CUtlVector< DebugFilter > m_debugFilterList;
  131. INextBot *m_selectedBot; // selected bot for further debug operations
  132. };
  133. inline int NextBotManager::GetNextBotCount( void ) const
  134. {
  135. return m_botList.Count();
  136. }
  137. inline bool NextBotManager::IsDebugging( unsigned int type ) const
  138. {
  139. if ( type & m_debugType )
  140. {
  141. return true;
  142. }
  143. return false;
  144. }
  145. inline void NextBotManager::SetDebugTypes( NextBotDebugType type )
  146. {
  147. m_debugType = (unsigned int)type;
  148. }
  149. inline void NextBotManager::Select( INextBot *bot )
  150. {
  151. m_selectedBot = bot;
  152. }
  153. inline void NextBotManager::DeselectAll( void )
  154. {
  155. m_selectedBot = NULL;
  156. }
  157. inline INextBot *NextBotManager::GetSelected( void ) const
  158. {
  159. return m_selectedBot;
  160. }
  161. // singleton accessor
  162. extern NextBotManager &TheNextBots( void );
  163. #endif // _NEXT_BOT_MANAGER_H_