Implement auto filling fabricators

This commit is contained in:
2025-03-30 00:55:51 +01:00
parent 5f7a30fce1
commit 7e40f7ec3d

View File

@@ -28,7 +28,7 @@ print(MOD_NAME .. " v" .. MOD_VERSION .. " loaded!")
---@param table table ---@param table table
---@param depth number? ---@param depth number?
function DumpTable(table, depth) local function DumpTable(table, depth)
if depth == nil then if depth == nil then
depth = 0 depth = 0
end end
@@ -527,23 +527,53 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
return return
end end
local recipe, err = getSelectedRecipeRequirements(fabricator.fabricator) local recipe
recipe, err = getSelectedRecipeRequirements(fabricator.fabricator)
if err then if err then
print(string.format("Error getting selected recipe requirements: %s", err)) print(string.format("Error getting selected recipe requirements: %s", err))
end end
DumpTable(recipe) DumpTable(recipe)
local toGet = recipe.requiredItems -- TODO: Maybe make it so every press cycles the input
-- For recipes that have multiple prefabs
-- But then again what if it has 3 items with 4 prefabs each..
-- Is that 4 iterations or 3*4 iterations?
local toFind = recipe.requiredItems
---@type Barotrauma.Item[]
local toGet = {}
---@type fun(item: Barotrauma.Item): boolean ---@type fun(item: Barotrauma.Item): boolean
local filter = function(item) local filter = function(item)
if item.Prefab.Identifier == recipe.targetItem.identifier then local found = false
return true if #toFind == 0 then return false end
-- toFind are all items we need to find
for i, itemInfo in ipairs(toFind) do
-- prefabs are all items that satisfy the requirements
for _, prefab in ipairs(itemInfo.prefabs) do
if item.Prefab.Identifier == prefab then
toGet[#toGet + 1] = item
itemInfo.amount = itemInfo.amount - 1
found = true
break
end
end
if itemInfo.amount <= 0 then
toFind[i] = nil
end
if found then break end
end end
return false return found
end end
local items = enqueueInventory(bagItem.OwnInventory, {}, filter) local items = enqueueInventory(bagItem.OwnInventory, {}, filter)
-- TODO: This might explode... Oh well?
local inputInventory = fabricator.item.OwnInventories[1]
local slot = -1
local previous = nil
for _, item in ipairs(items) do
if previous ~= item.Prefab.Identifier then slot = slot + 1 end
inputInventory.TryPutItem(item, slot, false, true, nil)
previous = item.Prefab.Identifier
end
DumpTable(items) DumpTable(items)
end, Hook.HookMethodType.After) end, Hook.HookMethodType.After)