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

Selecting a random player


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Halt
Senior Member
Join Date: Jan 2015
Location: Black Mesa
Old 09-20-2017 , 16:39   Selecting a random player
Reply With Quote #1

So my mod selects a random player on the start of a match to be the first zombie

PHP Code:
GetRandomPlayer()
{
    new 
players[MaxClients 1];
    new 
ClientCount;
    for    (new 
1<= MaxClientsi++)
        if(
IsClientInGame(i))
            if    (
PlayerTeam[i] == TFTeam_Blue && PlayerClass[i] >= && !bWasFirstZombie[i])
            {
                
players[ClientCount++] = i;
                
bWasFirstZombie[i] = true;
            }
    return (
ClientCount == 0) ? -players[GetRandomInt(0ClientCount 1)];

Now I set bWasFirstZombie to true for that client so the next round they wont be selected again. But where would I set it to false. It would have to be after this is fired the next round.
Halt is offline
headline
SourceMod Moderator
Join Date: Mar 2015
Old 09-20-2017 , 17:00   Re: Selecting a random player
Reply With Quote #2

Instead of a Boolean for WasFirstZombie per client, try having a single "first zombie" integer which just holds the client index. So all you have to check is that client != wasFirstZombie

Also if the inside of your if statements is more than one line, you should really be using braces for each exterior branch. In your case, the for loop and the IsClientInGame check should have brackets
headline is offline
Halt
Senior Member
Join Date: Jan 2015
Location: Black Mesa
Old 09-20-2017 , 17:44   Re: Selecting a random player
Reply With Quote #3

Like so?
PHP Code:
new WasFirstZombie 0//<----- Declared an int globally

GetRandomPlayer()
{
    new 
players[MaxClients 1];
    new 
ClientCount;
    for    (new 
1<= MaxClientsi++)
    {
        if(
IsClientInGame(i))
            if    (
PlayerTeam[i] == TFTeam_Blue && PlayerClass[i] >= && != WasFirstZombie[i])
            {
                
players[ClientCount++] = i;
                
WasFirstZombie == i//<---------- Did I set this correctly?
            
}
    }
    return (
ClientCount == 0) ? -players[GetRandomInt(0ClientCount 1)];

I also added the braces after the for loop. Is this what you were getting at?

Last edited by Halt; 09-20-2017 at 17:44.
Halt is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 09-20-2017 , 18:15   Re: Selecting a random player
Reply With Quote #4

== operator checks if the two things are equal and returns true or false
e.g. x == 5 checks if x is 5

= operator assigns value on right hand side to variable on left hand side
e.g. x = 5 makes x hold value 5

not sure what you're going for in your code but WasFirstZombie == i probably wont do what you want it to.
Also you should keep style consistent. You've got tab in front of second if (ew) but no tab in front of first

Last edited by hmmmmm; 09-20-2017 at 18:16.
hmmmmm is offline
Halt
Senior Member
Join Date: Jan 2015
Location: Black Mesa
Old 09-20-2017 , 18:20   Re: Selecting a random player
Reply With Quote #5

Minus the tab mistake which was a mistake, I'm not sure how to apply what hmmmmm is explaining to me. Could I see an example. It doesn't have to be applied to mine. Preferably not I'd learn better.
Halt is offline
fiction
Member
Join Date: May 2017
Old 09-20-2017 , 18:36   Re: Selecting a random player
Reply With Quote #6

Quote:
Originally Posted by Halt View Post
Minus the tab mistake which was a mistake, I'm not sure how to apply what hmmmmm is explaining to me. Could I see an example. It doesn't have to be applied to mine. Preferably not I'd learn better.
You should post some more code around WasFirstZombie and explain what GetRandomPlayer is being used for.
fiction is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 09-20-2017 , 18:37   Re: Selecting a random player
Reply With Quote #7

Code:
int g_FirstZombie = 0;

stock int GetFirstZombie()
{
   int players[MAXPLAYERS+1];
   int maxplayers = 0;

   for (int client = 1; client <= MaxClients; client++)
   {
      if (!IsClientInGame(client))
         continue;

      if (PlayerTeam[client] != TFTeam_Blue)
         continue;

      if (PlayerClass[client] < 1)
         continue;

      if (client == g_FirstZombie)
         continue;

      players[maxplayers++] = client;
   }

   if (maxplayers == 0)
      g_FirstZombie = -1;
   else
      g_FirstZombie = players[GetRandomInt(0, maxplayers - 1)];

   return g_FirstZombie;
}
Not tested. It should compile.

Couple of points.

1. Use the new syntax all the time
2. Name things based on what they do as it makes code more readable.
3. Use the same naming conventions. We normally lowercase all local variables within function scope and camel case everything that is global
4. Avoid loops with i, try to name i as to what it holds
5. Try to avoid heavily nested if statements

The result is a few more lines. But so much more readable.

Coding should always be a balance of readability and not focused completely on less lines.
__________________

Last edited by Neuro Toxin; 09-20-2017 at 18:46.
Neuro Toxin is offline
hmmmmm
Great Tester of Whatever
Join Date: Mar 2017
Location: ...
Old 09-20-2017 , 18:39   Re: Selecting a random player
Reply With Quote #8

PHP Code:
int x;
for( 
int i 05i++ )
{
    if( 
!= )
    {
        
== i// incorrect, this will check if x is equal to i, and return true or false, instead of making x = i
        
i// this is what you're probably trying to do, assigning value of i to x
    
}

Quote:
int players[MaxClients+1];
Won't compile since thats not a fixed size, should be:
PHP Code:
int[] players = new int[MaxClients+1]; 

Last edited by hmmmmm; 09-20-2017 at 18:43.
hmmmmm is offline
fiction
Member
Join Date: May 2017
Old 09-20-2017 , 19:11   Re: Selecting a random player
Reply With Quote #9

Quote:
Originally Posted by hmmmmm View Post
Won't compile since thats not a fixed size, should be:
It shouldn't be MaxClients+1 though, just MaxClients https://github.com/alliedmodders/sou....inc#L186-L198
fiction is offline
Halt
Senior Member
Join Date: Jan 2015
Location: Black Mesa
Old 09-20-2017 , 21:00   Re: Selecting a random player
Reply With Quote #10

Okay so a few questions on Toxins post

PHP Code:
int bFirstZombie 0//Couldnt this be new bFirstZombie?

stock int GetRandomPlayer()
{
    
int Players[MAXPLAYERS]; //Dont understand purpose
    
int MAXPLAYERS 0//Dont understand purpose also compiler threw an error here says it expected an identifier but found integer.
    
    
for    (int Client 1Client <= MaxClientsClient++) //Creates loop and what ends it
    
{
        if(
IsClientInGame(Client)) //Check for valid client
            
continue;
        
        if    (
PlayerTeam[Client] != TFTeam_Blue//Check for client on BLU team
            
continue;
        
        if    (
Client == bFirstZombie//Check for is the player was first zombie? A little confused
            
continue;
        
    }
    
    if    (
MAXPLAYERS == 0//Threw and error on compile (Redundant code constant expression is zero)
        
bFirstZombie = -1;
    else
        
bFirstZombie Players[GetRandomInt(0MAXPLAYERS 1)];
    return 
bFirstZombie;

Questions are in the block above. The compiler threw a few errors.

Secondly whats the point of MaxClients/MaxPlayers?


As for hmmmm's post so we are setting i to 0 then each time the loop runs 1 is added to i. Once I equals 5 the loop stops?

Also at what point is this Toxins code checking for the client index stored in bFirstZombie and selecting a different client if they were the first zombie last round?

Last edited by Halt; 09-20-2017 at 21:04.
Halt 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:14.


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