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.

243 lines
6.8 KiB

  1. // NextBotUtil.h
  2. // Utilities for the NextBot system
  3. // Author: Michael Booth, May 2006
  4. // Copyright (c) 2006 Turtle Rock Studios, Inc. - All Rights Reserved
  5. #ifndef _NEXT_BOT_UTIL_H_
  6. #define _NEXT_BOT_UTIL_H_
  7. #include "NextBotLocomotionInterface.h"
  8. //--------------------------------------------------------------------------------------------
  9. /**
  10. * A simple filter interface for various NextBot queries
  11. */
  12. class INextBotEntityFilter
  13. {
  14. public:
  15. // return true if the given entity passes this filter
  16. virtual bool IsAllowed( CBaseEntity *entity ) const = 0;
  17. };
  18. // trace filter callback functions. needed for use with the querycache/optimization functionality
  19. bool VisionTraceFilterFunction( IHandleEntity *pServerEntity, int contentsMask );
  20. bool IgnoreActorsTraceFilterFunction( IHandleEntity *pServerEntity, int contentsMask );
  21. //--------------------------------------------------------------------------------------------
  22. /**
  23. * Trace filter that skips all players and NextBots
  24. */
  25. class NextBotTraceFilterIgnoreActors : public CTraceFilterSimple
  26. {
  27. public:
  28. NextBotTraceFilterIgnoreActors( const IHandleEntity *passentity, int collisionGroup ) : CTraceFilterSimple( passentity, collisionGroup, IgnoreActorsTraceFilterFunction )
  29. {
  30. }
  31. };
  32. //--------------------------------------------------------------------------------------------
  33. /**
  34. * Trace filter that skips all players, NextBots, and non-LOS blockers
  35. */
  36. class NextBotVisionTraceFilter : public CTraceFilterSimple
  37. {
  38. public:
  39. NextBotVisionTraceFilter( const IHandleEntity *passentity, int collisionGroup ) : CTraceFilterSimple( passentity, collisionGroup, VisionTraceFilterFunction )
  40. {
  41. }
  42. };
  43. //--------------------------------------------------------------------------------------------
  44. /**
  45. * Trace filter that skips all NextBots, but includes Players
  46. */
  47. class NextBotTraceFilterIgnoreNextBots : public CTraceFilterSimple
  48. {
  49. public:
  50. NextBotTraceFilterIgnoreNextBots( const IHandleEntity *passentity, int collisionGroup )
  51. : CTraceFilterSimple( passentity, collisionGroup )
  52. {
  53. }
  54. virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
  55. {
  56. if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
  57. {
  58. CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
  59. #ifdef TERROR
  60. CBasePlayer *player = ToBasePlayer( entity );
  61. if ( player && player->IsGhost() )
  62. return false;
  63. #endif // TERROR
  64. return ( entity->MyNextBotPointer() == NULL );
  65. }
  66. return false;
  67. }
  68. };
  69. //--------------------------------------------------------------------------------------------
  70. /**
  71. * Trace filter that obeys INextBot::IsAbleToBlockMovementOf()
  72. */
  73. class NextBotTraceFilter : public CTraceFilterSimple
  74. {
  75. public:
  76. NextBotTraceFilter( const IHandleEntity *passentity, int collisionGroup )
  77. : CTraceFilterSimple( passentity, collisionGroup )
  78. {
  79. CBaseEntity *entity = const_cast<CBaseEntity *>(EntityFromEntityHandle( passentity ));
  80. m_passBot = entity->MyNextBotPointer();
  81. }
  82. virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
  83. {
  84. if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
  85. {
  86. CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
  87. #ifdef TERROR
  88. CBasePlayer *player = ToBasePlayer( entity );
  89. if ( player && player->IsGhost() )
  90. return false;
  91. #endif // TERROR
  92. // Skip players on the same team - they're not solid to us, and we'll avoid them
  93. if ( entity->IsPlayer() && m_passBot && m_passBot->GetEntity() &&
  94. m_passBot->GetEntity()->GetTeamNumber() == entity->GetTeamNumber() )
  95. return false;
  96. INextBot *bot = entity->MyNextBotPointer();
  97. return ( !bot || bot->IsAbleToBlockMovementOf( m_passBot ) );
  98. }
  99. return false;
  100. }
  101. const INextBot *m_passBot;
  102. };
  103. //--------------------------------------------------------------------------------------------
  104. /**
  105. * Trace filter that only hits players and NextBots
  106. */
  107. class NextBotTraceFilterOnlyActors : public CTraceFilterSimple
  108. {
  109. public:
  110. NextBotTraceFilterOnlyActors( const IHandleEntity *passentity, int collisionGroup )
  111. : CTraceFilterSimple( passentity, collisionGroup )
  112. {
  113. }
  114. virtual TraceType_t GetTraceType() const
  115. {
  116. return TRACE_ENTITIES_ONLY;
  117. }
  118. virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
  119. {
  120. if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
  121. {
  122. CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
  123. #ifdef TERROR
  124. CBasePlayer *player = ToBasePlayer( entity );
  125. if ( player && player->IsGhost() )
  126. return false;
  127. #endif // TERROR
  128. return ( entity->MyNextBotPointer() || entity->IsPlayer() );
  129. }
  130. return false;
  131. }
  132. };
  133. //--------------------------------------------------------------------------------------------
  134. /**
  135. * Trace filter that skips "traversable" entities. The "when" argument creates
  136. * a temporal context for asking if an entity is IMMEDIATELY traversable (like thin
  137. * glass that just breaks as we walk through it) or EVENTUALLY traversable (like a
  138. * breakable object that will take some time to break through)
  139. */
  140. class NextBotTraversableTraceFilter : public CTraceFilterSimple
  141. {
  142. public:
  143. NextBotTraversableTraceFilter( INextBot *bot, ILocomotion::TraverseWhenType when = ILocomotion::EVENTUALLY ) : CTraceFilterSimple( bot->GetEntity(), COLLISION_GROUP_NONE )
  144. {
  145. m_bot = bot;
  146. m_when = when;
  147. }
  148. virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
  149. {
  150. CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
  151. if ( m_bot->IsSelf( entity ) )
  152. {
  153. return false;
  154. }
  155. if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
  156. {
  157. return !m_bot->GetLocomotionInterface()->IsEntityTraversable( entity, m_when );
  158. }
  159. return false;
  160. }
  161. private:
  162. INextBot *m_bot;
  163. ILocomotion::TraverseWhenType m_when;
  164. };
  165. #ifdef OBSOLETE
  166. //--------------------------------------------------------------------------------------------
  167. /**
  168. * Trace filter that skips "traversable" entities, but hits other Actors.
  169. * Used for obstacle avoidance.
  170. */
  171. class NextBotMovementAvoidanceTraceFilter : public CTraceFilterSimple
  172. {
  173. public:
  174. NextBotMovementAvoidanceTraceFilter( INextBot *bot ) : CTraceFilterSimple( bot->GetEntity(), COLLISION_GROUP_NONE )
  175. {
  176. m_bot = bot;
  177. }
  178. virtual bool ShouldHitEntity( IHandleEntity *pServerEntity, int contentsMask )
  179. {
  180. CBaseEntity *entity = EntityFromEntityHandle( pServerEntity );
  181. #ifdef TERROR
  182. CBasePlayer *player = ToBasePlayer( entity );
  183. if ( player && player->IsGhost() )
  184. return false;
  185. #endif // TERROR
  186. if ( m_bot->IsSelf( entity ) )
  187. {
  188. return false;
  189. }
  190. if ( CTraceFilterSimple::ShouldHitEntity( pServerEntity, contentsMask ) )
  191. {
  192. return !m_bot->GetLocomotionInterface()->IsEntityTraversable( entity, ILocomotion::IMMEDIATELY );
  193. }
  194. return false;
  195. }
  196. private:
  197. INextBot *m_bot;
  198. };
  199. #endif
  200. #endif // _NEXT_BOT_UTIL_H_