-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBurrowBombAttack.lua
146 lines (122 loc) · 3.72 KB
/
BurrowBombAttack.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
function widget:GetInfo()
return {
name = "BurrowBombAttack",
desc = "attempt to make Imp and Snitch move towards enemies in their decloak radius. Version 0,77",
author = "terve886",
date = "2019",
license = "PD", -- should be compatible with Spring
layer = 11,
enabled = true
}
end
local UPDATE_FRAME=4
local BurrowBombStack = {}
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 GetUnitAllyTeam = Spring.GetUnitAllyTeam
local GetUnitIsDead = Spring.GetUnitIsDead
local GetMyTeamID = Spring.GetMyTeamID
local GetUnitDefID = Spring.GetUnitDefID
local IsUnitSelected = Spring.IsUnitSelected
local GetTeamUnits = Spring.GetTeamUnits
local ENEMY_DETECT_BUFFER = 50
local Echo = Spring.Echo
local Imp_NAME = "cloakbomb"
local Snitch_NAME = "shieldbomb"
local GetSpecState = Spring.GetSpectatingState
local CMD_STOP = CMD.STOP
local CMD_ATTACK = CMD.ATTACK
local CMD_MOVE = CMD.MOVE
local BurrowAttackController = {
unitID,
pos,
allyTeamID = GetMyAllyTeamID(),
range,
isSelected = false,
new = function(self, unitID)
--Echo("BurrowAttackController added:" .. unitID)
self = deepcopy(self)
self.unitID = unitID
self.range = UnitDefs[GetUnitDefID(self.unitID)].decloakDistance
self.pos = {GetUnitPosition(self.unitID)}
return self
end,
unset = function(self)
--Echo("BurrowAttackController removed:" .. self.unitID)
GiveOrderToUnit(self.unitID,CMD_STOP, {}, {""},1)
return nil
end,
isEnemyInRange = function (self)
local units = GetUnitsInSphere(self.pos[1], self.pos[2], self.pos[3], self.range+ENEMY_DETECT_BUFFER)
for i=1, #units do
if not(GetUnitAllyTeam(units[i]) == self.allyTeamID) then
if (GetUnitIsDead(units[i]) == false and self.isSelected == false) then
GiveOrderToUnit(self.unitID,CMD_ATTACK, {units[i]}, 0)
return
end
end
end
end,
handle=function(self)
self.isSelected = IsUnitSelected(self.unitID)
self.pos = {GetUnitPosition(self.unitID)}
self:isEnemyInRange()
end
}
function widget:UnitFinished(unitID, unitDefID, unitTeam)
if (UnitDefs[unitDefID].name==Imp_NAME or UnitDefs[unitDefID].name==Snitch_NAME)
and (unitTeam==GetMyTeamID()) then
BurrowBombStack[unitID] = BurrowAttackController:new(unitID);
end
end
function widget:UnitDestroyed(unitID)
if not (BurrowBombStack[unitID]==nil) then
BurrowBombStack[unitID]=BurrowBombStack[unitID]:unset();
end
end
function widget:GameFrame(n)
if (n%UPDATE_FRAME==0) then
for _,bomb in pairs(BurrowBombStack) do
bomb: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
-- 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==Imp_NAME or UnitDefs[DefID].name==Snitch_NAME) then
if (BurrowBombStack[units[i]]==nil) then
BurrowBombStack[units[i]]=BurrowAttackController:new(units[i])
end
end
end
end
function widget:PlayerChanged (playerID)
DisableForSpec()
end