/*================================================================================

	-----------------------------------
	-*- [ZP] Extra Item: Unlimited Clip -*-
	-----------------------------------

	Humans who buy this item keep both their backpack ammo AND their weapon
	clip full for the rest of the round. No reloading needed.

================================================================================*/

#include <amxmodx>
#include <cstrike>
#include <fakemeta>
#include <hamsandwich>
#include <engine>
#include <zombie_plague_special>

#define PLUGIN_NAME		"[ZP] Extra Item: Unlimited Clip"
#define PLUGIN_VERSION		"1.1"
#define PLUGIN_AUTHOR		"Kimi Code"

#define ITEM_NAME		"Unlimited Clip"
#define ITEM_COST		12
#define ITEM_TEAM		ZP_TEAM_HUMAN

new g_iItemID
new bool:g_bUnlimited[MAX_PLAYERS + 1]

public plugin_init()
{
	register_plugin(PLUGIN_NAME, PLUGIN_VERSION, PLUGIN_AUTHOR)
	g_iItemID = zp_register_extra_item(ITEM_NAME, ITEM_COST, ITEM_TEAM)

	register_forward(FM_PlayerPostThink, "fw_player_postthink")
	register_event("CurWeapon", "event_cur_weapon", "be", "1=1", "2!29")
	register_event("HLTV", "event_round_start", "a", "1=0", "2=0")
}

public event_round_start()
{
	for(new i = 1; i <= MAX_PLAYERS; i++)
		g_bUnlimited[i] = false
}

public client_connect(id)
	g_bUnlimited[id] = false

public client_disconnected(id)
	g_bUnlimited[id] = false

public zp_extra_item_selected(id, itemid)
{
	if(itemid != g_iItemID)
		return PLUGIN_CONTINUE

	if(zp_get_user_zombie(id))
	{
		client_print(id, print_chat, "[ZP] Zombies can't buy this item.")
		return PLUGIN_HANDLED
	}

	g_bUnlimited[id] = true
	refill_weapon(id)
	client_print(id, print_chat, "[ZP] You bought Unlimited Clip. No reloading this round.")
	return PLUGIN_HANDLED
}

public zp_user_infected_post(id, infector)
	g_bUnlimited[id] = false

public event_cur_weapon(id)
{
	if(g_bUnlimited[id] && is_user_alive(id) && !zp_get_user_zombie(id))
	{
		refill_weapon(id)
		// Stop the reload animation if one was already started
		new iEnt = fm_get_user_weapon_entity(id, get_user_weapon(id))
		if(pev_valid(iEnt))
		{
			new Float:flTime = get_gametime()
			set_pdata_float(iEnt, 48, flTime, 4) // m_flTimeWeaponIdle
		}
	}
}

public fw_player_postthink(id)
{
	if(!g_bUnlimited[id] || !is_user_alive(id) || zp_get_user_zombie(id))
		return FMRES_IGNORED

	// Stop the player from manually pressing reload
	if(pev(id, pev_button) & IN_RELOAD)
		set_pev(id, pev_button, pev(id, pev_button) & ~IN_RELOAD)

	refill_weapon(id)
	return FMRES_IGNORED
}

refill_weapon(id)
{
	new iWeapon = get_user_weapon(id)
	if(iWeapon <= 0 || iWeapon == CSW_KNIFE || iWeapon == CSW_HEGRENADE || iWeapon == CSW_FLASHBANG || iWeapon == CSW_SMOKEGRENADE || iWeapon == CSW_C4)
		return

	new iEnt = fm_get_user_weapon_entity(id, iWeapon)
	if(!pev_valid(iEnt))
		return

	// Keep a huge clip so the weapon never runs dry and never triggers a reload
	new iMaxClip = get_max_clip(iWeapon)
	if(iMaxClip > 0)
		cs_set_weapon_ammo(iEnt, 999)

	new iMaxBP = get_max_bpammo(iWeapon)
	if(iMaxBP > 0)
		cs_set_user_bpammo(id, iWeapon, iMaxBP)
}

stock fm_get_user_weapon_entity(id, weaponid)
{
	static classname[24]
	switch(weaponid)
	{
		case CSW_P228: classname = "weapon_p228"
		case CSW_SCOUT: classname = "weapon_scout"
		case CSW_XM1014: classname = "weapon_xm1014"
		case CSW_MAC10: classname = "weapon_mac10"
		case CSW_AUG: classname = "weapon_aug"
		case CSW_ELITE: classname = "weapon_elite"
		case CSW_FIVESEVEN: classname = "weapon_fiveseven"
		case CSW_UMP45: classname = "weapon_ump45"
		case CSW_SG550: classname = "weapon_sg550"
		case CSW_GALIL: classname = "weapon_galil"
		case CSW_FAMAS: classname = "weapon_famas"
		case CSW_USP: classname = "weapon_usp"
		case CSW_GLOCK18: classname = "weapon_glock18"
		case CSW_AWP: classname = "weapon_awp"
		case CSW_MP5NAVY: classname = "weapon_mp5navy"
		case CSW_M249: classname = "weapon_m249"
		case CSW_M3: classname = "weapon_m3"
		case CSW_M4A1: classname = "weapon_m4a1"
		case CSW_TMP: classname = "weapon_tmp"
		case CSW_G3SG1: classname = "weapon_g3sg1"
		case CSW_DEAGLE: classname = "weapon_deagle"
		case CSW_SG552: classname = "weapon_sg552"
		case CSW_AK47: classname = "weapon_ak47"
		case CSW_P90: classname = "weapon_p90"
		default: return 0
	}

	return find_ent_by_owner(-1, classname, id)
}

stock get_max_clip(weaponid)
{
	switch(weaponid)
	{
		case CSW_P228: return 13
		case CSW_SCOUT: return 10
		case CSW_XM1014: return 7
		case CSW_MAC10: return 30
		case CSW_AUG: return 30
		case CSW_ELITE: return 30
		case CSW_FIVESEVEN: return 20
		case CSW_UMP45: return 25
		case CSW_SG550: return 30
		case CSW_GALIL: return 35
		case CSW_FAMAS: return 25
		case CSW_USP: return 12
		case CSW_GLOCK18: return 20
		case CSW_AWP: return 10
		case CSW_MP5NAVY: return 30
		case CSW_M249: return 100
		case CSW_M3: return 8
		case CSW_M4A1: return 30
		case CSW_TMP: return 30
		case CSW_G3SG1: return 20
		case CSW_DEAGLE: return 7
		case CSW_SG552: return 30
		case CSW_AK47: return 30
		case CSW_P90: return 50
	}
	return 0
}

stock get_max_bpammo(weaponid)
{
	switch(weaponid)
	{
		case CSW_P228: return 52
		case CSW_SCOUT: return 90
		case CSW_HEGRENADE: return 1
		case CSW_XM1014: return 32
		case CSW_C4: return 1
		case CSW_MAC10: return 100
		case CSW_AUG: return 90
		case CSW_SMOKEGRENADE: return 1
		case CSW_ELITE: return 120
		case CSW_FIVESEVEN: return 100
		case CSW_UMP45: return 100
		case CSW_SG550: return 90
		case CSW_GALIL: return 90
		case CSW_FAMAS: return 90
		case CSW_USP: return 100
		case CSW_GLOCK18: return 120
		case CSW_AWP: return 30
		case CSW_MP5NAVY: return 120
		case CSW_M249: return 200
		case CSW_M3: return 32
		case CSW_M4A1: return 90
		case CSW_TMP: return 120
		case CSW_G3SG1: return 90
		case CSW_FLASHBANG: return 2
		case CSW_DEAGLE: return 35
		case CSW_SG552: return 90
		case CSW_AK47: return 90
		case CSW_KNIFE: return 0
		case CSW_P90: return 100
	}
	return 0
}
