Page 1 of 1

getting list of recipes with normal/expensive ingrediants

Posted: Mon Jun 25, 2018 8:07 am
by kingarthur
a have a script that loops thru all the recipes in the game and adds them to a list that is then looped thru to check for and replace a specific ingredient from a list with another. it works for all recipes that are just recipe.ingredients. if a recipe contains normal and expensive ingredients it does not work. from googling it seems that i needed to add "normal" to specify that but doesnt seem to have done anything


first part gets the list of recipes and second section loops thru and replaces the items

Code: Select all

local test_list = {}
local n_e_recipes = {}

for i, recipe in pairs(data.raw.recipe) do

	if recipe.ingredients then
		
		table.insert(test_list,recipe.name)
	
	elseif recipe.normal.ingredients then
	
		table.insert(n_e_recipes,recipe.name)

	end
end
	
for i, recipe_name in ipairs(test_list) do

	local ingredients = table.deepcopy(data.raw.recipe[recipe_name].ingredients)
	
	for k, ingredient in ipairs(ingredients) do
		
		for j,l in pairs(items_dict) do
		
			if ingredient.name == j then
			
				local recipeingredamount = (table.deepcopy(data.raw.recipe[recipe_name].ingredients[k].amount))
			
				data.raw.recipe[recipe_name].ingredients[k] = {l, recipeingredamount}
				
			end
		
		end
		
	end
	
end
this one should loop thru the normal.ingredients list

Code: Select all

for i, recipe_name in ipairs(n_e_recipes) do

	local ingredients = table.deepcopy(data.raw.recipe[recipe_name].normal.ingredients)
	
	for k, ingredient in ipairs(ingredients) do
		
		for j, item_name in ipairs(items_list) do
		
			if ingredient.name == item_name then
			
				local recipeingredamount = (table.deepcopy(data.raw.recipe[recipe_name].normal.ingredients[k].amount))
			
				data.raw.recipe[recipe_name].normal.ingredients[k] = {replacement_item, recipeingredamount}
				
			end
		
		end
		
	end
	
end
i cant tell if its falling to populate the n_e_recipes list or where else the issue is.

Re: getting list of recipes with normal/expensive ingrediants

Posted: Mon Jun 25, 2018 8:32 am
by Optera
I check if my arrays hold correct information with

Code: Select all

log(serpent.dump(array))
Sidenote: The version of serpent Factorio uses has issues with block and sometimes returns null even when there is data.
Dump is terrible to read for large arrays but it always contains correct data.

Re: getting list of recipes with normal/expensive ingrediants

Posted: Mon Jun 25, 2018 10:40 am
by darkfrei
I like to make the loop so:

Code: Select all

for i, recipe in pairs(data.raw.recipe) do
  for j, handler in pairs ({data.raw.recipe, data.raw.recipe.normal, data.raw.recipe.expensive}) do
    if handler and handler.ingredients then
      -- your code there
    end
  end
end
So if you haven't normal there is no problem, if you have it, it's also not a problem. Results will be done also in this loop.

Sometimes you have main_product, you can check it too.

Your

Code: Select all

amount = ingredients[k].amount
can be sometimes

Code: Select all

amount = ingredients[k][2]

Re: getting list of recipes with normal/expensive ingrediants

Posted: Tue Jun 26, 2018 1:25 pm
by Rseding91
Optera wrote:I check if my arrays hold correct information with

Code: Select all

log(serpent.dump(array))
Sidenote: The version of serpent Factorio uses has issues with block and sometimes returns null even when there is data.
Dump is terrible to read for large arrays but it always contains correct data.
The version of serpent that Factorio uses is the latest one available on the github so I'm not sure what you're talking about.

Re: getting list of recipes with normal/expensive ingrediants

Posted: Tue Jun 26, 2018 6:46 pm
by Optera
Rseding91 wrote:
Optera wrote:I check if my arrays hold correct information with

Code: Select all

log(serpent.dump(array))
Sidenote: The version of serpent Factorio uses has issues with block and sometimes returns null even when there is data.
Dump is terrible to read for large arrays but it always contains correct data.
The version of serpent that Factorio uses is the latest one available on the github so I'm not sure what you're talking about.
In 0.15 I had the effect of serpent.block showing nested tables partially as empty while serpent.dump correctly showed the all nested table values.
If the bug has since been fixed just ignore my statement.

Re: getting list of recipes with normal/expensive ingrediants

Posted: Wed Jun 27, 2018 1:47 am
by eradicator
He's probably talking about tables with self-references. Something like:

Code: Select all

local a = {1,2,3}
local b = {a,a}
print(serpent.block(b))
This would print something like {{1,2,3},nil} if i remember correctly, because by default the option to show self-references is off.

Re: getting list of recipes with normal/expensive ingrediants

Posted: Thu Jun 28, 2018 10:19 am
by DRY411S
darkfrei wrote:I like to make the loop so:

Code: Select all

for i, recipe in pairs(data.raw.recipe) do
  for j, handler in pairs ({data.raw.recipe, data.raw.recipe.normal, data.raw.recipe.expensive}) do
    if handler and handler.ingredients then
      -- your code there
    end
  end
end
So if you haven't normal there is no problem, if you have it, it's also not a problem. Results will be done also in this loop.

Sometimes you have main_product, you can check it too.

Your

Code: Select all

amount = ingredients[k].amount
can be sometimes

Code: Select all

amount = ingredients[k][2]

Nice!