Fix up reloading
This commit is contained in:
@@ -16,14 +16,13 @@ local LOAD_MAP = require("Cyka.quickreload_loadmap")
|
|||||||
---@param inventory Barotrauma.ItemInventory
|
---@param inventory Barotrauma.ItemInventory
|
||||||
---@return InventorySlot[]
|
---@return InventorySlot[]
|
||||||
local function getSlots(inventory)
|
local function getSlots(inventory)
|
||||||
|
---@type InventorySlot[]
|
||||||
local slots = {}
|
local slots = {}
|
||||||
|
---@type Barotrauma.Inventory.ItemSlot[]
|
||||||
local inventorySlots = inventory.slots
|
local inventorySlots = inventory.slots
|
||||||
for i, inventorySlot in ipairs(inventorySlots) do
|
for i, _ in ipairs(inventorySlots) do
|
||||||
slots[#slots + 1] = {
|
local invSlot = MyModGlobal.InventorySlot.new(inventory, i)
|
||||||
inventory = inventory,
|
slots[#slots + 1] = invSlot
|
||||||
slotIndex = i - 1,
|
|
||||||
slot = inventorySlot
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
return slots
|
return slots
|
||||||
end
|
end
|
||||||
@@ -36,28 +35,33 @@ local function getItemsPerSlot(slots)
|
|||||||
---@type table<InventorySlot, Barotrauma.Item[]>
|
---@type table<InventorySlot, Barotrauma.Item[]>
|
||||||
local movableBySlot = {}
|
local movableBySlot = {}
|
||||||
-- Get all the items and then we will sort them by condition and shit
|
-- Get all the items and then we will sort them by condition and shit
|
||||||
utils.enqueueAllPlayerItems({}, function(ititem, itemRef)
|
utils.enqueuePlayerItems({
|
||||||
-- We don't want to take oxygen out of our diving suit to load our plasma cutter
|
recurse = true,
|
||||||
-- Most loadable items have 1 capacity
|
loadRefs = true,
|
||||||
-- But some have 2 or 3 (coil speargun)
|
itemPredicate = function(ititem, itemRef)
|
||||||
if itemRef and itemRef.inventory and itemRef.inventory.Capacity < 4 then
|
MyModGlobal.debugPrint(string.format("Checking item: %s", tostring(ititem)))
|
||||||
-- MyModGlobal.debugPrint(string.format("Skipping small inventory %s", tostring(itemRef.inventory)))
|
-- 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)
|
||||||
|
if itemRef and itemRef.inventory and itemRef.inventory.Capacity < 4 then
|
||||||
|
-- MyModGlobal.debugPrint(string.format("Skipping small inventory %s", tostring(itemRef.inventory)))
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
-- MyModGlobal.debugPrint("Checking item:")
|
||||||
|
-- dump(slots)
|
||||||
|
-- MyModGlobal.debugPrint(ititem.Prefab.Identifier.Value)
|
||||||
|
for _, inventorySlot in ipairs(slots) do
|
||||||
|
local canMove = inventorySlot.inventory.CanBePutInSlot(ititem, inventorySlot.slotIndex1 - 1)
|
||||||
|
-- MyModGlobal.debugPrint(string.format("Can move to slot %d: %s", inventorySlot.slotIndex, tostring(canMove)))
|
||||||
|
if canMove then
|
||||||
|
movableBySlot[inventorySlot] = movableBySlot[inventorySlot] or {}
|
||||||
|
movableBySlot[inventorySlot][#movableBySlot[inventorySlot] + 1] = ititem
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
-- MyModGlobal.debugPrint("Checking item:")
|
})
|
||||||
-- dump(slots)
|
|
||||||
-- MyModGlobal.debugPrint(ititem.Prefab.Identifier.Value)
|
|
||||||
for _, inventorySlot in ipairs(slots) do
|
|
||||||
local canMove = inventorySlot.inventory.CanBePutInSlot(ititem, inventorySlot.slotIndex)
|
|
||||||
-- MyModGlobal.debugPrint(string.format("Can move to slot %d: %s", inventorySlot.slotIndex, tostring(canMove)))
|
|
||||||
if canMove then
|
|
||||||
movableBySlot[inventorySlot] = movableBySlot[inventorySlot] or {}
|
|
||||||
movableBySlot[inventorySlot][#movableBySlot[inventorySlot] + 1] = ititem
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end, true)
|
|
||||||
return movableBySlot
|
return movableBySlot
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -93,11 +97,11 @@ local function printPermissibleItems(movableBySlot)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param slot InventorySlot
|
---@param invSlot InventorySlot
|
||||||
---@param preferMinCondition boolean
|
---@param preferMinCondition boolean
|
||||||
local function tryReloadSlot(slot, preferMinCondition)
|
local function tryReloadSlot(invSlot, preferMinCondition)
|
||||||
---@type Barotrauma.Item
|
---@type Barotrauma.Item
|
||||||
local item = slot.slot.items[1]
|
local item = invSlot.item
|
||||||
if not item then
|
if not item then
|
||||||
MyModGlobal.debugPrint("No item in slot")
|
MyModGlobal.debugPrint("No item in slot")
|
||||||
return
|
return
|
||||||
@@ -115,13 +119,10 @@ local function tryReloadSlot(slot, preferMinCondition)
|
|||||||
MyModGlobal.debugPrint("No slots")
|
MyModGlobal.debugPrint("No slots")
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
-- MyModGlobal.debugPrint("Slots:")
|
|
||||||
-- dump(slots)
|
|
||||||
|
|
||||||
---@type table<InventorySlot, Barotrauma.Item[]>
|
---@type table<InventorySlot, Barotrauma.Item[]>
|
||||||
local movableBySlot = getItemsPerSlot(slots)
|
local movableBySlot = getItemsPerSlot(slots)
|
||||||
-- MyModGlobal.debugPrint("Movable by slot:")
|
-- MyModGlobal.debugPrint("Movable by slot:")
|
||||||
-- dump(movableBySlot)
|
|
||||||
|
|
||||||
local permissibleItems = LOAD_MAP[tostring(item.Prefab.Identifier.Value)]
|
local permissibleItems = LOAD_MAP[tostring(item.Prefab.Identifier.Value)]
|
||||||
if not permissibleItems then
|
if not permissibleItems then
|
||||||
@@ -156,22 +157,19 @@ local function tryReloadSlot(slot, preferMinCondition)
|
|||||||
-- We loaded as many as we have been allowed to
|
-- We loaded as many as we have been allowed to
|
||||||
-- And we do this check up front because an item may already
|
-- And we do this check up front because an item may already
|
||||||
-- Be partially loaded
|
-- Be partially loaded
|
||||||
local nowHave = #inventorySlot.slot.items
|
local nowHave = inventorySlot.stackSize
|
||||||
if nowHave >= permissible then
|
if nowHave >= permissible then
|
||||||
-- MyModGlobal.debugPrint(string.format(
|
MyModGlobal.debugPrint(string.format(
|
||||||
-- "Finished processing item: %s. Current slot has reached the permissible limit of %d items.",
|
"Finished processing item: %s. Current slot has reached the permissible limit of %d items.",
|
||||||
-- tostring(ititem.Prefab.Identifier.Value), permissible))
|
tostring(ititem.Prefab.Identifier.Value), permissible))
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
local moved = inventorySlot.inventory.TryPutItem(ititem, inventorySlot.slotIndex, false, true, Character.Controlled, true)
|
if not inventorySlot:canFit(ititem.Prefab) then
|
||||||
-- When the slot is full no more will be able to be moved
|
break
|
||||||
-- And tat that point we're done with that slot
|
end
|
||||||
if not moved then break end
|
utils.enqueueMove(ititem, inventorySlot)
|
||||||
numMoved = numMoved + 1
|
numMoved = numMoved + 1
|
||||||
|
|
||||||
-- else
|
|
||||||
-- MyModGlobal.debugPrint(string.format("Not permissible: %s", tostring(ititem.Prefab.Identifier.Value)))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -143,21 +143,14 @@ MyModGlobal.InventorySlot = {
|
|||||||
howManyCanFit = function(self, itemPrefab)
|
howManyCanFit = function(self, itemPrefab)
|
||||||
-- There is an item in the slot and it's not stackable with itemPrefab
|
-- There is an item in the slot and it's not stackable with itemPrefab
|
||||||
if self.item and not self.item.Prefab.Equals(itemPrefab) then
|
if self.item and not self.item.Prefab.Equals(itemPrefab) then
|
||||||
MyModGlobal.debugPrint(string.format(
|
|
||||||
"%s can fit 0 of %s because it already contains an item that is not stackable with %s (%s)",
|
|
||||||
tostring(self), tostring(itemPrefab), tostring(itemPrefab), tostring(self.item.Prefab)))
|
|
||||||
return 0
|
return 0
|
||||||
end
|
end
|
||||||
-- The slot is empty - we can fit as many as the game tells us
|
-- The slot is empty - we can fit as many as the game tells us
|
||||||
if not self.item then
|
if not self.item then
|
||||||
MyModGlobal.debugPrint(string.format("%s can fit %d of %s because it is empty", tostring(self),
|
|
||||||
itemPrefab.GetMaxStackSize(self.inventory), tostring(itemPrefab)))
|
|
||||||
return itemPrefab.GetMaxStackSize(self.inventory)
|
return itemPrefab.GetMaxStackSize(self.inventory)
|
||||||
end
|
end
|
||||||
-- The slot has an item that is stackable with itemPrefab
|
-- The slot has an item that is stackable with itemPrefab
|
||||||
-- We can fit as many as to fill the stack
|
-- We can fit as many as to fill the stack
|
||||||
MyModGlobal.debugPrint(string.format("%s can fit %d of %s because it contains %d items", tostring(self),
|
|
||||||
self.maxStackSize - self.stackSize, tostring(itemPrefab), self.stackSize))
|
|
||||||
return self.maxStackSize - self.stackSize
|
return self.maxStackSize - self.stackSize
|
||||||
end,
|
end,
|
||||||
---@param self InventorySlot
|
---@param self InventorySlot
|
||||||
@@ -281,7 +274,7 @@ local function ensureOptionsDefaults(options)
|
|||||||
options.slotPredicate = options.slotPredicate or function() return true end
|
options.slotPredicate = options.slotPredicate or function() return true end
|
||||||
options.inventoryPredicate = options.inventoryPredicate or function() return true end
|
options.inventoryPredicate = options.inventoryPredicate or function() return true end
|
||||||
options.loadRefs = options.loadRefs == true
|
options.loadRefs = options.loadRefs == true
|
||||||
options.itemRef = options.itemRef or nil
|
options.itemRef = options.itemRef or {}
|
||||||
options.recurse = options.recurse == true
|
options.recurse = options.recurse == true
|
||||||
return options
|
return options
|
||||||
end
|
end
|
||||||
@@ -417,17 +410,19 @@ do
|
|||||||
if not inventory then return options, "No inventory" end
|
if not inventory then return options, "No inventory" end
|
||||||
|
|
||||||
options.loadRefs = true
|
options.loadRefs = true
|
||||||
|
local originalItemPredicate = options.itemPredicate or function() return true end
|
||||||
options.itemPredicate = function(item)
|
options.itemPredicate = function(item)
|
||||||
if not item then return false end
|
if not item then return false end
|
||||||
local parentInventory = item.ParentInventory
|
local parentInventory = item.ParentInventory
|
||||||
if not parentInventory then return false end
|
if not parentInventory then return false end
|
||||||
if not parentInventory.Equals(inventory) then return false end
|
if not parentInventory.Equals(inventory) then return false end
|
||||||
return true
|
return originalItemPredicate(item, options.itemRef)
|
||||||
end
|
end
|
||||||
|
local originalSlotPredicate = options.slotPredicate or function() return true end
|
||||||
options.slotPredicate = function(slot, itemRef)
|
options.slotPredicate = function(slot, itemRef)
|
||||||
if not slot then return false end
|
if not slot then return false end
|
||||||
if itemRef.slotIndex1 and relevantPlayerInventorySlots[itemRef.slotIndex1] then
|
if itemRef.slotIndex1 and relevantPlayerInventorySlots[itemRef.slotIndex1] then
|
||||||
return true
|
return originalSlotPredicate(slot, itemRef)
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user