/*
				[ZP] Extra Item : Buy Ammo Packs
				
						by Fry!
						
						
	Description :
			
			You can buy some ammo packs (depends on cvar) for some Ca$h (depends on cvar).
			
	
	Cvars :
	
			zp_ammo_packs_cost "5000" - How much $ will cost ammo packs
			zp_ammo_packs_value "3"	  - How much packs you will get.
			
			
	Commands :
	
			say /buyammo or say_team /buyammo
			
	
	Credits :
	
			OzOWizard - For Idea
			
			
	Changelog :
	
			30/01/2009 - v1.0 - First release
			30/01/2009 - v1.0.1 - Fixed chat bug.
*/

#include <amxmodx>
#include <fakemeta>
#include <zombieplague>

#define PLUGIN "[ZP] Extra Item : Buy Ammo Packs"
#define VERSION "1.0"
#define AUTHOR "Fry!"

#define OFFSET_MONEY 115
#define OFFSET_LINUX 5

new g_ammo_cost, g_ammo_value
new MsgID_Money

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	register_cvar("zp_extra_buy_ammo_packs",VERSION,FCVAR_SERVER|FCVAR_EXTDLL|FCVAR_UNLOGGED|FCVAR_SPONLY)
	
	MsgID_Money = get_user_msgid("Money")
	
	g_ammo_cost = register_cvar("zp_ammo_packs_cost", "5000")
	g_ammo_value = register_cvar("zp_ammo_packs_value", "3")
	
	register_clcmd("say /buyammo", "buy_ammo")
	register_clcmd("say_team /buyammo", "buy_ammo")
	
	register_event("DeathMsg", "Dead", "a")
}

public Dead()
{
	new i = read_data(2)
	set_task(1.0, "plugin_msg", i)
}

public plugin_msg(id)
{
	client_print(id, print_chat, "[ZP] This server is running Buy Ammo Packs with $$$")
	client_print(id, print_chat, "[ZP] To buy Ammo Packs say /buyammo")
}

public buy_ammo(id)
{
	if (!is_user_alive(id))
	{
		client_print(id, print_chat, "[ZP] You can't buy ammo's if you are Dead")
		return PLUGIN_HANDLED
	}
	
	new user_cash = fm_get_user_money(id)
	new money_cost = get_pcvar_num(g_ammo_cost)
	
	new user_ammo = zp_get_user_ammo_packs(id)
	
	if (user_cash < money_cost)
	{
		client_print(id, print_chat, "[ZP] You don't have enough money to buy Ammo Pack[s]", money_cost)
		return PLUGIN_HANDLED
	}
	
	fm_set_user_money(id, user_cash - money_cost)
	zp_set_user_ammo_packs(id, user_ammo + get_pcvar_num(g_ammo_value))
	client_print(id, print_chat, "[ZP] You bought some ammo packs for $$$")
	
	return PLUGIN_CONTINUE
}
	
stock fm_set_user_money(index, money, flash=1)
{
	set_pdata_int(index, OFFSET_MONEY, money, OFFSET_LINUX)

	message_begin(MSG_ONE, MsgID_Money, {0,0,0}, index)
	write_long(money)
	write_byte(flash)
	message_end()
}

stock fm_get_user_money(index)
{
	return get_pdata_int(index, OFFSET_MONEY, OFFSET_LINUX)
} 
