SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 10/09/2011, 02:03 PM   #1
Tigerbeast11
Gangsta
 
Join Date: Aug 2009
Posts: 771
Reputation: 2
Default Team Balancer Question

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?
Tigerbeast11 is offline   Reply With Quote
Old 10/09/2011, 02:10 PM   #2
Darnell
Gangsta
 
Darnell's Avatar
 
Join Date: Jul 2010
Posts: 872
Reputation: 34
Default Re: Team Balancer Question

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.
Darnell is offline   Reply With Quote
Old 10/09/2011, 02:11 PM   #3
Basicz
Gangsta
 
Basicz's Avatar
 
Join Date: Aug 2010
Location: Indonesia, Surabaya.
Posts: 630
Reputation: 34
Default Re: Team Balancer Question

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.
__________________

- Quina, from the game " Final Fantasy IX "
Basicz is offline   Reply With Quote
Old 10/09/2011, 02:28 PM   #4
Tigerbeast11
Gangsta
 
Join Date: Aug 2009
Posts: 771
Reputation: 2
Default Re: Team Balancer Question

@Darnell

On Player Spawn, don't I need to set the bool to true?
Tigerbeast11 is offline   Reply With Quote
Old 10/09/2011, 02:51 PM   #5
Tigerbeast11
Gangsta
 
Join Date: Aug 2009
Posts: 771
Reputation: 2
Default Re: Team Balancer Question

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?
Tigerbeast11 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Team balancer? -Rebel Son- Help Archive 4 07/04/2011 10:22 PM
Team Balancer? sciman001 Help Archive 11 20/03/2011 03:59 AM
Team Balancer marinov Help Archive 2 07/11/2010 11:46 PM
Help me with Team Balancer Awerop Help Archive 2 09/04/2010 12:46 PM
Another question .... anyone can help me with a good team balancer .. ? AlbanianGuy Help Archive 5 07/02/2010 03:09 AM


All times are GMT. The time now is 05:57 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.