HR_Shaft

Team Score Display for SAPP

Apr 15th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.72 KB | None | 0 0
  1. -- Team Score Display
  2. -- by H® Shaft
  3.  
  4. -- Will show Red Team and Blue Team Scores with Time Left (HH:MM:SS) at the top of the screen in console text.
  5. -- Will show team death totals for both CTF and Team Slayer
  6. -- Players can toggle Team Score Display by typing /hide to HIDE, or /show to UNHIDE the display
  7. -- Designed for Team Games: CTF and Team Slayer
  8.  
  9.  
  10. api_version = "1.9.0.0"
  11. tick_counter_address = nil
  12. sv_map_reset_tick_address = nil
  13. timelimit_address = nil
  14. teamdeaths = {}
  15. message_hide = {}
  16.  
  17. function OnScriptLoad()
  18.     register_callback(cb['EVENT_JOIN'],"OnPlayerJoin")
  19.     register_callback(cb["EVENT_GAME_START"], "OnNewGame")
  20.     register_callback(cb["EVENT_GAME_END"], "OnGameEnd")
  21.     register_callback(cb['EVENT_DIE'], "OnPlayerDeath")
  22.     register_callback(cb['EVENT_CHAT'], "OnPlayerChat")
  23.     local tick_counter_sig = sig_scan("8B2D????????807D0000C644240600")
  24.     if (tick_counter_sig == 0) then return end
  25.     local sv_map_reset_tick_sig = sig_scan("8B510C6A018915????????E8????????83C404")
  26.     if (sv_map_reset_tick_sig == 0) then return end
  27.     local timelimit_location_sig = sig_scan("8B0D????????83C8FF85C97E17")
  28.     if (timelimit_location_sig == 0) then return end
  29.     tick_counter_address = read_dword(read_dword(tick_counter_sig + 2)) + 0xC
  30.     sv_map_reset_tick_address = read_dword(sv_map_reset_tick_sig + 7)
  31.     timelimit_address = read_dword(timelimit_location_sig + 2)
  32.     if get_var(0, "$gt") ~= "n/a" then
  33.         OnNewGame()
  34.     end
  35. end
  36.  
  37. function OnNewGame()
  38.     game_started = true
  39.     team_play = getteamplay()
  40.     game_type = get_var(0, "$gt")
  41.     teamdeaths["red"] = 0
  42.     teamdeaths["blue"] = 0 
  43.     timer(1000, "messenger")   
  44. end
  45.  
  46. function OnPlayerJoin(PlayerIndex)
  47.     announce = timer(12000, "timedannounce", PlayerIndex)
  48.     message_hide[get_var(PlayerIndex,"$hash")] = nil
  49. end
  50.  
  51. function OnPlayerChat(PlayerIndex, Message)
  52.     local response = nil
  53.     if Message == nil then
  54.         return true
  55.     end
  56.     local Message = string.lower(Message)
  57.     if Message ~= nil then     
  58.         if (Message == "hide") or (Message == "/hide") then
  59.             response = false
  60.             message_hide[get_var(PlayerIndex,"$hash")] = true
  61.             say(PlayerIndex, "The team scores have been hidden. Type /show to unhide. ")
  62.         elseif (Message == "show") or (Message == "/show") then
  63.             response = false
  64.             message_hide[get_var(PlayerIndex,"$hash")] = nil
  65.             say(PlayerIndex,"The team scores are no longer hidden. Type /hide to re-hide.")
  66.         end
  67.     end
  68.     return response
  69. end
  70.  
  71. function timedannounce(PlayerIndex)
  72.     if player_present(PlayerIndex) then
  73.         rprint(PlayerIndex, "|c SCORES: You can toggle display by typing /hide or /show")
  74.     end
  75.     return false   
  76. end
  77.  
  78. function OnGameEnd()
  79.     game_started = false
  80.     teamdeaths = {}
  81. end
  82.  
  83. function getteamplay()
  84.     if get_var(0,"$ffa") == "0" then
  85.         return true
  86.     else
  87.         return false
  88.     end
  89. end
  90.  
  91. function OnPlayerDeath(PlayerIndex, KillerIndex)
  92.     -- Killer Index is: -- falling = -1, server = -1, team-switch = -1, vehicle = 0, other players = 1 or greater
  93.     if game_started then
  94.         local vteam = get_var(PlayerIndex, "$team")
  95.         local killer = tonumber(KillerIndex)
  96.         if team_play then
  97.             if teamdeaths["red"] == nil then teamdeaths["red"] = 0 end
  98.             if teamdeaths["blue"] == nil then teamdeaths["blue"] = 0 end       
  99.             teamdeaths[vteam] = teamdeaths[vteam] + 1
  100.         end
  101.     end
  102. end
  103.  
  104. function messenger()
  105.     if game_started then
  106.         -- time remaining by 002
  107.         local timeleftinseconds = math.floor((read_dword(timelimit_address) - (read_dword(tick_counter_address) - read_dword(sv_map_reset_tick_address))) / 30)
  108.         local minutes = math.floor(timeleftinseconds / 60)
  109.         local seconds = timeleftinseconds % 60
  110.         local zero = "0"
  111.         if(seconds > 9) then zero = "" end
  112.         tmessage = "Time Left: "
  113.         if (minutes > 59) then
  114.             local hours = math.floor(minutes / 60)
  115.             minutes = minutes % 60
  116.             tmessage = tmessage .. hours .. ":"
  117.         end
  118.         time_left = tmessage .. minutes .. ":" .. zero .. seconds      
  119.         -- end time remaining
  120.  
  121.         if game_type == "ctf" or game_type == "slayer" then
  122.             if team_play then
  123.                 local red_score = tonumber(get_var(0,"$redscore"))
  124.                 local blue_score = tonumber(get_var(0,"$bluescore"))
  125.                 if teamdeaths["red"] == nil then teamdeaths["red"] = 0 end
  126.                 if teamdeaths["blue"] == nil then teamdeaths["blue"] = 0 end
  127.                 local red_deaths = teamdeaths["red"]
  128.                 local blue_deaths = teamdeaths["blue"]
  129.                 local message = string.format("                                  Red Team: %s | Blue Team: %s | %s", red_score, blue_score, time_left)
  130.                 local deaths =  string.format("                                   R Deaths: %s | B Deaths:   %s", red_deaths, blue_deaths)
  131.                 for PlayerIndex = 1,16 do
  132.                     local hash = get_var(PlayerIndex, "$hash")
  133.                     if (message_hide[hash] ~= true) then
  134.                         rprint(PlayerIndex, message)
  135.                         rprint(PlayerIndex, deaths)
  136.                         rprint(PlayerIndex, "\n")
  137.                         rprint(PlayerIndex, "\n")
  138.                         rprint(PlayerIndex, "\n")
  139.                         rprint(PlayerIndex, "\n")
  140.                         rprint(PlayerIndex, "\n")
  141.                         rprint(PlayerIndex, "\n")
  142.                         rprint(PlayerIndex, "\n")
  143.                         rprint(PlayerIndex, "\n")
  144.                         rprint(PlayerIndex, "\n")
  145.                         rprint(PlayerIndex, "\n")
  146.                         rprint(PlayerIndex, "\n")
  147.                         rprint(PlayerIndex, "\n")
  148.                         rprint(PlayerIndex, "\n")
  149.                         rprint(PlayerIndex, "\n")
  150.                         rprint(PlayerIndex, "\n")
  151.                         rprint(PlayerIndex, "\n")
  152.                         rprint(PlayerIndex, "\n")
  153.                         rprint(PlayerIndex, "\n")
  154.                         rprint(PlayerIndex, "\n")
  155.                         rprint(PlayerIndex, "\n")
  156.                     end
  157.                 end
  158.             end
  159.         end    
  160.         return true
  161.     else
  162.         return false
  163.     end
  164. end
  165.  
  166. function OnScriptUnload() end
  167.  
  168. function OnError(Message)
  169.     print(debug.traceback())
  170. end
  171.  
  172. -- Created by H® Shaft
  173. -- Visit http://halorace.org/forum/index.php
Add Comment
Please, Sign In to add comment