-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDataUtils.lua
More file actions
212 lines (189 loc) · 6.28 KB
/
DataUtils.lua
File metadata and controls
212 lines (189 loc) · 6.28 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
local ECSLoader = ECSLoader
local floor = math.floor
local GetBuffDataByIndex = C_UnitAuras.GetBuffDataByIndex
local GetInventoryItemLink = GetInventoryItemLink
local GetItemInfo = C_Item.GetItemInfo
local GetRuneForEquipmentSlot = C_Engraving.GetRuneForEquipmentSlot
local IsSoD = ECS.IsSoD
local IsSpellKnown = C_SpellBook.IsSpellKnown
local IsWotlk = ECS.IsWotlk
local match = string.match
local min = math.min
local strsplit = strsplit
local tonumber = tonumber
---@class DataUtils
local DataUtils = ECSLoader:CreateModule("DataUtils")
---@type Data
local Data = ECSLoader:ImportModule("Data")
--- Rounds every number down to the given decimal places
---@param num number
---@param decimalPlaces number
---@return number
function DataUtils:Round(num, decimalPlaces)
if (not num) then
return 0
end
local mult = 10^(decimalPlaces)
return floor(num * mult + 0.5) / mult
end
---@return boolean
function DataUtils:IsShapeshifted()
local i = 1
repeat
local aura = GetBuffDataByIndex("player", i)
i = i + 1
if aura and aura.spellId then
if Data.Aura.IsFeralForm[aura.spellId] then
return true
end
end
until (not aura)
return false
end
---@param weaponSkill number
---@param defenseValue number
---@return number
function DataUtils.GetMissChanceByDifference(weaponSkill, defenseValue)
local delta = defenseValue - weaponSkill
if delta <= 10 then
-- For a difference of 0-10 each point in weapon skill is worth 0.1% miss chance reduction
return 5 + delta * 0.1
elseif delta <= 14 then
-- For a difference of 11-14 each point in weapon skill is worth 0.4% miss chance reduction
local extraWeaponSkillDifference = ((15 - delta) * 0.2)
return DataUtils:Round(6 + delta * 0.2 - extraWeaponSkillDifference, 2)
elseif IsWotlk then
-- For a difference of 15+ each point in weapon skill is worth 0.2% miss chance reduction
return 5 + delta * 0.2
else
-- For a difference of 15+ each point in weapon skill is worth 0.2% miss chance reduction
return 6 + delta * 0.2
end
end
---@param level number
---@param weaponSkill number
---@param defenseValue number
---@return number
function DataUtils:GetGlancingChanceByDifference(level, weaponSkill, defenseValue)
local glancingChance = 0.1 + (defenseValue - min(level*5, weaponSkill)) * 0.02
-- Ensure the glancing chance does not exceed 1.0 (100%)
if glancingChance > 1.0 then
glancingChance = 1.0
end
return glancingChance
end
---@param weaponSkill number
---@param defenseValue number
---@return number
function DataUtils:GetGlancingDamage(weaponSkill, defenseValue)
local difference = defenseValue - weaponSkill
local low = min(0.91 ,(1.3 - 0.05 * difference))
-- Ensure low does not go below 0.01
if low < 0.01 then
low = 0.01
end
local high = 1.2 - (0.03 * difference)
if high > 0.99 then
high = 0.99
elseif high < 0.2 then
high = 0.2
end
local averageDamageFactor = (high + low) / 2
-- Ensure the average damage factor is not negative (damage reduction is not > 100%)
if averageDamageFactor < 0 then
averageDamageFactor = 0
end
return averageDamageFactor
end
---@param equipSlot EquipSlot
---@return number|nil
function DataUtils:GetEnchantForEquipSlot(equipSlot)
local slotId, _ = GetInventorySlotInfo(equipSlot)
local itemLink = GetInventoryItemLink("player", slotId)
return DataUtils:GetEnchantFromItemLink(itemLink)
end
---@param itemLink ItemLink
---@return number|nil
function DataUtils:GetEnchantFromItemLink(itemLink)
if itemLink then
local _, itemStringLink = GetItemInfo(itemLink)
if itemStringLink then
local _, _, enchant, _ = strsplit(":", itemStringLink, 4)
return tonumber(enchant)
end
end
return nil
end
---@param equipSlot EquipSlot
---@return number|nil
function DataUtils.GetRuneForEquipSlot(equipSlot)
local slotId, _ = GetInventorySlotInfo(equipSlot)
local runeInfo = GetRuneForEquipmentSlot(slotId)
if runeInfo then
return runeInfo.itemEnchantmentID
else
return nil
end
end
---@param itemLink ItemLink
---@return table<number | nil> | nil
function DataUtils:GetSocketedGemsFromItemLink(itemLink)
if itemLink then
local _, itemStringLink = GetItemInfo(itemLink)
if itemStringLink then
local _, _, gem1, gem2, gem3, _ = strsplit(":", itemStringLink, 6)
return {gem1 and tonumber(gem1) or nil, gem2 and tonumber(gem2) or nil, gem3 and tonumber(gem3) or nil}
end
end
return nil
end
---@return boolean
function DataUtils:CanParry()
return (IsSpellKnown(3127) or IsSpellKnown(18848) or IsSpellKnown(3124))
end
---@return boolean
function DataUtils:CanBlock()
return IsSpellKnown(107)
end
--- Search for the first known spell of the talentList and return the index of it, as that will be used as multiplier
---@param talentList table<number> the order of these spells matter. Starting with the lowest rank and ending with the highest.
---@return number
function DataUtils:GetActiveTalentSpell(talentList)
for i = #talentList,1,-1 do
if IsSpellKnown(talentList[i]) then
return i
end
end
return 0
end
---@return number
function DataUtils:CountTimewornItems()
local timeworn = 0
if IsSoD then
for i = 1, 18 do
local id, _ = GetInventoryItemID("player", i)
if Data.Item.IsTimeworn[id] then
timeworn = timeworn + 1
end
end
end
return timeworn
end
---@return number
function DataUtils:GetValueFromAuraTooltip(index, type)
if not ECS.scanningTooltip then
ECS.scanningTooltip = CreateFrame("GameTooltip", "scanningTooltip", nil, "GameTooltipTemplate")
ECS.scanningTooltip:SetOwner(WorldFrame, "ANCHOR_NONE")
end
ECS.scanningTooltip:ClearLines()
ECS.scanningTooltip:SetUnitAura("player", index, type)
local region = select(5, ECS.scanningTooltip:GetRegions())
if region and region:GetObjectType() == "FontString" then
local tooltip = region:GetText()
if tooltip then
return tonumber(match(tooltip, "%d[%d,.]*"))
end
end
return 0
end
return DataUtils