Advertisement
HR_Shaft

Spawn Vehicles v1.3 for SAPP - updated 7/21/16

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