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

not the same random player


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
JusTGo
Veteran Member
Join Date: Mar 2013
Old 06-02-2014 , 15:54   not the same random player
Reply With Quote #1

hi, i have this stock that retrive a random player but i don't want to pick the same random play 2 times in row.
PHP Code:
stock get_player_num(teamalive)
{
    static 
player_num
    player_num 
0
    
    
for(new 0g_MaxPlayersi++)
    {
        if(!
is_user_connected(i))
            continue
        if(
alive == AL_NOT)
        {
            if(
is_user_alive(i))
                continue
            } else if(
alive == AL_ALIVE) {
            if(!
is_user_alive(i))
                continue    
        }
        
        if(
team == TEAM_ALL)
        {
            if(
cs_get_user_team(i) == CS_TEAM_UNASSIGNED || cs_get_user_team(i) == CS_TEAM_SPECTATOR)
                continue
            } else if(
team == TEAM_T) {
            if(
cs_get_user_team(i) != CS_TEAM_T)
                continue
            } else if(
team == TEAM_CT) {
            if(
cs_get_user_team(i) != CS_TEAM_CT)
                continue
        }
        
        
player_num++
    }
    
    return 
player_num
}

stock get_random_player(teamalive)
{
    static 
list_player[33], list_player_num
    
static total_player
    total_player 
get_player_num(teamalive)
    
    for(new 
0total_playeri++)
        
list_player[i] = 0
    
    list_player_num 
0
    
    
for(new 0g_MaxPlayersi++)
    {
        if(!
is_user_connected(i))
            continue
        
        if(
alive == AL_NOT)
        {
            if(
is_user_alive(i))
                continue
            } else if(
alive == AL_ALIVE) {
            if(!
is_user_alive(i))
                continue    
        }
        
        if(
team == TEAM_ALL)
        {
            if(
cs_get_user_team(i) == CS_TEAM_UNASSIGNED || cs_get_user_team(i) == CS_TEAM_SPECTATOR)
                continue
            } else if(
team == TEAM_T) {
            if(
cs_get_user_team(i) != CS_TEAM_T)
                continue
            } else if(
team == TEAM_CT) {
            if(
cs_get_user_team(i) != CS_TEAM_CT)
                continue
        }

        
        
list_player[list_player_num] = i
        list_player_num
++
    }
    
    static 
random_playerrandom_player 0
    random_player 
list_player[random_num(0list_player_num 1)]    
    
    return 
random_player

__________________
JusTGo is offline
Kia
AlliedModders Donor
Join Date: Apr 2010
Location: In a world of madness
Old 06-02-2014 , 16:46   Re: not the same random player
Reply With Quote #2

This Stock is not very useful, can be a lot easier.
Just use get_players with random_num and to prevent getting the same player twice, just save the random player you've got and compare it with the next one.
__________________
Kia is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 06-02-2014 , 23:18   Re: not the same random player
Reply With Quote #3

Moved to Scripting Help.
__________________
fysiks is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 06-03-2014 , 06:03   Re: not the same random player
Reply With Quote #4

Quote:
Originally Posted by Kia View Post
This Stock is not very useful, can be a lot easier.
Just use get_players with random_num and to prevent getting the same player twice, just save the random player you've got and compare it with the next one.
i need the same like this one but wiht only not the same player yes i know it looks very compilcated from waht i try

i founded this
PHP Code:
new g_iLastChoosenPlayer

public client_disconnect(id)
{
    if( 
g_iLastChoosenPlayer == id )
    {
        
g_iLastChoosenPlayer 0
    
}
}

ChooseRandomPlayer()
{
    new 
iPlayers[32], iNum
    get_players
(iPlayersiNum"ac")
    if( 
iNum <= )
    {
        return 
iPlayers[0]
    }
    new 
iRandomNum random(iNum)
    new 
iRandomPlayer iPlayersiRandomNum ]
    if( 
iRandomPlayer == g_iLastChoosenPlayer )
    {
        
iPlayersiRandomNum ] = iPlayers[ --iNum ]
        
iRandomPlayer iPlayersrandom(iNum) ]
    }
    
g_iLastChoosenPlayer  iRandomPlayer
    
return iRandomPlayer

__________________
JusTGo is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 06-04-2014 , 04:41   Re: not the same random player
Reply With Quote #5

Something like this would work just fine:

