Fix up some more minor issues with stacker

This commit is contained in:
2025-03-30 15:55:38 +02:00
parent e1b658721e
commit 24848cfe5f
2 changed files with 52 additions and 40 deletions

View File

@@ -1,6 +1,7 @@
local quickstack = require("Cyka.quickstack") local quickstack = require("Cyka.quickstack")
local utils = require("Cyka.utils")
---@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.Item|nil ---@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.InventorySlot|nil
local function getInventorySlotUnderCursor() local function getInventorySlotUnderCursor()
-- Make sure we have a controlled character -- Make sure we have a controlled character
local controlledCharacter = Character.Controlled local controlledCharacter = Character.Controlled
@@ -13,11 +14,7 @@ local function getInventorySlotUnderCursor()
-- Check if mouse is over this slot -- Check if mouse is over this slot
if visualSlot:MouseOn() then if visualSlot:MouseOn() then
local slot = charInventory.slots[i] local slot = charInventory.slots[i]
local item = nil return visualSlot, charInventory, slot
if #slot.items > 0 then
item = slot.items[1]
end
return visualSlot, charInventory, item
end end
end end
end end
@@ -29,11 +26,7 @@ local function getInventorySlotUnderCursor()
for i, visualSlot in ipairs(itemInv.visualSlots) do for i, visualSlot in ipairs(itemInv.visualSlots) do
if visualSlot:MouseOn() then if visualSlot:MouseOn() then
local slot = itemInv.slots[i] local slot = itemInv.slots[i]
local item = nil return visualSlot, itemInv, slot
if #slot.items > 0 then
item = slot.items[1]
end
return visualSlot, itemInv, item
end end
end end
end end
@@ -45,11 +38,7 @@ local function getInventorySlotUnderCursor()
for i, visualSlot in ipairs(itemInv.visualSlots) do for i, visualSlot in ipairs(itemInv.visualSlots) do
if visualSlot:MouseOn() then if visualSlot:MouseOn() then
local slot = itemInv.slots[i] local slot = itemInv.slots[i]
local slotItem = nil return visualSlot, itemInv, slot
if #slot.items > 0 then
slotItem = slot.items[1]
end
return visualSlot, itemInv, slotItem
end end
end end
end end
@@ -61,14 +50,14 @@ end
local function tryStackCursorItem() local function tryStackCursorItem()
MyModGlobal.debugPrint("Shift + Left Click detected") MyModGlobal.debugPrint("Shift + Left Click detected")
local visualSlot, itemInv, item = getInventorySlotUnderCursor() local visualSlot, itemInv, slot = getInventorySlotUnderCursor()
if not visualSlot or not itemInv or not item then if not visualSlot or not itemInv or not slot then
MyModGlobal.debugPrint(string.format("No inventory slot or item found")) MyModGlobal.debugPrint(string.format("No inventory slot or item found"))
return return
end end
MyModGlobal.debugPrint(string.format("Visual slot: %s", tostring(visualSlot))) -- MyModGlobal.debugPrint(string.format("Visual slot: %s", tostring(visualSlot)))
MyModGlobal.debugPrint(string.format("Item inventory: %s", tostring(itemInv))) -- MyModGlobal.debugPrint(string.format("Item inventory: %s", tostring(itemInv)))
MyModGlobal.debugPrint(string.format("Item: %s", tostring(item))) -- MyModGlobal.debugPrint(string.format("Inventory slot: %s", tostring(slot)))
local controlledCharacter = Character.Controlled local controlledCharacter = Character.Controlled
if not controlledCharacter then if not controlledCharacter then
@@ -83,10 +72,15 @@ local function tryStackCursorItem()
end end
itemTree = quickstack.sortItemTree(itemTree) itemTree = quickstack.sortItemTree(itemTree)
err = quickstack.tryMoveItem(item, itemTree, true) local itemsToMove = utils.enqueueSlot(slot)
if err then for _, item in ipairs(itemsToMove) do
MyModGlobal.debugPrint(string.format("Error moving item: %s", err)) MyModGlobal.debugPrint(string.format("Enqueued item: %s", tostring(item)))
return end
-- MyModGlobal.debugPrint(string.format("Enqueued %d items from the inventory slot", #itemsToMove))
local errors = quickstack.tryMoveItems(itemsToMove, itemTree)
for _, error in ipairs(errors) do
MyModGlobal.debugPrint(string.format("Error moving item: %s", error))
end end
end end

View File

@@ -112,19 +112,19 @@ local function tryMoveItem(item, itemTree, force)
-- MyModGlobal.debugPrint(string.format("Attempting to move item: %s", item.Prefab.Identifier.Value)) -- MyModGlobal.debugPrint(string.format("Attempting to move item: %s", item.Prefab.Identifier.Value))
-- MyModGlobal.DumpTable(location) -- MyModGlobal.DumpTable(location)
if item.Condition <= item.MaxCondition then
-- MyModGlobal.debugPrint(string.format("Item %s has condition %.2f(<=%.2f), not stacking", item.Prefab.Identifier.Value, item.Condition, item.MaxCondition))
-- Don't even TRY to stack them
location = nil
end
local moved = false local moved = false
if location then if location then
-- First try to move to existing stacks -- First try to move to existing stacks
for _, itemLocation in ipairs(location) do for _, itemLocation in ipairs(location) do
if itemLocation.maxFits > 0 then -- We cannot stack items with decreased condition
local canBePut = itemLocation.inventory.CanBePutInSlot(item.Prefab, itemLocation.slotIndex, item.Condition)
-- MyModGlobal.debugPrint(string.format("Can be put in slot %d: %s", itemLocation.slotIndex, tostring(canBePut)))
if itemLocation.maxFits > 0 and canBePut then
moved = moved or itemLocation.inventory.TryPutItem(item, itemLocation.slotIndex, false, true, nil) moved = moved or itemLocation.inventory.TryPutItem(item, itemLocation.slotIndex, false, true, nil)
if moved then
itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex) itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex)
end
-- if moved then -- if moved then
-- MyModGlobal.debugPrint(string.format("Moved item to existing stack at slot index: %d", itemLocation .slotIndex)) -- MyModGlobal.debugPrint(string.format("Moved item to existing stack at slot index: %d", itemLocation .slotIndex))
-- else -- else
@@ -139,8 +139,20 @@ local function tryMoveItem(item, itemTree, force)
if not moved then if not moved then
-- MyModGlobal.debugPrint("No existing stacks found, trying empty slots...") -- MyModGlobal.debugPrint("No existing stacks found, trying empty slots...")
for _, itemLocation in ipairs(itemTree['empty']) do for _, itemLocation in ipairs(itemTree['empty']) do
moved = moved or itemLocation.inventory.TryPutItem(item, itemLocation.slotIndex, true, true, nil) local maxFits = itemLocation.maxFits
-- These empty slots are not guranteed to be empty, ironically
-- After we insert an item into one it's no longer empty
-- But it still is in the empty table
-- So we want to make sure we can insert our item
-- Into the maybe empty slots
itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex) itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex)
if maxFits > 0 then
-- MyModGlobal.debugPrint(string.format("Trying to move item to empty slot at index: %d", itemLocation.slotIndex))
moved = moved or itemLocation.inventory.TryPutItem(item, itemLocation.slotIndex, true, false, nil)
if moved then
itemLocation.maxFits = itemLocation.inventory.HowManyCanBePut(item.Prefab, itemLocation.slotIndex)
end
-- if moved then -- if moved then
-- MyModGlobal.debugPrint(string.format("Moved item to empty slot at index: %d", itemLocation.slotIndex)) -- MyModGlobal.debugPrint(string.format("Moved item to empty slot at index: %d", itemLocation.slotIndex))
-- else -- else
@@ -148,6 +160,7 @@ local function tryMoveItem(item, itemTree, force)
-- end -- end
end end
end end
end
-- If we still can not move the item give up -- If we still can not move the item give up
if not moved then if not moved then
@@ -250,7 +263,12 @@ local function quickStackItems(character)
return return
end end
-- local inventory = character.Inventory local inventory = character.Inventory
if not inventory or not inventory.slots then
MyModGlobal.debugPrint("Character has no inventory")
return
end
-- for i, slot in ipairs(inventory.slots) do -- for i, slot in ipairs(inventory.slots) do
-- if #slot.items > 0 then -- if #slot.items > 0 then
-- local item = slot.items[1] -- local item = slot.items[1]