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

init()
{
	precacheShader( "h1_damage_feedback" );
	precacheShader( "h1_damage_feedback_headshot" );

//	if ( getDvar( "scr_damagefeedback" ) == "" )
//		setDvar( "scr_damagefeedback", "1" );

//	if ( getDvar( "scr_damagefeedback" ) == "0" )
//		return;

	axis = getaiarray( "axis" );
	array_thread( axis, ::monitorDamage );
	add_global_spawn_function( "axis", ::monitorDamage );

	level.player thread init_damage_feedback();
}

init_damage_feedback()
{
	self.hud_damagefeedback = newHudElem( self );
	self.hud_damagefeedback.alignX = "center";
	self.hud_damagefeedback.alignY = "middle";
	self.hud_damagefeedback.horzAlign = "center";
	self.hud_damagefeedback.vertAlign = "middle";
	self.hud_damagefeedback.alpha = 0;
	self.hud_damagefeedback.archived = true;
	self.hud_damagefeedback setShader( "h1_damage_feedback", 50, 50 * 2 );
	self.hud_damagefeedback.y = 25;	// aligns it to the center of the crosshair.

	self.hud_damagefeedback_headshot = newHudElem( self );
	self.hud_damagefeedback_headshot.alignX = "center";
	self.hud_damagefeedback_headshot.alignY = "middle";
	self.hud_damagefeedback_headshot.horzAlign = "center";
	self.hud_damagefeedback_headshot.vertAlign = "middle";
	self.hud_damagefeedback_headshot.alpha = 0;
	self.hud_damagefeedback_headshot.archived = true;
	self.hud_damagefeedback_headshot setShader( "h1_damage_feedback_headshot", 50, 50 * 2 );
	self.hud_damagefeedback_headshot.color = ( 1, 0.2, 0.2 );
	self.hud_damagefeedback_headshot.y = 25;	// aligns it to the center of the crosshair.
}

monitorDamage()
{
	self waittill( "damage", damage, attacker, direction_vec, point, type, modelName, tagName );
	self damagefeedback_took_damage( damage, attacker, direction_vec, point, type, modelName, tagName );
}

damagefeedback_took_damage( damage, attacker, direction_vec, point, type, modelName, tagName )
{
	if ( !isplayer( attacker ) )
		return;
	legal_bullet_types = [];
	legal_bullet_types[ "MOD_PISTOL_BULLET" ] = true;
	legal_bullet_types[ "MOD_RIFLE_BULLET" ] = true;
	if ( isdefined( self.bullet_resistance ) )
	{
		if ( isdefined( legal_bullet_types[ type ] ) )
		{
			if ( damage <= self.bullet_resistance )
				return;
		}
	}
	if ( !isalive( self ) )
	{
		headshot = false;
		if ( ( self.damageLocation == "helmet" || self.damageLocation == "head" ) && isdefined( legal_bullet_types[ type ] ) )
			headshot = true;

		attacker updateDamageFeedback( self, true, headshot );
	}
	else
	{
		attacker updateDamageFeedback( self, false, false );
	}
}

updateDamageFeedback( attacked, kill, headshot )
{
	if ( getDvar( "cg_drawDamageFeedbackOption" ) == "0" )
		return;

	if ( !isPlayer( self ) )
		return;
	if ( !isdefined( attacked.team ) )
		return;
	if ( ( attacked.team == "allies" ) || ( attacked.team == "neutral" ) )
		return;
	
	if ( getDvar( "snd_hitsoundDisabled" ) == "0" )
		self playlocalsound( "SP_hit_alert_npc" );
	
	fadeTime = 1;	//fade out crosshair damage indicator over this time 
	
	if ( kill )
		self.hud_damagefeedback.color = ( 1, 0.2, 0.2 );
	else
		self.hud_damagefeedback.color = ( 1, 1, 1 );
	
	if ( kill && headshot )
		self.hud_damagefeedback_headshot.alpha = 1;
	else
		self.hud_damagefeedback.alpha = 1;

	if ( kill && headshot )
		self.hud_damagefeedback_headshot fadeOverTime( fadeTime );
	else
		self.hud_damagefeedback fadeOverTime( fadeTime );

	if ( kill && headshot )
		self.hud_damagefeedback_headshot.alpha = 0;
	else
		self.hud_damagefeedback.alpha = 0;

	offset = 0 * 240;
	self.hud_damagefeedback.y = 25 - int( offset );
	self.hud_damagefeedback_headshot.y = 25 - int( offset );
}