TL;DR
Please unify recipes to recipe.normal{ ingredients{}, results{} } and recipe.expensive{ ingredients{}, results{} }What ?
Currently, if i want to get the results of a recipe i have to check forrecipe.result, recipe results or normal.result, normal.results or expensive.result, expensive.results
and if the table has keys or not. This is terrible.
It is just to complicated for such a simple task and opens itself up to bugs.
Why ?
Just to get you an idea what we poor modders have to do to get key-value tables out of a recipes results:Code: Select all
---@param table table ``{"name", n?}``
---@return table ``{ name = "name", amount = n }``
local function add_pairs(table)
if table.name then return table end
local _t = table
_t.name = _t[1] ; _t[1] = nil
_t.amount = _t[2] or 1 ; _t[2] = nil
return _t
end
---@param data_recipe table ``data.raw.recipe["name"]``
function recipe_get_keywords(data_recipe)
local _return = {ingredients = nil, normal = nil, expensive = nil, result = nil, result_count = nil, results = nil}
if data_recipe.ingredients then
_return.ingredients = true
end
if data_recipe.normal then
_return.normal = true
end
if data_recipe.expensive then
_return.expensive = true
end
if data_recipe.result then
_return.result = true
end
if data_recipe.result_count then
_return.result_count = true
end
if data_recipe.results then
_return.results = true
end
return _return
end
---@param data_recipe table ``data.raw.recipe["name"]``
---@param difficulty? table ``{result = boolean, results = boolean, normal = boolean, expensive = boolean}``
function recipe_get_results(data_recipe, difficulty)
local _results = {results = {}, normal = {}, expensive = {}}
local _difficulty = difficulty or recipe_get_keywords(data_recipe)
if _difficulty.results then
for i, value in ipairs(data_recipe.results) do
_results.results[i] = add_pairs(value)
end
end
if _difficulty.result then
_results.results = add_pairs(data_recipe.result)
if data_recipe.result_count then
_results.results.amount = data_recipe.result_count
end
end
if _difficulty.normal then
if data_recipe.normal.result then
_results.normal.name = data_recipe.normal.result
_results.normal.amount = data_recipe.normal.result_amount or 1
else
for i, value in ipairs(data_recipe.normal.results) do
_results.normal[i] = add_pairs(value)
end
end
end
if _difficulty.expensive then
if data_recipe.expensive.result then
_results.expensive.name = data_recipe.expensive.result
_results.expensive.amount = data_recipe.result_count or 1
else
for i, value in ipairs(data_recipe.expensive.results) do
_results.expensive[i] = add_pairs(value)
end
end
end
return _results
end
Code: Select all
---@param data_recipe table ``data.raw.recipe["name"]``
---@param difficulty? table ``{result = boolean, results = boolean, normal = boolean, expensive = boolean}``
function recipe_get_results(data_recipe)
local _results = {normal = {}, expensive = {}}
if data_recipe.normal then
_results.normal = data_recipe.normal
end
if data_recipe.expensive then
_results.expensive = data_recipe.expensive
end
return _results
end
Shure, it will break some mods but then again, changing like 100 lines to a simple function (or even get rid of it) seems to be a good price to pay.
(This also applies to ingredients.)
Example recipe:
Code: Select all
{
type = "recipe",
name = "kovarex-enrichment-process",
energy_required = 60,
category = "centrifuging",
normal = {
enabled = false,
ingredients = {{ name = "uranium-235", amount = 40 }, { name = "uranium-238", amount = 5 }},
main_product = "",
results = {{ name = "uranium-235", amount = 41 }, { name = "uranium-238", amount = 2 }}
},
-- expensive would default to normal
icon = "__base__/graphics/icons/kovarex-enrichment-process.png",
icon_size = 64, icon_mipmaps = 4,
subgroup = "intermediate-product",
order = "r[uranium-processing]-c[kovarex-enrichment-process]",
allow_decomposition = false
}
This also applies (some way) to every other table which can have a difficulty setting like technology