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.

268 lines
8.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // Soundent.h - the entity that spawns when the world
  9. // spawns, and handles the world's active and free sound
  10. // lists.
  11. #ifndef SOUNDENT_H
  12. #define SOUNDENT_H
  13. #ifdef _WIN32
  14. #pragma once
  15. #endif
  16. enum
  17. {
  18. MAX_WORLD_SOUNDS_SP = 64, // Maximum number of sounds handled by the world at one time in single player.
  19. // This is also the number of entries saved in a savegame file (for b/w compatibility).
  20. MAX_WORLD_SOUNDS_MP = 128 // The sound array size is set this large but we'll only use gpGlobals->maxPlayers+32 entries in mp.
  21. };
  22. enum
  23. {
  24. SOUND_NONE = 0,
  25. SOUND_COMBAT = 0x00000001,
  26. SOUND_WORLD = 0x00000002,
  27. SOUND_PLAYER = 0x00000004,
  28. SOUND_DANGER = 0x00000008,
  29. SOUND_BULLET_IMPACT = 0x00000010,
  30. SOUND_CARCASS = 0x00000020,
  31. SOUND_MEAT = 0x00000040,
  32. SOUND_GARBAGE = 0x00000080,
  33. SOUND_THUMPER = 0x00000100, // keeps certain creatures at bay
  34. SOUND_BUGBAIT = 0x00000200, // gets the antlion's attention
  35. SOUND_PHYSICS_DANGER = 0x00000400,
  36. SOUND_DANGER_SNIPERONLY = 0x00000800, // only scares the sniper NPC.
  37. SOUND_MOVE_AWAY = 0x00001000,
  38. SOUND_PLAYER_VEHICLE = 0x00002000,
  39. SOUND_READINESS_LOW = 0x00004000, // Changes listener's readiness (Player Companion only)
  40. SOUND_READINESS_MEDIUM = 0x00008000,
  41. SOUND_READINESS_HIGH = 0x00010000,
  42. // Contexts begin here.
  43. SOUND_CONTEXT_FROM_SNIPER = 0x00100000, // additional context for SOUND_DANGER
  44. SOUND_CONTEXT_GUNFIRE = 0x00200000, // Added to SOUND_COMBAT
  45. SOUND_CONTEXT_MORTAR = 0x00400000, // Explosion going to happen here.
  46. SOUND_CONTEXT_COMBINE_ONLY = 0x00800000, // Only combine can hear sounds marked this way
  47. SOUND_CONTEXT_REACT_TO_SOURCE = 0x01000000, // React to sound source's origin, not sound's location
  48. SOUND_CONTEXT_EXPLOSION = 0x02000000, // Context added to SOUND_COMBAT, usually.
  49. SOUND_CONTEXT_EXCLUDE_COMBINE = 0x04000000, // Combine do NOT hear this
  50. SOUND_CONTEXT_DANGER_APPROACH = 0x08000000, // Treat as a normal danger sound if you see the source, otherwise turn to face source.
  51. SOUND_CONTEXT_ALLIES_ONLY = 0x10000000, // Only player allies can hear this sound
  52. SOUND_CONTEXT_PLAYER_VEHICLE = 0x20000000, // HACK: need this because we're not treating the SOUND_xxx values as true bit values! See switch in OnListened.
  53. SOUND_CONTEXT_FROM_FIRE = 0x40000000, // A fire is nearby
  54. SOUND_CONTEXT_FOLLOW_OWNER = 0x80000000, // The sound origin is at the owner
  55. ALL_CONTEXTS = 0xFFF00000,
  56. ALL_SCENTS = SOUND_CARCASS | SOUND_MEAT | SOUND_GARBAGE,
  57. ALL_SOUNDS = 0x000FFFFF & ~ALL_SCENTS,
  58. };
  59. // Make as many of these as you want.
  60. enum
  61. {
  62. SOUNDENT_CHANNEL_UNSPECIFIED = 0,
  63. SOUNDENT_CHANNEL_REPEATING,
  64. SOUNDENT_CHANNEL_REPEATED_DANGER, // for things that make danger sounds frequently.
  65. SOUNDENT_CHANNEL_REPEATED_PHYSICS_DANGER,
  66. SOUNDENT_CHANNEL_WEAPON,
  67. SOUNDENT_CHANNEL_INJURY,
  68. SOUNDENT_CHANNEL_BULLET_IMPACT,
  69. SOUNDENT_CHANNEL_NPC_FOOTSTEP,
  70. SOUNDENT_CHANNEL_SPOOKY_NOISE, // made by zombies in darkness
  71. SOUNDENT_CHANNEL_ZOMBINE_GRENADE,
  72. };
  73. enum
  74. {
  75. SOUNDLIST_EMPTY = -1
  76. };
  77. #define SOUNDENT_VOLUME_MACHINEGUN 1500.0
  78. #define SOUNDENT_VOLUME_SHOTGUN 1500.0
  79. #define SOUNDENT_VOLUME_PISTOL 1500.0
  80. #define SOUNDENT_VOLUME_EMPTY 500.0 // volume of the "CLICK" when you have no bullets
  81. enum
  82. {
  83. SOUND_PRIORITY_VERY_LOW = -2,
  84. SOUND_PRIORITY_LOW,
  85. SOUND_PRIORITY_NORMAL = 0,
  86. SOUND_PRIORITY_HIGH,
  87. SOUND_PRIORITY_VERY_HIGH,
  88. SOUND_PRIORITY_HIGHEST,
  89. };
  90. //=========================================================
  91. // CSound - an instance of a sound in the world.
  92. //=========================================================
  93. class CSound
  94. {
  95. DECLARE_SIMPLE_DATADESC();
  96. public:
  97. bool DoesSoundExpire() const;
  98. float SoundExpirationTime() const;
  99. void SetSoundOrigin( const Vector &vecOrigin ) { m_vecOrigin = vecOrigin; }
  100. const Vector& GetSoundOrigin( void );
  101. const Vector& GetSoundReactOrigin( void );
  102. bool FIsSound( void );
  103. bool FIsScent( void );
  104. bool IsSoundType( int nSoundFlags ) const;
  105. int SoundType( ) const;
  106. int SoundContext() const;
  107. int SoundTypeNoContext( ) const;
  108. int Volume( ) const;
  109. float OccludedVolume() { return m_iVolume * m_flOcclusionScale; }
  110. int NextSound() const;
  111. void Reset ( void );
  112. int SoundChannel( void ) const;
  113. bool ValidateOwner() const;
  114. EHANDLE m_hOwner; // sound's owner
  115. EHANDLE m_hTarget; // Sounds's target - an odd concept. For a gunfire sound, the target is the entity being fired at
  116. int m_iVolume; // how loud the sound is
  117. float m_flOcclusionScale; // How loud the sound is when occluded by the world. (volume * occlusionscale)
  118. int m_iType; // what type of sound this is
  119. int m_iNextAudible; // temporary link that NPCs use to build a list of audible sounds
  120. private:
  121. void Clear ( void );
  122. float m_flExpireTime; // when the sound should be purged from the list
  123. short m_iNext; // index of next sound in this list ( Active or Free )
  124. bool m_bNoExpirationTime;
  125. int m_ownerChannelIndex;
  126. Vector m_vecOrigin; // sound's location in space
  127. bool m_bHasOwner; // Lets us know if this sound was created with an owner. In case the owner goes null.
  128. #ifdef DEBUG
  129. int m_iMyIndex; // debugging
  130. #endif
  131. friend class CSoundEnt;
  132. };
  133. inline bool CSound::DoesSoundExpire() const
  134. {
  135. return m_bNoExpirationTime == false;
  136. }
  137. inline float CSound::SoundExpirationTime() const
  138. {
  139. return m_bNoExpirationTime ? FLT_MAX : m_flExpireTime;
  140. }
  141. inline bool CSound::IsSoundType( int nSoundFlags ) const
  142. {
  143. return (m_iType & nSoundFlags) != 0;
  144. }
  145. inline int CSound::SoundType( ) const
  146. {
  147. return m_iType;
  148. }
  149. inline int CSound::SoundContext( ) const
  150. {
  151. return m_iType & ALL_CONTEXTS;
  152. }
  153. inline int CSound::SoundTypeNoContext( ) const
  154. {
  155. return m_iType & ~ALL_CONTEXTS;
  156. }
  157. inline int CSound::Volume( ) const
  158. {
  159. return m_iVolume;
  160. }
  161. inline int CSound::NextSound() const
  162. {
  163. return m_iNext;
  164. }
  165. inline int CSound::SoundChannel( void ) const
  166. {
  167. return m_ownerChannelIndex;
  168. }
  169. // The owner is considered valid if:
  170. // -The sound never had an assigned owner (quite common)
  171. // -The sound was assigned an owner and that owner still exists
  172. inline bool CSound::ValidateOwner( void ) const
  173. {
  174. return ( !m_bHasOwner || (m_hOwner.Get() != NULL) );
  175. }
  176. //=========================================================
  177. // CSoundEnt - a single instance of this entity spawns when
  178. // the world spawns. The SoundEnt's job is to update the
  179. // world's Free and Active sound lists.
  180. //=========================================================
  181. class CSoundEnt : public CPointEntity
  182. {
  183. DECLARE_DATADESC();
  184. public:
  185. DECLARE_CLASS( CSoundEnt, CPointEntity );
  186. // Construction, destruction
  187. static bool InitSoundEnt();
  188. static void ShutdownSoundEnt();
  189. CSoundEnt();
  190. virtual ~CSoundEnt();
  191. virtual void OnRestore();
  192. void Precache ( void );
  193. void Spawn( void );
  194. void Think( void );
  195. void Initialize ( void );
  196. int ObjectCaps( void ) { return BaseClass::ObjectCaps() & ~FCAP_ACROSS_TRANSITION; }
  197. static int InsertSound ( int iType, const Vector &vecOrigin, int iVolume, float flDuration, CBaseEntity *pOwner = NULL, int soundChannelIndex = SOUNDENT_CHANNEL_UNSPECIFIED, CBaseEntity *pSoundTarget = NULL );
  198. static int ActiveList( void );// return the head of the active list
  199. static CSound* SoundPointerForIndex( int iIndex );// return a pointer for this index in the sound list
  200. static CSound* GetLoudestSoundOfType( int iType, const Vector &vecEarPosition );
  201. static int ClientSoundIndex ( edict_t *pClient );
  202. static void FreeSound( int iSound );
  203. bool IsEmpty( void );
  204. int ISoundsInList ( int iListType );
  205. int IAllocSound ( void );
  206. int FindOrAllocateSound( CBaseEntity *pOwner, int soundChannelIndex );
  207. private:
  208. static void FreeSound ( int iSound, int iPrevious );
  209. static int FreeList( void );// return the head of the free list
  210. int m_iFreeSound; // index of the first sound in the free sound list
  211. int m_iActiveSound; // indes of the first sound in the active sound list
  212. int m_cLastActiveSounds; // keeps track of the number of active sounds at the last update. (for diagnostic work)
  213. CSound m_SoundPool[ MAX_WORLD_SOUNDS_MP ];
  214. };
  215. //-----------------------------------------------------------------------------
  216. // Inline methods
  217. //-----------------------------------------------------------------------------
  218. inline bool CSoundEnt::IsEmpty( void )
  219. {
  220. return m_iActiveSound == SOUNDLIST_EMPTY;
  221. }
  222. #endif //SOUNDENT_H