Skip to content

Commit 1c9519b

Browse files
committed
Optimize itemLib.applyRange, improve startup time
1 parent 8b08b0b commit 1c9519b

File tree

2 files changed

+48
-29
lines changed

2 files changed

+48
-29
lines changed

src/Data/Rares.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -563,9 +563,9 @@ Suffix: FireResist4
563563
Suffix: ColdResist4
564564
Suffix: LightningResist4
565565
Implicits: 3
566-
{variant:1}+(12 to 16)% to Fire and Cold Resistances
567-
{variant:2}+(12 to 16)% to Cold and Lightning Resistances
568-
{variant:3}+(12 to 16)% to Fire and Lightning Resistances
566+
{variant:1}+(12-16)% to Fire and Cold Resistances
567+
{variant:2}+(12-16)% to Cold and Lightning Resistances
568+
{variant:3}+(12-16)% to Fire and Lightning Resistances
569569
]],[[
570570
Ring
571571
Unset Ring

src/Modules/ItemTools.lua

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,39 @@ function itemLib.getLineRangeMinMax(line)
5959
return rangeMin, rangeMax
6060
end
6161

62-
-- Apply range value (0 to 1) to a modifier that has a range: (x to x) or (x-x to x-x)
62+
local antonyms = {
63+
["increased"] = "reduced",
64+
["reduced"] = "increased",
65+
["more"] = "less",
66+
["less"] = "more",
67+
}
68+
69+
-- Apply range value (0 to 1) to a modifier that has a range: "(x-x)" or "(x-x) to (x-x)"
6370
function itemLib.applyRange(line, range, valueScalar)
64-
local numbers = 0
65-
local precision = nil
71+
local precisionSame = true
6672
-- Create a line with ranges removed to check if the mod is a high precision mod.
67-
local testLine = line:gsub("%((%d+)%-(%d+) to (%d+)%-(%d+)%)", "(%1-%2) to (%3-%4)")
68-
:gsub("(%+?)%((%-?%d+) to (%d+)%)", "%1(%2-%3)")
69-
:gsub("(%+?)%((%-?%d+%.?%d*)%-(%-?%d+%.?%d*)%)", function(plus, min, max) return plus.."1" end)
70-
:gsub("%-(%d+%%) increased", function(num) return num.." reduced" end)
71-
:gsub("%-(%d+%%) reduced", function(num) return num.." increased" end)
72-
:gsub("%-(%d+%%) more", function(num) return num.." less" end)
73-
:gsub("%-(%d+%%) less", function(num) return num.." more" end)
73+
local testLine = not line:find("-", 1, true) and line or
74+
line:gsub("(%+?)%((%-?%d+%.?%d*)%-(%-?%d+%.?%d*)%)",
75+
function(plus, min, max)
76+
min = tonumber(min)
77+
local maxPrecision = min + range * (tonumber(max) - min)
78+
local minPrecision = m_floor(maxPrecision + 0.5)
79+
if minPrecision ~= maxPrecision then
80+
precisionSame = false
81+
end
82+
return (minPrecision < 0 and "" or plus) .. tostring(minPrecision)
83+
end)
84+
:gsub("%-(%d+%%) (%a+)",
85+
function(num, word)
86+
local antonym = antonyms[word]
87+
return antonym and (num.." "..antonym) or ("-"..num.." "..word)
88+
end)
89+
90+
if precisionSame and (not valueScalar or valueScalar == 1) then
91+
return testLine
92+
end
93+
94+
local precision = nil
7495
local modList, extra = modLib.parseMod(testLine)
7596
if modList and not extra then
7697
for _, mod in pairs(modList) do
@@ -86,27 +107,25 @@ function itemLib.applyRange(line, range, valueScalar)
86107
if not precision and line:match("(%d+%.%d*)") then
87108
precision = data.defaultHighPrecision
88109
end
89-
line = line:gsub("%((%d+)%-(%d+) to (%d+)%-(%d+)%)", "(%1-%2) to (%3-%4)")
90-
:gsub("(%+?)%((%-?%d+) to (%d+)%)", "%1(%2-%3)")
91-
:gsub("(%+?)%((%-?%d+%.?%d*)%-(%-?%d+%.?%d*)%)",
110+
111+
local numbers = 0
112+
line = line:gsub("(%+?)%((%-?%d+%.?%d*)%-(%-?%d+%.?%d*)%)",
92113
function(plus, min, max)
93114
numbers = numbers + 1
94115
local power = 10 ^ (precision or 0)
95116
local numVal = m_floor((tonumber(min) + range * (tonumber(max) - tonumber(min))) * power + 0.5) / power
96-
if numVal < 0 then
97-
if plus == "+" then
98-
plus = ""
99-
end
100-
end
101-
return plus .. tostring(numVal)
117+
return (numVal < 0 and "" or plus) .. tostring(numVal)
102118
end)
103-
:gsub("%-(%d+%%) increased", function(num) return num.." reduced" end)
104-
:gsub("%-(%d+%%) reduced", function(num) return num.." increased" end)
105-
:gsub("%-(%d+%%) more", function(num) return num.." less" end)
106-
:gsub("%-(%d+%%) less", function(num) return num.." more" end)
107-
if numbers == 0 and line:match("(%d+%.?%d*)%%? ") then --If a mod contains x or x% and is not already a ranged value, then only the first number will be scalable as any following numbers will always be conditions or unscalable values.
108-
numbers = 1
109-
end
119+
:gsub("%-(%d+%%) (%a+)",
120+
function(num, word)
121+
local antonym = antonyms[word]
122+
return antonym and (num.." "..antonym) or ("-"..num.." "..word)
123+
end)
124+
125+
if numbers == 0 and line:match("(%d+%.?%d*)%%? ") then --If a mod contains x or x% and is not already a ranged value, then only the first number will be scalable as any following numbers will always be conditions or unscalable values.
126+
numbers = 1
127+
end
128+
110129
return itemLib.applyValueScalar(line, valueScalar, numbers, precision)
111130
end
112131

0 commit comments

Comments
 (0)