Fix item weights

This commit is contained in:
Simon Brodtmann 2025-02-03 21:56:54 +01:00
parent b6e46d3849
commit 42c7360a39

View file

@ -1,5 +1,52 @@
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
@ -11,6 +58,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)
-- Create new item -- Create new item
local hotItem = table.deepcopy(data.raw.item[itemName]) local hotItem = table.deepcopy(data.raw.item[itemName])
@ -22,6 +70,8 @@ function createHotVariant(config)
hotItem.icon = iconFolder .. "hot-" .. itemName .. ".png" hotItem.icon = iconFolder .. "hot-" .. itemName .. ".png"
end end
hotItem.order = hotItem.order .. "-hot" hotItem.order = hotItem.order .. "-hot"
hotItem.ingredient_to_weight_coefficient = item.ingredient_to_weight_coefficient
hotItem.weight = item.weight
hotItem.spoil_result = itemName hotItem.spoil_result = itemName
hotItem.spoil_ticks = spoilTicks hotItem.spoil_ticks = spoilTicks
data:extend({ hotItem }) data:extend({ hotItem })
@ -34,6 +84,7 @@ function createHotVariant(config)
end end
local results = table.filter(recipe.results, isResultMatch) local results = table.filter(recipe.results, isResultMatch)
if #results > 0 then if #results > 0 then
-- Change regular recipe to be the "hot" recipe
for _, result in pairs(results) do for _, result in pairs(results) do
result.name = hotItem.name result.name = hotItem.name
end end