using "OR" in recipes not "AND"

Place to get help with not working mods / modding interface.
Post Reply
ares0027
Long Handed Inserter
Long Handed Inserter
Posts: 65
Joined: Sun May 04, 2014 6:48 pm
Contact:

using "OR" in recipes not "AND"

Post by ares0027 »

so (i have no idea about lua and/or modding) i have found that a recipe is simply taking an ingredient (or multiple) and creating an output (or multiple) and it uses "AND" in that manner of speaking;

Code: Select all

ingredients = 
	{
      {"basic-inserter", 1},
      {"electronic-circuit", 2},
	  {"iron-plate", 10},
    },
so this recipe simply uses 1 basic-inserter AND 2 electronic-circuit AND 10 iron-plates. i am trying to create an assembly machine like player (uses lower level ingredients if it is necessary) so for the sake of argument i am looking for a way to convert this recipe to;

Code: Select all

ingredients = 
	{
      {"basic-inserter", 1},
      {"electronic-circuit", 2},
	  {"iron-plate", 10},
	  [OR] {"iron-ore",10"}
    },
is it possible? or is there way to create an assembly machine like this?

hoho
Filter Inserter
Filter Inserter
Posts: 675
Joined: Sat Jan 18, 2014 11:23 am
Contact:

Re: using "OR" in recipes not "AND"

Post by hoho »

The recipes list is essentially an array of ingredients. It's not really possible to add logic operations between them without modifying the base game on C++ level.

What if there are two different recipes for making iron plates, how would you choose between them?

It might be possible with a huge hack of traversing the recipe table and manually figuring out what can be made from what inside the "assembler" and I'm not even sure if that would be possible.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: using "OR" in recipes not "AND"

Post by FreeER »

It's not currently possible to do so in that manner, but you can actually have separate recipes that produce the same thing with different ingredients. Since the prototypes are written in a Lua file you can actually write a function to create those separate recipes, perhaps something like so (note I've barely tested this and it's not optimized at all sooo, keep that in mind):
code
test code
of course with lots of possible replacements it'd flood the recipe list a bit...

ares0027
Long Handed Inserter
Long Handed Inserter
Posts: 65
Joined: Sun May 04, 2014 6:48 pm
Contact:

Re: using "OR" in recipes not "AND"

Post by ares0027 »

FreeER wrote:It's not currently possible to do so in that manner, but you can actually have separate recipes that produce the same thing with different ingredients. Since the prototypes are written in a Lua file you can actually write a function to create those separate recipes, perhaps something like so (note I've barely tested this and it's not optimized at all sooo, keep that in mind):
code
test code
of course with lots of possible replacements it'd flood the recipe list a bit...

that is what i was dwelling on now. (i will also read and try to understand what section does what, especially the "table" part of the codes) but i have dug through some mods i have (dytech) and i think i am able to create a new menu/section and an assembler that crafts from that menu. but for each possible ingredient i think i have to create another recipe which results in a lot of (seemingly duplicate) recipes, which can create confusion, and/or beats the main purpose of the mod.


as i mentioned on my mod idea/request thread ( https://forums.factorio.com/forum/vie ... f=6&t=8512 ), if we take "Basic Inserter" recipe as example;

there should be;

A) An inserter recipe that requires;
1 Electronic Circuit
2 Iron Plates

B) Another insterter recipe that requires;
3 Copper Cables
4 Iron Plates

C) Yet another inserter recipe that requires (that results an extra copper cable or simply destroys it);
2 Copper Plates (Or if i can find a way 1,5x Copper Plates)
4 Iron Plates

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: using "OR" in recipes not "AND"

Post by FreeER »

ares0027 wrote:which can create confusion, and/or beats the main purpose of the mod.
Indeed. What could also be done is to simulate a crafter 'manually' by using a regular container and Lua code. You could even have a gui to select the recipes, it's alot more work but the only 'solution' that allows for this without crowding the recipe list(s).

the basic idea would be along these lines (with onbuiltentity, onrobotbuiltentity adding a crafterInfo table to glob.crafters and with checks for the entity being valid, and a gui being used to select the recipe)
Assumed table structure

Code: Select all

game.onevent(defines.events.ontick, function(event)
  for index, crafterInfo in pairs(glob.crafters) do
    if crafterInfo.recipe then
      local crafter = crafterInfo.entity
      local inventory = crafter.getinventory(defines.inventory.chest)
      local contents = inventory.getcontents()
      if checkIngredients(recipe, contents) then
        removeIngredients(inventory, recipe)
        insertProducts(inventory, recipe)
      end
    end
  end
end)

function checkIngredients(recipe, contents)
  for _, ingredientInfo in pairs(recipe.ingredients) do
    if not contents[ingredientInfo.name] or contents[ingredientInfo.name] < ingredientInfo.amount then
      return false
    end
  end
  return true
end

function removeIngredients(inventory, recipe)
  for _, ingredientInfo in pairs(recipe.ingredients) do
    inventory.remove{name=ingredientInfo.name, count=ingredientInfo.amount}
  end
end

function insertProducts(inventory, recipe)
  for _, productInfo in pairs(recipe.products) do
    inventory.insert{name=productInfo.name, count=productInfo.amount}
  end
end
You could even add energy use with some extra work...

ares0027
Long Handed Inserter
Long Handed Inserter
Posts: 65
Joined: Sun May 04, 2014 6:48 pm
Contact:

Re: using "OR" in recipes not "AND"

Post by ares0027 »

FreeER wrote:
ares0027 wrote:which can create confusion, and/or beats the main purpose of the mod.
Indeed. What could also be done is to simulate a crafter 'manually' by using a regular container and Lua code. You could even have a gui to select the recipes, it's alot more work but the only 'solution' that allows for this without crowding the recipe list(s).

the basic idea would be along these lines (with onbuiltentity, onrobotbuiltentity adding a crafterInfo table to glob.crafters and with checks for the entity being valid, and a gui being used to select the recipe)
Assumed table structure

Code: Select all

game.onevent(defines.events.ontick, function(event)
  for index, crafterInfo in pairs(glob.crafters) do
    if crafterInfo.recipe then
      local crafter = crafterInfo.entity
      local inventory = crafter.getinventory(defines.inventory.chest)
      local contents = inventory.getcontents()
      if checkIngredients(recipe, contents) then
        removeIngredients(inventory, recipe)
        insertProducts(inventory, recipe)
      end
    end
  end
end)

function checkIngredients(recipe, contents)
  for _, ingredientInfo in pairs(recipe.ingredients) do
    if not contents[ingredientInfo.name] or contents[ingredientInfo.name] < ingredientInfo.amount then
      return false
    end
  end
  return true
end

function removeIngredients(inventory, recipe)
  for _, ingredientInfo in pairs(recipe.ingredients) do
    inventory.remove{name=ingredientInfo.name, count=ingredientInfo.amount}
  end
end

function insertProducts(inventory, recipe)
  for _, productInfo in pairs(recipe.products) do
    inventory.insert{name=productInfo.name, count=productInfo.amount}
  end
end
You could even add energy use with some extra work...

you lost me after "a lot" part :D all i can do about coding is i can create a bat file to rename a file to another :D this was a mod request but no one showed any interest in it (i am not blaming anyone) and i have no idea about lua etc and it seems "a little" confusing for me :D and i really dont have time to start learning it but i will give it a try. thank you so much for all your replies.

Post Reply

Return to “Modding help”