#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <zombieplague>
#include <fun>
#include <zombieplague>

new g_knife, cvar_knife 
new cvar_attack1, cvar_attack2 
new cvar_knife_armor

new bool:g_HasKnife[33]

new const v_model[] = "models/zombie_plague/v_machete.mdl"
new const p_model[] = "models/zombie_plague/p_machete.mdl"
const m_pPlayer = 		41
const m_flNextPrimaryAttack = 	46
const m_flNextSecondaryAttack =	47
const m_flTimeWeaponIdle = 	48

public plugin_init()
{
	register_plugin("[ZP] Extra Item: Machete", "1.0", "Strax")
	g_knife = zp_register_extra_item("Machete", 30, ZP_TEAM_HUMAN)
	
	RegisterHam(Ham_TakeDamage, "player", "fw_TakeDamage")
	RegisterHam(Ham_Spawn, "player", "fwHamPlayerSpawnPost", 1)
	
	register_event("DeathMsg", "Death", "a")
	register_forward(FM_PlayerPreThink, "fw_PlayerPreThink")
	
	cvar_knife = register_cvar ("zp_machete_multi", "6")
	
	RegisterHam(Ham_Weapon_PrimaryAttack, "weapon_knife", "fw_Knife_PrimaryAttack_Post", 1)
	RegisterHam(Ham_Weapon_SecondaryAttack, "weapon_knife", "fw_Knife_SecondaryAttack_Post", 1)
	
	cvar_attack1 = register_cvar("zp_machete_rate1", "0.4")
	cvar_attack2 = register_cvar("zp_machete_rate2", "0.6")
	cvar_knife_armor = register_cvar("zp_machete_armor", "250")
	
}

public zp_extra_item_selected(id, item)
{
	if (item == g_knife)
	{
		g_HasKnife[id] = true
		
		set_user_armor(id, get_pcvar_num(cvar_knife_armor))
		
		client_print(id, print_chat, "[ZP] You have bought Machete!")
	}
}


public plugin_precache()
{
	precache_model(v_model)
	precache_model(p_model)

}

public fw_PlayerPreThink(id)
{

	if (g_HasKnife[id])
	{
		new weapon = get_user_weapon(id)
		
		switch (weapon)
		{
		case CSW_KNIFE:
			{
				set_pev(id, pev_viewmodel2, v_model)
				set_pev(id, pev_weaponmodel2, p_model)
			}
		}
	}
}
		
public client_connect(id)
{
	g_HasKnife[id] = false
}

public client_disconnect(id)
{
	g_HasKnife[id] = false
}

public Death()
{
	g_HasKnife[read_data(2)] = false
}

public fwHamPlayerSpawnPost(id)
{
	g_HasKnife[id] = false
}

public zp_user_infected_post(id)
{
	if (zp_get_user_zombie(id))
	{
	g_HasKnife[id] = false
	}
}

public fw_TakeDamage(victim, inflictor, attacker, Float:damage) 
{ 
	if (victim != attacker) 
{ 
	if(get_user_weapon(attacker) == CSW_KNIFE && g_HasKnife[attacker]) 
	SetHamParamFloat(4, damage * get_pcvar_float(cvar_knife)) 
}

}

public fw_Knife_PrimaryAttack_Post(knife)
{
	static id
	id = get_pdata_cbase(knife, m_pPlayer, 4)
	if(g_HasKnife[id])
	{
		static Float:flRate
		flRate = get_pcvar_float(cvar_attack1)
		
		set_pdata_float(knife, m_flNextPrimaryAttack, flRate, 4)
		set_pdata_float(knife, m_flNextSecondaryAttack, flRate, 4)
		set_pdata_float(knife, m_flTimeWeaponIdle, flRate, 4)		
	}
	return HAM_IGNORED
}

public fw_Knife_SecondaryAttack_Post(knife)
{
	static id
	id = get_pdata_cbase(knife, m_pPlayer, 4)

	if(g_HasKnife[id])
	{
		static Float:flRate
		flRate = get_pcvar_float(cvar_attack2)
		
		set_pdata_float(knife, m_flNextPrimaryAttack, flRate, 4)
		set_pdata_float(knife, m_flNextSecondaryAttack, flRate, 4)
		set_pdata_float(knife, m_flTimeWeaponIdle, flRate, 4)
	}
	return HAM_IGNORED
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1066\\ f0\\ fs16 \n\\ par }
*/
