Jump to content

4 Teams Balancer


Recommended Posts

Hi all,

Ive already made an team balancer for 2 teams but i now need one for 4 teams, but i dont know how to make it like this.

My 2 team balance script:

  
function balanceTeams ( thePlayer ) 
        local groveTeam = getTeamFromName ( "grove" ) 
        local ballasTeam = getTeamFromName ( "ballas" ) 
        local groveCount = countPlayersInTeam ( groveTeam ) 
        local ballasCount = countPlayersInTeam ( ballasTeam ) 
        if groveCount == ballasCount then  
                setPlayerTeam ( thePlayer , groveTeam ) 
        elseif groveCount > ballasCount then  
                setPlayerTeam ( thePlayer , ballasTeam )  
        elseif groveCount < ballasCount then 
                setPlayerTeam ( thePlayer , groveTeam )  
        end 
end 
  

mayby someone here on this forum knows the secret answer?

Greatings DJFrankie, Thx For The Help!

Link to comment

Hmm I'm totally new to LUA and I've never created anything in LUA yet, so here's my try... don't know if it works and stuff, so please let me know how it goes. I want to start programming in LUA and make things for MTA as well, LOL... still lacking time tho :D

Anywayz, I think the player should join the team with the least players... in that case you will always balance as good as possible. If teams are equal, the player just joins the first team. Hmmm, forgive me, I don't know what the team names are, so I just made up a few :P Whatever, here it goes:

function balanceTeams(thePlayer) 
    local teams = {getTeamFromName("grove"), getTeamFromName("ballas"), getTeamFromName("police"), getTeamFromName("gangster")} 
    local pos = 1 
    local min = countPlayersInTeam(teams[pos]) 
    for i = 2, teams.n do 
        count = countPlayersInTeam(teams[i]) 
        if count < c then 
            min = count 
            pos = i 
        end 
    end 
    setPlayerTeam(thePlayer, teams[pos]) 
end 

Alternatively, if you want one count call less, you may store all counts in an array beforehand...

function balanceTeams(thePlayer) 
    local teams = {getTeamFromName("grove"), getTeamFromName("ballas"), getTeamFromName("police"), getTeamFromName("gangster")} 
    local counts = {} 
    for i = 1, teams.n do 
        counts[i] = countPlayersInTeam(teams[i]) 
    end 
    local pos = 1 
    local min = counts[pos] 
    for i = 2, teams.n do 
        if counts[i] < c then 
            min = counts[i] 
            pos = i 
        end 
    end 
    setPlayerTeam(thePlayer, teams[pos]) 
end 

Note this script is extendible to as much teams you like... just add some teams to the teams array et voilà :D Good luck and let me know how it goes! :mrgreen:

Oh by the way... WHY the HELL does LUA start counting at 1? I'm soooo used to Java LOL... LUA seems kinda weird to me, but I'll live :?

Link to comment

Hmm what error messages do you get? Is it syntax? Cuz I'm not sure about the syntax LOL :P Perhaps my arrays are not correct or I use the same variable, i, which may not be valid... I don't know :-s well, since you asked for it, I guess you have some more LUA experience.. ghehe :P

By the way, did you check the team names, since I was not sure of them... oh and erm... if you change it to two teams, does it work?

function balanceTeams(thePlayer) 
    local teams = {getTeamFromName("grove"), getTeamFromName("ballas")} 
    local counts = {} 
    for i = 1, teams.n do 
        counts[i] = countPlayersInTeam(teams[i]) 
    end 
    local pos = 1 
    local min = counts[pos] 
    for i = 2, teams.n do 
        if counts[i] < c then 
            min = counts[i] 
            pos = i 
        end 
    end 
    setPlayerTeam(thePlayer, teams[pos]) 
end 

Link to comment

Oh wait... I see the variable min is highlighted... so I think that's an operator in LUA, hmm.. why don't you try this:

function balanceTeams(thePlayer) 
    local teams = {getTeamFromName("grove"), getTeamFromName("ballas"), getTeamFromName("police"), getTeamFromName("gangster")} 
    local counts = {} 
    for i = 1, teams.n do 
        counts[i] = countPlayersInTeam(teams[i]) 
    end 
    local pos = 1 
    local count = counts[pos] 
    for i = 2, teams.n do 
        if counts[i] < c then 
            count = counts[i] 
            pos = i 
        end 
    end 
    setPlayerTeam(thePlayer, teams[pos]) 
end 

It should work :-k what do you think about the algorithm? Does it make sense to you?

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...