Advertisement
HR_Shaft

Dynamic Vehicle Entry for SAPP

Jan 22nd, 2017
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.22 KB | None | 0 0
  1. -- Dynamic Vehicle Entry for SAPP
  2. -- by H® Shaft and Giraffe
  3.  
  4. -- Players only need to aim at a vehicle then press Action Key (E) to enter it.
  5. -- if they are in range and on the same team (if it is occupied),  and if it is upright
  6. -- they will be inserted into an available seat: 1) driver, 2) gunner, or 3) passenger
  7. -- Compatible with all halo PC/CE maps, stock and custom, protected or not, any vehicle in range
  8. -- Compatible with Multi-Team-Vehicles script for non-team games! http://pastebin.com/vq6HXEpA
  9.  
  10. -- max distance from a vehicle player can be to enter a vehicle (in world units)
  11. -- recommended: 30 for most stock maps, 50-100 for extremely large custom maps (hugeass, extinction, etc)
  12. MAX_DISTANCE = 30
  13.  
  14. -- if set to true, players will be shown the JOIN_MESSAGE below (how to use Dynamic Vehicle Entry)
  15. ENABLE_MESSAGE = true
  16.  
  17. -- message given to joining players if ENABLE_MESSAGE (above) is set to true
  18. -- should not be more than 3 entries, ensure the messages are within quotes,
  19. -- and separated by a comma between each message
  20. JOIN_MESSAGE = {
  21.     "Dynamic Vehicle Entry is Enabled!",
  22.     "Aim at a vehicle then press Action Key (E) to enter it.",
  23. }
  24.  
  25. -- sapp api version
  26. api_version = '1.10.0.0'
  27.  
  28. -- don't edit below here unless you know what you are doing --
  29. function OnScriptLoad()
  30.     register_callback(cb['EVENT_GAME_START'], "OnNewGame")
  31.     register_callback(cb['EVENT_GAME_END'], "OnGameEnd")
  32.     register_callback(cb['EVENT_TICK'], "OnTick")
  33.     register_callback(cb['EVENT_JOIN'], "OnPlayerJoin")
  34.     register_callback(cb['EVENT_LEAVE'], "OnPlayerLeave")
  35.     register_callback(cb['EVENT_DIE'], "OnPlayerDeath")
  36.    
  37.     if get_var(0, "$gt") ~= "n/a" then         
  38.         OnNewGame()
  39.     end
  40.    
  41. end
  42.  
  43. function OnNewGame()
  44.     game_started = true
  45.     LoadDefaults()
  46. end
  47.  
  48. function OnGameEnd()
  49.     game_started = false
  50. end
  51.  
  52. function OnPlayerJoin(PlayerIndex)
  53.     delay[PlayerIndex] = 0
  54.     if ENABLE_MESSAGE then
  55.         timer(12000, "Timed_Message", PlayerIndex)
  56.     end
  57. end
  58.  
  59. function Timed_Message(PlayerIndex)
  60.     if game_started then
  61.         if player_present(PlayerIndex) then
  62.             for l,message in pairs(JOIN_MESSAGE) do
  63.                 say(PlayerIndex,message)               
  64.             end
  65.         end
  66.     end
  67.     return false
  68. end
  69.  
  70. function OnPlayerLeave(PlayerIndex)
  71.     delay[PlayerIndex] = 0
  72. end
  73.  
  74. function OnPlayerDeath(PlayerIndex, KillerIndex)
  75.     if player_present(PlayerIndex) then
  76.         delay[PlayerIndex] = 0
  77.     end
  78. end
  79.  
  80. function OnTick()
  81.     if game_started then   
  82.         for i=1,16 do
  83.             if player_alive(i) then                
  84.                 local player_object = get_dynamic_player(i)
  85.                
  86.                 -- entry delay default and decrement
  87.                 if (delay[i] == nil) then delay[i] = 0 end
  88.                 if (delay[i] > 0) then delay[i] = delay[i] - 1 end
  89.                
  90.                 -- detect action key press - activate if no delay, and if player is not in a vehicle, check for vehicle intersect
  91.                 if not isinvehicle(i) and (read_bit(player_object + 0x208, 6) == 1) then
  92.                     if (delay[i] == 0) then
  93.                         delay[i] = math.floor(2*30)
  94.                         Intersect_Vehicle(i)
  95.                     end
  96.                 end                    
  97.             end
  98.         end
  99.     end
  100. end
  101.  
  102. function Intersect_Vehicle(PlayerIndex)
  103.     if not game_started then return end
  104.     local vehicle_id = 0
  105.     local player_object = get_dynamic_player(PlayerIndex)
  106.     local px, py, pz = read_vector3d(player_object + 0x5c)
  107.     local cx, cy, cz = read_vector3d(player_object + 0x230)
  108.     local ignore_this_pid = read_dword(get_player(PlayerIndex) + 0x34)
  109.     local crouch_state = read_float(player_object + 0x50C)                 
  110.     pz = pz + stand_height - (crouch_state * (stand_height - crouch_height))
  111.        
  112.     local success, x, y, z, target = intersect(px, py, pz, cx*1000, cy*1000, cz*1000, ignore_this_pid)
  113.    
  114.     if (success == true and target ~= 0xFFFFFFFF) then
  115.         local target_object = get_object_memory(target)
  116.         if (target_object ~= 0) then
  117.             local obj_type = read_word(target_object + 0xB4)
  118.             if (obj_type == 1) then -- target is a vehicle
  119.                 vehicle_id = target
  120.                 local distance = tonumber(math.sqrt(((x-px)*(x-px)) + ((y-py)*(y-py)) + ((z-pz)*(z-pz))))
  121.                 if (distance <= MAX_DISTANCE and vehicle_id ~= 0) then
  122.                     enter_vehicle_empty_seat(vehicle_id, PlayerIndex)                  
  123.                 end            
  124.             end
  125.         end
  126.     end
  127. end
  128.  
  129. function enter_vehicle_empty_seat(ObjectID, PlayerIndex)
  130.     if not game_started then return end
  131.     local vehicle = get_object_memory(ObjectID)
  132.     local vehicle_metaid = read_dword(vehicle)
  133.     local vehicle_tag = lookup_tag(vehicle_metaid)
  134.     local vehicle_data = read_dword(vehicle_tag + 0x14)
  135.     if (read_bit(vehicle + 0x8B, 7) == 1) then return false end -- do nothing if vehicle is flipped
  136.     local seats = {}
  137.     for i=1,16 do
  138.         if (player_alive(i)) then
  139.             local player = get_dynamic_player(i)
  140.             if (ObjectID == read_dword(player + 0x11C)) then
  141.                 -- using this method of reading team to enable use with multi-team vehicles
  142.                 if (read_word(get_player(PlayerIndex) + 0x20) ~= read_word(get_player(i) + 0x20)) then
  143.                     return false
  144.                 end
  145.                 seats[read_word(player + 0x2F0)] = i
  146.             end
  147.         end
  148.     end
  149.    
  150.     local driver_seat = nil
  151.     local gunner_seat = nil
  152.    
  153.     local seat_count = read_dword(vehicle_data + 0x2E4)
  154.     local seat_data = read_dword(vehicle_data + 0x2E8)
  155.     for i=0,seat_count-1 do
  156.         local label = read_string32(seat_data + i*284 + 0x4)
  157.         if (biped_seat_labels[label] == true) then
  158.             if (read_bit(seat_data + i*284, 2) == 1) then -- check if driver
  159.                 driver_seat = i
  160.             elseif (read_bit(seat_data + i*284, 3) == 1) then -- check if gunner
  161.                 gunner_seat = i
  162.             end
  163.         else
  164.             cprint(label)
  165.             seats[i] = 0
  166.         end
  167.     end
  168.    
  169.     if (driver_seat ~= nil and seats[driver_seat] == nil) then
  170.         enter_vehicle(ObjectID, PlayerIndex, driver_seat)
  171.         return true
  172.     end
  173.    
  174.     if (gunner_seat ~= nil and seats[gunner_seat] == nil) then
  175.         enter_vehicle(ObjectID, PlayerIndex, gunner_seat)
  176.         return true
  177.     end
  178.    
  179.     for i=0,seat_count-1 do
  180.         if (seats[i] == nil) then
  181.             enter_vehicle(ObjectID, PlayerIndex, i)
  182.             return true
  183.         end
  184.     end
  185.    
  186.     return false
  187. end
  188.  
  189. function LoadDefaults()
  190.    
  191.     -- tables
  192.     delay = {}
  193.     biped_seat_labels = {} 
  194.  
  195.     -- default globals
  196.     stand_height = nil
  197.     crouch_height = nil
  198.    
  199.     -- player values
  200.     for i = 1,16 do
  201.         if player_present(i) then
  202.             delay[i] = 0
  203.         end
  204.     end
  205.  
  206.     if get_var(0, "$gt") ~= "n/a" then
  207.                
  208.         -- biped data - don't edit anything here
  209.         -- checks if unit type is biped, and sets stand & crouch heights
  210.         -- crosschecks biped_animation data against vehicle seat labels
  211.         local globals_tag = lookup_tag("matg", "globals\\globals")
  212.         local globals_data = read_dword(globals_tag + 0x14)
  213.         local mp_info_data = read_dword(globals_data + 0x168)
  214.         local player_unit_type = read_dword(mp_info_data + 0x10)
  215.         if (player_unit_type == 0x62697064) then
  216.             local biped_metaid = read_dword(mp_info_data + 0x1C)
  217.             if (biped_metaid ~= 0xFFFFFFFF) then
  218.                 local biped_tag = lookup_tag(biped_metaid)
  219.                 local biped_data = read_dword(biped_tag + 0x14)
  220.                 stand_height = read_float(biped_data + 0x400)
  221.                 crouch_height = read_float(biped_data + 0x404)
  222.                 local biped_animation_metaid = read_dword(biped_data + 0x44)
  223.                 if (biped_animation_metaid ~= 0xFFFFFFFF) then
  224.                     local animation_tag = lookup_tag(biped_animation_metaid)
  225.                     local animation_data = read_dword(animation_tag + 0x14)
  226.                     local unit_seat_count = read_dword(animation_data + 0xC)
  227.                     local unit_seat_data = read_dword(animation_data + 0x10)
  228.                     for i=0,unit_seat_count-1 do
  229.                         local label = read_string32(unit_seat_data + i*100)
  230.                         biped_seat_labels[label] = true
  231.                     end
  232.                 end
  233.             end
  234.         end
  235.     end
  236. end
  237.  
  238. function isinvehicle(PlayerIndex)
  239.     local player_object = get_dynamic_player(PlayerIndex)
  240.     if player_object ~= 0 then
  241.         local vehicleId = read_dword(player_object + 0x11C)
  242.         if vehicleId == 0xFFFFFFFF then
  243.             return false
  244.         else
  245.             return true
  246.         end
  247.     else
  248.         return false
  249.     end
  250. end
  251.  
  252. function read_string32(ReadAddress)
  253.     local str = read_string(ReadAddress)
  254.     if (str:len() > 32) then
  255.         str = str:sub(1, 32)
  256.     end
  257.     return str
  258. end
  259.  
  260. function OnScriptUnload() end
  261.  
  262. function OnError(Message)
  263.     print(debug.traceback())
  264. end
  265.  
  266. -- Created by H® Shaft and Giraffe
  267. -- Visit http://halorace.org/forum/index.php
  268.  
  269. -- The player -db-GoNe/Juhleek - a well spoken liar, cheat.
  270. -- -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