#include <amxmodx>
#include <amxmisc>
#include <engine>
#include <zombieplague>

#define KZ_LEVEL ADMIN_LEVEL_E			// Flag "q"
#define MAXPLAYERS 32

new bool:canusehook[32]
new bool:ishooked[32]
new hookorigin[32][3]
new Sbeam

new HumanHook[33],ZombieHook[33],HookTime[33]
public plugin_init()
{
	register_plugin("[ZP] Admin Hook","1.0","p4ddY / Chrescoe1")

	register_clcmd("+hook","hook_on",KZ_LEVEL)
	register_clcmd("-hook","hook_off",KZ_LEVEL)

	register_event("HLTV", "event_round_start", "a", "1=0", "2=0")
}
public event_round_start(id)
{
    	for (new i; i < MAXPLAYERS + 1; i++)
        	{
		HumanHook[i]=0
		ZombieHook[i]=0
		HookTime[i]=0

	}
}
public plugin_precache() 
{
	precache_sound("zombie_plague/hook.wav")
	Sbeam = precache_model("sprites/laserbeam.spr")
}

public client_disconnect(id)
	remove_hook(id)

public client_putinserver(id)
	remove_hook(id)

public hook_on(id,level,cid)
{
	if(!canusehook[id-1] && !cmd_access(id,level,cid,1))
		return PLUGIN_HANDLED
	if(zp_get_user_zombie(id))
		if(ZombieHook[id]<4)
		{
			ZombieHook[id]+=1
			client_print(id,print_center,"Вы можете использовать Hook ещё %d раз(а)",4 - ZombieHook[id])
		}
		else
			return PLUGIN_HANDLED
	else
		if(HumanHook[id]<8)
		{
			HumanHook[id]+=1
			client_print(id,print_center,"Вы можете использовать Hook ещё %d раз(а)",8 - HumanHook[id])
		}
		else
			return PLUGIN_HANDLED

	get_user_origin(id,hookorigin[id-1],3)

	if(callfunc_begin("detect_cheat","prokreedz.amxx") == 1)
	{
		callfunc_push_int(id)
		callfunc_push_str("Hook")
		callfunc_end()
	}

	ishooked[id-1] = true

	emit_sound(id,CHAN_STATIC,"zombie_plague/hook.wav",1.0,ATTN_NORM,0,PITCH_NORM)
	HookTime[id]=100
	set_task(0.1,"hook_task",id,"",0,"ab")
	hook_task(id)

	return PLUGIN_HANDLED
}

public is_hooked(id)
{
	return ishooked[id-1]
}

public hook_off(id)
{
	remove_hook(id)

	return PLUGIN_HANDLED
}

public hook_task(id)
{
	if(!is_user_connected(id) || !is_user_alive(id)||HookTime[id]==0)
	{
		remove_hook(id)
		remove_beam(id)
	}
	else
	{
		remove_beam(id)
		draw_hook(id)
	}
	HookTime[id]-=1
	new origin[3], Float:velocity[3]
	get_user_origin(id,origin)
	new distance = get_distance(hookorigin[id-1],origin)
	if(distance > 25)
	{
		velocity[0] = (hookorigin[id-1][0] - origin[0]) * (2.0 * 300 / distance)
		velocity[1] = (hookorigin[id-1][1] - origin[1]) * (2.0 * 300 / distance)
		velocity[2] = (hookorigin[id-1][2] - origin[2]) * (2.0 * 300 / distance)

		entity_set_vector(id,EV_VEC_velocity,velocity)
	}
	else
	{
		entity_set_vector(id,EV_VEC_velocity,Float:{0.0,0.0,0.0})
		remove_hook(id)
	}
}

public draw_hook(id)
{
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
	write_byte(1)
	write_short(id)
	write_coord(hookorigin[id-1][0])
	write_coord(hookorigin[id-1][1])
	write_coord(hookorigin[id-1][2])
	write_short(Sbeam)
	write_byte(0)
	write_byte(0)
	write_byte(100)
	write_byte(10)
	write_byte(0)
	if(zp_get_user_zombie(id)) {
		write_byte(255)
		write_byte(0)
		write_byte(0)
	}
	else {
		write_byte(0)
		write_byte(255)
		write_byte(0)
	}
	write_byte(150)
	write_byte(0)
	message_end()
}

public remove_hook(id)
{
	if(task_exists(id))
		remove_task(id)
	remove_beam(id)
	ishooked[id-1] = false
}

public remove_beam(id)
{
	message_begin(MSG_BROADCAST,SVC_TEMPENTITY)
	write_byte(99)
	write_short(id)
	message_end()
}