Silica & Silicon

by brevven

Adds simple Silica and Silicon to the game, along with basic fiber optics. Compatible with Krastorio 2, Space Exploration, Bio Industries, and other mods. A standalone piece of BZ Mods With graphics by snouz.

Content
3 months ago
1.0 - 1.1
24.5K
Manufacturing

b Crash on loading with Industrial Revolution 2

3 years ago
(updated 3 years ago)

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!)

3 years ago

Hi. Thanks for your request. I've updated to be compatible.

I also made a couple minor updates to make the fiber optics a little smoother with IR2, though it obviously doesn't fit the theme very well for the circuit network...

Other than that it doesn't do very much, as silica and silicon are both already part of IR2.

3 years ago

Thanks for the fix! Actually, I only noticed the crash with IR2, but the same thing could occur with any other mod, so it may even be useful if IR2 is not active. :-)

New response