#include clientscripts\_utility; 
#include clientscripts\_fx;
#include clientscripts\_music;

//csc script by offthewall
//9/11/14: fixed flash for upgraded weapons
//8/26/14: changed how the script handles each gun and its fx

init()
{
	//add_weapon("weapon_string", "flash fx name", "brass fx name", "alternate fx name", "alternate tag"); //undefined disables the fx
	//BO1
	//thread add_weapon("zombie_asp_dw", "pistol_flash_view", "pistol_brass_view");
	//thread add_weapon("zombie_cz75_dw", "pistol_flash_view", "pistol_brass_view");
	//thread add_weapon("zombie_python_dw", "standard_flash_view");
	//thread add_weapon("zombie_makarov_dw", "pistol_flash_view", "pistol_brass_view");
	//thread add_weapon("zombie_hs10_dw", "shotgun_flash_view", "shotgun_brass_view");
	//thread add_weapon("zombie_m1911_dw", "pistol_flash_view", "pistol_brass_view");
	//BO2
	//thread add_weapon("zombie_executioner_dw", "standard_flash_view");
	//thread add_weapon("zombie_kap40_dw", "pistol_flash_view", "pistol_brass_view");
	//MW2
	//thread add_weapon("zombie_g18_dw", "pistol_flash_view", "pistol_brass_view");
	//thread add_weapon("zombie_model_1887_dw", "shotgun_flash_view");
	//thread add_weapon("zombie_ranger_dw", "shotgun_flash_view");
	//thread add_weapon("zombie_mp5k_dw", "pistol_flash_view", "pistol_brass_view");
	//MW3
	//thread add_weapon("zombie_fmg9_dw", "pistol_flash_view", "pistol_brass_view");
	//thread add_weapon("zombie_mp412_dw", "standard_flash_view");
	//thread add_weapon("zombie_p99_dw", "pistol_flash_view", "pistol_brass_view");
	//thread add_weapon("zombie_desert_eagle_dw", "standard_flash_view", "pistol_brass_view");
	//Ghost
	//thread add_weapon("zombie_m9a1_dw", "pistol_flash_view", "pistol_brass_view");
	//thread add_weapon("zombie_magnum_dw", "standard_flash_view");
	
	//add_weapon("winchester_upgraded", "shotgunflash_ice");
	//add_weapon("uzi_upgraded", "standard_flash_view_up");
	//add_weapon("zombie_colt", "standard_flash_view_up", "pistol_brass_view");
	//add_weapon("m249saw", "standard_flash_view", undefined, "standard_flash_view", "tag_flash2");
	//available fx types
	//level._effect["pistol_flash_view"] = loadfx("weapon/muzzleflashes/pistolflash_view");
	//level._effect["pistol_brass_view"] = loadfx("weapon/shellejects/pistol_view");
	//level._effect["pistol_flash_view_up"] = loadfx("weapon/muzzleflashes/pistolflash_ug");
	//level._effect["standard_flash_view"] = loadfx("weapon/muzzleflashes/standardflashview");
	//level._effect["standard_flash_view_up"] = loadfx("weapon/muzzleflashes/standardflashview_ug");
	//level._effect["shotgun_flash_view"] = loadfx("weapon/muzzleflashes/shotgunflash");
	//level._effect["shotgun_brass_view"] = loadfx("weapon/shellejects/shotgun_view");
	//level._effect["shotgun_flash_view_up"] = loadfx("weapon/muzzleflashes/shotgunflash_ug");

	add_weapon("mustang_sally", "pistol_flash", "pistol_brass");

	level._effect["pistol_flash"] = loadfx("weapon/muzzleflashes/pistolflash_view");
	level._effect["pistol_brass"] = loadfx("weapon/shellejects/pistol_view");
}

add_weapon(weapon, flash_name, brass_name, alt_tag_name, alt_tag) //need to thread this for client wait
{
	waitforclient( 0 );
	players = GetLocalPlayers();
	for( i = 0; i < players.size; i++ )
	{
		players[i] thread weapon_fired(i, weapon, flash_name, brass_name, alt_tag_name, alt_tag);
	}
}

weapon_fired(localclientnum, weapon, flash_name, brass_name, alt_tag_name, alt_tag)
{
	current_ammo = undefined;
	previous_ammo = undefined;
	active_weapon = undefined;
	reloading = undefined;
	upgraded = undefined;
	desired_flash = flash_name;
	for( ;; )
	{
		current_weapon = GetCurrentWeapon( localclientnum );
		if(current_weapon == weapon || current_weapon == weapon + "_upgraded" )
		{
			current_ammo = GetWeaponAmmoClip( localclientnum, current_weapon);
			if(!isdefined(previous_ammo))
			{
				//iprintlnbold("getting ammo size");
				active_weapon = current_weapon;
				previous_ammo = GetWeaponAmmoClip( localclientnum, current_weapon);
			}
			if(!isdefined(reloading) )
			{
				if(current_ammo < previous_ammo && previous_ammo - 1 == current_ammo && GetDvar( "cg_thirdperson" ) == "0" ) //weapon fire detect and ammo/flash fix
				//if(current_ammo < previous_ammo && previous_ammo - 1 == current_ammo) //weapon fire detect and ammo fix
				{
					if(current_weapon == weapon + "_upgraded" && !isdefined(upgraded))
					{
						//iprintlnbold("upgraded flash set");
						desired_flash = flash_name + "_up";
						upgraded = true;
					}
					else if(current_weapon != weapon + "_upgraded" && isdefined(upgraded))
					{
						//iprintlnbold("regular flash set");
						desired_flash = flash_name;
						upgraded = undefined;
					}
					if(isdefined(flash_name))
					{
						PlayViewmodelFx( localclientnum, level._effect[desired_flash], "tag_flash1" );
					}
					if(isdefined(brass_name))
					{
						PlayViewmodelFx( localclientnum, level._effect[brass_name], "tag_brass1" );
					}
					if(isdefined(alt_tag_name))
					{
						PlayViewmodelFx( localclientnum, level._effect[alt_tag_name], alt_tag );
					}
					previous_ammo = GetWeaponAmmoClip( localclientnum, current_weapon);
					//iprintlnbold("client weapon fired");
				}
				else if(current_ammo > previous_ammo)
				{
					previous_ammo = GetWeaponAmmoClip( localclientnum, current_weapon);
					//iprintlnbold("client reloaded");
				}
				if (previous_ammo == 0)
				{
					reloading = true;
					//iprintlnbold("client reloading");
				}
			}
			else if (isdefined(reloading) && current_ammo > 0 )
			{
				previous_ammo = GetWeaponAmmoClip( localclientnum, current_weapon);
				reloading = undefined;
				//iprintlnbold("client reloaded");
			}
		}
		if(current_weapon != active_weapon && isdefined(current_ammo))
		{
			//iprintlnbold("weapon switched");
			current_ammo = undefined;
			previous_ammo = undefined;
			reloading = undefined;
		}
	realWait(0.01);
	}
}