Raised This Month: $32 Target: $400
 8% 

Solved Questions about optimization and cs_set_user_team()


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
frio
Junior Member
Join Date: May 2020
Old 06-02-2020 , 21:25   Questions about optimization and cs_set_user_team()
Reply With Quote #1

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?

Last edited by frio; 06-02-2020 at 22:28. Reason: Marked as solved.
frio is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-02-2020 , 21:47   Re: Questions about optimization and cs_set_user_team()
Reply With Quote #2

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 ]++
                }
            }
        }
    } 
__________________

Last edited by Bugsy; 06-02-2020 at 22:54.
Bugsy is offline
+ARUKARI-
AlliedModders Donor
Join Date: Jul 2004
Location: Japan
Old 06-02-2020 , 21:48   Re: Questions about optimization and cs_set_user_team()
Reply With Quote #3

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
__________________
GitHub
SteamWishlist

六四天安門事件

Last edited by +ARUKARI-; 06-02-2020 at 21:55.
+ARUKARI- is offline
frio
Junior Member
Join Date: May 2020
Old 06-02-2020 , 22:27   Re: Questions about optimization and cs_set_user_team()
Reply With Quote #4

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- View Post
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.
frio is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-02-2020 , 22:55   Re: Questions about optimization and cs_set_user_team()
Reply With Quote #5

frio, your code that I edited wouldn't do this either, but I just made an edit to do so.
__________________
Bugsy is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 06-03-2020 , 09:55   Re: Questions about optimization and cs_set_user_team()
Reply With Quote #6

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.
__________________

Last edited by HamletEagle; 06-03-2020 at 09:59.
HamletEagle is offline
Reply


Thread Tools
Display Modes

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 04:05.


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