forked from Sidoine/Ovale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHealth.lua
328 lines (304 loc) · 10.2 KB
/
Health.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
--[[--------------------------------------------------------------------
Copyright (C) 2015 Johnny C. Lam.
See the file LICENSE.txt for copying permission.
--]]--------------------------------------------------------------------
local OVALE, Ovale = ...
local OvaleHealth = Ovale:NewModule("OvaleHealth", "AceEvent-3.0")
Ovale.OvaleHealth = OvaleHealth
--<private-static-properties>
local OvaleDebug = Ovale.OvaleDebug
local OvaleProfiler = Ovale.OvaleProfiler
-- Forward declarations for module dependencies.
local OvaleData = nil
local OvaleGUID = nil
local OvaleState = nil
local strsub = string.sub
local tonumber = tonumber
local wipe = wipe
local API_GetTime = GetTime
local API_UnitHealth = UnitHealth
local API_UnitHealthMax = UnitHealthMax
local INFINITY = math.huge
-- Register for debugging messages.
OvaleDebug:RegisterDebugging(OvaleHealth)
-- Register for profiling.
OvaleProfiler:RegisterProfiling(OvaleHealth)
local CLEU_DAMAGE_EVENT = {
DAMAGE_SHIELD = true,
DAMAGE_SPLIT = true,
RANGE_DAMAGE = true,
SPELL_BUILDING_DAMAGE = true,
SPELL_DAMAGE = true,
SPELL_PERIODIC_DAMAGE = true,
SWING_DAMAGE = true,
ENVIRONMENTAL_DAMAGE = true,
}
local CLEU_HEAL_EVENT = {
SPELL_HEAL = true,
SPELL_PERIODIC_HEAL = true,
}
--</private-static-properties>
--<public-static-properties>
-- Health of unit, indexed by GUID.
OvaleHealth.health = {}
-- Maximum health of unit, indexed by GUID.
OvaleHealth.maxHealth = {}
-- Running total of damage taken, indexed by GUID.
OvaleHealth.totalDamage = {}
-- Running total of healing taken, indexed by GUID.
OvaleHealth.totalHealing = {}
-- The CLEU timestamp that a GUID first was seen taking damage or healing.
OvaleHealth.firstSeen = {}
-- The CLEU timestamp that a GUID most recently was seen taking damage or healing.
OvaleHealth.lastUpdated = {}
-- Unused public property to suppress lint warnings.
--OvaleHealth.defaultTarget = nil
--</public-static-properties>
--<public-static-methods>
function OvaleHealth:OnInitialize()
-- Resolve module dependencies.
OvaleData = Ovale.OvaleData
OvaleGUID = Ovale.OvaleGUID
OvaleState = Ovale.OvaleState
end
function OvaleHealth:OnEnable()
self:RegisterEvent("PLAYER_REGEN_DISABLED")
self:RegisterEvent("PLAYER_REGEN_ENABLED")
self:RegisterEvent("UNIT_HEALTH_FREQUENT", "UpdateHealth")
self:RegisterEvent("UNIT_MAXHEALTH", "UpdateHealth")
self:RegisterMessage("Ovale_UnitChanged")
OvaleData:RegisterRequirement("health_pct", "RequireHealthPercentHandler", self)
OvaleData:RegisterRequirement("pet_health_pct", "RequireHealthPercentHandler", self)
OvaleData:RegisterRequirement("target_health_pct", "RequireHealthPercentHandler", self)
OvaleState:RegisterState(self, self.statePrototype)
end
function OvaleHealth:OnDisable()
OvaleState:UnregisterState(self)
OvaleData:UnregisterRequirement("health_pct")
OvaleData:UnregisterRequirement("pet_health_pct")
OvaleData:UnregisterRequirement("target_health_pct")
self:UnregisterEvent("PLAYER_REGEN_ENABLED")
self:UnregisterEvent("PLAYER_TARGET_CHANGED")
self:UnregisterEvent("UNIT_HEALTH_FREQUENT")
self:UnregisterEvent("UNIT_MAXHEALTH")
self:UnregisterMessage("Ovale_UnitChanged")
end
function OvaleHealth:COMBAT_LOG_EVENT_UNFILTERED(event, timestamp, cleuEvent, hideCaster, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, ...)
local arg12, arg13, arg14, arg15, arg16, arg17, arg18, arg19, arg20, arg21, arg22, arg23, arg24, arg25 = ...
self:StartProfiling("OvaleHealth_COMBAT_LOG_EVENT_UNFILTERED")
-- Keep a running total of damage and healing taken per GUID.
local healthUpdate = false
if CLEU_DAMAGE_EVENT[cleuEvent] then
local amount
if cleuEvent == "SWING_DAMAGE" then
amount = arg12
elseif cleuEvent == "ENVIRONMENTAL_DAMAGE" then
amount = arg13
else
amount = arg15
end
self:Debug(cleuEvent, destGUID, amount)
local total = self.totalDamage[destGUID] or 0
self.totalDamage[destGUID] = total + amount
healthUpdate = true
elseif CLEU_HEAL_EVENT[cleuEvent] then
local amount = arg15
self:Debug(cleuEvent, destGUID, amount)
local total = self.totalHealing[destGUID] or 0
self.totalHealing[destGUID] = total + amount
healthUpdate = true
end
if healthUpdate then
if not self.firstSeen[destGUID] then
self.firstSeen[destGUID] = timestamp
end
self.lastUpdated[destGUID] = timestamp
end
self:StopProfiling("OvaleHealth_COMBAT_LOG_EVENT_UNFILTERED")
end
function OvaleHealth:PLAYER_REGEN_DISABLED(event)
-- Entering combat.
self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
end
function OvaleHealth:PLAYER_REGEN_ENABLED(event)
-- Leaving combat.
self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
-- Clear running totals when leaving combat.
wipe(self.totalDamage)
wipe(self.totalHealing)
wipe(self.firstSeen)
wipe(self.lastUpdated)
end
function OvaleHealth:Ovale_UnitChanged(event, unitId, guid)
self:StartProfiling("Ovale_UnitChanged")
if unitId == "target" or unitId == "focus" then
self:Debug(event, unitId, guid)
self:UpdateHealth("UNIT_HEALTH_FREQUENT", unitId)
self:UpdateHealth("UNIT_MAXHEALTH", unitId)
self:StopProfiling("Ovale_UnitChanged")
end
end
function OvaleHealth:UpdateHealth(event, unitId)
if not unitId then return end
self:StartProfiling("OvaleHealth_UpdateHealth")
local func = API_UnitHealth
local db = self.health
if event == "UNIT_MAXHEALTH" then
func = API_UnitHealthMax
db = self.maxHealth
end
local amount = func(unitId)
if amount then
local guid = OvaleGUID:UnitGUID(unitId)
self:Debug(event, unitId, guid, amount)
if guid then
if amount > 0 then
db[guid] = amount
else
db[guid] = nil
self.firstSeen[guid] = nil
self.lastUpdated[guid] = nil
end
Ovale.refreshNeeded[guid] = true
end
end
self:StopProfiling("OvaleHealth_UpdateHealth")
end
--[[
Return the current health of the unit.
An optional GUID may be passed as a hint for the GUID of the unit.
--]]
function OvaleHealth:UnitHealth(unitId, guid)
local amount
if unitId then
guid = guid or OvaleGUID:UnitGUID(unitId)
if guid then
if unitId == "target" or unitId == "focus" then
-- The target and focus target are actively tracked.
amount = self.health[guid] or 0
else
-- Cache the health for later reference.
amount = API_UnitHealth(unitId)
self.health[guid] = amount
end
else
amount = 0
end
end
return amount
end
--[[
Return the maximum health of the unit.
An optional GUID may be passed as a hint for the GUID of the unit.
--]]
function OvaleHealth:UnitHealthMax(unitId, guid)
local amount
if unitId then
guid = guid or OvaleGUID:UnitGUID(unitId)
if guid then
if unitId == "target" or unitId == "focus" then
-- The target and focus target are actively tracked.
amount = self.maxHealth[guid] or 0
else
-- Cache the maximum health for later reference.
amount = API_UnitHealthMax(unitId)
self.maxHealth[guid] = amount
end
else
amount = 0
end
end
return amount
end
--[[
Return the estimated time to die in seconds for the unit.
An optional GUID may be passed as a hint for the GUID of the unit.
--]]
function OvaleHealth:UnitTimeToDie(unitId, guid)
self:StartProfiling("OvaleHealth_UnitTimeToDie")
local timeToDie = INFINITY
guid = guid or OvaleGUID:UnitGUID(unitId)
if guid then
local health = self:UnitHealth(unitId, guid)
local maxHealth = self:UnitHealthMax(unitId, guid)
if health and maxHealth then
if health == 0 then
timeToDie = 0
self.firstSeen[guid] = nil
self.lastUpdated[guid] = nil
elseif maxHealth > 5 then
-- Filter out targets whose maximum health is less than 5, which are probably target dummies.
local firstSeen, lastUpdated = self.firstSeen[guid], self.lastUpdated[guid]
local damage = self.totalDamage[guid] or 0
local healing = self.totalHealing[guid] or 0
if firstSeen and lastUpdated and lastUpdated > firstSeen and damage > healing then
timeToDie = health * (lastUpdated - firstSeen) / (damage - healing)
end
end
end
end
self:StopProfiling("OvaleHealth_UnitTimeToDie")
return timeToDie
end
-- Run-time check that the target is below a health percent threshold.
-- NOTE: Mirrored in statePrototype below.
function OvaleHealth:RequireHealthPercentHandler(spellId, atTime, requirement, tokens, index, targetGUID)
local verified = false
-- If index isn't given, then tokens holds the actual token value.
local threshold = tokens
if index then
threshold = tokens[index]
index = index + 1
end
if threshold then
local isBang = false
if strsub(threshold, 1, 1) == "!" then
isBang = true
threshold = strsub(threshold, 2)
end
threshold = tonumber(threshold) or 0
local guid, unitId
if strsub(requirement, 1, 7) == "target_" then
if targetGUID then
guid = targetGUID
unitId = OvaleGUID:GUIDUnit(guid)
else
unitId = self.defaultTarget or "target"
end
elseif strsub(requirement, 1, 4) == "pet_" then
unitId = "pet"
else
unitId = "player"
end
guid = guid or OvaleGUID:UnitGUID(unitId)
local health = OvaleHealth:UnitHealth(unitId, guid) or 0
local maxHealth = OvaleHealth:UnitHealthMax(unitId, guid) or 0
local healthPercent = (maxHealth > 0) and (health / maxHealth * 100) or 100
if not isBang and healthPercent <= threshold or isBang and healthPercent > threshold then
verified = true
end
local result = verified and "passed" or "FAILED"
if isBang then
self:Log(" Require %s health > %f%% (%f) at time=%f: %s", unitId, threshold, healthPercent, atTime, result)
else
self:Log(" Require %s health <= %f%% (%f) at time=%f: %s", unitId, threshold, healthPercent, atTime, result)
end
else
Ovale:OneTimeMessage("Warning: requirement '%s' is missing a threshold argument.", requirement)
end
return verified, requirement, index
end
--</public-static-methods>
--[[----------------------------------------------------------------------------
State machine for simulator.
--]]----------------------------------------------------------------------------
--<public-static-properties>
OvaleHealth.statePrototype = {}
--</public-static-properties>
--<private-static-properties>
local statePrototype = OvaleHealth.statePrototype
--</private-static-properties>
--<state-methods>
-- Mirrored methods.
statePrototype.RequireHealthPercentHandler = OvaleHealth.RequireHealthPercentHandler
--</state-methods>