#include <amxmodx>
#include <amxmisc>
#include <colorchat>
#include <cstrike>
#include <engine>
#include <fakemeta>
#include <fun>
#include <codmod>
#include <fakemeta_util>


#define PLUGIN "Bonusowe Paczki"
#define VERSION "1.0"
#define AUTHOR "Play"

native cod_get_user_coins(id) // zwraca ilosc monet
native cod_set_user_coins(id, wartosc) // ustawia ilosc monet

new const modelitem[] = "models/cod_paczka.mdl";

public plugin_init()
{
	register_plugin(PLUGIN, AUTHOR, VERSION)
	
	register_forward(FM_Touch, "fwd_touch")
	
	register_event("DeathMsg", "DeathMsg", "a")
	register_logevent("PoczatekRundy", 2, "1=Round_Start"); 
}
public plugin_precache(){
	precache_model(modelitem);
}

public PoczatekRundy()	
	kill_all_entity("paczka")

public DeathMsg()
{
	new kid = read_data(1)
	new vid = read_data(2)
	
	if( kid == vid )
		return PLUGIN_CONTINUE;
		
	if(is_user_connected(kid) || is_user_connected(vid))
		create_itm(vid, 0)
	
	return PLUGIN_CONTINUE;
}

public UzyjPaczki(id)
{
	if( !is_user_connected(id))
		return PLUGIN_HANDLED;
	
	
	switch(random_num(1, 5))
	{
		case 1:
		{
			new hp = get_user_health(id);
			new losowehp = random_num(-5, -20);
			set_user_health(id, hp+losowehp)
			ColorChat(id, NORMAL, "[^x04 Katujemy.eu ^x01 ] Znalazles trucizne. Tracisz ^x03%i^x01 HP!", losowehp)
		}
		case 2:
		{
			new hp = get_user_health(id);
			new losowehp = random_num(5, 50);
			set_user_health(id, hp+losowehp)
			ColorChat(id, NORMAL, "[^x04 Katujemy.eu ^x01 ] Znalazles apteczke. Dostales ^x03%i^x01 HP!", losowehp)	
		}
		case 3:
		{
			new kasa = cod_get_user_coins(id);
			new losowakasa = random_num(5, 20);
			cod_set_user_coins(id, kasa+losowakasa)
			ColorChat(id, NORMAL, "[^x04 Katujemy.eu ^x01 ] Znalazles sakiewke. Dostales ^x03%i^x01 Monet!", losowakasa)	
		}
		case 4:
		{
			ColorChat(id, NORMAL, "[^x04 Katujemy.eu ^x01 ] Posiadasz juz perk!")
			
			if(cod_get_user_perk(id))
				return PLUGIN_HANDLED
			
			cod_set_user_perk(id, -1, -1, 1);
		}
		case 5:
		{
			new losowyexp = random_num(100, 700);
			cod_set_user_xp(id, cod_get_user_xp(id) + losowyexp);
			ColorChat(id, NORMAL, "[^x04 Katujemy.eu ^x01 ] Znalazles ^x03%i^x01 Expa!", losowyexp)
		}
		case 6:
		{
			new losowypancerz = random_num(10, 200)
			set_user_armor(id, losowypancerz);
			ColorChat(id, NORMAL, "[^x04 Katujemy.eu ^x01 ] Znalazles ^x03%i%^x01 Pancerza!", losowypancerz)
		}
	}
	return PLUGIN_HANDLED;
}


public create_itm(id, id_item){
		
	new Float:origins[3]
	pev(id,pev_origin,origins);
	new entit=create_entity("info_target")
	
	
	origins[0]+=50.0
	origins[2]-=32.0
	
	
	set_pev(entit,pev_origin,origins)
	entity_set_model(entit,modelitem)
	set_pev(entit,pev_classname,"paczka");
	
	dllfunc(DLLFunc_Spawn, entit);
	set_pev(entit,pev_solid,SOLID_BBOX);
	
	engfunc(EngFunc_SetSize,entit,{-1.1, -1.1, -1.1},{1.1, 1.1, 1.1});
	set_pev(entit,pev_movetype,MOVETYPE_FLY);
	engfunc(EngFunc_DropToFloor,entit);
	
	
	set_pev(entit, pev_iuser1, id_item)
	fm_set_rendering(entit, kRenderFxGlowShell, 0, 0, 255, kRenderNormal, 16);

}
public fwd_touch(ent,id)
{       
	if(!is_user_alive(id)) 
		return FMRES_IGNORED;
	
	if(!pev_valid(ent)) 
		return FMRES_IGNORED;
	
	static classname[32];
	pev(ent,pev_classname,classname,31); 
	
	if(!equali(classname,"paczka")) 
		return FMRES_IGNORED;
	
	if(pev(id,pev_button))
	{
		UzyjPaczki(id)
		engfunc(EngFunc_RemoveEntity,ent);
	}
	return FMRES_IGNORED; 
}
public kill_all_entity(classname[]) {
	new iEnt = find_ent_by_class(-1, classname)
	while(iEnt > 0) {
		remove_entity(iEnt)
		iEnt = find_ent_by_class(iEnt, classname)		
	}
}
