Page 1 of 1

Copying recipes script

Posted: Sun Aug 30, 2015 12:00 am
by Syriusz
Hello, I have problem with script that supposed to copy recipes that have less than 3 ingredients, then change them a little and add them to recipe list. I am out of ideas how to fix it... I do not have any previous experience with Lua, so probably its noob mistake or so. Here is code:

Code: Select all

 
  function tablelength(T)  ---it's to count how many ingredients is in recipe
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end


 for _, recipe in pairs(data.raw.recipe) do
	if recipe.category ~= "pneumatic" and recipe.category ~="smelting" then
		if tablelength(recipe.ingredients) < 3 then 
			local rec = recipe
			local energy = recipe.energy_required
			rec.name= recipe.name.."-P"
			rec.ingredients[3]={type="fluid", name="compressed-air", amount=energy}
			rec.category="pneumatic"
			data:extend(rec)
		end
    end
end
Thanks in advance for any help!

Re: Copying recipes script

Posted: Sun Aug 30, 2015 2:27 am
by Rseding91
Try this:

Code: Select all

local excludedRecipeCategories = {pneumatic = true, smelting = true}

for k,recipe in pairs(data.raw.recipe) do
  if excludedRecipeCategories[recipe.category] == nil and #recipe.ingredients < 3 then
    local newRecipe = util.table.deepcopy(recipe)
    newRecipe.name = newRecipe.name .. "-P"
    newRecipe.ingredients[3] = {type="fluid", name="compressed-air", amount=newRecipe.energy_required}
    newRecipe.category = "pneumatic"
    data.raw.recipe[newRecipe.name] = newRecipe
  end
end

Re: Copying recipes script

Posted: Sun Aug 30, 2015 3:01 pm
by Syriusz
It looks so much better than mine, thanks!
Some recipes does not have energy_required and I forgot about it, so it took me a while to repair it by adding:

Code: Select all

if newRecipe.ingredients[3].amount==nil then
newRecipe.ingredients[3].amount=0.5
end
It works now as I wanted, and I learned much from your code! Great thanks!