Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ ### Team Auto-Balance and Team Shuffle ###]]--
- --[[ ### by H® Shaft for Phasor v2+ ###]]--
- -- autobalances teams during team games if there are 4 or more players
- -- occurs automatically on player leave and join
- -- any player may type 'balance' to balance the teams if not willing to wait.
- -- any admin may type 'shuffle' to shuffle or mix the teams.
- -- edit --
- autobal_delay = 15 --| Time in seconds to delay balancing teams during the game and when players join or leave
- --| you should not go lower than 10 seconds, to allow admins to switch players manually if they choose
- -- don't edit --
- cur_players = 0
- team_change = {}
- team_shuffle = {}
- team_play = false
- game_started = false
- game_autobal = nil
- map_reset = false
- function GetRequiredVersion()
- return 200
- end
- function OnScriptLoad(process, game, persistent)
- if game == "PC" then
- gametype_base = 0x671340
- network_base = 0x745BA8
- else
- gametype_base = 0x5F5498
- network_base = 0x6C7988
- end
- GAME = game
- team_play = getteamplay()
- cur_players = readword(network_base, 0x1A0)
- ScriptLoad()
- if not game_started and cur_players > 0 then game_started = true end
- if team_play and game_autobal == nil then
- game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
- end
- end
- function ScriptLoad()
- if map_reset == true then
- map_reset = false
- team_play = getteamplay()
- for i = 0,15 do
- if getplayer(i) then
- cur_players = cur_players + 1
- team_change[i] = false
- team_shuffle[i] = false
- end
- end
- if not game_started and cur_players > 0 then game_started = true end
- if team_play and game_autobal == nil then
- game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
- end
- end
- end
- function OnNewGame(map)
- if game == "PC" or GAME == "PC" then
- gametype_base = 0x671340
- network_base = 0x745BA8
- else
- gametype_base = 0x5F5498
- network_base = 0x6C7988
- end
- team_play = getteamplay()
- cur_players = readword(network_base, 0x1A0)
- game_started = true
- if not game_started and cur_players > 0 then game_started = true end
- if team_play and game_autobal == nil then
- game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
- end
- end
- function OnPlayerJoin(player)
- if getplayer(player) then
- cur_players = cur_players + 1
- team_change[player] = false
- team_shuffle[player] = false
- if game_started and team_play and cur_players > 3 then
- join_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
- end
- if team_play and cur_players > 3 and game_autobal == nil then
- game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
- end
- end
- end
- function OnPlayerLeave(player)
- if getplayer(player) then
- cur_players = cur_players - 1
- team_change[player] = {}
- team_shuffle[player] = {}
- if game_started and team_play and cur_players > 3 then
- leave_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
- end
- if team_play and cur_players < 4 and game_autobal then
- game_autobal = nil
- elseif team_play and cur_players > 3 and game_autobal == nil then
- game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
- end
- end
- end
- function getteamplay()
- if readbyte(gametype_base + 0x34) == 1 then
- return true
- else
- return false
- end
- end
- function OnServerChat(player, type, message)
- local response = nil
- if player then
- if string.lower(message) == "balance" then
- Balance_Teams()
- response = false
- end
- if string.lower(message) == "shuffle" then
- response = false
- if team_play then
- if cur_players > 3 then
- if isadmin(player) then
- for i=0,15 do
- if getplayer(i) then
- privatesay(i, "Shuffling teams.")
- local id = resolveplayer(i)
- if getteam(i) == 0 then
- svcmd("sv_changeteam " .. id)
- team_shuffle[i] = true
- team_change[i] = false
- if TeamsAreUneven() then Balance_Teams() end
- end
- if getteam(i) == 0 then
- svcmd("sv_changeteam " .. id)
- team_shuffle[i] = true
- team_change[i] = false
- if TeamsAreUneven() then Balance_Teams() end
- end
- sendconsoletext(player, "Team shuffle completed.")
- log_msg(1, getname(player) .. " used the shuffle teams command.")
- end
- end
- else
- response = true
- sendconsoletext(player, "You're not an admin. Logging attempt.")
- log_msg(1, getname(player) .. " attempted use of the shuffle command.")
- end
- else
- sendconsoletext(player, "Must have 4 or more players to shuffle teams.")
- end
- else
- sendconsoletext(player, "You cannot shuffle during FFA games.")
- end
- return response
- end
- end
- return response
- end
- function OnPlayerKill(killer, victim, mode)
- local response = nil
- if game_started then
- if mode == 1 then -- player was killed by falling or team-change
- if getplayer(victim) then
- if team_shuffle[victim] and not team_change[victim] then
- response = false -- player was shuffled, don't show death message
- team_shuffle[victim] = false
- elseif not team_change[victim] and not team_shuffle[victim] then
- response = true -- player fell and died, show death message
- else
- team_change[victim] = false
- response = false -- player changed teams, don't show death message
- end
- end
- end
- end
- return response
- end
- function AutoBalance(id, count)
- if game_started and team_play and cur_players > 3 then
- Balance_Teams()
- if join_autobal then join_autobal = nil end
- if leave_autobal then leave_autobal = nil end
- return true
- elseif game_started and team_play and cur_players < 4 then
- return false
- else
- return false
- end
- end
- -- inspired by 002's team balance for Sapp
- function Balance_Teams()
- if game_started and team_play then
- local redteam = getteamsize(0)
- local blueteam = getteamsize(1)
- if redteam > blueteam then
- while TeamsAreUneven() do
- while (getteamsize(0) > getteamsize(1)+1) do
- local randomred = SelectPlayer(0)
- if randomred ~= nil then
- changeteam(randomred, true)
- end
- end
- end
- elseif blueteam > redteam then
- while TeamsAreUneven() do
- while (getteamsize(1) > getteamsize(0)+1) do
- local randomblu = SelectPlayer(1)
- if randomblu ~= nil then
- changeteam(randomblu, true)
- end
- end
- end
- end
- end
- end
- -- inspired by 002's team balance for Sapp
- function TeamsAreUneven()
- local red = getteamsize(0)
- local blue = getteamsize(1)
- if (red > blue + 1 or blue > red + 1) then return true end
- return false
- end
- function SelectPlayer(team)
- local t = {}
- for i=0,15 do
- if getplayer(i) and getteam(i) == team then
- table.insert(t, i)
- end
- end
- if #t > 0 then
- local r = getrandomnumber(1, #t+1)
- return t[r]
- end
- return nil
- end
- function OnTeamChange(player, old_team, new_team, relevant)
- -- prevent unbalancing teams by team change
- local response = nil
- if getplayer(player) then
- local newteam = "New Team"
- local oldteam = "Old Team"
- if not team_play then response = false return response end
- if new_team == 0 then
- newteam = "Red Team"
- elseif new_team == 1 then
- newteam = "Blue Team"
- end
- if relevant == true or relevant == 1 then
- if getteamsize(old_team) == getteamsize(new_team) then
- privatesay(player, "You cannot change teams.")
- response = false
- elseif getteamsize(old_team) + 1 == getteamsize(new_team) then
- privatesay(player, "You cannot change teams.")
- response = false
- elseif getteamsize(old_team) == getteamsize(new_team) + 1 then
- privatesay(player, "You cannot change teams.")
- response = false
- elseif getteamsize(old_team) > getteamsize(new_team) then
- team_change[player] = true
- say(getname(player) .. " switched to the " .. newteam)
- response = true
- elseif getteamsize(old_team) < getteamsize(new_team) then
- team_change[player] = true
- say(getname(player) .. " switched to the " .. newteam)
- response = true
- end
- elseif relevant == false or relevant == 0 then
- if team_shuffle[player] then
- say(getname(player) .. " was team-shuffled to mix the teams.")
- team_shuffle[player] = false
- response = true
- else
- team_change[player] = true
- say(getname(player) .. " was team-switched to balance the teams.")
- response = true
- end
- end
- end
- return response
- end
- function OnServerCommand(player, command)
- local allow = nil
- local cmd = tokenizecmdstring(command)
- local tokencount = #cmd
- if tokencount > 0 then
- if cmd[1] == "sv_map_reset" then
- map_reset = true
- ScriptLoad()
- allow = true
- elseif cmd[1] == "sv_script_reload" then
- map_reset = true
- ScriptLoad()
- allow = true
- end
- end
- return allow
- end
- function OnGameEnd(stage)
- if stage == 1 then
- game_started = false
- if game_autobal then
- game_autobal = nil
- end
- if join_autobal then
- join_autobal = nil
- end
- if leave_autobal then
- leave_autobal = nil
- end
- end
- end
- --[[ Created by H® Shaft.
- Thanks to Oxide, AelitePrime, Nugget & Wizard -- and 002
- Visit http://halorace.org/forum/index.php?topic=514.0 or
- Visit http://pastebin.com/u/HR_Shaft for more phasor scripts
- ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement