#include maps\_hud_util;

registerWeaponInfo( name, string, type, clip )
{
	level._weaponInfo[ name ][ "type" ] = type;
	level._weaponInfo[ name ][ "clip" ] = clip;
	level._weaponInfo[ name ][ "string" ] = string;

	precacheString( string );
}

isWeaponRegistered( name )
{
	if ( !isDefined( level._weaponInfo[ name ] ) )
		return false;

	return true;
}

getWeaponInfoType( name )
{
	return level._weaponInfo[ name ][ "type" ];
}

getWeaponInfoClip( name )
{
	return level._weaponInfo[ name ][ "clip" ];
}

getWeaponInfoString( name )
{
	return level._weaponInfo[ name ][ "string" ];
}

init()
{
	precacheShader( "hud_bullets_rifle" );
	precacheShader( "hud_bullets_pistol" );
	precacheShader( "hud_bullets_sniper" );
	precacheShader( "hud_bullets_spread" );
	precacheShader( "hud_bullets_support_front" );
	precacheShader( "hud_bullets_support_back" );

	level._bulletAlphas = [];
	level._bulletAlphas[ level._bulletAlphas.size ] = 1.000;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.996;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.949;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.909;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.870;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.835;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.803;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.776;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.749;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.721;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.698;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.674;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.654;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.635;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.615;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.596;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.580;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.564;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.549;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.537;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.521;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.509;
	level._bulletAlphas[ level._bulletAlphas.size ] = 0.498;

	level._weaponInfo = [];
										// 
	registerWeaponInfo( "ak47", 					 &"WEAPON_AK47_FULLAUTO", 			"rifle", 	30 );
								// 
	registerWeaponInfo( "ak47_semi", 			 &"WEAPON_AK47_SEMIAUTO", 			"rifle", 	30 );
							// AK-47
	registerWeaponInfo( "ak47_grenadier", 		 &"WEAPON_AK47", 			"rifle", 	30 );
								// 
	registerWeaponInfo( "ak74u", 				 &"WEAPON_AK74U_FULLAUTO", 		"rifle", 	30 );
							// 
	registerWeaponInfo( "ak74u_semi", 			 &"WEAPON_AK74U_SEMIAUTO", 		"rifle", 	30 );
								// M9
	registerWeaponInfo( "beretta", 				 &"WEAPON_BERETTA", 		"pistol", 	15 );
										// G36C
	registerWeaponInfo( "g36c", 					 &"WEAPON_G36C", 			"rifle", 	30 );
								// M14
	registerWeaponInfo( "m14_scoped", 			 &"WEAPON_M14", 			"sniper", 	10 );
								// 
	registerWeaponInfo( "m16_basic", 			 &"WEAPON_M16A4_FULLAUTO", 			"rifle", 	30 );
							// 
	registerWeaponInfo( "m16_basic_semi", 		 &"WEAPON_M16A4_SEMIAUTO", 			"rifle", 	30 );
							// M16A4
	registerWeaponInfo( "m16_grenadier", 		 &"WEAPON_M16", 			"rifle", 	30 );
										// M203
	registerWeaponInfo( "m203", 					 &"WEAPON_M203", 			"grenade", 	1 );
										// RPG-7
	registerWeaponInfo( "rpg", 					 &"WEAPON_RPG", 			"grenade", 	5 );
										// M249 SAW
	registerWeaponInfo( "saw", 					 &"WEAPON_SAW", 			"support", 	100 );
									// 
	registerWeaponInfo( "m4_grunt", 				 &"WEAPON_M4_FULLAUTO", 			"rifle", 	30 );
							// 
	registerWeaponInfo( "m4_grunt_semi", 		 &"WEAPON_M4_SEMIAUTO", 			"rifle", 	30 );
								// M4A1
	registerWeaponInfo( "m4_grenadier", 			 &"WEAPON_M4", 			"rifle", 	30 );
								// M40A3
	registerWeaponInfo( "m40a3", 				 &"WEAPON_M40A3", 		"sniper", 	10 );
											// MP5
	registerWeaponInfo( "mp5", 					 &"WEAPON_MP5", 			"smg", 		30 );
								// 
	registerWeaponInfo( "mp5_silencer", 			 &"WEAPON_MP5SD", 		"smg", 		30 );
										// USP .45
	registerWeaponInfo( "usp", 					 &"WEAPON_USP", 			"pistol", 	10 );
										// AT4
	registerWeaponInfo( "at4", 					 &"WEAPON_AT4", 			"rocketlauncher", 	1 );
								// Dragunov
	registerWeaponInfo( "dragunov", 				 &"WEAPON_DRAGUNOV", 		"sniper", 	10 );
										// G3
	registerWeaponInfo( "g3", 					 &"WEAPON_G3", 			"rifle", 	30 );
							// W1200
	registerWeaponInfo( "winchester1200", 		 &"WEAPON_WINCHESTER1200", 			"shotgun", 	4 );
										// Mini-Uzi
	registerWeaponInfo( "uzi", 					 &"WEAPON_UZI", 			"smg", 	32 );

	level._player initWeaponHUD();
}

