Raised This Month: $ Target: $400
 0% 

Pick Random Player


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
meng
Veteran Member
Join Date: Oct 2005
Location: us
Old 09-27-2009 , 02:39   Pick Random Player
Reply With Quote #1

what i want to do pick a random player from the available living players.
im clueless. but maybe this helps.... its the general idea...
PHP Code:
Pick_T_Player()
{
    new 
players;
    new 
pickedplayer;
    for (new 
1<= MaxClientsi++)
    {
        if (
IsClientInGame(i) && 
        
IsPlayerAlive(i) &&
        
GetClientTeam(i) == 2)
        {
            
players++;
        }
    }
    
pickedplayer GetRandomInt1players);
    return 
pickedplayer;

i need "pickedplayer" to be a usable id i can use on player_spawn.
meng is offline
Send a message via Yahoo to meng
javalia
Senior Member
Join Date: May 2009
Location: korea, republic of
Old 09-27-2009 , 02:44   Re: Pick Random Player
Reply With Quote #2

i was made almost same thing for my jail server plugin. here.....

Code:
for(new i = 1;i <= 10000; i++){
        
                //죄수들 중에서 랜덤하게 1명을 골라서 죄수대장으로 만든다.
                tempselect = GetRandomInt(1, MaxClients);
        
                if(isplayerconnectedingamealive(tempselect) == true){
                    
                    if(GetClientTeam(tempselect) == 2){
                        
                        new String:gangleadername[64];
                        GetClientName(tempselect, gangleadername, 64);
                        gangleader = tempselect;
                    
                        for(new temp = 1;temp <= MaxClients;temp++){
                        
                            if(isplayerconnectedingamealive(temp) == true){
                        
                                if(GetClientTeam(temp) == 2){
                            
                                    if(temp == gangleader){
                            
                                        PrintToChat(temp, "\x04당신이 이번 라운드의 죄수대장입니다");
                            
                                    }else{
                                
                                        PrintToChat(temp, "\x04%s 님이 이번 라운드의 죄수대장입니다", gangleadername);
                                
                                    }
                            
                                }
                        
                            }
                        
                        }
                    
                        gangleaderhelppanel(tempselect);
                        break;
                    
                    }
            
                }
        
            }
it may more clear code if use while statement.....anyway this is loose way..
some times it will not work....

so...hmm...

maybe u can make a list to save alive t and
just randomy select from the list.
javalia is offline
BrutalGoerge
AlliedModders Donor
Join Date: Jul 2007
Old 09-27-2009 , 02:48   Re: Pick Random Player
Reply With Quote #3

@op that is likely to return an invalid index, or someone not alive and on team #2

PHP Code:
pick()
{
    new 
countclients[MaxClients];
    for (new 
1<= MaxClientsi++)
    {
        if (
IsClientInGame(i) && IsPlayerAlive(i) && GetClientTeam(i) == 2)
            
clients[count++] = i
    }
    return 
clients[GetRandomInt(0,count)];

__________________
My Pluggies If you like, consider to me.

Last edited by BrutalGoerge; 09-27-2009 at 02:51.
BrutalGoerge is offline
javalia
Senior Member
Join Date: May 2009
Location: korea, republic of
Old 09-27-2009 , 02:50   Re: Pick Random Player
Reply With Quote #4

Quote:
Originally Posted by BrutalGoerge View Post
@op that might return an invalid index

PHP Code:
pick()
{
    new 
countclients[MaxClients];
    for (new 
1<= MaxClientsi++)
    {
        if (
IsClientInGame(i) && IsPlayerAlive(i) && GetClientTeam(i) == 2)
            
client[count++] = i
    }
    return 
clients[GetRandomInt(0,count)];

what a incradevl awsome way!
my thing looks dummy......-_-;
javalia is offline
meng
Veteran Member
Join Date: Oct 2005
Location: us
Old 09-27-2009 , 02:58   Re: Pick Random Player
Reply With Quote #5

wow, slick. so the id im looking for is just an integer within MaxClients?

how do call it in player_spawn? since its not global v.

ex.
PHP Code:
new client GetClientOfUserId(GetEventInt(event"userid"));
if (
client == pickedplayer)
    
//do stuff 
meng is offline
Send a message via Yahoo to meng
Lord Canistra
Senior Member
Join Date: Mar 2009
Location: Tallinn, Estonia
Old 09-27-2009 , 02:58   Re: Pick Random Player
Reply With Quote #6

If I understood you correctly...

PHP Code:
new chosen GetRandomInt(1GetClientCount())

while(!
IsPlayerAlive(chosen))    // Here goes checking of whatever you need.
{
    
chosen GetRandomInt(1GetClientCount())
}

// Do something with player.
PrintToChatAll("Chosen id is %i"chosen
__________________
Lord Canistra is offline
meng
Veteran Member
Join Date: Oct 2005
Location: us
Old 09-27-2009 , 03:09   Re: Pick Random Player
Reply With Quote #7

hmm. looking this over. there may be another problem. i need a player to be picked per team. so i need to store ids of players on specific teams. then chose between those ids separately.

and thanks! that all is making sense.

can i do this?
PHP Code:
pick(clientteam)
{
    new 
countpicked[MaxClients];
    for (new 
1<= MaxClientsi++)
    {
        if (
IsClientInGame(i) && IsPlayerAlive(i) && GetClientTeam(i) == clientteam)
            
picked[count++] = i
    }
    return 
picked[GetRandomInt(0,count)];


public 
EventPlayerSpawn(Handle:event, const String:name[],bool:dontBroadcast)
{
    new 
client GetClientOfUserId(GetEventInt(event"userid"));
    new 
clientteam GetClientTeam(client);
    if (
client == pick(clientteam))
        
//do stuff 

Last edited by meng; 09-27-2009 at 03:29.
meng is offline
Send a message via Yahoo to meng
Kigen
BANNED
Join Date: Feb 2008
Old 09-27-2009 , 03:29   Re: Pick Random Player
Reply With Quote #8

Code:
PickPlayerFromTeam(f_iTeam)
{
	new f_iClients[MaxClients+1], f_iCount;
	for(new i=1;i<=MaxClients;i++)
		if ( IsClientInGame(i) && GetClientTeam(i) == f_iTeam && IsPlayerAlive(i) )
			f_iClients[f_iCount++] = i;
	if ( f_iCount == 0 )
		return -1; // No clients to choose from.
	else
		return f_iClients[GetRandomInt(0, f_iCount-1)];
}
Should work. But untested.
Kigen is offline
Theme97
Senior Member
Join Date: Mar 2009
Old 09-27-2009 , 03:56   Re: Pick Random Player
Reply With Quote #9

You're taking the wrong approach.

Say there are two CTs. The first CT spawns. pick() chooses him, so stuff happens. Then the other CT spawns. pick() runs again and also chooses him. Stuff happens to him too.

What you want to do is pick a T and CT on round_start instead of player_spawn.
__________________
I now have very little time to work on stuff and my main computer (which is the only one that can run TF2) is not cooperating with me, so don't expect many updates from me anytime soon.

[ALL] Karaoke
[TF2] Intel Timer | Crabmod | Randomizer | Stopwatch | Crits 4 All
Theme97 is offline
BrutalGoerge
AlliedModders Donor
Join Date: Jul 2007
Old 09-27-2009 , 04:24   Re: Pick Random Player
Reply With Quote #10

slight oversight on my part if you use what i posted... the return line should have 'count -1' since array starts at 0
__________________
My Pluggies If you like, consider to me.
BrutalGoerge 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 18:40.


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