From 11c7cea1ded1795e9198279a06756058945485f1 Mon Sep 17 00:00:00 2001 From: FlightControl Date: Sat, 19 Mar 2016 01:45:15 +0100 Subject: [PATCH] New ESCORT class, first version ... This class does now detection reporting to the main client group. --- Moose/Base.lua | 1 + Moose/Client.lua | 2 +- Moose/Database.lua | 1 + Moose/Escort.lua | 109 +++++++++++++++++++++++++++++++++++++++++++++ Moose/Group.lua | 25 +++++++++++ Moose/Routines.lua | 2 +- Moose/Unit.lua | 11 +++++ 7 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 Moose/Escort.lua diff --git a/Moose/Base.lua b/Moose/Base.lua index 3fab73c90..3a1a56dc2 100644 --- a/Moose/Base.lua +++ b/Moose/Base.lua @@ -22,6 +22,7 @@ _TraceClass = { --CARGO_SLINGLOAD = true, --CARGO_ZONE = true, --CLEANUP = true, + ESCORT = true, } --- The BASE Class diff --git a/Moose/Client.lua b/Moose/Client.lua index e5438f541..332bce256 100644 --- a/Moose/Client.lua +++ b/Moose/Client.lua @@ -307,7 +307,7 @@ self:T() self.Messages[MessageId].MessageTime = timer.getTime() self.Messages[MessageId].MessageDuration = MessageDuration if MessageInterval == nil then - self.Messages[MessageId].MessageInterval = 600 + self.Messages[MessageId].MessageInterval = 0 else self.Messages[MessageId].MessageInterval = MessageInterval end diff --git a/Moose/Database.lua b/Moose/Database.lua index d20f79f23..27265637b 100644 --- a/Moose/Database.lua +++ b/Moose/Database.lua @@ -1,6 +1,7 @@ --- Administers the Initial Sets of the Mission Templates as defined within the Mission Editor. -- Administers the Spawning of new Groups within the DCSRTE and administers these new Groups within the DATABASE object(s). -- @module DATABASE +-- @author FlightControl Include.File( "Routines" ) Include.File( "Base" ) diff --git a/Moose/Escort.lua b/Moose/Escort.lua new file mode 100644 index 000000000..a9fbd3722 --- /dev/null +++ b/Moose/Escort.lua @@ -0,0 +1,109 @@ +--- Taking the lead of AI escorting your flight. +-- The ESCORT class allows you to interact with escoring AI on your flight and take the lead. +-- The following commands will be available: +-- +-- * Pop-up and Scan Area +-- * Re-Join Formation +-- * Hold Position in x km +-- * Report identified targets +-- * Perform tasks per identified target: Report vector to target, paint target, kill target +-- +-- @module ESCORT +-- @author FlightControl + +Include.File( "Routines" ) +Include.File( "Base" ) +Include.File( "Database" ) +Include.File( "Group" ) +Include.File( "Zone" ) + +--- ESCORT class +-- @type +-- +ESCORT = { + ClassName = "ESCORT", + EscortName = nil, -- The Escort Name + Targets = {}, -- The identified targets +} + +--- ESCORT class constructor for an AI group +-- @param self +-- @param #CLIENT EscortClient The client escorted by the EscortGroup. +-- @param #GROUP EscortGroup The group AI escorting the EscortClient. +-- @param #string EscortName Name of the escort. +-- @return #ESCORT self +function ESCORT:New( EscortClient, EscortGroup, EscortName ) + local self = BASE:Inherit( self, BASE:New() ) + self:T( { EscortClient, EscortGroup, EscortName } ) + + self.EscortClient = EscortClient + self.EscortGroup = EscortGroup + self.EscortName = EscortName + + self.ScanForTargetsFunction = routines.scheduleFunction( self._ScanForTargets, { self }, timer.getTime() + 1, 10 ) +end + +function ESCORT:_ScanForTargets() + self:T() + + if self.EscortGroup:IsAlive() then + local EscortTargets = self.EscortGroup:GetDetectedTargets() + + local EscortTargetMessages = "" + for EscortTargetID, EscortTarget in pairs( EscortTargets ) do + local EscortObject = EscortTarget.object + self:T( EscortObject ) + if EscortObject and EscortObject:isExist() and EscortObject.id_ < 50000000 then + + local EscortTargetMessage = "" + + local EscortTargetUnit = UNIT:New( EscortObject ) + + local EscortTargetCategoryName = EscortTargetUnit:GetCategoryName() + local EscortTargetCategoryType = EscortTargetUnit:GetTypeName() + + + -- local EscortTargetIsDetected, + -- EscortTargetIsVisible, + -- EscortTargetLastTime, + -- EscortTargetKnowType, + -- EscortTargetKnowDistance, + -- EscortTargetLastPos, + -- EscortTargetLastVelocity + -- = self.EscortGroup:IsTargetDetected( EscortObject ) + -- + -- self:T( { EscortTargetIsDetected, + -- EscortTargetIsVisible, + -- EscortTargetLastTime, + -- EscortTargetKnowType, + -- EscortTargetKnowDistance, + -- EscortTargetLastPos, + -- EscortTargetLastVelocity } ) + + if EscortTarget.distance then + local EscortTargetUnitPositionVec3 = EscortTargetUnit:GetPositionVec3() + local EscortPositionVec3 = self.EscortGroup:GetPositionVec3() + local Distance = routines.utils.get3DDist( EscortTargetUnitPositionVec3, EscortPositionVec3 ) / 1000 + self:T( { self.EscortGroup:GetName(), EscortTargetUnit:GetName(), Distance, EscortTarget.visible } ) + if Distance <= 8 then + EscortTargetMessage = EscortTargetMessage .. " - " .. EscortTargetCategoryName .. " (" .. EscortTargetCategoryType .. ") " + EscortTargetMessage = EscortTargetMessage .. string.format( "%.2f", Distance ) .. " km" + if EscortTarget.visible then + EscortTargetMessage = EscortTargetMessage .. " visual contact" + end + end + end + + if EscortTargetMessage ~= "" then + EscortTargetMessages = EscortTargetMessages .. EscortTargetMessage .. "\n" + end + end + end + + if EscortTargetMessages ~= "" then + self.EscortClient:Message( EscortTargetMessages:gsub("\n$",""), 20, "/ESCORT.DetectedTargets", self.EscortName .. " reporting detected targets within 8 km range:" ) + end + else + routines.removeFunction( self.ScanForTargetsFunction ) + end +end diff --git a/Moose/Group.lua b/Moose/Group.lua index 1103b13be..d1768fd21 100644 --- a/Moose/Group.lua +++ b/Moose/Group.lua @@ -130,6 +130,16 @@ function GROUP:GetPoint() return GroupPoint end +--- Gets the current Point of the GROUP in VEC3 format. +-- @return #Vec3 Current Vec3 position of the group. +function GROUP:GetPositionVec3() + self:T( self.GroupName ) + + local GroupPoint = self:GetUnit(1):GetPositionVec3() + self:T( GroupPoint ) + return GroupPoint +end + --- Destroy a GROUP -- Note that this destroy method also raises a destroy event at run-time. -- So all event listeners will catch the destroy event of this GROUP. @@ -441,3 +451,18 @@ function GROUP:_GetController() return self.DCSGroup:getController() end + +function GROUP:GetDetectedTargets() + + return self:_GetController():getDetectedTargets() + +end + +function GROUP:IsTargetDetected( DCSObject ) + + local TargetIsDetected, TargetIsVisible, TargetLastTime, TargetKnowType, TargetKnowDistance, TargetLastPos, TargetLastVelocity + = self:_GetController():isTargetDetected( DCSObject ) + + return TargetIsDetected, TargetIsVisible, TargetLastTime, TargetKnowType, TargetKnowDistance, TargetLastPos, TargetLastVelocity + +end \ No newline at end of file diff --git a/Moose/Routines.lua b/Moose/Routines.lua index c591ef9c4..d8a61bde6 100644 --- a/Moose/Routines.lua +++ b/Moose/Routines.lua @@ -422,7 +422,7 @@ routines.tostringBR = function(az, dist, alt, metric) dist = routines.utils.round(routines.utils.metersToNM(dist), 2) end - local s = string.format('%03d°', az) .. ' for ' .. dist + local s = string.format('%03d', az) .. ' for ' .. dist if alt then if metric then diff --git a/Moose/Unit.lua b/Moose/Unit.lua index 6432e3b10..7af365ca5 100644 --- a/Moose/Unit.lua +++ b/Moose/Unit.lua @@ -9,6 +9,13 @@ Include.File( "Message" ) -- @type UNIT = { ClassName="UNIT", + CategoryName = { + [Unit.Category.AIRPLANE] = "Airplane", + [Unit.Category.HELICOPTER] = "Helicoper", + [Unit.Category.GROUND_UNIT] = "Ground Unit", + [Unit.Category.SHIP] = "Ship", + [Unit.Category.STRUCTURE] = "Structure", + } } function UNIT:New( DCSUnit ) @@ -112,3 +119,7 @@ function UNIT:OtherUnitInRadius( AwaitUnit, Radius ) return false end +function UNIT:GetCategoryName() + return self.CategoryName[ self.DCSUnit:getDesc().category ] +end +