Team Balance and Auto Team Balance
#1

Hey guys, I am making a TDM GM and I was wondering, if you can give me some ways how to balance teams (prevent 5 players joining team 1 and only 2 to team 2) ?
and also a team balance CMD to split them if much disconnected etc
Im not looking for scripts, Im looking for suggestions
Reply
#2

Make a global variable, increase it when player join the team, decrease it when the player disconnect/join another team.

Example;
PHP код:
static TeamBalance 0;
//OnPlayerConnect/Join team
if(TeamBalance == 5) return SCM(..); // or whatever
TeamBalance++;
//OnPlayerDisconnect/Leave team
TeamBalance--;
//You need to modify it if there is more than one team. 
Reply
#3

1. This is wrong section for that.

You can check that by making a variable, increase it when a player joins that class and decrease when a player leaves.
Reply
#4

Quote:
Originally Posted by iLearner
Посмотреть сообщение
1. This is wrong section for that.

You can check that by making a variable, increase it when a player joins that class and decrease when a player leaves.
What's better ?
1. TeamVariable where I store inside the players in each team
2. Simply check how many are in a team using an external stock every time they try to join a team (CheckTeamPlayers) etc.
Reply
#5

i'd prefer first.
Reply
#6

i would prefer using foreach include. it just help shrink the code and work even better!
Lets start with defines, variables and iterators.
Код:
#include a_samp
#include foreach
#include izcmd
new Iterator:TeamRedPlayer<MAX_PLAYERS>;
new Iterator:TeamBluePlayer<MAX_PLAYERS>;
new bool:IsInTDM[MAX_PLAYERS];

#define Team_Red 	1
#define Team_Blue 	2
Well now lets add a command so that we enter a team that can auto balance itself
Код:
CMD:tdm(playerid)
{
	if(Iter_Count(TeamRedPlayer) > Iter_Count(TeamBluePlayer)) // easy
		SetPlayerTeamEx(playerid, Team_Blue);
	else
		SetPlayerTeamEx(playerid, Team_Red);
	return 1;
}
while working with foreach we have made an extended stock for setting a player's team
Код:
stock SetPlayerTeamEx(playerid, teamid)
{
	switch(teamid)
	{
	    case Team_Red:
	    {
			SetPlayerTeam(playerid, Team_Red);///// Setting team id
			SetPlayerColor(playerid, 0xFF0000FF);///// Color Red
			Iter_Add(TeamRedPlayer, playerid);///// Adding in iterator
	    }
	    case Team_Blue:
	    {
			SetPlayerTeam(playerid, Team_Blue); ///// Setting team id
			SetPlayerColor(playerid, 0x0000FFFF); ///// Color blue
			Iter_Add(TeamBluePlayer, playerid); ///// Adding in iterator
	    }
	}
	IsInTDM[playerid] = true; // Confirm that player is in TDM minigame now
	return 1;
}
Well what if a player disconnects? we must exclude him to avoid false counting obviously
Код:

public OnPlayerDisconnect(playerid, reason)
{
	if(IsInTDM[playerid] == true) // checking if true
		ExcludePlayerFromTeam(playerid); //then exclude player
	return 1;
}



stock ExcludePlayerFromTeam(playerid)
{
	switch(GetPlayerTeam(playerid))
	{
		case Team_Red:
		{
			Iter_Remove(TeamRedPlayer, playerid); //// Removing player so he wont be counted
			SetPlayerColor(playerid, 0xFFFF00FF); ///// Color Setting yellow color say no team color
			SetPlayerTeam(playerid, NO_TEAM); ///// Setting team id to none
		}
		case Team_Blue:
		{
			Iter_Remove(TeamRedPlayer, playerid); //// Removing player so he wont be counted
			SetPlayerColor(playerid, 0xFFFF00FF); ///// Color Setting yellow color say no team color
			SetPlayerTeam(playerid, NO_TEAM); ///// Setting team id to none
		}
		default:
			return 0; // in case player was not in the specified team
	}
	IsInTDM[playerid] = false; // ensure that player is no more in this TDM minigame
	return 1;
}
Reply
#7

Quote:
Originally Posted by gurmani11
Посмотреть сообщение
i would prefer using foreach include. it just help shrink the code and work even better!
Lets start with defines, variables and iterators.
Код:
#include a_samp
#include foreach
#include izcmd
new Iterator:TeamRedPlayer<MAX_PLAYERS>;
new Iterator:TeamBluePlayer<MAX_PLAYERS>;
new bool:IsInTDM[MAX_PLAYERS];

#define Team_Red 	1
#define Team_Blue 	2
Well now lets add a command so that we enter a team that can auto balance itself
Код:
CMD:tdm(playerid)
{
	if(Iter_Count(TeamRedPlayer) > Iter_Count(TeamBluePlayer)) // easy
		SetPlayerTeamEx(playerid, Team_Blue);
	else
		SetPlayerTeamEx(playerid, Team_Red);
	return 1;
}
while working with foreach we have made an extended stock for setting a player's team
Код:
stock SetPlayerTeamEx(playerid, teamid)
{
	switch(teamid)
	{
	    case Team_Red:
	    {
			SetPlayerTeam(playerid, Team_Red);///// Setting team id
			SetPlayerColor(playerid, 0xFF0000FF);///// Color Red
			Iter_Add(TeamRedPlayer, playerid);///// Adding in iterator
	    }
	    case Team_Blue:
	    {
			SetPlayerTeam(playerid, Team_Blue); ///// Setting team id
			SetPlayerColor(playerid, 0x0000FFFF); ///// Color blue
			Iter_Add(TeamBluePlayer, playerid); ///// Adding in iterator
	    }
	}
	IsInTDM[playerid] = true; // Confirm that player is in TDM minigame now
	return 1;
}
Well what if a player disconnects? we must exclude him to avoid false counting obviously
Код:

public OnPlayerDisconnect(playerid, reason)
{
	if(IsInTDM[playerid] == true) // checking if true
		ExcludePlayerFromTeam(playerid); //then exclude player
	return 1;
}



stock ExcludePlayerFromTeam(playerid)
{
	switch(GetPlayerTeam(playerid))
	{
		case Team_Red:
		{
			Iter_Remove(TeamRedPlayer, playerid); //// Removing player so he wont be counted
			SetPlayerColor(playerid, 0xFFFF00FF); ///// Color Setting yellow color say no team color
			SetPlayerTeam(playerid, NO_TEAM); ///// Setting team id to none
		}
		case Team_Blue:
		{
			Iter_Remove(TeamRedPlayer, playerid); //// Removing player so he wont be counted
			SetPlayerColor(playerid, 0xFFFF00FF); ///// Color Setting yellow color say no team color
			SetPlayerTeam(playerid, NO_TEAM); ///// Setting team id to none
		}
		default:
			return 0; // in case player was not in the specified team
	}
	IsInTDM[playerid] = false; // ensure that player is no more in this TDM minigame
	return 1;
}
Thanks in advice, I'm not sure if it's worth it but I'll check it out.
Reply
#8

Worth what? If you start using Foreach, you'll save a load of time. If you mean, if it's worth learning or not, then maybe you shouldn't be scripting a server. The more you know with programming, the further you will go. Plugins extend the Pawn Language, includes as well. Adding extra functions, and helping developers tackle issues.
Reply
#9

In my opinion foreach is essential part of a gamemode and except the pre-made iterators, you can make your own iterators which come in handy for many cases.

To add to gurmani11's post, when I read "auto team balance" I remembered something I had written in the past:
pawn Код:
// modified for his version:

AutoTeamBalance()
{
    new i, j, random_player;

    Iter_Clear(TeamRedPlayer);
    Iter_Clear(TeamBluePlayer);

    foreach(i : Player)
    {
        if (/* player is logged in/spawned etc. - able to play in next round, in other words */)
        {
            Iter_Add(TeamRedPlayer, i);
        }
    }

    for (i = 0, j = Iter_Count(TeamRedPlayer) / 2; i <= j; i++)
    {
        random_player = Iter_Random(TeamRedPlayer);
     
        Iter_Add(TeamBluePlayer, random_player);
        Iter_Remove(TeamRedPlayer, random_player);
    }
}
Reply
#10

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
In my opinion foreach is essential part of a gamemode and except the pre-made iterators, you can make your own iterators which come in handy for many cases.

To add to gurmani11's post, when I read "auto team balance" I remembered something I had written in the past:
pawn Код:
// modified for his version:

AutoTeamBalance()
{
    new i, j, random_player;

    Iter_Clear(TeamRedPlayer);
    Iter_Clear(TeamBluePlayer);

    foreach(i : Player)
    {
        if (/* player is logged in/spawned etc. - able to play in next round, in other words */)
        {
            Iter_Add(TeamRedPlayer, i);
        }
    }

    for (i = 0, j = Iter_Count(TeamRedPlayer) / 2; i <= j; i++)
    {
        random_player = Iter_Random(TeamRedPlayer);
     
        Iter_Add(TeamBluePlayer, random_player);
        Iter_Remove(TeamRedPlayer, random_player);
    }
}
Well quite a nice work with random thingy.

what do you think about this?
Код:
stock AutoBalance()
{
	new GetDifference = Iter_Count(TeamRedPlayer) - Iter_Count(TeamBluePlayer);
	if(GetDifference < 2) return 0;
	else
	{
		if(Iter_Count(TeamRedPlayer) > Iter_Count(TeamBluePlayer))
			SetPlayerTeamEx(random(Iter_Count(TeamRedPlayer)), Team_Blue);
		else
			SetPlayerTeamEx(random(Iter_Count(TeamBluePlayer)), Team_Red);
	}
	return 1;
}
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)