#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <engine>
#include <fakemeta>
#include <fun>
#include <hamsandwich>
#include <zombieplague>

new const PLUGIN[] = "P90new"
new const VERSION[] = "1.0"
new const AUTHOR[] = "ds811888"

new const P_MODEL[] = "models/p_fnc.mdl"
new const V_MODEL[] = "models/v_fnc.mdl"
new const W_MODEL[] = "models/w_fnc.mdl"

new cvar_enabled, cvar_dmgmultiplier, cvar_cost

new const g_item_name[] = { "Автомат [Гадюка]" };
new g_itemid_p90new;

#define is_valid_player(%1) (1 <= %1 <= 32)

new g_hasgun_p90new[33]
new g_Firegun[33]
new g_canShoot[33]
new Float:g_last_shot_time[33]
new g_restarted

#define TASK_HUDAMMO	    1337
#define ID_HUDAMMO (taskid - TASK_HUDAMMO)

public plugin_precache()
{
	precache_model(P_MODEL)
	precache_model(V_MODEL)
	precache_model(W_MODEL)

	precache_sound("weapons/fnc_draw.wav")
}

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_cvar("wpn_trg_version", VERSION, FCVAR_SERVER)
	
	//register_clcmd("buy_p90new", "CmdBuyIt")
	
	register_event("CurWeapon", "Event_CurrentWeapon", "be", "1=1")
	register_event("HLTV", "Event_NewRound", "a", "1=0", "2=0");
	register_event("TextMsg", "Event_GameRestart", "a", "2=#Game_Commencing", "2=#Game_will_restart_in");
	
	register_concmd("amx_p90new", "cmd_givetrg", ADMIN_BAN, "<name>")

	register_forward(FM_SetModel, "fw_SetModel")
	RegisterHam(Ham_Killed, "player", "fw_PlayerKilled")
	RegisterHam(Ham_TakeDamage, "player", "fw_TakeDamage")
	register_forward(FM_Touch, "fw_Touch")
	register_forward(FM_CmdStart, "fw_CmdStart")
	register_forward(FM_PlayerPostThink, "fw_PlayerPostThink")
	register_forward(FM_UpdateClientData, "fw_UpdateClientData_Post", 1)
	
	cvar_enabled 		= register_cvar("amx_wpn_p90new", 	"1")
	cvar_cost 		= register_cvar("amx_wpn_p90new_cost", "0")
	cvar_dmgmultiplier	= register_cvar("amx_wpn_p90new_dmg", "200")

	g_itemid_p90new = zp_register_extra_item(g_item_name, get_pcvar_num(cvar_cost), ZP_TEAM_HUMAN);
}

public zp_extra_item_selected(player, itemid)
{
	if (itemid == g_itemid_p90new)
	{
		set_task(0.1,"CmdBuyIt",player)
	}
}


public fw_TakeDamage(victim, inflictor, attacker)
{
	if(is_valid_player(attacker) && get_user_weapon(attacker) == CSW_P90 && g_hasgun_p90new[attacker] )
	{
		SetHamParamFloat(4, get_pcvar_float(cvar_dmgmultiplier))
	}
}

public client_connect(id)
{
	g_hasgun_p90new[id] = false
	g_canShoot[id] = false
	g_last_shot_time[id] = 0.0
}

public cmd_givetrg(id,level,cid)
{
	if (!cmd_access(id,level,cid,2))
		return PLUGIN_HANDLED;
	new arg[32];
	read_argv(1,arg,31);
	
	new player = cmd_target(id,arg,7);
	if (!player) 
		return PLUGIN_HANDLED;
	
	new name[32];
	get_user_name(player,name,31);
	
	give_item(player, "weapon_p90")
	cs_set_user_bpammo(id , CSW_P90 , 100)
	g_hasgun_p90new[player] = true
	
	return PLUGIN_HANDLED
}

public CmdBuyIt(id)
{
	new money = zp_get_user_ammo_packs(id)
	new cost = get_pcvar_num(cvar_cost)

	if (get_pcvar_num(cvar_enabled) == 0)
		return PLUGIN_CONTINUE
	
	if (!is_user_alive(id))
		return PLUGIN_CONTINUE
/*		
	if (!cs_get_user_buyzone(id))
	{
		client_print(id, print_center, "")
		return PLUGIN_CONTINUE
	}
*/	
	if (g_hasgun_p90new[id] || user_has_weapon(id, CSW_P90))
	{
		client_print(id, print_center, "")
		return PLUGIN_CONTINUE
	}
/*	
	if (cs_get_user_vip(id))
	{
		client_print(id, print_center, "")
		return PLUGIN_CONTINUE
	}
*/	
	if (money < cost)
	{
		client_print(id, print_chat, "")
		return PLUGIN_CONTINUE
	}
	
	give_item(id, "weapon_p90")
	cs_set_user_bpammo(id , CSW_P90 , 100) 
	zp_set_user_ammo_packs(id, money - cost)
	g_hasgun_p90new[id] = true
	
	return PLUGIN_CONTINUE
}

