Advertisement
HR_Shaft

Object Tag Info Utility for SAPP

Aug 5th, 2015
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.51 KB | None | 0 0
  1. -- Object Tag Info Utility | by H® Shaft  
  2.  
  3. -- NOT used for games: Scripting utility to retrieve tag types, tag names, location and meta id's
  4.  
  5. -- type the following commands in chat to retrieve and log tag information into the sapp.log and on screen
  6.  
  7. -- COMMAND to type (without quotes):
  8. -- "weapon"     -- | retrieve the tag info for your current (primary) weapon
  9. -- "vehicle"    -- | retrieve the tag info for your current vehicle (you must be in a vehicle)
  10. -- "biped"      -- | retrieve the tag info for your current biped
  11. -- "coord"      -- | retrieve your coordinates x,y,z and rotation (yaw - the direction you are facing, not "aim")
  12. -- "save"       -- | type "save" to export all object data to a text file in your sapp directory, (takes 15 to 45 seconds be patient)
  13.  
  14. -- The information exported/stored will be:
  15. -- Object ID, Object Type, Object Tag Name, Object Coordinates (xyz), Object Yaw, Object Pitch, Object Roll, Object Rotation
  16. -- the file name which will be saved is "<mapname>_<gametype>_tag_info.txt" (i.e. bloodgulch_slayer_tag_info.txt)
  17.  
  18. -- With your new exported data, you can create a custom script and move objects where you want them, or back to their origin,
  19. -- or block their creation, or replace them, or destroy all objects and restore them to their origin.
  20.  
  21. -- the data exported will look like this, for each object exported using the save command:
  22.             --[1] = "obj_id: 3799056386",
  23.             --[2] = "obj_type: vehi",
  24.             --[3] = "tag_name: vehicles\\warthog\\mp_warthog",
  25.             --[4] = "meta_id: 3822322272",
  26.             --[5] = "X: 28.961212158203",
  27.             --[6] = "Y: -90.674217224121",
  28.             --[7] = "Z: 0.68556469678879",
  29.             --[8] = "yaw: -516601.1875",
  30.             --[9] = "pitch: -4.7147543232472e+27",
  31.             --[10] = "roll: 1.0005150372047e-33",
  32.             --[11] = "rotation: -1.5430639405479e-32"
  33.  
  34. -- Note: your gametype controls which weapons and vehicles spawn so you might need to make a custom gametype to spawn what you want then use that with this script
  35.  
  36. api_version = "1.8.0.0"
  37. game_started = false
  38. Object_IDS = {}
  39. equipment = {}
  40. vehicle = {}
  41. biped = {}
  42. weapon = {}
  43.  
  44. function OnScriptLoad()
  45.     register_callback(cb['EVENT_GAME_START'], "OnNewGame")
  46.     register_callback(cb['EVENT_GAME_END'], "OnGameEnd")
  47.     register_callback(cb['EVENT_OBJECT_SPAWN'], "OnObjectSpawn")
  48.     register_callback(cb['EVENT_SPAWN'], "OnPlayerSpawn")
  49.     register_callback(cb['EVENT_CHAT'], "OnPlayerChat")
  50. end
  51.  
  52. function OnScriptUnload() end
  53.  
  54. function OnNewGame()
  55.     game_started = true
  56.     map_name = get_var(0,"$map")
  57.     game_type = get_var(0,"$mode")
  58. end
  59.  
  60. function OnGameEnd()
  61.     game_started = false
  62. end
  63.  
  64. function OnObjectSpawn(PlayerIndex, MapID, ParentID, ObjectID)
  65.     table.insert(Object_IDS, ObjectID) 
  66.     return true
  67. end
  68.  
  69. function OnPlayerSpawn(PlayerIndex)
  70.     -- we want the biped to be listed in the object id's, but NOT his weapons to prevent confusion
  71.     execute_command_sequence("nades me 0;wdel", PlayerIndex)
  72. end
  73.  
  74. function PopulateTables()
  75.     for _,id in pairs(Object_IDS) do
  76.         local object = get_object_memory(id)
  77.         if object ~= 0 then
  78.             local obj_type = read_byte(object + 0xB4)
  79.             local obj_meta = read_dword(object)
  80.             local obj_name = read_string(read_dword(read_word(object) * 32 + 0x40440038))
  81.             local objtype = ""
  82.             local x, y, z = read_vector3d(object + 0x5c)
  83.             local yaw = read_float(object + 0x76)
  84.             local pitch = read_float(object + 0x7A)
  85.             local roll = read_float(object + 0x7E)
  86.             local rotation = read_float(object + 0x82)
  87.             if obj_type == 0 then
  88.                 objtype = "bipd"
  89.                 table.insert(biped, {"obj_id: " ..id, "obj_type: " ..objtype, "tag_name: " ..obj_name, "meta_id: " ..obj_meta, "X: " ..x, "Y: " ..y, "Z: " ..z, "yaw: " ..yaw, "pitch: " .. pitch, "roll: " ..roll, "rotation: " ..rotation})
  90.             elseif obj_type == 1 then
  91.                 objtype = "vehi"
  92.                 table.insert(vehicle, {"obj_id: " ..id, "obj_type: " ..objtype, "tag_name: " ..obj_name, "meta_id: " ..obj_meta, "X: " ..x, "Y: " ..y, "Z: " ..z, "yaw: " ..yaw, "pitch: " .. pitch, "roll: " ..roll, "rotation: " ..rotation})
  93.             elseif obj_type == 3 then
  94.                 objtype = "eqip"
  95.                 table.insert(equipment, {"obj_id: " ..id, "obj_type: " ..objtype, "tag_name: " ..obj_name, "meta_id: " ..obj_meta, "X: " ..x, "Y: " ..y, "Z: " ..z, "yaw: " ..yaw, "pitch: " .. pitch, "roll: " ..roll, "rotation: " ..rotation})  
  96.             elseif obj_type == 2 then
  97.                 objtype = "weap"
  98.                 table.insert(weapon, {"obj_id: " ..id, "obj_type: " ..objtype, "tag_name: " ..obj_name, "meta_id: " ..obj_meta, "X: " ..x, "Y: " ..y, "Z: " ..z, "yaw: " ..yaw, "pitch: " .. pitch, "roll: " ..roll, "rotation: " ..rotation})
  99.             end
  100.         end    
  101.     end
  102.     -- 15 seconds because it takes time to process table data
  103.     timer(15000, "SaveData")
  104.     return false
  105. end
  106.  
  107. function SaveData()
  108.     local file_name = ("sapp\\"..map_name.."_"..game_type.."_tag_info.txt")
  109.     -- if file exists, open it.
  110.     local file = io.open(file_name, "r")
  111.     if file == nil then
  112.         -- if file doesn't exist, create it, then open it.
  113.         file = io.open(file_name, "w")
  114.         local data = {equipment, vehicle, biped, weapon}
  115.         tablesave(data, file_name)
  116.         local str1 = string.format("Exported tag info file: %s", tostring(file_name))
  117.         execute_command("log_note \""..str1.."\"")         
  118.         file:close()       
  119.     elseif file ~= nil then
  120.         local data = {equipment, vehicle, biped, weapon}
  121.         local str1 = string.format("Exported tag info file: %s", tostring(file_name))
  122.         execute_command("log_note \""..str1.."\"")     
  123.         tablesave(data, file_name)         
  124.         file:close()
  125.     end
  126.     return false
  127. end
  128.  
  129. function OnPlayerChat(PlayerIndex, Message)
  130.     local response = nil
  131.     local name = get_var(PlayerIndex,"$name")
  132.     local Message = string.lower(Message)
  133.    
  134.     -- | retrieve your coordinates x,y,z and rotation (yaw) (the direction you are facing, not "aim")
  135.     if (Message == "coord") then
  136.         local player_object = get_dynamic_player(PlayerIndex)
  137.         local player_static = get_player(PlayerIndex)
  138.         local x,y,z = read_vector3d(player_object + 0x5C)
  139.         local rotation = read_float(player_static + 0x138)
  140.         local map_name = get_var(0,"$map")
  141.         local data = string.format("%s,%s,%s,%s,%s", tostring(map_name),tostring(x),tostring(y),tostring(z),tostring(rotation))
  142.         execute_command("log_note \""..data.."\"")     
  143.         say(PlayerIndex, "X: " .. x .. " Y: " .. y .. " Z: " .. z .. " R: " .. rotation)
  144.         response = false
  145.     end
  146.    
  147.     -- | retrieve the tag info for your current (primary) weapon
  148.     if (Message == "weapon") then
  149.         if isinvehicle(PlayerIndex) then
  150.             say(PlayerIndex, "You cannot be in a vehicle to retrieve your weapon's name and data.")
  151.         else   
  152.             local player_object = get_dynamic_player(PlayerIndex)
  153.             local weaponId = read_dword(player_object + 0x118)
  154.             local weap_obj = get_object_memory(weaponId)
  155.             if weap_obj ~= 0 then
  156.                 local weap_name = "unknown"
  157.                 weap_name = read_string(read_dword(read_word(weap_obj) * 32 + 0x40440038))
  158.                 local weap_meta = read_dword(weap_obj)
  159.                 local data = string.format("Map: %s  Weap Tag Name: %s  MetaId: %s", tostring(map_name),tostring(weap_name),tostring(weap_meta))       
  160.                 execute_command("log_note \""..data.."\"")     
  161.                 say(PlayerIndex, data) 
  162.             end
  163.         end
  164.         response = false   
  165.     end
  166.    
  167.     -- | retrieve the tag info for your current vehicle (you must be in a vehicle)
  168.     if (Message == "vehicle") then
  169.         if isinvehicle(PlayerIndex) then
  170.             local player_object = get_dynamic_player(PlayerIndex)
  171.             local veh_id = read_dword(player_object + 0x11C)
  172.             local veh_obj = get_object_memory(veh_id)
  173.             if veh_obj ~= 0 then
  174.                 local veh_name = "unknown"
  175.                 veh_name = read_string(read_dword(read_word(veh_obj) * 32 + 0x40440038))
  176.                 local veh_meta = read_dword(veh_obj)
  177.                 local data = string.format("Map: %s  Veh Tag Name: %s  MetaId: %s", tostring(map_name),tostring(veh_name),tostring(veh_meta))      
  178.                 execute_command("log_note \""..data.."\"")     
  179.                 say(PlayerIndex, data) 
  180.             end
  181.         else
  182.             say(PlayerIndex, "You must be in a vehicle to retrieve it's name and data.")
  183.         end
  184.         response = false   
  185.     end
  186.    
  187.     -- | retrieve the tag info for your current biped
  188.     if (Message == "biped") then
  189.         if isinvehicle(PlayerIndex) then
  190.             say(PlayerIndex, "You cannot be in a vehicle to retrieve your biped's name and data.")
  191.         else   
  192.             local player_object_id = read_dword(get_player(PlayerIndex) + 0x34)
  193.             local player_object = get_object_memory(player_object_id)
  194.             if player_object ~= 0 then
  195.                 local bipd_name = "unknown"
  196.                 bipd_name = read_string(read_dword(read_word(player_object) * 32 + 0x40440038))
  197.                 local bipd_meta = read_dword(player_object)
  198.                 local data = string.format("Map: %s  Biped Tag Name: %s  MetaId: %s", tostring(map_name),tostring(bipd_name),tostring(bipd_meta))      
  199.                 execute_command("log_note \""..data.."\"")     
  200.                 say(PlayerIndex, data) 
  201.             end
  202.         end
  203.         response = false   
  204.     end
  205.    
  206.     -- export all object data to a text file in your sapp directory
  207.     if (Message == "save") then
  208.         timer(1000, "PopulateTables")
  209.         say(PlayerIndex, "Exporting Object Tag Info: please wait 45 seconds, then check your sapp folder for the txt file export.")
  210.         response = false
  211.     end
  212.    
  213.     return response
  214. end
  215.  
  216. -- miscellaneous functions --
  217. function isinvehicle(PlayerIndex)
  218.     local player_object = get_dynamic_player(PlayerIndex)
  219.     local vehicleId = read_dword(player_object + 0x11C)
  220.     if vehicleId == 0xFFFFFFFF then
  221.         return false
  222.     else
  223.         return true
  224.     end
  225. end
  226.  
  227. --Nuggets table save and table load and related functions
  228. function tablesave(t, filename)
  229.     local file = io.open(filename, "w")
  230.     local spaces = 0
  231.     local function tab()
  232.         local str = ""
  233.         for i = 1,spaces do
  234.             str = str .. " "
  235.         end
  236.         return str
  237.     end
  238.     local function format(t)
  239.         spaces = spaces + 4
  240.         local str = "{ "
  241.         for k,v in opairs(t) do
  242.             -- Key datatypes
  243.             if type(k) == "string" then
  244.                 k = string.format("%q", k)
  245.             elseif k == math.inf then
  246.                 k = "1 / 0"
  247.             end
  248.             k = tostring(k)
  249.             -- Value datatypes
  250.             if type(v) == "string" then
  251.                 v = string.format("%q", v)
  252.             elseif v == math.inf then
  253.                 v = "1 / 0"
  254.             end
  255.             if type(v) == "table" then
  256.                 if tablelen(v) > 0 then
  257.                     str = str .. "\n" .. tab() .. "[" .. k .. "] = " .. format(v) .. ","
  258.                 else
  259.                     str = str .. "\n" .. tab() .. "[" .. k .. "] = {},"
  260.                 end
  261.             else
  262.                 str = str .. "\n" .. tab() .. "[" .. k .. "] = " .. tostring(v) .. ","
  263.             end
  264.         end
  265.         spaces = spaces - 4
  266.         return string.sub(str, 1, string.len(str) - 1) .. "\n" .. tab() .. "}"
  267.     end
  268.     file:write("return " .. format(t))
  269.     file:close()
  270. end
  271.  
  272. function tableload(filename)
  273.     local file = loadfile(filename)
  274.     if file then
  275.         return file() or {}
  276.     end
  277.     return {}
  278. end
  279.  
  280. function tablelen(t)
  281.     local count = 0
  282.     for k,v in pairs(t) do
  283.         count = count + 1
  284.     end
  285.     return count
  286. end
  287.  
  288. function opairs(t)
  289.     local keys = {}
  290.     for k,v in pairs(t) do
  291.         table.insert(keys, k)
  292.     end
  293.     table.sort(keys,
  294.     function(a,b)
  295.         if type(a) == "number" and type(b) == "number" then
  296.             return a < b
  297.         end
  298.         an = string.lower(tostring(a))
  299.         bn = string.lower(tostring(b))
  300.         if an ~= bn then
  301.             return an < bn
  302.         else
  303.             return tostring(a) < tostring(b)
  304.         end
  305.     end)
  306.     local count = 1
  307.     return function()
  308.         if unpack(keys) then
  309.             local key = keys[count]
  310.             local value = t[key]
  311.             count = count + 1
  312.             return key,value
  313.         end
  314.     end
  315. end
  316.  
  317. function spaces(n, delimiter)
  318.     delimiter = delimiter or ""
  319.     local str = ""
  320.     for i = 1, n do
  321.         if i == math.floor(n / 2) then
  322.             str = str .. delimiter
  323.         end
  324.         str = str .. " "
  325.     end
  326.     return str
  327. end
  328.  
  329. function OnError(Message)
  330.     print(debug.traceback())
  331. end
  332.  
  333. -- Created by H® Shaft
  334. -- Visit http://halorace.org/forum/index.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement