ah_sce_unpacked/unpacked/Custom_Token Token Arranger 022907.ttslua

611 lines
19 KiB
Plaintext
Raw Normal View History

2023-04-22 16:56:01 -04:00
-- Bundled by luabundle {"version":"1.6.0"}
local __bundle_require, __bundle_loaded, __bundle_register, __bundle_modules = (function(superRequire)
local loadingPlaceholder = {[{}] = true}
local register
local modules = {}
local require
local loaded = {}
register = function(name, body)
if not modules[name] then
modules[name] = body
end
end
require = function(name)
local loadedModule = loaded[name]
if loadedModule then
if loadedModule == loadingPlaceholder then
return nil
end
else
if not modules[name] then
if not superRequire then
local identifier = type(name) == 'string' and '\"' .. name .. '\"' or tostring(name)
error('Tried to require ' .. identifier .. ', but no such module has been registered')
else
return superRequire(name)
end
end
loaded[name] = loadingPlaceholder
loadedModule = modules[name](require, loaded, register, modules)
loaded[name] = loadedModule
end
return loadedModule
end
return require, loaded, register, modules
end)(nil)
__bundle_register("__root", function(require, _LOADED, __bundle_register, __bundle_modules)
require("accessories/TokenArranger")
end)
__bundle_register("accessories/TokenArranger", function(require, _LOADED, __bundle_register, __bundle_modules)
2024-01-06 21:32:07 -05:00
local chaosBagApi = require("chaosbag/ChaosBagApi")
2023-04-22 16:56:01 -04:00
local mythosAreaApi = require("core/MythosAreaApi")
-- common parameters
local buttonParameters = {}
buttonParameters.function_owner = self
buttonParameters.label = ""
buttonParameters.tooltip = "Increase / Decrease"
buttonParameters.color = { 0, 0, 0, 0 }
buttonParameters.width = 325
buttonParameters.height = 325
local inputParameters = {}
inputParameters.function_owner = self
inputParameters.font_size = 100
inputParameters.width = 250
inputParameters.height = inputParameters.font_size + 23
inputParameters.alignment = 3
inputParameters.validation = 2
inputParameters.tab = 2
2024-01-06 21:32:07 -05:00
local percentageLabel = {}
percentageLabel.function_owner = self
percentageLabel.click_function = "none"
percentageLabel.width = 0
percentageLabel.height = 0
2023-04-22 16:56:01 -04:00
-- variables with save function
2024-01-06 21:32:07 -05:00
local tokenPrecedence = {}
local percentage = false
local includeDrawnTokens = true
2023-04-22 16:56:01 -04:00
-- variables without save function
2024-01-06 21:32:07 -05:00
local updating = false
local TOKEN_NAMES = {
2023-04-22 16:56:01 -04:00
"Elder Sign",
"Skull",
"Cultist",
"Tablet",
"Elder Thing",
"Auto-fail",
"Bless",
"Curse",
"Frost",
""
}
-- saving the precedence settings and information on the most recently loaded data
function onSave()
return JSON.encode({
2024-01-06 21:32:07 -05:00
tokenPrecedence = tokenPrecedence,
percentage = percentage,
includeDrawnTokens = includeDrawnTokens
2023-04-22 16:56:01 -04:00
})
end
-- loading data, button creation and initial layouting
function onLoad(saveState)
if saveState ~= nil and saveState ~= "" then
2024-01-06 21:32:07 -05:00
local loadedData = JSON.decode(saveState)
tokenPrecedence = loadedData.tokenPrecedence
percentage = loadedData.percentage
includeDrawnTokens = loadedData.includeDrawnTokens
2023-04-22 16:56:01 -04:00
else
loadDefaultValues()
2023-08-27 21:09:46 -04:00
-- grab token metadata from mythos area
Wait.time(function() onTokenDataChanged(mythosAreaApi.returnTokenData()) end, 0.2)
2023-04-22 16:56:01 -04:00
end
2024-01-06 21:32:07 -05:00
createButtonsAndInputs()
2024-01-06 21:32:29 -05:00
-- maybe trigger layout() to draw percentage buttons
local objList = getObjectsWithTag("tempToken")
if #objList > 0 then
Wait.time(layout, 0.5)
end
2023-04-22 16:56:01 -04:00
-- context menu items
self.addContextMenuItem("Load default values", function()
loadDefaultValues()
updateUI()
layout()
end)
2024-01-06 21:32:07 -05:00
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()
2023-04-22 16:56:01 -04:00
if percentage then
percentage = false
else
percentage = "basic"
2023-08-27 21:09:46 -04:00
broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", "Yellow")
2023-04-22 16:56:01 -04:00
end
layout()
end)
2024-01-06 21:32:07 -05:00
self.addContextMenuItem("Toggle cumulative", function()
2023-04-22 16:56:01 -04:00
if percentage == "cumulative" then
percentage = "basic"
else
percentage = "cumulative"
end
2023-08-27 21:09:46 -04:00
broadcastToAll("Percentages are unreliable when using tokens that draw other tokens (bless or curse for example).", "Yellow")
2023-04-22 16:56:01 -04:00
layout()
end)
end
-- delete temporary tokens when destroyed
function onDestroy() deleteCopiedTokens() end
-- layout tokens when dropped (after 1.5 seconds)
function onDrop() Wait.time(layout, 1.5) end
-- delete temporary tokens when picked up
function onPickUp() deleteCopiedTokens() end
-- click_function for buttons on chaos tokens
function tokenClick(isRightClick, index)
local change = tonumber(isRightClick and "-1" or "1")
tokenPrecedence[TOKEN_NAMES[index]][1] = tokenPrecedence[TOKEN_NAMES[index]][1] + change
self.editInput({
index = index - 1,
value = tokenPrecedence[TOKEN_NAMES[index]][1]
})
layout()
end
-- input_function for input_boxes
function tokenInput(input, selected, index)
if selected == false then
local num = tonumber(input)
if num ~= nil then
tokenPrecedence[TOKEN_NAMES[index]][1] = num
end
layout()
end
end
-- loads the default precedence table
function loadDefaultValues()
-- 1st value: token modifiers for sorting
-- 2nd value: order for equivalent tokens (starts at 2 because of "+1" token)
tokenPrecedence = {
["Elder Sign"] = { 100, 2},
["Skull"] = { -1, 3},
["Cultist"] = { -2, 4},
["Tablet"] = { -3, 5},
["Elder Thing"] = { -4, 6},
["Auto-fail"] = { -100, 7},
["Bless"] = { 101, 8},
["Curse"] = { -101, 9},
["Frost"] = { -99, 10},
[""] = { 0, 11}
}
end
2024-01-06 21:32:07 -05:00
-- creates buttons and inputs
function createButtonsAndInputs()
2023-04-22 16:56:01 -04:00
local offset = 0.725
local pos = { x = { -1.067, 0.377 }, z = -2.175 }
-- button and inputs index 0-9
for i = 1, 10 do
if i < 6 then
buttonParameters.position = { pos.x[1], 0, pos.z + i * offset }
inputParameters.position = { pos.x[1] + offset, 0.1, pos.z + i * offset }
else
buttonParameters.position = { pos.x[2], 0, pos.z + (i - 5) * offset }
2024-01-06 21:32:07 -05:00
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
2023-04-22 16:56:01 -04:00
end
2024-01-06 21:32:07 -05:00
buttonParameters.click_function = "tokenClick" .. i
inputParameters.input_function = "tokenInput" .. i
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
2023-04-22 16:56:01 -04:00
2024-01-06 21:32:07 -05:00
-- setting click-/inputfunction
self.setVar(buttonParameters.click_function, function(_, _, isRightClick) tokenClick(isRightClick, i) end)
self.setVar(inputParameters.input_function, function(_, _, input, selected) tokenInput(input, selected, i) end)
-- button/input creation
self.createButton(buttonParameters)
self.createInput(inputParameters)
2023-04-22 16:56:01 -04:00
end
-- index 10: "Update / Hide" button
self.createButton({
function_owner = self,
label = "Update / Hide",
click_function = "layout",
tooltip = "Left-Click: Update!\nRight-Click: Hide Tokens!",
position = { 0.725, 0.1, 2.025 },
color = { 1, 1, 1 },
width = 675,
height = 175
})
end
-- update input fields
function updateUI()
for i = 1, 10 do
self.editInput({
index = i - 1,
value = tokenPrecedence[TOKEN_NAMES[i]][1]
})
end
end
-- order function for data sorting
function tokenValueComparator(left, right)
2024-01-06 21:32:07 -05:00
if left.value ~= right.value then
2023-04-22 16:56:01 -04:00
return left.value > right.value
elseif left.order ~= right.order then
return left.order < right.order
else
return false
end
end
-- deletes previously placed tokens
function deleteCopiedTokens()
for _, token in ipairs(getObjectsWithTag("tempToken")) do
token.destruct()
end
2024-01-06 21:32:07 -05:00
-- this removes the percentage buttons (by index 11+)
local buttonCount = #self.getButtons()
if buttonCount < 12 then return end
for i = buttonCount, 12, -1 do
self.removeButton(i - 1)
end
2023-04-22 16:56:01 -04:00
end
2023-08-27 21:09:46 -04:00
-- creates buttons as labels as display for percentage values
function createPercentageButton(tokenCount, valueCount, tokenName)
2024-01-06 21:32:29 -05:00
local startPos = Vector(2.3, -0.04, 0.875 * valueCount)
2023-04-22 16:56:01 -04:00
if percentage == "cumulative" then
2024-01-06 21:32:07 -05:00
percentageLabel.scale = { 1.5, 1.5, 1.5 }
2023-08-27 21:09:46 -04:00
percentageLabel.position = startPos - Vector(0, 0, 2.85)
else
2024-01-06 21:32:07 -05:00
percentageLabel.scale = { 2, 2, 2 }
2023-08-27 21:09:46 -04:00
percentageLabel.position = startPos - Vector(0, 0, 2.675)
end
-- determine font_color
if tokenName == "Elder Sign" then
2024-01-06 21:32:07 -05:00
percentageLabel.font_color = { 0.35, 0.71, 0.85 }
2023-08-27 21:09:46 -04:00
elseif tokenName == "Auto-fail" then
2024-01-06 21:32:07 -05:00
percentageLabel.font_color = { 0.86, 0.1, 0.1 }
-- check if the tokenName contains letters (e.g. symbol token)
2023-08-27 21:09:46 -04:00
elseif string.match(tokenName, "%a") ~= nil then
2024-01-06 21:32:07 -05:00
percentageLabel.font_color = { 0.68, 0.53, 0.86 }
2023-04-22 16:56:01 -04:00
else
2024-01-06 21:32:07 -05:00
percentageLabel.font_color = { 0.85, 0.67, 0.33 }
2023-04-22 16:56:01 -04:00
end
2023-08-27 21:09:46 -04:00
-- create label for base percentage
local basePercentage = math.floor((tokenCount.row / tokenCount.total) * 10000) / 100
percentageLabel.label = string.format("%s", string.format("%05.2f", basePercentage) .. "%")
self.createButton(percentageLabel)
-- optionally create label for cumulative percentage
if percentage == "cumulative" then
percentageLabel.position = startPos - Vector(0, 0, 2.45)
2024-01-06 21:32:07 -05:00
percentageLabel.font_color = { 1, 1, 1 }
2023-04-22 16:56:01 -04:00
-- only display one digit for 100%
2023-08-27 21:09:46 -04:00
if tokenCount.sum == tokenCount.total then
percentageLabel.label = "100.0%"
2023-04-22 16:56:01 -04:00
else
2023-08-27 21:09:46 -04:00
local cumulativePercentage = math.floor((tokenCount.sum / tokenCount.total) * 10000) / 100
percentageLabel.label = string.format("%s", string.format("%05.2f", cumulativePercentage) .. "%")
2023-04-22 16:56:01 -04:00
end
2023-08-27 21:09:46 -04:00
self.createButton(percentageLabel)
2023-04-22 16:56:01 -04:00
end
end
-- main function (delete old tokens, clone chaos bag content, sort it and position it)
function layout(_, _, isRightClick)
if updating then return end
updating = true
deleteCopiedTokens()
-- stop here if right-clicked
if isRightClick then
updating = false
return
end
2024-01-06 21:32:07 -05:00
-- get ChaosBag and stop if not found
local chaosBag = chaosBagApi.findChaosBag()
if not chaosBag then
updating = false
return
end
2023-04-22 16:56:01 -04:00
-- clone tokens from chaos bag (default position above trash can)
2024-01-06 21:32:07 -05:00
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(rawData) do
objData["Tags"] = { "tempToken" }
local value = tonumber(objData.Nickname)
local precedence = tokenPrecedence[objData.Nickname]
2023-04-22 16:56:01 -04:00
2024-01-06 21:32:29 -05:00
-- remove GUID to avoid issues for high latency clients
objData["GUID"] = nil
-- store data with value / precendence
2023-04-22 16:56:01 -04:00
data[i] = {
2024-01-06 21:32:07 -05:00
token = objData,
2023-04-22 16:56:01 -04:00
value = value or precedence[1]
}
-- order for comparator function
if precedence ~= nil then
data[i].order = precedence[2]
else
data[i].order = value
end
end
-- sort table by value (symbols last if same value)
table.sort(data, tokenValueComparator)
-- laying out the tokens
local pos = self.getPosition() + Vector(3.55, -0.05, -3.95)
if percentage then pos.z = pos.z - 3.05 end
local location = { x = pos.x, y = pos.y, z = pos.z }
2024-01-06 21:32:07 -05:00
local rotation = self.getRotation()
2023-04-22 16:56:01 -04:00
local currentValue = data[1].value
2023-08-27 21:09:46 -04:00
local tokenCount = { row = 0, sum = 0, total = #data }
2024-01-06 21:32:07 -05:00
local valueCount = 1
2023-04-22 16:56:01 -04:00
local tokenName = false
2023-08-27 21:09:46 -04:00
for i, item in ipairs(data) do
-- this is true for the first token in a new row
2023-04-22 16:56:01 -04:00
if item.value ~= currentValue then
if percentage then
2023-08-27 21:09:46 -04:00
tokenCount.sum = tokenCount.sum + tokenCount.row
createPercentageButton(tokenCount, valueCount, tokenName)
2023-04-22 16:56:01 -04:00
end
2024-01-06 21:32:07 -05:00
location.x = location.x - 1.75
location.z = pos.z
currentValue = item.value
valueCount = valueCount + 1
2023-08-27 21:09:46 -04:00
tokenCount.row = 0
2023-04-22 16:56:01 -04:00
end
2024-01-06 21:32:07 -05:00
spawnObjectData({
data = item.token,
position = location,
rotation = rotation
})
tokenName = item.token.Nickname
2023-04-22 16:56:01 -04:00
location.z = location.z - 1.75
2023-08-27 21:09:46 -04:00
tokenCount.row = tokenCount.row + 1
2023-04-22 16:56:01 -04:00
end
-- this is repeated to create the button for the last token
if percentage then
2023-08-27 21:09:46 -04:00
tokenCount.sum = tokenCount.sum + tokenCount.row
createPercentageButton(tokenCount, valueCount, tokenName)
2023-04-22 16:56:01 -04:00
end
-- introducing a small delay to limit update calls
Wait.time(function() updating = false end, 0.1)
end
-- called from outside to set default values for tokens
function onTokenDataChanged(parameters)
local tokenData = parameters.tokenData or {}
local currentScenario = parameters.currentScenario or ""
local useFrontData = parameters.useFrontData
-- update token precedence
for key, table in pairs(tokenData) do
local modifier = table.modifier
if modifier == -999 then modifier = 0 end
tokenPrecedence[key][1] = modifier
end
updateUI()
layout()
end
end)
2024-01-06 21:32:07 -05:00
__bundle_register("chaosbag/ChaosBagApi", function(require, _LOADED, __bundle_register, __bundle_modules)
2023-04-22 16:56:01 -04:00
do
2024-01-06 21:32:07 -05:00
local ChaosBagApi = {}
2023-04-22 16:56:01 -04:00
2024-01-06 21:32:07 -05:00
-- respawns the chaos bag with a new state of tokens
2024-02-17 19:48:30 -05:00
---@param tokenList table List of chaos token ids
2024-01-06 21:32:07 -05:00
ChaosBagApi.setChaosBagState = function(tokenList)
return Global.call("setChaosBagState", tokenList)
2023-04-22 16:56:01 -04:00
end
2024-01-06 21:32:07 -05:00
-- returns a Table List of chaos token ids in the current chaos bag
-- requires copying the data into a new table because TTS is weird about handling table return values in Global
ChaosBagApi.getChaosBagState = function()
local chaosBagContentsCatcher = Global.call("getChaosBagState")
local chaosBagContents = {}
for _, v in ipairs(chaosBagContentsCatcher) do
table.insert(chaosBagContents, v)
end
return chaosBagContents
end
-- checks scripting zone for chaos bag (also called by a lot of objects!)
ChaosBagApi.findChaosBag = function()
return Global.call("findChaosBag")
end
-- returns a table of object references to the tokens in play (does not include sealed tokens!)
ChaosBagApi.getTokensInPlay = function()
2024-02-04 10:51:51 -05:00
return Global.call("getChaosTokensinPlay")
2024-01-06 21:32:07 -05:00
end
-- returns all sealed tokens on cards to the chaos bag
2024-02-17 19:48:30 -05:00
---@param playerColor string Color of the player to show the broadcast to
2024-01-06 21:32:07 -05:00
ChaosBagApi.releaseAllSealedTokens = function(playerColor)
return Global.call("releaseAllSealedTokens", playerColor)
end
-- returns all drawn tokens to the chaos bag
2024-02-17 19:48:30 -05:00
ChaosBagApi.returnChaosTokens = function()
return Global.call("returnChaosTokens")
2024-01-06 21:32:07 -05:00
end
-- removes the specified chaos token from the chaos bag
2024-02-17 19:48:30 -05:00
---@param id string ID of the chaos token
2024-01-06 21:32:07 -05:00
ChaosBagApi.removeChaosToken = function(id)
return Global.call("removeChaosToken", id)
end
2024-02-04 10:51:51 -05:00
-- returns a chaos token to the bag and calls all relevant functions
2024-02-17 19:48:30 -05:00
---@param token tts__Object Chaos token to return
2024-02-04 10:51:51 -05:00
ChaosBagApi.returnChaosTokenToBag = function(token)
return Global.call("returnChaosTokenToBag", token)
end
2024-01-06 21:32:07 -05:00
-- spawns the specified chaos token and puts it into the chaos bag
2024-02-17 19:48:30 -05:00
---@param id string ID of the chaos token
2024-01-06 21:32:07 -05:00
ChaosBagApi.spawnChaosToken = function(id)
return Global.call("spawnChaosToken", id)
end
-- Checks to see if the chaos bag can be manipulated. If a player is searching the bag when tokens
-- are drawn or replaced a TTS bug can cause those tokens to vanish. Any functions which change the
-- contents of the bag should check this method before doing so.
-- This method will broadcast a message to all players if the bag is being searched.
2024-02-17 19:48:30 -05:00
---@return any canTouch True if the bag is manipulated, false if it should be blocked.
2024-01-06 21:32:07 -05:00
ChaosBagApi.canTouchChaosTokens = function()
return Global.call("canTouchChaosTokens")
end
-- called by playermats (by the "Draw chaos token" button)
2024-02-17 19:48:30 -05:00
---@param mat tts__Object Playermat that triggered this
---@param drawAdditional boolean Controls whether additional tokens should be drawn
---@param tokenType? string Name of token (e.g. "Bless") to be drawn from the bag
---@param guidToBeResolved? string GUID of the sealed token to be resolved instead of drawing a token from the bag
ChaosBagApi.drawChaosToken = function(mat, drawAdditional, tokenType, guidToBeResolved)
return Global.call("drawChaosToken", {mat = mat, drawAdditional = drawAdditional, tokenType = tokenType, guidToBeResolved = guidToBeResolved})
2024-01-06 21:32:07 -05:00
end
-- returns a Table List of chaos token ids in the current chaos bag
-- requires copying the data into a new table because TTS is weird about handling table return values in Global
ChaosBagApi.getIdUrlMap = function()
return Global.getTable("ID_URL_MAP")
end
return ChaosBagApi
2023-04-22 16:56:01 -04:00
end
end)
2024-02-04 10:51:51 -05:00
__bundle_register("core/MythosAreaApi", function(require, _LOADED, __bundle_register, __bundle_modules)
do
local MythosAreaApi = {}
local guidReferenceApi = require("core/GUIDReferenceApi")
local function getMythosArea()
return guidReferenceApi.getObjectByOwnerAndType("Mythos", "MythosArea")
end
2024-02-17 19:48:30 -05:00
---@return any: Table of chaos token metadata (if provided through scenario reference card)
2024-02-04 10:51:51 -05:00
MythosAreaApi.returnTokenData = function()
return getMythosArea().call("returnTokenData")
end
2024-02-17 19:48:30 -05:00
---@return any: Object reference to the encounter deck
2024-02-04 10:51:51 -05:00
MythosAreaApi.getEncounterDeck = function()
return getMythosArea().call("getEncounterDeck")
end
-- draw an encounter card for the requesting mat
2024-02-17 19:48:30 -05:00
---@param mat tts__Object Playermat that triggered this
---@param alwaysFaceUp boolean Whether the card should be drawn face-up
2024-02-04 10:51:51 -05:00
MythosAreaApi.drawEncounterCard = function(mat, alwaysFaceUp)
getMythosArea().call("drawEncounterCard", {mat = mat, alwaysFaceUp = alwaysFaceUp})
end
return MythosAreaApi
end
end)
__bundle_register("core/GUIDReferenceApi", function(require, _LOADED, __bundle_register, __bundle_modules)
do
local GUIDReferenceApi = {}
local function getGuidHandler()
return getObjectFromGUID("123456")
end
2024-02-17 19:48:30 -05:00
---@param owner string Parent object for this search
---@param type string Type of object to search for
---@return any: Object reference to the matching object
2024-02-04 10:51:51 -05:00
GUIDReferenceApi.getObjectByOwnerAndType = function(owner, type)
return getGuidHandler().call("getObjectByOwnerAndType", { owner = owner, type = type })
end
-- returns all matching objects as a table with references
2024-02-17 19:48:30 -05:00
---@param type string Type of object to search for
---@return table: List of object references to matching objects
2024-02-04 10:51:51 -05:00
GUIDReferenceApi.getObjectsByType = function(type)
return getGuidHandler().call("getObjectsByType", type)
end
-- returns all matching objects as a table with references
2024-02-17 19:48:30 -05:00
---@param owner string Parent object for this search
---@return table: List of object references to matching objects
2024-02-04 10:51:51 -05:00
GUIDReferenceApi.getObjectsByOwner = function(owner)
return getGuidHandler().call("getObjectsByOwner", owner)
end
-- sends new information to the reference handler to edit the main index
2024-02-17 19:48:30 -05:00
---@param owner string Parent of the object
---@param type string Type of the object
---@param guid string GUID of the object
2024-02-04 10:51:51 -05:00
GUIDReferenceApi.editIndex = function(owner, type, guid)
return getGuidHandler().call("editIndex", {
owner = owner,
type = type,
guid = guid
})
end
return GUIDReferenceApi
end
end)
2023-04-22 16:56:01 -04:00
return __bundle_require("__root")