#include <amxmodx>
#include <hamsandwich>
#include <fun>
#include <fakemeta>
#include <engine>
#include <zombieplague>

/* Credits go 
* SpaceDude - For his suicide Code from War3FT Plugin */

#define PLUGIN "[ZP] Extra : Merkaba Hanzelrusha"
#define VERSION "1.0.7"
#define AUTHOR "Kia"

#define EXPLODE_FREQ 		1.2
#define DMG_BLAST		(1<<6)

/* Integer */

new g_iItemID

/* CVars */

new cvar_explosions, cvar_damage, cvar_radius, cvar_force

/* Effect Stuff */

new g_sModelIndexFireball
new g_sModelIndexSmoke
new m_iSpriteTexture

public plugin_precache() 
{
	g_sModelIndexFireball 	= precache_model("sprites/zerogxplode.spr")
	g_sModelIndexSmoke 	= precache_model("sprites/steam1.spr")
	m_iSpriteTexture 	= precache_model("sprites/shockwave.spr")
	
	precache_sound("ambience/particle_suck1.wav")
}

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	/* Registering Item */
	
	g_iItemID = zp_register_extra_item("Merkaba Hanzelrusha", 15, ZP_TEAM_HUMAN)
	
	/* CVars */
	
	cvar_explosions  	= register_cvar("zp_mh_explosions",	"3")
	cvar_damage		= register_cvar("zp_mh_maxdamage",	"33")
	cvar_radius		= register_cvar("zp_mh_radius",		"300")
	cvar_force		= register_cvar("zp_mh_force",		"75")
	
}

public zp_extra_item_selected(id, ItemID)
{
	if (ItemID == g_iItemID )
	{
		func_ExecuteMH(id)
	}
}

public func_ExecuteMH(id)
{
	new Float:iDelay
	
	for(new i; i < get_pcvar_num(cvar_explosions); i++)
	{
		iDelay += EXPLODE_FREQ
		set_task(iDelay, "func_Explosion", id)
		set_task(iDelay, "func_Circle", id)
	}
}

public func_Explosion(id)
{
	emit_sound(id,CHAN_STATIC, "ambience/particle_suck1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
	
	new iOrigin[3]
	get_user_origin(id,iOrigin)
	message_begin( MSG_BROADCAST, SVC_TEMPENTITY )
	write_byte( TE_IMPLOSION )
	write_coord(iOrigin[0])
	write_coord(iOrigin[1])
	write_coord(iOrigin[2])
	write_byte(100)
	write_byte(20)
	write_byte(5)
	message_end()
	
	message_begin( MSG_PVS, SVC_TEMPENTITY, iOrigin )
	write_byte(TE_EXPLOSION)
	write_coord(iOrigin[0] + random_num(-100, 100))
	write_coord(iOrigin[1] + random_num(-100, 100))
	write_coord(iOrigin[2] + random_num(-50, 50))
	write_short(g_sModelIndexFireball)
	write_byte(random_num(0,20) + 20 )
	write_byte(12) 
	write_byte(TE_EXPLFLAG_NONE)
	message_end()
	
	message_begin(MSG_PVS, SVC_TEMPENTITY, iOrigin)
	write_byte(TE_SMOKE)
	write_coord(iOrigin[0] + random_num(-100, 100))
	write_coord(iOrigin[1] + random_num(-100, 100))
	write_coord(iOrigin[2] + random_num(-50, 50))
	write_short(g_sModelIndexSmoke)
	write_byte(60)
	write_byte(10)
	message_end()
	
	new players[32], pnum, tempid
	new TempOrigin[3]
	
	new iDistance, iMultiplier, iDamage
	
	new Float:vec[3];
	new Float:oldvelo[3];
	
	get_players(players, pnum, "ae", "TERRORIST")
	for(new i; i <= pnum; i++)
	{
		tempid = players[i]
		pev(tempid, pev_origin, TempOrigin)
		
		iDistance = get_distance(iOrigin, TempOrigin)
		
		if(iDistance < get_pcvar_num(cvar_radius) && get_user_team(id) != get_user_team(tempid) && is_user_alive(tempid))
		{
			iMultiplier = get_pcvar_num(cvar_damage) * get_pcvar_num(cvar_damage) / get_pcvar_num(cvar_radius)
			iDamage = (get_pcvar_num(cvar_radius) - iDistance) * iMultiplier
			
			set_user_health(tempid, get_user_health(tempid) - iDamage)
			
			get_user_velocity(tempid, oldvelo);
			create_velocity_vector(tempid , id , vec);
			vec[0] += oldvelo[0];
			vec[1] += oldvelo[1];
			set_user_velocity(tempid , vec)
		}
	}
}

// Stock by the one and only, Chronic :P
stock create_velocity_vector(victim,attacker,Float:velocity[3])
{
	if(!is_user_alive(victim) || !is_user_alive(attacker))
		return 0;

	new Float:vicorigin[3];
	new Float:attorigin[3];
	entity_get_vector(victim   , EV_VEC_origin , vicorigin);
	entity_get_vector(attacker , EV_VEC_origin , attorigin);

	new Float:origin2[3]
	origin2[0] = vicorigin[0] - attorigin[0];
	origin2[1] = vicorigin[1] - attorigin[1];

	new Float:largestnum = 0.0;

	if(floatabs(origin2[0])>largestnum) largestnum = floatabs(origin2[0]);
	if(floatabs(origin2[1])>largestnum) largestnum = floatabs(origin2[1]);

	origin2[0] /= largestnum;
	origin2[1] /= largestnum;

	velocity[0] = ( origin2[0] * (get_pcvar_float(cvar_force) * 3000) ) / get_entity_distance(victim , attacker);
	velocity[1] = ( origin2[1] * (get_pcvar_float(cvar_force) * 3000) ) / get_entity_distance(victim , attacker);
	if(velocity[0] <= 20.0 || velocity[1] <= 20.0)
	velocity[2] = random_float(200.0 , 275.0);

	return 1;
}
			
public func_Circle(id)
{
	new iOrigin[3]
	get_user_origin(id,iOrigin)
	
	message_begin(MSG_PAS, SVC_TEMPENTITY, iOrigin)
	write_byte(TE_BEAMCYLINDER)
	write_coord(iOrigin[0])
	write_coord(iOrigin[1])
	write_coord(iOrigin[2] - 16)
	write_coord(iOrigin[0])
	write_coord(iOrigin[1])
	write_coord(iOrigin[2] - 16 + 250)
	write_short(m_iSpriteTexture)
	write_byte(0)
	write_byte(0)
	write_byte(6)
	write_byte(1)
	write_byte(0)
	write_byte(25)
	write_byte(10)
	write_byte(0)
	write_byte(255) 
	write_byte(0)
	message_end()
}
