/* Plugin generated by AMXX-Studio */

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

#include <codmod>

#define PLUGIN "Bomber"
#define VERSION "1.0"
#define AUTHOR "CheQ"

new sprite_blast;
new ilosc_bomb[33];
new bool:ma_klase[33];

new const nazwa[] = "Wsparcie Bombowe";
new const opis[] = "Posiada MP5 i granatnik do niego ktorym mozemy stawiac bomby. (PPM) Detonacja USE";
new const bronie = 1<<CSW_MP5NAVY;
new const zdrowie = 0;
new const kondycja = 10;
new const inteligencja = 0;
new const wytrzymalosc = 0;

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_touch("Granatt", "*" , "DotykRakiety");
	register_event("HLTV", "NowaRunda", "a", "1=0", "2=0");
	RegisterHam(Ham_Spawn, "player", "Odrodzenie", 1);
	
	
	cod_register_class(nazwa, opis, bronie, zdrowie, kondycja, inteligencja, wytrzymalosc);
	
	register_forward(FM_PlayerPreThink, "PlayerPreThink") 
	
	register_cvar("bomb_dmg","55")
	
}
public cod_class_enabled(id)
{
	ma_klase[id] = true;
	ilosc_bomb[id] = 3;
}
public cod_class_disabled(id)
{
	ma_klase[id] = false;
	ilosc_bomb[id] = 0;
}
public NowaRunda()
{
	new iEnt = find_ent_by_class(-1, "GranattZ");
	while(iEnt > 0) 
	{
		remove_entity(iEnt);
		iEnt = find_ent_by_class(iEnt, "GranattZ");	
	}
	iEnt = find_ent_by_class(-1, "Granatt");
	while(iEnt > 0) 
	{
		remove_entity(iEnt);
		iEnt = find_ent_by_class(iEnt, "Granatt");	
	}
}
public PlayerPreThink(id)
{
	if(!is_user_alive(id) || !ma_klase[id])
		return FMRES_IGNORED 
	
	
	
	if((pev(id, pev_button) & IN_ATTACK2 && !(pev(id, pev_oldbuttons) & IN_ATTACK2)) && get_user_weapon(id) == CSW_MP5NAVY)
	{
		Stworz(id)		
	}
	if(pev(id, pev_button) & IN_USE && !(pev(id, pev_oldbuttons) & IN_USE))
	{
		Bum(id)		
	}
	
	return FMRES_IGNORED;
}
public plugin_precache()
{
	precache_model("models/rpgrocket.mdl");
	
	sprite_blast = precache_model("sprites/dexplo.spr");
}
public Odrodzenie(id)
{
	if(!ma_klase[id] || !is_user_alive(id))
		return PLUGIN_CONTINUE;
	
	ilosc_bomb[id] = 3;
	return PLUGIN_CONTINUE;
}
public Stworz(id)
{
	if(!ilosc_bomb[id])
	{
		client_print(id,print_center,"Nie masz juz bomb!")
		return PLUGIN_CONTINUE;
	}
	
	new Float: Origin[3], Float: vAngle[3], Float: Velocity[3];
	
	entity_get_vector(id, EV_VEC_v_angle, vAngle);
	entity_get_vector(id, EV_VEC_origin , Origin);
	
	new Ent = create_entity("info_target");
	
	entity_set_string(Ent, EV_SZ_classname, "Granatt");
	entity_set_model(Ent, "models/rpgrocket.mdl");
	
	vAngle[0] *= -1.0;
	
	entity_set_origin(Ent, Origin);
	entity_set_vector(Ent, EV_VEC_angles, vAngle);
	
	entity_set_int(Ent, EV_INT_effects, 2);
	entity_set_int(Ent, EV_INT_solid, SOLID_BBOX);
	entity_set_int(Ent, EV_INT_movetype, MOVETYPE_TOSS);
	entity_set_edict(Ent, EV_ENT_owner, id);
	
	VelocityByAim(id, 1000 , Velocity);
	entity_set_vector(Ent, EV_VEC_velocity ,Velocity);
	ilosc_bomb[id]--;
	client_print(id,print_center,"Pozostalo %i bomb",ilosc_bomb[id])
	

	return PLUGIN_CONTINUE;
}
public DotykRakiety(ent)
{
	if ( !is_valid_ent(ent))
		return;
	
	new attacker = entity_get_edict(ent, EV_ENT_owner);
	
	
	new Float: Origin[3];
	
	entity_get_vector(ent, EV_VEC_origin , Origin);
	
	new Ent = create_entity("info_target");
	
	entity_set_string(Ent, EV_SZ_classname, "GranattZ");
	entity_set_model(Ent, "models/rpgrocket.mdl");
	entity_set_origin(Ent, Origin);
	
	entity_set_int(Ent, EV_INT_effects, 2);
	entity_set_int(Ent, EV_INT_solid, SOLID_BBOX);
	entity_set_int(Ent, EV_INT_movetype, MOVETYPE_NOCLIP);
	entity_set_edict(Ent, EV_ENT_owner, attacker);
	
	remove_entity(ent);
} 
public Bum(id)
{
	new owner;
	new iEnt = find_ent_by_class(-1, "GranattZ");
	while(iEnt > 0) 
	{
		owner = entity_get_edict(iEnt, EV_ENT_owner);
		if(owner == id)
		{
			Wybuch(iEnt);
			remove_entity(iEnt);
		}
		iEnt = find_ent_by_class(iEnt, "GranattZ");	
	}
}
public Wybuch(ent)
{
	new attacker = entity_get_edict(ent, EV_ENT_owner);

	new Float:fOrigin[3], iOrigin[3];
	entity_get_vector( ent, EV_VEC_origin, fOrigin);
	iOrigin[0] = floatround(fOrigin[0]);
	iOrigin[1] = floatround(fOrigin[1]);
	iOrigin[2] = floatround(fOrigin[2]);
	
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY, iOrigin);
	write_byte(TE_EXPLOSION);
	write_coord(iOrigin[0]);
	write_coord(iOrigin[1]);
	write_coord(iOrigin[2]);
	write_short(sprite_blast);
	write_byte(32); // scale
	write_byte(20); // framerate
	write_byte(0);// flags
	message_end();
	
	new Float:dmg = get_cvar_float("bomb_dmg");
	
	new entlist[33];
	new numfound = find_sphere_class(ent,"player", 90.0 ,entlist, 32);
	
	for (new i=0; i < numfound; i++)
	{               
		new pid = entlist[i];
		
		ExecuteHam(Ham_TakeDamage, pid, ent, attacker, dmg , 1);
	}
	remove_entity(ent);
}
