Hi! While checking my Bio Industries for compatibility, I noticed that the game will crash on loading if both "Silica & Silicon" and "Industrial Revolution 2" are active:
Error while running setup for recipe prototype "concrete" (recipe): Duplicate item ingredients are not allowed (silica exists 2 or more times).
The problem is in this function in your util.lua:
function add_ingredient(recipe, ingredient, quantity)
if recipe ~= nil and recipe.ingredients ~= nil then
table.insert(recipe.ingredients, {ingredient, quantity})
end
end
You should loop over recipe.ingredients and check if the ingredient you want to add is already used in the recipe. If so, just add your quantity to the existing amount; if not, use your current table.insert() to add the ingredient. However, there's one complication: You can't know what mods have touched a recipe before yours. So each ingredient could be either in the short format ({name, amount}), or in the long format ({type = "item", name = ingredient, amount = quantity}), and you should check for both:
function add_ingredient(recipe, ingredient, quantity)
local found_ingredient = false
local name, amount
-- Recipe must exist
if recipe then
-- Recipe may produce something without ingredients
recipe.ingredients = recipe.ingredients or {}
-- Check if the ingredient is already used in the recipe
for k, i in ipairs(recipe.ingredients) do
-- Allow for short and long format of ingredients!
name = i.name or i[1]
amount = i.amount or i[2]
-- If ingredient already exists, add quantity to its amount and exit the loop
if name == ingredient then
found_ingredient = true
recipe.ingredients[k] = {type = "item", name = ingredient, amount = amount + quantity}
break
end
end
-- Add ingredient!
if not found_ingredient then
table.insert(recipe.ingredients, {type = "item", name = ingredient, amount = quantity})
end
end
end
Factorio starts up correctly after this change. Could you apply this to your code? (Of course, you're free to change anything you like. I aimed for readability, there still is room for optimization!)