// ALIEN GRUNT!

/********************REQUIRES Monster Mod*********************/
/** "agrunt" must be enabled in the monster precache config **/

/* CVARS - copy and paste to shconfig.cfg

//Alien Grunt
agrunt_level 0
agrunt_cooldown 30		// Number of seconds before you can spawn another monster (Default 30)
agrunt_radius 200		// Radius at aim location to find closest victim (Default 200)

*/

/*
* v1.2 - vittu - 7/19/06
*      - Renamed file from sh_agrunt to sh_aliengrunt.
*      - Small code changes, updated to amxmodx 1.75 only. Now uses fakemeta.
*
* v1.1 - vittu - 2/28/06
*      - Re-coded and cleaned up.
*      - Changed moseter spawn action to aim radius instead of exact aim.
*      - Changed cooldown to only set if monster spawned, also upped default
*          cooldown time because of this.
*
*   From Original code:
*   Created by litovietboi & [SIN]. Credits goes to Freecode for helping with keydown.
*/

#include <amxmodx>
#include <fakemeta>
#include <superheromod>

// GLOBAL VARIABLES
new HeroName[] = "Alien Grunt"
new bool:HasAlienGrunt[SH_MAXSLOTS+1]
new bool:NoTarget[SH_MAXSLOTS+1]
new CvarCooldown, CvarRadius
//--------------------------------------------------------------------------------------------------
public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO Alien Grunt", "1.2", "LiToViEtBoI/[SiN]")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("agrunt_level", "0")
	CvarCooldown = register_cvar("agrunt_cooldown", "30")
	CvarRadius = register_cvar("agrunt_radius", "200")

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(HeroName, "Summon Alien Grunt", "Summon an Alien Grunt on enemy closest to aim location with +power key", true, "agrunt_level")

	// REGISTER EVENTS THIS HERO WILL RESPOND TO! (AND SERVER COMMANDS)
	// INIT
	register_srvcmd("agrunt_init", "agrunt_init")
	shRegHeroInit(HeroName, "agrunt_init")

	// KEY DOWN
	register_srvcmd("agrunt_kd", "agrunt_kd")
	shRegKeyDown(HeroName, "agrunt_kd")

	// NEW SPAWN
	register_event("ResetHUD", "new_spawn", "b")
}
//--------------------------------------------------------------------------------------------------
public plugin_precache()
{
	precache_sound("ambience/port_suckin1.wav")
}
//--------------------------------------------------------------------------------------------------
public agrunt_init()
{
	// First Argument is an id
	new temp[6]
	read_argv(1, temp, 5)
	new id = str_to_num(temp)

	// 2nd Argument is 0 or 1 depending on whether the id has the hero
	read_argv(2, temp, 5)
	new hasPowers = str_to_num(temp)

	HasAlienGrunt[id] = (hasPowers != 0)
}
//--------------------------------------------------------------------------------------------------
public new_spawn(id)
{
	gPlayerUltimateUsed[id] = false
	NoTarget[id] = false
}
//--------------------------------------------------------------------------------------------------
public agrunt_kd()
{
	if ( !hasRoundStarted() )
		return

	// First Argument is an id
	new temp[6]
	read_argv(1, temp, 5)
	new id = str_to_num(temp)

	if ( !is_user_alive(id) )
		return 

	if ( gPlayerUltimateUsed[id] )
	{
		playSoundDenySelect(id)
		return
	}

	agrunt_summon(id)
}
//--------------------------------------------------------------------------------------------------
public agrunt_summon(id)
{
	if ( !shModActive() || !is_user_alive(id) || !HasAlienGrunt[id] )
		return

	new distance, aimOrigin[3], vicOrigin[3], radius[SH_MAXSLOTS+1]
	new players[SH_MAXSLOTS], pnum, victim, targetid
	new idTeam = get_user_team(id)

	radius[id] = get_pcvar_num(CvarRadius)

	get_user_origin(id, aimOrigin, 3)

	get_players(players, pnum, "a")

	// Find the closest target to aim location, and make sure monster can spawn on them
	for (new i = 0; i < pnum; i++)
	{
		victim = players[i]

		if ( !is_user_alive(victim) || idTeam  == get_user_team(victim) )
			continue

		get_user_origin(victim, vicOrigin)

		distance = get_distance(vicOrigin, aimOrigin)

		if ( distance < radius[id] )
		{
			if ( pev(victim, pev_flags) & FL_NOTARGET )
			{
				NoTarget[id] = true
				continue
			}

			radius[id] = distance
			targetid = victim
		}
	}

	if ( targetid && is_user_alive(targetid) )
	{
		emit_sound(id, CHAN_STATIC, "ambience/port_suckin1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)
		emit_sound(targetid, CHAN_STATIC, "ambience/port_suckin1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)

		new targetName[32]
		get_user_name(targetid, targetName, 31)
		client_print(id, print_chat, "[SH](%s) Monster Spawned on %s", HeroName, targetName)

		server_cmd("monster agrunt #%i", targetid)

		new Float:cooldown = get_pcvar_float(CvarCooldown)
		if ( cooldown > 0.0 )
			ultimateTimer(id, cooldown)

		NoTarget[id] = false
	}
	else
	{
		switch(NoTarget[id])
		{
			case false:
			{
				client_print(id, print_center, "[SH](%s) No target found", HeroName)
				playSoundDenySelect(id)
			}

			case true:
			{
				client_print(id, print_center, "[SH](%s) Unable to spawn monster on victim", HeroName)
				NoTarget[id] = false
				playSoundDenySelect(id)
			}
		}
	}
}
//--------------------------------------------------------------------------------------------------
public client_connect(id)
{
	HasAlienGrunt[id] = false
	NoTarget[id] = false
}
//--------------------------------------------------------------------------------------------------