Advertisement
rockbandcheeseman

WeaponDetect

Oct 11th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.97 KB | None | 0 0
  1. -- Weapon Spawn and Movement Detection --
  2.  
  3. -- List of weapons we should print messages for when they spawn or move (enter as tagnames with quotes around the name and commas in between)
  4. monitored = {"weapons\\rocket launcher\\rocket launcher",
  5.            
  6.             }
  7.            
  8. --[[
  9.  
  10.     List of weapon tags:
  11.    
  12.     weapons\\assault rifle\\assault rifle
  13.     weapons\\ball\\ball
  14.     weapons\\flag\\flag
  15.     weapons\\flamethrower\\flamethrower
  16.     weapons\\gravity rifle\\gravity rifle
  17.     weapons\\needler\\mp_needler
  18.     weapons\\pistol\\pistol
  19.     weapons\\plasma pistol\\plasma pistol
  20.     weapons\\plasma rifle\\plasma rifle
  21.     weapons\\plasma_cannon\\plasma_cannon
  22.     weapons\\rocket launcher\\rocket launcher
  23.     weapons\\shotgun\\shotgun
  24.     weapons\\sniper rifle\\sniper rifle
  25.    
  26. --]]
  27.  
  28. message_begin_game = false  -- Set this to true if you want the spawn message to print when the weapons spawn at the beginning of the game (I've assumed you wouldn't want that, but I'll give you the option)
  29. spawn_message = " has spawned."  -- Message printed when a weapon has spawned (begins with the name of the weapon)
  30. move_message = " has moved."  -- Message printed when a weapon has moved (begins with the name of the weapon)
  31.  
  32. function GetRequiredVersion()
  33.  
  34.     return 200
  35. end
  36.  
  37. function OnScriptLoad(processId, game, persistent)
  38.  
  39.  
  40. end
  41.  
  42. --[[
  43. function OnScriptUnload()
  44.  
  45.  
  46. end
  47. --]]
  48.  
  49. function OnNewGame(map)
  50.  
  51.     if not message_begin_game then
  52.         print_message = false
  53.         registertimer(1000, "MessageDelay")
  54.     end
  55.    
  56.     info = {}  -- Stores information about the weapons we're tracking
  57.    
  58.     movement_timer = registertimer(100, "MovementTimer")
  59. end
  60.  
  61. function MessageDelay(id, count)
  62.  
  63.     print_message = true
  64.     return false
  65. end
  66.  
  67. function MovementTimer(id, count)
  68.  
  69.     for objId,data in pairs(info) do
  70.         local m_object = getobject(objId)  -- check if the object still exists
  71.         if m_object then
  72.             if not info[objId].held then  -- We will stop monitoring this weapon once it has been held
  73.                 -- Check if a player is holding this weapon
  74.                 for i = 0,15 do
  75.                     local m_player = getplayer(i)
  76.                     if m_player then  -- If this player exists...
  77.                         local player_objId = readdword(m_player, 0x34)
  78.                         local m_object = getobject(player_objId)
  79.                         if m_object then  -- If this player is alive...
  80.                             for slot = 0,3 do
  81.                                 local weapId = readdword(m_object, 0x2F8 + (slot * 4))
  82.                                 if weapId == objId then
  83.                                     info[objId].held = true
  84.                                     break
  85.                                 end
  86.                             end
  87.                            
  88.                             if info[objId].held then break end
  89.                         end
  90.                     end
  91.                 end
  92.                
  93.                 if not info[objId].held then  -- Check again to make sure the weapon isn't being held
  94.                     if not info[objId].moved then  -- If the weapon hasn't yet moved...
  95.                         -- Check if the weapon has moved
  96.                         local x, y, z = getobjectcoords(objId)
  97.                         local cushion = 0.2  -- Allow the weapon to move within a very small cushion before we send a message
  98.                         local dist = math.sqrt((x - data.spawn_x) ^ 2 + (y - data.spawn_y) ^ 2 + (z - data.spawn_z) ^ 2)
  99.                         if dist >= cushion then
  100.                             info[objId].moved = true
  101.                             say(capitalize(aoran(info[objId].name)) .. " " .. info[objId].name .. move_message)
  102.                         end
  103.                     end
  104.                 end
  105.             end
  106.         else
  107.             info[objId] = nil  -- If this object doesn't exist, nullify its key in the info table
  108.         end
  109.     end
  110.    
  111.     return true
  112. end
  113.  
  114. function OnGameEnd(stage)
  115.  
  116.     if stage == 1 then
  117.         removetimer(movement_timer)
  118.     end
  119. end
  120.  
  121. function OnObjectCreation(objId)
  122.  
  123.     local m_object = getobject(objId)
  124.     local mapId = readdword(m_object)
  125.     local tagname, tagtype = gettaginfo(mapId)  -- Get the tagname and tagtype from the mapId
  126.     if tagtype == "weap" then  -- If this is a weapon...
  127.         if table.find(monitored, tagname) then  -- If this weapon tagname is in the monitored table...
  128.            
  129.             local weap_name = getnamefromtag(tagname)
  130.             if print_message then
  131.                 say(capitalize(aoran(weap_name)) .. " " .. weap_name .. spawn_message)  -- Say the message
  132.             end
  133.            
  134.             -- Store information about this weapon
  135.             local x, y, z = getobjectcoords(objId)  -- Coordinates
  136.             info[objId] = {}
  137.             info[objId].spawn_x = x
  138.             info[objId].spawn_y = y
  139.             info[objId].spawn_z = z
  140.             info[objId].name = weap_name  -- Store the name of the weapon
  141.             info[objId].moved = false  -- Becomes true if the weapon moves from its spawn location
  142.             info[objId].held = false  -- Becomes true if a player picks up this weapon
  143.         end
  144.     end
  145. end
  146.  
  147. -- A function to determine if "a" or "an" should be used; it's not perfect but it will work in this case
  148. function aoran(next_word)
  149.  
  150.     local vowels = {"A", "E", "I", "O", "U", "a", "e", "i", "o", "u"}
  151.     local first = string.sub(next_word, 1, 1)
  152.     if table.find(vowels, first) then
  153.         return "an"
  154.     else
  155.         return "a"
  156.     end
  157. end
  158.  
  159. function capitalize(str)
  160.  
  161.     local first = string.sub(str, 1, 1)
  162.     local rest = string.sub(str, 2, string.len(str))
  163.     return string.upper(first) .. rest
  164. end
  165.  
  166. function getnamefromtag(tag)
  167.  
  168.     if tag == "weapons\\assault rifle\\assault rifle" then
  169.         return "Assault Rifle"
  170.     elseif tag == "weapons\\ball\\ball" then
  171.         return "Oddball"
  172.     elseif tag == "weapons\\flag\\flag" then
  173.         return "Flag"
  174.     elseif tag == "weapons\\flamethrower\\flamethrower" then
  175.         return "Flamethrower"
  176.     elseif tag == "weapons\\gravity rifle\\gravity rifle" then
  177.         return "Gravity Rifle"
  178.     elseif tag == "weapons\\needler\\mp_needler" then
  179.         return "Needler"
  180.     elseif tag == "weapons\\pistol\\pistol" then
  181.         return "Pistol"
  182.     elseif tag == "weapons\\plasma pistol\\plasma pistol" then
  183.         return "Plasma Pistol"
  184.     elseif tag == "weapons\\plasma rifle\\plasma rifle" then
  185.         return "Plasma Rifle"
  186.     elseif tag == "weapons\\plasma_cannon\\plasma_cannon" then
  187.         return "Fuel Rod"
  188.     elseif tag == "weapons\\rocket launcher\\rocket launcher" then
  189.         return "Rocket Launcher"
  190.     elseif tag == "weapons\\shotgun\\shotgun" then
  191.         return "Shotgun"
  192.     elseif tag == "weapons\\sniper rifle\\sniper rifle" then
  193.         return "Sniper Rifle"
  194.     end
  195. end
  196.  
  197. function table.find(t, val)
  198.  
  199.     for k,v in pairs(t) do
  200.         if v == val then
  201.             return k
  202.         end
  203.     end
  204. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement