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.

174 lines
5.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef AI_SENSES_H
  8. #define AI_SENSES_H
  9. #include "tier1/utlvector.h"
  10. #include "tier1/utlmap.h"
  11. #include "simtimer.h"
  12. #include "ai_component.h"
  13. #include "soundent.h"
  14. #if defined( _WIN32 )
  15. #pragma once
  16. #endif
  17. class CBaseEntity;
  18. class CSound;
  19. //-------------------------------------
  20. DECLARE_POINTER_HANDLE( AISightIter_t );
  21. DECLARE_POINTER_HANDLE( AISoundIter_t );
  22. // GetFirstSeenEntity can take these as optional parameters to search for
  23. // a specific type of entity.
  24. enum seentype_t
  25. {
  26. SEEN_ALL = -1, // Default
  27. SEEN_HIGH_PRIORITY = 0,
  28. SEEN_NPCS,
  29. SEEN_MISC
  30. };
  31. #define SENSING_FLAGS_NONE 0x00000000
  32. #define SENSING_FLAGS_DONT_LOOK 0x00000001 // Effectively makes the NPC blind
  33. #define SENSING_FLAGS_DONT_LISTEN 0x00000002 // Effectively makes the NPC deaf
  34. #define SENSING_FLAGS_IGNORE_PORTALS 0x00000004 // Don't use portals to extend the senses
  35. //-----------------------------------------------------------------------------
  36. // class CAI_ScriptConditions
  37. //
  38. // Purpose:
  39. //-----------------------------------------------------------------------------
  40. class CAI_Senses : public CAI_Component
  41. {
  42. public:
  43. CAI_Senses()
  44. : m_LookDist(2048),
  45. m_LastLookDist(-1),
  46. m_TimeLastLook(-1),
  47. m_iAudibleList(0),
  48. m_TimeLastLookHighPriority( -1 ),
  49. m_TimeLastLookNPCs( -1 ),
  50. m_TimeLastLookMisc( -1 )
  51. {
  52. m_SeenArrays[0] = &m_SeenHighPriority;
  53. m_SeenArrays[1] = &m_SeenNPCs;
  54. m_SeenArrays[2] = &m_SeenMisc;
  55. m_iSensingFlags = SENSING_FLAGS_NONE;
  56. }
  57. float GetDistLook() const { return m_LookDist; }
  58. void SetDistLook( float flDistLook ) { m_LookDist = flDistLook; }
  59. virtual void PerformSensing();
  60. void Listen( void );
  61. void Look( int iDistance );// basic sight function for npcs
  62. bool ShouldSeeEntity( CBaseEntity *pEntity ); // logical query
  63. bool CanSeeEntity( CBaseEntity *pSightEnt ); // more expensive cone & raycast test
  64. #ifdef PORTAL
  65. bool CanSeeEntityThroughPortal( const CPortal_Base2D *pPortal, CBaseEntity *pSightEnt ); // more expensive cone & raycast test
  66. #endif
  67. bool DidSeeEntity( CBaseEntity *pSightEnt ) const; // a less expensive query that looks at cached results from recent conditionsa gathering
  68. CBaseEntity * GetFirstSeenEntity( AISightIter_t *pIter, seentype_t iSeenType = SEEN_ALL ) const;
  69. CBaseEntity * GetNextSeenEntity( AISightIter_t *pIter ) const;
  70. CSound * GetFirstHeardSound( AISoundIter_t *pIter );
  71. CSound * GetNextHeardSound( AISoundIter_t *pIter );
  72. CSound * GetClosestSound( bool fScent = false, int validTypes = ALL_SOUNDS | ALL_SCENTS, bool bUsePriority = true );
  73. bool CanHearSound( CSound *pSound );
  74. // children of this class may need to overload this function to allow for more specialized checks such as angle of elevation, etc.
  75. virtual bool IsWithinSenseDistance( const Vector &source, const Vector &dest, float dist ) { return ( source.DistToSqr( dest ) < dist * dist ); }
  76. //---------------------------------
  77. float GetTimeLastUpdate( CBaseEntity *pEntity );
  78. //---------------------------------
  79. void AddSensingFlags( int iFlags ) { m_iSensingFlags |= iFlags; }
  80. void RemoveSensingFlags( int iFlags ) { m_iSensingFlags &= ~iFlags; }
  81. bool HasSensingFlags( int iFlags ) { return (m_iSensingFlags & iFlags) == iFlags; }
  82. DECLARE_SIMPLE_DATADESC();
  83. protected:
  84. int GetAudibleList() const { return m_iAudibleList; }
  85. virtual bool WaitingUntilSeen( CBaseEntity *pSightEnt );
  86. void BeginGather();
  87. void NoteSeenEntity( CBaseEntity *pSightEnt );
  88. void EndGather( int nSeen, CUtlVector<EHANDLE> *pResult );
  89. bool Look( CBaseEntity *pSightEnt );
  90. #ifdef PORTAL
  91. bool LookThroughPortal( const CPortal_Base2D *pPortal, CBaseEntity *pSightEnt );
  92. #endif
  93. virtual int LookForHighPriorityEntities( int iDistance );
  94. int LookForNPCs( int iDistance );
  95. int LookForObjects( int iDistance );
  96. bool SeeEntity( CBaseEntity *pEntity );
  97. private:
  98. float m_LookDist; // distance npc sees (Default 2048)
  99. float m_LastLookDist;
  100. float m_TimeLastLook;
  101. int m_iAudibleList; // first index of a linked list of sounds that the npc can hear.
  102. CUtlVector<EHANDLE> m_SeenHighPriority;
  103. CUtlVector<EHANDLE> m_SeenNPCs;
  104. CUtlVector<EHANDLE> m_SeenMisc;
  105. CUtlVector<EHANDLE> *m_SeenArrays[3];
  106. float m_TimeLastLookHighPriority;
  107. float m_TimeLastLookNPCs;
  108. float m_TimeLastLookMisc;
  109. int m_iSensingFlags;
  110. };
  111. //-----------------------------------------------------------------------------
  112. class CAI_SensedObjectsManager : public IEntityListener
  113. {
  114. public:
  115. void Init();
  116. void Term();
  117. CBaseEntity * GetFirst( int *pIter );
  118. CBaseEntity * GetNext( int *pIter );
  119. virtual void AddEntity( CBaseEntity *pEntity );
  120. private:
  121. virtual void OnEntitySpawned( CBaseEntity *pEntity );
  122. virtual void OnEntityDeleted( CBaseEntity *pEntity );
  123. CUtlVector<EHANDLE> m_SensedObjects;
  124. };
  125. extern CAI_SensedObjectsManager g_AI_SensedObjectsManager;
  126. //-----------------------------------------------------------------------------
  127. #endif // AI_SENSES_H