-
Notifications
You must be signed in to change notification settings - Fork 4
/
taskforce.lua
603 lines (524 loc) · 19 KB
/
taskforce.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
---
-- AI units which can be set to automatically capture target zones, advance through captured zones and be reinforced when taking casualties.
-- @module TaskForce
---
-- @type TaskForce
-- @extends class#Class
-- @field #number country Country ID
-- @field #number coalition Coalition ID
-- @field #list<#string> baseZones List of base zones
-- @field #list<task#CaptureTask> 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 #boolean useRoads Wether the task force should use roads or not (default: false)
-- @field #string skill Skill of units (default: "High")
-- @field #list<group#Group> groups Groups of the task force
-- @field #number target Current target zone index
-- @field #number reinforcementTimerId Reinforcement timer identifier
-- @field #number stopReinforcementTimerId Reinforcement stopping timer identifier
-- @field #number advancementTimerId Advancement timer identifier
autogft_TaskForce = autogft_Class:create()
---
-- Creates a new task force instance.
-- To override, use ${TaskForce.setReinforceTimer} or ${TaskForce.setRespawnTimer}.
-- @param #TaskForce self
-- @return #TaskForce This instance (self)
function autogft_TaskForce:new()
self = self:createInstance()
self.country = nil
self.baseZones = {}
self.tasks = {}
self.speed = 100
self.maxDistanceKM = 10
self.useRoads = false
self.skill = "High"
self.groups = {}
self.target = 1
local function autoInitialize()
self:autoInitialize()
end
autogft.scheduleFunction(autoInitialize, 2)
return self
end
---
-- Specifies the task force to stop using roads when advancing through the next tasks that are added.
-- @param #TaskForce self
-- @return #TaskForce
function autogft_TaskForce:stopUsingRoads()
self.useRoads = false
return self
end
---
-- Specifies the task force to use roads when advancing through the next tasks that are added.
-- @param #TaskForce self
-- @return #TaskForce
function autogft_TaskForce:startUsingRoads()
self.useRoads = true
return self
end
---
-- Sets the maximum time reinforcements will keep coming.
-- @param #TaskForce self
-- @param #number time Time [seconds] until reinforcements will stop coming
-- @return #TaskForce
function autogft_TaskForce:setReinforceTimerMax(time)
if self.stopReinforcementTimerId then
timer.removeFunction(self.stopReinforcementTimerId)
end
local function killTimer()
self:stopReinforcing()
end
self.stopReinforcementTimerId = autogft.scheduleFunction(killTimer, time)
return self
end
---
-- Automatically initializes the task force by starting timers (if not started) and adding groups and units (if not added).
-- Default reinforcement timer intervals is 600 seconds. Default advancement timer intervals is 300 seconds.
-- @param #TaskForce self
-- @return #TaskForce
function autogft_TaskForce:autoInitialize()
if #self.groups <= 0 then
self:autoAddUnitLayoutFromBases()
self:reinforce()
end
if #self.baseZones > 0 then
if not self.reinforcementTimerId then
self:setRespawnTimer(600)
end
end
if not self.advancementTimerId then
self:setAdvancementTimer(300)
end
return self
end
---
-- Automatically adds groups and units.
-- Determines which groups and units that should be added to the task force by looking at a list of units and copying the layout.
-- @param #TaskForce self
-- @return #TaskForce
function autogft_TaskForce:autoAddUnitLayout(units)
if not self.country then
self:setCountry(units[1]:getCountry())
end
-- Create a table of groups {group = {type = count}}
local groupUnits = {}
-- Iterate through own base units
for _, unit in pairs(units) do
local dcsGroupId = unit:getGroup():getID()
-- Check if table has this group
if not groupUnits[dcsGroupId] then
groupUnits[dcsGroupId] = {}
end
-- Check if group has this type
local typeName = unit:getTypeName()
if not groupUnits[dcsGroupId][typeName] then
groupUnits[dcsGroupId][typeName] = 0
end
-- Count the number of units in this group of that type
groupUnits[dcsGroupId][typeName] = groupUnits[dcsGroupId][typeName] + 1
end
-- Iterate through the table of groups, add groups and units
for _, group in pairs(groupUnits) do
self:addGroup()
for type, count in pairs(group) do
self:addUnits(count, type)
end
end
return self
end
---
-- Looks through base zones for units and attempts to add the same layout to the task force (by invoking ${TaskForce.autoAddUnitLayout})
-- @param #TaskForce self
-- @return #TaskForce
function autogft_TaskForce:autoAddUnitLayoutFromBases()
-- Determine coalition based on units in base zones
local ownUnitsInBases = autogft.getUnitsInZones(coalition.side.BLUE, self.baseZones)
if #ownUnitsInBases <= 0 then
ownUnitsInBases = autogft.getUnitsInZones(coalition.side.RED, self.baseZones)
end
if #ownUnitsInBases > 0 then
self:autoAddUnitLayout(ownUnitsInBases)
end
return self
end
---
-- Stops the advancement timer
-- @param #TaskForce self
-- @return #TaskForce
function autogft_TaskForce:stopAdvancementTimer()
if self.advancementTimerId then
timer.removeFunction(self.advancementTimerId)
self.advancementTimerId = nil
end
return self
end
---
-- Adds an intermidiate zone task (see @{task#taskTypes.INTERMIDIATE}).
-- @param #TaskForce self
-- @param #string zoneName
-- @return #TaskForce
function autogft_TaskForce:addIntermidiateZone(zoneName)
return self:addTask(autogft_CaptureTask:new(zoneName, self.coalition))
end
---
-- Adds a task to the task force
-- @param #TaskForce self
-- @param task#Task task
-- @return #TaskForce
function autogft_TaskForce:addTask(task)
task.useRoads = self.useRoads
task.speed = self.speed
self.tasks[#self.tasks + 1] = task
return self
end
---
-- Adds another group specification to the task force.
-- After a group is added, use @{#TaskForce.addUnits} to add units.
-- See "unit-types" for a complete list of available unit types.
-- @param #TaskForce self
-- @return #TaskForce This instance (self)
function autogft_TaskForce:addGroup()
local unitSpec = autogft_UnitSpec:new(count, type)
self.groups[#self.groups + 1] = autogft_Group:new(self)
return self
end
---
-- Triggers task force reinforcing (invokes @{#TaskForce.reinforceFromUnits}) by either looking up units in base zones or spawning new units.
-- @param #TaskForce self
-- @param #boolean useSpawning (Optional) Specifies wether to spawn new units or use pre-existing units (default is false, using units located in base)
-- @return #TaskForce This instance (self)
function autogft_TaskForce:reinforce(useSpawning)
assert(#self.groups > 0, "Task force as no group/unit specifications.")
assert(#self.baseZones > 0, "Task force has no base zones. Use \"addBaseZone\" to add a base zone.")
local availableUnits
if not useSpawning then
availableUnits = {}
if not self.country then
availableUnits = autogft.getUnitsInZones(coalition.side.BLUE, self.baseZones)
if #availableUnits <= 0 then
availableUnits = autogft.getUnitsInZones(coalition.side.BLUE, self.baseZones)
end
if #availableUnits > 0 then
self:setCountry(availableUnits[1]:getCountry())
end
else
availableUnits = autogft.getUnitsInZones(self.coalition, self.baseZones)
end
end
return self:reinforceFromUnits(availableUnits)
end
---
-- Checks the status of tasks, and sets the current target task (@{#TaskForce.target}) of this task force.
-- @param #TaskForce self
-- @return #TaskForce This object (self)
function autogft_TaskForce:updateTarget()
local newTarget
local taskIndex = 1
while not newTarget and taskIndex <= #self.tasks do
if not self.tasks[taskIndex]:isAccomplished() then
newTarget = taskIndex
end
taskIndex = taskIndex + 1
end
if not newTarget then
newTarget = #self.tasks
end
self.target = newTarget
return self
end
---
-- Sets all units to move towards the current target.
-- @param #TaskForce self
-- @return #TaskForce This instance (self)
function autogft_TaskForce:advance()
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
return self
end
---
-- Starts a timer which updates the current target zone, and issues the task force units to engage it on given time intervals.
-- Invokes @{#TaskForce.moveToTarget}.
-- @param #TaskForce self
-- @param #number timeInterval Seconds between each target update
-- @return #TaskForce This instance (self)
function autogft_TaskForce:setAdvancementTimer(timeInterval)
self:stopAdvancementTimer()
local function updateAndAdvance()
self:updateTarget()
self:advance()
self.advancementTimerId = autogft.scheduleFunction(updateAndAdvance, timeInterval)
end
self.advancementTimerId = autogft.scheduleFunction(updateAndAdvance, timeInterval)
return self
end
---
-- Starts a timer which reinforces the task force.
-- @param #TaskForce self
-- @param #number timeInterval Time [seconds] between each reinforcement
-- @param #boolean useSpawning (Optional) Specifies wether to spawn new units or use pre-existing units (default: false)
-- @return #TaskForce This instance (self)
function autogft_TaskForce:setReinforceTimer(timeInterval, useSpawning)
self:stopReinforcing()
local function reinforce()
self:reinforce(useSpawning)
self.reinforcementTimerId = autogft.scheduleFunction(reinforce, timeInterval)
end
autogft.scheduleFunction(reinforce, 1)
return self
end
---
-- Checks if a particular unit is present in this task force.
-- @param #TaskForce self
-- @param DCSUnit#Unit unit Unit in question
-- @return #boolean True if this task force contains the unit, false otherwise.
function autogft_TaskForce:containsUnit(unit)
for groupIndex = 1, #self.groups do
if self.groups[groupIndex]:containsUnit(unit) then return true end
end
return false
end
---
-- Reinforce the task force by spawning new units.
-- @param #TaskForce self
-- @return #TaskForce This instance (self)
function autogft_TaskForce:respawn()
return self:reinforce(true)
end
---
-- Starts a timer which reinforces the task force by spawning new units.
-- @param #TaskForce self
-- @param #number timeInterval Time [seconds] between each reinforcement
-- @return #TaskForce This instance (self)
function autogft_TaskForce:setRespawnTimer(timeInterval)
return self:setReinforceTimer(timeInterval, true)
end
---
-- Sets the country ID of this task force.
-- @param #TaskForce self
-- @param #number country Country ID
-- @return #TaskForce This instance (self)
function autogft_TaskForce:setCountry(country)
self.country = country
self.coalition = coalition.getCountryCoalition(country)
-- Update capturing tasks coalition
for i = 1, #self.tasks do
local task = self.tasks[i]
if task:instanceOf(autogft_CaptureTask) then
task.coalition = self.coalition
end
end
return self
end
---
-- Adds a base zone to the task force, used for reinforcing (spawning or staging area).
-- @param #TaskForce self
-- @param #string baseZone Name of base zone
-- @return #TaskForce This instance (self)
function autogft_TaskForce:addBaseZone(baseZone)
autogft.assertZoneExists(baseZone)
self.baseZones[#self.baseZones + 1] = baseZone
return self
end
---
-- Adds a control zone task (see @{task#taskTypes.CONTROL}).
-- @param #TaskForce self
-- @param #string zoneName Name of target zone
-- @return #TaskForce This instance (self)
function autogft_TaskForce:addControlZone(zoneName)
return self:addTask(autogft_ControlTask:new(zoneName, self.coalition))
end
---
-- Sets the skill of the task force reinforcement units.
-- @param #TaskForce self
-- @param #string skill New skill
-- @return #TaskForce This instance (self)
function autogft_TaskForce:setSkill(skill)
self.skill = skill
return self
end
---
-- Sets the maximum distance of unit routes (see @{#TaskForce.maxDistanceKM}).
-- If set, this number constrains how far groups of the task force will move between each move command (advancement).
-- When units are moving towards a target, units will stop at this distance and wait for the next movement command.
-- This prevents lag when computing routes over long distances.
-- @param #TaskForce self
-- @param #number maxDistanceKM Maximum distance (kilometres)
-- @return #TaskForce This instance (self)
function autogft_TaskForce:setMaxRouteDistance(maxDistanceKM)
self.maxDistanceKM = maxDistanceKM
return self
end
---
-- Sets the desired speed of the task force units when advancing (see @{#TaskForce.speed}).
-- @param #TaskForce self
-- @param #boolean speed New speed (in knots)
-- @return #TaskForce This instance (self)
function autogft_TaskForce:setSpeed(speed)
self.speed = speed
if #self.tasks > 0 then self.tasks[#self.tasks].speed = self.speed end
return self
end
---
-- Attempts to reinforce the task force according to its unit specifications using a list of available units.
-- Reinforced units will immidiately start moving towards current target zone (see @{#TaskForce.target}).
-- @param #TaskForce self
-- @param #list<DCSUnit#Unit> availableUnits (Optional) Available units (if this list is not specified, units will be spawned)
-- @param #string groupNamePrefix (Optional) Name prefix to use when creating groups
-- @return #TaskForce This instance (self)
function autogft_TaskForce:reinforceFromUnits(availableUnits, groupNamePrefix)
local finished = false
local spawnedUnitCount, takenUnits, replacedUnitNameCounter, addedGroupUnitsCount, spawnZone
if availableUnits then
-- Cancel if the list of available units have a length of 0
if #availableUnits <= 0 then finished = true else takenUnits = {} end
else
spawnedUnitCount = 0
replacedUnitNameCounter = 0
end
local replacedGroupNameCounter = 0
-- Iterate through task force groups
local groupIndex = 1
while groupIndex <= #self.groups and not finished do
local group = self.groups[groupIndex]
if not group:exists() then
local groupUnits = {}
local function addGroupUnit(type, name, x, y, heading)
groupUnits[#groupUnits + 1] = {
["type"] = type,
["transportable"] =
{
["randomTransportable"] = false,
},
["x"] = x,
["y"] = y,
["heading"] = heading,
["name"] = name,
["skill"] = self.skill,
["playerCanDrive"] = true
}
addedGroupUnitsCount = addedGroupUnitsCount + 1
end
if not availableUnits then
spawnZone = trigger.misc.getZone(self.baseZones[math.random(#self.baseZones)])
end
for unitSpecIndex = 1, #group.unitSpecs do
local unitSpec = group.unitSpecs[unitSpecIndex]
addedGroupUnitsCount = 0
if availableUnits then
-- Use pre-existing units
local availableUnitIndex = 1
while addedGroupUnitsCount < unitSpec.count and availableUnitIndex <= #availableUnits do
local unit = availableUnits[availableUnitIndex]
if unit:isExist()
and not takenUnits[availableUnitIndex]
and unit:getTypeName() == unitSpec.type then
local x = unit:getPosition().p.x
local y = unit:getPosition().p.z
local heading = autogft.getUnitHeading(unit)
addGroupUnit(unitSpec.type, unit:getName(), x, y, heading)
takenUnits[availableUnitIndex] = true
end
availableUnitIndex = availableUnitIndex + 1
end
if #takenUnits >= #availableUnits then finished = true end
else
-- Spawn new units
while addedGroupUnitsCount < unitSpec.count do
local name
-- Find a unique unit name
while (not name) or Unit.getByName(name) do
replacedUnitNameCounter = replacedUnitNameCounter + 1
name = "autogft unit #" .. replacedUnitNameCounter
end
local x = spawnZone.point.x + 15 * spawnedUnitCount
local y = spawnZone.point.z - 15 * spawnedUnitCount
addGroupUnit(unitSpec.type, name, x, y, 0)
spawnedUnitCount = spawnedUnitCount + 1
end
end
end
if #groupUnits > 0 then
local groupName
-- Find a unique group name
while (not groupName) or Group.getByName(groupName) do
replacedGroupNameCounter = replacedGroupNameCounter + 1
groupName = "autogft group #" .. replacedGroupNameCounter
if groupNamePrefix then groupName = groupNamePrefix .. "-" .. groupName end
end
local dcsGroupData = {
["route"] = {},
["units"] = groupUnits,
["name"] = groupName
}
-- Create a group
local dcsGroup = coalition.addGroup(self.country, Group.Category.GROUND, dcsGroupData)
-- Issue group to control zone
self.groups[groupIndex]:setDCSGroup(dcsGroup)
self.groups[groupIndex]:advance()
end
end
groupIndex = groupIndex + 1
end
return self
end
---
-- Scans the map once for any pre-existing units to control in this task force.
-- Groups with name starting with the scan prefix will be considered.
-- A task force will only take control of units according to the task force unit specification.
-- @param #TaskForce self
-- @return #TaskForce
function autogft_TaskForce:scanUnits(groupNamePrefix)
local coalitionGroups = {
coalition.getGroups(coalition.side.BLUE),
coalition.getGroups(coalition.side.RED)
}
local availableUnits = {}
local coalition = 1
while coalition <= #coalitionGroups and #availableUnits == 0 do
for _, group in pairs(coalitionGroups[coalition]) do
if group:getName():find(groupNamePrefix) == 1 then
local units = group:getUnits()
for unitIndex = 1, #units do
availableUnits[#availableUnits + 1] = units[unitIndex]
end
end
end
coalition = coalition + 1
end
if #availableUnits > 0 then
if not self.country then
self:setCountry(availableUnits[1]:getCountry())
end
if #self.groups <= 0 then
self:autoAddUnitLayout(availableUnits)
end
self:reinforceFromUnits(availableUnits, groupNamePrefix)
end
return self
end
---
-- Stops the reinforcing/respawning timers.
-- @param #TaskForce self
-- @return #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
---
-- Adds unit specifications to the most recently added group (see @{#TaskForce.addGroup}) of the task force.
-- @param #TaskForce self
-- @return #TaskForce
function autogft_TaskForce:addUnits(count, type)
if #self.groups <= 0 then self:addGroup() end
self.groups[#self.groups]:addUnitSpec(autogft_UnitSpec:new(count, type))
return self
end