From 49b0c7bc52ce88596501c9f9c970e761ef4d103e Mon Sep 17 00:00:00 2001 From: dscarpac Date: Sat, 5 Oct 2024 07:52:34 -0500 Subject: [PATCH] draft --- .../TheRedClock2.814c79.json | 4 +- .../TheRedClock5.696894.json | 4 +- src/core/Global.ttslua | 45 +++++++ src/core/token/TokenManagerApi.ttslua | 14 +++ src/playercards/cards/TheRedClock.ttslua | 119 ++++++++++++++++++ src/util/TokenSpawnTool.ttslua | 46 +------ xml/playercards/TheRedClock.xml | 33 +++++ 7 files changed, 217 insertions(+), 48 deletions(-) create mode 100644 src/playercards/cards/TheRedClock.ttslua create mode 100644 xml/playercards/TheRedClock.xml diff --git a/objects/AllPlayerCards.15bb07/TheRedClock2.814c79.json b/objects/AllPlayerCards.15bb07/TheRedClock2.814c79.json index e4d782af..9eda219a 100644 --- a/objects/AllPlayerCards.15bb07/TheRedClock2.814c79.json +++ b/objects/AllPlayerCards.15bb07/TheRedClock2.814c79.json @@ -33,7 +33,7 @@ "IgnoreFoW": false, "LayoutGroupSortIndex": 0, "Locked": false, - "LuaScript": "", + "LuaScript": "require(\"playercards/cards/TheRedClock\")", "LuaScriptState": "", "MeasureMovement": false, "Name": "Card", @@ -58,5 +58,5 @@ "scaleZ": 1 }, "Value": 0, - "XmlUI": "" + "XmlUI": "\u003cInclude src=\"playercards/TheRedClock.xml\"/\u003e" } diff --git a/objects/AllPlayerCards.15bb07/TheRedClock5.696894.json b/objects/AllPlayerCards.15bb07/TheRedClock5.696894.json index 9a3907ce..02438919 100644 --- a/objects/AllPlayerCards.15bb07/TheRedClock5.696894.json +++ b/objects/AllPlayerCards.15bb07/TheRedClock5.696894.json @@ -33,7 +33,7 @@ "IgnoreFoW": false, "LayoutGroupSortIndex": 0, "Locked": false, - "LuaScript": "", + "LuaScript": "require(\"playercards/cards/TheRedClock\")", "LuaScriptState": "", "MeasureMovement": false, "Name": "Card", @@ -58,5 +58,5 @@ "scaleZ": 1 }, "Value": 0, - "XmlUI": "" + "XmlUI": "\u003cInclude src=\"playercards/TheRedClock.xml\"/\u003e" } diff --git a/src/core/Global.ttslua b/src/core/Global.ttslua index e8e3493c..08011f2d 100644 --- a/src/core/Global.ttslua +++ b/src/core/Global.ttslua @@ -2478,6 +2478,51 @@ function TokenManager.replenishTokens(card, useInfo) end end +-- adds a use to a card +---@param card tts__Object Card that should get a use added +---@param useType string Type of uses to be added +function TokenManager.addUseToCard(params) + + log(params.useType) + local card = params.card + local useType = params.useType + local metadata = JSON.decode(card.getGMNotes()) or {} + -- get correct data for location + if metadata.type == "Location" then + if not card.is_face_down and metadata.locationFront ~= nil then + metadata = metadata.locationFront + elseif metadata.locationBack ~= nil then + metadata = metadata.locationBack + end + + -- if there are no uses at all, add "empty" uses for fake replenishing (only for clues) + if metadata.uses == nil then + metadata.uses = { { token = "clue" } } + end + end + + local match = false + for _, useInfo in ipairs(metadata.uses or {}) do + if useInfo.token == useType then + -- artificially create replenish data to re-use that existing functionality + useInfo.count = 999 + useInfo.replenish = 1 + match = true + else + -- artificially disable other uses from replenishing + useInfo.replenish = nil + end + end + + -- if matching uses were found, perform the "fake" replenish + if match then + TokenManager.maybeReplenishCard({ card = card, uses = metadata.uses }) + return true + else + return false + end +end + --------------------------------------------------------- -- Callback functions for token spawning --------------------------------------------------------- diff --git a/src/core/token/TokenManagerApi.ttslua b/src/core/token/TokenManagerApi.ttslua index 42b6e51c..f9a87e70 100644 --- a/src/core/token/TokenManagerApi.ttslua +++ b/src/core/token/TokenManagerApi.ttslua @@ -96,5 +96,19 @@ do }) end + -- adds a use to a card + ---@param card tts__Object Card that should get a use added + ---@param useType string Type of uses to be added + function TokenManagerApi.addUseToCard(card, useType) + local result = Global.call("callTable", { + { "TokenManager", "addUseToCard" }, + { + card = card, + useType = useType + } + }) + return result + end + return TokenManagerApi end diff --git a/src/playercards/cards/TheRedClock.ttslua b/src/playercards/cards/TheRedClock.ttslua new file mode 100644 index 00000000..02d1631b --- /dev/null +++ b/src/playercards/cards/TheRedClock.ttslua @@ -0,0 +1,119 @@ +require("playercards/CardsWithHelper") +local guidReferenceApi = require("core/GUIDReferenceApi") +local playermatApi = require("playermat/PlayermatApi") +local searchLib = require("util/SearchLib") +local tokenManagerApi = require("core/token/TokenManagerApi") + + +-- intentionally global +hasXML = true +isHelperEnabled = false + +function onLoad(savedData) + -- get level via metadata and proceed accordingly: + -- lvl 2: one action + -- lvl 5: two actions + + notes = JSON.decode(self.getGMNotes()) + if savedData and savedData ~= "" then + local loadedData = JSON.decode(savedData) + isHelperEnabled = loadedData.isHelperEnabled + end + syncDisplayWithOptionPanel() +end + +function updateSave() + self.script_state = JSON.encode({ + isHelperEnabled = isHelperEnabled + }) +end + +function addCharge(player) + tokenManagerApi.addUseToCard(self, "resource") + Wait.time(function() searchSelf() end, 5) + log(foundTokens) + if foundTokens == 3 then + addAction(player) + end +end + +function searchSelf(destroy) + clickableResourceCounter = nil + foundTokens = 0 + + for _, obj in ipairs(searchLib.onObject(self, "isTileOrToken")) do + local image = obj.getCustomObject().image + if image == "https://steamusercontent-a.akamaihd.net/ugc/2028355744161230179/E3A30A2D661A12205D98D4D0E374591586C5C486/" then + foundTokens = foundTokens + math.abs(obj.getQuantity()) + if destroy then obj.destruct() end + return foundTokens + elseif obj.getMemo() == "resourceCounter" then + foundTokens = obj.getVar("val") + clickableResourceCounter = obj + return foundTokens + end + end +end + +function takeAll(player) + local destroy = true + searchSelf(destroy) + local matColor = playermatApi.getMatColorByPosition(self.getPosition()) + playermatApi.updateCounter(matColor, "ResourceCounter", _, foundTokens) + + if clickableResourceCounter then + clickableResourceCounter.call("updateVal", 0) + end + + if notes.id == "08058" then + Wait.frames(function() addCharge(player) end, 2) + broadcastToColor("Moved " .. foundTokens .. " resource(s) to " .. matColor .. "'s resource pool and placed one charge.", player.color) + else + broadcastToColor("Moved " .. foundTokens .. " resource(s) to " .. matColor .. "'s resource pool.", player.color) + end +end + +function addAction(player) + if notes.id == "08058" then + spawnActionToken(2) + broadcastToColor("Spawning two temporary action tokens.", player.color) + else + spawnActionToken(1) + broadcastToColor("Spawning one temporary action token.", player.color) + end +end + +function spawnActionToken(numTokens) + local position = self.getPosition() + local matColor = playermatApi.getMatColorByPosition(position) + local mat = guidReferenceApi.getObjectByOwnerAndType(matColor, "Playermat") + local rotation = mat.getRotation() + + -- find empty action token slots by checking snap points + local snaps = mat.getSnapPoints() + + -- get empty slots + local emptyPositions = {} + for i, snap in ipairs(snaps) do + if i > 1 then + if snap.tags[1] == "UniversalToken" then + local snapPos = mat.positionToWorld(snap.position) + local searchResult = searchLib.atPosition(snapPos, "isUniversalToken") + if #searchResult == 0 then + table.insert(emptyPositions, snapPos) + end + end + end + end + + local activeInvestigatorData = playermatApi.getActiveInvestigatorData(matColor) + local callbackParams = { class = activeInvestigatorData.class, symbol = activeInvestigatorData.class, addTag = "Temporary" } + local callbackName = "updateUniversalActionAbilityToken" + for i = 1, numTokens do + if emptyPositions[i] ~= nil then + tokenManagerApi.spawnToken(emptyPositions[i] + Vector(0, 0.7, 0), "universalActionAbility", rotation, callbackName, callbackParams) + else + tokenManagerApi.spawnToken(position + Vector(0, 0.7, 0), "universalActionAbility", rotation, callbackName, callbackParams) + end + end +end diff --git a/src/util/TokenSpawnTool.ttslua b/src/util/TokenSpawnTool.ttslua index f52cc436..8e0918a3 100644 --- a/src/util/TokenSpawnTool.ttslua +++ b/src/util/TokenSpawnTool.ttslua @@ -28,7 +28,7 @@ function onScriptingButtonDown(index, playerColor) local card = getTargetCard(playerColor, position) if card and not card.is_face_down then - local status = addUseToCard(card, tokenType) + local status = tokenManagerApi.addUseToCard(card, tokenType) if status == true then return end end @@ -37,7 +37,7 @@ function onScriptingButtonDown(index, playerColor) local card = getTargetCard(playerColor, position) if card and (not card.is_face_down or card.hasTag("Location")) then - local status = addUseToCard(card, tokenType) + local status = tokenManagerApi.addUseToCard(card, tokenType) if status == true then return end end @@ -85,45 +85,3 @@ function getTargetCard(playerColor, position) end end end - --- adds a use to a card (TODO: probably move this to the TokenManager?) ----@param card tts__Object Card that should get a use added ----@param useType string Type of uses to be added -function addUseToCard(card, useType) - local metadata = JSON.decode(card.getGMNotes()) or {} - - -- get correct data for location - if metadata.type == "Location" then - if not card.is_face_down and metadata.locationFront ~= nil then - metadata = metadata.locationFront - elseif metadata.locationBack ~= nil then - metadata = metadata.locationBack - end - - -- if there are no uses at all, add "empty" uses for fake replenishing (only for clues) - if metadata.uses == nil then - metadata.uses = { { token = "clue" } } - end - end - - local match = false - for _, useInfo in ipairs(metadata.uses or {}) do - if useInfo.token == useType then - -- artificially create replenish data to re-use that existing functionality - useInfo.count = 999 - useInfo.replenish = 1 - match = true - else - -- artificially disable other uses from replenishing - useInfo.replenish = nil - end - end - - -- if matching uses were found, perform the "fake" replenish - if match then - tokenManagerApi.maybeReplenishCard(card, metadata.uses) - return true - else - return false - end -end diff --git a/xml/playercards/TheRedClock.xml b/xml/playercards/TheRedClock.xml new file mode 100644 index 00000000..25f49bd9 --- /dev/null +++ b/xml/playercards/TheRedClock.xml @@ -0,0 +1,33 @@ + +