#include <amxmodx>
#include <engine>
#pragma reqlib "biohazardf"

new g_flashlight[33];

public plugin_init()
{
	register_plugin("FlashLight", "1.0", "RoleX")
	register_event("Flashlight", "event_flashlight", "b")
	register_event("ResetHUD", "event_resethud", "be")
	register_event("Damage", "event_damage", "b")
}

public event_resethud(id)
{
	g_flashlight[id] = false
}

native is_user_zombie(index)
public event_flashlight(id)
{
	if(is_user_zombie(id)) return
	
	if(g_flashlight[id])
	{
		g_flashlight[id] = 0;
	}
	else
	{
		g_flashlight[id] = 1;
	}

	message_begin(MSG_ONE,get_user_msgid("Flashlight"),{0,0,0},id);
	write_byte(g_flashlight[id]);
	write_byte(100);
	message_end();
	entity_set_int(id,EV_INT_effects,entity_get_int(id,EV_INT_effects) & ~EF_DIMLIGHT);
}

public event_damage(id)
{
	if(get_user_health(id) < 1)
	flashlight(id, false)
}

forward event_infect2(index)
public event_infect2(id)
{
	flashlight(id, false)
}

public client_PreThink(id)
{
	if(is_user_alive(id) && g_flashlight[id])
	{
		new origin[3];
		get_user_origin(id,origin,3);
		message_begin(MSG_BROADCAST,SVC_TEMPENTITY);
		write_byte(27);
		write_coord(origin[0]);
		write_coord(origin[1]);
		write_coord(origin[2]);
		write_byte(10);
		write_byte(150);
		write_byte(150);
		write_byte(150)
		write_byte(1);
		write_byte(10);
		message_end();
	}
}

stock flashlight(index, bool:status = false)
{
	g_flashlight[index] = status

	static msg_flashlight
	if(!msg_flashlight) msg_flashlight = get_user_msgid("Flashlight")
	
	message_begin(MSG_ONE, msg_flashlight, _, index)
	write_byte(status ? 1 : 0)
	write_byte(100)
	message_end()
}