Team Balancer Question
#1

I have searched many team balancer scripts and this is what seemed to turn up:
pawn Code:
new team1;
new team2;

public OnPlayerRequestSpawn(playerid)
{
if(team1 > team2)
{
return 0;
}
else if(team2 > team1)
{
return 0;
}
return 1;
}

public OnPlayerSpawn(playerid)
{
if(Team[playerid] == TEAM_GROVE)
{
team1++;
}
else if(Team[playerid] == TEAM_BALLA)
{
team2++;
}
return 1;
}
But this wouldn't be accurate right? Everytime a player spawns, it would add one to his team variable. So if players on one team died more than others, they would have more added to their variable?
Reply
#2

pawn Code:
public OnPlayerRequestSpawn(playerid)
{
    new balla = GetPlayersInTeamFromMaxPlayers(TEAM_BALLAS);
    new grove = GetPlayersInTeamFromMaxPlayers(TEAM_GROVE);
    if(balla > grove && gTeam[playerid] == TEAM_GROVE)
    {
        GameTextForPlayer(playerid, "~r~Team Full!~n~~w~Choose Another Team!", 3000, 5);
        return 0;
    }
    else if(grove > balla && gTeam[playerid] == TEAM_GROVE)
    {
        GameTextForPlayer(playerid, "~r~Team Full!~n~~w~Choose Another Team!", 3000, 5);
        return 0;
    }
    return 1;
}
pawn Code:
stock GetPlayersInTeamFromMaxPlayers(teamid)
{
    new playercount = 0;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(HasPlayerSpawned[i] == false) continue;
        if(GetPlayerState(i) == PLAYER_STATE_NONE) continue;
        if(gTeam[i] != teamid) continue;
        playercount++;
    }
    return playercount;
}
* The stock is added in my stocks include.
Edit it to your variables.
EDIT :
Add
new bool:HasPlayerSpawned[MAX_PLAYERS];
Ontop of script, sorry, I forgot to mention that.
Reply
#3

Create a variable to check if the player has spawned before.
And decrease it's team size in OnPlayerDisconnect.
pawn Code:
new
    bool: hasSpawnedBefore[ MAX_PLAYERS ],
    team1, team2
;

public OnPlayerDisconnect( playerid, reason )
{
    if ( gTeam[ playerid ] == TEAM_GROVE )
        team1 --;

    else if ( gTeam[ playerid ] == TEAM_BALLA )
        team2 --;

    return 1;
}

public OnPlayerSpawn( playerid )
{
    if ( !hasSpawnedBefore[ playerid ] )
    {
        hasSpawnedBefore[ playerid ] = true;

        if ( Team[ playerid ] == TEAM_GROVE )
            team1 ++;

        else if ( Team[ playerid ] == TEAM_BALLA )
            team2 ++;
    }

    return 1;
}

public OnPlayerRequestSpawn( playerid )
{
    switch ( gTeam[ playerid ] )
    {
        case TEAM_GROVE : { if ( team1 > team2 ) return !GameTextForPlayer( playerid, "~w~TEAM FULL", 3000, 3 ); }
        case TEAM_BALLA : { if ( team2 > team1 ) return !GameTextForPlayer( playerid, "~w~TEAM FULL", 3000, 3 ); }
    }

    return 1;
}
( Set the player team in RequestClass first though ).

@ Darnell

error: HasPlayerSpawned is not defined.
Reply
#4

@Darnell

On Player Spawn, don't I need to set the bool to true?
Reply
#5

So, I've got this:
pawn Code:
public OnPlayerRequestSpawn(playerid)
{
    new team1 = GetPlayersInTeamFromMaxPlayers(TEAM_RED);
    new team2 = GetPlayersInTeamFromMaxPlayers(TEAM_GREEN);
    if(team1 > team2 && Team[playerid] == TEAM_RED && !IsPlayerVIP(playerid))
    {
        TextDrawShowForPlayer(playerid,balancer);
        SetTimer("HideBalancer",4000,0);
        return 0;
    }
    else if(team2 > team1 && Team[playerid] == TEAM_GREEN && !IsPlayerVIP(playerid))
    {
        TextDrawShowForPlayer(playerid,balancer);
        SetTimer("HideBalancer",4000,0);
        return 0;
    }
    return 1;
}

forward HideBalancer(playerid);
public HideBalancer(playerid)
{
    TextDrawHideForPlayer(playerid,balancer);
}

stock GetPlayersInTeamFromMaxPlayers(teamid)
{
    new playercount = 0;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(HasPlayerSpawned[i] == false) continue;
        if(GetPlayerState(i) == PLAYER_STATE_NONE) continue;
        if(Team[i] != teamid) continue;
        playercount++;
    }
    return playercount;
}
Would this work? Or do I need something else?
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)