public Event_CurrentWeapon(id)
{
	new weaponID = read_data(2)
	
	if (weaponID == CSW_P90)
	{
		if (g_hasgun_p90new[id])
		{
			entity_set_string(id, EV_SZ_viewmodel, V_MODEL)
			
			entity_set_string(id, EV_SZ_weaponmodel, P_MODEL)
		}
	}
}

public Event_NewRound(id)
{
	if (g_restarted)
	{
		for (new i = 1; i <= get_maxplayers(); i++)
		{
			g_hasgun_p90new[i] = false
			remove_task(i+TASK_HUDAMMO)
		}
		g_restarted = false
	}
}

public Event_GameRestart()
{
	g_restarted = true
}
				
public fw_SetModel(ent, const model[])
{
	if (!pev_valid(ent))	
		return FMRES_IGNORED
	
	if(!equali(model, "models/w_p90.mdl")) 
		return FMRES_IGNORED
	
	new owner = pev(ent, pev_owner)
	new classname[33]; pev(ent, pev_classname, classname, charsmax(classname))
	
	if(equal(classname, "weaponbox"))
	{
		if(g_hasgun_p90new[owner])
		{
			g_hasgun_p90new[owner] = false
			
			engfunc(EngFunc_SetModel, ent, W_MODEL)
			
			set_task(0.1, "touch_fix", owner)
			
			remove_task(owner+TASK_HUDAMMO)
			
			return FMRES_SUPERCEDE
		}
	}
	
	return FMRES_IGNORED

}

public fw_PlayerKilled(victim, attacker, shouldgib)
{
	if (g_hasgun_p90new[victim])		
	{
		g_hasgun_p90new[victim] = false
		g_canShoot[victim] = false
		g_Firegun[victim] = false
		
		remove_task(victim+TASK_HUDAMMO)
	}
}

public fw_Touch(ent, toucher)
{
	if(!pev_valid(ent) || !pev_valid(toucher))
		return FMRES_IGNORED;
	
	new model[33], toucherclass[33], entityclass[33]
	pev(ent, pev_model, model, charsmax(model))
	pev(ent, pev_classname, entityclass, charsmax(entityclass))
	pev(toucher, pev_classname, toucherclass, charsmax(toucherclass))
	
	if (!equali(toucherclass, "player") || !equali(entityclass, "weaponbox"))
		return FMRES_IGNORED
	
	if(equali(model, W_MODEL))
	{
		if(allowed_toucher(toucher))
		{
			g_hasgun_p90new[toucher] = true
			g_canShoot[toucher] = true
			g_Firegun[toucher] = false
		}
	}
	
	return FMRES_IGNORED;
}

public fw_CmdStart(id, uc_handle, seed) 
{
	if (!is_user_alive(id))
		return FMRES_IGNORED
		
	if (!g_hasgun_p90new[id])
		return FMRES_IGNORED
		
	new ammo, clip, weapon = get_user_weapon(id, clip, ammo)
	
	if (weapon != 5)
		return FMRES_IGNORED
		
	new buttons = get_uc(uc_handle, UC_Buttons)
	
	if(buttons & IN_ATTACK)
	{
		g_Firegun[id] = true
	
		buttons &= ~IN_ATTACK
		set_uc(uc_handle, UC_Buttons, buttons)
	}
	else 
		g_Firegun[id] = false	
		
	return FMRES_HANDLED
}

public fw_PlayerPostThink(id)
{
	if (!is_user_alive(id))
		return FMRES_IGNORED
		
	if (!g_hasgun_p90new[id])
		return FMRES_IGNORED
		
	new ammo, clip, weapon = get_user_weapon(id, clip, ammo)
	
	if (weapon != 5)
		return FMRES_IGNORED

	return FMRES_IGNORED
}

public fw_UpdateClientData_Post(id, sendweapons, cd_handle) 
{
	if (!is_user_alive(id))
		return FMRES_IGNORED
		
	if (!g_hasgun_p90new[id])
		return FMRES_IGNORED
	
	new ammo, clip, weapon = get_user_weapon(id, clip, ammo)
	
	if (weapon != 5)
		return FMRES_IGNORED
		
	set_cd(cd_handle, CD_ID, 0)
	return FMRES_HANDLED
}

public touch_fix(id)
{
	if (g_hasgun_p90new[id])
		g_hasgun_p90new[id] = false
}

stock allowed_toucher(player)
{
	if (g_hasgun_p90new[player])
		return false
	
	return true
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs16 \n\\ par }
*/
