Auto team balance
#1

Hi, im trying to script a AAD command. What i am trying to do is to whenever the command is typed, players are equally distribuited into two teams. How can i do that?
Reply
#2

1) Build a list of all online players
2) Randomize the list
3) Divide number of players by two
4) Set half the players in the list to one team half to the other
Reply
#3

Im trying, but its very confusing, how am i going to list the online players count and then randomize it?
Reply
#4

Here's an idea but it does not use a cmd.

Код:
new TeamPlayers[ <Amount of teams here> ];

public OnPlayerRequestSpawn(playerid)
{
  for(new teams; teams < [Team Amount]; teams++)
  {
    if(teams == GetPlayerTeam(playerid)) continue; //Or gTeam[playerid], or w/e you use.
    if(TeamPlayers[Player Team Variable] > TeamPlayers[teams] + 3)
    {
      SendClientMessage(playerid, COLOR_RED, "You can't join this team as it would unbalance the game!");
      return 0;
    }
  }
}
Reply
#5

Quote:
Originally Posted by [uL]Pottus
Посмотреть сообщение
1) Build a list of all online players
2) Randomize the list
3) Divide number of players by two
4) Set half the players in the list to one team half to the other
Could you give me something to start off? like an example?
Reply
#6

bump
Reply
#7

Here you go, Using zcmd
pawn Код:
CMD:balanceteams(playerid, params[])
{
    new TotalPlayers = 0;
    new A=0, B=1;
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            switch (i)
            {
                case A: SetPlayerTeam(playerid, 1); // First team
                case B: SetPlayerTeam(playerid, 2); // Second team
            }
        }
        A += 2;
        B += 2;
    }
    return 1;
}
player with Id 0 = team 0
Id 1 = team 1
id 2 = team 0
id 3 = team 1 and so on
Reply
#8

It should work:
pawn Код:
new
    half = floatround( Iter_Count(Player) / 2 ),
    count = -1
;
foreach(new i : Player)
{
    if( ++count <= half ) SetPlayerTeam( i, 1 );
    else SetPlayerTeam( i, 2 );
}
Reply
#9

/\ The only thing that will happen is that the teams are uneven ...

That should be what [uL]Pottus said, just compromised
pawn Код:
new
    rand,
    count,
    i = -1,
    tmp[MAX_PLAYERS]
;
// 1)
while(++i != MAX_PLAYERS) {
    if(IsPlayerConnected(i)) {
        tmp[count++] = i;
    }
}
// 2) 3) 4)
for(i = count / 2; count; ) {
    if(i < count) {
        rand = random(count--);

        SetPlayerTeam(tmp[rand], 1);

        tmp[rand] = tmp[count];
    } else {
        SetPlayerTeam(tmp[--count], 2);
    }
}
Reply
#10

Well if the online players are an even number, it will just split them up to the half but if the number of the online players is an odd number (let's say 51), what are we gonna do? Throw a player out? It'd be just 26-25.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)