Unified token discarding

This commit is contained in:
Chr1Z93 2024-11-18 23:41:19 +01:00
parent d898676b6a
commit 7e26a25d7d
4 changed files with 39 additions and 47 deletions

View File

@ -2869,11 +2869,7 @@ function handleTokenAttaching(params)
end
Wait.condition(
function()
if card ~= nil then
handleTokenDetaching({ card = card })
end
end,
function() handleTokenDetaching({ card = card }) end,
function()
if card ~= nil and player ~= nil and player.seated then
return card.resting and not tableContains(player.getHoldingObjects(), card)
@ -2886,7 +2882,7 @@ end
function handleTokenDetaching(params)
local card = params.card
if next(card.getAttachments()) == nil then return end
if card == nil or next(card.getAttachments()) == nil then return end
-- restore card settings
if cardSetting[card] ~= nil then
@ -2912,3 +2908,29 @@ function handleTokenDetaching(params)
end
end
end
-- removes tokens from the provided card/deck
function removeTokensFromObject(params)
local object = params.object
local trash = guidReferenceApi.getObjectByOwnerAndType(params.owner, "Trash")
if object.hasTag("CardThatSeals") then
local func = object.getVar("resetSealedTokens") -- check if function exists (it won't for older custom content)
if func ~= nil then
object.call("resetSealedTokens")
end
end
for _, obj in ipairs(searchLib.onObject(object)) do
if tokenChecker.isChaosToken(obj) then
returnChaosTokenToBag( { token = obj, fromBag = false } )
elseif obj.getGUID() ~= "4ee1f2" and -- table
obj ~= self and
obj.type ~= "Deck" and
obj.type ~= "Card" and
obj.memo ~= nil and
obj.getLock() == false then
trash.putObject(obj)
end
end
end

View File

@ -59,6 +59,13 @@ do
Global.call("handleTokenDetaching", { card = card })
end
-- discards tokens from an object
---@param object tts__Object Object that should get tokens removed
---@param owner string Owner of this object (for discarding)
function GlobalApi.removeTokensFromObject(object, owner)
Global.call("removeTokensFromObject", { object = object, owner = owner })
end
-- loads saved options
---@param options table Set a new state for the option table
function GlobalApi.loadOptionPanelSettings(options)

View File

@ -106,7 +106,7 @@ function onCollisionEnter(collisionInfo)
-- reset spawned tokens and remove tokens from cards in encounter deck / discard area
Wait.frames(function() tokenSpawnTrackerApi.resetTokensSpawned(object) end, 1)
GlobalApi.handleTokenDetaching(object)
removeTokensFromObject(object)
GlobalApi.removeTokensFromObject(object, "Mythos")
elseif inArea(localPos, SCENARIO_REFERENCE_AREA) then
-- detect scenario reference card and attempt to load data from it
@ -167,7 +167,7 @@ function onObjectEnterContainer(container, object)
local localPos = self.positionToLocal(container.getPosition())
if inArea(localPos, ENCOUNTER_DECK_AREA) or inArea(localPos, ENCOUNTER_DISCARD_AREA) then
tokenSpawnTrackerApi.resetTokensSpawned(object)
removeTokensFromObject(object)
GlobalApi.removeTokensFromObject(object, "Mythos")
end
end
@ -313,17 +313,3 @@ function inArea(point, bounds)
and point.z > bounds.upperLeft.z
and point.z < bounds.lowerRight.z)
end
-- removes tokens from the provided card/deck
function removeTokensFromObject(object)
local TRASH = guidReferenceApi.getObjectByOwnerAndType("Mythos", "Trash")
for _, obj in ipairs(searchLib.onObject(object, "isTileOrToken")) do
if obj.getGUID() ~= "4ee1f2" and -- table
obj ~= self and
obj.memo ~= nil and
obj.getLock() == false and
not tokenChecker.isChaosToken(obj) then
TRASH.putObject(obj)
end
end
end

View File

@ -1300,7 +1300,7 @@ function onCollisionEnter(collisionInfo)
elseif inArea(localCardPos, DECK_DISCARD_AREA) then
GlobalApi.handleTokenDetaching(object)
tokenSpawnTrackerApi.resetTokensSpawned(object)
removeTokensFromObject(object)
GlobalApi.removeTokensFromObject(object, matColor)
elseif object.is_face_down == false then
-- main uses spawning
@ -1341,30 +1341,7 @@ function onObjectEnterContainer(container, object)
local localCardPos = self.positionToLocal(object.getPosition())
if inArea(localCardPos, DECK_DISCARD_AREA) then
tokenSpawnTrackerApi.resetTokensSpawned(object)
removeTokensFromObject(object)
end
end
-- removes tokens from the provided card/deck
function removeTokensFromObject(object)
if object.hasTag("CardThatSeals") then
local func = object.getVar("resetSealedTokens") -- check if function exists (it won't for older custom content)
if func ~= nil then
object.call("resetSealedTokens")
end
end
for _, obj in ipairs(searchLib.onObject(object)) do
if tokenChecker.isChaosToken(obj) then
chaosBagApi.returnChaosTokenToBag(obj, false)
elseif obj.getGUID() ~= "4ee1f2" and -- table
obj ~= self and
obj.type ~= "Deck" and
obj.type ~= "Card" and
obj.memo ~= nil and
obj.getLock() == false then
ownedObjects.Trash.putObject(obj)
end
GlobalApi.removeTokensFromObject(object, matColor)
end
end