daily pastebin goal
20%
SHARE
TWEET

Taxi for SAPP v1.0

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