-
Notifications
You must be signed in to change notification settings - Fork 6
/
unit_cloaked_spy_comm_default_move.lua
109 lines (90 loc) · 3.15 KB
/
unit_cloaked_spy_comm_default_move.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
function widget:GetInfo()
return {
name = "Cloaked spy/comm default move",
desc = "Changes default command of cloaked spies and commanders to move",
author = "Errrrrrr, BrainDamage",
version = "1.0",
date = "April 19, 2023",
license = "GNU GPL, v2 or later",
layer = 0,
enabled = true,
}
end
local unitSelectedLimit = 12 -- SKIP widget behavior if selected unit count is greater than this number
local isCommander = {}
for unitDefID, unitDef in pairs(UnitDefs) do
if unitDef.customParams.iscommander then
isCommander[unitDefID] = true
end
end
local isSpy = {
[UnitDefNames.armspy.id] = true,
[UnitDefNames.corspy.id] = true,
}
local spGetSelectedUnitsSorted = Spring.GetSelectedUnitsSorted
local spGetUnitStates = Spring.GetUnitStates
local spGetSelectedUnitsCount = Spring.GetSelectedUnitsCount
local spGetMyTeamID = Spring.GetMyTeamID
local spySelected = false
local commSelected = false
local gameStarted = false
local selectionChanged = false
local CMD_MOVE = CMD.MOVE
local CMD_CLOAK = 37382
function maybeRemoveSelf()
if Spring.GetSpectatingState() and (Spring.GetGameFrame() > 0 or gameStarted) then
widgetHandler:RemoveWidget()
end
end
function widget:GameStart()
gameStarted = true
maybeRemoveSelf()
end
function widget:PlayerChanged(playerID)
maybeRemoveSelf()
end
function widget:Initialize()
if Spring.IsReplay() or Spring.GetGameFrame() > 0 then
maybeRemoveSelf()
end
end
function widget:SelectionChanged(sel)
selectionChanged = true
end
local selChangedSec = 0
function widget:Update(dt)
selChangedSec = selChangedSec + dt
if selectionChanged and selChangedSec > 0.1 then
selChangedSec = 0
selectionChanged = false
spySelected = false
commSelected = false
local numberSelected = spGetSelectedUnitsCount()
if numberSelected > unitSelectedLimit then return end -- skip if selecting too many units
local selectedUnitTypes = spGetSelectedUnitsSorted()
for unitDefID, units in pairs(selectedUnitTypes) do
if isCommander[unitDefID] or isSpy[unitDefID] then
for _, unitID in pairs(units) do
-- we only care if any of them is actually cloaked
if select(5,spGetUnitStates(unitID,false,true)) then
if isSpy[unitDefID] then spySelected = true end
if isCommander[unitDefID] then commSelected = true end
end
end
end
end
--Spring.Echo("Selection has comm: "..tostring(commSelected)..", has spy: "..tostring(spySelected))
end
end
-- We must detect cloak commands to update behavior even when selection is unchanged
function widget:UnitCommand(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOpts, cmdTag, playerID, fromSynced, fromLua)
if (cmdID == CMD_CLOAK) and (isCommander[unitDefID] or isSpy[unitDefID]) and (teamID == spGetMyTeamID()) then
--Spring.Echo("cloak command used!!")
selectionChanged = true
end
end
function widget:DefaultCommand()
if spySelected or commSelected then
return CMD_MOVE
end
end