-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathState.lua
More file actions
602 lines (504 loc) · 18.1 KB
/
State.lua
File metadata and controls
602 lines (504 loc) · 18.1 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
-- State.lua
-- Generic game state tracking for PriorityHelper (3.3.5a compatible)
-- Class modules register buffs, debuffs, cooldowns, talents, glyphs via the registration API.
local DH = PriorityHelper
if not DH then return end
local ns = DH.ns
local class = DH.Class
local state = DH.State
-- State variables
state.now = 0
state.offset = 0
state.gcd = 0
state.gcd_remains = 0
state.latency = 0.05
state.inCombat = false
state.GUID = nil
state.level = 1
-- Resources (using power type numbers for 3.3.5a)
-- 0 = Mana, 1 = Rage, 2 = Focus, 3 = Energy, 4 = Happiness, 5 = Runes, 6 = Runic Power
state.health = { current = 0, max = 0, pct = 0 }
state.mana = { current = 0, max = 0, pct = 0, regen = 0 }
state.energy = { current = 0, max = 100, regen = 10 }
state.rage = { current = 0, max = 100 }
state.combo_points = { current = 0, max = 5 }
state.runic_power = { current = 0, max = 100 }
-- Target info
state.target = {
exists = false,
guid = nil,
health = { current = 0, max = 0, pct = 0 },
time_to_die = 300,
distance = 0,
inRange = false,
canAttack = false,
}
-- Buffs and debuffs
state.buff = {}
state.debuff = {}
-- Cooldowns
state.cooldown = {}
-- Talent tracking
state.talent = {}
-- Glyph tracking
state.glyph = {}
-- Set bonuses
state.set_bonus = {}
-- Equipped items
state.equipped = {}
-- Swing timer
state.swings = {
mainhand = 0,
mainhand_speed = 2.5,
}
-- Form tracking (Druid forms, stances, etc.)
state.form = 0
state.cat_form = false
state.bear_form = false
state.moonkin_form = false
-- Active enemies tracking
state.active_enemies = 1
-- Stat tracking
state.stat = {
attack_power = 0,
spell_power = 0,
crit = 0,
haste = 0,
armor_pen_rating = 0,
spell_haste = 1,
}
-- Settings shortcut
state.settings = {}
-- ============================================================================
-- METATABLES
-- ============================================================================
-- Metatable for buff tracking
local buffMT = {
__index = function(t, k)
if k == "up" then
return t.remains > 0
elseif k == "down" then
return t.remains <= 0
elseif k == "remains" then
return t.expires and math.max(0, t.expires - GetTime()) or 0
elseif k == "stack" or k == "stacks" then
return t.count or 0
elseif k == "duration" then
return t._duration or 0
end
return rawget(t, k)
end
}
local function CreateAuraTable()
return setmetatable({
expires = 0,
count = 0,
_duration = 0,
applied = 0,
last_applied = 0,
}, buffMT)
end
-- Metatable for cooldown tracking
local cooldownMT = {
__index = function(t, k)
if k == "up" or k == "ready" then
return t.remains <= 0.1
elseif k == "down" then
return t.remains > 0.1
elseif k == "remains" then
local start, duration = t.start or 0, t.duration or 0
if start == 0 then return 0 end
local now = state.now or GetTime()
return math.max(0, start + duration - now)
end
return rawget(t, k)
end
}
local function CreateCooldownTable()
return setmetatable({
start = 0,
duration = 0,
}, cooldownMT)
end
-- Metatable for talents
local talentMT = {
__index = function(t, k)
local data = rawget(t, k)
if data then return data end
return { rank = 0 }
end
}
-- Metatable for glyphs
local glyphMT = {
__index = function(t, k)
return { enabled = false }
end
}
-- Export CreateAuraTable for class modules that need custom aura tracking
ns.CreateAuraTable = CreateAuraTable
ns.CreateCooldownTable = CreateCooldownTable
-- ============================================================================
-- INITIALIZATION (uses registered data)
-- ============================================================================
function state:Init()
self.GUID = UnitGUID("player")
self.level = UnitLevel("player")
-- Initialize buff tables from registered data
for _, buff in ipairs(ns.registered.buffs) do
self.buff[buff] = CreateAuraTable()
end
-- Initialize debuff tables from registered data
for _, debuff in ipairs(ns.registered.debuffs) do
self.debuff[debuff] = CreateAuraTable()
end
-- Initialize cooldown tables from registered data
for key, _ in pairs(ns.registered.cooldowns) do
self.cooldown[key] = CreateCooldownTable()
end
-- Set metatables
setmetatable(self.talent, talentMT)
setmetatable(self.glyph, glyphMT)
end
-- ============================================================================
-- STATE RESET (called each update cycle)
-- ============================================================================
function state:Reset()
self.now = GetTime()
self.GUID = UnitGUID("player")
self.level = UnitLevel("player")
-- Update combat state
self.inCombat = UnitAffectingCombat("player")
ns.inCombat = self.inCombat
-- Update GCD
-- GetSpellCooldown returns (start, duration) — if a spell has no real CD,
-- it returns the GCD. If it has a real CD, it returns whichever is longer.
-- We check the registered GCD spell first. If it reports a long CD (it has
-- a real cooldown), we scan registered cooldowns to find one that's only on GCD.
self.gcd = 1.5
self.gcd_remains = 0
self._gcd_start = 0
local gcdSpellId = ns.registered.gcdSpellId
local gcdStart, gcdDuration
if gcdSpellId then
gcdStart, gcdDuration = GetSpellCooldown(gcdSpellId)
end
-- If the reference spell is on a real CD (> 2s), find a spell that's only on GCD
if not gcdStart or not gcdDuration or gcdDuration > 2 then
for _, spellId in pairs(ns.registered.cooldowns) do
local s, d = GetSpellCooldown(spellId)
if s and s > 0 and d and d > 0 and d <= 2 then
gcdStart, gcdDuration = s, d
break
end
end
end
if gcdStart and gcdStart > 0 and gcdDuration and gcdDuration > 0 and gcdDuration <= 2 then
self.gcd = gcdDuration -- Actual GCD (affected by haste)
self.gcd_remains = math.max(0, gcdStart + gcdDuration - self.now)
self._gcd_start = gcdStart
end
-- Update resources
self:UpdateResources()
-- Update target
self:UpdateTarget()
-- Update active enemy count
self:UpdateActiveEnemies()
-- Update form
self:UpdateForm()
-- Update buffs
self:UpdateBuffs()
-- Update debuffs
self:UpdateDebuffs()
-- Update cooldowns
self:UpdateCooldowns()
-- Update stats
self:UpdateStats()
-- Update talents
self:UpdateTalents()
-- Update glyphs
self:UpdateGlyphs()
-- Update settings reference
if DH.db then
self.settings = DH.db
end
end
-- ============================================================================
-- RESOURCE UPDATES
-- ============================================================================
function state:UpdateResources()
-- Health
self.health.current = UnitHealth("player")
self.health.max = UnitHealthMax("player")
self.health.pct = self.health.max > 0 and (self.health.current / self.health.max * 100) or 0
-- Mana (power type 0)
self.mana.current = UnitPower("player", 0)
self.mana.max = UnitPowerMax("player", 0)
self.mana.pct = self.mana.max > 0 and (self.mana.current / self.mana.max * 100) or 0
-- Energy (power type 3)
self.energy.current = UnitPower("player", 3)
self.energy.max = UnitPowerMax("player", 3)
if self.energy.max == 0 then self.energy.max = 100 end
-- Rage (power type 1)
self.rage.current = UnitPower("player", 1)
self.rage.max = 100
-- Combo Points
self.combo_points.current = GetComboPoints("player", "target")
-- Runic Power (power type 6)
self.runic_power.current = UnitPower("player", 6)
self.runic_power.max = UnitPowerMax("player", 6)
if self.runic_power.max == 0 then self.runic_power.max = 100 end
end
function state:UpdateActiveEnemies()
if UnitExists("target") and UnitCanAttack("player", "target") then
self.active_enemies = 1
else
self.active_enemies = 0
end
end
function state:UpdateTarget()
self.target.exists = UnitExists("target")
self.target.guid = UnitGUID("target")
if self.target.exists then
self.target.health.current = UnitHealth("target")
self.target.health.max = UnitHealthMax("target")
self.target.health.pct = self.target.health.max > 0 and (self.target.health.current / self.target.health.max * 100) or 0
-- Estimate time to die
if self.target.health.pct < 20 then
self.target.time_to_die = 10
elseif self.target.health.pct < 35 then
self.target.time_to_die = 30
else
self.target.time_to_die = 300
end
-- Check if target is a training dummy
local name = UnitName("target")
if name and name:find("Dummy") then
self.target.time_to_die = DH.db and DH.db.common and DH.db.common.dummy_ttd or 300
if self.debuff.training_dummy then
self.debuff.training_dummy.expires = self.now + 3600
end
else
if self.debuff.training_dummy then
self.debuff.training_dummy.expires = 0
end
end
-- Range check using CheckInteractDistance (3.3.5a compatible)
if CheckInteractDistance("target", 3) then
self.target.distance = 5
self.target.inRange = true
elseif CheckInteractDistance("target", 4) then
self.target.distance = 20
self.target.inRange = false
else
self.target.distance = 40
self.target.inRange = false
end
self.target.canAttack = UnitCanAttack("player", "target")
else
self.target.health.current = 0
self.target.health.max = 0
self.target.health.pct = 0
self.target.time_to_die = 0
self.target.distance = 40
self.target.inRange = false
self.target.canAttack = false
end
end
-- ============================================================================
-- FORM / STANCE
-- ============================================================================
function state:UpdateForm()
self.form = GetShapeshiftForm()
-- Reset form booleans (class modules set these via form handlers)
self.cat_form = false
self.bear_form = false
self.moonkin_form = false
-- Dispatch to registered form handlers
for formId, handler in pairs(ns.registered.formHandlers) do
if self.form == formId then
handler(self)
end
end
end
-- ============================================================================
-- BUFF / DEBUFF UPDATES (using registered maps)
-- ============================================================================
function state:UpdateBuffs()
-- Track which buff keys are form-related (set by form handlers)
local formBuffKeys = {}
for formId, _ in pairs(ns.registered.formHandlers) do
-- Form handlers manage their own buff state, skip those
end
-- Reset non-form buff expires
for key, buff in pairs(self.buff) do
if type(buff) == "table" and buff.expires then
-- Don't reset form buffs (handled by UpdateForm via form handlers)
if not buff._isForm then
buff.expires = 0
buff.count = 0
end
end
end
-- Scan player buffs
for i = 1, 40 do
local name, _, icon, count, debuffType, duration, expirationTime, source, _, _, spellId = UnitBuff("player", i)
if not name then break end
local key = ns.registered.buffMap[spellId]
if key and self.buff[key] then
-- Permanent buffs (Righteous Fury, auras, etc.) have expirationTime = 0
if not expirationTime or expirationTime == 0 then
self.buff[key].expires = self.now + 7200 -- Treat as 2 hours
else
self.buff[key].expires = expirationTime
end
self.buff[key].count = count or 1
self.buff[key]._duration = duration or 0
self.buff[key].applied = (expirationTime and expirationTime > 0) and (expirationTime - (duration or 0)) or self.now
end
end
end
function state:UpdateDebuffs()
if not self.target.exists then return end
-- Reset debuff expires
for key, debuff in pairs(self.debuff) do
if type(debuff) == "table" and debuff.expires and key ~= "training_dummy" then
debuff.expires = 0
debuff.count = 0
end
end
-- Scan target debuffs
for i = 1, 40 do
local name, _, icon, count, debuffType, duration, expirationTime, source, _, _, spellId = UnitDebuff("target", i)
if not name then break end
if source == "player" then
-- Try spell ID map first
local key = ns.registered.debuffMap[spellId]
-- Fallback to name patterns
if not key and name then
local lowerName = name:lower()
for _, pattern in ipairs(ns.registered.debuffNamePatterns) do
if lowerName:find(pattern[1]) then
key = pattern[2]
break
end
end
end
if key and self.debuff[key] then
self.debuff[key].expires = expirationTime or (self.now + 3600)
self.debuff[key].count = count or 1
self.debuff[key]._duration = duration or 0
end
end
-- Track external debuffs (from other players)
if source ~= "player" then
local key = ns.registered.externalDebuffMap[spellId]
if not key and name then
local lowerName = name:lower()
for _, pattern in ipairs(ns.registered.externalDebuffNamePatterns) do
if lowerName:find(pattern[1]) then
key = pattern[2]
break
end
end
end
if key and self.debuff[key] then
self.debuff[key].expires = expirationTime or (self.now + 3600)
end
end
end
end
-- ============================================================================
-- COOLDOWN UPDATES (using registered data)
-- ============================================================================
function state:UpdateCooldowns()
local gcdStart = self._gcd_start or 0
local gcdDuration = self.gcd or 1.5
for key, spellId in pairs(ns.registered.cooldowns) do
local start, duration, enabled = GetSpellCooldown(spellId)
if self.cooldown[key] then
-- Filter out GCD: if this spell shares the same start time and duration
-- as the GCD, it's just on GCD with no real cooldown.
-- Also filter if duration matches GCD duration (haste-adjusted).
local isJustGCD = false
if start and duration then
if start == gcdStart and math.abs(duration - gcdDuration) < 0.01 then
isJustGCD = true
elseif duration <= gcdDuration + 0.01 then
isJustGCD = true
end
end
if not isJustGCD and duration and duration > 0 then
self.cooldown[key].start = start or 0
self.cooldown[key].duration = duration or 0
else
self.cooldown[key].start = 0
self.cooldown[key].duration = 0
end
end
end
end
-- ============================================================================
-- STAT UPDATES
-- ============================================================================
function state:UpdateStats()
local base, posBuff, negBuff = UnitAttackPower("player")
self.stat.attack_power = base + posBuff + negBuff
self.stat.spell_power = GetSpellBonusDamage(4) -- Nature damage
self.stat.crit = GetCritChance()
self.stat.haste = GetCombatRatingBonus(18) -- CR_HASTE_MELEE
self.stat.armor_pen_rating = GetCombatRating(25) -- CR_ARMOR_PENETRATION
local spellHaste = GetCombatRatingBonus(20) -- CR_HASTE_SPELL
self.stat.spell_haste = 1 + (spellHaste / 100)
end
-- ============================================================================
-- TALENT / GLYPH UPDATES (using registered data)
-- ============================================================================
function state:UpdateTalents()
for _, data in ipairs(ns.registered.talents) do
local tab, index, key = data[1], data[2], data[3]
local _, _, _, _, rank = GetTalentInfo(tab, index)
self.talent[key] = { rank = rank or 0 }
end
end
function state:UpdateGlyphs()
-- Reset all registered glyph keys
local glyphKeys = {}
for _, key in pairs(ns.registered.glyphs) do
glyphKeys[key] = true
end
for key in pairs(glyphKeys) do
self.glyph[key] = { enabled = false }
end
-- Scan equipped glyphs (3.3.5a has 6 glyph slots)
for i = 1, 6 do
local enabled, glyphType, glyphTooltipIndex, glyphSpell, icon = GetGlyphSocketInfo(i)
if enabled and glyphSpell then
local key = ns.registered.glyphs[glyphSpell]
if key then
self.glyph[key] = { enabled = true }
end
end
end
end
-- ============================================================================
-- CONVENIENCE ACCESSORS
-- ============================================================================
setmetatable(state, {
__index = function(t, k)
if k == "ttd" then
return t.target.time_to_die
elseif k == "time" then
return t.now
elseif k == "query_time" then
return t.now + t.offset
elseif k == "haste" then
return 1 / (1 + t.stat.haste / 100)
elseif k == "mainhand_speed" then
local speed = UnitAttackSpeed("player")
return speed or 2.5
end
return rawget(t, k)
end
})
-- Initialize on load
state:Init()