// MADNESS! - The animated flash characters, from http://www.madnesscombat.com/

/* CVARS - copy and paste to shconfig.cfg

//Madness
madness_level 9
madness_health 200		//how much health madness has
madness_armor 100		//how much armor madness has
madness_m3mult 2.0		//Damage multiplyer for his M3

*/

/*
* v1.3 - vittu - 6/21/05
*      - Minor code clean up.
*
*  Ripped from the hero - Morpheus (by RadidEskimo & Freecode).
*  Weapon model by Eichler69 & Biohazard
*/

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

// GLOBAL VARIABLES
new gHeroName[]="Madness"
new bool:gHasMadnessPower[SH_MAXSLOTS+1]
//----------------------------------------------------------------------------------------------
public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO Madness", "1.3", "Assassin")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("madness_level", "9")
	register_cvar("madness_health", "200")
	register_cvar("madness_armor", "100")
	register_cvar("madness_m3mult", "2.0")

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(gHeroName, "Dual M3's", "Dual M3's/Extra Damage/Unlimited Ammo. Extra HP and AP.", false, "madness_level")

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

	// EVENTS
	register_event("ResetHUD", "newSpawn","b")
	register_event("CurWeapon", "weaponChange", "be", "1=1")
	register_event("Damage", "madness_damage", "b", "2!0")

	// Let Server know about Madness's Variable
	shSetMaxHealth(gHeroName, "madness_health")
	shSetMaxArmor(gHeroName, "madness_armor")
	shSetShieldRestrict(gHeroName)
}
//----------------------------------------------------------------------------------------------
public plugin_precache()
{
	precache_model("models/shmod/madness_m3.mdl")
}
//----------------------------------------------------------------------------------------------
public madness_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)

	if (!is_user_connected(id)) return

	//Reset thier shield restrict status
	//Shield restrict MUST be before weapons are given out
	shResetShield(id)

	if ( is_user_alive(id) ) {
		if ( hasPowers ) {
			madness_weapons(id)
			switchmodel(id)
		}
		//This gets run if they had the power but don't anymore
		else if ( !hasPowers && gHasMadnessPower[id] ) {
			engclient_cmd(id, "drop", "weapon_m3")
			shRemHealthPower(id)
			shRemArmorPower(id)
		}
	}
	//Sets this variable to the current status
	gHasMadnessPower[id] = (hasPowers != 0)
}
//----------------------------------------------------------------------------------------------
public newSpawn(id)
{
	if ( shModActive() && gHasMadnessPower[id] && is_user_alive(id) ) {
		set_task(0.1, "madness_weapons", id)

		new clip, ammo, wpnid = get_user_weapon(id, clip, ammo)
		if (wpnid != CSW_M3 && wpnid > 0) {
			new wpn[32]
			get_weaponname(wpnid, wpn, 31)
			engclient_cmd(id, wpn)
		}
	}
}
//----------------------------------------------------------------------------------------------
public madness_weapons(id)
{
	if ( shModActive() && is_user_alive(id) ) {
		shGiveWeapon(id,"weapon_m3")
	}
}
//----------------------------------------------------------------------------------------------
public switchmodel(id)
{
	if ( !is_user_alive(id) ) return

	new clip, ammo, wpnid = get_user_weapon(id, clip, ammo)
	if ( wpnid == CSW_M3 ) {
		// Weapon Model change thanks to [CCC]Taz-Devil
		Entvars_Set_String(id, EV_SZ_viewmodel, "models/shmod/madness_m3.mdl")
	}
}
//----------------------------------------------------------------------------------------------
public weaponChange(id)
{
	if ( !gHasMadnessPower[id] || !shModActive() ) return

	new wpnid = read_data(2)
	new clip = read_data(3)

	if ( wpnid != CSW_M3 ) return

	switchmodel(id)

	// Never Run Out of Ammo!
	if ( clip == 0 ) {
		shReloadAmmo(id)
	}
}
//----------------------------------------------------------------------------------------------
public madness_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 ( gHasMadnessPower[attacker] && weapon == CSW_M3 && is_user_alive(id) ) {
		// do extra damage
		new extraDamage = floatround(damage * get_cvar_float("madness_m3mult") - damage)
		if (extraDamage > 0) shExtraDamage(id, attacker, extraDamage, "m3", headshot)
	}
}
//----------------------------------------------------------------------------------------------
public client_connect(id)
{
	gHasMadnessPower[id] = false
}
//----------------------------------------------------------------------------------------------