Replace calls to utils for factored out functions

This commit is contained in:
2025-03-30 23:14:40 +02:00
parent 7bee770482
commit a210fe27c6
4 changed files with 16 additions and 167 deletions

View File

@@ -2,105 +2,10 @@
local quickstack = require("Cyka.quickstack")
local utils = require("Cyka.utils")
-- There is actually no need to recurse deep
-- Because we can only have an item in the inventory open
-- And not an item in an item in the inventory
-- So in theory we only need to recurse 1 deep
---@param inventory Barotrauma.Inventory
---@param slots InventorySlot[]
---@param depth number
---@return InventorySlot[], string?
local function getMouseoverSlots(inventory, slots, depth)
slots = slots or {}
depth = depth or 0
if depth > 1 then return slots, nil end
local visualSlots = inventory.visualSlots
if not visualSlots then return nil, "Inventory has no visual slots" end
for i, visualSlot in ipairs(visualSlots) do
local item
local itemInventory
-- local err
local slot = inventory.slots[i]
if not slot then
-- MyModGlobal.debugPrint("Slot is not a valid slot")
goto continue
end
if #slot.items == 0 then
goto mouseover
end
item = slot.items[1]
if not item then
goto mouseover
end
itemInventory = item.OwnInventory
if not itemInventory then
goto mouseover
end
-- print("Before: " .. #slots)--
getMouseoverSlots(itemInventory, slots, depth + 1)
-- if err then
-- MyModGlobal.debugPrint(string.format("Error getting mouseover slots: %s", err))
-- end
-- print("After: " .. #slots)
::mouseover::
if visualSlot:MouseOn() then
slots[#slots + 1] = {
inventory = inventory,
slotIndex = i,
slot = slot
}
end
::continue::
end
return slots, nil
end
---@return InventorySlot[], string?
local function getInventorySlotsUnderCursor()
-- Make sure we have a controlled character
local controlledCharacter = Character.Controlled
if not controlledCharacter then return nil, "No controlled character" end
local inventory = controlledCharacter.Inventory
if not inventory then return nil, "No inventory" end
local mouseoverSlots, err = getMouseoverSlots(inventory)
if err then return mouseoverSlots, err end
local openContainers = quickstack.getOpenContainers()
for _, container in ipairs(openContainers) do
local containerInventories = container.OwnInventories
for containerInventory in containerInventories do
for i, visualSlot in ipairs(containerInventory.visualSlots) do
if visualSlot:MouseOn() then
local slot = containerInventory.slots[i]
mouseoverSlots[#mouseoverSlots + 1] = {
inventory = containerInventory,
slotIndex = i,
slot = slot
}
end
end
end
end
return mouseoverSlots, nil
end
local targetInventory = nil
local slotThrottle = {}
local function tryStackCursorItem()
local slots, err = getInventorySlotsUnderCursor()
local slots, err = utils.getSlotsUnderCursor()
if err then
-- MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err))
return
@@ -181,8 +86,7 @@ local function tryStackCursorItem()
end
local function setTargetInventory()
---@type InventorySlot[]
local slots, err = getInventorySlotsUnderCursor()
local slots, err = utils.getSlotsUnderCursor()
if err then
MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err))
return
@@ -224,5 +128,4 @@ end
return {
tryStackCursorItem = tryStackCursorItem,
setTargetInventory = setTargetInventory,
getInventorySlotsUnderCursor = getInventorySlotsUnderCursor
}

View File

