diff --git a/Moose Development/Moose/Core/Goal.lua b/Moose Development/Moose/Core/Goal.lua new file mode 100644 index 000000000..711609bff --- /dev/null +++ b/Moose Development/Moose/Core/Goal.lua @@ -0,0 +1,269 @@ +--- **Core** -- Base class that models processes to achieve goals. +-- +-- ==== +-- +-- GOAL models processes that have an objective with a defined achievement. Derived classes implement the ways how the achievements can be realized. +-- +-- ==== +-- +-- ### Author: **Sven Van de Velde (FlightControl)** +-- +-- ==== +-- +-- @module Goal + +do -- Goal + + --- @type GOAL + -- @extends Core.Fsm#FSM + + + --- # GOAL class, extends @{Fsm#FSM} + -- + -- GOAL models processes that have an objective with a defined achievement. Derived classes implement the ways how the achievements can be realized. + -- + -- ## 1. GOAL constructor + -- + -- * @{#GOAL.New}(): Creates a new GOAL object. + -- + -- ## 2. GOAL is a finite state machine (FSM). + -- + -- ### 2.1 GOAL States + -- + -- * **Off**: The goal is not timely measured. + -- * **On**: The goal is timely being measured. + -- * **Achieved**: The objective is achieved. + -- + -- ### 2.2 GOAL Events + -- + -- * **@{#GOAL.Start}()**: Start Measuring the Goal. + -- * **@{#GOAL.Stop}()**: Stop Measuring the Goal. + -- * **@{#GOAL.IsAchieved}()**: Check if the Goal is Achieved. + -- + -- @field #GOAL + GOAL = { + ClassName = "GOAL", + } + + --- @field #table GOAL.States + GOAL.States = {} + + --- GOAL Constructor. + -- @param #GOAL self + -- @return #GOAL + function GOAL:New() + + local self = BASE:Inherit( self, FSM:New() ) -- #GOAL + self:F( {} ) + + + do + + --- On State for GOAL + -- @field GOAL.On + + --- On State Handler OnLeave for GOAL + -- @function [parent=#GOAL] OnLeaveOn + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- On State Handler OnEnter for GOAL + -- @function [parent=#GOAL] OnEnterOn + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + GOAL.States.On = "On" + + end + + do + + --- Off State for GOAL + -- @field GOAL.Off + + --- Off State Handler OnLeave for GOAL + -- @function [parent=#GOAL] OnLeaveOff + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Off State Handler OnEnter for GOAL + -- @function [parent=#GOAL] OnEnterOff + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + GOAL.States.Off = "Off" + + end + + --- Achieved State for GOAL + -- @field GOAL.Achieved + GOAL.States.Achieved = "Achieved" + + --- Achieved State Handler OnLeave for GOAL + -- @function [parent=#GOAL] OnLeaveAchieved + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Achieved State Handler OnEnter for GOAL + -- @function [parent=#GOAL] OnEnterAchieved + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + + self:SetStartState( GOAL.States.Off ) + self:AddTransition( GOAL.States.Off, "Start", GOAL.States.On ) + + --- Start Handler OnBefore for GOAL + -- @function [parent=#GOAL] OnBeforeStart + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Start Handler OnAfter for GOAL + -- @function [parent=#GOAL] OnAfterStart + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- Start Trigger for GOAL + -- @function [parent=#GOAL] Start + -- @param #GOAL self + + --- Start Asynchronous Trigger for GOAL + -- @function [parent=#GOAL] __Start + -- @param #GOAL self + -- @param #number Delay + + self:AddTransition( GOAL.States.On, "Stop", GOAL.States.Off ) + + --- Stop Handler OnBefore for GOAL + -- @function [parent=#GOAL] OnBeforeStop + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Stop Handler OnAfter for GOAL + -- @function [parent=#GOAL] OnAfterStop + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- Stop Trigger for GOAL + -- @function [parent=#GOAL] Stop + -- @param #GOAL self + + --- Stop Asynchronous Trigger for GOAL + -- @function [parent=#GOAL] __Stop + -- @param #GOAL self + -- @param #number Delay + + + self:AddTransition( GOAL.States.On, "IsAchieved", GOAL.States.On ) + + --- IsAchieved Handler OnBefore for GOAL + -- @function [parent=#GOAL] OnBeforeIsAchieved + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- IsAchieved Handler OnAfter for GOAL + -- @function [parent=#GOAL] OnAfterIsAchieved + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- IsAchieved Trigger for GOAL + -- @function [parent=#GOAL] IsAchieved + -- @param #GOAL self + + --- IsAchieved Asynchronous Trigger for GOAL + -- @function [parent=#GOAL] __IsAchieved + -- @param #GOAL self + -- @param #number Delay + + self:AddTransition( GOAL.States.On, "Achieved", GOAL.States.Achieved ) + + --- Achieved Handler OnBefore for GOAL + -- @function [parent=#GOAL] OnBeforeAchieved + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Achieved Handler OnAfter for GOAL + -- @function [parent=#GOAL] OnAfterAchieved + -- @param #GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- Achieved Trigger for GOAL + -- @function [parent=#GOAL] Achieved + -- @param #GOAL self + + --- Achieved Asynchronous Trigger for GOAL + -- @function [parent=#GOAL] __Achieved + -- @param #GOAL self + -- @param #number Delay + + + self.AchievedScheduler = nil + + self:SetEventPriority( 5 ) + + return self + end + + --- @param #GOAL self + -- @param From + -- @param Event + -- @param To + function GOAL:onafterOn( From, Event, To ) + if not self.AchievedScheduler then + self.AchievedScheduler = self:ScheduleRepeat( 15, 15, 0, nil, self.CheckAchieved, self ) + end + end + + --- @param #GOAL self + -- @param From + -- @param Event + -- @param To + function GOAL:onafterOff( From, Event, To ) + self:ScheduleStop( self.CheckAchieved ) + self.ArchievedScheduler = nil + end + + --- @param #GOAL self + -- @param From + -- @param Event + -- @param To + function GOAL:CheckAchieved( From, Event, To ) + self:IsAchieved() + end + +end \ No newline at end of file diff --git a/Moose Development/Moose/Core/ZoneGoal.lua b/Moose Development/Moose/Core/ZoneGoal.lua new file mode 100644 index 000000000..0ad882ab2 --- /dev/null +++ b/Moose Development/Moose/Core/ZoneGoal.lua @@ -0,0 +1,262 @@ +--- **Core** -- Base class that models processes to achieve goals involving a Zone. +-- +-- ==== +-- +-- ZONE_GOAL models processes that have an objective with a defined achievement involving a Zone. Derived classes implement the ways how the achievements can be realized. +-- +-- ==== +-- +-- ### Author: **Sven Van de Velde (FlightControl)** +-- +-- ==== +-- +-- @module Zone + +do -- Zone + + --- @type ZONE_GOAL + -- @extends Core.Goal#GOAL + + + --- # ZONE_GOAL class, extends @{Goal#GOAL} + -- + -- ZONE_GOAL models processes that have an objective with a defined achievement involving a Zone. Derived classes implement the ways how the achievements can be realized. + -- + -- ## 1. ZONE_GOAL constructor + -- + -- * @{#ZONE_GOAL.New}(): Creates a new ZONE_GOAL object. + -- + -- ## 2. ZONE_GOAL is a finite state machine (FSM). + -- + -- ### 2.1 ZONE_GOAL States + -- + -- * **Empty**: The Zone is Empty. + -- * **Guarded**: The Zone is Guarded. + -- + -- ### 2.2 ZONE_GOAL Events + -- + -- * **@{#ZONE_GOAL.Guard}()**: Set the Zone to Guarded. + -- * **@{#ZONE_GOAL.Empty}()**: Set the Zone to Empty. + -- + -- @field #ZONE_GOAL + ZONE_GOAL = { + ClassName = "ZONE_GOAL", + } + + --- @field #table ZONE_GOAL.States + ZONE_GOAL.States = {} + + --- ZONE_GOAL Constructor. + -- @param #ZONE_GOAL self + -- @param Core.Zone#ZONE_BASE Zone A @{Zone} object with the goal to be achieved. + -- @return #ZONE_GOAL + function ZONE_GOAL:New( Zone ) + + local self = BASE:Inherit( self, FSM:New() ) -- #ZONE_GOAL + self:F( { Zone = Zone } ) + + self.Zone = Zone -- Core.Zone#ZONE_BASE + self.Goal = GOAL:New():Start() + + do + + --- Guarded State Handler OnLeave for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] OnLeaveGuarded + -- @param #ZONE_GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Guarded State Handler OnEnter for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] OnEnterGuarded + -- @param #ZONE_GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + ZONE_GOAL.States.Guarded = "Guarded" + + end + + + do + + --- Empty State Handler OnLeave for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] OnLeaveEmpty + -- @param #ZONE_GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Empty State Handler OnEnter for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] OnEnterEmpty + -- @param #ZONE_GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + ZONE_GOAL.States.Empty = "Empty" + + end + + + self:AddTransition( "*", "Guard", ZONE_GOAL.States.Guarded ) + + --- Guard Handler OnBefore for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] OnBeforeGuard + -- @param #ZONE_GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Guard Handler OnAfter for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] OnAfterGuard + -- @param #ZONE_GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- Guard Trigger for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] Guard + -- @param #ZONE_GOAL self + + --- Guard Asynchronous Trigger for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] __Guard + -- @param #ZONE_GOAL self + -- @param #number Delay + + self:AddTransition( "*", "Empty", ZONE_GOAL.States.Empty ) + + --- Empty Handler OnBefore for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] OnBeforeEmpty + -- @param #ZONE_GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Empty Handler OnAfter for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] OnAfterEmpty + -- @param #ZONE_GOAL self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- Empty Trigger for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] Empty + -- @param #ZONE_GOAL self + + --- Empty Asynchronous Trigger for ZONE_GOAL + -- @function [parent=#ZONE_GOAL] __Empty + -- @param #ZONE_GOAL self + -- @param #number Delay + + + self.SmokeTime = nil + + return self + end + + --- Get the Zone + -- @param #ZONE_GOAL self + -- @return Core.Zone#ZONE_BASE + function ZONE_GOAL:GetZone() + return self.Zone + end + + + --- Get the name of the ProtectZone + -- @param #ZONE_GOAL self + -- @return #string + function ZONE_GOAL:GetZoneName() + return self.Zone:GetName() + end + + --- Smoke the center of theh zone. + -- @param #ZONE_GOAL self + -- @param #SMOKECOLOR.Color SmokeColor + function ZONE_GOAL:Smoke( SmokeColor ) + + self:F( { SmokeColor = SmokeColor} ) + + self.SmokeColor = SmokeColor + end + + + --- Flare the center of the zone. + -- @param #ZONE_GOAL self + -- @param #SMOKECOLOR.Color FlareColor + function ZONE_GOAL:Flare( FlareColor ) + self.Zone:FlareZone( FlareColor, math.random( 1, 360 ) ) + end + + + --- When started, check the Smoke and the Zone status. + -- @param #ZONE_GOAL self + function ZONE_GOAL:onafterGuard() + + --self:GetParent( self ):onafterStart() + + self:E("Guard") + + --self:ScheduleRepeat( 15, 15, 0.1, nil, self.StatusZone, self ) + if not self.SmokeScheduler then + self.SmokeScheduler = self:ScheduleRepeat( 1, 1, 0.1, nil, self.StatusSmoke, self ) + end + end + + function ZONE_GOAL:IsGuarded() + + local IsGuarded = self.Zone:IsAllInZoneOfCoalition( self.Coalition ) + self:E( { IsGuarded = IsGuarded } ) + return IsGuarded + end + + + function ZONE_GOAL:IsEmpty() + + local IsEmpty = self.Zone:IsNoneInZone() + self:E( { IsEmpty = IsEmpty } ) + return IsEmpty + end + + + --- Check status Zone. + -- @param #ZONE_GOAL self + function ZONE_GOAL:StatusZone() + + self:E( { State = self:GetState() } ) + + self.Zone:Scan() + + if self:IsGuarded() then + self:Guard() + end + + if self:IsEmpty() then + self:Empty() + end + + end + + --- Check status Smoke. + -- @param #ZONE_GOAL self + function ZONE_GOAL:StatusSmoke() + + self:F({self.SmokeTime, self.SmokeColor}) + + local CurrentTime = timer.getTime() + + if self.SmokeTime == nil or self.SmokeTime + 300 <= CurrentTime then + if self.SmokeColor then + self.Zone:GetCoordinate():Smoke( self.SmokeColor ) + --self.SmokeColor = nil + self.SmokeTime = CurrentTime + end + end + end + +end diff --git a/Moose Development/Moose/Core/ZoneGoalCoalition.lua b/Moose Development/Moose/Core/ZoneGoalCoalition.lua new file mode 100644 index 000000000..152b8388d --- /dev/null +++ b/Moose Development/Moose/Core/ZoneGoalCoalition.lua @@ -0,0 +1,342 @@ +--- **Core** -- Base class that models processes to achieve goals involving a Zone for a Coalition. +-- +-- ==== +-- +-- ZONE_GOAL_COALITION models processes that have an objective with a defined achievement involving a Zone. Derived classes implement the ways how the achievements can be realized. +-- +-- ==== +-- +-- ### Author: **Sven Van de Velde (FlightControl)** +-- +-- ==== +-- +-- @module ZoneGoal + +do -- ZoneGoal + + --- @type ZONE_GOAL_COALITION + -- @extends Core.Goal#ZONE_GOAL_COALITION + + + --- # ZONE_GOAL_COALITION class, extends @{Goal#GOAL} + -- + -- ZONE_GOAL_COALITION models processes that have an objective with a defined achievement involving a Zone. Derived classes implement the ways how the achievements can be realized. + -- + -- ## 1. ZONE_GOAL_COALITION constructor + -- + -- * @{#ZONE_GOAL_COALITION.New}(): Creates a new ZONE_GOAL_COALITION object. + -- + -- ## 2. ZONE_GOAL_COALITION is a finite state machine (FSM). + -- + -- ### 2.1 ZONE_GOAL_COALITION States + -- + -- * **Off**: The goal is not timely measured. + -- * **On**: The goal is timely being measured. + -- * **Achieved**: The objective is achieved. + -- + -- ### 2.2 ZONE_GOAL_COALITION Events + -- + -- * **@{#ZONE_GOAL_COALITION.Start}()**: Start Measuring the Goal. + -- * **@{#ZONE_GOAL_COALITION.Stop}()**: Stop Measuring the Goal. + -- * **@{#ZONE_GOAL_COALITION.IsAchieved}()**: Check if the Goal is Achieved. + -- + -- @field #ZONE_GOAL_COALITION + ZONE_GOAL_COALITION = { + ClassName = "ZONE_GOAL_COALITION", + } + + --- @field #table ZONE_GOAL_COALITION.States + ZONE_GOAL_COALITION.States = {} + + --- ZONE_GOAL_COALITION Constructor. + -- @param #ZONE_GOAL_COALITION self + -- @param Core.Zone#ZONE Zone A @{Zone} object with the goal to be achieved. + -- @param DCSCoalition.DCSCoalition#coalition Coalition The initial coalition owning the zone. + -- @return #ZONE_GOAL_COALITION + function ZONE_GOAL_COALITION:New( Zone, Coalition ) + + local self = BASE:Inherit( self, ZONE_GOAL:New( Zone ) ) -- #ZONE_GOAL_COALITION + self:F( { Zone = Zone, Coalition = Coalition } ) + + self:SetCoalition( Coalition ) + + do + + --- Captured State Handler OnLeave for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] OnLeaveCaptured + -- @param #ZONE_GOAL_COALITION self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Captured State Handler OnEnter for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] OnEnterCaptured + -- @param #ZONE_GOAL_COALITION self + -- @param #string From + -- @param #string Event + -- @param #string To + + ZONE_GOAL_COALITION.States.Captured = "Captured" + + end + + + do + + --- Attacked State Handler OnLeave for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] OnLeaveAttacked + -- @param #ZONE_GOAL_COALITION self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Attacked State Handler OnEnter for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] OnEnterAttacked + -- @param #ZONE_GOAL_COALITION self + -- @param #string From + -- @param #string Event + -- @param #string To + + ZONE_GOAL_COALITION.States.Attacked = "Attacked" + + end + + + + self:AddTransition( { ZONE_GOAL_COALITION.States.Guarded, ZONE_GOAL_COALITION.States.Empty }, "Attack", ZONE_GOAL_COALITION.States.Attacked ) + + --- Attack Handler OnBefore for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] OnBeforeAttack + -- @param #ZONE_GOAL_COALITION self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Attack Handler OnAfter for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] OnAfterAttack + -- @param #ZONE_GOAL_COALITION self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- Attack Trigger for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] Attack + -- @param #ZONE_GOAL_COALITION self + + --- Attack Asynchronous Trigger for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] __Attack + -- @param #ZONE_GOAL_COALITION self + -- @param #number Delay + + self:AddTransition( { ZONE_GOAL_COALITION.States.Guarded, ZONE_GOAL_COALITION.States.Attacked, ZONE_GOAL_COALITION.States.Empty }, "Capture", ZONE_GOAL_COALITION.States.Captured ) + + --- Capture Handler OnBefore for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] OnBeforeCapture + -- @param #ZONE_GOAL_COALITION self + -- @param #string From + -- @param #string Event + -- @param #string To + -- @return #boolean + + --- Capture Handler OnAfter for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] OnAfterCapture + -- @param #ZONE_GOAL_COALITION self + -- @param #string From + -- @param #string Event + -- @param #string To + + --- Capture Trigger for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] Capture + -- @param #ZONE_GOAL_COALITION self + + --- Capture Asynchronous Trigger for ZONE_GOAL_COALITION + -- @function [parent=#ZONE_GOAL_COALITION] __Capture + -- @param #ZONE_GOAL_COALITION self + -- @param #number Delay + + return self + end + + + --- Set the owning coalition of the zone. + -- @param #ZONE_GOAL_COALITION self + -- @param DCSCoalition.DCSCoalition#coalition Coalition + function ZONE_GOAL_COALITION:SetCoalition( Coalition ) + self.Coalition = Coalition + end + + + --- Get the owning coalition of the zone. + -- @param #ZONE_GOAL_COALITION self + -- @return DCSCoalition.DCSCoalition#coalition Coalition. + function ZONE_GOAL_COALITION:GetCoalition() + return self.Coalition + end + + + --- Get the owning coalition name of the zone. + -- @param #ZONE_GOAL_COALITION self + -- @return #string Coalition name. + function ZONE_GOAL_COALITION:GetCoalitionName() + + if self.Coalition == coalition.side.BLUE then + return "Blue" + end + + if self.Coalition == coalition.side.RED then + return "Red" + end + + if self.Coalition == coalition.side.NEUTRAL then + return "Neutral" + end + + return "" + end + + + function ZONE_GOAL_COALITION:IsGuarded() + + local IsGuarded = self.Zone:IsAllInZoneOfCoalition( self.Coalition ) + self:E( { IsGuarded = IsGuarded } ) + return IsGuarded + end + + function ZONE_GOAL_COALITION:IsCaptured() + + local IsCaptured = self.Zone:IsAllInZoneOfOtherCoalition( self.Coalition ) + self:E( { IsCaptured = IsCaptured } ) + return IsCaptured + end + + + function ZONE_GOAL_COALITION:IsAttacked() + + local IsAttacked = self.Zone:IsSomeInZoneOfCoalition( self.Coalition ) + self:E( { IsAttacked = IsAttacked } ) + return IsAttacked + end + + + + --- Mark. + -- @param #ZONE_GOAL_COALITION self + function ZONE_GOAL_COALITION:Mark() + + local Coord = self.Zone:GetCoordinate() + local ZoneName = self:GetZoneName() + local State = self:GetState() + + if self.MarkRed and self.MarkBlue then + self:E( { MarkRed = self.MarkRed, MarkBlue = self.MarkBlue } ) + Coord:RemoveMark( self.MarkRed ) + Coord:RemoveMark( self.MarkBlue ) + end + + if self.Coalition == coalition.side.BLUE then + self.MarkBlue = Coord:MarkToCoalitionBlue( "Guard Zone: " .. ZoneName .. "\nStatus: " .. State ) + self.MarkRed = Coord:MarkToCoalitionRed( "Capture Zone: " .. ZoneName .. "\nStatus: " .. State ) + else + self.MarkRed = Coord:MarkToCoalitionRed( "Guard Zone: " .. ZoneName .. "\nStatus: " .. State ) + self.MarkBlue = Coord:MarkToCoalitionBlue( "Capture Zone: " .. ZoneName .. "\nStatus: " .. State ) + end + end + + --- Bound. + -- @param #ZONE_GOAL_COALITION self + function ZONE_GOAL_COALITION:onenterGuarded() + + --self:GetParent( self ):onenterGuarded() + + if self.Coalition == coalition.side.BLUE then + --elf.ProtectZone:BoundZone( 12, country.id.USA ) + else + --self.ProtectZone:BoundZone( 12, country.id.RUSSIA ) + end + + self:Mark() + + end + + function ZONE_GOAL_COALITION:onenterCaptured() + + --self:GetParent( self ):onenterCaptured() + + local NewCoalition = self.ProtectZone:GetCoalition() + self:E( { NewCoalition = NewCoalition } ) + self:SetCoalition( NewCoalition ) + + self:Mark() + end + + + function ZONE_GOAL_COALITION:onenterEmpty() + + --self:GetParent( self ):onenterEmpty() + + self:Mark() + end + + + function ZONE_GOAL_COALITION:onenterAttacked() + + --self:GetParent( self ):onenterAttacked() + + self:Mark() + end + + + --- When started, check the Coalition status. + -- @param #ZONE_GOAL self + function ZONE_GOAL_COALITION:onafterGuard() + + --self:E({BASE:GetParent( self )}) + --BASE:GetParent( self ).onafterGuard( self ) + + if not self.SmokeScheduler then + self.SmokeScheduler = self:ScheduleRepeat( 1, 1, 0.1, nil, self.StatusSmoke, self ) + end + if not self.ScheduleStatusZone then + self.ScheduleStatusZone = self:ScheduleRepeat( 15, 15, 0.1, nil, self.StatusZone, self ) + end + end + + + function ZONE_GOAL_COALITION:IsCaptured() + + local IsCaptured = self.Zone:IsAllInZoneOfOtherCoalition( self.Coalition ) + self:E( { IsCaptured = IsCaptured } ) + return IsCaptured + end + + + function ZONE_GOAL_COALITION:IsAttacked() + + local IsAttacked = self.Zone:IsSomeInZoneOfCoalition( self.Coalition ) + self:E( { IsAttacked = IsAttacked } ) + return IsAttacked + end + + --- Check status Coalition ownership. + -- @param #ZONE_GOAL_COALITION self + function ZONE_GOAL_COALITION:StatusZone() + + self:E( { State = self:GetState() } ) + + self:GetParent( self ):StatusZone() + + if self:IsAttacked() then + self:Attack() + end + + if self:IsCaptured() then + self:Capture() + end + + end + +end + diff --git a/Moose Development/Moose/Functional/Protect.lua b/Moose Development/Moose/Functional/Protect.lua index fe3a580b4..7b163ea2b 100644 --- a/Moose Development/Moose/Functional/Protect.lua +++ b/Moose Development/Moose/Functional/Protect.lua @@ -22,167 +22,6 @@ PROTECT = { ClassName = "PROTECT", } ---- PROTECT constructor. --- @param #PROTECT self --- @param Core.Zone#ZONE ProtectZone A @{Zone} object to protect. --- @param DCSCoalition.DCSCoalition#coalition Coalition The initial coalition owning the zone. --- @return #PROTECT --- @usage --- -- Protect the zone --- ProtectZone = PROTECT:New( ZONE:New( "Zone" ) ) --- -function PROTECT:New( ProtectZone, Coalition ) - - local self = BASE:Inherit( self, FSM:New() ) -- #PROTECT - - self.ProtectZone = ProtectZone -- Core.Zone#ZONE - self.ProtectUnitSet = SET_UNIT:New() - self.ProtectStaticSet = SET_STATIC:New() - self.CaptureUnitSet = SET_UNIT:New() - - self:SetStartState( "-" ) - - self:AddTransition( "-", "Start", "Guarded" ) - - --- Start Handler OnBefore for PROTECT - -- @function [parent=#PROTECT] OnBeforeStart - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - -- @return #boolean - - --- Start Handler OnAfter for PROTECT - -- @function [parent=#PROTECT] OnAfterStart - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - - --- Start Trigger for PROTECT - -- @function [parent=#PROTECT] Start - -- @param #PROTECT self - - --- Start Asynchronous Trigger for PROTECT - -- @function [parent=#PROTECT] __Start - -- @param #PROTECT self - -- @param #number Delay - - self:AddTransition( { "Captured", "Attacked", "Empty" }, "Guard", "Guarded" ) - - --- Guard Handler OnBefore for PROTECT - -- @function [parent=#PROTECT] OnBeforeGuard - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - -- @return #boolean - - --- Guard Handler OnAfter for PROTECT - -- @function [parent=#PROTECT] OnAfterGuard - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - - --- Guard Trigger for PROTECT - -- @function [parent=#PROTECT] Guard - -- @param #PROTECT self - - --- Guard Asynchronous Trigger for PROTECT - -- @function [parent=#PROTECT] __Guard - -- @param #PROTECT self - -- @param #number Delay - - self:AddTransition( { "Guarded", "Attacked" }, "Empty", "Empty" ) - - --- Empty Handler OnBefore for PROTECT - -- @function [parent=#PROTECT] OnBeforeEmpty - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - -- @return #boolean - - --- Empty Handler OnAfter for PROTECT - -- @function [parent=#PROTECT] OnAfterEmpty - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - - --- Empty Trigger for PROTECT - -- @function [parent=#PROTECT] Empty - -- @param #PROTECT self - - --- Empty Asynchronous Trigger for PROTECT - -- @function [parent=#PROTECT] __Empty - -- @param #PROTECT self - -- @param #number Delay - - self:AddTransition( { "Guarded", "Empty" }, "Attack", "Attacked" ) - - --- Attack Handler OnBefore for PROTECT - -- @function [parent=#PROTECT] OnBeforeAttack - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - -- @return #boolean - - --- Attack Handler OnAfter for PROTECT - -- @function [parent=#PROTECT] OnAfterAttack - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - - --- Attack Trigger for PROTECT - -- @function [parent=#PROTECT] Attack - -- @param #PROTECT self - - --- Attack Asynchronous Trigger for PROTECT - -- @function [parent=#PROTECT] __Attack - -- @param #PROTECT self - -- @param #number Delay - - self:AddTransition( { "Guarded", "Attacked", "Empty" }, "Capture", "Captured" ) - - --- Capture Handler OnBefore for PROTECT - -- @function [parent=#PROTECT] OnBeforeCapture - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - -- @return #boolean - - --- Capture Handler OnAfter for PROTECT - -- @function [parent=#PROTECT] OnAfterCapture - -- @param #PROTECT self - -- @param #string From - -- @param #string Event - -- @param #string To - - --- Capture Trigger for PROTECT - -- @function [parent=#PROTECT] Capture - -- @param #PROTECT self - - --- Capture Asynchronous Trigger for PROTECT - -- @function [parent=#PROTECT] __Capture - -- @param #PROTECT self - -- @param #number Delay - - - - self:SetCoalition( Coalition ) - - self.SmokeTime = nil - - return self - -end - - --- Get the ProtectZone -- @param #PROTECT self -- @return Core.Zone#ZONE_BASE @@ -209,52 +48,30 @@ end --- Get the owning coalition of the zone. -- @param #PROTECT self --- @return DCSCoalition.DCSCoalition#coalition Coalition +-- @return DCSCoalition.DCSCoalition#coalition Coalition. function PROTECT:GetCoalition() return self.Coalition end ---- Add a unit to the protection. +--- Get the owning coalition name of the zone. -- @param #PROTECT self --- @param Wrapper.Unit#UNIT ProtectUnit A @{Unit} object to protect. -function PROTECT:AddProtectUnit( ProtectUnit ) - self.ProtectUnitSet:AddUnit( ProtectUnit ) -end +-- @return #string Coalition name. +function PROTECT:GetCoalitionName() ---- Get the Protect unit Set. --- @param #PROTECT self --- @return Wrapper.Unit#UNIT The Set of capture units. -function PROTECT:GetProtectUnitSet() - return self.ProtectUnitSet -end - ---- Add a static to the protection. --- @param #PROTECT self --- @param Wrapper.Unit#UNIT ProtectStatic A @{Static} object to protect. -function PROTECT:AddProtectStatic( ProtectStatic ) - self.ProtectStaticSet:AddStatic( ProtectStatic ) -end - ---- Get the Protect static Set. --- @param #PROTECT self --- @return Wrapper.Unit#UNIT The Set of capture statics. -function PROTECT:GetProtectStaticSet() - return self.ProtectStaticSet -end - ---- Add a Capture unit to allow to capture the zone. --- @param #PROTECT self --- @param Wrapper.Unit#UNIT CaptureUnit A @{Unit} object to allow a capturing. -function PROTECT:AddCaptureUnit( CaptureUnit ) - self.CaptureUnitSet:AddUnit( CaptureUnit ) -end - ---- Get the Capture unit Set. --- @param #PROTECT self --- @return Wrapper.Unit#UNIT The Set of capture units. -function PROTECT:GetCaptureUnitSet() - return self.CaptureUnitSet + if self.Coalition == coalition.side.BLUE then + return "Blue" + end + + if self.Coalition == coalition.side.RED then + return "Red" + end + + if self.Coalition == coalition.side.NEUTRAL then + return "Neutral" + end + + return "" end diff --git a/Moose Development/Moose/Functional/ZoneCaptureCoalition.lua b/Moose Development/Moose/Functional/ZoneCaptureCoalition.lua new file mode 100644 index 000000000..27cf8d615 --- /dev/null +++ b/Moose Development/Moose/Functional/ZoneCaptureCoalition.lua @@ -0,0 +1,67 @@ +--- **Core** -- Base class that models processes to capture a Zone for a Coalition, guarded by another Coalition. +-- +-- ==== +-- +-- ZONE_CAPTURE_COALITION models processes that have an objective with a defined achievement involving a Zone. Derived classes implement the ways how the achievements can be realized. +-- +-- ==== +-- +-- ### Author: **Sven Van de Velde (FlightControl)** +-- +-- ==== +-- +-- @module ZoneCaptureCoalition + +do -- ZoneGoal + + --- @type ZONE_CAPTURE_COALITION + -- @extends Core.ZoneGoalCoalition#ZONE_GOAL_COALITION + + + --- # ZONE_CAPTURE_COALITION class, extends @{Goal#GOAL} + -- + -- ZONE_CAPTURE_COALITION models processes that have an objective with a defined achievement involving a Zone. Derived classes implement the ways how the achievements can be realized. + -- + -- ## 1. ZONE_CAPTURE_COALITION constructor + -- + -- * @{#ZONE_CAPTURE_COALITION.New}(): Creates a new ZONE_CAPTURE_COALITION object. + -- + -- ## 2. ZONE_CAPTURE_COALITION is a finite state machine (FSM). + -- + -- ### 2.1 ZONE_CAPTURE_COALITION States + -- + -- ### 2.2 ZONE_CAPTURE_COALITION Events + -- + -- @field #ZONE_CAPTURE_COALITION + ZONE_CAPTURE_COALITION = { + ClassName = "ZONE_CAPTURE_COALITION", + } + + --- @field #table ZONE_CAPTURE_COALITION.States + ZONE_CAPTURE_COALITION.States = {} + + --- ZONE_CAPTURE_COALITION Constructor. + -- @param #ZONE_CAPTURE_COALITION self + -- @param Core.Zone#ZONE Zone A @{Zone} object with the goal to be achieved. + -- @param DCSCoalition.DCSCoalition#coalition Coalition The initial coalition owning the zone. + -- @return #ZONE_CAPTURE_COALITION + function ZONE_CAPTURE_COALITION:New( Zone, Coalition ) + + local self = BASE:Inherit( self, ZONE_GOAL_COALITION:New( Zone, Coalition ) ) -- #ZONE_CAPTURE_COALITION + + self:F( { Zone = Zone, Coalition = Coalition } ) + + return self + end + + + --- @param #ZONE_CAPTURE_COALITION self + function ZONE_CAPTURE_COALITION:onenterCaptured() + + self:GetParent( self ):onenterCaptured() + + self.Goal:Achieved() + end + +end + diff --git a/Moose Development/Moose/Tasking/Task.lua b/Moose Development/Moose/Tasking/Task.lua index e6a791794..856ab08c8 100644 --- a/Moose Development/Moose/Tasking/Task.lua +++ b/Moose Development/Moose/Tasking/Task.lua @@ -175,28 +175,34 @@ function TASK:New( Mission, SetGroupAssign, TaskName, TaskType, TaskBriefing ) --- Goal Handler OnBefore for TASK -- @function [parent=#TASK] OnBeforeGoal -- @param #TASK self - -- @param Wrapper.Controllable#CONTROLLABLE Controllable -- @param #string From -- @param #string Event -- @param #string To + -- @param Wrapper.Unit#UNIT PlayerUnit The @{Unit} of the player. + -- @param #string PlayerName The name of the player. -- @return #boolean --- Goal Handler OnAfter for TASK -- @function [parent=#TASK] OnAfterGoal -- @param #TASK self - -- @param Wrapper.Controllable#CONTROLLABLE Controllable -- @param #string From -- @param #string Event -- @param #string To + -- @param Wrapper.Unit#UNIT PlayerUnit The @{Unit} of the player. + -- @param #string PlayerName The name of the player. --- Goal Trigger for TASK -- @function [parent=#TASK] Goal -- @param #TASK self + -- @param Wrapper.Unit#UNIT PlayerUnit The @{Unit} of the player. + -- @param #string PlayerName The name of the player. --- Goal Asynchronous Trigger for TASK -- @function [parent=#TASK] __Goal -- @param #TASK self -- @param #number Delay + -- @param Wrapper.Unit#UNIT PlayerUnit The @{Unit} of the player. + -- @param #string PlayerName The name of the player. @@ -1274,7 +1280,7 @@ function TASK:onenterAssigned( From, Event, To, PlayerUnit, PlayerName ) self:GetMission():__Start( 1 ) -- When the task is assigned, the task goal needs to be checked of the derived classes. - self:__Goal( -10 ) -- Polymorphic + self:__Goal( -10, PlayerUnit, PlayerName ) -- Polymorphic self:SetMenu() end diff --git a/Moose Development/Moose/Tasking/Task_Protect.lua b/Moose Development/Moose/Tasking/Task_Protect.lua index 959a570dc..c70a5b144 100644 --- a/Moose Development/Moose/Tasking/Task_Protect.lua +++ b/Moose Development/Moose/Tasking/Task_Protect.lua @@ -193,10 +193,16 @@ do -- TASK_CAPTURE_ZONE Mission:AddTask( self ) + self.TaskCoalition = Protect:GetCoalition() + self.TaskCoalitionName = Protect:GetCoalitionName() + self.TaskZoneName = self.Protect:GetProtectZoneName() + self:SetBriefing( TaskBriefing or - "Capture zone " .. self.Protect:GetProtectZoneName() .. "." + "Capture zone " .. self.TaskZoneName .. "." ) + + self:UpdateTaskInfo() return self end @@ -208,7 +214,8 @@ do -- TASK_CAPTURE_ZONE local ZoneCoordinate = self.Protect:GetProtectZone():GetCoordinate() self:SetInfo( "Coordinates", ZoneCoordinate, 0 ) - + self:SetInfo( "Zone Name", self.TaskZoneName, 10 ) + self:SetInfo( "Zone Coalition", self.TaskCoalitionName, 11 ) end function TASK_CAPTURE_ZONE:ReportOrder( ReportGroup ) @@ -221,13 +228,23 @@ do -- TASK_CAPTURE_ZONE --- @param #TASK_CAPTURE_ZONE self - function TASK_CAPTURE_ZONE:onafterGoal( TaskUnit, From, Event, To ) + -- @param Wrapper.Unit#UNIT TaskUnit + function TASK_CAPTURE_ZONE:OnAfterGoal( From, Event, To, PlayerUnit, PlayerName ) + + self:E( { PlayerUnit = PlayerUnit } ) - if self.Protect:IsState( "Captured" ) then - self:Success() + if self.Protect then + local ProtectCoalition = self.Protect:GetCoalition() + local TaskCoalition = self.Coalition + + self:E( { ProtectCoalition = ProtectCoalition, TaskCoalition = TaskCoalition } ) + + if ProtectCoalition ~= TaskCoalition then + self:Success() + end end - self:__Goal( -10 ) + self:__Goal( -10, PlayerUnit, PlayerName ) end --- Set a score when a target in scope of the A2G attack, has been destroyed . @@ -277,7 +294,6 @@ do -- TASK_CAPTURE_ZONE return self end - - + end diff --git a/Moose Development/Moose/Wrapper/Identifiable.lua b/Moose Development/Moose/Wrapper/Identifiable.lua index cd7243f4b..cd86668c0 100644 --- a/Moose Development/Moose/Wrapper/Identifiable.lua +++ b/Moose Development/Moose/Wrapper/Identifiable.lua @@ -159,6 +159,36 @@ function IDENTIFIABLE:GetCoalition() return nil end +--- Returns the name of the coalition of the Identifiable. +-- @param #IDENTIFIABLE self +-- @return #string The name of the coalition. +-- @return #nil The DCS Identifiable is not existing or alive. +function IDENTIFIABLE:GetCoalitionName() + self:F2( self.IdentifiableName ) + + local DCSIdentifiable = self:GetDCSObject() + + if DCSIdentifiable then + local IdentifiableCoalition = DCSIdentifiable:getCoalition() + self:T3( IdentifiableCoalition ) + + if IdentifiableCoalition == coalition.side.BLUE then + return "Blue" + end + + if IdentifiableCoalition == coalition.side.RED then + return "Red" + end + + if IdentifiableCoalition == coalition.side.NEUTRAL then + return "Neutral" + end + end + + self:E( self.ClassName .. " " .. self.IdentifiableName .. " not found!" ) + return nil +end + --- Returns country of the Identifiable. -- @param #IDENTIFIABLE self -- @return Dcs.DCScountry#country.id The country identifier. diff --git a/Moose Mission Setup/Moose.files b/Moose Mission Setup/Moose.files index 2793ab797..09d931114 100644 --- a/Moose Mission Setup/Moose.files +++ b/Moose Mission Setup/Moose.files @@ -16,6 +16,9 @@ Core/Message.lua Core/Fsm.lua Core/Radio.lua Core/SpawnStatic.lua +Core/Goal.lua +Core/ZoneGoal.lua +Core/ZoneGoalCoalition.lua Core/Cargo.lua Core/Spot.lua @@ -41,7 +44,7 @@ Functional/AirbasePolice.lua Functional/Detection.lua Functional/Designate.lua Functional/RAT.lua -Functional/Protect.lua +Functional/ZoneCaptureCoalition.lua AI/AI_Balancer.lua AI/AI_A2A.lua diff --git a/Moose Mission Setup/Moose.lua b/Moose Mission Setup/Moose.lua index ef444d452..1a7ca62aa 100644 --- a/Moose Mission Setup/Moose.lua +++ b/Moose Mission Setup/Moose.lua @@ -1,5 +1,5 @@ env.info('*** MOOSE DYNAMIC INCLUDE START *** ') -env.info('Moose Generation Timestamp: 20170930_1343') +env.info('Moose Generation Timestamp: 20171002_1236') local base=_G __Moose={} __Moose.Include=function(IncludeFile) @@ -33,6 +33,9 @@ __Moose.Include('Core/Message.lua') __Moose.Include('Core/Fsm.lua') __Moose.Include('Core/Radio.lua') __Moose.Include('Core/SpawnStatic.lua') +__Moose.Include('Core/Goal.lua') +__Moose.Include('Core/ZoneGoal.lua') +__Moose.Include('Core/ZoneGoalCoalition.lua') __Moose.Include('Core/Cargo.lua') __Moose.Include('Core/Spot.lua') __Moose.Include('Wrapper/Object.lua') @@ -56,7 +59,7 @@ __Moose.Include('Functional/AirbasePolice.lua') __Moose.Include('Functional/Detection.lua') __Moose.Include('Functional/Designate.lua') __Moose.Include('Functional/RAT.lua') -__Moose.Include('Functional/Protect.lua') +__Moose.Include('Functional/ZoneCaptureCoalition.lua') __Moose.Include('AI/AI_Balancer.lua') __Moose.Include('AI/AI_A2A.lua') __Moose.Include('AI/AI_A2A_Patrol.lua')