Files
Buta-018-EagleHunt/Scripts/misc.lua
T
XiaoChen c55b615554 - fix bugs for event listener
- change redalarm triggered by random(seems not work as expected)
2025-04-10 21:13:11 +08:00

582 lines
21 KiB
Lua

-- hanle miscellaneous mission events
--Red alarm when under attack
local redAlarmEvent = {
DebugMode = false,
redAlarm = false,
ignoreList = {
['Red-RescueInf-1'] = true,
['Red-Scud'] = true,
},
alarmOffGroups = {
['Red-PowerStation-1'] = true,
},
}
do
function redAlarmEvent:redAlarm_On()
if self.redAlarm then return end
Utils.Debug('==================\n\n敌人因为受到攻击, 拉响了警报!\n\n==================')
self.redAlarm = true
local redGrounds = coalition.getGroups(1,Group.Category.GROUND)
for i, group in ipairs(redGrounds) do
local groupIsActivate = true
for i,unit in ipairs(group:getUnits()) do
if not unit:isActive() then
groupIsActivate = false
end
end
if not groupIsActivate and not redAlarmEvent.ignoreList[group:getName()] then
if redAlarmEvent.DebugMode then
Utils.Debug('Activate Group: '..group:getName())
end
timer.scheduleFunction(function (group)
group:activate()
end,group,timer.getTime()+math.random(120,600))
end
end
end
function redAlarmEvent:onEvent(event)
if not self.redAlarm then
if event.id == world.event.S_EVENT_KILL then
local object = event.target
if not object then return end
if not object:isExist() then return end
if not object.getCoalition then return end
if object:getCoalition() ~= coalition.side.RED then return end
for groupName, value in pairs(redAlarmEvent.alarmOffGroups) do
if redAlarmEvent.DebugMode then
Utils.Debug('redAlarmEvent - Trigger Group: '..object:getName())
Utils.Debug('redAlarmEvent - groupName: '..groupName)
end
if object:getName() == groupName then
return
end
end
local objectCategory, unitCategory = Object.getCategory(object)
if objectCategory == Object.Category.UNIT then
if unitCategory == Unit.Category.GROUND_UNIT or unitCategory == Unit.Category.STRUCTURE then
if math.random(1,10) <= 4 then
self:redAlarm_On()
world.removeEventHandler(redAlarmEvent)
end
end
end
if objectCategory == Object.Category.STATIC then
if math.random(1,10) == 1 then
self:redAlarm_On()
world.removeEventHandler(redAlarmEvent)
end
end
end
end
end
world.addEventHandler(redAlarmEvent)
end
-- Power Station
do
local PowerStationEvent = {
connectedGroupList = {
'Red-EWR-CRAM-1',
'Red-CWStorage-CRAM-1',
'Red-SAMSite-CRAM-1',
'Red-EWR-1',
'Red-Rescue-SAM-1',
},
replaceStatics = {
['Static M92 Container watchtower lights-1'] = {
ori = 'Container_watchtower_lights',
replace = 'Container_watchtower',
},
['Static M92 Container watchtower lights-2-1'] = {
ori = 'Container_watchtower_lights',
replace = 'Container_watchtower',
},
['Static M92 Container watchtower lights-3-1'] = {
ori = 'Container_watchtower_lights',
replace = 'Container_watchtower',
},
['Static M92 Container watchtower lights-4-1'] = {
ori = 'Container_watchtower_lights',
replace = 'Container_watchtower',
},
['Static M92 Container watchtower lights-5-1'] = {
ori = 'Container_watchtower_lights',
replace = 'Container_watchtower',
},
['静态 M92 集装箱瞭望塔(有灯光)-1-1'] = {
ori = 'Container_watchtower_lights',
replace = 'Container_watchtower',
},
['静态 M92 集装箱瞭望塔(有灯光)-2-1'] = {
ori = 'Container_watchtower_lights',
replace = 'Container_watchtower',
},
['Static M92 NF-2 LightOn-1-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['Static M92 NF-2_LightOn-2-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['静态 M92 NF-2 泛光灯组(打开)-1-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['静态 M92 NF-2 泛光灯组(打开)-2-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['静态 M92 NF-2 泛光灯组(打开)-3-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['静态 M92 NF-2 泛光灯组(打开)-4-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['静态 M92 NF-2 泛光灯组(打开)-5-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['静态 M92 NF-2 泛光灯组(打开)-6-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['静态 M92 NF-2 泛光灯组(打开)-7-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
['静态 M92 NF-2 泛光灯组(打开)-8-1'] = {
ori = 'NF-2_LightOn',
replace = 'NF-2_LightOff01',
},
},
newStaticObjs = {},
}
function PowerStationEvent:onEvent(event)
if event.id == world.event.S_EVENT_DEAD then
local object = event.initiator
if not object.getName then return end
if object:getName() == 'Red-PowerStation-1' then
-- 关闭防空
for i, groupName in ipairs(PowerStationEvent.connectedGroupList) do
local group = Group.getByName(groupName)
if group then
if group:isExist() and group:getUnits() then
local groupIsAlive = false
for i, unit in ipairs(group:getUnits()) do
if unit:getLife() >= 1 then
groupIsAlive= true
end
end
if not groupIsAlive then
group = nil
end
end
end
--关闭
timer.scheduleFunction(function (group)
if not group then return end
if not group:isExist() then return end
trigger.action.setGroupAIOff(group)
end,group,timer.getTime()+math.random(1,10))
--开启
timer.scheduleFunction(function (group)
if not group then return end
if not group:isExist() then return end
trigger.action.setGroupAIOn(group)
end,group,timer.getTime()+math.random(180,900))
end
-- 关闭静态物体灯光
local objectIndex = 1
for groupName, staticData in pairs(PowerStationEvent.replaceStatics) do
local staticLight = StaticObject.getByName(groupName)
if staticLight then
local staticPoint = staticLight:getPoint()
local staticType_Ori = staticLight:getTypeName()
local staticType_Replace = PowerStationEvent.replaceStatics[groupName].replace
-- local staticeHeading = math.deg(Utils.getObjHeading(staticLight))
local staticeHeading = Utils.getObjHeading(staticLight)
local countryId = staticLight:getCountry()
--- 记录初始状态
local staticData = {}
-- 记录位置
staticData.name = 'lightsOff-'..objectIndex
objectIndex = objectIndex + 1
staticData.type = staticType_Replace
staticData.x = staticPoint.x
staticData.y = staticPoint.z
-- 记录方向
staticData.heading = staticeHeading
--关闭
timer.scheduleFunction(function (args)
-- 删除旧物体
args.staticLight:destroy()
-- 刷新新物体
timer.scheduleFunction(function (vars)
local newStaticObj = coalition.addStaticObject(vars.countryId,vars.staticData)
if newStaticObj then
PowerStationEvent.replaceStatics[vars.groupName].newObj = newStaticObj
end
end,{groupName = args.groupName,staticData = args.staticData,countryId = args.countryId},timer.getTime()+0.1)
end,{groupName = groupName,staticLight = staticLight,staticData = staticData,countryId = countryId},timer.getTime()+math.random(3,10)*0.1)
--开启
timer.scheduleFunction(function (args)
-- 删除旧物体
PowerStationEvent.replaceStatics[groupName].newObj:destroy()
-- 刷新新物体.
timer.scheduleFunction(function (vars)
vars.staticData.name = vars.groupName
vars.staticData.type = PowerStationEvent.replaceStatics[vars.groupName].ori
local newStaticObj = coalition.addStaticObject(vars.countryId,vars.staticData)
end,{groupName = args.groupName,staticData = args.staticData,countryId = args.countryId},timer.getTime()+0.1)
end,{groupName = groupName,staticData = staticData,countryId = countryId},timer.getTime()+math.random(180,900))
end
end
world.removeEventHandler(PowerStationEvent)
end
end
end
world.addEventHandler(PowerStationEvent)
end
-- Mines
do
Mines = {}
Mines.DebugMod = false
Mines.DetectionRange = 1500
Mines.FireRange = 300
Mines.TriggerExplode = 0.5 --kg
Mines.Warhead_AH = 3 --kg
Mines.Warhead_AG = 6 --kg
Mines.MonitorStartDelay = 1
Mines.MonitorRepeatTime = 30
Mines.ActivatedMonitorRepeatTime = 2
Mines.MineFieldZonePolygon = Utils.getWayPoints('mineZoneGroup-1')
if Utils.getTableSize(Mines.MineFieldZonePolygon) < 3 then
if Mines.DebugMod then
Utils.Debug('Misc.lua - Mines: MineFieldZonePolygon has not enough waypoints.')
end
return
end
-- DrawMineZone
Utils.drawPolygon(-1,Utils.getMarkID(),Mines.MineFieldZonePolygon,{0,0,0,0.1},{0,0,0,0.1},2)
Mines.AllMines = {}
function Mines:New(staticObject)
if not staticObject then return end
local obj = {}
obj.staticObject = staticObject
obj.staticObjectName = staticObject:getName()
obj.point = staticObject:getPoint()
if Mines.DebugMod then
trigger.action.circleToAll(-1,Utils.getMarkID(),obj.point,Mines.DetectionRange,{1,0,0,1},{1,0,0,0.3},2)
trigger.action.circleToAll(-1,Utils.getMarkID(),obj.point,Mines.FireRange,{1,0,0,1},{1,0,0,0.5},1)
end
setmetatable(obj, self)
self.__index = self
Mines.AllMines[obj.staticObjectName] = obj
obj:Start()
return obj
end
function Mines:Remove(removeStatic)
if not Mines.AllMines[self.staticObjectName] then return end
self:ActivatedMonitorStop()
self:Stop()
if removeStatic then
if self.staticObject then
if self.staticObject:isExist() then
self.staticObject:destroy()
end
end
end
Mines.AllMines[self.staticObjectName] = nil
end
function Mines:TriggerActive(targetPoint,targetSpeed,targetHeading)
if not targetPoint then return end
if type(targetPoint) ~= 'table' then return end
if not self.staticObject:isExist() then
self:Remove()
return
end
if self.staticObject:getLife() < 1 then
self:Remove()
return
end
-- trigger explode
trigger.action.explosion(self.point,Mines.TriggerExplode)
self:Remove(true)
-- Warhead_AH explode
local shellTravelTime = math.random(3,5)*0.1
local predictedExplodePoint = Utils.vecTranslate(targetPoint,targetHeading,(targetSpeed*shellTravelTime))
predictedExplodePoint.z = predictedExplodePoint.y
predictedExplodePoint.y = targetPoint.y
timer.scheduleFunction(function (predictedExplodePoint)
local explodePoint = Utils.deepCopy(predictedExplodePoint)
-- x
if math.random(1,10) >= 5 then
explodePoint.x = explodePoint.x + math.random(25,80)*0.1
else
explodePoint.x = explodePoint.x - math.random(25,80)*0.1
end
-- z
if math.random(1,10) >= 5 then
explodePoint.z = explodePoint.z + math.random(25,80)*0.1
else
explodePoint.z = explodePoint.z - math.random(25,80)*0.1
end
-- y
if math.random(1,10) >= 5 then
explodePoint.y = explodePoint.y + math.random(0,50)*0.1
else
explodePoint.y = explodePoint.y - math.random(0,50)*0.1
end
trigger.action.explosion(explodePoint,Mines.Warhead_AH)
end,predictedExplodePoint,timer.getTime()+shellTravelTime)
end
function Mines._ActivatedMonitorFunction(vars,time)
local self = vars.context
local repeatTime = vars.repeatTime
if Mines.DebugMod then
Utils.Debug('_ActivatedMonitorFunction.')
end
local target = vars.target
if not target then return end
if not target:isExist() then return end
if target:getLife() < 1 then return end
local targetPoint = target:getPoint()
local Distance = Utils.get3DDist(targetPoint,self.point)
if Mines.DebugMod then
Utils.Debug('Distance: '..Distance)
end
if Distance > Mines.DetectionRange then
if Mines.DebugMod then
Utils.Debug('Enemy unit out mine DetectionRange.')
end
self:ActivatedMonitorStop()
self:Start(1)
return
end
if Distance <= Mines.FireRange then
local chance = math.random(1,10)
if chance >= 5 then
local targetSpeed = Utils.mag(target:getVelocity())
local targetHeading = math.deg(Utils.getObjHeading(target))
trigger.action.outText(target:getName()..'触发了防空地雷!',10)
self:TriggerActive(targetPoint,targetSpeed,targetHeading)
return
end
end
return time + repeatTime
end
function Mines:ActivatedMonitorStart(target,Delay,RepeatScanSeconds)
if not target then return end
Mines.MonitorStartDelay = Delay or Mines.MonitorStartDelay
local tDelay = Delay or Mines.MonitorStartDelay
local RepeatScanInterval = RepeatScanSeconds or Mines.ActivatedMonitorRepeatTime
if self.ActivatedMonitorID then
self:ActivatedMonitorStop()
end
self.ActivatedMonitorID = timer.scheduleFunction(self._ActivatedMonitorFunction,{target = target,context = self,repeatTime = RepeatScanInterval},timer.getTime() + tDelay)
end
function Mines:ActivatedMonitorStop()
if not self.ActivatedMonitorID then return end
timer.removeFunction(self.ActivatedMonitorID)
self.ActivatedMonitorID = nil
end
function Mines:CheckEnemyInDetectionRange(enemyGroup)
if enemyGroup:isExist() then
for j,unit in ipairs(enemyGroup:getUnits()) do
if unit:getLife() > 1 then
local enemyPoint = unit:getPoint()
local Distance = Utils.get3DDist(enemyPoint,self.point)
if Mines.DebugMod then
-- Utils.Debug('Distance: '..Distance)
end
if Distance <= self.DetectionRange then
if math.random(1,10) < 7 then
if Mines.DebugMod then
Utils.Debug('Helo unit in mine DetectionRange.')
end
self:ActivatedMonitorStart(unit,1)
return true
end
end
end
end
end
return false
end
function Mines._MonitorFunction(vars,time)
local self = vars.context
local repeatTime = vars.repeatTime
if Mines.DebugMod then
-- Utils.Debug('_MonitorFunction')
end
--AG
local enemyGroundGroups = coalition.getGroups(coalition.side.BLUE,Group.Category.GROUND)
for i,group in ipairs(enemyGroundGroups) do
for j,unit in ipairs(group:getUnits()) do
local enemyPoint = unit:getPoint()
if Utils.pointInPolygon(enemyPoint, Mines.MineFieldZonePolygon) then
if Mines.DebugMod then
Utils.Debug('Ground unit in mine polygon.')
end
local chance = math.random(1,100)
if chance > 70 then
trigger.action.explosion(enemyPoint,Mines.Warhead_AG)
end
end
end
end
--AH
local enemyHeloGroups = coalition.getGroups(coalition.side.BLUE,Group.Category.HELICOPTER)
for i,group in ipairs(enemyHeloGroups) do
if self:CheckEnemyInDetectionRange(group) then
self:Stop()
break
end
end
local enemyPlaneGroups = coalition.getGroups(coalition.side.BLUE,Group.Category.AIRPLANE)
for i,group in ipairs(enemyPlaneGroups) do
if self:CheckEnemyInDetectionRange(group) then
self:Stop()
break
end
end
return time + repeatTime
end
function Mines:Start(Delay,RepeatScanSeconds)
Mines.MonitorStartDelay = Delay or Mines.MonitorStartDelay
local tDelay = Delay or Mines.MonitorStartDelay
Mines.MonitorStartDelay = Mines.MonitorStartDelay + 1
local RepeatScanInterval = RepeatScanSeconds or Mines.MonitorRepeatTime
if self.MonitorID then
self:Stop()
end
self.MonitorID = timer.scheduleFunction(self._MonitorFunction,{context = self,repeatTime = RepeatScanInterval},timer.getTime() + tDelay)
end
function Mines:Stop()
if not self.MonitorID then return end
timer.removeFunction(self.MonitorID)
self.MonitorID = nil
end
--- Init
for i=1,200,1 do
local myMinesStaticObjName = 'Static Barrel-'..i..'-1'
local myMineStaticObj = StaticObject.getByName(myMinesStaticObjName)
if myMineStaticObj then
local myMine = Mines:New(myMineStaticObj)
end
-- Add mines to alarm ignore
redAlarmEvent.alarmOffGroups[myMinesStaticObjName] = true
end
-- Add Patrol to alarm ignore
if PatrolUnit then
if PatrolUnit.AllGroups then
for groupName, value in ipairs(PatrolUnit.AllGroups) do
redAlarmEvent.alarmOffGroups[groupName] = true
end
end
end
end