#include <amxmodx>
#include <zombieplaguenew1.3>

#define PLUGIN "[ZP] Extra Item: Become Sniper"
#define VERSION "0.0.1"
#define AUTHOR "none"

new g_sniper
new g_msgSayText
new g_maxplayers

new pcvar_enabled, pcvar_cost, pcvar_hudtime

public plugin_init()
{
	register_plugin( PLUGIN, VERSION, AUTHOR )
	
	pcvar_enabled = register_cvar( "zp_sniper_buy", "1" )
	pcvar_cost = register_cvar( "zp_sniper_cost", "15" )
	pcvar_hudtime = register_cvar( "zp_sniper_hudtime", "2.0" )
	
	g_sniper = zp_register_extra_item( "Become Sniper", get_pcvar_num( pcvar_cost ) , ZP_TEAM_HUMAN )

	g_maxplayers = get_maxplayers()
	g_msgSayText = get_user_msgid( "SayText" )

	register_cvar( "zp_extra_sniper", VERSION, FCVAR_SERVER|FCVAR_EXTDLL|FCVAR_UNLOGGED|FCVAR_SPONLY )
}

public zp_extra_item_selected( id, item )
{
	if( !get_pcvar_num( pcvar_enabled ) )
		return PLUGIN_HANDLED
	
	if( item == g_sniper )
	{
		if( zp_has_round_started() )
		{
			colored_print( id, "^x04[ZP]^x01 You must buy a Sniper before the round start!" )
			return ZP_PLUGIN_HANDLED
		}

		zp_make_user_sniper( id )

		colored_print( id, "^x04[ZP]^x01 became a Sniper!" )

		set_task( get_pcvar_float( pcvar_hudtime ), "sniper_message", id )
	}
	return PLUGIN_HANDLED
}

public sniper_message( id )
{
	new szName[ 32 ]
	get_user_name( id, szName, 31 )
	set_hudmessage( 255, 0, 0, 0.05, 0.45, 1, 0.0, 5.0, 1.0, 1.0, -1 )
	show_hudmessage( 0, "%s became a Sniper!", szName )
}

stock colored_print( target, const message[],  any:... )
{
	static buffer[ 512 ]

	if( !target )
	{
		static player
		for( player = 1; player <= g_maxplayers; player++ )
		{
			if ( !is_user_connected( player ) )
				continue;
			
			vformat( buffer, charsmax( buffer ), message, 3 )
			
			message_begin( MSG_ONE_UNRELIABLE, g_msgSayText, _, player )
			write_byte( player )
			write_string( buffer )
			message_end()
		}
	}

	else
	{
		vformat( buffer, charsmax( buffer ), message, 3 )
		
		message_begin( MSG_ONE, g_msgSayText, _, target )
		write_byte( target )
		write_string( buffer )
		message_end()
	}
}