More lil fixes and reworks

Such as early stopping for the item search
This commit is contained in:
2025-03-30 03:21:42 +02:00
parent 8cbdf3d549
commit 5fdb49c40b

View File

@@ -1,4 +1,5 @@
if SERVER then return end if SERVER then return end
-- Docs: https://evilfactory.github.io/LuaCsForBarotrauma/lua-docs/manual/common-questions/
-- Register necessary types and make fields accessible -- Register necessary types and make fields accessible
LuaUserData.RegisterType("Barotrauma.Items.Components.ItemContainer+SlotRestrictions") LuaUserData.RegisterType("Barotrauma.Items.Components.ItemContainer+SlotRestrictions")
@@ -184,7 +185,7 @@ local _
---@param item Barotrauma.Item ---@param item Barotrauma.Item
---@param queue Barotrauma.Item[] ---@param queue Barotrauma.Item[]
---@param predicate? fun(item: Barotrauma.Item): boolean ---@param predicate? fun(item: Barotrauma.Item): boolean
---@return Barotrauma.Item[], string ---@return Barotrauma.Item[], string?
enqueueItem = function(item, queue, predicate) enqueueItem = function(item, queue, predicate)
queue = queue or {} queue = queue or {}
predicate = predicate or function() return true end predicate = predicate or function() return true end
@@ -202,10 +203,9 @@ enqueueItem = function(item, queue, predicate)
end end
local ok, stop = predicate(item) local ok, stop = predicate(item)
if ok then if ok then
-- debugPrint(string.format("Enqueuing item: %s", item.Prefab.Identifier.Value))
queue[#queue + 1] = item queue[#queue + 1] = item
end end
if stop then return queue, nil end if stop then return queue, "Stop" end
-- debugPrint(string.format("Item enqueued. Current queue size: %d", #queue)) -- debugPrint(string.format("Item enqueued. Current queue size: %d", #queue))
return queue, nil return queue, nil
end end
@@ -213,21 +213,21 @@ end
---@param slot Barotrauma.ItemInventory.Slot ---@param slot Barotrauma.ItemInventory.Slot
---@param queue Barotrauma.Item[] ---@param queue Barotrauma.Item[]
---@param predicate? fun(item: Barotrauma.Item): boolean ---@param predicate? fun(item: Barotrauma.Item): boolean
---@return Barotrauma.Item[], string ---@return Barotrauma.Item[], string?
enqueueSlot = function(slot, queue, predicate) enqueueSlot = function(slot, queue, predicate)
queue = queue or {} queue = queue or {}
predicate = predicate or function() return true end predicate = predicate or function() return true end
-- debugPrint(string.format("Enqueuing slot with %d items.", #slot.items)) -- debugPrint(string.format("Enqueuing slot with %d items.", #slot.items))
-- We don't want to shadow queue -- We don't want to shadow queue
-- local err local err
-- If the slot is empty there's nothing to iterate -- If the slot is empty there's nothing to iterate
-- And we will naturally return queue as is -- And we will naturally return queue as is
for _, item in ipairs(slot.items) do for _, item in ipairs(slot.items) do
-- Only the final leaf nodes decide upon the predicate -- Only the final leaf nodes decide upon the predicate
queue, _ = enqueueItem(item, queue, predicate) queue, err = enqueueItem(item, queue, predicate)
-- if err then if err then
-- debugPrint(string.format("Error enqueuing item: %s", err)) return queue, err
-- end end
end end
-- debugPrint(string.format("Finished enqueuing slot. Current queue size: %d", #queue)) -- debugPrint(string.format("Finished enqueuing slot. Current queue size: %d", #queue))
return queue return queue
@@ -236,18 +236,18 @@ end
---@param inventory Barotrauma.ItemInventory ---@param inventory Barotrauma.ItemInventory
---@param queue Barotrauma.Item[] ---@param queue Barotrauma.Item[]
---@param predicate? fun(item: Barotrauma.Item): boolean ---@param predicate? fun(item: Barotrauma.Item): boolean
---@return Barotrauma.Item[] ---@return Barotrauma.Item[], string?
enqueueInventory = function(inventory, queue, predicate) enqueueInventory = function(inventory, queue, predicate)
queue = queue or {} queue = queue or {}
predicate = predicate or function() return true end predicate = predicate or function() return true end
-- debugPrint(string.format("Enqueuing inventory with %d slots.", #inventory.slots)) -- debugPrint(string.format("Enqueuing inventory with %d slots.", #inventory.slots))
-- local err local err
for _, slot in ipairs(inventory.slots) do for _, slot in ipairs(inventory.slots) do
-- Only the final leaf nodes decide upon the predicate -- Only the final leaf nodes decide upon the predicate
queue, _ = enqueueSlot(slot, queue, predicate) queue, err = enqueueSlot(slot, queue, predicate)
-- if err then if err then
-- debugPrint(string.format("Error enqueuing slot: %s", err)) return queue, err
-- end end
end end
-- debugPrint(string.format("Finished enqueuing inventory. Current queue size: %d", #queue)) -- debugPrint(string.format("Finished enqueuing inventory. Current queue size: %d", #queue))
return queue return queue
@@ -539,8 +539,9 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
recipe, err = getSelectedRecipeRequirements(fabricator.fabricator) recipe, err = getSelectedRecipeRequirements(fabricator.fabricator)
if err then if err then
print(string.format("Error getting selected recipe requirements: %s", err)) print(string.format("Error getting selected recipe requirements: %s", err))
return
end end
DumpTable(recipe) -- DumpTable(recipe)
-- TODO: Maybe make it so every press cycles the input -- TODO: Maybe make it so every press cycles the input
-- For recipes that have multiple prefabs -- For recipes that have multiple prefabs
@@ -549,7 +550,7 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
local toFind = recipe.requiredItems local toFind = recipe.requiredItems
---@type Barotrauma.Item[] ---@type Barotrauma.Item[]
local toGet = {} local toGet = {}
---@type fun(item: Barotrauma.Item): boolean ---@type fun(item: Barotrauma.Item): boolean, boolean
local filter = function(item) local filter = function(item)
local found = false local found = false
-- toFind are all items we need to find -- toFind are all items we need to find
@@ -561,19 +562,20 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
-- debugPrint(string.format("That'll do %s %s", item.Prefab.Identifier.Value, prefab)) -- debugPrint(string.format("That'll do %s %s", item.Prefab.Identifier.Value, prefab))
toGet[#toGet + 1] = item toGet[#toGet + 1] = item
itemInfo.amount = itemInfo.amount - 1 itemInfo.amount = itemInfo.amount - 1
found = false found = true
break break
end end
end end
if itemInfo.amount <= 0 then toFind[i] = nil end if itemInfo.amount <= 0 then toFind[i] = nil end
if found then break end if found then break end
end end
-- debugPrint(string.format("Found %s %s", item.Prefab.Identifier.Value, tostring(found))) -- debugPrint(string.format("Found %s %s", item.Prefab.Identifier.Value, tostring(#toFind)))
return found, #toFind == 0 return found, #toFind == 0
end end
-- DumpTable(toGet) -- DumpTable(toGet)
local items = enqueueInventory(bagItem.OwnInventory, {}, filter) local items = enqueueInventory(bagItem.OwnInventory, {}, filter)
-- DumpTable(items)
-- TODO: This might explode... Oh well? -- TODO: This might explode... Oh well?
local inputInventory = fabricator.item.OwnInventories[1] local inputInventory = fabricator.item.OwnInventories[1]
for iinventory in fabricator.item.OwnInventories do for iinventory in fabricator.item.OwnInventories do
@@ -587,7 +589,7 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
local previous = nil local previous = nil
for _, item in ipairs(items) do for _, item in ipairs(items) do
if previous ~= item.Prefab.Identifier then slot = slot + 1 end if previous ~= item.Prefab.Identifier then slot = slot + 1 end
-- inputInventory.TryPutItem(item, slot, false, true, nil) inputInventory.TryPutItem(item, slot, false, true, nil)
previous = item.Prefab.Identifier previous = item.Prefab.Identifier
end end
DumpTable(items) DumpTable(items)
@@ -644,7 +646,8 @@ end
-- Example: Add a key binding to buy all items in the current store -- Example: Add a key binding to buy all items in the current store
-- when the 'B' key is pressed -- when the 'B' key is pressed
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if PlayerInput.KeyHit(CONFIG.MAX_BUY) then if not PlayerInput.KeyHit(CONFIG.MAX_BUY) then return end
local cargoManager = Game.GameSession.Campaign.CargoManager local cargoManager = Game.GameSession.Campaign.CargoManager
if not cargoManager then if not cargoManager then
print("No cargo manager available") print("No cargo manager available")
@@ -685,5 +688,4 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
end end
end end
end end
end
end, Hook.HookMethodType.After) end, Hook.HookMethodType.After)