Created
October 7, 2022 15:54
-
-
Save akarnokd/3b0569bf6e3f2df412597a842579a91c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function transfer(sourceInventory, destinationInventory, recipe, alimit, tag) | |
if not destinationInventory then return end | |
local limits = { } | |
if recipe then | |
for _, ingredient in pairs(recipe.ingredients) do | |
if alimit then | |
limits[ingredient.name] = math.min(alimit, ingredient.amount + 1) | |
else | |
limits[ingredient.name] = ingredient.amount + 1 | |
end | |
end | |
end | |
local content = sourceInventory.get_contents() | |
for name, count in pairs(content) do | |
local limit = limits[name] or alimit | |
local toInsert = count | |
if limit then | |
local currentCount = destinationInventory.get_item_count(name) | |
if currentCount + toInsert > limit then | |
toInsert = limit - currentCount | |
end | |
end | |
if toInsert > 0 then | |
inserted = destinationInventory.insert({ name = name, count = toInsert }) | |
if inserted > 0 then | |
--log(tag .. " | Transfer " .. name .. " x " .. toInsert .. " (" .. inserted .. ") " .. tag) | |
sourceInventory.remove({ name = name, count = inserted }) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment