SA-MP Forums Archive
A simple Team Balancer ++ - 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: A simple Team Balancer ++ (/showthread.php?tid=309737)



A simple Team Balancer ++ - legho - 08.01.2012

Ok ive created a simple balancer for teams so you automaticly get set to which team is lowest on players when you spawn..

Please note: My teams are team 1 and team 2 thats why i have a +1 on the random so there cant be a 0

Код:
stock BalanceTeams(playerid)
{
	if(ServerGameMode == 1)
	{
		if(TeamOp == TeamSpet)
		{
		    PlayerInfo[playerid][pTeam] = random(2)+1;
		    if(PlayerInfo[playerid][pTeam] == 1)
		    {
		        TeamOp ++;
			}
		    if(PlayerInfo[playerid][pTeam] == 2)
		    {
		        TeamSpet ++;
			}
		}
		if(TeamOp > TeamSpet)
		{
		    PlayerInfo[playerid][pTeam] = 2;
		    TeamOp ++;
		}
		if(TeamSpet > TeamOp)
		{
		    PlayerInfo[playerid][pTeam] = 1;
		    TeamSpet ++;
		}
		return 1;
	}
	return 1;
}
But it doesent work any clues why?
This is stopping my whole script working so please please help!


Re: A simple Team Balancer ++ - JamesC - 08.01.2012

If TeamOp has a greater number of players, you put the player in TeamSpet but are increasing the count of TeamOp. Similarly, you are putting the player in TeamOp and increasing the count of TeamSpet. You need to increase the count of the team that the player was put in, not the other team.


Re: A simple Team Balancer ++ - Rob_Maate - 09.01.2012

I'll give you a little snippet out of my code, so you can get the basic idea. You seem to have a relatively firm grasp on pawno, so it shoudln't be a challenge

pawn Код:
public AutoAssign(playerid)
{
    new RedCount, BlueCount;
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(EventTeam[i] == EVENT_TEAM_RED)
        {
            RedCount ++;
        }
        if(EventTeam[i] == EVENT_TEAM_BLUE)
        {
            BlueCount ++;
        }
    }
    if(RedCount > BlueCount)
    {
        return EVENT_TEAM_BLUE;
    }
    if(BlueCount > RedCount)
    {
        return EVENT_TEAM_RED;
    }
    if(BlueCount == RedCount)
    {
        new decide = random(2);
        if(decide == 0)
        {
            return EVENT_TEAM_RED;
        }
        if(decide == 1)
        {
            return EVENT_TEAM_BLUE;
        }
    }
    return 0;
}



Re: A simple Team Balancer ++ - Lorenc_ - 09.01.2012

Quote:
Originally Posted by Rob_Maate
Посмотреть сообщение
I'll give you a little snippet out of my code, so you can get the basic idea. You seem to have a relatively firm grasp on pawno, so it shoudln't be a challenge

pawn Код:
public AutoAssign(playerid)
{
    new RedCount, BlueCount;
    for(new i=0; i<MAX_PLAYERS; i++)
    {
        if(EventTeam[i] == EVENT_TEAM_RED)
        {
            RedCount ++;
        }
        if(EventTeam[i] == EVENT_TEAM_BLUE)
        {
            BlueCount ++;
        }
    }
    if(RedCount > BlueCount)
    {
        return EVENT_TEAM_BLUE;
    }
    if(BlueCount > RedCount)
    {
        return EVENT_TEAM_RED;
    }
    if(BlueCount == RedCount)
    {
        new decide = random(2);
        if(decide == 0)
        {
            return EVENT_TEAM_RED;
        }
        if(decide == 1)
        {
            return EVENT_TEAM_BLUE;
        }
    }
    return 0;
}
Stop using loops!!!!! You can increase/decrease the team count within the if statements... Then do a checktowards which team the player is in and decrease the count. Think more effective :O


Re: A simple Team Balancer ++ - Rob_Maate - 09.01.2012

Lol what the hell?

1loop isn't gonna do much


Re: A simple Team Balancer ++ - Lorenc_ - 09.01.2012

Quote:
Originally Posted by Rob_Maate
Посмотреть сообщение
Lol what the hell?

1loop isn't gonna do much
Well, don't respond at all if you think that is so while there is better code that can be provided and even more shorter... Do a bench mark with that code as well, see which one is faster.

That loop has no check for IsPlayerConnected, meaning it can be unsafe in various ways :O.


Re: A simple Team Balancer ++ - Rob_Maate - 09.01.2012

if(IsPlayerConnected(playerid))
{
AutoAssign(playerid);
}


Re: A simple Team Balancer ++ - legho - 09.01.2012

Hey ok abit confused with the mixed comments personally i cant see much wrong with mine. like i say 2 teams
PlayerInfo[playerid][pTeam] = 1; and PlayerInfo[playerid][pTeam] = 2;
i dont have a team 0 so does the random(2)+1; feature work will that return either a 1 or a 2?

and then if there is anything wrong can someone edit it please and show me what is wrong
Much appreciated


Re: A simple Team Balancer ++ - Lorenc_ - 09.01.2012

Quote:
Originally Posted by legho
Посмотреть сообщение
Hey ok abit confused with the mixed comments personally i cant see much wrong with mine. like i say 2 teams
PlayerInfo[playerid][pTeam] = 1; and PlayerInfo[playerid][pTeam] = 2;
i dont have a team 0 so does the random(2)+1; feature work will that return either a 1 or a 2?

and then if there is anything wrong can someone edit it please and show me what is wrong
Much appreciated
Well, check if the 2 teams are the same, if they are just use "random" to find a team to be assigned to. However don't use the previous guys code, stick with yours and just fix up a bit.


Re: A simple Team Balancer ++ - legho - 10.01.2012

Yeah fixed it up all i need to know
what this will return?

random(2)+1; either a 1 or a 2?