From 42c7360a395737fce7e31b661fb27d64cd8351fd Mon Sep 17 00:00:00 2001 From: Simon Brodtmann Date: Mon, 3 Feb 2025 21:56:54 +0100 Subject: [PATCH] Fix item weights --- hot-metals/prototypes/hot-metals.lua | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/hot-metals/prototypes/hot-metals.lua b/hot-metals/prototypes/hot-metals.lua index 63daa24..d6ab1a1 100644 --- a/hot-metals/prototypes/hot-metals.lua +++ b/hot-metals/prototypes/hot-metals.lua @@ -1,5 +1,52 @@ 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) local itemName = config local spoilTicks = defaultSpoilTicks @@ -11,6 +58,7 @@ function createHotVariant(config) iconFolder = config.iconFolder or iconFolder end local item = data.raw.item[itemName] + item.weight = getWeight(item) -- Create new item local hotItem = table.deepcopy(data.raw.item[itemName]) @@ -22,6 +70,8 @@ function createHotVariant(config) hotItem.icon = iconFolder .. "hot-" .. itemName .. ".png" end 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_ticks = spoilTicks data:extend({ hotItem }) @@ -34,6 +84,7 @@ function createHotVariant(config) end local results = table.filter(recipe.results, isResultMatch) if #results > 0 then + -- Change regular recipe to be the "hot" recipe for _, result in pairs(results) do result.name = hotItem.name end