Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ ### Team Balance and Auto-Balance ###]]--
- --[[ ### by H® Shaft for Phasor v2 ###]]--
- -- autobalances teams during team games if there are 4 or more players, does as frequently as set in 'frequency'
- -- any player may type 'balance' to balance the teams.
- -- edit --
- frequency = 1 --| How often (in minutes) should the teams be auto-balanced?
- -- don't edit --
- team_play = false
- cur_players = 0
- autobal = nil
- game_started = false
- function GetRequiredVersion()
- return 200
- end
- function OnScriptLoad(process, game, persistent)
- if game == "PC" then
- gametype_base = 0x671340
- network_base = 0x745BA8
- GAME = game
- else
- gametype_base = 0x5F5498
- network_base = 0x6C7988
- GAME = game
- end
- team_play = getteamplay()
- cur_players = readword(network_base, 0x1A0)
- if not game_started and cur_players > 0 then game_started = true autobal = nil end
- end
- function OnNewGame(map)
- if 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 team_play then
- if cur_players ~= nil and cur_players > 3 then
- if autobal == nil and game_started then
- autobal = registertimer(frequency * 1000, "AutoBalance")
- end
- 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 = registertimer(50, "BalanceTeams", player)
- log_msg(1, "Teams Manually Balanced by: " .. getname(player))
- return false
- end
- end
- end
- function BalanceTeams(id, count, player)
- if team_play then
- if count == 1 then
- if getteamsize(1) == getteamsize(0) then
- privatesay(player, "Teams are balanced.")
- elseif getteamsize(1) + 1 == getteamsize(0) then
- privatesay(player, "Teams are balanced.")
- elseif getteamsize(1) == getteamsize(0) + 1 then
- privatesay(player, "Teams are balanced.")
- elseif getteamsize(1) > getteamsize(0) then
- changeteam(SelectPlayer(1), true)
- say("Teams are Un-even. Balancing Teams.")
- elseif getteamsize(1) < getteamsize(0) then
- changeteam(SelectPlayer(0), true)
- say("Teams are Un-even. Balancing Teams")
- end
- end
- else
- privatesay(player, "This command is disabled since it is not a team based game.")
- end
- end
- function AutoBalance(id, count)
- if getteamsize(1) == getteamsize(0) then
- elseif getteamsize(1) + 1 == getteamsize(0) then
- elseif getteamsize(1) == getteamsize(0) + 1 then
- elseif getteamsize(1) > getteamsize(0) then
- changeteam(SelectPlayer(1), true)
- say("Auto-Balancing Teams.")
- log_msg(1, "Teams Automatically Balanced.")
- elseif getteamsize(1) < getteamsize(0) then
- changeteam(SelectPlayer(0), true)
- say("Auto-Balancing Teams")
- log_msg(1, "Teams Automatically Balanced.")
- end
- return true
- end
- function SelectPlayer(team)
- local t = {}
- for i=0,15 do
- if getplayer(i) then
- if getteam(i) == team then
- table.insert(t, i)
- end
- 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)
- local response = nil
- if getplayer(player) then
- local newteam = ""
- if old_team == 0 then
- newteam = "Blue Team."
- elseif old_team == 1 then
- newteam = "Red Team."
- end
- if relevant == true or relevant == 1 then
- if getteamsize(old_team) == getteamsize(new_team) then
- response = false
- privatesay(player, "You cannot change teams.")
- elseif getteamsize(old_team) + 1 == getteamsize(new_team) then
- response = false
- privatesay(player, "You cannot change teams.")
- elseif getteamsize(old_team) == getteamsize(new_team) + 1 then
- response = false
- privatesay(player, "You cannot change teams.")
- elseif getteamsize(old_team) > getteamsize(new_team) then
- response = true
- say(getname(player) .. " changed teams to the " .. newteam)
- elseif getteamsize(old_team) < getteamsize(new_team) then
- response = true
- say(getname(player) .. " changed teams to the " .. newteam)
- end
- elseif relevant == false or relevant == 0 then
- say(getname(player) .. " was auto-balance moved to the " .. newteam)
- sendconsoletext(player, "**Team Balance** Your team was changed, but your score remains unchanged.")
- end
- end
- return response
- end
- function OnGameEnd(stage)
- if stage == 1 then
- game_started = false
- if autobal then
- removetimer(autobal)
- autobal = nil
- end
- if balance then
- removetimer(balance)
- balance = nil
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement