/*================================================
Name: Quark Zombie
Description: Can Make a Trap. And Trap the Human.
Author: Dias
For: Zombie Plague Mod 4.3
================================================*/

#include <amxmodx>
#include <fakemeta>
#include <engine>
#include <fun>
#include <zombieplague>

new const zclass_name[] = "Quark Zombie"
new const zclass_info[] = "(G) To Put a Trap"
new const zclass_model[] = "zombie_classic1"
new const zclass_clawmodel[] = "v_knife_classic1.mdl"
const zclass_health = 2000
const zclass_speed = 250
const Float:zclass_gravity = 1.0
const Float:zclass_knockback = 1.0

// Main Trap Vars
new g_zquark
new bool:can_make_trap[33]
new bool:player_trapped[33]

new const trap_string[] = "trap"
new const trap_model[] = "models/zombie_plague/zombie_trap.mdl"

new cvar_cooldown
new cvar_trap_hp
new cvar_trap_time

public plugin_init()
{
	register_plugin("[ZP] Zombie Class: Quark", "1.2", "Dias")
	register_clcmd("drop", "use_skill")
	register_logevent("event_roundend", 2, "1=Round_End")
	register_touch(trap_string, "*", "fw_touch")
	register_forward(FM_PlayerPreThink, "fw_think", 0);

	cvar_register()
}

public cvar_register()
{
	cvar_cooldown = register_cvar("qz_cooldown", "45")
	cvar_trap_time = register_cvar("qz_trap_time", "25")
	cvar_trap_hp = register_cvar("qz_trap_hp", "2500")
}

public plugin_precache()
{
	g_zquark = zp_register_zombie_class(zclass_name, zclass_info, zclass_model, zclass_clawmodel, zclass_health, zclass_speed, zclass_gravity, zclass_knockback)	
	precache_model(trap_model)
}

public event_roundend(id)
{
	can_make_trap[id] = false
	player_trapped[id] = false
	
	remove_entity_name(trap_string)
}

public zp_user_infected_post(victim, attacker)
{
	if(zp_get_user_zombie_class(victim) == g_zquark)
	{
		client_print(victim, print_chat, "[Quark Zombie] Press (G) to Put a Trap !!!")
		can_make_trap[victim] = true
	}
	if(player_trapped[victim] == true)
	{
		player_trapped[victim] = false
		remove_entity_name(trap_string)
		remove_task(victim)
	}
}

public use_skill(id)
{
	if(is_user_alive(id) && zp_get_user_zombie(id) && zp_get_user_zombie_class(id) == g_zquark)
	{
		if(can_make_trap[id])
		{
			create_trap(id)
			} else {
			client_print(id, print_chat, "[Quark Zombie] You can't put the trap Now. Please Wait For %i", get_pcvar_num(cvar_cooldown))
		}
	}
}

public create_trap(id)
{
	new Float:Origin[3]
	entity_get_vector(id, EV_VEC_origin, Origin)
	
	Origin[2] -= 35.0
	
	new trap = create_entity("info_target")
	entity_set_vector(trap, EV_VEC_origin, Origin)
	//Origin[2] += 70.0
	//entity_set_vector(id, EV_VEC_origin, Origin)
	
	entity_set_float(trap, EV_FL_takedamage, 1.0)
	entity_set_float(trap, EV_FL_health, get_pcvar_float(cvar_trap_hp))
	
	entity_set_string(trap, EV_SZ_classname, trap_string)
	entity_set_model(trap, trap_model)	
	entity_set_int(trap, EV_INT_solid, 1)
	
	entity_set_byte(trap,EV_BYTE_controller1,125);
	entity_set_byte(trap,EV_BYTE_controller2,125);
	entity_set_byte(trap,EV_BYTE_controller3,125);
	entity_set_byte(trap,EV_BYTE_controller4,125);
	
	new Float:size_max[3] = {5.0,5.0,5.0}
	new Float:size_min[3] = {-5.0,-5.0,-5.0}
	entity_set_size(trap, size_min, size_max)
	
	entity_set_float(trap, EV_FL_animtime, 2.0)
	entity_set_float(trap, EV_FL_framerate, 1.0)
	entity_set_int(trap, EV_INT_sequence, 0)
	
	drop_to_floor(trap)
	
	can_make_trap[id] = false
	set_task(get_pcvar_float(cvar_cooldown), "reset_cooldown", id)
}

public reset_cooldown(id)
{
	if(is_user_alive(id) && zp_get_user_zombie(id) && zp_get_user_zombie_class(id) == g_zquark)
	{
		if(can_make_trap[id] == false)
		{
			can_make_trap[id] = true
			client_print(id, print_chat, "[Quark Zombie] Now you can use your ability. Press (G)")
		}
	}	
}

public fw_touch(trap, id)
{
	if(!pev_valid(trap))
		return	
	
	if(is_user_alive(id) && !zp_get_user_zombie(id))
	{
		new ent = find_ent_by_class(0, trap_string)
		entity_set_int(ent, EV_INT_sequence, 1)
		
		player_trapped[id] = true
		set_task(get_pcvar_float(cvar_trap_time), "remove_trap", id)
	}
}

public remove_trap(id)
{
	set_user_maxspeed(id, -1.0)
	set_user_gravity(id, 1.0)
	player_trapped[id] = false
	
	new ent = find_ent_by_class(0, trap_string)
	
	remove_entity(ent)
	remove_task(id)
	client_print(id, print_chat, "Now. You are out of Trap")
}

public spawn_post(id)
{
    if(is_user_alive(id))
    {
         player_trapped[id] = false
         
    }
}  

public fw_think(id)
{
	if(is_user_alive(id) && player_trapped[id] == true)
	{
		set_user_maxspeed(id, 0.1)
		set_user_gravity(id, 10000.0)
	}
}
