#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <hamsandwich>
#include <halomod>

#define PLUGIN "HaloMod Weapons"
#define VERSION HALOVERSION
#define AUTHOR "Soccdoodcss"

new pNades, gSpawn

public plugin_precache()
{
	gSpawn = register_forward(FM_Spawn, "fwd_spawn")
	RegisterHam(Ham_Touch, "armoury_entity", "block")
	RegisterHam(Ham_Touch, "weapon_shield", "block")
	RegisterHam(Ham_Touch, "weaponbox", "onTouch")
}

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)
	register_event("RoundTime", "onRound", "b")
	register_event("CurWeapon","onWeapon","be","1=1")
	
	unregister_forward(FM_Spawn, gSpawn)
	
	pNades = register_cvar("halo_nades", "4", FCVAR_SERVER)
}

public fwd_spawn(ent)
{
	if(!pev_valid(ent) || !is_halo_on()) 
		return FMRES_IGNORED
	
	static classname[32]
	pev(ent, pev_classname, classname, 31)
	
	if(equal(classname, "func_buyzone"))
	{
		set_pev(ent, pev_solid, SOLID_NOT)
		return FMRES_SUPERCEDE
	}
	
	return FMRES_IGNORED
}

public onTouch(ent, id)
{
	if(!is_user_alive(id) || !pev_valid(ent) || !is_halo_on())
		return HAM_HANDLED
	
	new model[33]
	pev(ent, pev_model, model, 32)
	
	if(!equal(model, "models/w_hegrenade.mdl") && !equal(model, "models/w_hegrenade_halo.mdl"))
		return HAM_HANDLED
	
	new nades = cs_get_user_bpammo(id, CSW_HEGRENADE)
	
	if(nades < 1 || nades >= get_pcvar_num(pNades))
		return HAM_HANDLED
	
	message_begin(MSG_ONE, get_user_msgid("WeapPickup"), {0,0,0}, id)
	write_byte(CSW_HEGRENADE)
	message_end()
	
	set_pev(ent, pev_flags, pev(ent, pev_flags) | FL_KILLME)
	
	cs_set_user_bpammo(id, CSW_HEGRENADE, nades + 1)
	
	return HAM_SUPERCEDE
}

public onRound(id)
{
	if(!is_user_alive(id) || !is_halo_on())
		return PLUGIN_HANDLED
	
	set_task(0.1, "giveWeaps", id)
	
	return PLUGIN_HANDLED
}

public onWeapon(id)
{
	if(is_user_alive(id) && is_halo_on())
	{
		new weapid = read_data(2)
		if(weapid != CSW_HEGRENADE && weapid != CSW_FLASHBANG && weapid != CSW_SMOKEGRENADE && weapid != CSW_KNIFE)
			cs_set_user_bpammo(id, weapid, 100)
	}
}

public giveWeaps(id)
{
	if(is_user_alive(id))
	{
		fm_strip_user_weapons(id)
		new weap[33], weapid
		for(new x = 0; x < HALOWEAPS; x++)
		{
			weapid = gWeapId[x]
			
			get_weaponname(weapid, weap, 32)
			bacon_give_weapon(id, weap)
		}
	}
}

public block()
{
	if(!is_halo_on())
		return HAM_HANDLED
	
	return HAM_SUPERCEDE
}

public blockbuyzone(ent)
{
	if(!is_halo_on())
		return HAM_HANDLED
	
	set_pev(ent, pev_solid, SOLID_NOT)
	return HAM_SUPERCEDE
}
