//GENERAL! - Military General, gives troops courage to fight harder.

/*

//General
general_level 0
general_dmg 5			//Amount of damage added to attacks by teammates
general_cooldown 0.0	//Cooldown between added damage to attacks
general_benefit 0		//General's get extra attack damage from other General's on their team (0=no 1=yes)

*/

/*
* v1.1 - vittu - 5/4/05
*      - Removed useless code.
*      - Fixed extra attack damage from self to only be given to other players not self.
*      - Added cooldown cvar as an option.
*      - Added cvar for to only allow extra attack damage non-Generals.
*
*   Hero Created by [Pentium4].killa
*/

#include <amxmod>
#include <superheromod>

// GLOBAL VARIABLES
new gHeroName[]="General"
new bool:gHasGeneralPowers[SH_MAXSLOTS+1]
//----------------------------------------------------------------------------------------------
public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO General", "1.1", "[Pentium4].killa")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("general_level", "0")
	register_cvar("general_dmg", "5")
	register_cvar("general_cooldown", "0.0")
	register_cvar("general_benefit", "0")

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(gHeroName, "ExtraDamage for Teammates", "Because of Encouragement from the General, Teammates will do Extra Damage with weapon attacks.", false, "general_level")

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

	// EVENTS
	register_event("ResetHUD", "newSpawn", "b")
	register_event("Damage", "general_damage", "b", "2!0")
}
//----------------------------------------------------------------------------------------------
public general_init()
{
	new temp[6]
	read_argv(1, temp, 5)

	new id = str_to_num(temp)
	read_argv(2,temp,5)
	new hasPowers = str_to_num(temp)

	gHasGeneralPowers[id] = (hasPowers != 0)
}
//----------------------------------------------------------------------------------------------
public newSpawn(id)
{
	if (!shModActive()) return

	gPlayerUltimateUsed[id] = false
}
//----------------------------------------------------------------------------------------------
public general_damage(id)
{
	if (!shModActive() || !is_user_alive(id)) return

	new players[SH_MAXSLOTS], pnum, idteam
	new weapon, bodypart, attacker = get_user_attacker(id, weapon, bodypart)
	new headshot = bodypart == 1 ? 1 : 0

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

	if ( !get_cvar_num("general_benefit") ) {
		if ( gHasGeneralPowers[attacker] ) return
	}

	//get all alive players
	get_players(players, pnum, "a")

	for (new i = 0; i < pnum; i++) {
		idteam = players[i]

		if ( idteam == attacker ) continue
		if ( gPlayerUltimateUsed[idteam] || !is_user_alive(idteam) ) continue

		if ( gHasGeneralPowers[idteam] && is_user_alive(id) && get_user_team(idteam) == get_user_team(attacker) && id != attacker) {
			new extraDamage = floatround(get_cvar_float("general_dmg"))
			if (extraDamage > 0) {
				shExtraDamage(id, attacker, extraDamage, "Encouragement", headshot)

				if (get_cvar_float("general_cooldown") > 0.0) ultimateTimer(idteam, get_cvar_float("general_cooldown"))
			}
		}
	}
}
//----------------------------------------------------------------------------------------------