Code:
new Array:g_aPlayers; public plugin_init() {     g_aPlayers = ArrayCreate(); } FillPlayersArray() {     /*         Call this to add all players to the array.     */         ArrayClear( g_aPlayers );         new Players[ 32 ], iNum;     get_players( Players, iNum, "a" );         for( new i ; i < iNum ; i ++ )         ArrayPushCell( g_aPlayers, Players[ i ] ); } AddPlayerArray( id ) {     /*         Call this to add a single player to the array.     */         ArrayPushCell( g_aPlayers, id ) } GetRandomPlayer() {     /*         This will return a random player from the array         and then remove their index so they wont be         chosen again.     */         new iRand = random_num( 0, ArraySize( g_aPlayers ) - 1 );     new iPlayer = ArrayGetCell( g_aPlayers, iRand );         ArrayDeleteItem( g_aPlayers, iRand );         return iPlayer; } PlayerDisconnected( id ) {     /*         Call this when the player disconnects to remove         them from the array.     */         new iSize = ArraySize( g_aPlayers );         for( new i ; i < iSize ; i ++ )     {         if( id == ArrayGetCell( g_aPlayers, i ) )         {             ArrayDeleteItem( g_aPlayers, i );             break;         }     } }
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.

Last edited by hornet; 06-06-2014 at 22:17.
hornet is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 06-04-2014 , 06:56   Re: not the same random player
Reply With Quote #6

Quote:
Originally Posted by hornet View Post
Something like this would work just fine:

Code:
new Array:g_aPlayers; public plugin_init() { &nbsp;&nbsp;&nbsp;&nbsp;g_aPlayers = ArrayCreate(); } FillPlayersArray() { &nbsp;&nbsp;&nbsp;&nbsp;/* &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;Call this to add all players to the array. &nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;ArrayClear( g_aPlayers ); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;new Players[ 32 ], iNum; &nbsp;&nbsp;&nbsp;&nbsp;get_players( Players, iNum, "a" ); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for( new i ; i < iNum ; i ++ ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;ArrayPushCell( g_aPlayers, Players[ i ] ); } AddPlayerArray( id ) { &nbsp;&nbsp;&nbsp;&nbsp;/* &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;Call this to add a single player to the array. &nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;ArrayPushCell( g_aPlayers, id ) } GetRandomPlayer() { &nbsp;&nbsp;&nbsp;&nbsp;/* &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;This will return a random player from the array &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;and then remove their index so they wont be &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;chosen again. &nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;new iRand = random( 0, ArraySize( g_aPlayers ) - 1 ); &nbsp;&nbsp;&nbsp;&nbsp;new iPlayer = ArrayGetCell( g_aPlayers, iRand ); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;ArrayDeleteItem( g_aPlayers, iRand ); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;return iPlayer; } PlayerDisconnected( id ) { &nbsp;&nbsp;&nbsp;&nbsp;/* &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;Call this when the player disconnects to remove &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;them from the array. &nbsp;&nbsp;&nbsp;&nbsp;*/ &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;new iSize = ArraySize( g_aPlayers ); &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;for( new i ; i < iSize ; i ++ ) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;if( id == ArrayGetCell( g_aPlayers, i ) ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;ArrayDeleteItem( g_aPlayers, i ); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;&nbsp;&nbsp;&nbsp;&nbsp;break; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb sp;} &nbsp;&nbsp;&nbsp;&nbsp;} }
thnx Just 2 question i was using get_random_player(TEAM_CT, AL_ALIVE) now what i use ?
and what if the server have 2 players ? and they both choosed player1 choosed in first round and player2 choosed in scond round and 3 round will it choose player 1 ?
__________________
JusTGo is offline
Black Rose
Veteran Member
Join Date: Feb 2011
Location: Stockholm, Sweden
Old 06-04-2014 , 19:05   Re: not the same random player
Reply With Quote #7

Quote:
Originally Posted by hornet View Post
...
You've used random() by mistake instead of random_num().

Quote:
Originally Posted by JusTGo View Post
thnx Just 2 question i was using get_random_player(TEAM_CT, AL_ALIVE) now what i use ?
and what if the server have 2 players ? and they both choosed player1 choosed in first round and player2 choosed in scond round and 3 round will it choose player 1 ?
Depends on what you do with it. It will not return any player (and will throw an error) unless you fill it with players again.

It would be better if you explained what you actually want.
__________________

Last edited by Black Rose; 06-04-2014 at 19:11.
Black Rose is offline
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 06-06-2014 , 22:18   Re: not the same random player
Reply With Quote #8

Quote:
Originally Posted by Black Rose View Post
You've used random() by mistake instead of random_num().
Thanks, updated.
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.
hornet is offline
JusTGo
Veteran Member
Join Date: Mar 2013
Old 06-16-2014 , 18:18   Re: not the same random player
Reply With Quote #9

sorry for being a noob, but this is a bit complicated for me. i was using this get_random_player(TEAM_CT, AL_ALIVE) and i see that your way doesn't have those 2.
__________________
JusTGo is offline
Nextra
Veteran Member
Join Date: Apr 2008
Location: Germany
Old 06-16-2014 , 22:03   Re: not the same random player
Reply With Quote #10

There are some useful randomization examples and general tips in this tutorial by Exolent: https://forums.alliedmods.net/showthread.php?t=74666

In particular his GetRandomPlayers example function could be interesting for you.
__________________
In Flames we trust!

Last edited by Nextra; 06-16-2014 at 22:04.
Nextra 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 01:53.


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