-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBombCustomWaypoint.lua
275 lines (235 loc) · 8.35 KB
/
BombCustomWaypoint.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
function widget:GetInfo()
return {
name = "BombCustomWaypoint",
desc = "Adds command to factories with kamikaze units to make them go to another waypoint. Version 1,06",
author = "terve886",
date = "2020",
license = "PD", -- should be compatible with Spring
layer = 11,
handler = true, --for adding customCommand into UI
enabled = true
}
end
local GL_LINE_STRIP = GL.LINE_STRIP
local glLineWidth = gl.LineWidth
local glColor = gl.Color
local glBeginEnd = gl.BeginEnd
local glVertex = gl.Vertex
local circleList
local FactoryStack = {}
local GetUnitHeading = Spring.GetUnitHeading
local GetUnitMaxRange = Spring.GetUnitMaxRange
local GetUnitPosition = Spring.GetUnitPosition
local GiveOrderToUnit = Spring.GiveOrderToUnit
local GetGroundHeight = Spring.GetGroundHeight
local GetUnitsInSphere = Spring.GetUnitsInSphere
local GetMyAllyTeamID = Spring.GetMyAllyTeamID
local GetUnitAllyTeam = Spring.GetUnitAllyTeam
local GetUnitIsDead = Spring.GetUnitIsDead
local GetMyTeamID = Spring.GetMyTeamID
local GetUnitDefID = Spring.GetUnitDefID
local GetUnitStates = Spring.GetUnitStates
local GetTeamUnits = Spring.GetTeamUnits
local GetUnitNearestEnemy = Spring.GetUnitNearestEnemy
local Echo = Spring.Echo
local Shieldfac_NAME = "factoryshield"
local Amphfac_NAME = "factoryamph"
local Jumpfac_NAME = "factoryjump"
local Cloakfac_NAME = "factorycloak"
local Gunshipfac_NAME = "factorygunship"
local Imp_NAME = "cloakbomb"
local Scuttle_NAME = "jumpbomb"
local Snitch_NAME = "shieldbomb"
local Limpet_NAME = "amphbomb"
local Blastwing_NAME = "gunshipbomb"
local ENEMY_DETECT_BUFFER = 40
local GetSpecState = Spring.GetSpectatingState
local pi = math.pi
local FULL_CIRCLE_RADIANT = 2 * pi
local HEADING_TO_RAD = (pi*2/65536 )
local CMD_UNIT_SET_TARGET = 34923
local CMD_UNIT_CANCEL_TARGET = 34924
local CMD_STOP = CMD.STOP
local CMD_MOVE = CMD.MOVE
local CMD_RAW_MOVE = 31109
local CMD_FIRE_STATE = CMD.FIRE_STATE
local CMD_UNIT_AI = 36214
local selectedFactories = nil
local sin = math.sin
local cos = math.cos
local atan = math.atan
local CMD_SET_BOMB_WAYPOINT = 19812
local ShieldfacUnitDefID = UnitDefNames["factoryshield"].id
local CloakfacUnitDefID = UnitDefNames["factorycloak"].id
local AmphfacUnitDefID = UnitDefNames["factoryamph"].id
local JumpfacUnitDefID = UnitDefNames["factoryjump"].id
local GunshipfacUnitDefID = UnitDefNames["factorygunship"].id
local cmdSetBombWayPoint = {
id = CMD_SET_BOMB_WAYPOINT,
type = CMDTYPE.ICON_MAP,
tooltip = 'Set custom waypoint for bomb units.',
cursor = 'Target',
action = 'reclaim',
params = { },
texture = 'LuaUI/Images/commands/Bold/wait_death.png',
pos = {CMD_ONOFF,CMD_REPEAT,CMD_MOVE_STATE,CMD_FIRE_STATE, CMD_RETREAT},
}
local FactoryWaypointController = {
unitID,
pos,
allyTeamID = GetMyAllyTeamID(),
targetParams = nil,
new = function(self, unitID)
--Echo("FactoryWaypointController added:" .. unitID)
self = deepcopy(self)
self.unitID = unitID
self.pos = {GetUnitPosition(self.unitID)}
return self
end,
unset = function(self)
--Echo("FactoryWaypointController removed:" .. self.unitID)
GiveOrderToUnit(self.unitID,CMD_STOP, {}, {""},1)
return nil
end,
setTargetParams = function (self, params)
self.targetParams = params
end
}
function widget:UnitCreated(unitID, unitDefID, unitTeam, builderID)
if not(FactoryStack[builderID]==nil) and FactoryStack[builderID].targetParams~=nil and (unitTeam==GetMyTeamID())then
if (UnitDefs[unitDefID].name==Scuttle_NAME or UnitDefs[unitDefID].name==Snitch_NAME or UnitDefs[unitDefID].name==Limpet_NAME or UnitDefs[unitDefID].name==Imp_NAME or UnitDefs[unitDefID].name==Blastwing_NAME)then
GiveOrderToUnit(unitID, CMD_RAW_MOVE, {FactoryStack[builderID].targetParams[1], FactoryStack[builderID].targetParams[2], FactoryStack[builderID].targetParams[3]},0)
end
end
end
function widget:UnitFinished(unitID, unitDefID, unitTeam)
if (UnitDefs[unitDefID].name==Jumpfac_NAME or UnitDefs[unitDefID].name==Shieldfac_NAME or UnitDefs[unitDefID].name==Amphfac_NAME or UnitDefs[unitDefID].name==Cloakfac_NAME or UnitDefs[unitDefID].name==Gunshipfac_NAME)
and (unitTeam==GetMyTeamID() and FactoryStack[unitID]==nil) then
FactoryStack[unitID] = FactoryWaypointController:new(unitID);
end
end
function widget:UnitGiven(unitID, unitDefID, unitTeam, oldTeam)
if (UnitDefs[unitDefID].name==Jumpfac_NAME or UnitDefs[unitDefID].name==Shieldfac_NAME or UnitDefs[unitDefID].name==Amphfac_NAME or UnitDefs[unitDefID].name==Cloakfac_NAME or UnitDefs[unitDefID].name==Gunshipfac_NAME)
and (unitTeam==GetMyTeamID() and FactoryStack[unitID]==nil) then
FactoryStack[unitID] = FactoryWaypointController:new(unitID);
end
end
function widget:UnitTaken(unitID, unitDefID, unitTeam, newTeam)
if (UnitDefs[unitDefID].name==Jumpfac_NAME or UnitDefs[unitDefID].name==Shieldfac_NAME or UnitDefs[unitDefID].name==Amphfac_NAME or UnitDefs[unitDefID].name==Cloakfac_NAME or UnitDefs[unitDefID].name==Gunshipfac_NAME)
and (unitTeam==GetMyTeamID() and FactoryStack[unitID]==nil) then
FactoryStack[unitID] = FactoryWaypointController:new(unitID);
end
end
function widget:UnitDestroyed(unitID)
if not (FactoryStack[unitID]==nil) then
FactoryStack[unitID]=FactoryStack[unitID]:unset();
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
--- COMMAND HANDLING
function widget:CommandNotify(cmdID, params, options)
if selectedFactories ~= nil then
if (cmdID == CMD_SET_BOMB_WAYPOINT)then
for i=1, #selectedFactories do
if (FactoryStack[selectedFactories[i]])then
FactoryStack[selectedFactories[i]]:setTargetParams(params)
end
end
return true
end
end
end
function widget:SelectionChanged(selectedUnits)
selectedFactories = filterFactories(selectedUnits)
end
function filterFactories(units)
local filtered = {}
local n = 0
for i = 1, #units do
local unitID = units[i]
if (CloakfacUnitDefID == GetUnitDefID(unitID) or JumpfacUnitDefID == GetUnitDefID(unitID) or ShieldfacUnitDefID == GetUnitDefID(unitID) or AmphfacUnitDefID == GetUnitDefID(unitID) or GunshipfacUnitDefID == 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 selectedFactories then
local customCommands = widgetHandler.customCommands
customCommands[#customCommands+1] = cmdSetBombWayPoint
end
end
function widget:DrawWorld()
------Crash location estimator---
for unitID, Unit in pairs (FactoryStack) do
if (Unit.targetParams~=nil)then
local numAoECircles = 5
local aoe = 50
local alpha = 0.75
for i=1,numAoECircles do --Reference: draw a AOE rings , gui_attack_aoe.lua by Evil4Zerggin
local proportion = (i/(numAoECircles + 1))
local radius = aoe * proportion
local alphamult = alpha*(1-proportion)
glColor(0, 4, 4,alphamult)
gl.PushMatrix()
gl.Translate(Unit.targetParams[1],Unit.targetParams[2],Unit.targetParams[3])
gl.Scale(radius, radius, radius)
gl.CallList(circleList)
gl.PopMatrix()
end
end
end
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 circleVertex = function()
local circleDivs, PI = 64 , pi
for i = 1, circleDivs do
local theta = 2 * PI * i / circleDivs
glVertex(cos(theta), 0, sin(theta))
end
end
local circleDraw = function() glBeginEnd(GL.LINE_LOOP, circleVertex ) end --Reference: draw a circle , gui_attack_aoe.lua by Evil4Zerggin
circleList = gl.CreateList(circleDraw)
local units = GetTeamUnits(Spring.GetMyTeamID())
for i=1, #units do
DefID = GetUnitDefID(units[i])
if (UnitDefs[DefID].name==Jumpfac_NAME or UnitDefs[DefID].name==Shieldfac_NAME or UnitDefs[DefID].name==Amphfac_NAME or UnitDefs[DefID].name==Cloakfac_NAME or UnitDefs[DefID].name==Gunshipfac_NAME) then
if (FactoryStack[units[i]]==nil) then
FactoryStack[units[i]]=FactoryWaypointController:new(units[i])
end
end
end
end
function widget:Shutdown()
if circleList then
gl.DeleteList(circleList)
end
end
function widget:PlayerChanged (playerID)
DisableForSpec()
end