#include <amxmodx>
#include <hamsandwich>
#tryinclude <biohazard>

#if !defined _biohazard_included
        #assert Biohazard functions file required!
#endif

#define TASKID_CHECK 124
#define TASKID_RADAR 531
#define HIDE_CROSSHAIR (1<<6)

new cvar_bodyexplodes, cvar_infectblood, cvar_radar, cvar_nocrosshair, cvar_zombie_vision, g_maxplayers
new bool:g_nocrosshair[33]

public plugin_init()
{
	register_plugin("misc addons", "0.4", "cheap_suit")
	is_biomod_active() ? plugin_init2() : pause("ad")
}

public plugin_init2()
{
	RegisterHam(Ham_Killed, "player", "bacon_killed")
	RegisterHam(Ham_Spawn, "player",  "bacon_spawn_post", 1)
	register_message(get_user_msgid("ClCorpse"), "msg_clcorpse")
	register_event("HLTV",   "event_newround", "a", "1=0", "2=0")
	register_event("Damage", "event_damage",   "b")
	
	cvar_radar 	   = register_cvar("bh_zombie_radar",   "1")
	cvar_bodyexplodes  = register_cvar("bh_zombie_explode", "1")
	cvar_zombie_vision = register_cvar("bh_zombie_vision",  "120")
	cvar_infectblood   = register_cvar("bh_infect_blood",   "1")
	cvar_nocrosshair  = register_cvar("bh_nocrosshair",    "1")
	g_maxplayers 	  = get_maxplayers()
}

public bacon_spawn_post(id)
{
	if(is_user_alive(id) && g_nocrosshair[id])
		message_hideweapon(id, 0)
}

public msg_clcorpse()
{
	if(!get_pcvar_num(cvar_bodyexplodes)) return PLUGIN_CONTINUE
	
	static id; id = get_msg_arg_int(12)
	return (is_user_connected(id) && is_user_zombie(id)) ? PLUGIN_HANDLED : PLUGIN_CONTINUE
}

public bacon_killed(victim, attacker, shouldgib)
{
	if(is_user_zombie(victim) && get_pcvar_num(cvar_bodyexplodes))
	{
		SetHamParamInteger(3, 2)
		return HAM_HANDLED
	}
	return HAM_IGNORED
}

public event_infect2(id)
{
	if(get_pcvar_num(cvar_infectblood))
	{
		static origin[3]; get_user_origin(id, origin)
		message_begin(MSG_BROADCAST, SVC_TEMPENTITY)
		write_byte(TE_PARTICLEBURST)
		write_coord(origin[0])
		write_coord(origin[1])
		write_coord(origin[2])
		write_short(50)
		write_byte(70)
		write_byte(3)
		message_end()
	}
	
	static vision; vision = get_pcvar_num(cvar_zombie_vision)
	if(vision)
	{
		if(vision < 90) vision = 90
	
		static msg_setfov
		if(!msg_setfov) msg_setfov = get_user_msgid("SetFOV")
		
		message_begin(MSG_ONE, msg_setfov, _, id)
		write_byte(vision)
		message_end()
	}
	
	if(get_pcvar_num(cvar_radar))
	{
		remove_task(TASKID_CHECK)
		set_task(1.0, "task_check", TASKID_CHECK)
	}
	
	if(get_pcvar_num(cvar_nocrosshair))
		message_hideweapon(id, HIDE_CROSSHAIR), g_nocrosshair[id] = true
}

public event_newround() 
{
	remove_task(TASKID_CHECK)
	remove_task(TASKID_RADAR)
}

public client_disconnect(id)
{
	remove_task(TASKID_CHECK)
	set_task(1.0, "task_check", TASKID_CHECK)
}

public event_damage(id)
{
	if(get_user_health(id) < 1 && !is_user_zombie(id))
	{
		remove_task(TASKID_CHECK)
		set_task(1.0, "task_check", TASKID_CHECK)
	}
}

public task_check()
{
	static survivor; survivor = last_survivor()
	if(survivor) 
	{
		static param[1]; param[0] = survivor
		set_task(1.0, "task_radar", TASKID_RADAR, param, 1)
	}
}

public task_radar(param[])
{
	static id; id = param[0]
	if(!is_user_alive(id))
	{
		static msg_bombpickup
		if(!msg_bombpickup) msg_bombpickup = get_user_msgid("BombPickup")
		
		message_begin(MSG_ALL, msg_bombpickup)
		message_end()
		
		return
	}

	static origin[3]
	get_user_origin(id, origin)
	
	static msg_bombdrop
	if(!msg_bombdrop) msg_bombdrop = get_user_msgid("BombDrop")
	
	message_begin(MSG_ALL, msg_bombdrop)
	write_coord(origin[0])
	write_coord(origin[1])
	write_coord(origin[2])
	write_byte(0)
	message_end()
	
	set_task(1.0, "task_radar", TASKID_RADAR, param, 1)
}

stock last_survivor()
{
	static id, count, survivor[33]; count = 0
	for(id = 1; id <= g_maxplayers; id++) if(is_user_alive(id) && !is_user_zombie(id)) survivor[count++] = id
	return count == 1 ? survivor[0] : 0
}

stock message_hideweapon(id, flag)
{
	static msg_hideweapon
	if(!msg_hideweapon) msg_hideweapon = get_user_msgid("HideWeapon")
		
	message_begin(MSG_ONE, msg_hideweapon, _, id)
	write_byte(flag)
	message_end()
}