Advertisement
HR_Shaft

Spawn Vehicles for SAPP - PC/CE Stock Maps

Sep 15th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.75 KB | None | 0 0
  1. -- Spawn Vehicles for Stock PC/CE maps v1.0 for SAPP
  2. -- by H® Shaft
  3.  
  4. -- This script allows players to:
  5. -- type "V" to spawn the 'default' vehicle, at their location, and put them in the drivers seat, then if abandoned long enough; destroy the vehicle
  6.  
  7. -- This script allows server admins to:
  8. -- specify a default vehicle, per map, via a table of vehicle tag names (see bottom)
  9. -- enable or disable, per map, player vehicle spawning via a table of map names (see bottom)
  10. -- set the destruction time before an abandoned player-spawned-vehicle is destroyed (not the same as "despawning")
  11. -- limit the number of vehicle spawns that each player can spawn per game (too many vehicles can cause lag, and clutter the game)
  12. -- Reset: admins may type 'vreset' to reset vehicle spawn counts, and player spawn delay times to 0
  13. -- script will make an entry into sapp log each time a vehicle spawn is called if there is an error, or no default vehicle name supplied
  14. -- Does NOT affect gametype vehicle respawn times, or gametype spawned vehicles - ONLY player spawned vehicles
  15.  
  16. vehicle_limit = 8       -- | Number of times a player can spawn a vehicle during one game (I recommend leaving this # below 10)
  17. delay_time = 30         -- | Amount of time in seconds a player must wait before (re)spawning vehicles. change to zero/0 to remove delay
  18. destroy_time = 45       -- | Amount of time in seconds before a spawned hog is destroyed after players leave it, and if it has 'no driver'.
  19.  
  20. -- sapp api version
  21. api_version = "1.8.0.0"
  22. -- do not edit --
  23. --game_started = false
  24. vehicles_spawned = {}
  25. spawn_delay = {}
  26. vehicles = {}
  27.  
  28. function OnScriptLoad()
  29.     register_callback(cb['EVENT_GAME_START'],"OnNewGame")
  30.     register_callback(cb['EVENT_GAME_END'],"OnGameEnd")
  31.     register_callback(cb['EVENT_JOIN'],"OnPlayerJoin")
  32.     register_callback(cb['EVENT_LEAVE'],"OnPlayerLeave")
  33.     register_callback(cb['EVENT_CHAT'], "OnPlayerChat")    
  34.     LoadDefaults()
  35.     if get_var(0, "$gt") ~= "n/a" then
  36.         game_started = true
  37.         map_name = get_var(1,"$map")   
  38.         default_vehicle[map_name] = default_vehicle[map_name] or false
  39.         for i = 1,16 do
  40.             if player_present(i) then
  41.                 vehicles_spawned[i] = vehicles_spawned[i] or 0
  42.                 spawn_delay[i] = delay_time
  43.             end
  44.         end    
  45.     end
  46. end
  47.  
  48. function OnScriptUnload()
  49.     vehicles = {}
  50.     vehicles_spawned = {}
  51.     spawn_delay = {}
  52.     default_vehicle = {}
  53.     vehicle_spawn_enabled = {}
  54. end
  55.  
  56. function OnPlayerChat(PlayerIndex, Message)
  57.     local response = nil
  58.     local isadmin = nil
  59.     if (tonumber(get_var(PlayerIndex,"$lvl"))) > 0 then isadmin = true else isadmin = false end    
  60.     local name = get_var(PlayerIndex,"$name")
  61.     local Message = string.lower(Message)
  62.    
  63.     if (Message == "v") or (Message == "/v") then
  64.         response = false
  65.         if player_present(PlayerIndex) then
  66.             if game_started then
  67.                 if player_alive(PlayerIndex) then
  68.                     if isinvehicle(PlayerIndex) == false then
  69.                         if vehicle_spawn_enabled[map_name] then
  70.                             if vehicles_spawned[PlayerIndex] < vehicle_limit then
  71.                                 if spawn_delay[PlayerIndex] == 0 then  
  72.                                     if default_vehicle[map_name] ~= nil then
  73.                                         local player_object = get_dynamic_player(PlayerIndex)
  74.                                         local player_static = get_player(PlayerIndex)
  75.                                         local x,y,z = read_vector3d(player_object + 0x5C)
  76.                                         local rotation = read_float(player_static + 0x138)
  77.                                         local veh_name = default_vehicle[map_name]
  78.                                         local vehicleId = spawn_object("vehi", veh_name, x, y, z+0.5, rotation)
  79.                                         table.insert(vehicles, {vehicleId, tonumber(destroy_time)})
  80.                                         local str2 = string.format("**ERROR!** %s does not have a vehicle. Please find & specify the vehicle tag name for this map.", tostring(map_name))                              
  81.                                         enter_vehicle(vehicleId, PlayerIndex, 0)
  82.                                         vehicles_spawned[PlayerIndex] = vehicles_spawned[PlayerIndex] + 1
  83.                                         say(PlayerIndex, name.. ": You have " .. vehicle_limit - vehicles_spawned[PlayerIndex] .. " vehicle calls remaining.")
  84.                                         spawn_delay[PlayerIndex] = delay_time                                  
  85.                                     else
  86.                                         say(PlayerIndex, "**ERROR** You cannot spawn a vehicle at this time due to an error. ")
  87.                                         execute_command("log_note \""..str2.."\"")                                     
  88.                                     end
  89.                                 else
  90.                                     say(PlayerIndex, "**DELAY** You must wait " .. spawn_delay[PlayerIndex] .. " seconds before you can spawn a vehicle.")
  91.                                 end    
  92.                             else
  93.                                 say(PlayerIndex, "**LIMIT** You have reached the max amount of vehicle spawns for this game. ")
  94.                             end
  95.                         else
  96.                             say(PlayerIndex, "**DISABLED** That feature is disabled for this map. " )                      
  97.                         end
  98.                     else
  99.                         say(PlayerIndex, "**DERP!** You already have a vehicle, duh.")
  100.                     end
  101.                 else
  102.                     say(PlayerIndex, "**DERP!** You cannot spawn a vehicle while dead, duh. ")
  103.                 end
  104.             else
  105.                 say(PlayerIndex, "**DERP!** You cannot spawn a vehicle until the game begins, duh. ")
  106.             end
  107.         end
  108.     end
  109.    
  110.     -- allows admins to type vreset to reset player vehicle spawn counts and spawn delays to 0
  111.     if (Message == "vreset") then
  112.         response = false
  113.         if isadmin == true then
  114.             LoadDefaults()
  115.             delay_timer = nil
  116.             cont_check = nil           
  117.             delay_timer = timer(1000, "DelayVehicleSpawn")
  118.             cont_check = timer(1000, "continuous_vehicle_check")           
  119.             for PlayerIndex = 1,16 do
  120.                 if player_present(PlayerIndex) then
  121.                     vehicles_spawned[PlayerIndex] = 0
  122.                     spawn_delay[PlayerIndex] = 0
  123.                 end
  124.             end
  125.         end
  126.     end    
  127.    
  128.     return response
  129. end
  130.  
  131. function continuous_vehicle_check()
  132.     if game_started then
  133.         for _,id in pairs(vehicles) do
  134.             local veh_obj = get_object_memory(id[1])
  135.             if (veh_obj ~= 0) then
  136.                 local driver = read_dword(veh_obj + 0x324)             
  137.                 if (driver == 0xFFFFFFFF) and (id[2] <= 0) then
  138.                     timer(34, "destroy_vehicle", id[1])
  139.                 elseif (driver == 0xFFFFFFFF) and (id[2] > 0) then
  140.                     id[2] = id[2] - 1      
  141.                 elseif (driver ~= 0xFFFFFFFF) then
  142.                     id[2] = destroy_time
  143.                 end    
  144.             end
  145.         end
  146.     end
  147.     return true
  148. end
  149.                    
  150. function destroy_vehicle(ObjectID)
  151.     if game_started then
  152.         local veh_obj = get_object_memory(ObjectID)
  153.         if (veh_obj == 0) then return false end
  154.         table.remove(vehicles, ObjectID)
  155.         destroy_object(ObjectID)
  156.     end
  157.     return false
  158. end
  159.  
  160. function OnPlayerJoin(PlayerIndex)
  161.     if player_present(PlayerIndex) then
  162.         vehicles_spawned[PlayerIndex] = vehicles_spawned[PlayerIndex] or 0
  163.         spawn_delay[PlayerIndex] = delay_time
  164.     end
  165. end
  166.  
  167. function DelayVehicleSpawn()
  168.     for i=1,16 do
  169.         if player_present(i) then
  170.             if spawn_delay[i] ~= nil then
  171.                 if spawn_delay[i] > 0 then
  172.                     spawn_delay[i] = spawn_delay[i] - 1
  173.                 elseif spawn_delay[i] <= 0 then
  174.                     spawn_delay[i] = 0
  175.                 end
  176.             elseif spawn_delay[i] == nil then
  177.                 if player_present(i) then
  178.                     spawn_delay[i] = delay_time
  179.                 end
  180.             end
  181.         end
  182.     end
  183.     return true
  184. end
  185.  
  186. function OnPlayerLeave(PlayerIndex)
  187.     if player_present(PlayerIndex) then
  188.         vehicles_spawned[PlayerIndex] = {}
  189.         spawn_delay[PlayerIndex] = {}      
  190.     end
  191. end
  192.  
  193. function OnNewGame()
  194.     LoadDefaults() 
  195.     game_started = true
  196.     for i = 1,16 do
  197.         if player_present(i) then
  198.             vehicles_spawned[i] = vehicles_spawned[i] or 0
  199.             spawn_delay[i] = delay_time
  200.         end
  201.     end
  202.     delay_timer = timer(1000, "DelayVehicleSpawn")
  203.     cont_check = timer(1000, "continuous_vehicle_check")   
  204. end
  205.  
  206. function OnGameEnd()
  207.     game_started = false
  208.     vehicles = {}
  209.     vehicles_spawned = {}
  210.     spawn_delay = {}
  211.     default_vehicle = {}
  212.     vehicle_spawn_enabled = {} 
  213. end
  214.  
  215. function PlayerIsDriver(PlayerIndex)
  216.     if (player_present(PlayerIndex) == false) then return false end
  217.     local player_object = get_dynamic_player(PlayerIndex)
  218.     local player_object_id = read_dword(get_player(PlayerIndex) + 0x34)
  219.     local vehicleId = read_dword(player_object + 0x11C)
  220.     if (vehicleId == 0xFFFFFFFF) then return false end
  221.     local obj_id = get_object_memory(vehicleId)
  222.     return read_dword(obj_id + 0x324) == player_object_id
  223. end
  224.  
  225. function GetplayerVehicleId(PlayerIndex)
  226.     if (player_alive(PlayerIndex) == false) then return false end
  227.     local player_object = get_dynamic_player(PlayerIndex)
  228.     local vehicleId = read_dword(player_object + 0x11C)
  229.     if (vehicleId ~= 0xFFFFFFFF) then return vehicleId else return false end
  230. end
  231.  
  232. function isinvehicle(PlayerIndex)  
  233.     local player_object = get_dynamic_player(PlayerIndex)
  234.     local vehicleId = read_dword(player_object + 0x11C)
  235.     if vehicleId == 0xFFFFFFFF then
  236.         return false
  237.     else
  238.         return true
  239.     end
  240. end
  241.  
  242. function get_tag_info(obj_type, obj_name)
  243.     local tag_id = lookup_tag(obj_type, obj_name)
  244.     return tag_id ~= 0 and read_dword(tag_id + 0xC) or nil
  245. end
  246.  
  247. function LoadDefaults()
  248.    
  249.     -- stock PC/CE vehicle tag names you can use in the table "default_vehicle" below: 
  250.     -- vehicles\\banshee\\banshee_mp
  251.     -- vehicles\\c gun turret\\c gun turret_mp
  252.     -- vehicles\\ghost\\ghost_mp
  253.     -- vehicles\\rwarthog\\rwarthog
  254.     -- vehicles\\scorpion\\scorpion_mp
  255.     -- vehicles\\warthog\\mp_warthog
  256.    
  257.     -- default vehicle tag names for spawning for each map. Specify the default vehicle your want for each map:
  258.     default_vehicle = {
  259.     ["beavercreek"] = "vehicles\\warthog\\mp_warthog",
  260.     ["bloodgulch"] = "vehicles\\warthog\\mp_warthog",
  261.     ["boardingaction"] = "vehicles\\warthog\\mp_warthog",
  262.     ["carousel"] = "vehicles\\warthog\\mp_warthog",
  263.     ["chillout"] = "vehicles\\warthog\\mp_warthog",
  264.     ["damnation"] = "vehicles\\warthog\\mp_warthog",
  265.     ["dangercanyon"] = "vehicles\\warthog\\mp_warthog",
  266.     ["deathisland"] = "vehicles\\warthog\\mp_warthog",
  267.     ["gephyrophobia"] = "vehicles\\warthog\\mp_warthog",
  268.     ["hangemhigh"] = "vehicles\\warthog\\mp_warthog",
  269.     ["icefields"] = "vehicles\\warthog\\mp_warthog",
  270.     ["infinity"] = "vehicles\\warthog\\mp_warthog",
  271.     ["longest"] = "vehicles\\warthog\\mp_warthog",
  272.     ["prisoner"] = "vehicles\\warthog\\mp_warthog",
  273.     ["putput"] = "vehicles\\warthog\\mp_warthog",
  274.     ["ratrace"] = "vehicles\\warthog\\mp_warthog",
  275.     ["sidewinder"] = "vehicles\\warthog\\mp_warthog",
  276.     ["timberland"] = "vehicles\\warthog\\mp_warthog",
  277.     ["wizard"] = "vehicles\\warthog\\mp_warthog",
  278.     }
  279.    
  280.     --Enable/disable vehicle spawn: false = disable, true = enable: specify for each map if vehicle spawning is enabled
  281.     vehicle_spawn_enabled = {
  282.     ["beavercreek"] = false,
  283.     ["bloodgulch"] = true,
  284.     ["boardingaction"] = false,
  285.     ["carousel"] = false,
  286.     ["chillout"] = false,
  287.     ["damnation"] = false,
  288.     ["dangercanyon"] = true,
  289.     ["deathisland"] = true,
  290.     ["gephyrophobia"] = true,
  291.     ["hangemhigh"] = false,
  292.     ["icefields"] = true,
  293.     ["infinity"] = true,
  294.     ["longest"] = false,
  295.     ["prisoner"] = false,
  296.     ["putput"] = false,
  297.     ["ratrace"] = false,
  298.     ["sidewinder"] = true,
  299.     ["timberland"] = true,
  300.     ["wizard"] = false,
  301.     }
  302.    
  303.     map_name = get_var(1,"$map")   
  304.     default_vehicle[map_name] = default_vehicle[map_name] or false 
  305. end
  306.  
  307. function OnError(Message)
  308.     print(debug.traceback())
  309. end
  310.    
  311. -- Created by H® Shaft
  312. -- Visit http://halorace.org/forum/index.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement