2
0
mirror of https://github.com/FWGS/hlsdk-xash3d synced 2024-11-25 11:19:39 +01:00
hlsdk-xash3d/dlls/soundent.cpp

379 lines
9.7 KiB
C++
Raw Normal View History

2016-06-04 15:24:23 +02:00
/***
*
* Copyright (c) 1996-2002, Valve LLC. All rights reserved.
*
* This product contains software technology licensed from Id
* Software, Inc. ("Id Technology"). Id Technology (c) 1996 Id Software, Inc.
* All Rights Reserved.
*
* Use, distribution, and modification of this source code and/or resulting
* object code is restricted to non-commercial enhancements to products from
* Valve LLC. All other use, distribution, or modification is prohibited
* without written permission from Valve LLC.
*
****/
2016-06-04 15:24:23 +02:00
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "monsters.h"
#include "soundent.h"
LINK_ENTITY_TO_CLASS( soundent, CSoundEnt )
2016-06-04 15:24:23 +02:00
CSoundEnt *pSoundEnt;
//=========================================================
// CSound - Clear - zeros all fields for a sound
//=========================================================
2016-08-01 13:13:13 +02:00
void CSound::Clear( void )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
m_vecOrigin = g_vecZero;
m_iType = 0;
m_iVolume = 0;
m_flExpireTime = 0;
m_iNext = SOUNDLIST_EMPTY;
m_iNextAudible = 0;
2016-06-04 15:24:23 +02:00
}
//=========================================================
// Reset - clears the volume, origin, and type for a sound,
// but doesn't expire or unlink it.
//=========================================================
2016-08-01 13:13:13 +02:00
void CSound::Reset( void )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
m_vecOrigin = g_vecZero;
m_iType = 0;
m_iVolume = 0;
m_iNext = SOUNDLIST_EMPTY;
2016-06-04 15:24:23 +02:00
}
//=========================================================
// FIsSound - returns TRUE if the sound is an Audible sound
//=========================================================
2016-08-01 13:13:13 +02:00
BOOL CSound::FIsSound( void )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
if( m_iType & ( bits_SOUND_COMBAT | bits_SOUND_WORLD | bits_SOUND_PLAYER | bits_SOUND_DANGER ) )
2016-06-04 15:24:23 +02:00
{
return TRUE;
}
return FALSE;
}
//=========================================================
// FIsScent - returns TRUE if the sound is actually a scent
//=========================================================
2016-08-01 13:13:13 +02:00
BOOL CSound::FIsScent( void )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
if( m_iType & ( bits_SOUND_CARCASS | bits_SOUND_MEAT | bits_SOUND_GARBAGE ) )
2016-06-04 15:24:23 +02:00
{
return TRUE;
}
return FALSE;
}
//=========================================================
// Spawn
//=========================================================
2016-08-01 13:13:13 +02:00
void CSoundEnt::Spawn( void )
2016-06-04 15:24:23 +02:00
{
pev->solid = SOLID_NOT;
Initialize();
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 1.0f;
2016-06-04 15:24:23 +02:00
}
//=========================================================
// Think - at interval, the entire active sound list is checked
// for sounds that have ExpireTimes less than or equal
// to the current world time, and these sounds are deallocated.
//=========================================================
2016-08-01 13:13:13 +02:00
void CSoundEnt::Think( void )
2016-06-04 15:24:23 +02:00
{
int iSound;
int iPreviousSound;
2019-10-13 13:49:25 +02:00
pev->nextthink = gpGlobals->time + 0.3f;// how often to check the sound list.
2016-06-04 15:24:23 +02:00
iPreviousSound = SOUNDLIST_EMPTY;
iSound = m_iActiveSound;
2016-08-01 13:13:13 +02:00
while( iSound != SOUNDLIST_EMPTY )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
if( m_SoundPool[ iSound ].m_flExpireTime <= gpGlobals->time && m_SoundPool[ iSound ].m_flExpireTime != SOUND_NEVER_EXPIRE )
2016-06-04 15:24:23 +02:00
{
int iNext = m_SoundPool[ iSound ].m_iNext;
// move this sound back into the free list
FreeSound( iSound, iPreviousSound );
iSound = iNext;
}
else
{
iPreviousSound = iSound;
iSound = m_SoundPool[ iSound ].m_iNext;
}
}
2016-08-01 13:13:13 +02:00
if( m_fShowReport )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
ALERT( at_aiconsole, "Soundlist: %d / %d (%d)\n", ISoundsInList( SOUNDLISTTYPE_ACTIVE ),ISoundsInList( SOUNDLISTTYPE_FREE ), ISoundsInList( SOUNDLISTTYPE_ACTIVE ) - m_cLastActiveSounds );
m_cLastActiveSounds = ISoundsInList( SOUNDLISTTYPE_ACTIVE );
2016-06-04 15:24:23 +02:00
}
}
//=========================================================
// Precache - dummy function
//=========================================================
2016-08-01 13:13:13 +02:00
void CSoundEnt::Precache( void )
2016-06-04 15:24:23 +02:00
{
}
//=========================================================
// FreeSound - clears the passed active sound and moves it
// to the top of the free list. TAKE CARE to only call this
// function for sounds in the Active list!!
//=========================================================
2016-08-01 13:13:13 +02:00
void CSoundEnt::FreeSound( int iSound, int iPrevious )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
if( !pSoundEnt )
2016-06-04 15:24:23 +02:00
{
// no sound ent!
return;
}
2016-08-01 13:13:13 +02:00
if( iPrevious != SOUNDLIST_EMPTY )
2016-06-04 15:24:23 +02:00
{
// iSound is not the head of the active list, so
// must fix the index for the Previous sound
2016-08-01 13:13:13 +02:00
//pSoundEnt->m_SoundPool[iPrevious].m_iNext = m_SoundPool[iSound].m_iNext;
pSoundEnt->m_SoundPool[iPrevious].m_iNext = pSoundEnt->m_SoundPool[iSound].m_iNext;
2016-06-04 15:24:23 +02:00
}
else
{
// the sound we're freeing IS the head of the active list.
2016-08-01 13:13:13 +02:00
pSoundEnt->m_iActiveSound = pSoundEnt->m_SoundPool[iSound].m_iNext;
2016-06-04 15:24:23 +02:00
}
// make iSound the head of the Free list.
2016-08-01 13:13:13 +02:00
pSoundEnt->m_SoundPool[iSound].m_iNext = pSoundEnt->m_iFreeSound;
2016-06-04 15:24:23 +02:00
pSoundEnt->m_iFreeSound = iSound;
}
//=========================================================
// IAllocSound - moves a sound from the Free list to the
// Active list returns the index of the alloc'd sound
//=========================================================
2016-08-01 13:13:13 +02:00
int CSoundEnt::IAllocSound( void )
2016-06-04 15:24:23 +02:00
{
int iNewSound;
2016-08-01 13:13:13 +02:00
if( m_iFreeSound == SOUNDLIST_EMPTY )
2016-06-04 15:24:23 +02:00
{
// no free sound!
2016-08-01 13:13:13 +02:00
ALERT( at_console, "Free Sound List is full!\n" );
2016-06-04 15:24:23 +02:00
return SOUNDLIST_EMPTY;
}
// there is at least one sound available, so move it to the
// Active sound list, and return its SoundPool index.
2016-08-01 13:13:13 +02:00
2016-06-04 15:24:23 +02:00
iNewSound = m_iFreeSound;// copy the index of the next free sound
2016-08-01 13:13:13 +02:00
m_iFreeSound = m_SoundPool[m_iFreeSound].m_iNext;// move the index down into the free list.
2016-06-04 15:24:23 +02:00
2016-08-01 13:13:13 +02:00
m_SoundPool[iNewSound].m_iNext = m_iActiveSound;// point the new sound at the top of the active list.
2016-06-04 15:24:23 +02:00
m_iActiveSound = iNewSound;// now make the new sound the top of the active list. You're done.
return iNewSound;
}
//=========================================================
// InsertSound - Allocates a free sound and fills it with
// sound info.
//=========================================================
2016-08-01 13:13:13 +02:00
void CSoundEnt::InsertSound( int iType, const Vector &vecOrigin, int iVolume, float flDuration )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
int iThisSound;
2016-06-04 15:24:23 +02:00
2016-08-01 13:13:13 +02:00
if( !pSoundEnt )
2016-06-04 15:24:23 +02:00
{
// no sound ent!
return;
}
iThisSound = pSoundEnt->IAllocSound();
2016-08-01 13:13:13 +02:00
if( iThisSound == SOUNDLIST_EMPTY )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
ALERT( at_console, "Could not AllocSound() for InsertSound() (DLL)\n" );
2016-06-04 15:24:23 +02:00
return;
}
2016-08-01 13:13:13 +02:00
pSoundEnt->m_SoundPool[iThisSound].m_vecOrigin = vecOrigin;
pSoundEnt->m_SoundPool[iThisSound].m_iType = iType;
pSoundEnt->m_SoundPool[iThisSound].m_iVolume = iVolume;
pSoundEnt->m_SoundPool[iThisSound].m_flExpireTime = gpGlobals->time + flDuration;
2016-06-04 15:24:23 +02:00
}
//=========================================================
// Initialize - clears all sounds and moves them into the
// free sound list.
//=========================================================
2016-08-01 13:13:13 +02:00
void CSoundEnt::Initialize( void )
2016-06-04 15:24:23 +02:00
{
int i;
int iSound;
2016-06-13 15:45:25 +02:00
m_cLastActiveSounds = 0;
2016-06-04 15:24:23 +02:00
m_iFreeSound = 0;
m_iActiveSound = SOUNDLIST_EMPTY;
2016-08-01 13:13:13 +02:00
for( i = 0; i < MAX_WORLD_SOUNDS; i++ )
{
// clear all sounds, and link them into the free sound list.
2016-08-01 13:13:13 +02:00
m_SoundPool[i].Clear();
m_SoundPool[i].m_iNext = i + 1;
2016-06-04 15:24:23 +02:00
}
2016-08-01 13:13:13 +02:00
m_SoundPool[i - 1].m_iNext = SOUNDLIST_EMPTY;// terminate the list here.
2016-06-04 15:24:23 +02:00
// now reserve enough sounds for each client
2016-08-01 13:13:13 +02:00
for( i = 0; i < gpGlobals->maxClients; i++ )
2016-06-04 15:24:23 +02:00
{
iSound = pSoundEnt->IAllocSound();
2016-08-01 13:13:13 +02:00
if( iSound == SOUNDLIST_EMPTY )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
ALERT( at_console, "Could not AllocSound() for Client Reserve! (DLL)\n" );
2016-06-04 15:24:23 +02:00
return;
}
2016-08-01 13:13:13 +02:00
pSoundEnt->m_SoundPool[iSound].m_flExpireTime = SOUND_NEVER_EXPIRE;
2016-06-04 15:24:23 +02:00
}
2016-08-01 13:13:13 +02:00
if( CVAR_GET_FLOAT( "displaysoundlist" ) == 1 )
2016-06-04 15:24:23 +02:00
{
m_fShowReport = TRUE;
}
else
{
m_fShowReport = FALSE;
}
}
//=========================================================
// ISoundsInList - returns the number of sounds in the desired
// sound list.
//=========================================================
2016-08-01 13:13:13 +02:00
int CSoundEnt::ISoundsInList( int iListType )
2016-06-04 15:24:23 +02:00
{
int i;
int iThisSound = 0;
2016-08-01 13:13:13 +02:00
if( iListType == SOUNDLISTTYPE_FREE )
2016-06-04 15:24:23 +02:00
{
iThisSound = m_iFreeSound;
}
2016-08-01 13:13:13 +02:00
else if( iListType == SOUNDLISTTYPE_ACTIVE )
2016-06-04 15:24:23 +02:00
{
iThisSound = m_iActiveSound;
}
else
{
2016-08-01 13:13:13 +02:00
ALERT( at_console, "Unknown Sound List Type!\n" );
2016-06-04 15:24:23 +02:00
}
2016-08-01 13:13:13 +02:00
if( iThisSound == SOUNDLIST_EMPTY )
2016-06-04 15:24:23 +02:00
{
return 0;
}
i = 0;
2016-08-01 13:13:13 +02:00
while( iThisSound != SOUNDLIST_EMPTY )
2016-06-04 15:24:23 +02:00
{
i++;
2016-08-01 13:13:13 +02:00
iThisSound = m_SoundPool[iThisSound].m_iNext;
2016-06-04 15:24:23 +02:00
}
return i;
}
//=========================================================
// ActiveList - returns the head of the active sound list
//=========================================================
2016-08-01 13:13:13 +02:00
int CSoundEnt::ActiveList( void )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
if( !pSoundEnt )
2016-06-04 15:24:23 +02:00
{
return SOUNDLIST_EMPTY;
}
return pSoundEnt->m_iActiveSound;
}
//=========================================================
// FreeList - returns the head of the free sound list
//=========================================================
2016-08-01 13:13:13 +02:00
int CSoundEnt::FreeList( void )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
if( !pSoundEnt )
2016-06-04 15:24:23 +02:00
{
return SOUNDLIST_EMPTY;
}
return pSoundEnt->m_iFreeSound;
}
//=========================================================
// SoundPointerForIndex - returns a pointer to the instance
// of CSound at index's position in the sound pool.
//=========================================================
2016-08-01 13:13:13 +02:00
CSound *CSoundEnt::SoundPointerForIndex( int iIndex )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
if( !pSoundEnt )
2016-06-04 15:24:23 +02:00
{
return NULL;
}
2016-08-01 13:13:13 +02:00
if( iIndex > ( MAX_WORLD_SOUNDS - 1 ) )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
ALERT( at_console, "SoundPointerForIndex() - Index too large!\n" );
2016-06-04 15:24:23 +02:00
return NULL;
}
2016-08-01 13:13:13 +02:00
if( iIndex < 0 )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
ALERT( at_console, "SoundPointerForIndex() - Index < 0!\n" );
2016-06-04 15:24:23 +02:00
return NULL;
}
2016-08-01 13:13:13 +02:00
return &pSoundEnt->m_SoundPool[iIndex];
2016-06-04 15:24:23 +02:00
}
//=========================================================
// Clients are numbered from 1 to MAXCLIENTS, but the client
// reserved sounds in the soundlist are from 0 to MAXCLIENTS - 1,
// so this function ensures that a client gets the proper index
// to his reserved sound in the soundlist.
//=========================================================
2016-08-01 13:13:13 +02:00
int CSoundEnt::ClientSoundIndex( edict_t *pClient )
2016-06-04 15:24:23 +02:00
{
int iReturn = ENTINDEX( pClient ) - 1;
2021-06-07 02:05:58 +02:00
#if _DEBUG
2016-08-01 13:13:13 +02:00
if( iReturn < 0 || iReturn > gpGlobals->maxClients )
2016-06-04 15:24:23 +02:00
{
2016-08-01 13:13:13 +02:00
ALERT( at_console, "** ClientSoundIndex returning a bogus value! **\n" );
2016-06-04 15:24:23 +02:00
}
#endif // _DEBUG
return iReturn;
2016-04-17 22:30:05 +02:00
}