void CAI_Senses::Listen( void ) { m_iAudibleList = SOUNDLIST_EMPTY; int iSoundMask = GetOuter()->GetSoundInterests(); if ( iSoundMask != SOUND_NONE && !(GetOuter()->HasSpawnFlags(SF_NPC_WAIT_TILL_SEEN)) ) { int iSound = CSoundEnt::ActiveList(); while ( iSound != SOUNDLIST_EMPTY ) { CSound *pCurrentSound = CSoundEnt::SoundPointerForIndex( iSound ); if ( pCurrentSound && (iSoundMask & pCurrentSound->SoundType()) && CanHearSound( pCurrentSound ) ) { // the npc cares about this sound, and it's close enough to hear. pCurrentSound->m_iNextAudible = m_iAudibleList; m_iAudibleList = iSound; } iSound = pCurrentSound->NextSound(); } } GetOuter()->OnListened(); }
//========================================================= // InsertSound - Allocates a free sound and fills it with // sound info. //========================================================= void CSoundEnt::InsertSound ( int iType, const Vector &vecOrigin, int iVolume, float flDuration, CBaseEntity *pOwner, int soundChannelIndex, CBaseEntity *pSoundTarget ) { int iThisSound; if ( !g_pSoundEnt ) return; if( soundChannelIndex == SOUNDENT_CHANNEL_UNSPECIFIED ) { // No sound channel specified. So just make a new sound. iThisSound = g_pSoundEnt->IAllocSound(); } else { // If this entity has already got a sound in the soundlist that's on this // channel, update that sound. Otherwise add a new one. iThisSound = g_pSoundEnt->FindOrAllocateSound( pOwner, soundChannelIndex ); } if ( iThisSound == SOUNDLIST_EMPTY ) { //DevMsg( "Could not AllocSound() for InsertSound() (Game DLL)\n" ); return; } CSound *pSound; pSound = &g_pSoundEnt->m_SoundPool[ iThisSound ]; pSound->SetSoundOrigin( vecOrigin ); pSound->m_iType = iType; pSound->m_iVolume = iVolume; pSound->m_flOcclusionScale = 0.5; pSound->m_flExpireTime = gpGlobals->curtime + flDuration; pSound->m_bNoExpirationTime = false; pSound->m_hOwner.Set( pOwner ); pSound->m_hTarget.Set( pSoundTarget ); pSound->m_ownerChannelIndex = soundChannelIndex; // Keep track of whether this sound had an owner when it was made. If the sound has a long duration, // the owner could disappear by the time someone hears this sound, so we have to look at this boolean // and throw out sounds who have a NULL owner but this field set to true. (sjb) 12/2/2005 if( pOwner ) { pSound->m_bHasOwner = true; } else { pSound->m_bHasOwner = false; } if( displaysoundlist.GetInt() == 1 ) { Msg(" Added Sound! Type:%d Duration:%f (Time:%f)\n", pSound->SoundType(), flDuration, gpGlobals->curtime ); } if( displaysoundlist.GetInt() == 2 && (iType & SOUND_DANGER) ) { Msg(" Added Danger Sound! Duration:%f (Time:%f)\n", flDuration, gpGlobals->curtime ); } }
//----------------------------------------------------------------------------- // Purpose: Listens for sounds and updates the value of the SoundLevel output. //----------------------------------------------------------------------------- void CEnvMicrophone::Think(void) { int nSound = CSoundEnt::ActiveList(); bool fHearSound = false; float flMaxVolume = 0; // // Find the loudest sound that this microphone cares about. // while (nSound != SOUNDLIST_EMPTY) { CSound *pCurrentSound = CSoundEnt::SoundPointerForIndex(nSound); if (pCurrentSound) { if (m_nSoundMask & pCurrentSound->SoundType()) { float flVolume = 0; if (CanHearSound(pCurrentSound, flVolume) && (flVolume > flMaxVolume)) { flMaxVolume = flVolume; fHearSound = true; } } } nSound = pCurrentSound->NextSound(); } if( fHearSound ) { m_OnHeardSound.FireOutput( this, this ); } if (flMaxVolume != m_SoundLevel.Get()) { // // Don't smooth if we are within an epsilon. This allows the output to stop firing // much more quickly. // if (fabs(flMaxVolume - m_SoundLevel.Get()) < MICROPHONE_SETTLE_EPSILON) { m_SoundLevel.Set(flMaxVolume, this, this); } else { m_SoundLevel.Set(flMaxVolume * (1 - m_flSmoothFactor) + m_SoundLevel.Get() * m_flSmoothFactor, this, this); } } SetNextThink( gpGlobals->curtime + 0.1f ); }