From 0c591fa7f297c4f8325c392884e9156d3fc86d3f Mon Sep 17 00:00:00 2001 From: Birger Skogeng Pedersen Date: Sun, 8 Jan 2017 17:51:00 +0100 Subject: [PATCH] Implemented task force group destinations Groups will now move through previously captured zones when advancing as described in #20. --- autogft/core.lua | 15 ++++++++++++++ autogft/taskforcegroup.lua | 40 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/autogft/core.lua b/autogft/core.lua index 36b18b7..f702202 100644 --- a/autogft/core.lua +++ b/autogft/core.lua @@ -7,6 +7,21 @@ autogft_debugMode = false -- Utility function definitions +--- +-- @param DCSUnit#Unit unit +-- @param DCSZone#Zone zone +-- @return #boolean +function autogft_unitIsWithinZone(unit, zone) + local pos = unit:getPosition().p + local dx = zone.point.x - pos.x + local dy = zone.point.z - pos.z + local radiusSquared = zone.radius * zone.radius + if (dx*dx + dy*dy) <= radiusSquared then + return true + end + return false +end + --- -- @param #list units -- @param #string type diff --git a/autogft/taskforcegroup.lua b/autogft/taskforcegroup.lua index d51a450..d7919da 100644 --- a/autogft/taskforcegroup.lua +++ b/autogft/taskforcegroup.lua @@ -6,6 +6,7 @@ -- @field #list unitSpecs -- @field taskforce#autogft_TaskForce taskForce -- @field DCSGroup#Group dcsGroup +-- @field #number destination autogft_TaskForceGroup = {} --- @@ -16,6 +17,39 @@ function autogft_TaskForceGroup:new(taskForce) self = setmetatable({}, {__index = autogft_TaskForceGroup}) self.unitSpecs = {} self.taskForce = taskForce + self:setDCSGroup(nil) + return self +end + +--- +-- @param #autogft_TaskForceGroup self +-- @return #autogft_TaskForceGroup +function autogft_TaskForceGroup:updateDestination() + + if self.dcsGroup then + local units = self.dcsGroup:getUnits() + if #units > 0 then + -- Get group lead + local groupLead + local unitIndex = 1 + while unitIndex <= #units and not groupLead do + if units[unitIndex]:isExist() then groupLead = units[unitIndex] end + end + + -- Check location of group lead + if groupLead then + local destinationZone = trigger.misc.getZone(self.taskForce.targetZones[self.destination].name) + if autogft_unitIsWithinZone(groupLead, destinationZone) then + -- If destination reached, update target + if self.destination < self.taskForce.target then + self.destination = self.destination + 1 + elseif self.destination > self.taskForce.target then + self.destination = self.destination - 1 + end + end + end + end + end return self end @@ -61,9 +95,10 @@ end -- @param #autogft_TaskForceGroup self -- @return #autogft_TaskForceGroup function autogft_TaskForceGroup:advance() - if self:exists() then - local destinationZone = trigger.misc.getZone(self.taskForce.targetZones[self.taskForce.target].name) + self:updateDestination() + + local destinationZone = trigger.misc.getZone(self.taskForce.targetZones[self.destination].name) local destinationZonePos2 = { x = destinationZone.point.x, y = destinationZone.point.z @@ -117,5 +152,6 @@ end -- @return #autogft_TaskForceGroup function autogft_TaskForceGroup:setDCSGroup(newGroup) self.dcsGroup = newGroup + self.destination = 1 return self end