Refactor quickreload
This commit is contained in:
@@ -3,24 +3,77 @@ local cursormacroer = require("Cyka.cursormacroer")
|
|||||||
local utils = require("Cyka.utils")
|
local utils = require("Cyka.utils")
|
||||||
local dump = require("Cyka.dump")
|
local dump = require("Cyka.dump")
|
||||||
|
|
||||||
|
-- Some items allow multiple items to be loaded
|
||||||
|
-- Such as welders and cutters
|
||||||
|
-- But we don't really want to disqualify those programmatically
|
||||||
|
-- Because it is not so obvious what item we REALLY want to load
|
||||||
|
-- So we will just hardcode tools and their whitelisted magazines
|
||||||
|
---@type table<string, table<string, boolean>>
|
||||||
|
local LOAD_MAP = {
|
||||||
|
}
|
||||||
|
|
||||||
---@param inventory Barotrauma.ItemInventory
|
---@param inventory Barotrauma.ItemInventory
|
||||||
---@param predicate fun(slot: InventorySlot): boolean
|
---@return InventorySlot[]
|
||||||
---@return InventorySlot[], string?
|
local function getSlots(inventory)
|
||||||
local function findSlotsThat(inventory, predicate)
|
|
||||||
local slots = {}
|
local slots = {}
|
||||||
for i, slot in ipairs(inventory.slots) do
|
local inventorySlots = inventory.slots
|
||||||
local inventorySlot = {
|
for i, inventorySlot in ipairs(inventorySlots) do
|
||||||
slot = slot,
|
slots[#slots + 1] = {
|
||||||
inventory = inventory,
|
inventory = inventory,
|
||||||
slotIndex = i - 1
|
slotIndex = i - 1,
|
||||||
|
slot = inventorySlot
|
||||||
}
|
}
|
||||||
if predicate(inventorySlot) then
|
|
||||||
slots[#slots + 1] = inventorySlot
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return slots
|
return slots
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param from Barotrauma.ItemInventory
|
||||||
|
---@param slots InventorySlot[]
|
||||||
|
---@return table<InventorySlot, Barotrauma.Item[]>
|
||||||
|
local function getItemsPerSlot(from, slots)
|
||||||
|
-- How many items can we move to what slot
|
||||||
|
-- We don't yet know what can fit into what slot
|
||||||
|
---@type table<InventorySlot, Barotrauma.Item[]>
|
||||||
|
local movableBySlot = {}
|
||||||
|
-- Get all the items and then we will sort them by condition and shit
|
||||||
|
utils.enqueueInventory(from, {}, function(ititem)
|
||||||
|
-- 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)
|
||||||
|
return movableBySlot
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param movableBySlot table<InventorySlot, Barotrauma.Item[]>
|
||||||
|
---@return table<InventorySlot, table<Barotrauma.ItemPrefab, boolean>>
|
||||||
|
local function getPermissibleItemsPerSlot(movableBySlot)
|
||||||
|
-- The point of this exercise is to eliminate slots that can have
|
||||||
|
-- Multiple items
|
||||||
|
-- What do we put into those? Any? All?
|
||||||
|
-- What if those slots belong to a container?
|
||||||
|
-- Are we reloading 30 slot containers?
|
||||||
|
---@type table<InventorySlot, table<Barotrauma.ItemPrefab, boolean>>
|
||||||
|
local permissibleItemsPerSlot = {}
|
||||||
|
for inventorySlot, items in pairs(movableBySlot) do
|
||||||
|
for _, ititem in ipairs(items) do
|
||||||
|
local thisone = tostring(ititem.Prefab.Identifier.Value)
|
||||||
|
permissibleItemsPerSlot[inventorySlot][thisone] = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return permissibleItemsPerSlot
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
---@param slot InventorySlot
|
---@param slot InventorySlot
|
||||||
---@param preferMinCondition boolean
|
---@param preferMinCondition boolean
|
||||||
local function tryReloadSlot(slot, preferMinCondition)
|
local function tryReloadSlot(slot, preferMinCondition)
|
||||||
@@ -36,17 +89,6 @@ local function tryReloadSlot(slot, preferMinCondition)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
---@type InventorySlot[]
|
|
||||||
local toLoad = {}
|
|
||||||
local inventorySlots = inventory.slots
|
|
||||||
for i, inventorySlot in ipairs(inventorySlots) do
|
|
||||||
toLoad[#toLoad + 1] = {
|
|
||||||
inventory = inventory,
|
|
||||||
slotIndex = i - 1,
|
|
||||||
slot = inventorySlot
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
local character = Character.Controlled
|
local character = Character.Controlled
|
||||||
if not character then
|
if not character then
|
||||||
MyModGlobal.debugPrint("No character")
|
MyModGlobal.debugPrint("No character")
|
||||||
@@ -58,47 +100,24 @@ local function tryReloadSlot(slot, preferMinCondition)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
-- How many items can we move to what slot
|
---@type InventorySlot[]
|
||||||
-- We don't yet know what can fit into what slot
|
local slots = getSlots(inventory)
|
||||||
|
-- MyModGlobal.debugPrint("Slots:")
|
||||||
|
-- dump(slots)
|
||||||
|
|
||||||
---@type table<InventorySlot, Barotrauma.Item[]>
|
---@type table<InventorySlot, Barotrauma.Item[]>
|
||||||
local movableBySlot = {}
|
local movableBySlot = getItemsPerSlot(characterInventory, slots)
|
||||||
-- Get all the items and then we will sort them by condition and shit
|
-- MyModGlobal.debugPrint("Movable by slot:")
|
||||||
utils.enqueueInventory(characterInventory, {}, function(ititem)
|
|
||||||
for _, inventorySlot in ipairs(toLoad) do
|
|
||||||
local canMove = inventorySlot.inventory.CanBePutInSlot(ititem, inventorySlot.slotIndex)
|
|
||||||
if canMove then
|
|
||||||
movableBySlot[inventorySlot] = movableBySlot[inventorySlot] or {}
|
|
||||||
movableBySlot[inventorySlot][#movableBySlot[inventorySlot] + 1] = ititem
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end)
|
|
||||||
-- print("movableBySlot")
|
|
||||||
-- dump(movableBySlot)
|
-- dump(movableBySlot)
|
||||||
|
|
||||||
-- The point of this exercise is to eliminate slots that can have
|
local permissibleItems = LOAD_MAP[tostring(item.Prefab.Identifier.Value)]
|
||||||
-- Multiple items
|
if not permissibleItems then
|
||||||
-- What do we put into those? Any? All?
|
MyModGlobal.debugPrint("No permissible items for item")
|
||||||
-- What if those slots belong to a container?
|
local permissibleItemsPerSlot = getPermissibleItemsPerSlot(movableBySlot)
|
||||||
-- Are we reloading 30 slot containers?
|
MyModGlobal.debugPrint("Can load per slot:")
|
||||||
---@type table<InventorySlot, Barotrauma.ItemPrefab>
|
dump(permissibleItemsPerSlot)
|
||||||
local permissibleItemsPerSlot = {}
|
return
|
||||||
for inventorySlot, items in pairs(movableBySlot) do
|
|
||||||
for _, ititem in ipairs(items) do
|
|
||||||
local existing = permissibleItemsPerSlot[inventorySlot]
|
|
||||||
local thisone = ititem.Prefab
|
|
||||||
if existing and not existing.Equals(thisone) then
|
|
||||||
print("Already have an item in this slot, can not have 2?")
|
|
||||||
movableBySlot[inventorySlot] = nil
|
|
||||||
end
|
end
|
||||||
permissibleItemsPerSlot[inventorySlot] = thisone
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- print("permissibleItemsPerSlot")
|
|
||||||
-- dump(permissibleItemsPerSlot)
|
|
||||||
-- print("movableBySlot")
|
|
||||||
-- dump(movableBySlot)
|
|
||||||
|
|
||||||
-- Sort items by condition (asc or desc) but also
|
-- Sort items by condition (asc or desc) but also
|
||||||
-- Make sure items with 0 condition are at the end
|
-- Make sure items with 0 condition are at the end
|
||||||
@@ -116,7 +135,7 @@ local function tryReloadSlot(slot, preferMinCondition)
|
|||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
dump(movableBySlot)
|
-- dump(movableBySlot)
|
||||||
|
|
||||||
for inventorySlot, items in pairs(movableBySlot) do
|
for inventorySlot, items in pairs(movableBySlot) do
|
||||||
for _, ititem in ipairs(items) do
|
for _, ititem in ipairs(items) do
|
||||||
|
Reference in New Issue
Block a user