diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index cdb3e5d..2496403 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -68,6 +68,7 @@ LuaUserData.RegisterType( 'System.Collections.Immutable.ImmutableArray`1[[Barotrauma.Items.Components.ItemContainer+SlotRestrictions, Barotrauma]]') LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.Items.Components.ItemContainer'], 'slotRestrictions') LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.ItemInventory'], 'slots') +LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.ItemInventory'], 'slotsPerRow') LuaUserData.MakeFieldAccessible(Descriptors["Barotrauma.CharacterInventory"], "slots") LuaUserData.RegisterType("Barotrauma.Store") LuaUserData.RegisterType("Barotrauma.GUIComponent") diff --git a/QuickStackToBag/Lua/Cyka/quickunload.lua b/QuickStackToBag/Lua/Cyka/quickunload.lua index 93f3e2f..92f26b2 100644 --- a/QuickStackToBag/Lua/Cyka/quickunload.lua +++ b/QuickStackToBag/Lua/Cyka/quickunload.lua @@ -64,19 +64,38 @@ local function tryUnloadSlot(slot) -- print("Before sorting:") -- dump(nearbySlots) + local slotsPerRow = slot.inventory.slotsPerRow or 1 + local getGridPos = function(slotIndex) + local x = slotIndex % slotsPerRow + local y = math.floor(slotIndex / slotsPerRow) + return x, y + end + + -- We are offsetting here by 1 because the backend uses 0-indexed slots + -- And the lua uses 1-indexed slots + -- We are trying to match the backend behavior for sorting + local slotx, sloty = getGridPos(slot.slotIndex - 1) + -- print(string.format("Slot position %d: %d, %d", slot.slotIndex, slotx, sloty)) table.sort(nearbySlots, function(a, b) - local distanceA = math.abs(a.slotIndex - slot.slotIndex) - local distanceB = math.abs(b.slotIndex - slot.slotIndex) - return distanceA < distanceB + local ax, ay = getGridPos(a.slotIndex) + local bx, by = getGridPos(b.slotIndex) + + local distA = math.max(math.abs(ax - slotx), math.abs(ay - sloty)) + local distB = math.max(math.abs(bx - slotx), math.abs(by - sloty)) + + if distA == distB then + return a.slotIndex < b.slotIndex + end + return distA < distB end) - -- print("After sorting:") - -- dump(nearbySlots) + -- print(string.format("Current slot: %d at (%d, %d)", slot.slotIndex, slotx, sloty)) for _, iitem in ipairs(toUnload) do for _, nearbySlot in ipairs(nearbySlots) do local canAccept = nearbySlot.inventory.CanBePutInSlot(iitem.Prefab, nearbySlot.slotIndex) if canAccept then local moved = nearbySlot.inventory.TryPutItem(iitem, nearbySlot.slotIndex, true, false, nil) + -- print(string.format("Moved item %s to slot %d", iitem.Name, nearbySlot.slotIndex)) if moved then break end end end