For example consider the gun turrets in this chest:
Code: Select all
-- doesn't handle uncompressed stacks without a loop
-- doesn't handle damaged vs undamaged items
existing_stack.transfer_stack(inventory.find_item_stack(existing_stack.name))
Something like:
LuaInventory.fill_stack(stack, item_name) → boolean
Fill stack with items from this inventory
Parameters
stack :: LuaItemStack Stack to fill, may be empty
item_name :: string Item to fill stack with. If stack is non-empty, must be the same as stack.name
Return value
true if stack is now full
Code: Select all
function fill_stack(self, stack, item_name)
local stack_size = game.item_prototypes[item_name].stack_size
if stack.valid_for_read then
assert(item_name == stack.name)
if stack.count == stack_size then
return true -- full already, do nothing
end
end -- else empty stack
for i=1,#self do
stack.transfer_stack(self[i]) -- no-op if not stackable
if stack.valid_for_read and stack.count >= stack_size then
return true -- full, finished
end
end
return false -- not full
end
Code: Select all
inventory.find_item_stack('gun-turret') -- as now, don't care, whichever comes first
inventory.find_item_stack('gun-turret', true) -- only find damaged items
inventory.find_item_stack('gun-turret', false) -- only find undamaged items
The full use case was to try and fill as many of the filtered (blue, as per LuaInventory.get_filter(slot_index)) slots in a wagon as possible pulling form another container. Probably the entire thing is too specific.