AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   random player help Code! (https://forums.alliedmods.net/showthread.php?t=329658)

XxScripteRxX 01-02-2021 17:08

random player help Code!
 
i want wehen i write /random example : amxx selected player roki and when roki is already selected by the plugin and when you write again / random not to select roki only the other players who are not selected roki not to be selected a second time by the plugin I need such a function


Code:

#include <amxmodx>
#include <amxmisc>
#include <fun>

public plugin_init()
{
        register_plugin("Random Player knife arena", "0.2.1", "badboy");
        register_clcmd("say /random", "RandomPlayer");
}

public RandomPlayer(id)
{
        if(~get_user_flags(id) & ADMIN_BAN)
                return PLUGIN_HANDLED;
       
        getweps()
   
        static player1, player2;
        static players[32], iPnum;
        get_players(players, iPnum, "e", "TERRORIST");
       
        player1 = players[random(iPnum)];
       
        if(!player1)
        {
                console_print(id, "[ShowUrSkills] Random player not found.");
                return PLUGIN_HANDLED;
        }
           
        get_players(players, iPnum, "e", "CT");
       
        player2 = players[random(iPnum)];
       
        if(!player2)
        {
                console_print(id, "[ShowUrSkills] Random player not found.");
                return PLUGIN_HANDLED;
        }
           
        static sName[32];
        static const iNameLen = sizeof(sName) - 1
       
        get_user_name(player1, sName, iNameLen);
        client_print(0, print_chat, "[ShowUrSkills] TERRORIST Random player are Chosed %s.", sName);
       
        get_user_name(player2, sName, iNameLen);
        client_print(0, print_chat, "[ShowUrSkills] CT Random player are Chosed %s.", sName);

        return PLUGIN_HANDLED;
}

stock getweps()
{
        new Players[32], PlayersNum, id
        get_players(Players, PlayersNum, "h")
       
        for( new i; i < PlayersNum; i++ )
        {
                id = Players[i]
               
                if( is_user_connected(id) && is_user_alive(id) )
                {

                }
        }
}


fysiks 01-02-2021 18:22

Re: random player help Code!
 
Quote:

Originally Posted by Napoleon_be (Post 2731071)
try this

  • Using a goto is a terrible coding practice in a language that supports other better methods (e.g. loops).
  • repeatedly using random to avoid a repeat is non-deterministic and could cause an infinitely loop (in theory).
  • "player2 == player2" and "player1 == player1" will always return true. Which, coincidentally, puts you into an infinte loop.

I know that I've seen a thread about choosing a random player/number without repeating a player/number but I'm not able to find it at the moment. See if you can find this on the forums somewhere. I have a feeling it might be by Bugsy because he's got a bunch of good code on here.

However, it might only be useful for numbers and a more robust method might be required to handle players because the list of players could actually change between executions of the command. But, it would be a good start and would probably be "good enough" for what you need.

Natsheh 01-02-2021 19:29

Re: random player help Code!
 
here

...
Code:
random_player( const team=-1 ) {    static pnum[4] = {0,0,0,0}, players[4][32], szTeam[12];    switch( team )    {       case 0: copy(szTeam, charsmax(szTeam), "UNA"); // UNA or UNASSIGNED, not sure :)       case 1: copy(szTeam, charsmax(szTeam), "TERRORIST");       case 2: copy(szTeam, charsmax(szTeam), "CT");       case 3: copy(szTeam, charsmax(szTeam), "SPECTATOR");       default: szTeam[0] = 0;    }    new x = max(team, 0);        if(!pnum[x])    {       get_players(players[x], pnum[x], (team == -1) ? "":"e" , szTeam)       if(!pnum[x]) return 0;    }        new iRandom_player, slot;    while ( pnum[x] > 0 && (!is_user_connected(iRandom_player = players[x][(slot=random(pnum[x]))]) || (( 3 >= team >= 0) && get_user_team(iRandom_player) != team) ))    {       pnum[x]--;       players[x][slot] = players[x][pnum[x]];    }            if(pnum[x] == 0) {       get_players(players[x], pnum[x], (team == -1) ? "":"e", szTeam)       iRandom_player = (pnum[x] > 0) ? players[x][random(pnum[x])]:0;    }        if(pnum[x] > 0)    {        pnum[x]--;        players[x][slot] = players[x][pnum[x]];    }    return iRandom_player; }

Give this a test it should work as intended..

Napoleon_be 01-02-2021 22:16

Re: random player help Code!
 
Quote:

Originally Posted by fysiks (Post 2731075)
  • Using a goto is a terrible coding practice in a language that supports other better methods (e.g. loops).
  • repeatedly using random to avoid a repeat is non-deterministic and could cause an infinitely loop (in theory).
  • "player2 == player2" and "player1 == player1" will always return true. Which, coincidentally, puts you into an infinte loop.

I know that I've seen a thread about choosing a random player/number without repeating a player/number but I'm not able to find it at the moment. See if you can find this on the forums somewhere. I have a feeling it might be by Bugsy because he's got a bunch of good code on here.

However, it might only be useful for numbers and a more robust method might be required to handle players because the list of players could actually change between executions of the command. But, it would be a good start and would probably be "good enough" for what you need.

I didn't realize goto was a no-go, could you explain me more why it's better to use loops or for example returns?.

And yea, as i see my checks now it makes sense it won't work as it should.
I'll see if i can find the thread you're talking about.

EDIT: I couldn't find the post you were talking about, so i took my time to work on this a little bit. Not a lot of work, untested, but is probably already way better than what it was first though.

I found one post though, by Connor: https://forums.alliedmods.net/showpo...64&postcount=4

PHP Code:

#pragma semicolon 1

#include <amxmodx>

#define PLUGIN  "Random Player"
#define VERSION "1.0"
#define AUTHOR  "NapoleoN#"

#if !defined MAX_PLAYERS
    
const MAX_PLAYERS 32;
#endif

public plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR);

    
register_clcmd("say /random""sayRandom");
}

public 
sayRandom(id)
{
    if(
get_user_flags(id) & ADMIN_BAN)
    {
        new 
iPlayers[2][MAX_PLAYERS], iNum[2];

        for(new 
isizeof(iNum); i++)
        {
            
get_players(iPlayers[i], iNum[i], "ae"iNum[0] ? "TERRORIST" "CT");
        }

        new 
iRandom[2], iLastRandom[2];
        
iRandom[0] = iPlayers[0][random(iNum[0])]; // TS
        
iRandom[1] = iPlayers[1][random(iNum[1])]; // CT

        
new szName[2][MAX_PLAYERS 1];

        for(new 
isizeof(iRandom); i++)
        {
            if(
iRandom[i] && iLastRandom[i] != iRandom[i])
            {
                
get_user_name(iRandom[i], szName[i][iRandom[i]], charsmax(szName));
                
client_print(0print_chat"[ShowUrSkills] %s Random player has been chosen %s."iRandom[0] ? "TERRORIST" "CT"szName[i][iRandom[i]]);

                
iLastRandom[i] = iRandom[i];

                break;
            }
        }
    }



fysiks 01-03-2021 00:09

Re: random player help Code!
 
Quote:

Originally Posted by Napoleon_be (Post 2731098)
I didn't realize goto was a no-go, could you explain me more why it's better to use loops or for example returns?.

I'm sure there is a bunch of topics out there about this but the first link I found (here) has a bunch of info about the topic.

TLDR: You'll get eaten by a dinosaur!

Natsheh 01-03-2021 00:20

Re: random player help Code!
 
as for the solution i'd recommend using the function i wrote in post #4

XxScripteRxX 01-03-2021 03:53

Re: random player help Code!
 
Quote:

Originally Posted by Napoleon_be (Post 2731071)
try this

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <fun>

static player1player2;

public 
plugin_init()
{
    
register_plugin("Random Player knife arena""0.2.1""badboy");
    
register_clcmd("say /random""RandomPlayer");
}

public 
RandomPlayer(id)
{
    if(~
get_user_flags(id) & ADMIN_BAN)
        return 
PLUGIN_HANDLED;

    @
SamePlayerFound:

    
getweps()

    static 
players[32], iPnum;
    
get_players(playersiPnum"e""TERRORIST");

    
player1 players[random(iPnum)];

    if(!
player1)
    {
        
console_print(id"[ShowUrSkills] Random player not found.");
        return 
PLUGIN_HANDLED;
    }

    if(
player1 == player1)
    {
        
console_print(id"[ShowUrSkills] Duplicate ts player chosen, retrying");
        goto @
SamePlayerFound;
    }

    
get_players(playersiPnum"e""CT");

    
player2 players[random(iPnum)];

    if(!
player2)
    {
        
console_print(id"[ShowUrSkills] Random player not found.");
        return 
PLUGIN_HANDLED;
    }

    if(
player2 == player2)
    {
        
console_print(id"[ShowUrSkills] Duplicate ct player chosen, retrying");
        goto @
SamePlayerFound;
    }

    static 
sName[32];
    static const 
iNameLen sizeof(sName) - 1

    get_user_name
(player1sNameiNameLen);
    
client_print(0print_chat"[ShowUrSkills] TERRORIST Random player are Chosed %s."sName);

    
get_user_name(player2sNameiNameLen);
    
client_print(0print_chat"[ShowUrSkills] CT Random player are Chosed %s."sName);

    return 
PLUGIN_HANDLED;
}

stock getweps()
{
    new 
Players[32], PlayersNumid
    get_players
(PlayersPlayersNum"h")

    for( new 
iPlayersNumi++ )
    {
        
id Players[i]

        if( 
is_user_connected(id) && is_user_alive(id) )
        {

        }
    }




I tried all the codes from the people who answered here but the codes don't work and wehen i type /random server get crash by this code

XxScripteRxX 01-03-2021 03:57

Re: random player help Code!
 
Quote:

Originally Posted by Natsheh (Post 2731112)
as for the solution i'd recommend using the function i wrote in post #4

i tryed but wehen i write /random nothing are showing

Natsheh 01-03-2021 04:01

Re: random player help Code!
 
Quote:

Originally Posted by XxScripteRxX (Post 2731123)
i tryed but wehen i write /random nothing are showing

can you show the code after you've edit?

XxScripteRxX 01-03-2021 04:04

Re: random player help Code!
 
Quote:

Originally Posted by Natsheh (Post 2731124)
can you show the code after you've edit?


so need like this ?


Code:

#include <amxmodx>
#include <amxmisc>
#include <fun>

public plugin_init()
{
        register_plugin("Random Player knife arena", "0.2.1", "badboy");
        register_clcmd("say /random", "RandomPlayer");
}

public RandomPlayer(id)
{
        if(~get_user_flags(id) & ADMIN_BAN)
                return PLUGIN_HANDLED;
       
        getweps()
   
        static player1, player2;
        static players[32], iPnum;
        get_players(players, iPnum, "e", "TERRORIST");
       
        player1 = players[random(iPnum)];
       
        if(!player1)
        {
                console_print(id, "[ShowUrSkills] Random player not found.");
                return PLUGIN_HANDLED;
        }
           
        get_players(players, iPnum, "e", "CT");
       
        player2 = players[random(iPnum)];
       
        if(!player2)
        {
                console_print(id, "[ShowUrSkills] Random player not found.");
                return PLUGIN_HANDLED;
        }
           
        static sName[32];
        static const iNameLen = sizeof(sName) - 1
       
        get_user_name(player1, sName, iNameLen);
        client_print(0, print_chat, "[ShowUrSkills] TERRORIST Random player are Chosed %s.", sName);
       
        get_user_name(player2, sName, iNameLen);
        client_print(0, print_chat, "[ShowUrSkills] CT Random player are Chosed %s.", sName);

        return PLUGIN_HANDLED;
}

random_player( const team=-1 )
{
  static pnum[4] = {0,0,0,0}, players[4][32], szTeam[12];
  switch( team )
  {
      case 0: copy(szTeam, charsmax(szTeam), "UNA"); // UNA or UNASSIGNED, not sure :)
      case 1: copy(szTeam, charsmax(szTeam), "TERRORIST");
      case 2: copy(szTeam, charsmax(szTeam), "CT");
      case 3: copy(szTeam, charsmax(szTeam), "SPECTATOR");
      default: szTeam[0] = 0;
  }

  new x = max(team, 0);
 
  if(!pnum[x])
  {
      get_players(players[x], pnum[x], (team == -1) ? "":"e" , szTeam)
      if(!pnum[x]) return 0;
  }
 
  new iRandom_player, slot;
  while ( pnum[x] > 0 && (!is_user_connected(iRandom_player = players[x][(slot=random(pnum[x]))]) || (( 3 >= team >= 0) && get_user_team(iRandom_player) != team) ))
  {
      pnum[x]--;
      players[x][slot] = players[x][pnum[x]];
  }
 
 
  if(pnum[x] == 0) {
      get_players(players[x], pnum[x], (team == -1) ? "":"e", szTeam)
      iRandom_player = (pnum[x] > 0) ? players[x][random(pnum[x])]:0;
  }
 
  if(pnum[x] > 0)
  {
      pnum[x]--;
      players[x][slot] = players[x][pnum[x]];
  }
  return iRandom_player;
}

stock getweps()
{
        new Players[32], PlayersNum, id
        get_players(Players, PlayersNum, "h")
       
        for( new i; i < PlayersNum; i++ )
        {
                id = Players[i]
               
                if( is_user_connected(id) && is_user_alive(id) )
                {

                }
        }
}



All times are GMT -4. The time now is 14:13.

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