mirror of
https://github.com/FlightControl-Master/MOOSE.git
synced 2025-10-29 16:58:06 +00:00
Last Updates
This commit is contained in:
504
Moose Development/Moose/Tasking/DetectionManager.lua
Normal file
504
Moose Development/Moose/Tasking/DetectionManager.lua
Normal file
@@ -0,0 +1,504 @@
|
||||
--- This module contains the DETECTION_MANAGER class and derived classes.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- 1) @{DetectionManager#DETECTION_MANAGER} class, extends @{Base#BASE}
|
||||
-- ====================================================================
|
||||
-- The @{DetectionManager#DETECTION_MANAGER} class defines the core functions to report detected objects to groups.
|
||||
-- Reportings can be done in several manners, and it is up to the derived classes if DETECTION_MANAGER to model the reporting behaviour.
|
||||
--
|
||||
-- 1.1) DETECTION_MANAGER constructor:
|
||||
-- -----------------------------------
|
||||
-- * @{DetectionManager#DETECTION_MANAGER.New}(): Create a new DETECTION_MANAGER instance.
|
||||
--
|
||||
-- 1.2) DETECTION_MANAGER reporting:
|
||||
-- ---------------------------------
|
||||
-- Derived DETECTION_MANAGER classes will reports detected units using the method @{DetectionManager#DETECTION_MANAGER.ReportDetected}(). This method implements polymorphic behaviour.
|
||||
--
|
||||
-- The time interval in seconds of the reporting can be changed using the methods @{DetectionManager#DETECTION_MANAGER.SetReportInterval}().
|
||||
-- To control how long a reporting message is displayed, use @{DetectionManager#DETECTION_MANAGER.SetReportDisplayTime}().
|
||||
-- Derived classes need to implement the method @{DetectionManager#DETECTION_MANAGER.GetReportDisplayTime}() to use the correct display time for displayed messages during a report.
|
||||
--
|
||||
-- Reporting can be started and stopped using the methods @{DetectionManager#DETECTION_MANAGER.StartReporting}() and @{DetectionManager#DETECTION_MANAGER.StopReporting}() respectively.
|
||||
-- If an ad-hoc report is requested, use the method @{DetectionManager#DETECTION_MANAGER#ReportNow}().
|
||||
--
|
||||
-- The default reporting interval is every 60 seconds. The reporting messages are displayed 15 seconds.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- 2) @{DetectionManager#DETECTION_REPORTING} class, extends @{DetectionManager#DETECTION_MANAGER}
|
||||
-- =========================================================================================
|
||||
-- The @{DetectionManager#DETECTION_REPORTING} class implements detected units reporting. Reporting can be controlled using the reporting methods available in the @{DetectionManager#DETECTION_MANAGER} class.
|
||||
--
|
||||
-- 2.1) DETECTION_REPORTING constructor:
|
||||
-- -------------------------------
|
||||
-- The @{DetectionManager#DETECTION_REPORTING.New}() method creates a new DETECTION_REPORTING instance.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- 3) @{#DETECTION_DISPATCHER} class, extends @{#DETECTION_MANAGER}
|
||||
-- ================================================================
|
||||
-- The @{#DETECTION_DISPATCHER} class implements the dynamic dispatching of tasks upon groups of detected units determined a @{Set} of FAC (groups).
|
||||
-- The FAC will detect units, will group them, and will dispatch @{Task}s to groups. Depending on the type of target detected, different tasks will be dispatched.
|
||||
-- Find a summary below describing for which situation a task type is created:
|
||||
--
|
||||
-- * **CAS Task**: Is created when there are enemy ground units within range of the FAC, while there are friendly units in the FAC perimeter.
|
||||
-- * **BAI Task**: Is created when there are enemy ground units within range of the FAC, while there are NO other friendly units within the FAC perimeter.
|
||||
-- * **SEAD Task**: Is created when there are enemy ground units wihtin range of the FAC, with air search radars.
|
||||
--
|
||||
-- Other task types will follow...
|
||||
--
|
||||
-- 3.1) DETECTION_DISPATCHER constructor:
|
||||
-- --------------------------------------
|
||||
-- The @{#DETECTION_DISPATCHER.New}() method creates a new DETECTION_DISPATCHER instance.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- ### Contributions: Mechanist, Prof_Hilactic, FlightControl - Concept & Testing
|
||||
-- ### Author: FlightControl - Framework Design & Programming
|
||||
--
|
||||
-- @module DetectionManager
|
||||
|
||||
do -- DETECTION MANAGER
|
||||
|
||||
--- DETECTION_MANAGER class.
|
||||
-- @type DETECTION_MANAGER
|
||||
-- @field Set#SET_GROUP SetGroup The groups to which the FAC will report to.
|
||||
-- @field Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
|
||||
-- @extends Base#BASE
|
||||
DETECTION_MANAGER = {
|
||||
ClassName = "DETECTION_MANAGER",
|
||||
SetGroup = nil,
|
||||
Detection = nil,
|
||||
}
|
||||
|
||||
--- FAC constructor.
|
||||
-- @param #DETECTION_MANAGER self
|
||||
-- @param Set#SET_GROUP SetGroup
|
||||
-- @param Detection#DETECTION_BASE Detection
|
||||
-- @return #DETECTION_MANAGER self
|
||||
function DETECTION_MANAGER:New( SetGroup, Detection )
|
||||
|
||||
-- Inherits from BASE
|
||||
local self = BASE:Inherit( self, BASE:New() ) -- Detection#DETECTION_MANAGER
|
||||
|
||||
self.SetGroup = SetGroup
|
||||
self.Detection = Detection
|
||||
|
||||
self:SetReportInterval( 30 )
|
||||
self:SetReportDisplayTime( 25 )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Set the reporting time interval.
|
||||
-- @param #DETECTION_MANAGER self
|
||||
-- @param #number ReportInterval The interval in seconds when a report needs to be done.
|
||||
-- @return #DETECTION_MANAGER self
|
||||
function DETECTION_MANAGER:SetReportInterval( ReportInterval )
|
||||
self:F2()
|
||||
|
||||
self._ReportInterval = ReportInterval
|
||||
end
|
||||
|
||||
|
||||
--- Set the reporting message display time.
|
||||
-- @param #DETECTION_MANAGER self
|
||||
-- @param #number ReportDisplayTime The display time in seconds when a report needs to be done.
|
||||
-- @return #DETECTION_MANAGER self
|
||||
function DETECTION_MANAGER:SetReportDisplayTime( ReportDisplayTime )
|
||||
self:F2()
|
||||
|
||||
self._ReportDisplayTime = ReportDisplayTime
|
||||
end
|
||||
|
||||
--- Get the reporting message display time.
|
||||
-- @param #DETECTION_MANAGER self
|
||||
-- @return #number ReportDisplayTime The display time in seconds when a report needs to be done.
|
||||
function DETECTION_MANAGER:GetReportDisplayTime()
|
||||
self:F2()
|
||||
|
||||
return self._ReportDisplayTime
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- Reports the detected items to the @{Set#SET_GROUP}.
|
||||
-- @param #DETECTION_MANAGER self
|
||||
-- @param Detection#DETECTION_BASE Detection
|
||||
-- @return #DETECTION_MANAGER self
|
||||
function DETECTION_MANAGER:ReportDetected( Detection )
|
||||
self:F2()
|
||||
|
||||
end
|
||||
|
||||
--- Schedule the FAC reporting.
|
||||
-- @param #DETECTION_MANAGER self
|
||||
-- @param #number DelayTime The delay in seconds to wait the reporting.
|
||||
-- @param #number ReportInterval The repeat interval in seconds for the reporting to happen repeatedly.
|
||||
-- @return #DETECTION_MANAGER self
|
||||
function DETECTION_MANAGER:Schedule( DelayTime, ReportInterval )
|
||||
self:F2()
|
||||
|
||||
self._ScheduleDelayTime = DelayTime
|
||||
|
||||
self:SetReportInterval( ReportInterval )
|
||||
|
||||
self.FacScheduler = SCHEDULER:New(self, self._FacScheduler, { self, "DetectionManager" }, self._ScheduleDelayTime, self._ReportInterval )
|
||||
return self
|
||||
end
|
||||
|
||||
--- Report the detected @{Unit#UNIT}s detected within the @{Detection#DETECTION_BASE} object to the @{Set#SET_GROUP}s.
|
||||
-- @param #DETECTION_MANAGER self
|
||||
function DETECTION_MANAGER:_FacScheduler( SchedulerName )
|
||||
self:F2( { SchedulerName } )
|
||||
|
||||
return self:ProcessDetected( self.Detection )
|
||||
|
||||
-- self.SetGroup:ForEachGroup(
|
||||
-- --- @param Group#GROUP Group
|
||||
-- function( Group )
|
||||
-- if Group:IsAlive() then
|
||||
-- return self:ProcessDetected( self.Detection )
|
||||
-- end
|
||||
-- end
|
||||
-- )
|
||||
|
||||
-- return true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
do -- DETECTION_REPORTING
|
||||
|
||||
--- DETECTION_REPORTING class.
|
||||
-- @type DETECTION_REPORTING
|
||||
-- @field Set#SET_GROUP SetGroup The groups to which the FAC will report to.
|
||||
-- @field Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
|
||||
-- @extends #DETECTION_MANAGER
|
||||
DETECTION_REPORTING = {
|
||||
ClassName = "DETECTION_REPORTING",
|
||||
}
|
||||
|
||||
|
||||
--- DETECTION_REPORTING constructor.
|
||||
-- @param #DETECTION_REPORTING self
|
||||
-- @param Set#SET_GROUP SetGroup
|
||||
-- @param Detection#DETECTION_AREAS Detection
|
||||
-- @return #DETECTION_REPORTING self
|
||||
function DETECTION_REPORTING:New( SetGroup, Detection )
|
||||
|
||||
-- Inherits from DETECTION_MANAGER
|
||||
local self = BASE:Inherit( self, DETECTION_MANAGER:New( SetGroup, Detection ) ) -- #DETECTION_REPORTING
|
||||
|
||||
self:Schedule( 1, 30 )
|
||||
return self
|
||||
end
|
||||
|
||||
--- Creates a string of the detected items in a @{Detection}.
|
||||
-- @param #DETECTION_MANAGER self
|
||||
-- @param Set#SET_UNIT DetectedSet The detected Set created by the @{Detection#DETECTION_BASE} object.
|
||||
-- @return #DETECTION_MANAGER self
|
||||
function DETECTION_REPORTING:GetDetectedItemsText( DetectedSet )
|
||||
self:F2()
|
||||
|
||||
local MT = {} -- Message Text
|
||||
local UnitTypes = {}
|
||||
|
||||
for DetectedUnitID, DetectedUnitData in pairs( DetectedSet:GetSet() ) do
|
||||
local DetectedUnit = DetectedUnitData -- Unit#UNIT
|
||||
if DetectedUnit:IsAlive() then
|
||||
local UnitType = DetectedUnit:GetTypeName()
|
||||
|
||||
if not UnitTypes[UnitType] then
|
||||
UnitTypes[UnitType] = 1
|
||||
else
|
||||
UnitTypes[UnitType] = UnitTypes[UnitType] + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for UnitTypeID, UnitType in pairs( UnitTypes ) do
|
||||
MT[#MT+1] = UnitType .. " of " .. UnitTypeID
|
||||
end
|
||||
|
||||
return table.concat( MT, ", " )
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- Reports the detected items to the @{Set#SET_GROUP}.
|
||||
-- @param #DETECTION_REPORTING self
|
||||
-- @param Group#GROUP Group The @{Group} object to where the report needs to go.
|
||||
-- @param Detection#DETECTION_AREAS Detection The detection created by the @{Detection#DETECTION_BASE} object.
|
||||
-- @return #boolean Return true if you want the reporting to continue... false will cancel the reporting loop.
|
||||
function DETECTION_REPORTING:ProcessDetected( Group, Detection )
|
||||
self:F2( Group )
|
||||
|
||||
self:E( Group )
|
||||
local DetectedMsg = {}
|
||||
for DetectedAreaID, DetectedAreaData in pairs( Detection:GetDetectedAreas() ) do
|
||||
local DetectedArea = DetectedAreaData -- Detection#DETECTION_AREAS.DetectedArea
|
||||
DetectedMsg[#DetectedMsg+1] = " - Group #" .. DetectedAreaID .. ": " .. self:GetDetectedItemsText( DetectedArea.Set )
|
||||
end
|
||||
local FACGroup = Detection:GetDetectionGroups()
|
||||
FACGroup:MessageToGroup( "Reporting detected target groups:\n" .. table.concat( DetectedMsg, "\n" ), self:GetReportDisplayTime(), Group )
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
do -- DETECTION_DISPATCHER
|
||||
|
||||
--- DETECTION_DISPATCHER class.
|
||||
-- @type DETECTION_DISPATCHER
|
||||
-- @field Set#SET_GROUP SetGroup The groups to which the FAC will report to.
|
||||
-- @field Detection#DETECTION_BASE Detection The DETECTION_BASE object that is used to report the detected objects.
|
||||
-- @field Mission#MISSION Mission
|
||||
-- @field Group#GROUP CommandCenter
|
||||
-- @extends DetectionManager#DETECTION_MANAGER
|
||||
DETECTION_DISPATCHER = {
|
||||
ClassName = "DETECTION_DISPATCHER",
|
||||
Mission = nil,
|
||||
CommandCenter = nil,
|
||||
Detection = nil,
|
||||
}
|
||||
|
||||
|
||||
--- DETECTION_DISPATCHER constructor.
|
||||
-- @param #DETECTION_DISPATCHER self
|
||||
-- @param Set#SET_GROUP SetGroup
|
||||
-- @param Detection#DETECTION_BASE Detection
|
||||
-- @return #DETECTION_DISPATCHER self
|
||||
function DETECTION_DISPATCHER:New( Mission, CommandCenter, SetGroup, Detection )
|
||||
|
||||
-- Inherits from DETECTION_MANAGER
|
||||
local self = BASE:Inherit( self, DETECTION_MANAGER:New( SetGroup, Detection ) ) -- #DETECTION_DISPATCHER
|
||||
|
||||
self.Detection = Detection
|
||||
self.CommandCenter = CommandCenter
|
||||
self.Mission = Mission
|
||||
|
||||
self:Schedule( 30 )
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- Creates a SEAD task when there are targets for it.
|
||||
-- @param #DETECTION_DISPATCHER self
|
||||
-- @param Detection#DETECTION_AREAS.DetectedArea DetectedArea
|
||||
-- @return Set#SET_UNIT TargetSetUnit: The target set of units.
|
||||
-- @return #nil If there are no targets to be set.
|
||||
function DETECTION_DISPATCHER:EvaluateSEAD( DetectedArea )
|
||||
self:F( { DetectedArea.AreaID } )
|
||||
|
||||
local DetectedSet = DetectedArea.Set
|
||||
local DetectedZone = DetectedArea.Zone
|
||||
|
||||
-- Determine if the set has radar targets. If it does, construct a SEAD task.
|
||||
local RadarCount = DetectedSet:HasSEAD()
|
||||
|
||||
if RadarCount > 0 then
|
||||
|
||||
-- Here we're doing something advanced... We're copying the DetectedSet, but making a new Set only with SEADable Radar units in it.
|
||||
local TargetSetUnit = SET_UNIT:New()
|
||||
TargetSetUnit:SetDatabase( DetectedSet )
|
||||
TargetSetUnit:FilterHasSEAD()
|
||||
TargetSetUnit:FilterOnce() -- Filter but don't do any events!!! Elements are added manually upon each detection.
|
||||
|
||||
return TargetSetUnit
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Creates a CAS task when there are targets for it.
|
||||
-- @param #DETECTION_DISPATCHER self
|
||||
-- @param Detection#DETECTION_AREAS.DetectedArea DetectedArea
|
||||
-- @return Task#TASK_BASE
|
||||
function DETECTION_DISPATCHER:EvaluateCAS( DetectedArea )
|
||||
self:F( { DetectedArea.AreaID } )
|
||||
|
||||
local DetectedSet = DetectedArea.Set
|
||||
local DetectedZone = DetectedArea.Zone
|
||||
|
||||
|
||||
-- Determine if the set has radar targets. If it does, construct a SEAD task.
|
||||
local GroundUnitCount = DetectedSet:HasGroundUnits()
|
||||
local FriendliesNearBy = self.Detection:IsFriendliesNearBy( DetectedArea )
|
||||
|
||||
if GroundUnitCount > 0 and FriendliesNearBy == true then
|
||||
|
||||
-- Copy the Set
|
||||
local TargetSetUnit = SET_UNIT:New()
|
||||
TargetSetUnit:SetDatabase( DetectedSet )
|
||||
TargetSetUnit:FilterOnce() -- Filter but don't do any events!!! Elements are added manually upon each detection.
|
||||
|
||||
return TargetSetUnit
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Creates a BAI task when there are targets for it.
|
||||
-- @param #DETECTION_DISPATCHER self
|
||||
-- @param Detection#DETECTION_AREAS.DetectedArea DetectedArea
|
||||
-- @return Task#TASK_BASE
|
||||
function DETECTION_DISPATCHER:EvaluateBAI( DetectedArea, FriendlyCoalition )
|
||||
self:F( { DetectedArea.AreaID } )
|
||||
|
||||
local DetectedSet = DetectedArea.Set
|
||||
local DetectedZone = DetectedArea.Zone
|
||||
|
||||
|
||||
-- Determine if the set has radar targets. If it does, construct a SEAD task.
|
||||
local GroundUnitCount = DetectedSet:HasGroundUnits()
|
||||
local FriendliesNearBy = self.Detection:IsFriendliesNearBy( DetectedArea )
|
||||
|
||||
if GroundUnitCount > 0 and FriendliesNearBy == false then
|
||||
|
||||
-- Copy the Set
|
||||
local TargetSetUnit = SET_UNIT:New()
|
||||
TargetSetUnit:SetDatabase( DetectedSet )
|
||||
TargetSetUnit:FilterOnce() -- Filter but don't do any events!!! Elements are added manually upon each detection.
|
||||
|
||||
return TargetSetUnit
|
||||
end
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Evaluates the removal of the Task from the Mission.
|
||||
-- Can only occur when the DetectedArea is Changed AND the state of the Task is "Planned".
|
||||
-- @param #DETECTION_DISPATCHER self
|
||||
-- @param Mission#MISSION Mission
|
||||
-- @param Task#TASK_BASE Task
|
||||
-- @param Detection#DETECTION_AREAS.DetectedArea DetectedArea
|
||||
-- @return Task#TASK_BASE
|
||||
function DETECTION_DISPATCHER:EvaluateRemoveTask( Mission, Task, DetectedArea )
|
||||
|
||||
if Task then
|
||||
if Task:IsStatePlanned() and DetectedArea.Changed == true then
|
||||
Mission:RemoveTaskMenu( Task )
|
||||
Task = Mission:RemoveTask( Task )
|
||||
end
|
||||
end
|
||||
|
||||
return Task
|
||||
end
|
||||
|
||||
|
||||
--- Assigns tasks in relation to the detected items to the @{Set#SET_GROUP}.
|
||||
-- @param #DETECTION_DISPATCHER self
|
||||
-- @param Detection#DETECTION_AREAS Detection The detection created by the @{Detection#DETECTION_AREAS} object.
|
||||
-- @return #boolean Return true if you want the task assigning to continue... false will cancel the loop.
|
||||
function DETECTION_DISPATCHER:ProcessDetected( Detection )
|
||||
self:F2()
|
||||
|
||||
local AreaMsg = {}
|
||||
local TaskMsg = {}
|
||||
local ChangeMsg = {}
|
||||
|
||||
local Mission = self.Mission
|
||||
|
||||
--- First we need to the detected targets.
|
||||
for DetectedAreaID, DetectedAreaData in ipairs( Detection:GetDetectedAreas() ) do
|
||||
|
||||
local DetectedArea = DetectedAreaData -- Detection#DETECTION_AREAS.DetectedArea
|
||||
local DetectedSet = DetectedArea.Set
|
||||
local DetectedZone = DetectedArea.Zone
|
||||
self:E( { "Targets in DetectedArea", DetectedArea.AreaID, DetectedSet:Count(), tostring( DetectedArea ) } )
|
||||
DetectedSet:Flush()
|
||||
|
||||
local AreaID = DetectedArea.AreaID
|
||||
|
||||
-- Evaluate SEAD Tasking
|
||||
local SEADTask = Mission:GetTask( "SEAD." .. AreaID )
|
||||
SEADTask = self:EvaluateRemoveTask( Mission, SEADTask, DetectedArea )
|
||||
if not SEADTask then
|
||||
local TargetSetUnit = self:EvaluateSEAD( DetectedArea ) -- Returns a SetUnit if there are targets to be SEADed...
|
||||
if TargetSetUnit then
|
||||
SEADTask = Mission:AddTask( TASK_SEAD:New( Mission, self.SetGroup, "SEAD." .. AreaID, TargetSetUnit , DetectedZone ) ):StatePlanned()
|
||||
end
|
||||
end
|
||||
if SEADTask and SEADTask:IsStatePlanned() then
|
||||
SEADTask:SetPlannedMenu()
|
||||
TaskMsg[#TaskMsg+1] = " - " .. SEADTask:GetStateString() .. " SEAD " .. AreaID .. " - " .. SEADTask.TargetSetUnit:GetUnitTypesText()
|
||||
end
|
||||
|
||||
-- Evaluate CAS Tasking
|
||||
local CASTask = Mission:GetTask( "CAS." .. AreaID )
|
||||
CASTask = self:EvaluateRemoveTask( Mission, CASTask, DetectedArea )
|
||||
if not CASTask then
|
||||
local TargetSetUnit = self:EvaluateCAS( DetectedArea ) -- Returns a SetUnit if there are targets to be SEADed...
|
||||
if TargetSetUnit then
|
||||
CASTask = Mission:AddTask( TASK_A2G:New( Mission, self.SetGroup, "CAS." .. AreaID, "CAS", TargetSetUnit , DetectedZone, DetectedArea.NearestFAC ) ):StatePlanned()
|
||||
end
|
||||
end
|
||||
if CASTask and CASTask:IsStatePlanned() then
|
||||
CASTask:SetPlannedMenu()
|
||||
TaskMsg[#TaskMsg+1] = " - " .. CASTask:GetStateString() .. " CAS " .. AreaID .. " - " .. CASTask.TargetSetUnit:GetUnitTypesText()
|
||||
end
|
||||
|
||||
-- Evaluate BAI Tasking
|
||||
local BAITask = Mission:GetTask( "BAI." .. AreaID )
|
||||
BAITask = self:EvaluateRemoveTask( Mission, BAITask, DetectedArea )
|
||||
if not BAITask then
|
||||
local TargetSetUnit = self:EvaluateBAI( DetectedArea, self.CommandCenter:GetCoalition() ) -- Returns a SetUnit if there are targets to be SEADed...
|
||||
if TargetSetUnit then
|
||||
BAITask = Mission:AddTask( TASK_A2G:New( Mission, self.SetGroup, "BAI." .. AreaID, "BAI", TargetSetUnit , DetectedZone, DetectedArea.NearestFAC ) ):StatePlanned()
|
||||
end
|
||||
end
|
||||
if BAITask and BAITask:IsStatePlanned() then
|
||||
BAITask:SetPlannedMenu()
|
||||
TaskMsg[#TaskMsg+1] = " - " .. BAITask:GetStateString() .. " BAI " .. AreaID .. " - " .. BAITask.TargetSetUnit:GetUnitTypesText()
|
||||
end
|
||||
|
||||
if #TaskMsg > 0 then
|
||||
|
||||
local ThreatLevel = Detection:GetTreatLevelA2G( DetectedArea )
|
||||
|
||||
local DetectedAreaVec3 = DetectedZone:GetVec3()
|
||||
local DetectedAreaPointVec3 = POINT_VEC3:New( DetectedAreaVec3.x, DetectedAreaVec3.y, DetectedAreaVec3.z )
|
||||
local DetectedAreaPointLL = DetectedAreaPointVec3:ToStringLL( 3, true )
|
||||
AreaMsg[#AreaMsg+1] = string.format( " - Area #%d - %s - Threat Level [%s] (%2d)",
|
||||
DetectedAreaID,
|
||||
DetectedAreaPointLL,
|
||||
string.rep( "■", ThreatLevel ),
|
||||
ThreatLevel
|
||||
)
|
||||
|
||||
-- Loop through the changes ...
|
||||
local ChangeText = Detection:GetChangeText( DetectedArea )
|
||||
|
||||
if ChangeText ~= "" then
|
||||
ChangeMsg[#ChangeMsg+1] = string.gsub( string.gsub( ChangeText, "\n", "%1 - " ), "^.", " - %1" )
|
||||
end
|
||||
end
|
||||
|
||||
-- OK, so the tasking has been done, now delete the changes reported for the area.
|
||||
Detection:AcceptChanges( DetectedArea )
|
||||
|
||||
end
|
||||
|
||||
if #AreaMsg > 0 then
|
||||
for TaskGroupID, TaskGroup in pairs( self.SetGroup:GetSet() ) do
|
||||
if not TaskGroup:GetState( TaskGroup, "Assigned" ) then
|
||||
self.CommandCenter:MessageToGroup(
|
||||
string.format( "HQ Reporting - Target areas for mission '%s':\nAreas:\n%s\n\nTasks:\n%s\n\nChanges:\n%s ",
|
||||
self.Mission:GetName(),
|
||||
table.concat( AreaMsg, "\n" ),
|
||||
table.concat( TaskMsg, "\n" ),
|
||||
table.concat( ChangeMsg, "\n" )
|
||||
), self:GetReportDisplayTime(), TaskGroup
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
end
|
||||
738
Moose Development/Moose/Tasking/Mission.lua
Normal file
738
Moose Development/Moose/Tasking/Mission.lua
Normal file
@@ -0,0 +1,738 @@
|
||||
--- A MISSION is the main owner of a Mission orchestration within MOOSE . The Mission framework orchestrates @{CLIENT}s, @{TASK}s, @{STAGE}s etc.
|
||||
-- A @{CLIENT} needs to be registered within the @{MISSION} through the function @{AddClient}. A @{TASK} needs to be registered within the @{MISSION} through the function @{AddTask}.
|
||||
-- @module Mission
|
||||
|
||||
--- The MISSION class
|
||||
-- @type MISSION
|
||||
-- @extends Base#BASE
|
||||
-- @field #MISSION.Clients _Clients
|
||||
-- @field Menu#MENU_COALITION MissionMenu
|
||||
-- @field #string MissionBriefing
|
||||
MISSION = {
|
||||
ClassName = "MISSION",
|
||||
Name = "",
|
||||
MissionStatus = "PENDING",
|
||||
_Clients = {},
|
||||
Tasks = {},
|
||||
TaskMenus = {},
|
||||
TaskCategoryMenus = {},
|
||||
TaskTypeMenus = {},
|
||||
_ActiveTasks = {},
|
||||
GoalFunction = nil,
|
||||
MissionReportTrigger = 0,
|
||||
MissionProgressTrigger = 0,
|
||||
MissionReportShow = false,
|
||||
MissionReportFlash = false,
|
||||
MissionTimeInterval = 0,
|
||||
MissionCoalition = "",
|
||||
SUCCESS = 1,
|
||||
FAILED = 2,
|
||||
REPEAT = 3,
|
||||
_GoalTasks = {}
|
||||
}
|
||||
|
||||
--- @type MISSION.Clients
|
||||
-- @list <Client#CLIENTS>
|
||||
|
||||
function MISSION:Meta()
|
||||
|
||||
local self = BASE:Inherit( self, BASE:New() )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- This is the main MISSION declaration method. Each Mission is like the master or a Mission orchestration between, Clients, Tasks, Stages etc.
|
||||
-- @param #MISSION self
|
||||
-- @param #string MissionName is the name of the mission. This name will be used to reference the status of each mission by the players.
|
||||
-- @param #string MissionPriority is a string indicating the "priority" of the Mission. f.e. "Primary", "Secondary" or "First", "Second". It is free format and up to the Mission designer to choose. There are no rules behind this field.
|
||||
-- @param #string MissionBriefing is a string indicating the mission briefing to be shown when a player joins a @{CLIENT}.
|
||||
-- @param DCSCoalitionObject#coalition MissionCoalition is a string indicating the coalition or party to which this mission belongs to. It is free format and can be chosen freely by the mission designer. Note that this field is not to be confused with the coalition concept of the ME. Examples of a Mission Coalition could be "NATO", "CCCP", "Intruders", "Terrorists"...
|
||||
-- @return #MISSION self
|
||||
function MISSION:New( MissionName, MissionPriority, MissionBriefing, MissionCoalition )
|
||||
|
||||
self = MISSION:Meta()
|
||||
self:T( { MissionName, MissionPriority, MissionBriefing, MissionCoalition } )
|
||||
|
||||
self.Name = MissionName
|
||||
self.MissionPriority = MissionPriority
|
||||
self.MissionBriefing = MissionBriefing
|
||||
self.MissionCoalition = MissionCoalition
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Gets the mission name.
|
||||
-- @param #MISSION self
|
||||
-- @return #MISSION self
|
||||
function MISSION:GetName()
|
||||
return self.Name
|
||||
end
|
||||
|
||||
--- Add a scoring to the mission.
|
||||
-- @param #MISSION self
|
||||
-- @return #MISSION self
|
||||
function MISSION:AddScoring( Scoring )
|
||||
self.Scoring = Scoring
|
||||
return self
|
||||
end
|
||||
|
||||
--- Get the scoring object of a mission.
|
||||
-- @param #MISSION self
|
||||
-- @return #SCORING Scoring
|
||||
function MISSION:GetScoring()
|
||||
return self.Scoring
|
||||
end
|
||||
|
||||
|
||||
--- Sets the Planned Task menu.
|
||||
-- @param #MISSION self
|
||||
function MISSION:SetPlannedMenu()
|
||||
|
||||
for _, Task in pairs( self.Tasks ) do
|
||||
local Task = Task -- Task#TASK_BASE
|
||||
Task:RemoveMenu()
|
||||
Task:SetPlannedMenu()
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
--- Sets the Assigned Task menu.
|
||||
-- @param #MISSION self
|
||||
-- @param Task#TASK_BASE Task
|
||||
-- @param #string MenuText The menu text.
|
||||
-- @return #MISSION self
|
||||
function MISSION:SetAssignedMenu( Task )
|
||||
|
||||
for _, Task in pairs( self.Tasks ) do
|
||||
local Task = Task -- Task#TASK_BASE
|
||||
Task:RemoveMenu()
|
||||
Task:SetAssignedMenu()
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
--- Removes a Task menu.
|
||||
-- @param #MISSION self
|
||||
-- @param Task#TASK_BASE Task
|
||||
-- @return #MISSION self
|
||||
function MISSION:RemoveTaskMenu( Task )
|
||||
|
||||
Task:RemoveMenu()
|
||||
end
|
||||
|
||||
|
||||
--- Gets the mission menu for the coalition.
|
||||
-- @param #MISSION self
|
||||
-- @param Group#GROUP TaskGroup
|
||||
-- @return Menu#MENU_COALITION self
|
||||
function MISSION:GetMissionMenu( TaskGroup )
|
||||
local TaskGroupName = TaskGroup:GetName()
|
||||
return self.MenuMission[TaskGroupName]
|
||||
end
|
||||
|
||||
|
||||
--- Clears the mission menu for the coalition.
|
||||
-- @param #MISSION self
|
||||
-- @return #MISSION self
|
||||
function MISSION:ClearMissionMenu()
|
||||
self.MissionMenu:Remove()
|
||||
self.MissionMenu = nil
|
||||
end
|
||||
|
||||
--- Get the TASK identified by the TaskNumber from the Mission. This function is useful in GoalFunctions.
|
||||
-- @param #string TaskIndex is the Index of the @{Task} within the @{Mission}.
|
||||
-- @param #number TaskID is the ID of the @{Task} within the @{Mission}.
|
||||
-- @return Task#TASK_BASE The Task
|
||||
-- @return #nil Returns nil if no task was found.
|
||||
function MISSION:GetTask( TaskName )
|
||||
self:F( { TaskName } )
|
||||
|
||||
return self.Tasks[TaskName]
|
||||
end
|
||||
|
||||
|
||||
--- Register a @{Task} to be completed within the @{Mission}.
|
||||
-- Note that there can be multiple @{Task}s registered to be completed.
|
||||
-- Each Task can be set a certain Goals. The Mission will not be completed until all Goals are reached.
|
||||
-- @param #MISSION self
|
||||
-- @param Task#TASK_BASE Task is the @{Task} object.
|
||||
-- @return Task#TASK_BASE The task added.
|
||||
function MISSION:AddTask( Task )
|
||||
|
||||
local TaskName = Task:GetTaskName()
|
||||
self:F( TaskName )
|
||||
self.Tasks[TaskName] = self.Tasks[TaskName] or { n = 0 }
|
||||
|
||||
self.Tasks[TaskName] = Task
|
||||
|
||||
return Task
|
||||
end
|
||||
|
||||
--- Removes a @{Task} to be completed within the @{Mission}.
|
||||
-- Note that there can be multiple @{Task}s registered to be completed.
|
||||
-- Each Task can be set a certain Goals. The Mission will not be completed until all Goals are reached.
|
||||
-- @param #MISSION self
|
||||
-- @param Task#TASK_BASE Task is the @{Task} object.
|
||||
-- @return #nil The cleaned Task reference.
|
||||
function MISSION:RemoveTask( Task )
|
||||
|
||||
local TaskName = Task:GetTaskName()
|
||||
self:F( TaskName )
|
||||
self.Tasks[TaskName] = self.Tasks[TaskName] or { n = 0 }
|
||||
|
||||
Task:CleanUp() -- Cleans all events and sets task to nil to get Garbage Collected
|
||||
|
||||
-- Ensure everything gets garbarge collected.
|
||||
self.Tasks[TaskName] = nil
|
||||
Task = nil
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Return the next @{Task} ID to be completed within the @{Mission}.
|
||||
-- @param #MISSION self
|
||||
-- @param Task#TASK_BASE Task is the @{Task} object.
|
||||
-- @return Task#TASK_BASE The task added.
|
||||
function MISSION:GetNextTaskID( Task )
|
||||
|
||||
local TaskName = Task:GetTaskName()
|
||||
self:F( TaskName )
|
||||
self.Tasks[TaskName] = self.Tasks[TaskName] or { n = 0 }
|
||||
|
||||
self.Tasks[TaskName].n = self.Tasks[TaskName].n + 1
|
||||
|
||||
return self.Tasks[TaskName].n
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- old stuff
|
||||
|
||||
--- Returns if a Mission has completed.
|
||||
-- @return bool
|
||||
function MISSION:IsCompleted()
|
||||
self:F()
|
||||
return self.MissionStatus == "ACCOMPLISHED"
|
||||
end
|
||||
|
||||
--- Set a Mission to completed.
|
||||
function MISSION:Completed()
|
||||
self:F()
|
||||
self.MissionStatus = "ACCOMPLISHED"
|
||||
self:StatusToClients()
|
||||
end
|
||||
|
||||
--- Returns if a Mission is ongoing.
|
||||
-- treturn bool
|
||||
function MISSION:IsOngoing()
|
||||
self:F()
|
||||
return self.MissionStatus == "ONGOING"
|
||||
end
|
||||
|
||||
--- Set a Mission to ongoing.
|
||||
function MISSION:Ongoing()
|
||||
self:F()
|
||||
self.MissionStatus = "ONGOING"
|
||||
--self:StatusToClients()
|
||||
end
|
||||
|
||||
--- Returns if a Mission is pending.
|
||||
-- treturn bool
|
||||
function MISSION:IsPending()
|
||||
self:F()
|
||||
return self.MissionStatus == "PENDING"
|
||||
end
|
||||
|
||||
--- Set a Mission to pending.
|
||||
function MISSION:Pending()
|
||||
self:F()
|
||||
self.MissionStatus = "PENDING"
|
||||
self:StatusToClients()
|
||||
end
|
||||
|
||||
--- Returns if a Mission has failed.
|
||||
-- treturn bool
|
||||
function MISSION:IsFailed()
|
||||
self:F()
|
||||
return self.MissionStatus == "FAILED"
|
||||
end
|
||||
|
||||
--- Set a Mission to failed.
|
||||
function MISSION:Failed()
|
||||
self:F()
|
||||
self.MissionStatus = "FAILED"
|
||||
self:StatusToClients()
|
||||
end
|
||||
|
||||
--- Send the status of the MISSION to all Clients.
|
||||
function MISSION:StatusToClients()
|
||||
self:F()
|
||||
if self.MissionReportFlash then
|
||||
for ClientID, Client in pairs( self._Clients ) do
|
||||
Client:Message( self.MissionCoalition .. ' "' .. self.Name .. '": ' .. self.MissionStatus .. '! ( ' .. self.MissionPriority .. ' mission ) ', 10, "Mission Command: Mission Status")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Handles the reporting. After certain time intervals, a MISSION report MESSAGE will be shown to All Players.
|
||||
function MISSION:ReportTrigger()
|
||||
self:F()
|
||||
|
||||
if self.MissionReportShow == true then
|
||||
self.MissionReportShow = false
|
||||
return true
|
||||
else
|
||||
if self.MissionReportFlash == true then
|
||||
if timer.getTime() >= self.MissionReportTrigger then
|
||||
self.MissionReportTrigger = timer.getTime() + self.MissionTimeInterval
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Report the status of all MISSIONs to all active Clients.
|
||||
function MISSION:ReportToAll()
|
||||
self:F()
|
||||
|
||||
local AlivePlayers = ''
|
||||
for ClientID, Client in pairs( self._Clients ) do
|
||||
if Client:GetDCSGroup() then
|
||||
if Client:GetClientGroupDCSUnit() then
|
||||
if Client:GetClientGroupDCSUnit():getLife() > 0.0 then
|
||||
if AlivePlayers == '' then
|
||||
AlivePlayers = ' Players: ' .. Client:GetClientGroupDCSUnit():getPlayerName()
|
||||
else
|
||||
AlivePlayers = AlivePlayers .. ' / ' .. Client:GetClientGroupDCSUnit():getPlayerName()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local Tasks = self:GetTasks()
|
||||
local TaskText = ""
|
||||
for TaskID, TaskData in pairs( Tasks ) do
|
||||
TaskText = TaskText .. " - Task " .. TaskID .. ": " .. TaskData.Name .. ": " .. TaskData:GetGoalProgress() .. "\n"
|
||||
end
|
||||
MESSAGE:New( self.MissionCoalition .. ' "' .. self.Name .. '": ' .. self.MissionStatus .. ' ( ' .. self.MissionPriority .. ' mission )' .. AlivePlayers .. "\n" .. TaskText:gsub("\n$",""), 10, "Mission Command: Mission Report" ):ToAll()
|
||||
end
|
||||
|
||||
|
||||
--- Add a goal function to a MISSION. Goal functions are called when a @{TASK} within a mission has been completed.
|
||||
-- @param function GoalFunction is the function defined by the mission designer to evaluate whether a certain goal has been reached after a @{TASK} finishes within the @{MISSION}. A GoalFunction must accept 2 parameters: Mission, Client, which contains the current MISSION object and the current CLIENT object respectively.
|
||||
-- @usage
|
||||
-- PatriotActivation = {
|
||||
-- { "US SAM Patriot Zerti", false },
|
||||
-- { "US SAM Patriot Zegduleti", false },
|
||||
-- { "US SAM Patriot Gvleti", false }
|
||||
-- }
|
||||
--
|
||||
-- function DeployPatriotTroopsGoal( Mission, Client )
|
||||
--
|
||||
--
|
||||
-- -- Check if the cargo is all deployed for mission success.
|
||||
-- for CargoID, CargoData in pairs( Mission._Cargos ) do
|
||||
-- if Group.getByName( CargoData.CargoGroupName ) then
|
||||
-- CargoGroup = Group.getByName( CargoData.CargoGroupName )
|
||||
-- if CargoGroup then
|
||||
-- -- Check if the cargo is ready to activate
|
||||
-- CurrentLandingZoneID = routines.IsUnitInZones( CargoGroup:getUnits()[1], Mission:GetTask( 2 ).LandingZones ) -- The second task is the Deploytask to measure mission success upon
|
||||
-- if CurrentLandingZoneID then
|
||||
-- if PatriotActivation[CurrentLandingZoneID][2] == false then
|
||||
-- -- Now check if this is a new Mission Task to be completed...
|
||||
-- trigger.action.setGroupAIOn( Group.getByName( PatriotActivation[CurrentLandingZoneID][1] ) )
|
||||
-- PatriotActivation[CurrentLandingZoneID][2] = true
|
||||
-- MessageToBlue( "Mission Command: Message to all airborne units! The " .. PatriotActivation[CurrentLandingZoneID][1] .. " is armed. Our air defenses are now stronger.", 60, "BLUE/PatriotDefense" )
|
||||
-- MessageToRed( "Mission Command: Our satellite systems are detecting additional NATO air defenses. To all airborne units: Take care!!!", 60, "RED/PatriotDefense" )
|
||||
-- Mission:GetTask( 2 ):AddGoalCompletion( "Patriots activated", PatriotActivation[CurrentLandingZoneID][1], 1 ) -- Register Patriot activation as part of mission goal.
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
-- end
|
||||
--
|
||||
-- local Mission = MISSIONSCHEDULER.AddMission( 'NATO Transport Troops', 'Operational', 'Transport 3 groups of air defense engineers from our barracks "Gold" and "Titan" to each patriot battery control center to activate our air defenses.', 'NATO' )
|
||||
-- Mission:AddGoalFunction( DeployPatriotTroopsGoal )
|
||||
function MISSION:AddGoalFunction( GoalFunction )
|
||||
self:F()
|
||||
self.GoalFunction = GoalFunction
|
||||
end
|
||||
|
||||
--- Register a new @{CLIENT} to participate within the mission.
|
||||
-- @param CLIENT Client is the @{CLIENT} object. The object must have been instantiated with @{CLIENT:New}.
|
||||
-- @return CLIENT
|
||||
-- @usage
|
||||
-- Add a number of Client objects to the Mission.
|
||||
-- Mission:AddClient( CLIENT:FindByName( 'US UH-1H*HOT-Deploy Troops 1', 'Transport 3 groups of air defense engineers from our barracks "Gold" and "Titan" to each patriot battery control center to activate our air defenses.' ):Transport() )
|
||||
-- Mission:AddClient( CLIENT:FindByName( 'US UH-1H*RAMP-Deploy Troops 3', 'Transport 3 groups of air defense engineers from our barracks "Gold" and "Titan" to each patriot battery control center to activate our air defenses.' ):Transport() )
|
||||
-- Mission:AddClient( CLIENT:FindByName( 'US UH-1H*HOT-Deploy Troops 2', 'Transport 3 groups of air defense engineers from our barracks "Gold" and "Titan" to each patriot battery control center to activate our air defenses.' ):Transport() )
|
||||
-- Mission:AddClient( CLIENT:FindByName( 'US UH-1H*RAMP-Deploy Troops 4', 'Transport 3 groups of air defense engineers from our barracks "Gold" and "Titan" to each patriot battery control center to activate our air defenses.' ):Transport() )
|
||||
function MISSION:AddClient( Client )
|
||||
self:F( { Client } )
|
||||
|
||||
local Valid = true
|
||||
|
||||
if Valid then
|
||||
self._Clients[Client.ClientName] = Client
|
||||
end
|
||||
|
||||
return Client
|
||||
end
|
||||
|
||||
--- Find a @{CLIENT} object within the @{MISSION} by its ClientName.
|
||||
-- @param CLIENT ClientName is a string defining the Client Group as defined within the ME.
|
||||
-- @return CLIENT
|
||||
-- @usage
|
||||
-- -- Seach for Client "Bomber" within the Mission.
|
||||
-- local BomberClient = Mission:FindClient( "Bomber" )
|
||||
function MISSION:FindClient( ClientName )
|
||||
self:F( { self._Clients[ClientName] } )
|
||||
return self._Clients[ClientName]
|
||||
end
|
||||
|
||||
|
||||
--- Get all the TASKs from the Mission. This function is useful in GoalFunctions.
|
||||
-- @return {TASK,...} Structure of TASKS with the @{TASK} number as the key.
|
||||
-- @usage
|
||||
-- -- Get Tasks from the Mission.
|
||||
-- Tasks = Mission:GetTasks()
|
||||
-- env.info( "Task 2 Completion = " .. Tasks[2]:GetGoalPercentage() .. "%" )
|
||||
function MISSION:GetTasks()
|
||||
self:F()
|
||||
|
||||
return self._Tasks
|
||||
end
|
||||
|
||||
|
||||
--[[
|
||||
_TransportExecuteStage: Defines the different stages of Transport unload/load execution. This table is internal and is used to control the validity of Transport load/unload timing.
|
||||
|
||||
- _TransportExecuteStage.EXECUTING
|
||||
- _TransportExecuteStage.SUCCESS
|
||||
- _TransportExecuteStage.FAILED
|
||||
|
||||
--]]
|
||||
_TransportExecuteStage = {
|
||||
NONE = 0,
|
||||
EXECUTING = 1,
|
||||
SUCCESS = 2,
|
||||
FAILED = 3
|
||||
}
|
||||
|
||||
|
||||
--- The MISSIONSCHEDULER is an OBJECT and is the main scheduler of ALL active MISSIONs registered within this scheduler. It's workings are considered internal and is automatically created when the Mission.lua file is included.
|
||||
-- @type MISSIONSCHEDULER
|
||||
-- @field #MISSIONSCHEDULER.MISSIONS Missions
|
||||
MISSIONSCHEDULER = {
|
||||
Missions = {},
|
||||
MissionCount = 0,
|
||||
TimeIntervalCount = 0,
|
||||
TimeIntervalShow = 150,
|
||||
TimeSeconds = 14400,
|
||||
TimeShow = 5
|
||||
}
|
||||
|
||||
--- @type MISSIONSCHEDULER.MISSIONS
|
||||
-- @list <#MISSION> Mission
|
||||
|
||||
--- This is the main MISSIONSCHEDULER Scheduler function. It is considered internal and is automatically created when the Mission.lua file is included.
|
||||
function MISSIONSCHEDULER.Scheduler()
|
||||
|
||||
|
||||
-- loop through the missions in the TransportTasks
|
||||
for MissionName, MissionData in pairs( MISSIONSCHEDULER.Missions ) do
|
||||
|
||||
local Mission = MissionData -- #MISSION
|
||||
|
||||
if not Mission:IsCompleted() then
|
||||
|
||||
-- This flag will monitor if for this mission, there are clients alive. If this flag is still false at the end of the loop, the mission status will be set to Pending (if not Failed or Completed).
|
||||
local ClientsAlive = false
|
||||
|
||||
for ClientID, ClientData in pairs( Mission._Clients ) do
|
||||
|
||||
local Client = ClientData -- Client#CLIENT
|
||||
|
||||
if Client:IsAlive() then
|
||||
|
||||
-- There is at least one Client that is alive... So the Mission status is set to Ongoing.
|
||||
ClientsAlive = true
|
||||
|
||||
-- If this Client was not registered as Alive before:
|
||||
-- 1. We register the Client as Alive.
|
||||
-- 2. We initialize the Client Tasks and make a link to the original Mission Task.
|
||||
-- 3. We initialize the Cargos.
|
||||
-- 4. We flag the Mission as Ongoing.
|
||||
if not Client.ClientAlive then
|
||||
Client.ClientAlive = true
|
||||
Client.ClientBriefingShown = false
|
||||
for TaskNumber, Task in pairs( Mission._Tasks ) do
|
||||
-- Note that this a deepCopy. Each client must have their own Tasks with own Stages!!!
|
||||
Client._Tasks[TaskNumber] = routines.utils.deepCopy( Mission._Tasks[TaskNumber] )
|
||||
-- Each MissionTask must point to the original Mission.
|
||||
Client._Tasks[TaskNumber].MissionTask = Mission._Tasks[TaskNumber]
|
||||
Client._Tasks[TaskNumber].Cargos = Mission._Tasks[TaskNumber].Cargos
|
||||
Client._Tasks[TaskNumber].LandingZones = Mission._Tasks[TaskNumber].LandingZones
|
||||
end
|
||||
|
||||
Mission:Ongoing()
|
||||
end
|
||||
|
||||
|
||||
-- For each Client, check for each Task the state and evolve the mission.
|
||||
-- This flag will indicate if the Task of the Client is Complete.
|
||||
local TaskComplete = false
|
||||
|
||||
for TaskNumber, Task in pairs( Client._Tasks ) do
|
||||
|
||||
if not Task.Stage then
|
||||
Task:SetStage( 1 )
|
||||
end
|
||||
|
||||
|
||||
local TransportTime = timer.getTime()
|
||||
|
||||
if not Task:IsDone() then
|
||||
|
||||
if Task:Goal() then
|
||||
Task:ShowGoalProgress( Mission, Client )
|
||||
end
|
||||
|
||||
--env.info( 'Scheduler: Mission = ' .. Mission.Name .. ' / Client = ' .. Client.ClientName .. ' / Task = ' .. Task.Name .. ' / Stage = ' .. Task.ActiveStage .. ' - ' .. Task.Stage.Name .. ' - ' .. Task.Stage.StageType )
|
||||
|
||||
-- Action
|
||||
if Task:StageExecute() then
|
||||
Task.Stage:Execute( Mission, Client, Task )
|
||||
end
|
||||
|
||||
-- Wait until execution is finished
|
||||
if Task.ExecuteStage == _TransportExecuteStage.EXECUTING then
|
||||
Task.Stage:Executing( Mission, Client, Task )
|
||||
end
|
||||
|
||||
-- Validate completion or reverse to earlier stage
|
||||
if Task.Time + Task.Stage.WaitTime <= TransportTime then
|
||||
Task:SetStage( Task.Stage:Validate( Mission, Client, Task ) )
|
||||
end
|
||||
|
||||
if Task:IsDone() then
|
||||
--env.info( 'Scheduler: Mission '.. Mission.Name .. ' Task ' .. Task.Name .. ' Stage ' .. Task.Stage.Name .. ' done. TaskComplete = ' .. string.format ( "%s", TaskComplete and "true" or "false" ) )
|
||||
TaskComplete = true -- when a task is not yet completed, a mission cannot be completed
|
||||
|
||||
else
|
||||
-- break only if this task is not yet done, so that future task are not yet activated.
|
||||
TaskComplete = false -- when a task is not yet completed, a mission cannot be completed
|
||||
--env.info( 'Scheduler: Mission "'.. Mission.Name .. '" Task "' .. Task.Name .. '" Stage "' .. Task.Stage.Name .. '" break. TaskComplete = ' .. string.format ( "%s", TaskComplete and "true" or "false" ) )
|
||||
break
|
||||
end
|
||||
|
||||
if TaskComplete then
|
||||
|
||||
if Mission.GoalFunction ~= nil then
|
||||
Mission.GoalFunction( Mission, Client )
|
||||
end
|
||||
if MISSIONSCHEDULER.Scoring then
|
||||
MISSIONSCHEDULER.Scoring:_AddMissionTaskScore( Client:GetClientGroupDCSUnit(), Mission.Name, 25 )
|
||||
end
|
||||
|
||||
-- if not Mission:IsCompleted() then
|
||||
-- end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local MissionComplete = true
|
||||
for TaskNumber, Task in pairs( Mission._Tasks ) do
|
||||
if Task:Goal() then
|
||||
-- Task:ShowGoalProgress( Mission, Client )
|
||||
if Task:IsGoalReached() then
|
||||
else
|
||||
MissionComplete = false
|
||||
end
|
||||
else
|
||||
MissionComplete = false -- If there is no goal, the mission should never be ended. The goal status will be set somewhere else.
|
||||
end
|
||||
end
|
||||
|
||||
if MissionComplete then
|
||||
Mission:Completed()
|
||||
if MISSIONSCHEDULER.Scoring then
|
||||
MISSIONSCHEDULER.Scoring:_AddMissionScore( Mission.Name, 100 )
|
||||
end
|
||||
else
|
||||
if TaskComplete then
|
||||
-- Reset for new tasking of active client
|
||||
Client.ClientAlive = false -- Reset the client tasks.
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
else
|
||||
if Client.ClientAlive then
|
||||
env.info( 'Scheduler: Client "' .. Client.ClientName .. '" is inactive.' )
|
||||
Client.ClientAlive = false
|
||||
|
||||
-- This is tricky. If we sanitize Client._Tasks before sanitizing Client._Tasks[TaskNumber].MissionTask, then the original MissionTask will be sanitized, and will be lost within the garbage collector.
|
||||
-- So first sanitize Client._Tasks[TaskNumber].MissionTask, after that, sanitize only the whole _Tasks structure...
|
||||
--Client._Tasks[TaskNumber].MissionTask = nil
|
||||
--Client._Tasks = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- If all Clients of this Mission are not activated, then the Mission status needs to be put back into Pending status.
|
||||
-- But only if the Mission was Ongoing. In case the Mission is Completed or Failed, the Mission status may not be changed. In these cases, this will be the last run of this Mission in the Scheduler.
|
||||
if ClientsAlive == false then
|
||||
if Mission:IsOngoing() then
|
||||
-- Mission status back to pending...
|
||||
Mission:Pending()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Mission:StatusToClients()
|
||||
|
||||
if Mission:ReportTrigger() then
|
||||
Mission:ReportToAll()
|
||||
end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
--- Start the MISSIONSCHEDULER.
|
||||
function MISSIONSCHEDULER.Start()
|
||||
if MISSIONSCHEDULER ~= nil then
|
||||
--MISSIONSCHEDULER.SchedulerId = routines.scheduleFunction( MISSIONSCHEDULER.Scheduler, { }, 0, 2 )
|
||||
MISSIONSCHEDULER.SchedulerId = SCHEDULER:New( nil, MISSIONSCHEDULER.Scheduler, { }, 0, 2 )
|
||||
end
|
||||
end
|
||||
|
||||
--- Stop the MISSIONSCHEDULER.
|
||||
function MISSIONSCHEDULER.Stop()
|
||||
if MISSIONSCHEDULER.SchedulerId then
|
||||
routines.removeFunction(MISSIONSCHEDULER.SchedulerId)
|
||||
MISSIONSCHEDULER.SchedulerId = nil
|
||||
end
|
||||
end
|
||||
|
||||
--- This is the main MISSION declaration method. Each Mission is like the master or a Mission orchestration between, Clients, Tasks, Stages etc.
|
||||
-- @param Mission is the MISSION object instantiated by @{MISSION:New}.
|
||||
-- @return MISSION
|
||||
-- @usage
|
||||
-- -- Declare a mission.
|
||||
-- Mission = MISSION:New( 'Russia Transport Troops SA-6',
|
||||
-- 'Operational',
|
||||
-- 'Transport troops from the control center to one of the SA-6 SAM sites to activate their operation.',
|
||||
-- 'Russia' )
|
||||
-- MISSIONSCHEDULER:AddMission( Mission )
|
||||
function MISSIONSCHEDULER.AddMission( Mission )
|
||||
MISSIONSCHEDULER.Missions[Mission.Name] = Mission
|
||||
MISSIONSCHEDULER.MissionCount = MISSIONSCHEDULER.MissionCount + 1
|
||||
-- Add an overall AI Client for the AI tasks... This AI Client will facilitate the Events in the background for each Task.
|
||||
--MissionAdd:AddClient( CLIENT:Register( 'AI' ) )
|
||||
|
||||
return Mission
|
||||
end
|
||||
|
||||
--- Remove a MISSION from the MISSIONSCHEDULER.
|
||||
-- @param MissionName is the name of the MISSION given at declaration using @{AddMission}.
|
||||
-- @usage
|
||||
-- -- Declare a mission.
|
||||
-- Mission = MISSION:New( 'Russia Transport Troops SA-6',
|
||||
-- 'Operational',
|
||||
-- 'Transport troops from the control center to one of the SA-6 SAM sites to activate their operation.',
|
||||
-- 'Russia' )
|
||||
-- MISSIONSCHEDULER:AddMission( Mission )
|
||||
--
|
||||
-- -- Now remove the Mission.
|
||||
-- MISSIONSCHEDULER:RemoveMission( 'Russia Transport Troops SA-6' )
|
||||
function MISSIONSCHEDULER.RemoveMission( MissionName )
|
||||
MISSIONSCHEDULER.Missions[MissionName] = nil
|
||||
MISSIONSCHEDULER.MissionCount = MISSIONSCHEDULER.MissionCount - 1
|
||||
end
|
||||
|
||||
--- Find a MISSION within the MISSIONSCHEDULER.
|
||||
-- @param MissionName is the name of the MISSION given at declaration using @{AddMission}.
|
||||
-- @return MISSION
|
||||
-- @usage
|
||||
-- -- Declare a mission.
|
||||
-- Mission = MISSION:New( 'Russia Transport Troops SA-6',
|
||||
-- 'Operational',
|
||||
-- 'Transport troops from the control center to one of the SA-6 SAM sites to activate their operation.',
|
||||
-- 'Russia' )
|
||||
-- MISSIONSCHEDULER:AddMission( Mission )
|
||||
--
|
||||
-- -- Now find the Mission.
|
||||
-- MissionFind = MISSIONSCHEDULER:FindMission( 'Russia Transport Troops SA-6' )
|
||||
function MISSIONSCHEDULER.FindMission( MissionName )
|
||||
return MISSIONSCHEDULER.Missions[MissionName]
|
||||
end
|
||||
|
||||
-- Internal function used by the MISSIONSCHEDULER menu.
|
||||
function MISSIONSCHEDULER.ReportMissionsShow( )
|
||||
for MissionName, Mission in pairs( MISSIONSCHEDULER.Missions ) do
|
||||
Mission.MissionReportShow = true
|
||||
Mission.MissionReportFlash = false
|
||||
end
|
||||
end
|
||||
|
||||
-- Internal function used by the MISSIONSCHEDULER menu.
|
||||
function MISSIONSCHEDULER.ReportMissionsFlash( TimeInterval )
|
||||
local Count = 0
|
||||
for MissionName, Mission in pairs( MISSIONSCHEDULER.Missions ) do
|
||||
Mission.MissionReportShow = false
|
||||
Mission.MissionReportFlash = true
|
||||
Mission.MissionReportTrigger = timer.getTime() + Count * TimeInterval
|
||||
Mission.MissionTimeInterval = MISSIONSCHEDULER.MissionCount * TimeInterval
|
||||
env.info( "TimeInterval = " .. Mission.MissionTimeInterval )
|
||||
Count = Count + 1
|
||||
end
|
||||
end
|
||||
|
||||
-- Internal function used by the MISSIONSCHEDULER menu.
|
||||
function MISSIONSCHEDULER.ReportMissionsHide( Prm )
|
||||
for MissionName, Mission in pairs( MISSIONSCHEDULER.Missions ) do
|
||||
Mission.MissionReportShow = false
|
||||
Mission.MissionReportFlash = false
|
||||
end
|
||||
end
|
||||
|
||||
--- Enables a MENU option in the communications menu under F10 to control the status of the active missions.
|
||||
-- This function should be called only once when starting the MISSIONSCHEDULER.
|
||||
function MISSIONSCHEDULER.ReportMenu()
|
||||
local ReportMenu = SUBMENU:New( 'Status' )
|
||||
local ReportMenuShow = COMMANDMENU:New( 'Show Report Missions', ReportMenu, MISSIONSCHEDULER.ReportMissionsShow, 0 )
|
||||
local ReportMenuFlash = COMMANDMENU:New('Flash Report Missions', ReportMenu, MISSIONSCHEDULER.ReportMissionsFlash, 120 )
|
||||
local ReportMenuHide = COMMANDMENU:New( 'Hide Report Missions', ReportMenu, MISSIONSCHEDULER.ReportMissionsHide, 0 )
|
||||
end
|
||||
|
||||
--- Show the remaining mission time.
|
||||
function MISSIONSCHEDULER:TimeShow()
|
||||
self.TimeIntervalCount = self.TimeIntervalCount + 1
|
||||
if self.TimeIntervalCount >= self.TimeTriggerShow then
|
||||
local TimeMsg = string.format("%00d", ( self.TimeSeconds / 60 ) - ( timer.getTime() / 60 )) .. ' minutes left until mission reload.'
|
||||
MESSAGE:New( TimeMsg, self.TimeShow, "Mission time" ):ToAll()
|
||||
self.TimeIntervalCount = 0
|
||||
end
|
||||
end
|
||||
|
||||
function MISSIONSCHEDULER:Time( TimeSeconds, TimeIntervalShow, TimeShow )
|
||||
|
||||
self.TimeIntervalCount = 0
|
||||
self.TimeSeconds = TimeSeconds
|
||||
self.TimeIntervalShow = TimeIntervalShow
|
||||
self.TimeShow = TimeShow
|
||||
end
|
||||
|
||||
--- Adds a mission scoring to the game.
|
||||
function MISSIONSCHEDULER:Scoring( Scoring )
|
||||
|
||||
self.Scoring = Scoring
|
||||
end
|
||||
|
||||
910
Moose Development/Moose/Tasking/Task.lua
Normal file
910
Moose Development/Moose/Tasking/Task.lua
Normal file
@@ -0,0 +1,910 @@
|
||||
--- This module contains the TASK_BASE class.
|
||||
--
|
||||
-- 1) @{#TASK_BASE} class, extends @{Base#BASE}
|
||||
-- ============================================
|
||||
-- 1.1) The @{#TASK_BASE} class implements the methods for task orchestration within MOOSE.
|
||||
-- ----------------------------------------------------------------------------------------
|
||||
-- The class provides a couple of methods to:
|
||||
--
|
||||
-- * @{#TASK_BASE.AssignToGroup}():Assign a task to a group (of players).
|
||||
-- * @{#TASK_BASE.AddProcess}():Add a @{Process} to a task.
|
||||
-- * @{#TASK_BASE.RemoveProcesses}():Remove a running @{Process} from a running task.
|
||||
-- * @{#TASK_BASE.AddStateMachine}():Add a @{StateMachine} to a task.
|
||||
-- * @{#TASK_BASE.RemoveStateMachines}():Remove @{StateMachine}s from a task.
|
||||
-- * @{#TASK_BASE.HasStateMachine}():Enquire if the task has a @{StateMachine}
|
||||
-- * @{#TASK_BASE.AssignToUnit}(): Assign a task to a unit. (Needs to be implemented in the derived classes from @{#TASK_BASE}.
|
||||
-- * @{#TASK_BASE.UnAssignFromUnit}(): Unassign the task from a unit.
|
||||
--
|
||||
-- 1.2) Set and enquire task status (beyond the task state machine processing).
|
||||
-- ----------------------------------------------------------------------------
|
||||
-- A task needs to implement as a minimum the following task states:
|
||||
--
|
||||
-- * **Success**: Expresses the successful execution and finalization of the task.
|
||||
-- * **Failed**: Expresses the failure of a task.
|
||||
-- * **Planned**: Expresses that the task is created, but not yet in execution and is not assigned yet.
|
||||
-- * **Assigned**: Expresses that the task is assigned to a Group of players, and that the task is in execution mode.
|
||||
--
|
||||
-- A task may also implement the following task states:
|
||||
--
|
||||
-- * **Rejected**: Expresses that the task is rejected by a player, who was requested to accept the task.
|
||||
-- * **Cancelled**: Expresses that the task is cancelled by HQ or through a logical situation where a cancellation of the task is required.
|
||||
--
|
||||
-- A task can implement more statusses than the ones outlined above. Please consult the documentation of the specific tasks to understand the different status modelled.
|
||||
--
|
||||
-- The status of tasks can be set by the methods **State** followed by the task status. An example is `StateAssigned()`.
|
||||
-- The status of tasks can be enquired by the methods **IsState** followed by the task status name. An example is `if IsStateAssigned() then`.
|
||||
--
|
||||
-- 1.3) Add scoring when reaching a certain task status:
|
||||
-- -----------------------------------------------------
|
||||
-- Upon reaching a certain task status in a task, additional scoring can be given. If the Mission has a scoring system attached, the scores will be added to the mission scoring.
|
||||
-- Use the method @{#TASK_BASE.AddScore}() to add scores when a status is reached.
|
||||
--
|
||||
-- 1.4) Task briefing:
|
||||
-- -------------------
|
||||
-- A task briefing can be given that is shown to the player when he is assigned to the task.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- ### Authors: FlightControl - Design and Programming
|
||||
--
|
||||
-- @module Task
|
||||
|
||||
--- The TASK_BASE class
|
||||
-- @type TASK_BASE
|
||||
-- @field Scheduler#SCHEDULER TaskScheduler
|
||||
-- @field Mission#MISSION Mission
|
||||
-- @field StateMachine#STATEMACHINE Fsm
|
||||
-- @field Set#SET_GROUP SetGroup The Set of Groups assigned to the Task
|
||||
-- @extends Base#BASE
|
||||
TASK_BASE = {
|
||||
ClassName = "TASK_BASE",
|
||||
TaskScheduler = nil,
|
||||
ProcessClasses = {}, -- The container of the Process classes that will be used to create and assign new processes for the task to ProcessUnits.
|
||||
Processes = {}, -- The container of actual process objects instantiated and assigned to ProcessUnits.
|
||||
Players = nil,
|
||||
Scores = {},
|
||||
Menu = {},
|
||||
SetGroup = nil,
|
||||
}
|
||||
|
||||
--- Instantiates a new TASK_BASE. Should never be used. Interface Class.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Mission#MISSION The mission wherein the Task is registered.
|
||||
-- @param Set#SET_GROUP SetGroup The set of groups for which the Task can be assigned.
|
||||
-- @param #string TaskName The name of the Task
|
||||
-- @param #string TaskType The type of the Task
|
||||
-- @param #string TaskCategory The category of the Task (A2G, A2A, Transport, ... )
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:New( Mission, SetGroup, TaskName, TaskType, TaskCategory )
|
||||
|
||||
local self = BASE:Inherit( self, BASE:New() )
|
||||
self:E( "New TASK " .. TaskName )
|
||||
|
||||
self.Processes = {}
|
||||
self.Fsm = {}
|
||||
|
||||
self.Mission = Mission
|
||||
self.SetGroup = SetGroup
|
||||
|
||||
self:SetCategory( TaskCategory )
|
||||
self:SetType( TaskType )
|
||||
self:SetName( TaskName )
|
||||
self:SetID( Mission:GetNextTaskID( self ) ) -- The Mission orchestrates the task sequences ..
|
||||
|
||||
self.TaskBriefing = "You are assigned to the task: " .. self.TaskName .. "."
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Cleans all references of a TASK_BASE.
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #nil
|
||||
function TASK_BASE:CleanUp()
|
||||
|
||||
_EVENTDISPATCHER:OnPlayerLeaveRemove( self )
|
||||
_EVENTDISPATCHER:OnDeadRemove( self )
|
||||
_EVENTDISPATCHER:OnCrashRemove( self )
|
||||
_EVENTDISPATCHER:OnPilotDeadRemove( self )
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
--- Assign the @{Task}to a @{Group}.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Group#GROUP TaskGroup
|
||||
-- @return #TASK_BASE
|
||||
function TASK_BASE:AssignToGroup( TaskGroup )
|
||||
self:F2( TaskGroup:GetName() )
|
||||
|
||||
local TaskGroupName = TaskGroup:GetName()
|
||||
|
||||
TaskGroup:SetState( TaskGroup, "Assigned", self )
|
||||
|
||||
self:RemoveMenuForGroup( TaskGroup )
|
||||
self:SetAssignedMenuForGroup( TaskGroup )
|
||||
|
||||
local TaskUnits = TaskGroup:GetUnits()
|
||||
for UnitID, UnitData in pairs( TaskUnits ) do
|
||||
local TaskUnit = UnitData -- Unit#UNIT
|
||||
local PlayerName = TaskUnit:GetPlayerName()
|
||||
if PlayerName ~= nil or PlayerName ~= "" then
|
||||
self:AssignToUnit( TaskUnit )
|
||||
end
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Send the briefng message of the @{Task} to the assigned @{Group}s.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:SendBriefingToAssignedGroups()
|
||||
self:F2()
|
||||
|
||||
for TaskGroupName, TaskGroup in pairs( self.SetGroup:GetSet() ) do
|
||||
|
||||
if self:IsAssignedToGroup( TaskGroup ) then
|
||||
TaskGroup:Message( self.TaskBriefing, 60 )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
--- Assign the @{Task} from the @{Group}s.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:UnAssignFromGroups()
|
||||
self:F2()
|
||||
|
||||
for TaskGroupName, TaskGroup in pairs( self.SetGroup:GetSet() ) do
|
||||
|
||||
TaskGroup:SetState( TaskGroup, "Assigned", nil )
|
||||
local TaskUnits = TaskGroup:GetUnits()
|
||||
for UnitID, UnitData in pairs( TaskUnits ) do
|
||||
local TaskUnit = UnitData -- Unit#UNIT
|
||||
local PlayerName = TaskUnit:GetPlayerName()
|
||||
if PlayerName ~= nil or PlayerName ~= "" then
|
||||
self:UnAssignFromUnit( TaskUnit )
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Returns if the @{Task} is assigned to the Group.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Group#GROUP TaskGroup
|
||||
-- @return #boolean
|
||||
function TASK_BASE:IsAssignedToGroup( TaskGroup )
|
||||
|
||||
local TaskGroupName = TaskGroup:GetName()
|
||||
|
||||
if self:IsStateAssigned() then
|
||||
if TaskGroup:GetState( TaskGroup, "Assigned" ) == self then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
--- Assign the @{Task}to an alive @{Unit}.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:AssignToUnit( TaskUnit )
|
||||
self:F( TaskUnit:GetName() )
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
--- UnAssign the @{Task} from an alive @{Unit}.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:UnAssignFromUnit( TaskUnitName )
|
||||
self:F( TaskUnitName )
|
||||
|
||||
if self:HasStateMachine( TaskUnitName ) == true then
|
||||
self:RemoveStateMachines( TaskUnitName )
|
||||
self:RemoveProcesses( TaskUnitName )
|
||||
end
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Set the menu options of the @{Task} to all the groups in the SetGroup.
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:SetPlannedMenu()
|
||||
|
||||
local MenuText = self:GetPlannedMenuText()
|
||||
for TaskGroupID, TaskGroup in pairs( self.SetGroup:GetSet() ) do
|
||||
if not self:IsAssignedToGroup( TaskGroup ) then
|
||||
self:SetPlannedMenuForGroup( TaskGroup, MenuText )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Set the menu options of the @{Task} to all the groups in the SetGroup.
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:SetAssignedMenu()
|
||||
|
||||
for TaskGroupID, TaskGroup in pairs( self.SetGroup:GetSet() ) do
|
||||
if self:IsAssignedToGroup( TaskGroup ) then
|
||||
self:SetAssignedMenuForGroup( TaskGroup )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
--- Remove the menu options of the @{Task} to all the groups in the SetGroup.
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:RemoveMenu()
|
||||
|
||||
for TaskGroupID, TaskGroup in pairs( self.SetGroup:GetSet() ) do
|
||||
self:RemoveMenuForGroup( TaskGroup )
|
||||
end
|
||||
end
|
||||
|
||||
--- Set the planned menu option of the @{Task}.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Group#GROUP TaskGroup
|
||||
-- @param #string MenuText The menu text.
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:SetPlannedMenuForGroup( TaskGroup, MenuText )
|
||||
self:E( TaskGroup:GetName() )
|
||||
|
||||
local TaskMission = self.Mission:GetName()
|
||||
local TaskCategory = self:GetCategory()
|
||||
local TaskType = self:GetType()
|
||||
|
||||
local Mission = self.Mission
|
||||
|
||||
Mission.MenuMission = Mission.MenuMission or {}
|
||||
local MenuMission = Mission.MenuMission
|
||||
|
||||
Mission.MenuCategory = Mission.MenuCategory or {}
|
||||
local MenuCategory = Mission.MenuCategory
|
||||
|
||||
Mission.MenuType = Mission.MenuType or {}
|
||||
local MenuType = Mission.MenuType
|
||||
|
||||
self.Menu = self.Menu or {}
|
||||
local Menu = self.Menu
|
||||
|
||||
local TaskGroupName = TaskGroup:GetName()
|
||||
MenuMission[TaskGroupName] = MenuMission[TaskGroupName] or MENU_GROUP:New( TaskGroup, TaskMission, nil )
|
||||
|
||||
MenuCategory[TaskGroupName] = MenuCategory[TaskGroupName] or {}
|
||||
MenuCategory[TaskGroupName][TaskCategory] = MenuCategory[TaskGroupName][TaskCategory] or MENU_GROUP:New( TaskGroup, TaskCategory, MenuMission[TaskGroupName] )
|
||||
|
||||
MenuType[TaskGroupName] = MenuType[TaskGroupName] or {}
|
||||
MenuType[TaskGroupName][TaskType] = MenuType[TaskGroupName][TaskType] or MENU_GROUP:New( TaskGroup, TaskType, MenuCategory[TaskGroupName][TaskCategory] )
|
||||
|
||||
if Menu[TaskGroupName] then
|
||||
Menu[TaskGroupName]:Remove()
|
||||
end
|
||||
Menu[TaskGroupName] = MENU_GROUP_COMMAND:New( TaskGroup, MenuText, MenuType[TaskGroupName][TaskType], self.MenuAssignToGroup, { self = self, TaskGroup = TaskGroup } )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Set the assigned menu options of the @{Task}.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Group#GROUP TaskGroup
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:SetAssignedMenuForGroup( TaskGroup )
|
||||
self:E( TaskGroup:GetName() )
|
||||
|
||||
local TaskMission = self.Mission:GetName()
|
||||
|
||||
local Mission = self.Mission
|
||||
|
||||
Mission.MenuMission = Mission.MenuMission or {}
|
||||
local MenuMission = Mission.MenuMission
|
||||
|
||||
self.MenuStatus = self.MenuStatus or {}
|
||||
local MenuStatus = self.MenuStatus
|
||||
|
||||
|
||||
self.MenuAbort = self.MenuAbort or {}
|
||||
local MenuAbort = self.MenuAbort
|
||||
|
||||
local TaskGroupName = TaskGroup:GetName()
|
||||
MenuMission[TaskGroupName] = MenuMission[TaskGroupName] or MENU_GROUP:New( TaskGroup, TaskMission, nil )
|
||||
MenuStatus[TaskGroupName] = MENU_GROUP_COMMAND:New( TaskGroup, "Task Status", MenuMission[TaskGroupName], self.MenuTaskStatus, { self = self, TaskGroup = TaskGroup } )
|
||||
MenuAbort[TaskGroupName] = MENU_GROUP_COMMAND:New( TaskGroup, "Abort Task", MenuMission[TaskGroupName], self.MenuTaskAbort, { self = self, TaskGroup = TaskGroup } )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Remove the menu option of the @{Task} for a @{Group}.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Group#GROUP TaskGroup
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:RemoveMenuForGroup( TaskGroup )
|
||||
|
||||
local TaskGroupName = TaskGroup:GetName()
|
||||
|
||||
local Mission = self.Mission
|
||||
local MenuMission = Mission.MenuMission
|
||||
local MenuCategory = Mission.MenuCategory
|
||||
local MenuType = Mission.MenuType
|
||||
local MenuStatus = self.MenuStatus
|
||||
local MenuAbort = self.MenuAbort
|
||||
local Menu = self.Menu
|
||||
|
||||
Menu = Menu or {}
|
||||
if Menu[TaskGroupName] then
|
||||
Menu[TaskGroupName]:Remove()
|
||||
Menu[TaskGroupName] = nil
|
||||
end
|
||||
|
||||
MenuType = MenuType or {}
|
||||
if MenuType[TaskGroupName] then
|
||||
for _, Menu in pairs( MenuType[TaskGroupName] ) do
|
||||
Menu:Remove()
|
||||
end
|
||||
MenuType[TaskGroupName] = nil
|
||||
end
|
||||
|
||||
MenuCategory = MenuCategory or {}
|
||||
if MenuCategory[TaskGroupName] then
|
||||
for _, Menu in pairs( MenuCategory[TaskGroupName] ) do
|
||||
Menu:Remove()
|
||||
end
|
||||
MenuCategory[TaskGroupName] = nil
|
||||
end
|
||||
|
||||
MenuStatus = MenuStatus or {}
|
||||
if MenuStatus[TaskGroupName] then
|
||||
MenuStatus[TaskGroupName]:Remove()
|
||||
MenuStatus[TaskGroupName] = nil
|
||||
end
|
||||
|
||||
MenuAbort = MenuAbort or {}
|
||||
if MenuAbort[TaskGroupName] then
|
||||
MenuAbort[TaskGroupName]:Remove()
|
||||
MenuAbort[TaskGroupName] = nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function TASK_BASE.MenuAssignToGroup( MenuParam )
|
||||
|
||||
local self = MenuParam.self
|
||||
local TaskGroup = MenuParam.TaskGroup
|
||||
|
||||
self:AssignToGroup( TaskGroup )
|
||||
end
|
||||
|
||||
function TASK_BASE.MenuTaskStatus( MenuParam )
|
||||
|
||||
local self = MenuParam.self
|
||||
local TaskGroup = MenuParam.TaskGroup
|
||||
|
||||
--self:AssignToGroup( TaskGroup )
|
||||
end
|
||||
|
||||
function TASK_BASE.MenuTaskAbort( MenuParam )
|
||||
|
||||
local self = MenuParam.self
|
||||
local TaskGroup = MenuParam.TaskGroup
|
||||
|
||||
--self:AssignToGroup( TaskGroup )
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- Returns the @{Task} name.
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #string TaskName
|
||||
function TASK_BASE:GetTaskName()
|
||||
return self.TaskName
|
||||
end
|
||||
|
||||
|
||||
--- This is the key worker function for the class. Instantiate a new Process based on the ProcessName to @{Task} and assign it to the ProcessUnit.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Unit#UNIT ProcessUnit The unit to which the process should be assigned.
|
||||
-- @param #string ProcessName The name of the Process.
|
||||
-- @return Process#PROCESS The Process that was added.
|
||||
function TASK_BASE:AssignProcess( ProcessUnit, ProcessName )
|
||||
self:F( { ProcessName } )
|
||||
local ProcessUnitName = ProcessUnit:GetName()
|
||||
|
||||
-- Create the Process instance base on the ProcessClasses collection assigned to the Task
|
||||
local ProcessClass, ProcessArguments
|
||||
ProcessClass, ProcessArguments = self:GetProcessClass( ProcessName )
|
||||
|
||||
local Process = ProcessClass:New( unpack( ProcessArguments ) ) -- Process#PROCESS
|
||||
Process:SetControllable( ProcessUnit )
|
||||
|
||||
self.Processes = self.Processes or {}
|
||||
self.Processes[ProcessUnitName] = self.Processes[ProcessUnitName] or {}
|
||||
|
||||
self.Processes[ProcessUnitName][ProcessName] = Process
|
||||
|
||||
return Process
|
||||
end
|
||||
|
||||
|
||||
--- Get the default or currently assigned @{Process} class with key ProcessName.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string ProcessName
|
||||
-- @return Process#PROCESS
|
||||
-- @return #table
|
||||
function TASK_BASE:GetProcessClass( ProcessName )
|
||||
|
||||
local ProcessClass = self.ProcessClasses[ProcessName].Class
|
||||
local ProcessArguments = self.ProcessClasses[ProcessName].Arguments
|
||||
|
||||
return ProcessClass, ProcessArguments
|
||||
end
|
||||
|
||||
|
||||
--- Set the Process default class with key ProcessName providing the ProcessClass and the constructor initialization parameters when it is assigned to a Unit by the task.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string ProcessName
|
||||
-- @param Process#PROCESS ProcessClass
|
||||
-- @param #table ... The parameters for the New() constructor of the ProcessClass, when the Task is assigning a new Process to the Unit.
|
||||
-- @return Process#PROCESS
|
||||
function TASK_BASE:SetProcessClass( ProcessName, ProcessClass, ... )
|
||||
|
||||
self.ProcessClasses[ProcessName] = self.ProcessClasses[ProcessName] or {}
|
||||
self.ProcessClasses[ProcessName].Class = ProcessClass
|
||||
self.ProcessClasses[ProcessName].Arguments = ...
|
||||
|
||||
return ProcessClass
|
||||
end
|
||||
|
||||
|
||||
--- Remove Processes from @{Task} with key @{Unit}
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskUnitName
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:RemoveProcesses( TaskUnitName )
|
||||
|
||||
for ProcessID, ProcessData in pairs( self.Processes[TaskUnitName] ) do
|
||||
local Process = ProcessData -- Process#PROCESS
|
||||
Process:StopEvents()
|
||||
Process = nil
|
||||
self.Processes[TaskUnitName][ProcessID] = nil
|
||||
self:E( self.Processes[TaskUnitName][ProcessID] )
|
||||
end
|
||||
self.Processes[TaskUnitName] = nil
|
||||
end
|
||||
|
||||
--- Fail processes from @{Task} with key @{Unit}
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskUnitName
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:FailProcesses( TaskUnitName )
|
||||
|
||||
for ProcessID, ProcessData in pairs( self.Processes[TaskUnitName] ) do
|
||||
local Process = ProcessData -- Process#PROCESS
|
||||
Process.Fsm:Fail()
|
||||
end
|
||||
end
|
||||
|
||||
--- Add a FiniteStateMachine to @{Task} with key @{Unit}
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:AddStateMachine( TaskUnit, Fsm )
|
||||
local TaskUnitName = TaskUnit:GetName()
|
||||
self.Fsm[TaskUnitName] = self.Fsm[TaskUnitName] or {}
|
||||
self.Fsm[TaskUnitName][#self.Fsm[TaskUnitName]+1] = Fsm
|
||||
return Fsm
|
||||
end
|
||||
|
||||
--- Remove FiniteStateMachines from @{Task} with key @{Unit}
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskUnitName
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:RemoveStateMachines( TaskUnitName )
|
||||
|
||||
for _, Fsm in pairs( self.Fsm[TaskUnitName] ) do
|
||||
Fsm = nil
|
||||
self.Fsm[TaskUnitName][_] = nil
|
||||
self:E( self.Fsm[TaskUnitName][_] )
|
||||
end
|
||||
self.Fsm[TaskUnitName] = nil
|
||||
end
|
||||
|
||||
--- Checks if there is a FiniteStateMachine assigned to @{Unit} for @{Task}
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskUnitName
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:HasStateMachine( TaskUnitName )
|
||||
|
||||
self:F( { TaskUnitName, self.Fsm[TaskUnitName] ~= nil } )
|
||||
return ( self.Fsm[TaskUnitName] ~= nil )
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
--- Register a potential new assignment for a new spawned @{Unit}.
|
||||
-- Tasks only get assigned if there are players in it.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Event#EVENTDATA Event
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:_EventAssignUnit( Event )
|
||||
if Event.IniUnit then
|
||||
self:F( Event )
|
||||
local TaskUnit = Event.IniUnit
|
||||
if TaskUnit:IsAlive() then
|
||||
local TaskPlayerName = TaskUnit:GetPlayerName()
|
||||
if TaskPlayerName ~= nil then
|
||||
if not self:HasStateMachine( TaskUnit ) then
|
||||
-- Check if the task was assigned to the group, if it was assigned to the group, assign to the unit just spawned and initiate the processes.
|
||||
local TaskGroup = TaskUnit:GetGroup()
|
||||
if self:IsAssignedToGroup( TaskGroup ) then
|
||||
self:AssignToUnit( TaskUnit )
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Catches the "player leave unit" event for a @{Unit} ....
|
||||
-- When a player is an air unit, and leaves the unit:
|
||||
--
|
||||
-- * and he is not at an airbase runway on the ground, he will fail its task.
|
||||
-- * and he is on an airbase and on the ground, the process for him will just continue to work, he can switch airplanes, and take-off again.
|
||||
-- This is important to model the change from plane types for a player during mission assignment.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Event#EVENTDATA Event
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:_EventPlayerLeaveUnit( Event )
|
||||
self:F( Event )
|
||||
if Event.IniUnit then
|
||||
local TaskUnit = Event.IniUnit
|
||||
local TaskUnitName = Event.IniUnitName
|
||||
|
||||
-- Check if for this unit in the task there is a process ongoing.
|
||||
if self:HasStateMachine( TaskUnitName ) then
|
||||
if TaskUnit:IsAir() then
|
||||
if TaskUnit:IsAboveRunway() then
|
||||
-- do nothing
|
||||
else
|
||||
self:E( "IsNotAboveRunway" )
|
||||
-- Player left airplane during an assigned task and was not at an airbase.
|
||||
self:FailProcesses( TaskUnitName )
|
||||
self:UnAssignFromUnit( TaskUnitName )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- UnAssigns a @{Unit} that is left by a player, crashed, dead, ....
|
||||
-- There are only assignments if there are players in it.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Event#EVENTDATA Event
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:_EventDead( Event )
|
||||
self:F( Event )
|
||||
if Event.IniUnit then
|
||||
local TaskUnit = Event.IniUnit
|
||||
local TaskUnitName = Event.IniUnitName
|
||||
|
||||
-- Check if for this unit in the task there is a process ongoing.
|
||||
if self:HasStateMachine( TaskUnitName ) then
|
||||
self:FailProcesses( TaskUnitName )
|
||||
self:UnAssignFromUnit( TaskUnitName )
|
||||
end
|
||||
|
||||
local TaskGroup = Event.IniUnit:GetGroup()
|
||||
TaskGroup:SetState( TaskGroup, "Assigned", nil )
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
--- Gets the Scoring of the task
|
||||
-- @param #TASK_BASE self
|
||||
-- @return Scoring#SCORING Scoring
|
||||
function TASK_BASE:GetScoring()
|
||||
return self.Mission:GetScoring()
|
||||
end
|
||||
|
||||
|
||||
--- Gets the Task Index, which is a combination of the Task category, the Task type, the Task name.
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #string The Task ID
|
||||
function TASK_BASE:GetTaskIndex()
|
||||
|
||||
local TaskCategory = self:GetCategory()
|
||||
local TaskType = self:GetType()
|
||||
local TaskName = self:GetName()
|
||||
|
||||
return TaskCategory .. "." ..TaskType .. "." .. TaskName
|
||||
end
|
||||
|
||||
--- Sets the Name of the Task
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskName
|
||||
function TASK_BASE:SetName( TaskName )
|
||||
self.TaskName = TaskName
|
||||
end
|
||||
|
||||
--- Gets the Name of the Task
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #string The Task Name
|
||||
function TASK_BASE:GetName()
|
||||
return self.TaskName
|
||||
end
|
||||
|
||||
--- Sets the Type of the Task
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskType
|
||||
function TASK_BASE:SetType( TaskType )
|
||||
self.TaskType = TaskType
|
||||
end
|
||||
|
||||
--- Gets the Type of the Task
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #string TaskType
|
||||
function TASK_BASE:GetType()
|
||||
return self.TaskType
|
||||
end
|
||||
|
||||
--- Sets the Category of the Task
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskCategory
|
||||
function TASK_BASE:SetCategory( TaskCategory )
|
||||
self.TaskCategory = TaskCategory
|
||||
end
|
||||
|
||||
--- Gets the Category of the Task
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #string TaskCategory
|
||||
function TASK_BASE:GetCategory()
|
||||
return self.TaskCategory
|
||||
end
|
||||
|
||||
--- Sets the ID of the Task
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskID
|
||||
function TASK_BASE:SetID( TaskID )
|
||||
self.TaskID = TaskID
|
||||
end
|
||||
|
||||
--- Gets the ID of the Task
|
||||
-- @param #TASK_BASE self
|
||||
-- @return #string TaskID
|
||||
function TASK_BASE:GetID()
|
||||
return self.TaskID
|
||||
end
|
||||
|
||||
|
||||
--- Sets a @{Task} to status **Success**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:StateSuccess()
|
||||
self:SetState( self, "State", "Success" )
|
||||
return self
|
||||
end
|
||||
|
||||
--- Is the @{Task} status **Success**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:IsStateSuccess()
|
||||
return self:GetStateString() == "Success"
|
||||
end
|
||||
|
||||
--- Sets a @{Task} to status **Failed**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:StateFailed()
|
||||
self:SetState( self, "State", "Failed" )
|
||||
return self
|
||||
end
|
||||
|
||||
--- Is the @{Task} status **Failed**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:IsStateFailed()
|
||||
return self:GetStateString() == "Failed"
|
||||
end
|
||||
|
||||
--- Sets a @{Task} to status **Planned**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:StatePlanned()
|
||||
self:SetState( self, "State", "Planned" )
|
||||
return self
|
||||
end
|
||||
|
||||
--- Is the @{Task} status **Planned**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:IsStatePlanned()
|
||||
return self:GetStateString() == "Planned"
|
||||
end
|
||||
|
||||
--- Sets a @{Task} to status **Assigned**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:StateAssigned()
|
||||
self:SetState( self, "State", "Assigned" )
|
||||
return self
|
||||
end
|
||||
|
||||
--- Is the @{Task} status **Assigned**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:IsStateAssigned()
|
||||
return self:GetStateString() == "Assigned"
|
||||
end
|
||||
|
||||
--- Sets a @{Task} to status **Hold**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:StateHold()
|
||||
self:SetState( self, "State", "Hold" )
|
||||
return self
|
||||
end
|
||||
|
||||
--- Is the @{Task} status **Hold**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:IsStateHold()
|
||||
return self:GetStateString() == "Hold"
|
||||
end
|
||||
|
||||
--- Sets a @{Task} to status **Replanned**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:StateReplanned()
|
||||
self:SetState( self, "State", "Replanned" )
|
||||
return self
|
||||
end
|
||||
|
||||
--- Is the @{Task} status **Replanned**.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:IsStateReplanned()
|
||||
return self:GetStateString() == "Replanned"
|
||||
end
|
||||
|
||||
--- Gets the @{Task} status.
|
||||
-- @param #TASK_BASE self
|
||||
function TASK_BASE:GetStateString()
|
||||
return self:GetState( self, "State" )
|
||||
end
|
||||
|
||||
--- Sets a @{Task} briefing.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskBriefing
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:SetBriefing( TaskBriefing )
|
||||
self.TaskBriefing = TaskBriefing
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- Adds a score for the TASK to be achieved.
|
||||
-- @param #TASK_BASE self
|
||||
-- @param #string TaskStatus is the status of the TASK when the score needs to be given.
|
||||
-- @param #string ScoreText is a text describing the score that is given according the status.
|
||||
-- @param #number Score is a number providing the score of the status.
|
||||
-- @return #TASK_BASE self
|
||||
function TASK_BASE:AddScore( TaskStatus, ScoreText, Score )
|
||||
self:F2( { TaskStatus, ScoreText, Score } )
|
||||
|
||||
self.Scores[TaskStatus] = self.Scores[TaskStatus] or {}
|
||||
self.Scores[TaskStatus].ScoreText = ScoreText
|
||||
self.Scores[TaskStatus].Score = Score
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- StateMachine callback function for a TASK
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
-- @param Event#EVENTDATA Event
|
||||
function TASK_BASE:OnAssigned( TaskUnit, Fsm, Event, From, To )
|
||||
|
||||
self:E("Assigned")
|
||||
|
||||
local TaskGroup = TaskUnit:GetGroup()
|
||||
|
||||
TaskGroup:Message( self.TaskBriefing, 20 )
|
||||
|
||||
self:RemoveMenuForGroup( TaskGroup )
|
||||
self:SetAssignedMenuForGroup( TaskGroup )
|
||||
|
||||
end
|
||||
|
||||
|
||||
--- StateMachine callback function for a TASK
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
-- @param Event#EVENTDATA Event
|
||||
function TASK_BASE:OnSuccess( TaskUnit, Fsm, Event, From, To )
|
||||
|
||||
self:E("Success")
|
||||
|
||||
self:UnAssignFromGroups()
|
||||
|
||||
local TaskGroup = TaskUnit:GetGroup()
|
||||
self.Mission:SetPlannedMenu()
|
||||
|
||||
self:StateSuccess()
|
||||
|
||||
-- The task has become successful, the event catchers can be cleaned.
|
||||
self:CleanUp()
|
||||
|
||||
end
|
||||
|
||||
--- StateMachine callback function for a TASK
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
-- @param Event#EVENTDATA Event
|
||||
function TASK_BASE:OnFailed( TaskUnit, Fsm, Event, From, To )
|
||||
|
||||
self:E( { "Failed for unit ", TaskUnit:GetName(), TaskUnit:GetPlayerName() } )
|
||||
|
||||
-- A task cannot be "failed", so a task will always be there waiting for players to join.
|
||||
-- When the player leaves its unit, we will need to check whether he was on the ground or not at an airbase.
|
||||
-- When the player crashes, we will need to check whether in the group there are other players still active. It not, we reset the task from Assigned to Planned, otherwise, we just leave as Assigned.
|
||||
|
||||
self:UnAssignFromGroups()
|
||||
self:StatePlanned()
|
||||
|
||||
end
|
||||
|
||||
--- StateMachine callback function for a TASK
|
||||
-- @param #TASK_BASE self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
-- @param Event#EVENTDATA Event
|
||||
function TASK_BASE:OnStateChange( TaskUnit, Fsm, Event, From, To )
|
||||
|
||||
if self:IsTrace() then
|
||||
MESSAGE:New( "Task " .. self.TaskName .. " : " .. Event .. " changed to state " .. To, 15 ):ToAll()
|
||||
end
|
||||
|
||||
self:E( { Event, From, To } )
|
||||
self:SetState( self, "State", To )
|
||||
|
||||
if self.Scores[To] then
|
||||
local Scoring = self:GetScoring()
|
||||
if Scoring then
|
||||
Scoring:_AddMissionScore( self.Mission, self.Scores[To].ScoreText, self.Scores[To].Score )
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
--- @param #TASK_BASE self
|
||||
function TASK_BASE:_Schedule()
|
||||
self:F2()
|
||||
|
||||
self.TaskScheduler = SCHEDULER:New( self, _Scheduler, {}, 15, 15 )
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- @param #TASK_BASE self
|
||||
function TASK_BASE._Scheduler()
|
||||
self:F2()
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
84
Moose Development/Moose/Tasking/TaskMenu.lua
Normal file
84
Moose Development/Moose/Tasking/TaskMenu.lua
Normal file
@@ -0,0 +1,84 @@
|
||||
--- @module Task_Client_Menu
|
||||
|
||||
--- TASK2_MENU_CLIENT class
|
||||
-- @type TASK2_MENU_CLIENT
|
||||
-- @field Unit#UNIT TaskUnit
|
||||
-- @field Set#SET_UNIT TargetSet
|
||||
-- @field Menu#MENU_CLIENT_COMMAND MenuTask
|
||||
-- @extends Task2#TASK2
|
||||
TASK2_MENU_CLIENT = {
|
||||
ClassName = "TASK2_MENU_CLIENT",
|
||||
TargetSet = nil,
|
||||
}
|
||||
|
||||
|
||||
--- Creates a new MENU handling machine.
|
||||
-- @param #TASK2_MENU_CLIENT self
|
||||
-- @param Mission#MISSION Mission
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @param #string MenuText The text of the menu item.
|
||||
-- @return #TASK2_MENU_CLIENT self
|
||||
function TASK2_MENU_CLIENT:New( Mission, TaskUnit, MenuText )
|
||||
|
||||
-- Inherits from BASE
|
||||
local self = BASE:Inherit( self, TASK2:New( Mission, TaskUnit ) ) -- #TASK2_MENU_CLIENT
|
||||
|
||||
self.MenuText = MenuText
|
||||
|
||||
self.Fsm = STATEMACHINE_TASK:New( self, {
|
||||
initial = 'Unassigned',
|
||||
events = {
|
||||
{ name = 'Menu', from = 'Unassigned', to = 'AwaitingMenu' },
|
||||
{ name = 'Assign', from = 'AwaitingMenu', to = 'Assigned' },
|
||||
},
|
||||
callbacks = {
|
||||
onMenu = self.OnMenu,
|
||||
onAssign = self.OnAssign,
|
||||
},
|
||||
endstates = {
|
||||
'Assigned'
|
||||
},
|
||||
} )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Task Events
|
||||
|
||||
--- StateMachine callback function for a TASK2
|
||||
-- @param #TASK2_MENU_CLIENT self
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
function TASK2_MENU_CLIENT:OnMenu( Fsm, Event, From, To )
|
||||
self:E( { Event, From, To, self.TaskUnit.UnitName} )
|
||||
|
||||
self.TaskUnit:Message( "Press F10 for task menu", 15 )
|
||||
self.Menu = MENU_CLIENT:New( self.TaskUnit, self.Mission:GetName(), nil )
|
||||
self.MenuTask = MENU_CLIENT_COMMAND:New( self.TaskUnit, self.MenuText, self.Menu, self.MenuAssign, self )
|
||||
end
|
||||
|
||||
--- Menu function.
|
||||
-- @param #TASK2_MENU_CLIENT self
|
||||
function TASK2_MENU_CLIENT:MenuAssign()
|
||||
self:E( )
|
||||
|
||||
self.TaskUnit:Message( "Menu Assign", 15 )
|
||||
|
||||
self:NextEvent( self.Fsm.Assign )
|
||||
end
|
||||
|
||||
--- StateMachine callback function for a TASK2
|
||||
-- @param #TASK2_MENU_CLIENT self
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
function TASK2_MENU_CLIENT:OnAssign( Fsm, Event, From, To )
|
||||
self:E( { Event, From, To, self.TaskUnit.UnitName} )
|
||||
|
||||
self.TaskUnit:Message( "Assign Task", 15 )
|
||||
self.MenuTask:Remove()
|
||||
end
|
||||
|
||||
150
Moose Development/Moose/Tasking/Task_A2G.lua
Normal file
150
Moose Development/Moose/Tasking/Task_A2G.lua
Normal file
@@ -0,0 +1,150 @@
|
||||
--- (AI) (SP) (MP) Tasking for Air to Ground Processes.
|
||||
--
|
||||
-- 1) @{#TASK_A2G} class, extends @{Task#TASK_BASE}
|
||||
-- =================================================
|
||||
-- The @{#TASK_A2G} class defines a CAS or BAI task of a @{Set} of Target Units,
|
||||
-- located at a Target Zone, based on the tasking capabilities defined in @{Task#TASK_BASE}.
|
||||
-- The TASK_A2G is implemented using a @{Statemachine#STATEMACHINE_TASK}, and has the following statuses:
|
||||
--
|
||||
-- * **None**: Start of the process
|
||||
-- * **Planned**: The SEAD task is planned. Upon Planned, the sub-process @{Process_Assign#PROCESS_ASSIGN_ACCEPT} is started to accept the task.
|
||||
-- * **Assigned**: The SEAD task is assigned to a @{Group#GROUP}. Upon Assigned, the sub-process @{Process_Route#PROCESS_ROUTE} is started to route the active Units in the Group to the attack zone.
|
||||
-- * **Success**: The SEAD task is successfully completed. Upon Success, the sub-process @{Process_SEAD#PROCESS_SEAD} is started to follow-up successful SEADing of the targets assigned in the task.
|
||||
-- * **Failed**: The SEAD task has failed. This will happen if the player exists the task early, without communicating a possible cancellation to HQ.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- ### Authors: FlightControl - Design and Programming
|
||||
--
|
||||
-- @module Task_A2G
|
||||
|
||||
|
||||
do -- TASK_A2G
|
||||
|
||||
--- The TASK_A2G class
|
||||
-- @type TASK_A2G
|
||||
-- @extends Task#TASK_BASE
|
||||
TASK_A2G = {
|
||||
ClassName = "TASK_A2G",
|
||||
}
|
||||
|
||||
--- Instantiates a new TASK_A2G.
|
||||
-- @param #TASK_A2G self
|
||||
-- @param Mission#MISSION Mission
|
||||
-- @param Set#SET_GROUP SetGroup The set of groups for which the Task can be assigned.
|
||||
-- @param #string TaskName The name of the Task.
|
||||
-- @param #string TaskType BAI or CAS
|
||||
-- @param Set#SET_UNIT UnitSetTargets
|
||||
-- @param Zone#ZONE_BASE TargetZone
|
||||
-- @return #TASK_A2G self
|
||||
function TASK_A2G:New( Mission, SetGroup, TaskName, TaskType, TargetSetUnit, TargetZone, FACUnit )
|
||||
local self = BASE:Inherit( self, TASK_BASE:New( Mission, SetGroup, TaskName, TaskType, "A2G" ) )
|
||||
self:F()
|
||||
|
||||
self.TargetSetUnit = TargetSetUnit
|
||||
self.TargetZone = TargetZone
|
||||
self.FACUnit = FACUnit
|
||||
|
||||
_EVENTDISPATCHER:OnPlayerLeaveUnit( self._EventPlayerLeaveUnit, self )
|
||||
_EVENTDISPATCHER:OnDead( self._EventDead, self )
|
||||
_EVENTDISPATCHER:OnCrash( self._EventDead, self )
|
||||
_EVENTDISPATCHER:OnPilotDead( self._EventDead, self )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Removes a TASK_A2G.
|
||||
-- @param #TASK_A2G self
|
||||
-- @return #nil
|
||||
function TASK_A2G:CleanUp()
|
||||
|
||||
self:GetParent( self ):CleanUp()
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
--- Assign the @{Task} to a @{Unit}.
|
||||
-- @param #TASK_A2G self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @return #TASK_A2G self
|
||||
function TASK_A2G:AssignToUnit( TaskUnit )
|
||||
self:F( TaskUnit:GetName() )
|
||||
|
||||
local ProcessAssign = self:AddProcess( TaskUnit, PROCESS_ASSIGN_ACCEPT:New( self, TaskUnit, self.TaskBriefing ) )
|
||||
local ProcessRoute = self:AddProcess( TaskUnit, PROCESS_ROUTE:New( self, TaskUnit, self.TargetZone ) )
|
||||
local ProcessDestroy = self:AddProcess( TaskUnit, PROCESS_DESTROY:New( self, self.TaskType, TaskUnit, self.TargetSetUnit ) )
|
||||
local ProcessSmoke = self:AddProcess( TaskUnit, PROCESS_SMOKE_TARGETS:New( self, TaskUnit, self.TargetSetUnit, self.TargetZone ) )
|
||||
local ProcessJTAC = self:AddProcess( TaskUnit, PROCESS_JTAC:New( self, TaskUnit, self.TargetSetUnit, self.FACUnit ) )
|
||||
|
||||
local Process = self:AddStateMachine( TaskUnit, STATEMACHINE_TASK:New( self, TaskUnit, {
|
||||
initial = 'None',
|
||||
events = {
|
||||
{ name = 'Next', from = 'None', to = 'Planned' },
|
||||
{ name = 'Next', from = 'Planned', to = 'Assigned' },
|
||||
{ name = 'Reject', from = 'Planned', to = 'Rejected' },
|
||||
{ name = 'Next', from = 'Assigned', to = 'Success' },
|
||||
{ name = 'Fail', from = 'Assigned', to = 'Failed' },
|
||||
{ name = 'Fail', from = 'Arrived', to = 'Failed' }
|
||||
},
|
||||
callbacks = {
|
||||
onNext = self.OnNext,
|
||||
onRemove = self.OnRemove,
|
||||
},
|
||||
subs = {
|
||||
Assign = { onstateparent = 'Planned', oneventparent = 'Next', fsm = ProcessAssign.Fsm, event = 'Start', returnevents = { 'Next', 'Reject' } },
|
||||
Route = { onstateparent = 'Assigned', oneventparent = 'Next', fsm = ProcessRoute.Fsm, event = 'Start' },
|
||||
Destroy = { onstateparent = 'Assigned', oneventparent = 'Next', fsm = ProcessDestroy.Fsm, event = 'Start', returnevents = { 'Next' } },
|
||||
Smoke = { onstateparent = 'Assigned', oneventparent = 'Next', fsm = ProcessSmoke.Fsm, event = 'Start', },
|
||||
JTAC = { onstateparent = 'Assigned', oneventparent = 'Next', fsm = ProcessJTAC.Fsm, event = 'Start', },
|
||||
}
|
||||
} ) )
|
||||
|
||||
ProcessRoute:AddScore( "Failed", "failed to destroy a ground unit", -100 )
|
||||
ProcessDestroy:AddScore( "Destroy", "destroyed a ground unit", 25 )
|
||||
ProcessDestroy:AddScore( "Failed", "failed to destroy a ground unit", -100 )
|
||||
|
||||
Process:Next()
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- StateMachine callback function for a TASK
|
||||
-- @param #TASK_A2G self
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
-- @param Event#EVENTDATA Event
|
||||
function TASK_A2G:OnNext( Fsm, Event, From, To, Event )
|
||||
|
||||
self:SetState( self, "State", To )
|
||||
|
||||
end
|
||||
|
||||
--- @param #TASK_A2G self
|
||||
function TASK_A2G:GetPlannedMenuText()
|
||||
return self:GetStateString() .. " - " .. self:GetTaskName() .. " ( " .. self.TargetSetUnit:GetUnitTypesText() .. " )"
|
||||
end
|
||||
|
||||
|
||||
--- @param #TASK_A2G self
|
||||
function TASK_A2G:_Schedule()
|
||||
self:F2()
|
||||
|
||||
self.TaskScheduler = SCHEDULER:New( self, _Scheduler, {}, 15, 15 )
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- @param #TASK_A2G self
|
||||
function TASK_A2G._Scheduler()
|
||||
self:F2()
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
283
Moose Development/Moose/Tasking/Task_AIBalancer.lua
Normal file
283
Moose Development/Moose/Tasking/Task_AIBalancer.lua
Normal file
@@ -0,0 +1,283 @@
|
||||
--- This module contains the AIBALANCER class.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- 1) @{AIBalancer#AIBALANCER} class, extends @{Base#BASE}
|
||||
-- =======================================================
|
||||
-- The @{AIBalancer#AIBALANCER} class controls the dynamic spawning of AI GROUPS depending on a SET_CLIENT.
|
||||
-- There will be as many AI GROUPS spawned as there at CLIENTS in SET_CLIENT not spawned.
|
||||
-- The AIBalancer uses the @{PatrolZone#PATROLZONE} class to make AI patrol an zone until the fuel treshold is reached.
|
||||
--
|
||||
-- 1.1) AIBALANCER construction method:
|
||||
-- ------------------------------------
|
||||
-- Create a new AIBALANCER object with the @{#AIBALANCER.New} method:
|
||||
--
|
||||
-- * @{#AIBALANCER.New}: Creates a new AIBALANCER object.
|
||||
--
|
||||
-- 1.2) AIBALANCER returns AI to Airbases:
|
||||
-- ---------------------------------------
|
||||
-- You can configure to have the AI to return to:
|
||||
--
|
||||
-- * @{#AIBALANCER.ReturnToHomeAirbase}: Returns the AI to the home @{Airbase#AIRBASE}.
|
||||
-- * @{#AIBALANCER.ReturnToNearestAirbases}: Returns the AI to the nearest friendly @{Airbase#AIRBASE}.
|
||||
--
|
||||
-- 1.3) AIBALANCER allows AI to patrol specific zones:
|
||||
-- ---------------------------------------------------
|
||||
-- Use @{AIBalancer#AIBALANCER.SetPatrolZone}() to specify a zone where the AI needs to patrol.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- **API CHANGE HISTORY**
|
||||
-- ======================
|
||||
--
|
||||
-- The underlying change log documents the API changes. Please read this carefully. The following notation is used:
|
||||
--
|
||||
-- * **Added** parts are expressed in bold type face.
|
||||
-- * _Removed_ parts are expressed in italic type face.
|
||||
--
|
||||
-- Hereby the change log:
|
||||
--
|
||||
-- 2016-08-17: SPAWN:**InitCleanUp**( SpawnCleanUpInterval ) replaces SPAWN:_CleanUp_( SpawnCleanUpInterval )
|
||||
--
|
||||
-- * Want to ensure that the methods starting with **Init** are the first called methods before any _Spawn_ method is called!
|
||||
-- * This notation makes it now more clear which methods are initialization methods and which methods are Spawn enablement methods.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- AUTHORS and CONTRIBUTIONS
|
||||
-- =========================
|
||||
--
|
||||
-- ### Contributions:
|
||||
--
|
||||
-- * **Dutch_Baron (James)**: Who you can search on the Eagle Dynamics Forums.
|
||||
-- Working together with James has resulted in the creation of the AIBALANCER class.
|
||||
-- James has shared his ideas on balancing AI with air units, and together we made a first design which you can use now :-)
|
||||
--
|
||||
-- * **SNAFU**:
|
||||
-- Had a couple of mails with the guys to validate, if the same concept in the GCI/CAP script could be reworked within MOOSE.
|
||||
-- None of the script code has been used however within the new AIBALANCER moose class.
|
||||
--
|
||||
-- ### Authors:
|
||||
--
|
||||
-- * FlightControl: Framework Design & Programming
|
||||
--
|
||||
-- @module AIBalancer
|
||||
|
||||
|
||||
|
||||
--- AIBALANCER class
|
||||
-- @type AIBALANCER
|
||||
-- @field Set#SET_CLIENT SetClient
|
||||
-- @field Spawn#SPAWN SpawnAI
|
||||
-- @field #boolean ToNearestAirbase
|
||||
-- @field Set#SET_AIRBASE ReturnAirbaseSet
|
||||
-- @field DCSTypes#Distance ReturnTresholdRange
|
||||
-- @field #boolean ToHomeAirbase
|
||||
-- @field PatrolZone#PATROLZONE PatrolZone
|
||||
-- @extends Base#BASE
|
||||
AIBALANCER = {
|
||||
ClassName = "AIBALANCER",
|
||||
PatrolZones = {},
|
||||
AIGroups = {},
|
||||
}
|
||||
|
||||
--- Creates a new AIBALANCER object, building a set of units belonging to a coalitions, categories, countries, types or with defined prefix names.
|
||||
-- @param #AIBALANCER self
|
||||
-- @param SetClient A SET_CLIENT object that will contain the CLIENT objects to be monitored if they are alive or not (joined by a player).
|
||||
-- @param SpawnAI A SPAWN object that will spawn the AI units required, balancing the SetClient.
|
||||
-- @return #AIBALANCER self
|
||||
function AIBALANCER:New( SetClient, SpawnAI )
|
||||
|
||||
-- Inherits from BASE
|
||||
local self = BASE:Inherit( self, BASE:New() )
|
||||
|
||||
self.SetClient = SetClient
|
||||
if type( SpawnAI ) == "table" then
|
||||
if SpawnAI.ClassName and SpawnAI.ClassName == "SPAWN" then
|
||||
self.SpawnAI = { SpawnAI }
|
||||
else
|
||||
local SpawnObjects = true
|
||||
for SpawnObjectID, SpawnObject in pairs( SpawnAI ) do
|
||||
if SpawnObject.ClassName and SpawnObject.ClassName == "SPAWN" then
|
||||
self:E( SpawnObject.ClassName )
|
||||
else
|
||||
self:E( "other object" )
|
||||
SpawnObjects = false
|
||||
end
|
||||
end
|
||||
if SpawnObjects == true then
|
||||
self.SpawnAI = SpawnAI
|
||||
else
|
||||
error( "No SPAWN object given in parameter SpawnAI, either as a single object or as a table of objects!" )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
self.ToNearestAirbase = false
|
||||
self.ReturnHomeAirbase = false
|
||||
|
||||
self.AIMonitorSchedule = SCHEDULER:New( self, self._ClientAliveMonitorScheduler, {}, 1, 10, 0 )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Returns the AI to the nearest friendly @{Airbase#AIRBASE}.
|
||||
-- @param #AIBALANCER self
|
||||
-- @param DCSTypes#Distance ReturnTresholdRange If there is an enemy @{Client#CLIENT} within the ReturnTresholdRange given in meters, the AI will not return to the nearest @{Airbase#AIRBASE}.
|
||||
-- @param Set#SET_AIRBASE ReturnAirbaseSet The SET of @{Set#SET_AIRBASE}s to evaluate where to return to.
|
||||
function AIBALANCER:ReturnToNearestAirbases( ReturnTresholdRange, ReturnAirbaseSet )
|
||||
|
||||
self.ToNearestAirbase = true
|
||||
self.ReturnTresholdRange = ReturnTresholdRange
|
||||
self.ReturnAirbaseSet = ReturnAirbaseSet
|
||||
end
|
||||
|
||||
--- Returns the AI to the home @{Airbase#AIRBASE}.
|
||||
-- @param #AIBALANCER self
|
||||
-- @param DCSTypes#Distance ReturnTresholdRange If there is an enemy @{Client#CLIENT} within the ReturnTresholdRange given in meters, the AI will not return to the nearest @{Airbase#AIRBASE}.
|
||||
function AIBALANCER:ReturnToHomeAirbase( ReturnTresholdRange )
|
||||
|
||||
self.ToHomeAirbase = true
|
||||
self.ReturnTresholdRange = ReturnTresholdRange
|
||||
end
|
||||
|
||||
--- Let the AI patrol a @{Zone} with a given Speed range and Altitude range.
|
||||
-- @param #AIBALANCER self
|
||||
-- @param PatrolZone#PATROLZONE PatrolZone The @{PatrolZone} where the AI needs to patrol.
|
||||
-- @return PatrolZone#PATROLZONE self
|
||||
function AIBALANCER:SetPatrolZone( PatrolZone, PatrolFloorAltitude, PatrolCeilingAltitude, PatrolMinSpeed, PatrolMaxSpeed )
|
||||
|
||||
self.PatrolZone = PATROLZONE:New(
|
||||
self.SpawnAI,
|
||||
PatrolZone,
|
||||
PatrolFloorAltitude,
|
||||
PatrolCeilingAltitude,
|
||||
PatrolMinSpeed,
|
||||
PatrolMaxSpeed
|
||||
)
|
||||
end
|
||||
|
||||
--- Get the @{PatrolZone} object assigned by the @{AIBalancer} object.
|
||||
-- @param #AIBALANCER self
|
||||
-- @return PatrolZone#PATROLZONE PatrolZone The @{PatrolZone} where the AI needs to patrol.
|
||||
function AIBALANCER:GetPatrolZone()
|
||||
|
||||
return self.PatrolZone
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- @param #AIBALANCER self
|
||||
function AIBALANCER:_ClientAliveMonitorScheduler()
|
||||
|
||||
self.SetClient:ForEachClient(
|
||||
--- @param Client#CLIENT Client
|
||||
function( Client )
|
||||
local ClientAIAliveState = Client:GetState( self, 'AIAlive' )
|
||||
self:T( ClientAIAliveState )
|
||||
if Client:IsAlive() then
|
||||
if ClientAIAliveState == true then
|
||||
Client:SetState( self, 'AIAlive', false )
|
||||
|
||||
local AIGroup = self.AIGroups[Client.UnitName] -- Group#GROUP
|
||||
|
||||
-- local PatrolZone = Client:GetState( self, "PatrolZone" )
|
||||
-- if PatrolZone then
|
||||
-- PatrolZone = nil
|
||||
-- Client:ClearState( self, "PatrolZone" )
|
||||
-- end
|
||||
|
||||
if self.ToNearestAirbase == false and self.ToHomeAirbase == false then
|
||||
AIGroup:Destroy()
|
||||
else
|
||||
-- We test if there is no other CLIENT within the self.ReturnTresholdRange of the first unit of the AI group.
|
||||
-- If there is a CLIENT, the AI stays engaged and will not return.
|
||||
-- If there is no CLIENT within the self.ReturnTresholdRange, then the unit will return to the Airbase return method selected.
|
||||
|
||||
local PlayerInRange = { Value = false }
|
||||
local RangeZone = ZONE_RADIUS:New( 'RangeZone', AIGroup:GetVec2(), self.ReturnTresholdRange )
|
||||
|
||||
self:E( RangeZone )
|
||||
|
||||
_DATABASE:ForEachPlayer(
|
||||
--- @param Unit#UNIT RangeTestUnit
|
||||
function( RangeTestUnit, RangeZone, AIGroup, PlayerInRange )
|
||||
self:E( { PlayerInRange, RangeTestUnit.UnitName, RangeZone.ZoneName } )
|
||||
if RangeTestUnit:IsInZone( RangeZone ) == true then
|
||||
self:E( "in zone" )
|
||||
if RangeTestUnit:GetCoalition() ~= AIGroup:GetCoalition() then
|
||||
self:E( "in range" )
|
||||
PlayerInRange.Value = true
|
||||
end
|
||||
end
|
||||
end,
|
||||
|
||||
--- @param Zone#ZONE_RADIUS RangeZone
|
||||
-- @param Group#GROUP AIGroup
|
||||
function( RangeZone, AIGroup, PlayerInRange )
|
||||
local AIGroupTemplate = AIGroup:GetTemplate()
|
||||
if PlayerInRange.Value == false then
|
||||
if self.ToHomeAirbase == true then
|
||||
local WayPointCount = #AIGroupTemplate.route.points
|
||||
local SwitchWayPointCommand = AIGroup:CommandSwitchWayPoint( 1, WayPointCount, 1 )
|
||||
AIGroup:SetCommand( SwitchWayPointCommand )
|
||||
AIGroup:MessageToRed( "Returning to home base ...", 30 )
|
||||
else
|
||||
-- Okay, we need to send this Group back to the nearest base of the Coalition of the AI.
|
||||
--TODO: i need to rework the POINT_VEC2 thing.
|
||||
local PointVec2 = POINT_VEC2:New( AIGroup:GetVec2().x, AIGroup:GetVec2().y )
|
||||
local ClosestAirbase = self.ReturnAirbaseSet:FindNearestAirbaseFromPointVec2( PointVec2 )
|
||||
self:T( ClosestAirbase.AirbaseName )
|
||||
AIGroup:MessageToRed( "Returning to " .. ClosestAirbase:GetName().. " ...", 30 )
|
||||
local RTBRoute = AIGroup:RouteReturnToAirbase( ClosestAirbase )
|
||||
AIGroupTemplate.route = RTBRoute
|
||||
AIGroup:Respawn( AIGroupTemplate )
|
||||
end
|
||||
end
|
||||
end
|
||||
, RangeZone, AIGroup, PlayerInRange
|
||||
)
|
||||
|
||||
end
|
||||
end
|
||||
else
|
||||
if not ClientAIAliveState or ClientAIAliveState == false then
|
||||
Client:SetState( self, 'AIAlive', true )
|
||||
|
||||
|
||||
-- OK, spawn a new group from the SpawnAI objects provided.
|
||||
local SpawnAICount = #self.SpawnAI
|
||||
local SpawnAIIndex = math.random( 1, SpawnAICount )
|
||||
local AIGroup = self.SpawnAI[SpawnAIIndex]:Spawn()
|
||||
AIGroup:E( "spawning new AIGroup" )
|
||||
--TODO: need to rework UnitName thing ...
|
||||
self.AIGroups[Client.UnitName] = AIGroup
|
||||
|
||||
--- Now test if the AIGroup needs to patrol a zone, otherwise let it follow its route...
|
||||
if self.PatrolZone then
|
||||
self.PatrolZones[#self.PatrolZones+1] = PATROLZONE:New(
|
||||
self.PatrolZone.PatrolZone,
|
||||
self.PatrolZone.PatrolFloorAltitude,
|
||||
self.PatrolZone.PatrolCeilingAltitude,
|
||||
self.PatrolZone.PatrolMinSpeed,
|
||||
self.PatrolZone.PatrolMaxSpeed
|
||||
)
|
||||
|
||||
if self.PatrolZone.PatrolManageFuel == true then
|
||||
self.PatrolZones[#self.PatrolZones]:ManageFuel( self.PatrolZone.PatrolFuelTresholdPercentage, self.PatrolZone.PatrolOutOfFuelOrbitTime )
|
||||
end
|
||||
self.PatrolZones[#self.PatrolZones]:SetGroup( AIGroup )
|
||||
|
||||
--self.PatrolZones[#self.PatrolZones+1] = PatrolZone
|
||||
|
||||
--Client:SetState( self, "PatrolZone", PatrolZone )
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
)
|
||||
return true
|
||||
end
|
||||
|
||||
|
||||
|
||||
137
Moose Development/Moose/Tasking/Task_Pickup.lua
Normal file
137
Moose Development/Moose/Tasking/Task_Pickup.lua
Normal file
@@ -0,0 +1,137 @@
|
||||
--- This module contains the TASK_PICKUP classes.
|
||||
--
|
||||
-- 1) @{#TASK_PICKUP} class, extends @{Task#TASK_BASE}
|
||||
-- ===================================================
|
||||
-- The @{#TASK_PICKUP} class defines a pickup task of a @{Set} of @{CARGO} objects defined within the mission.
|
||||
-- based on the tasking capabilities defined in @{Task#TASK_BASE}.
|
||||
-- The TASK_PICKUP is implemented using a @{Statemachine#STATEMACHINE_TASK}, and has the following statuses:
|
||||
--
|
||||
-- * **None**: Start of the process
|
||||
-- * **Planned**: The SEAD task is planned. Upon Planned, the sub-process @{Process_Assign#PROCESS_ASSIGN_ACCEPT} is started to accept the task.
|
||||
-- * **Assigned**: The SEAD task is assigned to a @{Group#GROUP}. Upon Assigned, the sub-process @{Process_Route#PROCESS_ROUTE} is started to route the active Units in the Group to the attack zone.
|
||||
-- * **Success**: The SEAD task is successfully completed. Upon Success, the sub-process @{Process_SEAD#PROCESS_SEAD} is started to follow-up successful SEADing of the targets assigned in the task.
|
||||
-- * **Failed**: The SEAD task has failed. This will happen if the player exists the task early, without communicating a possible cancellation to HQ.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- ### Authors: FlightControl - Design and Programming
|
||||
--
|
||||
-- @module Task_PICKUP
|
||||
|
||||
|
||||
do -- TASK_PICKUP
|
||||
|
||||
--- The TASK_PICKUP class
|
||||
-- @type TASK_PICKUP
|
||||
-- @extends Task#TASK_BASE
|
||||
TASK_PICKUP = {
|
||||
ClassName = "TASK_PICKUP",
|
||||
}
|
||||
|
||||
--- Instantiates a new TASK_PICKUP.
|
||||
-- @param #TASK_PICKUP self
|
||||
-- @param Mission#MISSION Mission
|
||||
-- @param Set#SET_GROUP AssignedSetGroup The set of groups for which the Task can be assigned.
|
||||
-- @param #string TaskName The name of the Task.
|
||||
-- @param #string TaskType BAI or CAS
|
||||
-- @param Set#SET_UNIT UnitSetTargets
|
||||
-- @param Zone#ZONE_BASE TargetZone
|
||||
-- @return #TASK_PICKUP self
|
||||
function TASK_PICKUP:New( Mission, AssignedSetGroup, TaskName, TaskType )
|
||||
local self = BASE:Inherit( self, TASK_BASE:New( Mission, AssignedSetGroup, TaskName, TaskType, "PICKUP" ) )
|
||||
self:F()
|
||||
|
||||
_EVENTDISPATCHER:OnPlayerLeaveUnit( self._EventPlayerLeaveUnit, self )
|
||||
_EVENTDISPATCHER:OnDead( self._EventDead, self )
|
||||
_EVENTDISPATCHER:OnCrash( self._EventDead, self )
|
||||
_EVENTDISPATCHER:OnPilotDead( self._EventDead, self )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Removes a TASK_PICKUP.
|
||||
-- @param #TASK_PICKUP self
|
||||
-- @return #nil
|
||||
function TASK_PICKUP:CleanUp()
|
||||
|
||||
self:GetParent( self ):CleanUp()
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
--- Assign the @{Task} to a @{Unit}.
|
||||
-- @param #TASK_PICKUP self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @return #TASK_PICKUP self
|
||||
function TASK_PICKUP:AssignToUnit( TaskUnit )
|
||||
self:F( TaskUnit:GetName() )
|
||||
|
||||
local ProcessAssign = self:AddProcess( TaskUnit, PROCESS_ASSIGN_ACCEPT:New( self, TaskUnit, self.TaskBriefing ) )
|
||||
local ProcessPickup = self:AddProcess( TaskUnit, PROCESS_PICKUP:New( self, self.TaskType, TaskUnit ) )
|
||||
|
||||
local Process = self:AddStateMachine( TaskUnit, STATEMACHINE_TASK:New( self, TaskUnit, {
|
||||
initial = 'None',
|
||||
events = {
|
||||
{ name = 'Next', from = 'None', to = 'Planned' },
|
||||
{ name = 'Next', from = 'Planned', to = 'Assigned' },
|
||||
{ name = 'Next', from = 'Assigned', to = 'Success' },
|
||||
{ name = 'Fail', from = 'Assigned', to = 'Failed' },
|
||||
},
|
||||
callbacks = {
|
||||
onNext = self.OnNext,
|
||||
},
|
||||
subs = {
|
||||
Assign = { onstateparent = 'Planned', oneventparent = 'Next', fsm = ProcessAssign.Fsm, event = 'Start', returnevents = { 'Next', 'Reject' } },
|
||||
Pickup = { onstateparent = 'Assigned', oneventparent = 'Next', fsm = ProcessDestroy.Fsm, event = 'Start', returnevents = { 'Next' } },
|
||||
}
|
||||
} ) )
|
||||
|
||||
ProcessRoute:AddScore( "Failed", "failed to destroy a ground unit", -100 )
|
||||
ProcessDestroy:AddScore( "Pickup", "Picked-Up a Cargo", 25 )
|
||||
ProcessDestroy:AddScore( "Failed", "failed to destroy a ground unit", -100 )
|
||||
|
||||
Process:Next()
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- StateMachine callback function for a TASK
|
||||
-- @param #TASK_PICKUP self
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
-- @param Event#EVENTDATA Event
|
||||
function TASK_PICKUP:OnNext( Fsm, Event, From, To, Event )
|
||||
|
||||
self:SetState( self, "State", To )
|
||||
|
||||
end
|
||||
|
||||
--- @param #TASK_PICKUP self
|
||||
function TASK_PICKUP:GetPlannedMenuText()
|
||||
return self:GetStateString() .. " - " .. self:GetTaskName() .. " ( " .. self.TargetSetUnit:GetUnitTypesText() .. " )"
|
||||
end
|
||||
|
||||
|
||||
--- @param #TASK_PICKUP self
|
||||
function TASK_PICKUP:_Schedule()
|
||||
self:F2()
|
||||
|
||||
self.TaskScheduler = SCHEDULER:New( self, _Scheduler, {}, 15, 15 )
|
||||
return self
|
||||
end
|
||||
|
||||
|
||||
--- @param #TASK_PICKUP self
|
||||
function TASK_PICKUP._Scheduler()
|
||||
self:F2()
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
135
Moose Development/Moose/Tasking/Task_SEAD.lua
Normal file
135
Moose Development/Moose/Tasking/Task_SEAD.lua
Normal file
@@ -0,0 +1,135 @@
|
||||
--- This module contains the TASK_SEAD classes.
|
||||
--
|
||||
-- 1) @{#TASK_SEAD} class, extends @{Task#TASK_BASE}
|
||||
-- =================================================
|
||||
-- The @{#TASK_SEAD} class defines a SEAD task for a @{Set} of Target Units, located at a Target Zone,
|
||||
-- based on the tasking capabilities defined in @{Task#TASK_BASE}.
|
||||
-- The TASK_SEAD is implemented using a @{Statemachine#STATEMACHINE_TASK}, and has the following statuses:
|
||||
--
|
||||
-- * **None**: Start of the process
|
||||
-- * **Planned**: The SEAD task is planned. Upon Planned, the sub-process @{Process_Assign#PROCESS_ASSIGN_ACCEPT} is started to accept the task.
|
||||
-- * **Assigned**: The SEAD task is assigned to a @{Group#GROUP}. Upon Assigned, the sub-process @{Process_Route#PROCESS_ROUTE} is started to route the active Units in the Group to the attack zone.
|
||||
-- * **Success**: The SEAD task is successfully completed. Upon Success, the sub-process @{Process_SEAD#PROCESS_SEAD} is started to follow-up successful SEADing of the targets assigned in the task.
|
||||
-- * **Failed**: The SEAD task has failed. This will happen if the player exists the task early, without communicating a possible cancellation to HQ.
|
||||
--
|
||||
-- ===
|
||||
--
|
||||
-- ### Authors: FlightControl - Design and Programming
|
||||
--
|
||||
-- @module Task_SEAD
|
||||
|
||||
|
||||
|
||||
do -- TASK_SEAD
|
||||
|
||||
--- The TASK_SEAD class
|
||||
-- @type TASK_SEAD
|
||||
-- @field Set#SET_UNIT TargetSetUnit
|
||||
-- @extends Task#TASK_BASE
|
||||
TASK_SEAD = {
|
||||
ClassName = "TASK_SEAD",
|
||||
}
|
||||
|
||||
--- Instantiates a new TASK_SEAD.
|
||||
-- @param #TASK_SEAD self
|
||||
-- @param Mission#MISSION Mission
|
||||
-- @param Set#SET_GROUP SetGroup The set of groups for which the Task can be assigned.
|
||||
-- @param #string TaskName The name of the Task.
|
||||
-- @param Set#SET_UNIT UnitSetTargets
|
||||
-- @param Zone#ZONE_BASE TargetZone
|
||||
-- @return #TASK_SEAD self
|
||||
function TASK_SEAD:New( Mission, SetGroup, TaskName, TargetSetUnit, TargetZone )
|
||||
local self = BASE:Inherit( self, TASK_BASE:New( Mission, SetGroup, TaskName, "SEAD", "A2G" ) )
|
||||
self:F()
|
||||
|
||||
self.TargetSetUnit = TargetSetUnit
|
||||
self.TargetZone = TargetZone
|
||||
|
||||
-- ASSIGN_ACCEPT:New(TaskBriefing)
|
||||
self:SetProcessClass( "ASSIGN", ASSIGN_ACCEPT, self.TaskBriefing )
|
||||
|
||||
-- ROUTE_ZONE:New(TargetZone)
|
||||
self:SetProcessClass( "ROUTE", ROUTE_ZONE, self.TargetZone )
|
||||
|
||||
-- ACCOUNT_DEADS:New(TargetSetUnit,TaskName)
|
||||
self:SetProcessClass( "ACCOUNT", ACCOUNT_DEADS, self.TargetSetUnit, "SEAD" )
|
||||
|
||||
-- SMOKE_TARGETS_ZONE:New( self.TargetSetUnit, self.TargetZone )
|
||||
--self:SetProcessClass( "SMOKE", SMOKE_TARGETS_ZONE, self.TargetSetUnit, self.TargetZone )
|
||||
|
||||
_EVENTDISPATCHER:OnPlayerLeaveUnit( self._EventPlayerLeaveUnit, self )
|
||||
_EVENTDISPATCHER:OnDead( self._EventDead, self )
|
||||
_EVENTDISPATCHER:OnCrash( self._EventDead, self )
|
||||
_EVENTDISPATCHER:OnPilotDead( self._EventDead, self )
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- Removes a TASK_SEAD.
|
||||
-- @param #TASK_SEAD self
|
||||
-- @return #nil
|
||||
function TASK_SEAD:CleanUp()
|
||||
|
||||
self:GetParent(self):CleanUp()
|
||||
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
|
||||
--- Assign the @{Task} to a @{Unit}.
|
||||
-- @param #TASK_SEAD self
|
||||
-- @param Unit#UNIT TaskUnit
|
||||
-- @return #TASK_SEAD self
|
||||
function TASK_SEAD:AssignToUnit( TaskUnit )
|
||||
self:F( TaskUnit:GetName() )
|
||||
|
||||
local ProcessAssign = self:AssignProcess( TaskUnit, "ASSIGN" )
|
||||
local ProcessRoute = self:AssignProcess( TaskUnit, "ROUTE" )
|
||||
local ProcessSEAD = self:AssignProcess( TaskUnit, "ACCOUNT" )
|
||||
--local ProcessSmoke = self:AssignProcess( TaskUnit, "SMOKE" )
|
||||
|
||||
local FSMT = {
|
||||
initial = 'None',
|
||||
events = {
|
||||
{ name = 'Next', from = 'None', to = 'Planned' },
|
||||
{ name = 'Next', from = 'Planned', to = 'Assigned' },
|
||||
{ name = 'Reject', from = 'Planned', to = 'Rejected' },
|
||||
{ name = 'Next', from = 'Assigned', to = 'Success' },
|
||||
{ name = 'Fail', from = 'Assigned', to = 'Failed' },
|
||||
{ name = 'Fail', from = 'Arrived', to = 'Failed' }
|
||||
},
|
||||
subs = {
|
||||
Assign = { onstateparent = 'Planned', oneventparent = 'Next', fsm = ProcessAssign, event = 'Start', returnevents = { 'Next', 'Reject' } },
|
||||
Route = { onstateparent = 'Assigned', oneventparent = 'Next', fsm = ProcessRoute, event = 'Start' },
|
||||
Sead = { onstateparent = 'Assigned', oneventparent = 'Next', fsm = ProcessSEAD, event = 'Start', returnevents = { 'Next' } },
|
||||
--Smoke = { onstateparent = 'Assigned', oneventparent = 'Next', fsm = ProcessSmoke, event = 'Start', }
|
||||
}
|
||||
}
|
||||
|
||||
local Process = self:AddStateMachine( TaskUnit, STATEMACHINE_TASK:New( FSMT, self, TaskUnit ) )
|
||||
|
||||
Process:Next()
|
||||
|
||||
return self
|
||||
end
|
||||
|
||||
--- StateMachine callback function for a TASK
|
||||
-- @param #TASK_SEAD self
|
||||
-- @param StateMachine#STATEMACHINE_TASK Fsm
|
||||
-- @param #string Event
|
||||
-- @param #string From
|
||||
-- @param #string To
|
||||
-- @param Event#EVENTDATA Event
|
||||
function TASK_SEAD:onafterNext( Fsm, Event, From, To )
|
||||
|
||||
self:SetState( self, "State", To )
|
||||
|
||||
end
|
||||
|
||||
--- @param #TASK_SEAD self
|
||||
function TASK_SEAD:GetPlannedMenuText()
|
||||
return self:GetStateString() .. " - " .. self:GetTaskName() .. " ( " .. self.TargetSetUnit:GetUnitTypesText() .. " )"
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user