Vehicle Spawner Mod: Add Vehicles to Your World
-- This script handles spawning vehicles at specified locations on the map. -- The script uses a table to store vehicle spawn data, including the vehicle type, location, and spawn chance. -- When a grid square is loaded, the script checks if there is any spawn data for that location and attempts to spawn a vehicle if the spawn chance is met. -- The script also includes an 'always' flag to force a vehicle spawn at a location, regardless of spawn chance. -- The script includes some pre-defined vehicle spawn locations as examples, like NW Roadblock. -- The MAX_RAND variable determines the odds of all vehicles spawning, with higher numbers resulting in less chance of spawn. -- The script also includes a debug mode that can be enabled to print messages to the console and player chat.
local print = print local getPlayer = getPlayer local Say = Say local ZombRand = ZombRand local addVehicleDebug = addVehicleDebug local tostring = tostring local SandboxVars = SandboxVars local setFallOnFront = setFallOnFront local AddWorldInventoryItem = AddWorldInventoryItem local getCell = getCell local getGridSquare = getGridSquare local createRandomDeadBody = createRandomDeadBody local table = table local format = string.format -- NOTE: we need this local ipairs = ipairs local getActivatedMods = getActivatedMods local size = size local get = get local Dir = IsoDirections -- NOTE: dont pull every direction into local space, just the enum will do.
local DEBUG_MSG = false local MAX_RAND = 100 -- this is used to dictate the odds of all of the vehicles spawning; the higher the number, the less chance they spawn
local SpawnTable = { }
-- NOTE: function adding spawns, instead of nasty manual table inserts -- this way we can define default values local function addSpecialSpawn(data) data.skin = data.skin or nil data.z = data.z or 0 data.chance = data.chance or 100 data.always = data.always or false
-- instead of a list table, use a dict table. build the key off the string x,y,z position
-- theres a number of ways of generating this string, in real lua this is fastest (kahlua needs confirmation)
local key = format('%i,%i,%i', data.x, data.y, data.z)
SpawnTable[key] = data
end
-- basic function redundancy. local function debugSay(text) if not DEBUG_MSG then return end print('DEBUG: '.. text) getPlayer():Say(text, 1.0, 1.0, 0.0, UIFont.Dialogue, 30.0, 'radio')
end
local function checkSpawn(square) if not square then return end if square:getModData().spawnVehicle then return end square:getModData().spawnVehicle = true local x, y, z = square:getX(), square:getY(), square:getZ() -- NOTE: build the key, and check the table, no need to itterate, or store mod data in the square local key = format('%i,%i,%i', x, y, z) local spawn = SpawnTable[key] if not spawn then return end
local roll = ZombRand(0, MAX_RAND)
local chance = spawn.chance
debugSay(format('Check Spawn (type: %s), roll vs chance: %s vs %s (always: %s)', spawn.type or 'nil', roll, spawn.chance, tostring(spawn.always)))
-- NOTE: reversed the check logic. why? its just cleaner. saves a indentation level
if roll > chance and (not spawn.always or not ALWAYS_OVERRIDE) then
SpawnTable[key] = nil
return
end
debugSay('Roll passed, attempting spawn..')
--local car = addVehicleDebug(spawn.type, spawn.dir, nil, getCell():getGridSquare(x, y, 0))
local car = addVehicleDebug(spawn.type, spawn.dir, spawn.skin, square)
SpawnTable[key] = nil
end
Events.LoadGridsquare.Add(checkSpawn) -- every time a grid square is loaded, checks for any vehicle spawn list entries -- TODO: change specific vehicles to categories as appropriate
--NW Roadblock -- These are an example of table entries that use the Always flag to always spawn addSpecialSpawn({type = 'fr_horse', x = 8581, y = 8512, Dir.SE, chance = 100, always = true}) addSpecialSpawn({type = 'fr_horse', x = 8579, y = 8516, dir = Dir.SE, chance = 100, always = true}) addSpecialSpawn({type = 'fr_horse', x = 8588, y = 8519, dir = Dir.SE, chance = 100, always = true}) addSpecialSpawn({type = 'fr_horse', x = 8588, y = 8504, dir = Dir.SE, chance = 100, always = true})
addSpecialSpawn({type = 'fr_horsecarriage', x = 8548, y = 8506, dir = Dir.SE, chance = 100, always = true})
原文地址: https://www.cveoy.top/t/topic/nvvH 著作权归作者所有。请勿转载和采集!