Repository navigation
Expand file tree
/
Copy pathMeter.lua
More file actions
596 lines (557 loc) · 23.4 KB
/
Copy pathMeter.lua
File metadata and controls
596 lines (557 loc) · 23.4 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
-- LifeTimeStats - Forever's built-in damage meter (C_DamageMeter).
-- Forever does not give addons the combat log, but its own meter keeps a list of fights
-- (GetAvailableCombatSessions: sessionID, name, durationSeconds) with readable totals and
-- per-spell rows. After combat every fight not counted yet is read once, oldest first.
-- Copyright (C) 2026 Malikor. Licensed under GPL-3.0-or-later (see LICENSE).
local _, ns = ...
local Add, Max, Tally, On = ns.Add, ns.Max, ns.Tally, ns.On
local S = ns.IsSecret
local GetTime = GetTime
-- Enum.DamageMeterType on Forever 1.60.1 (from /lts dm).
local DT = { damage = 0, heal = 2, interrupts = 5, dispels = 6, taken = 7, avoidable = 8, deaths = 9, enemy = 10 }
ns.DM_TYPES = DT
local MAX_PER_PASS = 20
local secretReads = 0
local function Zone() local z = ns.GetZone and ns.GetZone() return (z and z ~= "") and z or "?" end
local function Num(v)
if S(v) then
ns.src.dmSecret = true
secretReads = secretReads + 1
return nil
end
if not ns.BadNumber(v) then return v end
end
local function Name(v) if type(v) == "string" and not S(v) and v ~= "" then return v end end
local function Persist() ns.src.dmReadable = true; if ns.settings then ns.settings.dmReadable = true end end
local function Me() local g = UnitGUID("player") if S(g) then return nil end return g end
local function Band() local l = UnitLevel("player") or 1 local b = math.floor(l / 10) * 10 return (b == 0 and 1 or b) .. "-" .. (b + 9) end
local function LevelKey() return string.format("%02d", UnitLevel("player") or 1) end
local function Round1(x) return math.floor(x * 10 + 0.5) / 10 end
local function SpellName(id)
if not Num(id) then return nil end
local n
if C_Spell and C_Spell.GetSpellName then local ok, r = pcall(C_Spell.GetSpellName, id); if ok then n = r end end
if not n and GetSpellInfo then local ok, r = pcall(GetSpellInfo, id); if ok then n = r end end
if id == 6603 or id == 1 or n == "Attack" or n == "Auto Attack" then return "Melee" end
if id == 75 or n == "Auto Shot" then return "Auto Shot" end
if type(n) == "string" and not S(n) and n ~= "" then return n end
return "Spell " .. id
end
ns.DMSpellName = SpellName
-- Iterate combatSpells rows with a readable amount above zero.
local function Spells(src)
local list = type(src) == "table" and src.combatSpells
if type(list) ~= "table" or S(list) then return function() end end
local i = 0
return function()
while true do
i = i + 1
local sp = list[i]
if sp == nil then return nil end
if type(sp) == "table" then
local amt = Num(sp.totalAmount)
if amt and amt > 0 then return sp, amt end
end
end
end
end
local function Total(t)
if type(t) ~= "table" then return nil end
return Num(t.totalAmount) or Num(t.amount) or Num(t.total) or Num(t.value)
end
local function Sources(sess)
local list = type(sess) == "table" and (sess.combatSources or sess.sources)
return type(list) == "table" and not S(list) and list or nil
end
local function IsMe(src, me, myAmt)
local isMe = src.isLocalPlayer
if not S(isMe) and isMe == true then return true end
local g = src.sourceGUID
if me and not S(g) and g == me then return true end
if myAmt and Total(src) == myAmt then
local cls = src.classFilename
local _, mine = UnitClass("player")
if not S(cls) and cls == mine then return true end
end
return false
end
local function Call(fname, a, b, c)
local DM = C_DamageMeter
local fn = type(DM) == "table" and DM[fname]
if type(fn) ~= "function" then return nil end
local ok, res = pcall(fn, a, b, c)
if ok and type(res) == "table" then return res end
end
-- My source for one fight and one meter type: totals and per-spell rows.
local function MySource(id, kind)
local t, me = DT[kind], Me()
if me then
local res = Call("GetCombatSessionSourceFromID", id, t, me)
local v = Total(res)
if v then ns.src.dmCall = "GetCombatSessionSourceFromID" return v, res end
end
local list = Sources(Call("GetCombatSessionFromID", id, t))
if list then
for _, src in pairs(list) do
if type(src) == "table" and IsMe(src, me) then
local v = Total(src)
if v then ns.src.dmCall = "GetCombatSessionFromID" return v, src end
end
end
end
end
-- Current fight, by type (used when the client has no fight list, and for death causes).
local function Local(kind)
local t, me = DT[kind], Me()
if me then
local res = Call("GetCombatSessionSourceFromType", 1, t, me)
local v = Total(res)
if v then return v, res end
end
local list = Sources(Call("GetCombatSessionFromType", 1, t))
if list then
for _, src in pairs(list) do
if type(src) == "table" and IsMe(src, me) then
local v = Total(src)
if v then return v, src end
end
end
end
end
ns.DMLocal = Local
local function HasSessionList()
return type(C_DamageMeter) == "table" and type(C_DamageMeter.GetAvailableCombatSessions) == "function"
end
local function SessionList()
if not HasSessionList() then return nil end
local ok, list = pcall(C_DamageMeter.GetAvailableCombatSessions)
if not ok or type(list) ~= "table" or S(list) then return nil end
local out = {}
for _, s in pairs(list) do
if type(s) == "table" and Num(s.sessionID) then out[#out + 1] = s end
end
table.sort(out, function(a, b) return a.sessionID < b.sessionID end)
return out
end
ns.DMSessionList = SessionList
-- Biggest single hit: combatSpellDetails holds per-spell detail. Its field names are not
-- documented, so any readable number whose key says max / largest / biggest / highest is
-- taken as the biggest hit (only if it is not more than the spell's total).
local function ProbeDetails(sp, nm, amt)
local det = sp.combatSpellDetails
if type(det) ~= "table" or S(det) then return end
local best, bestCrit
local function Walk(t, depth)
for k, v in pairs(t) do
if type(v) == "table" and depth < 2 then Walk(v, depth + 1)
elseif type(k) == "string" and not S(v) and type(v) == "number" then
local lk = k:lower()
ns.src.dmDetailKeys = ns.src.dmDetailKeys or {}
ns.src.dmDetailKeys[k] = true
if (lk:find("max") or lk:find("largest") or lk:find("biggest") or lk:find("highest")) and v > 0 and v <= amt then
if lk:find("crit") then
if not bestCrit or v > bestCrit then bestCrit = v end
elseif not best or v > best then best = v; ns.src.dmHitKey = k end
end
end
end
end
Walk(det, 0)
if best then
Max("max_hit", best, nm)
if ns.settings then ns.settings.dmHitReadable = true end
end
if bestCrit then Max("max_crit", bestCrit, nm) end
end
local function B_InGroup()
local g = IsInGroup and IsInGroup()
if S(g) then return false end
return g and true or false
end
-- ------------------------------------------------------------ one fight
local function ReadFight(sess, credited, allowMissing, accept)
local id = sess and sess.sessionID
local name = Name(sess and sess.name)
local dur = sess and Num(sess.durationSeconds)
local solo = not B_InGroup()
local function Read(kind)
if id then return MySource(id, kind) end
return Local(kind)
end
local before = secretReads
local d, dsrc = Read("damage")
local h, hsrc = Read("heal")
local tk, tsrc = Read("taken")
local av, asrc = Read("avoidable")
local it, dp = Read("interrupts"), Read("dispels")
-- A numbered row can arrive before its totals become readable. No counters
-- or cursor movement until the row settles or the bounded retry expires.
if not allowMissing and (secretReads > before or not (d or h or tk or av or it or dp)
or (credited and (d == nil or tk == nil))) then return false end
-- Mark the row read before applying its effects, so a detail field error
-- cannot make a later wake apply the same totals again.
accept()
ns.src.dmSessions = (ns.src.dmSessions or 0) + 1
if name then Tally("foughtenemies", name) end
if dur then Add("dm_fight_time", dur); Max("max_dm_fight", dur, name) end
-- damage done
if d and d > 0 then
Persist()
ns.src.dmCounted = (ns.src.dmCounted or 0) + d
if not (credited and credited.skipDone) then Add("dmg_done", math.max(0, d - (credited and credited.done or 0))) end
Max("max_fight_damage", d, name or Zone())
Add("dm_fights_dmg", 1)
Tally("dmglevel", LevelKey(), d)
if dur and dur > 0 then Tally("timelevel", LevelKey(), dur) end
local dps = (dur and dur >= 3) and Round1(d / dur) or nil
if dps then Max("max_fight_dps", dps, name) end
local top, topAmt = nil, 0
for sp, amt in Spells(dsrc) do
local nm = SpellName(sp.spellID)
local det = sp.combatSpellDetails
if type(det) ~= "table" or S(det) then det = {} end
local pet = (ns.Bool(det.isPet) and (Name(det.unitName) or "Pet")) or Name(sp.creatureName)
if pet then
Add("dmg_pet", amt); Tally("petdamage", pet, amt)
elseif nm then
Tally("spelldamage", nm, amt)
Tally("bandspells", Band() .. " · " .. nm, amt)
if amt > topAmt then top, topAmt = nm, amt end
end
local ok = Num(sp.overkillAmount); if ok and ok > 0 then Add("overkill", ok) end
ProbeDetails(sp, pet and (pet .. " (pet)") or nm, amt)
end
if top then Max("max_spell_fight", topAmt, top) end
-- boss fights: a boss died within the last 20 seconds
local boss = ns.lastBossKill
if boss and GetTime() - boss.t < 20 and not boss.read then
boss.read = true
Tally("bossdamage", boss.name, d)
Max("max_boss_damage", d, boss.name)
if dps then Max("best_boss_dps", dps, boss.name) end
end
-- group share: your part of everyone's damage in this fight
if id and not solo then
local list = Sources(Call("GetCombatSessionFromID", id, DT.damage))
if list then
local total, players, top2, me = 0, 0, 0, Me()
for _, src in pairs(list) do
local v = type(src) == "table" and Total(src)
if v and v > 0 then
total = total + v; players = players + 1
if not IsMe(src, me, d) and v > top2 then top2 = v end
end
end
if players >= 2 and total > 0 then
Add("group_fights")
Add("group_dmg_all", total); Add("group_dmg_mine", d)
Max("max_group_share", math.floor(d * 100 / total + 0.5), name)
if d >= top2 then Add("meter_topped") end
end
end
end
end
-- healing done
if h and h > 0 then
Persist(); Add("heal_done", h); Max("max_fight_heal", h, name or Zone())
for sp, amt in Spells(hsrc) do local nm = SpellName(sp.spellID); if nm then Tally("spellheal", nm, amt) end end
end
-- damage taken
if tk and tk > 0 then
Persist()
if not (credited and credited.skipTaken) then Add("dmg_taken", math.max(0, tk - (credited and credited.taken or 0))) end
Max("max_fight_taken", tk, name or Zone())
for sp, amt in Spells(tsrc) do
local det = sp.combatSpellDetails
if type(det) ~= "table" or S(det) then det = {} end
local who = Name(det.unitName) or Name(sp.creatureName) or name
if who then Tally("hurtby", who, amt) end
local nm = SpellName(sp.spellID); if nm then Tally("hurtbyspell", nm, amt) end
if ns.Bool(sp.isDeadly) then Add("deadly_hits", 1) end
end
end
-- avoidable damage taken ("standing in the fire")
if av and av > 0 then
Persist(); Add("dmg_avoidable", av); Add("fights_in_fire")
Max("max_fight_avoidable", av, name or Zone())
for sp, amt in Spells(asrc) do local nm = SpellName(sp.spellID); if nm then Tally("standinfire", nm, amt) end end
end
-- damage each enemy took (only yours while solo)
if id and solo then
local list = Sources(Call("GetCombatSessionFromID", id, DT.enemy))
if list then
for _, src in pairs(list) do
local v = type(src) == "table" and Total(src)
local who = type(src) == "table" and Name(src.name)
if v and v > 0 and who then Tally("damagetoenemy", who, v) end
end
end
end
-- deaths in your group during the fight (not yours: those are counted on PLAYER_DEAD)
if id and not solo then
local list = Sources(Call("GetCombatSessionFromID", id, DT.deaths))
if list then
local me = Me()
for _, src in pairs(list) do
if type(src) == "table" and not IsMe(src, me) then
local t = Num(src.deathTimeSeconds)
if t and t > 0 then Add("allies_died") end
end
end
end
end
if it and it > 0 then Persist(); Add("interrupts", it) end
if dp and dp > 0 then Persist(); Add("dispels", dp) end
return true
end
-- Fallback damage is credited immediately. Match it to numbered meter results
-- so the meter fills only the missing amount in each direction.
local activeFight, currentFight, lastSeenID, firstFightBaseline
local pendingFights, creditsByID = {}, {}
local readGeneration, readPending, retries = 0, false, 0
local MAX_RETRIES = 2
function ns.TrackFightDamage(kind, amount)
if not activeFight then return false end
activeFight[kind] = (activeFight[kind] or 0) + amount
return true
end
local function NewestID(list)
return list and (#list > 0 and list[#list].sessionID or 0)
end
local function MatchCredits(list)
local newest = NewestID(list)
if newest == nil then return end
if lastSeenID and newest < lastSeenID then creditsByID = {} end
lastSeenID = newest
local oldest = list[1] and list[1].sessionID
if oldest then
for id in pairs(creditsByID) do
if id < oldest then creditsByID[id] = nil end
end
end
if #pendingFights == 0 or newest == 0 then return end
-- More than one fight can finish before the list updates. Claim a result
-- only when the observed IDs identify a unique match. Where coverage
-- overlaps, keep direct totals and omit only the affected meter direction.
local matches, waiting = {}, {}
for _, fight in ipairs(pendingFights) do
local floor = fight.baseline or 0
if newest < floor then floor = 0 end
local ids = {}
for _, session in ipairs(list) do
local id = session.sessionID
if id > floor and not creditsByID[id] then ids[#ids + 1] = id end
end
if #ids == 0 then waiting[#waiting + 1] = fight end
for _, id in ipairs(ids) do
local match = matches[id]
if not match then
match = { done = 0, taken = 0, ambiguous = false }
matches[id] = match
else
match.ambiguous = true
end
match.ambiguous = match.ambiguous or fight.ambiguous or #ids > 1
match.done = math.max(match.done, fight.done or 0)
match.taken = math.max(match.taken, fight.taken or 0)
end
end
pendingFights = waiting
for id, match in pairs(matches) do
if match.ambiguous then
match.skipDone = match.done > 0
match.skipTaken = match.taken > 0
end
creditsByID[id] = match
end
end
-- ------------------------------------------------------------ after combat: all unread fights
function ns.DMCatchUp(allowMissing)
if not ns.char or ns.cleuSeen or activeFight then return end
local m = ns.char.meta
local list = SessionList()
if not list then
if currentFight and type(C_DamageMeter) == "table" and not HasSessionList() then
if not ReadFight(nil, currentFight, allowMissing, function() currentFight = nil end) then return true end
end
return
end
currentFight = nil
MatchCredits(list)
if #list == 0 then return end
local newest = list[#list].sessionID
if not m.dmLastID or newest < m.dmLastID then
local baseline = firstFightBaseline
-- With no observed start, avoid backfilling older meter history.
m.dmLastID = baseline and baseline <= newest and baseline or newest - 1
end
firstFightBaseline = nil
local done = 0
for _, sess in ipairs(list) do
if sess.sessionID > m.dmLastID then
if not ReadFight(sess, creditsByID[sess.sessionID], allowMissing, function()
m.dmLastID = sess.sessionID
creditsByID[sess.sessionID] = nil
end) then return true end
done = done + 1
if done >= MAX_PER_PASS then break end
end
end
for id in pairs(creditsByID) do
if id <= m.dmLastID then creditsByID[id] = nil end
end
end
local function InvalidateRead()
readGeneration = readGeneration + 1
readPending, retries = false, 0
end
local function QueueRead(delay)
if readPending or activeFight then return end
local generation = readGeneration
readPending = true
C_Timer.After(delay, ns.Guard("damage meter read", function()
if generation ~= readGeneration then return end
readPending = false
local waiting = ns.DMCatchUp(retries >= MAX_RETRIES)
if waiting and retries < MAX_RETRIES then
retries = retries + 1
QueueRead(0.5)
elseif not waiting then
retries = 0
end
end))
end
local function SettleHealth(fight)
if fight.health and fight.health > (fight.taken or 0) then
Add("dmg_taken", fight.health - (fight.taken or 0))
fight.taken = fight.health
end
end
local function AddPending(fight)
pendingFights[#pendingFights + 1] = fight
-- Keep observations bounded if the meter is disabled or publishes late.
if #pendingFights > MAX_PER_PASS then
local oldest = table.remove(pendingFights, 1)
local nextFight = pendingFights[1]
nextFight.baseline = math.min(oldest.baseline or 0, nextFight.baseline or 0)
nextFight.done = math.max(oldest.done or 0, nextFight.done or 0)
nextFight.taken = math.max(oldest.taken or 0, nextFight.taken or 0)
nextFight.ambiguous = true
end
end
On("PLAYER_REGEN_DISABLED", function()
if activeFight then return end
if currentFight and not HasSessionList() and ns.Bool(UnitAffectingCombat("player")) == false then
ns.DMCatchUp()
end
InvalidateRead()
local list = SessionList()
MatchCredits(list)
local baseline, cursor = NewestID(list), ns.char.meta.dmLastID
if firstFightBaseline == nil and (not cursor or (baseline and baseline < cursor)) then
firstFightBaseline = baseline or false
end
activeFight = { baseline = baseline or cursor }
currentFight = nil
end)
On("PLAYER_REGEN_ENABLED", function()
local fight = activeFight
activeFight = nil
if fight then
SettleHealth(fight)
currentFight = fight
if HasSessionList() then
AddPending(fight)
MatchCredits(SessionList())
end
end
QueueRead(0.8)
end)
On("DAMAGE_METER_COMBAT_SESSION_UPDATED", function(_, _, id)
if Num(id) and id > (ns.char.meta.dmLastID or 0) and not activeFight then QueueRead(0) end
end)
On("DAMAGE_METER_CURRENT_SESSION_UPDATED", function()
if currentFight and not HasSessionList() then QueueRead(0) end
end)
ns.FlushCombatDamage = ns.Guard("save combat totals", function()
if activeFight then SettleHealth(activeFight)
elseif HasSessionList() then ns.DMCatchUp() end
end)
function ns.SaveFightCorrelation()
if HasSessionList() and (activeFight or #pendingFights > 0 or next(creditsByID)) then
return { pending = pendingFights, byID = creditsByID, active = activeFight,
seen = lastSeenID, baseline = firstFightBaseline }
end
end
-- Login / reload: fights from before are not counted again. Also note the meter's own
-- overall total so /lts debug can compare it with what was counted this session.
local function OverallMine()
local v = Total(Call("GetCombatSessionSourceFromType", 0, DT.damage, Me()))
return v
end
On("PLAYER_ENTERING_WORLD", function(_, isLogin, isReload)
if not (isLogin or isReload) or not ns.char then return end
activeFight, currentFight, lastSeenID, firstFightBaseline = nil, nil, nil, nil
pendingFights, creditsByID = {}, {}
InvalidateRead()
local saved = ns.savedFightCorrelation
ns.savedFightCorrelation = nil
local restored = isReload and not isLogin and type(saved) == "table"
if restored then
pendingFights = type(saved.pending) == "table" and saved.pending or {}
creditsByID = type(saved.byID) == "table" and saved.byID or {}
lastSeenID, firstFightBaseline = saved.seen, saved.baseline
if type(saved.active) == "table" then
if ns.Bool(UnitAffectingCombat("player")) then activeFight = saved.active
else AddPending(saved.active) end
end
end
local list = SessionList()
if not restored and list and #list > 0 then
local newest = list[#list].sessionID
local m = ns.char.meta
if not m.dmLastID or newest < m.dmLastID then m.dmLastID = newest end
end
ns.src.dmOverall0 = OverallMine() or 0
ns.src.dmCounted = 0
if restored then QueueRead(0.8) end
end)
-- "Missed fights" check for /lts debug: meter overall since login vs what was counted.
function ns.DMGap()
local now = OverallMine()
if not now then return nil end
local meter = now - (ns.src.dmOverall0 or 0)
return meter, ns.src.dmCounted or 0
end
-- Killing blow from the current fight: the hit marked deadly, else the biggest hitter.
function ns.DMKiller()
local _, src = Local("taken")
if type(src) ~= "table" then return nil end
local deadly, biggest, bigAmt
for sp, amt in Spells(src) do
if ns.Bool(sp.isDeadly) then deadly = sp end
if not bigAmt or amt > bigAmt then biggest, bigAmt = sp, amt end
end
local sp = deadly or biggest
if not sp then return nil end
local det = type(sp.combatSpellDetails) == "table" and sp.combatSpellDetails or {}
return { name = Name(det.unitName) or Name(sp.creatureName), spell = SpellName(sp.spellID), deadly = deadly and true or false }
end
-- In a fight now, or one ended in the last 3 seconds.
local inFight, fightEnd = false, -100
On("PLAYER_REGEN_DISABLED", function() inFight = true end)
On("PLAYER_REGEN_ENABLED", function() inFight = false; fightEnd = GetTime() end)
function ns.FightingRecently() return inFight or GetTime() - fightEnd < 3 end
-- Your death-recap ID from the meter's deaths list for the current fight.
function ns.DMMyRecapID()
local list = Sources(Call("GetCombatSessionFromType", 1, DT.deaths))
if not list then return nil end
local me, best = Me(), nil
for _, src in pairs(list) do
if type(src) == "table" and IsMe(src, me) then
local id = Num(src.deathRecapID)
if id and id > 0 and (not best or id > best) then best = id end
end
end
return best
end