Minor fixes in reloader

This commit is contained in:
2025-03-30 21:43:31 +02:00
parent 273cbcff82
commit 068f72082a

View File

@@ -10,9 +10,14 @@ local dump = require("Cyka.dump")
-- So we will just hardcode tools and their whitelisted magazines
---@type table<string, table<string, boolean>>
local LOAD_MAP = {
plasmacutter = {
oxygentank = true
}
plasmacutter = { oxygentank = true },
weldingtool = { weldingfueltank = true },
revolver = { revolverround = true },
harpooncoilrifle = {
spear = true,
batterycell = true
},
anechoicdivingsuit = { oxygentank = true, },
}
---@param inventory Barotrauma.ItemInventory
@@ -98,6 +103,7 @@ local function tryReloadSlot(slot, preferMinCondition)
MyModGlobal.debugPrint("No item in slot")
return
end
MyModGlobal.debugPrint(string.format("Reloading item %s", item.Prefab.Identifier.Value))
local inventory = item.OwnInventory
if not inventory then
MyModGlobal.debugPrint("Item has no own inventory")
@@ -117,6 +123,10 @@ local function tryReloadSlot(slot, preferMinCondition)
---@type InventorySlot[]
local slots = getSlots(inventory)
if #slots == 0 then
MyModGlobal.debugPrint("No slots")
return
end
-- MyModGlobal.debugPrint("Slots:")
-- dump(slots)
@@ -137,86 +147,34 @@ local function tryReloadSlot(slot, preferMinCondition)
-- We don't really want to load those
for _, items in pairs(movableBySlot) do
table.sort(items, function(a, b)
if a.ConditionPercentage == 0 and b.ConditionPercentage ~= 0 then
if a.Condition == 0 and b.Condition ~= 0 then
return false
elseif a.ConditionPercentage ~= 0 and b.ConditionPercentage == 0 then
elseif a.Condition ~= 0 and b.Condition == 0 then
return true
elseif preferMinCondition then
return a.ConditionPercentage < b.ConditionPercentage
return a.Condition < b.Condition
else
return a.ConditionPercentage > b.ConditionPercentage
return a.Condition > b.Condition
end
end)
end
-- dump(movableBySlot)
local numMoved = 0
for inventorySlot, items in pairs(movableBySlot) do
for _, ititem in ipairs(items) do
local moved = inventorySlot.inventory.TryPutItem(ititem, inventorySlot.slotIndex, false, true, nil)
-- When the slot is full no more will be able to be moved
-- And tat that point we're done with that slot
if not moved then break end
if permissibleItems[tostring(ititem.Prefab.Identifier.Value)] then
local moved = inventorySlot.inventory.TryPutItem(ititem, inventorySlot.slotIndex, false, true, nil)
-- When the slot is full no more will be able to be moved
-- And tat that point we're done with that slot
if not moved then break end
numMoved = numMoved + 1
else
MyModGlobal.debugPrint(string.format("Not permissible: %s", tostring(ititem.Prefab.Identifier.Value)))
end
end
end
-- -- Where can we put our toUnload items?
-- local nearbySlots = findSlotsThat(slot.inventory, function(islot)
-- local isEmpty = #islot.slot.items == 0
-- if isEmpty then return true end
-- for _, prefab in ipairs(toUnloadByPrefab) do
-- local canAccept = islot.inventory.CanBePutInSlot(prefab, islot.slotIndex)
-- if canAccept then return true end
-- end
-- return false
-- end)
-- -- print("Before sorting:")
-- -- dump(nearbySlots)
-- -- Some inventories don't have slots per row, like the player inventory
-- local slotsPerRow = 900
-- local ok, err = pcall(function()
-- slotsPerRow = slot.inventory.slotsPerRow
-- end)
-- if not ok then
-- MyModGlobal.debugPrint(string.format("Error getting slots per row: %s", err))
-- end
-- 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 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(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
-- end
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