#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <cstrike>
#include <zombie_mode_ai>
#include <hamsandwich>
#include <fun>

// Other Const
new exp_spr, RingSpr
new g_exploded[200]
new const zb_model[] = "models/zombie_mode_ai/npc_boomer.mdl"
new const ExpSound[] = "zombie_mode_ai/SUICIDAL_EXPLOSION_NANO_BOOM.WAV"
new const AlarmSound[] = "zombie_mode_ai/SUICIDAL_EXPLOSION_NANO_ALARM.WAV"

#define TASK_DO 22222
#define TASK_EXPLODE 32321

#define RADIUS 100.0

// Class IDs
new g_zclass_cuicidal, modelindex

public plugin_init()
{
	register_plugin("[ZMAI] Zombie Class: Bommer Zombie", "1.0", "Dias")

	register_event("HLTV", "remove_ent", "a", "1=0", "2=0")
	register_logevent("remove_ent", 2, "1=Round_End")
}

public plugin_precache()
{
	modelindex = precache_model(zb_model)
	g_zclass_cuicidal = zmai_register_class(500.0, 185.0, zb_model, modelindex)

	exp_spr = precache_model("sprites/zombie_mode_ai/bommer_exp.spr")
	RingSpr = precache_model("sprites/shockwave.spr")
	
	precache_sound(ExpSound)
	precache_sound(AlarmSound)
}

public remove_ent(ent)
{
	if(task_exists(TASK_DO)) remove_task(TASK_DO)
	if(task_exists(TASK_EXPLODE)) remove_task(TASK_EXPLODE)
}

public zmai_zombie_create(ent)
{
	if (zmai_is_zombie(ent) && zmai_is_valid_zombie(ent) && zmai_get_class(ent) == g_zclass_cuicidal)
	{
		new Ent[1]
		Ent[0] = ent
		set_task(1.0, "find_enemy", TASK_DO, Ent, sizeof(Ent), "b")
	}
}

public find_enemy(Ent[])
{
	new ent = Ent[0]
	new Float:distance
	
	if(!is_valid_ent(ent))
		return PLUGIN_HANDLED
	
	static Victim, Float:Origin[3]
	Victim = -1
	pev(ent, pev_origin, Origin)
	
	while((Victim = find_ent_in_sphere(Victim, Origin, RADIUS)) != 0)
	{			
		if(!is_user_alive(Victim))
			continue;
		
		distance = entity_range(ent, Victim)
		if((distance <= RADIUS - 20.0) && !g_exploded[ent])
		{
			zmai_set_think(ent, 0)
			explode(ent)
			entity_set_int(ent, EV_INT_movetype, MOVETYPE_NONE)
			g_exploded[ent] = 1
		}
	}
	
	return PLUGIN_CONTINUE
}

public explode(ent)
{
	if (zmai_is_zombie(ent) && zmai_is_valid_zombie(ent) && zmai_get_class(ent) == g_zclass_cuicidal)
	{
		set_rendering1(ent, 250, 0, 0, 16.0, kRenderFxGlowShell, kRenderNormal)
		emit_sound(ent, CHAN_BODY, AlarmSound, 1.0, ATTN_NORM, 0, PITCH_NORM)
	
		new Ent[1]
		Ent[0] = ent
	
		set_task(2.0 + 0.2, "do_skill", TASK_EXPLODE, Ent, sizeof(Ent))
	}
}

public do_skill(Ent[])
{
	new ent = Ent[0]
	if (zmai_is_zombie(ent) && zmai_is_valid_zombie(ent) && zmai_get_class(ent) == g_zclass_cuicidal)
	{
		new Float:Origin[3]
		pev(ent, pev_origin, Origin)
		
		// Collisions
		static iVictim
		iVictim = -1
	
		message_begin(MSG_BROADCAST ,SVC_TEMPENTITY)
		write_byte(TE_EXPLOSION)
		engfunc(EngFunc_WriteCoord, Origin[0])
		engfunc(EngFunc_WriteCoord, Origin[1])
		engfunc(EngFunc_WriteCoord, Origin[2])
		write_short(exp_spr)
		write_byte(20)
		write_byte(30)
		write_byte(4)
		message_end()
		
		engfunc(EngFunc_MessageBegin, MSG_PVS, SVC_TEMPENTITY, Origin, 0)
		write_byte(TE_BEAMCYLINDER) // TE id
		engfunc(EngFunc_WriteCoord, Origin[0]) // x
		engfunc(EngFunc_WriteCoord, Origin[1]) // y
		engfunc(EngFunc_WriteCoord, Origin[2]) // z
		engfunc(EngFunc_WriteCoord, Origin[0]) // x axis
		engfunc(EngFunc_WriteCoord, Origin[1]) // y axis
		engfunc(EngFunc_WriteCoord, Origin[2] + RADIUS) // z axis
		write_short(RingSpr) // sprite
		write_byte(0) // startframe
		write_byte(0) // framerate
		write_byte(10) // life
		write_byte(25) // width
		write_byte(0) // noise
		write_byte(255) // red
		write_byte(255) // green
		write_byte(127) // blue
		write_byte(200) // brightness
		write_byte(0) // speed
		message_end()
		
		emit_sound(ent, CHAN_BODY, ExpSound, 1.0, ATTN_NORM, 0, PITCH_NORM)
		
		while((iVictim = find_ent_in_sphere(iVictim, Origin, RADIUS)) != 0)
		{			
			if(!is_user_alive(iVictim))
				continue;
			
			// Do Effect 2
			new Float:vec[3] = {50.0, 50.0, 50.0}
			entity_set_vector(iVictim, EV_VEC_punchangle, vec)
			
			ExecuteHam(Ham_TakeDamage, iVictim, 0, iVictim, 50.0, DMG_BULLET) 
		}
		
		g_exploded[ent] = 0
		remove_entity(ent)
		zmai_set_zombie_count(zmai_get_zombie_count() - 1)
	}
}

//setting the render to an entity
stock set_rendering1(ent, r=255, g=255, b=255, Float:amount=0.0, fx=kRenderFxNone, mode=kRenderNormal)
{
	if(!pev_valid(ent)) 
		return 0
		
	static Float:color[3]
	
	color[0]=float(r)
	color[1]=float(g)
	color[2]=float(b)
	
	set_pev(ent,pev_renderfx,fx)
	set_pev(ent,pev_rendercolor,color)
	set_pev(ent,pev_rendermode,mode)
	set_pev(ent,pev_renderamt,amount)
    
	return 1
}  
