-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_context_select.lua
More file actions
318 lines (280 loc) · 10.2 KB
/
cmd_context_select.lua
File metadata and controls
318 lines (280 loc) · 10.2 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
local widgetName = "Context Select"
function widget:GetInfo()
return {
name = widgetName,
desc = [[
Adds a select action which tries to select units depending on cursor location.
If cursor is almost directly next to the unit it will select all units from the same group.
If unit is not in group it will select all visible units of this type.
Otherwise, it will select units close to the cursor. If selection didn't change it will go to the next step:
1. Units with relative hp (default <50%).
2. One builder. Repeating will cycle the builders in range.
3. One skuttle/spybot, close to the cursor
4. All radar and jammers, close to the cursor
5. Otherwise:
- If nothing could be selected in previous steps - cycle your labs across the whole map.
- If something could be selected in previous steps - select all units from the selected group/all visible units of selected type
(this one is mostly used for cycling between damaged units and their group)
]],
author = "SuperKitowiec",
version = 1.3,
layer = 0,
enabled = true
}
end
local debugMode = false
local config = {
healthThreshold = 50,
selectDamaged = true,
}
local OPTION_SPECS = {
{
configVariable = "healthThreshold",
name = "Select health",
description = "Will select units with this health %",
type = "slider",
min = 0,
max = 100,
step = 1,
value = 50,
},
{
configVariable = "selectDamaged",
name = "Select damaged",
description = "If true, will select units below 'Select health'. If false, will select units above 'Select health'",
type = "bool",
value = true,
}
}
local GetSelectedUnits = Spring.GetSelectedUnits
local SendCommands = Spring.SendCommands
local specialUnitNames = {
armspy = true, -- arm spybot
corspy = true, -- cor spybot
corsktl = true, -- cor skuttle
}
local radarAndJammerNames = {
armaser = true, -- arm bot jammer
armmark = true, -- arm bot radar
corspec = true, -- cor bot jammer
corvoyr = true, -- cor bot radar
armseer = true, -- arm veh radar
armjam = true, -- arm veh jammer
corvrad = true, -- cor veh radar
coreter = true, -- cor veh jammer
armpeep = true, -- arm t1 airscout
armsehak = true, -- arm seaplane scout
armawac = true, -- arm t2 airscout
corawac = true, -- cor t2 airscout
corfink = true, -- cor t1 airscout
corhunt = true, -- cor seaplane scout
}
local commanderNames = {
armcom = true,
corcom = true,
legcom = true,
}
local specialUnitsQuery, radarsAndJammersQuery, relativeHealthQuery, commanderQuery;
local function dump(o)
if type(o) == 'table' then
local s = '{ '
for k, v in pairs(o) do
if type(k) ~= 'number' then
k = '"' .. k .. '"'
end
s = s .. '[' .. k .. '] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
local function debug(message)
if debugMode then
Spring.SendCommands(string.format("say a:%s", dump(message)))
end
end
local function tableKeys(tbl)
local keys = {}
for k, _ in pairs(tbl) do
table.insert(keys, k)
end
return keys
end
local function getOptionValue(optionSpec)
if optionSpec.type == "slider" then
return config[optionSpec.configVariable]
elseif optionSpec.type == "bool" then
return config[optionSpec.configVariable]
elseif optionSpec.type == "select" then
for i, v in ipairs(optionSpec.options) do
if config[optionSpec.configVariable] == v then
return i
end
end
end
end
local function applyOptions()
relativeHealthQuery = config.selectDamaged
and "Not_RelativeHealth_" .. config.healthThreshold
or "RelativeHealth_" .. config.healthThreshold
end
local function setOptionValue(optionSpec, value)
if optionSpec.type == "slider" then
config[optionSpec.configVariable] = value
elseif optionSpec.type == "bool" then
config[optionSpec.configVariable] = value
elseif optionSpec.type == "select" then
config[optionSpec.configVariable] = optionSpec.options[value]
end
applyOptions()
end
local function createOnChange(optionSpec)
return function(i, value, force)
setOptionValue(optionSpec, value)
end
end
local function getOptionId(optionSpec)
return "cmd_context_select_" .. optionSpec.configVariable
end
local function createOptionFromSpec(optionSpec)
local option = table.copy(optionSpec)
option.configVariable = nil
option.enabled = nil
option.id = getOptionId(optionSpec)
option.widgetname = widgetName
option.value = getOptionValue(optionSpec)
option.onchange = createOnChange(optionSpec)
return option
end
local function setsEqual(set1, set2)
if #set1 ~= #set2 then
return false
end
local setMap = {}
for _, value in ipairs(set1) do
setMap[value] = (setMap[value] or 0) + 1
end
for _, value in ipairs(set2) do
if not setMap[value] or setMap[value] == 0 then
return false
end
setMap[value] = setMap[value] - 1
end
for _, count in pairs(setMap) do
if count ~= 0 then
return false
end
end
return true
end
local selectedUnits = {}
local cycleBuildings;
local function sendCommand(command, message)
SendCommands(command)
local newSelectedUnits = GetSelectedUnits();
local selectionNotChanged = setsEqual(selectedUnits, newSelectedUnits)
local isInvalidSelection = #newSelectedUnits == 0 or selectionNotChanged
if not isInvalidSelection and message ~= nil and debugMode then
debug(message)
end
if selectionNotChanged and #newSelectedUnits > 0 and #selectedUnits == 0 and debugMode then
debug("Selection didn't change - trying the next step...")
end
if #newSelectedUnits > 0 then
cycleBuildings = false
end
return isInvalidSelection
end
local function getBiggestUnitGroup(currentUnits)
local unitGroups = {}
local result
local currentMax = -1
for _, unitId in ipairs(currentUnits) do
local unitGroup = Spring.GetUnitGroup(unitId)
if unitGroup ~= nil then
if unitGroups[unitGroup] == nil then
unitGroups[unitGroup] = 1
else
unitGroups[unitGroup] = unitGroups[unitGroup] + 1
end
end
end
for groupId, count in pairs(unitGroups) do
if count > currentMax then
currentMax = count
result = groupId
end
end
return result
end
local function SmartSelect()
cycleBuildings = true
selectedUnits = GetSelectedUnits()
sendCommand("select FromMouseC_20+_Not_Building+_ClearSelection_SelectClosestToCursor+")
local newSelectedUnits = GetSelectedUnits()
if #newSelectedUnits > 0 then
local unitGroup = Spring.GetUnitGroup(newSelectedUnits[1])
if unitGroup ~= nil then
sendCommand("group " .. Spring.GetUnitGroup(newSelectedUnits[1]), "Selecting all units in the group")
else
sendCommand("select Visible+_InPrevSel+_ClearSelection_SelectAll+", "Selecting visible units of this type")
end
else
if sendCommand("select FromMouseC_400+_IdMatches_" .. specialUnitsQuery .. "+_ClearSelection_SelectClosestToCursor+", "Selecting single spybot or skuttle") then
if sendCommand("select FromMouseC_500+_Buildoptions_Not_Building+_ClearSelection_SelectNum_1+", "Cycling through nearby builders...") then
if sendCommand("select FromMouseC_600+_Not_Building_" .. relativeHealthQuery .. "+_ClearSelection_SelectAll+", "Selecting units with relative hp (default <50%)") then
--if sendCommand("select FromMouseC_400+_IdMatches_" .. radarsAndJammersQuery .. "+_ClearSelection_SelectAll+", "Selecting nearby radars and jammers") then
if cycleBuildings then
sendCommand("select AllMap+_IdMatches_" .. commanderQuery .. "+_ClearSelection_SelectNum_1+", "Cycling through commanders")
else
if #selectedUnits > 0 then
local unitGroup = getBiggestUnitGroup(selectedUnits)
Spring.SelectUnitArray(selectedUnits)
if unitGroup ~= nil then
debug("Selecting all units in group " .. unitGroup)
SendCommands("group " .. unitGroup)
else
debug("Selecting visible units of this type")
SendCommands("select Visible+_InPrevSel+_ClearSelection_SelectAll+")
end
end
end
--end
end
end
end
end
end
function widget:Initialize()
if WG['options'] ~= nil then
WG['options'].addOptions(table.map(OPTION_SPECS, createOptionFromSpec))
end
specialUnitsQuery = table.concat(tableKeys(specialUnitNames), "_IdMatches_")
radarsAndJammersQuery = table.concat(tableKeys(radarAndJammerNames), "_IdMatches_")
commanderQuery = table.concat(tableKeys(commanderNames), "_IdMatches_")
relativeHealthQuery = config.selectDamaged
and "Not_RelativeHealth_" .. config.healthThreshold
or "RelativeHealth_" .. config.healthThreshold
widgetHandler:AddAction("context_select", SmartSelect, nil, 'p')
end
function widget:Shutdown()
if WG['options'] ~= nil then
WG['options'].removeOptions(table.map(OPTION_SPECS, getOptionId))
end
end
function widget:GetConfigData()
local result = {}
for _, option in ipairs(OPTION_SPECS) do
result[option.configVariable] = getOptionValue(option)
end
return result
end
function widget:SetConfigData(data)
for _, option in ipairs(OPTION_SPECS) do
local configVariable = option.configVariable
if data[configVariable] ~= nil then
setOptionValue(option, data[configVariable])
end
end
end