Advertisement
HR_Shaft

Auto-Balance v2 for Phasor v2+

Jul 15th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.68 KB | None | 0 0
  1. --[[ ###  Team Balance and Auto-Balance v2  ###]]--
  2. --[[ ###     by H® Shaft for Phasor v2+     ###]]--
  3.  
  4. -- EDITED 7/19/2014 corrected small error. If you downloaded before this date, you need to download it again.
  5. -- autobalances teams during team games if there are 4 or more players
  6. -- occurs automatically on player leave and join
  7. -- any player may type 'balance' to balance the teams if not willing to wait
  8.  
  9. -- edit --
  10. autobal_delay = 15  --| Time in seconds to delay balancing teams during the game and when players join or leave
  11.                     --| you should not go lower than 10 seconds, to allow admins to switch players manually if they choose
  12.  
  13. -- don't edit --
  14. cur_players = 0
  15. team_change = {}
  16. team_play = false
  17. game_started = false
  18. game_autobal = nil
  19. map_reset = false
  20.  
  21. function GetRequiredVersion()
  22.     return 200
  23. end
  24.  
  25. function OnScriptLoad(process, game, persistent)
  26.     if game == "PC" then
  27.         gametype_base = 0x671340
  28.         network_base = 0x745BA8
  29.     else
  30.         gametype_base = 0x5F5498
  31.         network_base = 0x6C7988
  32.     end
  33.    
  34.     GAME = game
  35.     team_play = getteamplay()
  36.     cur_players = readword(network_base, 0x1A0)
  37.     ScriptLoad()
  38.    
  39.     if not game_started and cur_players > 0 then game_started = true end
  40.    
  41.     if team_play and game_autobal == nil then
  42.         game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
  43.     end
  44.    
  45. end
  46.  
  47. function ScriptLoad()
  48.     if map_reset == true then
  49.         map_reset = false
  50.         team_play = getteamplay()
  51.        
  52.         for i = 0,15 do
  53.             if getplayer(i) then   
  54.                 cur_players = cur_players + 1
  55.                 team_change[i] = false
  56.             end
  57.         end
  58.        
  59.         if not game_started and cur_players > 0 then game_started = true end   
  60.        
  61.         if team_play and game_autobal == nil then
  62.             game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
  63.         end
  64.     end
  65. end
  66.  
  67. function OnNewGame(map)
  68.     if game == "PC" or GAME == "PC" then
  69.         gametype_base = 0x671340
  70.         network_base = 0x745BA8
  71.     else
  72.         gametype_base = 0x5F5498
  73.         network_base = 0x6C7988
  74.     end
  75.    
  76.     team_play = getteamplay()
  77.     cur_players = readword(network_base, 0x1A0)
  78.     game_started = true
  79.    
  80.     if not game_started and cur_players > 0 then game_started = true end
  81.    
  82.     if team_play and game_autobal == nil then
  83.         game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
  84.     end    
  85. end
  86.  
  87. function OnPlayerJoin(player)
  88.     if getplayer(player) then
  89.         cur_players = cur_players + 1
  90.         team_change[player] = false
  91.         if game_started and team_play and cur_players > 3 then
  92.             join_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
  93.         end
  94.         if team_play and cur_players > 3 and game_autobal == nil then
  95.             game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
  96.         end    
  97.     end
  98. end
  99.  
  100. function OnPlayerLeave(player)
  101.     if getplayer(player) then
  102.         cur_players = cur_players - 1
  103.         team_change[player] = {}
  104.         if game_started and team_play and cur_players > 3 then
  105.             leave_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
  106.         end    
  107.         if team_play and cur_players < 4 and game_autobal then
  108.             game_autobal = nil
  109.         elseif team_play and cur_players > 3 and game_autobal == nil then  
  110.             game_autobal = registertimer(autobal_delay * 1000, "AutoBalance")
  111.         end            
  112.     end
  113. end
  114.  
  115. function getteamplay()
  116.     if readbyte(gametype_base + 0x34) == 1 then
  117.         return true
  118.     else
  119.         return false
  120.     end
  121. end
  122.  
  123. function OnServerChat(player, type, message)
  124.     local response = nil
  125.    
  126.     if player then 
  127.         if string.lower(message) == "balance" then
  128.             Balance_Teams()
  129.             response = false           
  130.         end    
  131.     end
  132.    
  133.     return response
  134. end    
  135.  
  136. function OnPlayerKill(killer, victim, mode)
  137.     local response = nil
  138.    
  139.     if game_started then
  140.         if mode == 1 then -- player was killed by falling or team-change
  141.             if getplayer(victim) then  
  142.                 if not team_change[victim] then
  143.                     response = true  -- player fell and died, show death message
  144.                 else
  145.                     team_change[victim] = false
  146.                     response = false -- player changed teams, don't show death message             
  147.                 end
  148.             end
  149.         end
  150.     end
  151.    
  152.     return response
  153. end
  154.  
  155. function AutoBalance(id, count)
  156.     if game_started and team_play and cur_players > 3 then
  157.         Balance_Teams()
  158.         if join_autobal then join_autobal = nil end
  159.         if leave_autobal then leave_autobal = nil end
  160.         return true
  161.     elseif game_started and team_play and cur_players < 4 then 
  162.         return false
  163.     else
  164.         return false
  165.     end
  166. end
  167.  
  168. -- inspired by 002's team balance for Sapp
  169. function Balance_Teams()
  170.     if game_started and team_play then
  171.         local redteam = getteamsize(0)
  172.         local blueteam = getteamsize(1)
  173.         if redteam > blueteam then
  174.             while TeamsAreUneven() do
  175.                 while (getteamsize(0) > getteamsize(1)+1) do
  176.                     local randomred = SelectPlayer(0)
  177.                     if randomred ~= nil then
  178.                         changeteam(randomred, true)
  179.                     end
  180.                 end
  181.             end
  182.         elseif blueteam > redteam then
  183.             while TeamsAreUneven() do
  184.                 while (getteamsize(1) > getteamsize(0)+1) do
  185.                     local randomblu = SelectPlayer(1)
  186.                     if randomblu ~= nil then                           
  187.                         changeteam(randomblu, true)
  188.                     end
  189.                 end
  190.             end    
  191.         end
  192.     end
  193. end
  194.  
  195. -- inspired by 002's team balance for Sapp
  196. function TeamsAreUneven()
  197.     local red = getteamsize(0)
  198.     local blue = getteamsize(1)
  199.     if (red > blue + 1 or blue > red + 1) then return true end
  200.     return false
  201. end
  202.  
  203. function SelectPlayer(team)
  204.     local t = {}
  205.     for i=0,15 do
  206.         if getplayer(i) and getteam(i) == team then
  207.             table.insert(t, i)
  208.         end
  209.     end
  210.     if #t > 0 then
  211.         local r = getrandomnumber(1, #t+1)
  212.         return t[r]
  213.     end
  214.     return nil
  215. end
  216.  
  217. function OnTeamChange(player, old_team, new_team, relevant)
  218.     -- prevent unbalancing teams by team change
  219.     local response = nil
  220.     if getplayer(player) then
  221.    
  222.         local newteam = "New Team"
  223.         local oldteam = "Old Team"
  224.        
  225.         if not team_play then response = false return response end
  226.        
  227.         if new_team == 0 then
  228.             newteam = "Red Team"       
  229.         elseif new_team == 1 then
  230.             newteam = "Blue Team"      
  231.         end        
  232.        
  233.         if relevant == true or relevant == 1 then
  234.             if getteamsize(old_team) == getteamsize(new_team) then
  235.                 privatesay(player, "You cannot change teams.")
  236.                 response = false
  237.             elseif getteamsize(old_team) + 1 == getteamsize(new_team) then
  238.                 privatesay(player, "You cannot change teams.")
  239.                 response = false
  240.             elseif getteamsize(old_team) == getteamsize(new_team) + 1 then
  241.                 privatesay(player, "You cannot change teams.")
  242.                 response = false
  243.             elseif getteamsize(old_team) > getteamsize(new_team) then
  244.                 team_change[player] = true
  245.                 say(getname(player) .. " switched to the "  .. newteam)
  246.                 response = true
  247.             elseif getteamsize(old_team) < getteamsize(new_team) then
  248.                 team_change[player] = true
  249.                 say(getname(player) .. " switched to the "  .. newteam)
  250.                 response = true
  251.             end
  252.         elseif relevant == false or relevant == 0 then
  253.             team_change[player] = true
  254.             say(getname(player) .. " was team-switched to balance the teams.")
  255.             response = true
  256.         end
  257.        
  258.     end
  259.    
  260.     return response
  261. end
  262.  
  263. function OnServerCommand(player, command)
  264.     local allow = nil
  265.     local cmd = tokenizecmdstring(command)
  266.     local tokencount = #cmd
  267.     if tokencount > 0 then
  268.    
  269.         if cmd[1] == "sv_map_reset" then
  270.             map_reset = true       
  271.             ScriptLoad()                   
  272.             allow = true
  273.            
  274.         elseif cmd[1] == "sv_script_reload" then
  275.             map_reset = true
  276.             ScriptLoad()       
  277.             allow = true
  278.         end
  279.        
  280.     end
  281.     return allow
  282. end
  283.  
  284. function OnGameEnd(stage)
  285.     if stage == 1 then
  286.         game_started = false
  287.         if game_autobal then
  288.             game_autobal = nil
  289.         end
  290.         if join_autobal then
  291.             join_autobal = nil
  292.         end
  293.         if leave_autobal then
  294.             leave_autobal = nil
  295.         end    
  296.     end
  297. end
  298.  
  299. --[[ Created by H® Shaft.
  300. Thanks to Oxide, AelitePrime, Nugget & Wizard -- and 002
  301. Visit http://halorace.org/forum/index.php?topic=514.0 or
  302. Visit http://pastebin.com/u/HR_Shaft for more phasor scripts
  303. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement