From fe6d88fa43f48b4f8a9f286fd561d33c833ba47d Mon Sep 17 00:00:00 2001 From: ZioPao Date: Thu, 16 Nov 2023 19:45:11 +0100 Subject: [PATCH] Fixing prosthesis equipping and unequipping --- .../lua/client/TOC/Handlers/PlayerHandler.lua | 1 + .../client/TOC/Handlers/ProsthesisHandler.lua | 75 +++++++++++++------ media/lua/client/TOC/Main.lua | 17 ++++- media/scripts/TOC_prosthesis_items.txt | 18 ++--- 4 files changed, 76 insertions(+), 35 deletions(-) diff --git a/media/lua/client/TOC/Handlers/PlayerHandler.lua b/media/lua/client/TOC/Handlers/PlayerHandler.lua index 1c51c58..f057b61 100644 --- a/media/lua/client/TOC/Handlers/PlayerHandler.lua +++ b/media/lua/client/TOC/Handlers/PlayerHandler.lua @@ -291,6 +291,7 @@ function ISBaseTimedAction:perform() PlayerHandler.playerObj:getXp():AddXP(Perks["Side_" .. side], 1) -- TODO Make it dynamic local prostGroup = StaticData.LIMBS_TO_PROST_GROUP_MATCH_IND_STR[limbName] if not modDataHandler:getIsCicatrized(limbName) and modDataHandler:getIsProstEquipped(prostGroup) then + TOC_DEBUG.print("Trying for bleed, player met the criteria") PlayerHandler.TryRandomBleed(self.character, limbName) end end diff --git a/media/lua/client/TOC/Handlers/ProsthesisHandler.lua b/media/lua/client/TOC/Handlers/ProsthesisHandler.lua index 43df61c..345a15c 100644 --- a/media/lua/client/TOC/Handlers/ProsthesisHandler.lua +++ b/media/lua/client/TOC/Handlers/ProsthesisHandler.lua @@ -61,6 +61,23 @@ function ProsthesisHandler.CheckIfEquippable(bodyLocation) -- No acceptable cut limbs return false end + + +---Handle equipping or unequipping prosthetics +---@param item InventoryItem +---@param isEquipping boolean +function ProsthesisHandler.SearchAndSetupProsthesis(item, isEquipping) + if not ProsthesisHandler.CheckIfProst(item) then return end + + local group = ProsthesisHandler.GetGroup(item) + TOC_DEBUG.print("applying prosthesis stuff for " .. group) + local modDataHandler = ModDataHandler.GetInstance() + modDataHandler:setIsProstEquipped(group, isEquipping) + modDataHandler:apply() + +end + + ------------------------- --* Events *-- @@ -70,29 +87,41 @@ end ------------------------- --* Overrides *-- --- ---@diagnostic disable-next-line: duplicate-set-field --- function ISWearClothing:isValid() --- TOC_DEBUG.print("ISWearClothing:isValid") --- local bodyLocation = self.item:getBodyLocation() --- if not string.contains(bodyLocation, bodyLocArmProst) then --- return true --- else --- return ProsthesisHandler.CheckIfEquippable(bodyLocation) --- end --- end +---@diagnostic disable-next-line: duplicate-set-field +local og_ISWearClothing_isValid = ISWearClothing.isValid +function ISWearClothing:isValid() + local isEquippable = og_ISWearClothing_isValid(self) + local isProst = ProsthesisHandler.CheckIfProst(self.item) + + if not isProst then return isEquippable end + + --the item that we gets is the OG one, so if we're coming from the left one and wanna switch to the right one we're still gonna get the Left bodylocation + local bodyLocation = self.item:getBodyLocation() + if isEquippable and string.contains(bodyLocation, bodyLocArmProst) then + isEquippable = ProsthesisHandler.CheckIfEquippable(bodyLocation) + end + + return isEquippable +end + +local og_ISWearClothing_perform = ISWearClothing.perform +function ISWearClothing:perform() + og_ISWearClothing_perform(self) + ProsthesisHandler.SearchAndSetupProsthesis(self.item, true) +end local og_ISClothingExtraAction_isValid = ISClothingExtraAction.isValid ---@diagnostic disable-next-line: duplicate-set-field function ISClothingExtraAction:isValid() + local isEquippable = og_ISClothingExtraAction_isValid(self) + local isProst = ProsthesisHandler.CheckIfProst(self.itemExtra) + + if not isProst then return isEquippable end --the item that we gets is the OG one, so if we're coming from the left one and wanna switch to the right one we're still gonna get the Left bodylocation - -- TODO isValid can be run multiple times, for some reason. local testItem = InventoryItemFactory.CreateItem(self.extra) local bodyLocation = testItem:getBodyLocation() - local isEquippable = og_ISClothingExtraAction_isValid(self) - if isEquippable and not string.contains(bodyLocation, bodyLocArmProst) then - isEquippable = true - else + if isEquippable and string.contains(bodyLocation, bodyLocArmProst) then isEquippable = ProsthesisHandler.CheckIfEquippable(bodyLocation) end @@ -104,21 +133,19 @@ function ISClothingExtraAction:stop() og_ISClothingExtraAction_stop(self) if ProsthesisHandler.CheckIfProst(self.item) then getPlayer():Say(getText("UI_Say_CantEquip")) - end end local og_ISClothingExtraAction_perform = ISClothingExtraAction.perform function ISClothingExtraAction:perform() - if ProsthesisHandler.CheckIfProst(self.item) then - local group = ProsthesisHandler.GetGroup(self.item) - TOC_DEBUG.print("applying prosthesis stuff for " .. group) - local modDataHandler = ModDataHandler.GetInstance() - modDataHandler:setIsProstEquipped(group, true) - modDataHandler:apply() - end - og_ISClothingExtraAction_perform(self) + ProsthesisHandler.SearchAndSetupProsthesis(self.item, true) +end + +local og_ISUnequipAction_perform = ISUnequipAction.perform +function ISUnequipAction:perform() + og_ISUnequipAction_perform(self) + ProsthesisHandler.SearchAndSetupProsthesis(self.item, false) end diff --git a/media/lua/client/TOC/Main.lua b/media/lua/client/TOC/Main.lua index db60214..040045d 100644 --- a/media/lua/client/TOC/Main.lua +++ b/media/lua/client/TOC/Main.lua @@ -1,5 +1,6 @@ local PlayerHandler = require("TOC/Handlers/PlayerHandler") local CommonMethods = require("TOC/CommonMethods") +local CommandsData = require("TOC/CommandsData") ------------------ @@ -51,6 +52,9 @@ end function Main.Initialize() + + -- TODO In SP we should clean Global Mod Data when a player dies! + ---Looop until we've successfully initialized the mod local function TryToInitialize() local pl = getPlayer() @@ -66,7 +70,18 @@ function Main.Initialize() CommonMethods.SafeStartEvent("OnTick", TryToInitialize) end +---Clean the TOC table for that SP player, to prevent from clogging it up +---@param player IsoPlayer +function Main.WipeSinglePlayerData(player) + local key = CommandsData.GetKey(player:getUsername()) + ModData.remove(key) + ModData.transmit(key) +end --* Events *-- -Events.OnGameBoot.Add(Main.Start) \ No newline at end of file +Events.OnGameBoot.Add(Main.Start) + +if not isClient() and not isServer() then + Events.OnPlayerDeath.Add(Main.WipeSinglePlayerData) +end diff --git a/media/scripts/TOC_prosthesis_items.txt b/media/scripts/TOC_prosthesis_items.txt index 8995843..16a5c44 100644 --- a/media/scripts/TOC_prosthesis_items.txt +++ b/media/scripts/TOC_prosthesis_items.txt @@ -17,8 +17,8 @@ module TOC ClothingItemExtraOption = InstallProstRight, clothingExtraSubmenu = InstallProstLeft, BodyLocation = TOC_ArmProst_L, - Weight = 0, - CombatSpeedModifier = 0.7, + Weight = 1.5, + CombatSpeedModifier = 1.1, BloodLocation = UpperArms;UpperBody, Insulation = 1.0, WindResistance = 1.0, @@ -36,9 +36,8 @@ module TOC ClothingItemExtraOption = InstallProstLeft, clothingExtraSubmenu = InstallProstRight, BodyLocation = TOC_ArmProst_R, - - Weight = 0, - CombatSpeedModifier = 0.7, + Weight = 1.5, + CombatSpeedModifier = 1.1, BloodLocation = UpperArms;UpperBody, Insulation = 1.0, WindResistance = 1.0, @@ -57,8 +56,8 @@ module TOC ClothingItemExtraOption = InstallProstRight, clothingExtraSubmenu = InstallProstLeft, BodyLocation = TOC_ArmProst_L, - Weight = 0, - CombatSpeedModifier = 0.7, + Weight = 2, + CombatSpeedModifier = 1.5, BloodLocation = UpperArms;UpperBody, Insulation = 1.0, WindResistance = 1.0, @@ -76,9 +75,8 @@ module TOC ClothingItemExtraOption = InstallProstLeft, clothingExtraSubmenu = InstallProstRight, BodyLocation = TOC_ArmProst_R, - - Weight = 0, - CombatSpeedModifier = 0.7, + Weight = 2, + CombatSpeedModifier = 1.5, BloodLocation = UpperArms;UpperBody, Insulation = 1.0, WindResistance = 1.0,