Add Supplier def; generify cache logic

This commit is contained in:
Zenon Seth
2023-11-06 23:42:34 +00:00
parent 991499ed1e
commit 0657728644
19 changed files with 404 additions and 104 deletions

View File

@@ -74,4 +74,39 @@ function logistica.take_stack_from_mass_storage(stackToTake, network, collectorF
storageInv:set_list(MASS_STORAGE_LIST_NAME, storageList)
end
return false
end
-- attempts to insert the given itemstack in the network, returns how many items were inserted
function logistica.insert_item_in_network(itemstack, networkId)
local network = logistica.networks[networkId]
if not itemstack or not network then return 0 end
local workingStack = ItemStack(itemstack)
-- check demanders first
for hash, _ in pairs(network.demanders) do
local pos = minetest.get_position_from_hash(hash)
logistica.load_position(pos)
local taken = 0 -- logistica.try_to_give_item_to_demander(pos, workingStack)
local leftover = workingStack:get_count() - taken
if leftover <= 0 then return itemstack:get_count() end -- we took all items
workingStack:set_count(leftover)
end
-- check storages
local storages = {}
if itemstack:get_stack_max() <= 1 then
storages = network.item_storage
else
storages = network.mass_storage
end
for hash, _ in pairs(storages) do
local pos = minetest.get_position_from_hash(hash)
logistica.load_position(pos)
local taken = logistica.try_to_add_item_to_storage(pos, workingStack)
local leftover = workingStack:get_count() - taken
if leftover <= 0 then return itemstack:get_count() end -- we took all items
workingStack:set_count(leftover)
end
return itemstack:get_count() - workingStack:get_count()
end