forked from KvaNTy/MPRD
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-final-fixes.lua
More file actions
106 lines (93 loc) · 3.79 KB
/
data-final-fixes.lua
File metadata and controls
106 lines (93 loc) · 3.79 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
local Vertical_Display = settings.startup["mprd-vertical-display"].value
local Amount_Display = settings.startup["mprd-amount-display"].value
-- Result definition structure can vary:
-- {name="product_name", amount=int}
-- {name="product_name", amount=int, probability=int}
-- {type="item|fluid", name="product_name", amount=int}
-- {type="item|fluid", name="product_name", amount=int, probability=int}
-- {[1]="item|fluid", [2]="product_name", [3]=int, [4]=int}
local function parse_product_info(result)
local p_type = nil
local p_name = nil
local p_amount = nil
local p_probability = nil
-- Get Type
if result.type ~= nil then p_type = result.type
else
-- {[1]="item|fluid", [2]="product_name", ... }
if type(result[1]) == "string" and type(result[2]) == "string" then p_type = result[1] end
end
-- Get Name
if result.name ~= nil then p_name = result.name
else
if p_type ~= nil then p_name = result[2] -- {[1]="item|fluid", [2]="product_name", [3]=int}
else p_name = result[1] -- {[1]="product_name", [2]=int}
end
end
-- Get Amount
if result.amount ~= nil then p_amount = result.amount
else
if p_type ~= nil then p_amount = result[3] -- {[1]="item|fluid", [2]="product_name", [3]=int}
else p_amount = result[2] -- {[1]="product_name", [2]=int}
end
end
-- Get Probability
if result.probability ~= nil then p_probability = result.probability
else
if p_type ~= nil then p_probability = result[4] -- {[1]="item|fluid", [2]="product_name", [3]=int, [4]=int}
else p_probability = result[3] -- {[1]="product_name", [2]=int, [3]=int}
end
end
return {type=p_type, name=p_name, amount=p_amount, probability=p_probability}
end
local function format_line(product, name_prefix, output)
if Vertical_Display then
table.insert(output, "\n")
table.insert(output, {name_prefix .. product.name})
else
table.insert(output, ", ")
table.insert(output, {name_prefix .. product.name})
end
if Amount_Display and product.amount ~= nil then
if product.probability ~= nil and product.probability ~= 1 then
table.insert(output, " [" .. product.amount .. "*" .. product.probability * 100 .. "%]")
else
table.insert(output, " [" .. product.amount .. "]")
end
end
return output
end
local function generate_description(recipe, results)
local new_description = {'', {"recipe-description.mprd-caption"}, " "}
local fluids = {}
for _, result in pairs(results) do
local product = parse_product_info(result)
-- If type is not defined product supposed to be an Item
if product.type == "item" or product.type == nil then
new_description = format_line(product, "item-name.", new_description)
else
fluids = format_line(product, "fluid-name.", fluids)
end
end
for _, key in pairs(fluids) do table.insert(new_description, key) end -- Items first, Fluids last
if not Vertical_Display then table.remove(new_description, 4) end -- Remove preemptive first comma
if recipe.localised_description or recipe.description then -- Preserve existing description
if recipe.localised_description then table.insert(new_description, 2, recipe.localised_description)
else table.insert(new_description, 2, {recipe.description}) end
table.insert(new_description, 3, "\n\n")
end
--table.insert(new_description, "\n") -- In case additional spacing is needed
return new_description
end
local function get_results(recipe)
if recipe.results ~= nil then return recipe.results end
if recipe.normal ~= nil and recipe.normal.results ~= nil then return recipe.normal.results end
end
for _, recipe in pairs(data.raw["recipe"]) do
if recipe.results ~= nil or (recipe.normal ~= nil and recipe.normal.results ~= nil) then
local results = get_results(recipe)
if results ~= nil and #results > 1 then -- Only for multi-product recipes
recipe.localised_description = generate_description(recipe, results)
end
end
end