Portable Half-Life SDK. GoldSource and Xash3D. Crossplatform.
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.

1927 lines
48 KiB

9 years ago
/***
*
* 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.
*
****/
//=========================================================
// sound.cpp
//=========================================================
#include "extdll.h"
#include "util.h"
#include "cbase.h"
#include "weapons.h"
#include "player.h"
#include "talkmonster.h"
#include "gamerules.h"
static char *memfgets( byte *pMemFile, int fileSize, int &filePos, char *pBuffer, int bufferSize );
// ==================== GENERIC AMBIENT SOUND ======================================
// runtime pitch shift and volume fadein/out structure
// NOTE: IF YOU CHANGE THIS STRUCT YOU MUST CHANGE THE SAVE/RESTORE VERSION NUMBER
// SEE BELOW (in the typedescription for the class)
typedef struct dynpitchvol
{
// NOTE: do not change the order of these parameters
// NOTE: unless you also change order of rgdpvpreset array elements!
int preset;
int pitchrun; // pitch shift % when sound is running 0 - 255
int pitchstart; // pitch shift % when sound stops or starts 0 - 255
int spinup; // spinup time 0 - 100
int spindown; // spindown time 0 - 100
int volrun; // volume change % when sound is running 0 - 10
int volstart; // volume change % when sound stops or starts 0 - 10
int fadein; // volume fade in time 0 - 100
int fadeout; // volume fade out time 0 - 100
// Low Frequency Oscillator
8 years ago
int lfotype; // 0) off 1) square 2) triangle 3) random
9 years ago
int lforate; // 0 - 1000, how fast lfo osciallates
8 years ago
9 years ago
int lfomodpitch; // 0-100 mod of current pitch. 0 is off.
int lfomodvol; // 0-100 mod of current volume. 0 is off.
int cspinup; // each trigger hit increments counter and spinup pitch
8 years ago
int cspincount;
9 years ago
int pitch;
int spinupsav;
int spindownsav;
int pitchfrac;
int vol;
int fadeinsav;
int fadeoutsav;
int volfrac;
8 years ago
int lfofrac;
int lfomult;
9 years ago
} dynpitchvol_t;
#define CDPVPRESETMAX 27
// presets for runtime pitch and vol modulation of ambient sounds
dynpitchvol_t rgdpvpreset[CDPVPRESETMAX] =
9 years ago
{
// pitch pstart spinup spindwn volrun volstrt fadein fadeout lfotype lforate modptch modvol cspnup
{1, 255, 75, 95, 95, 10, 1, 50, 95, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{2, 255, 85, 70, 88, 10, 1, 20, 88, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{3, 255, 100, 50, 75, 10, 1, 10, 75, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{4, 100, 100, 0, 0, 10, 1, 90, 90, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{5, 100, 100, 0, 0, 10, 1, 80, 80, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{6, 100, 100, 0, 0, 10, 1, 50, 70, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{7, 100, 100, 0, 0, 5, 1, 40, 50, 1, 50, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0},
{8, 100, 100, 0, 0, 5, 1, 40, 50, 1, 150, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0},
{9, 100, 100, 0, 0, 5, 1, 40, 50, 1, 750, 0, 10, 0, 0,0,0,0,0,0,0,0,0,0},
{10,128, 100, 50, 75, 10, 1, 30, 40, 2, 8, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{11,128, 100, 50, 75, 10, 1, 30, 40, 2, 25, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{12,128, 100, 50, 75, 10, 1, 30, 40, 2, 70, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{13,50, 50, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{14,70, 70, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{15,90, 90, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{16,120, 120, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{17,180, 180, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{18,255, 255, 0, 0, 10, 1, 20, 50, 0, 0, 0, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{19,200, 75, 90, 90, 10, 1, 50, 90, 2, 100, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{20,255, 75, 97, 90, 10, 1, 50, 90, 1, 40, 50, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{21,100, 100, 0, 0, 10, 1, 30, 50, 3, 15, 20, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{22,160, 160, 0, 0, 10, 1, 50, 50, 3, 500, 25, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{23,255, 75, 88, 0, 10, 1, 40, 0, 0, 0, 0, 0, 5, 0,0,0,0,0,0,0,0,0,0},
{24,200, 20, 95, 70, 10, 1, 70, 70, 3, 20, 50, 0, 0, 0,0,0,0,0,0,0,0,0,0},
{25,180, 100, 50, 60, 10, 1, 40, 60, 2, 90, 100, 100, 0, 0,0,0,0,0,0,0,0,0,0},
{26,60, 60, 0, 0, 10, 1, 40, 70, 3, 80, 20, 50, 0, 0,0,0,0,0,0,0,0,0,0},
{27,128, 90, 10, 10, 10, 1, 20, 40, 1, 5, 10, 20, 0, 0,0,0,0,0,0,0,0,0,0}
};
class CAmbientGeneric : public CBaseEntity
{
public:
8 years ago
void KeyValue( KeyValueData* pkvd );
9 years ago
void Spawn( void );
void Precache( void );
8 years ago
void EXPORT ToggleUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
9 years ago
void EXPORT RampThink( void );
8 years ago
void InitModulationParms( void );
9 years ago
8 years ago
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
virtual int ObjectCaps( void ) { return ( CBaseEntity::ObjectCaps() & ~FCAP_ACROSS_TRANSITION ); }
9 years ago
float m_flAttenuation; // attenuation value
8 years ago
dynpitchvol_t m_dpv;
9 years ago
8 years ago
BOOL m_fActive; // only TRUE when the entity is playing a looping sound
BOOL m_fLooping; // TRUE when the sound played will loop
9 years ago
};
LINK_ENTITY_TO_CLASS( ambient_generic, CAmbientGeneric )
TYPEDESCRIPTION CAmbientGeneric::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CAmbientGeneric, m_flAttenuation, FIELD_FLOAT ),
DEFINE_FIELD( CAmbientGeneric, m_fActive, FIELD_BOOLEAN ),
DEFINE_FIELD( CAmbientGeneric, m_fLooping, FIELD_BOOLEAN ),
// HACKHACK - This is not really in the spirit of the save/restore design, but save this
// out as a binary data block. If the dynpitchvol_t is changed, old saved games will NOT
// load these correctly, so bump the save/restore version if you change the size of the struct
// The right way to do this is to split the input parms (read in keyvalue) into members and re-init this
// struct in Precache(), but it's unlikely that the struct will change, so it's not worth the time right now.
DEFINE_ARRAY( CAmbientGeneric, m_dpv, FIELD_CHARACTER, sizeof(dynpitchvol_t) ),
};
IMPLEMENT_SAVERESTORE( CAmbientGeneric, CBaseEntity )
9 years ago
//
// ambient_generic - general-purpose user-defined static sound
//
8 years ago
void CAmbientGeneric::Spawn( void )
9 years ago
{
/*
-1 : "Default"
0 : "Everywhere"
200 : "Small Radius"
125 : "Medium Radius"
80 : "Large Radius"
*/
8 years ago
if( FBitSet( pev->spawnflags, AMBIENT_SOUND_EVERYWHERE ) )
9 years ago
{
m_flAttenuation = ATTN_NONE;
}
8 years ago
else if( FBitSet( pev->spawnflags, AMBIENT_SOUND_SMALLRADIUS ) )
9 years ago
{
m_flAttenuation = ATTN_IDLE;
}
8 years ago
else if( FBitSet( pev->spawnflags, AMBIENT_SOUND_MEDIUMRADIUS ) )
9 years ago
{
m_flAttenuation = ATTN_STATIC;
}
8 years ago
else if( FBitSet( pev->spawnflags, AMBIENT_SOUND_LARGERADIUS ) )
9 years ago
{
m_flAttenuation = ATTN_NORM;
}
else
{
// if the designer didn't set a sound attenuation, default to one.
9 years ago
m_flAttenuation = ATTN_STATIC;
}
const char *szSoundFile = STRING( pev->message );
9 years ago
if( FStringNull( pev->message ) || szSoundFile[0] == '\0' )
9 years ago
{
ALERT( at_error, "EMPTY AMBIENT AT: %f, %f, %f\n", (double)pev->origin.x, (double)pev->origin.y, (double)pev->origin.z );
pev->nextthink = gpGlobals->time + 0.1f;
9 years ago
SetThink( &CBaseEntity::SUB_Remove );
return;
}
8 years ago
pev->solid = SOLID_NOT;
pev->movetype = MOVETYPE_NONE;
9 years ago
// Set up think function for dynamic modification
// of ambient sound's pitch or volume. Don't
// start thinking yet.
8 years ago
SetThink( &CAmbientGeneric::RampThink );
pev->nextthink = 0.0f;
9 years ago
// allow on/off switching via 'use' function.
SetUse( &CAmbientGeneric::ToggleUse );
m_fActive = FALSE;
8 years ago
if( FBitSet( pev->spawnflags, AMBIENT_SOUND_NOT_LOOPING ) )
9 years ago
m_fLooping = FALSE;
else
m_fLooping = TRUE;
8 years ago
Precache();
9 years ago
}
8 years ago
void CAmbientGeneric::Precache( void )
9 years ago
{
const char *szSoundFile = STRING( pev->message );
9 years ago
if( !FStringNull( pev->message ) && szSoundFile[0] != '\0' )
9 years ago
{
8 years ago
if( *szSoundFile != '!' )
PRECACHE_SOUND( szSoundFile );
9 years ago
}
9 years ago
// init all dynamic modulation parms
InitModulationParms();
8 years ago
if( !FBitSet( pev->spawnflags, AMBIENT_SOUND_START_SILENT ) )
9 years ago
{
// start the sound ASAP
8 years ago
if( m_fLooping )
9 years ago
m_fActive = TRUE;
}
8 years ago
if( m_fActive )
9 years ago
{
8 years ago
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile,
( m_dpv.vol * 0.01f ), m_flAttenuation, SND_SPAWNING, m_dpv.pitch );
9 years ago
pev->nextthink = gpGlobals->time + 0.1f;
9 years ago
}
}
// RampThink - Think at 5hz if we are dynamically modifying
// pitch or volume of the playing sound. This function will
// ramp pitch and/or volume up or down, modify pitch/volume
// with lfo if active.
8 years ago
void CAmbientGeneric::RampThink( void )
9 years ago
{
const char *szSoundFile = STRING( pev->message );
9 years ago
int pitch = m_dpv.pitch;
int vol = m_dpv.vol;
int flags = 0;
int fChanged = 0; // FALSE if pitch and vol remain unchanged this round
8 years ago
int prev;
9 years ago
8 years ago
if( !m_dpv.spinup && !m_dpv.spindown && !m_dpv.fadein && !m_dpv.fadeout && !m_dpv.lfotype )
9 years ago
return; // no ramps or lfo, stop thinking
// ==============
// pitch envelope
// ==============
8 years ago
if( m_dpv.spinup || m_dpv.spindown )
9 years ago
{
prev = m_dpv.pitchfrac >> 8;
8 years ago
if( m_dpv.spinup > 0 )
9 years ago
m_dpv.pitchfrac += m_dpv.spinup;
8 years ago
else if( m_dpv.spindown > 0 )
9 years ago
m_dpv.pitchfrac -= m_dpv.spindown;
pitch = m_dpv.pitchfrac >> 8;
8 years ago
if( pitch > m_dpv.pitchrun )
9 years ago
{
pitch = m_dpv.pitchrun;
m_dpv.spinup = 0; // done with ramp up
}
8 years ago
if( pitch < m_dpv.pitchstart )
9 years ago
{
// pitch = m_dpv.pitchstart;
9 years ago
m_dpv.spindown = 0; // done with ramp down
// shut sound off
8 years ago
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile,
0, 0, SND_STOP, 0 );
9 years ago
// return without setting nextthink
return;
}
8 years ago
if( pitch > 255 )
pitch = 255;
if( pitch < 1 )
pitch = 1;
9 years ago
m_dpv.pitch = pitch;
8 years ago
fChanged |= ( prev != pitch );
9 years ago
flags |= SND_CHANGE_PITCH;
}
// ==================
// amplitude envelope
// ==================
8 years ago
if( m_dpv.fadein || m_dpv.fadeout )
9 years ago
{
prev = m_dpv.volfrac >> 8;
8 years ago
if( m_dpv.fadein > 0 )
9 years ago
m_dpv.volfrac += m_dpv.fadein;
8 years ago
else if( m_dpv.fadeout > 0 )
9 years ago
m_dpv.volfrac -= m_dpv.fadeout;
vol = m_dpv.volfrac >> 8;
8 years ago
if( vol > m_dpv.volrun )
9 years ago
{
vol = m_dpv.volrun;
m_dpv.fadein = 0; // done with ramp up
}
8 years ago
if( vol < m_dpv.volstart )
9 years ago
{
// vol = m_dpv.volstart;
9 years ago
m_dpv.fadeout = 0; // done with ramp down
9 years ago
// shut sound off
8 years ago
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile,
0, 0, SND_STOP, 0 );
9 years ago
// return without setting nextthink
return;
}
8 years ago
if( vol > 100 )
vol = 100;
if( vol < 1 )
vol = 1;
9 years ago
m_dpv.vol = vol;
8 years ago
fChanged |= ( prev != vol );
9 years ago
flags |= SND_CHANGE_VOL;
}
// ===================
// pitch/amplitude LFO
// ===================
8 years ago
if( m_dpv.lfotype )
9 years ago
{
int pos;
8 years ago
if( m_dpv.lfofrac > 0x6fffffff )
9 years ago
m_dpv.lfofrac = 0;
// update lfo, lfofrac/255 makes a triangle wave 0-255
m_dpv.lfofrac += m_dpv.lforate;
pos = m_dpv.lfofrac >> 8;
8 years ago
if( m_dpv.lfofrac < 0 )
9 years ago
{
m_dpv.lfofrac = 0;
8 years ago
m_dpv.lforate = abs( m_dpv.lforate );
9 years ago
pos = 0;
}
8 years ago
else if( pos > 255 )
9 years ago
{
pos = 255;
8 years ago
m_dpv.lfofrac = ( 255 << 8 );
m_dpv.lforate = -abs( m_dpv.lforate );
9 years ago
}
8 years ago
switch( m_dpv.lfotype )
9 years ago
{
case LFO_SQUARE:
8 years ago
if( pos < 128 )
9 years ago
m_dpv.lfomult = 255;
else
m_dpv.lfomult = 0;
break;
case LFO_RANDOM:
8 years ago
if( pos == 255 )
m_dpv.lfomult = RANDOM_LONG( 0, 255 );
9 years ago
break;
case LFO_TRIANGLE:
default:
m_dpv.lfomult = pos;
break;
}
8 years ago
if( m_dpv.lfomodpitch )
9 years ago
{
prev = pitch;
// pitch 0-255
8 years ago
pitch += ( ( m_dpv.lfomult - 128 ) * m_dpv.lfomodpitch ) / 100;
9 years ago
8 years ago
if( pitch > 255 )
pitch = 255;
if( pitch < 1 )
pitch = 1;
9 years ago
8 years ago
fChanged |= ( prev != pitch );
9 years ago
flags |= SND_CHANGE_PITCH;
}
8 years ago
if( m_dpv.lfomodvol )
9 years ago
{
// vol 0-100
prev = vol;
8 years ago
vol += ( ( m_dpv.lfomult - 128 ) * m_dpv.lfomodvol ) / 100;
9 years ago
8 years ago
if( vol > 100 )
vol = 100;
if( vol < 0 )
vol = 0;
8 years ago
fChanged |= ( prev != vol );
9 years ago
flags |= SND_CHANGE_VOL;
}
}
// Send update to playing sound only if we actually changed
// pitch or volume in this routine.
8 years ago
if( flags && fChanged )
9 years ago
{
8 years ago
if( pitch == PITCH_NORM )
9 years ago
pitch = PITCH_NORM + 1; // don't send 'no pitch' !
8 years ago
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile,
( vol * 0.01f ), m_flAttenuation, flags, pitch );
9 years ago
}
// update ramps at 5hz
pev->nextthink = gpGlobals->time + 0.2f;
9 years ago
return;
}
// Init all ramp params in preparation to
// play a new sound
8 years ago
void CAmbientGeneric::InitModulationParms( void )
9 years ago
{
int pitchinc;
m_dpv.volrun = (int)( pev->health * 10 ); // 0 - 100
8 years ago
if( m_dpv.volrun > 100 )
m_dpv.volrun = 100;
if( m_dpv.volrun < 0 )
m_dpv.volrun = 0;
9 years ago
// get presets
8 years ago
if( m_dpv.preset != 0 && m_dpv.preset <= CDPVPRESETMAX )
9 years ago
{
// load preset values
m_dpv = rgdpvpreset[m_dpv.preset - 1];
// fixup preset values, just like
// fixups in KeyValue routine.
8 years ago
if( m_dpv.spindown > 0 )
m_dpv.spindown = ( 101 - m_dpv.spindown ) * 64;
if( m_dpv.spinup > 0 )
m_dpv.spinup = ( 101 - m_dpv.spinup ) * 64;
9 years ago
m_dpv.volstart *= 10;
m_dpv.volrun *= 10;
8 years ago
if( m_dpv.fadein > 0 )
m_dpv.fadein = ( 101 - m_dpv.fadein ) * 64;
if( m_dpv.fadeout > 0 )
m_dpv.fadeout = ( 101 - m_dpv.fadeout ) * 64;
9 years ago
m_dpv.lforate *= 256;
m_dpv.fadeinsav = m_dpv.fadein;
m_dpv.fadeoutsav = m_dpv.fadeout;
m_dpv.spinupsav = m_dpv.spinup;
m_dpv.spindownsav = m_dpv.spindown;
}
m_dpv.fadein = m_dpv.fadeinsav;
m_dpv.fadeout = 0;
8 years ago
if( m_dpv.fadein )
9 years ago
m_dpv.vol = m_dpv.volstart;
else
m_dpv.vol = m_dpv.volrun;
m_dpv.spinup = m_dpv.spinupsav;
m_dpv.spindown = 0;
8 years ago
if( m_dpv.spinup )
9 years ago
m_dpv.pitch = m_dpv.pitchstart;
else
m_dpv.pitch = m_dpv.pitchrun;
8 years ago
if( m_dpv.pitch == 0 )
9 years ago
m_dpv.pitch = PITCH_NORM;
m_dpv.pitchfrac = m_dpv.pitch << 8;
m_dpv.volfrac = m_dpv.vol << 8;
m_dpv.lfofrac = 0;
8 years ago
m_dpv.lforate = abs( m_dpv.lforate );
9 years ago
m_dpv.cspincount = 1;
8 years ago
if( m_dpv.cspinup )
9 years ago
{
8 years ago
pitchinc = ( 255 - m_dpv.pitchstart ) / m_dpv.cspinup;
9 years ago
m_dpv.pitchrun = m_dpv.pitchstart + pitchinc;
8 years ago
if( m_dpv.pitchrun > 255 )
m_dpv.pitchrun = 255;
9 years ago
}
8 years ago
if( ( m_dpv.spinupsav || m_dpv.spindownsav || ( m_dpv.lfotype && m_dpv.lfomodpitch ) )
&& ( m_dpv.pitch == PITCH_NORM ) )
9 years ago
m_dpv.pitch = PITCH_NORM + 1; // must never send 'no pitch' as first pitch
// if we intend to pitch shift later!
9 years ago
}
//
// ToggleUse - turns an ambient sound on or off. If the
// ambient is a looping sound, mark sound as active (m_fActive)
// if it's playing, innactive if not. If the sound is not
// a looping sound, never mark it as active.
//
8 years ago
void CAmbientGeneric::ToggleUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
const char *szSoundFile = STRING( pev->message );
9 years ago
float fraction;
8 years ago
if( useType != USE_TOGGLE )
9 years ago
{
8 years ago
if( ( m_fActive && useType == USE_ON ) || ( !m_fActive && useType == USE_OFF ) )
9 years ago
return;
}
// Directly change pitch if arg passed. Only works if sound is already playing.
8 years ago
if( useType == USE_SET && m_fActive ) // Momentary buttons will pass down a float in here
9 years ago
{
fraction = value;
8 years ago
if( fraction > 1.0f )
fraction = 1.0f;
if( fraction < 0.0f )
fraction = 0.01f;
9 years ago
m_dpv.pitch = (int)( fraction * 255.0f );
9 years ago
8 years ago
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile, 0, 0, SND_CHANGE_PITCH, m_dpv.pitch );
9 years ago
return;
}
// Toggle
// m_fActive is TRUE only if a looping sound is playing.
8 years ago
if( m_fActive )
{
// turn sound off
8 years ago
if( m_dpv.cspinup )
9 years ago
{
// Don't actually shut off. Each toggle causes
// incremental spinup to max pitch
8 years ago
if( m_dpv.cspincount <= m_dpv.cspinup )
{
9 years ago
int pitchinc;
// start a new spinup
m_dpv.cspincount++;
8 years ago
pitchinc = ( 255 - m_dpv.pitchstart ) / m_dpv.cspinup;
9 years ago
m_dpv.spinup = m_dpv.spinupsav;
m_dpv.spindown = 0;
m_dpv.pitchrun = m_dpv.pitchstart + pitchinc * m_dpv.cspincount;
8 years ago
if( m_dpv.pitchrun > 255 )
m_dpv.pitchrun = 255;
9 years ago
pev->nextthink = gpGlobals->time + 0.1f;
9 years ago
}
}
else
{
m_fActive = FALSE;
9 years ago
// HACKHACK - this makes the code in Precache() work properly after a save/restore
pev->spawnflags |= AMBIENT_SOUND_START_SILENT;
8 years ago
if( m_dpv.spindownsav || m_dpv.fadeoutsav )
9 years ago
{
// spin it down (or fade it) before shutoff if spindown is set
m_dpv.spindown = m_dpv.spindownsav;
m_dpv.spinup = 0;
m_dpv.fadeout = m_dpv.fadeoutsav;
m_dpv.fadein = 0;
pev->nextthink = gpGlobals->time + 0.1f;
9 years ago
}
else
8 years ago
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile, 0, 0, SND_STOP, 0 );
9 years ago
}
}
else
{
// turn sound on
9 years ago
// only toggle if this is a looping sound. If not looping, each
// trigger will cause the sound to play. If the sound is still
// playing from a previous trigger press, it will be shut off
// and then restarted.
8 years ago
if( m_fLooping )
9 years ago
m_fActive = TRUE;
else
// shut sound off now - may be interrupting a long non-looping sound
8 years ago
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile, 0, 0, SND_STOP, 0 );
9 years ago
// init all ramp params for startup
9 years ago
InitModulationParms();
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile, ( m_dpv.vol * 0.01f ), m_flAttenuation, 0, m_dpv.pitch );
9 years ago
pev->nextthink = gpGlobals->time + 0.1f;
9 years ago
}
}
9 years ago
// KeyValue - load keyvalue pairs into member data of the
// ambient generic. NOTE: called BEFORE spawn!
8 years ago
void CAmbientGeneric::KeyValue( KeyValueData *pkvd )
9 years ago
{
// NOTE: changing any of the modifiers in this code
// NOTE: also requires changing InitModulationParms code.
// preset
8 years ago
if( FStrEq( pkvd->szKeyName, "preset" ) )
9 years ago
{
8 years ago
m_dpv.preset = atoi( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
// pitchrun
8 years ago
else if( FStrEq( pkvd->szKeyName, "pitch" ) )
9 years ago
{
8 years ago
m_dpv.pitchrun = atoi( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
8 years ago
if( m_dpv.pitchrun > 255 )
m_dpv.pitchrun = 255;
if( m_dpv.pitchrun < 0 )
m_dpv.pitchrun = 0;
}
9 years ago
// pitchstart
8 years ago
else if( FStrEq( pkvd->szKeyName, "pitchstart" ) )
9 years ago
{
8 years ago
m_dpv.pitchstart = atoi( pkvd->szValue );
pkvd->fHandled = TRUE;
9 years ago
8 years ago
if( m_dpv.pitchstart > 255 )
m_dpv.pitchstart = 255;
if( m_dpv.pitchstart < 0 )
m_dpv.pitchstart = 0;
9 years ago
}
// spinup
8 years ago
else if( FStrEq( pkvd->szKeyName, "spinup" ) )
9 years ago
{
8 years ago
m_dpv.spinup = atoi( pkvd->szValue );
9 years ago
8 years ago
if( m_dpv.spinup > 100 )
m_dpv.spinup = 100;
if( m_dpv.spinup < 0 )
m_dpv.spinup = 0;
if( m_dpv.spinup > 0 )
m_dpv.spinup = ( 101 - m_dpv.spinup ) * 64;
9 years ago
m_dpv.spinupsav = m_dpv.spinup;
pkvd->fHandled = TRUE;
}
9 years ago
// spindown
8 years ago
else if( FStrEq( pkvd->szKeyName, "spindown" ) )
9 years ago
{
8 years ago
m_dpv.spindown = atoi( pkvd->szValue );
9 years ago
8 years ago
if( m_dpv.spindown > 100 )
m_dpv.spindown = 100;
if( m_dpv.spindown < 0 )
m_dpv.spindown = 0;
if( m_dpv.spindown > 0 )
m_dpv.spindown = ( 101 - m_dpv.spindown ) * 64;
9 years ago
m_dpv.spindownsav = m_dpv.spindown;
pkvd->fHandled = TRUE;
}
// volstart
8 years ago
else if( FStrEq( pkvd->szKeyName, "volstart" ) )
9 years ago
{
8 years ago
m_dpv.volstart = atoi( pkvd->szValue );
9 years ago
8 years ago
if( m_dpv.volstart > 10 )
m_dpv.volstart = 10;
if( m_dpv.volstart < 0 )
m_dpv.volstart = 0;
9 years ago
m_dpv.volstart *= 10; // 0 - 100
pkvd->fHandled = TRUE;
}
// fadein
8 years ago
else if( FStrEq( pkvd->szKeyName, "fadein" ) )
9 years ago
{
8 years ago
m_dpv.fadein = atoi( pkvd->szValue );
8 years ago
if( m_dpv.fadein > 100 )
m_dpv.fadein = 100;
if( m_dpv.fadein < 0 )
m_dpv.fadein = 0;
9 years ago
8 years ago
if( m_dpv.fadein > 0 )
m_dpv.fadein = ( 101 - m_dpv.fadein ) * 64;
9 years ago
m_dpv.fadeinsav = m_dpv.fadein;
pkvd->fHandled = TRUE;
}
// fadeout
8 years ago
else if( FStrEq( pkvd->szKeyName, "fadeout" ) )
9 years ago
{
8 years ago
m_dpv.fadeout = atoi( pkvd->szValue );
8 years ago
if( m_dpv.fadeout > 100 )
m_dpv.fadeout = 100;
if( m_dpv.fadeout < 0 )
m_dpv.fadeout = 0;
9 years ago
8 years ago
if( m_dpv.fadeout > 0 )
m_dpv.fadeout = ( 101 - m_dpv.fadeout ) * 64;
9 years ago
m_dpv.fadeoutsav = m_dpv.fadeout;
pkvd->fHandled = TRUE;
}
// lfotype
8 years ago
else if( FStrEq( pkvd->szKeyName, "lfotype" ) )
9 years ago
{
8 years ago
m_dpv.lfotype = atoi( pkvd->szValue );
if( m_dpv.lfotype > 4 )
m_dpv.lfotype = LFO_TRIANGLE;
9 years ago
pkvd->fHandled = TRUE;
}
// lforate
8 years ago
else if( FStrEq( pkvd->szKeyName, "lforate" ) )
9 years ago
{
8 years ago
m_dpv.lforate = atoi( pkvd->szValue );
8 years ago
if( m_dpv.lforate > 1000 )
m_dpv.lforate = 1000;
if( m_dpv.lforate < 0 )
m_dpv.lforate = 0;
9 years ago
m_dpv.lforate *= 256;
pkvd->fHandled = TRUE;
}
// lfomodpitch
8 years ago
else if( FStrEq( pkvd->szKeyName, "lfomodpitch" ) )
9 years ago
{
8 years ago
m_dpv.lfomodpitch = atoi( pkvd->szValue );
if( m_dpv.lfomodpitch > 100 )
m_dpv.lfomodpitch = 100;
if( m_dpv.lfomodpitch < 0 )
m_dpv.lfomodpitch = 0;
9 years ago
pkvd->fHandled = TRUE;
}
// lfomodvol
8 years ago
else if( FStrEq( pkvd->szKeyName, "lfomodvol" ) )
9 years ago
{
8 years ago
m_dpv.lfomodvol = atoi( pkvd->szValue );
if( m_dpv.lfomodvol > 100 )
m_dpv.lfomodvol = 100;
if( m_dpv.lfomodvol < 0 )
m_dpv.lfomodvol = 0;
9 years ago
pkvd->fHandled = TRUE;
}
// cspinup
8 years ago
else if( FStrEq( pkvd->szKeyName, "cspinup" ) )
9 years ago
{
8 years ago
m_dpv.cspinup = atoi( pkvd->szValue );
if( m_dpv.cspinup > 100 )
m_dpv.cspinup = 100;
if( m_dpv.cspinup < 0 )
m_dpv.cspinup = 0;
9 years ago
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}
// =================== ROOM SOUND FX ==========================================
class CEnvSound : public CPointEntity
{
public:
void KeyValue( KeyValueData* pkvd);
void Spawn( void );
void Think( void );
8 years ago
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
9 years ago
float m_flRadius;
float m_flRoomtype;
};
LINK_ENTITY_TO_CLASS( env_sound, CEnvSound )
TYPEDESCRIPTION CEnvSound::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CEnvSound, m_flRadius, FIELD_FLOAT ),
DEFINE_FIELD( CEnvSound, m_flRoomtype, FIELD_FLOAT ),
};
IMPLEMENT_SAVERESTORE( CEnvSound, CPointEntity )
9 years ago
8 years ago
void CEnvSound::KeyValue( KeyValueData *pkvd )
9 years ago
{
8 years ago
if( FStrEq( pkvd->szKeyName, "radius" ) )
9 years ago
{
8 years ago
m_flRadius = atof( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
8 years ago
if( FStrEq( pkvd->szKeyName, "roomtype" ) )
9 years ago
{
8 years ago
m_flRoomtype = atof( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
}
// returns TRUE if the given sound entity (pev) is in range
// and can see the given player entity (pevTarget)
8 years ago
BOOL FEnvSoundInRange( entvars_t *pev, entvars_t *pevTarget, float *pflRange )
9 years ago
{
CEnvSound *pSound = GetClassPtr( (CEnvSound *)pev );
Vector vecSpot1 = pev->origin + pev->view_ofs;
Vector vecSpot2 = pevTarget->origin + pevTarget->view_ofs;
Vector vecRange;
float flRange;
TraceResult tr;
8 years ago
UTIL_TraceLine( vecSpot1, vecSpot2, ignore_monsters, ENT( pev ), &tr );
9 years ago
// check if line of sight crosses water boundary, or is blocked
8 years ago
if( ( tr.fInOpen && tr.fInWater ) || tr.flFraction != 1 )
9 years ago
return FALSE;
// calc range from sound entity to player
vecRange = tr.vecEndPos - vecSpot1;
flRange = vecRange.Length();
8 years ago
if( pSound->m_flRadius < flRange )
9 years ago
return FALSE;
8 years ago
if( pflRange )
9 years ago
*pflRange = flRange;
return TRUE;
}
//
// A client that is visible and in range of a sound entity will
// have its room_type set by that sound entity. If two or more
// sound entities are contending for a client, then the nearest
// sound entity to the client will set the client's room_type.
// A client's room_type will remain set to its prior value until
// a new in-range, visible sound entity resets a new room_type.
//
// CONSIDER: if player in water state, autoset roomtype to 14,15 or 16.
8 years ago
void CEnvSound::Think( void )
9 years ago
{
// get pointer to client if visible; FIND_CLIENT_IN_PVS will
// cycle through visible clients on consecutive calls.
8 years ago
edict_t *pentPlayer = FIND_CLIENT_IN_PVS( edict() );
9 years ago
CBasePlayer *pPlayer = NULL;
8 years ago
if( FNullEnt( pentPlayer ) )
9 years ago
goto env_sound_Think_slow; // no player in pvs of sound entity, slow it down
8 years ago
pPlayer = GetClassPtr( (CBasePlayer *)VARS( pentPlayer ) );
9 years ago
float flRange;
// check to see if this is the sound entity that is
// currently affecting this player
8 years ago
if( !FNullEnt( pPlayer->m_pentSndLast ) && ( pPlayer->m_pentSndLast == ENT( pev ) ) )
{
9 years ago
// this is the entity currently affecting player, check
// for validity
8 years ago
if( pPlayer->m_flSndRoomtype != 0 && pPlayer->m_flSndRange != 0 )
{
9 years ago
// we're looking at a valid sound entity affecting
// player, make sure it's still valid, update range
8 years ago
if( FEnvSoundInRange( pev, VARS( pentPlayer ), &flRange ) )
{
9 years ago
pPlayer->m_flSndRange = flRange;
goto env_sound_Think_fast;
}
else
{
9 years ago
// current sound entity affecting player is no longer valid,
// flag this state by clearing room_type and range.
// NOTE: we do not actually change the player's room_type
// NOTE: until we have a new valid room_type to change it to.
pPlayer->m_flSndRange = 0;
pPlayer->m_flSndRoomtype = 0;
goto env_sound_Think_slow;
}
}
else
{
9 years ago
// entity is affecting player but is out of range,
// wait passively for another entity to usurp it...
goto env_sound_Think_slow;
}
}
// if we got this far, we're looking at an entity that is contending
// for current player sound. the closest entity to player wins.
8 years ago
if( FEnvSoundInRange( pev, VARS(pentPlayer), &flRange ) )
9 years ago
{
8 years ago
if( flRange < pPlayer->m_flSndRange || pPlayer->m_flSndRange == 0 )
9 years ago
{
// new entity is closer to player, so it wins.
8 years ago
pPlayer->m_pentSndLast = ENT( pev );
9 years ago
pPlayer->m_flSndRoomtype = m_flRoomtype;
pPlayer->m_flSndRange = flRange;
9 years ago
// send room_type command to player's server.
// this should be a rare event - once per change of room_type
// only!
8 years ago
//CLIENT_COMMAND( pentPlayer, "room_type %f", m_flRoomtype );
9 years ago
MESSAGE_BEGIN( MSG_ONE, SVC_ROOMTYPE, NULL, pentPlayer ); // use the magic #1 for "one client"
WRITE_SHORT( (short)m_flRoomtype ); // sequence number
MESSAGE_END();
// crank up nextthink rate for new active sound entity
// by falling through to think_fast...
}
// player is not closer to the contending sound entity,
// just fall through to think_fast. this effectively
// cranks up the think_rate of entities near the player.
}
// player is in pvs of sound entity, but either not visible or
// not in range. do nothing, fall through to think_fast...
env_sound_Think_fast:
pev->nextthink = gpGlobals->time + 0.25f;
9 years ago
return;
env_sound_Think_slow:
pev->nextthink = gpGlobals->time + 0.75f;
9 years ago
return;
}
//
// env_sound - spawn a sound entity that will set player roomtype
// when player moves in range and sight.
//
//
8 years ago
void CEnvSound::Spawn()
9 years ago
{
// spread think times
pev->nextthink = gpGlobals->time + RANDOM_FLOAT( 0.0f, 0.5f );
9 years ago
}
// ==================== SENTENCE GROUPS, UTILITY FUNCTIONS ======================================
#define CSENTENCE_LRU_MAX 32 // max number of elements per sentence group
// group of related sentences
typedef struct sentenceg
{
char szgroupname[CBSENTENCENAME_MAX];
int count;
unsigned char rgblru[CSENTENCE_LRU_MAX];
} SENTENCEG;
#define CSENTENCEG_MAX 200 // max number of sentence groups
// globals
SENTENCEG rgsentenceg[CSENTENCEG_MAX];
int fSentencesInit = FALSE;
char gszallsentencenames[CVOXFILESENTENCEMAX][CBSENTENCENAME_MAX];
int gcallsentences = 0;
// randomize list of sentence name indices
8 years ago
void USENTENCEG_InitLRU( unsigned char *plru, int count )
9 years ago
{
int i, j, k;
unsigned char temp;
8 years ago
if( !fSentencesInit )
9 years ago
return;
8 years ago
if( count > CSENTENCE_LRU_MAX )
9 years ago
count = CSENTENCE_LRU_MAX;
8 years ago
for( i = 0; i < count; i++ )
plru[i] = (unsigned char)i;
9 years ago
// randomize array
8 years ago
for( i = 0; i < ( count * 4 ); i++ )
9 years ago
{
8 years ago
j = RANDOM_LONG( 0, count - 1 );
k = RANDOM_LONG( 0, count -1 );
9 years ago
temp = plru[j];
plru[j] = plru[k];
plru[k] = temp;
}
}
// ignore lru. pick next sentence from sentence group. Go in order until we hit the last sentence,
// then repeat list if freset is true. If freset is false, then repeat last sentence.
// ipick is passed in as the requested sentence ordinal.
// ipick 'next' is returned.
// return of -1 indicates an error.
8 years ago
int USENTENCEG_PickSequential( int isentenceg, char *szfound, int ipick, int freset )
9 years ago
{
const char *szgroupname;
9 years ago
unsigned char count;
8 years ago
if( !fSentencesInit )
9 years ago
return -1;
8 years ago
if( isentenceg < 0 )
9 years ago
return -1;
szgroupname = rgsentenceg[isentenceg].szgroupname;
count = rgsentenceg[isentenceg].count;
8 years ago
if( count == 0 )
9 years ago
return -1;
8 years ago
if( ipick >= count )
ipick = count - 1;
9 years ago
sprintf( szfound, "!%s%d", szgroupname, ipick );
8 years ago
if( ipick >= count )
9 years ago
{
8 years ago
if( freset )
9 years ago
// reset at end of list
return 0;
else
return count;
}
return ipick + 1;
}
// pick a random sentence from rootname0 to rootnameX.
// picks from the rgsentenceg[isentenceg] least
// recently used, modifies lru array. returns the sentencename.
// note, lru must be seeded with 0-n randomized sentence numbers, with the
// rest of the lru filled with -1. The first integer in the lru is
// actually the size of the list. Returns ipick, the ordinal
// of the picked sentence within the group.
8 years ago
int USENTENCEG_Pick( int isentenceg, char *szfound )
9 years ago
{
const char *szgroupname;
9 years ago
unsigned char *plru;
unsigned char i;
unsigned char count;
unsigned char ipick;
int ffound = FALSE;
8 years ago
if( !fSentencesInit )
9 years ago
return -1;
8 years ago
if( isentenceg < 0 )
9 years ago
return -1;
szgroupname = rgsentenceg[isentenceg].szgroupname;
count = rgsentenceg[isentenceg].count;
plru = rgsentenceg[isentenceg].rgblru;
8 years ago
while( !ffound )
9 years ago
{
8 years ago
for(i = 0; i < count; i++ )
if( plru[i] != 0xFF )
9 years ago
{
ipick = plru[i];
plru[i] = 0xFF;
ffound = TRUE;
break;
}
8 years ago
if( !ffound )
USENTENCEG_InitLRU( plru, count );
9 years ago
else
{
sprintf( szfound, "!%s%d", szgroupname, ipick );
9 years ago
return ipick;
}
}
return -1;
}
// ===================== SENTENCE GROUPS, MAIN ROUTINES ========================
// Given sentence group rootname (name without number suffix),
// get sentence group index (isentenceg). Returns -1 if no such name.
8 years ago
int SENTENCEG_GetIndex( const char *szgroupname )
9 years ago
{
int i;
8 years ago
if( !fSentencesInit || !szgroupname )
9 years ago
return -1;
// search rgsentenceg for match on szgroupname
i = 0;
8 years ago
while( rgsentenceg[i].count )
9 years ago
{
8 years ago
if( !strcmp( szgroupname, rgsentenceg[i].szgroupname ) )
9 years ago
return i;
i++;
}
return -1;
}
// given sentence group index, play random sentence for given entity.
// returns ipick - which sentence was picked to
// play from the group. Ipick is only needed if you plan on stopping
// the sound before playback is done (see SENTENCEG_Stop).
8 years ago
int SENTENCEG_PlayRndI( edict_t *entity, int isentenceg, float volume, float attenuation, int flags, int pitch )
9 years ago
{
char name[64];
int ipick;
8 years ago
if( !fSentencesInit )
9 years ago
return -1;
name[0] = 0;
8 years ago
ipick = USENTENCEG_Pick( isentenceg, name );
if( ipick > 0 )
8 years ago
EMIT_SOUND_DYN( entity, CHAN_VOICE, name, volume, attenuation, flags, pitch );
9 years ago
return ipick;
}
// same as above, but takes sentence group name instead of index
8 years ago
int SENTENCEG_PlayRndSz( edict_t *entity, const char *szgroupname, float volume, float attenuation, int flags, int pitch )
9 years ago
{
char name[64];
int ipick;
int isentenceg;
8 years ago
if( !fSentencesInit )
9 years ago
return -1;
name[0] = 0;
8 years ago
isentenceg = SENTENCEG_GetIndex( szgroupname );
if( isentenceg < 0 )
9 years ago
{
ALERT( at_console, "No such sentence group %s\n", szgroupname );
return -1;
}
8 years ago
ipick = USENTENCEG_Pick( isentenceg, name );
if( ipick >= 0 && name[0] )
EMIT_SOUND_DYN( entity, CHAN_VOICE, name, volume, attenuation, flags, pitch );
9 years ago
return ipick;
}
// play sentences in sequential order from sentence group. Reset after last sentence.
8 years ago
int SENTENCEG_PlaySequentialSz( edict_t *entity, const char *szgroupname, float volume, float attenuation, int flags, int pitch, int ipick, int freset )
9 years ago
{
char name[64];
int ipicknext;
int isentenceg;
8 years ago
if( !fSentencesInit )
9 years ago
return -1;
name[0] = 0;
isentenceg = SENTENCEG_GetIndex(szgroupname);
8 years ago
if( isentenceg < 0 )
9 years ago
return -1;
8 years ago
ipicknext = USENTENCEG_PickSequential(isentenceg, name, ipick, freset );
if( ipicknext >= 0 && name[0] )
EMIT_SOUND_DYN( entity, CHAN_VOICE, name, volume, attenuation, flags, pitch );
9 years ago
return ipicknext;
}
// for this entity, for the given sentence within the sentence group, stop
// the sentence.
8 years ago
void SENTENCEG_Stop( edict_t *entity, int isentenceg, int ipick )
9 years ago
{
char buffer[64];
8 years ago
if( !fSentencesInit )
9 years ago
return;
8 years ago
if( isentenceg < 0 || ipick < 0 )
9 years ago
return;
sprintf( buffer, "!%s%d", rgsentenceg[isentenceg].szgroupname, ipick );
9 years ago
8 years ago
STOP_SOUND( entity, CHAN_VOICE, buffer );
9 years ago
}
// open sentences.txt, scan for groups, build rgsentenceg
// Should be called from world spawn, only works on the
// first call and is ignored subsequently.
void SENTENCEG_Init()
{
char buffer[512];
char szgroup[64];
int i, j;
int isentencegs;
8 years ago
if( fSentencesInit )
9 years ago
return;
8 years ago
memset( gszallsentencenames, 0, CVOXFILESENTENCEMAX * CBSENTENCENAME_MAX );
9 years ago
gcallsentences = 0;
8 years ago
memset( rgsentenceg, 0, CSENTENCEG_MAX * sizeof(SENTENCEG) );
9 years ago
isentencegs = -1;
int filePos = 0, fileSize;
byte *pMemFile = g_engfuncs.pfnLoadFileForMe( "sound/sentences.txt", &fileSize );
8 years ago
if( !pMemFile )
9 years ago
return;
memset( buffer, 0, 512 );
8 years ago
memset( szgroup, 0, 64 );
9 years ago
// for each line in the file...
8 years ago
while( memfgets( pMemFile, fileSize, filePos, buffer, 511 ) != NULL )
9 years ago
{
// skip whitespace
i = 0;
8 years ago
while( buffer[i] && buffer[i] == ' ' )
9 years ago
i++;
8 years ago
if( !buffer[i] )
9 years ago
continue;
8 years ago
if( buffer[i] == '/' || !isalpha( buffer[i] ) )
9 years ago
continue;
// get sentence name
j = i;
8 years ago
while( buffer[j] && buffer[j] != ' ' )
9 years ago
j++;
8 years ago
if( !buffer[j] )
9 years ago
continue;
if( gcallsentences >= CVOXFILESENTENCEMAX )
9 years ago
{
ALERT( at_error, "Too many sentences in sentences.txt! >%d\n", gcallsentences );
9 years ago
break;
}
// null-terminate name and save in sentences array
buffer[j] = 0;
const char *pString = buffer + i;
8 years ago
if( strlen( pString ) >= CBSENTENCENAME_MAX )
ALERT( at_warning, "Sentence %s longer than %d letters\n", pString, CBSENTENCENAME_MAX - 1 );
9 years ago
strcpy( gszallsentencenames[gcallsentences++], pString );
j--;
8 years ago
if( j <= i )
9 years ago
continue;
8 years ago
if( !isdigit( buffer[j] ) )
9 years ago
continue;
// cut out suffix numbers
8 years ago
while( j > i && isdigit( buffer[j] ) )
9 years ago
j--;
8 years ago
if( j <= i )
9 years ago
continue;
8 years ago
buffer[j + 1] = 0;
9 years ago
// if new name doesn't match previous group name,
// make a new group.
8 years ago
if( strcmp( szgroup, &( buffer[i] ) ) )
9 years ago
{
// name doesn't match with prev name,
// copy name into group, init count to 1
isentencegs++;
8 years ago
if( isentencegs >= CSENTENCEG_MAX )
9 years ago
{
8 years ago
ALERT( at_error, "Too many sentence groups in sentences.txt!\n" );
9 years ago
break;
}
8 years ago
strcpy( rgsentenceg[isentencegs].szgroupname, &( buffer[i] ) );
9 years ago
rgsentenceg[isentencegs].count = 1;
8 years ago
strcpy( szgroup, &( buffer[i] ) );
9 years ago
continue;
}
else
{
//name matches with previous, increment group count
8 years ago
if( isentencegs >= 0 )
9 years ago
rgsentenceg[isentencegs].count++;
}
}
g_engfuncs.pfnFreeFile( pMemFile );
9 years ago
fSentencesInit = TRUE;
// init lru lists
i = 0;
8 years ago
while( rgsentenceg[i].count && i < CSENTENCEG_MAX )
9 years ago
{
8 years ago
USENTENCEG_InitLRU( &( rgsentenceg[i].rgblru[0] ), rgsentenceg[i].count );
9 years ago
i++;
}
}
// convert sentence (sample) name to !sentencenum, return !sentencenum
8 years ago
int SENTENCEG_Lookup( const char *sample, char *sentencenum )
9 years ago
{
int i;
9 years ago
// this is a sentence name; lookup sentence number
// and give to engine as string.
8 years ago
for( i = 0; i < gcallsentences; i++ )
if( !stricmp( gszallsentencenames[i], sample + 1 ) )
9 years ago
{
8 years ago
if( sentencenum )
9 years ago
{
sprintf(sentencenum, "!%d", i);
9 years ago
}
return i;
}
// sentence name not found!
return -1;
}
8 years ago
void EMIT_SOUND_DYN( edict_t *entity, int channel, const char *sample, float volume, float attenuation, int flags, int pitch )
9 years ago
{
8 years ago
if( sample && *sample == '!' )
9 years ago
{
char name[32];
8 years ago
if( SENTENCEG_Lookup( sample, name ) >= 0 )
EMIT_SOUND_DYN2( entity, channel, name, volume, attenuation, flags, pitch );
9 years ago
else
ALERT( at_aiconsole, "Unable to find %s in sentences.txt\n", sample );
}
else
8 years ago
EMIT_SOUND_DYN2( entity, channel, sample, volume, attenuation, flags, pitch );
9 years ago
}
// play a specific sentence over the HEV suit speaker - just pass player entity, and !sentencename
8 years ago
void EMIT_SOUND_SUIT( edict_t *entity, const char *sample )
9 years ago
{
float fvol;
int pitch = PITCH_NORM;
8 years ago
fvol = CVAR_GET_FLOAT( "suitvolume" );
if( RANDOM_LONG( 0, 1 ) )
pitch = RANDOM_LONG( 0, 6 ) + 98;
9 years ago
if( fvol > 0.05f )
8 years ago
EMIT_SOUND_DYN( entity, CHAN_STATIC, sample, fvol, ATTN_NORM, 0, pitch );
9 years ago
}
// play a sentence, randomly selected from the passed in group id, over the HEV suit speaker
8 years ago
void EMIT_GROUPID_SUIT( edict_t *entity, int isentenceg )
9 years ago
{
float fvol;
int pitch = PITCH_NORM;
8 years ago
fvol = CVAR_GET_FLOAT( "suitvolume" );
if( RANDOM_LONG( 0, 1 ) )
pitch = RANDOM_LONG( 0, 6 ) + 98;
9 years ago
if( fvol > 0.05f )
8 years ago
SENTENCEG_PlayRndI( entity, isentenceg, fvol, ATTN_NORM, 0, pitch );
9 years ago
}
// play a sentence, randomly selected from the passed in groupname
8 years ago
void EMIT_GROUPNAME_SUIT( edict_t *entity, const char *groupname )
9 years ago
{
float fvol;
int pitch = PITCH_NORM;
8 years ago
fvol = CVAR_GET_FLOAT( "suitvolume" );
if( RANDOM_LONG( 0, 1 ) )
pitch = RANDOM_LONG( 0, 6 ) + 98;
9 years ago
if( fvol > 0.05f )
8 years ago
SENTENCEG_PlayRndSz( entity, groupname, fvol, ATTN_NORM, 0, pitch );
9 years ago
}
// ===================== MATERIAL TYPE DETECTION, MAIN ROUTINES ========================
//
// Used to detect the texture the player is standing on, map the
// texture name to a material type. Play footstep sound based
// on material type.
// open materials.txt, get size, alloc space,
// save in array. Only works first time called,
// ignored on subsequent calls.
static char *memfgets( byte *pMemFile, int fileSize, int &filePos, char *pBuffer, int bufferSize )
{
// Bullet-proofing
8 years ago
if( !pMemFile || !pBuffer )
9 years ago
return NULL;
8 years ago
if( filePos >= fileSize )
9 years ago
return NULL;
int i = filePos;
int last = fileSize;
// fgets always NULL terminates, so only read bufferSize-1 characters
8 years ago
if( last - filePos > ( bufferSize - 1 ) )
last = filePos + ( bufferSize - 1 );
9 years ago
int stop = 0;
// Stop at the next newline (inclusive) or end of buffer
8 years ago
while( i < last && !stop )
9 years ago
{
8 years ago
if( pMemFile[i] == '\n' )
9 years ago
stop = 1;
i++;
}
// If we actually advanced the pointer, copy it over
8 years ago
if( i != filePos )
9 years ago
{
// We read in size bytes
int size = i - filePos;
// copy it out
8 years ago
memcpy( pBuffer, pMemFile + filePos, sizeof(byte) * size );
9 years ago
// If the buffer isn't full, terminate (this is always true)
8 years ago
if( size < bufferSize )
9 years ago
pBuffer[size] = 0;
// Update file pointer
filePos = i;
return pBuffer;
}
// No data read, bail
return NULL;
}
// given texture name, find texture type
// if not found, return type 'concrete'
// NOTE: this routine should ONLY be called if the
// current texture under the player changes!
extern "C" char PM_FindTextureType( char *name );
8 years ago
char TEXTURETYPE_Find( char *name )
9 years ago
{
return PM_FindTextureType(name);
9 years ago
}
// play a strike sound based on the texture that was hit by the attack traceline. VecSrc/VecEnd are the
// original traceline endpoints used by the attacker, iBulletType is the type of bullet that hit the texture.
// returns volume of strike instrument (crowbar) to play
8 years ago
float TEXTURETYPE_PlaySound( TraceResult *ptr, Vector vecSrc, Vector vecEnd, int iBulletType )
9 years ago
{
// hit the world, try to play sound based on texture material type
9 years ago
char chTextureType;
float fvol;
float fvolbar;
char szbuffer[64];
const char *pTextureName;
float rgfl1[3];
float rgfl2[3];
const char *rgsz[4];
9 years ago
int cnt;
float fattn = ATTN_NORM;
short sModelIndex = -1;
byte flags = 0;
9 years ago
8 years ago
if( !g_pGameRules->PlayTextureSounds() )
return 0.0f;
9 years ago
8 years ago
CBaseEntity *pEntity = CBaseEntity::Instance( ptr->pHit );
9 years ago
chTextureType = 0;
8 years ago
if( pEntity && pEntity->Classify() != CLASS_NONE && pEntity->Classify() != CLASS_MACHINE )
9 years ago
// hit body
chTextureType = CHAR_TEX_FLESH;
else
{
// hit world
// find texture under strike, get material type
// copy trace vector into array for trace_texture
8 years ago
vecSrc.CopyToArray( rgfl1 );
vecEnd.CopyToArray( rgfl2 );
9 years ago
// get texture from entity or world (world is ent(0))
8 years ago
if( pEntity )
pTextureName = TRACE_TEXTURE( ENT( pEntity->pev ), rgfl1, rgfl2 );
9 years ago
else
8 years ago
pTextureName = TRACE_TEXTURE( ENT( 0 ), rgfl1, rgfl2 );
8 years ago
if( pTextureName )
9 years ago
{
// strip leading '-0' or '+0~' or '{' or '!'
8 years ago
if( *pTextureName == '-' || *pTextureName == '+' )
9 years ago
pTextureName += 2;
8 years ago
if( *pTextureName == '{' || *pTextureName == '!' || *pTextureName == '~' || *pTextureName == ' ' )
9 years ago
pTextureName++;
// '}}'
8 years ago
strcpy( szbuffer, pTextureName );
9 years ago
szbuffer[CBTEXTURENAMEMAX - 1] = 0;
8 years ago
// ALERT( at_console, "texture hit: %s\n", szbuffer );
9 years ago
// get texture type
8 years ago
chTextureType = TEXTURETYPE_Find( szbuffer );
9 years ago
}
}
8 years ago
switch( chTextureType )
9 years ago
{
default:
8 years ago
case CHAR_TEX_CONCRETE:
case CHAR_TEX_CONCRETE2:
fvol = 0.9f;
fvolbar = 0.6f;
9 years ago
rgsz[0] = "player/pl_step1.wav";
rgsz[1] = "player/pl_step2.wav";
cnt = 2;
if( chTextureType == CHAR_TEX_CONCRETE2 )
sModelIndex = g_sModelIndexConcreteGibs;
9 years ago
break;
8 years ago
case CHAR_TEX_METAL:
fvol = 0.9f;
fvolbar = 0.3f;
9 years ago
rgsz[0] = "player/pl_metal1.wav";
rgsz[1] = "player/pl_metal2.wav";
cnt = 2;
break;
8 years ago
case CHAR_TEX_DIRT:
case CHAR_TEX_ROCK:
case CHAR_TEX_SMOKE:
fvol = 0.9f;
fvolbar = 0.1f;
9 years ago
rgsz[0] = "player/pl_dirt1.wav";
rgsz[1] = "player/pl_dirt2.wav";
rgsz[2] = "player/pl_dirt3.wav";
cnt = 3;
if( chTextureType == CHAR_TEX_SMOKE && iBulletType != BULLET_PLAYER_CROWBAR )
UTIL_Smoke( ptr->vecEndPos, 25, 12, sModelIndex );
else if( chTextureType == CHAR_TEX_ROCK )
{
sModelIndex = g_sModelIndexRockGibs;
flags = BREAK_CONCRETE;
}
9 years ago
break;
8 years ago
case CHAR_TEX_VENT:
fvol = 0.5f;
fvolbar = 0.3f;
9 years ago
rgsz[0] = "player/pl_duct1.wav";
rgsz[1] = "player/pl_duct1.wav";
cnt = 2;
break;
8 years ago
case CHAR_TEX_GRATE:
fvol = 0.9f;
fvolbar = 0.5f;
9 years ago
rgsz[0] = "player/pl_grate1.wav";
rgsz[1] = "player/pl_grate4.wav";
cnt = 2;
break;
8 years ago
case CHAR_TEX_TILE:
fvol = 0.8f;
fvolbar = 0.2f;
9 years ago
rgsz[0] = "player/pl_tile1.wav";
rgsz[1] = "player/pl_tile3.wav";
rgsz[2] = "player/pl_tile2.wav";
rgsz[3] = "player/pl_tile4.wav";
cnt = 4;
break;
case CHAR_TEX_FLUID:
8 years ago
case CHAR_TEX_SLOSH:
fvol = 0.9f;
fvolbar = 0.0f;
9 years ago
rgsz[0] = "player/pl_slosh1.wav";
rgsz[1] = "player/pl_slosh3.wav";
rgsz[2] = "player/pl_slosh2.wav";
rgsz[3] = "player/pl_slosh4.wav";
cnt = 4;
break;
8 years ago
case CHAR_TEX_WOOD:
case CHAR_TEX_WOOD2:
fvol = 0.9f;
fvolbar = 0.2f;
9 years ago
rgsz[0] = "debris/wood1.wav";
rgsz[1] = "debris/wood2.wav";
rgsz[2] = "debris/wood3.wav";
cnt = 3;
if( chTextureType == CHAR_TEX_WOOD2 )
sModelIndex = g_sModelIndexWoodGibs;
9 years ago
break;
case CHAR_TEX_GLASS:
case CHAR_TEX_COMPUTER:
fvol = 0.8f;
fvolbar = 0.2f;
9 years ago
rgsz[0] = "debris/glass1.wav";
rgsz[1] = "debris/glass2.wav";
rgsz[2] = "debris/glass3.wav";
cnt = 3;
break;
case CHAR_TEX_FLESH:
8 years ago
if( iBulletType == BULLET_PLAYER_CROWBAR )
return 0.0f; // crowbar already makes this sound
fvol = 1.0f;
fvolbar = 0.2f;
9 years ago
rgsz[0] = "weapons/bullet_hit1.wav";
rgsz[1] = "weapons/bullet_hit2.wav";
fattn = 1.0f;
9 years ago
cnt = 2;
break;
}
// did we hit a breakable?
8 years ago
if( pEntity && FClassnameIs( pEntity->pev, "func_breakable" ) )
9 years ago
{
// drop volumes, the object will already play a damaged sound
fvol /= 1.5f;
fvolbar /= 2.0f;
9 years ago
}
8 years ago
else if( chTextureType == CHAR_TEX_COMPUTER )
9 years ago
{
// play random spark if computer
if( ptr->flFraction != 1.0f && RANDOM_LONG( 0, 1 ) )
9 years ago
{
UTIL_Sparks( ptr->vecEndPos );
float flVolume = RANDOM_FLOAT( 0.7f, 1.0f );//random volume range
8 years ago
switch( RANDOM_LONG( 0, 1 ) )
9 years ago
{
8 years ago
case 0:
UTIL_EmitAmbientSound( ENT( 0 ), ptr->vecEndPos, "buttons/spark5.wav", flVolume, ATTN_NORM, 0, 100 );
break;
case 1:
UTIL_EmitAmbientSound( ENT( 0 ), ptr->vecEndPos, "buttons/spark6.wav", flVolume, ATTN_NORM, 0, 100 );
break;
/*case 0:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "buttons/spark5.wav", flVolume, ATTN_NORM );
break;
case 1:
EMIT_SOUND( ENT( pev ), CHAN_VOICE, "buttons/spark6.wav", flVolume, ATTN_NORM );
break;*/
9 years ago
}
}
}
if( iBulletType != BULLET_PLAYER_CROWBAR && sModelIndex != -1 )
UTIL_BreakModel( ptr->vecEndPos, Vector( 25, 25, 8 ), gpGlobals->v_forward * -100.0f, 10, sModelIndex, 1, 50, flags );
9 years ago
// play material hit sound
8 years ago
UTIL_EmitAmbientSound( ENT( 0 ), ptr->vecEndPos, rgsz[RANDOM_LONG( 0, cnt - 1 )], fvol, fattn, 0, 96 + RANDOM_LONG( 0, 0xf ) );
//EMIT_SOUND_DYN( ENT( m_pPlayer->pev ), CHAN_WEAPON, rgsz[RANDOM_LONG( 0, cnt - 1 )], fvol, ATTN_NORM, 0, 96 + RANDOM_LONG( 0, 0xf ) );
9 years ago
return fvolbar;
}
// ===================================================================================
//
// Speaker class. Used for announcements per level, for door lock/unlock spoken voice.
//
class CSpeaker : public CBaseEntity
{
public:
8 years ago
void KeyValue( KeyValueData *pkvd );
9 years ago
void Spawn( void );
void Precache( void );
8 years ago
void EXPORT ToggleUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value );
9 years ago
void EXPORT SpeakerThink( void );
8 years ago
virtual int Save( CSave &save );
virtual int Restore( CRestore &restore );
static TYPEDESCRIPTION m_SaveData[];
9 years ago
8 years ago
virtual int ObjectCaps( void ) { return ( CBaseEntity::ObjectCaps() & ~FCAP_ACROSS_TRANSITION ); }
8 years ago
int m_preset; // preset number
9 years ago
};
LINK_ENTITY_TO_CLASS( speaker, CSpeaker )
8 years ago
TYPEDESCRIPTION CSpeaker::m_SaveData[] =
9 years ago
{
DEFINE_FIELD( CSpeaker, m_preset, FIELD_INTEGER ),
};
IMPLEMENT_SAVERESTORE( CSpeaker, CBaseEntity )
9 years ago
//
// ambient_generic - general-purpose user-defined static sound
//
8 years ago
void CSpeaker::Spawn( void )
9 years ago
{
const char *szSoundFile = STRING( pev->message );
9 years ago
if( !m_preset && ( FStringNull( pev->message ) || szSoundFile[0] == '\0' ) )
9 years ago
{
ALERT( at_error, "SPEAKER with no Level/Sentence! at: %f, %f, %f\n", (double)pev->origin.x, (double)pev->origin.y, (double)pev->origin.z );
pev->nextthink = gpGlobals->time + 0.1f;
9 years ago
SetThink( &CBaseEntity::SUB_Remove );
return;
}
8 years ago
pev->solid = SOLID_NOT;
pev->movetype = MOVETYPE_NONE;
9 years ago
8 years ago
SetThink( &CSpeaker::SpeakerThink );
pev->nextthink = 0.0f;
9 years ago
// allow on/off switching via 'use' function.
SetUse( &CSpeaker::ToggleUse );
8 years ago
Precache();
9 years ago
}
#define ANNOUNCE_MINUTES_MIN 0.25f
#define ANNOUNCE_MINUTES_MAX 2.25f
9 years ago
8 years ago
void CSpeaker::Precache( void )
9 years ago
{
8 years ago
if( !FBitSet( pev->spawnflags, SPEAKER_START_SILENT ) )
9 years ago
// set first announcement time for random n second
pev->nextthink = gpGlobals->time + RANDOM_FLOAT( 5.0f, 15.0f );
9 years ago
}
8 years ago
void CSpeaker::SpeakerThink( void )
9 years ago
{
const char* szSoundFile = "";
float flvolume = pev->health * 0.1f;
float flattenuation = 0.3f;
9 years ago
int flags = 0;
int pitch = 100;
// Wait for the talkmonster to finish first.
8 years ago
if( gpGlobals->time <= CTalkMonster::g_talkWaitTime )
9 years ago
{
pev->nextthink = CTalkMonster::g_talkWaitTime + RANDOM_FLOAT( 5.0f, 10.0f );
9 years ago
return;
}
8 years ago
if( m_preset )
9 years ago
{
// go lookup preset text, assign szSoundFile
8 years ago
switch( m_preset )
9 years ago
{
8 years ago
case 1:
szSoundFile = "C1A0_";
break;
case 2:
szSoundFile = "C1A1_";
break;
case 3:
szSoundFile = "C1A2_";
break;
case 4:
szSoundFile = "C1A3_";
break;
case 5:
szSoundFile = "C1A4_";
break;
case 6:
szSoundFile = "C2A1_";
break;
case 7:
szSoundFile = "C2A2_";
break;
case 8:
szSoundFile = "C2A3_";
break;
case 9:
szSoundFile = "C2A4_";
break;
case 10:
szSoundFile = "C2A5_";
break;
case 11:
szSoundFile = "C3A1_";
break;
case 12:
szSoundFile = "C3A2_";
break;
9 years ago
}
}
else
szSoundFile = STRING( pev->message );
8 years ago
if( szSoundFile[0] == '!' )
9 years ago
{
// play single sentence, one shot
8 years ago
UTIL_EmitAmbientSound( ENT( pev ), pev->origin, szSoundFile,
flvolume, flattenuation, flags, pitch );
9 years ago
// shut off and reset
pev->nextthink = 0.0f;
9 years ago
}
else
{
// make random announcement from sentence group
8 years ago
if( SENTENCEG_PlayRndSz( ENT( pev ), szSoundFile, flvolume, flattenuation, flags, pitch ) < 0 )
ALERT( at_console, "Level Design Error!\nSPEAKER has bad sentence group name: %s\n",szSoundFile );
9 years ago
// set next announcement time for random 5 to 10 minute delay
pev->nextthink = gpGlobals->time + RANDOM_FLOAT( ANNOUNCE_MINUTES_MIN * 60.0f, ANNOUNCE_MINUTES_MAX * 60.0f );
9 years ago
CTalkMonster::g_talkWaitTime = gpGlobals->time + 5.0f; // time delay until it's ok to speak: used so that two NPCs don't talk at once
9 years ago
}
return;
}
//
// ToggleUse - if an announcement is pending, cancel it. If no announcement is pending, start one.
//
8 years ago
void CSpeaker::ToggleUse( CBaseEntity *pActivator, CBaseEntity *pCaller, USE_TYPE useType, float value )
9 years ago
{
int fActive = ( pev->nextthink > 0.0f );
9 years ago
// fActive is TRUE only if an announcement is pending
8 years ago
if( useType != USE_TOGGLE )
9 years ago
{
// ignore if we're just turning something on that's already on, or
// turning something off that's already off.
8 years ago
if( ( fActive && useType == USE_ON ) || ( !fActive && useType == USE_OFF ) )
9 years ago
return;
}
8 years ago
if( useType == USE_ON )
9 years ago
{
// turn on announcements
pev->nextthink = gpGlobals->time + 0.1f;
9 years ago
return;
}
8 years ago
if( useType == USE_OFF )
9 years ago
{
// turn off announcements
pev->nextthink = 0.0f;
9 years ago
return;
}
// Toggle announcements
8 years ago
if( fActive )
9 years ago
{
// turn off announcements
pev->nextthink = 0.0f;
9 years ago
}
else
{
// turn on announcements
pev->nextthink = gpGlobals->time + 0.1f;
}
9 years ago
}
// KeyValue - load keyvalue pairs into member data
// NOTE: called BEFORE spawn!
8 years ago
void CSpeaker::KeyValue( KeyValueData *pkvd )
9 years ago
{
// preset
8 years ago
if( FStrEq( pkvd->szKeyName, "preset" ) )
9 years ago
{
8 years ago
m_preset = atoi( pkvd->szValue );
9 years ago
pkvd->fHandled = TRUE;
}
else
CBaseEntity::KeyValue( pkvd );
}