Rethinking structure a bit

This commit is contained in:
ZioPao
2024-01-08 10:57:01 +01:00
parent 6b1205e160
commit 312294edb8
19 changed files with 229 additions and 222 deletions

View File

@@ -0,0 +1,214 @@
local BaseHandler = require("TOC/UI/Interactions/HealthPanelBaseHandler")
local StaticData = require("TOC/StaticData")
local DataController = require("TOC/Controllers/DataController")
local CutLimbAction = require("TOC/TimedActions/CutLimbAction")
---------------------
-- TODO Add interaction to cut and bandage!
--* Various functions to help during these pesky checks
---Check if the item type corresponds to a compatible saw
---@param itemType string
local function CheckIfSaw(itemType)
return itemType:contains(StaticData.SAWS_TYPES_IND_STR.saw) or itemType:contains(StaticData.SAWS_TYPES_IND_STR.gardenSaw)
end
---Return a compatible bandage
---@param player IsoPlayer
---@return InventoryItem?
local function GetBandageItem(player)
local plInv = player:getInventory()
-- TODO Add other options, like ripped sheets and such items
local bandageItem = plInv:FindAndReturn("Base.Bandage") or plInv:FindAndReturn("Base.RippedSheets")
return bandageItem -- TODO check this warning
end
---Return a suture needle or thread (only if the player has a needle too)
---@param player IsoPlayer
---@return InventoryItem?
local function GetStitchesItem(player)
-- TODO Search for thread
local plInv = player:getInventory()
local needleItem = plInv:FindAndReturn("Base.SutureNeedle")
if needleItem ~= nil then return needleItem end
-- Didn't find the suture one, so let's search for the normal one + thread
needleItem = plInv:FindAndReturn("Base.Needle")
if needleItem == nil then return nil end
-- Found the normal one, searching for thread
local threadItem = plInv:FindAndReturn("Base.Thread")
if threadItem then return threadItem end
end
---Add the action to the queue
---@param limbName string
---@param surgeon IsoPlayer
---@param patient IsoPlayer
---@param sawItem InventoryItem
---@param stitchesItem InventoryItem?
---@param bandageItem InventoryItem?
local function PerformAction(surgeon, patient, limbName, sawItem, stitchesItem, bandageItem)
ISTimedActionQueue.add(CutLimbAction:new(surgeon, patient, limbName, sawItem, stitchesItem, bandageItem))
end
local textAmp = getText("ContextMenu_Amputate")
local textAmpBandage = getText("ContextMenu_Amputate_Bandage")
local textAmpStitch = getText("ContextMenu_Amputate_Stitch")
local textAmpStitchBandage = getText("ContextMenu_Amputate_Stitch_Bandage")
---Adds the actions to the inventory context menu
---@param player IsoPlayer
---@param context ISContextMenu
---@param sawItem InventoryItem
---@param stitchesItem InventoryItem?
---@param bandageItem InventoryItem?
local function AddInvAmputationOptions(player, context, sawItem, stitchesItem, bandageItem)
local text
-- Set the correct text option
if stitchesItem and bandageItem then
--TOC_DEBUG.print("stitches and bandage")
text = textAmpStitchBandage
elseif not bandageItem and stitchesItem then
--TOC_DEBUG.print("only stitches")
text = textAmpStitch
elseif not stitchesItem and bandageItem then
--TOC_DEBUG.print("only bandages")
text = textAmpBandage
else
text = textAmp
end
TOC_DEBUG.print("Current text " .. tostring(text))
local option = context:addOption(text, nil)
local subMenu = context:getNew(context)
context:addSubMenu(option, subMenu)
for i = 1, #StaticData.LIMBS_STR do
local limbName = StaticData.LIMBS_STR[i]
if not DataController.GetInstance():getIsCut(limbName) then
local limbTranslatedName = getText("ContextMenu_Limb_" .. limbName)
subMenu:addOption(limbTranslatedName, player, PerformAction, player, limbName, sawItem, stitchesItem, bandageItem)
end
end
end
---Handler for OnFillInventoryObjectContextMenu
---@param playerNum number
---@param context ISContextMenu
---@param items table
local function AddInventoryAmputationMenu(playerNum, context, items)
local item
-- We can't access the item if we don't create the loop and start ipairs.
for _, v in ipairs(items) do
item = v
if not instanceof(v, "InventoryItem") then
item = v.items[1]
end
break
end
local itemType = item:getType()
if CheckIfSaw(itemType) then
local player = getSpecificPlayer(playerNum)
local sawItem = item
local stitchesItem = GetStitchesItem(player)
local bandageItem = GetBandageItem(player)
TOC_DEBUG.print("Stitches item: " .. tostring(stitchesItem))
TOC_DEBUG.print("Bandage item: " .. tostring(bandageItem))
AddInvAmputationOptions(player, context, sawItem, stitchesItem, bandageItem)
-- TODO Add stitches option and mixes
end
end
Events.OnFillInventoryObjectContextMenu.Add(AddInventoryAmputationMenu)
-------------------------------------
---@class CutLimbHandler : BaseHandler
---@field items table
---@field limbName string
---@field itemType string temporary
local CutLimbHandler = BaseHandler:derive("CutLimbHandler")
---Creates new CutLimbHandler
---@param panel ISUIElement
---@param bodyPart BodyPart
---@return CutLimbHandler
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))
if CheckIfSaw(itemType) then
TOC_DEBUG.print("added to list -> " .. itemType)
self:addItem(self.items.ITEMS, item)
end
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] and not DataController.GetInstance():getIsCut(self.limbName) then
TOC_DEBUG.print("addToMenu, types > 0")
for i=1, #types do
context:addOption(getText("ContextMenu_Amputate"), self, self.onMenuOptionSelected, types[i])
end
end
end
function CutLimbHandler:dropItems(items)
local types = self:getAllItemTypes(items)
if #self.items.ITEMS > 0 and #types == 1 and StaticData.BODYLOCS_IND_BPT[self.limbName] then
self:onMenuOptionSelected(types[1])
return true
end
return false
end
---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 DataController.GetInstance():getIsCut(self.limbName)
end
function CutLimbHandler:perform(previousAction, itemType)
local item = self:getItemOfType(self.items.ITEMS, itemType)
previousAction = self:toPlayerInventory(item, previousAction)
TOC_DEBUG.print("perform CutLimbHandler on " .. self.limbName)
local action = CutLimbAction:new(self:getDoctor(),self:getPatient(), self.limbName, item)
ISTimedActionQueue.addAfter(previousAction, action)
end
return CutLimbHandler

