AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Only one random player (https://forums.alliedmods.net/showthread.php?t=320508)

Saint Sinner 12-25-2019 09:59

Only one random player
 
Merry Christmas!

I want to select only one random player

If i am TERO i want to select random player from CT
If i am CT i want to select random player from TERO

But also is wrong in this code becouse choose more players

Code:

public fwButtonUsed(button, test)
{
    if( g_bChallenging && is_user_alive ( test))
    {
        client_print( test, print_chat, "* Someone is already starting a challenge." );
        return HAM_HANDLED;
    }
   
    static iPlayers[32], iNum, id, iRandom[32], iPlayer, iRandomNum;
 
    get_players( iPlayers, iNum, "a");
 
    for ( new i = 0 ; i < iNum ; i++ )
    {
        id = iPlayers[i];
       
        if ( is_user_alive( test) && ( cs_get_user_team( test) == CS_TEAM_CT ) )
        {
            if ( cs_get_user_team(id) == CS_TEAM_T )
            {
                iRandom[ iRandomNum++ ] = id;
                iPlayer = iRandom[ random( iRandomNum ) ];
 
                g_bChallenging = true;

              is_in_arena[test] = 1;
              is_in_arena[iPlayer] = 1;
               
                client_print( test, print_chat, "* iPlayer Tero Hooked" );
            }
        }
        if ( is_user_alive( test) && ( cs_get_user_team( test) == CS_TEAM_T ) )
        {
            if ( cs_get_user_team(id) == CS_TEAM_CT )
            {
                iRandom[ iRandomNum++ ] = id;
                iPlayer = iRandom[ random( iRandomNum ) ];
 
                g_bChallenging = true;
               
              is_in_arena[test] = 1;
              is_in_arena[iPlayer] = 1;
           
              client_print( test, print_chat, "* iPlayer Counter Hooked" );
            }
        }
    }
    return HAM_HANDLED;
}


JocAnis 12-25-2019 12:14

Re: Only one random player
 
I think you need to add
Code:

break;
line, when the player is choosed (so it will stop to look for other players in that for( loop ) )

Xalus 12-25-2019 13:30

Re: Only one random player
 
Should just use a simple function like this.

PHP Code:

get_random_challenger(client)
{
  new 
player_list[32], player_amount
  get_players
(player_listplayer_amount"ae", (cs_get_user_team(client) == CS_TEAM_T) ? "CT" "TERRORIST")

  return 
player_listrandom(player_amount) ]



Saint Sinner 12-25-2019 13:36

Re: Only one random player
 
1 Attachment(s)
Yes i tried .... already not work..

Quote:

Originally Posted by JocAnis (Post 2678005)
I think you need to add
Code:

break;
line, when the player is choosed (so it will stop to look for other players in that for( loop ) )

The full code is here

Code:

#include <amxmodx>
#include <amxmisc>
#include <fakemeta>
#include <engine>
#include <hamsandwich>
#include <fun>
#include <cstrike>
#include <xs>

#define PLUGIN "Button"
#define VERSION "1.0"
#define AUTHOR "raizo"

#pragma tabsize 0
#pragma compress 1

static const ButtonModel[]        = "models/button.mdl";
static const ButtonName[]          = "knifebutton";
static const ButtonCfg[]          = "%s/Button/%s.cfg"
static const Save_Player_Origin[] = "%s/Duel_Origins/%s.cfg"

new Float:ButtonSizeMin[3] = {-8.0,-8.0, 0.0};
new Float:ButtonSizeMax[3] = { 8.0, 8.0, 50.0};
new Float:Player_Origin[3]
new Float:his_player_spawn[33][3]
new Float:fVelocity[3]

new is_in_arena[33]
new his_player_spawns[33]

new entbutton
new SetHealth;
new cvar_distance_te
new cvar_distance_ct
new cvar_non_stop;
new MAXPLAYERS

new bool:g_bChallenging;



public plugin_precache( )
{       
    precache_model(ButtonModel)
}

public plugin_init()
{
    register_plugin(PLUGIN, VERSION, AUTHOR)
   
    register_clcmd("say /button","create_button_menu");
   
    cvar_distance_te = register_cvar("duel_distance_te","350")
    cvar_distance_ct = register_cvar("duel_distance_ct","350")
    cvar_non_stop = register_cvar("duel_stop","1")
    SetHealth = register_cvar( "duel_health", "35" );
   
    RegisterHam(Ham_Use, "func_button", "fwButtonUsed")
    RegisterHam(Ham_Spawn,"player","Player_spawn_position",1)
    RegisterHam(Ham_Killed, "player", "Player_Kill_Arena", 1)
       
    register_forward(FM_CmdStart,"Duel_Dont_Stop");
       
    load_button_cords()
    load_player_coords()
   
    MAXPLAYERS = get_maxplayers();
       
    new cfgdir[32], urlfile[64]
    get_configsdir(cfgdir, charsmax(cfgdir))
    formatex(urlfile, charsmax(urlfile), "%s/Button", cfgdir)
    if(!dir_exists(urlfile))
    {
        mkdir(urlfile)
    }
 
    new cfgdir2[32], urlfile2[64]
    get_configsdir(cfgdir2, charsmax(cfgdir2))
    formatex(urlfile2, charsmax(urlfile2), "%s/Duel_Origins", cfgdir2)
    if(!dir_exists(urlfile2))
    {
        mkdir(urlfile2)
    }
}

public create_button_menu( id )
{
    new flags = get_user_flags(id)
    if(!(flags & ADMIN_RCON))
    {
        client_print(id,print_chat,"You have no access to this command")
        return PLUGIN_HANDLED
    }
    new gMenu = menu_create("\y[\r Button Menu\y ]\w By raizo", "create_button_menu_handled")
   
    menu_additem(gMenu, "\wCreate Button", "0")
    menu_additem(gMenu, "\wRemove Button", "1")
    menu_additem(gMenu, "\wSave Button", "2")
    menu_additem(gMenu, "\wSave Player Origin", "3")
   
    menu_display(id, gMenu, 0)

    return PLUGIN_HANDLED; 
}

public create_button_menu_handled(id, menu, item, code)     
{
    if ( item == MENU_EXIT )   
    {
        menu_destroy(menu)     
        return PLUGIN_HANDLED; 
    }
    switch(item) 
    {
        case 0: create_button(id)
        case 1: remove_button(id)
        case 2: save_button(id)
        case 3: save_player_coords(id)
    }
    return PLUGIN_HANDLED; 
}

public create_button(id)
{
    if(!is_user_connected(id))
        return PLUGIN_HANDLED

    static Float:xorigin[3], Float:fAngles[3];
   
    get_user_hitpoint(id,xorigin)
   
    entity_get_vector( id, EV_VEC_angles, fAngles )
   
    fAngles[1] += 180;

    entbutton = engfunc( EngFunc_CreateNamedEntity, engfunc( EngFunc_AllocString, "func_button" ) )

    if( is_valid_ent(entbutton))
    {
        entity_set_string(entbutton, EV_SZ_classname, ButtonName);
        entity_set_int(entbutton, EV_INT_solid, SOLID_BBOX);
        entity_set_int(entbutton, EV_INT_movetype, MOVETYPE_NONE);
        entity_set_model(entbutton, ButtonModel);
        entity_set_size(entbutton, ButtonSizeMin, ButtonSizeMax);
        entity_set_origin(entbutton,xorigin)
        entity_set_vector(entbutton, EV_VEC_angles, fAngles )
    }
    create_button_menu( id )
    return PLUGIN_HANDLED
}

public remove_button(id)
{
    new ent = -1;

    while ((ent = find_ent_by_class(ent, ButtonName)))
    {
        remove_entity(ent);
    }
    create_button_menu( id )
}

stock get_user_hitpoint(id,Float:hOrigin[3])  {
    if(!is_user_alive(id))
        return 0;
 
    new Float:fOrigin[3],Float:fvAngle[3],Float:fvOffset[3],Float:fvOrigin[3],Float:feOrigin[3];
    new Float:fTemp[3];
 
    pev(id,pev_origin,fOrigin);
    pev(id,pev_v_angle,fvAngle);
    pev(id,pev_view_ofs,fvOffset);
 
    xs_vec_add(fOrigin,fvOffset,fvOrigin);
 
    engfunc(EngFunc_AngleVectors,fvAngle,feOrigin,fTemp,fTemp);
 
    xs_vec_mul_scalar(feOrigin,9999.9,feOrigin);
    xs_vec_add(fvOrigin,feOrigin,feOrigin);
 
    engfunc(EngFunc_TraceLine,fvOrigin,feOrigin,0,id);
    global_get(glb_trace_endpos,hOrigin);
 
    return 1;
}

public save_button(id)
{
        if(!is_user_alive(id))
            return 1;
       
        static sConfigsDir[64], sFile[64];
        get_configsdir(sConfigsDir, sizeof sConfigsDir - 1);
       
        static sMapName[32];
        get_mapname(sMapName, sizeof sMapName - 1);
       
        formatex(sFile, sizeof sFile - 1, ButtonCfg, sConfigsDir, sMapName);
       
        if(file_exists(sFile))
                delete_file(sFile);
       
        new iEnt = -1, Float:fEntOrigin[3], Float:fEntAngles[3], iCount;
        static sBuffer[256];
       
        while((iEnt = engfunc(EngFunc_FindEntityByString, iEnt, "classname", ButtonName)))
        {
                pev(iEnt, pev_origin, fEntOrigin);
                pev(iEnt, pev_angles, fEntAngles);
               
                formatex(sBuffer, sizeof sBuffer - 1, "%f %f %f | %f %f %f", fEntOrigin[0], fEntOrigin[1], fEntOrigin[2], fEntAngles[0], fEntAngles[1], fEntAngles[2]);

                write_file(sFile, sBuffer, -1);
               
                iCount++;
        }
        client_print(id, print_chat, "Successfuly saved all snowman origins (%d) for map %s!", iCount,sMapName);
        create_button_menu( id )
        return 0;
}

public load_button_cords()
{
    static sConfigsDir[64], sFile[64];
    get_configsdir(sConfigsDir, sizeof sConfigsDir - 1);       
   
    static sMapName[32];
    get_mapname(sMapName, sizeof sMapName - 1);
   
    formatex(sFile, sizeof sFile - 1, ButtonCfg, sConfigsDir, sMapName);
       
    if(!file_exists(sFile))
        return 1;
       
    static sFileOrigin[3][32], sFileAngles[3][32], iLine, iLength, sBuffer[256];
    static sTemp1[128], sTemp2[128];
    static Float:fOrigin[3], Float:fAngles[3];
       
    while(read_file(sFile, iLine++, sBuffer, sizeof sBuffer - 1, iLength))
    {
        if((sBuffer[0]==';') || !iLength)
            continue;
               
        strtok(sBuffer, sTemp1, sizeof sTemp1 - 1, sTemp2, sizeof sTemp2 - 1, '|', 0);
               
        parse(sTemp1, sFileOrigin[0], sizeof sFileOrigin[] - 1, sFileOrigin[1], sizeof sFileOrigin[] - 1, sFileOrigin[2], sizeof sFileOrigin[] - 1);
               
        fOrigin[0] = str_to_float(sFileOrigin[0]);
        fOrigin[1] = str_to_float(sFileOrigin[1]);
        fOrigin[2] = str_to_float(sFileOrigin[2]);
               
        parse(sTemp2, sFileAngles[0], sizeof sFileAngles[] - 1, sFileAngles[1], sizeof sFileAngles[] - 1, sFileAngles[2], sizeof sFileAngles[] - 1);
               
        fAngles[0] = str_to_float(sFileAngles[0]);
        fAngles[1] = str_to_float(sFileAngles[1]);
        fAngles[2] = str_to_float(sFileAngles[2]);
       
        entbutton = engfunc( EngFunc_CreateNamedEntity, engfunc( EngFunc_AllocString, "func_button" ) )

        if( is_valid_ent(entbutton))
        {
            entity_set_string(entbutton, EV_SZ_classname, ButtonName);
            entity_set_int(entbutton, EV_INT_solid, SOLID_BBOX);
            entity_set_int(entbutton, EV_INT_movetype, MOVETYPE_NONE);
            entity_set_model(entbutton, ButtonModel);
            entity_set_size(entbutton, ButtonSizeMin, ButtonSizeMax);
            entity_set_origin(entbutton,fOrigin)
            entity_set_vector(entbutton, EV_VEC_angles, fAngles )
        }
    }
    return 1;

}

public fwButtonUsed(button, raizo)
{
    if( g_bChallenging && is_user_alive ( raizo ))
    {
        client_print( raizo, print_chat, "* Someone is already starting a challenge." );
        return HAM_HANDLED;
    }
   
    static iPlayers[32], iNum, id, iRandom[32], iPlayer, iRandomNum;
 
    get_players( iPlayers, iNum, "a");
 
    for ( new i = 0 ; i < iNum ; i++ )
    {
        id = iPlayers[i];
       
        if ( is_user_alive( raizo ) && ( cs_get_user_team( raizo ) == CS_TEAM_CT ) )
        {
            if ( cs_get_user_team(id) == CS_TEAM_T )
            {
                iRandom[ iRandomNum++ ] = id;
                iPlayer = iRandom[ random( iRandomNum ) ];
 
                // do what you want with iPlayer who is a T
           
                g_bChallenging = true;

              is_in_arena[raizo] = 1;
              is_in_arena[iPlayer] = 1;
               
                client_print( raizo, print_chat, "* iPlayer Tero Hooked" );
               
              set_spawn_positions(raizo,Player_Origin)
                set_spawn_positions(iPlayer,Player_Origin)
            }
        }
        if ( is_user_alive( raizo ) && ( cs_get_user_team( raizo ) == CS_TEAM_T ) )
        {
            if ( cs_get_user_team(id) == CS_TEAM_CT )
            {
                iRandom[ iRandomNum++ ] = id;
                iPlayer = iRandom[ random( iRandomNum ) ];
 
                // do what you want with iPlayer who is a CT
           
                g_bChallenging = true;
               
              is_in_arena[raizo] = 1;
              is_in_arena[iPlayer] = 1;
           
              client_print( raizo, print_chat, "* iPlayer Counter Hooked" );
             
              set_spawn_positions(raizo,Player_Origin)
                set_spawn_positions(iPlayer,Player_Origin)
            }
        }
    }
    return HAM_HANDLED;
}

public save_player_coords(id)
{
    new found;
    new cfgdir[32], mapname[32], urlfile[64]
    get_configsdir(cfgdir, charsmax(cfgdir))
    get_mapname(mapname, charsmax(mapname))
    formatex(urlfile, charsmax(urlfile), Save_Player_Origin, cfgdir, mapname)
 
    if (file_exists(urlfile))
        delete_file(urlfile)
 
    new lineset[128]
    new Float:origin[3]

    found++
    pev(id,pev_origin,origin);
    format(lineset, charsmax(lineset), "%.f %.f %.f", origin[0], origin[1], origin[2])
    write_file(urlfile, lineset,found)

    if(!found)
        client_print(id, print_chat, "Couldn't save ! No origins found.")
    else client_print(id, print_chat, "%d Player Origins Saved!",found)
   
    load_player_coords()
}

public load_player_coords()
{
    new cfgdir[32], mapname[32], filepath[512]
    get_configsdir(cfgdir, charsmax(cfgdir))
    get_mapname(mapname, charsmax(mapname))

        new readdata[128]
        new txtlen
   
        formatex(filepath, charsmax(filepath), Save_Player_Origin, cfgdir, mapname)

        if ( file_exists(filepath) )
        {
            new sOrigins[3][16];
           
            new i
            new fsize = file_size(filepath,1)
            for (new line=0;line<=fsize;line++)
            {
                read_file(filepath,line,readdata,127,txtlen)

                parse(readdata, sOrigins[0], 15, sOrigins[1], 15, sOrigins[2], 15)
                for(i = 0; i < 3; i++)
                {
                    Player_Origin[i] = str_to_float(sOrigins[i])
                }
            }                   
        }
        return PLUGIN_CONTINUE
}

public Player_Kill_Arena(victimid, killerid)
{
    if (victimid == killerid || !is_user_alive(killerid))
        return
       
    if(is_in_arena[killerid])
    {
            ExecuteHam(Ham_CS_RoundRespawn, killerid)
    }
}

public Player_spawn_position(id)
{
    if(is_user_alive(id))
    {
        if(is_in_arena[id])
        {
            spawn_back(id)
        }
    }
}

public spawn_back(id)
{
    set_spawn_positions(id,Player_Origin)
   
    his_player_spawns[id] ++

    if(his_player_spawns[id] >= 10)
    {
            his_player_spawns[id] = 0;
        set_task(1.0, "end_the_duel", id)
    }
}

public end_the_duel(id)
{
    if(is_user_alive(id))
    {
        is_in_arena[id] = 0;
        g_bChallenging = false;
        ExecuteHam(Ham_CS_RoundRespawn, id);
    }
}

public client_disconnect(id)
{
    for(new raizo; raizo < MAXPLAYERS;raizo++)
    {
        if(is_in_arena[raizo])
        {
          end_the_duel(raizo)
        } 
    }
}

public set_spawn_positions(id,Float:origin[3])
{
    new iHealth = get_pcvar_num( SetHealth );
               
    if( iHealth > 0 )
    {
        set_user_health( id, iHealth );
    }
       
    if(get_user_team(id) == 1)
    {
        his_player_spawn[id][0] = origin[0] - get_pcvar_float(cvar_distance_te);
        his_player_spawn[id][1] = origin[1]
        his_player_spawn[id][2] = origin[2]
       
        his_player_angle(id, Float:{0.0, 0.0, 0.0})       
    }
    if(get_user_team(id) == 2)
    {
        his_player_spawn[id][0] = origin[0] + get_pcvar_float(cvar_distance_ct);
        his_player_spawn[id][1] = origin[1]
        his_player_spawn[id][2] = origin[2]
       
        his_player_angle(id, Float:{0.0, 180.0, 0.0})       
    }
    entity_set_origin(id,his_player_spawn[id])
       
}

public Duel_Dont_Stop(id)
{
    if(is_in_arena[id] && get_pcvar_num(cvar_non_stop) )
    {
        if(get_user_team(id) == 1)
        {
                fVelocity[0] = 250.0;
                fVelocity[1] = 0.0;
                fVelocity[2] = 0.0;
               
                set_pev( id, pev_velocity, fVelocity )
        }
        if(get_user_team(id) == 2)
        {
                fVelocity[0] = -250.0;
                fVelocity[1] = 0.0;
                fVelocity[2] = 0.0;
               
                set_pev( id, pev_velocity, fVelocity )
        }
    }
}

stock his_player_angle( index , Float:fAngle[ 3 ])
{
    entity_set_vector( index , EV_VEC_angles , fAngle );
    entity_set_int( index , EV_INT_fixangle , 1 );
}


Saint Sinner 12-26-2019 04:14

Re: Only one random player
 
Quote:

Originally Posted by Xalus (Post 2678013)
Should just use a simple function like this.

PHP Code:

get_random_challenger(client)
{
  new 
player_list[32], player_amount
  get_players
(player_listplayer_amount"ae", (cs_get_user_team(client) == CS_TEAM_T) ? "CT" "TERRORIST")

  return 
player_listrandom(player_amount) ]



L 12/26/2019 - 11:24:58: [AMXX] Run time error 4: index out of bounds
L 12/26/2019 - 11:24:58: [AMXX] [0] Untitled.sma::get_random_challenger (line 300)
L 12/26/2019 - 11:24:58: [AMXX] [1] Untitled.sma::fwButtonUsed (line 283)

i tried similarity methods but get this error

Code:

public fwButtonUsed(button, raizo)
{
    if( g_bChallenging && is_user_alive ( raizo ))
    {
        client_print( raizo, print_chat, "* Someone is already starting a challenge." );
        return HAM_HANDLED;
    }
   
    static players[32], iPnum;
   
    static player;
   
    get_players(players, iPnum, "ae");
    player = players[random(iPnum)];
   
    g_bChallenging = true;
   
    is_in_arena[raizo] = 1;
    is_in_arena[player] = 1;
   
    cs_set_user_team(raizo,CS_TEAM_T)
    cs_set_user_team(player,CS_TEAM_CT)
   
    set_spawn_positions(raizo,Player_Origin)
    set_spawn_positions(player,Player_Origin)

    return HAM_HANDLED;
}


iceeedr 12-26-2019 08:33

Re: Only one random player
 
PHP Code:

get_random_challenger(Clientbool:TeamTr)
{
    new 
iPlayers[MAX_PLAYERS], iNumiRandom[MAX_PLAYERS 1], iRandomNumid
    get_players
(iPlayersiNum"ae"TeamTr ?  "TERRORIST" "CT"
    for(new 
iiNum;i++)
    { 
        
id iPlayers[i]
        
        if(
Client != id)
        {
            
iRandom[++ iRandomNum] = id
        
}
    }
    return 
iRandom[random_num(1iRandomNum)]



Saint Sinner 12-26-2019 09:31

Re: Only one random player
 
This not will work... This found caller, I want to not found caller... when i pressed button i want to found random players without me!

iceeedr 12-26-2019 09:45

Re: Only one random player
 
updated

Saint Sinner 12-26-2019 13:14

Re: Only one random player
 
1: I want to set spawn location to id !
ID = CALLER
set_spawn_positions(id,Player_Origin)

2: I want to set spawn location to random player!
PLAYER = RANDOM CLIENT
set_spawn_positions(player,Player_Origin)

If search random client i want to no include Caller!

raizo11 12-26-2019 22:31

Re: Only one random player
 
Code:

static players[32], iPnum, player;

get_players(players, iPnum);
player = players[random(iPnum)];
   
if(!is_in_same_team(id,player))
{
    client_print( id, print_chat, "* Some Team Found " );
    return HAM_HANDLED;
}

Code:

stock is_in_same_team(id,enemy)
{
    if(cs_get_user_team(id) == cs_get_user_team(enemy))
        return PLUGIN_HANDLED
    return PLUGIN_CONTINUE
}



All times are GMT -4. The time now is 02:41.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.