#include <amxmodx>
#include <engine>
#include <hamsandwich>

#define flag_get(%1,%2) (%1 & (1 << (%2 & 31)))
#define flag_set(%1,%2) (%1 |= (1 << (%2 & 31)))
#define flag_unset(%1,%2) (%1 &= ~(1 << (%2 & 31)))

new g_iMaxPlayers;
new g_iMsgSayText;

new g_enable;
new g_firstspawn;

public plugin_init() 
{
	register_plugin( "BH", "0.1", "Flash" );
	
	register_clcmd( "say /bhmenu", "ShowMenu" );
	register_clcmd( "say_team /bhmenu", "ShowMenu" );
	
	
	register_menucmd( register_menuid( "MENU_ID" ), 1023, "menu_handler" );
	
	g_iMaxPlayers = get_maxplayers();
	g_iMsgSayText = get_user_msgid( "SayText" );
}

public client_connect( id ) 
{
	flag_set( g_enable, id );
	flag_set( g_firstspawn, id );
}

public client_disconnect( id ) 
{
	flag_unset( g_enable, id );
	flag_unset( g_firstspawn, id );
}

public client_PreThink( id ) 
{
	if ( !is_user_alive( id ) || !flag_get( g_enable, id ) )
	{
		return PLUGIN_CONTINUE;
	}

	entity_set_float( id, EV_FL_fuser2, 0.0 );

	if ( entity_get_int( id, EV_INT_button) & 2 ) 
	{
		static flags;
		flags = entity_get_int( id, EV_INT_flags );

		if ( flags & FL_WATERJUMP || !( flags & FL_ONGROUND ) || entity_get_int( id, EV_INT_waterlevel ) >= 2 )
		{
			return PLUGIN_CONTINUE;
		}

		static Float:velocity[3];
		entity_get_vector( id, EV_VEC_velocity, velocity );

		velocity[2] += 250.0;

		entity_set_vector( id, EV_VEC_velocity, velocity );

		entity_set_int( id, EV_INT_gaitsequence, 6 );
	}

	return PLUGIN_CONTINUE;
}


public ShowMenu( id )
{
	new sMenu[1024], keys, len;
	
	len = formatex( sMenu, charsmax( sMenu ), "\yУправление авто-распрыжкой^nСтатус: %s^n^n", flag_get( g_enable, id ) ? "\rВкл." : "\dВыкл." );
	
	if ( !flag_get( g_enable, id ) )
	{
		keys |= ( 1 << 0 );
		len += formatex( sMenu[len], charsmax( sMenu ) - len, "\r1. \wВключить^n" );
	}
	else
	{
		len += formatex( sMenu[len], charsmax( sMenu ) - len, "\d1. Включить^n" );
	}
	
	if ( flag_get( g_enable, id ) )
	{
		keys |= ( 1 << 1 );
		len += formatex( sMenu[len], charsmax( sMenu ) - len, "\r2. \wВыключить" );
	}
	else
	{
		len += formatex( sMenu[len], charsmax( sMenu ) - len, "\d2. Выключить" );
	}
	
	show_menu( id, keys, sMenu, -1, "MENU_ID" );

	return PLUGIN_CONTINUE;
}

public menu_handler( id, key )
{
	switch( key )
	{
		case 0:
		{
			flag_set( g_enable, id );
			print_color( id, "^x04[BH] ^x01Распрыжка ^x03включена" );
		}
		
		case 1:
		{
			flag_unset( g_enable, id );
			print_color( id, "^x04[BH] ^x01Распрыжка ^x03выключена" );
		}
	}
}

stock print_color( const id, const sMessage[], any:... )
{
	new sMsg[256];
	sMsg[0] = 0x01;
	
	vformat( sMsg[1], 251, sMessage, 3 );
	sMsg[192] = '^0';
	
	if ( id )
	{
		message_begin( MSG_ONE_UNRELIABLE, g_iMsgSayText, _, id );
		write_byte( id );	
		write_string( sMsg );
		message_end( );
	}
	else
	{
		for ( new i = 1; i <= g_iMaxPlayers; i++ )
		{
			if ( is_user_connected( i ) )
			{
				message_begin( MSG_ONE_UNRELIABLE, g_iMsgSayText, _, i );
				write_byte( i );	
				write_string( sMsg );
				message_end( );
			}
		}
	}	
	
	return 1;
}