AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Questions about optimization and cs_set_user_team() (https://forums.alliedmods.net/showthread.php?t=324967)

frio 06-02-2020 21:25

Questions about optimization and cs_set_user_team()
 
I want to assign everyone to a random team. This following code loops through all the players and assigns them to a random team if the team is not maxed out, otherwise it will assign them to the other team.
PHP Code:

    for (0numberOfPlayersi++) {
        
rand random_num(01)
        switch(
rand) {
            case 
1: {
                if (
ctCount maxPlayersPerTeam) {
                    
cs_set_user_team(players[i], CS_TEAM_CT)
                    
ctCount++
                } else {
                    
cs_set_user_team(players[i], CS_TEAM_T)
                    
tCount++
                }
            }
            default: {
                if (
tCount maxPlayersPerTeam) {
                    
cs_set_user_team(players[i], CS_TEAM_T)
                    
tCount++
                } else {
                    
cs_set_user_team(players[i], CS_TEAM_CT)
                    
ctCount++
                }
            }
        }
    } 

It works as intended, but I have two questions about optimization:
  1. I know you are not supposed to re-index arrays, but is the above code fine given that I believe you are only ever accessing players[i] once per for-loop?
  2. It is possible that the above code encounters someone on a team and the random variable will have them "transferred" over to the same team. Would it be more optimal to have a check before transferring? Something like:
    PHP Code:

    if (cs_get_user_team(players[i]) != CS_TEAM_CTcs_set_user_team(players[i], CS_TEAM_CT

    I know the above would re-index the array, I would change that afterwards if this is the better way to go.

Edit: One bonus 3rd question: Why do I always see variables in this forum start with "sz"? Like szVariableName?

Bugsy 06-02-2020 21:47

Re: Questions about optimization and cs_set_user_team()
 
1. Correct, you are only accessing players[] once per pass so it is fine.

2. Yeah, see the below code.

For your last question, it's called Hungarian notation. It helps the developer and/or person looking at the code what type of data is held in the variable. sz being null-terminated string.

PHP Code:

    new iPlayer CsTeams:csCurrentTeam csTeamCountCsTeams ];

    for ( 
numberOfPlayers i++ ) 
    {
        
iPlayer players];
        
csCurrentTeam cs_get_user_teamiPlayer );
        
        switch ( 
CsTeams:random_num_:CS_TEAM_T _:CS_TEAM_CT ) ) 
        {
            case 
CS_TEAM_T
            {
                if ( 
csTeamCountCS_TEAM_T ] < maxPlayersPerTeam )
                {
                    if ( 
csCurrentTeam != CS_TEAM_T 
                    {
                        
cs_set_user_teamiPlayer CS_TEAM_T)
                    } 
                    
csTeamCountCS_TEAM_T ]++
                }
                else 
                {
                    
cs_set_user_teamiPlayer CS_TEAM_CT)
                    
csTeamCountCS_TEAM_CT ]++
                }
            }
            case 
CS_TEAM_CT
            {
                if ( 
csTeamCountCS_TEAM_CT ] < maxPlayersPerTeam 
                {
                    if ( 
csCurrentTeam != CS_TEAM_CT )
                    {
                        
cs_set_user_teamiPlayer CS_TEAM_CT)
                    }
                    
csTeamCountCS_TEAM_CT ]++
                } 
                else 
                {
                    
cs_set_user_teamiPlayer CS_TEAM_T)
                    
csTeamCountCS_TEAM_T ]++
                }
            }
        }
    } 


+ARUKARI- 06-02-2020 21:48

Re: Questions about optimization and cs_set_user_team()
 
FYI.
PHP Code:

    new const teams[] = {CS_TEAM_CTCS_TEAM_T};
    new 
count[2];
    new 
id;
    for (
0numberOfPlayersi++) 
    {
        
id players[i];
        
team random_num(01)
        if (
cs_get_user_team(id) != teams[team])
        {
            if (
count[team] < maxPlayersPerTeam)
            {
                
cs_set_user_team(idteams[team])
                
count[team]++
            }
        }
    } 

3:
https://forums.alliedmods.net/showthread.php?t=85274

frio 06-02-2020 22:27

Re: Questions about optimization and cs_set_user_team()
 
Thank you both for your answers! Bugsy's code was very enlightening.

There was also some good info on programming habits that Arukari linked that I will be reading later.

One thing: I probably should have been more specific, but the following code will not increase "count[team]" if the player is already part of the randomly chosen team. This should not be the case, it should increase regardless. But I can fix that myself.

Quote:

Originally Posted by +ARUKARI- (Post 2703747)
PHP Code:

    new const teams[] = {CS_TEAM_CTCS_TEAM_T};
    new 
count[2];
    new 
id;
    for (
0numberOfPlayersi++) 
    {
        
id players[i];
        
team random_num(01)
        if (
cs_get_user_team(id) != teams[team])
        {
            if (
count[team] < maxPlayersPerTeam)
            {
                
cs_set_user_team(idteams[team])
                
count[team]++
            }
        }
    } 


Again, thanks for the speedy replies!

Thread marked as solved.

Bugsy 06-02-2020 22:55

Re: Questions about optimization and cs_set_user_team()
 
frio, your code that I edited wouldn't do this either, but I just made an edit to do so.

HamletEagle 06-03-2020 09:55

Re: Questions about optimization and cs_set_user_team()
 
I want to add that the "sz" thing(hungarian notation) is a matter of style and you are not required to use it. IMO it is not needed anyway as long as you make an effort to name your variables properly.
For example playersCount is clearly a number(integer), naming it iPlayersCount doesn't add any useful information and can make certain variable names too verbose.
Another example: fileLine[128] is clearly a string, you know that even if it's not prefixed with sz.

Anyway, as with any coding style related topic, the most important thing is to pick a style and be consitent. Don't mix multiple styles in the same code and you are good.


All times are GMT -4. The time now is 16:56.

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