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.

169 lines
4.7 KiB

  1. //========= Copyright 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. //-----------------------------------------------------------------------------
  35. // class CAI_ScriptConditions
  36. //
  37. // Purpose:
  38. //-----------------------------------------------------------------------------
  39. class CAI_Senses : public CAI_Component
  40. {
  41. public:
  42. CAI_Senses()
  43. : m_LookDist(2048),
  44. m_LastLookDist(-1),
  45. m_TimeLastLook(-1),
  46. m_iAudibleList(0),
  47. m_TimeLastLookHighPriority( -1 ),
  48. m_TimeLastLookNPCs( -1 ),
  49. m_TimeLastLookMisc( -1 )
  50. {
  51. m_SeenArrays[0] = &m_SeenHighPriority;
  52. m_SeenArrays[1] = &m_SeenNPCs;
  53. m_SeenArrays[2] = &m_SeenMisc;
  54. m_iSensingFlags = SENSING_FLAGS_NONE;
  55. }
  56. float GetDistLook() const { return m_LookDist; }
  57. void SetDistLook( float flDistLook ) { m_LookDist = flDistLook; }
  58. void PerformSensing();
  59. void Listen( void );
  60. void Look( int iDistance );// basic sight function for npcs
  61. bool ShouldSeeEntity( CBaseEntity *pEntity ); // logical query
  62. bool CanSeeEntity( CBaseEntity *pSightEnt ); // more expensive cone & raycast test
  63. #ifdef PORTAL
  64. bool CanSeeEntityThroughPortal( const CProp_Portal *pPortal, CBaseEntity *pSightEnt ); // more expensive cone & raycast test
  65. #endif
  66. bool DidSeeEntity( CBaseEntity *pSightEnt ) const; // a less expensive query that looks at cached results from recent conditionsa gathering
  67. CBaseEntity * GetFirstSeenEntity( AISightIter_t *pIter, seentype_t iSeenType = SEEN_ALL ) const;
  68. CBaseEntity * GetNextSeenEntity( AISightIter_t *pIter ) const;
  69. CSound * GetFirstHeardSound( AISoundIter_t *pIter );
  70. CSound * GetNextHeardSound( AISoundIter_t *pIter );
  71. CSound * GetClosestSound( bool fScent = false, int validTypes = ALL_SOUNDS | ALL_SCENTS, bool bUsePriority = true );
  72. bool CanHearSound( CSound *pSound );
  73. //---------------------------------
  74. float GetTimeLastUpdate( CBaseEntity *pEntity );
  75. //---------------------------------
  76. void AddSensingFlags( int iFlags ) { m_iSensingFlags |= iFlags; }
  77. void RemoveSensingFlags( int iFlags ) { m_iSensingFlags &= ~iFlags; }
  78. bool HasSensingFlags( int iFlags ) { return (m_iSensingFlags & iFlags) == iFlags; }
  79. DECLARE_SIMPLE_DATADESC();
  80. private:
  81. int GetAudibleList() const { return m_iAudibleList; }
  82. bool WaitingUntilSeen( CBaseEntity *pSightEnt );
  83. void BeginGather();
  84. void NoteSeenEntity( CBaseEntity *pSightEnt );
  85. void EndGather( int nSeen, CUtlVector<EHANDLE> *pResult );
  86. bool Look( CBaseEntity *pSightEnt );
  87. #ifdef PORTAL
  88. bool LookThroughPortal( const CProp_Portal *pPortal, CBaseEntity *pSightEnt );
  89. #endif
  90. int LookForHighPriorityEntities( int iDistance );
  91. int LookForNPCs( int iDistance );
  92. int LookForObjects( int iDistance );
  93. bool SeeEntity( CBaseEntity *pEntity );
  94. float m_LookDist; // distance npc sees (Default 2048)
  95. float m_LastLookDist;
  96. float m_TimeLastLook;
  97. int m_iAudibleList; // first index of a linked list of sounds that the npc can hear.
  98. CUtlVector<EHANDLE> m_SeenHighPriority;
  99. CUtlVector<EHANDLE> m_SeenNPCs;
  100. CUtlVector<EHANDLE> m_SeenMisc;
  101. CUtlVector<EHANDLE> *m_SeenArrays[3];
  102. float m_TimeLastLookHighPriority;
  103. float m_TimeLastLookNPCs;
  104. float m_TimeLastLookMisc;
  105. int m_iSensingFlags;
  106. };
  107. //-----------------------------------------------------------------------------
  108. class CAI_SensedObjectsManager : public IEntityListener
  109. {
  110. public:
  111. void Init();
  112. void Term();
  113. CBaseEntity * GetFirst( int *pIter );
  114. CBaseEntity * GetNext( int *pIter );
  115. virtual void AddEntity( CBaseEntity *pEntity );
  116. private:
  117. virtual void OnEntitySpawned( CBaseEntity *pEntity );
  118. virtual void OnEntityDeleted( CBaseEntity *pEntity );
  119. CUtlVector<EHANDLE> m_SensedObjects;
  120. };
  121. extern CAI_SensedObjectsManager g_AI_SensedObjectsManager;
  122. //-----------------------------------------------------------------------------
  123. #endif // AI_SENSES_H