Raised This Month: $51 Target: $400
 12% 

Random player/s.


Post New Thread Reply   
 
Thread Tools Display Modes
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 05-09-2009 , 15:31   Re: Random player/s.
Reply With Quote #11

Quote:
Originally Posted by Lee View Post
I don't know why you guys are using set_task() and recursion when a simple while loop would be more efficient.
There's no need even for a while.
SnoW is offline
Send a message via MSN to SnoW
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 05-09-2009 , 15:53   Re: Random player/s.
Reply With Quote #12

Indeed (depending on what approach to collecting valid players is taken). Here's an efficient and simple solution to a simple problem.

Code:
#include <amxmodx> new bool:isConnected[33]; public client_putinserver(id)     isConnected[id] = true;     public client_disconnect(id)     isConnected[id] = false; getRandomPlayer() {     //returns player index or 0 if no players are found         static playerList[32];     new playerCount;         for(new i = 1; i < 33; i++)     {         if(!isConnected[i])             continue;                     if(1 < get_user_team(i) < 4)             playerList[playerCount++] = i;     }             return playerCount ? playerList[random_num(0, playerCount - 1)] : 0; }

Edit: this doesn't meet the request because I was following what the the other three did. :/
__________________
No support via PM.

Last edited by Lee; 05-09-2009 at 16:07.
Lee is offline
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 05-09-2009 , 16:55   Re: Random player/s.
Reply With Quote #13

Lee is right, a loop is enough.
this is for 1 random player:

PHP Code:
#include <amxmodx>


public plugin_init()
    
register_event("HLTV""NewRound""a""1=0""2=0");

public 
NewRound()
{
    new 
playersnum get_playersnum();
    new 
randPlayer;
    
    do
        
randPlayer random_num(1playersnum);
    
    while (
/* randPlayer is a invalid player */);

__________________
O o
/Ż________________________
| IMMA FIRIN' MAH LAZOR!!!
\_ŻŻŻ
Dores is offline
padilha007
Senior Member
Join Date: Jul 2008
Old 05-09-2009 , 17:34   Re: Random player/s.
Reply With Quote #14

Quote:
Originally Posted by Exolent[jNr] View Post
padilha's way is only good if you want 1 player.
If you want random players, see the tutorial that arkshine gave.

@SnoW

try use my code before say anything
__________________

padilha007 is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 05-09-2009 , 17:35   Re: Random player/s.
Reply With Quote #15

Code:
GetRandomPlayer() {     new players[32], pnum;     get_players(players, pnum);         if( !pnum ) return 0;         return players[random(pnum)]; }

It's been shown that get_players() is faster than looping through players.
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
SnoW
Veteran Member
Join Date: Oct 2008
Location: Finland WisdomNuggets: 8
Old 05-10-2009 , 04:21   Re: Random player/s.
Reply With Quote #16

Quote:
Originally Posted by padilha007 View Post
@SnoW

try use my code before say anything
And why I should do that? I already said the problems that it had and attached a code which is five times better than yours. I don't need to test it since I see from the code that it isn't efficient at all.
SnoW is offline
Send a message via MSN to SnoW
micke1101
Veteran Member
Join Date: Jan 2008
Location: Banned-town
Old 05-10-2009 , 04:36   Re: Random player/s.
Reply With Quote #17

Thank you all .
I guess ill use padilha's function.
But isnt it possible that it takes same person twice? (since i wanna make 4 players ct (with different equipments) and the rest terrorist (1 with special maybe that could be done with checking who has the bomb.))
micke1101 is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 05-10-2009 , 10:30   Re: Random player/s.
Reply With Quote #18

Hah, this forum never changes, discussing minor details about code that doesn't even meet the requirements.

padilha007: you've been told what's wrong with your code. You're using set_task() as a control structure and that's simply not what it's for. Your code also includes a redundant test.

Exolent: whether or not to use get_players() once in a single round is a matter of style. The speed difference, if measurable, is negligible. Not only that, but you need to determine if the players are alive (or on the correct team), which is "unsupported" using get_players() flags for whatever reason.

Dores: that code will result in a infinite loop if there's no valid players connected. You also include a similar bug as hleV (by assuming player indexes are consecutive and begin at 1) and so could be stuck in an infinite loop even if there are valid players connected.

micke1101: you need to follow Exolent's tutorial. As has been stated multiple times, none of the code here meets your request. Post again if you need help with it.
__________________
No support via PM.

Last edited by Lee; 05-10-2009 at 10:54.
Lee is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-10-2009 , 10:58   Re: Random player/s.
Reply With Quote #19

Here's a way you could do it. This will only return a player that has not yet been selected. You could modify the code to make it automatically reset selected players when all players have been selected. If you are unsure how to do this just ask.

PHP Code:
new g_Selected;
new 
g_MaxPlayers;

g_MaxPlayers get_maxplayers();

ResetSelected()
{
    
g_Selected 0;
}

GetRandomPlayer()
{
    new 
iPlayerBits;
    new 
i;
    
    
//Make a bitsum of connected player indexes. 
    //You can add additional checks here (bot,team,admin, etc).
    
for ( <= g_MaxPlayers i++ )
        if ( 
is_user_connected) )
            
iPlayerBits |= ( << ( ) );
    
    
//No players are connected.
    
if ( !iPlayerBits )
        return 
0;

    
//Keep looping until there is no players that haven't been selected.
    
while( iPlayerBits != g_Selected )
    {
        
//Get random player id
        
= ( random_numg_MaxPlayers ) - );
        
        
//If player is connected and has not yet been selected
        
if ( ( iPlayerBits & ( << ) ) && !( g_Selected & ( << ) ) )
        {
            
//Set bit in g_Selected bit-field so they will be ignored on the next call
            
g_Selected |= ( << );
            return ( 
);
        }
    }
    
    
//All players have already been selected.
    
return 0;

__________________

Last edited by Bugsy; 05-10-2009 at 11:26.
Bugsy is online now
Dores
Veteran Member
Join Date: Jun 2008
Location: You really don't wanna k
Old 05-10-2009 , 16:39   Re: Random player/s.
Reply With Quote #20

I've posted that code knowing that it might result in a infinite loop. the only thing needs to be changed is using max players instead of players amount, and to make sure that the players amount is not 0.
__________________
O o
/Ż________________________
| IMMA FIRIN' MAH LAZOR!!!
\_ŻŻŻ
Dores 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 11:25.


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