How to find out the ingredients an assembling machine is missing?

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
tomkcook
Burner Inserter
Burner Inserter
Posts: 14
Joined: Mon Feb 19, 2018 1:30 pm
Contact:

How to find out the ingredients an assembling machine is missing?

Post by tomkcook »

Once I've got a reference to an assembling machine, is there a way to find out what ingredients it needs to craft its current recipe that it doesn't already have enough of?

I realise "enough" is a rather difficult concept here, as an inserter can put half a dozen of something into a machine and call it enough while a miniloader from the miniloaders mod can put 200 of the same item into the machine before it thinks that is "enough". I'm not really fussy about which of these definitions is used.

To put it another way, if I had an assembling machine and a box with all the ingredients for the current recipe, and an inserter putting the items from the box into the assembling machine, is there a way in Lua code to predict what item the inserter will put into the assembling machine next?

DaleStan
Filter Inserter
Filter Inserter
Posts: 368
Joined: Mon Jul 09, 2018 2:40 am
Contact:

Re: How to find out the ingredients an assembling machine is missing?

Post by DaleStan »

I think this can, in most cases, be done by leveraging existing mods.

For the inserter version, use a recipe combinator from Crafting Combinator to convert from the selected recipe to the required ingredients, multiply by -2, and add the inventory as read by Inventory Sensor. Negative numbers represent ingredients to be loaded. The inserter will load either 'to the the first available ingredient slot' or 'from the first available chest slot'.

For the loader version, again use a recipe combinator to get the ingredients, feed that to an each/each=>each arithmetic combinator, and feed that into a stack combinator, to find out how big each stack is.

There are special cases around recipes that take more than a full stack of their ingredients, and there are probably plenty of optimization opportunities. I know Stack Combinator is not the most UPS-friendly device in the world, but I believe that comes from needing to be able to support arbitrary collections of input signals, instead of a limited collection of relatively small values.

tomkcook
Burner Inserter
Burner Inserter
Posts: 14
Joined: Mon Feb 19, 2018 1:30 pm
Contact:

Re: How to find out the ingredients an assembling machine is missing?

Post by tomkcook »

Thanks! I hadn't thought of trying to do this with an inventory sensor.

I've already crossed the bridge of making a custom version of the recipe combinator so was trying to achieve this with a custom crafting combinator.

From what you've said, it should be possible to use:

Code: Select all

local recipe = assembler.get_recipe()
local inventory = assembler.get_inventory(defines.inventory.assembling_machine_input)
local outputs = {}
local signal_index = 1
for ii, ing in pairs(recipe.ingredients) do
    local count = ing.amount - inventory.get_item_count(ing.name)
    if count > 0 then
        table.insert(outputs, {
            signal = { type='item', name='ing.name' },
            count = count,
            index = signal_index
        })
        signal_index = signal_index + 1
    end
end
Or words to that general effect - there are probably mistakes above.

Post Reply

Return to “Modding discussion”