-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
485 lines (437 loc) · 15.8 KB
/
Copy pathCore.lua
File metadata and controls
485 lines (437 loc) · 15.8 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
--[[ RaidLeadKit - who in the group can provide each variant, and what is up.
Nothing here runs on a timer. Roster, pet and talent changes only mark the
state dirty; it is worked out again when the panel is opened (see
RLK:Refresh). The scan for active auras runs once a second, and only while
the panel is open (Display.lua). ]]
RaidLeadKit = RaidLeadKit or {}
local RLK = RaidLeadKit
-- variant states, in rising order of news
RLK.NONE, RLK.UNKNOWN, RLK.IN_RAID = 1, 2, 3
local LGT = LibStub("LibGroupTalents-1.0")
RLK.defaults = {
point = { "CENTER", "CENTER", 0, 200 },
locked = false,
showMinor = false,
hidden = false,
anchor = "auto", -- see RLK.ANCHORS
buttonSize = 32, -- the button
iconSize = 24, -- the icons in the panel
rows = 12, -- rows per column before the next column starts
lootWarning = true, -- warn on a boss pull when master loot is not set
-- Marking rules, in order: { key = npc id or name, icon = 1..8, or 0 for
-- the first icon no rule asks for by name ("auto") }
markRules = {},
}
-- names in Esc > Key Bindings (Bindings.xml)
BINDING_HEADER_RAIDLEADKIT = "|cffff2020Raid|rLeadKit"
BINDING_NAME_RAIDLEADKIT_TOGGLE = "Open / close the checklist"
-- ---------------------------------------------------------------------------
-- marking rules
-- ---------------------------------------------------------------------------
RLK.MARK_ICONS = { -- raid target index -> its texture and English name
[1] = { "Interface\\TargetingFrame\\UI-RaidTargetingIcon_1", "Star" },
[2] = { "Interface\\TargetingFrame\\UI-RaidTargetingIcon_2", "Circle" },
[3] = { "Interface\\TargetingFrame\\UI-RaidTargetingIcon_3", "Diamond" },
[4] = { "Interface\\TargetingFrame\\UI-RaidTargetingIcon_4", "Triangle" },
[5] = { "Interface\\TargetingFrame\\UI-RaidTargetingIcon_5", "Moon" },
[6] = { "Interface\\TargetingFrame\\UI-RaidTargetingIcon_6", "Square" },
[7] = { "Interface\\TargetingFrame\\UI-RaidTargetingIcon_7", "Cross" },
[8] = { "Interface\\TargetingFrame\\UI-RaidTargetingIcon_8", "Skull" },
}
-- a rule's key is an npc id (a number) or a name (a string)
function RLK:RuleKey(text)
text = strtrim(text or "")
if text == "" then return nil end
return tonumber(text) or text
end
function RLK:FindRule(key)
for i, rule in ipairs(self.db.markRules) do
if rule.key == key then return i, rule end
end
end
function RLK:AddRule(text, icon)
local key = self:RuleKey(text)
if not key then return false end
if self:FindRule(key) then return false, "already in the list" end
table.insert(self.db.markRules, { key = key, icon = icon or 0 })
return true
end
-- renaming a rule in place; the key stays as it was if the new one is taken
function RLK:SetRuleKey(index, text)
local rule = self.db.markRules[index]
if not rule then return false end
local key = self:RuleKey(text)
if not key then return false end
if key == rule.key then return true end
if self:FindRule(key) then return false, "already in the list" end
rule.key = key
return true
end
function RLK:RemoveRule(index)
table.remove(self.db.markRules, index)
end
function RLK:Print(msg)
DEFAULT_CHAT_FRAME:AddMessage("|cffff2020Raid|rLeadKit: " .. tostring(msg))
end
-- every row list with the kind of aura its variants leave
local SECTIONS
-- ---------------------------------------------------------------------------
-- data prep: localized names, lookups
-- ---------------------------------------------------------------------------
local famKeyByName = {} -- localized pet family -> PET_FAMILIES key
RLK.variantsByAura = { buff = {}, debuff = {} } -- aura name -> { variant, ... }
local function prepare(rows, kind)
local horde = UnitFactionGroup("player") == "Horde"
for _, row in ipairs(rows) do
row.kind = kind
for _, v in ipairs(row.variants) do
v.row = row
local id = (horde and v.hordeSpell) or v.spell
v.spellId = id
v.name, _, v.icon = GetSpellInfo(id)
v.name = v.name or ("spell " .. id)
v.talentName = v.talent and GetSpellInfo(v.talent)
v.providers, v.unknown, v.holders = {}, {}, {}
v.state = RLK.NONE
for _, auraId in ipairs(v.auras) do
local aura = GetSpellInfo(auraId)
if aura then
local list = RLK.variantsByAura[kind][aura] or {}
list[#list + 1] = v
RLK.variantsByAura[kind][aura] = list
end
end
end
end
end
-- ---------------------------------------------------------------------------
-- group members
-- ---------------------------------------------------------------------------
-- unit tokens of everyone in the group, the player included; reused table
local units = {}
function RLK:GroupUnits()
wipe(units)
local raid = GetNumRaidMembers()
if raid > 0 then
for i = 1, raid do units[#units + 1] = "raid" .. i end
else
units[1] = "player"
for i = 1, GetNumPartyMembers() do units[#units + 1] = "party" .. i end
end
return units
end
local function petToken(unit)
if unit == "player" then return "pet" end
return (unit:gsub("(%d+)$", "pet%1"))
end
-- the owner of a pet token ("raidpet5" -> "raid5", "pet" -> "player")
local function ownerToken(unit)
if unit == "pet" then return "player" end
local kind, n = unit:match("^(%a-)pet(%d+)$")
if kind then return kind .. n end
return unit
end
RLK.ownerToken = ownerToken
-- The player's own talents, read straight from the game: talent name -> rank,
-- active spec only. LibGroupTalents lags behind a spec switch of our own (it
-- reads the talents as the switch is cast, before the game has changed them).
local myTalents = {}
local function readMyTalents()
wipe(myTalents)
for tab = 1, GetNumTalentTabs() do
for i = 1, GetNumTalents(tab) do
local name, _, _, _, rank = GetTalentInfo(tab, i)
if name and rank and rank > 0 then myTalents[name] = rank end
end
end
end
-- Can this member provide the variant? true, false, or nil for "cannot tell yet".
local function provides(v, unit)
if v.pet then
local pet = petToken(unit)
if not UnitExists(pet) then return nil end
local key = famKeyByName[UnitCreatureFamily(pet) or ""]
if not key then return nil end -- no family, or a client language we do not know
return key == v.pet
end
if v.talentName then
if UnitIsUnit(unit, "player") then return myTalents[v.talentName] ~= nil end
-- no spec means the talents have not arrived yet
if not LGT:GetUnitTalentSpec(unit) then return nil end
return LGT:UnitHasTalent(unit, v.talentName) ~= nil
end
return true
end
-- Does this member care about the row's buff? (see "audience" in Data.lua)
local PHYSICAL = { WARRIOR = true, ROGUE = true, DEATHKNIGHT = true, HUNTER = true }
local CASTER = { MAGE = true, WARLOCK = true, PRIEST = true }
function RLK:Wants(row, unit, class)
local a = row.audience
if a == "mana" then
return UnitPowerType(unit) == 0
elseif a == "physical" then
if PHYSICAL[class] then return true end
if CASTER[class] then return false end
-- hybrids go by their role; not known yet counts them in
local role = LGT:GetUnitRole(unit)
return role == nil or role == "melee" or role == "tank"
end
return true
end
-- ---------------------------------------------------------------------------
-- state
-- ---------------------------------------------------------------------------
RLK.dirty = true
function RLK:MarkDirty()
self.dirty = true
if self.OnDirty then self:OnDirty() end
end
-- The group as last read: { unit, name, class, here, covered }. The entries
-- are reused from read to read, so a scan a second makes no garbage.
-- here false for anyone whose auras cannot be read (out of range,
-- dead, offline); they are left out of the missing counts
-- covered [row] = true when the member carries any variant of the row
RLK.members = {}
local memberPool = {}
local function readMembers()
local members = RLK.members
local units = RLK:GroupUnits()
for i, unit in ipairs(units) do
local m = memberPool[i]
if not m then
m = { covered = {} }
memberPool[i] = m
end
local _, class = UnitClass(unit)
m.unit, m.name, m.class = unit, UnitName(unit), class
members[i] = m
end
for i = #units + 1, #members do members[i] = nil end
return members
end
-- v.state, v.providers (names) and v.unknown (names) for every variant
function RLK:Refresh()
if not self.dirty then return end
self.dirty = false
local members = readMembers()
readMyTalents()
for _, s in ipairs(SECTIONS) do
for _, row in ipairs(s.rows) do
for _, v in ipairs(row.variants) do
wipe(v.providers)
wipe(v.unknown)
for _, m in ipairs(members) do
if m.class == v.class then
local ok = provides(v, m.unit)
if ok then
v.providers[#v.providers + 1] = m.name
elseif ok == nil then
v.unknown[#v.unknown + 1] = m.name
end
end
end
v.state = #v.providers > 0 and self.IN_RAID or #v.unknown > 0 and self.UNKNOWN or self.NONE
end
end
end
end
-- ---------------------------------------------------------------------------
-- active auras
-- ---------------------------------------------------------------------------
local DEBUFF_UNITS = { "target", "focus", "boss1", "boss2", "boss3", "boss4" }
local scannedEnemies = {} -- guid -> true, reused
-- The class behind an aura's caster, pets counting as their owner; nil when
-- the caster cannot be seen (out of range, a totem).
local function casterClass(caster)
if not caster then return nil end
local _, class = UnitClass(ownerToken(caster))
return class
end
-- Does an aura cast by this class belong to the variant? With no caster to
-- go by, every variant whose class is in the group counts.
local function fits(v, class)
if class then return v.class == class end
return v.state ~= RLK.NONE
end
-- v.active, and while up: v.holders (member entries carrying a buff),
-- v.expires (soonest end, nil when it does not run out), v.stacks (debuffs);
-- row.active and row.missing; RLK.fighting
function RLK:ScanActive()
for _, s in ipairs(SECTIONS) do
for _, row in ipairs(s.rows) do
for _, v in ipairs(row.variants) do
v.active, v.expires, v.stacks = false, nil, nil
wipe(v.holders)
end
end
end
local buffs = self.variantsByAura.buff
for _, m in ipairs(readMembers()) do
local unit = m.unit
m.here = UnitIsConnected(unit) and not UnitIsDeadOrGhost(unit) and UnitIsVisible(unit)
wipe(m.covered)
local i = 1
while true do
local name, _, _, _, _, duration, expires, caster = UnitBuff(unit, i)
if not name then break end
local list = buffs[name]
if list then
local class = casterClass(caster)
for _, v in ipairs(list) do
if fits(v, class) then
v.active = true
local holders = v.holders
if holders[#holders] ~= m then holders[#holders + 1] = m end
if duration and duration > 0 and (not v.expires or expires < v.expires) then
v.expires = expires
end
m.covered[v.row] = true
end
end
end
i = i + 1
end
end
-- debuffs: the first hostile unit in DEBUFF_UNITS order that carries one
-- wins; a unit seen under two tokens (the target is boss1) is read once
local debuffs = self.variantsByAura.debuff
wipe(scannedEnemies)
for _, unit in ipairs(DEBUFF_UNITS) do
local guid = UnitExists(unit) and UnitCanAttack("player", unit) and UnitGUID(unit)
if guid and not scannedEnemies[guid] then
scannedEnemies[guid] = true
local i = 1
while true do
local name, _, _, count, _, duration, expires, caster = UnitDebuff(unit, i)
if not name then break end
local list = debuffs[name]
if list then
local class = casterClass(caster)
for _, v in ipairs(list) do
if not v.active and fits(v, class) then
v.active = true
v.stacks = count and count > 1 and count or nil
v.expires = duration and duration > 0 and expires or nil
end
end
end
i = i + 1
end
end
end
-- per row: is any variant up, and for counted rows how many still lack it
for _, s in ipairs(SECTIONS) do
for _, row in ipairs(s.rows) do
row.active = false
for _, v in ipairs(row.variants) do
if v.active then row.active = true break end
end
row.missing = row.count and self:RowMissing(row) or 0
end
end
-- A buff that comes from a proc or a cooldown is only missing in a fight;
-- a debuff also needs an enemy to put it on.
self.inCombat = UnitAffectingCombat("player")
self.fighting = self.inCombat and UnitExists("target")
and UnitCanAttack("player", "target") and not UnitIsDead("target")
end
-- For a counted row: how many members want the buff but carry no variant of
-- it. With lists = true also those members, and the ones whose auras could
-- not be read (not counted); only a tooltip asks for them.
function RLK:RowMissing(row, lists)
local n, missing, away = 0, lists and {}, lists and {}
for _, m in ipairs(self.members) do
if not m.covered[row] and self:Wants(row, m.unit, m.class) then
if m.here then
n = n + 1
if missing then missing[#missing + 1] = m end
elseif away then
away[#away + 1] = m
end
end
end
return n, missing, away
end
-- ---------------------------------------------------------------------------
-- init + events
-- ---------------------------------------------------------------------------
local function copy(v)
if type(v) ~= "table" then return v end
local t = {}
for k, x in pairs(v) do t[k] = copy(x) end
return t
end
function RLK:Init()
RaidLeadKitDB = RaidLeadKitDB or {}
RaidLeadKitDB.scale = nil -- replaced by buttonSize / iconSize
for k, v in pairs(self.defaults) do
if RaidLeadKitDB[k] == nil then RaidLeadKitDB[k] = copy(v) end
end
self.db = RaidLeadKitDB
for key, names in pairs(self.PET_FAMILIES) do
for _, n in ipairs(names) do famKeyByName[n] = key end
end
SECTIONS = {
{ rows = self.BUFFS, kind = "buff" },
{ rows = self.DEBUFFS, kind = "debuff" },
{ rows = self.TOTEMS, kind = "buff" },
{ rows = self.PALADIN_BLESSINGS, kind = "buff" },
{ rows = self.PALADIN_AURAS, kind = "buff" },
}
for _, s in ipairs(SECTIONS) do prepare(s.rows, s.kind) end
local function dirty() RLK:MarkDirty() end
LGT.RegisterCallback(self, "LibGroupTalents_Update", dirty)
LGT.RegisterCallback(self, "LibGroupTalents_Remove", dirty)
LGT.RegisterCallback(self, "LibGroupTalents_RoleChange", dirty)
self:CreateDisplay()
-- missing when Config.lua was added to the .toc after the client started:
-- /reload does not pick up new files, a restart does
if self.InitConfig then self:InitConfig() end
end
-- ---------------------------------------------------------------------------
-- spec switches of other players
-- ---------------------------------------------------------------------------
-- Someone casting "Activate Primary/Secondary Spec" gets their talents read
-- again a few seconds later: by then the game hands out the new spec, and
-- LibGroupTalents no longer throttles a read it just did for that player.
local SPEC_SWITCH = {}
for _, id in ipairs(TALENT_ACTIVATION_SPELLS or {}) do
local name = GetSpellInfo(id)
if name then SPEC_SWITCH[name] = true end
end
local REREAD_AFTER = 6
local rereads = {} -- guid -> when
local function onUpdate(self)
local now = GetTime()
for guid, when in pairs(rereads) do
if now >= when then
rereads[guid] = nil
LGT:RefreshTalentsByGUID(guid)
RLK:MarkDirty()
end
end
if not next(rereads) then self:SetScript("OnUpdate", nil) end
end
local driver = CreateFrame("Frame")
driver:RegisterEvent("PLAYER_LOGIN")
driver:SetScript("OnEvent", function(self, event, unit, spell)
if event == "PLAYER_LOGIN" then
RLK:Init()
self:RegisterEvent("RAID_ROSTER_UPDATE")
self:RegisterEvent("PARTY_MEMBERS_CHANGED")
self:RegisterEvent("UNIT_PET")
self:RegisterEvent("PLAYER_TALENT_UPDATE")
self:RegisterEvent("ACTIVE_TALENT_GROUP_CHANGED")
self:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
elseif event == "UNIT_SPELLCAST_SUCCEEDED" then
if not SPEC_SWITCH[spell] or UnitIsUnit(unit, "player") then return end
if not (unit:match("^raid%d+$") or unit:match("^party%d+$")) then return end
local guid = UnitGUID(unit)
if guid then
rereads[guid] = GetTime() + REREAD_AFTER
self:SetScript("OnUpdate", onUpdate)
end
return
end
RLK:MarkDirty()
end)