Advertisement
HR_Shaft

Race Seating for SAPP (compatible with ALL game types)

Feb 11th, 2017
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.37 KB | None | 0 0
  1. -- Race Seating for SAPP (compatible with ALL game types and bungholes)  
  2.  
  3. -- If player enters a vehicle gunner seat with no driver they are moved to the driver seat and notified
  4. -- If driver dies, any gunner/passenger is automatically ejected after EJECT_TIME expires - see EJECT_TIME below
  5.  
  6. -- Designed specifically to accomodate race AND mixed gametype PREFERENCES:
  7.     -- On-Entry seating priority: driver, then gunner, then passenger based on preferences and gametype
  8.     -- entry into passenger and gunner seats during (non-team) 'free-for-all' games, results in being put into driver seat
  9.     -- will eject gunners/passengers when: no driver, driver dies, ejects, leaves or based on preferences
  10.     -- option to UPGRADE_PASSENGER to better seat of vehicle, 'when entering', (if a better seat is available)
  11.     -- turn seats on/off per your preference
  12.  
  13.  
  14. -- ======= BEGIN CONFIGURATION ============
  15.  
  16. -- Amount of time in seconds before a gunner/passenger is ejected if no driver (or when driver dies/leaves).
  17. -- 3 seconds is perfect: if vehicle is in the air when driver ejects/dies/leaves, we don't want to eject gunner/passenger immediately, or they may die
  18. -- note: less than 2 seconds NOT recommended
  19. EJECT_TIME = 3
  20.  
  21. -- true or false, whether or not to allow entering specific seats
  22. -- During 'Non-Team Games' for race gametype, gunner and passenger seats are disabled automatically
  23. -- recommended: Leave these set to true, see lines 271 and 277 for race defaults
  24. ALLOW_DRIVER_SEAT = true
  25. ALLOW_GUNNER_SEAT = true
  26. ALLOW_PASSENGER_SEAT = true
  27.  
  28.  
  29. -- Require a driver before allowing players to enter other vehicle seats? true or false
  30. -- true = players can ONLY enter allowed seats if there is a driver
  31. -- false = can enter allowed seats without a driver
  32. -- set to true for mixed gametype servers (i.e. R.O.C.K.S.), including race
  33. -- see lines 284 for race seating defaults
  34. DRIVER_REQUIRED = false    
  35.  
  36.  
  37. -- true or false, whether or not to UPGRADE_PASSENGER to better seat of vehicle, 'when entering', if available, priority:
  38. -- 1) NO driver: upgrade/move player from passenger to driver seat, then
  39. -- 2) HAS DRIVER BUT NO GUNNER: upgrade/move player from passenger to gunner seat
  40. -- note: will only take effect 'if seat is enabled/allowed' if a better seat is available
  41. UPGRADE_PASSENGER = true
  42.  
  43.  
  44. -- Message given to players who attempts to enter a driverless vehicle
  45. DRIVERLESS_MESSAGE = "FIRST player to reach vehicle DRIVES!"
  46.  
  47. -- Message to players who are ejected from a driverless vehicle - anti-camp
  48. EJECT_MESSAGE = "|*EJECTED!*| You have NO DRIVER! Please DRIVE!"
  49.  
  50.  
  51. -- The command to use to "reset the map" and to reload
  52. RESET_COMMAND = "reset"
  53.  
  54. -- ======= END CONFIGURATION ============
  55.  
  56. -- don't edit below --
  57. api_version = "1.9.0.0"
  58. function OnScriptLoad()
  59.     register_callback(cb['EVENT_VEHICLE_ENTER'], "OnVehicleEnter")
  60.     register_callback(cb['EVENT_GAME_START'], "OnNewGame")
  61.     register_callback(cb['EVENT_GAME_END'], "OnGameEnd")
  62.     register_callback(cb['EVENT_TICK'], "OnTick")
  63.     register_callback(cb['EVENT_COMMAND'], "OnCommand")
  64.     players_in_vehicle = {}
  65.     team_play = getteamplay()
  66.     game_type = get_var(0,"$gt")
  67.     game_started = true    
  68.     if get_var(0, "$gt") ~= "n/a" then
  69.         game_started = true
  70.         OnNewGame()
  71.     else
  72.         game_started = false
  73.     end
  74. end
  75.  
  76. function OnScriptUnload()
  77.     players_in_vehicle = {}
  78. end
  79.  
  80. function OnNewGame()
  81.     game_started = true
  82.     SetDefaults()  
  83. end
  84.  
  85. function OnGameEnd()
  86.     game_started = false               
  87. end
  88.  
  89. function OnVehicleEnter(PlayerIndex, Seat) -- Passenger/Gunner seats: only applies when DRIVER_REQUIRED is set to false
  90.     local player_object_id = read_dword(get_player(PlayerIndex) + 0x34)
  91.     local player_object = get_dynamic_player(PlayerIndex)
  92.     local vehicleId = read_dword(player_object + 0x11C)
  93.     local vehicle_object = get_object_memory(vehicleId)
  94.    
  95.     -- gunner seat preferences     
  96.     if Seat == "2" then
  97.         if (vehicle_object ~= 0 and game_started == true) then
  98.             local driver = read_dword(vehicle_object + 0x324)
  99.             local gunner = read_dword(vehicle_object + 0x328)          
  100.             -- race preferences: put gunner into the driver seat, then set gunner seat as empty
  101.             -- may need to customize for 'gunner is driver' configuration (? don't thinks so, but maybe)
  102.             if (game_type == "race") then
  103.                 if (driver == 0xFFFFFFFF and ALLOW_DRIVER_SEAT == true) then
  104.                     enter_vehicle(vehicleId, PlayerIndex, 0)
  105.                     write_dword(vehicle_object + 0x324, player_object_id)
  106.                     write_dword(vehicle_object + 0x328, 0xFFFFFFFF)
  107.                     if team_play then
  108.                         say(PlayerIndex, get_var(PlayerIndex, "$name") .. " -- " .. DRIVERLESS_MESSAGE .. " You are now the DRIVER!")
  109.                     else
  110.                         say(PlayerIndex, get_var(PlayerIndex, "$name") .. " -- You were UPGRADED! You are now the DRIVER!")
  111.                     end
  112.                 elseif (driver ~= 0xFFFFFFFF and gunner == 0xFFFFFFFF and ALLOW_GUNNER_SEAT == true) then
  113.                     enter_vehicle(vehicleId, PlayerIndex, 2)
  114.                     write_dword(vehicle_object + 0x328, player_object_id)                  
  115.                 end            
  116.             -- non-race gametypes  
  117.             elseif (game_type ~= "race") then
  118.                 if (driver ~= 0xFFFFFFFF and gunner == 0xFFFFFFFF and ALLOW_GUNNER_SEAT == true) then
  119.                     enter_vehicle(vehicleId, PlayerIndex, 2)
  120.                     write_dword(vehicle_object + 0x328, player_object_id)                  
  121.                 end                        
  122.             end
  123.         end
  124.        
  125.     -- passenger seat preferences
  126.     -- UPGRADE_PASSENGER: put passenger into the driver or gunner seat, then set passenger seat as empty
  127.     elseif (Seat == "1" or (tonumber(Seat) > 2)) then
  128.         if (vehicle_object ~= 0 and game_started == true) then
  129.             local driver = read_dword(vehicle_object + 0x324)
  130.             local gunner = read_dword(vehicle_object + 0x328)
  131.             local passenger = read_dword(vehicle_object + 0x32C)           
  132.             if (UPGRADE_PASSENGER == true) then
  133.                 if (driver == 0xFFFFFFFF and ALLOW_DRIVER_SEAT == true and game_type == "race") then
  134.                     enter_vehicle(vehicleId, PlayerIndex, 0)
  135.                     write_dword(vehicle_object + 0x324, player_object_id)
  136.                     if team_play then
  137.                         say(PlayerIndex, get_var(PlayerIndex, "$name") .. " -- " .. DRIVERLESS_MESSAGE .. " You are now the DRIVER!")
  138.                     else
  139.                         say(PlayerIndex, get_var(PlayerIndex, "$name") .. " -- You were UPGRADED! You are now the DRIVER!")
  140.                     end
  141.                     write_dword(vehicle_object + 0x32c, 0xFFFFFFFF) -- set seat to empty   
  142.                 elseif (driver ~= 0xFFFFFFFF and gunner == 0xFFFFFFFF and ALLOW_GUNNER_SEAT == true and game_type == "race") then  
  143.                     enter_vehicle(vehicleId, PlayerIndex, 2)
  144.                     write_dword(vehicle_object + 0x328, player_object_id)
  145.                     say(PlayerIndex, get_var(PlayerIndex, "$name") .. " -- You were UPGRADED! You are now the GUNNER!")
  146.                     write_dword(vehicle_object + 0x32c, 0xFFFFFFFF) -- set seat to empty
  147.                 elseif (driver == 0xFFFFFFFF and ALLOW_DRIVER_SEAT == true and game_type ~= "race") then   
  148.                     enter_vehicle(vehicleId, PlayerIndex, 0)
  149.                     write_dword(vehicle_object + 0x328, player_object_id)
  150.                     if team_play then
  151.                         say(PlayerIndex, get_var(PlayerIndex, "$name") .. " -- " .. DRIVERLESS_MESSAGE .. " You are now the DRIVER!")
  152.                     else
  153.                         say(PlayerIndex, get_var(PlayerIndex, "$name") .. " -- You were UPGRADED! You are now the DRIVER!")
  154.                     end
  155.                     write_dword(vehicle_object + 0x32c, 0xFFFFFFFF) -- set seat to empty
  156.                 elseif (driver ~= 0xFFFFFFFF and gunner == 0xFFFFFFFF and ALLOW_GUNNER_SEAT == true and game_type ~= "race") then  
  157.                     enter_vehicle(vehicleId, PlayerIndex, 2)
  158.                     write_dword(vehicle_object + 0x328, player_object_id)
  159.                     say(PlayerIndex, get_var(PlayerIndex, "$name") .. " -- You were UPGRADED! You are now the GUNNER!")
  160.                     write_dword(vehicle_object + 0x32c, 0xFFFFFFFF) -- set seat to empty                   
  161.                 elseif (driver ~= 0xFFFFFFFF and gunner ~= 0xFFFFFFFF and passenger == 0xFFFFFFFF and ALLOW_PASSENGER_SEAT == true) then
  162.                     enter_vehicle(vehicleId, PlayerIndex, tonumber(Seat))
  163.                     write_dword(vehicle_object + 0x32c, player_object_id)                      
  164.                 end
  165.             elseif (UPGRADE_PASSENGER == false and passenger == 0xFFFFFFFF and ALLOW_PASSENGER_SEAT == true) then
  166.                 enter_vehicle(vehicleId, PlayerIndex, 1)
  167.                 write_dword(vehicle_object + 0x32c, player_object_id)          
  168.             end
  169.         end
  170.        
  171.     -- driver seat preferences 
  172.     elseif (Seat == "0" and ALLOW_DRIVER_SEAT == true) then
  173.         if (vehicle_object ~= 0 and game_started == true) then
  174.             enter_vehicle(vehicleId, PlayerIndex, 0)
  175.             write_dword(vehicle_object + 0x324, player_object_id)
  176.         end    
  177.     end
  178. end
  179.  
  180. function OnTick()-- ejects passengers and gunners if there is no driver, original by 002
  181.     local time = os.clock()
  182.     for i=1,16 do
  183.         if player_alive(i) then
  184.             local player_object = get_dynamic_player(i)
  185.             local vehicleId = read_dword(player_object + 0x11C)
  186.             local vehicle_object = get_object_memory(vehicleId)
  187.             if (vehicle_object ~= 0 and game_started == true) then
  188.                 -- gunners
  189.                 if (VehicleHasDriver(vehicleId) == false and PlayerIsGunner(i) == true) then
  190.                     if (players_in_vehicle[i] == nil) then
  191.                         players_in_vehicle[i] = time
  192.                     elseif (time > (players_in_vehicle[i] + EJECT_TIME)) then
  193.                         -- race gunners
  194.                         if (game_type == "race") then
  195.                             if (read_byte(player_object + 0x2A3) ~= 27) then
  196.                                 exit_vehicle(i)
  197.                                 say(i, get_var(i, "$name") .. ": " .. EJECT_MESSAGE)
  198.                                 players_in_vehicle[i] = nil
  199.                             end            
  200.                         -- non-race gunners, driver required
  201.                         elseif (game_type ~= "race" and DRIVER_REQUIRED == true) then
  202.                             if (read_byte(player_object + 0x2A3) ~= 27) then
  203.                                 exit_vehicle(i)
  204.                                 say(i, get_var(i, "$name") .. ": " .. EJECT_MESSAGE)
  205.                                 players_in_vehicle[i] = nil
  206.                             end                    
  207.                         end
  208.                     end
  209.                 -- passengers
  210.                 elseif (PlayerIsGunner(i) == false and VehicleHasDriver(vehicleId) == false) then
  211.                     if (players_in_vehicle[i] == nil) then
  212.                         players_in_vehicle[i] = time
  213.                     elseif (time > (players_in_vehicle[i] + EJECT_TIME)) then
  214.                         -- race passengers
  215.                         if (game_type == "race") then
  216.                             if (read_byte(player_object + 0x2A3) ~= 27) then
  217.                                 exit_vehicle(i)
  218.                                 say(i, get_var(i, "$name") .. ": " .. EJECT_MESSAGE)
  219.                                 players_in_vehicle[i] = nil
  220.                             end            
  221.                         -- non-race passengers, driver required
  222.                         elseif (game_type ~= "race" and DRIVER_REQUIRED == true) then
  223.                             if (read_byte(player_object + 0x2A3) ~= 27) then
  224.                                 exit_vehicle(i)
  225.                                 say(i, get_var(i, "$name") .. ": " .. EJECT_MESSAGE)
  226.                                 players_in_vehicle[i] = nil
  227.                             end                    
  228.                         end                                
  229.                     end        
  230.                 else
  231.                     players_in_vehicle[i] = nil
  232.                 end
  233.             end
  234.         end
  235.     end
  236. end
  237.  
  238. function OnCommand(PlayerIndex,Command,Environment,Password)
  239.     local response = true
  240.     Command = string.lower(Command)
  241.    
  242.     -- if chat command, i.e. chat: /reset
  243.     if (Environment == 2) then
  244.         if (Command == RESET_COMMAND) or (Command == "/reset") or (Command == "reset") then
  245.             if (tonumber(get_var(PlayerIndex, "$lvl")) >= 1) then
  246.                 response = true
  247.                 OnScriptUnload()               
  248.                 OnScriptLoad()
  249.                 execute_command("sv_map_reset")
  250.             else
  251.                 response = false
  252.             end
  253.         end
  254.        
  255.     -- if console or rcon  
  256.     elseif (Environment == 0) or (Environment == 1) then
  257.         if (Command == "sv_map_reset") or (Command == RESET_COMMAND) then  
  258.             response = true
  259.             OnScriptUnload()   
  260.             OnScriptLoad()
  261.             execute_command("sv_map_reset")
  262.         end        
  263.     end
  264.    
  265.     return response
  266. end
  267.  
  268. function SetDefaults()
  269.     players_in_vehicle = {}
  270.     team_play = getteamplay()
  271.     game_type = get_var(0,"$gt")
  272.     game_started = true
  273.    
  274.     if get_var(0, "$gt") ~= "n/a" then
  275.         game_started = true    
  276.     else
  277.         game_started = false
  278.     end
  279.            
  280.     -- FFA RACE: IF you use my Multi-Team-Vehicles/MTV Script, - AND = you are using this script 'with' EITHER 'TAXI' or 'DYNAMIC VEHICLE ENTRY' SCRIPTS,
  281.     -- ALLOW_GUNNER_SEAT and/or ALLLOW_PASSENGER_SEAT (your choice) must be set to true, or this script will block player vehicle entry
  282.    
  283.     -- set seat preferences
  284.     if (game_type == "race") then
  285.         ALLOW_DRIVER_SEAT = true
  286.         ALLOW_GUNNER_SEAT = true
  287.         ALLOW_PASSENGER_SEAT = true
  288.         UPGRADE_PASSENGER = true
  289.         DRIVER_REQUIRED = false
  290.     elseif (game_type ~= "race") then
  291.         ALLOW_DRIVER_SEAT = ALLOW_DRIVER_SEAT or true
  292.         ALLOW_GUNNER_SEAT = ALLOW_GUNNER_SEAT or true
  293.         ALLOW_PASSENGER_SEAT = ALLOW_PASSENGER_SEAT or true
  294.         UPGRADE_PASSENGER = UPGRADE_PASSENGER or true
  295.         DRIVER_REQUIRED = DRIVER_REQUIRED or true      
  296.     end
  297.    
  298.     --enable/disable seating according to configuration: -- created by Giraffe (Excellent work!)
  299.     for i=0,1 do
  300.         local vehicles_count = nil
  301.         local vehicles_data = nil
  302.         local size = nil
  303.        
  304.         if (i == 0) then
  305.             local globals_tag = lookup_tag("matg", "globals\\globals")
  306.             local globals_data = read_dword(globals_tag + 0x14)
  307.             local mp_info_data = read_dword(globals_data + 0x168)
  308.            
  309.             vehicles_count = read_dword(mp_info_data + 0x20)
  310.             vehicles_data = read_dword(mp_info_data + 0x24)
  311.             size = 16
  312.         else
  313.             local scenario_tag = read_dword(0x40440000)
  314.             local scenario_data = read_dword(scenario_tag + 0x14)
  315.            
  316.             vehicles_count = read_dword(scenario_data + 0x24C)
  317.             vehicles_data = read_dword(scenario_data + 0x250)
  318.             size = 48
  319.         end
  320.                
  321.         for j=0,vehicles_count-1 do
  322.             local vehicle_metaid = read_dword(vehicles_data + j*size + 0xC)
  323.             if(vehicle_metaid ~= 0xFFFFFFFF) then
  324.                 local vehicle_tag = lookup_tag(vehicle_metaid)
  325.                 local vehicle_data = read_dword(vehicle_tag + 0x14)
  326.                 local seats_count = read_dword(vehicle_data + 0x2E4)
  327.                 local seats_data = read_dword(vehicle_data + 0x2E8)
  328.                
  329.                 for k=0,seats_count-1 do
  330.                     if (read_bit(seats_data + k*284, 2) == 1) then -- if driver seat
  331.                         if (not ALLOW_DRIVER_SEAT) then
  332.                             write_string(seats_data + k*284 + 0x4, "Driver")
  333.                         end
  334.                     elseif (read_bit(seats_data + k*284, 3) == 1) then -- if gunner seat
  335.                         if (not ALLOW_GUNNER_SEAT) then
  336.                             write_string(seats_data + k*284 + 0x4, "Gunner")
  337.                         end
  338.                        
  339.                         if (DRIVER_REQUIRED) then
  340.                             write_bit(seats_data + k*284 + 0x1, 1, 1) -- enables 'not valid without driver' flag
  341.                         end
  342.                     else -- if not driver and/or gunner seat assumed to be passenger seat
  343.                         if (not ALLOW_PASSENGER_SEAT) then
  344.                             write_string(seats_data + k*284 + 0x4, "Bitch seat")
  345.                         end
  346.                        
  347.                         if (DRIVER_REQUIRED) then
  348.                             write_bit(seats_data + k*284 + 0x1, 1, 1) -- enables 'not valid without driver' flag
  349.                         end
  350.                     end
  351.                 end
  352.                
  353.             end
  354.         end
  355.     end
  356. end
  357.  
  358. function getteamplay()
  359.     if (get_var(0,"$ffa") == "0") then
  360.         return true
  361.     elseif (get_var(0,"$ffa") == "1") then
  362.         return false
  363.     end
  364. end
  365.  
  366. function VehicleHasDriver(VehicleID)
  367.     local vehicle_object = get_object_memory(VehicleID)
  368.     if (vehicle_object == 0) then return false end
  369.     return read_dword(vehicle_object + 0x324) ~= 0xFFFFFFFF
  370. end
  371.  
  372. function PlayerIsGunner(PlayerIndex)
  373.     if (player_present(PlayerIndex) == false) then return false end
  374.     local player_object = get_dynamic_player(PlayerIndex)
  375.     local pid = read_dword(get_player(PlayerIndex) + 0x34)
  376.     local vehicleId = read_dword(player_object + 0x11C)
  377.     if (vehicleId == 0xFFFFFFFF) then return false end
  378.     local obj_id = get_object_memory(vehicleId)
  379.     return read_dword(obj_id + 0x328) == pid
  380. end
  381.  
  382. function OnError(Message)
  383.     print(debug.traceback())
  384. end
  385.  
  386. -- Created by H® Shaft
  387. -- includes code from Giraffe, 002 and Skylace re-written to add RACE server configurations/preferences, thank you!
  388. -- Visit http://halorace.org
  389.  
  390. -- The player -db-GoNe/Juhleek - a well spoken liar, cheat.
  391. -- -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