forked from FAForever/fa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspreadattack.lua
More file actions
460 lines (399 loc) · 18.7 KB
/
spreadattack.lua
File metadata and controls
460 lines (399 loc) · 18.7 KB
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
------------------------------------------------------------------------
-- --
-- File : /mod/TestCommandHook/spreadattack.lua --
-- Author : Magic Power --
-- --
-- Summary : Hooks an ingame key and spreads the attack orders given --
-- --
------------------------------------------------------------------------
-- This table contains a shadow copy of a subset of the real orders.
ShadowOrders = {}
-----------------------------------------
-- Function MakeShadowCopyOrders(command)
-----------------------------------------
-- Global variables needed for this function:
-- Some orders need to be changed for them to work when garbling orders.
-- Note that the Stop command is handled implicitly by the clear bit being set.
TranslatedOrder = {
["AggressiveMove"] = "AggressiveMove",
["Attack"] = "Attack",
["Capture"] = "Capture",
["Guard"] = "Guard",
["Move"] = "Move",
["Nuke"] = "Nuke",
["OverCharge"] = "OverCharge",
["Patrol"] = "Patrol",
["Reclaim"] = "Reclaim",
["Repair"] = "Repair",
["Tactical"] = "Tactical",
["FormAggressiveMove"] = "AggressiveMove", -- Form actions currently not supported
["FormAttack"] = "Attack", -- Form actions currently not supported
["FormMove"] = "Move", -- Form actions currently not supported
["FormPatrol"] = "Patrol", -- Form actions currently not supported
}
-- This function makes a shadow copy of the orders given to the units.
-- Due to it's use, only a subset of the orders will be kept.
function MakeShadowCopyOrders(command)
-- If the order has the Clear bit set, then all previously issued orders will be removed first,
-- even if the specific order will not be handled below.
-- This conveniently also handles the Stop order (= clear all orders).
if command.Clear == true then
for _,unit in ipairs(command.Units) do
ShadowOrders[unit:GetEntityId()] = {}
end
end
-- Skip handling the order if it does not belong to the given subset.
if not TranslatedOrder[command.CommandType] then
return
end
local Order = {
CommandType = TranslatedOrder[command.CommandType],
Position = command.Target.Position,
EntityId = nil,
}
if command.Target.Type == "Entity" then
Order.EntityId = command.Target.EntityId
end
-- Add this order to each individual unit.
for _,unit in ipairs(command.Units) do
-- Initialise the orders table, if needed.
if not ShadowOrders[unit:GetEntityId()] then
ShadowOrders[unit:GetEntityId()] = {}
end
table.insert(ShadowOrders[unit:GetEntityId()],Order)
end
end -- function MakeShadowCopyorders(command)
---------------------------
-- Function FixOrders(unit)
---------------------------
-- This function tries to fix a unit's moved or deleted shadow orders
-- based on its current command queue.
-- It can only get ability names and positions of current orders,
-- so it can't retarget orders with a changed entity target.
function FixOrders(unit)
-- The factory exclusion is because GetCommandQueue doesn't work right for them.
-- This means we can't fix orders for Fatboy, Megalith, Tempest, or carriers.
if not unit or unit:IsInCategory("FACTORY") then
return
end
local unitOrders = ShadowOrders[unit:GetEntityId()]
if not unitOrders or not unitOrders[1] then
return
end
local queue = unit:GetCommandQueue()
local filteredQueue = {}
for _,command in ipairs(queue) do
local Order = {
CommandType = TranslatedOrder[command.type],
Position = command.position,
}
if Order.CommandType then
table.insert(filteredQueue, Order)
end
end
local numOrders = table.getn(unitOrders)
-- We can't trust the shadow orders if commands were added without getting a copy.
if numOrders < table.getn(filteredQueue) then
WARN("Spreadattack: Command queue is longer than the shadow order list.")
return
end
-- First check for entire blocks of orders that have been deleted.
local orderIndex = 1
local queueIndex = 1
local orderType = false
local lastBlockType = false
while orderIndex <= numOrders do
orderType = unitOrders[orderIndex].CommandType
local nextOrderIndex = orderIndex + 1
while unitOrders[nextOrderIndex].CommandType == orderType do
nextOrderIndex = nextOrderIndex + 1
end
if orderType == lastBlockType then
orderIndex = nextOrderIndex
continue
end
local nextQueueIndex = queueIndex
while filteredQueue[nextQueueIndex].CommandType == orderType do
nextQueueIndex = nextQueueIndex + 1
end
if nextQueueIndex == queueIndex then
-- Block not found.
for i = nextOrderIndex - 1, orderIndex, -1 do
table.remove(unitOrders, i)
numOrders = numOrders - 1
end
nextOrderIndex = orderIndex
else
lastBlockType = orderType
end
orderIndex = nextOrderIndex
queueIndex = nextQueueIndex
end
-- Now fix the orders within each block of the same type.
orderIndex = 1
queueIndex = 1
while orderIndex <= numOrders do
orderType = unitOrders[orderIndex].CommandType
local nextOrderIndex = orderIndex + 1
local numEntityTargets = 0
while unitOrders[nextOrderIndex].CommandType == orderType do
if unitOrders[nextOrderIndex].EntityId then
numEntityTargets = numEntityTargets + 1
end
nextOrderIndex = nextOrderIndex + 1
end
local nextQueueIndex = queueIndex
while filteredQueue[nextQueueIndex].CommandType == orderType do
nextQueueIndex = nextQueueIndex + 1
end
-- Check if orders were removed from the queue and try to identify them.
local numDeletedOrders = nextOrderIndex - orderIndex - (nextQueueIndex - queueIndex)
if numDeletedOrders ~= 0 then
if numEntityTargets == 0 then
-- With only position targets it doesn't matter which orders we delete.
while numDeletedOrders > 0 do
table.remove(unitOrders, nextOrderIndex - 1)
numOrders = numOrders - 1
nextOrderIndex = nextOrderIndex - 1
numDeletedOrders = numDeletedOrders - 1
end
-- Fix the positions of any moved orders.
for i = 0, nextOrderIndex - orderIndex - 1, 1 do
if not unitOrders[i + orderIndex].EntityId then
unitOrders[i + orderIndex].Position = filteredQueue[i + queueIndex].Position
end
end
else
-- This part is only for the most complex situations and shouldn't be needed often in practice.
-- Here we go through the block of orders and try to determine which to remove. Priority for removal:
-- 1. Entity targets with a valid current position that don't match any command position (could have changed target)
-- 2. position targets that don't match any command position (could have moved)
-- 3. Entity targets with unknown current position that don't match any command position (could have moved or changed)
local Matches = {}
local lastMatchIndex = 0
local lastMatchQueueIndex = queueIndex - 1
local lastMatchAlignment = orderIndex - queueIndex
for i = orderIndex, nextOrderIndex - 1, 1 do
local match = false
local priority = 2
local position = unitOrders[i].Position
if unitOrders[i].EntityId then
priority = 3
local target = GetUnitById(unitOrders[i].EntityId)
if target then
position = target:GetPosition()
priority = 1
end
end
for j = lastMatchQueueIndex + 1, nextQueueIndex - 1, 1 do
if VDist3Sq(position, filteredQueue[j].Position) <= 0.0001 then
-- If the shadow orders and command queue have the same number of entries since the last match,
-- mark any mismatches in between as matches since no orders were removed.
if i - j == lastMatchAlignment and j > lastMatchQueueIndex + 1 then
for k = 1, j - lastMatchQueueIndex - 1, 1 do
Matches[k + lastMatchIndex].Priority = false
Matches[k + lastMatchIndex].Match = k + lastMatchQueueIndex
end
else
lastMatchAlignment = i - j
end
match = j
priority = false
lastMatchQueueIndex = j
lastMatchIndex = table.getn(Matches) + 1
break
end
end
table.insert(Matches, {Match = match, Priority = priority})
end
-- Delete unmatched commands by priority.
for priority = 1, 3, 1 do
if numDeletedOrders <= 0 then
break
end
for i = table.getn(Matches), 1, -1 do
if Matches[i].Priority == priority then
table.remove(Matches, i)
table.remove(unitOrders, i + orderIndex - 1)
numOrders = numOrders - 1
nextOrderIndex = nextOrderIndex - 1
numDeletedOrders = numDeletedOrders - 1
if numDeletedOrders <= 0 then
break
end
end
end
end
-- Fix the positions of any moved orders.
local positionIndex = Matches[1].Match or queueIndex
for i = 0, nextOrderIndex - orderIndex - 1, 1 do
if not unitOrders[i + orderIndex].EntityId then
if not Matches[i + 1].Match then
Matches[i + 1].Match = positionIndex
end
unitOrders[i + orderIndex].Position = filteredQueue[Matches[i + 1].Match].Position
end
positionIndex = (Matches[i + 1].Match or positionIndex) + 1
end
end
else
-- No orders were deleted so just fix any moved orders with position targets.
for i = 0, nextOrderIndex - orderIndex - 1, 1 do
if not unitOrders[i + orderIndex].EntityId then
unitOrders[i + orderIndex].Position = filteredQueue[i + queueIndex].Position
end
end
end
orderIndex = nextOrderIndex
queueIndex = nextQueueIndex
end
end
--------------------------
-- Function SpreadAttack()
--------------------------
-- This function rearranges all targeted orders randomly for every unit.
function SpreadAttack()
-- Get the currently selected units.
local curSelection = GetSelectedUnits()
if not curSelection then
return
end
-- Switch the orders for each unit.
for index,unit in ipairs(curSelection) do
FixOrders(unit)
local unitOrders = ShadowOrders[unit:GetEntityId()]
-- Only mix orders if this unit has any orders to mix.
if not unitOrders or not unitOrders[1] then
continue
end
-- Find all consecutive mixable orders, and only mix those.
local beginAction,endAction,action,counter,actionAlwaysMixed = nil,nil,nil,1,false
local alwaysMix = {"Attack", "Nuke", "Tactical"}
while unitOrders[counter] ~= nil do
beginAction = nil
-- Search for the first entry of a mixable order.
while beginAction == nil and unitOrders[counter] ~= nil do
for _,v in ipairs(alwaysMix) do
if unitOrders[counter].CommandType == v then
beginAction = counter
action = unitOrders[counter].CommandType
actionAlwaysMixed = true
break
elseif unitOrders[counter].EntityId then
beginAction = counter
action = unitOrders[counter].CommandType
actionAlwaysMixed = false
end
end
counter = counter + 1
end
endAction = beginAction
-- Search for the last entry of a mixable order in this series.
while unitOrders[counter] ~= nil do
if unitOrders[counter].CommandType == action and (actionAlwaysMixed or unitOrders[counter].EntityId) then
endAction = counter
counter = counter + 1
else
break
end
end
-- Skip if there was no mixable order found, or only one order (can't swap one command).
if beginAction == nil or endAction == beginAction then
break
end
-- Rearrange the first few mixable orders (equal to the number of targets) so that the targets are uniformly distributed on the first pass.
-- For example, 3 units attacking 8 units (? denotes random target):
-- Unit 1: 1, 4, 7, ?, ?, ?, ?, ?
-- Unit 2: 2, 5, 8, ?, ?, ?, ?, ?
-- Unit 3: 3, 6, ?, ?, ?, ?, ?, ?
local unitCount = table.getn(curSelection)
local numOrders = endAction - beginAction + 1
-- "and 1 or 0" is lua's ugly alternative to the ternary operator. Same as (...) ? 1 : 0
local stableTargetNum = math.floor(numOrders / unitCount) + ((math.mod(numOrders, unitCount) >= index) and 1 or 0)
for i = 0, stableTargetNum - 1 do
-- For if the targets outnumber the units targeting them.
local targetBlock = i * unitCount
unitOrders[i + beginAction],unitOrders[targetBlock + beginAction + index - 1]
= unitOrders[targetBlock + beginAction + index - 1],unitOrders[i + beginAction]
end
beginAction = beginAction + stableTargetNum
-- Randomize the remaining mixable orders.
for i = beginAction,endAction do
local randomorder = math.random(beginAction,endAction)
if randomorder ~= i then
unitOrders[i],unitOrders[randomorder] = unitOrders[randomorder],unitOrders[i]
end
end
-- Repeat this loop and search for more mixable order series.
end
-- All targeted orders have been mixed, now it's time to reassign those orders.
-- Since giving orders is a Sim-side command, use a SimCallback function.
SimCallback( {
Func = "GiveOrders",
Args = {
unit_orders = unitOrders,
unit_id = unit:GetEntityId(),
From = GetFocusArmy()
}
}, false)
-- Handle the next unit.
end
end -- Function SpreadAttack()
----------------------------
-- Function GiveOrders(Data)
----------------------------
local IssueOrderFunctions = nil
-- This function re-issues all shadow orders to the selected units.
-- Since the orders herein are Sim-side commands, this function needs to be called through a SimCallback.
function GiveOrders(Data)
-- Doing this here ensures it's done in sim code instead of UI code.
if not IssueOrderFunctions then
IssueOrderFunctions = {
["Attack"] = IssueAttack,
["Move"] = IssueMove,
["Guard"] = IssueGuard,
["Tactical"] = IssueTactical,
--["AggressiveMove"] = IssueAggressiveMove,
--["Capture"] = IssueCapture,
--["Nuke"] = IssueNuke,
--["OverCharge"] = IssueOverCharge,
--["Patrol"] = IssuePatrol,
--["Repair"] = IssueRepair,
--["Reclaim"] = IssueReclaim,
}
end
if OkayToMessWithArmy(Data.From) then --Check for cheats/exploits
local unit = GetEntityById(Data.unit_id)
-- Skip units with no valid shadow orders.
if not Data.unit_orders or not Data.unit_orders[1] then
return
end
if unit:GetBlueprint().CategoriesHash.BOMBER then
for key, order in Data.unit_orders or {} do
if order.CommandType == "Move" then
local bomberPosition = unit:GetPosition()
--reject all move orders that are closer than 20
if VDist2(bomberPosition[1], bomberPosition[3], order.Position[1], order.Position[3]) < 20 then
table.remove (Data.unit_orders, key)
end
end
end
end
-- All orders will be re-issued, so all existing orders have to be cleared first.
IssueClearCommands({ unit })
-- Re-issue all orders.
for _,order in ipairs(Data.unit_orders) do
local Function = IssueOrderFunctions[order.CommandType]
if not Function then
continue
end
local target = order.Position
if order.EntityId then
target = GetEntityById(order.EntityId)
end
if target then
Function({ unit }, target)
end
end
end
end -- function GiveOrders(Data)