View File

@@ -0,0 +1,136 @@
-- Had to copy and paste this stuff from the base game since this is a local only class. Kinda shit, but eh
---@class BaseHandler : ISBaseObject
---@field panel ISUIElement
---@field bodyPart BodyPart
---@field items table
local BaseHandler = ISBaseObject:derive("BaseHandler")
---@param panel ISUIElement
---@param bodyPart BodyPart
---@return table
function BaseHandler:new(panel, bodyPart)
local o = {}
setmetatable(o, self)
self.__index = self
o.panel = panel
o.bodyPart = bodyPart
o.items = {}
return o
end
function BaseHandler:isInjured()
local bodyPart = self.bodyPart
return (bodyPart:HasInjury() or bodyPart:stitched() or bodyPart:getSplintFactor() > 0) and not bodyPart:bandaged()
end
function BaseHandler:checkItems()
for k,v in pairs(self.items) do
table.wipe(v)
end
local containers = ISInventoryPaneContextMenu.getContainers(self:getDoctor())
local done = {}
local childContainers = {}
for i=1,containers:size() do
local container = containers:get(i-1)
done[container] = true
table.wipe(childContainers)
self:checkContainerItems(container, childContainers)
for _,container2 in ipairs(childContainers) do
if not done[container2] then
done[container2] = true
self:checkContainerItems(container2, nil)
end
end
end
end
function BaseHandler:checkContainerItems(container, childContainers)
local containerItems = container:getItems()
for i=1,containerItems:size() do
local item = containerItems:get(i-1)
if item:IsInventoryContainer() then
if childContainers then
table.insert(childContainers, item:getInventory())
end
else
---@diagnostic disable-next-line: undefined-field
self:checkItem(item) -- This is in inherited classes, we never use this class by itself
end
end
end
function BaseHandler:dropItems(items)
return false
end
function BaseHandler:addItem(items, item)
table.insert(items, item)
end
function BaseHandler:getAllItemTypes(items)
local done = {}
local types = {}
for _,item in ipairs(items) do
if not done[item:getFullType()] then
table.insert(types, item:getFullType())
done[item:getFullType()] = true
end
end
return types
end
function BaseHandler:getItemOfType(items, type)
for _,item in ipairs(items) do
if item:getFullType() == type then
return item
end
end
return nil
end
function BaseHandler:getItemOfTag(items, type)
for _,item in ipairs(items) do
if item:hasTag(type) then
return item
end
end
return nil
end
function BaseHandler:getAllItemsOfType(items, type)
local items = {}
for _,item in ipairs(items) do
if item:getFullType() == type then
table.insert(items, item)
end
end
return items
end
function BaseHandler:onMenuOptionSelected(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8)
ISTimedActionQueue.add(HealthPanelAction:new(self:getDoctor(), self, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8))
end
function BaseHandler:toPlayerInventory(item, previousAction)
if item:getContainer() ~= self:getDoctor():getInventory() then
local action = ISInventoryTransferAction:new(self:getDoctor(), item, item:getContainer(), self:getDoctor():getInventory())
ISTimedActionQueue.addAfter(previousAction, action)
-- FIXME: ISHealthPanel.actions never gets cleared
self.panel.actions = self.panel.actions or {}
self.panel.actions[action] = self.bodyPart
return action
end
return previousAction
end
function BaseHandler:getDoctor()
return self.panel.otherPlayer or self.panel.character
end
function BaseHandler:getPatient()
return self.panel.character
end
return BaseHandler

