From a79bc11834da7b9f30071ae01cf9c504bc0b38e5 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Fri, 21 Apr 2017 06:58:28 +0200 Subject: [PATCH 1/2] Progress --- Moose Development/Moose/AI/AI_Designate.lua | 110 +++++++++++--------- 1 file changed, 58 insertions(+), 52 deletions(-) diff --git a/Moose Development/Moose/AI/AI_Designate.lua b/Moose Development/Moose/AI/AI_Designate.lua index 35ed1639a..d892da22c 100644 --- a/Moose Development/Moose/AI/AI_Designate.lua +++ b/Moose Development/Moose/AI/AI_Designate.lua @@ -237,6 +237,9 @@ do -- AI_DESIGNATE self:SetLaserCodes( 1688 ) + self.LaserCodesUsed = {} + + self.Detection:__Start( 2 ) @@ -253,6 +256,8 @@ do -- AI_DESIGNATE self.LaserCodes = ( type( LaserCodes ) == "table" ) and LaserCodes or { LaserCodes } self:E(self.LaserCodes) + + self.LaserCodesUsed = {} return self end @@ -330,39 +335,35 @@ do -- AI_DESIGNATE DesignateMenu ) - if self.Spots[Index] then - - MENU_GROUP_COMMAND:New( - AttackGroup, - "Switch laser Off", - DetectedMenu, - self.MenuLaseOff, - self, - AttackGroup, - Index - ) - else - MENU_GROUP_COMMAND:New( - AttackGroup, - "Lase target 60 secs", - DetectedMenu, - self.MenuLaseOn, - self, - AttackGroup, - Index, - 60 - ) - MENU_GROUP_COMMAND:New( - AttackGroup, - "Lase target 120 secs", - DetectedMenu, - self.MenuLaseOn, - self, - AttackGroup, - Index, - 120 - ) - end + MENU_GROUP_COMMAND:New( + AttackGroup, + "Lase target 60 secs", + DetectedMenu, + self.MenuLaseOn, + self, + AttackGroup, + Index, + 60 + ) + MENU_GROUP_COMMAND:New( + AttackGroup, + "Lase target 120 secs", + DetectedMenu, + self.MenuLaseOn, + self, + AttackGroup, + Index, + 120 + ) + MENU_GROUP_COMMAND:New( + AttackGroup, + "Switch laser Off", + DetectedMenu, + self.MenuLaseOff, + self, + AttackGroup, + Index + ) MENU_GROUP_COMMAND:New( AttackGroup, @@ -425,32 +426,37 @@ do -- AI_DESIGNATE function AI_DESIGNATE:onafterLasing( From, Event, To, AttackGroup, Index, Duration ) local TargetSetUnit = self.Detection:GetDetectedSet( Index ) - + local Targets = false TargetSetUnit:ForEachUnit( --- @param Wrapper.Unit#UNIT SmokeUnit - function( SmokeUnit ) + function( TargetUnit ) self:E("In procedure") --if math.random( 1, ( 100 * TargetSetUnit:Count() ) / 100 ) <= 100 then - if SmokeUnit:IsAlive() then - local NearestRecceGroup = self.RecceSet:FindNearestGroupFromPointVec2( SmokeUnit:GetPointVec2() ) - if NearestRecceGroup then - for UnitID, UnitData in pairs( NearestRecceGroup:GetUnits() or {} ) do - local RecceUnit = UnitData -- Wrapper.Unit#UNIT - Targets = true - if RecceUnit:IsLasing() == false then - self.Spots[Index] = self.Spots[Index] or {} - local Spots = self.Spots[Index] - local LaserCode = self.LaserCodes[math.random(1, #self.LaserCodes)] - local Spot = RecceUnit:LaseUnit( SmokeUnit, LaserCode, Duration ) - Spots[RecceUnit] = Spot - RecceUnit:MessageToGroup( "Lasing " .. SmokeUnit:GetTypeName() .. " for " .. Duration .. " seconds. Laser Code: " .. Spot.LaserCode, 15, AttackGroup ) - break - else + if TargetUnit:IsAlive() then + local Spot = self.Spots[TargetUnit] + if (not Spot) or ( Spot and Spot:IsLasing() == false ) then + local NearestRecceGroup = self.RecceSet:FindNearestGroupFromPointVec2( TargetUnit:GetPointVec2() ) + if NearestRecceGroup then + for UnitID, UnitData in pairs( NearestRecceGroup:GetUnits() or {} ) do + local RecceUnit = UnitData -- Wrapper.Unit#UNIT + Targets = true + if RecceUnit:IsLasing() == false then + local LaserCode = self.LaserCodes[math.random(1, #self.LaserCodes)] + local Spot = RecceUnit:LaseUnit( TargetUnit, LaserCode, Duration ) + self.Spots[TargetUnit] = Spot + RecceUnit:MessageToGroup( "Lasing " .. TargetUnit:GetTypeName() .. " for " .. Duration .. " seconds. Laser Code: " .. Spot.LaserCode, 15, AttackGroup ) + break + end end end + else + local RecceUnit = Spot.Recce + RecceUnit:MessageToGroup( "Lasing " .. TargetUnit:GetTypeName() .. " for " .. Duration .. " seconds. Laser Code: " .. Spot.LaserCode, 15, AttackGroup ) end + else + self.Spots[TargetUnit] = nil end --end end @@ -473,7 +479,7 @@ do -- AI_DESIGNATE local TargetSetUnit = self.Detection:GetDetectedSet( Index ) - local Spots = self.Spots[Index] + local Spots = self.Spots for SpotID, SpotData in pairs( Spots ) do local Spot = SpotData -- Core.Spot#SPOT @@ -482,7 +488,7 @@ do -- AI_DESIGNATE end Spots = nil - self.Spots[Index] = nil + self.Spots = {} self:SetDesignateMenu() end From 51022be4d4e06ba0b69fc207b12eee4c773eef5e Mon Sep 17 00:00:00 2001 From: FlightControl Date: Fri, 21 Apr 2017 14:01:01 +0200 Subject: [PATCH 2/2] Working version --- Moose Development/Moose/AI/AI_Designate.lua | 44 ++++++++--- Moose Development/Moose/Core/Base.lua | 6 ++ Moose Development/Moose/Core/Set.lua | 22 +++++- Moose Development/Moose/Core/Spot.lua | 74 ++++++++++++++++++- .../Moose/Functional/Detection.lua | 4 +- .../Moose/Wrapper/Positionable.lua | 2 +- 6 files changed, 134 insertions(+), 18 deletions(-) diff --git a/Moose Development/Moose/AI/AI_Designate.lua b/Moose Development/Moose/AI/AI_Designate.lua index d892da22c..8f64c7b8f 100644 --- a/Moose Development/Moose/AI/AI_Designate.lua +++ b/Moose Development/Moose/AI/AI_Designate.lua @@ -118,6 +118,8 @@ do -- AI_DESIGNATE -- @function [parent=#AI_DESIGNATE] __Detect -- @param #AI_DESIGNATE self -- @param #number Delay + + self:AddTransition( "*", "LaseOn", "Lasing" ) @@ -308,6 +310,8 @@ do -- AI_DESIGNATE -- @return #AI_DESIGNATE function AI_DESIGNATE:SetDesignateMenu() + self.GroupSet:Flush() + self.GroupSet:ForEachGroup( --- @param Wrapper.Group#GROUP GroupReport @@ -426,8 +430,19 @@ do -- AI_DESIGNATE function AI_DESIGNATE:onafterLasing( From, Event, To, AttackGroup, Index, Duration ) local TargetSetUnit = self.Detection:GetDetectedSet( Index ) + + TargetSetUnit:Flush() - local Targets = false + for TargetUnit, SpotData in pairs( self.Spots ) do + local Spot = SpotData -- Core.Spot#SPOT + if not Spot:IsLasing() then + local LaserCode = Spot.LaserCode --(Not deleted when stopping with lasing). + self.LaserCodesUsed[LaserCode] = nil + self.Spots[TargetUnit] = nil + end + end + + local MoreTargets = false TargetSetUnit:ForEachUnit( --- @param Wrapper.Unit#UNIT SmokeUnit @@ -441,17 +456,22 @@ do -- AI_DESIGNATE if NearestRecceGroup then for UnitID, UnitData in pairs( NearestRecceGroup:GetUnits() or {} ) do local RecceUnit = UnitData -- Wrapper.Unit#UNIT - Targets = true if RecceUnit:IsLasing() == false then - local LaserCode = self.LaserCodes[math.random(1, #self.LaserCodes)] - local Spot = RecceUnit:LaseUnit( TargetUnit, LaserCode, Duration ) - self.Spots[TargetUnit] = Spot - RecceUnit:MessageToGroup( "Lasing " .. TargetUnit:GetTypeName() .. " for " .. Duration .. " seconds. Laser Code: " .. Spot.LaserCode, 15, AttackGroup ) - break + local LaserCodeIndex = math.random(1, #self.LaserCodes) + local LaserCode = self.LaserCodes[LaserCodeIndex] + if not self.LaserCodesUsed[LaserCode] then + MoreTargets = true + self.LaserCodesUsed[LaserCode] = LaserCodeIndex + local Spot = RecceUnit:LaseUnit( TargetUnit, LaserCode, Duration ) + self.Spots[TargetUnit] = Spot + RecceUnit:MessageToGroup( "Lasing " .. TargetUnit:GetTypeName() .. " for " .. Duration .. " seconds. Laser Code: " .. Spot.LaserCode, 15, AttackGroup ) + break + end end end end else + MoreTargets = true local RecceUnit = Spot.Recce RecceUnit:MessageToGroup( "Lasing " .. TargetUnit:GetTypeName() .. " for " .. Duration .. " seconds. Laser Code: " .. Spot.LaserCode, 15, AttackGroup ) end @@ -462,12 +482,11 @@ do -- AI_DESIGNATE end ) - if Targets == true then + if MoreTargets == true then self:__Lasing( -30, AttackGroup, Index, Duration ) else - self:__LaseOff( -0.2, AttackGroup, Index ) - end - + self:__LaseOff( 1, AttackGroup, Index ) + end self:SetDesignateMenu() end @@ -477,6 +496,8 @@ do -- AI_DESIGNATE -- @return #AI_DESIGNATE function AI_DESIGNATE:onafterLaseOff( From, Event, To, AttackGroup, Index ) + self.RecceSet:GetFirst():MessageToGroup( "Stopped lasing.", 15, AttackGroup ) + local TargetSetUnit = self.Detection:GetDetectedSet( Index ) local Spots = self.Spots @@ -489,6 +510,7 @@ do -- AI_DESIGNATE Spots = nil self.Spots = {} + self.LaserCodesUsed = {} self:SetDesignateMenu() end diff --git a/Moose Development/Moose/Core/Base.lua b/Moose Development/Moose/Core/Base.lua index 64bcd30d2..8e8bec750 100644 --- a/Moose Development/Moose/Core/Base.lua +++ b/Moose Development/Moose/Core/Base.lua @@ -454,6 +454,12 @@ do -- Event Handling -- @param #BASE self -- @param Core.Event#EVENTDATA EventData The EventData structure. + --- Occurs when an object is dead. + -- initiator : The unit that is dead. + -- @function [parent=#BASE] OnEventDead + -- @param #BASE self + -- @param Core.Event#EVENTDATA EventData The EventData structure. + --- Occurs when an object is completely destroyed. -- initiator : The unit that is was destroyed. -- @function [parent=#BASE] OnEvent diff --git a/Moose Development/Moose/Core/Set.lua b/Moose Development/Moose/Core/Set.lua index bac65d9aa..0087fd667 100644 --- a/Moose Development/Moose/Core/Set.lua +++ b/Moose Development/Moose/Core/Set.lua @@ -342,6 +342,26 @@ function SET_BASE:_FilterStart() return self end +--- Starts the filtering of the Dead events for the collection. +-- @param #SET_BASE self +-- @return #SET_BASE self +function SET_BASE:FilterDeads() + + self:HandleEvent( EVENTS.Dead, self._EventOnDeadOrCrash ) + + return self +end + +--- Starts the filtering of the Crash events for the collection. +-- @param #SET_BASE self +-- @return #SET_BASE self +function SET_BASE:FilterCrashes() + + self:HandleEvent( EVENTS.Crash, self._EventOnDeadOrCrash ) + + return self +end + --- Stops the filtering for the defined collection. -- @param #SET_BASE self -- @return #SET_BASE self @@ -430,7 +450,7 @@ function SET_BASE:_EventOnDeadOrCrash( Event ) if Event.IniDCSUnit then local ObjectName, Object = self:FindInDatabase( Event ) - if ObjectName and Object ~= nil then + if ObjectName then self:Remove( ObjectName ) end end diff --git a/Moose Development/Moose/Core/Spot.lua b/Moose Development/Moose/Core/Spot.lua index 870a1f3ee..f532ffd15 100644 --- a/Moose Development/Moose/Core/Spot.lua +++ b/Moose Development/Moose/Core/Spot.lua @@ -32,7 +32,11 @@ do --- @type SPOT - -- @extends BASE + -- @extends Core.Base#BASE + + + --- + -- @field #SPOT SPOT = { ClassName = "SPOT", } @@ -50,8 +54,61 @@ do self:SetStartState( "Off" ) self:AddTransition( "Off", "LaseOn", "On" ) + + --- LaseOn Handler OnBefore for SPOT + -- @function [parent=#SPOT] OnBeforeLaseOn + -- @param #SPOT self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- LaseOn Handler OnAfter for SPOT + -- @function [parent=#SPOT] OnAfterLaseOn + -- @param #SPOT self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- LaseOn Trigger for SPOT + -- @function [parent=#SPOT] LaseOn + -- @param #SPOT self + + --- LaseOn Asynchronous Trigger for SPOT + -- @function [parent=#SPOT] __LaseOn + -- @param #SPOT self + -- @param #number Delay + + + self:AddTransition( "On", "Lasing", "On" ) self:AddTransition( "On" , "LaseOff", "Off" ) + + --- LaseOff Handler OnBefore for SPOT + -- @function [parent=#SPOT] OnBeforeLaseOff + -- @param #SPOT self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- LaseOff Handler OnAfter for SPOT + -- @function [parent=#SPOT] OnAfterLaseOff + -- @param #SPOT self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- LaseOff Trigger for SPOT + -- @function [parent=#SPOT] LaseOff + -- @param #SPOT self + + --- LaseOff Asynchronous Trigger for SPOT + -- @function [parent=#SPOT] __LaseOff + -- @param #SPOT self + -- @param #number Delay + + self.Recce = Recce @@ -87,9 +144,22 @@ do self.ScheduleID = self.LaseScheduler:Schedule( self, StopLase, {self}, Duration ) end + self:HandleEvent( EVENTS.Dead ) + self:__Lasing( -0.2 ) end + --- @param #SPOT self + -- @param Core.Event#EVENTDATA EventData + function SPOT:OnEventDead(EventData) + if self.Target then + if EventData.IniDCSUnitName == self.Target:GetName() then + self:E( {"Target dead ", self.Target:GetName() } ) + self:__LaseOff( 0.1 ) + end + end + end + --- @param #SPOT self -- @param From -- @param Event @@ -112,6 +182,7 @@ do -- @return #SPOT function SPOT:onafterLaseOff( From, Event, To ) + self:E( {"Stopped lasing for ", self.Target:GetName() } ) self.Spot:destroy() self.Spot = nil if self.ScheduleID then @@ -120,7 +191,6 @@ do self.ScheduleID = nil self.Target = nil - self.LaserCode = nil return self end diff --git a/Moose Development/Moose/Functional/Detection.lua b/Moose Development/Moose/Functional/Detection.lua index 307cd31bb..f876970b7 100644 --- a/Moose Development/Moose/Functional/Detection.lua +++ b/Moose Development/Moose/Functional/Detection.lua @@ -506,8 +506,6 @@ do -- DETECTION_BASE self.DetectionRun = 0 self:UnIdentifyAllDetectedObjects() -- Resets the DetectedObjectsIdentified table - self.DetectionSetGroup:Flush() - for DetectionGroupID, DetectionGroupData in pairs( self.DetectionSetGroup:GetSet() ) do self:E( {DetectionGroupData}) self:__DetectionGroup( DetectDelay, DetectionGroupData ) -- Process each detection asynchronously. @@ -1132,7 +1130,7 @@ do -- DETECTION_BASE self.DetectedItems[self.DetectedItemCount] = DetectedItem end - DetectedItem.Set = Set or SET_UNIT:New() + DetectedItem.Set = Set or SET_UNIT:New():FilterDeads():FilterCrashes() DetectedItem.ItemID = self.DetectedItemMax DetectedItem.Removed = false diff --git a/Moose Development/Moose/Wrapper/Positionable.lua b/Moose Development/Moose/Wrapper/Positionable.lua index ffef55978..be15b3466 100644 --- a/Moose Development/Moose/Wrapper/Positionable.lua +++ b/Moose Development/Moose/Wrapper/Positionable.lua @@ -475,7 +475,7 @@ function POSITIONABLE:LaseUnit( Target, LaserCode, Duration ) local TargetVec3 = Target:GetVec3() self:E("bulding spot") - self.Spot = SPOT:New( self ) + self.Spot = SPOT:New( self ) -- Core.Spot#SPOT self.Spot:LaseOn( Target, LaserCode, Duration) return self.Spot