#include < amxmodx >
#include < amxmisc >
#include < fun >
#include < fakemeta >
#include < cstrike >
#include < hamsandwich >
 
#pragma semicolon       1
 
#define VERSION         "0.0.1"
 
#define Max_Players     32
 
#define OFFSET_PRIMARYWEAPON    116
#define OFFSET_C4_SLOT      372
 
#define Weapon_Menu_Name    "Fegyverek"
#define Pistol_Menu_Name    "Pisztolyok"
 
#define Next_Button     "Következő"
#define Back_Button     "Vissza"
#define Exit_Button     "Kilépés"
 
#define Money_Hud       ( 1 << 5 )
 
#define PREFIX "Fegyomenu"
 
new g_iWPCT;
new g_iWPTE;
 
new pCvarMaxCTWps;
new pCvarMaxTEWps;
 
new pCvarWPBlock;
 
new pCvarFlash;
new pCvarHe;
new pCvarSmoke;
 
new pCvarKevlar;
new pCvarDefuser;
 
new pCvarPrefix;
new pCvarMoney;
new pCvarBlockBuy;
new pCvarUnAmmo;
 
new const g_szMessages [ ] [ ] = 
{
    "",
    "Csapatonként csak két ember AWP-zhet!",
    "^3%s^1 WP^4 csak^1 4v4-től használható!",
    "A vásárlás letíltva!",
    "Te már választottál fegyvert!"
};
 
new const g_szWeaponMenuNames [ ] [ ] =  {
 
    "M4A1",
    "AK47",
    "AWP",
    "M249",
    "AUG",
    "Famas",
    "Galil",
    "MP5",
    "XM1014",
    "M3",
    "Scout"
};
 
new const g_szWeaponsName [ ] [ ] = {
 
    "weapon_m4a1",
    "weapon_ak47",
    "weapon_awp",
    "weapon_m249",
    "weapon_aug",
    "weapon_famas",
    "weapon_galil",
    "weapon_mp5navy",
    "weapon_xm1014",
    "weapon_m3",
    "weapon_scout"
};
 
new const g_szPistolsMenuNames [ ] [ ] =  {
 
    "Deagle",
    "USP",
    "Glock18",
    "Fiveseven",
    "P228",
    "Elite"
};
 
new const g_szPistolsName [ ] [ ] = {
 
    "weapon_deagle",
    "weapon_usp",
    "weapon_glock18",
    "weapon_fiveseven",
    "weapon_p228",
    "weapon_elite"
};
 
 
new const g_szPistolName [ ] = "weapon_deagle";
 
const DoNotReload = ( ( 1 << CSW_HEGRENADE ) | ( 1 << CSW_SMOKEGRENADE ) | ( 1 << CSW_FLASHBANG ) | ( 1 << CSW_KNIFE ) | ( 1 << CSW_C4 ) );
new const maxAmmo[ 31 ] = {
 
    0,
    52, 
    0, 
    90, 
    1, 
    32, 
    1, 
    100, 
    90, 
    1, 
    120, 
    100, 
    100, 
    90, 
    90, 
    90, 
    100, 
    120, 
    30, 
    120, 
    200, 
    32, 
    90, 
    120, 
    90, 
    2, 
    35, 
    90, 
    90, 
    0, 
    100 
};
 
new g_szBuyCommands[  ][  ] = 
{
    "usp", "glock", "deagle", "p228", "elites", "fn57", "m3", "xm1014", "mp5", "tmp", "p90", "mac10", "ump45", "ak47", 
    "galil", "famas", "sg552", "m4a1", "aug", "scout", "awp", "g3sg1", "sg550", "m249", "vest", "vesthelm", "flash", "hegren",
    "sgren", "defuser", "nvgs", "shield", "primammo", "secammo", "km45", "9x19mm", "nighthawk", "228compact", "12gauge",
    "autoshotgun", "smg", "mp", "c90", "cv47", "defender", "clarion", "krieg552", "bullpup", "magnum", "d3au1", "krieg550",
    "buyammo1", "buyammo2"
};
 
