#include maps\mp\_utility;
#include common_scripts\utility;

/*
=============
///ScriptDocBegin
"Name: update_exo_battery_hud( <weapon_name> , <additional_endon_string> )"
"Summary: updates the LUI battery HUD."
"Module: Entity"
"CallOn: a player"
"MandatoryArg: <weapon_name>: string - the name of the weapon for which the player is using battery energy."
"MandatoryArg: <weapon_active_flag>: bool - tracks the active state of the weapon. Ends updating "
"Example: player thread update_exo_battery_hud( "exoshield_equipment_mp" );"
"SPMP: MP"
///ScriptDocEnd
=============
*/
update_exo_battery_hud( weapon_name )
{
	level endon( "game_ended" );
	self endon( "death" );
	self endon( "disconnect" );
	self endon( "joined_team" );
	self endon( "faux_spawn" );
	self endon( "kill_battery" );
	
	if ( !IsPlayer(self) )
		return;
	
	while ( self get_exo_ability_hud_omnvar_value( weapon_name, "ui_exo_battery_toggle" ) == 1 )
	{
		battery_energy = self BatteryGetCharge( weapon_name );
		// Setting ui_exo_battery_level to the current energy level.
		self maps\mp\_exo_battery::set_exo_ability_hud_omnvar( weapon_name, "ui_exo_battery_level", battery_energy );
		
		wait( 0.05 );
	}
}


/*
low_exo_battery_energy_sfx( weapon_active_flag )		// self = player
{
	self endon( "disconnect" );
	self endon( "joined_team" );
	self endon( "faux_spawn" );
	self endon( "kill_battery" );
	
	self.is_playing_low_battery_sfx = false;
	
	while ( isReallyAlive( self ) && weapon_active_flag == true )
	{
		battery_energy = self BatteryGetCharge( weapon_name );
		battery_capacity = self BatteryGetSize( weapon_name );
		
		if ( !self.is_playing_low_battery_sfx && battery_energy < battery_capacity / 4 )
		{
			self.is_playing_low_battery_sfx = true;
			
			self thread play_low_energy_sfx();
		}
		
		wait( 0.05 );
	}
	
	// Turning off the low battery SFX.
	self notify( "stop_low_battery_sfx" );
	self.is_playing_low_battery_sfx = false;
}


play_low_energy_sfx()			// self = player
{
	self endon( "death" );
	self endon( "disconnect" );
	self endon( "faux_spawn" );
	self endon( "joined_team" );
	self endon( "stop_low_battery_sfx" );
	
	while ( true )
	{
		self PlayLocalSound( "mp_exo_bat_low" );
		
		wait( 0.6 );
	}
}
*/


/*
=============
///ScriptDocBegin
"Name: set_exo_hud_omnvar( <weapon_name> , <omnvar_name> , <omnvar_value> )"
"Summary: checks whether the player has the given weapon in their tactical or lethal slot and changes the appropriate omnvar."
"Module: Entity"
"CallOn: a player"
"MandatoryArg: <weapon_name>: string - name of the weapon you want to check for."
"MandatoryArg: <omnvar_name>: string - omnvar that you want to set."
"MandatoryArg: <omnvar_value>: value to which you want to set the omnvar."
"Example: player set_exo_hud_omnvar( "exoshield_equipment_mp", "ui_exo_battery_level", 0 );"
"SPMP: MP"
///ScriptDocEnd
=============
*/
set_exo_ability_hud_omnvar( weapon_name, omnvar_name, omnvar_value )
{
	if ( self GetTacticalWeapon() == weapon_name )
	{
		self SetClientOmnvar( omnvar_name + "0", omnvar_value );
		
		if ( omnvar_name == "ui_exo_battery_toggle" )
		{
			if ( omnvar_value == 1 )
			{
				self SetClientOmnvar( "ui_exo_battery_iconA", weapon_name );
			}
		}
	}
	else if ( self GetLethalWeapon() == weapon_name )
	{
		self SetClientOmnvar( omnvar_name + "1", omnvar_value );
		
		if ( omnvar_name == "ui_exo_battery_toggle" )
		{
			if ( omnvar_value == 1 )
			{
				self SetClientOmnvar( "ui_exo_battery_iconB", weapon_name );
			}
		}
	}
	else
	{
		// The player doesn't have an Exo Ability - toggle all battery HUD off.
		self SetClientOmnvar( "ui_exo_battery_iconA", "reset" );
		self SetClientOmnvar( "ui_exo_battery_iconB", "reset" );
		self SetClientOmnvar( "ui_exo_battery_toggle0", 0 );
		self SetClientOmnvar( "ui_exo_battery_toggle1", 0 );
	}
}


