- Update maxSpawn cap for red jet sapwn. Now presets can set max group spawn number.

- Fix bug for event handler when event.initiator is not Unit class(like some classes do not have function getPlayerName but can trigger birth/crash event).
This commit is contained in:
2025-03-05 15:40:31 +08:00
parent a2cf76bd5d
commit bb12fdf947
+299 -273
View File
@@ -3,297 +3,323 @@
]] ]]
jetSpawner = { jetSpawner = {
------------------------------------- -------------------------------------
-- 配置参数区 -- -- 配置参数区 --
------------------------------------- -------------------------------------
version = '0.0.2', version = '0.0.3',
DebugMode = true, -- 调试模式开关 DebugMode = true, -- 调试模式开关
-- 定义预设红方飞机群组 -- 定义预设红方飞机群组
jetPresets = { jetPresets = {
{ {
categoryName = 'fighters_light', -- 飞机类型预设 categoryName = 'fighters_light', -- 飞机类型预设
groups = {'Aerial-1','Aerial-2','Aerial-4'}, -- 预设的群组名, 每次刷新随机抽取 groups = {'Aerial-1','Aerial-2','Aerial-4'}, -- 预设的群组名, 每次刷新随机抽取
baseTime = 60, -- 基础刷新时间 baseTime = 60, -- 基础刷新时间
maxSpawn = 6, -- 最大数量限制, 暂未使用 maxSpawn = 6, -- 最大数量限制, 暂未使用
},
{
categoryName = 'fighters_heavy',
groups = {'Aerial-3','Aerial-5'},
baseTime = 60,
maxSpawn = 6,
},
{
categoryName = 'AWACS',
groups = {'Aerial-6','Aerial-7'},
baseTime = 90,
maxSpawn = 2,
},
}, },
{
categoryName = 'fighters_heavy', -- 根据玩家调整刷新率
groups = {'Aerial-3','Aerial-5'}, player_ratio_table = {
baseTime = 60, [3] = 4.5, -- 当玩家数小于3人刷新率为 3
maxSpawn = 6, [5] = 2.5,
[10] = 1.5,
[20] = 1,
[30] = 0.75,
[40] = 0.55,
[50] = 0.45,
[60] = 0.35,
[9999] = 0.25,
}, },
{
categoryName = 'AWACS', -- 是否仅计算已升空玩家
groups = {'Aerial-6','Aerial-7'}, only_count_airborne_players = true,
baseTime = 90,
maxSpawn = 2, RANDOM_DEVIATION = 0.5, -- 时间随机浮动比例(±50%)
}, -------------------------------------
-- 配置参数区 END --
}, -------------------------------------
-- 根据玩家调整刷新率 jetTemplate = {},
player_ratio_table = { playerList = {},
[10] = 1, -- 当玩家数小于10人刷新率为 1 allMonitors = {},
[20] = 0.8, spawnedGroups = {},
[30] = 0.5, }
[40] = 0.4,
[50] = 0.3, do
[60] = 0.2, --Tools
[9999] = 0.05, function deepCopy(object)
}, local lookup_table = {}
local function _copy(object)
-- 是否仅计算已升空玩家 if type(object) ~= "table" then
only_count_airborne_players = false, return object
elseif lookup_table[object] then
RANDOM_DEVIATION = 0.1, -- 时间随机浮动比例(±10%) return lookup_table[object]
------------------------------------- end
-- 配置参数区 END -- local new_table = {}
------------------------------------- lookup_table[object] = new_table
for index, value in pairs(object) do
jetTemplate = {}, new_table[_copy(index)] = _copy(value)
playerList = {}, end
allMonitors = {}, return setmetatable(new_table, getmetatable(object))
spawnedGroups = {}, end
} return _copy(object)
do
--Tools
function deepCopy(object)
local lookup_table = {}
local function _copy(object)
if type(object) ~= "table" then
return object
elseif lookup_table[object] then
return lookup_table[object]
end
local new_table = {}
lookup_table[object] = new_table
for index, value in pairs(object) do
new_table[_copy(index)] = _copy(value)
end
return setmetatable(new_table, getmetatable(object))
end
return _copy(object)
end
function Debug(msg,time)
if jetSpawner.DebugMode then
local time = time or 10
trigger.action.outText("[DEBUG] "..msg,time)
end end
end
function Debug(msg,time)
-- 获取红方机预设 if jetSpawner.DebugMode then
local red_jets = env.mission.coalition.red.country[1].plane.group local time = time or 10
for i, presetData in ipairs(jetSpawner.jetPresets) do trigger.action.outText("[DEBUG] "..msg,time)
jetSpawner.jetTemplate[presetData.categoryName] = jetSpawner.jetTemplate[presetData.categoryName] or {} end
for j, groupName in ipairs(presetData.groups) do end
for k, groupData in ipairs(red_jets) do
if groupData.name == groupName then -- 获取红方机预设
local tempData = deepCopy(groupData) local red_jets = env.mission.coalition.red.country[1].plane.group
tempData.lateActivation = false for i, presetData in ipairs(jetSpawner.jetPresets) do
-- 直接以群组名为键存储 jetSpawner.jetTemplate[presetData.categoryName] = jetSpawner.jetTemplate[presetData.categoryName] or {}
jetSpawner.jetTemplate[presetData.categoryName][groupName] = tempData for j, groupName in ipairs(presetData.groups) do
for k, groupData in ipairs(red_jets) do
if groupData.name == groupName then
local tempData = deepCopy(groupData)
tempData.lateActivation = false
-- 直接以群组名为键存储
jetSpawner.jetTemplate[presetData.categoryName][groupName] = tempData
end
end end
end end
end end
end
-- 更新玩家数量
-- 更新玩家数量 function jetSpawner:addplayerList(playerName,event)
function jetSpawner:addplayerList(playerName,event) if not self.playerList[playerName] then
if not self.playerList[playerName] then self.playerList[playerName] = true
self.playerList[playerName] = true end
--Debug
if not event then return end
local eventTable = {
[world.event.S_EVENT_BIRTH] = 'S_EVENT_BIRTH',
[world.event.S_EVENT_TAKEOFF] = 'S_EVENT_TAKEOFF',
}
Debug("Player number +1 by \'event "..eventTable[event.id].." handler.\' Player name: "..playerName..' current playerNum: '..jetSpawner.tableCount(self.playerList))
end end
--Debug function jetSpawner:deleteplayerList(playerName,event)
if not event then return end if self.playerList[playerName] then
local eventTable = { self.playerList[playerName] = nil
[world.event.S_EVENT_BIRTH] = 'S_EVENT_BIRTH', end
[world.event.S_EVENT_TAKEOFF] = 'S_EVENT_TAKEOFF',
} --Debug
Debug("Player number +1 by \'Birth handler.\' Player name: "..playerName..' current playerNum: '..jetSpawner.tableCount(self.playerList)) if not event then return end
Debug('Event: '..eventTable[event.id]) local eventTable = {
end [world.event.S_EVENT_CRASH] = 'S_EVENT_CRASH',
[world.event.S_EVENT_PLAYER_LEAVE_UNIT] = 'S_EVENT_PLAYER_LEAVE_UNIT',
function jetSpawner:deleteplayerList(playerName,event) }
if self.playerList[playerName] then Debug("Player number -1 by \'event "..eventTable[event.id].." handler.\' Player name: "..playerName..' current playerNum: '..jetSpawner.tableCount(self.playerList))
self.playerList[playerName] = nil
end end
--Debug function jetSpawner.tableCount(table)
if not event then return end if not table then return end
local eventTable = { if type(table) ~= 'table' then return end
[world.event.S_EVENT_CRASH] = 'S_EVENT_CRASH',
[world.event.S_EVENT_PLAYER_LEAVE_UNIT] = 'S_EVENT_PLAYER_LEAVE_UNIT', local count = 0
} for _ in pairs(table) do
Debug("Player number -1 by \'Birth handler.\' Player name: "..playerName..' current playerNum: '..jetSpawner.tableCount(self.playerList)) count = count + 1
Debug('Event: '..eventTable[event.id]) end
end return count
function jetSpawner.tableCount(table)
if type(table) ~= 'table' then return end
local count = 0
for _ in pairs(table) do
count = count + 1
end end
return count
end local eventHandler = {}
function eventHandler:onEvent(event)
local eventHandler = {} --- playerNum +1
function eventHandler:onEvent(event) -- 仅计算起飞玩家: 关
--- playerNum +1 if not jetSpawner.only_count_airborne_players then
-- 仅计算起飞玩家: 关 if event.id == world.event.S_EVENT_BIRTH then
if not jetSpawner.only_count_airborne_players then local unit = event.initiator
if event.id == world.event.S_EVENT_BIRTH then if not unit or not unit.getPlayerName then return end
local playerName = unit:getPlayerName()
if playerName then
jetSpawner:addplayerList(playerName,event)
end
end
end
-- 仅计算起飞玩家: 开
if jetSpawner.only_count_airborne_players then
if event.id == world.event.S_EVENT_TAKEOFF then
local unit = event.initiator
if not unit or not unit.getPlayerName then return end
local playerName = unit:getPlayerName()
if playerName then
jetSpawner:addplayerList(playerName,event)
end
end
end
-- playerNum -1
if event.id == world.event.S_EVENT_CRASH or event.id == world.event.S_EVENT_PLAYER_LEAVE_UNIT then
local unit = event.initiator local unit = event.initiator
if not unit then return end if not unit or not unit.getPlayerName then return end
local playerName = unit:getPlayerName() local playerName = unit:getPlayerName()
if playerName then if playerName then
jetSpawner:addplayerList(playerName,event) jetSpawner:deleteplayerList(playerName,event)
end end
end end
end end
world.addEventHandler(eventHandler)
-- 仅计算起飞玩家: 开
if jetSpawner.only_count_airborne_players then -- 为每一种预设飞机添加刷新控制器
if event.id == world.event.S_EVENT_TAKEOFF then function jetSpawner._spawnMonitor(vars,time)
local unit = event.initiator local self = vars.context
if not unit then return end local preset = vars.preset
--抽取要刷新的飞机
local playerName = unit:getPlayerName() local spawnGroupName = preset.groups[math.random(1,#preset.groups)]
if playerName then Debug('spawnGroupName: '..spawnGroupName..' categoryName: '..preset.categoryName)
jetSpawner:addplayerList(playerName,event)
local spawnGroupData = self.jetTemplate[preset.categoryName][spawnGroupName]
if not spawnGroupData then
Debug('Can not get spawnGroupData')
env.error('jetSpawner: Can not get spawnGroupData for group: '..spawnGroupName..', please check \"jetPresets\"')
return time + 100
end
-- 给新群组命名
spawnGroupData.name = nil
local newGroupName = preset.categoryName..'_'..spawnGroupData.units[1].type
if self.spawnedGroups[preset.categoryName] then
if self.spawnedGroups[preset.categoryName][newGroupName] then
for groupName,group in pairs(self.spawnedGroups[preset.categoryName][newGroupName]) do
if not group:isExist() then
spawnGroupData.name = groupName
self.spawnedGroups[preset.categoryName][newGroupName][groupName] = nil
break
end
end
end end
end end
if not spawnGroupData.name then
-- 获取群组序号
local categoryTable = self.spawnedGroups[preset.categoryName] or {}
local tempTable = categoryTable[newGroupName] or {}
local groupNameIndex = jetSpawner.tableCount(tempTable) or 0
spawnGroupData.name = newGroupName..'-'..(groupNameIndex + 1)
end
for i,unitData in ipairs(spawnGroupData.units) do
unitData.name = spawnGroupData.name..'-'..i
end
-- 判断是否到达最大数量限制
local canSpawn = true
if preset.maxSpawn and preset.maxSpawn > 0 then
local count = 0
if self.spawnedGroups[preset.categoryName] then
for groupName, group in pairs(self.spawnedGroups[preset.categoryName]) do
count = count + jetSpawner.tableCount(self.spawnedGroups[preset.categoryName][groupName])
end
end
if count >= preset.maxSpawn then
canSpawn = false
end
Debug('Category: '..preset.categoryName..' have spawned: ('..count..'/'..preset.maxSpawn..')')
end
-- 刷新飞机
if canSpawn then
local presetGroup = Group.getByName(spawnGroupName)
if presetGroup then
local category = presetGroup:getCategory()
local newGroup = coalition.addGroup(country.id.CJTF_RED,category,spawnGroupData)
if newGroup then
self.spawnedGroups[preset.categoryName] = self.spawnedGroups[preset.categoryName] or {}
self.spawnedGroups[preset.categoryName][newGroupName] = self.spawnedGroups[preset.categoryName][newGroupName] or {}
self.spawnedGroups[preset.categoryName][newGroupName][spawnGroupData.name] = newGroup
end
end
end
--设定下次刷新时间
local Ratio = 0.05
for maxPlayer,ratio in pairs(self.player_ratio_table) do
if self.tableCount(self.playerList) <= maxPlayer then
Ratio = ratio
break
end
end
local nextSpawnTime = preset.baseTime
local minTime = math.max(0, nextSpawnTime * (1 - self.RANDOM_DEVIATION))
local maxTime = nextSpawnTime * (1 + self.RANDOM_DEVIATION)
nextSpawnTime = math.random(minTime, maxTime)
Debug('playerNumber:' ..self.tableCount(self.playerList)..' ratio: '..Ratio..' Preset:'..preset.categoryName..' nextSpawnTime: '..nextSpawnTime * Ratio)
return time + nextSpawnTime * Ratio
end end
-- playerNum -1 function jetSpawner:_spawnMonitorStart(preset,delay)
if event.id == world.event.S_EVENT_CRASH or event.id == world.event.S_EVENT_PLAYER_LEAVE_UNIT then if not preset then return end
local unit = event.initiator
if not unit or not unit.getPlayerName then return end local delay = delay or preset.baseTime or 1
if delay <= 0 then
local playerName = unit:getPlayerName() delay = preset.baseTime or 1
if playerName then end
jetSpawner:deleteplayerList(playerName,event) local timerID = timer.scheduleFunction(self._spawnMonitor,{context = self, preset = preset},timer.getTime()+delay)
return timerID
end
function jetSpawner:_spawnMonitorStop(presetName)
local timerID = jetSpawner.allMonitors[presetName]
if not timerID then return end
timer.removeFunction(timerID)
jetSpawner.allMonitors[presetName] = nil
end
function jetSpawner:startAllMonitors()
for i, presetData in ipairs(self.jetPresets) do
local preset = presetData -- 局部变量避免闭包问题
self.allMonitors[preset.categoryName] = self:_spawnMonitorStart(preset)
end end
end end
jetSpawner:startAllMonitors()
Debug('jetSpawner Ver: '..jetSpawner.version)
end end
world.addEventHandler(eventHandler)
-- 为每一种预设飞机添加刷新控制器
function jetSpawner._spawnMonitor(vars,time)
local self = vars.context
local preset = vars.preset
--抽取要刷新的飞机
local spawnGroupName = preset.groups[math.random(1,#preset.groups)]
Debug('spawnGroupName: '..spawnGroupName..' categoryName: '..preset.categoryName)
local spawnGroupData = self.jetTemplate[preset.categoryName][spawnGroupName]
if not spawnGroupData then
Debug('Can not get spawnGroupData')
env.error('jetSpawner: Can not get spawnGroupData for group: '..spawnGroupName..', please check \"jetPresets\"')
return time + 100
end
-- 给新群组命名 -- Author: Buta
spawnGroupData.name = nil -- Date: 2025-03-02
local newGroupName = preset.categoryName..'_'..spawnGroupData.units[1].type
self.spawnedGroups[newGroupName] = self.spawnedGroups[newGroupName] or {}
for groupName,group in pairs(self.spawnedGroups[newGroupName]) do
if not group:isExist() then
spawnGroupData.name = groupName
self.spawnedGroups[newGroupName][groupName] = nil
break
end
end
if not spawnGroupData.name then
-- 获取群组序号
local groupNameIndex = jetSpawner.tableCount(self.spawnedGroups[newGroupName]) or 0
spawnGroupData.name = newGroupName..'-'..(groupNameIndex + 1)
end
for i,unitData in ipairs(spawnGroupData.units) do
unitData.name = spawnGroupData.name..'-'..i
end
--刷新飞机
local presetGroup = Group.getByName(spawnGroupName)
if presetGroup then
local category = presetGroup:getCategory()
local newGroup = coalition.addGroup(country.id.CJTF_RED,category,spawnGroupData)
if newGroup then
self.spawnedGroups[newGroupName] = self.spawnedGroups[newGroupName] or {}
self.spawnedGroups[newGroupName][spawnGroupData.name] = newGroup
end
end
--设定下次刷新时间
local Ratio = 0.05
for maxPlayer,ratio in pairs(self.player_ratio_table) do
if self.tableCount(self.playerList) <= maxPlayer then
Ratio = ratio
break
end
end
local nextSpawnTime = preset.baseTime
local minTime = math.max(0, nextSpawnTime * (1 - self.RANDOM_DEVIATION))
local maxTime = nextSpawnTime * (1 + self.RANDOM_DEVIATION)
nextSpawnTime = math.random(minTime, maxTime)
Debug('playerNumber:' ..self.tableCount(self.playerList)..' ratio: '..Ratio..' Preset:'..preset.categoryName..' nextSpawnTime: '..nextSpawnTime * Ratio)
return time + nextSpawnTime * Ratio
end
function jetSpawner:_spawnMonitorStart(preset,delay)
if not preset then return end
local delay = delay or preset.baseTime or 1
if delay <= 0 then
delay = preset.baseTime or 1
end
local timerID = timer.scheduleFunction(self._spawnMonitor,{context = self, preset = preset},timer.getTime()+delay)
return timerID
end
function jetSpawner:_spawnMonitorStop(presetName)
local timerID = jetSpawner.allMonitors[presetName]
if not timerID then return end
timer.removeFunction(timerID)
jetSpawner.allMonitors[presetName] = nil
end
function jetSpawner:startAllMonitors()
for i, presetData in ipairs(self.jetPresets) do
local preset = presetData -- 局部变量避免闭包问题
self.allMonitors[preset.categoryName] = self:_spawnMonitorStart(preset)
end
end
jetSpawner:startAllMonitors()
Debug('jetSpawner')
end
-- Author: Buta
-- Date: 2025-03-02