#include <amxmodx>
#include <fakemeta>
#include <fakemeta_util>
#include <cstrike>
#include <hamsandwich>
#include <zombieplague>

#define PLUGIN "Frag Reward"
#define VERSION "0.1.0"
#define AUTHOR "PomanoB"

#if cellbits == 32
const OFFSET_CLIPAMMO = 51
#else
const OFFSET_CLIPAMMO = 65
#endif
const OFFSET_LINUX_WEAPONS = 4

new Array:g_rewards
new g_rewardCount

enum _:FRAG_DATA
{
	FRAG_COUNT,
	FRAG_WEAPON[32],
	FRAG_AMMO
}

public plugin_init() 
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	
	RegisterHam(Ham_CS_RoundRespawn, "player", "fwdSpawn", 1)
	
	register_srvcmd("set_frag_reward", "cmdSetReward")
	
	g_rewards = ArrayCreate(FRAG_DATA)
}

public plugin_end()
{
	ArrayDestroy(g_rewards)
}

public cmdSetReward()
{
	new arg[32]
	
	new d[FRAG_DATA]
	
	read_argv(1, arg, 31)
	d[FRAG_COUNT] = str_to_num(arg)
	
	read_argv(2, d[FRAG_WEAPON], 31)
	
	read_argv(3, arg, 31)
	d[FRAG_AMMO] = str_to_num(arg)
	
	ArrayPushArray(g_rewards, d)
	
	g_rewardCount++
}

public fwdSpawn(id)
{
	if( zp_get_user_zombie( id ) || zp_get_user_nemesis( id ) || zp_get_user_survivor( id ) )
		return

	if (is_user_alive(id))
	{
		new frags = get_user_frags(id)
		new i, d[FRAG_DATA], lastFrag = -1
		for(i = 0; i < g_rewardCount; i++)
		{
			ArrayGetArray(g_rewards, i, d)
			if (frags >= d[FRAG_COUNT] && (lastFrag == d[FRAG_COUNT] || lastFrag == -1))
			{
				fm_give_item(id, d[FRAG_WEAPON])
				
				if (d[FRAG_AMMO])
					cs_set_user_bpammo(id, get_weaponid(d[FRAG_WEAPON]), d[FRAG_AMMO]) 
			
				if (lastFrag == -1)
					lastFrag = d[FRAG_COUNT]
			}
		}
	}
}