// BISHOP! - from X-MEN, member of the X.S.E. who is trapped in a past that is no longer his own.
// In truth he can't absorb a projectile weapons energy, but this is cs so what you gonna do.

/*

//Bishop
bishop_level 7
bishop_absorbmult 0.50	//Weapon damage taken X this cvar = damage absorbed [def=0.50]
bishop_damagemult 0.75	//Energy absorbed X this cvar = extra weapon damage dealt [def=0.75]
bishop_blastmult 2.50	//Energy absorbed X this cvar = damage that Energy Blast deals [def=2.50]

*/

/*
* v1.1 - vittu - 12/28/05
*      - Cleaned up code.
*      - Changed look and color of energy blast and hud message a bit.
*      - Fixed stored absorbed damage to be seperate for each Bishop user.
*      - Fixed Energy Blast to not repeat on user in crosshair.
*
*/

#include <amxmod>
#include <superheromod>

// GLOBAL VARIABLES
new g_heroName[]="Bishop"
new bool:g_hasBishop[SH_MAXSLOTS+1]
new g_absorbedDamage[SH_MAXSLOTS+1]
new g_spriteLaser, g_spriteExplosion
//----------------------------------------------------------------------------------------------
public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO Bishop", "1.1", "scoutPractice")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("bishop_level", "7")
	register_cvar("bishop_absorbmult", "0.50")
	register_cvar("bishop_damagemult", "0.75")
	register_cvar("bishop_blastmult", "2.50")

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(g_heroName, "Absorb Energy", "Absorb Damage and use it with your weapons! Or release all energy to deal even more damage.", true, "bishop_level")

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

	// KEY DOWN
	register_srvcmd("bishop_kd", "bishop_kd")
	shRegKeyDown(g_heroName, "bishop_kd")

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

	// ABSORB ENERGY!
	register_event("Damage", "bishop_damage", "b", "2!0")

	// LOOP
	set_task(1.0, "bishop_loop", 0, "", 0, "b")
}
//----------------------------------------------------------------------------------------------
public plugin_precache()
{
	g_spriteLaser = precache_model("sprites/laserbeam.spr")
	g_spriteExplosion = precache_model("sprites/zerogxplode.spr")
}
//----------------------------------------------------------------------------------------------
public bishop_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)

	g_hasBishop[id] = (hasPowers != 0)
}
//----------------------------------------------------------------------------------------------
public newSpawn(id)
{
	g_absorbedDamage[id] = 0
}
//----------------------------------------------------------------------------------------------
// RESPOND TO KEYDOWN
public bishop_kd()
{
	if ( !hasRoundStarted() ) return

	// First Argument is an id
	new temp[6]
	read_argv(1,temp,5)
	new id = str_to_num(temp)

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

	// Let them know if they have no energy
	if ( g_absorbedDamage[id] <= 0 ) {
		client_print(id, print_chat, "[SH](Bishop) You have NO energy left in reserve!")
		playSoundDenySelect(id)
		return
	}

	release_energy(id)
}
//----------------------------------------------------------------------------------------------
public bishop_loop()
{
	for (new id = 1; id <= SH_MAXSLOTS; id++) {
		if ( g_hasBishop[id] && is_user_alive(id) ) {
			new message[128]
			format(message, 127, "Total Energy Absorbed: %i", g_absorbedDamage[id])
			set_hudmessage(50, 50, 255, -1.0, 0.10, 0, 1.0, 1.0, 0.0, 0.0, 4)
			show_hudmessage(id, message)
		}
	}
}
//----------------------------------------------------------------------------------------------
public bishop_damage(id)
{
	if ( !shModActive() || !is_user_alive(id) ) return

	new damage = read_data(2)
	new weapon, bodypart, attacker = get_user_attacker(id, weapon, bodypart)

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

	if ( g_hasBishop[id] && attacker != id ) {
		// Absorb Damage
		new damageAbsorbed = floatround(get_cvar_float("bishop_absorbmult") * damage)
		g_absorbedDamage[id] += damageAbsorbed

		client_print(id, print_center, "You absorbed %d energy points", damageAbsorbed)

		// Screen Flash To Acknowledge damage has been absorbed
		new alphanum = damage * 2
		if (alphanum > 200) alphanum = 200
		else if (alphanum < 40) alphanum = 40
		setScreenFlash(id, 255, 90, 102, 10, alphanum)
	}

	if ( g_hasBishop[attacker] && is_user_alive(id) && attacker != id && g_absorbedDamage[attacker] > 0 ) {
		// headshots but not on nades
		new headshot = (bodypart == 1 && weapon != CSW_HEGRENADE) ? 1 : 0
		new weaponName[32]
		get_weaponname(weapon, weaponName, 31)
		replace(weaponName, 31, "weapon_", "")

		// Deal Extra Damage
		new energyDamage = floatround(get_cvar_float("bishop_damagemult") * g_absorbedDamage[attacker])
		if ( energyDamage > 0 ) shExtraDamage(id, attacker, energyDamage, weaponName, headshot)

		// Should extra weapon damage be made to cost energy points?
	}
}
//----------------------------------------------------------------------------------------------
public release_energy(id)
{
	if ( !is_user_alive(id) || !g_hasBishop[id] ) return

	new userAim[3], vicOrigin[3]
	new FFOn = get_cvar_num("mp_friendlyfire")
	new blastDamage = floatround(get_cvar_float("bishop_blastmult") * g_absorbedDamage[id])
	new bool:hit[SH_MAXSLOTS+1] = false

	get_user_origin(id, userAim, 3)

	beam_effects(id, userAim)

	for (new vic = 1; vic <= SH_MAXSLOTS; vic++) {
		if ( is_user_alive(vic) && ( get_user_team(id) != get_user_team(vic) || FFOn ) ) {

			get_user_origin(vic, vicOrigin)

			if ( get_distance(userAim, vicOrigin) <= 150 ) {
				shExtraDamage(vic, id, blastDamage, "Energy Blast")
				hit[id] = true
			}
		}
	}

	if ( hit[id] ) {
		client_print(id, print_chat, "[SH](Bishop) ENERGY BLAST of %d Hit Points", blastDamage)
	}
	else {
		client_print(id, print_chat, "[SH](Bishop) Your Energy Blast MISSED!")
	}

	g_absorbedDamage[id] = 0
}
//----------------------------------------------------------------------------------------------
public beam_effects(id, userAim[3])
{
	if ( !shModActive() || !is_user_alive(id) || !g_hasBishop[id] ) return

	new userEyesight[3]
	get_user_origin(id, userEyesight, 1)

	// Energy Beam
	message_begin(MSG_PAS, SVC_TEMPENTITY, userEyesight)
	write_byte(1)				// TE_BEAMENTPOINT
	write_short(id)			// start entity
	write_coord(userAim[0])		// end position
	write_coord(userAim[1])
	write_coord(userAim[2])
	write_short(g_spriteLaser)	// sprite index
	write_byte(0)		// starting frame
	write_byte(10)		// frame rate in 0.1's
	write_byte(2)		// life in 0.1s
	write_byte(80)		// line width in 0.1's
	write_byte(9)		// noise amplitude in 0.01's
	write_byte(255)	// Red
	write_byte(90)		// Green
	write_byte(102)	// Blue
	write_byte(150)	// brightness
	write_byte(100)	// scroll speed in 0.1's
	message_end()

	// Explosion (smoke, sound/effects)
	message_begin(MSG_PAS, SVC_TEMPENTITY, userAim)
	write_byte(3)			// TE_EXPLOSION
	write_coord(userAim[0])	// start position
	write_coord(userAim[1])
	write_coord(userAim[2])
	write_short(g_spriteExplosion)	// sprite index
	write_byte(30)			// scale in 0.1's
	write_byte(30)			// framerate
	write_byte(8)			// flags
	message_end()
}
//----------------------------------------------------------------------------------------------