// Polaris!

/* CVARS - copy and paste to shconfig.cfg

polaris_level 5
polaris_boost 400			// How much boost to give to the grenades
polaris_range 300		// How close the grenades have to be before polaris reacts

	Based on/Ripped from sh_penguin by Yang/vittu and sh_magneto by AssKicR/JTP10181.
	Special thanks to vittu for his suggestions that really improved the hero
*/

#include <amxmodx>
#include <superheromod>
#include <csx>
#include <engine>

// GLOBAL VARIABLES
new gHeroName[]="Polaris"
new bool:gHasPolarisPowers[SH_MAXSLOTS+1]
new gSpriteLightning
new gRange, gBoost

public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO Polaris","1.11","K-OS")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("polaris_level", "5" )
	register_cvar("polaris_boost", "400" )
	register_cvar("polaris_range", "300" )

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(gHeroName, "Metal Control", "Deflect grenades", false, "polaris_level" )

	// INIT
	register_srvcmd("polaris_init", "polaris_init")
	shRegHeroInit(gHeroName, "polaris_init")

}

public plugin_precache()
{
	precache_sound("turret/tu_fire1.wav")
	gSpriteLightning = precache_model("sprites/lgtning.spr")
}

public polaris_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 wolverine skills ( Wow everyone has wolverine powers :) )
	read_argv(2,temp,5)
	new hasPowers = str_to_num(temp)
	
	gHasPolarisPowers[id] = (hasPowers != 0)
}

public grenade_throw(index, greindex, wId)
{
   if ( !shModActive() ) return

   new parm[1]
   parm[0] = greindex
   set_task(0.5, "find_polaris", greindex+15814, parm, 1, "b")
}

public find_polaris(parm[])
{
	new grenadeID = parm[0]
	
	if ( !is_valid_ent(grenadeID) ) {
		remove_task(grenadeID+15814)
		return
	}

	// Some grenades are still valid after they exploded
	new w_model[64]
	entity_get_string(grenadeID, EV_SZ_model, w_model, 63)
	if( contain(w_model, "models") == -1 ) {
		remove_task(grenadeID+15814)
		return
	}

	new players[SH_MAXSLOTS], pnum				
	get_players(players, pnum, "a")

	gRange = get_cvar_num("polaris_range")

	// Find a polaris
	for (new i = 0; i < pnum; i++) {
		if ( !gHasPolarisPowers[players[i]] ) continue

		if( get_entity_distance(players[i], grenadeID) < gRange ) {
			entity_give_boost(grenadeID, players[i])
				
			lightning_effect(grenadeID, players[i], 10)
			emit_sound(players[i], CHAN_AUTO, "turret/tu_fire1.wav", 1.0, ATTN_NORM, 0, PITCH_NORM)

			if( contain(w_model, "shmod") == -1 )	// Only reset the timer of standard grenades
				entity_set_float(grenadeID, EV_FL_nextthink, halflife_time() + 3.0)
				
			break
		}
	}
}

entity_give_boost(entity, target)	// Not really target, but the point it's moving away from.
{
	if ( !is_valid_ent(entity) ) return

	new Float:fl_Origin[3], Float:fl_Target[3], Float:fl_Velocity[3]
	entity_get_vector(target, EV_VEC_origin, fl_Target)
	entity_get_vector(entity, EV_VEC_origin, fl_Origin)

	// I only want the horizontal direction
	fl_Origin[2] = 0.0
	fl_Target[2] = 0.0
		
	gBoost = get_cvar_num("polaris_boost")
	new Float:fl_Dist = vector_distance(fl_Origin, fl_Target)

	// Get angle vector
	fl_Velocity[0] = (fl_Origin[0] -  fl_Target[0]) / fl_Dist	
	fl_Velocity[1] = (fl_Origin[1] -  fl_Target[1]) / fl_Dist

	fl_Velocity[0] = fl_Velocity[0] * gBoost
	fl_Velocity[1] = fl_Velocity[1] * gBoost
	fl_Velocity[2] = gBoost*0.8		// The grenade always gets a boost upwards

	entity_set_vector(entity, EV_VEC_velocity, fl_Velocity)
}

lightning_effect(id, targetid, linewidth)
{
	message_begin( MSG_BROADCAST, SVC_TEMPENTITY )
	write_byte( 8 )
	write_short(id)			// start entity
	write_short(targetid)	// entity
	write_short(gSpriteLightning )	// model
	write_byte( 0 ) 		// starting frame
	write_byte( 15 )  		// frame rate
	write_byte( 2 )  		// life
	write_byte( linewidth )	// line width
	write_byte( 10 )  		// noise amplitude
	write_byte( 255 )		// r, g, b
	write_byte( 255 )		// r, g, b
	write_byte( 255 )		// r, g, b
	write_byte( 255 )		// brightness
	write_byte( 0 )			// scroll speed
	message_end()
}
