From b14df82d78cc260d5bd26893000ead6c8b4bcf82 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 30 Mar 2025 14:39:17 +0200 Subject: [PATCH] Factor out shop buyer and xpticker --- QuickStackToBag/Lua/Autorun/init.lua | 147 -------------------------- QuickStackToBag/Lua/Cyka/quickbuy.lua | 76 +++++++++++++ QuickStackToBag/Lua/Cyka/xpticker.lua | 15 +++ 3 files changed, 91 insertions(+), 147 deletions(-) create mode 100644 QuickStackToBag/Lua/Cyka/quickbuy.lua create mode 100644 QuickStackToBag/Lua/Cyka/xpticker.lua diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index 7a3d08a..db54a16 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -163,150 +163,3 @@ LuaUserData.MakeMethodAccessible(Descriptors["Barotrauma.CargoManager"], "GetCon -- local repairButton = repairableComponent.RepairButton -- return repairButton ~= nil and repairButton.Visible -- end - - ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- - - ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- - - ----@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" - end - - local map = Game.GameSession.Campaign.Map - if not map or not map.CurrentLocation or not map.CurrentLocation.Stores then - return nil, "No map found" - end - - local location = map.CurrentLocation - - -- Otherwise, determine which store is active by checking the cargo manager - local cargoManager = Game.GameSession.Campaign.CargoManager - if not cargoManager then - return nil, "No cargo manager found" - end - - -- Find which store has items in the cart - local stores = {} - for _, store in pairs(location.Stores) do - if #cargoManager:GetBuyCrateItems(store) > 0 then - stores[#stores + 1] = store - end - end - - return stores, nil -end - --- Example: Add a key binding to buy all items in the current store --- when the 'B' key is pressed -Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) - if not PlayerInput.KeyHit(CONFIG.MAX_BUY) then return end - - local cargoManager = Game.GameSession.Campaign.CargoManager - if not cargoManager then - print("No cargo manager available") - return - end - - local stores, err = getCurrentStore() - if err then - print(string.format("Error getting current store: %s", err)) - return - end - - 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] = { - quantity = -item.Quantity, - prefab = item.ItemPrefab -- Store the ItemPrefab object - } - 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 - local idValue = item.ItemPrefab.Identifier.Value - if toAdd[idValue] then - toAdd[idValue].quantity = toAdd[idValue].quantity + item.Quantity - end - end - - for idValue, info in pairs(toAdd) do - if info.quantity > 0 then - print(string.format("Adding %d of %s to the buy crate", info.quantity, idValue)) - -- Use the stored ItemPrefab object, not the string identifier - cargoManager:ModifyItemQuantityInBuyCrate(store.Identifier, info.prefab, info.quantity) - end - end - end -end, Hook.HookMethodType.After) - ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------- - -local amountExperience = 600 -local passiveExperienceDelay = 2 -local passiveExperienceTimer = 0 - -Hook.Add("think", "examples.passiveExperience", function() - if Timer.GetTime() < passiveExperienceTimer then return end - - for k, v in pairs(Character.CharacterList) do - if not v.IsDead and v.Info ~= nil then - v.Info.GiveExperience(amountExperience) - end - end - - passiveExperienceTimer = Timer.GetTime() + passiveExperienceDelay -end) diff --git a/QuickStackToBag/Lua/Cyka/quickbuy.lua b/QuickStackToBag/Lua/Cyka/quickbuy.lua new file mode 100644 index 0000000..428f830 --- /dev/null +++ b/QuickStackToBag/Lua/Cyka/quickbuy.lua @@ -0,0 +1,76 @@ +---@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" + end + + local map = Game.GameSession.Campaign.Map + if not map or not map.CurrentLocation or not map.CurrentLocation.Stores then + return nil, "No map found" + end + + local location = map.CurrentLocation + + -- Otherwise, determine which store is active by checking the cargo manager + local cargoManager = Game.GameSession.Campaign.CargoManager + if not cargoManager then + return nil, "No cargo manager found" + end + + -- Find which store has items in the cart + local stores = {} + for _, store in pairs(location.Stores) do + if #cargoManager:GetBuyCrateItems(store) > 0 then + stores[#stores + 1] = store + end + end + + return stores, nil +end + +-- Example: Add a key binding to buy all items in the current store +-- when the 'B' key is pressed +Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) + if not PlayerInput.KeyHit(MyModGlobal.CONFIG.MAX_BUY) then return end + + local cargoManager = Game.GameSession.Campaign.CargoManager + if not cargoManager then + MyModGlobal.debugPrint("No cargo manager available") + return + end + + local stores, err = getCurrentStore() + if err then + MyModGlobal.debugPrint(string.format("Error getting current store: %s", err)) + return + end + + 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] = { + quantity = -item.Quantity, + prefab = item.ItemPrefab -- Store the ItemPrefab object + } + 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 + local idValue = item.ItemPrefab.Identifier.Value + if toAdd[idValue] then + toAdd[idValue].quantity = toAdd[idValue].quantity + item.Quantity + end + end + + for idValue, info in pairs(toAdd) do + if info.quantity > 0 then + MyModGlobal.debugPrint(string.format("Adding %d of %s to the buy crate", info.quantity, idValue)) + -- Use the stored ItemPrefab object, not the string identifier + cargoManager:ModifyItemQuantityInBuyCrate(store.Identifier, info.prefab, info.quantity) + end + end + end +end, Hook.HookMethodType.After) diff --git a/QuickStackToBag/Lua/Cyka/xpticker.lua b/QuickStackToBag/Lua/Cyka/xpticker.lua new file mode 100644 index 0000000..149fb16 --- /dev/null +++ b/QuickStackToBag/Lua/Cyka/xpticker.lua @@ -0,0 +1,15 @@ +local amountExperience = 600 +local passiveExperienceDelay = 2 +local passiveExperienceTimer = 0 + +Hook.Add("think", "examples.passiveExperience", function() + if Timer.GetTime() < passiveExperienceTimer then return end + + for k, v in pairs(Character.CharacterList) do + if not v.IsDead and v.Info ~= nil then + v.Info.GiveExperience(amountExperience) + end + end + + passiveExperienceTimer = Timer.GetTime() + passiveExperienceDelay +end)