-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfh_util.lua
44 lines (38 loc) · 1.29 KB
/
fh_util.lua
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
require("util")
local fh_util = {}
function fh_util.make_item_id(name, quality)
return name .. "|" .. quality
end
function fh_util.canonize_item(item, quality)
if not item then
return
end
local item_name = item
if type(item) ~= "string" then
item_name = item.name
if not item_name then
return
end
if type(item_name) ~= "string" then
item_name = item_name.name
end
if not (item.object_name and string.match(item.object_name, "Prototype$")) and item.quality and not quality then
quality = item.quality
end
end
local quality_name = type(quality) == "string" and quality or (quality or prototypes.quality.normal).name
---@class ItemWithQuality
return { name = item_name, quality = quality_name }
end
function fh_util.add_item_to_table(table, item, quality)
local canonic_item = fh_util.canonize_item(item, quality)
if canonic_item then
table[fh_util.make_item_id(canonic_item.name, canonic_item.quality)] = canonic_item
end
end
function fh_util.is_same_item(item1, item2)
item1 = fh_util.canonize_item(item1)
item2 = fh_util.canonize_item(item2)
return item1 and item2 and item1.name == item2.name and item1.quality == item2.quality
end
return fh_util