Skip to content

Commit b0adb69

Browse files
authored
Optimize itemLib.applyRange, improve startup time (#6407)
* Add applyRange tests * Optimize itemLib.applyRange, improve startup time
1 parent 8440ffb commit b0adb69

File tree

3 files changed

+96
-29
lines changed

3 files changed

+96
-29
lines changed

spec/System/TestItemTools_spec.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
local applyRangeTests = {
2+
-- Number without range
3+
[{ "+10 to maximum Life", 1.0, 1.0 }] = "+10 to maximum Life",
4+
[{ "+10 to maximum Life", 1.0, 1.5 }] = "+15 to maximum Life",
5+
[{ "+10 to maximum Life", 0.5, 1.0 }] = "+10 to maximum Life",
6+
[{ "+10 to maximum Life", 0.5, 1.5 }] = "+15 to maximum Life",
7+
-- One range
8+
[{ "+(10-20) to maximum Life", 1.0, 1.0 }] = "+20 to maximum Life",
9+
[{ "+(10-20) to maximum Life", 1.0, 1.5 }] = "+30 to maximum Life",
10+
[{ "+(10-20) to maximum Life", 0.5, 1.0 }] = "+15 to maximum Life",
11+
[{ "+(10-20) to maximum Life", 0.5, 1.5 }] = "+22 to maximum Life",
12+
-- Two ranges
13+
[{ "Adds (60-80) to (270-300) Physical Damage", 1.0, 1.0 }] = "Adds 80 to 300 Physical Damage",
14+
[{ "Adds (60-80) to (270-300) Physical Damage", 1.0, 1.5 }] = "Adds 120 to 450 Physical Damage",
15+
[{ "Adds (60-80) to (270-300) Physical Damage", 0.5, 1.0 }] = "Adds 70 to 285 Physical Damage",
16+
[{ "Adds (60-80) to (270-300) Physical Damage", 0.5, 1.5 }] = "Adds 105 to 427 Physical Damage",
17+
-- Range with increased/reduced
18+
[{ "(10--10)% increased Charges per use", 1.0, 1.0 }] = "10% reduced Charges per use",
19+
[{ "(10--10)% increased Charges per use", 1.0, 1.5 }] = "15% reduced Charges per use",
20+
[{ "(10--10)% increased Charges per use", 0.5, 1.0 }] = "0% increased Charges per use",
21+
[{ "(10--10)% increased Charges per use", 0.5, 1.5 }] = "0% increased Charges per use",
22+
[{ "(10--10)% increased Charges per use", 0.0, 1.0 }] = "10% increased Charges per use",
23+
[{ "(10--10)% increased Charges per use", 0.0, 1.5 }] = "15% increased Charges per use",
24+
-- Range with constant numbers after
25+
[{ "(15-20)% increased Cold Damage per 1% Cold Resistance above 75%", 1.0, 1.0 }] = "20% increased Cold Damage per 1% Cold Resistance above 75%",
26+
[{ "(15-20)% increased Cold Damage per 1% Cold Resistance above 75%", 1.0, 1.5 }] = "30% increased Cold Damage per 1% Cold Resistance above 75%",
27+
[{ "(15-20)% increased Cold Damage per 1% Cold Resistance above 75%", 0.5, 1.0 }] = "18% increased Cold Damage per 1% Cold Resistance above 75%",
28+
[{ "(15-20)% increased Cold Damage per 1% Cold Resistance above 75%", 0.5, 1.5 }] = "27% increased Cold Damage per 1% Cold Resistance above 75%",
29+
-- High precision range
30+
[{ "Regenerate (66.7-75) Life per second", 1.0, 1.0 }] = "Regenerate 75 Life per second",
31+
[{ "Regenerate (66.7-75) Life per second", 1.0, 1.5 }] = "Regenerate 112.5 Life per second",
32+
[{ "Regenerate (66.7-75) Life per second", 0.5, 1.0 }] = "Regenerate 70.9 Life per second",
33+
[{ "Regenerate (66.7-75) Life per second", 0.5, 1.5 }] = "Regenerate 106.3 Life per second",
34+
-- Range with plus sign that is removed when negative
35+
[{ "+(-25-50)% to Fire Resistance", 1.0, 1.0 }] = "+50% to Fire Resistance",
36+
[{ "+(-25-50)% to Fire Resistance", 1.0, 1.5 }] = "+75% to Fire Resistance",
37+
[{ "+(-25-50)% to Fire Resistance", 0.0, 1.0 }] = "-25% to Fire Resistance",
38+
[{ "+(-25-50)% to Fire Resistance", 0.0, 1.5 }] = "-37% to Fire Resistance",
39+
}
40+
41+
describe("TestItemTools", function()
42+
for args, expected in pairs(applyRangeTests) do
43+
it(string.format("tests applyRange('%s', %.2f, %.2f)", unpack(args)), function()
44+
local result = itemLib.applyRange(unpack(args))
45+
assert.are.equals(expected, result)
46+
end)
47+
end
48+
end)

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)