Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Spawn Vehicles for Stock PC/CE maps v1.3 for SAPP
- -- by H® Shaft
- -- ## 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)
- -- changelog:
- -- 1/4/2016: corrected code which caused duplicate timers
- -- 7/20/2016 updated isinvehicle function
- -- 7/21/2016 updated chat vehicle spawn default values in case map is reset/reloaded, added reset command
- -- This script allows players to:
- -- 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
- -- This script allows server admins to:
- -- specify a default vehicle, per map, via a table of vehicle tag names (see bottom)
- -- enable or disable, per map, player vehicle spawning via a table of map names (see bottom)
- -- set the destruction time before an abandoned player-spawned-vehicle is destroyed (not the same as "despawning")
- -- limit the number of vehicle spawns that each player can spawn per game (too many vehicles can cause lag, and clutter the game)
- -- Reset: admins may type '/reset' to reset vehicle spawn counts, and player spawn delay times to 0, then resets the map
- -- 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
- -- Does NOT affect gametype vehicle respawn times, or gametype spawned vehicles - ONLY player spawned vehicles
- vehicle_limit = 10 -- | Number of times a player can spawn a vehicle during one game (I recommend leaving this # below 10)
- 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
- destroy_time = 30 -- | Amount of time in seconds before a spawned hog is destroyed after players leave it, and if it has 'no driver'.
- admin_level = 2 -- | Admin level and higher can use the reset_command
- 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
- -- sapp api version
- api_version = "1.9.0.0"
- -- do not edit --
- vehicles_spawned = {}
- spawn_delay = {}
- vehicles = {}
- game_started = false
- delay_timer = nil
- cont_check = nil
- destroyvehicle = nil
- resettimers = nil
- function OnScriptLoad()
- register_callback(cb['EVENT_GAME_START'],"OnNewGame")
- register_callback(cb['EVENT_GAME_END'],"OnGameEnd")
- register_callback(cb['EVENT_JOIN'],"OnPlayerJoin")
- register_callback(cb['EVENT_LEAVE'],"OnPlayerLeave")
- register_callback(cb['EVENT_CHAT'], "OnPlayerChat")
- register_callback(cb['EVENT_COMMAND'],"OnCommand")
- LoadDefaults()
- if get_var(0, "$gt") ~= "n/a" then
- game_started = true
- map_name = get_var(1,"$map")
- default_vehicle[map_name] = default_vehicle[map_name] or false
- if delay_timer == nil then
- delay_timer = timer(1000, "DelayVehicleSpawn")
- end
- if cont_check == nil then
- cont_check = timer(1000, "continuous_vehicle_check")
- end
- for i = 1,16 do
- if player_present(i) then
- vehicles_spawned[i] = 0
- spawn_delay[i] = delay_time
- end
- end
- end
- end
- function OnScriptUnload()
- vehicles = {}
- vehicles_spawned = {}
- spawn_delay = {}
- default_vehicle = {}
- vehicle_spawn_enabled = {}
- end
- function OnPlayerChat(PlayerIndex, Message)
- local response = nil
- local isadmin = nil
- if (tonumber(get_var(PlayerIndex,"$lvl"))) >= admin_level then isadmin = true else isadmin = false end
- local name = get_var(PlayerIndex,"$name")
- local Message = string.lower(Message)
- if (Message == "v") or (Message == "/v") or (Message == "hog") or (Message == "/hog") then
- response = false
- if player_present(PlayerIndex) then
- if game_started then
- if player_alive(PlayerIndex) then
- if isinvehicle(PlayerIndex) == false then
- if vehicle_spawn_enabled[map_name] then
- if vehicles_spawned[PlayerIndex] == nil then vehicles_spawned[PlayerIndex] = 0 end
- if vehicles_spawned[PlayerIndex] < vehicle_limit then
- if spawn_delay[PlayerIndex] == 0 then
- if default_vehicle[map_name] ~= nil then
- local player_object = get_dynamic_player(PlayerIndex)
- local player_static = get_player(PlayerIndex)
- local x,y,z = read_vector3d(player_object + 0x5C)
- local rotation = read_float(player_static + 0x138)
- local veh_name = default_vehicle[map_name]
- local vehicleId = spawn_object("vehi", veh_name, x, y, z+0.5, rotation)
- table.insert(vehicles, {vehicleId, tonumber(destroy_time)})
- local str2 = string.format("**ERROR!** %s does not have a vehicle. Please find & specify the vehicle tag name for this map.", tostring(map_name))
- enter_vehicle(vehicleId, PlayerIndex, 0)
- vehicles_spawned[PlayerIndex] = vehicles_spawned[PlayerIndex] + 1
- say(PlayerIndex, name.. ": You have " .. vehicle_limit - vehicles_spawned[PlayerIndex] .. " vehicle calls remaining.")
- spawn_delay[PlayerIndex] = delay_time
- else
- say(PlayerIndex, "**ERROR** You cannot spawn a vehicle at this time due to an error. ")
- execute_command("log_note \""..str2.."\"")
- end
- else
- say(PlayerIndex, "**DELAY** You must wait " .. spawn_delay[PlayerIndex] .. " seconds before you can spawn a vehicle.")
- end
- else
- say(PlayerIndex, "**LIMIT** You have reached the max amount of vehicle spawns for this game. ")
- end
- else
- say(PlayerIndex, "**DISABLED** That feature is disabled for this map. " )
- end
- else
- say(PlayerIndex, "**DERP!** You already have a vehicle, duh.")
- end
- else
- say(PlayerIndex, "**DERP!** You cannot spawn a vehicle while dead, duh. ")
- end
- else
- say(PlayerIndex, "**DERP!** You cannot spawn a vehicle until the game begins, duh. ")
- end
- end
- end
- return response
- end
- function continuous_vehicle_check()
- if (game_started == true) then
- for _,id in pairs(vehicles) do
- local veh_obj = get_object_memory(id[1])
- if (veh_obj ~= 0) then
- local driver = read_dword(veh_obj + 0x324)
- if (driver == 0xFFFFFFFF) and (id[2] <= 0) then
- destroyvehicle = timer(34, "destroy_vehicle", id[1])
- elseif (driver == 0xFFFFFFFF) and (id[2] > 0) then
- id[2] = id[2] - 1
- elseif (driver ~= 0xFFFFFFFF) then
- id[2] = destroy_time
- end
- end
- end
- return true
- else
- return false
- end
- end
- function destroy_vehicle(ObjectID)
- if (game_started == true) then
- local veh_obj = get_object_memory(ObjectID)
- if (veh_obj == 0) then return false end
- table.remove(vehicles, ObjectID)
- destroy_object(ObjectID)
- return false
- else
- return false
- end
- end
- function OnPlayerJoin(PlayerIndex)
- if player_present(PlayerIndex) then
- vehicles_spawned[PlayerIndex] = vehicles_spawned[PlayerIndex] or 0
- spawn_delay[PlayerIndex] = delay_time
- end
- end
- function DelayVehicleSpawn()
- if (game_started == true) then
- for i=1,16 do
- if player_present(i) then
- if spawn_delay[i] ~= nil then
- if spawn_delay[i] > 0 then
- spawn_delay[i] = spawn_delay[i] - 1
- elseif spawn_delay[i] <= 0 then
- spawn_delay[i] = 0
- end
- elseif spawn_delay[i] == nil then
- if player_present(i) then
- spawn_delay[i] = delay_time
- end
- end
- end
- end
- return true
- else
- return false
- end
- end
- function OnPlayerLeave(PlayerIndex)
- vehicles_spawned[PlayerIndex] = nil
- spawn_delay[PlayerIndex] = nil
- end
- function OnNewGame()
- LoadDefaults()
- game_started = true
- for i = 1,16 do
- if player_present(i) then
- vehicles_spawned[i] = vehicles_spawned[i] or 0
- spawn_delay[i] = delay_time
- end
- end
- if delay_timer == nil then
- delay_timer = timer(1000, "DelayVehicleSpawn")
- end
- if cont_check == nil then
- cont_check = timer(1000, "continuous_vehicle_check")
- end
- end
- function OnGameEnd()
- game_started = false
- vehicles = {}
- vehicles_spawned = {}
- spawn_delay = {}
- default_vehicle = {}
- vehicle_spawn_enabled = {}
- delay_timer = nil
- cont_check = nil
- destroyvehicle = nil
- resettimers = nil
- end
- function PlayerIsDriver(PlayerIndex)
- if (player_present(PlayerIndex) == false) then return false end
- local player_object = get_dynamic_player(PlayerIndex)
- local player_object_id = read_dword(get_player(PlayerIndex) + 0x34)
- local vehicleId = read_dword(player_object + 0x11C)
- if (vehicleId == 0xFFFFFFFF) then return false end
- local obj_id = get_object_memory(vehicleId)
- return read_dword(obj_id + 0x324) == player_object_id
- end
- function GetplayerVehicleId(PlayerIndex)
- if (player_alive(PlayerIndex) == false) then return false end
- local player_object = get_dynamic_player(PlayerIndex)
- local vehicleId = read_dword(player_object + 0x11C)
- if (vehicleId ~= 0xFFFFFFFF) then return vehicleId else return false end
- end
- function isinvehicle(PlayerIndex) -- edited 7/20/2016
- local player_object = get_dynamic_player(PlayerIndex)
- if player_object ~= 0 then
- local vehicleId = read_dword(player_object + 0x11C)
- if vehicleId == 0xFFFFFFFF then
- return false
- else
- return true
- end
- else
- return false
- end
- end
- function OnCommand(PlayerIndex,Command,Environment,Password)
- Command = string.lower(Command)
- -- if chat command, i.e. chat: /reset
- if (Environment == 2) then
- if (Command == reset_command) then
- if (tonumber(get_var(PlayerIndex, "$lvl")) >= admin_level) then
- game_started = false
- execute_command("lua_reload spawn_vehicles")
- execute_command("sv_map_reset")
- game_started = true
- return false
- end
- end
- -- if console or rcon sv_map_reset
- elseif (Environment == 0) or (Environment == 1) then
- if (Command == "sv_map_reset") then
- game_started = false
- execute_command("lua_reload spawn_vehicles")
- game_started = true
- return true
- end
- end
- return true
- end
- function LoadDefaults()
- -- stock PC/CE vehicle tag names you can use in the table "default_vehicle" below:
- -- vehicles\\banshee\\banshee_mp
- -- vehicles\\c gun turret\\c gun turret_mp
- -- vehicles\\ghost\\ghost_mp
- -- vehicles\\rwarthog\\rwarthog
- -- vehicles\\scorpion\\scorpion_mp
- -- vehicles\\warthog\\mp_warthog
- -- default vehicle tag names for spawning for each map. Specify the default vehicle your want for each map:
- default_vehicle = {
- ["beavercreek"] = "vehicles\\warthog\\mp_warthog",
- ["bloodgulch"] = "vehicles\\rwarthog\\rwarthog",
- ["boardingaction"] = "vehicles\\rwarthog\\rwarthog",
- ["carousel"] = "vehicles\\warthog\\mp_warthog",
- ["chillout"] = "vehicles\\warthog\\mp_warthog",
- ["damnation"] = "vehicles\\warthog\\mp_warthog",
- ["dangercanyon"] = "vehicles\\rwarthog\\rwarthog",
- ["deathisland"] = "vehicles\\rwarthog\\rwarthog",
- ["gephyrophobia"] = "vehicles\\rwarthog\\rwarthog",
- ["hangemhigh"] = "vehicles\\warthog\\mp_warthog",
- ["icefields"] = "vehicles\\rwarthog\\rwarthog",
- ["infinity"] = "vehicles\\rwarthog\\rwarthog",
- ["longest"] = "vehicles\\warthog\\mp_warthog",
- ["prisoner"] = "vehicles\\warthog\\mp_warthog",
- ["putput"] = "vehicles\\warthog\\mp_warthog",
- ["ratrace"] = "vehicles\\warthog\\mp_warthog",
- ["sidewinder"] = "vehicles\\rwarthog\\rwarthog",
- ["timberland"] = "vehicles\\rwarthog\\rwarthog",
- ["wizard"] = "vehicles\\warthog\\mp_warthog"
- }
- --Enable/disable vehicle spawn: false = disable, true = enable: specify for each map if vehicle spawning is enabled
- vehicle_spawn_enabled = {
- ["beavercreek"] = false,
- ["bloodgulch"] = true,
- ["boardingaction"] = true,
- ["carousel"] = false,
- ["chillout"] = false,
- ["damnation"] = false,
- ["dangercanyon"] = true,
- ["deathisland"] = true,
- ["gephyrophobia"] = true,
- ["hangemhigh"] = false,
- ["icefields"] = true,
- ["infinity"] = true,
- ["longest"] = false,
- ["prisoner"] = false,
- ["putput"] = false,
- ["ratrace"] = false,
- ["sidewinder"] = true,
- ["timberland"] = true,
- ["wizard"] = false
- }
- map_name = get_var(1,"$map")
- default_vehicle[map_name] = default_vehicle[map_name] or false
- end
- function OnError(Message)
- print(debug.traceback())
- end
- -- Created by H® Shaft
- -- Visit http://halorace.org/forum/index.php
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement