/*	Copyright © 2009, ConnorMcLeod

	Unlimited Ammo is free software;
	you can redistribute it and/or modify it under the terms of the
	GNU General Public License as published by the Free Software Foundation.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Unlimited Ammo; if not, write to the
	Free Software Foundation, Inc., 59 Temple Place - Suite 330,
	Boston, MA 02111-1307, USA.
*/

#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>

#define PLUGIN "Unlimited Ammo"
#define AUTHOR "ConnorMcLeod"
#define VERSION "0.1.0"

#define MAX_PLAYERS 32

#define m_iId 43
#define m_iClip 51
#define m_iClientClip 52

#define m_pActiveItem 373

const NOCLIP_WPN_BS	= ((1<<2)|(1<<CSW_HEGRENADE)|(1<<CSW_SMOKEGRENADE)|(1<<CSW_FLASHBANG)|(1<<CSW_KNIFE)|(1<<CSW_C4))
const NOCROSSHAIR_WPN_BS = ((1<<CSW_SCOUT)|(1<<CSW_AWP)|(1<<CSW_G3SG1)|(1<<CSW_SG550))

new gmsgCurWeapon, gmsgHideWeapon, g_iPlayer, g_iHide
new g_iFhWriteByte
new g_iFhMessageEnd
new g_iMaxPlayers
new g_iTask
new g_iSyncHud

new g_bIsAlive[MAX_PLAYERS+1]

public plugin_init()
{
	register_plugin(PLUGIN, VERSION, AUTHOR)

	new szWeapon[17]
	for(new i=1; i<=CSW_P90; i++)
	{
		if( !(NOCLIP_WPN_BS & (1<<i)) && get_weaponname(i, szWeapon, charsmax(szWeapon)) )
		{
			RegisterHam(Ham_Item_UpdateClientData, szWeapon, "Weapon_UpdateClientData")
		}
	}

	RegisterHam(Ham_Spawn, "player", "Player_Spawn", 1)
	RegisterHam(Ham_Killed, "player", "Player_Killed", 1)

	register_forward(FM_MessageBegin, "MessageBegin")
	gmsgCurWeapon = get_user_msgid("CurWeapon")
	gmsgHideWeapon = get_user_msgid("HideWeapon")

	new iMaxEnts = global_get(glb_maxEntities)
	new szClassName[2]

	g_iMaxPlayers = get_maxplayers()    
	for(new iEnt=g_iMaxPlayers+1; iEnt<iMaxEnts; iEnt++)
	{
		if( pev_valid(iEnt) )
		{
			pev(iEnt, pev_classname, szClassName, charsmax(szClassName))
			if( !szClassName[0] )
			{
				g_iTask = iEnt
				RegisterHamFromEntity(Ham_Think, g_iTask, "Print_Unlimited", 1)
				break
			}
		}
	}

	g_iSyncHud = CreateHudSyncObj()
}

public Player_Spawn( id )
{
	g_bIsAlive[id] = is_user_alive(id)
}

public Player_Killed( id )
{
	g_bIsAlive[id] = is_user_alive(id)
}

public client_disconnect( id )
{
	g_bIsAlive[id] = false
}

public Weapon_UpdateClientData(iEnt, id)
{
	static iClip, iClientClip
	iClip = get_pdata_int(iEnt, m_iClip, 4)
	iClientClip = get_pdata_int(iEnt, m_iClientClip, 4)

	if( iClip < iClientClip )
	{
		set_pdata_int(iEnt, m_iClip, iClientClip, 4)
		return HAM_HANDLED
	}
	return HAM_IGNORED
}

public MessageBegin(MSG_DEST, iMsgId, Float:fOrigin[3], id)
{
	if( iMsgId == gmsgCurWeapon )
	{
		g_iFhWriteByte = register_forward(FM_WriteByte, "WriteByte")
		g_iPlayer = id
	}
}

public WriteByte(iByte)
{
	static iCount, iActive

	switch( ++iCount )
	{
		case 1:
		{
			iActive = iByte
		}
		case 2:
		{
			if( iActive )
			{
				if( NOCROSSHAIR_WPN_BS & (1<<iByte) )
				{
					g_iHide = 64
					g_iFhMessageEnd = register_forward(FM_MessageEnd, "MessageEnd", 1)
				}
				else
				{
					g_iHide = 0
					g_iFhMessageEnd = register_forward(FM_MessageEnd, "MessageEnd", 1)
				}
			}
			write_byte(CSW_KNIFE)
			return FMRES_SUPERCEDE
		}
		case 3:
		{
			iCount = 0		

			unregister_forward(FM_WriteByte, g_iFhWriteByte)

			write_byte(0)
			return FMRES_SUPERCEDE
		}
	}

	return FMRES_IGNORED
}

public MessageEnd()
{
	unregister_forward(FM_MessageEnd, g_iFhMessageEnd, 1)

	message_begin(MSG_ONE_UNRELIABLE, gmsgHideWeapon, _, g_iPlayer)
	write_byte(g_iHide)
	message_end()
}

public Print_Unlimited( iEnt )
{
	if( iEnt != g_iTask )
	{
		return
	}

	set_hudmessage(100, 72, 12, 0.90, 0.95, 0, 0.000001, 0.31, 0.000001, 0.000001, 1)

	static id, iWeapon, iWeaponId
	for(id=1; id<=g_iMaxPlayers; id++)
	{
		if( g_bIsAlive[id] )
		{
			iWeapon = get_pdata_cbase(id, m_pActiveItem)
			if( iWeapon > 0 )
			{
				iWeaponId = get_pdata_int(iWeapon, m_iId, 4)
				if( !( NOCLIP_WPN_BS & (1<<iWeaponId) ) )
				{
					ShowSyncHudMsg(id, g_iSyncHud, "unlimited")
				}
			}
		}
	}
}