Simplify weight calculation to prevent infinite recursion
This commit is contained in:
parent
215dd76d90
commit
ff57a87914
1 changed files with 1 additions and 48 deletions
|
@ -1,52 +1,5 @@
|
||||||
local defaultSpoilTicks = settings.startup["hot-metals-time"].value * 60
|
local defaultSpoilTicks = settings.startup["hot-metals-time"].value * 60
|
||||||
|
|
||||||
---@param name data.ItemID
|
|
||||||
---@return data.ItemPrototype?
|
|
||||||
function getItem(name)
|
|
||||||
if data.raw.item[name] then
|
|
||||||
return data.raw.item[name] --[[@as data.ItemPrototype]]
|
|
||||||
end
|
|
||||||
for item_type in pairs(defines.prototypes.item) do
|
|
||||||
local type_lookup = data.raw[item_type]
|
|
||||||
if type_lookup and type_lookup[name] then
|
|
||||||
return type_lookup[name] --[[@as data.ItemPrototype]]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param table item
|
|
||||||
---@return number
|
|
||||||
function getWeight(item)
|
|
||||||
if type(item) == "string" then
|
|
||||||
item = getItem(item)
|
|
||||||
end
|
|
||||||
if item.weight then return item.weight end
|
|
||||||
local factor = item.ingredient_to_weight_coefficient or 0.5
|
|
||||||
for _, recipe in pairs(data.raw.recipe) do
|
|
||||||
if recipe.results == nil then
|
|
||||||
goto continue
|
|
||||||
end
|
|
||||||
local function isResultMatch(result)
|
|
||||||
return result.name == item.name
|
|
||||||
end
|
|
||||||
local results = table.filter(recipe.results, isResultMatch)
|
|
||||||
if #results > 0 then
|
|
||||||
for _, ingredient in pairs(recipe.ingredients) do
|
|
||||||
if ingredient.type == "item" then
|
|
||||||
local weight = data.raw.item[ingredient.name].weight or getWeight(data.raw.item[ingredient.name])
|
|
||||||
local amount = ingredient.amount
|
|
||||||
if ingredient.amount_min and ingredient.amount_max then
|
|
||||||
amount = (ingredient.amount_min + ingredient.amount_max) / 2
|
|
||||||
end
|
|
||||||
return amount * weight * factor
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
::continue::
|
|
||||||
end
|
|
||||||
return 100
|
|
||||||
end
|
|
||||||
|
|
||||||
function createHotVariant(config)
|
function createHotVariant(config)
|
||||||
local itemName = config
|
local itemName = config
|
||||||
local spoilTicks = defaultSpoilTicks
|
local spoilTicks = defaultSpoilTicks
|
||||||
|
@ -58,7 +11,7 @@ function createHotVariant(config)
|
||||||
iconFolder = config.iconFolder or iconFolder
|
iconFolder = config.iconFolder or iconFolder
|
||||||
end
|
end
|
||||||
local item = data.raw.item[itemName]
|
local item = data.raw.item[itemName]
|
||||||
item.weight = getWeight(item)
|
item.weight = item.weight or 10000
|
||||||
|
|
||||||
-- Create new item
|
-- Create new item
|
||||||
local hotItem = table.deepcopy(data.raw.item[itemName])
|
local hotItem = table.deepcopy(data.raw.item[itemName])
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue