From 8cc5170c1081a1917fb670cae71b7c688773edaf Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Thu, 28 Sep 2023 19:44:54 +0200 Subject: [PATCH 1/2] added option to include drawn tokens --- objects/TokenArranger.022907.luascriptstate | 2 +- src/accessories/TokenArranger.ttslua | 78 +++++++++++++-------- src/chaosbag/ChaosBagApi.ttslua | 5 ++ src/core/Global.ttslua | 4 +- 4 files changed, 59 insertions(+), 30 deletions(-) diff --git a/objects/TokenArranger.022907.luascriptstate b/objects/TokenArranger.022907.luascriptstate index 638cf57f..919c5c6c 100644 --- a/objects/TokenArranger.022907.luascriptstate +++ b/objects/TokenArranger.022907.luascriptstate @@ -1 +1 @@ -{"percentage":false,"tokenPrecedence":{"":[0,11],"Auto-fail":[-100,7],"Bless":[101,8],"Cultist":[-2,4],"Curse":[-101,9],"Elder Sign":[100,2],"Elder Thing":[-4,6],"Frost":[-99,10],"Skull":[-1,3],"Tablet":[-3,5]}} +{"includeDrawnTokens":true,"percentage":false,"tokenPrecedence":{"":[0,11],"Auto-fail":[-100,7],"Bless":[101,8],"Cultist":[-2,4],"Curse":[-101,9],"Elder Sign":[100,2],"Elder Thing":[-4,6],"Frost":[-99,10],"Skull":[-1,3],"Tablet":[-3,5]}} diff --git a/src/accessories/TokenArranger.ttslua b/src/accessories/TokenArranger.ttslua index 8111b455..282d940d 100644 --- a/src/accessories/TokenArranger.ttslua +++ b/src/accessories/TokenArranger.ttslua @@ -26,12 +26,13 @@ percentageLabel.width = 0 percentageLabel.height = 0 -- variables with save function -local tokenPrecedence = {} -local percentage = false +local tokenPrecedence = {} +local percentage = false +local includeDrawnTokens = true -- variables without save function -local updating = false -local TOKEN_NAMES = { +local updating = false +local TOKEN_NAMES = { "Elder Sign", "Skull", "Cultist", @@ -47,17 +48,19 @@ local TOKEN_NAMES = { -- saving the precedence settings and information on the most recently loaded data function onSave() return JSON.encode({ - tokenPrecedence = tokenPrecedence, - percentage = percentage + tokenPrecedence = tokenPrecedence, + percentage = percentage, + includeDrawnTokens = includeDrawnTokens }) end -- loading data, button creation and initial layouting function onLoad(saveState) if saveState ~= nil and saveState ~= "" then - local loadedData = JSON.decode(saveState) - tokenPrecedence = loadedData.tokenPrecedence - percentage = loadedData.percentage + local loadedData = JSON.decode(saveState) + tokenPrecedence = loadedData.tokenPrecedence + percentage = loadedData.percentage + includeDrawnTokens = loadedData.includeDrawnTokens else loadDefaultValues() @@ -74,23 +77,32 @@ function onLoad(saveState) layout() end) - self.addContextMenuItem("Toggle percentages", function() + self.addContextMenuItem("Include drawn tokens", function() + includeDrawnTokens = not includeDrawnTokens + local text = includeDrawnTokens and " " or " not " + broadcastToAll("Token Arranger will" .. text .. "include currently drawn chaos tokens.", "Orange") + layout() + end) + + self.addContextMenuItem("Toggle percentages", function() if percentage then percentage = false else percentage = "basic" - broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", "Yellow") + broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", + "Yellow") end layout() end) - self.addContextMenuItem("Toggle cumulative", function() + self.addContextMenuItem("Toggle cumulative", function() if percentage == "cumulative" then percentage = "basic" else percentage = "cumulative" end - broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", "Yellow") + broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", + "Yellow") layout() end) end @@ -156,7 +168,7 @@ function createButtonsAndInputs() inputParameters.position = { pos.x[1] + offset, 0.1, pos.z + i * offset } else buttonParameters.position = { pos.x[2], 0, pos.z + (i - 5) * offset } - inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset } + inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset } end buttonParameters.click_function = "tokenClick" .. i @@ -226,23 +238,23 @@ function createPercentageButton(tokenCount, valueCount, tokenName) local startPos = Vector(2.3, -0.05, 0.875 * valueCount) if percentage == "cumulative" then - percentageLabel.scale = {1.5, 1.5, 1.5} + percentageLabel.scale = { 1.5, 1.5, 1.5 } percentageLabel.position = startPos - Vector(0, 0, 2.85) else - percentageLabel.scale = {2, 2, 2} + percentageLabel.scale = { 2, 2, 2 } percentageLabel.position = startPos - Vector(0, 0, 2.675) end -- determine font_color if tokenName == "Elder Sign" then - percentageLabel.font_color = {0.35, 0.71, 0.85} + percentageLabel.font_color = { 0.35, 0.71, 0.85 } elseif tokenName == "Auto-fail" then - percentageLabel.font_color = {0.86, 0.1, 0.1} - -- check if the tokenName contains letters (e.g. symbol token) + percentageLabel.font_color = { 0.86, 0.1, 0.1 } + -- check if the tokenName contains letters (e.g. symbol token) elseif string.match(tokenName, "%a") ~= nil then - percentageLabel.font_color = {0.68, 0.53, 0.86} + percentageLabel.font_color = { 0.68, 0.53, 0.86 } else - percentageLabel.font_color = {0.85, 0.67, 0.33} + percentageLabel.font_color = { 0.85, 0.67, 0.33 } end -- create label for base percentage @@ -253,8 +265,8 @@ function createPercentageButton(tokenCount, valueCount, tokenName) -- optionally create label for cumulative percentage if percentage == "cumulative" then percentageLabel.position = startPos - Vector(0, 0, 2.45) - percentageLabel.font_color = {1, 1, 1} - + percentageLabel.font_color = { 1, 1, 1 } + -- only display one digit for 100% if tokenCount.sum == tokenCount.total then percentageLabel.label = "100.0%" @@ -278,7 +290,7 @@ function layout(_, _, isRightClick) return end - -- get ChaosBag and stop if not found + -- get ChaosBag and stop if not found local chaosBag = chaosBagApi.findChaosBag() if not chaosBag then updating = false @@ -286,8 +298,18 @@ function layout(_, _, isRightClick) end -- clone tokens from chaos bag (default position above trash can) + local rawData = chaosBag.getData().ContainedObjects + + -- optionally get the data for tokens in play + if includeDrawnTokens then + for _, token in pairs(chaosBagApi.getTokensInPlay()) do + if token ~= nil then table.insert(rawData, token.getData()) end + end + end + + -- generate layout data local data = {} - for i, objData in ipairs(chaosBag.getData().ContainedObjects) do + for i, objData in ipairs(rawData) do objData["Tags"] = { "tempToken" } local value = tonumber(objData.Nickname) local precedence = tokenPrecedence[objData.Nickname] @@ -316,7 +338,7 @@ function layout(_, _, isRightClick) local rotation = self.getRotation() local currentValue = data[1].value local tokenCount = { row = 0, sum = 0, total = #data } - local valueCount = 1 + local valueCount = 1 local tokenName = false for i, item in ipairs(data) do @@ -339,8 +361,8 @@ function layout(_, _, isRightClick) position = location, rotation = rotation }) - tokenName = item.token.Nickname - location.z = location.z - 1.75 + tokenName = item.token.Nickname + location.z = location.z - 1.75 tokenCount.row = tokenCount.row + 1 end diff --git a/src/chaosbag/ChaosBagApi.ttslua b/src/chaosbag/ChaosBagApi.ttslua index e0252266..3ca84e94 100644 --- a/src/chaosbag/ChaosBagApi.ttslua +++ b/src/chaosbag/ChaosBagApi.ttslua @@ -23,6 +23,11 @@ do return Global.call("findChaosBag") end + -- returns a table of object references to the tokens in play (does not include sealed tokens!) + ChaosBagApi.getTokensInPlay = function() + return Global.getTable("chaosTokens") + end + -- returns all sealed tokens on cards to the chaos bag ChaosBagApi.releaseAllSealedTokens = function(playerColor) return Global.call("releaseAllSealedTokens", playerColor) diff --git a/src/core/Global.ttslua b/src/core/Global.ttslua index 0ac0f349..aa7415e9 100644 --- a/src/core/Global.ttslua +++ b/src/core/Global.ttslua @@ -32,8 +32,10 @@ local NOT_INTERACTABLE = { "975c39", -- vertical border right } -local chaosTokens = {} +-- global variable for access +chaosTokens = {} local chaosTokensLastMat = nil + local bagSearchers = {} local MAT_COLORS = {"White", "Orange", "Green", "Red"} local hideTitleSplashWaitFunctionId = nil From c13582f5b0d33ae43f6b41f493e3cb21986603e8 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Thu, 28 Sep 2023 19:49:10 +0200 Subject: [PATCH 2/2] fixed some auto-formatting --- src/accessories/TokenArranger.ttslua | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/accessories/TokenArranger.ttslua b/src/accessories/TokenArranger.ttslua index 282d940d..8be13a39 100644 --- a/src/accessories/TokenArranger.ttslua +++ b/src/accessories/TokenArranger.ttslua @@ -89,8 +89,7 @@ function onLoad(saveState) percentage = false else percentage = "basic" - broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", - "Yellow") + broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", "Yellow") end layout() end) @@ -101,8 +100,7 @@ function onLoad(saveState) else percentage = "cumulative" end - broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", - "Yellow") + broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", "Yellow") layout() end) end @@ -349,10 +347,10 @@ function layout(_, _, isRightClick) createPercentageButton(tokenCount, valueCount, tokenName) end - location.x = location.x - 1.75 - location.z = pos.z - currentValue = item.value - valueCount = valueCount + 1 + location.x = location.x - 1.75 + location.z = pos.z + currentValue = item.value + valueCount = valueCount + 1 tokenCount.row = 0 end @@ -361,8 +359,8 @@ function layout(_, _, isRightClick) position = location, rotation = rotation }) - tokenName = item.token.Nickname - location.z = location.z - 1.75 + tokenName = item.token.Nickname + location.z = location.z - 1.75 tokenCount.row = tokenCount.row + 1 end