#include <amxmodx>
#include <cstrike>
#include <fun>
#include <hamsandwich>
#include <fakemeta>
#include <engine>

#define VERSION "1.2.3"
#define OFFSET_PRIMARYWEAPON 116
#define IsPlayer(%1) (1 <= %1 <= g_iMaxPlayers) 

new bool: g_HasArrest[33]

new MaxCuffs[33]
new g_iMaxPlayers

new handcuffs_on, max_handcuffs, distance, delay

new const ARREST[] = "jail/arrestation/menotte.wav"
new const HANDCUFFS[] = "jail/arrestation/mainenlair.wav"

public plugin_init()
{
	register_plugin("Jailbreak Arrest", VERSION, "GuiSmoO'")

	register_event("HLTV", "NewRound", "a", "1=0", "2=0")
	
	register_clcmd("arrest", "commandestop")
	register_clcmd("stop", "commandestop")
	
	handcuffs_on = register_cvar("jail_handcuffs_on", "1")
	max_handcuffs = register_cvar("jail_max_handcuffs", "1")
	distance = register_cvar("jail_distance_handcuffs", "150")
	delay = register_cvar("jail_delay_respawn", "4")
	
	g_iMaxPlayers = get_maxplayers()
	
	register_dictionary( "jailbreak_arrest.txt")
}

public plugin_precache()
{
	precache_sound(ARREST)
	precache_sound(HANDCUFFS)	
}

public client_putinserver(id)
{
	g_HasArrest[id] = false
}

public NewRound()
{
	new Joueurs[32], Player, id
	get_players(Joueurs, Player)
	
	for(new i = 0; i < Player ; i++)
	{
		id = Joueurs[i] 
		g_HasArrest[id] = false
		
		if(cs_get_user_team(id) == CS_TEAM_CT && get_pcvar_num(handcuffs_on))
		{
			MaxCuffs[id] = get_pcvar_num(max_handcuffs)
			client_print(id, print_chat,"%L", id, "RECEIVE_HANDCUFFS", MaxCuffs[id])
			client_print(id, print_chat,"%L", id, "HELP_USE")
		}
	}
	return PLUGIN_CONTINUE;
}

public commandestop(id)
{
	new name[32], Username[32]
	new playerid, body
	
	new Float:flDistance = get_user_aiming(id, playerid, body)
	
	get_user_name(id,name, charsmax(name))
	get_user_name(playerid,Username, charsmax(Username))
	
	if(!get_pcvar_num(handcuffs_on))
		return PLUGIN_HANDLED;

	if(is_user_alive(id) && is_user_alive(playerid) && IsPlayer(playerid) && cs_get_user_team(id) == CS_TEAM_CT 
	&& cs_get_user_team(playerid) == CS_TEAM_T&& MaxCuffs[id] > 0 && flDistance <= get_pcvar_float(distance))
	{
		MaxCuffs[id]--
		client_cmd(id, "spk %s", ARREST)
		
		player_arrest(playerid)
		client_print(id, print_chat,"%L", id, "ARRESTS", name, Username)
		client_print(playerid, print_chat,"%L", playerid, "ARRESTED", name)
	}
	else if(MaxCuffs[id] == 0)
	{
		client_print(id, print_chat,"%L", id, "MAX_HANDCUFFS")
		return PLUGIN_HANDLED;
	}
	else if(flDistance > get_pcvar_float(distance))
	{
		client_print(id, print_chat,"%L", id, "PRISONNER_FAR")
		return PLUGIN_HANDLED;
	}
	return PLUGIN_CONTINUE;
}

public prison(playerid)
{
	if(g_HasArrest[playerid] == true)
	{
		reset_arrest(playerid)
	}
}

player_arrest(index)
{
	client_cmd(index, "spk %s", ARREST)
	set_entity_flags(index, FL_FROZEN, 1)
	set_user_godmode(index, 1)
	strip_user_weapons(index)
	set_pdata_int(index, OFFSET_PRIMARYWEAPON, 0)
	g_HasArrest[index] = true
	set_task(get_pcvar_float(delay), "prison", index)
}

reset_arrest(index)
{
	ExecuteHamB(Ham_CS_RoundRespawn, index)
	strip_user_weapons(index)
	set_pdata_int(index, OFFSET_PRIMARYWEAPON, 0)
	give_item(index, "weapon_knife")
	set_entity_flags(index, FL_FROZEN, 0)
	set_user_godmode(index, 0)
	g_HasArrest[index] = false
}