if not CBW_Battle then return end local B = CBW_Battle local CV = CBW_Battle.Console local balance_cmd = "bm_balance" CV.Autobalance = CV_RegisterVar { name = balance_cmd, defaultvalue = 0, flags = CV_NETVAR, PossibleValue = CV_OnOff } --// rev: Balanced autobalance --[[ A player will NOT be autobalanced depending on the following factors in order: 1. If game mode has a cap type, then if they got lots, they are exempt 2. Depending on points. If they got lots, they are exempt 3. Depending on time. If they got lots of time, they are exempt. If everything up to this point has been equal .. (e.g. let's say 3 players with equal caps, points, time) .. then it will be RNG. ]] --// team: Red/blue --// amount: The amount required to balance teams --// NOTE: Caps reset when map changes, in-game time resets when map changes/player spectates (see MapChange hook) local function autobalance_team(team, amount) local sorted = team table.sort(sorted, function(p1, p2) if not (p1 or p2) then return end -- Flagholders are exempt and won't be autobalanced if p1.gotflag then return true elseif p2.gotflag then return false end --// 1. Flag caps if (gametype == GT_CTF or gametype == GT_BATTLECTF) and p1.flagscore == p2.flagscore then --// 2. Score if p1.score == p2.score then --// 3. Time since the player has NOT been a spectator if p1.ingametime == p2.ingametime then --// Let's assume this is RNG (it's actually not, gonna come abck to this later) return #p1 > #p2 else return p1.ingametime > p2.ingametime end else return p1.score > p2.score end else return p1.caps > p2.caps end end) local count = 0 for i = #sorted, 1, -1 do local p = sorted[i] --// If amount reached, don't iterate over players anymore. if count >= amount then break end --// small hack, since this runs every frame, up the count if it's already players with autobalance --// and continue the loop if not (p and p.mo) or p.autobalancing then count = $+1 continue end p.autobalancing = 1 count = $+1 end end addHook("ThinkFrame", function() if not G_GametypeHasTeams() then return end if CV.Autobalance.value and CV_FindVar("autobalance").value then CV_Set(CV.Autobalance, "0") -- TODO: this is printed twice, idk why print("`autobalance` must be off in order for `bm_balance` to work!") return elseif not CV.Autobalance.value then return end local redteam = {} local bluteam = {} for p in players.iterate do --// Count players on red/blue. Ignores spectators. if not p.spectator then if p.ctfteam == 1 then -- RED table.insert(redteam, p) elseif p.ctfteam == 2 then -- BLUE table.insert(bluteam, p) end end end --// If teams are unequal, let's do something about it --// Also won't cause autobalance if the other team is equal - 1 local rlen = #redteam local blen = #bluteam local blimit = (blen + 1) == rlen local rlimit = (rlen + 1) == blen local amount = abs(rlen-blen)-1 if amount < 0 then amount = 0 end if rlen ~= blen and not (blimit or rlimit) then --// Check which team has most players, and autobalance from there. if rlen > blen then autobalance_team(redteam, amount) else autobalance_team(bluteam, amount) end --// If teams somehow got equalized, let's cancel the autobalance process. elseif rlen == blen or (blimit or rlimit) then for p in players.iterate do p.autobalancing = nil end end end)