Skip to content

Commit

Permalink
Implemented task force "tasks", replacing task force target zones (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
birgersp committed Jan 8, 2017
1 parent f29c1ac commit 1cc4ccf
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 38 deletions.
95 changes: 59 additions & 36 deletions autogft/taskforce.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- @type autogft_TaskForce
-- @field #number country Country ID
-- @field #list<#string> baseZones List of base zones
-- @field #list<controlzone#autogft_ControlZone> targetZones List of target zones
-- @field #list<taskforcetask#autogft_TaskForceTask> tasks List of tasks
-- @field #number speed Desired speed of moving units, in knots (default: max speed)
-- @field #number maxDistanceKM Maximum distance of task force routes between each advancement, in kilometres (default: 10)
-- @field #string formation Formation of moving units (default: "cone")
Expand All @@ -26,7 +26,7 @@ function autogft_TaskForce:new()
self = setmetatable({}, {__index = autogft_TaskForce})
self.country = country.id.RUSSIA
self.baseZones = {}
self.targetZones = {}
self.tasks = {}
self.speed = 100
self.maxDistanceKM = 10
self.formation = "cone"
Expand All @@ -39,6 +39,16 @@ function autogft_TaskForce:new()
return self
end

---
-- Adds a task to the task force
-- @param #autogft_TaskForce self
-- @param #autogft_TaskForceTask task
-- @return #autogft_TaskForce
function autogft_TaskForce:addTask(task)
self.tasks[#self.tasks + 1] = task
return self
end

---
-- Adds a group specification to declare which units the task force shall consist of.
-- If no count or type is specified, an empty group is added. Units can be added to the group with @{#autogft_TaskForce.addUnits}.
Expand Down Expand Up @@ -83,37 +93,52 @@ end
-- @return #autogft_TaskForce This object (self)
function autogft_TaskForce:updateTarget()

local redVehicles = mist.makeUnitTable({'[red][vehicle]'})
local blueVehicles = mist.makeUnitTable({'[blue][vehicle]'})

local done = false
local zoneIndex = 1
while done == false and zoneIndex <= #self.targetZones do
local zone = self.targetZones[zoneIndex]
local newStatus = nil
if #mist.getUnitsInZones(redVehicles, {zone.name}) > 0 then
newStatus = coalition.side.RED
end
local newTarget

if #mist.getUnitsInZones(blueVehicles, {zone.name}) > 0 then
if newStatus == coalition.side.RED then
newStatus = coalition.side.NEUTRAL
local ownCoalition = coalition.getCountryCoalition(self.country)
local enemyCoalition
if ownCoalition == coalition.side.RED then
enemyCoalition = coalition.side.BLUE
else
enemyCoalition = coalition.side.RED
end

local taskIndex = 1
while not newTarget and taskIndex <= #self.tasks do
local task = self.tasks[taskIndex]

if task.type == autogft_taskTypes.CONTROL then
local enemyUnits = autogft_getUnitsInZones(enemyCoalition, {task.zoneName})
if #enemyUnits > 0 then
task.cleared = false
else
newStatus = coalition.side.BLUE
local ownUnits = autogft_getUnitsInZones(ownCoalition, {task.zoneName})
if #ownUnits > 0 then
task.cleared = true
end
end
elseif task.type == autogft_taskTypes.CAPTURE then
local enemyUnits = autogft_getUnitsInZones(enemyCoalition, {task.zoneName})
if not task.cleared then
if #enemyUnits <= 0 then
local ownUnits = autogft_getUnitsInZones(ownCoalition, {task.zoneName})
if #ownUnits > 0 then
task.cleared = true
end
end
end
else
task.cleared = true
end

if newStatus ~= nil then
zone.status = newStatus
end

if zone.status ~= coalition.getCountryCoalition(self.country) then
self.target = zoneIndex
done = true
if not task.cleared then
newTarget = taskIndex
else
taskIndex = taskIndex + 1
end
zoneIndex = zoneIndex + 1
end


if newTarget then self.target = newTarget end
return self
end

Expand All @@ -122,7 +147,7 @@ end
-- @param #autogft_TaskForce self
-- @return #autogft_TaskForce This instance (self)
function autogft_TaskForce:advance()
assert(#self.targetZones > 0, "Task force has no target zones. Use \"addControlZone\" to add a target zone.")
assert(#self.tasks > 0, "Task force has no tasks. Use \"addControlTask\" to add a control zone task.")
for i = 1, #self.groups do
if self.groups[i]:exists() then self.groups[i]:advance() end
end
Expand Down Expand Up @@ -163,7 +188,7 @@ function autogft_TaskForce:setReinforceTimer(timeInterval, maxTime, useSpawning)

if maxTime ~= nil and maxTime > 0 then
local function killTimer()
self:stopReinforcing()
self:stopReinforcing()
end
self.stopReinforcementTimerId = autogft_scheduleFunction(killTimer, maxTime)
end
Expand Down Expand Up @@ -238,10 +263,8 @@ end
-- @param #string name Name of target control zone
-- @return #autogft_TaskForce This instance (self)
function autogft_TaskForce:addControlZone(name)
autogft_assertZoneExists(name)
local targetControlZone = autogft_ControlZone:new(name)
self.targetZones[#self.targetZones + 1] = targetControlZone
return self
local task = autogft_TaskForceTask:new(name, autogft_taskTypes.CONTROL)
return self:addTask(task)
end

---
Expand All @@ -254,7 +277,7 @@ function autogft_TaskForce:setSkill(skill)
return self
end



---
-- Sets the maximum distance of unit routes (see @{#autogft_TaskForce.maxDistanceKM}).
-- If set, this number constrains how far groups of the task force will move between each move command (advancement).
Expand Down Expand Up @@ -435,17 +458,17 @@ end
-- @param #autogft_TaskForce self
-- @return #autogft_TaskForce
function autogft_TaskForce:stopReinforcing()

if self.reinforcementTimerId then
timer.removeFunction(self.reinforcementTimerId)
self.reinforcementTimerId = nil
end

if self.stopReinforcementTimerId then
timer.removeFunction(self.stopReinforcementTimerId)
self.stopReinforcementTimerId = nil
end

return self
end

Expand Down
4 changes: 2 additions & 2 deletions autogft/taskforcegroup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function autogft_TaskForceGroup:updateDestination()

-- Check location of group lead
if groupLead then
local destinationZone = trigger.misc.getZone(self.taskForce.targetZones[self.destination].name)
local destinationZone = trigger.misc.getZone(self.taskForce.tasks[self.destination].zoneName)
if autogft_unitIsWithinZone(groupLead, destinationZone) then
-- If destination reached, update target
if self.destination < self.taskForce.target then
Expand Down Expand Up @@ -98,7 +98,7 @@ function autogft_TaskForceGroup:advance()
if self:exists() then
self:updateDestination()

local destinationZone = trigger.misc.getZone(self.taskForce.targetZones[self.destination].name)
local destinationZone = trigger.misc.getZone(self.taskForce.tasks[self.destination].zoneName)
local destinationZonePos2 = {
x = destinationZone.point.x,
y = destinationZone.point.z
Expand Down

0 comments on commit 1cc4ccf

Please sign in to comment.