new const g_szMenuCommands[  ] [  ] = 
{
    "fegyo",
    "say fegyo",
    "say_team fegyo",
 
    "fegyver",
    "say fegyver",
    "say_team fegyver",
 
    "say /fegyo",
    "say_team /fegyo",
 
    "say /fegyver",
    "say_team /fegyver"
 
};
 
new g_szChatPrefix[ 64 ];
new g_msgHideWeapon;
new g_bHasWeapon[ Max_Players+1 ];
 
public plugin_init ( ) {
 
    register_plugin( "Advanced Weapon Menu", VERSION, "#YouCantStopMe" );
 
    RegisterHam( Ham_Spawn, "player", "func_OpenWeaponMenu", 1 );
 
    pCvarMaxCTWps   = register_cvar( "awm_max_ct_awp",  "2" );
    pCvarMaxTEWps   = register_cvar( "awm_max_te_awp",  "2" );
 
    pCvarWPBlock    = register_cvar( "awm_allow_wp_player",     "4" );
 
    pCvarFlash  = register_cvar( "awm_give_flash",  "2" );
    pCvarHe     = register_cvar( "awm_give_he",         "1" );
    pCvarSmoke  = register_cvar( "awm_give_smoke",  "0" );
 
    pCvarKevlar = register_cvar( "awm_give_kevlar", "2" );
    pCvarDefuser    = register_cvar( "awm_give_defuser",    "1" );
 
    pCvarUnAmmo = register_cvar( "awm_unlimited_ammo",  "1" );
 
    pCvarBlockBuy   = register_cvar( "awm_block_buy",   "1" );
    pCvarMoney  = register_cvar( "awm_set_money",   "0" );
    pCvarPrefix     = register_cvar( "awm_prefix",      PREFIX );
 
    g_msgHideWeapon = get_user_msgid( "HideWeapon" );
 
    for( new i = 0; i < sizeof( g_szBuyCommands ); i++ )
        register_clcmd( g_szBuyCommands[ i ], "cmd_BlockBuy" );
 
    for( new i = 0; i < sizeof( g_szMenuCommands ); i++ )
        register_clcmd( g_szMenuCommands[ i ], "cmd_ShowWeaponMenu" );
 
    register_event( "ResetHUD", "onResetHUD", "b" );
    register_event( "CurWeapon", "eCurWeapon", "be", "1=1" );
 
    register_message( g_msgHideWeapon, "msgHideWeapon" );
 
    register_logevent( "eRoundEnd", 2, "1=Round_End" );
}
 
public client_connect( iClient ) {
 
    g_bHasWeapon[ iClient ] = false;
}
 
public client_disconnect( iClient ) {
 
    g_bHasWeapon[ iClient ] = false;
}
 
public cmd_ShowWeaponMenu( iClient ) {
 
    switch( g_bHasWeapon[ iClient ] ) {
 
        case true: 
        {
            get_pcvar_string( pCvarPrefix, g_szChatPrefix, charsmax( g_szChatPrefix ) );
            ColorChat( iClient, "^3%s %s" , g_szChatPrefix, g_szMessages[ 4 ] );
 
            return PLUGIN_HANDLED;
        }
        case false:     func_OpenWeaponMenu( iClient );
    }
    return PLUGIN_HANDLED;
}
 
public cmd_BlockBuy( iClient ) {
 
    if( !get_pcvar_num( pCvarBlockBuy ) )
        return PLUGIN_CONTINUE;
 
    get_pcvar_string( pCvarPrefix, g_szChatPrefix, charsmax( g_szChatPrefix ) );
    ColorChat( iClient,  "^3%s %s" , g_szChatPrefix, g_szMessages[ 3 ] );
    return PLUGIN_HANDLED;
}
 
public eCurWeapon( iClient ) {
 
    if( get_pcvar_num( pCvarUnAmmo ) ) {
 
        if( is_user_alive( iClient ) ) {
 
            new weapon = read_data( 2 );
            if( !( DoNotReload & ( 1 << weapon ) ) ) {
 
                cs_set_user_bpammo( iClient, weapon, maxAmmo[ weapon ] );
            }
        }
    }
}
 
