Advertisement
HR_Shaft

Gun Me for SAPP

Jan 2nd, 2017
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.98 KB | None | 0 0
  1. -- Gun Me for SAPP
  2. -- by H® Shaft
  3.  
  4. -- allows vehicle drivers to press their melee/f key to insert a walking teammate into their gunner or passenger seat
  5. -- any vehicle occupant can type "gun me" to insert a walking teammate into their driver, gunner or passenger seat
  6. -- will not pickup any player who has flag or oddball
  7.  
  8. api_version = "1.9.0.0"
  9. -- do not touch --
  10. gunme = {}
  11. game_started = false
  12.  
  13. function OnScriptLoad()
  14.     register_callback(cb['EVENT_TICK'],"OnTick")
  15.     register_callback(cb['EVENT_JOIN'],"OnPlayerJoin")
  16.     register_callback(cb['EVENT_LEAVE'],"OnPlayerLeave")   
  17.     register_callback(cb['EVENT_GAME_START'],"OnNewGame")
  18.     register_callback(cb['EVENT_GAME_END'],"OnGameEnd")
  19.     register_callback(cb['EVENT_CHAT'], "OnPlayerChat")
  20.     if get_var(0, "$gt") ~= "n/a" then
  21.         game_started = true
  22.         for i=1,16 do
  23.             if player_present(i) then
  24.                 gunme[i] = 0
  25.             end    
  26.         end
  27.     end
  28. end
  29.  
  30. function OnScriptUnload()
  31.     gunme = {}
  32. end
  33.  
  34. function OnPlayerJoin(PlayerIndex)
  35.     if player_present(PlayerIndex) then
  36.         gunme[PlayerIndex] = 0 
  37.     end
  38. end
  39.  
  40. function OnPlayerLeave(PlayerIndex)
  41.     if player_present(PlayerIndex) then
  42.         gunme[PlayerIndex] = {}
  43.     end
  44. end
  45.  
  46. function OnNewGame()
  47.     game_started = true
  48. end
  49.  
  50. function OnGameEnd()
  51.     game_started = false
  52.     gunme = {}
  53. end
  54.  
  55. function OnTick()
  56.     for i=1,16 do
  57.         if player_present(i) then
  58.             if player_alive(i) then
  59.                 local player_dyn = get_dynamic_player(i)
  60.                 if (player_dyn ~= 0) then
  61.                     if PlayerIsDriver(i) then
  62.                         if gunme[i] == nil then gunme[i] = 0 end
  63.                         if gunme[i] > 0 then gunme[i] = gunme[i] - 1 end
  64.                         if (gunme[i] == 0) and (read_bit(player_dyn + 0x208, 7) == 1) then
  65.                             getWalker(tonumber(i))
  66.                             gunme[i] = math.floor(1.5*30)
  67.                         end
  68.                     end
  69.                 end
  70.             end
  71.         end
  72.     end
  73. end
  74.  
  75. function OnPlayerChat(PlayerIndex, Message)
  76.     local response = nil
  77.     local name = get_var(PlayerIndex,"$name")
  78.     local Message = string.lower(Message)
  79.     if (Message == "gunme") or (Message == "/gunme") or (Message == "gun me") or (Message == "/gun me") then
  80.         response = false
  81.         if player_present(PlayerIndex) then
  82.             if (game_started == true) then
  83.                 if player_alive(PlayerIndex) then
  84.                     if isinvehicle(PlayerIndex) == true then
  85.                         getWalker(tonumber(PlayerIndex))
  86.                     else
  87.                         say(PlayerIndex, "**Gun Me** You must be in a vehicle to use this feature.")
  88.                     end
  89.                 else
  90.                     say(PlayerIndex, "**Gun Me** Derp! You are dead!")
  91.                 end
  92.             else
  93.                 say(PlayerIndex, "**Gun Me** Please wait until the next map begins.")
  94.             end
  95.         end
  96.     end
  97.     return response
  98. end
  99.  
  100. -- checks available seats of players vehicle, looks for teammates not in vehicles, then injects them into an availble seat
  101. function getWalker(PlayerIndex)
  102.     if (player_alive(PlayerIndex) ~= true) then return false end
  103.     local team = get_var(PlayerIndex,"$team")
  104.     local m_vehicleId = GetplayerVehicleId(PlayerIndex)
  105.     if m_vehicleId ~= false then
  106.         if isinvehicle(PlayerIndex) then
  107.             if game_started == true then
  108.                 if tonumber(checkSpaces(PlayerIndex, m_vehicleId)) > 0 then
  109.                     local player2
  110.                     for i = 1,16 do
  111.                         if PlayerIndex ~= i and player_alive(i) and get_var(i,"$team") == team and not isinvehicle(i) then
  112.                             player2 = i
  113.                             break
  114.                         end
  115.                     end
  116.                     if player2 then
  117.                         if not Has_Objective(player2) then
  118.                             InjectPlayer(player2, tonumber(m_vehicleId), false)
  119.                         else
  120.                             say(PlayerIndex, "**Gun Me** No available teammates, or teammate has flag or oddball.")
  121.                         end
  122.                     else
  123.                         say(PlayerIndex, "**Gun Me** No available teammates.")
  124.                     end
  125.                 else
  126.                     say(PlayerIndex, "**Gun Me** No empty seats in your vehicle.")
  127.                 end
  128.             elseif game_started == false then
  129.                 say(PlayerIndex, "**Gun Me** Please wait until the next map begins.")
  130.             end
  131.         else
  132.             say(PlayerIndex, "**Gun Me** You must be in a vehicle to use this feature.")
  133.         end
  134.     end
  135. end
  136.  
  137. -- inserts selected player into selected vehicle
  138. function InjectPlayer(PlayerIndex, m_vehicleId, count)
  139.     if (player_alive(PlayerIndex) ~= true) then return false end
  140.     local bool = false
  141.     if player_alive(PlayerIndex) then
  142.         if m_vehicleId ~= false then
  143.             local veh_obj = get_object_memory(m_vehicleId)
  144.             local driver = read_dword(veh_obj + 0x324)
  145.             local gunner = read_dword(veh_obj + 0x328)
  146.             local passenger = read_dword(veh_obj + 0x32C)  
  147.             if driver == 0xFFFFFFFF then
  148.                 enter_vehicle(m_vehicleId, PlayerIndex, 0)
  149.                 bool = true
  150.             elseif gunner == 0xFFFFFFFF then
  151.                 enter_vehicle(m_vehicleId, PlayerIndex, 2)
  152.                 bool = true
  153.             elseif (passenger == 0xFFFFFFFF or passenger  == 0) then
  154.                 enter_vehicle(m_vehicleId, PlayerIndex, 1)
  155.                 bool = true
  156.             end
  157.             if bool and not count then
  158.                 say(PlayerIndex, "**Gun Me** You have been put into the vehicle by a teammate.")
  159.             end
  160.         end
  161.     end
  162. end
  163.  
  164. function PlayerIsDriver(PlayerIndex)
  165.     if (player_alive(PlayerIndex) == false) then return false end
  166.     local player_object = get_dynamic_player(PlayerIndex)
  167.     local player_object_id = read_dword(get_player(PlayerIndex) + 0x34)
  168.     local vehicleId = read_dword(player_object + 0x11C)
  169.     if (vehicleId == 0xFFFFFFFF) then return false end
  170.     local obj_id = get_object_memory(vehicleId)
  171.     return read_dword(obj_id + 0x324) == player_object_id
  172. end
  173.  
  174. -- returns vehicle object id player is in, or false
  175. function GetplayerVehicleId(PlayerIndex)
  176.     if (player_alive(PlayerIndex) == false) then return false end
  177.     local player_object = get_dynamic_player(PlayerIndex)
  178.     local vehicleId = read_dword(player_object + 0x11C)
  179.     if (vehicleId ~= 0xFFFFFFFF) then return vehicleId else return false end
  180. end
  181.  
  182. function get_vehicle_name(MetaID) -- Thanks to Giraffe!
  183.     if (MetaID == 0xFFFFFFFF) then
  184.         return "NULL METAID"
  185.     else
  186.         local globals_tag = lookup_tag("matg", "globals\\globals")
  187.         local globals_data = read_dword(globals_tag + 0x14)
  188.         local interface_bitmaps_data = read_dword(globals_data + 0x144)
  189.         local hud_globals_metaid = read_dword(interface_bitmaps_data + 0x6C)
  190.         local hud_globals_tag = lookup_tag(hud_globals_metaid)
  191.         local hud_globals_data = read_dword(hud_globals_tag + 0x14)
  192.         local hud_icon_messages_metaid = read_dword(hud_globals_data + 0xC0)
  193.         local hud_icon_messages_tag = lookup_tag(hud_icon_messages_metaid)
  194.         local hud_icon_messages_data = read_dword(hud_icon_messages_tag + 0x14)
  195.         local string_references_count = read_dword(hud_icon_messages_data)
  196.         local string_references_data = read_dword(hud_icon_messages_data + 0x4)
  197.         local strings = {}
  198.         for i=0,string_references_count-1 do
  199.             local bytes = read_dword(string_references_data + i*20)
  200.             local string_data = read_dword(string_references_data + i*20 + 0xC)
  201.             local string = ''
  202.             for j=0,bytes-3,2 do
  203.                 string = string .. string.char(read_char(string_data +j))
  204.             end
  205.             strings[i] = string
  206.         end
  207.         local vehicle_tag = lookup_tag(MetaID)
  208.         local vehicle_data = read_dword(vehicle_tag + 0x14)
  209.         local hud_text_message_index = read_short(vehicle_data + 0x13C)
  210.         if(strings[hud_text_message_index] ~= nil) then
  211.             return strings[hud_text_message_index]
  212.         else
  213.             return strings[0]
  214.         end
  215.     end
  216. end
  217.  
  218. function isinvehicle(PlayerIndex)
  219.     local player_object = get_dynamic_player(PlayerIndex)
  220.     if player_object ~= 0 then
  221.         local vehicleId = read_dword(player_object + 0x11C)
  222.         if vehicleId == 0xFFFFFFFF then
  223.             return false
  224.         else
  225.             return true
  226.         end
  227.     else
  228.         return false
  229.     end
  230. end
  231.  
  232. -- returns vehicle tag name of players vehicle for function GetVehicleSeats, which sets number of available seats in the vehicle
  233. function GetPlayerVehicleTagName(PlayerIndex)
  234.     if isinvehicle(PlayerIndex) and game_started then
  235.         local player_object = get_dynamic_player(PlayerIndex)
  236.         local vehicleId = read_dword(player_object + 0x11C)
  237.         local vehicle_obj = get_object_memory(vehicleId)
  238.         if (vehicle_obj ~= 0) then
  239.             local vehicle_metaid = read_dword(vehicle_obj)     
  240.             local veh_name = get_vehicle_name(vehicle_metaid)
  241.             if veh_name ~= nil and veh_name ~= "NULL METAID" then
  242.                 return veh_name
  243.             else
  244.                 return nil
  245.             end
  246.         end
  247.     end
  248. end
  249.  
  250. -- returns the number of available seats in players vehicle, requires GetVehicleSeats function
  251. function checkSpaces(PlayerIndex, m_vehicleId)
  252.     if (player_alive(PlayerIndex) == false) then return 0 end
  253.     local seats = GetVehicleSeats(PlayerIndex, m_vehicleId)
  254.     local team = get_var(PlayerIndex,"$team")
  255.     if (m_vehicleId ~= nil) then
  256.         if seats > 0  then
  257.             for i = 1,16 do
  258.                 if PlayerIndex ~= i and player_alive(i) and get_var(i,"$team") == team and isinvehicle(i) then
  259.                     if GetplayerVehicleId(i) == m_vehicleId then
  260.                         seats = seats - 1
  261.                     end
  262.                 end
  263.             end
  264.         end
  265.         return seats
  266.     end    
  267. end
  268.  
  269. -- returns number of seats in selected vehicle, sets the number of seats (some vehicles have seats not correctly setup)
  270. -- return the number of vehicle seats not including the driver
  271. function GetVehicleSeats(PlayerIndex, m_vehicleId)
  272.     if (player_alive(PlayerIndex) == false) then return false end
  273.     if m_vehicleId ~= false then
  274.         local avail_seats = 0
  275.         local veh_name = string.lower(GetPlayerVehicleTagName(PlayerIndex))
  276.         if veh_name ~= nil then
  277.             if string.find(veh_name, "ghost") or string.find(veh_name, "banshee") or string.find(veh_name, "turret") or string.find(veh_name, "snowhog") or string.find(veh_name, "hogfoot") or string.find(veh_name, "civi") then
  278.                 avail_seats = 0    
  279.             elseif string.find(veh_name, "hog") or string.find(veh_name, "hawg") or string.find(veh_name, "puma") then
  280.                 avail_seats = 2
  281.             elseif string.find(veh_name, "scorpion") or string.find(veh_name, "falcon") or string.find(veh_name, "car") then
  282.                 avail_seats = 1
  283.             else
  284.                 avail_seats = 0
  285.             end
  286.             local seats = avail_seats
  287.             return tonumber(seats)
  288.         end
  289.     else
  290.         return false
  291.     end
  292. end
  293.  
  294. -- iterates thru all players weapons, returns true if they have flag or oddball, if not, returns false
  295. -- this is specifically checking the 'must be readied' flag of each of their weapons
  296. function Has_Objective(PlayerIndex)
  297.     local response = false
  298.     if get_var(0, "$gt") ~= "n/a" then
  299.         if (get_var(0, "$gt") == "ctf") or (get_var(0, "$gt") == "oddball") then
  300.             local player_object = get_dynamic_player(PlayerIndex)
  301.             for w=0,3 do
  302.                 local weap_id = read_dword(player_object + 0x2F8 + 0x4 * w)
  303.                 if (weap_id ~= 0xFFFFFFFF) then
  304.                     local weap_obj = get_object_memory(weap_id)
  305.                     if (weap_obj ~= 0) then
  306.                         local obj_type = read_byte(weap_obj + 0xB4)
  307.                         local tag_address = read_word(weap_obj)
  308.                         local tagdata = read_dword(read_dword(0x40440000) + tag_address * 0x20 + 0x14)
  309.                         if (read_bit(tagdata + 0x308,3) == 1) then
  310.                             response = true
  311.                         end
  312.                     end    
  313.                 end
  314.             end    
  315.         end
  316.     end
  317.     return response
  318. end
  319.  
  320. function OnError(Message)
  321.     print(debug.traceback())
  322. end
  323.  
  324. -- Created by H® Shaft
  325. -- Visit http://halorace.org/forum/index.php
  326.  
  327. -- The player -db-GoNe/Juhleek - a well spoken liar, cheat.
  328. -- -db- is: "diverging from the believable" - How ironic. A race clan where admins use rcon to favor themselves to win.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement