Repository navigation
Expand file tree
/
Copy pathCastTracker.lua
More file actions
394 lines (336 loc) · 9.91 KB
/
Copy pathCastTracker.lua
File metadata and controls
394 lines (336 loc) · 9.91 KB
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
local _, ns = ...
local MAX_BOSS_UNITS = 5
local MAX_NAMEPLATE_UNITS = 40
local activeCards = {}
local cardsByCastBarID = {}
local currentCardByUnit = {}
local unitListeners = {}
local lifecycleFrame
local cachedTargetAbilities
local startEvents = {
"UNIT_SPELLCAST_START",
"UNIT_SPELLCAST_CHANNEL_START",
"UNIT_SPELLCAST_EMPOWER_START",
}
local stopEvents = {
"UNIT_SPELLCAST_STOP",
"UNIT_SPELLCAST_FAILED",
"UNIT_SPELLCAST_FAILED_QUIET",
"UNIT_SPELLCAST_INTERRUPTED",
"UNIT_SPELLCAST_CHANNEL_STOP",
"UNIT_SPELLCAST_EMPOWER_STOP",
}
local isStartEvent = {
UNIT_SPELLCAST_START = true,
UNIT_SPELLCAST_CHANNEL_START = true,
UNIT_SPELLCAST_EMPOWER_START = true,
}
local castBarIDInFifthArgument = {
UNIT_SPELLCAST_INTERRUPTED = true,
UNIT_SPELLCAST_CHANNEL_STOP = true,
}
local function IsNameplateUnit(unit)
local index = string.match(unit, "^nameplate(%d+)$")
index = tonumber(index)
return index and index >= 1 and index <= MAX_NAMEPLATE_UNITS
end
local function IsBossUnit(unit)
local index = string.match(unit, "^boss(%d+)$")
index = tonumber(index)
return index and index >= 1 and index <= MAX_BOSS_UNITS
end
local function IsEnabledUnit(unit)
local db = ns.GetDatabase()
if unit == "target" then
return db.trackTarget
elseif unit == "focus" then
return db.trackFocus
elseif IsBossUnit(unit) then
return db.trackBosses
elseif IsNameplateUnit(unit) then
return db.trackNameplates
end
return false
end
local function IsHostileUnit(unit)
local canAttack = UnitCanAttack("player", unit)
if issecretvalue(canAttack) then
return false
end
return canAttack == true
end
local function RefreshDisplay()
if ns.editModeActive then
return
end
local db = ns.GetDatabase()
if not db.enabled then
ns.HideSpellCards()
return
end
local activeCount = #activeCards
if activeCount > 0 then
ns.ShowTimedSpellCards(activeCards, math.min(activeCount, db.maximumVisibleTooltips))
return
end
if db.showTargetAbilities and cachedTargetAbilities then
ns.ShowTargetSpellCards(
cachedTargetAbilities,
math.min(#cachedTargetAbilities, db.maximumVisibleTooltips)
)
return
end
ns.HideSpellCards()
end
local function CancelTimer(timer)
if timer then
timer:Cancel()
end
end
local function RemoveCard(card, suppressRefresh)
if not card.inQueue then
return
end
card.inQueue = false
CancelTimer(card.minimumTimer)
card.minimumTimer = nil
if card.castBarID ~= nil and cardsByCastBarID[card.castBarID] == card then
cardsByCastBarID[card.castBarID] = nil
end
if currentCardByUnit[card.unit] == card then
currentCardByUnit[card.unit] = nil
end
for index = #activeCards, 1, -1 do
if activeCards[index] == card then
table.remove(activeCards, index)
break
end
end
card.spellID = nil
if not suppressRefresh then
RefreshDisplay()
end
end
local function TryExpireCard(card)
if card.inQueue and card.castEnded and card.minimumElapsed then
RemoveCard(card)
end
end
local function ScheduleMinimumTimer(card)
CancelTimer(card.minimumTimer)
card.minimumTimer = nil
local duration = ns.GetDatabase().minimumDisplayTime
local remaining = duration - (GetTime() - card.startedAt)
if remaining <= 0 then
card.minimumElapsed = true
TryExpireCard(card)
return
end
card.minimumElapsed = false
card.minimumTimer = C_Timer.NewTimer(remaining, function()
card.minimumTimer = nil
card.minimumElapsed = true
TryExpireCard(card)
end)
end
local function MarkCardEnded(card)
if not card or not card.inQueue then
return
end
card.castEnded = true
if currentCardByUnit[card.unit] == card then
currentCardByUnit[card.unit] = nil
end
TryExpireCard(card)
end
local function EnforceVisibleLimit()
local limit = ns.GetDatabase().maximumVisibleTooltips
while #activeCards > limit do
RemoveCard(activeCards[#activeCards], true)
end
end
local function AddCastCard(unit, spellID, castBarID)
local db = ns.GetDatabase()
if not db.enabled or not IsEnabledUnit(unit) or not IsHostileUnit(unit) then
return
end
if castBarID ~= nil and cardsByCastBarID[castBarID] then
return
end
MarkCardEnded(currentCardByUnit[unit])
local card = {
unit = unit,
spellID = spellID,
castBarID = castBarID,
startedAt = GetTime(),
castEnded = false,
minimumElapsed = false,
inQueue = true,
}
table.insert(activeCards, 1, card)
currentCardByUnit[unit] = card
if castBarID ~= nil then
cardsByCastBarID[castBarID] = card
end
ScheduleMinimumTimer(card)
EnforceVisibleLimit()
RefreshDisplay()
end
local function EndCastCard(unit, castBarID)
local card
if castBarID ~= nil then
card = cardsByCastBarID[castBarID]
else
card = currentCardByUnit[unit]
end
MarkCardEnded(card)
end
local function RemoveCardsForUnit(unit)
for index = #activeCards, 1, -1 do
local card = activeCards[index]
if card.unit == unit then
RemoveCard(card, true)
end
end
currentCardByUnit[unit] = nil
RefreshDisplay()
end
local function EndCurrentCardForUnit(unit)
MarkCardEnded(currentCardByUnit[unit])
end
local function ClearAllCards()
for index = #activeCards, 1, -1 do
RemoveCard(activeCards[index], true)
end
wipe(cardsByCastBarID)
wipe(currentCardByUnit)
RefreshDisplay()
end
local function ResolveTargetAbilities()
cachedTargetAbilities = nil
local db = ns.GetDatabase()
if not db.enabled or not db.showTargetAbilities then
return
end
if not UnitExists("target") or not IsHostileUnit("target") then
return
end
local creatureID = UnitCreatureID("target")
if issecretvalue(creatureID) then
return
end
if creatureID then
cachedTargetAbilities = ns.TargetAbilityData[creatureID]
end
end
local function HandleUnitEvent(unit, event, payloadUnit, castGUID, spellID, fourthArgument, fifthArgument, sixthArgument)
if isStartEvent[event] then
AddCastCard(unit, spellID, fourthArgument)
return
end
local castBarID
if event == "UNIT_SPELLCAST_EMPOWER_STOP" then
castBarID = sixthArgument
elseif castBarIDInFifthArgument[event] then
castBarID = fifthArgument
else
castBarID = fourthArgument
end
EndCastCard(unit, castBarID)
end
local function CreateUnitListener(unit)
local frame = CreateFrame("Frame")
for _, event in ipairs(startEvents) do
frame:RegisterUnitEvent(event, unit)
end
for _, event in ipairs(stopEvents) do
frame:RegisterUnitEvent(event, unit)
end
frame:SetScript("OnEvent", function(_, event, ...)
-- The payload unit, cast GUID, and spell ID may be secret. The known
-- listener token is used instead, and only castBarID is correlated.
HandleUnitEvent(unit, event, ...)
end)
unitListeners[#unitListeners + 1] = frame
end
local function OnLifecycleEvent(_, event, unit)
if event == "PLAYER_TARGET_CHANGED" then
EndCurrentCardForUnit("target")
ResolveTargetAbilities()
RefreshDisplay()
elseif event == "PLAYER_FOCUS_CHANGED" then
EndCurrentCardForUnit("focus")
elseif event == "UNIT_NAME_UPDATE" then
if not cachedTargetAbilities then
ResolveTargetAbilities()
RefreshDisplay()
end
elseif event == "NAME_PLATE_UNIT_REMOVED" then
if not issecretvalue(unit) and type(unit) == "string" and IsNameplateUnit(unit) then
RemoveCardsForUnit(unit)
end
elseif event == "INSTANCE_ENCOUNTER_ENGAGE_UNIT" then
for index = 1, MAX_BOSS_UNITS do
local bossUnit = "boss" .. index
if not UnitExists(bossUnit) then
RemoveCardsForUnit(bossUnit)
end
end
elseif event == "PLAYER_ENTERING_WORLD" then
cachedTargetAbilities = nil
ClearAllCards()
ResolveTargetAbilities()
RefreshDisplay()
end
end
function ns.InitializeCastTracker()
CreateUnitListener("target")
CreateUnitListener("focus")
for index = 1, MAX_BOSS_UNITS do
CreateUnitListener("boss" .. index)
end
for index = 1, MAX_NAMEPLATE_UNITS do
CreateUnitListener("nameplate" .. index)
end
lifecycleFrame = CreateFrame("Frame")
lifecycleFrame:RegisterEvent("PLAYER_TARGET_CHANGED")
lifecycleFrame:RegisterEvent("PLAYER_FOCUS_CHANGED")
lifecycleFrame:RegisterUnitEvent("UNIT_NAME_UPDATE", "target")
lifecycleFrame:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
lifecycleFrame:RegisterEvent("INSTANCE_ENCOUNTER_ENGAGE_UNIT")
lifecycleFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
lifecycleFrame:SetScript("OnEvent", OnLifecycleEvent)
ResolveTargetAbilities()
RefreshDisplay()
end
function ns.RefreshCastDisplay()
RefreshDisplay()
end
function ns.OnSettingsChanged()
ns.OnPartyDebuffSettingsChanged()
local db = ns.GetDatabase()
if not db.enabled then
cachedTargetAbilities = nil
ClearAllCards()
return
end
for index = #activeCards, 1, -1 do
local card = activeCards[index]
if not IsEnabledUnit(card.unit) then
RemoveCard(card, true)
end
end
for index = #activeCards, 1, -1 do
local card = activeCards[index]
if card.inQueue then
ScheduleMinimumTimer(card)
end
end
EnforceVisibleLimit()
if not db.showTargetAbilities then
cachedTargetAbilities = nil
elseif not cachedTargetAbilities then
ResolveTargetAbilities()
end
RefreshDisplay()
end