#include <amxmodx>
#include <cstrike>
#include <fakemeta>

new cvar_enabled, cvar_fov, cvar_health, cvar_strip_weapons

new g_msgSetFOV

public plugin_init() 
{
	register_plugin("Crazy Syrup", "1.0", "ds811888")

	register_clcmd("say /buy_csy", "cmd_plugins_main")

	cvar_enabled 		= register_cvar("csy_enabled","1")
	cvar_fov 		= register_cvar("csy_fov", "110")
	cvar_health		= register_cvar("csy_health","2000")
	cvar_strip_weapons	= register_cvar("csy_strip_weapons","1")
}

public cmd_plugins_main(id)
{
	if(!get_pcvar_num(cvar_enabled))
		return PLUGIN_HANDLED

   	new money = cs_get_user_money(id)
   	new csy = 8000 // crazy syrup cost

	if(money <= csy)
	{
		client_print(id, print_center, "[M.L] You need 8000$ to buy!")
	}
	else
	{
	cs_set_user_money(id, money - csy)  	

	static fov
	fov = get_pcvar_num(cvar_fov)
	
	if (fov != 90 && fov != 0)
	{
		message_begin(MSG_ONE, g_msgSetFOV, _, id)
		write_byte(fov) // angle
		message_end()
	}

	// Add health
	fm_set_user_health(id, get_pcvar_num(cvar_health))

	// Strip off from weapons
	if (get_pcvar_num(cvar_strip_weapons))
	{
	fm_strip_user_weapons(id)
	fm_give_item(id, "weapon_knife")
	}

	client_print(id, print_chat, "[M.L] You buy a gene medicine!")
	}

	return PLUGIN_CONTINUE
}

// Strip user weapons (from fakemeta_util)
stock fm_strip_user_weapons(id)
{
	static ent
	ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "player_weaponstrip"))
	if (!pev_valid(ent)) return;
	
	dllfunc(DLLFunc_Spawn, ent)
	dllfunc(DLLFunc_Use, ent, id)
	engfunc(EngFunc_RemoveEntity, ent)
}

// Give an item to a player (from fakemeta_util)
stock fm_give_item(id, const item[])
{
	static ent
	ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, item))
	if (!pev_valid(ent)) return;
	
	static Float:originF[3]
	pev(id, pev_origin, originF)
	set_pev(ent, pev_origin, originF)
	set_pev(ent, pev_spawnflags, pev(ent, pev_spawnflags) | SF_NORESPAWN)
	dllfunc(DLLFunc_Spawn, ent)
	
	static save
	save = pev(ent, pev_solid)
	dllfunc(DLLFunc_Touch, ent, id)
	if (pev(ent, pev_solid) != save)
		return;
	
	engfunc(EngFunc_RemoveEntity, ent)
}

// Health (from fakemeta_util)
stock fm_set_user_health(index, health) {
	health > 0 ? set_pev(index, pev_health, float(health)) : dllfunc(DLLFunc_ClientKill, index);

	return 1;
}