public onResetHUD( iClient ) {
 
    if( !get_pcvar_num( pCvarMoney ) ) {
 
        message_begin( MSG_ONE, g_msgHideWeapon, _, iClient );
        write_byte( Money_Hud );
        message_end( );
    }
}
 
public msgHideWeapon( ) {
 
    if( !get_pcvar_num( pCvarMoney ) ) {
 
        set_msg_arg_int( 1, ARG_BYTE, get_msg_arg_int( 1 ) | Money_Hud );
    }
}
 
public eRoundEnd ( ) {
 
    g_iWPCT = 0;
    g_iWPTE = 0;
}
 
public func_OpenWeaponMenu ( iClient ) {
 
    if( !is_user_alive( iClient ) )
        return;
 
    cs_set_user_money( iClient, get_pcvar_num( pCvarMoney ) );
 
    get_pcvar_string( pCvarPrefix, g_szChatPrefix, charsmax( g_szChatPrefix ) );
 
    g_bHasWeapon[ iClient ] = false;
 
    new szMenuTitle[ 121 ];
    new szMenuItem[ 121 ];
 
    format( szMenuTitle, charsmax( szMenuTitle ), "\r%s^n\w%s", g_szChatPrefix, Weapon_Menu_Name );
 
    StripUserWeapons( iClient );
    new menu = menu_create( szMenuTitle, "func_OpenWeaponMenu_handler" );
 
    for( new i = 0; i < sizeof( g_szWeaponMenuNames ); i++ ) {
 
        if( i != 2 ) {
 
            format( szMenuItem, charsmax( szMenuItem ), "\w%s", g_szWeaponMenuNames[ i ] );
        }
        else {
            switch( get_user_team( iClient ) )
            {
                case 1: format( szMenuItem, charsmax( szMenuItem ), "\w%s \y[Első %d embernek]", g_szWeaponMenuNames[ i ], get_pcvar_num( pCvarMaxTEWps ) );
                case 2: format( szMenuItem, charsmax( szMenuItem ), "\w%s \y[Első %d embernek]", g_szWeaponMenuNames[ i ], get_pcvar_num( pCvarMaxCTWps ) );
                default: continue;
            }
        }
 
        menu_additem( menu, szMenuItem, _, 0 );
    }
 
    menu_setprop( menu, MPROP_EXIT, MEXIT_ALL );
    menu_setprop( menu, MPROP_BACKNAME, Back_Button );
    menu_setprop( menu, MPROP_NEXTNAME, Next_Button );
    menu_setprop( menu, MPROP_EXITNAME, Exit_Button );
    menu_display( iClient, menu );
}
 
