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

#define DMG_BULLET (1<<1)
        
new const nazwa[]   = "Raph";
new const opis[]    = "2skoki,1/5 z noza,Gdy kucnie jest lekko widzialny";
new const bronie    = (1<<CSW_SMOKEGRENADE);
new const zdrowie   = 15;
new const kondycja  = 25;
new const inteligencja = 5;
new const wytrzymalosc = 15;
    
new skoki[33];

new ma_klase[33];

public plugin_init()
{
	register_plugin(nazwa, "1.0", "Z!3L0ny");

	cod_register_class(nazwa, opis, bronie, zdrowie, kondycja, inteligencja, wytrzymalosc);

	register_forward(FM_PlayerPreThink, "fwPrethink_Niewidzialnosc", 1);

   
	register_forward(FM_CmdStart, "fwCmdStart_MultiJump");
        RegisterHam(Ham_TakeDamage, "player", "TakeDamage");
	RegisterHam( Ham_Spawn, "player", "fwSpawn_ModelGracza" );
      RegisterHam(Ham_Spawn, "player", "Odrodzenie", 1);
        RegisterHam(Ham_Spawn, "player", "Spawn", 1);
	register_event("CurWeapon", "ModelBroni_CurWeapon", "be", "1=1");
	register_forward(FM_SetModel, "ModelBroni_fw_SetModel");
}

public plugin_precache()
{

	static szModel[ ] = "models/player/raph/raph.mdl";
	precache_model( szModel );
	precache_model("models/raph/v_kosa.mdl");
}

public cod_class_enabled(id)
{
	give_item(id, "weapon_smokegrenade");
	ma_klase[id] = true;

}

public cod_class_disabled(id)
{
	set_rendering(id,kRenderFxNone, 0, 0, 0, kRenderTransAlpha, 255);
    	ma_klase[id] = false;

}


//Przy kucaniu
public fwPrethink_Niewidzialnosc(id)
{
	if(!ma_klase[id])
		return;

	new button = get_user_button(id);
	if( button & IN_DUCK && get_user_weapon(id) == CSW_KNIFE)
	{
		set_rendering(id,kRenderFxNone, 0, 0, 0, kRenderTransAlpha, 80);
	}
	else
	{
		set_rendering(id,kRenderFxNone, 0, 0, 0, kRenderTransAlpha, 255);
	}
}

public fwCmdStart_MultiJump(id, uc_handle)
{
	if(!is_user_alive(id) || !ma_klase[id])
		return FMRES_IGNORED;

	new flags = pev(id, pev_flags);

	if((get_uc(uc_handle, UC_Buttons) & IN_JUMP) && !(flags & FL_ONGROUND) && !(pev(id, pev_oldbuttons) & IN_JUMP) && skoki[id])
	{
		skoki[id]--;
		new Float:velocity[3];
		pev(id, pev_velocity,velocity);
		velocity[2] = random_float(265.0,285.0);
		set_pev(id, pev_velocity,velocity);
	}
	else if(flags & FL_ONGROUND)
		skoki[id] = 1;

	return FMRES_IGNORED;
}

public fwSpawn_ModelGracza( id )
{
	if( !is_user_alive( id ) || !ma_klase[ id ] )
		return;

	engfunc( EngFunc_SetClientKeyValue, id, engfunc( EngFunc_GetInfoKeyBuffer, id ), "model", "raph" );
}

public ModelBroni_CurWeapon(id)
{
        new weapon = read_data(2);

        if(ma_klase[id])
        {
                if(weapon == CSW_KNIFE)
                {
                        set_pev(id, pev_viewmodel2, "models/raph/v_kosa.mdl")
                        set_pev(id, pev_weaponmodel2, "")
                }
        }
}

public ModelBroni_fw_SetModel(entity, model[])
{
        if(!pev_valid(entity))
                return FMRES_IGNORED

        if(!equali(model, "models/w_knife.mdl"))
                return FMRES_IGNORED;

        new entityowner = pev(entity, pev_owner);

        if(!ma_klase[entityowner])
                return FMRES_IGNORED;

        engfunc(EngFunc_SetModel, entity, "")
        return FMRES_SUPERCEDE
}
public Spawn(id)

{

	    if(ma_klase[id])

	    {

			    strip_user_weapons(id);					  // zabiera wszystkie bronie danej osobie

			    give_item(id, "weapon_knife");			   // daje nóż danej osobie

	    }

}
public TakeDamage(this, idinflictor, idattacker, Float:damage, damagebits)
{
    if(!is_user_connected(idattacker))
        return HAM_IGNORED;
        
    if(!ma_klase[idattacker])
        return HAM_IGNORED;      
        
    if(get_user_team(this) != get_user_team(idattacker) && get_user_weapon(idattacker) == CSW_KNIFE && damagebits & DMG_BULLET && random_num(1, 5) == 1)
        cod_inflict_damage(idattacker, this, float(get_user_health(this))-damage+1.0, 0.0, idinflictor, damagebits);
            
    return HAM_IGNORED;
}
public Odrodzenie(id)
{
	if(ma_klase[id])
	{
		cs_set_user_model(id, "raph");
	}
}