Advertisement
HR_Shaft

Headshot Announcer v1.0 for SAPP

Dec 5th, 2016
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.68 KB | None | 0 0
  1. -- Headshot Announcer v1.0 for SAPP
  2. -- by H® Shaft 12/5/2016
  3.  
  4. -- The PRIMARY purpose of this script:
  5. -- is to announce when a player is killed by a "head-shot", and -
  6. -- to show everyone their "kills by head-shots count" in (parenthesis).
  7.  
  8. -- **Example** this kill message shows H® Shaft has '14' head-shot-kills:
  9.  
  10.     -- " BOOM! H® Shaft ASSASSINATED New001 with a HEAD-SHOT! (14) "
  11.  
  12. -- The SECONDARY purpose of this script to allow admins identify all players kill counts -vs- kills-by-headshot counts
  13. -- Note: many players are very good at killing by headshots; it doesn't mean they are cheating or aim-botting!
  14.  
  15. -- ADMIN USAGE:
  16. -- admins can type "/ss" for the ShowStats command to see all players kill counts -vs- kills-by-headshot counts
  17. -- admins can specify the admin level of those who can execute the command (see ADMIN_LEVEL below)
  18.  
  19. -- SAMPLE OUTPUT of ShowStats command (Player # is ID#):
  20. -- ID#: 1 --   H® Shaft --  KILLS: 4 --   HEADSHOTS: 3
  21. -- ID#: 2 --   Killer --  KILLS: 0 --   HEADSHOTS: 0
  22.  
  23. -- This admin level and higher can use the /ss (ShowStats) command, and see all players head shot scores
  24. ADMIN_LEVEL = 2
  25.  
  26. -- if set to true, when the /ss command is used, log all players kill counts and kills-by-headshot counts into sapps log
  27. LOG_RESULTS = true
  28.  
  29. -- The command to use to "reset the map" AND reset players kill count and kills-by-headshot counts zero
  30. RESET_COMMAND = "reset"
  31.  
  32. --- END CONFIGURATION ----
  33. --- don't edit below  ---
  34. api_version = "1.9.0.0"
  35.  
  36. function OnScriptLoad()
  37.     register_callback(cb['EVENT_JOIN'], "OnPlayerJoin")
  38.     register_callback(cb['EVENT_GAME_START'], "OnNewGame")
  39.     register_callback(cb['EVENT_GAME_END'], "OnGameEnd")
  40.     register_callback(cb['EVENT_DIE'], "OnPlayerDie")
  41.     register_callback(cb['EVENT_CHAT'], "OnPlayerChat")
  42.     register_callback(cb['EVENT_DAMAGE_APPLICATION'], "OnDamageApplication")
  43.     register_callback(cb['EVENT_COMMAND'], "OnCommand")
  44.     head_shot = {}
  45.     headshot_count = {}
  46.     if get_var(0, "$gt") ~= "n/a" then
  47.         OnNewGame()    
  48.     end
  49. end
  50.  
  51. function OnScriptUnload() end
  52.  
  53. function OnNewGame()
  54.     game_started = true
  55.     for i = 1, 16 do
  56.         if player_present(i) then
  57.             head_shot[i] = false
  58.             headshot_count[i] = 0          
  59.         end
  60.     end
  61. end
  62.  
  63. function OnGameEnd()
  64.     game_started = false
  65.     for i = 1,16 do
  66.         if player_present(i) then
  67.             local id = get_var(i, "$n")
  68.             if headshot_count[id] == nil then headshot_count[id] = 0 end
  69.             local headshots = tonumber(headshot_count[id])
  70.             local name = get_var(i, "$name")
  71.             local kills = tonumber(get_var(i, "$kills"))
  72.             if kills == nil then kills = 0 end
  73.             local player_stats = string.format("ID#: %s --   %s --  KILLS: %s --   HEADSHOTS: %s", id, name, kills, headshots)
  74.             if (LOG_RESULTS == true) then
  75.                 execute_command("log_note \""..player_stats.."\"")
  76.             end
  77.             head_shot[id] = false
  78.             headshot_count[id] = 0 
  79.         end
  80.     end    
  81. end
  82.  
  83. function OnPlayerJoin(PlayerIndex)
  84.     if player_present(PlayerIndex) then
  85.         head_shot[PlayerIndex] = false
  86.         headshot_count[PlayerIndex] = 0
  87.     end
  88. end
  89.  
  90. function OnPlayerLeave(PlayerIndex)
  91.     head_shot[PlayerIndex] = nil
  92.     headshot_count[PlayerIndex] = nil
  93. end
  94.  
  95. function OnDamageApplication(PlayerIndex, CauserIndex, MetaID, Damage, HitString, Backtap)
  96.     if (game_started == true) then
  97.         local CausesHeadShot = OnDamageLookup(PlayerIndex, CauserIndex)
  98.         if (CausesHeadShot ~= nil) then
  99.             if (HitString == "head") then
  100.                 head_shot[PlayerIndex] = true
  101.             else
  102.                 head_shot[PlayerIndex] = false
  103.             end
  104.         else
  105.             head_shot[PlayerIndex] = false
  106.         end
  107.     end
  108. end
  109.  
  110. function OnPlayerDie(PlayerIndex, KillerIndex)
  111.     if (game_started == true) then
  112.         local killer = tonumber(KillerIndex)
  113.         if (killer > 0) then -- killer is another player       
  114.             local vname = get_var(PlayerIndex, "$name")
  115.             local kname = get_var(KillerIndex,"$name")
  116.             local ktype = string.upper(generatekill(killtype))
  117.             if headshot_count[KillerIndex] == nil then headshot_count[KillerIndex] = 0 end
  118.             if (PlayerIndex ~= killer) then
  119.                 if (get_var(KillerIndex, "$team") ~= get_var(PlayerIndex, "$team")) then
  120.                     if (head_shot[PlayerIndex] == true) then
  121.                         headshot_count[KillerIndex] = headshot_count[KillerIndex] + 1
  122.                         local count = headshot_count[KillerIndex]
  123.                         if (kname ~= nil) then
  124.                             if (ktype~= nil) then
  125.                                 say_all("BOOM!  " ..kname .. "  " .. ktype .. "  " .. vname .. " with a HEAD-SHOT! (" .. count .. ")")
  126.                             else
  127.                                 say_all("BOOM! " ..kname .. " NAILED " .. vname .. " with a HEAD-SHOT! (" .. count .. ")")
  128.                             end
  129.                         else
  130.                             say_all("BOOM! " ..vname .. " was  GUTTED  by a 'random' HEAD-SHOT!") -- killer unknown, such as killer left server, or error
  131.                         end
  132.                     end
  133.                 end
  134.             end
  135.             head_shot[PlayerIndex] = false
  136.         end
  137.     end
  138. end
  139.  
  140. function OnDamageLookup(ReceiverIndex, CauserIndex)
  141.     local response = nil
  142.     if (CauserIndex and ReceiverIndex ~= CauserIndex) then
  143.         local c_team = read_word(get_player(CauserIndex) + 0x20)
  144.         local r_team = read_word(get_player(ReceiverIndex) + 0x20) 
  145.         if (c_team ~= r_team) then
  146.             local tag_address = read_dword(0x40440000)
  147.             local tag_count = read_word(0x4044000C)
  148.             for A=0,tag_count-1 do
  149.                 local tag_id = tag_address + A * 0x20
  150.                 if (read_dword(tag_id) == 1785754657) then
  151.                     local tag_data = read_dword(tag_id + 0x14)
  152.                     if (tag_data ~= nil) then  
  153.                         if (read_word(tag_data + 0x1C6) == 5) or (read_word(tag_data + 0x1C6) == 6) then -- damage category: bullets
  154.                             if (read_bit(tag_data + 0x1C8, 1) == 1) then -- damage effect: can cause head shots
  155.                                 response = true
  156.                             else
  157.                                 response = false
  158.                             end
  159.                         end                
  160.                     end
  161.                 end
  162.             end                                
  163.         end
  164.     end
  165.     return response
  166. end
  167.  
  168. function OnPlayerChat(PlayerIndex, Message)
  169.     local response = nil
  170.     if Message == nil then
  171.         return true
  172.     end
  173.     local name = get_var(PlayerIndex,"$name")
  174.     local Message = string.lower(Message)
  175.     local isadmin = nil
  176.     if (tonumber(get_var(PlayerIndex,"$lvl"))) >= ADMIN_LEVEL then isadmin = true else isadmin = false end
  177.     if Message ~= nil then
  178.    
  179.         if (Message == "/ss") then
  180.             if (isadmin == true) then
  181.                 local data1 = string.format("%s used the showstats (/ss) command. ", tostring(name))
  182.                 if (LOG_RESULTS == true) then
  183.                     execute_command("log_note \""..data1.."\"")
  184.                 end
  185.                 ShowStats(PlayerIndex) 
  186.                 response = false
  187.             else
  188.                 local data2 = string.format("%s attempted to use the showstats (/ss) command. ", tostring(name))
  189.                 if (LOG_RESULTS == true) then
  190.                     execute_command("log_note \""..data2.."\"")
  191.                 end                
  192.                 say(PlayerIndex, "You are not allowed to use this command, or you must log in first.")
  193.                 response = true
  194.             end
  195.         end
  196.        
  197.     end
  198.     return response
  199. end    
  200.  
  201. function ShowStats(PlayerIndex)
  202.     for j=1,30 do
  203.         rprint(PlayerIndex," ")
  204.     end
  205.     for i = 1,16 do
  206.         if player_present(i) then
  207.             local id = get_var(i, "$n")
  208.             if headshot_count[id] == nil then headshot_count[id] = 0 end
  209.             local headshots = tonumber(headshot_count[id])
  210.             local name = get_var(i, "$name")
  211.             local kills = tonumber(get_var(i, "$kills"))
  212.             if kills == nil then kills = 0 end
  213.             local player_stats = string.format("ID#: %s --   %s --  KILLS: %s --   HEADSHOTS: %s", id, name, kills, headshots)
  214.             rprint(PlayerIndex, player_stats)
  215.             if (LOG_RESULTS == true) then
  216.                 execute_command("log_note \""..player_stats.."\"")
  217.             end    
  218.         end
  219.     end    
  220. end
  221.  
  222. function OnCommand(PlayerIndex,Command,Environment,Password)
  223.     Command = string.lower(Command)
  224.     -- if chat command, i.e. chat: /reset
  225.     if (Environment == 2) then
  226.         if (Command == RESET_COMMAND) then
  227.             if (tonumber(get_var(PlayerIndex, "$lvl")) >= ADMIN_LEVEL) then
  228.                 for i = 1, 16 do
  229.                     if player_present(i) then
  230.                         head_shot[i] = false
  231.                         headshot_count[i] = 0      
  232.                     end
  233.                 end
  234.                 execute_command("sv_map_reset")
  235.                 return false
  236.             end
  237.         end
  238.     -- if console or rcon  
  239.     elseif (Environment == 0) or (Environment == 1) then
  240.         if (Command == "sv_map_reset") then
  241.             for i = 1, 16 do
  242.                 if player_present(i) then
  243.                     head_shot[i] = false
  244.                     headshot_count[i] = 0      
  245.                 end
  246.             end
  247.             execute_command("sv_map_reset")    
  248.             return true
  249.         end
  250.     end
  251.     return true
  252. end
  253.  
  254. function generatekill(killtype)
  255.     local killtype = {"destroyed", "fubbared", "disemboweled", "violated", "eviscerated", "assassinated", "slaughtered", "exterminated", "murdered", "eradicated", "executed", "snuffed", "eliminated", "liquidated"}
  256.     local killcount = #killtype
  257.     local rand_type = rand(1, killcount+1)
  258.     local kill_type = string.format("%s",  killtype[rand_type])
  259.     if (kill_type ~= nil) then
  260.         return kill_type
  261.     else
  262.         return "killed"
  263.     end
  264. end
  265.  
  266. function OnError(Message)
  267.     print(debug.traceback())
  268. end
  269.    
  270. -- Created by H® Shaft
  271. -- Visit http://halorace.org/forum/index.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement