-
Notifications
You must be signed in to change notification settings - Fork 40
/
buildings_status.lua
237 lines (216 loc) · 8.56 KB
/
buildings_status.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
_G._savedEnv = getfenv()
module( "buildings_status", package.seeall )
TYPE_TOWER = "tower"
TYPE_MELEE = "melee"
TYPE_RANGED = "ranged"
TYPE_SHRINE = "shrine"
TYPE_ANCIENT = "ancient"
-- TODO: this system is are really ugly.
local buildings = {
{["ApiID"]=TOWER_TOP_1, ["Type"]=TYPE_TOWER}, -- 1
{["ApiID"]=TOWER_TOP_2, ["Type"]=TYPE_TOWER},
{["ApiID"]=TOWER_TOP_3, ["Type"]=TYPE_TOWER},
{["ApiID"]=TOWER_MID_1, ["Type"]=TYPE_TOWER}, -- 4
{["ApiID"]=TOWER_MID_2, ["Type"]=TYPE_TOWER},
{["ApiID"]=TOWER_MID_3, ["Type"]=TYPE_TOWER},
{["ApiID"]=TOWER_BOT_1, ["Type"]=TYPE_TOWER}, -- 7
{["ApiID"]=TOWER_BOT_2, ["Type"]=TYPE_TOWER},
{["ApiID"]=TOWER_BOT_3, ["Type"]=TYPE_TOWER},
{["ApiID"]=TOWER_BASE_1, ["Type"]=TYPE_TOWER}, -- 10
{["ApiID"]=TOWER_BASE_2, ["Type"]=TYPE_TOWER}, -- 11
{["ApiID"]=BARRACKS_TOP_MELEE, ["Type"]=TYPE_MELEE}, -- 12
{["ApiID"]=BARRACKS_TOP_RANGED, ["Type"]=TYPE_RANGED},
{["ApiID"]=BARRACKS_MID_MELEE, ["Type"]=TYPE_MELEE}, -- 14
{["ApiID"]=BARRACKS_MID_RANGED, ["Type"]=TYPE_RANGED},
{["ApiID"]=BARRACKS_BOT_MELEE, ["Type"]=TYPE_MELEE}, -- 16
{["ApiID"]=BARRACKS_BOT_RANGED, ["Type"]=TYPE_RANGED},
{["ApiID"]=0, ["Type"]=TYPE_ANCIENT}, -- 18
{["ApiID"]=SHRINE_JUNGLE_1, ["Type"]=TYPE_SHRINE},
{["ApiID"]=SHRINE_JUNGLE_2, ["Type"]=TYPE_SHRINE},
{["ApiID"]=SHRINE_BASE_1, ["Type"]=TYPE_SHRINE},
{["ApiID"]=SHRINE_BASE_2, ["Type"]=TYPE_SHRINE},
{["ApiID"]=SHRINE_BASE_3, ["Type"]=TYPE_SHRINE}
}
local offsetByLane = {[LANE_TOP] = 0, [LANE_MID] = 3, [LANE_BOT] = 6}
local tableBuildings = {}
local towers = {}
local barracks = {}
local shrines = {}
local lastUpdate = -9999
-- fill the empty tables
local function Initialize()
tableBuildings[TEAM_RADIANT] = {}
tableBuildings[TEAM_DIRE] = {}
local team = GetTeam()
for i, building in ipairs(buildings) do
tableBuildings[TEAM_RADIANT][i] = {}
tableBuildings[TEAM_DIRE][i] = {}
local health = 0
local pos_radiant = nil
local pos_dire = nil
if building.Type == TYPE_TOWER then
health = GetTower(team, building.ApiID):GetMaxHealth()
pos_radiant = GetTower(TEAM_RADIANT, building.ApiID):GetLocation()
pos_dire = GetTower(TEAM_DIRE, building.ApiID):GetLocation()
towers[#towers+1] = i
elseif building.Type == TYPE_MELEE then
health = GetBarracks(team, building.ApiID):GetMaxHealth()
pos_radiant = GetBarracks(TEAM_RADIANT, building.ApiID):GetLocation()
pos_dire = GetBarracks(TEAM_DIRE, building.ApiID):GetLocation()
barracks[#barracks+1] = i
elseif building.Type == TYPE_RANGED then
health = GetBarracks(team, building.ApiID):GetMaxHealth()
pos_radiant = GetBarracks(TEAM_RADIANT, building.ApiID):GetLocation()
pos_dire = GetBarracks(TEAM_DIRE, building.ApiID):GetLocation()
barracks[#barracks+1] = i
elseif building.Type == TYPE_SHRINE then
health = GetShrine(team, building.ApiID):GetMaxHealth()
pos_radiant = GetShrine(TEAM_RADIANT, building.ApiID):GetLocation()
pos_dire = GetShrine(TEAM_DIRE, building.ApiID):GetLocation()
shrines[#shrines+1] = i
elseif building.Type == TYPE_ANCIENT then
health = GetAncient(team):GetMaxHealth()
pos_radiant = GetAncient(TEAM_RADIANT):GetLocation()
pos_dire = GetAncient(TEAM_DIRE):GetLocation()
end
tableBuildings[TEAM_RADIANT][i].ApiID = building.ApiID
tableBuildings[TEAM_RADIANT][i].Type = building.Type
tableBuildings[TEAM_RADIANT][i].MaxHealth = health
tableBuildings[TEAM_RADIANT][i].LastSeenHealth = health
tableBuildings[TEAM_RADIANT][i].Vector = pos_radiant
tableBuildings[TEAM_DIRE][i].ApiID = building.ApiID
tableBuildings[TEAM_DIRE][i].Type = building.Type
tableBuildings[TEAM_DIRE][i].MaxHealth = health
tableBuildings[TEAM_DIRE][i].LastSeenHealth = health
tableBuildings[TEAM_DIRE][i].Vector = pos_dire
end
--printBuildings()
end
function Update(forceUpdate)
if lastUpdate < -1000 then
Initialize()
end
if DotaTime() - lastUpdate < 0.5 then
if (not forceUpdate) then return end
end
lastUpdate = DotaTime()
for i, _ in ipairs(tableBuildings[TEAM_RADIANT]) do
GetHealth(TEAM_RADIANT, i, false)
end
for i, _ in ipairs(tableBuildings[TEAM_DIRE]) do
GetHealth(TEAM_DIRE, i, false)
end
end
function GetHealth(team, id, cacheOnly)
if cacheOnly == nil then cacheOnly = true end -- thats -not- the same as 'cacheOnly = cacheOnly or true'
local seen = tableBuildings[team][id].LastSeenHealth
if cacheOnly then return seen end
if seen <= 0 then return -1 end
local building = GetHandle(team, id)
if building == nil then
tableBuildings[team][id].LastSeenHealth = -1
return -1
end
local health = building:GetHealth()
if health > -1 then
tableBuildings[team][id].LastSeenHealth = health
return health
else
return seen
end
end
function GetLocation(team, id)
local result = tableBuildings[team][id].Vector
return result
end
function GetHandle(team, id)
local building = tableBuildings[team][id]
if building == nil then return nil end
if building.Type == TYPE_TOWER then
return GetTower(team, building.ApiID)
elseif building.Type == TYPE_MELEE then
return GetBarracks(team, building.ApiID)
elseif building.Type == TYPE_RANGED then
return GetBarracks(team, building.ApiID)
elseif building.Type == TYPE_SHRINE then
return GetShrine(team, building.ApiID)
elseif building.Type == TYPE_ANCIENT then
return GetAncient(team)
end
return nil
end
function GetStandingBuildingIDs(team)
local ids = {}
for i, _ in ipairs(tableBuildings[team]) do
if GetHealth(team, i) > 0 then
ids[#ids+1] = i
end
end
return ids
end
function GetType(team, id)
return tableBuildings[team][id].Type
end
function GetApiID(team, id)
return tableBuildings[team][id].ApiID
end
function GetDestroyableTowers(team)
local ids = {}
for _, id in pairs(towers) do
if GetHealth(team, id) > 0 and GetHandle(team, id) ~= nil and (not GetHandle(team, id):IsInvulnerable()) then
ids[#ids+1] = id
end
end
return ids
end
function printBuildings()
print("Buildings Radiant")
for i, building in pairs(tableBuildings[TEAM_RADIANT]) do
print(i, building.LastSeenHealth, building.Vector)
end
print("Buildings Dire")
for i, building in pairs(tableBuildings[TEAM_DIRE]) do
print(i, building.LastSeenHealth, building.Vector)
end
end
-- TODO: ugly.
-- check tower dependencies (glyph doesn't matter). Lane can be nil, to get all lanes' towers
-- throne is considered to be on all lanes
function GetVulnerableBuildingIDs(team, lane)
local ids = {}
-- TODO: use a method that isn't depending on exact order in tableBuildings
for j = 0,6,3 do -- for all lanes
if lane == nil or j == offsetByLane[lane] then
for i = 1,3,1 do -- towers from t1 to t3
if GetHealth(team, i+j) > 0 then
ids[#ids+1] = i+j
break
end
end
end
end
if GetHealth(team, 3) <= 0 and lane == nil or lane == LANE_TOP then -- t3 top
if GetHealth(team, 12) > 0 then ids[#ids+1] = 12 end -- melee
if GetHealth(team, 13) > 0 then ids[#ids+1] = 13 end -- ranged
end
if GetHealth(team, 6) <= 0 and lane == nil or lane == LANE_MID then -- t3 mid
if GetHealth(team, 14) > 0 then ids[#ids+1] = 14 end -- melee
if GetHealth(team, 15) > 0 then ids[#ids+1] = 15 end -- ranged
end
if GetHealth(team, 9) <= 0 and lane == nil or lane == LANE_BOT then -- t3 bot
if GetHealth(team, 16) > 0 then ids[#ids+1] = 16 end -- melee
if GetHealth(team, 17) > 0 then ids[#ids+1] = 17 end -- ranged
end
-- t4s
if GetHealth(team, 3) <= 0 or GetHealth(team, 6) <= 0 or GetHealth(team, 9) <= 0 and lane == nil or lane == LANE_MID then -- check t3s
if GetHealth(team, 10) > 0 then ids[#ids+1] = 10 end
if GetHealth(team, 11) > 0 then ids[#ids+1] = 11 end
end
-- throne
if GetHealth(team, 10) <= 0 or GetHealth(team, 11) <= 0 and lane == nil or lane == LANE_MID then -- check t4s
ids[#ids+1] = 18
end
-- TODO: check shrines (outside base)
return ids
end
for k,v in pairs( buildings_status ) do _G._savedEnv[k] = v end