/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <hamsandwich>
#include <fakemeta_util>

#define PLUGIN "[ZP] Toxic Bomb"
#define VERSION "0.1"
#define AUTHOR "Chrescoe1"

//#define public_test

#if !defined public_test
	#include <zombieplague>
#endif

#define weapon "weapon_smokegrenade"
#define standart_wmodel "models/w_smokegrenade.mdl"

new bool:g_has_bomb[33]
const Float:next_think=0.65
const Float:damage_radius=220.0
const Float:gas_damage=8.8
const think_to_delete = 20
const SmokeCount = 15

new const v_grenade_model[]="models/v_zombibomb_bd-s.mdl"
new const p_grenade_model[]="models/p_zombibomb_bd.mdl"
new const w_grenade_model[]="models/w_zombibomb_bd.mdl"
new const bomb_classname[]="zp_toxic_bomb"
new const gas_classname[]="Toxic_Gas"
new gSmoke,Gibs_Model

public plugin_precache()
{
	precache_model(v_grenade_model)
	precache_model(p_grenade_model)
	precache_model(w_grenade_model)
	gSmoke= precache_model("sprites/gas_puff_01g.spr")
	Gibs_Model=precache_model("models/woodgibs.mdl")
}
#if !defined public_test
new zp_new_item
#endif
public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_forward(FM_SetModel, "fw_SetModel")
	register_forward(FM_Touch, "fw_Touch")
	
	RegisterHam(Ham_Think, "info_target", "Ham_Gas_Think")
	RegisterHam(Ham_Item_Deploy, weapon, "fw_Item_Deploy_Post", 1)
	
	#if !defined public_test
		zp_new_item= zp_register_extra_item("Toxic Bomb", 5, ZP_TEAM_ZOMBIE)
	#else
		register_clcmd("say /tb","get_toxicbnomb")
	#endif
}
public fw_Item_Deploy_Post(ent)
{
	static id; id = fm_cs_get_weapon_ent_owner(ent)
	if (pev_valid(id))
	if(g_has_bomb[id])
	{
		set_pev(id, pev_viewmodel2, v_grenade_model)
		set_pev(id, pev_weaponmodel2, p_grenade_model)
	}
}
#if !defined public_test
public zp_extra_item_selected(id,itemid)
{
	if(itemid == zp_new_item)
	{
		if(g_has_bomb[id])
		{
			client_print(id,print_chat,"Error! You have this item!")
			return ZP_PLUGIN_HANDLED
		}
		else get_toxicbnomb(id)
	}
	return PLUGIN_HANDLED
}
#endif
public get_toxicbnomb(id)g_has_bomb[id]=true,fm_give_item(id, weapon)
public client_connect(id)g_has_bomb[id]=false
public zp_user_humanized_post(id,survivor)g_has_bomb[id]=false
public fw_SetModel(ent, const Model[])
{
	if(!pev_valid(ent))return FMRES_IGNORED
	if(equal(Model, standart_wmodel))
	{
		static id; id = pev(ent, pev_owner)
		if(g_has_bomb[id])
		#if !defined public_test
		if(zp_get_user_zombie(id))
		#endif
		{
			set_pev(ent, pev_classname, bomb_classname)	
			set_pev(ent, pev_dmgtime, 9999999.0)
			engfunc(EngFunc_SetModel, ent, w_grenade_model)
			
			g_has_bomb[id]=false
			return FMRES_SUPERCEDE
		}
	}
	return FMRES_IGNORED	
}
public fw_Touch(ent, touch)
{
	if(!pev_valid(ent))return HAM_IGNORED
		
	static Classname[32]; pev(ent, pev_classname, Classname, sizeof(Classname))
	if(equal(Classname, bomb_classname))
	{
		static Float:Origin[3]
		pev(ent, pev_origin, Origin)
		set_pev(ent,pev_iuser1,1)
		
		static Entity_Gas
		Entity_Gas = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "info_target"))
		engfunc(EngFunc_SetOrigin, Entity_Gas, Origin)
		set_pev(Entity_Gas, pev_classname, gas_classname)
		set_pev(Entity_Gas, pev_solid, SOLID_NOT)
		set_pev(Entity_Gas, pev_movetype, MOVETYPE_NONE)
		set_pev(Entity_Gas, pev_takedamage, 0.0)
		set_pev(Entity_Gas, pev_deadflag, DEAD_NO)
		set_pev(Entity_Gas, pev_nextthink, get_gametime() + 0.01)
		set_pev(Entity_Gas,pev_owner,pev(ent,pev_owner))
		
		message_begin(MSG_BROADCAST,SVC_TEMPENTITY); 
		write_byte(TE_BREAKMODEL); // TE_
		engfunc(EngFunc_WriteCoord, Origin[0])
		engfunc(EngFunc_WriteCoord, Origin[1])
		engfunc(EngFunc_WriteCoord, Origin[2]+24.0)
		write_coord(14); // size X
		write_coord(14); // size Y
		write_coord(14); // size Z
		write_coord(random_num(-50,50)); // velocity X
		write_coord(random_num(-50,50)); // velocity Y
		write_coord(25); // velocity Z
		write_byte(10); // random velocity
		write_short(Gibs_Model); // sprite
		write_byte(20); // count
		write_byte(15); // life
		write_byte(0x08); // flags
		message_end(); 
		
		engfunc(EngFunc_RemoveEntity, ent)
		
		return HAM_SUPERCEDE
	}
	return HAM_IGNORED
}
public Ham_Gas_Think(ent)
{
	if(!pev_valid(ent))return
	static Classname[32];pev(ent, pev_classname, Classname, sizeof(Classname))
	if(equal(Classname, gas_classname))
	{
		static owner,Float:origin[3],victim
		owner = pev(ent,pev_owner)
		pev(ent,pev_origin,origin)
		victim=-1
		while((victim = engfunc(EngFunc_FindEntityInSphere, victim, origin, damage_radius)) != 0)
		{
			if(is_user_connected(victim))
			if (is_user_alive(victim))
			#if !defined public_test
			if (!zp_get_user_zombie(victim))
			#endif
			{
				#if !defined public_test
				if(pev(victim,pev_health)>gas_damage)ExecuteHamB(Ham_TakeDamage, victim,ent, 0, gas_damage, DMG_NERVEGAS);
				else
				#endif
				ExecuteHamB(Ham_TakeDamage, victim, ent, owner, gas_damage, DMG_NERVEGAS)
				ScreenFade(victim,1,0,125,0,125)
			}
		}
		engfunc(EngFunc_MessageBegin, MSG_PVS, SVC_TEMPENTITY, origin, 0)
		write_byte(TE_FIREFIELD);
		engfunc(EngFunc_WriteCoord,origin[0])
		engfunc(EngFunc_WriteCoord,origin[1])
		engfunc(EngFunc_WriteCoord,origin[2]+50)
		write_short(floatround(damage_radius/2))//radius
		write_short(gSmoke )
		write_byte(SmokeCount)	//Count
		write_byte(TEFIRE_FLAG_ALPHA | TEFIRE_FLAG_SOMEFLOAT | TEFIRE_FLAG_LOOP );
		write_byte(25)	//duration in sec
		message_end()
		
		set_pev(ent,pev_iuser2,pev(ent,pev_iuser2)+1)
		if(pev(ent,pev_iuser2)>think_to_delete)engfunc(EngFunc_RemoveEntity, ent)
		else set_pev(ent,pev_nextthink,get_gametime()+next_think)
	}
}
//Stocks
stock ScreenFade(id, Timer, Colors1, Colors2, Colors3, Alpha)
{
	if(!is_user_connected(id)) return
	
	message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("ScreenFade"), _, id);
	write_short((1<<12) * Timer)
	write_short( 14<<18 )
	write_short( 2<<2 )	
	write_byte(Colors1)
	write_byte(Colors2)
	write_byte(Colors3)
	write_byte(Alpha)
	message_end()
}
stock fm_cs_get_weapon_ent_owner(ent)
{
	if(pev_valid(ent)!= 2)return -1
	return get_pdata_cbase(ent, 41, 4)
}
