From 18cae1de984891e4391c1142b0b292e53b19e8e1 Mon Sep 17 00:00:00 2001 From: ZioPao Date: Thu, 16 Nov 2023 18:19:47 +0100 Subject: [PATCH] Fixed CutLimbHandler once again --- .../lua/client/TOC/Handlers/PlayerHandler.lua | 51 ++++++++++++++----- .../lua/client/TOC/UI/CutLimbInteractions.lua | 20 +++++--- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/media/lua/client/TOC/Handlers/PlayerHandler.lua b/media/lua/client/TOC/Handlers/PlayerHandler.lua index 5f0a509..fb56019 100644 --- a/media/lua/client/TOC/Handlers/PlayerHandler.lua +++ b/media/lua/client/TOC/Handlers/PlayerHandler.lua @@ -13,6 +13,7 @@ local StaticData = require("TOC/StaticData") ---@class PlayerHandler ---@field playerObj IsoPlayer +---@field hasBeenDamaged boolean local PlayerHandler = {} ---Setup the Player Handler and modData, only for local client @@ -96,29 +97,26 @@ end ------------------------- --* Events *-- +--- Locks OnPlayerGetDamage event, to prevent it from getting spammed constantly +PlayerHandler.hasBeenDamaged = false + ---Check if the player has in infected body part or if they have been hit in a cut area ----@param character IsoGameCharacter ----@param damageType string ----@param damageAmount number -function PlayerHandler.CheckDamage(character, damageType, damageAmount) - - -- TODO This fucking event barely works. Bleeding seems to be the only thing that triggers it. use this to trigger something else and then do not let it keep going - +---@param character IsoPlayer +function PlayerHandler.HandleDamage(character) -- TOC_DEBUG.print("Player got hit!") -- TOC_DEBUG.print(damageType) if character ~= getPlayer() then return end local bd = character:getBodyDamage() local modDataHandler = ModDataHandler.GetInstance() + local modDataNeedsUpdate = false for i=1, #StaticData.LIMBS_STR do local limbName = StaticData.LIMBS_STR[i] local bptEnum = StaticData.BODYLOCS_IND_BPT[limbName] local bodyPart = bd:getBodyPart(bptEnum) - if modDataHandler:getIsCut(limbName) then -- Generic injury, let's heal it since they already cut the limb off - --FIXME conflicts with the other thing... Ah fuck wait I'm retarded if bodyPart:HasInjury() then PlayerHandler.HealArea(bodyPart) end @@ -131,27 +129,52 @@ function PlayerHandler.CheckDamage(character, damageType, damageAmount) else if bodyPart:bitten() or bodyPart:IsInfected() then modDataHandler:setIsInfected(limbName, true) - modDataHandler:apply() + modDataNeedsUpdate = true end end end -- Check other body parts that are not included in the mod, if there's a bite there then the player is fucked -- We can skip this loop if the player has been infected. The one before we kinda need it to handle correctly the bites in case the player wanna cut stuff off anyway - if ModDataHandler.GetInstance():getIsIgnoredPartInfected() then return end + if modDataHandler:getIsIgnoredPartInfected() then return end for i=1, #StaticData.IGNORED_BODYLOCS_BPT do local bodyPartType = StaticData.IGNORED_BODYLOCS_BPT[i] local bodyPart = bd:getBodyPart(bodyPartType) if bodyPart and (bodyPart:bitten() or bodyPart:IsInfected()) then - ModDataHandler.GetInstance():setIsIgnoredPartInfected(true) + modDataHandler:setIsIgnoredPartInfected(true) + modDataNeedsUpdate = true end end - -- TODO in theory we should sync modData, but it's gonna be expensive as fuck. Figure it out + -- TODO in theory should sync modData, but it's gonna be expensive as fuck. Figure it out + if modDataNeedsUpdate then + modDataHandler:apply() + end + + -- Disable the lock + PlayerHandler.hasBeenDamaged = false + end -Events.OnPlayerGetDamage.Add(PlayerHandler.CheckDamage) +---Setup HandleDamage, triggered by OnPlayerGetDamage +---@param character IsoGameCharacter +---@param damageType string +---@param damageAmount number +function PlayerHandler.OnGetDamage(character, damageType, damageAmount) + + -- TODO Check if other players in the online triggers this + + if PlayerHandler.hasBeenDamaged == false then + -- Start checks + + -- TODO Add a timer before we can re-enable this bool? + PlayerHandler.hasBeenDamaged = true + PlayerHandler.HandleDamage(character) + end +end + +Events.OnPlayerGetDamage.Add(PlayerHandler.OnGetDamage) ---Updates the cicatrization process, run when a limb has been cut. Run it every 1 hour function PlayerHandler.UpdateCicatrization() diff --git a/media/lua/client/TOC/UI/CutLimbInteractions.lua b/media/lua/client/TOC/UI/CutLimbInteractions.lua index 18dd3a4..c4e28e0 100644 --- a/media/lua/client/TOC/UI/CutLimbInteractions.lua +++ b/media/lua/client/TOC/UI/CutLimbInteractions.lua @@ -144,6 +144,7 @@ Events.OnFillInventoryObjectContextMenu.Add(AddInventoryAmputationMenu) ---@class CutLimbHandler : BaseHandler ---@field items table ---@field limbName string +---@field itemType string temporary local CutLimbHandler = BaseHandler:derive("CutLimbHandler") @@ -155,14 +156,16 @@ function CutLimbHandler:new(panel, bodyPart) local o = BaseHandler.new(self, panel, bodyPart) o.items.ITEMS = {} o.limbName = BodyPartType.ToString(bodyPart:getType()) + o.itemType = "Saw" TOC_DEBUG.print("init CutLimbHandler") return o end ---@param item InventoryItem function CutLimbHandler:checkItem(item) + TOC_DEBUG.print("CutLimbHandler checkItem") local itemType = item:getType() - TOC_DEBUG.print("checkItem: " .. tostring(itemType)) + --TOC_DEBUG.print("checkItem: " .. tostring(itemType)) if CheckIfSaw(itemType) then TOC_DEBUG.print("added to list -> " .. itemType) @@ -172,11 +175,12 @@ end ---@param context ISContextMenu function CutLimbHandler:addToMenu(context) + TOC_DEBUG.print("CutLimbHandler addToMenu") local types = self:getAllItemTypes(self.items.ITEMS) - if #types > 0 and StaticData.BODYLOCS_IND_BPT[self.limbName] then + if #types > 0 and StaticData.BODYLOCS_IND_BPT[self.limbName] and not ModDataHandler.GetInstance():getIsCut(self.limbName) then TOC_DEBUG.print("addToMenu, types > 0") - if not ModDataHandler.GetInstance():getIsCut(self.limbName) then - context:addOption(getText("ContextMenu_Amputate"), self, self.onMenuOptionSelected) + for i=1, #types do + context:addOption(getText("ContextMenu_Amputate"), self, self.onMenuOptionSelected, types[i]) end end end @@ -190,8 +194,12 @@ function CutLimbHandler:dropItems(items) return false end -function CutLimbHandler:isValid(itemType) - return (not ModDataHandler.GetInstance():getIsCut(self.limbName)) and self:getItemOfType(self.items.ITEMS, itemType) +---Check if CutLimbHandler is valid, the limb must not be cut to be valid +---@return boolean +function CutLimbHandler:isValid() + TOC_DEBUG.print("CutLimbHandler isValid") + self:checkItems() + return not ModDataHandler.GetInstance():getIsCut(self.limbName) end function CutLimbHandler:perform(previousAction, itemType)