// GAMBIT! - from the X-men. Can convert any inorganic object's potential energy into kinetic energy on contact.

/* CVARS - copy and paste to shconfig.cfg

//Gambit
gambit_level 0
gambit_grenademult 60.9		//Damage multiplyer from orginal damage amount (def 60.9)
gambit_grenadetimer 30.0		//How many seconds delay for new grenade after nade is thrown (def 30.0)
gambit_cooldown 120.0		//How many seconds until extra grenade damage can be used again (def 120.0)

*/

/*
* v1.3 - vittu - 9/27/05
*      - Fixed up cooldown to only be set once.
*      - Removed some useless checks.
*
* v1.2 - vittu - 6/19/05
*      - Minor code clean up.
*
* v1.1 - vittu - 5/10/05
*      - Fixed giving new grenades using a more reliable event.
*      - Fixed grenade timer to only be set if you have no nades,
*          avoids timer exploiting.
*      - Added cooldown cvar, sets only if someone is hurt by gambit.
*      - Added grenade glow and trail only for gambit nades.
*
*   Hero Created by Vectren
*/

#include <amxmod>
#include <Vexd_Utilities>
#include <superheromod>

#define AMMOX_HEGRENADE 12

// GLOBAL VARIABLES
new gHeroName[]="Gambit"
new bool:gHasGambitPower[SH_MAXSLOTS+1]
new gGrenTrail
new const HEGRENADE_MODEL[] = "models/w_hegrenade.mdl"
//----------------------------------------------------------------------------------------------
public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO Gambit", "1.3", "Vectren / vittu")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("gambit_level", "0")
	register_cvar("gambit_grenademult", "60.9")
	register_cvar("gambit_grenadetimer", "30.0")
	register_cvar("gambit_cooldown", "120.0")

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(gHeroName, "Kinetically Charged Nades", "Charge your HE Grenades with Kinetic Energy for Extra Damage, also refill HE Grenades.", false, "gambit_level")

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

	// NEW SPAWN
	register_event("ResetHUD", "newSpawn", "b")

	// EXTRA NADE DAMAGE
	register_event("Damage", "gambit_damage", "b", "2!0")

	// FIND THROWN GRENADES
	register_event("AmmoX", "on_AmmoX", "b")
}
//----------------------------------------------------------------------------------------------
public plugin_precache()
{
	gGrenTrail = precache_model("sprites/zbeam5.spr")
}
//----------------------------------------------------------------------------------------------
public gambit_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)

	gHasGambitPower[id] = (hasPowers!=0)

	if ( hasPowers && is_user_alive(id) ) {
		gambit_weapons(id)
	}
}
//----------------------------------------------------------------------------------------------
public newSpawn(id)
{
	if ( shModActive() && gHasGambitPower[id] && is_user_alive(id) ) {
		gPlayerUltimateUsed[id] = false
		set_task(0.1, "gambit_weapons", id)
	}
}
//----------------------------------------------------------------------------------------------
public gambit_weapons(id)
{
	if ( shModActive() && gHasGambitPower[id] && is_user_alive(id) ) {
		shGiveWeapon(id, "weapon_hegrenade")
	}
}
//----------------------------------------------------------------------------------------------
public gambit_damage(id)
{
	if ( !shModActive() || !is_user_alive(id) ) return

	new damage = read_data(2)
	new weapon, bodypart, attacker = get_user_attacker(id, weapon, bodypart)
	new headshot = bodypart == 1 ? 1 : 0

	if ( attacker <= 0 || attacker > SH_MAXSLOTS ) return

	if ( gHasGambitPower[attacker] && weapon == CSW_HEGRENADE && is_user_alive(id) && !gPlayerUltimateUsed[attacker] ) {
		// do extra damage
		new extraDamage = floatround(damage * get_cvar_float("gambit_grenademult") - damage)
		if (extraDamage > 0) shExtraDamage(id, attacker, extraDamage, "grenade", headshot)

		if ( attacker != id ) {
			// Set the cooldown in x seconds because nades can hurt more then one person
			// only when damaging others
			new parm[1]
			parm[0] = attacker
			set_task(0.2, "gambit_setcooldown", 0, parm,1)
		}
	}
}
//----------------------------------------------------------------------------------------------
public on_AmmoX(id)
{
	if ( !shModActive() || !is_user_alive(id) ) return

	new iAmmoType = read_data(1)
	new iAmmoCount = read_data(2)

	if ( iAmmoType == AMMOX_HEGRENADE && gHasGambitPower[id] ) {

		if (iAmmoCount == 0) {
			set_task(get_cvar_float("gambit_grenadetimer"), "gambit_weapons", id)

			if ( !gPlayerUltimateUsed[id] ) {
				// Have to Find the current HE grenade
				new iCurrent = -1
				while ( ( iCurrent = FindEntity(iCurrent, "grenade") ) > 0 ) {
					new string[32]
					Entvars_Get_String(iCurrent, EV_SZ_model, string, 31)

					if ( id == Entvars_Get_Edict(iCurrent, EV_ENT_owner) && equali(HEGRENADE_MODEL, string)) {

						new Float:glowColor[3] = {225.0, 0.0, 20.0}

						// Make the nade glow
						Entvars_Set_Int(iCurrent, EV_INT_renderfx, kRenderFxGlowShell)
						Entvars_Set_Vector(iCurrent, EV_VEC_rendercolor, glowColor)

						// Make the nade a bit invisible to make glow look better
						Entvars_Set_Int(iCurrent, EV_INT_rendermode, kRenderTransAlpha)
						Entvars_Set_Float(iCurrent, EV_FL_renderamt, 100.0 )

						// Make a trail
						message_begin(MSG_BROADCAST ,SVC_TEMPENTITY)
						write_byte(22)			//TE_BEAMFOLLOW
						write_short(iCurrent)	// entity:attachment to follow
						write_short(gGrenTrail)	// sprite index
						write_byte(10)		// life in 0.1's
						write_byte(10)		// line width in 0.1's
						write_byte(225)	// colour
						write_byte(90)
						write_byte(102)
						write_byte(255)	// brightness
						message_end()
					}
				}
			}
		}
		else if (iAmmoCount > 0) {
			// Got a new nade remove the timer
			remove_task(id)
		}
	}
}
//----------------------------------------------------------------------------------------------
public gambit_setcooldown(parm[])
{
	new id = parm[0]

	if ( !is_user_alive(id) || gPlayerUltimateUsed[id] ) return

	// Cooldown will only be set if user hurts someone with a gambit nade
	new Float:gambitCooldown = get_cvar_float("gambit_cooldown")
	if (gambitCooldown > 0.0) ultimateTimer(id, gambitCooldown)
}
//----------------------------------------------------------------------------------------------
public client_connect(id)
{
	gHasGambitPower[id] = false
}
//----------------------------------------------------------------------------------------------