Calculate how many items to add to cart
This commit is contained in:
@@ -598,7 +598,7 @@ end, Hook.HookMethodType.After)
|
||||
----------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
---@return Barotrauma.Location.StoreInfo, string?
|
||||
---@return Barotrauma.Location.StoreInfo[], string?
|
||||
local function getCurrentStore()
|
||||
if not Game or not Game.GameSession or not Game.GameSession.Campaign then
|
||||
return nil, "No game session found"
|
||||
@@ -618,13 +618,14 @@ local function getCurrentStore()
|
||||
end
|
||||
|
||||
-- Find which store has items in the cart
|
||||
local stores = {}
|
||||
for _, store in pairs(location.Stores) do
|
||||
if #cargoManager:GetBuyCrateItems(store) > 0 then
|
||||
return store, nil
|
||||
stores[#stores + 1] = store
|
||||
end
|
||||
end
|
||||
|
||||
return nil, "No store found"
|
||||
return stores, nil
|
||||
end
|
||||
|
||||
-- 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
|
||||
end
|
||||
|
||||
local store, err = getCurrentStore()
|
||||
local stores, err = getCurrentStore()
|
||||
if err then
|
||||
print(string.format("Error getting current store: %s", err))
|
||||
return
|
||||
end
|
||||
|
||||
-- Get items available at the store
|
||||
local items = cargoManager:GetBuyCrateItems(store)
|
||||
for item in items do
|
||||
print("Item: " .. item.Prefab.Name.Value)
|
||||
for _, store in ipairs(stores) do
|
||||
local toAdd = {}
|
||||
-- Get items available at the store
|
||||
local items = cargoManager:GetBuyCrateItems(store)
|
||||
for item in items do
|
||||
-- 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, Hook.HookMethodType.After)
|
||||
|
||||
Reference in New Issue
Block a user