ETMods.net

N!tmod, a Wolfenstein: Enemy Territory Modification!

You are not logged in.

Announcement

You can donate to help us keeping services online.

#1 10-Sep-25 11:00:54

Drago
Member
Registered: 07-May-23
Posts: 25

custom balance.lua

Hello!
Here's my observation:
Players don't like being moved mid-game when teams are unbalanced because a player disconnects.

Here's a lua file that helps balance teams. It's very effective and works wonderfully. Its strong point is that it balances players who join games in progress.
What I'd like to see in this script is to neutralize the part that moves the last player who joined the game because another player left.
I tried to do it myself but I didn't succeed.
I know there are pros on this forum who use "lua" files.
So I'm asking for your help.

Sincerely,


modname = "Auto balance"
version = "0.2"

function et_InitGame(levelTime,randomSeed,restart)
    et.RegisterModname(modname .. " " .. version)
end

-- Max diff pour les joueurs (laisser 2)
unevenDiff = 2

-- Max diff pour les bots (laisser 1 (permet de faire du 2v2 au lieu du 1v3))
unevenBotDiff = 1

-- Temps de check
max_unevenTime = 20

-- Max diff pour les joueurs (laisser 4)
max_unevenDiff = 4

axisPlayers = {}
alliedPlayers = {}
unevenTime = -1

function et_RunFrame(levelTime)
    local numAlliedPlayers = #alliedPlayers
    local numAxisPlayers = #axisPlayers

    -- Count the number of real players in each team
    local numAlliedRealPlayers = 0
    local numAxisRealPlayers = 0

    -- Count the number of bots in each team
    local numAlliedBots = 0
    local numAxisBots = 0

    local maxClients = tonumber(et.trap_Cvar_Get("sv_maxclients"))

    for i = 0, maxClients - 1 do
        local team = tonumber(et.gentity_get(i, "sess.sessionTeam"))
        local isBot = tonumber(et.gentity_get(i, "pers.localClient"))

        if team ~= nil and isBot ~= nil then
            if isBot == 0 then
                if team == 1 then
                    numAxisRealPlayers = numAxisRealPlayers + 1
                elseif team == 2 then
                    numAlliedRealPlayers = numAlliedRealPlayers + 1
                end
            elseif isBot == 1 then
                if team == 1 then
                    numAxisBots = numAxisBots + 1
                elseif team == 2 then
                    numAlliedBots = numAlliedBots + 1
                end
            end
        end
    end

    -- Balance teams based on real players
    if numAlliedRealPlayers >= (numAxisRealPlayers + unevenDiff) then
        local clientNum = alliedPlayers[numAlliedPlayers]
        et.trap_SendConsoleCommand(et.EXEC_APPEND, "put " .. clientNum .. " r ; qsay Team balancing... " .. et.gentity_get(clientNum, "pers.netname") .. "^7 moved to ^1AXIS")
    elseif numAxisRealPlayers >= (numAlliedRealPlayers + unevenDiff) then
        local clientNum = axisPlayers[numAxisPlayers]
        et.trap_SendConsoleCommand(et.EXEC_APPEND, "put " .. clientNum .. " b ; qsay Team balancing... " .. et.gentity_get(clientNum, "pers.netname") .. "^7 moved to ^4ALLIES")
    elseif numAlliedRealPlayers >= (numAxisRealPlayers + max_unevenDiff) then
        local clientNum = alliedPlayers[numAlliedPlayers]
        et.trap_SendConsoleCommand(et.EXEC_APPEND, "put " .. clientNum .. " r ; qsay Team balancing... " .. et.gentity_get(clientNum, "pers.netname") .. "^7 moved to ^1AXIS")
    elseif numAxisRealPlayers >= (numAlliedRealPlayers + max_unevenDiff) then
        local clientNum = axisPlayers[numAxisPlayers]
        et.trap_SendConsoleCommand(et.EXEC_APPEND, "put " .. clientNum .. " b ; qsay Team balancing... " .. et.gentity_get(clientNum, "pers.netname") .. "^7 moved to ^4ALLIES")
    else
        unevenTime = -1
    end

    -- Balance teams based on bots
    if numAlliedBots > numAxisBots + unevenBotDiff then
        local numBotsToMove = numAlliedBots - numAxisBots
        for i = 1, numBotsToMove do
            local botNum = findBotOnTeam(2)  -- Find a bot in the Allies team
            if botNum ~= nil then
                et.trap_SendConsoleCommand(et.EXEC_APPEND, "put " .. botNum .. " r ; qsay Team balancing... " .. et.gentity_get(botNum, "pers.netname") .. "^7 moved to ^1AXIS")
            else
                break
            end
        end
    elseif numAxisBots > numAlliedBots + unevenBotDiff then
        local numBotsToMove = numAxisBots - numAlliedBots
        for i = 1, numBotsToMove do
            local botNum = findBotOnTeam(1)  -- Find a bot in the Axis team
            if botNum ~= nil then
                et.trap_SendConsoleCommand(et.EXEC_APPEND, "put " .. botNum .. " b ; qsay Team balancing... " .. et.gentity_get(botNum, "pers.netname") .. "^7 moved to ^4ALLIES")
            else
                break
            end
        end
    end
