-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClaymoreBombing.lua
422 lines (374 loc) · 13.2 KB
/
ClaymoreBombing.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
function widget:GetInfo()
return {
name = "ClaymoreBombing",
desc = "Allows Claymores to drop bombs while transported. Version 3.20",
author = "terve886",
date = "2019",
license = "PD", -- should be compatible with Spring
layer = 0,
handler = true, --for adding customCommand into UI
enabled = true -- loaded by default?
}
end
local UPDATE_FRAME=1
local currentFrame = 0
local CMD_FORCE_DROP_UNIT = 35000
local CMD_INSERT = 1
local CMD_DROP_BOMB = 35000
local GiveOrderToUnit = Spring.GiveOrderToUnit
local GetUnitDefID = Spring.GetUnitDefID
local GetUnitPosition = Spring.GetUnitPosition
local GetGroundHeight = Spring.GetGroundHeight
local GetMyAllyTeamID = Spring.GetMyAllyTeamID
local GetMyTeamID = Spring.GetMyTeamID
local GetTeamUnits = Spring.GetTeamUnits
local GetUnitsInCylinder = Spring.GetUnitsInCylinder
local GetUnitAllyTeam = Spring.GetUnitAllyTeam
local Echo = Spring.Echo
local Charon_NAME = "gunshiptrans"
local Claymore_NAME = "hoverdepthcharge"
local Hercules_NAME = "gunshipheavytrans"
local Imp_NAME = "cloakbomb"
local Scuttle_NAME = "jumpbomb"
local Snitch_NAME = "shieldbomb"
local Limpet_NAME = "amphbomb"
local GetSpecState = Spring.GetSpectatingState
local GetUnitIsTransporting = Spring.GetUnitIsTransporting
local GetUnitWeaponState = Spring.GetUnitWeaponState
local CMD_INSERT = CMD.INSERT
local CMD_LOAD_UNITS = CMD.LOAD_UNITS
local CMD_UNLOAD_UNITS = CMD.UNLOAD_UNITS
local CMD_OPT_SHIFT = CMD.OPT_SHIFT
local CMD_TIMEWAIT = CMD.TIMEWAIT
local CMD_STOP = CMD.STOP
local selectedTransports = nil
local reloaderStack = {}
local TransporterStack = {}
local CMD_ATTACK_MOVE_ID = 16
local CMD_FIND_PAD = 33411
local CMD_SET_RELOAD_ZONE = 19894
local CMD_DROP_CLAYMORE_BOMB = 19893
local CMD_RELOAD_CLAYMORE = 19892
local CharonUnitDefID = UnitDefNames["gunshiptrans"].id
local HerculesUnitDefID = UnitDefNames["gunshipheavytrans"].id
local cmdReloadClaymore = {
id = CMD_RELOAD_CLAYMORE,
type = CMDTYPE.ICON,
tooltip = 'Makes transport land Claymore with the purpose of letting it reload before lifting it up again',
action = 'repair',
params = { },
texture = 'LuaUI/Images/plus_green.png',
pos = {CMD_ONOFF,CMD_REPEAT,CMD_MOVE_STATE,CMD_FIRE_STATE, CMD_RETREAT},
}
local cmdDropClaymoreBomb = {
id = CMD_DROP_CLAYMORE_BOMB,
type = CMDTYPE.ICON,
tooltip = 'Makes transport force claymore bomb drop',
action = 'reclaim',
params = {},
texture = 'LuaUI/Images/commands/states/divebomb_shield.png',
pos = {CMD_ONOFF,CMD_REPEAT,CMD_MOVE_STATE,CMD_FIRE_STATE, CMD_RETREAT},
}
local cmdSetReloadingZone = {
id = CMD_SET_RELOAD_ZONE,
type = CMDTYPE.ICON_MAP,
tooltip = 'Sets automatic resupply Zone for the transport',
action = '',
params = {},
texture = 'LuaUI/Images/commands/Bold/drop_flag.png',
pos = {CMD_ONOFF,CMD_REPEAT,CMD_MOVE_STATE,CMD_FIRE_STATE, CMD_RETREAT},
}
local reloadController = {
unitID,
targetFrame,
pickupTarget,
new = function(self, unitID, target, reload)
self = deepcopy(self)
self.unitID = unitID
self.targetFrame = currentFrame+50
self.pickupTarget = target
return self
end,
handle = function(self)
if (currentFrame>self.targetFrame)then
local reload = GetUnitWeaponState(self.pickupTarget, 1, "reloadState")
if (reload) then
if(currentFrame > reload)then
GiveOrderToUnit(self.unitID, CMD_INSERT,{0, CMD_LOAD_UNITS, CMD_OPT_SHIFT, self.pickupTarget}, {"alt"})
reloaderStack[self.unitID] = nil
return true
else
self.targetFrame=reload
return false
end
end
end
end
}
local transportController = {
unitID,
pos,
allyTeamID = GetMyAllyTeamID(),
reloadZone,
autoReloadToggle = true,
fight = false,
fightPos,
new = function(self, unitID)
--Echo("transportController added:" .. unitID)
self = deepcopy(self)
self.unitID = unitID
self.pos = {GetUnitPosition(self.unitID)}
return self
end,
unset = function(self)
--Echo("transportController removed:" .. self.unitID)
GiveOrderToUnit(self.unitID,CMD_STOP, {}, {""},1)
return nil
end,
getToggleState = function(self)
return self.autoReloadToggle
end,
toggleOn = function (self)
self.autoReloadToggle = true
end,
toggleOff = function (self)
self.autoReloadToggle = false
end,
toggleFightOn = function (self)
self.fight = true
end,
toggleFightOff = function (self)
self.fight = false
end,
setReloadZone = function(self, coordinates)
self.reloadZone = coordinates
end,
handle = function(self)
if(self.fight)then
self.pos = {GetUnitPosition(self.unitID)}
local units = GetUnitsInCylinder(self.pos[1], self.pos[3], 50)
for i=1, #units do
if not (GetUnitAllyTeam(units[i]) == self.allyTeamID) then
DefID = GetUnitDefID(units[i])
if not(DefID == nil)then
if (UnitDefs[DefID].canFly == false and not( UnitDefs[DefID].name == Claymore_NAME))then
transportedUnit = GetUnitIsTransporting(self.unitID)
if (transportedUnit[1] == nil) then
--Echo("No unit being transported")
return
end
transportedUnitID = transportedUnit[1]
DefID = GetUnitDefID(transportedUnitID)
if (UnitDefs[DefID].name == Claymore_NAME) then
if(self.reloadZone and self.autoReloadToggle)then
--Unit is sent to reloadzone--
GiveOrderToUnit(self.unitID, CMD_FORCE_DROP_UNIT, {},{""})
GiveOrderToUnit(transportedUnitID, CMD_DROP_BOMB, {},0)
--Echo("Bomb drop order given to unit:" .. transportedUnitID)
GiveOrderToUnit(self.unitID, CMD_INSERT,{0, CMD_LOAD_UNITS, CMD_OPT_SHIFT, transportedUnitID}, {"alt"})
--Echo("Load order given to unit:" .. selectedTransports[i])
GiveOrderToUnit(self.unitID, CMD_INSERT,{0, CMD_UNLOAD_UNITS, CMD_OPT_SHIFT, self.reloadZone[1],self.reloadZone[2],self.reloadZone[3]}, {"alt"})
local reloadState = GetUnitWeaponState(transportedUnitID, 1, "reloadState")
reloaderStack[self.unitID] = reloadController:new(self.unitID, transportedUnitID, reloadState);
else
GiveOrderToUnit(self.unitID, CMD_FORCE_DROP_UNIT, {},{""})
GiveOrderToUnit(transportedUnitID, CMD_DROP_BOMB, {},0)
--Echo("Bomb drop order given to unit:" .. transportedUnitID)
GiveOrderToUnit(self.unitID, CMD_INSERT,{0, CMD_LOAD_UNITS, CMD_OPT_SHIFT, transportedUnitID}, {"alt"})
GiveOrderToUnit(self.unitID, CMD_INSERT,{0, CMD_ATTACK_MOVE_ID, CMD_OPT_SHIFT, self.fightPos[1],self.fightPos[2],self.fightPos[3]}, {"alt"})
--Echo("Load order given to unit:" .. selectedTransports[i])
end
end
end
end
end
end
end
end
}
function widget:UnitCreated(unitID, unitDefID, unitTeam)
if (UnitDefs[unitDefID].name==Charon_NAME or UnitDefs[unitDefID].name==Hercules_NAME)
and (unitTeam==GetMyTeamID()) then
TransporterStack[unitID] = transportController:new(unitID);
end
end
function widget:UnitTaken(unitID, unitDefID, unitTeam, newTeam)
if (UnitDefs[unitDefID].name==Charon_NAME or UnitDefs[unitDefID].name==Hercules_NAME)
and not TransporterStack[unitID] then
TransporterStack[unitID] = transportController:new(unitID);
end
end
function widget:UnitDestroyed(unitID)
if not (TransporterStack[unitID]==nil) then
TransporterStack[unitID]=TransporterStack[unitID]:unset();
end
end
--- COMMAND HANDLING
function widget:CommandNotify(cmdID, params, options)
if selectedTransports ~= nil then
if (cmdID == CMD_DROP_CLAYMORE_BOMB)then
for i=1, #selectedTransports do
repeat
transportedUnit = GetUnitIsTransporting(selectedTransports[i])
if (transportedUnit[1] == nil) then
--Echo("No unit being transported")
i=i+1
break
end
transportedUnitID = transportedUnit[1]
DefID = GetUnitDefID(transportedUnitID)
if (UnitDefs[DefID].name == Claymore_NAME or UnitDefs[DefID].name == Limpet_NAME or UnitDefs[DefID].name == Snitch_NAME or UnitDefs[DefID].name == Imp_NAME or UnitDefs[DefID].name == Scuttle_NAME) then
if(TransporterStack[selectedTransports[i]] and TransporterStack[selectedTransports[i]].reloadZone and TransporterStack[selectedTransports[i]].autoReloadToggle)then
--Unit is sent to reloadzone--
GiveOrderToUnit(selectedTransports[i], CMD_FORCE_DROP_UNIT, {},{""})
GiveOrderToUnit(transportedUnitID, CMD_DROP_BOMB, {},0)
--Echo("Bomb drop order given to unit:" .. transportedUnitID)
GiveOrderToUnit(selectedTransports[i], CMD_INSERT,{0, CMD_LOAD_UNITS, CMD_OPT_SHIFT, transportedUnitID}, {"alt"})
--Echo("Load order given to unit:" .. selectedTransports[i])
GiveOrderToUnit(selectedTransports[i], CMD_INSERT,{0, CMD_UNLOAD_UNITS, CMD_OPT_SHIFT, TransporterStack[selectedTransports[i]].reloadZone[1],TransporterStack[selectedTransports[i]].reloadZone[2],TransporterStack[selectedTransports[i]].reloadZone[3]}, {"alt"})
local reloadState = GetUnitWeaponState(transportedUnitID, 1, "reloadState")
reloaderStack[selectedTransports[i]] = reloadController:new(selectedTransports[i], transportedUnitID, reloadState);
else
GiveOrderToUnit(selectedTransports[i], CMD_INSERT,{0, CMD_FORCE_DROP_UNIT, CMD_OPT_SHIFT}, {"alt"})
GiveOrderToUnit(transportedUnitID, CMD_DROP_BOMB, {},0)
--Echo("Bomb drop order given to unit:" .. transportedUnitID)
GiveOrderToUnit(selectedTransports[i], CMD_INSERT,{0, CMD_LOAD_UNITS, CMD_OPT_SHIFT, transportedUnitID}, {"alt"})
--Echo("Load order given to unit:" .. selectedTransports[i])
end
end
until true
end
return true
end
if (cmdID == CMD_RELOAD_CLAYMORE)then
for i=1, #selectedTransports do
repeat
transportedUnit = GetUnitIsTransporting(selectedTransports[i])
if (transportedUnit[1] == nil) then
Echo("No unit being transported")
i=i+1
break
end
transportedUnitID = transportedUnit[1]
DefID = GetUnitDefID(transportedUnitID)
if (UnitDefs[DefID].name == Claymore_NAME) then
local reloadState = GetUnitWeaponState(transportedUnitID, 1, "reloadState")
if(currentFrame >= reloadState)then
break --already loaded
else
position = {GetUnitPosition(selectedTransports[i])}
GiveOrderToUnit(selectedTransports[i], CMD_UNLOAD_UNITS, {position[1], GetGroundHeight(position[1], position[3]), position[3], 1},0)
reloaderStack[selectedTransports[i]] = reloadController:new(selectedTransports[i], transportedUnitID, reloadState);
end
end
until true
end
end
if (cmdID == CMD_FIND_PAD)then
local toggleStateGot = false
local toggleState
for i=1, #selectedTransports do
if (TransporterStack[selectedTransports[i]])then
if (toggleStateGot == false)then
toggleState = TransporterStack[selectedTransports[i]]:getToggleState()
toggleStateGot = true
end
if (toggleState) then
TransporterStack[selectedTransports[i]]:toggleOff()
Echo("Return to reloadZone turned Off")
else
TransporterStack[selectedTransports[i]]:toggleOn()
Echo("Return to reloadZone turned On")
end
end
end
return true
end
if (cmdID == CMD_SET_RELOAD_ZONE and #params==3)then
for i=1, #selectedTransports do
TransporterStack[selectedTransports[i]]:setReloadZone(params)
end
return
end
end
end
function widget:UnitCommand(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOpts, cmdTag)
if (TransporterStack[unitID])then
if (cmdID == CMD_ATTACK_MOVE_ID) then
TransporterStack[unitID]:toggleFightOn()
TransporterStack[unitID].fightPos=cmdParams
else
TransporterStack[unitID]:toggleFightOff()
end
end
end
function widget:SelectionChanged(selectedUnits)
selectedTransports = filterTransports(selectedUnits)
end
function filterTransports(units)
local filtered = {}
local n = 0
for i = 1, #units do
local unitID = units[i]
if (CharonUnitDefID == GetUnitDefID(unitID) or HerculesUnitDefID == GetUnitDefID(unitID)) then
n = n + 1
filtered[n] = unitID
end
end
if n == 0 then
return nil
else
return filtered
end
end
function widget:CommandsChanged()
if selectedTransports ~= nil then
local customCommands = widgetHandler.customCommands
customCommands[#customCommands+1] = cmdDropClaymoreBomb
customCommands[#customCommands+1] = cmdReloadClaymore
customCommands[#customCommands+1] = cmdSetReloadingZone
end
end
function widget:GameFrame(n)
currentFrame = n
for _,Reloader in pairs(reloaderStack) do
Reloader:handle()
end
for _,ClaymoreBomber in pairs(TransporterStack) do
ClaymoreBomber:handle()
end
end
function deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[deepcopy(orig_key)] = deepcopy(orig_value)
end
setmetatable(copy, deepcopy(getmetatable(orig)))
else
copy = orig
end
return copy
end
-- The rest of the code is there to disable the widget for spectators
local function DisableForSpec()
if GetSpecState() then
widgetHandler:RemoveWidget()
end
end
function widget:Initialize()
DisableForSpec()
local units = GetTeamUnits(GetMyTeamID())
for i=1, #units do
unitID = units[i]
DefID = GetUnitDefID(unitID)
if (UnitDefs[DefID].name==Charon_NAME or UnitDefs[DefID].name==Hercules_NAME) then
TransporterStack[unitID] = transportController:new(unitID);
end
end
end
function widget:PlayerChanged (playerID)
DisableForSpec()
end