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

#define OFFSET_AMMO_HEGRENADE 388

new const perk_name[] = "Zestaw Semtex"
new const perk_desc[] = "Dostajesz LW granaty HE, ktore kleja sie do wszystkiego (rowniez do graczy)"

new bool: ma_perk[33], wartosc_perku[33];


public plugin_init() 
{
	register_plugin(perk_name, "0.1", "VEN, edit by Hleb")
	cod_register_perk(perk_name, perk_desc, 2, 3)
	register_touch("grenade", "player", "touch_nade")
	register_forward(FM_EmitSound, "forward_emit_sound")
	RegisterHam(Ham_Spawn, "player", "PlayerSpawn", 1);
}
public cod_perk_enabled(id, wartosc)
{
	cod_give_weapon(id, CSW_HEGRENADE);
	wartosc_perku[id] = wartosc;
	ma_perk[id] = true;
	PlayerSpawn(id);
}
public cod_perk_disabled(id)
{
	cod_take_weapon(id, CSW_HEGRENADE);
	ma_perk[id] = false;
	wartosc_perku[id] = 0;
}
public PlayerSpawn(id)
{
	if(!wartosc_perku[id]) return;

	set_pdata_int(id, OFFSET_AMMO_HEGRENADE, wartosc_perku[id], 5);
}
public touch_nade(nade, id) 
{
	if (!is_nade_bounce(nade) || !ma_perk[pev(nade, pev_owner)] || is_nade_excluded(nade))
		return

	set_pev(nade, pev_aiment, id)
	set_pev(nade, pev_movetype, MOVETYPE_FOLLOW)
	set_pev(nade, pev_sequence, 0)
}
public forward_emit_sound(nade, channel, sound[]) 
{
	if(!ma_perk[pev(nade, pev_owner)])
		return FMRES_IGNORED
		
	if (is_nade_bounce(nade)) 
	{
		if (equal(sound, "weapons/he_bounce-1.wav") && !is_nade_excluded(nade)) 
		{
			set_pev(nade, pev_movetype, MOVETYPE_NONE)
			set_pev(nade, pev_sequence, 0)
		}
	}
	return FMRES_IGNORED
}
stock bool:is_nade_bounce(nade) 
{
	return pev(nade, pev_movetype) == MOVETYPE_BOUNCE
}

stock bool:is_nade_excluded(nade) 
{
	new model[32]

	pev(nade, pev_model, model, 31)
	
	if (equal(model, "models/w_hegrenade.mdl"))
		return false
	
	return true

}
