Update first playable version
- add brief - fix event handler bugs
This commit is contained in:
+346
-25
@@ -1,18 +1,23 @@
|
||||
-- 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
|
||||
local redAlarmEvent = {
|
||||
redAlarm = false,
|
||||
ignoreList = {
|
||||
['Red-RescueInf-1'] = true,
|
||||
['Red-Scud'] = true,
|
||||
}
|
||||
}
|
||||
|
||||
function redAlarmEvent:redAlarm_On()
|
||||
Utils.Debug('==================\n\n敌人因为受到攻击, 拉响了警报!\n\n==================')
|
||||
if self.redAlarm then return end
|
||||
|
||||
Utils.Debug('==================\n\n敌人因为受到攻击, 拉响了警报!\n\n==================')
|
||||
self.redAlarm = true
|
||||
local redGrounds = coalition.getGroups(1,Group.Category.GROUND)
|
||||
|
||||
@@ -26,6 +31,9 @@ do
|
||||
|
||||
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))
|
||||
@@ -37,12 +45,27 @@ do
|
||||
|
||||
function redAlarmEvent:onEvent(event)
|
||||
if not self.redAlarm then
|
||||
if event.id == world.event.S_EVENT_HIT or event.id == world.event.S_EVENT_KILL or event.id == world.event.S_EVENT_DEAD then
|
||||
local object = event.initiator
|
||||
if not object:getCoalition() == 1 then return end
|
||||
if object:getName() == 'Red-PowerStation-1' then return end
|
||||
if event.id == world.event.S_EVENT_HIT then
|
||||
local object = event.target
|
||||
if not object then return end
|
||||
if not object:isExist() then return end
|
||||
|
||||
local objectCategory, unitCategory = Unit.getCategory(object)
|
||||
if not object.getCoalition then return end
|
||||
if object:getCoalition() == 2 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
|
||||
self:redAlarm_On()
|
||||
@@ -145,8 +168,10 @@ do
|
||||
newStaticObjs = {},
|
||||
}
|
||||
function PowerStationEvent:onEvent(event)
|
||||
if event.id == world.event.S_EVENT_KILL or event.id == world.event.S_EVENT_DEAD then
|
||||
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
|
||||
@@ -239,17 +264,313 @@ do
|
||||
world.addEventHandler(PowerStationEvent)
|
||||
end
|
||||
|
||||
--Debug
|
||||
local PowerStation = StaticObject.getByName('Red-PowerStation-1')
|
||||
if not PowerStation then return end
|
||||
-- Mines
|
||||
do
|
||||
Mines = {}
|
||||
|
||||
timer.scheduleFunction(function (PowerStation)
|
||||
Utils.Debug('触发电厂被炸event')
|
||||
Mines.DebugMod = false
|
||||
Mines.DetectionRange = 1500
|
||||
Mines.FireRange = 300
|
||||
|
||||
event = {
|
||||
id = world.event.S_EVENT_DEAD,
|
||||
initiator = PowerStation,
|
||||
}
|
||||
Mines.TriggerExplode = 0.5 --kg
|
||||
Mines.Warhead_AH = 3 --kg
|
||||
|
||||
world.onEvent(event)
|
||||
end,PowerStation,timer.getTime()+10)
|
||||
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
|
||||
Reference in New Issue
Block a user