/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <engine>
#include <cstrike>

#define PLUGIN "Dual katana"
#define VERSION "start"
#define AUTHOR "Barney"
#define m_flNextAttack		83

new g_dkatana[33]


new const dkatama_models[][]={"models/v_katanad.mdl","models/p_katanad.mdl"}

new const oldknife_sounds[][] =
{
	"weapons/knife_deploy1.wav",
	"weapons/knife_hit1.wav",
	"weapons/knife_hit2.wav",
	"weapons/knife_hit3.wav",
	"weapons/knife_hit4.wav",
	"weapons/knife_hitwall1.wav",
	"weapons/knife_slash1.wav",
	"weapons/knife_slash2.wav",
	"weapons/knife_stab.wav"
}

new const knife_sounds[][] =
{
	"weapons/katanad_draw.wav",
	"weapons/katanad_hit1.wav",
	"weapons/katanad_hit2.wav",
	"weapons/katanad_hit2.wav",
	"weapons/katanad_hit1.wav",
	"weapons/knife_hitwall1.wav",
	"weapons/katanad_slash1.wav", 
	"weapons/katanad_slash2.wav",
	"weapons/katanad_stab.wav"
}


public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_clcmd("say /dkatana","dkatana_buy")
	register_event("CurWeapon","CurrentWeapon","be","1=1")
	RegisterHam(Ham_Weapon_PrimaryAttack,"weapon_knife","fw_primaryattack",1)
	RegisterHam(Ham_Weapon_SecondaryAttack,"weapon_knife","fw_secondaryattack",1)
	RegisterHam(Ham_TakeDamage,"player","fw_takedmg")
	register_forward(FM_EmitSound, "fw_emitsound")
}

public CurrentWeapon(id){
	if(g_dkatana[id]){
	if(get_user_weapon(id)!=CSW_KNIFE && task_exists(id+1221))
	remove_task(id+1221)
	if(get_user_weapon(id)==CSW_KNIFE){
		set_pev(id,pev_viewmodel2,dkatama_models[0])
		set_pev(id, pev_weaponmodel2, dkatama_models[1])
	}
	}
}

public fw_emitsound(id, channel, const sound[])
{
	if(!is_user_alive(id)|| get_user_weapon(id) != CSW_KNIFE || !g_dkatana[id])
		return FMRES_IGNORED

	for(new i = 0; i < sizeof oldknife_sounds; i++)
	{
		if(equal(sound, oldknife_sounds[i]))
		{
			emit_sound(id, channel, knife_sounds[i], VOL_NORM, ATTN_NORM, 0, PITCH_NORM)

			return FMRES_SUPERCEDE
		}
	}
			
	return FMRES_IGNORED
}

public dkatana_buy(id){
	g_dkatana[id]=1
}

public fw_primaryattack(wpn){
	new id=pev(wpn,pev_owner)
	if(g_dkatana[id]){
	UTIL_PlayWeaponAnimation(id,7)
	set_task(0.2,"nextprattack",id+1221)
	set_pdata_float(id,m_flNextAttack,1.0)
	return HAM_SUPERCEDE
	}
	return PLUGIN_CONTINUE
}

public fw_secondaryattack(wpn){
	new id=pev(wpn,pev_owner)
	if(g_dkatana[id]){
	UTIL_PlayWeaponAnimation(id,5)
	set_task(0.4,"nextscattack",id+1221)
	set_pdata_float(id,m_flNextAttack,1.5)
	return HAM_SUPERCEDE
	}
	return PLUGIN_CONTINUE
}

public fw_takedmg(vic, inflictor, id, Float:damage){
	if(g_dkatana[id]){
	SetHamParamFloat(4, damage * 3.0)
	}
	if(g_dkatana[vic])
	return HAM_SUPERCEDE
	return PLUGIN_CONTINUE
}

public nextprattack(id){
	id-=1221
	new weapon = find_ent_by_owner ( -1, "weapon_knife", id )
	ExecuteHam(Ham_Weapon_PrimaryAttack, weapon)
}
	
public nextscattack(id){
	id-=1221
	new weapon = find_ent_by_owner ( -1, "weapon_knife", id )
	ExecuteHam(Ham_Weapon_SecondaryAttack, weapon)
	set_pev(id, pev_weaponanim, 5)
}
	
stock UTIL_PlayWeaponAnimation(const Player, const Sequence)
{
	set_pev(Player, pev_weaponanim, Sequence)
	
	message_begin(MSG_ONE_UNRELIABLE, SVC_WEAPONANIM, .player = Player)
	write_byte(Sequence)
	write_byte(0)
	message_end()
}

public plugin_precache(){
	for(new i;i<sizeof dkatama_models;i++)
	precache_model(dkatama_models[i])
	for(new i;i<sizeof knife_sounds;i++)
	precache_sound(knife_sounds[i])
}
