forked from zxbc/BAR_widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_distributive_commands.lua
1104 lines (904 loc) · 31.5 KB
/
cmd_distributive_commands.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function widget:GetInfo()
return {
name = "Distributive Commands",
desc = "Distribute multiple commands onto selected units evenly. Press meta during shift queuing to distribute currently queued commands; press alt during shift to distribtue the longest queue among selected units.",
author = "Errrrrrr",
version = "1.0",
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true,
handler = true,
}
end
-------------------------------------------------------------------------------------
-- Press meta during shift queuing to distribute current queue to selected units
-- Press alt during shift to distribute the longest queue among selected units
-- Build commands are not processed (there is split build widget!)
-- Distribute after an area command will split the command onto units/features in area
-- Commands are always assigned to the closest units, with reasonable optimization
--
-- Custom keybind actions:
-- distributive_orders_distribute
-- distributive_orders_longestqueue
-------------------------------------------------------------------------------------
-- params
local custom_keybind_mode = false
local split_area_mode = true -- this mode allows for the distribute key to split area commands
-- *highly* recommend keeping this on, because longestQueue method
-- cannot distribute area settarget (it won't be in queue)
local insert_mode = false -- this is a legacy mode, no longer viable
-- How long should algorithms take. (0.05+ gives visible stutter, default: 0.03)
local maxHngTime = 0.03 -- Desired maximum time for hungarian algorithm
local maxNoXTime = 0.03 -- Strict maximum time for backup algorithm
-- Hungarian algorithm params
local defaultHungarianUnits = 20 -- Need a baseline to start from when no config data saved
local maxHungarianUnits = defaultHungarianUnits -- Also set when loading config
local minHungarianUnits = 10 -- If we kept reducing maxUnits it can get to a point where it can never increase, so we enforce minimums on the algorithms.
local unitIncreaseThresh = 0.85 -- We only increase maxUnits if the units are great enough for time to be meaningful
-- Vars
local cmdStash = {} -- { number id, params = table params, options = table opts }
local selectedUnits = {} -- {number unitID1, number unitID2, ...}
local nodes = {} --{number x, number y, number z, cmdItem cmd}
local selChanged = false
local heldDown = false
local osclock = os.clock
local tsort = table.sort
local floor = math.floor
local ceil = math.ceil
local sqrt = math.sqrt
local sin = math.sin
local cos = math.cos
local max = math.max
local huge = math.huge
local pi2 = 2*math.pi
-- Shortcuts
local echo = Spring.Echo
local maxUnits = Game.maxUnits
local spGetUnitPosition = Spring.GetUnitPosition
local spGetModKeyState = Spring.GetModKeyState
local spGetInvertQueueKey = Spring.GetInvertQueueKey
local spGetCommandQueue = Spring.GetCommandQueue
local spGetUnitPosition = Spring.GetUnitPosition
local spGetActiveCommand = Spring.GetActiveCommand
local spSetActiveCommand = Spring.SetActiveCommand
local spGetDefaultCommand = Spring.GetDefaultCommand
local spGetFeaturePosition = Spring.GetFeaturePosition
local spGetUnitCommands = Spring.GetUnitCommands
local spGiveOrderArrayToUnitArray = Spring.GiveOrderArrayToUnitArray
local spAreTeamsAllied = Spring.AreTeamsAllied
local spGetUnitsInCylinder = Spring.GetUnitsInCylinder
local spGetFeaturesInCylinder = Spring.GetFeaturesInCylinder
local spGetMyTeamID = Spring.GetMyTeamID
local spGetUnitTeam = Spring.GetUnitTeam
local spAreTeamsAllied = Spring.AreTeamsAllied
local GetUnitDefID = Spring.GetUnitDefID
local CMD_SETTARGET = 34923
-- What commands can be issued at a position or unit/feature ID (Only used by GetUnitPosition)
local positionCmds = {
[CMD.MOVE]=true, [CMD.ATTACK]=true, [CMD.RECLAIM]=true, [CMD.RESTORE]=true, [CMD.RESURRECT]=true,
[CMD.PATROL]=true, [CMD.CAPTURE]=true, [CMD.FIGHT]=true, [CMD.MANUALFIRE]=true, [CMD.REPAIR]=true,
[CMD.UNLOAD_UNIT]=true, [CMD.UNLOAD_UNITS]=true,[CMD.LOAD_UNITS]=true, [CMD.GUARD]=true, [CMD.AREA_ATTACK] = true,
[CMD_SETTARGET] = true -- set target
}
-- classification of possible area commands
-- the unclassified area commands should be issued at position
local hostileCmds = {
[CMD.ATTACK]=true, [CMD.CAPTURE]=true, [CMD_SETTARGET]=true
}
local friendlyCmds = { -- this is questionably useful
--[CMD.REPAIR]=true
}
local featureCmds = {
[CMD.RECLAIM]=true, [CMD.RESURRECT]=true
}
-- converts table 3 deep to string
function tableToString(t)
local result = ""
if type(t) ~= "table" then
result = tostring(t)
elseif t == nil then
result = "nil"
else
for k, v in pairs(t) do
result = result .. "[" .. tostring(k) .. "] = "
if type(v) == "table" then
result = result .. "{"
for k2, v2 in pairs(v) do
result = result .. "[" .. tostring(k2) .. "] = "
if type(v2) == "table" then
result = result .. "{"
for k3, v3 in pairs(v2) do
result = result .. "[" .. tostring(k3) .. "] = " .. tostring(v3) .. ", "
end
result = result .. "}, "
else
result = result .. tostring(v2) .. ", "
end
end
result = result .. "}, "
else
result = result .. tostring(v) .. ", "
end
end
end
return "{" .. result:sub(1, -3) .. "}"
end
-- deepcopy a table
local function deepCopy(original)
if type(original) ~= 'table' then
return original
end
local copy = {}
for key, value in next, original, nil do
copy[deepCopy(key)] = deepCopy(value)
end
return setmetatable(copy, getmetatable(original))
end
local function GetUnitDef(unitID)
local unitDefID = GetUnitDefID(unitID)
if unitDefID then
local unitDef = UnitDefs[unitDefID]
return unitDef
end
return nil
end
-- Initialize the widget
function widget:Initialize()
selectedUnits = {}
cmdStash = {}
nodes = {}
if custom_keybind_mode then
widgetHandler.actionHandler:AddAction(self, "distributive_cmds_distribute", distribute, nil, "p")
widgetHandler.actionHandler:AddAction(self, "distributive_cmds_longestqueue", distributeLongest, nil, "p")
widgetHandler.actionHandler:AddAction(self, "distributive_cmds_toggle_split_area", toggleSplitAreaMode, nil, "p")
end
end
function widget:Shutdown()
selectedUnits = {}
cmdStash = {}
nodes = {}
end
function widget:SelectionChanged(sel)
selChanged = true
selectedUnits = sel
cmdStash = {}
nodes = {}
end
function toggleSplitAreaMode(_, _, args)
split_area_mode = not split_area_mode
echo("Distributive Orders - Split Area Mode: "..(split_area_mode and "on" or "off"))
end
local function GetUnitWithLongestQueue()
local longestQueueUnitID = nil
local longestQueueLength = 0
local longestCommands = nil
for i = 1, #selectedUnits do
local unitID = selectedUnits[i]
local unitDef = GetUnitDef(unitID)
if unitDef and not unitDef.isBuilder then
local commands = spGetUnitCommands(unitID, -1)
if commands then
local queueLength = #commands
if queueLength > longestQueueLength then
longestQueueUnitID = unitID
longestQueueLength = queueLength
longestCommands = commands
end
end
end
end
return longestQueueUnitID, longestCommands
end
function distributeLongest(_, _, args)
--if not heldDown then return end
local unitID, commands = GetUnitWithLongestQueue()
if commands and #commands > 1 then
cmdStash = commands
updateNodes()
executeCommands(true)
end
end
function distribute(_, _, args)
if not heldDown then return end
executeCommands(true)
end
function widget:KeyPress(key, mods, isRepeat)
if key == 304 then
heldDown = true
end
if not custom_keybind_mode then
--[[ if mods.alt and key == 105 then -- alt-i
toggleSplitAreaMode()
end ]]
if key == 308 and mods.shift then
distributeLongest()
end
if key == 32 and mods.shift then
distribute()
end
end
end
function widget:KeyRelease(key, mods)
if heldDown and key == 304 then
heldDown = false
cmdStash = {}
nodes = {}
end
end
-- Execute all cmds in cmdStash
function executeCommands(noShift)
if #cmdStash == 0 then return end
local unitArr = {}
local orderArr = {}
local numUnits = #selectedUnits
local numCmds= #cmdStash
if #nodes < numUnits then return end
local orders
if (numUnits <= maxHungarianUnits) then
orders = GetOrdersHungarian(nodes, selectedUnits, numUnits, false)
else
orders = GetOrdersNoX(nodes, selectedUnits, numUnits, false)
end
for i=1, #orders do
local orderPair = orders[i]
local unitID = orderPair[1]
local nodeItem = orderPair[2]
local cmdItem = nodeItem[4]
local cmdID = cmdItem[1] or cmdItem.id
local options = cmdItem.options
local params = cmdItem.params
local altOpts = GetCmdOpts(true, false, false, false, false)
local cmdOpts
if insert_mode and (cmdItem[1] ~= 34923) and not queueSplit then -- settarget cannot be inserted
cmdOpts = GetCmdOpts(false, false, true, true, false)
GiveNotifyingOrderToUnit(unitArr, orderArr, orderPair[1], CMD.INSERT, {0, cmdID, cmdOpts.coded, unpack(params)}, altOpts)
else
if cmdItem[1] == 34923 or noShift then -- settarget must not be queued
cmdOpts = GetCmdOpts(options.alt, options.ctrl, false, false, false)
else
cmdOpts = GetCmdOpts(options.alt, options.ctrl, options.meta, not noShift, false)
end
GiveNotifyingOrderToUnit(unitArr, orderArr, unitID, cmdID, cmdItem.params, cmdOpts)
end
if (i == #orders and #unitArr > 0) or #unitArr >= 100 then
spGiveOrderArrayToUnitArray(unitArr, orderArr, true)
unitArr = {}
orderArr = {}
end
end
spGiveOrderArrayToUnitArray(unitArr, orderArr, true)
cmdStash = {} -- empty cmdStash when we're done
nodes = {}
end
-- This turns cmds into nodes for matching alg
function updateNodes()
local numUnits = #selectedUnits
local numCmds = #cmdStash
local numPerGroup = floor(numUnits/numCmds)
local numLeftOvers = numUnits % numCmds
local numInGroup = {} -- the precise number in each group
for i = 1, numCmds do
numInGroup[i] = numPerGroup
if i <= numLeftOvers then numInGroup[i] = numPerGroup + 1 end
end
if numUnits == 0 or numCmds == 0 then
nodes = {}
return false
end
-- bin nodes into coordinates of CmdStash elements
local count = 1
for i, cmdItem in ipairs(cmdStash) do
local cx, cy, cz = GetCommandPos(cmdItem)
-- node format: {number x, number y, number z, cmdItem cmd}
local curCommandNode = {cx, cy, cz, cmdItem}
for j = count, count + numInGroup[i] - 1 do
nodes[j] = curCommandNode
end
count = count + numInGroup[i]
end
if #nodes < numUnits then return false end
--echo("nodes updated!: "..tableToString(nodes))
end
function isHostile(unitID)
local myTeamID = spGetMyTeamID()
local unitTeamID = spGetUnitTeam(unitID)
-- Check if the unit's team is not allied with your team
return not spAreTeamsAllied(myTeamID, unitTeamID)
end
-- intercept commands issued during heldDown and save them
function widget:CommandNotify(id, cmdParams, cmdOpts)
-- if build, we skip this whole thing because split build could be used
--local alt, ctrl, meta, shift = GetModKeys()
if id < 0 then -- skip builds
heldDown = false
cmdStash = {}
nodes = {}
return false
end
-- add command to cmdStash if keys held
if heldDown then
-- { number id, params = table params, options = table opts }
--cmdOpts = GetCmdOpts(cmdOpts.alt, cmdOpts.ctrl, cmdOpts.meta,cmdOpts.shift,false)
local cmdItem = { id, params = cmdParams, options = cmdOpts }
if split_area_mode and cmdParams[4] and cmdParams[4] ~= 0 then
-- figure out what kind of units to filter
local areaUnits = spGetUnitsInCylinder(cmdParams[1],cmdParams[3],cmdParams[4])
local areaFeatures = spGetFeaturesInCylinder(cmdParams[1],cmdParams[3],cmdParams[4])
-- feature command
if featureCmds[id] then
-- simply grab all features and create commands
for i=1, #areaFeatures do
local cmdCopy = deepCopy(cmdItem)
cmdCopy.params = {areaFeatures[i]+32000}
if #cmdStash < #selectedUnits then cmdStash[#cmdStash+1] = cmdCopy end
end
end
-- friendly command
if friendlyCmds[id] then
for i=1, #areaUnits do
local unitID = areaUnits[i]
if not isHostile(unitID) then
local cmdCopy = deepCopy(cmdItem)
cmdCopy.params = {unitID}
if #cmdStash < #selectedUnits then cmdStash[#cmdStash+1] = cmdCopy end
end
end
end
-- hostile command
if hostileCmds[id] then
for i=1, #areaUnits do
local unitID = areaUnits[i]
if isHostile(unitID) then
local cmdCopy = deepCopy(cmdItem)
cmdCopy.params = {unitID}
if #cmdStash < #selectedUnits then cmdStash[#cmdStash+1] = cmdCopy end
end
end
end
updateNodes()
end
if #cmdStash < #selectedUnits then -- we will not queue more if we have more cmds than units
cmdStash[#cmdStash+1] = cmdItem
updateNodes()
--echo("CMD added! cmdID: "..tostring(id)..", name: "..tostring(CMD[id])..", cmdParams: "..tableToString(cmdParams)..", cmdOpts"..tableToString(cmdOpts))
end
return false -- to block or not to block
end
end
---------------------------------------------------------------------------------------------------------
-- Matching Algorithms
---------------------------------------------------------------------------------------------------------
function GetOrdersNoX(nodes, units, unitCount, shifted)
-- Remember when we start
-- This is for capping total time
-- Note: We at least complete initial assignment
local startTime = osclock()
---------------------------------------------------------------------------------------------------------
-- Find initial assignments
---------------------------------------------------------------------------------------------------------
local unitSet = {}
local fdist = -1
local fm
for u = 1, unitCount do
-- Get unit position
local ux, uz
if shifted then
ux, _, uz = GetUnitFinalPosition(units[u])
else
ux, _, uz = spGetUnitPosition(units[u])
end
if ux then
unitSet[u] = {ux, units[u], uz, -1} -- Such that x/z are in same place as in nodes (So we can use same sort function)
-- Work on finding furthest points (As we have ux/uz already)
for i = u - 1, 1, -1 do
local up = unitSet[i]
local vx, vz = up[1], up[3]
local dx, dz = vx - ux, vz - uz
local dist = dx*dx + dz*dz
if (dist > fdist) then
fdist = dist
fm = (vz - uz) / (vx - ux)
end
end
end
end
-- Maybe nodes are further apart than the units
for i = 1, unitCount - 1 do
local np = nodes[i]
if nil == np then break end
local nx, nz = np[1], np[3]
for j = i + 1, unitCount do
local mp = nodes[j]
if nil == mp then break end
local mx, mz = mp[1], mp[3]
local dx, dz = mx - nx, mz - nz
local dist = dx*dx + dz*dz
if (dist > fdist) then
fdist = dist
fm = (mz - nz) / (mx - nx)
end
end
end
local function sortFunc(a, b)
-- y = mx + c
-- c = y - mx
-- c = y + x / m (For perp line)
return (a[3] + a[1] / fm) < (b[3] + b[1] / fm)
end
tsort(unitSet, sortFunc)
tsort(nodes, sortFunc)
for u = 1, unitCount do
unitSet[u][4] = nodes[u]
end
---------------------------------------------------------------------------------------------------------
-- Main part of algorithm
---------------------------------------------------------------------------------------------------------
-- M/C for each finished matching
local Ms = {}
local Cs = {}
-- Stacks to hold finished and still-to-check units
local stFin = {}
local stFinCnt = 0
local stChk = {}
local stChkCnt = 0
-- Add all units to check stack
for u = 1, unitCount do
stChk[u] = u
end
stChkCnt = unitCount
-- Begin algorithm
while ((stChkCnt > 0) and (osclock() - startTime < maxNoXTime)) do
-- Get unit, extract position and matching node position
local u = stChk[stChkCnt]
local ud = unitSet[u]
local ux, uz = ud[1], ud[3]
local mn = ud[4]
local nx, nz = mn[1], mn[3]
-- Calculate M/C
local Mu = (nz - uz) / (nx - ux)
local Cu = uz - Mu * ux
-- Check for clashes against finished matches
local clashes = false
for i = 1, stFinCnt do
-- Get opposing unit and matching node position
local f = stFin[i]
local fd = unitSet[f]
local tn = fd[4]
-- Get collision point
local ix = (Cs[f] - Cu) / (Mu - Ms[f])
local iz = Mu * ix + Cu
-- Check bounds
if ((ux - ix) * (ix - nx) >= 0) and
((uz - iz) * (iz - nz) >= 0) and
((fd[1] - ix) * (ix - tn[1]) >= 0) and
((fd[3] - iz) * (iz - tn[3]) >= 0) then
-- Lines cross
-- Swap matches, note this retains solution integrity
ud[4] = tn
fd[4] = mn
-- Remove clashee from finished
stFin[i] = stFin[stFinCnt]
stFinCnt = stFinCnt - 1
-- Add clashee to top of check stack
stChkCnt = stChkCnt + 1
stChk[stChkCnt] = f
-- No need to check further
clashes = true
break
end
end
if not clashes then
-- Add checked unit to finished
stFinCnt = stFinCnt + 1
stFin[stFinCnt] = u
-- Remove from to-check stack (Easily done, we know it was one on top)
stChkCnt = stChkCnt - 1
-- We can set the M/C now
Ms[u] = Mu
Cs[u] = Cu
end
end
---------------------------------------------------------------------------------------------------------
-- Return orders
---------------------------------------------------------------------------------------------------------
local orders = {}
for i = 1, unitCount do
local unit = unitSet[i]
orders[i] = {unit[2], unit[4]}
end
return orders
end
function GetOrdersHungarian(nodes, units, unitCount, shifted)
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
-- (the following code is written by gunblob)
-- this code finds the optimal solution (slow, but effective!)
-- it uses the hungarian algorithm from http://www.public.iastate.edu/~ddoty/HungarianAlgorithm.html
-- if this violates gpl license please let gunblob and me know
-------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------
local t = osclock()
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
-- cache node<->unit distances
local distances = {}
--for i = 1, unitCount do distances[i] = {} end
for i = 1, unitCount do
local uID = units[i]
local ux, uz
if shifted then
ux, _, uz = GetUnitFinalPosition(uID)
else
ux, _, uz = spGetUnitPosition(uID)
end
if ux then
distances[i] = {}
local dists = distances[i]
for j = 1, unitCount do
local nodePos = nodes[j]
if nil == nodePos or nil == nodePos[1] or nil == nodePos[3] then break end
local dx, dz = nodePos[1] - ux, nodePos[3] - uz
dists[j] = floor(sqrt(dx*dx + dz*dz) + 0.5)
-- Integer distances = greatly improved algorithm speed
end
end
end
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
-- find optimal solution and send orders
local result = findHungarian(distances, unitCount)
--------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------
-- determine needed time and optimize the maxUnits limit
local delay = osclock() - t
if (delay > maxHngTime) and (maxHungarianUnits > minHungarianUnits) then
-- Delay is greater than desired, we have to reduce units
maxHungarianUnits = maxHungarianUnits - 1
else
-- Delay is less than desired, so thats OK
-- To make judgements we need number of units to be close to max
-- Because we are making predictions of time and we want them to be accurate
if (#units > maxHungarianUnits*unitIncreaseThresh) then
-- This implementation of Hungarian algorithm is O(n3)
-- Because we have less than maxUnits, but are altering maxUnits...
-- We alter the time, to 'predict' time we would be getting at maxUnits
-- We then recheck that against maxHngTime
local nMult = maxHungarianUnits / #units
if ((delay*nMult*nMult*nMult) < maxHngTime) then
maxHungarianUnits = maxHungarianUnits + 1
else
if (maxHungarianUnits > minHungarianUnits) then
maxHungarianUnits = maxHungarianUnits - 1
end
end
end
end
-- Return orders
local orders = {}
for i = 1, unitCount do
local rPair = result[i]
orders[i] = {units[rPair[1]], nodes[rPair[2]]}
end
return orders
end
function findHungarian(array, n)
-- Vars
local colcover = {}
local rowcover = {}
local starscol = {}
local primescol = {}
-- Initialization
for i = 1, n do
rowcover[i] = false
colcover[i] = false
starscol[i] = false
primescol[i] = false
end
-- Subtract minimum from rows
for i = 1, n do
local aRow = array[i]
local minVal = aRow[1]
for j = 2, n do
if aRow[j] < minVal then
minVal = aRow[j]
end
end
for j = 1, n do
aRow[j] = aRow[j] - minVal
end
end
-- Subtract minimum from columns
for j = 1, n do
local minVal = array[1][j]
for i = 2, n do
if array[i][j] < minVal then
minVal = array[i][j]
end
end
for i = 1, n do
array[i][j] = array[i][j] - minVal
end
end
-- Star zeroes
for i = 1, n do
local aRow = array[i]
for j = 1, n do
if (aRow[j] == 0) and not colcover[j] then
colcover[j] = true
starscol[i] = j
break
end
end
end
-- Start solving system
while true do
-- Are we done ?
local done = true
for i = 1, n do
if not colcover[i] then
done = false
break
end
end
if done then
local pairings = {}
for i = 1, n do
pairings[i] = {i, starscol[i]}
end
return pairings
end
-- Not done
local r, c = stepPrimeZeroes(array, colcover, rowcover, n, starscol, primescol)
stepFiveStar(colcover, rowcover, r, c, n, starscol, primescol)
end
end
function doPrime(array, colcover, rowcover, n, starscol, r, c, rmax, primescol)
primescol[r] = c
local starCol = starscol[r]
if starCol then
rowcover[r] = true
colcover[starCol] = false
for i = 1, rmax do
if not rowcover[i] and (array[i][starCol] == 0) then
local rr, cc = doPrime(array, colcover, rowcover, n, starscol, i, starCol, rmax, primescol)
if rr then
return rr, cc
end
end
end
return
else
return r, c
end
end
function stepPrimeZeroes(array, colcover, rowcover, n, starscol, primescol)
-- Infinite loop
while true do
-- Find uncovered zeros and prime them
for i = 1, n do
if not rowcover[i] then
local aRow = array[i]
for j = 1, n do
if (aRow[j] == 0) and not colcover[j] then
local i, j = doPrime(array, colcover, rowcover, n, starscol, i, j, i-1, primescol)
if i then
return i, j
end
break -- this row is covered
end
end
end
end
-- Find minimum uncovered
local minVal = huge
for i = 1, n do
if not rowcover[i] then
local aRow = array[i]
for j = 1, n do
if (aRow[j] < minVal) and not colcover[j] then
minVal = aRow[j]
end
end
end
end
-- There is the potential for minVal to be 0, very very rarely though. (Checking for it costs more than the +/- 0's)
-- Covered rows = +
-- Uncovered cols = -
for i = 1, n do
local aRow = array[i]
if rowcover[i] then
for j = 1, n do
if colcover[j] then
aRow[j] = aRow[j] + minVal
end
end
else
for j = 1, n do
if not colcover[j] then
aRow[j] = aRow[j] - minVal
end
end
end
end
end
end
function stepFiveStar(colcover, rowcover, row, col, n, starscol, primescol)
-- Star the initial prime
primescol[row] = false
starscol[row] = col
local ignoreRow = row -- Ignore the star on this row when looking for next
repeat
local noFind = true
for i = 1, n do
if (starscol[i] == col) and (i ~= ignoreRow) then
noFind = false
-- Unstar the star
-- Turn the prime on the same row into a star (And ignore this row (aka star) when searching for next star)
local pcol = primescol[i]
primescol[i] = false
starscol[i] = pcol
ignoreRow = i
col = pcol
break
end
end
until noFind
for i = 1, n do
rowcover[i] = false
colcover[i] = false
primescol[i] = false
end
for i = 1, n do
local scol = starscol[i]
if scol then
colcover[scol] = true
end
end
end
-- Helper functions from here
function GetModKeys()
local alt, ctrl, meta, shift = spGetModKeyState()
if spGetInvertQueueKey() then -- Shift inversion
shift = not shift
end
return alt, ctrl, meta, shift
end
function GetUnitFinalPosition(uID)
local ux, uy, uz = spGetUnitPosition(uID)
local cmds = spGetCommandQueue(uID,5000)
if cmds then
for i = #cmds, 1, -1 do
local cmd = cmds[i]
if (cmd.id < 0) or positionCmds[cmd.id] then
local params = cmd.params
if #params >= 3 then
return params[1], params[2], params[3]
else
if #params == 1 then
local pID = params[1]
local px, py, pz
if pID > maxUnits then
px, py, pz = spGetFeaturePosition(pID - maxUnits)
else
px, py, pz = spGetUnitPosition(pID)
end
if px then
return px, py, pz
end
end
end
end
end
end
return ux, uy, uz
end
function GiveNotifyingOrderToUnit(uArr, oArr, uID, cmdID, cmdParams, cmdOpts)
for _, w in ipairs(widgetHandler.widgets) do
if w.UnitCommandNotify and w:UnitCommandNotify(uID, cmdID, cmdParams, cmdOpts) then
return
end
end
uArr[#uArr + 1] = uID
oArr[#oArr + 1] = {cmdID, cmdParams, cmdOpts.coded}
return
end
function GetCmdOpts(alt, ctrl, meta, shift, right)
local opts = { alt=alt, ctrl=ctrl, meta=meta, shift=shift, right=right }
local coded = 0
if alt then coded = coded + CMD.OPT_ALT end
if ctrl then coded = coded + CMD.OPT_CTRL end
if meta then coded = coded + CMD.OPT_META end
if shift then coded = coded + CMD.OPT_SHIFT end
if right then coded = coded + CMD.OPT_RIGHT end
opts.coded = coded
return opts
end
function GetUnitOrFeaturePosition(id)
if id <= Game.maxUnits then
return Spring.GetUnitPosition(id)
else