SA-MP Forums Archive
Auto team balance - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Auto team balance (/showthread.php?tid=477512)



Auto team balance - GwENiko - 24.11.2013

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?


Re: Auto team balance - Pottus - 24.11.2013

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


Re: Auto team balance - GwENiko - 24.11.2013

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


Re: Auto team balance - Zues - 24.11.2013

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;
    }
  }
}



Re: Auto team balance - GwENiko - 26.11.2013

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?


Re: Auto team balance - GwENiko - 29.11.2013

bump


Re: Auto team balance - xVIP3Rx - 29.11.2013

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


Re: Auto team balance - Konstantinos - 29.11.2013

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 );
}



AW: Auto team balance - Nero_3D - 29.11.2013

/\ 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);
    }
}



Re: Auto team balance - Konstantinos - 29.11.2013

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.