Raised This Month: $ Target: $400
 0% 

random player help Code!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
XxScripteRxX
Member
Join Date: Aug 2019
Old 01-02-2021 , 17:08   random player help Code!
Reply With Quote #1

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) )
		{

		}
	}
}
XxScripteRxX is offline
Old 01-02-2021, 17:43
Napoleon_be
This message has been deleted by Napoleon_be.
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-02-2021 , 18:22   Re: random player help Code!
Reply With Quote #2

Quote:
Originally Posted by Napoleon_be View Post
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.
__________________

Last edited by fysiks; 01-02-2021 at 18:25.
fysiks is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-02-2021 , 19:29   Re: random player help Code!
Reply With Quote #3

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..
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-03-2021 at 01:31.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
Napoleon_be
Veteran Member
Join Date: Jul 2011
Location: Belgium
Old 01-02-2021 , 22:16   Re: random player help Code!
Reply With Quote #4

Quote:
Originally Posted by fysiks View Post
  • 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;
            }
        }
    }

__________________

Last edited by Napoleon_be; 01-02-2021 at 23:09.
Napoleon_be is offline
Send a message via Skype™ to Napoleon_be
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 01-03-2021 , 00:09   Re: random player help Code!
Reply With Quote #5

Quote:
Originally Posted by Napoleon_be View Post
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!
__________________
fysiks is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-03-2021 , 00:20   Re: random player help Code!
Reply With Quote #6

as for the solution i'd recommend using the function i wrote in post #4
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !


Last edited by Natsheh; 01-03-2021 at 00:21.
Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
XxScripteRxX
Member
Join Date: Aug 2019
Old 01-03-2021 , 03:53   Re: random player help Code!
Reply With Quote #7

Quote:
Originally Posted by Napoleon_be View Post
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

Last edited by XxScripteRxX; 01-03-2021 at 03:56.
XxScripteRxX is offline
XxScripteRxX
Member
Join Date: Aug 2019
Old 01-03-2021 , 03:57   Re: random player help Code!
Reply With Quote #8

Quote:
Originally Posted by Natsheh View Post
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
XxScripteRxX is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 01-03-2021 , 04:01   Re: random player help Code!
Reply With Quote #9

Quote:
Originally Posted by XxScripteRxX View Post
i tryed but wehen i write /random nothing are showing
can you show the code after you've edit?
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
XxScripteRxX
Member
Join Date: Aug 2019
Old 01-03-2021 , 04:04   Re: random player help Code!
Reply With Quote #10

Quote:
Originally Posted by Natsheh View Post
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) )
		{

		}
	}
}
XxScripteRxX is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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