-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoMark.lua
More file actions
629 lines (548 loc) · 22.4 KB
/
Copy pathAutoMark.lua
File metadata and controls
629 lines (548 loc) · 22.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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
---------------------------------------------------------------------------
-- PugzRaidTools - Auto Marking Engine
-- Automatically applies raid marks to players based on NPC death events
-- or group swap triggers. Each preset contains mark groups with their
-- own trigger logic and mark assignments.
--
-- Mark application uses SetRaidTarget("raidN", icon) which works in
-- combat and only requires leader/assistant.
---------------------------------------------------------------------------
local _, PRT = ...
PRT._autoMarkKills = {} -- [npcId] = cumulative kill count (independent from AutoSwap)
PRT._autoMarkFired = {} -- [key] = true (tracks which mark groups have fired)
PRT._autoMarkRetryJobs = {}
local AUTO_MARK_RETRY_INTERVAL = 0.25
local AUTO_MARK_VERIFY_DELAY = 0.45
local AUTO_MARK_POST_SWAP_DELAY = 0.30
local AUTO_MARK_DEFAULT_RETRY_DURATION = 3
local AUTO_MARK_MAX_RETRY_DURATION = 10
---------------------------------------------------------------------------
-- Initialization
---------------------------------------------------------------------------
function PRT:InitAutoMark()
local db = self:GetDB()
if db.autoMark and db.autoMark.presets then
for _, preset in ipairs(db.autoMark.presets) do
self:EnsureAutoMarkPresetDefaults(preset)
end
end
self._autoMarkFrame = CreateFrame("Frame")
self._autoMarkFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
self._autoMarkFrame:RegisterEvent("GROUP_ROSTER_UPDATE")
self._autoMarkFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
self._autoMarkFrame:SetScript("OnEvent", function()
PRT:UpdateAutoMarkListeners()
end)
self:UpdateAutoMarkListeners()
end
---------------------------------------------------------------------------
-- Listener management — register/unregister from shared CLEU dispatcher
---------------------------------------------------------------------------
function PRT:UpdateAutoMarkListeners()
local db = self:GetDB()
local am = db.autoMark
if not am then return end
local shouldListen = false
if am.enabled and am.activePreset ~= "" then
local preset = self:GetAutoMarkPreset(am.activePreset)
if preset and self:IsAutoMarkPresetLocationAllowed(preset) then
for _, mg in ipairs(preset.markGroups) do
if #mg.npcTriggers > 0 then
shouldListen = true
break
end
end
end
end
if shouldListen then
PRT:RegisterCLEUListener("automark", function() PRT:OnAutoMarkCombatLog() end)
else
PRT:UnregisterCLEUListener("automark")
end
end
---------------------------------------------------------------------------
-- Preset helpers
---------------------------------------------------------------------------
function PRT:GetAutoMarkPreset(name)
if not name or name == "" then return nil end
local db = self:GetDB()
for _, p in ipairs(db.autoMark.presets) do
if p.name == name then return p end
end
end
function PRT:EnsureAutoMarkPresetDefaults(preset)
if not preset then return end
if preset.instanceId == nil then preset.instanceId = 0 end
if preset.allowAnywhere == nil then preset.allowAnywhere = false end
preset.markGroups = preset.markGroups or {}
for _, mg in ipairs(preset.markGroups) do
self:EnsureAutoMarkRuleDefaults(mg)
end
end
function PRT:EnsureAutoMarkRuleDefaults(mg)
if not mg then return end
if mg.retryUnavailable == nil then mg.retryUnavailable = false end
mg.retryDuration = tonumber(mg.retryDuration) or AUTO_MARK_DEFAULT_RETRY_DURATION
mg.retryDuration = math.max(1, math.min(AUTO_MARK_MAX_RETRY_DURATION, mg.retryDuration))
mg.marks = mg.marks or {}
mg.npcTriggers = mg.npcTriggers or {}
mg.swapTriggers = mg.swapTriggers or {}
mg.conditionals = mg.conditionals or {}
end
function PRT:IsAutoMarkPresetLocationAllowed(preset)
if not preset then return false end
if preset.allowAnywhere then return true end
if not IsInRaid() then return false end
local filterId = preset.instanceId or 0
if filterId == 0 then return true end
local _, _, _, _, _, _, _, instanceMapID = GetInstanceInfo()
return instanceMapID == filterId
end
---------------------------------------------------------------------------
-- CLEU callback — fast-path exit for non-death events
---------------------------------------------------------------------------
function PRT:OnAutoMarkCombatLog()
local _, subEvent, _, _, _, _, _, destGUID = CombatLogGetCurrentEventInfo()
if subEvent ~= "UNIT_DIED" then return end
local npcId = PRT.GetNpcId(destGUID)
if not npcId then return end
local db = self:GetDB()
local am = db.autoMark
local preset = self:GetAutoMarkPreset(am.activePreset)
if not preset then return end
if not self:IsAutoMarkPresetLocationAllowed(preset) then return end
self._autoMarkKills[npcId] = (self._autoMarkKills[npcId] or 0) + 1
for _, mg in ipairs(preset.markGroups) do
self:EvaluateMarkGroupNPCTriggers(mg, npcId, am.activePreset)
end
end
---------------------------------------------------------------------------
-- NPC trigger evaluation
---------------------------------------------------------------------------
function PRT:EvaluateMarkGroupNPCTriggers(mg, killedNpcId, presetName)
local key = presetName .. ":" .. mg.name
-- Already fired and not repeatable? Skip
if self._autoMarkFired[key] and not mg.repeatable then return end
-- Does this mark group care about the NPC that just died?
local relevant = false
for _, trigger in ipairs(mg.npcTriggers) do
if trigger.npcId == killedNpcId then
relevant = true
break
end
end
if not relevant then return end
-- Evaluate trigger requirements (Any / All / Conditional)
if not self:CheckTriggerRequirements(mg) then return end
self._autoMarkFired[key] = true
PRT.Print(("Auto Mark: %s / %s triggered"):format(presetName, mg.name))
-- Reset kill counters for triggers that reached their threshold.
-- Subtract the threshold rather than zeroing so excess kills carry forward.
-- Example: count=2, kills=3 when fired → counter becomes 1, meaning one
-- kill is already "banked" toward the next repeat cycle.
-- In "all" mode, all triggers will have met threshold at this point.
-- In "any" mode, only the trigger(s) that reached threshold are subtracted.
for _, trigger in ipairs(mg.npcTriggers) do
local kills = self._autoMarkKills[trigger.npcId] or 0
local threshold = trigger.count or 1
if kills >= threshold then
self._autoMarkKills[trigger.npcId] = kills - threshold
end
end
-- 1-second delay before applying marks (matches WeakAura behaviour)
C_Timer.After(1.0, function()
local preset = PRT:GetAutoMarkPreset(presetName)
if preset and PRT:IsAutoMarkPresetLocationAllowed(preset) then
PRT:ApplyMarkGroup(mg, {
presetName = presetName,
queueKey = key,
})
end
end)
end
---------------------------------------------------------------------------
-- Group swap trigger - called by Reorder.lua before and after a comp is applied
---------------------------------------------------------------------------
function PRT:BeginGroupSwapAutoMark(compName)
local db = self:GetDB()
local am = db.autoMark
if not am or not am.enabled or am.activePreset == "" then return end
local preset = self:GetAutoMarkPreset(am.activePreset)
if not preset then return end
if not self:IsAutoMarkPresetLocationAllowed(preset) then return end
local applications = {}
for _, mg in ipairs(preset.markGroups) do
for _, st in ipairs(mg.swapTriggers) do
if st.compName == compName then
local key = am.activePreset .. ":" .. mg.name .. ":swap:" .. compName
if not self._autoMarkFired[key] or mg.repeatable then
self._autoMarkFired[key] = true
PRT.Print(("Auto Mark: %s / %s (swap: %s)"):format(
am.activePreset, mg.name, compName))
self:EnsureAutoMarkRuleDefaults(mg)
local application = self:CreateAutoMarkApplication(mg, {
presetName = am.activePreset,
queueKey = key,
})
self:ReportSkippedAutoMarkAssignments(application)
applications[#applications + 1] = application
-- Raw raid-index rules only have meaning after the reorder.
if application.stableTargets then
self:ApplyAutoMarkApplication(application, {
clear = true,
queue = false,
})
end
end
break
end
end
end
return applications
end
function PRT:FinishGroupSwapAutoMark(applications)
if not applications or #applications == 0 then return end
C_Timer.After(AUTO_MARK_POST_SWAP_DELAY, function()
for _, application in ipairs(applications) do
local preset = PRT:GetAutoMarkPreset(application.presetName)
if preset and PRT:IsAutoMarkPresetLocationAllowed(preset) then
-- A successful pre-swap observation must be checked again after
-- the roster changes, but the clear step remains one-shot.
for _, assignment in ipairs(application.assignments) do
assignment.complete = nil
end
PRT:ApplyAutoMarkApplication(application, {
clear = not application.cleared,
queue = application.mg.retryUnavailable,
rebuild = not application.stableTargets,
})
end
end
end)
end
-- Compatibility path for callers that do not support the explicit pre/post API.
function PRT:OnGroupSwapForAutoMark(compName)
self:FinishGroupSwapAutoMark(self:BeginGroupSwapAutoMark(compName))
end
---------------------------------------------------------------------------
-- Trigger requirement logic (Any / All / Conditional)
---------------------------------------------------------------------------
function PRT:CheckTriggerRequirements(mg)
local mode = mg.triggerMode or "any"
local triggers = mg.npcTriggers
if mode == "any" then
for _, trigger in ipairs(triggers) do
local count = self._autoMarkKills[trigger.npcId] or 0
if count >= (trigger.count or 1) then
return true
end
end
return false
elseif mode == "all" then
if #triggers == 0 then return false end
for _, trigger in ipairs(triggers) do
local count = self._autoMarkKills[trigger.npcId] or 0
if count < (trigger.count or 1) then
return false
end
end
return true
elseif mode == "conditional" then
for _, cond in ipairs(mg.conditionals or {}) do
local met = false
for _, trigger in ipairs(triggers) do
if trigger.name == cond.triggerName then
local count = self._autoMarkKills[trigger.npcId] or 0
met = count >= (trigger.count or 1)
break
end
end
-- Legacy migration: old data used mustBeTrue=false as "must be false"
local mustTrue = cond.mustBeTrue or false
local mustFalse = cond.mustBeFalse
if mustFalse == nil then mustFalse = not mustTrue end
if mustTrue and not met then return false end
if mustFalse and met then return false end
end
return true
end
return false
end
---------------------------------------------------------------------------
-- Apply marks
---------------------------------------------------------------------------
local function AutoMarkNow()
return GetTime and GetTime() or 0
end
local function IsValidMarkIcon(icon)
return icon and icon >= 0 and icon <= 8
end
function PRT:IsAutoMarkUnitAddressable(unit)
if not unit then return false end
if UnitExists and not UnitExists(unit) then return false end
if UnitIsVisible and not UnitIsVisible(unit) then return false end
return true
end
function PRT:BuildAutoMarkAssignments(mg)
local assignments = {}
local skippedAssignments = {}
local assignmentIndexByIcon = {}
local applyOn = mg.applyOn or "name"
local smartComp
if applyOn == "position" and mg.smartAssign and mg.smartComp and mg.smartComp ~= "" then
smartComp = self:GetComp(mg.smartComp)
end
local stableTargets = applyOn == "name" or smartComp ~= nil
for _, mark in ipairs(mg.marks or {}) do
local icon = tonumber(mark.icon)
if IsValidMarkIcon(icon) then
local assignment = {
icon = icon,
sourceName = mark.playerName or "",
position = tonumber(mark.position) or 0,
}
if applyOn == "name" then
if assignment.sourceName ~= "" then
assignment.identityKey = self:GetPlayerIdentityKey(assignment.sourceName)
end
elseif smartComp then
local targetName = smartComp.roster[assignment.position]
if targetName and targetName ~= "" then
assignment.sourceName = targetName
assignment.identityKey = self:GetRosterSlotIdentityKey(smartComp.roster, assignment.position)
if not assignment.identityKey then
skippedAssignments[#skippedAssignments + 1] = {
sourceName = targetName,
position = assignment.position,
icon = icon,
}
end
end
elseif assignment.position >= 1 and assignment.position <= 40 then
assignment.raidPosition = assignment.position
end
if assignment.identityKey or assignment.raidPosition then
local existingIndex = icon ~= 0 and assignmentIndexByIcon[icon]
if existingIndex then
-- A raid icon can only belong to one unit. Preserve the
-- old sequential behavior where the last row wins.
assignments[existingIndex] = assignment
else
assignments[#assignments + 1] = assignment
if icon ~= 0 then
assignmentIndexByIcon[icon] = #assignments
end
end
end
end
end
return assignments, stableTargets, skippedAssignments
end
function PRT:ReportSkippedAutoMarkAssignments(application)
local skipped = application and application.skippedAssignments or {}
if #skipped == 0 or application.skippedAssignmentsReported then return end
application.skippedAssignmentsReported = true
local parts = {}
for _, assignment in ipairs(skipped) do
parts[#parts + 1] = ("%s (slot %d)"):format(
tostring(assignment.sourceName), assignment.position)
end
PRT.Print(("Auto Mark: skipped %d unresolved Smart Assign %s: %s.")
:format(#skipped,
#skipped == 1 and "row" or "rows",
table.concat(parts, ", ")))
end
function PRT:CreateAutoMarkApplication(mg, opts)
opts = opts or {}
self:EnsureAutoMarkRuleDefaults(mg)
local assignments, stableTargets, skippedAssignments =
self:BuildAutoMarkAssignments(mg)
return {
mg = mg,
presetName = opts.presetName,
queueKey = opts.queueKey or tostring(mg),
assignments = assignments,
stableTargets = stableTargets,
skippedAssignments = skippedAssignments,
cleared = false,
}
end
function PRT:ResolveAutoMarkAssignmentUnit(assignment)
if assignment.identityKey then
return self:FindRaidUnitByIdentityKey(assignment.identityKey)
end
if assignment.raidPosition
and assignment.raidPosition <= GetNumGroupMembers() then
return "raid" .. assignment.raidPosition, assignment.raidPosition
end
end
function PRT:ClearAddressableRaidMarks()
for i = 1, GetNumGroupMembers() do
local unit = "raid" .. i
if self:IsAutoMarkUnitAddressable(unit) and GetRaidTargetIndex(unit) then
SetRaidTarget(unit, 0)
end
end
end
function PRT:TryAutoMarkAssignment(assignment)
local unit = self:ResolveAutoMarkAssignmentUnit(assignment)
if not self:IsAutoMarkUnitAddressable(unit) then
return false
end
local observed = GetRaidTargetIndex(unit) or 0
if observed == assignment.icon then
assignment.complete = true
assignment.awaitingVerification = nil
return true
end
local now = AutoMarkNow()
if assignment.awaitingVerification
and (now - (assignment.lastAttempt or 0)) < AUTO_MARK_VERIFY_DELAY then
return false
end
SetRaidTarget(unit, assignment.icon)
assignment.awaitingVerification = true
assignment.lastAttempt = now
return false
end
function PRT:StartAutoMarkRetryQueue(application)
local pending = false
for _, assignment in ipairs(application.assignments) do
if not assignment.complete then
pending = true
break
end
end
if not pending then return end
local duration = tonumber(application.mg.retryDuration) or AUTO_MARK_DEFAULT_RETRY_DURATION
duration = math.max(1, math.min(AUTO_MARK_MAX_RETRY_DURATION, duration))
self._autoMarkRetrySerial = (self._autoMarkRetrySerial or 0) + 1
local serial = self._autoMarkRetrySerial
local queueKey = application.queueKey
local deadline = AutoMarkNow() + duration
self._autoMarkRetryJobs[queueKey] = serial
local function Finish()
if PRT._autoMarkRetryJobs[queueKey] == serial then
PRT._autoMarkRetryJobs[queueKey] = nil
end
end
local function Retry()
if PRT._autoMarkRetryJobs[queueKey] ~= serial then return end
local db = PRT:GetDB()
local preset = application.presetName and PRT:GetAutoMarkPreset(application.presetName)
if not db.autoMark.enabled
or (application.presetName and db.autoMark.activePreset ~= application.presetName)
or (application.presetName and not preset)
or (preset and not PRT:IsAutoMarkPresetLocationAllowed(preset)) then
Finish()
return
end
local pending = 0
for _, assignment in ipairs(application.assignments) do
if not assignment.complete then
PRT:TryAutoMarkAssignment(assignment)
if not assignment.complete then
pending = pending + 1
end
end
end
if pending == 0 or AutoMarkNow() >= deadline then
Finish()
return
end
C_Timer.After(AUTO_MARK_RETRY_INTERVAL, Retry)
end
C_Timer.After(AUTO_MARK_RETRY_INTERVAL, Retry)
end
function PRT:ApplyAutoMarkApplication(application, opts)
opts = opts or {}
local mg = application.mg
if not IsInRaid() then return end
if not (UnitIsGroupLeader("player") or UnitIsGroupAssistant("player")) then
PRT.Print("Auto Mark: must be leader or assistant to set marks.")
return
end
if opts.rebuild then
application.assignments,
application.stableTargets,
application.skippedAssignments =
self:BuildAutoMarkAssignments(mg)
application.skippedAssignmentsReported = nil
self:ReportSkippedAutoMarkAssignments(application)
end
if opts.clear and mg.unmarkAll and not application.cleared then
self:ClearAddressableRaidMarks()
application.cleared = true
end
for _, assignment in ipairs(application.assignments) do
if not assignment.complete then
self:TryAutoMarkAssignment(assignment)
end
end
if opts.queue and mg.retryUnavailable then
self:StartAutoMarkRetryQueue(application)
end
end
function PRT:ApplyMarkGroup(mg, opts)
opts = opts or {}
local application = self:CreateAutoMarkApplication(mg, opts)
self:ReportSkippedAutoMarkAssignments(application)
self:ApplyAutoMarkApplication(application, {
clear = true,
queue = mg.retryUnavailable,
})
return application
end
--- Player Name mode: find the named player in the raid and mark them.
function PRT:ApplyMarkByName(mark)
if not mark.playerName or mark.playerName == "" then return end
local assignment = {
icon = mark.icon,
sourceName = mark.playerName,
identityKey = self:GetPlayerIdentityKey(mark.playerName),
}
return self:TryAutoMarkAssignment(assignment)
end
--- Raid Position mode (no Smart Assign): position maps directly to raid index.
function PRT:ApplyMarkByPosition(mark)
local pos = mark.position or 0
if pos < 1 or pos > 40 then return end
local assignment = {
icon = mark.icon,
sourceName = "",
position = pos,
raidPosition = pos,
}
return self:TryAutoMarkAssignment(assignment)
end
--- Raid Position + Smart Assign: look up the player name from the
--- saved composition at the given position, then find that player
--- in the raid by name (stable across re-sorts).
function PRT:ApplyMarkBySmartPosition(mark, compName)
local comp = self:GetComp(compName)
if not comp then
-- Fallback to direct position
self:ApplyMarkByPosition(mark)
return
end
local pos = mark.position or 0
if pos < 1 or pos > 40 then return end
local targetName = comp.roster[pos]
if not targetName or targetName == "" then return end
local assignment = {
icon = mark.icon,
sourceName = targetName,
position = pos,
identityKey = self:GetRosterSlotIdentityKey(comp.roster, pos),
}
return self:TryAutoMarkAssignment(assignment)
end
---------------------------------------------------------------------------
-- Kill counter / fired-state reset
---------------------------------------------------------------------------
function PRT:ResetAutoMarkCounters()
wipe(self._autoMarkKills)
wipe(self._autoMarkFired)
wipe(self._autoMarkRetryJobs)
PRT.Print("Auto Mark counters reset.")
end