/*
=============
///ScriptDocBegin
"Name: get_exo_ability_hud_omnvar_value( <weapon_name> , <omnvar_name//self=player> , <returnsstring> )"
"Summary: returns the appropriate omnvar value for the Exo Ability corresponding to weapon_name. Returns -1 if the user doesn't have an Exo Ability."
"Module: Entity"
"CallOn: a player"
"MandatoryArg: <weapon_name>: must be a valid Exo Ability (see _utility::is_exo_ability_weapon())"
"MandatoryArg: <omnvar_name>: must be a valid omnvar in omnvars.csv (e.g. ui_exo_battery_toggle, ui_exo_battery_level, exo_ability_nrg_req, or exo_ability_nrg_total)."
"Example: omnvar = player get_exo_ability_hud_omnvar_value( weapon_name, "ui_exo_battery_toggle" );"
"SPMP: MP"
///ScriptDocEnd
=============
*/
get_exo_ability_hud_omnvar_value( weapon_name, omnvar_name )
{
	Assert( is_exo_ability_weapon( weapon_name ) );
	
	if ( self GetTacticalWeapon() == weapon_name )
	{
		return self GetClientOmnvar( omnvar_name + "0" );
	}
	else if ( self GetLethalWeapon() == weapon_name )
	{
		return self GetClientOmnvar( omnvar_name + "1" );
	}
	return -1;
}


play_insufficient_tactical_energy_sfx()		// self = player
{
	level endon( "game_ended" );
	self endon( "disconnect" );
	self endon( "death" );
	self endon( "faux_spawn" );
	self endon( "joined_team" );
	self endon( "kill_battery" );
	
	// Removing old notifies if they already exist.
	self NotifyOnPlayerCommandRemove( "tried_left_exo_ability", "+smoke" );
	
	// Need to wait, otherwise the below notification won't be added properly.
	wait( 0.05 );
	
	self NotifyOnPlayerCommand( "tried_left_exo_ability", "+smoke" );
	
	while ( true )
	{
		self waittill( "tried_left_exo_ability" );
		
		weapon_name = self GetTacticalWeapon();
		
		if ( is_exo_ability_weapon( weapon_name ) )
		{
			if ( self BatteryGetCharge( weapon_name ) < BatteryReqToUse( weapon_name ) )
			{
				// The player doesn't have enough energy to use the Exo Ability.
				self PlayLocalSound( "mp_exo_bat_empty" );
			}
		}
	}
}


play_insufficient_lethal_energy_sfx()		// self = player
{
	level endon( "game_ended" );
	self endon( "disconnect" );
	self endon( "death" );
	self endon( "faux_spawn" );
	self endon( "joined_team" );
	self endon( "kill_battery" );
	
	// Removing old notify if it already exist.
	self NotifyOnPlayerCommandRemove( "tried_right_exo_ability", "+frag" );
	
	// Need to wait, otherwise the below notification won't be added properly.
	wait( 0.05 );
	
	self NotifyOnPlayerCommand( "tried_right_exo_ability", "+frag" );
	
	while ( true )
	{
		self waittill( "tried_right_exo_ability" );
		
		weapon_name = self GetLethalWeapon();
		
		if ( is_exo_ability_weapon( weapon_name ) )
		{
			if ( self BatteryGetCharge( weapon_name ) < BatteryReqToUse( weapon_name ) )
			{
				// The player doesn't have enough energy to use the Exo Ability.
				self PlayLocalSound( "mp_exo_bat_empty" );
			}
		}
	}
}
