/*
	Adrenaline Boost
	Author: Ben Retan
	Description: A piece of equipment used to give you temporarily increased speed
*/

#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include common_scripts\utility;
#include maps\mp\gametypes\_hostmigration;
#include maps\mp\perks\_perkfunctions;


CONST_mute_device_radius_min = 350;	// Radius at which sounds are fully attenuated
CONST_mute_device_radius_max = 600;	// Radius at which sounds are no longer attenuated
CONST_mute_device_timeout = 20;		// How long the muting effect should last
CONST_mute_device_fade_time = 0.25;	// How long it should take to fade out the muting effect when the device expires

watchMuteBombUsage()
{
	self endon( "spawned_player" );
	self endon( "disconnect" );
	self endon( "death" );
	self endon( "faux_spawn" );
	
	if(!IsDefined(level.adrenalineSettings))
	{
		MuteBombInit();
	}

	while ( 1 )
	{
		self waittill( "grenade_fire", mute_device, weapname );
		if ( weapname == "mute_bomb_mp" )
		{
			if( !IsAlive( self ) )
			{
				mute_device delete();
				return;
			}

			thread tryUseMuteBomb( mute_device );
		}
	}	
}

MuteBombInit()
{	
	self.adrenalineSettings = SpawnStruct();
	
}

tryUseMuteBomb( mute_device ) // self == player
{
	// Force init (this is for test bots)
	if(!IsDefined(self.adrenalineSettings))
	{
		MuteBombInit();
	}

	thread StartMuteBomb( mute_device );
	thread MonitorPlayerDeath( mute_device );
	mute_device thread MonitorMuteBombDeath();

	return true;
}

StartMuteBomb( mute_device )
{
	self endon("ClearMuteBomb");
	self endon("death");
	mute_device endon( "death" );
	
	mute_device PlaySound( "mute_device_activate" );
	wait( 0.75 );

	mute_device AddSoundMuteDevice( CONST_mute_device_radius_min, CONST_mute_device_radius_max, CONST_mute_device_fade_time );
	mute_device PlayLoopSound( "mute_device_active_lp" );
	mute_device thread MonitorMuteBombPlayers();

	wait( CONST_mute_device_timeout );

	mute_device ScaleVolume( 0.0, CONST_mute_device_fade_time );
	mute_device RemoveSoundMuteDevice( CONST_mute_device_fade_time );
	mute_device notify( "ShutdownMuteBomb" );
	
	wait( CONST_mute_device_fade_time );

	mute_device StopLoopSound();
}


MonitorPlayerDeath( mute_device )
{
	mute_device endon( "ShutdownMuteBomb" );

	self waittill( "death" );
	
	if ( IsDefined( mute_device ) )
	{
		mute_device RemoveSoundMuteDevice( CONST_mute_device_fade_time );
		mute_device notify( "ShutdownMuteBomb" );
	}
}


MonitorMuteBombDeath() // self == mute_device
{
	self endon( "ShutdownMuteBomb" );

	self waittill( "death" );
	
	if ( IsDefined( self ) )
	{
		self RemoveSoundMuteDevice( CONST_mute_device_fade_time );
		self notify( "ShutdownMuteBomb" );
	}
}


MonitorMuteBombPlayers() // self == mute_device
{
	self endon( "ShutdownMuteBomb" );

	// Use a radius halfway between the min and max
	radius = CONST_mute_device_radius_min + ( ( CONST_mute_device_radius_max - CONST_mute_device_radius_min ) / 2 );

	self.touchingPlayers = [];

	while ( 1 )
	{
		foreach ( player in level.players )
		{
			dist = Distance( self.origin, player.origin );
			touching = array_contains( self.touchingPlayers, player );
	
			if ( dist <= radius )
			{
				if ( !touching )
				{
					self.touchingPlayers = array_add( self.touchingPlayers, player );
					player PlayLocalSound( "mute_device_active_enter" );
				}
			}
			else
			{
				if ( touching )
				{
					self.touchingPlayers = array_remove( self.touchingPlayers, player );
					player PlayLocalSound( "mute_device_active_exit" );
				}
			}
		}

		wait( 0.05 );
	}
}
