Advertisement
HR_Shaft

Spawn Vehicles for Stock PC/CE maps v1.1

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