Calculate how many items to add to cart

This commit is contained in:
2025-03-30 01:43:20 +01:00
parent c615256716
commit d0cbb14f40

View File

@@ -598,7 +598,7 @@ end, Hook.HookMethodType.After)
---------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------
---@return Barotrauma.Location.StoreInfo, string? ---@return Barotrauma.Location.StoreInfo[], string?
local function getCurrentStore() local function getCurrentStore()
if not Game or not Game.GameSession or not Game.GameSession.Campaign then if not Game or not Game.GameSession or not Game.GameSession.Campaign then
return nil, "No game session found" return nil, "No game session found"
@@ -618,13 +618,14 @@ local function getCurrentStore()
end end
-- Find which store has items in the cart -- Find which store has items in the cart
local stores = {}
for _, store in pairs(location.Stores) do for _, store in pairs(location.Stores) do
if #cargoManager:GetBuyCrateItems(store) > 0 then if #cargoManager:GetBuyCrateItems(store) > 0 then
return store, nil stores[#stores + 1] = store
end end
end end
return nil, "No store found" return stores, nil
end 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
@@ -637,16 +638,33 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
return return
end end
local store, err = getCurrentStore() local stores, err = getCurrentStore()
if err then if err then
print(string.format("Error getting current store: %s", err)) print(string.format("Error getting current store: %s", err))
return return
end end
for _, store in ipairs(stores) do
local toAdd = {}
-- Get items available at the store -- Get items available at the store
local items = cargoManager:GetBuyCrateItems(store) local items = cargoManager:GetBuyCrateItems(store)
for item in items do for item in items do
print("Item: " .. item.Prefab.Name.Value) -- We have already added this many of item
toAdd[item.ItemPrefab.Identifier.Value] = -item.Quantity
end
for item in store.Stock do
-- So if we add the total amount available
-- We get the amount we have to add to buy entire stock
if toAdd[item.ItemPrefab.Identifier.Value] then
toAdd[item.ItemPrefab.Identifier.Value] = toAdd[item.ItemPrefab.Identifier.Value] + item.Quantity
end
end
for item, amount in pairs(toAdd) do
if amount > 0 then
print(string.format("Adding %d of %s to the buy crate", amount, item))
cargoManager.ModifyItemQuantityInBuyCrate(store.Identifier, item, amount)
end
end
end end
end end
end, Hook.HookMethodType.After) end, Hook.HookMethodType.After)