-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstats.lua
492 lines (437 loc) · 16.6 KB
/
stats.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
---@class AddonEnv
local _addon = select(2, ...);
---@return UniformStat
local function UniformStat()
---@class UniformStat
---@field val integer
---@field buffs string[]
return {
---@type number
val = 0,
---@type string[]
buffs = {}
}
end
---@return UniformStatMult
local function UniformStatMult()
---@class UniformStatMult
---@field currentMult number
---@field vals table<string, integer>
---@field buffs string[]
return {
currentMult = 1,
vals = {},
buffs = {}
}
end
local function SchoolStatTable()
local SCHOOL = _addon.CONST.SCHOOL;
-- Keys are school types found in _addon.CONST.SCHOOL
---@type table<SpellSchool, UniformStat>
local schoolTable = {
[SCHOOL.PHYSICAL] = UniformStat(),
[SCHOOL.HOLY] = UniformStat(),
[SCHOOL.FIRE] = UniformStat(),
[SCHOOL.NATURE] = UniformStat(),
[SCHOOL.FROST] = UniformStat(),
[SCHOOL.SHADOW] = UniformStat(),
[SCHOOL.ARCANE] = UniformStat()
}
return schoolTable;
end
local function SchoolStatTableMult()
local SCHOOL = _addon.CONST.SCHOOL;
-- Keys are school types found in _addon.CONST.SCHOOL
---@type table<SpellSchool, UniformStatMult>
local schoolTable = {
[SCHOOL.PHYSICAL] = UniformStatMult(),
[SCHOOL.HOLY] = UniformStatMult(),
[SCHOOL.FIRE] = UniformStatMult(),
[SCHOOL.NATURE] = UniformStatMult(),
[SCHOOL.FROST] = UniformStatMult(),
[SCHOOL.SHADOW] = UniformStatMult(),
[SCHOOL.ARCANE] = UniformStatMult()
}
return schoolTable;
end
local function CreatureTypeStatTable()
local CREATURE_TYPE = _addon.CONST.CREATURE_TYPE;
-- Keys are creature types found in _addon.CONST.CREATURE_TYPE
---@type table<CreatureType, UniformStat>
local creatureTable = {
[CREATURE_TYPE.BEAST] = UniformStat(),
[CREATURE_TYPE.DRAGONKIN] = UniformStat(),
[CREATURE_TYPE.DEMON] = UniformStat(),
[CREATURE_TYPE.ELEMENTAL] = UniformStat(),
[CREATURE_TYPE.GIANT] = UniformStat(),
[CREATURE_TYPE.UNDEAD] = UniformStat(),
[CREATURE_TYPE.HUMANOID] = UniformStat(),
[CREATURE_TYPE.CRITTER] = UniformStat(),
[CREATURE_TYPE.MECHANICAL] = UniformStat(),
[CREATURE_TYPE.NOT_SPECIFIED] = UniformStat(),
[CREATURE_TYPE.TOTEM] = UniformStat()
}
return creatureTable;
end
local function SpellStatTable()
---@type table<integer, UniformStat>
local spellTable = {};
return spellTable;
end
local function SpellStatTableMult()
---@type table<integer, UniformStatMult>
local spellTable = setmetatable({}, {
__index = function(self, key)
rawset(self, key, UniformStatMult());
return self[key];
end
});
return spellTable;
end
local function MechanicStatTable()
-- Keys are mechanic types found in _addon.CONST.SPELL_MECHANIC
---@type table<SpellMechanic, UniformStat>
local mt = {}
for _, mechanicId in pairs(_addon.CONST.SPELL_MECHANIC) do
mt[mechanicId] = UniformStat();
end
return mt;
end
---@class InternalStats
local stats = {
manaMax = 0, -- Maximum mana
manaCurrent = 0, -- Current mana if update is active
baseMana = 0,
manaRegBase = 0, -- Mana regen based on spirit /sec
manaRegCasting = 0, -- Mana regen from spirit while casting /sec
manaRegAura = 0, -- Mana regen from SPELL_AURA_MOD_POWER_REGEN sources /sec
spellPower = {
[_addon.CONST.SCHOOL.PHYSICAL] = 0,
[_addon.CONST.SCHOOL.HOLY] = 0,
[_addon.CONST.SCHOOL.FIRE] = 0,
[_addon.CONST.SCHOOL.NATURE] = 0,
[_addon.CONST.SCHOOL.FROST] = 0,
[_addon.CONST.SCHOOL.SHADOW] = 0,
[_addon.CONST.SCHOOL.ARCANE] = 0
},
spellHealing = 0,
spellCrit = {
[_addon.CONST.SCHOOL.PHYSICAL] = 0,
[_addon.CONST.SCHOOL.HOLY] = 0,
[_addon.CONST.SCHOOL.FIRE] = 0,
[_addon.CONST.SCHOOL.NATURE] = 0,
[_addon.CONST.SCHOOL.FROST] = 0,
[_addon.CONST.SCHOOL.SHADOW] = 0,
[_addon.CONST.SCHOOL.ARCANE] = 0
},
attackPower = 0,
attackPowerRanged = 0,
attackSpeed = {
mainhand = 0,
offhand = 0,
ranged = 0
},
baseAttackSpeed = {
mainhand = 0,
offhand = 0,
ranged = 0
},
attack = {
mainhand = 0,
offhand = 0,
ranged = 0
},
attackCrit = {
mainhand = 0,
ranged = 0
},
attackDmg = {
mainhand = {
min = 0, -- Base damage!
max = 0 -- Base damage!
},
offhand = {
min = 0, -- Base damage!
max = 0 -- Base damage!
},
ranged = {
min = 0, -- Base damage!
max = 0 -- Base damage!
}
},
weaponBaseDamage = {
mainhand = {
min = 0,
max = 0
},
offhand = {
min = 0,
max = 0
},
ranged = {
min = 0,
max = 0
}
},
schoolModSpellPen = SchoolStatTable(),
schoolModPctDamage = SchoolStatTable(),
--schoolModFlatCritChance = SchoolStatTable(),
schoolModPctCritMult = SchoolStatTable(),
schoolCritBaseMult = SchoolStatTable(),
targetSchoolModDamageTaken = SchoolStatTable(),
targetSchoolModResistancePct = SchoolStatTable(),
targetSchoolModCritTaken = SchoolStatTable(),
targetSchoolModHit = SchoolStatTable(),
targetHealingRecieved = UniformStat(),
targetSchoolModSpellPower = SchoolStatTable(),
targetModHealPower = UniformStat(),
schoolModPctDamageMult = SchoolStatTableMult(),
spellModPctEffect = SpellStatTable(),
spellModPctDamageHealing = SpellStatTable(),
spellModPctDoTHoT = SpellStatTable(),
spellModFlatDuration = SpellStatTable(), -- In milliseconds
spellModFlatTickperiod = SpellStatTable(), -- In ms
spellModFlatValue = SpellStatTable(),
spellModFlatSpellpower = SpellStatTable(),
spellModEff1FlatSpellpower = SpellStatTable(),
spellModMageNWR = SpellStatTable(),
spellModGCDms = SpellStatTable(), -- In ms
spellModChainMult = SpellStatTable(),
spellModAddTriggerSpell = SpellStatTable(),
spellModFlatHitChance = SpellStatTable(),
spellModFlatCritChance = SpellStatTable(),
spellModPctCritMult = SpellStatTable(),
spellModFlatSpellScale = SpellStatTable(),
spellModPctSpellScale = SpellStatTable(),
spellModClearCastChance = SpellStatTable(),
spellModCharges = SpellStatTable(),
spellModCritManaRestore = SpellStatTable(),
spellModManaRestore = SpellStatTable(),
spellModAllowDotCrit = SpellStatTable(),
spellModAllowDotHaste = SpellStatTable(),
spellModExtraOnCrit = SpellStatTable(),
spellModExtraOnHit = SpellStatTable(),
targetSpellModDmgTakenPersonal = SpellStatTableMult(),
versusModPctDamage = CreatureTypeStatTable(),
versusModPctCritDamage = CreatureTypeStatTable(),
versusModFlatSpellpower = CreatureTypeStatTable(),
versusModFlatAttackpower = CreatureTypeStatTable(),
versusModFlatAttackpowerRanged = CreatureTypeStatTable(),
versusModFlatDamage = CreatureTypeStatTable(),
targetMechanicModDmgTakenPct = MechanicStatTable(),
mp5 = UniformStat();
fsrRegenMult = UniformStat();
modhealingDone = UniformStat(),
hitBonus = UniformStat();
hitBonusSpell = UniformStat();
clearCastChanceDmg = UniformStat(),
clearCastChance = UniformStat(),
illumination = UniformStat(),
earthfuryReturn = UniformStat(),
shamanLightningOverload = SpellStatTable(),
castManaRestoreAvg = UniformStat(),
modCriticalHealing = UniformStat(),
};
---Update base mana for current level.
---@param level integer
local function UpdateBaseMana(level)
local _, eclass = UnitClass("player");
local basemana = _addon.GetBaseMana(eclass, level);
if basemana ~= stats.baseMana then
stats.baseMana = basemana;
_addon:TriggerUpdate();
end
end
--- Update spell power stats from API
local function UpdateSpellPower()
_addon.util.PrintDebug("Updating spell power");
for i = 1, 7, 1 do
_addon.stats.spellPower[i] = GetSpellBonusDamage(i);
end
_addon:TriggerUpdate();
end
---Update attack power.
local function UpdateAttackPower()
local base, posBuff, negBuff = UnitAttackPower("player");
stats.attackPower = base + posBuff + negBuff;
_addon:TriggerUpdate();
end
---Update ranged attack power.
local function UpdateRangedAttackPower()
local base, posBuff, negBuff = UnitRangedAttackPower("player");
stats.attackPowerRanged = base + posBuff + negBuff;
_addon:TriggerUpdate();
end
--- Update dmg done mods
local function UpdateDmgDoneMods()
local spellHealing = GetSpellBonusHealing();
if spellHealing ~= stats.spellHealing then
_addon.util.PrintDebug("Updating healing");
stats.spellHealing = spellHealing;
end
_addon.util.PrintDebug("Updating dmg done mods");
for i = 1, 7, 1 do
stats.spellCrit[i] = GetSpellCritChance(i);
end
stats.attackCrit.mainhand = GetCritChance();
stats.attackCrit.ranged = GetRangedCritChance();
_addon:TriggerUpdate();
end
do
local LEVEL_REGEN_MULT = {
0.034965, 0.034191, 0.033465, 0.032526, 0.031661, 0.031076, 0.030523, 0.029994, 0.029307, 0.028661,
0.027584, 0.026215, 0.025381, 0.024300, 0.023345, 0.022748, 0.021958, 0.021386, 0.020790, 0.020121,
0.019733, 0.019155, 0.018819, 0.018316, 0.017936, 0.017576, 0.017201, 0.016919, 0.016581, 0.016233,
0.015994, 0.015707, 0.015464, 0.015204, 0.014956, 0.014744, 0.014495, 0.014302, 0.014094, 0.013895,
0.013724, 0.013522, 0.013363, 0.013175, 0.012996, 0.012853, 0.012687, 0.012539, 0.012384, 0.012233,
0.012113, 0.011973, 0.011859, 0.011714, 0.011575, 0.011473, 0.011342, 0.011245, 0.011110, 0.010999,
0.010700, 0.010522, 0.010290, 0.010119, 0.009968, 0.009808, 0.009651, 0.009553, 0.009445, 0.009327,
0.008859, 0.008415, 0.007993, 0.007592, 0.007211, 0.006849, 0.006506, 0.006179, 0.005869, 0.005575
}
---Update spirit+int based and MP5 regen values
function _addon:UpdateManaRegen()
local _, spirit = UnitStat("player", 5);
local spiritIntRegen = 0;
if _addon.IS_CLASSIC then
local _, class = UnitClass("player");
if class == "PRIEST" or class == "MAGE" then
spiritIntRegen = (12.5 + spirit / 4) / 2;
else
spiritIntRegen = (15 + spirit / 5) / 2;
end
else
local level = UnitLevel("player");
local _, int = UnitStat("player", 4);
spiritIntRegen = (math.sqrt(int) * spirit * LEVEL_REGEN_MULT[level]) * 0.6;
-- Low levels have increased regen until lvl 15.
if level < 15 then
local mult = 3.2446 * math.exp(-0.078 * level);
spiritIntRegen = spiritIntRegen * mult;
end
end
local newmanaRegCasting = spiritIntRegen * (stats.fsrRegenMult.val / 100);
local newmanaRegAura = math.max(0, GetManaRegen() - spiritIntRegen);
-- Remove minor errors in spirit regen calculation if mp5 should be 0.
if newmanaRegAura < 0.1 then newmanaRegAura = 0 end
if stats.manaRegBase ~= spiritIntRegen
or stats.manaRegCasting ~= newmanaRegCasting
or stats.manaRegAura ~= newmanaRegAura then
stats.manaRegBase = spiritIntRegen;
stats.manaRegCasting = newmanaRegCasting;
stats.manaRegAura = newmanaRegAura;
self:TriggerUpdate();
end
end
end
--- Update general stats from API
local function UpdateStats()
_addon.util.PrintDebug("Updating stats");
stats.manaMax = UnitPowerMax("player", 0);
_addon:TriggerUpdate();
end
--- Update weapon attack speeds
local function UpdateAttackSpeeds()
local m,o = UnitAttackSpeed("player");
local r = UnitRangedDamage("player");
stats.attackSpeed.mainhand = m and m or 0;
stats.attackSpeed.offhand = o and o or 0;
stats.attackSpeed.ranged = r and r or 0;
_addon.util.PrintDebug(("Updated attack speeds: %s, %s, %s"):format(stats.attackSpeed.mainhand, stats.attackSpeed.offhand, stats.attackSpeed.ranged));
_addon:TriggerUpdate();
end
--- Update weapon attack skill
local function UpdateWeaponAttack()
local mh, mhMod, oh, ohMod = UnitAttackBothHands("player");
local r, rMod = UnitRangedAttack("player");
stats.attack.mainhand = mh + mhMod;
stats.attack.offhand = oh + ohMod;
stats.attack.ranged = r + rMod;
_addon.util.PrintDebug(("Updated attack: M: %d + %d O: %d + %d R: %d + %d"):format(mh, mhMod, oh, ohMod, r, rMod));
_addon:TriggerUpdate();
end
--- Update melee attack damage
local function UpdateAttackDmg()
_addon.util.PrintDebug("Updated melee dmg");
local low, high, offLow, offHigh, _, _, pctMod = UnitDamage("player");
stats.attackDmg.mainhand.min = low / pctMod;
stats.attackDmg.mainhand.max = high / pctMod;
stats.attackDmg.offhand.min = offLow / pctMod;
stats.attackDmg.offhand.max = offHigh / pctMod;
_addon:TriggerUpdate();
end
--- Update ranged attack damage
local function UpdateRangedAttackDmg()
_addon.util.PrintDebug("Updated ranged dmg");
local _, lowDmg, hiDmg, _, _, pctMod = UnitRangedDamage("player");
stats.attackDmg.ranged.min = lowDmg / pctMod;
stats.attackDmg.ranged.max = hiDmg / pctMod;
_addon:TriggerUpdate();
end
local oldApiHitBonus = 0;
local oldApiHitBonusSpell = 0;
--- Combat ratings updated (seems to be hit modifier in classic)
local function CombatRatingUpdate()
_addon.util.PrintDebug("Combat rating update");
---@type number
local meleeHitBonus;
---@type number
local spellHitBonus;
if _addon.IS_CLASSIC then
meleeHitBonus = 0; -- GetHitModifier(); -- TODO: Only updates if weapon is equipped, not if it's removed
spellHitBonus = 0; -- GetSpellHitModifier(); -- TODO: Broken? Returns stupid numbers for no reason
else
meleeHitBonus = GetCombatRatingBonus(CR_HIT_MELEE) -- + GetHitModifier(); -- TODO: Only updates if weapon is equipped, not if it's removed
spellHitBonus = GetCombatRatingBonus(CR_HIT_SPELL) -- + GetSpellHitModifier(); -- TODO: Broken? Returns stupid numbers for no reason
end
local changed = false;
if meleeHitBonus ~= oldApiHitBonus then
stats.hitBonus.val = stats.hitBonus.val - oldApiHitBonus + meleeHitBonus;
oldApiHitBonus = meleeHitBonus;
changed = true;
end
if spellHitBonus ~= oldApiHitBonusSpell then
stats.hitBonusSpell.val = stats.hitBonusSpell.val - oldApiHitBonusSpell + spellHitBonus;
oldApiHitBonusSpell = spellHitBonus;
changed = true;
end
if changed then
_addon.util.PrintDebug("Updated hit mods from API. M: " .. meleeHitBonus .. " - S: " .. spellHitBonus);
_addon:TriggerUpdate();
end
end
_addon.stats = stats;
_addon.events.Register("UNIT_ATTACK_POWER", function (unit) if unit == "player" then UpdateAttackPower() end end);
_addon.events.Register("UNIT_RANGED_ATTACK_POWER", function (unit) if unit == "player" then UpdateRangedAttackPower() end end);
_addon.events.Register("UNIT_RANGEDDAMAGE", function (unit) if unit == "player" then UpdateRangedAttackDmg() end end);
_addon.events.Register("UNIT_DAMAGE", function (unit) if unit == "player" then UpdateAttackDmg() end end);
_addon.events.Register("UNIT_ATTACK", function (unit) if unit == "player" then UpdateWeaponAttack() end end);
_addon.events.Register("UNIT_ATTACK_SPEED", function (unit) if unit == "player" then UpdateAttackSpeeds() end end);
_addon.events.Register("COMBAT_RATING_UPDATE", function () CombatRatingUpdate() end);
_addon.events.Register("PLAYER_DAMAGE_DONE_MODS", function (unit) if unit == "player" then UpdateDmgDoneMods() end end);
_addon.events.Register("SPELL_POWER_CHANGED", function () UpdateSpellPower() end);
_addon.events.Register("PLAYER_LEVEL_UP", function (level) UpdateBaseMana(level) end);
_addon.events.Register("PLAYER_ENTERING_WORLD", function()
UpdateStats();
UpdateSpellPower();
UpdateDmgDoneMods();
UpdateAttackSpeeds();
UpdateWeaponAttack();
UpdateAttackDmg();
UpdateRangedAttackDmg();
CombatRatingUpdate();
_addon:UpdateManaRegen();
UpdateAttackPower();
UpdateRangedAttackPower();
UpdateBaseMana(UnitLevel("player"));
end);
_addon.events.Register("UNIT_STATS", function (unit)
if unit == "player" then
UpdateStats();
_addon:UpdateManaRegen();
end
end);
_addon.events.Register("UNIT_AURA", function (unit)
if unit == "player" then
_addon:UpdateManaRegen();
end
end);