@@ -59,18 +59,39 @@ function itemLib.getLineRangeMinMax(line)
5959 return rangeMin , rangeMax
6060end
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)"
6370function 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 )
111130end
112131
0 commit comments