AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Suggestions / Requests (https://forums.alliedmods.net/forumdisplay.php?f=12)
-   -   Randomize Teams (https://forums.alliedmods.net/showthread.php?t=330553)

ssproxima 02-09-2021 23:27

Randomize Teams
 
Hello,

Can any body help me with a plugin in which if an admin types /random the players of both team gets randomly put in both teams and it can only be used one time per map.

raizo11 02-10-2021 05:35

Re: Randomize Teams
 
Code:

/* Plugin generated by AMXX-Studio */

#include <amxmodx>
#include <amxmisc>
#include <cstrike>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "My Computer"

new One_Time = 1;

public plugin_init() {
        register_plugin(PLUGIN, VERSION, AUTHOR)
       
        register_clcmd( "say /random","Command_Random" );
}


public Command_Random(id)
{
    new flags = get_user_flags(id)
   
    if(!(flags & ADMIN_KICK))
    {
        client_print(id,print_chat,"You have no access to this command")
        return PLUGIN_HANDLED
    }
   
    if(One_Time == 1)
    {
       
        new num, players[32]
       
        get_players(players,num)
       
        for(new i=0;i<=num;i++)
        {
            if(cs_get_user_team(players[i])==CS_TEAM_SPECTATOR)
              continue
               
            new rannum = random_num(1,2)
           
            if(rannum==1)
            {
                cs_set_user_team(players[i],CS_TEAM_T)
            }
            else
            {
                cs_set_user_team(players[i],CS_TEAM_CT)
            }
        }
        client_print(id,print_chat,"You have changed successfully the teams!")
    }
    else
    {
            client_print(id,print_chat,"Only one time per map")
    }
    One_Time = 0
   
    return PLUGIN_HANDLED
}


ssproxima 02-10-2021 09:19

Re: Randomize Teams
 
Thanks will try it out and give feedback :)

Natsheh 02-10-2021 14:28

Re: Randomize Teams
 
raizo why're you creating a 33 sized array when 1 boolean variable will just do fine.

also

checking if user is connected is useless since get_players function is guaranteed to return connected players on the runtime.

an addition on creating variables inside a loop is highly not recommended + save your indexed array in an int variable rather than reindexing it everytime.

Code:

        for(new i=0;i<=num;i++)
        {
            if(!is_user_connected(players[i]))
                continue
               
            if(cs_get_user_team(players[i])==CS_TEAM_SPECTATOR)
              continue
               
            new rannum = random_num(1,2)
           
            if(rannum==1)
            {
                cs_set_user_team(players[i],CS_TEAM_T)
            }
            else
            {
                cs_set_user_team(players[i],CS_TEAM_CT)
            }
        }


ssproxima 02-10-2021 22:12

Re: Randomize Teams
 
The plugin works as expected but I was able to randomize many times in a round of course I tested it with bots and not real players on server. Would it affect?

Napoleon_be 02-11-2021 06:33

Re: Randomize Teams
 
try this, i didn't test it.

PHP Code:

/* Sublime AMXX Editor v2.2 */

#pragma semicolon 1

#include <amxmodx>
// #include <amxmisc>
#include <cstrike>
// #include <engine>
// #include <fakemeta>
// #include <hamsandwich>
// #include <fun>
// #include <xs>
// #include <sqlx>

#define PLUGIN  "Randomize Teams"
#define VERSION "1.0"
#define AUTHOR  "NapoleoN#"

#if !defined MAX_PLAYERS
    
const MAX_PLAYERS 32;
#endif

new bool:bUsed;

public 
plugin_init()
{
    
register_plugin(PLUGINVERSIONAUTHOR);

    
register_clcmd("say /random""randomizeTeams");
}

public 
randomizeTeams(id)
{
    if(
get_user_flags(id) & ADMIN_KICK)
    {
        if(!
bUsed)
        {
            new 
iPlayers[MAX_PLAYERS], iNum;
            
get_players(iPlayersiNum);

            for(new 
iiNumi++)
            {
                
cs_set_user_team(iPlayers[i], CS_TEAM_T);

                if(
iPlayers[1] <= iNum)
                {
                    
cs_set_user_team(iPlayers[1], CS_TEAM_CT);
                }
            }
            
bUsed true;
        }
    }



Natsheh 02-11-2021 07:33

Re: Randomize Teams
 
He asked for the command to be used once per map not to be used for each player just once.

Also Napoleon if(iPlayers[i++] the I is post increment so it will return the old value.

Napoleon_be 02-11-2021 08:14

Re: Randomize Teams
 
You're totaly right, updated my code. Should all be fine now.

Natsheh 02-11-2021 11:24

Re: Randomize Teams
 
your method of randomizing is kind incorrect and confusing.

here a simple way.

PHP Code:

public randomizeTeams(id)
{
    if(
get_user_flags(id) & ADMIN_KICK)
    {
        if(!
bUsed)
        {
            new 
iPlayers[MAX_PLAYERS], iNum;
            
get_players(iPlayersiNum);
            if(
iNum <= 1)
            {
                        
client_print(idprint_chat"There must be atleast 2 players to randomize!");
                        return;
            }

            new 
iHalf iNum 2

            
for(new ijplayeriHalfi++)
            {
                    
player iPlayers[(j=random(iNum))];
                    
cs_set_user_team(playerCS_TEAM_CT);
                    
iPlayers[j] = iPlayers[--iNum];
            }

            for(new 
ijplayeriHalfi++)
            {
                    
player iPlayers[(j=random(iNum))];
                    
cs_set_user_team(playerCS_TEAM_T);
                    
iPlayers[j] = iPlayers[--iNum];
            }

            
bUsed true;
        }
    }



Napoleon_be 02-12-2021 15:57

Re: Randomize Teams
 
Was thinking of doing it that way too first, but couldn't really figure out how to and didn't really want to spend too much time testing it.


All times are GMT -4. The time now is 19:54.

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