#include <amxmodx>
#include <cstrike>
#include <engine>

new g_bwEnt[33]
new cvar_enable

static const PLUGIN_NAME[] 	= "Back Weapons"
static const PLUGIN_AUTHOR[] 	= "Cheap_Suit"
static const PLUGIN_VERSION[]	= "1.1"

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
	register_cvar(PLUGIN_NAME, PLUGIN_VERSION, FCVAR_SPONLY|FCVAR_SERVER)	
	cvar_enable = register_cvar("amx_backweapons", "1")	
}

public plugin_precache()
{
	precache_model("models/bw/bw_ak47.mdl")
	precache_model("models/bw/bw_aug.mdl")
	precache_model("models/bw/bw_awp.mdl")
	precache_model("models/bw/bw_famas.mdl")
	precache_model("models/bw/bw_g3sg1.mdl")
	precache_model("models/bw/bw_galil.mdl")
	precache_model("models/bw/bw_m249.mdl")
	precache_model("models/bw/bw_m3.mdl")
	precache_model("models/bw/bw_m4a1.mdl")
	precache_model("models/bw/bw_mp5navy.mdl")
	precache_model("models/bw/bw_p90.mdl")
	precache_model("models/bw/bw_scout.mdl")
	precache_model("models/bw/bw_sg550.mdl")
	precache_model("models/bw/bw_sg552.mdl")
	precache_model("models/bw/bw_ump45.mdl")
	precache_model("models/bw/bw_xm1014.mdl")
	precache_model("models/pshell.mdl")
}

public client_connect(id)
{
	if(g_bwEnt[id] > 0)
		remove_entity(g_bwEnt[id])
	g_bwEnt[id] = 0
}

public client_disconnect(id)
{
	if(g_bwEnt[id] > 0)
		remove_entity(g_bwEnt[id])
	g_bwEnt[id] = 0
}

public client_PreThink(id)
{
	if(!is_user_connected(id))
		return PLUGIN_CONTINUE
	
	if(!is_user_alive(id) && g_bwEnt[id] > 0)
	{
		remove_entity(g_bwEnt[id])
		g_bwEnt[id] = 0
		
		return PLUGIN_CONTINUE
	}
	
	if(!cs_get_user_hasprim(id) && g_bwEnt[id] > 0)
	{
		remove_entity(g_bwEnt[id])
		g_bwEnt[id] = 0
		
		return PLUGIN_CONTINUE
	}
	
	if(!get_pcvar_num(cvar_enable))
		return PLUGIN_CONTINUE
		
	if(!is_user_alive(id) || !cs_get_user_hasprim(id))
		return PLUGIN_CONTINUE
		
	if(g_bwEnt[id] < 1)
	{
		g_bwEnt[id] = create_entity("info_target")
		if(g_bwEnt[id] > 0)
		{
			entity_set_int(g_bwEnt[id], EV_INT_movetype, MOVETYPE_FOLLOW)
			entity_set_edict(g_bwEnt[id], EV_ENT_aiment, id)
		}
	}

	if(g_bwEnt[id] < 1)
		return PLUGIN_CONTINUE
		
	new temp[2], curWeapon = get_user_weapon(id, temp[0], temp[1])
	switch(curWeapon)
	{
		case CSW_KNIFE, CSW_C4, CSW_HEGRENADE, CSW_SMOKEGRENADE, CSW_FLASHBANG,
		CSW_GLOCK18, CSW_USP, CSW_P228, CSW_DEAGLE, CSW_ELITE, CSW_FIVESEVEN:
		{
			new modelName[33], weaponID = get_weapon_id(id)
			get_model_name(weaponID, modelName, 32)
		
			if(equal(modelName, "models/pshell.mdl"))
			{
				set_entity_visibility(g_bwEnt[id], 0)
				return PLUGIN_CONTINUE
			}
			
			set_entity_visibility(g_bwEnt[id], 1)
			entity_set_model(g_bwEnt[id], modelName)
		}
		default: set_entity_visibility(g_bwEnt[id], 0)
	}
	return PLUGIN_CONTINUE
}

stock get_weapon_id(id)
{
	new weapStr[32], iNum, weaponID
	get_user_weapons(id, weapStr, iNum)
	for(new i = 0; i < iNum; i++)
	{
		switch(weapStr[i])
		{
			case CSW_AK47, CSW_AUG, CSW_AWP, CSW_FAMAS, CSW_G3SG1, CSW_GALIL, CSW_M249,CSW_M3, 
			CSW_M4A1, CSW_MP5NAVY, CSW_P90, CSW_SCOUT, CSW_SG550, CSW_SG552, CSW_UMP45, CSW_XM1014:
			{
				weaponID = weapStr[i]
				break
			}
			default: weaponID = 0
		}
	}
	return weaponID
}

stock get_model_name(weaponID, returnString[], returnLen)
{
	new modelName
	switch(weaponID)
	{
		case CSW_AK47: 		modelName = format(returnString, returnLen, "models/bw/bw_ak47.mdl")
		case CSW_AUG: 		modelName = format(returnString, returnLen, "models/bw/bw_aug.mdl")
		case CSW_AWP: 		modelName = format(returnString, returnLen, "models/bw/bw_awp.mdl")
		case CSW_FAMAS: 	modelName = format(returnString, returnLen, "models/bw/bw_famas.mdl")
		case CSW_G3SG1: 	modelName = format(returnString, returnLen, "models/bw/bw_g3sg1.mdl")
		case CSW_GALIL: 	modelName = format(returnString, returnLen, "models/bw/bw_galil.mdl")
		case CSW_M249: 		modelName = format(returnString, returnLen, "models/bw/bw_m249.mdl")
		case CSW_M3: 		modelName = format(returnString, returnLen, "models/bw/bw_m3.mdl")
		case CSW_M4A1: 		modelName = format(returnString, returnLen, "models/bw/bw_m4a1.mdl")
		case CSW_MP5NAVY: 	modelName = format(returnString, returnLen, "models/bw/bw_mp5navy.mdl")
		case CSW_P90: 		modelName = format(returnString, returnLen, "models/bw/bw_p90.mdl")
		case CSW_SCOUT: 	modelName = format(returnString, returnLen, "models/bw/bw_scout.mdl")
		case CSW_SG550: 	modelName = format(returnString, returnLen, "models/bw/bw_sg550.mdl")
		case CSW_SG552: 	modelName = format(returnString, returnLen, "models/bw/bw_sg552.mdl")
		case CSW_UMP45: 	modelName = format(returnString, returnLen, "models/bw/bw_ump45.mdl")
		case CSW_XM1014: 	modelName = format(returnString, returnLen, "models/bw/bw_xm1014.mdl")
		default:		modelName = format(returnString, returnLen, "models/pshell.mdl")
	}
	return modelName
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1033\\ f0\\ fs16 \n\\ par }
*/
