Advertisement
HR_Shaft

Taxi 1.1 for SAPP

Jul 20th, 2016
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.31 KB | None | 0 0
  1. -- Taxi 1.1 for SAPP by H® Shaft 1/4/2016, updated 7/20/2016, edited 8/6/2016 corrected line 296
  2.  
  3. -- Allows walking players to hitch a ride in a teammates vehicle.
  4. -- To activate, just crouch! Or, type "Taxi" in the chat.
  5. -- Best used with team race gametypes.
  6.  
  7. -- do not touch --
  8. api_version = "1.9.0.0"
  9. taxi, hails, set_seat, enable_taxi, enable_passenger = {}, {}, {}, {}, {}
  10. taxi_delay = 2
  11. game_started = false
  12.  
  13. function OnScriptLoad()
  14.     register_callback(cb['EVENT_TICK'],"OnTaxiCall")
  15.     register_callback(cb['EVENT_DIE'], "OnPlayerDeath")
  16.     register_callback(cb['EVENT_JOIN'],"OnPlayerJoin")
  17.     register_callback(cb['EVENT_LEAVE'],"OnPlayerLeave")   
  18.     register_callback(cb['EVENT_GAME_START'],"OnNewGame")
  19.     register_callback(cb['EVENT_GAME_END'],"OnGameEnd")
  20.     register_callback(cb['EVENT_CHAT'], "OnPlayerChat")
  21.     LoadDefaults()
  22.     if get_var(0, "$gt") ~= "n/a" then
  23.         game_started = true
  24.         for i=1,16 do
  25.             if player_present(i) then
  26.                 hails[i], set_seat[i] = 0, {}
  27.                 local player_object = get_dynamic_player(i)
  28.                 if (player_object ~= 0) then
  29.                     if isinvehicle(i) then
  30.                         if read_byte(player_object + 0x2A0) == 0 then -- << driver
  31.                             set_seat[i].seat = 0
  32.                             set_seat[i].vehicle = read_dword(player_object + 0x11C)
  33.                         elseif read_byte(player_object + 0x2A0) == 1 then -- << passenger
  34.                             set_seat[i].seat = 1
  35.                             set_seat[i].vehicle = read_dword(player_object + 0x11C)
  36.                         elseif read_byte(player_object + 0x2A0) == 2 then -- << gunner
  37.                             set_seat[i].seat = 2
  38.                             set_seat[i].vehicle = read_dword(player_object + 0x11C)    
  39.                         end
  40.                     end
  41.                 end
  42.             end
  43.         end
  44.         map_name = get_var(1,"$map")
  45.         enable_taxi[map_name] = enable_taxi[map_name] or false
  46.         enable_passenger[map_name] = enable_passenger[map_name] or false
  47.     end
  48. end
  49.  
  50. function OnScriptUnload()
  51.     taxi, hails, set_seat, enable_taxi = {}, {}, {}, {}
  52. end
  53.  
  54. function OnPlayerJoin(PlayerIndex)
  55.     if player_present(PlayerIndex) then
  56.         hails[PlayerIndex], set_seat[PlayerIndex] = 0, {}  
  57.     end
  58. end
  59.  
  60. function OnPlayerLeave(PlayerIndex)
  61.     if player_present(PlayerIndex) then
  62.         if set_seat[PlayerIndex].vehicle then
  63.             if set_seat[PlayerIndex].seat == 1 then write_dword(set_seat[PlayerIndex].vehicle + 0x32C, 0xFFFFFFFF) end 
  64.         end
  65.         set_seat[PlayerIndex] = {}
  66.         set_seat[PlayerIndex] = nil
  67.     end
  68. end
  69.  
  70. function OnPlayerDeath(PlayerIndex, KillerIndex)
  71.     if player_present(PlayerIndex) then
  72.         if (game_started == true) then
  73.             if set_seat[PlayerIndex].seat == 1 then
  74.                 write_dword(set_seat[PlayerIndex].vehicle + 0x32C, 0xFFFFFFFF)
  75.             end
  76.             set_seat[PlayerIndex] = {}
  77.             set_seat[PlayerIndex].seat = nil
  78.         end
  79.     end
  80. end
  81.  
  82. function OnVehicleEnter(PlayerIndex, Seat)
  83.     if (game_started == true) then
  84.         if player_present(PlayerIndex) then
  85.             local player_object = get_dynamic_player(PlayerIndex)
  86.             if (player_object ~= 0) then
  87.                 local player_object_id = read_dword(get_player(PlayerIndex) + 0x34)    
  88.                 local vehicleId = read_dword(player_object + 0x11C)
  89.                 local veh_obj = get_object_memory(vehicleId)
  90.                 set_seat[PlayerIndex].seat = Seat
  91.                 set_seat[PlayerIndex].vehicle = read_dword(player_object + 0x11C)
  92.                 if Seat == "1" then
  93.                     timer(0, "assignpassenger", {player_obj_id, veh_obj})
  94.                 end
  95.             end
  96.         end    
  97.     end    
  98. end
  99.  
  100. function OnVehicleEject(PlayerIndex)
  101.     if (game_started == true) then
  102.         if player_present(PlayerIndex) then
  103.             local player_object = get_dynamic_player(PlayerIndex)
  104.             local player_object_id = read_dword(get_player(PlayerIndex) + 0x34)
  105.             if (player_object ~= 0) then   
  106.                 if set_seat[PlayerIndex].seat == 1 then resetpassenger(PlayerIndex) end
  107.                 set_seat[PlayerIndex] = {}
  108.                 set_seat[PlayerIndex].seat = nil
  109.             end
  110.         end
  111.     end
  112. end
  113.  
  114. function OnNewGame()
  115.     map_name = get_var(1,"$map")
  116.     LoadDefaults()
  117.     game_started = true
  118.     enable_taxi[map_name] = enable_taxi[map_name] or false
  119.     enable_passenger[map_name] = enable_passenger[map_name] or false
  120. end
  121.  
  122. function OnGameEnd()
  123.     game_started = false
  124. end
  125.  
  126. -- associated with a key press, not a player animation state
  127. function OnTaxiCall()
  128.     for i=1,16 do
  129.         if player_present(i) then
  130.             local player_object = get_dynamic_player(i)
  131.             if (player_object ~= 0) then
  132.                 if hails[i] > 0 then hails[i] = hails[i] - 1 end
  133.                 if not isinvehicle(i) then
  134.                     local player_crouch = bit.band(read_dword(player_object + 0x208),7)
  135.                     local id = i
  136.                     if player_crouch == 1 and taxi[id] == nil then
  137.                         taxi[id] = OnPlayerCrouch(id)
  138.                     elseif player_crouch ~= 1 and taxi[id] ~= nil then
  139.                         taxi[id] = nil
  140.                     end
  141.                 end
  142.             end
  143.         end    
  144.     end
  145. end
  146.  
  147. function OnPlayerCrouch(PlayerIndex)
  148.     if (game_started == true) then
  149.         if hails[PlayerIndex] == 0 then
  150.             getTaxi(PlayerIndex)
  151.             hails[PlayerIndex] = (taxi_delay*30)
  152.         end
  153.         return true
  154.     else
  155.         return false
  156.     end
  157. end
  158.  
  159. function OnPlayerChat(PlayerIndex, Message)
  160.     local response = nil
  161.     local name = get_var(PlayerIndex,"$name")
  162.     local Message = string.lower(Message)
  163.  
  164.     if (Message == "taxi") or (Message == "/taxi") or (Message == "taxo") or (Message == "taxci") then
  165.         response = false
  166.         if player_present(PlayerIndex) then
  167.             if (game_started == true) then
  168.                 if player_alive(PlayerIndex) then
  169.                     if (enable_taxi[map_name] == true) then
  170.                         if isinvehicle(PlayerIndex) then
  171.                             say(PlayerIndex, "**Taxi** You are already in a vehicle.")
  172.                         else   
  173.                             getTaxi(PlayerIndex)
  174.                         end
  175.                     else
  176.                         say(PlayerIndex, "**Taxi** Is not enabled for this map.")
  177.                     end
  178.                 else
  179.                     say(PlayerIndex, "**Taxi** You are dead. Try again after you respawn.")
  180.                 end
  181.             else
  182.                 say(PlayerIndex, "**Taxi** Please wait until the next map begins.")
  183.             end
  184.         end
  185.     end
  186.    
  187.     return response
  188. end
  189.  
  190. function getTaxi(PlayerIndex)
  191.     local player2
  192.     local team = get_var(PlayerIndex,"$team")
  193.     for i=0,15 do
  194.         if PlayerIndex ~= i and get_var(i,"$team") == team and isinvehicle(i) and checkSpaces(i, get_var(i,"$team"), GetplayerVehicleId(i)) > 0 then
  195.             player2 = i
  196.             break
  197.         end
  198.     end
  199.     if player2 then
  200.         InjectPlayer(PlayerIndex, GetplayerVehicleId(player2), true)
  201.     else
  202.         if (enable_taxi[map_name] == true) then
  203.             say(PlayerIndex, "**Taxi** Sorry no taxi available. Please try again later.")
  204.         end
  205.     end
  206. end
  207.  
  208. -- inserts selected player into selected vehicle
  209. function InjectPlayer(PlayerIndex, m_vehicleId, count)
  210.     if (player_alive(PlayerIndex) ~= true) then return false end
  211.     local bool = false
  212.     if player_alive(PlayerIndex) then
  213.         local name = get_var(PlayerIndex,"$name")
  214.         if m_vehicleId ~= false then
  215.             local veh_obj = get_object_memory(m_vehicleId)
  216.             local driver = read_dword(veh_obj + 0x324)
  217.             local gunner = read_dword(veh_obj + 0x328)
  218.             local passenger = read_dword(veh_obj + 0x32C)  
  219.             if driver == 0xFFFFFFFF then
  220.                 enter_vehicle(m_vehicleId, PlayerIndex, 0)
  221.                 bool = true
  222.             elseif gunner == 0xFFFFFFFF then
  223.                 enter_vehicle(m_vehicleId, PlayerIndex, 2)
  224.                 bool = true
  225.             elseif enable_passenger and (passenger == 0xFFFFFFFF or passenger  == 0) then
  226.                 enter_vehicle(m_vehicleId, PlayerIndex, 1)
  227.                 bool = true
  228.             end
  229.             if bool and count then
  230.                 say(PlayerIndex, "**Taxi** Did you call a taxi? Welcome aboard!")
  231.             end
  232.         end
  233.     end
  234. end
  235.  
  236. -- returns the number of available seats in players vehicle, requires GetVehicleSeats function
  237. function checkSpaces(PlayerIndex, m_vehicleId)
  238.     if (player_alive(PlayerIndex) == false) then return 0 end
  239.     local seats = GetVehicleSeats(PlayerIndex, m_vehicleId)
  240.     local team = get_var(PlayerIndex,"$team")
  241.     if (m_vehicleId ~= nil) then
  242.         if seats > 0  then
  243.             for i = 1,16 do
  244.                 if PlayerIndex ~= i and player_alive(i) and get_var(i,"$team") == team and isinvehicle(i) then
  245.                     if GetplayerVehicleId(i) == m_vehicleId then
  246.                         seats = seats - 1
  247.                     end
  248.                 end
  249.             end
  250.         end
  251.         return seats
  252.     end    
  253. end
  254.  
  255. -- returns number of seats in selected vehicle, sets the number of seats (some vehicles have seats not correctly setup)
  256. -- return the number of vehicle seats not including the driver
  257. function GetVehicleSeats(PlayerIndex, m_vehicleId)
  258.     if (player_alive(PlayerIndex) == false) then return false end
  259.     if m_vehicleId ~= false then
  260.         local avail_seats = 0
  261.         local veh_name = GetPlayerVehicleTagName(PlayerIndex)
  262.         if veh_name ~= nil then
  263.             if string.find(veh_name, "ghost") or string.find(veh_name, "banshee") or string.find(veh_name, "turret") then
  264.                 avail_seats = 0    
  265.             elseif string.find(veh_name, "hog") then
  266.                 avail_seats = 2
  267.             elseif string.find(veh_name, "scorpion") then
  268.                 avail_seats = 1
  269.             else
  270.                 avail_seats = 0
  271.             end
  272.             local seats = avail_seats
  273.             return tonumber(seats)
  274.         end
  275.     else
  276.         return 0
  277.     end
  278. end
  279.  
  280. -- returns vehicle tag name of players vehicle
  281. function GetPlayerVehicleTagName(PlayerIndex)
  282.     if isinvehicle(PlayerIndex) then
  283.         local player_object = get_dynamic_player(PlayerIndex)
  284.         local vehicleId = read_dword(player_object + 0x11C)
  285.         local vehicle_obj = get_object_memory(vehicleId)
  286.         local veh_name = read_string(read_dword(read_word(vehicle_obj) * 32 + 0x40440038))
  287.         if veh_name ~= nil then
  288.             return veh_name
  289.         else
  290.             return nil
  291.         end
  292.     end
  293. end
  294.  
  295. function resetpassenger(PlayerIndex)
  296.     if set_seat[PlayerIndex].seat == nil then return end
  297.     if set_seat[PlayerIndex].vehicle then
  298.         write_dword(set_seat[PlayerIndex].vehicle + 0x32C, 0xFFFFFFFF)
  299.         set_seat[PlayerIndex].seat = nil
  300.     end
  301. end
  302.  
  303. function getplayerobjectid(PlayerIndex)
  304.     local playerindex = to_player_index(PlayerIndex)
  305.     return read_dword(get_player(playerindex) + 0x34) or nil
  306. end
  307.  
  308. -- assign passenger seat player to vehicle
  309. -- overcomes defect in reliability of passenger seat reading for scripts
  310. function assignpassenger(arg)
  311.     if (game_started == true) then
  312.         set_seat[objectidtoplayer(arg[1])].vehicle = arg[2]
  313.         writedword(arg[2] + 0x32C, arg[1])
  314.     end
  315.     return false
  316. end
  317.  
  318. -- returns PlayerIndex from an ObjectID
  319. function objectidtoplayer(ObjectID)
  320.     local object = get_object_memory(ObjectID)
  321.     if object ~= 0 then
  322.     local playerId = read_word(object + 0xC0)
  323.     return to_player_index(playerId) ~= 0 or nil end
  324. end
  325.  
  326. -- returns vehicle object id player is in, or false
  327. function GetplayerVehicleId(PlayerIndex)
  328.     if (player_alive(PlayerIndex) == false) then return false end
  329.     local player_object = get_dynamic_player(PlayerIndex)
  330.     local vehicleId = read_dword(player_object + 0x11C)
  331.     if (vehicleId ~= 0xFFFFFFFF) then return vehicleId else return false end
  332. end
  333.  
  334. -- returns true if player is in a vehicle, or false if not
  335. function isinvehicle(PlayerIndex)  -- edited 7/20/2016
  336.     local player_object = get_dynamic_player(PlayerIndex)
  337.     if player_object ~= 0 then
  338.         local vehicleId = read_dword(player_object + 0x11C)
  339.         if vehicleId == 0xFFFFFFFF then
  340.             return false
  341.         else
  342.             return true
  343.         end
  344.     else
  345.         return false
  346.     end
  347. end
  348.  
  349. -- set defaults on a per map basis, entire map name should be within quotes, even when map name has
  350. -- square brackets, parenthesis, periods; example: ["[h3] core"]
  351. function LoadDefaults()
  352.     -- default enable/disable TAXI for the specified map: true = enable, false = disable
  353.     enable_taxi = {
  354.         ["beavercreek"] = false,
  355.         ["bloodgulch"] = true,
  356.         ["boardingaction"] = true,
  357.         ["carousel"] = false,
  358.         ["chillout"] = false,
  359.         ["damnation"] = false,
  360.         ["dangercanyon"] = true,
  361.         ["deathisland"] = true,
  362.         ["gephyrophobia"] = true,
  363.         ["hangemhigh"] = false,
  364.         ["icefields"] = true,
  365.         ["infinity"] = true,
  366.         ["longest"] = false,
  367.         ["prisoner"] = false,
  368.         ["putput"] = false,
  369.         ["ratrace"] = false,
  370.         ["sidewinder"] = true,
  371.         ["timberland"] = true,
  372.         ["wizard"] = false
  373.         }
  374.     -- default enable/disable taxi to PASSENGER seat for the specified map: true = enable, false = disable
  375.     -- ignored if taxi is disabled for the map
  376.     enable_passenger = {
  377.         ["beavercreek"] = false,
  378.         ["bloodgulch"] = true,
  379.         ["boardingaction"] = false,
  380.         ["carousel"] = false,
  381.         ["chillout"] = false,
  382.         ["damnation"] = false,
  383.         ["dangercanyon"] = true,
  384.         ["deathisland"] = true,
  385.         ["gephyrophobia"] = true,
  386.         ["hangemhigh"] = false,
  387.         ["icefields"] = true,
  388.         ["infinity"] = true,
  389.         ["longest"] = false,
  390.         ["prisoner"] = false,
  391.         ["putput"] = false,
  392.         ["ratrace"] = false,
  393.         ["sidewinder"] = true,
  394.         ["timberland"] = true,
  395.         ["wizard"] = false
  396.         }  
  397. end
  398.  
  399. function OnError(Message)
  400.     print(debug.traceback())
  401. end
  402.  
  403. -- Created by H® Shaft
  404. -- Visit http://halorace.org/forum/index.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement