diff --git a/Apache_Training_Range_Caucasus.miz b/Apache_Training_Range_Caucasus.miz deleted file mode 100644 index a0c67d5..0000000 Binary files a/Apache_Training_Range_Caucasus.miz and /dev/null differ diff --git a/Apache_Training_Range_CaucasusVer1.2.miz b/Apache_Training_Range_CaucasusVer1.2.miz new file mode 100644 index 0000000..0043b3c Binary files /dev/null and b/Apache_Training_Range_CaucasusVer1.2.miz differ diff --git a/Saves/New callsign/Data_New callsign.csv b/Saves/New callsign/Data_New callsign.csv new file mode 100644 index 0000000..0e00d1e --- /dev/null +++ b/Saves/New callsign/Data_New callsign.csv @@ -0,0 +1,2 @@ +玩家,开始时间,持续时间,击杀敌方目标,击杀友方目标,敌方最近时的距离,结束原因 +New callsign,08:00:08,71s,1,0,4003.3米,玩家进入了靶场 \ No newline at end of file diff --git a/Scripts/Range.lua b/Scripts/Range.lua index cb7b55c..1a6ed96 100644 --- a/Scripts/Range.lua +++ b/Scripts/Range.lua @@ -1,7 +1,7 @@ Range = {} -Range.DebugMod = true +Range.DebugMod = false Range.ZoneIndex = 1 Range.MonitorRepeatTime = 5 @@ -21,14 +21,8 @@ if lfs and io then end Range.SpawnedGroups_Default = { - [coalition.side.RED] = { - static = nil, - moving = nil, - }, - [coalition.side.BLUE] = { - static = nil, - moving = nil, - }, + [coalition.side.RED] = {}, + [coalition.side.BLUE] = {}, } Range.PlayerData_Default = { startTime = nil, @@ -73,7 +67,7 @@ do return end - local obj = {} + local obj = {} local polygonPoints = Range.ZoneToPolygon(rangeZone) if not polygonPoints then return end @@ -99,35 +93,6 @@ do setmetatable(obj, self) self.__index = self - - local playerSpawnEventHandler = { - listeningZone = obj - } - function playerSpawnEventHandler:onEvent(event) - if event.id == world.event.S_EVENT_BIRTH then - local rangeObj = self.listeningZone - local object = event.initiator - if not object:isExist() then return end - - local SpawnPoint = object:getPoint() - - if not object.getPlayerName then return end - local playerName = object:getPlayerName() - if not playerName then return end - - if rangeObj:CanBindPlayer(object) then - - obj:Init(object,true) - trigger.action.outText('靶场: '..obj.zoneName..' 已分配给玩家: '..playerName,10) - - if Range.DebugMod then - Utils.Debug('Bind player: '..playerName..' to Zone: '..rangeObj.zoneName) - end - end - end - end - world.addEventHandler(playerSpawnEventHandler) - if Range.DebugMod then Utils.Debug('New Range: '..obj.zoneName) end @@ -140,8 +105,6 @@ do function Range:Init(playerUnit,notdrawZone) self.playerUnit = playerUnit - self.playerData = Range.PlayerData_Default - self.spawnedGroups = Range.SpawnedGroups_Default self.playSound = true if playerUnit then @@ -149,19 +112,36 @@ do end if not notdrawZone then + if self.assignedRangeZoneDrawID then + + if not self.playerUnit then + trigger.action.setMarkupColorFill(self.assignedRangeZoneDrawID,{0,1,0,0.2}) + trigger.action.setMarkupTypeLine(self.assignedRangeZoneDrawID,2) + end + + if self.playerUnit then + trigger.action.setMarkupColorFill(self.assignedRangeZoneDrawID,{1,0,0,0.2}) + trigger.action.setMarkupTypeLine(self.assignedRangeZoneDrawID,1) + end + end + + if self.assignedRangeZoneDrawID then + if not self.playerUnit then + trigger.action.setMarkupText(self.assignedRangeZoneTextID, '靶场: '..self.zoneName..' 当前使用: 无') + end + if self.playerUnit then + trigger.action.setMarkupText(self.assignedRangeZoneTextID, '靶场: '..self.zoneName..' 当前使用: '..self.playerName) + end + end + if not self.assignedRangeZoneDrawID then self.assignedRangeZoneDrawID = Utils.getMarkID() Utils.drawPolygon(-1,self.assignedRangeZoneDrawID,self.zonePolygon,{1,0,0,0.2},{0,1,0,0.2},2) - else - trigger.action.setMarkupColorFill(self.assignedRangeZoneDrawID,{0,1,0,0.2}) - trigger.action.setMarkupTypeLine(self.assignedRangeZoneDrawID,2) end if not self.assignedRangeZoneTextID then self.assignedRangeZoneTextID = Utils.getMarkID() trigger.action.textToAll(-1,self.assignedRangeZoneTextID,self.zonePolygon[1],{0,0,0,1},{1,1,1,0.5},12,false,'靶场: '..self.zoneName..' 当前使用: 无') - else - trigger.action.setMarkupText(self.assignedRangeZoneTextID, '靶场: '..self.zoneName..' 当前使用: 无') end end @@ -169,8 +149,10 @@ do Utils.Debug('playerData') Utils.Debug(self.playerData) end - + self.EndTraining({context = self,notShowResults = true}) + self.spawnedGroups = Utils.deepCopy(Range.SpawnedGroups_Default) + self.playerData = Utils.deepCopy(Range.PlayerData_Default) end function Range:Remove() @@ -212,19 +194,25 @@ do return polygon end - function Range:CanBindPlayer(object) + function Range.CanBindPlayer(object) if not object then return end if not object:isExist() then return end - if self.playerUnit then return false end local playerPoint = object:getPoint() - - if Range.DebugMod then - local Distance = Utils.get2DDist(playerPoint,self.center) - Utils.Debug('Distance: '..Distance) + + for zoneName,zone in pairs(Range.AllRangeZones) do + if Range.DebugMod then + local Distance = Utils.get2DDist(playerPoint,zone.center) + Utils.Debug(zoneName..' Distance: '..Distance) + end + + if Utils.get2DDist(playerPoint,zone.center) <= zone.radius+7000 then + if zone.playerUnit then return end + return zone + end end - return Utils.get2DDist(playerPoint,self.center) <= 11000 + return end function Range:CreatePlayerMenu(playerUnit) @@ -382,50 +370,43 @@ do Utils.Debug('Range:SpawnStatic, tCoalition: '..tCoalition..' spawnNumber: '..spawnNumber) end - local spawnPoint = self:GetRandSpawnPoint() - - if not spawnPoint then - spawnPoint = {x=self.center.x,y=self.center.z} - end - - local coalitionName = { - [coalition.side.RED] = 'Red', - [coalition.side.BLUE] = 'Blue', - } - - local spawnData = {} - spawnData.name = self.zoneName..'_'..coalitionName[tCoalition]..'_Static' - spawnData.task = "Ground Nothing" - spawnData.units = {} + local spawnedGroups = {} for i=1,spawnNumber,1 do - local unitSpawnPoint = Utils.getRandPointInCircle(spawnPoint,50) + local spawnPoint = self:GetRandSpawnPoint() - local unitData = {} - unitData.name = spawnData.name..'-'..i - unitData.type = self.TargetTemplates[tCoalition][math.random(1,#self.TargetTemplates[tCoalition])] - - unitData.x = unitSpawnPoint.x - unitData.y = unitSpawnPoint.y - - if i == 1 then - unitData.x = spawnPoint.x - unitData.y = spawnPoint.y + if not spawnPoint then + spawnPoint = {x=self.center.x,y=self.center.z} end - spawnData.units[i] = unitData - end + local coalitionName = { + [coalition.side.RED] = 'Red', + [coalition.side.BLUE] = 'Blue', + } - local newGroup = self:SpawnGroup(tCoalition,spawnData) - if not newGroup then return end - if not newGroup:isExist() then return end + local spawnData = {} + spawnData.name = self.zoneName..'_'..coalitionName[tCoalition]..'_Static-'..i + spawnData.task = "Ground Nothing" + spawnData.units = { + [1] = { + x = spawnPoint.x, + y = spawnPoint.y, + name = spawnData.name..'-1', + type = self.TargetTemplates[tCoalition][math.random(1,#self.TargetTemplates[tCoalition])] + }, + } - local Controller = newGroup:getController() - timer.scheduleFunction(self.setGroupGreen,Controller,timer.getTime()+1) + local newGroup = self:SpawnGroup(tCoalition,spawnData) + if not newGroup then return end + if not newGroup:isExist() then return end - self.spawnedGroups[tCoalition].static = newGroup + local Controller = newGroup:getController() + timer.scheduleFunction(self.setGroupGreen,Controller,timer.getTime()+1) - return newGroup + table.insert(self.spawnedGroups[tCoalition], newGroup) + end + + return true end function Range:SpawnMoving(tCoalition,spawnNumber) if not tCoalition or not spawnNumber then return end @@ -466,83 +447,78 @@ do -- }, } - local path = pathTable[math.random(1,#pathTable)] - - local spawnPoint = Utils.getRandPointInCircle(path.start,self.radius/2,100,path.startAngleMin+path.add,path.startAngleMin) - - if not spawnPoint then - spawnPoint = {x=self.center.x,y=self.center.z} - end - - local destinationPoint = Utils.getRandPointInCircle(path.destination,self.radius/2,100,path.destAngleMin+path.add,path.destAngleMin) - - local coalitionName = { - [coalition.side.RED] = 'Red', - [coalition.side.BLUE] = 'Blue', - } - - local spawnData = {} - spawnData.name = self.zoneName..'_'..coalitionName[tCoalition]..'_Moving' - spawnData.task = "Ground Nothing" - spawnData.units = {} - for i=1,spawnNumber,1 do - local unitSpawnPoint = Utils.getRandPointInCircle(spawnPoint,50) + local path = pathTable[math.random(1,#pathTable)] - local unitData = {} - unitData.name = spawnData.name..'-'..i - unitData.type = self.TargetTemplates[tCoalition][math.random(1,#self.TargetTemplates[tCoalition])] + local spawnPoint = Utils.getRandPointInCircle(path.start,self.radius/2,100,path.startAngleMin+path.add,path.startAngleMin) - unitData.x = unitSpawnPoint.x - unitData.y = unitSpawnPoint.y - - if i == 1 then - unitData.x = spawnPoint.x - unitData.y = spawnPoint.y + if not spawnPoint then + spawnPoint = {x=self.center.x,y=self.center.z} end + + local destinationPoint = Utils.getRandPointInCircle(path.destination,self.radius/2,100,path.destAngleMin+path.add,path.destAngleMin) + + local coalitionName = { + [coalition.side.RED] = 'Red', + [coalition.side.BLUE] = 'Blue', + } + + local spawnData = {} + spawnData.name = self.zoneName..'_'..coalitionName[tCoalition]..'_Moving-'..i + spawnData.task = "Ground Nothing" + spawnData.units = { + [1] = { + x = spawnPoint.x, + y = spawnPoint.y, + name = spawnData.name..'-1', + type = self.TargetTemplates[tCoalition][math.random(1,#self.TargetTemplates[tCoalition])] + }, + } + + local newGroup = self:SpawnGroup(tCoalition,spawnData) + if not newGroup then return end + if not newGroup:isExist() then return end + + local Controller = newGroup:getController() + timer.scheduleFunction(self.setGroupGreen,Controller,timer.getTime()+1) + + local speed = math.random(20/3.6,60/3.6) + local MovingMission = self:getTaskOnRoad(spawnPoint,destinationPoint,speed) + timer.scheduleFunction(function (vars) + local Controller = vars.Controller + local Mission = vars.Mission + if not Controller or not Mission then return end + + Controller:setTask(Mission) + end,{Controller = Controller, Mission = MovingMission},timer.getTime()+1) + + table.insert(self.spawnedGroups[tCoalition], newGroup) - spawnData.units[i] = unitData end - local newGroup = self:SpawnGroup(tCoalition,spawnData) - if not newGroup then return end - if not newGroup:isExist() then return end - - local Controller = newGroup:getController() - timer.scheduleFunction(self.setGroupGreen,Controller,timer.getTime()+1) - - local speed = math.random(5/1.944,15/1.944) - local MovingMission = self:getTaskOnRoad(spawnPoint,destinationPoint,speed) - timer.scheduleFunction(function (vars) - local Controller = vars.Controller - local Mission = vars.Mission - if not Controller or not Mission then return end - - Controller:setTask(Mission) - end,{Controller = Controller, Mission = MovingMission},timer.getTime()+1) - - self.spawnedGroups[tCoalition].moving = newGroup - - return newGroup - + return true end function Range:DespawnTargets() - self.spawnedGroups = self.spawnedGroups or Range.SpawnedGroups_Default + self.spawnedGroups = self.spawnedGroups or Utils.deepCopy(Range.SpawnedGroups_Default) if Range.DebugMod then Utils.Debug('Range.DespawnTargets') + Utils.Debug('self.spawnedGroups: ') + Utils.Debug(self.spawnedGroups) end - for coalition,types in pairs(self.spawnedGroups) do - for type,group in pairs(types) do - if group:isExist() then + for tCoalition,groups in pairs(self.spawnedGroups) do + for i,group in ipairs(groups) do + if group and group.isExist then + if group:isExist() then - if Range.DebugMod then - Utils.Debug('destroy: '..group:getName()) + if Range.DebugMod then + Utils.Debug('destroy: '..group:getName()) + end + + group:destroy() end - - group:destroy() end end end @@ -587,7 +563,7 @@ do end -- 格式化输出(秒保留两位小数) - return string.format("%s %d°%02d.%d\"", direction, degrees, minutes, seconds) + return string.format("%s %02d°%02d.%03d\"", direction, degrees, minutes, seconds) end --@param vec3 @@ -604,7 +580,8 @@ do local lat,lon,alt = coord.LOtoLL(oriPoint) local MGRSTable = coord.LLtoMGRS(lat,lon) - return MGRSTable.UTMZone .. ' ' .. MGRSTable.MGRSDigraph .. ' ' .. MGRSTable.Easting .. ' ' .. MGRSTable.Northing + -- return MGRSTable.UTMZone .. ' ' .. MGRSTable.MGRSDigraph .. ' ' .. MGRSTable.Easting .. ' ' .. MGRSTable.Northing + return string.format('%03s %s %05s %05s',MGRSTable.UTMZone,MGRSTable.MGRSDigraph,MGRSTable.Easting,MGRSTable.Northing) end if tType == 'latlon' or tType == 'LL' then @@ -622,7 +599,7 @@ do Utils.Debug('Range.SpawnTargets') end - self.playerData = Range.PlayerData_Default + self.playerData = Utils.deepCopy(Range.PlayerData_Default) local RedStaticNumber = math.random(Range.TotalStaticNum-Range.TotalBlueNum,Range.TotalStaticNum) local RedMovingNumber = Range.TotalRedNum-RedStaticNumber @@ -655,10 +632,10 @@ do msg = msg..' 训练开始, 开始时间: '..startTime..' (游戏内)\n' msg = msg..'==================================\n' msg = msg..' 靶场范围坐标:\n' - msg = msg..' 1: MGRS: '..Range.Format_Coord(self.zonePolygon[1],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[1],'LL')..'\n' - msg = msg..' 2: MGRS: '..Range.Format_Coord(self.zonePolygon[2],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[2],'LL')..'\n' - msg = msg..' 3: MGRS: '..Range.Format_Coord(self.zonePolygon[3],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[3],'LL')..'\n' - msg = msg..' 4: MGRS: '..Range.Format_Coord(self.zonePolygon[4],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[4],'LL')..'\n' + msg = msg..'1: MGRS: '..Range.Format_Coord(self.zonePolygon[1],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[1],'LL')..'\n' + msg = msg..'2: MGRS: '..Range.Format_Coord(self.zonePolygon[2],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[2],'LL')..'\n' + msg = msg..'3: MGRS: '..Range.Format_Coord(self.zonePolygon[3],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[3],'LL')..'\n' + msg = msg..'4: MGRS: '..Range.Format_Coord(self.zonePolygon[4],'MGRS')..' | '..Range.Format_Coord(self.zonePolygon[4],'LL')..'\n' trigger.action.outTextForUnit(playerUnit:getID(),msg,300,true) local playerName = playerUnit:getPlayerName() @@ -668,7 +645,7 @@ do --Sound if self.playSound then - trigger.action.outSoundForGroup(playerUnit:getGroup():getID(),'audio/SFX/success.ogg') + trigger.action.outSoundForUnit(playerUnit:getID(),'audio/SFX/success.ogg') end if Range.DebugMod then @@ -773,24 +750,27 @@ do local enemyKilled = self.playerData.enemyKilled local allyKilled = self.playerData.allyKilled local cloesestEnemyDistanceTest = (math.floor(self.playerData.cloesestEnemyDistance*10)/10)..'米' - if self.playerData.cloesestEnemyDistance > 15000 then + if self.playerData.cloesestEnemyDistance >= 15000 then cloesestEnemyDistanceTest = '非常远.' end local reason_text = '无' if reason then reason_text = reason end - local txt_file = io.open(dir_path.."/"..fildName..".txt","w") - local txt_data = { - ['玩家'] = playerName, - ['开始时间'] = startTime, - ['持续时间'] = usedTime, - ['击杀敌方目标'] = enemyKilled, - ['击杀友方目标'] = allyKilled, - ['敌方最近时的距离'] = cloesestEnemyDistanceTest, - ['结束原因'] = reason_text, - } - txt_data = net.lua2json(txt_data) + -- local txt_file = io.open(dir_path.."/"..fildName..".txt","w") + -- local txt_data = { + -- ['玩家'] = playerName, + -- ['开始时间'] = startTime, + -- ['持续时间'] = usedTime, + -- ['击杀敌方目标'] = enemyKilled, + -- ['击杀友方目标'] = allyKilled, + -- ['敌方最近时的距离'] = cloesestEnemyDistanceTest, + -- ['结束原因'] = reason_text, + -- } + -- txt_data = net.lua2json(txt_data) + + -- txt_file:write(txt_data) + -- txt_file:close() local csv_file = io.open(dir_path.."/"..fildName..".csv","w") local csv_data = "玩家,开始时间,持续时间,击杀敌方目标,击杀友方目标,敌方最近时的距离,结束原因\n" @@ -803,8 +783,6 @@ do csv_data = csv_data..cloesestEnemyDistanceTest..',' csv_data = csv_data..reason_text - -- txt_file:write(txt_data) - txt_file:close() csv_file:write(csv_data) csv_file:close() @@ -838,7 +816,7 @@ do msg = msg..'\n击杀友方目标: '..self.playerData.allyKilled local cloesestEnemyDistanceTest = (math.floor(self.playerData.cloesestEnemyDistance*10)/10)..'米' - if self.playerData.cloesestEnemyDistance > 15000 then + if self.playerData.cloesestEnemyDistance >= 15000 then cloesestEnemyDistanceTest = '非常远.' end msg = msg..'\n敌方最近时的距离: '..cloesestEnemyDistanceTest @@ -853,17 +831,193 @@ do --Sound if self.playSound then if not Error then - trigger.action.outSoundForGroup(playerUnit:getGroup():getID(),'audio/SFX/success.ogg') + trigger.action.outSoundForUnit(playerUnit:getID(),'audio/SFX/success.ogg') end if Error then - trigger.action.outSoundForGroup(playerUnit:getGroup():getID(),'audio/SFX/fail.ogg') + trigger.action.outSoundForUnit(playerUnit:getID(),'audio/SFX/fail.ogg') end end self:writeFile(reason) end + local ev = {} + function ev:onEvent(event) + if event.id == world.event.S_EVENT_BIRTH then + -- local rangeObj = self.listeningZone + local object = event.initiator + if not object:isExist() then return end + + local SpawnPoint = object:getPoint() + + if not object.getPlayerName then return end + local playerName = object:getPlayerName() + if not playerName then return end + + local rangeZone = Range.CanBindPlayer(object) + + if rangeZone then + rangeZone.playerName = playerName + + rangeZone:Init(object) + trigger.action.outText('靶场: '..rangeZone.zoneName..' 已分配给玩家: '..playerName,10) + + local msg = '' + msg = msg..'===================================\n' + msg = msg..' 靶场范围坐标:\n' + msg = msg..'===================================\n' + msg = msg..'1: MGRS: '..Range.Format_Coord(rangeZone.zonePolygon[1],'MGRS')..' | '..Range.Format_Coord(rangeZone.zonePolygon[1],'LL')..'\n' + msg = msg..'2: MGRS: '..Range.Format_Coord(rangeZone.zonePolygon[2],'MGRS')..' | '..Range.Format_Coord(rangeZone.zonePolygon[2],'LL')..'\n' + msg = msg..'3: MGRS: '..Range.Format_Coord(rangeZone.zonePolygon[3],'MGRS')..' | '..Range.Format_Coord(rangeZone.zonePolygon[3],'LL')..'\n' + msg = msg..'4: MGRS: '..Range.Format_Coord(rangeZone.zonePolygon[4],'MGRS')..' | '..Range.Format_Coord(rangeZone.zonePolygon[4],'LL')..'\n' + trigger.action.outTextForUnit(object:getID(),msg,300) + + if Range.DebugMod then + Utils.Debug('Bind player: '..playerName..' to Zone: '..rangeZone.zoneName) + end + end + end + + if event.id == world.event.S_EVENT_PLAYER_LEAVE_UNIT then + local initiator = event.initiator + if not initiator then return end + if not initiator:isExist() then return end + if not initiator.getPlayerName then return end + + local playerName = initiator:getPlayerName() + if not playerName then return end + + for zoneName,zone in pairs(Range.AllRangeZones) do + if zone.playerName then + if playerName == zone.playerName then + + zone:Init() + return + end + end + end + end + + -- if event.id == world.event.S_EVENT_KILL then + -- local initiator = event.initiator + -- if not initiator then return end + -- if not initiator:isExist() then return end + -- if not initiator.getPlayerName then return end + + -- local playerName = initiator:getPlayerName() + -- if not playerName then return end + + -- local targetUnit = event.target + -- if not targetUnit then return end + -- local tCoalition = targetUnit:getCoalition() + -- local pCoalition = initiator:getCoalition() + + -- local otherSide = { + -- [coalition.side.BLUE] = coalition.side.RED, + -- [coalition.side.RED] = coalition.side.BLUE, + -- } + + -- for zoneName,zone in pairs(Range.AllRangeZones) do + -- if zone.playerName then + -- if playerName == zone.playerName then + -- if tCoalition == otherSide[pCoalition] then + -- zone.playerData.enemyKilled = zone.playerData.enemyKilled + 1 + + -- Utils.Debug('enemyKilled: '..zone.playerData.enemyKilled) + -- if Range.DebugMod then + -- end + + -- return + -- end + -- if tCoalition == pCoalition then + -- zone.playerData.allyKilled = zone.playerData.allyKilled + 1 + + -- Utils.Debug('allyKilled: '..zone.playerData.allyKilled) + -- if Range.DebugMod then + -- end + + -- return + -- end + -- end + -- end + -- end + + -- end + end + world.addEventHandler(ev) + + local hitEvent = { + AddHandlerTimerDelay = 1, --Second + } + function hitEvent:AddEventHandler(Delay) + local tDelay = Delay or self.AddHandlerTimerDelay + + timer.scheduleFunction(function (eventHandler) + world.addEventHandler(eventHandler) + end,self,timer.getTime() + tDelay) + end + + function hitEvent:isUnitAlive(unit) + if not unit then return false end + + if not unit:isExist() then return false end + + if unit:getLife() < 1 then return false end + + return true + end + + function hitEvent:onEvent(event) + if event.id == world.event.S_EVENT_HIT then + local initiator = event.initiator + if not initiator then return end + if not initiator:isExist() then return end + if not initiator.getPlayerName then return end + + local playerName = initiator:getPlayerName() + if not playerName then return end + + local targetUnit = event.target + if not targetUnit then return end + local tCoalition = targetUnit:getCoalition() + local pCoalition = initiator:getCoalition() + + local otherSide = { + [coalition.side.BLUE] = coalition.side.RED, + [coalition.side.RED] = coalition.side.BLUE, + } + + for zoneName,zone in pairs(Range.AllRangeZones) do + if zone.playerName then + if playerName == zone.playerName then + if tCoalition == otherSide[pCoalition] then + if not self:isUnitAlive(targetUnit) then + zone.playerData.enemyKilled = zone.playerData.enemyKilled + 1 + trigger.action.outTextForUnit(initiator:getID(),'击杀敌方目标: '..targetUnit:getTypeName(),2) + + world.removeEventHandler(hitEvent) + self:AddEventHandler() + return + end + end + if tCoalition == pCoalition then + if not self:isUnitAlive(targetUnit) then + zone.playerData.allyKilled = zone.playerData.allyKilled + 1 + trigger.action.outTextForUnit(initiator:getID(),'击杀友方目标: '..targetUnit:getTypeName(),2) + + world.removeEventHandler(hitEvent) + self:AddEventHandler() + return + end + end + end + end + end + end + end + world.addEventHandler(hitEvent) + function Range._MonitorFunction(vars,time) local self = vars.context local repeatTime = vars.repeatTime @@ -912,51 +1066,32 @@ do Utils.Debug(self.spawnedGroups) end - local enemyKillCount = 0 - local allyKillCount = 0 + for tCoalition,group in pairs(self.spawnedGroups[coalition.side.RED]) do + if group and group.isExist then + if group:isExist() then + if Range.DebugMod then + Utils.Debug('group: '..group:getName()) + end - for type,group in pairs(self.spawnedGroups[coalition.side.RED]) do - if group:isExist() then - if Range.DebugMod then - Utils.Debug('group: '..group:getName()) - end - for j, unit in ipairs(group:getUnits()) do - if unit:isExist() then - if Range.DebugMod then - Utils.Debug('unit: '..unit:getName()) - end - if unit:getLife() >= 1 then - targetIsExist = true + for j, unit in ipairs(group:getUnits()) do + if unit:isExist() then + if Range.DebugMod then + Utils.Debug('unit: '..unit:getName()) + end + if unit:getLife() >= 1 then + targetIsExist = true - local targetPoint = unit:getPoint() - local distance = Utils.get3DDist(playerPoint,targetPoint) - if distance < self.playerData.cloesestEnemyDistance then - self.playerData.cloesestEnemyDistance = distance + local targetPoint = unit:getPoint() + local distance = Utils.get3DDist(playerPoint,targetPoint) + if distance < self.playerData.cloesestEnemyDistance then + self.playerData.cloesestEnemyDistance = distance + end end end - else - enemyKillCount = enemyKillCount + 1 end end end end - self.enemyKilled = enemyKillCount - - for type,group in pairs(self.spawnedGroups[coalition.side.BLUE]) do - if group:isExist() then - if Range.DebugMod then - Utils.Debug('group: '..group:getName()) - end - for j, unit in ipairs(group:getUnits()) do - if unit:isExist() then - if unit:getLife() < 1 then - allyKillCount = allyKillCount + 1 - end - end - end - end - end - self.allyKilled = allyKillCount -- 所有敌方目标被摧毁 if not targetIsExist then @@ -965,7 +1100,7 @@ do end -- 玩家飞行速度低于40节 - if playerPoint.y-land.getHeight({x=playerPoint.x,y=playerPoint.z}) > 10 then + if playerPoint.y-land.getHeight({x=playerPoint.x,y=playerPoint.z}) > 100 then local playerSpeed = Utils.mag(playerUnit:getVelocity()) if not playerSpeed then self:Init() @@ -977,7 +1112,7 @@ do if self.playerSpeedHint <= 0 then if playerSpeed <= 40/1.944 then - trigger.action.outTextForUnit(playerUnit:getID(),'============\n速度小于 40 节, 当前速度: '..(math.floor((playerSpeed/1.944)*10)/10)..'节\n============',5) + trigger.action.outTextForUnit(playerUnit:getID(),'============\n速度小于 40 节, 当前速度: '..(math.floor((playerSpeed*1.944)*10)/10)..'节\n============',5) self.playerSpeedHint = Range.playerSpeedHintRepeatTime end end diff --git a/jetSpawner.lua b/jetSpawner.lua new file mode 100644 index 0000000..407e6d5 --- /dev/null +++ b/jetSpawner.lua @@ -0,0 +1,351 @@ +--[[ + 用来根据在线玩家数量刷新红方战机的脚本 +]] +jetSpawner = { + +------------------------------------- +-- 配置参数区 -- +------------------------------------- + version = '0.0.3', + + DebugMode = false, -- 调试模式开关 + + -- 定义预设红方飞机群组 + jetPresets = { + { + categoryName = '1', -- 飞机类型预设 + groups = {'1-2','2-2','3-2','4-2','5-2','6-2','7-2','8-2','9-2','10-2','11-2','12-2','13-2','14-2','15-2','16-2'}, -- 预设的群组名, 每次刷新随机抽取 + baseTime = 909, -- 基础刷新时间 + maxSpawn = 1, -- 最大数量限制, 设置为 >0 的数视为启用, 设置为 <=0 的数或者 nil/false 视为不启用. + }, + { + categoryName = '2', + groups = {'1-3','2-3','3-3','4-3','5-3','6-3','7-3','8-3','9-3','10-3','11-3','12-3','13-3','14-3','15-3','16-3'}, + baseTime = 1824, + maxSpawn = 1, + }, + { + categoryName = '3', + groups = {'1-4','2-4','3-4','4-4','5-4','6-4','7-4','8-4','9-4','10-4','11-4','12-4','13-4','14-4','15-4','16-4'}, + baseTime = 1430, + maxSpawn = 1, + }, + { + categoryName = '4', + groups = {'1-5','2-5','3-5','4-5','5-5','6-5','7-5','8-5','9-5','10-5','11-5','12-5','13-5','14-5','15-5','16-5'}, + baseTime = 1879, + maxSpawn = 2, + }, + { + categoryName = '5', + groups = {'1-6','2-6','3-6','4-6','5-6','6-6','7-6','8-6','9-6','10-6','11-6','12-6','13-6','14-6','15-6','16-6'}, + baseTime = 2346, + maxSpawn = 1, + }, + { + categoryName = '6', + groups = {'1-7','2-7','3-7','4-7','5-7','6-7','7-7','8-7','9-7','10-7','11-7','12-7','13-7','14-7','15-7','16-7'}, + baseTime = 2878, + maxSpawn = 2, + }, + { + categoryName = '7', + groups = {'1-8','2-8','3-8','4-8','5-8','6-8','7-8','8-8','9-8','10-8','11-8','12-8','13-8','14-8','15-8','16-8'}, + baseTime = 2010, + maxSpawn = 1, + }, + }, + + -- 根据玩家调整刷新率 + player_ratio_table = { + [3] = 4.5, -- 当玩家数小于3人刷新率为 3 + [5] = 2.5, + [10] = 1.5, + [20] = 1, + [30] = 0.75, + [40] = 0.55, + [50] = 0.45, + [60] = 0.35, + [9999] = 0.25, + }, + + -- 是否仅计算已升空玩家 + only_count_airborne_players = true, + + RANDOM_DEVIATION = 0.5, -- 时间随机浮动比例(±50%) +------------------------------------- +-- 配置参数区 END -- +------------------------------------- + + jetTemplate = {}, + playerList = {}, + allMonitors = {}, + spawnedGroups = {}, +} + +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 + + -- 获取红方机预设 + local red_jets = env.mission.coalition.red.country[1].plane.group + for i, presetData in ipairs(jetSpawner.jetPresets) do + jetSpawner.jetTemplate[presetData.categoryName] = jetSpawner.jetTemplate[presetData.categoryName] or {} + 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 + + -- 更新玩家数量 + function jetSpawner:addplayerList(playerName,event) + if not self.playerList[playerName] then + 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 + + function jetSpawner:deleteplayerList(playerName,event) + if self.playerList[playerName] then + self.playerList[playerName] = nil + end + + --Debug + if not event then return end + local eventTable = { + [world.event.S_EVENT_CRASH] = 'S_EVENT_CRASH', + [world.event.S_EVENT_PLAYER_LEAVE_UNIT] = 'S_EVENT_PLAYER_LEAVE_UNIT', + } + Debug("Player number -1 by \'event "..eventTable[event.id].." handler.\' Player name: "..playerName..' current playerNum: '..jetSpawner.tableCount(self.playerList)) + end + + function jetSpawner.tableCount(table) + if not table then return end + if type(table) ~= 'table' then return end + + local count = 0 + for _ in pairs(table) do + count = count + 1 + end + return count + end + + local eventHandler = {} + function eventHandler:onEvent(event) + --- playerNum +1 + -- 仅计算起飞玩家: 关 + if not jetSpawner.only_count_airborne_players then + if event.id == world.event.S_EVENT_BIRTH 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 + + -- 仅计算起飞玩家: 开 + 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 + if not unit or not unit.getPlayerName then return end + + local playerName = unit:getPlayerName() + if playerName then + jetSpawner:deleteplayerList(playerName,event) + end + 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 + -- 给新群组命名 + 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 + + 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 + + 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 Ver: '..jetSpawner.version) +end + + + + + + + + + + + + + + + + + + + +-- Author: Buta +-- Date: 2025-03-02 \ No newline at end of file