/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <hamsandwich>
#include <zombieplague>

#define PLUGIN "Zombie Plague extra item - Radar"
#define VERSION "1.3"
#define AUTHOR "Sonic Son'edit"

new const  cost = 5 // CTouMocTb

new g_msgHostageAdd, g_msgHostageDel, g_maxplayers, g_itemid_radar;

new player_has_radar[33];
new zp_radar_beep;
new zp_radar_show_dead;

new cvar_zp_radar_beep;
new cvar_zp_radar_single_round;
new cvar_zp_radar_show_dead;

new const sound_radar_beep[] = "zombie_plague/nes_8bit_alien3_radar_beep1.wav"


public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_event("HLTV", "event_round_start", "a")
	
	RegisterHam(Ham_Killed, "player", "fw_PlayerKilled")

	g_msgHostageAdd = get_user_msgid("HostagePos")
	g_msgHostageDel = get_user_msgid("HostageK")
	
	cvar_zp_radar_beep = register_cvar("zp_radar_beep", "1")
	cvar_zp_radar_single_round = register_cvar("zp_radar_single_round", "0")
	cvar_zp_radar_show_dead = register_cvar("zp_radar_show_dead", "0")
	
	g_maxplayers = get_maxplayers()

	g_itemid_radar = zp_register_extra_item("Radar Scanner", cost, ZP_TEAM_HUMAN)
	
	for (new i = 1; i<=g_maxplayers;i++) player_has_radar[i] = false;	
	
	set_task (2.0,"radar_scan",_,_,_,"b");
}

public plugin_precache()
{
	precache_sound(sound_radar_beep)
}

public plugin_cfg()
{
	new cfgdir[32]
	get_localinfo("amxx_configsdir",cfgdir,sizeof cfgdir)
	server_cmd("exec %s/zp_radar.cfg", cfgdir)
}

public event_round_start()
{
	if (get_pcvar_num(cvar_zp_radar_beep) == 1)
		zp_radar_beep=true;
	else
		zp_radar_beep=false;
	
	if (get_pcvar_num(cvar_zp_radar_show_dead) == 1)
		zp_radar_show_dead=true;
	else
		zp_radar_show_dead=false;
}

public zp_extra_item_selected(player, itemid)
{
	if (itemid == g_itemid_radar) player_has_radar[player] = true;
}

public radar_scan()
{	
	new zombie_count = 0;
	new zombie_list[32];
	new ZombieCoords[3];
	new id, i;
	
	for (new id=1; id<=g_maxplayers; id++)
	{
		if (zp_radar_show_dead)
		{
			if (zp_get_user_zombie(id))
			{
				zombie_count++;
				zombie_list[zombie_count]=id;
			}
		}
		else
		{
			if ((zp_get_user_zombie(id))&&(is_user_alive(id)))
			{
				zombie_count++;
				zombie_list[zombie_count]=id;
			}
		}
	}
	
	for (id=1; id<=g_maxplayers; id++)
	{
		if ((!is_user_alive(id))||(!player_has_radar[id])) continue;
		
		for (i=1;i<=zombie_count;i++)
		{			
			get_user_origin(zombie_list[i], ZombieCoords)
		
			message_begin(MSG_ONE_UNRELIABLE, g_msgHostageAdd, {0,0,0}, id)
			write_byte(id)
			write_byte(i)		
			write_coord(ZombieCoords[0])
			write_coord(ZombieCoords[1])
			write_coord(ZombieCoords[2])
			message_end()
		
			message_begin(MSG_ONE_UNRELIABLE, g_msgHostageDel, {0,0,0}, id)
			write_byte(i)
			message_end()
		}
		if (zp_radar_beep) emit_sound(id, CHAN_ITEM, sound_radar_beep, 1.0, ATTN_NORM, 0, PITCH_NORM);
	}
}

public zp_user_infected_post(id, infector)
{
	player_has_radar[id] = false;
}

public fw_PlayerKilled(victim, attacker, shouldgib)
{	
	player_has_radar[victim] = false;
}

public zp_round_ended(winteam)
{
	if (get_pcvar_num(cvar_zp_radar_single_round)==1) 
		for (new id=1; id<=g_maxplayers; id++)
			player_has_radar[id] = false;
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1049\\ f0\\ fs16 \n\\ par }
*/
