-
Notifications
You must be signed in to change notification settings - Fork 996
Expand file tree
/
Copy pathItemTools.lua
More file actions
57 lines (53 loc) · 2.11 KB
/
Copy pathItemTools.lua
File metadata and controls
57 lines (53 loc) · 2.11 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
-- Path of Building
--
-- Module: Item Tools
-- Various functions for dealing with items.
--
local t_insert = table.insert
local t_remove = table.remove
local m_min = math.min
local m_max = math.max
local m_floor = math.floor
itemLib = { }
-- Apply range value (0 to 1) to a modifier that has a range: (x to x) or (x-x to x-x)
function itemLib.applyRange(line, range)
return line:gsub("%((%d+)%-(%d+) to (%d+)%-(%d+)%)", "(%1-%2) to (%3-%4)")
:gsub("(%+?)%((%-?%d+) to (%d+)%)", "%1(%2-%3)")
:gsub("(%+?)%((%-?%d+)%-(%d+)%)",
function(plus, min, max)
local numVal = m_floor(tonumber(min) + range * (tonumber(max) - tonumber(min)) + 0.5)
if numVal < 0 then
if plus == "+" then
plus = ""
end
end
return plus .. tostring(numVal)
end)
:gsub("%((%d+%.?%d*)%-(%d+%.?%d*)%)",
function(min, max)
local numVal = m_floor((tonumber(min) + range * (tonumber(max) - tonumber(min))) * 10 + 0.5) / 10
return tostring(numVal)
end)
:gsub("%-(%d+%%) increased", function(num) return num.." reduced" end)
end
-- Clean item text by removing or replacing unsupported or redundant characters or sequences
function itemLib.sanitiseItemText(text)
-- Something something unicode support something grumble
return text:gsub("^%s+",""):gsub("%s+$",""):gsub("\r\n","\n"):gsub("%b<>",""):gsub("–","-"):gsub("\226\128\147","-"):gsub("\226\136\146","-"):gsub("ö","o"):gsub("\195\182","o"):gsub("[\128-\255]","?")
end
function itemLib.formatModLine(modLine, dbMode)
local line = (not dbMode and modLine.range and itemLib.applyRange(modLine.line, modLine.range)) or modLine.line
if line:match("^%+?0%%? ") or (line:match(" %+?0%%? ") and not line:match("0 to [1-9]")) or line:match(" 0%-0 ") or line:match(" 0 to 0 ") then -- Hack to hide 0-value modifiers
return
end
local colorCode
if modLine.extra then
colorCode = colorCodes.UNSUPPORTED
if launch.devModeAlt then
line = line .. " ^1'" .. modLine.extra .. "'"
end
else
colorCode = (modLine.crafted and colorCodes.CRAFTED) or (modLine.custom and colorCodes.CUSTOM) or (modLine.fractured and colorCodes.FRACTURED) or colorCodes.MAGIC
end
return colorCode..line
end