// STEEL! DC comics superhero. Dr. John Henry Irons once worked as a ballistics engineer.

/* CVARS - copy and paste to shconfig.cfg

//Steel
steel_level 0
steel_hpsetsuit 40		//Health # to set suit on (def 40)
steel_aps 3			//Amount of Armor regenerated per second when suit is on (def 3)
steel_maxaps 100		//Max Armor regenerated per second when suit is on (def 100)

*/

#include <amxmod>
#include <superheromod>

// GLOBAL VARIABLES
new gHeroName[]="Steel"
new bool:g_HasSteelPower[SH_MAXSLOTS+1]
new bool:g_hasSuit[SH_MAXSLOTS+1]
new g_hpSetSuit
//----------------------------------------------------------------------------------------------
public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO Steel", "1.0", "HeKTiK ChAoS")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("steel_level", "0")
	register_cvar("steel_hpsetsuit", "40")
	register_cvar("steel_aps", "3")
	register_cvar("steel_maxaps", "100")

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(gHeroName, "Armored Battle Suit", "Suit activates when Health gets low: you can only get shot in the Head, regen Armor, and all weapons are 2x Damage", false, "steel_level")

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

	// EVENTS
	register_event("ResetHUD", "newSpawn", "b")
	register_event("Damage", "steel_damage", "b", "2!0")

	// LOOP
	set_task(0.1, "steel_loop", 0, "", 0, "b")

	// ARMOR LOOP
	set_task(1.0, "steel_armorloop", 0, "", 0, "b")
}
//----------------------------------------------------------------------------------------------
public steel_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)

	// Set this cvar since it will get called alot
	g_hpSetSuit = get_cvar_num("steel_hpsetsuit")

	// This gets run if they had the power but don't anymore
	if ( !hasPowers && g_HasSteelPower[id] && is_user_alive(id) ) {
		set_user_hitzones(0, id, 255)
		shUnglow(id)
	}

	// Sets this variable to the current status
	g_HasSteelPower[id] = (hasPowers != 0)
}
//----------------------------------------------------------------------------------------------
public newSpawn(id)
{
	if ( shModActive() && g_HasSteelPower[id] && is_user_alive(id) && g_hasSuit[id] ) {
		set_user_hitzones(0, id, 255)
		g_hasSuit[id] = false
	}
}
//----------------------------------------------------------------------------------------------
public steel_loop()
{
	if ( !shModActive() || !hasRoundStarted() ) return

	for ( new id = 1; id <= SH_MAXSLOTS; id++ ) {
		if ( g_HasSteelPower[id] && is_user_alive(id) ) {
			new userHealth = get_user_health(id)
			if( userHealth <= g_hpSetSuit ) {
				// Only need to set it once, if they don't have the suit
				if ( !g_hasSuit[id] ) {
					set_user_hitzones(0, id, 3)
					g_hasSuit[id] = true
				}
			}
			else {
				// Only need to set it once, if they have the suit
				if ( g_hasSuit[id] ) {
					shUnglow(id)
					client_print(id, print_chat, "[SH](%s) Battle Suit removed you no longer need it", gHeroName)
					set_user_hitzones(0, id, 255)
					g_hasSuit[id] = false
				}
			}
		}
	}
}
//----------------------------------------------------------------------------------------------
public steel_armorloop()
{
	if ( !shModActive() || !hasRoundStarted() ) return

	for ( new id = 1; id <= SH_MAXSLOTS; id++ ) {
		if ( g_HasSteelPower[id] && is_user_alive(id) && g_hasSuit[id] ) {
			// Set glow here since it's called less
			shGlow(id, 35, 40, 50)

			new userArmor = get_user_armor(id)
			if ( userArmor < get_cvar_num("steel_maxaps") ) {
				// Give the armor item if armor is 0 so CS knows the player has armor
				if (userArmor <= 0) give_item(id, "item_assaultsuit")

				set_user_armor(id, userArmor + get_cvar_num("steel_aps"))
			}
		}
	}
}
//----------------------------------------------------------------------------------------------
public steel_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 ( g_HasSteelPower[attacker] && is_user_alive(id) && id != attacker && g_hasSuit[id] ) {
		// add the same ammount of damage caused (this will double it)
		new extraDamage = damage

		// find the weapons name
		new wpn[32]
		get_weaponname(weapon, wpn, 31)
		replace(wpn, 31, "weapon_", "")

		shExtraDamage(id, attacker, extraDamage, wpn, headshot)
	}
}
//----------------------------------------------------------------------------------------------