LUA代码,请写中文注释内容:-- 本地函数和变量定义 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 -- 注意:需要这个函数 local ipairs = ipairs local getActivatedMods = getActivatedMods local size = size local get = get local Dir = IsoDirections -- 注意:只需要引入枚举

-- 调试模式开关 local DEBUG_MSG = false -- 最大随机值,用于生成车辆的概率计算 local MAX_RAND = 100

-- 车辆生成表格 local SpawnTable = {}

-- 新增车辆生成函数 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 -- 生成表格键值,使用 x,y,z 三个坐标的字符串形式 local key = format('%i,%i,%i', data.x, data.y, data.z) -- 将生成数据添加到表格中 SpawnTable[key] = data end

-- 调试信息输出函数 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() -- 生成表格键值 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))) -- 如果随机值大于概率值,并且 always 标记为 false,则不生成车辆 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, spawn.skin, square) SpawnTable[key] = nil end

-- 注册检查生成函数 Events.LoadGridsquare.Add(checkSpawn)

-- TODO: 根据需要将特定车辆更改为类别

-- 添加车辆生成数据,这里是一个示例 addSpecialSpawn({type = 'fr_horse', x = 8581, y = 8512, dir = 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}) -- 马车生成数据

车辆生成系统:自动生成车辆