public func_OpenWeaponMenu_handler( iClient, iMenu, iItem ) {
 
    if( iItem == MENU_EXIT ) {
 
        StripUserWeapons( iClient );
        g_bHasWeapon[ iClient ] = false;
        menu_destroy( iMenu );
        return PLUGIN_HANDLED;
    }
 
    new data[ 6 ], szName[ 64 ];
    new access, callback;
    menu_item_getinfo( iMenu, iItem, access, data, charsmax( data ), szName, charsmax( szName ), callback );
 
    get_pcvar_string( pCvarPrefix, g_szChatPrefix, charsmax( g_szChatPrefix ) );
 
    if( iItem != 2 ) {
 
        give_item( iClient, g_szWeaponsName[ iItem ] );
 
        ColorChat( iClient,  "^3%s^1 Te az ^4%s^1-t választottad!", g_szChatPrefix ,g_szWeaponMenuNames[ iItem ] );
    }
    else {
 
        new iTeams[ CsTeams ];
        GetPlayerCount( iTeams );
 
        if( iTeams[ CS_TEAM_T ] < get_pcvar_num( pCvarWPBlock ) 
        || iTeams[ CS_TEAM_CT ] < get_pcvar_num( pCvarWPBlock ) ) {
 
            ColorChat( iClient,  g_szMessages[ 2 ], g_szChatPrefix );
            func_OpenWeaponMenu( iClient );
 
            return PLUGIN_HANDLED;
        }
 
        new CsTeams:userTeam = cs_get_user_team( iClient );
        if( userTeam == CS_TEAM_CT ) {
 
            if( g_iWPCT < get_pcvar_num( pCvarMaxCTWps ) ) {
 
                give_item( iClient, g_szWeaponsName[ iItem ] );
                give_item( iClient, g_szPistolName );
 
                ColorChat( iClient,  "^3%s^1 Te az ^4%s^1-t választottad!", g_szChatPrefix, g_szWeaponMenuNames[ iItem ] );
                g_iWPCT++;
            }
            else {
 
                client_print( iClient, print_center, g_szMessages[ 1 ] );
                func_OpenWeaponMenu ( iClient );
 
                return PLUGIN_HANDLED;
            }
        }
 
        if( userTeam == CS_TEAM_T ) {
 
            if( g_iWPTE < get_pcvar_num( pCvarMaxTEWps ) ) {
 
 
                give_item( iClient, g_szWeaponsName[ iItem ] );
                give_item( iClient, g_szPistolName );
 
                ColorChat( iClient,  "^3%s^1 Te az ^4%s^1-t választottad!", g_szChatPrefix, g_szWeaponMenuNames[ iItem ] );
                g_iWPTE++;
            }
            else {
 
                client_print( iClient, print_center, g_szMessages[ 1 ] );
                func_OpenWeaponMenu( iClient );
 
                return PLUGIN_HANDLED;
            }
        }
    }
    func_OpenPistolMenu( iClient );
 
    menu_destroy( iMenu );
    return PLUGIN_HANDLED;
}
public func_OpenPistolMenu ( iClient ) {
 
    if( !is_user_alive( iClient ) )
        return;
 
    cs_set_user_money( iClient, get_pcvar_num( pCvarMoney ) );
 
    get_pcvar_string( pCvarPrefix, g_szChatPrefix, charsmax( g_szChatPrefix ) );
 
    new szMenuTitle[ 121 ];
    new szMenuItem[ 121 ];
 
    format( szMenuTitle, charsmax( szMenuTitle ), "\r%s^n\w%s", g_szChatPrefix, Pistol_Menu_Name );
 
    new menu = menu_create( szMenuTitle, "func_OpenPistolMenu_handler" );
 
    for( new i = 0; i < sizeof( g_szPistolsMenuNames ); i++ ) {
 
        format( szMenuItem, charsmax( szMenuItem ), "\w%s", g_szPistolsMenuNames[ i ] );
        menu_additem( menu, szMenuItem, _, 0 );
    }
 
    menu_setprop( menu, MPROP_EXIT, MEXIT_ALL );
    menu_setprop( menu, MPROP_BACKNAME, Back_Button );
    menu_setprop( menu, MPROP_NEXTNAME, Next_Button );
    menu_setprop( menu, MPROP_EXITNAME, Exit_Button );
    menu_display( iClient, menu );
}
 
public func_OpenPistolMenu_handler( iClient, iMenu, iItem ) {
 
    if( iItem == MENU_EXIT ) {
 
        StripUserWeapons( iClient );
        g_bHasWeapon[ iClient ] = false;
        menu_destroy( iMenu );
        return PLUGIN_HANDLED;
    }
 
    new data[ 6 ], szName[ 64 ];
    new access, callback;
    menu_item_getinfo( iMenu, iItem, access, data, charsmax( data ), szName, charsmax( szName ), callback );
    get_pcvar_string( pCvarPrefix, g_szChatPrefix, charsmax( g_szChatPrefix ) );
 
    give_item( iClient, g_szPistolsName[ iItem ] );
    give_item( iClient, "weapon_knife" );
 
    give_player_stuff( iClient );
 
    ColorChat( iClient,  "^3%s^1 Te az ^4%s^1-t választottad!", g_szChatPrefix ,g_szPistolsMenuNames[ iItem ] );
 
    menu_destroy( iMenu );
    return PLUGIN_HANDLED;
}
 
GetPlayerCount( iTeamPlayers[ CsTeams ] ) {
 
    new iPlayers[ 32 ] , iPlayerCount;
 
    get_players( iPlayers , iPlayerCount ); 
 
    for ( new i = 0 ; i < iPlayerCount ; i++ )
        iTeamPlayers[ cs_get_user_team( iPlayers[ i ] ) ]++;
}  
 
stock give_player_stuff( iClient ) {
 
    if( get_pcvar_num( pCvarFlash ) ) {
 
        give_item( iClient, "weapon_flashbang" );
        cs_set_user_bpammo( iClient, CSW_FLASHBANG, get_pcvar_num( pCvarFlash ) );
    }
 
    if( get_pcvar_num( pCvarHe ) ) {
 
        give_item( iClient, "weapon_hegrenade" );
        cs_set_user_bpammo( iClient, CSW_HEGRENADE, get_pcvar_num( pCvarHe ) );
    }
 
    if( get_pcvar_num( pCvarSmoke ) ) {
 
        give_item( iClient, "weapon_smokegrenade" );
        cs_set_user_bpammo( iClient, CSW_SMOKEGRENADE, get_pcvar_num( pCvarSmoke ) );
    }
 
    if( get_pcvar_num( pCvarKevlar ) ) {
 
        switch( get_pcvar_num( pCvarKevlar ) ) {
 
            case 1: give_item( iClient, "item_kevlar" );
            case 2: give_item( iClient, "item_assaultsuit" );
            default: return PLUGIN_CONTINUE;
        }
    }
 
    if( get_pcvar_num( pCvarDefuser ) ) {
 
        give_item( iClient, "item_thighpack" );
    }
 
    if( !get_pcvar_num( pCvarUnAmmo ) ) {
 
        new weapons[ 32 ];
        new weaponsnum;
        get_user_weapons( iClient, weapons, weaponsnum );
        for( new i = 0; i < weaponsnum; i++ )
            if( is_user_alive( iClient ) )
                if( maxAmmo[ weapons[ i ] ] > 0 )
                    cs_set_user_bpammo( iClient, weapons[ i ], maxAmmo[ weapons[ i ] ] );
    }
 
    g_bHasWeapon[ iClient ] = true;
    return PLUGIN_CONTINUE;
}
 
stock ColorChat( iClient, const input[], any:...) 
{ 
    new count = 1, players[ 32 ];
    static msg[ 191 ];
    vformat( msg, 190, input, 3 );
 
    replace_all( msg, 190, "^x01" , "^1");
    replace_all( msg, 190, "^x03" , "^3");
    replace_all( msg, 190, "^x04" , "^4"); 
 
    if ( iClient )  players[ 0 ] = iClient; else get_players( players , count , "ch" );
    { 
        for ( new i = 0; i < count; i++ ) 
        { 
            if ( is_user_connected( players[ i ] ) ) 
            { 
                message_begin( MSG_ONE_UNRELIABLE , get_user_msgid( "SayText" ), _, players[ i ] );
                write_byte( players[ i ] ); 
                write_string( msg ); 
                message_end(  ); 
            } 
        } 
    } 
}  
 
 
//Stolen from CSDM Weapon Menu
stock StripUserWeapons( iClient ) {
 
    new iC4Ent = get_pdata_cbase( iClient, OFFSET_C4_SLOT );
 
    if( iC4Ent > 0 ) {
 
        set_pdata_cbase( iClient, OFFSET_C4_SLOT, FM_NULLENT );
    }
 
    strip_user_weapons( iClient );
    set_pdata_int( iClient, OFFSET_PRIMARYWEAPON, 0 );
 
    if( iC4Ent > 0 )  {
 
        set_pev( iClient, pev_weapons, pev( iClient, pev_weapons ) | ( 1 << CSW_C4 ) );
        set_pdata_cbase( iClient, OFFSET_C4_SLOT, iC4Ent );
 
        cs_set_user_bpammo( iClient, CSW_C4, 1 );
        cs_set_user_plant( iClient, 1 );
    }
 
    return PLUGIN_HANDLED;
}