@@ -146,7 +146,13 @@ local function tryStackFabricator(character)
-- dump(itemsOnSubmarine)
-- MyModGlobal.DumpTable(toGet)
local items = utils.enqueueInventory(bagItem.OwnInventory, {}, filter)
local items, _ = utils.enqueueAllOwnedItems({}, filter)
-- if err then
-- print(string.format("Error enqueueing all owned items: %s", err))
-- return
-- end
-- dump(items)
-- MyModGlobal.DumpTable(items)
-- TODO: This might explode... Oh well?
local inputInventory = fabricator.item.OwnInventories[1]
@@ -157,49 +163,6 @@ local function tryStackFabricator(character)
end
end
-- If we looked at all items we have on person and still are missing some
-- Look at all the items on the submarine
local hasAny = false
for _, itemInfo in pairs(toFind) do
if itemInfo.amount > 0 then
hasAny = true
break
end
end
if hasAny then
local submarine = character.Submarine
local found
if not submarine then
MyModGlobal.debugPrint("Submarine not found.")
goto done
end
for item in submarine.GetItems(false) do
if remaining == 0 then goto done end
for i, itemInfo in pairs(toFind) do
found = false
for _, prefab in ipairs(itemInfo.prefabs) do
-- MyModGlobal.debugPrint(string.format("Checking %s against %s", item.Prefab.Identifier.Value, prefab))
if tostring(item.Prefab.Identifier.Value) == prefab then
-- MyModGlobal.debugPrint(string.format("That'll do %s %s", item.Prefab.Identifier.Value, prefab))
items[#items + 1] = item
itemInfo.amount = itemInfo.amount - 1
found = true
break
end
end
if itemInfo.amount <= 0 then
-- MyModGlobal.debugPrint(string.format("Removing %s from toFind", itemInfo.prefabs[1]))
toFind[i] = nil
remaining = remaining - 1
end
if found then break end
end
end
::done::
end
local slot = -1
local previous = nil
for _, item in ipairs(items) do

View File

@@ -26,16 +26,15 @@ local function getSlots(inventory)
return slots
end
---@param from Barotrauma.ItemInventory
---@param slots InventorySlot[]
---@return table<InventorySlot, Barotrauma.Item[]>
local function getItemsPerSlot(from, slots)
local function getItemsPerSlot(slots)
-- How many items can we move to what slot
-- We don't yet know what can fit into what slot
---@type table<InventorySlot, Barotrauma.Item[]>
local movableBySlot = {}
-- Get all the items and then we will sort them by condition and shit
utils.enqueueInventory(from, {}, function(ititem, itemRef)
utils.enqueueAllPlayerItems({}, function(ititem, itemRef)
-- We don't want to take oxygen out of our diving suit to load our plasma cutter
-- Most loadable items have 1 capacity
-- But some have 2 or 3 (coil speargun)
@@ -108,17 +107,6 @@ local function tryReloadSlot(slot, preferMinCondition)
return
end
local character = Character.Controlled
if not character then
MyModGlobal.debugPrint("No character")
return
end
local characterInventory = character.Inventory
if not characterInventory then
MyModGlobal.debugPrint("No character inventory")
return
end
---@type InventorySlot[]
local slots = getSlots(inventory)
if #slots == 0 then
@@ -129,7 +117,7 @@ local function tryReloadSlot(slot, preferMinCondition)
-- dump(slots)
---@type table<InventorySlot, Barotrauma.Item[]>
local movableBySlot = getItemsPerSlot(characterInventory, slots)
local movableBySlot = getItemsPerSlot(slots)
-- MyModGlobal.debugPrint("Movable by slot:")
-- dump(movableBySlot)
@@ -186,16 +174,11 @@ local function tryReloadSlot(slot, preferMinCondition)
MyModGlobal.debugPrint(string.format("Moved %d items to load %s", numMoved, tostring(item.Prefab.Identifier.Value)))
end
---@param minCondition boolean Prefer items with lowest condition
---@param preferMinCondition boolean Prefer items with lowest condition
local function tryReloadCursorItem(preferMinCondition)
local slots, err = cursormacroer.getInventorySlotsUnderCursor()
local slots, err = utils.getSlotsUnderCursor()
if err then
-- MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err))
return
end
if not slots or #slots == 0 then
-- MyModGlobal.debugPrint("No items in slot")
MyModGlobal.debugPrint(string.format("Error getting slots under cursor: %s", err))
return
end

View File

@@ -106,7 +106,7 @@ local function tryUnloadSlot(slot)
end
local function tryUnloadCursorItem()
local slots, err = cursormacroer.getInventorySlotsUnderCursor()
local slots, err = utils.getSlotsUnderCursor()
if err then
-- MyModGlobal.debugPrint(string.format("Error getting inventory slot: %s", err))
return