Now unload items to nearest index on the grid

Instead of the array
This commit is contained in:
2025-03-30 20:33:04 +02:00
parent 7dc1ed9968
commit a574e56c1c
2 changed files with 25 additions and 5 deletions

View File

@@ -68,6 +68,7 @@ LuaUserData.RegisterType(
'System.Collections.Immutable.ImmutableArray`1[[Barotrauma.Items.Components.ItemContainer+SlotRestrictions, Barotrauma]]') 'System.Collections.Immutable.ImmutableArray`1[[Barotrauma.Items.Components.ItemContainer+SlotRestrictions, Barotrauma]]')
LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.Items.Components.ItemContainer'], 'slotRestrictions') LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.Items.Components.ItemContainer'], 'slotRestrictions')
LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.ItemInventory'], 'slots') LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.ItemInventory'], 'slots')
LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.ItemInventory'], 'slotsPerRow')
LuaUserData.MakeFieldAccessible(Descriptors["Barotrauma.CharacterInventory"], "slots") LuaUserData.MakeFieldAccessible(Descriptors["Barotrauma.CharacterInventory"], "slots")
LuaUserData.RegisterType("Barotrauma.Store") LuaUserData.RegisterType("Barotrauma.Store")
LuaUserData.RegisterType("Barotrauma.GUIComponent") LuaUserData.RegisterType("Barotrauma.GUIComponent")

View File

@@ -64,19 +64,38 @@ local function tryUnloadSlot(slot)
-- print("Before sorting:") -- print("Before sorting:")
-- dump(nearbySlots) -- 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) table.sort(nearbySlots, function(a, b)
local distanceA = math.abs(a.slotIndex - slot.slotIndex) local ax, ay = getGridPos(a.slotIndex)
local distanceB = math.abs(b.slotIndex - slot.slotIndex) local bx, by = getGridPos(b.slotIndex)
return distanceA < distanceB
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) end)
-- print("After sorting:") -- print(string.format("Current slot: %d at (%d, %d)", slot.slotIndex, slotx, sloty))
-- dump(nearbySlots)
for _, iitem in ipairs(toUnload) do for _, iitem in ipairs(toUnload) do
for _, nearbySlot in ipairs(nearbySlots) do for _, nearbySlot in ipairs(nearbySlots) do
local canAccept = nearbySlot.inventory.CanBePutInSlot(iitem.Prefab, nearbySlot.slotIndex) local canAccept = nearbySlot.inventory.CanBePutInSlot(iitem.Prefab, nearbySlot.slotIndex)
if canAccept then if canAccept then
local moved = nearbySlot.inventory.TryPutItem(iitem, nearbySlot.slotIndex, true, false, nil) 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 if moved then break end
end end
end end