-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTargettingAI_Artemis.lua
241 lines (209 loc) · 6.23 KB
/
TargettingAI_Artemis.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
function widget:GetInfo()
return {
name = "TargettingAI_Artemis",
desc = "attempt to make Artemis not fire cheap aircraft with metal cost less than 270. Meant to be used with hold/return fire state. Version 1,00",
author = "terve886",
date = "2019",
license = "PD", -- should be compatible with Spring
layer = 11,
handler = true, --for adding customCommand into UI
enabled = true
}
end
local UPDATE_FRAME=4
local ArtemisStack = {}
local GetUnitMaxRange = Spring.GetUnitMaxRange
local GetUnitPosition = Spring.GetUnitPosition
local GetMyAllyTeamID = Spring.GetMyAllyTeamID
local GiveOrderToUnit = Spring.GiveOrderToUnit
local GetGroundHeight = Spring.GetGroundHeight
local GetUnitsInSphere = Spring.GetUnitsInSphere
local GetUnitsInCylinder = Spring.GetUnitsInCylinder
local GetUnitAllyTeam = Spring.GetUnitAllyTeam
local GetUnitNearestEnemy = Spring.GetUnitNearestEnemy
local GetUnitIsDead = Spring.GetUnitIsDead
local GetMyTeamID = Spring.GetMyTeamID
local GetUnitDefID = Spring.GetUnitDefID
local GetTeamUnits = Spring.GetTeamUnits
local GetUnitStates = Spring.GetUnitStates
local Echo = Spring.Echo
local Artemis_NAME = "turretaaheavy"
local GetSpecState = Spring.GetSpectatingState
local CMD_STOP = CMD.STOP
local CMD_ATTACK = CMD.ATTACK
local CMD_Change_MetalTarget = 19497
local ArtemisUnitDefID = UnitDefNames["turretaaheavy"].id
local selectedArtemis = nil
local cmdChangeMetalTarget = {
id = CMD_Change_MetalTarget,
type = CMDTYPE.ICON,
tooltip = 'Makes Puppy rocket rush enemies that get close.',
action = 'oneclickwep',
params = { },
texture = 'LuaUI/Images/commands/Bold/dgun.png',
pos = {CMD_ONOFF,CMD_REPEAT,CMD_MOVE_STATE,CMD_FIRE_STATE, CMD_RETREAT},
}
local ArtemisController = {
unitID,
pos,
allyTeamID = GetMyAllyTeamID(),
range,
forceTarget,
metalTarget,
metalTargetValue,
new = function(self, unitID)
--Echo("ArtemisController added:" .. unitID)
self = deepcopy(self)
self.unitID = unitID
self.range = GetUnitMaxRange(self.unitID)
self.pos = {GetUnitPosition(self.unitID)}
self.metalTarget = {220, 300, 700}
self.metalTargetValue = 1
return self
end,
unset = function(self)
--Echo("ArtemisController removed:" .. self.unitID)
GiveOrderToUnit(self.unitID,CMD_STOP, {}, {""},1)
return nil
end,
setForceTarget = function(self, param)
self.forceTarget = param[1]
end,
changeMetalTarget = function(self)
self.metalTargetValue = self.metalTargetValue+1
if (self.metalTargetValue > #self.metalTarget)then
self.metalTargetValue = 1
end
Echo("Minimum metal filter changed to:" .. self.metalTarget[self.metalTargetValue])
end,
isEnemyInRange = function (self)
local units = GetUnitsInCylinder(self.pos[1], self.pos[3], self.range)
local target = nil
for i=1, #units do
if not (GetUnitAllyTeam(units[i]) == self.allyTeamID) then
if (units[i]==self.forceTarget and GetUnitIsDead(units[i]) == false)then
GiveOrderToUnit(self.unitID,CMD_ATTACK, units[i], 0)
return true
end
DefID = GetUnitDefID(units[i])
if not(DefID == nil)then
if (GetUnitIsDead(units[i]) == false and UnitDefs[DefID].isAirUnit == true and UnitDefs[DefID].metalCost >= self.metalTarget[self.metalTargetValue]) then
if (target == nil) then
target = units[i]
end
if (UnitDefs[GetUnitDefID(target)].metalCost < UnitDefs[DefID].metalCost)then
target = units[i]
end
end
end
end
end
if (target == nil) then
GiveOrderToUnit(self.unitID,CMD_STOP, {}, {""},1)
else
GiveOrderToUnit(self.unitID,CMD_ATTACK, target, 0)
end
end,
handle=function(self)
if(GetUnitStates(self.unitID).firestate==1)then
self:isEnemyInRange()
end
end
}
function widget:UnitCommand(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOpts, cmdTag)
if (UnitDefs[unitDefID].name == Artemis_NAME and cmdID == CMD_ATTACK and #cmdParams == 1) then
if (ArtemisStack[unitID])then
ArtemisStack[unitID]:setForceTarget(cmdParams)
end
end
end
function widget:UnitFinished(unitID, unitDefID, unitTeam)
if (UnitDefs[unitDefID].name==Artemis_NAME)
and (unitTeam==GetMyTeamID()) then
ArtemisStack[unitID] = ArtemisController:new(unitID);
end
end
function widget:UnitDestroyed(unitID)
if not (ArtemisStack[unitID]==nil) then
ArtemisStack[unitID]=ArtemisStack[unitID]:unset();
end
end
function widget:GameFrame(n)
if (n%UPDATE_FRAME==0) then
for _,Artemis in pairs(ArtemisStack) do
Artemis:handle()
end
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 selectedArtemis ~= nil then
if (cmdID == CMD_Change_MetalTarget)then
for i=1, #selectedArtemis do
if(ArtemisStack[selectedArtemis[i]])then
ArtemisStack[selectedArtemis[i]]:changeMetalTarget()
end
end
end
end
end
function widget:SelectionChanged(selectedUnits)
selectedArtemis = filterArtemis(selectedUnits)
end
function filterArtemis(units)
local filtered = {}
local n = 0
for i = 1, #units do
local unitID = units[i]
if (ArtemisUnitDefID == 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 selectedArtemis then
local customCommands = widgetHandler.customCommands
customCommands[#customCommands+1] = cmdChangeMetalTarget
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 units = GetTeamUnits(Spring.GetMyTeamID())
for i=1, #units do
DefID = GetUnitDefID(units[i])
if (UnitDefs[DefID].name==Artemis_NAME) then
if (ArtemisStack[units[i]]==nil) then
ArtemisStack[units[i]]=ArtemisController:new(units[i])
end
end
end
end
function widget:PlayerChanged (playerID)
DisableForSpec()
end