View File

@@ -0,0 +1,79 @@
local BaseHandler = require("TOC/UI/Interactions/HealthPanelBaseHandler")
local CommonMethods = require("TOC/CommonMethods")
local DataController = require("TOC/Controllers/DataController")
local CleanWoundAction = require("TOC/TimedActions/CleanWoundAction")
-------------------------
---@class WoundCleaningHandler : BaseHandler
---@field username string
---@field limbName string
local WoundCleaningHandler = BaseHandler:derive("WoundCleaningHandler")
---comment
---@param panel any
---@param bodyPart any
---@param username string
---@return table
function WoundCleaningHandler:new(panel, bodyPart, username)
local o = BaseHandler.new(self, panel, bodyPart)
o.items.ITEMS = {}
o.username = username
o.limbName = CommonMethods.GetLimbNameFromBodyPart(bodyPart)
return o
end
function WoundCleaningHandler:checkItem(item)
if item:getBandagePower() >= 2 then
self:addItem(self.items.ITEMS, item)
end
end
function WoundCleaningHandler:addToMenu(context)
local types = self:getAllItemTypes(self.items.ITEMS)
if #types > 0 and self:isValid() then
local option = context:addOption("Clean Wound", nil)
local subMenu = context:getNew(context)
context:addSubMenu(option, subMenu)
for i=1,#types do
local item = self:getItemOfType(self.items.ITEMS, types[i])
subMenu:addOption(item:getName(), self, self.onMenuOptionSelected, item:getFullType())
end
end
end
function WoundCleaningHandler:dropItems(items)
local types = self:getAllItemTypes(items)
if #self.items.ITEMS > 0 and #types == 1 and self:isInjured() and self.bodyPart:isNeedBurnWash() then
-- FIXME: A bandage can be used to clean a burn or bandage it
self:onMenuOptionSelected(types[1])
return true
end
return false
end
function WoundCleaningHandler:isValid()
-- TODO Check if cut and not cicatrized and dirty
-- todo get username
if self.limbName == nil then return false end
local dcInst = DataController.GetInstance(self.username)
--and dcInst:getWoundDirtyness(self.limbName) > 0.1
return dcInst:getIsCut(self.limbName) and not dcInst:getIsCicatrized(self.limbName)
--return self:getItemOfType(self.items.ITEMS, itemType)
end
function WoundCleaningHandler:perform(previousAction, itemType)
local item = self:getItemOfType(self.items.ITEMS, itemType)
previousAction = self:toPlayerInventory(item, previousAction)
local action = CleanWoundAction:new(self:getDoctor(), self:getPatient(), item, self.bodyPart)
ISTimedActionQueue.addAfter(previousAction, action)
end
return WoundCleaningHandler