#include <amxmodx>
#include <fun>
#include <cstrike>

new bool:g_hasC4[33]
new PcvarCost
static const PLUGIN_NAME[] = "Buy C4"
static const PLUGIN_AUTHOR[] = "Locks"
static const PLUGIN_VERSION[] = "1.2"

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_AUTHOR, PLUGIN_VERSION)
	register_concmd("say buy_c4", "buy_c4")
	PcvarCost = register_cvar("amx_c4_cost", "6000")
	register_event("DeathMsg", "death", "a")
}

public buy_c4(id)
{
	new CsTeams:team = cs_get_user_team(id)

	if ( team == CS_TEAM_CT )
	{
		client_print(id, print_chat, "[AMXX] Must be a Terrorist in order to purchase a C4.")
		return PLUGIN_HANDLED
	}

	if ( g_hasC4[id] )
	{
		client_print(id, print_chat, "[AMXX] You already have a C4.")
	}

	if ( !is_user_alive(id) )
	{
		client_print(id,print_chat,"[AMXX] Dead clients are not allow to buy a C4.")
		return PLUGIN_HANDLED
	}

	if ( !cs_get_user_buyzone(id) )
	{
		client_print(id, print_chat, "[AMXX] Must be in the buyzone to purchase a c4.")
		return PLUGIN_HANDLED
	}

	new money = cs_get_user_money(id)
	new cost = get_pcvar_num(PcvarCost)

	if ( money < cost )
	{
		client_print(id, print_chat, "[AMXX] You don't have enough money to buy a C4. ($%i needed).", cost)
		return PLUGIN_CONTINUE
	}

	give_item(id, "weapon_c4")
	cs_set_user_money(id, money - cost)
	client_print(id, print_chat, "[AMXX] You have purchased a C4.")
	cs_set_user_plant(id, 1, 1)
	g_hasC4[id] = true

	return PLUGIN_CONTINUE
}

public client_connect(id)
{
	g_hasC4[id] = false
	return PLUGIN_HANDLED
}

public client_disconnect(id)
{
	g_hasC4[id] = false
	return PLUGIN_HANDLED
}

public death()
{
	new id = read_data(2)
	g_hasC4[id] = false
}