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

#define DMG_BULLET (1<<1)

new const nazwa[]   = "Terminator";
new const opis[]    = "1/25 szansa na zabicie z M249, odporny na HS";
new const bronie    = 1<<CSW_M249;
new const zdrowie   = 0;
new const kondycja  = 0;
new const inteligencja = 0;
new const wytrzymalosc = 0;

new bool:ma_klase[33];


public plugin_init()
{
	register_plugin(nazwa, "1.0", "Play");
	
	cod_register_class(nazwa, opis, bronie, zdrowie, kondycja, inteligencja, wytrzymalosc);
	
	RegisterHam(Ham_TakeDamage, "player", "TakeDamage");
	register_forward(FM_TraceLine, "TraceLine");	

}
public cod_class_enabled(id)
{
	ma_klase[id] = true;
}
public cod_class_disabled(id)
{
	ma_klase[id] = false;
}
public TraceLine(Float:start[3], Float:end[3], conditions, id, trace)
{
	if(get_tr2(trace, TR_iHitgroup) != HIT_HEAD)
		return FMRES_IGNORED;
	  
	new iHit = get_tr2(trace, TR_pHit);
	if(!is_user_connected(iHit))
		return FMRES_IGNORED;
	if(!ma_klase[iHit])
		return FMRES_IGNORED;
	  
	set_tr2(trace, TR_iHitgroup, 8);
	return FMRES_IGNORED;
}
public TakeDamage(this, idinflictor, idattacker, Float:damage, damagebits)
{
	if(!is_user_connected(idattacker))
		return HAM_IGNORED; 
	
	if(!ma_klase[idattacker])
		return HAM_IGNORED;
	
	if(damagebits & DMG_BULLET)
	{
		new weapon = get_user_weapon(idattacker);
		
		if(weapon == CSW_M249 && random_num(1,25) == 1) 
			cod_inflict_damage(idattacker, this, float(get_user_health(this))-damage+1.0, 0.0, idinflictor, damagebits);
		
	}
	
	return HAM_IGNORED;
}
