From 56da8583b08c92234d6be65082f7ca97be0179e9 Mon Sep 17 00:00:00 2001 From: ZioPao Date: Mon, 13 Nov 2023 23:46:35 +0100 Subject: [PATCH] Fixes to CutLimbHandler --- media/lua/client/TOC/ClientRelayCommands.lua | 7 ++-- .../client/TOC/Handlers/AmputationHandler.lua | 17 ++++++++ .../client/TOC/TimedActions/CutLimbAction.lua | 42 +++++++++++++++---- .../lua/client/TOC/UI/CutLimbInteractions.lua | 37 ++++++++-------- media/lua/client/TOC/UI/HealthPanel.lua | 26 ++++++++++++ media/lua/server/TOC/DebugCommands.lua | 4 +- media/lua/server/TOC/ServerDataHandler.lua | 1 + media/lua/server/TOC/ServerRelayCommands.lua | 11 +++-- media/lua/shared/TOC/CommandsData.lua | 2 +- 9 files changed, 109 insertions(+), 38 deletions(-) diff --git a/media/lua/client/TOC/ClientRelayCommands.lua b/media/lua/client/TOC/ClientRelayCommands.lua index d1645e1..a831ed8 100644 --- a/media/lua/client/TOC/ClientRelayCommands.lua +++ b/media/lua/client/TOC/ClientRelayCommands.lua @@ -16,13 +16,13 @@ local function InitAmputationHandler(limbName, surgeonNum) return handler end ----comment +---Receive the damage from another player during the amputation ---@param args receiveDamageDuringAmputationParams function ClientRelayCommands.ReceiveDamageDuringAmputation(args) - local handler = InitAmputationHandler(args.limbName, args.surgeonNum) - handler:damageDuringAmputation() + AmputationHandler.ApplyDamageDuringAmputation(getPlayer(), args.limbName) end +---Creates a new handler and execute the amputation function on this client ---@param args receiveExecuteAmputationActionParams function ClientRelayCommands.ReceiveExecuteAmputationAction(args) local handler = InitAmputationHandler(args.limbName, args.surgeonNum) @@ -32,6 +32,7 @@ end local function OnServerRelayCommand(module, command, args) if module == CommandsData.modules.TOC_RELAY and ClientRelayCommands[command] then + TOC_DEBUG.print("Received Server Relay command - " .. tostring(command)) ClientRelayCommands[command](args) end end diff --git a/media/lua/client/TOC/Handlers/AmputationHandler.lua b/media/lua/client/TOC/Handlers/AmputationHandler.lua index b931105..44486d9 100644 --- a/media/lua/client/TOC/Handlers/AmputationHandler.lua +++ b/media/lua/client/TOC/Handlers/AmputationHandler.lua @@ -40,8 +40,25 @@ function AmputationHandler:new(limbName, surgeonPl) end +--* Static methods *-- + +---comment +---@param player IsoPlayer +---@param limbName string +function AmputationHandler.ApplyDamageDuringAmputation(player, limbName) + local bodyDamage = player:getBodyDamage() + local bodyPartType = BodyPartType[limbName] + local bodyDamagePart = bodyDamage:getBodyPart(bodyPartType) + TOC_DEBUG.print("damage patient - " .. tostring(bodyPartType)) + + bodyDamagePart:setBleeding(true) + bodyDamagePart:setCut(true) + bodyDamagePart:setBleedingTime(ZombRand(10, 20)) +end + --* Main methods *-- + ---Damage the player part during the amputation process function AmputationHandler:damageDuringAmputation() local bodyDamage = self.patientPl:getBodyDamage() diff --git a/media/lua/client/TOC/TimedActions/CutLimbAction.lua b/media/lua/client/TOC/TimedActions/CutLimbAction.lua index 51fb28b..d264e6e 100644 --- a/media/lua/client/TOC/TimedActions/CutLimbAction.lua +++ b/media/lua/client/TOC/TimedActions/CutLimbAction.lua @@ -7,14 +7,16 @@ local CommandsData = require("TOC/CommandsData") ---@field patient IsoPlayer ---@field character IsoPlayer ---@field limbName string +---@field item InventoryItem local CutLimbAction = ISBaseTimedAction:derive("CutLimbAction") ---Starts CutLimbAction ---@param patient IsoPlayer ---@param surgeon IsoPlayer ---@param limbName string +---@param item InventoryItem ---@return CutLimbAction -function CutLimbAction:new(surgeon, patient, limbName) +function CutLimbAction:new(surgeon, patient, limbName, item) local o = {} setmetatable(o, self) self.__index = self @@ -23,6 +25,7 @@ function CutLimbAction:new(surgeon, patient, limbName) o.character = surgeon o.patient = patient o.limbName = limbName + o.item = item o.stopOnWalk = true o.stopOnRun = true @@ -41,22 +44,45 @@ end function CutLimbAction:start() if self.patient == self.character then -- Self - self.handler = AmputationHandler:new(self.limbName) - self.handler:damageDuringAmputation() + AmputationHandler.ApplyDamageDuringAmputation(self.patient, self.limbName) else - -- Other player - ----@type relayDamageDuringAmputationParams - - + -- Another player + ---@type relayDamageDuringAmputationParams local params = {patientNum = self.patient:getOnlineID(), limbName = self.limbName} sendClientCommand(CommandsData.modules.TOC_RELAY, CommandsData.server.Relay.RelayDamageDuringAmputation, params ) end + + -- Setup cosmetic stuff + self:setActionAnim("SawLog") + self:setOverrideHandModels(self.item:getStaticModel()) + -- if self.character:getPrimaryHandItem() then + -- ISTimedActionQueue.add(ISUnequipAction:new(self.character, self.character:getPrimaryHandItem(), 2)) + -- end + -- if self.character:getSecondaryHandItem() and self.character:getSecondaryHandItem() ~= self.character:getPrimaryHandItem() then + -- ISTimedActionQueue.add(ISUnequipAction:new(self.character, self.surgeon:getSecondaryHandItem(), 2)) + -- end + + -- if sawItem then + -- self:setOverrideHandModels(sawItem:getStaticModel(), nil) + + -- end + + +end + +function CutLimbAction:waitToStart() + if self.character == self.patient then + return false + end + self.character:faceThisObject(self.patient) + return self.character:shouldBeTurning() end function CutLimbAction:perform() if self.patient == self.character then TOC_DEBUG.print("patient and surgeon are the same, executing on the client") - self.handler:execute(true) + local handler = AmputationHandler:new(self.limbName) + handler:execute(true) else TOC_DEBUG.print("patient and surgeon not the same, sending relay to server") -- Other player diff --git a/media/lua/client/TOC/UI/CutLimbInteractions.lua b/media/lua/client/TOC/UI/CutLimbInteractions.lua index 3fab4b7..ab242c2 100644 --- a/media/lua/client/TOC/UI/CutLimbInteractions.lua +++ b/media/lua/client/TOC/UI/CutLimbInteractions.lua @@ -15,14 +15,15 @@ end ---@param limbName string ---@param surgeon IsoPlayer ---@param patient IsoPlayer -local function PerformAction(limbName, surgeon, patient) - ISTimedActionQueue.add(CutLimbAction:new(surgeon, patient, limbName)) +local function PerformAction(surgeon, patient, limbName, item) + ISTimedActionQueue.add(CutLimbAction:new(surgeon, patient, limbName, item)) end ---Adds the actions to the inventory context menu ---@param surgeonNum number ---@param context ISContextMenu -local function AddInventoryAmputationOptions(surgeonNum, context) +---@param item InventoryItem +local function AddInventoryAmputationOptions(surgeonNum, context, item) local surgeonObj = getSpecificPlayer(surgeonNum) local option = context:addOption(getText("ContextMenu_Amputate"), nil) local subMenu = context:getNew(context) @@ -31,7 +32,7 @@ local function AddInventoryAmputationOptions(surgeonNum, context) local limbName = StaticData.LIMBS_STRINGS[i] if not ModDataHandler.GetInstance():getIsCut(limbName) then local limbTranslatedName = getText("ContextMenu_Limb_" .. limbName) - subMenu:addOption(limbTranslatedName, limbName, PerformAction, surgeonObj, surgeonObj) -- TODO Should be patient, not surgeon + subMenu:addOption(limbTranslatedName, surgeonObj, PerformAction, surgeonObj, limbName, item) -- TODO Should be patient, not surgeon end end end @@ -43,7 +44,7 @@ end local function AddInventoryAmputationMenu(player, context, items) local item = items[1] -- Selected item if CheckIfSaw(item.name) then - AddInventoryAmputationOptions(player, context) + AddInventoryAmputationOptions(player, context, item) end end @@ -53,6 +54,7 @@ Events.OnFillInventoryObjectContextMenu.Add(AddInventoryAmputationMenu) ------------------------------------- ---@class CutLimbHandler : BaseHandler +---@field items table local CutLimbHandler = BaseHandler:derive("CutLimbHandler") @@ -63,24 +65,24 @@ local CutLimbHandler = BaseHandler:derive("CutLimbHandler") function CutLimbHandler:new(panel, bodyPart) local o = BaseHandler.new(self, panel, bodyPart) o.items.ITEMS = {} + TOC_DEBUG.print("init CutLimbHandler") return o end ---@param item InventoryItem function CutLimbHandler:checkItem(item) local itemType = item:getType() - if CheckIfSaw(itemType) then + if string.contains(itemType, "Saw") then self:addItem(self.items.ITEMS, item) end end ---@param context ISContextMenu function CutLimbHandler:addToMenu(context) - - --TOC_DEBUG.print("addToMenu running") + local types = self:getAllItemTypes(self.items.ITEMS) local limbName = BodyPartType.ToString(self.bodyPart:getType()) - --TOC_DEBUG.print(limbName) - if StaticData.BODYPARTSTYPES_ENUM[limbName] then + if #types > 0 and StaticData.BODYPARTSTYPES_ENUM[limbName] then + TOC_DEBUG.print("addToMenu, types > 0") if not ModDataHandler.GetInstance():getIsCut(limbName) then context:addOption(getText("ContextMenu_Amputate"), self, self.onMenuOptionSelected) end @@ -89,7 +91,8 @@ end function CutLimbHandler:dropItems(items) local types = self:getAllItemTypes(items) - if #self.items.ITEMS > 0 and #types == 1 then + local limbName = BodyPartType.ToString(self.bodyPart:getType()) + if #self.items.ITEMS > 0 and #types == 1 and StaticData.BODYPARTSTYPES_ENUM[limbName] then self:onMenuOptionSelected(types[1]) return true end @@ -97,17 +100,17 @@ function CutLimbHandler:dropItems(items) end function CutLimbHandler:isValid(itemType) - return true -- TODO Workaround for now + local limbName = BodyPartType.ToString(self.bodyPart:getType()) + return (not ModDataHandler.GetInstance():getIsCut(limbName)) and self:getItemOfType(self.items.ITEMS, itemType) end function CutLimbHandler:perform(previousAction, itemType) - --local item = self:getItemOfType(self.items.ITEMS, itemType) - --previousAction = self:toPlayerInventory(item, previousAction) - -- TODO This is broken like this! + local item = self:getItemOfType(self.items.ITEMS, itemType) + previousAction = self:toPlayerInventory(item, previousAction) local limbName = BodyPartType.ToString(self.bodyPart:getType()) TOC_DEBUG.print("perform CutLimbHandler on " .. limbName) - local action = CutLimbAction:new(self:getDoctor(),self:getPatient(), limbName) - ISTimedActionQueue.add(action) + local action = CutLimbAction:new(self:getDoctor(),self:getPatient(), limbName, item) + ISTimedActionQueue.addAfter(previousAction, action) end return CutLimbHandler diff --git a/media/lua/client/TOC/UI/HealthPanel.lua b/media/lua/client/TOC/UI/HealthPanel.lua index 644d61c..e62facd 100644 --- a/media/lua/client/TOC/UI/HealthPanel.lua +++ b/media/lua/client/TOC/UI/HealthPanel.lua @@ -17,6 +17,8 @@ ISHealthBodyPartPanel = ISBodyPartPanel:derive("ISHealthBodyPartPanel") local og_ISHealthPanel_dropItemsOnBodyPart = ISHealthPanel.dropItemsOnBodyPart function ISHealthPanel:dropItemsOnBodyPart(bodyPart, items) og_ISHealthPanel_dropItemsOnBodyPart(self, bodyPart, items) + + TOC_DEBUG.print("override to dropItemsOnBodyPart running") local cutLimbHandler = CutLimbHandler:new(self, bodyPart) for _,item in ipairs(items) do cutLimbHandler:checkItem(item) @@ -35,10 +37,34 @@ function ISHealthPanel:doBodyPartContextMenu(bodyPart, x, y) -- To not recreate it but reuse the one that has been created in the original method local context = getPlayerContextMenu(playerNum) local cutLimbHandler = CutLimbHandler:new(self, bodyPart) + + self:checkItems({cutLimbHandler}) + cutLimbHandler:addToMenu(context) end + + + + + + + + + + + + + + + + + + + + + --* Modifications to handle visible amputation on the health menu *-- local og_ISHealthPanel_initialise = ISHealthPanel.initialise diff --git a/media/lua/server/TOC/DebugCommands.lua b/media/lua/server/TOC/DebugCommands.lua index 5ab69d3..cca0244 100644 --- a/media/lua/server/TOC/DebugCommands.lua +++ b/media/lua/server/TOC/DebugCommands.lua @@ -11,9 +11,7 @@ function DebugCommands.PrintAllTocData(playerObj, args) TOC_DEBUG.printTable(ServerDataHandler.modData) end - - ----comment +---Print ALL TOC data ---@param playerObj IsoPlayer ---@param args printTocDataParams function DebugCommands.PrintTocData(playerObj, args) diff --git a/media/lua/server/TOC/ServerDataHandler.lua b/media/lua/server/TOC/ServerDataHandler.lua index d7634c6..0d64a53 100644 --- a/media/lua/server/TOC/ServerDataHandler.lua +++ b/media/lua/server/TOC/ServerDataHandler.lua @@ -14,6 +14,7 @@ end ---@param key string ---@param table tocModData function ServerDataHandler.AddTable(key, table) + TOC_DEBUG.print("Adding table with key: " .. tostring(key)) ModData.add(key, table) -- Add it to the server mod data ServerDataHandler.modData[key] = table end diff --git a/media/lua/server/TOC/ServerRelayCommands.lua b/media/lua/server/TOC/ServerRelayCommands.lua index d337a20..c1bfb7e 100644 --- a/media/lua/server/TOC/ServerRelayCommands.lua +++ b/media/lua/server/TOC/ServerRelayCommands.lua @@ -6,19 +6,17 @@ local ServerRelayCommands = {} -- TODO We can easily make this a lot more simple without having functions ----comment ----@param surgeonPl IsoPlayer +---Relay DamageDuringAmputation to another client ---@param args relayDamageDuringAmputationParams -function ServerRelayCommands.RelayDamageDuringAmputation(surgeonPl, args) +function ServerRelayCommands.RelayDamageDuringAmputation(_, args) local patientPl = getPlayerByOnlineID(args.patientNum) - local surgeonNum = surgeonPl:getOnlineID() ---@type receiveDamageDuringAmputationParams - local params = {surgeonNum = surgeonNum, limbName = args.limbName} + local params = {limbName = args.limbName} sendServerCommand(patientPl, CommandsData.modules.TOC_RELAY, CommandsData.client.Relay.ReceiveDamageDuringAmputation, params) end ----comment +---Relay ExecuteAmputationAction to another client ---@param surgeonPl IsoPlayer ---@param args relayExecuteAmputationActionParams function ServerRelayCommands.RelayExecuteAmputationAction(surgeonPl, args) @@ -36,6 +34,7 @@ end local function OnClientRelayCommand(module, command, playerObj, args) if module == CommandsData.modules.TOC_RELAY and ServerRelayCommands[command] then + TOC_DEBUG.print("Received Client Relay command - " .. tostring(command)) ServerRelayCommands[command](playerObj, args) end end diff --git a/media/lua/shared/TOC/CommandsData.lua b/media/lua/shared/TOC/CommandsData.lua index 6b4a864..c440017 100644 --- a/media/lua/shared/TOC/CommandsData.lua +++ b/media/lua/shared/TOC/CommandsData.lua @@ -10,7 +10,7 @@ CommandsData.modules = { CommandsData.client = { Relay = { - ReceiveDamageDuringAmputation = "ReceiveDamageDuringAmputation", ---@alias receiveDamageDuringAmputationParams {surgeonNum : number, limbName : string} + ReceiveDamageDuringAmputation = "ReceiveDamageDuringAmputation", ---@alias receiveDamageDuringAmputationParams { limbName : string} ReceiveExecuteAmputationAction = "ReceiveExecuteAmputationAction" ---@alias receiveExecuteAmputationActionParams {surgeonNum : number, limbName : string} } }