end

function findBotOnTeam(team)
    local maxClients = tonumber(et.trap_Cvar_Get("sv_maxclients"))

    for i = 0, maxClients - 1 do
        local teamNum = tonumber(et.gentity_get(i, "sess.sessionTeam"))
        local isBot = tonumber(et.gentity_get(i, "pers.localClient"))

        if teamNum == team and isBot == 1 then
            return i
        end
    end

    return nil
end

function et_ClientSpawn(clientNum, revived, teamChange, restoreHealth)
    if teamChange ~= 0 then
        local team = tonumber(et.gentity_get(clientNum, "sess.sessionTeam"))
        local isBot = tonumber(et.gentity_get(clientNum, "pers.localClient"))

        if isBot == 0 then
            local numAlliedPlayers = #alliedPlayers
            local numAxisPlayers = #axisPlayers

            if team == 1 then
                for i, num in ipairs(alliedPlayers) do
                    if num == clientNum then
                        table.remove(alliedPlayers, i)
                        break
                    end
                end

                if numAlliedPlayers >= numAxisPlayers + unevenDiff then
                    table.insert(axisPlayers, 1, clientNum)
                else
                    table.insert(axisPlayers, clientNum)
                end
            elseif team == 2 then
                for i, num in ipairs(axisPlayers) do
                    if num == clientNum then
                        table.remove(axisPlayers, i)
                        break
                    end
                end

                if numAxisPlayers >= numAlliedPlayers + unevenDiff then
                    table.insert(alliedPlayers, 1, clientNum)
                else
                    table.insert(alliedPlayers, clientNum)
                end
            end
        end
    end
end

function et_ClientDisconnect(clientNum)
    for i, num in ipairs(alliedPlayers) do
        if num == clientNum then
            table.remove(alliedPlayers, i)
            return
        end
    end
    for i, num in ipairs(axisPlayers) do
        if num == clientNum then
            table.remove(axisPlayers, i)
            return
        end
    end
end

Offline

#2 10-Sep-25 11:43:16

Drago
Member
Registered: 07-May-23
Posts: 25

Re: custom balance.lua

For clarity:
What I want to keep in this script is that players who arrive during the game are moved in a balanced manner.
That is, if there are, for example, more players in the "allies" team, the one who tries to join that team automatically ends up in the "axis" team.

What I want to remove is the movement of a player in a team because a player has left, thus unbalancing the teams.
The game will be unbalanced until a new player arrives.
In short, I want to maintain balance when a player arrives, but I don't want it to be rebalanced when a player leaves the server.

Offline

Board footer

Powered by FluxBB