2009-04-17 01:09:52 +02:00
/*
* ============================================================================
*
2009-07-05 08:49:23 +02:00
* Zombie : Reloaded
2009-04-17 01:09:52 +02:00
*
2009-06-12 05:51:26 +02:00
* File : ambientsounds . inc
* Type : Core
* Description : Plays ambient sounds to clients .
*
* Copyright ( C ) 2009 Greyscale , Richard Helgeby
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2009-04-17 01:09:52 +02:00
*
* ============================================================================
*/
/**
* Global variable that tells if ambient sound cvar data was loaded successfully .
*/
new bool : g_bAmbientSounds ;
/**
* Global variable to store ambient sounds timer handle .
*/
new Handle : tAmbientSounds = INVALID_HANDLE ;
/**
* Array for flagging client to play sound .
*/
new bool : bAmbientSoundsIsPlaying [ MAXPLAYERS + 1 ];
/**
* Load ambient sound data .
*/
AmbientSoundsLoad ()
{
// Validate cvars.
g_bAmbientSounds = AmbientSoundsValidateConfig ();
}
/**
* Client is joining the server .
*
* @ param client The client index .
*/
AmbientSoundsClientInit ( client )
{
// Reset flag to play sound on client.
bAmbientSoundsIsPlaying [ client ] = false ;
}
/**
* Validate ambient sounds cvars .
*/
bool : AmbientSoundsValidateConfig ()
{
// If ambience is disabled, then stop.
2009-04-20 02:56:26 +02:00
new bool : ambience = GetConVarBool ( g_hCvarsList [ CVAR_AMBIENTSOUNDS ]);
2009-04-17 01:09:52 +02:00
if ( ! ambience )
{
2009-11-24 21:51:48 +01:00
g_bAmbientSounds = false ;
2009-04-17 01:09:52 +02:00
return false ;
}
// Get ambient sound file.
decl String : sound [ SOUND_MAX_PATH ];
2009-04-20 02:56:26 +02:00
GetConVarString ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_FILE ], sound , sizeof ( sound ));
2009-04-17 01:09:52 +02:00
Format ( sound , sizeof ( sound ), " sound/%s " , sound );
// If file doesn't exist, then log error and stop.
if ( ! FileExists ( sound , true ))
{
// Log invalid sound file error.
2009-06-08 06:05:50 +02:00
LogEvent ( false , LogType_Error , LOG_CORE_EVENTS , LogModule_SEffects , " Config Validation " , " Invalid sound file specified in \" zr_ambientsounds_file \" : %s " , sound );
2009-04-17 01:09:52 +02:00
return false ;
}
// If volume is muted or invalid, then log error and stop.
2009-04-20 02:56:26 +02:00
new Float : ambientvolume = GetConVarFloat ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_VOLUME ]);
2009-04-17 01:09:52 +02:00
if ( ambientvolume <= 0.0 )
{
// Log invalid ambient sound volume error.
2009-10-27 22:53:03 +01:00
LogEvent ( false , LogType_Error , LOG_CORE_EVENTS , LogModule_SEffects , " Config Validation " , " Ambient sound volume specified in \" zr_ambientsounds_volume \" is either muted or invalid. " );
2009-05-07 03:06:08 +02:00
2009-04-17 01:09:52 +02:00
return false ;
}
// If length is invalid, then log error and stop.
2009-04-20 02:56:26 +02:00
new Float : ambientlength = GetConVarFloat ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_LENGTH ]);
2009-04-17 01:09:52 +02:00
if ( ambientlength <= 0.0 )
{
// Log invalid ambient sound length error.
2009-06-08 06:05:50 +02:00
LogEvent ( false , LogType_Error , LOG_CORE_EVENTS , LogModule_SEffects , " Config Validation " , " Ambient sound length specified in \" zr_ambientsounds_length \" is invalid. " );
2009-05-07 03:06:08 +02:00
2009-04-17 01:09:52 +02:00
return false ;
}
// Add sound file to downloads table.
AddFileToDownloadsTable ( sound );
2009-11-24 21:51:48 +01:00
g_bAmbientSounds = true ;
2009-04-17 01:09:52 +02:00
return true ;
}
/**
* Map is starting .
2009-04-23 06:39:11 +02:00
*/
2009-04-17 01:09:52 +02:00
AmbientSoundsOnMapStart ()
{
// Reset timer handle.
tAmbientSounds = INVALID_HANDLE ;
}
/**
* The round is starting .
*/
AmbientSoundsOnRoundStart ()
{
// Restart ambient sound for all clients.
AmbientSoundsRestart ();
}
2009-05-05 07:40:15 +02:00
/**
* The round is ending .
*/
AmbientSoundsOnRoundEnd ()
{
// x = client index
for ( new x = 1 ; x <= MaxClients ; x ++ )
{
bAmbientSoundsIsPlaying [ x ] = false ;
}
}
2009-04-17 01:09:52 +02:00
/**
* Client is spawning into the game .
*
* @ param client The client index .
*/
2009-06-22 08:51:23 +02:00
AmbientSoundsOnClientSpawnPost ( client )
2009-04-17 01:09:52 +02:00
{
// If ambience is disabled, then stop.
if ( ! g_bAmbientSounds )
{
return ;
}
// If flag is enabled, then stop.
if ( bAmbientSoundsIsPlaying [ client ])
{
return ;
}
// Get ambient sound file.
decl String : sound [ SOUND_MAX_PATH ];
2009-04-20 02:56:26 +02:00
GetConVarString ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_FILE ], sound , sizeof ( sound ));
2009-04-17 01:09:52 +02:00
// Get ambient sound volume.
2009-04-20 02:56:26 +02:00
new Float : ambientvolume = GetConVarFloat ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_VOLUME ]);
2009-04-17 01:09:52 +02:00
// Emit ambient sound.
SEffectsEmitAmbientSound ( sound , ambientvolume , client );
2009-05-05 07:40:15 +02:00
// Flag client that sound is playing.
bAmbientSoundsIsPlaying [ client ] = true ;
2009-04-17 01:09:52 +02:00
}
/**
* Restart ambient sound for all clients .
*/
AmbientSoundsRestart ()
{
// If timer is running, kill it.
if ( tAmbientSounds != INVALID_HANDLE )
{
KillTimer ( tAmbientSounds );
}
// If ambience is disabled, then stop.
if ( ! g_bAmbientSounds )
{
return ;
}
// Get ambient sound length.
2009-04-20 02:56:26 +02:00
new Float : ambientlength = GetConVarFloat ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_LENGTH ]);
2009-04-17 01:09:52 +02:00
// Start ambient sounds timer.
tAmbientSounds = CreateTimer ( ambientlength , AmbientSoundsTimer , _ , TIMER_FLAG_NO_MAPCHANGE );
}
/**
* Timer callback , Replays ambient sound on all clients .
*
* @ param timer The timer handle .
*/
public Action : AmbientSoundsTimer ( Handle : timer )
{
// If ambience is disabled, then stop.
if ( ! g_bAmbientSounds )
{
return ;
}
// Get ambient sound file.
decl String : sound [ SOUND_MAX_PATH ];
2009-04-20 02:56:26 +02:00
GetConVarString ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_FILE ], sound , sizeof ( sound ));
2009-04-17 01:09:52 +02:00
// Get ambient sound volume.
2009-04-20 02:56:26 +02:00
new Float : ambientvolume = GetConVarFloat ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_VOLUME ]);
2009-04-17 01:09:52 +02:00
2009-05-05 07:40:15 +02:00
// Stop sound before playing again.
SEffectsStopAmbientSound ( sound );
2009-04-17 01:09:52 +02:00
// Emit ambient sound.
SEffectsEmitAmbientSound ( sound , ambientvolume );
// Get ambient sound length.
2009-04-20 02:56:26 +02:00
new Float : ambientlength = GetConVarFloat ( g_hCvarsList [ CVAR_AMBIENTSOUNDS_LENGTH ]);
2009-04-17 01:09:52 +02:00
// Start new timer with sound length as delay.
tAmbientSounds = CreateTimer ( ambientlength , AmbientSoundsTimer , _ , TIMER_FLAG_NO_MAPCHANGE );
2009-05-01 11:22:45 +02:00
}