initWeaponHUD()
{
	if ( !isDefined( self.hud_bullets ) )
		self.hud_bullets = [];

	if ( !isDefined( self.hud_bullets[ 0 ] ) )
	{
		self.hud_bullets[ 0 ] = createIcon( undefined, 24, 96 );
		self.hud_bullets[ 0 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
		self.hud_bullets[ 0 ].sort = 10;
	}

	if ( !isDefined( self.hud_bullets[ 1 ] ) )
	{
		self.hud_bullets[ 1 ] = createIcon( undefined, 24, 96 );
		self.hud_bullets[ 1 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
		self.hud_bullets[ 1 ].color = ( 0.7, 0.7, 0.7 );
		self.hud_bullets[ 1 ].sort = 9;
	}

	if ( !isDefined( self.hud_bullets[ 2 ] ) )
	{
		self.hud_bullets[ 2 ] = createIcon( undefined, 24, 96 );
		self.hud_bullets[ 2 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
		self.hud_bullets[ 2 ].sort = 10;
	}

	if ( !isDefined( self.hud_bullets[ 3 ] ) )
	{
		self.hud_bullets[ 3 ] = createIcon( undefined, 24, 96 );
		self.hud_bullets[ 3 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
		self.hud_bullets[ 3 ].color = ( 0.7, 0.7, 0.7 );
		self.hud_bullets[ 3 ].sort = 9;
	}

	if ( !isDefined( self.hud_bullets[ 4 ] ) )
	{
		self.hud_bullets[ 4 ] = createIcon( undefined, 24, 96 );
		self.hud_bullets[ 4 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
		self.hud_bullets[ 4 ].sort = 10;
	}
	/*
	if ( !isDefined( self.hud_totalammo ) )
	{
		self.hud_totalammo = createFontString( "default", 1.5 );
		self.hud_totalammo setParent( self.hud_bullets[0] );
		self.hud_totalammo setPoint( "TOPRIGHT", "BOTTOMRIGHT", 0, 5 );
	}
	
	if ( !isDefined( self.hud_weaponname ) )
	{
		self.hud_weaponname = createFontString( "default", 1.5 );
		self.hud_weaponname setParent( self.hud_totalammo );
		self.hud_weaponname setPoint( "RIGHT", "LEFT", -40, 0 );
	}
	*/
	self thread updateBulletHudThink();
}


updateBulletHudThink()
{
	self endon( "death" );
	lastAmmoCount = -1;
	lastWeapon = "";
	lastWeaponType = "";

	while ( true )
	{
		weapon = self getCurrentWeapon();
		if ( isWeaponRegistered( weapon ) )
		{
			weaponType = self getWeaponInfoType( weapon );
			ammoCount = self GetWeaponAmmoClip( self GetCurrentWeapon() );

			if ( ( weapon != lastWeapon ) && ( weaponType != lastWeaponType ) )
			{
				self setHudWeaponType( weaponType );
				lastWeapon = weapon;
				lastWeaponType = weaponType;
				lastAmmoCount = -1;
			}

			if ( ammoCount != lastAmmoCount )
			{
				self updateHudWeaponAmmo( weapon, ammoCount );
				lastAmmoCount = ammoCount;
			}
			/*			
			self.hud_totalammo setValue( self GetWeaponAmmoStock( self GetCurrentWeapon() ) );
			if ( self getFractionMaxAmmo( weapon ) < 0.25 )
				self.hud_totalammo.color = (1,0,0);
			else
				self.hud_totalammo.color = (1,1,1);
				
			self.hud_weaponname setText( getWeaponInfoString( weapon ) );
			*/
		}

		wait( 0.05 );
	}

}

setHudWeaponType( type )
{
	self.pers[ "weaponType" ] = type;

	if ( !isDefined( self.hud_bullets ) )
		return;

	for ( index = 0; index < self.hud_bullets.size; index++ )
		self.hud_bullets[ index ].alpha = 0;

	switch( type )
	{
		case "pistol":
			self.hud_bullets[ 0 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
			self.hud_bullets[ 0 ] setIconShader( "hud_bullets_pistol" );
			self.hud_bullets[ 0 ].alpha = 1;
		break;
		case "smg":
			self.hud_bullets[ 0 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
			self.hud_bullets[ 0 ] setIconShader( "hud_bullets_rifle" );
			self.hud_bullets[ 1 ] setIconShader( "hud_bullets_rifle" );
			self.hud_bullets[ 1 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -4, -50 );
			self.hud_bullets[ 0 ].alpha = 1;
			self.hud_bullets[ 1 ].alpha = 1;
		break;
		case "rifle":
			self.hud_bullets[ 0 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
			self.hud_bullets[ 0 ] setIconShader( "hud_bullets_rifle" );
			self.hud_bullets[ 1 ] setIconShader( "hud_bullets_rifle" );
			self.hud_bullets[ 1 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -4, -50 );
			self.hud_bullets[ 0 ].alpha = 1;
			self.hud_bullets[ 1 ].alpha = 1;
		break;
		case "sniper":
			self.hud_bullets[ 0 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
			self.hud_bullets[ 0 ] setIconShader( "hud_bullets_sniper" );
			self.hud_bullets[ 0 ].alpha = 1;
		break;
		case "spread":
			self.hud_bullets[ 0 ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6, -47 );
			self.hud_bullets[ 0 ] setIconShader( "hud_bullets_spread" );
			self.hud_bullets[ 0 ].alpha = 1;
		break;
		case "support":
			xOffset = 0;
			for ( index = 0; index < 5; index++ )
			{
				self.hud_bullets[ index ] setPoint( "BOTTOMRIGHT", "BOTTOMRIGHT", -6 + xOffset, -70 );
				self.hud_bullets[ index ].alpha = 1;

				if ( index % 2 )
					self.hud_bullets[ index ] setIconShader( "hud_bullets_support_back" );
				else
					self.hud_bullets[ index ] setIconShader( "hud_bullets_support_front" );

				xOffset -= 14;
			}
		break;
	}
}

getHudWeaponType()
{
	return( self.pers[ "weaponType" ] );
}

updateHudWeaponAmmo( weapon, ammoCount )
{
	if ( !isDefined( self.hud_bullets ) )
		return;

	switch( self getHudWeaponType() )
	{
		case "pistol":
			baseDiff = 15 - getWeaponInfoClip( weapon );
			diff = getWeaponInfoClip( weapon ) - ammoCount;

			self.hud_bullets[ 0 ].alpha = level._bulletAlphas[ baseDiff + diff ];
		break;
		case "rifle":
			diff = getWeaponInfoClip( weapon ) - ammoCount;

			col1Diff = int( diff / 2 );
			col1Diff += ( diff %2 );
			col2Diff = int( diff / 2 );

			self.hud_bullets[ 0 ].alpha = level._bulletAlphas[ col1Diff ];
			self.hud_bullets[ 1 ].alpha = level._bulletAlphas[ col2Diff ];
		break;
		case "smg":
			diff = getWeaponInfoClip( weapon ) - ammoCount;

			col1Diff = int( diff / 2 );
			col1Diff += ( diff %2 );
			col2Diff = int( diff / 2 );

			self.hud_bullets[ 0 ].alpha = level._bulletAlphas[ col1Diff ];
			self.hud_bullets[ 1 ].alpha = level._bulletAlphas[ col2Diff ];
		break;
		case "sniper":
			baseDiff = 15 - getWeaponInfoClip( weapon );
			diff = getWeaponInfoClip( weapon ) - ammoCount;

			self.hud_bullets[ 0 ].alpha = level._bulletAlphas[ baseDiff + diff ];
		break;
		case "spread":
			baseDiff = 15 - getWeaponInfoClip( weapon );
			diff = getWeaponInfoClip( weapon ) - ammoCount;

			self.hud_bullets[ 0 ].alpha = level._bulletAlphas[ baseDiff + diff ];
		break;
		case "support":
			baseDiff = 100 - getWeaponInfoClip( weapon );
			diff = getWeaponInfoClip( weapon ) - ammoCount;
			diff = baseDiff + diff;

			bulletOffset = 20;
			for ( index = 4; index >= 0; index -- )
			{
				if ( diff > bulletOffset )
					self.hud_bullets[ index ].alpha = 0;
				else if ( ( bulletOffset - diff ) <= 20 )
					self.hud_bullets[ index ].alpha = level._bulletAlphas[ diff - ( bulletOffset - 20 ) ];
				else
					self.hud_bullets[ index ].alpha = 1;

				bulletOffset += 20;
			}
		break;
	}
}
