#include <amxmodx>
#include <engine>
#include <biohazard>


stock ChatColor(const id, const input[], any:...)
{
	new count = 1, players[32]
	static msg[191]
	vformat(msg, 190, input, 3)
    
	replace_all(msg, 190, "!g", "^4") 
	replace_all(msg, 190, "!y", "^1") 
	replace_all(msg, 190, "!r", "^3") 
	replace_all(msg, 190, "!b", "^0")
    
	if (id) players[0] = id; else get_players(players, count, "ch")
	{
		for (new i = 0; i < count; i++)
		{
			if (is_user_connected(players[i]))
			{
				message_begin(MSG_ONE_UNRELIABLE, get_user_msgid("SayText"), {0,0,0}, players[i])
				write_byte(players[i]);
				write_string(msg);
				message_end();
			}
		}
	}
}

new g_radius, infect_me;

public plugin_init() 
{         
	register_plugin("L4D : Infect Me","1.0","Good_Hash")
	is_biomod_active() ? plugin_init2() : pause("ad")
}


public plugin_init2()
 {
	register_dictionary("biohazard.txt")
	
	register_clcmd("say /infectme","inf");
	register_clcmd("say /inf","inf");
	register_clcmd("say /izombie","inf");
	
	infect_me			 = register_cvar("l4d_infect_me", 		"1"					);
	g_radius			 = register_cvar("l4d_infect_me_radius", 		"350"					);
}


public inf(id)
{
	if ( get_pcvar_num(infect_me) < 1 )
		return PLUGIN_HANDLED;
	
	if(is_user_zombie(id))
	{
		ChatColor(id, "%L",id, "L4D_ALREADY_INFECTED" );
		return PLUGIN_HANDLED
	}
	
	if(!is_user_zombie(id) && L4D_Check_Infection_Rad(id))
		infect_user(id, 0)
	
	return PLUGIN_HANDLED;
}

stock L4D_Check_Infection_Rad(id)
{
	new bool:bad_place;
	new radius = get_pcvar_num(g_radius);
	
	for( new i = 1; i <= 33; i++ )
	{
		if( is_user_alive( i ) && !is_user_zombie( i ) )
		{
			new Distance; Distance = get_entity_distance( i, id )
			if( Distance <= radius && is_visible( id, i ) ) 
			{
				bad_place = true;
			}
		}
	}
	if ( bad_place)
		return false
	return true
}
