getting list of recipes with normal/expensive ingrediants

Place to get help with not working mods / modding interface.
Post Reply
kingarthur
Smart Inserter
Smart Inserter
Posts: 1459
Joined: Sun Jun 15, 2014 11:39 am
Contact:

getting list of recipes with normal/expensive ingrediants

Post 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.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: getting list of recipes with normal/expensive ingrediants

Post 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.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: getting list of recipes with normal/expensive ingrediants

Post 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]

Rseding91
Factorio Staff
Factorio Staff
Posts: 13209
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: getting list of recipes with normal/expensive ingrediants

Post 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.
If you want to get ahold of me I'm almost always on Discord.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: getting list of recipes with normal/expensive ingrediants

Post 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.

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: getting list of recipes with normal/expensive ingrediants

Post 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.

User avatar
DRY411S
Filter Inserter
Filter Inserter
Posts: 727
Joined: Sun Mar 13, 2016 9:48 am
Contact:

Re: getting list of recipes with normal/expensive ingrediants

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

Post Reply

Return to “Modding help”