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.

256 lines
6.5 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef AI_SCRIPTCONDITIONS_H
  8. #define AI_SCRIPTCONDITIONS_H
  9. #include "baseentity.h"
  10. #include "entityoutput.h"
  11. #include "simtimer.h"
  12. #include "ai_npcstate.h"
  13. #if defined( _WIN32 )
  14. #pragma once
  15. #endif
  16. //-----------------------------------------------------------------------------
  17. class CAI_ProxTester
  18. {
  19. public:
  20. CAI_ProxTester()
  21. : m_distSq( 0 ),
  22. m_fInside( false )
  23. {
  24. }
  25. void Init( float dist )
  26. {
  27. m_fInside = ( dist > 0 );
  28. m_distSq = dist * dist;
  29. }
  30. bool Check( CBaseEntity *pEntity1, CBaseEntity *pEntity2 )
  31. {
  32. if ( m_distSq != 0 && pEntity1 && pEntity2 )
  33. {
  34. float distSq = ( pEntity1->GetAbsOrigin() - pEntity2->GetAbsOrigin() ).LengthSqr();
  35. bool fInside = ( distSq < m_distSq );
  36. return ( m_fInside == fInside );
  37. }
  38. return true;
  39. }
  40. DECLARE_SIMPLE_DATADESC();
  41. private:
  42. float m_distSq;
  43. bool m_fInside;
  44. };
  45. //-----------------------------------------------------------------------------
  46. class CAI_ScriptConditionsElement
  47. {
  48. public:
  49. DECLARE_SIMPLE_DATADESC();
  50. void SetActor( CBaseEntity *pEntity ) { m_hActor = pEntity; }
  51. CBaseEntity *GetActor( void ){ return m_hActor.Get(); }
  52. void SetTimer( CSimTimer timer ) { m_Timer = timer; }
  53. CSimTimer *GetTimer( void ) { return &m_Timer; }
  54. void SetTimeOut( CSimTimer timeout) { m_Timeout = timeout; }
  55. CSimTimer *GetTimeOut( void ) { return &m_Timeout; }
  56. private:
  57. EHANDLE m_hActor;
  58. CSimTimer m_Timer;
  59. CSimTimer m_Timeout;
  60. };
  61. //-----------------------------------------------------------------------------
  62. // class CAI_ScriptConditions
  63. //
  64. // Purpose: Watches a set of conditions relative to a given NPC, and when they
  65. // are all satisfied, fires the relevant output
  66. //-----------------------------------------------------------------------------
  67. class CAI_ScriptConditions : public CBaseEntity, public IEntityListener
  68. {
  69. DECLARE_CLASS( CAI_ScriptConditions, CBaseEntity );
  70. public:
  71. CAI_ScriptConditions()
  72. : m_fDisabled( true ),
  73. m_flRequiredTime( 0 ),
  74. m_fMinState( NPC_STATE_IDLE ),
  75. m_fMaxState( NPC_STATE_IDLE ),
  76. m_fScriptStatus( TRS_NONE ),
  77. m_fActorSeePlayer( TRS_NONE ),
  78. m_flPlayerActorProximity( 0 ),
  79. m_flPlayerActorFOV( -1 ),
  80. m_fPlayerActorLOS( TRS_NONE ),
  81. m_fActorSeeTarget( TRS_NONE ),
  82. m_flActorTargetProximity( 0 ),
  83. m_flPlayerTargetProximity( 0 ),
  84. m_flPlayerTargetFOV( 0 ),
  85. m_fPlayerTargetLOS( TRS_NONE ),
  86. m_fPlayerBlockingActor( TRS_NONE ),
  87. m_flMinTimeout( 0 ),
  88. m_flMaxTimeout( 0 ),
  89. m_fActorInPVS( TRS_NONE ),
  90. m_fActorInVehicle( TRS_NONE ),
  91. m_fPlayerInVehicle( TRS_NONE )
  92. {
  93. #ifndef HL2_EPISODIC
  94. m_hActor = NULL;
  95. #endif
  96. }
  97. private:
  98. void Spawn();
  99. void Activate();
  100. void EvaluationThink();
  101. void Enable();
  102. void Disable();
  103. void SetThinkTime() { SetNextThink( gpGlobals->curtime + 0.250 ); }
  104. // Evaluators
  105. struct EvalArgs_t
  106. {
  107. CBaseEntity *pActor;
  108. CBasePlayer *pPlayer;
  109. CBaseEntity *pTarget;
  110. };
  111. bool EvalState( const EvalArgs_t &args );
  112. bool EvalActorSeePlayer( const EvalArgs_t &args );
  113. bool EvalPlayerActorLook( const EvalArgs_t &args );
  114. bool EvalPlayerTargetLook( const EvalArgs_t &args );
  115. bool EvalPlayerActorProximity( const EvalArgs_t &args );
  116. bool EvalPlayerTargetProximity( const EvalArgs_t &args );
  117. bool EvalActorTargetProximity( const EvalArgs_t &args );
  118. bool EvalActorSeeTarget( const EvalArgs_t &args );
  119. bool EvalPlayerActorLOS( const EvalArgs_t &args );
  120. bool EvalPlayerTargetLOS( const EvalArgs_t &args );
  121. bool EvalPlayerBlockingActor( const EvalArgs_t &args );
  122. bool EvalActorInPVS( const EvalArgs_t &args );
  123. bool EvalPlayerInVehicle( const EvalArgs_t &args );
  124. bool EvalActorInVehicle( const EvalArgs_t &args );
  125. void OnEntitySpawned( CBaseEntity *pEntity );
  126. int AddNewElement( CBaseEntity *pActor );
  127. bool ActorInList( CBaseEntity *pActor );
  128. void UpdateOnRemove( void );
  129. // Input handlers
  130. void InputEnable( inputdata_t &inputdata );
  131. void InputDisable( inputdata_t &inputdata );
  132. // Output handlers
  133. COutputEvent m_OnConditionsSatisfied;
  134. COutputEvent m_OnConditionsTimeout;
  135. COutputEvent m_NoValidActors;
  136. //---------------------------------
  137. #ifndef HL2_EPISODIC
  138. CBaseEntity *GetActor() { return m_hActor.Get(); }
  139. #endif
  140. CBasePlayer *GetPlayer() { return UTIL_GetLocalPlayer(); }
  141. //---------------------------------
  142. // @Note (toml 07-17-02): At some point, it may be desireable to switch to using function objects instead of functions. Probably
  143. // if support for NPCs addiing custom conditions becomes necessary
  144. typedef bool (CAI_ScriptConditions::*EvaluationFunc_t)( const EvalArgs_t &args );
  145. struct EvaluatorInfo_t
  146. {
  147. EvaluationFunc_t pfnEvaluator;
  148. const char *pszName;
  149. };
  150. static EvaluatorInfo_t gm_Evaluators[];
  151. //---------------------------------
  152. // Evaluation helpers
  153. static bool IsInFOV( CBaseEntity *pViewer, CBaseEntity *pViewed, float fov, bool bTrueCone );
  154. static bool PlayerHasLineOfSight( CBaseEntity *pViewer, CBaseEntity *pViewed, bool fNot );
  155. static bool ActorInPlayersPVS( CBaseEntity *pActor, bool bNot );
  156. virtual void OnRestore( void );
  157. //---------------------------------
  158. // General conditions info
  159. bool m_fDisabled;
  160. bool m_bLeaveAsleep;
  161. EHANDLE m_hTarget;
  162. float m_flRequiredTime; // How long should the conditions me true
  163. #ifndef HL2_EPISODIC
  164. EHANDLE m_hActor;
  165. CSimTimer m_Timer; // @TODO (toml 07-16-02): save/load of timer once Jay has save/load of contained objects
  166. CSimTimer m_Timeout;
  167. #endif
  168. //---------------------------------
  169. // Specific conditions data
  170. NPC_STATE m_fMinState;
  171. NPC_STATE m_fMaxState;
  172. ThreeState_t m_fScriptStatus;
  173. ThreeState_t m_fActorSeePlayer;
  174. string_t m_Actor;
  175. float m_flPlayerActorProximity;
  176. CAI_ProxTester m_PlayerActorProxTester;
  177. float m_flPlayerActorFOV;
  178. bool m_bPlayerActorFOVTrueCone;
  179. ThreeState_t m_fPlayerActorLOS;
  180. ThreeState_t m_fActorSeeTarget;
  181. float m_flActorTargetProximity;
  182. CAI_ProxTester m_ActorTargetProxTester;
  183. float m_flPlayerTargetProximity;
  184. CAI_ProxTester m_PlayerTargetProxTester;
  185. float m_flPlayerTargetFOV;
  186. bool m_bPlayerTargetFOVTrueCone;
  187. ThreeState_t m_fPlayerTargetLOS;
  188. ThreeState_t m_fPlayerBlockingActor;
  189. ThreeState_t m_fActorInPVS;
  190. float m_flMinTimeout;
  191. float m_flMaxTimeout;
  192. ThreeState_t m_fActorInVehicle;
  193. ThreeState_t m_fPlayerInVehicle;
  194. CUtlVector< CAI_ScriptConditionsElement > m_ElementList;
  195. //---------------------------------
  196. DECLARE_DATADESC();
  197. };
  198. //=============================================================================
  199. #endif // AI_SCRIPTCONDITIONS_H