Advertisement
HR_Shaft

Team Balance and Auto-Balance v1 for Phasor v2

Mar 30th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.83 KB | None | 0 0
  1. --[[ ###  Team Balance and Auto-Balance  ###]]--
  2. --[[ ###    by H® Shaft for Phasor v2    ###]]--
  3.  
  4. -- autobalances teams during team games if there are 4 or more players, does as frequently as set in 'frequency'
  5. -- any player may type 'balance' to balance the teams.
  6.  
  7. -- edit --
  8. frequency = 1  --| How often (in minutes) should the teams be auto-balanced?
  9.  
  10. -- don't edit --
  11. team_play = false
  12. cur_players = 0
  13. autobal = nil
  14. game_started = false
  15.  
  16. function GetRequiredVersion()
  17.     return 200
  18. end
  19.  
  20. function OnScriptLoad(process, game, persistent)
  21.     if game == "PC" then
  22.         gametype_base = 0x671340
  23.         network_base = 0x745BA8
  24.         GAME = game
  25.     else
  26.         gametype_base = 0x5F5498
  27.         network_base = 0x6C7988
  28.         GAME = game
  29.     end
  30.     team_play = getteamplay()
  31.     cur_players = readword(network_base, 0x1A0)
  32.     if not game_started and cur_players > 0 then game_started = true autobal = nil end
  33. end
  34.  
  35. function OnNewGame(map)
  36.     if GAME == "PC" then
  37.         gametype_base = 0x671340
  38.         network_base = 0x745BA8
  39.     else
  40.         gametype_base = 0x5F5498
  41.         network_base = 0x6C7988
  42.     end
  43.    
  44.     team_play = getteamplay()
  45.     cur_players = readword(network_base, 0x1A0)
  46.     game_started = true
  47.    
  48.     if team_play then
  49.         if cur_players ~= nil and cur_players > 3 then
  50.             if autobal == nil and game_started then
  51.                 autobal = registertimer(frequency * 1000, "AutoBalance")
  52.             end
  53.         end        
  54.     end    
  55. end
  56.  
  57. function getteamplay()
  58.     if readbyte(gametype_base + 0x34) == 1 then
  59.         return true
  60.     else
  61.         return false
  62.     end
  63. end
  64.  
  65. function OnServerChat(player, type, message)
  66.     local response = nil
  67.     if player then
  68.         if string.lower(message) == "balance" then
  69.             balance = registertimer(50, "BalanceTeams", player)
  70.             log_msg(1, "Teams Manually Balanced by: " .. getname(player))
  71.             return false
  72.         end
  73.     end
  74. end
  75.  
  76. function BalanceTeams(id, count, player)
  77.     if team_play then
  78.         if count == 1 then
  79.             if getteamsize(1) == getteamsize(0) then
  80.                 privatesay(player, "Teams are balanced.")
  81.             elseif getteamsize(1) + 1 == getteamsize(0) then
  82.                 privatesay(player, "Teams are balanced.")
  83.             elseif getteamsize(1) == getteamsize(0) + 1 then
  84.                 privatesay(player, "Teams are balanced.")
  85.             elseif getteamsize(1) > getteamsize(0) then
  86.                 changeteam(SelectPlayer(1), true)
  87.                 say("Teams are Un-even. Balancing Teams.")         
  88.             elseif getteamsize(1) < getteamsize(0) then
  89.                 changeteam(SelectPlayer(0), true)
  90.                 say("Teams are Un-even. Balancing Teams")              
  91.             end
  92.         end
  93.     else
  94.         privatesay(player, "This command is disabled since it is not a team based game.")
  95.     end
  96. end
  97.  
  98. function AutoBalance(id, count)
  99.     if getteamsize(1) == getteamsize(0) then
  100.     elseif getteamsize(1) + 1 == getteamsize(0) then
  101.     elseif getteamsize(1) == getteamsize(0) + 1 then
  102.     elseif getteamsize(1) > getteamsize(0) then
  103.         changeteam(SelectPlayer(1), true)
  104.         say("Auto-Balancing Teams.")
  105.         log_msg(1, "Teams Automatically Balanced.")        
  106.     elseif getteamsize(1) < getteamsize(0) then
  107.         changeteam(SelectPlayer(0), true)
  108.         say("Auto-Balancing Teams")
  109.         log_msg(1, "Teams Automatically Balanced.")        
  110.     end
  111.     return true
  112. end    
  113.  
  114. function SelectPlayer(team)
  115.     local t = {}
  116.     for i=0,15 do
  117.         if getplayer(i) then
  118.             if getteam(i) == team then
  119.                 table.insert(t, i)
  120.             end
  121.         end
  122.     end
  123.     if #t > 0 then
  124.         local r = getrandomnumber(1, #t+1)
  125.         return t[r]
  126.     end
  127.     return nil
  128. end
  129.  
  130. function OnTeamChange(player, old_team, new_team, relevant)
  131.     local response = nil
  132.     if getplayer(player) then
  133.         local newteam = ""
  134.         if old_team == 0 then
  135.             newteam = "Blue Team."
  136.         elseif old_team == 1 then
  137.             newteam = "Red Team."
  138.         end
  139.         if relevant == true or relevant == 1 then
  140.             if getteamsize(old_team) == getteamsize(new_team) then
  141.                 response = false
  142.                 privatesay(player, "You cannot change teams.")
  143.             elseif getteamsize(old_team) + 1 == getteamsize(new_team) then
  144.                 response = false
  145.                 privatesay(player, "You cannot change teams.")
  146.             elseif getteamsize(old_team) == getteamsize(new_team) + 1 then
  147.                 response = false
  148.                 privatesay(player, "You cannot change teams.")
  149.             elseif getteamsize(old_team) > getteamsize(new_team) then
  150.                 response = true
  151.                 say(getname(player) .. " changed teams to the " .. newteam)
  152.             elseif getteamsize(old_team) < getteamsize(new_team) then
  153.                 response = true
  154.                 say(getname(player) .. " changed teams to the " .. newteam)
  155.             end
  156.         elseif relevant == false or relevant == 0 then
  157.             say(getname(player) .. " was auto-balance moved to the " .. newteam)
  158.             sendconsoletext(player, "**Team Balance** Your team was changed, but your score remains unchanged.")
  159.         end
  160.     end
  161.     return response
  162. end
  163.  
  164. function OnGameEnd(stage)
  165.     if stage == 1 then
  166.         game_started = false
  167.         if autobal then
  168.             removetimer(autobal)
  169.             autobal = nil
  170.         end
  171.         if balance then
  172.             removetimer(balance)
  173.             balance = nil
  174.         end
  175.     end
  176. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement