// PSYLOCKE - from the X-men. Betsy Braddock possesses many telepathic abilities.

/* CVARS - copy and paste to shconfig.cfg

//Psylocke
psylocke_level 2

*/

/*
* v1.3 - vittu - 7/31/07
*      - Converted to amxmodx
*      - Optimized code
*
* v1.2 - vittu - 6/22/05
*      - Minor code clean up.
*
* v1.1 - vittu - 3/29/05
*      - Fixed loop to actually check all enemies for the closeset one.
*      - Did usual clean up and removal of useless code.
*
*   Hero Created by StuD|MaN
*/

#include <amxmodx>
#include <cstrike>
#include <superheromod>

new gHeroName[] = "Psylocke"
new bool:gHasPsylocke[SH_MAXSLOTS+1]
new gMsgSync
//----------------------------------------------------------------------------------------------
public plugin_init()
{
	// Plugin Info
	register_plugin("SUPERHERO Psylocke", "1.3", "StuD|MaN")

	// DO NOT EDIT THIS FILE TO CHANGE CVARS, USE THE SHCONFIG.CFG
	register_cvar("psylocke_level", "2")

	// FIRE THE EVENT TO CREATE THIS SUPERHERO!
	shCreateHero(gHeroName, "Psychic Detection", "Recieve a Telepathic Warning when an Enemy is near.", false, "psylocke_level")

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

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

	gMsgSync = CreateHudSyncObj()
}
//----------------------------------------------------------------------------------------------
public psylocke_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)

	gHasPsylocke[id] = hasPowers ? true : false
}
//----------------------------------------------------------------------------------------------
public psylocke_loop()
{
	if ( !shModActive() || !hasRoundStarted() ) return

	static players[SH_MAXSLOTS], pnum, i, e, id, enemy, closestEnemy
	static Origin[3], eOrigin[3]
	static CsTeams:idTeam

	// need to count bots for second loop but not alive players for either
	get_players(players, pnum, "ah")

	for (i = 0; i < pnum; i++) {
		id = players[i]
		if ( is_user_bot(id) || !gHasPsylocke[id] ) continue

		idTeam = cs_get_user_team(id)
		get_user_origin(id, Origin)

		//Anyone closer than this is within 30 meters
		closestEnemy = 1201

		for (e = 0; e < pnum; e++) {
			enemy = players[e]
			if ( idTeam == cs_get_user_team(enemy) ) continue

			get_user_origin(enemy, eOrigin)

			closestEnemy = min(closestEnemy, get_distance(eOrigin, Origin))
		}

		if ( closestEnemy == 1201 ) continue

		// get_distance() returns inches and 1 inch = 0.0254 meters
		switch(floatround(closestEnemy * 0.0254))
		{
			case 0..10: {
				set_hudmessage(255, 0, 0, 0.01, 0.27, 1, 6.0, 0.5, 0.1, 0.1, 4)
				ShowSyncHudMsg(id, gMsgSync, "DANGER!  Enemy Detected Within 10 Meters!!!")
			}
			case 11..20: {
				set_hudmessage(255, 155, 0, 0.01, 0.27, 1, 6.0, 0.5, 0.1, 0.1, 4)
				ShowSyncHudMsg(id, gMsgSync, "WARNING! Enemy Detected Within 20 Meters!!")
			}
			case 21..30: {
				set_hudmessage(255, 255, 255, 0.01, 0.27, 1, 6.0, 0.5, 0.1, 0.1, 4)
				ShowSyncHudMsg(id, gMsgSync, "CAUTION!  Enemy Detected Within 30 Meters!")
			}
		}
	}
}
//----------------------------------------------------------------------------------------------
