-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomarker.lua
502 lines (421 loc) · 15 KB
/
automarker.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
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
local addon, ns = ...
local LibMobs = LibStub("LibMobs")
assert(LibMobs, "Filger requires LibMobs")
-- Blizzard
local NUM_RAID_ICONS = _G.NUM_RAID_ICONS or 8
local MAX_RAID_MEMBERS = _G.MAX_RAID_MEMBERS or 40
local MAX_PARTY_MEMBERS = _G.MAX_PARTY_MEMBERS or 4
local CanBeRaidTarget = _G.CanBeRaidTarget
local GetRaidTargetIndex = _G.GetRaidTargetIndex
local IsInGroup =_G.IsInGroup
local IsInRaid =_G.IsInRaid
local IsInInstance =_G.IsInInstance
-- local SetRaidTarget = _G.SetRaidTarget
local UnitAffectingCombat = _G.UnitAffectingCombat
local UnitThreatSituation = _G.UnitThreatSituation
local UnitExists = _G.UnitExists
local UnitName = _G.UnitName
local UnitGUID = _G.UnitGUID
-- local UnitInRange = _G.UnitInRange
local UnitIsDead = _G.UnitIsDead
local UnitIsUnit = _G.UnitIsUnit
local UnitIsGroupAssistant =_G.UnitIsGroupAssistant
local UnitIsGroupLeader =_G.UnitIsGroupLeader
local CombatLogGetCurrentEventInfo =_G.CombatLogGetCurrentEventInfo
local CombatLog_Object_IsA =_G.CombatLog_Object_IsA
local GetNumGroupMembers =_G.GetNumGroupMembers
-- Mine
local NUM_NAMEPLATES = 40
local UPDATE_INTERVAL = 1
local COMBAT_EVENTS = {
["DAMAGE_SHIELD"] = true,
["DAMAGE_SPLIT"] = true,
["RANGE_DAMAGE"] = true,
["SPELL_AURA_APPLIED"] = true,
["SPELL_CAST_SUCCESS"] = true,
["SPELL_DAMAGE"] = true,
["SPELL_HEAL"] = true,
["SPELL_MISSED"] = true,
["SWING_DAMAGE"] = true,
["SWING_MISSED"] = true,
}
local RAID_ICONS = {
[8] = "Skull",
[7] = "Cross",
[6] = "Square",
[5] = "Moon",
[4] = "Triangle",
[3] = "Diamond",
[2] = "Circle",
[1] = "Star"
}
local element_proto = {
_combat = false,
units = {},
guids = {},
enemies = {},
nameplates = {},
-- loop
all = {},
actives = {},
sorted = {},
queue = {},
-- icons
assignedIcons = {},
manuallyAssignedIcons = {},
}
do
local units = {
["player"] = true,
["pet"] = true,
-- ["target"] = nil,
-- ["focus"] = nil,
-- ["mouseover"] = nil,
-- ["party1"] = nil,
-- ["party2"] = nil,
-- ["party3"] = nil,
-- ["party4"] = nil,
}
function element_proto:print(...)
print("|cffff8000AutoMarker|r", ...)
end
function element_proto:table_length(tbl)
local n = 0
for k, v in next, tbl do
n = n + 1
end
return n
end
function element_proto:AddUnit(unit, guid)
if unit and not guid then
guid = UnitGUID(unit)
end
self.units[guid or "none"] = unit
self.guids[unit or "none"] = guid
end
function element_proto:RemoveUnit(unit, guid)
if unit and not guid then
guid = UnitGUID(unit)
end
self.units[guid or "none"] = nil
self.guids[unit or "none"] = nil
end
function element_proto:GetUnitByGUID(guid)
local unit = self.units[guid]
if self.guids[unit] == guid then
return unit
end
return nil
end
function element_proto:GetRaidTargetIndex(unit, guid)
return GetRaidTargetIndex(unit) or self.manuallyAssignedIcons[guid] or self.assignedIcons[guid]
end
function element_proto:SetRaidTargetIndex(data, index)
SetRaidTarget(data.unit, index)
self.assignedIcons[index] = data
self.assignedIcons[data.guid] = index
end
function element_proto:GetNextRaidTargetIndex()
for iconIndex = NUM_RAID_ICONS, 1, -1 do
if self.assignedIcons[iconIndex] == nil and self.manuallyAssignedIcons[iconIndex] == nil then
return iconIndex
end
end
return nil
end
function element_proto:UpdateManuallyAssignedUnit(unit)
if unit and UnitExists(unit) then
local name = UnitName(unit)
local guid = UnitGUID(unit)
local iconIndex = GetRaidTargetIndex(unit)
if iconIndex and RAID_ICONS[iconIndex] then
if self.assignedIcons[iconIndex] then
-- the raid target was assigned by the addon itself
self.manuallyAssignedIcons[iconIndex] = nil
self.manuallyAssignedIcons[guid] = nil
else
-- the raid target was assigned by somebody else
self.manuallyAssignedIcons[iconIndex] = { unit = unit, guid = guid }
self.manuallyAssignedIcons[guid] = iconIndex
end
end
end
end
function element_proto:UpdateManuallyAssigned()
if IsInRaid() then
for index = 1, MAX_RAID_MEMBERS do
local unit = "raid" .. index
units[unit] = UnitExists(unit)
end
for index = 1, MAX_PARTY_MEMBERS do
local unit = "party" .. index
units[unit] = nil
end
elseif IsInGroup() then
for index = 1, MAX_PARTY_MEMBERS do
local unit = "party" .. index
units[unit] = UnitExists(unit)
end
else
for index = 1, MAX_RAID_MEMBERS do
local unit = "raid" .. index
units[unit] = nil
end
for index = 1, MAX_PARTY_MEMBERS do
local unit = "party" .. index
units[unit] = nil
end
end
self.manuallyAssignedIcons = table.wipe(self.manuallyAssignedIcons or {})
-- validate units
for unit, _ in next, units do
self:UpdateManuallyAssignedUnit(unit)
end
-- validate nameplates
for unit, _ in next, self.nameplates do
self:UpdateManuallyAssignedUnit(unit)
end
end
function element_proto:IsInCombat(unit, guid)
return (UnitThreatSituation("player", unit) ~= nil) -- check if you have threat
or (UnitAffectingCombat(unit)) -- check if you in combat
or (self.enemies[guid]) -- check if unit attack you or any group member
or (UnitIsUnit(unit .. "target", "player")) -- check if unit is targeting you
end
function element_proto:UnitIsInCombatWithParty(unit)
for member, _ in next, units do
if not UnitIsUnit(member, unit) and UnitThreatSituation(member, unit) ~= nil then
return true
end
end
return self:IsInCombat(unit)
end
end
--[[
FUNCTION element_proto:Enable()
Check conditions to see if we can automatically mark units
--]]
function element_proto:Enable()
-- check if player is inside a instance
local isInInstance, instanceType = IsInInstance()
local instanceName, _, _, _, _, _, _, instanceID, _, _ = GetInstanceInfo()
self.__instanceID = instanceID
-- check if player is in a raid/party group
local isInGroup, isInRaid = IsInGroup(), IsInRaid()
self.__inGroup = isInGroup or isInRaid
-- check if player if the group leader
local isLeader, isAssistant = UnitIsGroupLeader("player"), UnitIsGroupAssistant("player")
self.__isLeader = isLeader or not self.__inGroup
-- enabled when:
-- the player is inside an instance
-- the player is the group leader or if he is solo
local enabled = isInInstance and (isLeader or (not isInGroup) or (not isInRaid))
if enabled and not self.__init then
self:RegisterEvent("PLAYER_REGEN_ENABLED")
self:RegisterEvent("PLAYER_REGEN_DISABLED")
self:RegisterEvent("RAID_TARGET_UPDATE")
self:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self.__init = true
elseif not enabled then
self:UnregisterEvent("PLAYER_REGEN_ENABLED")
self:UnregisterEvent("PLAYER_REGEN_DISABLED")
self:UnregisterEvent("RAID_TARGET_UPDATE")
self:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self.__init = false
end
end
function element_proto:AssignRaidIcon(event, data)
if not data then return end
local unit = data.unit
local guid = data.guid
-- ignore if unit already have a raid target
-- if GetRaidTargetIndex(unit) then return end
if self.assignedIcons[guid] or self.manuallyAssignedIcons[guid] then return end
-- check if there is a raid target not in use
local iconIndex = self:GetNextRaidTargetIndex()
if iconIndex and self:UnitIsInCombatWithParty(unit) then
self:SetRaidTargetIndex(data, iconIndex)
else
table.insert(self.queue, data)
end
end
element_proto.SortData = function(a, b)
if (a.priority ~= b.priority) then
return a.priority > b.priority
end
return a.unit < b.unit
end
function element_proto:FilterData(data)
return data and data.unit and CanBeRaidTarget(data.unit) and data.npcID and data.mark
end
function element_proto:ProcessData(unit)
if UnitExists(unit) then
local name = UnitName(unit)
local guid = UnitGUID(unit)
-- break GUID into useful information, such as instanceID, zoneID, npcID, etc.
local guidInfo = LibMobs:ParseCreatureGUID(guid) or {}
-- check if unit is a important npc that should be watched
local creatureInfo = LibMobs:GetCreature(guidInfo.instanceID, guidInfo.npcID) or { priority = -1 }
return Mixin({ unit = unit, guid = guid, name = name }, guidInfo, creatureInfo)
end
return nil
end
--[[
FUNCTION element_proto:Update(event, unit)
--]]
function element_proto:Update(event, unit)
if not self.__init then return end
local changed = false
if not unit then
self.all = table.wipe(self.all or {})
self.actives = table.wipe(self.actives or {})
for unit, _ in next, self.nameplates do
local data = self:ProcessData(unit)
if data then
self.all[data.guid] = data
if self:FilterData(data) then
self.actives[data.guid] = true
end
end
end
changed = true
else
if UnitIsDead(unit) then
local guid = UnitGUID(unit)
self.all[guid] = nil
self.actives[guid] = nil
local iconIndex = GetRaidTargetIndex(unit) or self.manuallyAssignedIcons[iconIndex or 0] or self.assignedIcons[iconIndex or 0]
if iconIndex then
self.assignedIcons[iconIndex] = nil
self.manuallyAssignedIcons[iconIndex] = nil
end
changed = true
else
local data = self:ProcessData(unit)
if data then
self.all[data.guid] = data
if self:FilterData(data) then
self.actives[data.guid] = true
changed = true
end
end
end
end
if changed then
self.sorted = table.wipe(self.sorted or {})
-- self.queue = table.wipe(self.queue or {})
for guid, _ in next, self.actives do
table.insert(self.sorted, self.all[guid])
end
table.sort(self.sorted, self.SortData)
-- local max = math.min(#self.sorted, NUM_RAID_ICONS)
-- for index = 1, max do
-- self:AssignRaidIcon(event, self.sorted[index])
-- end
for index, data in next, self.sorted do
self:AssignRaidIcon(event, data, index)
end
end
end
function element_proto:PLAYER_REGEN_ENABLED()
self._combat = false
-- stop processing the queue
self:SetScript("OnUpdate", nil)
for index = 1, NUM_RAID_ICONS do
self:print(index, RAID_ICONS[index], self.assignedIcons[index], self.manuallyAssignedIcons[index])
end
end
function element_proto:PLAYER_REGEN_DISABLED()
self._combat = true
-- start queue processing
self.interval = 0
self:SetScript("OnUpdate", self.OnUpdate)
self:Update("PLAYER_REGEN_DISABLED")
end
function element_proto:NAME_PLATE_UNIT_ADDED(unit)
local guid = UnitGUID(unit)
-- register nameplate
self.nameplates[unit] = guid
self:AddUnit(unit, guid)
self:Update("NAME_PLATE_UNIT_ADDED", unit)
end
function element_proto:NAME_PLATE_UNIT_REMOVED(unit)
local guid = UnitGUID(unit)
-- unregister nameplate
self.nameplates[unit] = nil
self:RemoveUnit(unit, guid)
self:Update("NAME_PLATE_UNIT_REMOVED", unit)
end
function element_proto:RAID_TARGET_UPDATE()
self:UpdateManuallyAssigned()
end
function element_proto:COMBAT_LOG_EVENT_UNFILTERED()
local timestamp, subevent, _, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags = CombatLogGetCurrentEventInfo()
if subevent == "UNIT_DIED" and destGUID then
local unit = self:GetUnitByGUID(destGUID)
if unit and unit:match("nameplate%d") then
self.units[destGUID] = nil
self.guids[unit] = nil
end
local iconIndex = self.assignedIcons[destGUID] or self.manuallyAssignedIcons[destGUID]
if iconIndex then
self.assignedIcons[destGUID] = nil
self.assignedIcons[iconIndex] = nil
self.manuallyAssignedIcons[destGUID] = nil
self.manuallyAssignedIcons[iconIndex] = nil
end
elseif COMBAT_EVENTS[subevent] then
-- check if the source is an enemy
local isSourceEnemy = CombatLog_Object_IsA(sourceFlags, COMBATLOG_FILTER_HOSTILE_UNITS)
-- check if the destination is the player, a party member, or a raid member
local isDestPlayerOrGroup = CombatLog_Object_IsA(destFlags, COMBATLOG_FILTER_ME) or
CombatLog_Object_IsA(destFlags, COMBATLOG_FILTER_MY_PARTY) or
CombatLog_Object_IsA(destFlags, COMBATLOG_FILTER_MY_RAID)
if isSourceEnemy and isDestPlayerOrGroup then
self.enemies[sourceGUID] = true
end
end
end
--[[
trigers when:
- player enters in a group
- add/remove group member
- group member zone into the instance
--]]
function element_proto:GROUP_ROSTER_UPDATE()
self:Enable()
end
function element_proto:PLAYER_ENTERING_WORLD(isLogin, isReload)
self:UpdateManuallyAssigned()
self:Enable()
end
function element_proto:PLAYER_LOGIN()
self.__init = true
-- check if you can mark units
self:RegisterEvent("GROUP_ROSTER_UPDATE")
self:RegisterEvent("PLAYER_ENTERING_WORLD")
-- scan nameplates
self:RegisterEvent("NAME_PLATE_UNIT_ADDED")
self:RegisterEvent("NAME_PLATE_UNIT_REMOVED")
-- this is not needed anymore
self:UnregisterEvent("PLAYER_LOGIN")
end
function element_proto:OnEvent(event, ...)
if self[event] then
self[event](self, ...)
end
end
function element_proto:OnUpdate(elapsed)
self.interval = (self.interval or 0) - (elapsed or 1)
if self.interval <= 0 then
for index = 1, #self.queue do
local data = table.remove(self.queue, 1)
self:AssignRaidIcon("QUEUE", data)
end
table.sort(self.queue, self.SortData)
self.interval = UPDATE_INTERVAL
end
end
local frame = Mixin(CreateFrame("Frame"), element_proto)
-- frame:RegisterEvent("PLAYER_LOGIN")
frame:SetScript("OnEvent", frame.OnEvent)