ah_sce_unpacked/unpacked/Custom_Token Token Arranger 022907.ttslua

439 lines
12 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)
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
-- variables with save function
local tokenPrecedence = {}
local percentage = false
-- variables without save function
local updating = false
local TOKEN_NAMES = {
"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({
tokenPrecedence = tokenPrecedence,
percentage = percentage
})
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
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
createButtonsAndInputs(true)
-- context menu items
self.addContextMenuItem("Load default values", function()
loadDefaultValues()
updateUI()
layout()
end)
self.addContextMenuItem("Toggle percentages", function()
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)
self.addContextMenuItem("Toggle cumulative", function()
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
-- helper functions to carry index
function attachIndex(click_function, index)
local fn_name = click_function .. index
_G[fn_name] = function(_, _, isRightClick)
_G[click_function](isRightClick, index)
end
return fn_name
end
function attachIndex2(input_function, index)
local fn_name = input_function .. index
_G[fn_name] = function(_, _, input, selected)
_G[input_function](input, selected, index)
end
return fn_name
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
-- creates buttons and inputs (if argument is true)
function createButtonsAndInputs(loadInputs)
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 }
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
end
buttonParameters.click_function = attachIndex("tokenClick", i)
self.createButton(buttonParameters)
-- only create inputs on initial load
if loadInputs then
inputParameters.input_function = attachIndex2("tokenInput", i)
inputParameters.value = tokenPrecedence[TOKEN_NAMES[i]][1]
self.createInput(inputParameters)
end
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)
if (left.value ~= right.value) then
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
-- this removes the percentage buttons
self.clearButtons()
createButtonsAndInputs()
end
2023-08-27 21:09:46 -04:00
-- creates buttons as labels as display for percentage values
function createPercentageButton(tokenCount, valueCount, tokenName)
-- parameters for button creation
local percentageLabel = {}
percentageLabel.click_function = "none"
percentageLabel.width = 0
percentageLabel.height = 0
local startPos = Vector(2.3, -0.05, 0.875 * valueCount)
2023-04-22 16:56:01 -04:00
if percentage == "cumulative" then
2023-08-27 21:09:46 -04:00
percentageLabel.scale = {1.5, 1.5, 1.5}
percentageLabel.position = startPos - Vector(0, 0, 2.85)
else
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}
elseif tokenName == "Auto-fail" then
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}
2023-04-22 16:56:01 -04:00
else
2023-08-27 21:09:46 -04: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)
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
local chaosBag = Global.call("findChaosBag")
local data = {}
-- clone tokens from chaos bag (default position above trash can)
for i, obj in ipairs(chaosBag.getData().ContainedObjects) do
obj["Tags"] = { "tempToken" }
2023-08-27 21:09:46 -04:00
local value = tonumber(obj.Nickname)
local precedence = tokenPrecedence[obj.Nickname]
2023-04-22 16:56:01 -04:00
local spawnedObj = spawnObjectData({
data = obj,
position = { 0.49, 3, 0 }
})
data[i] = {
token = spawnedObj,
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 }
local currentValue = data[1].value
2023-08-27 21:09:46 -04:00
local tokenCount = { row = 0, sum = 0, total = #data }
2023-04-22 16:56:01 -04:00
local valueCount = 1
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
2023-08-27 21:09:46 -04:00
location.x = location.x - 1.75
location.z = pos.z
currentValue = item.value
valueCount = valueCount + 1
tokenCount.row = 0
2023-04-22 16:56:01 -04:00
end
item.token.setPosition(location)
item.token.setRotation(self.getRotation())
2023-08-27 21:09:46 -04:00
item.token.setLock(true)
2023-04-22 16:56:01 -04:00
location.z = location.z - 1.75
tokenName = item.token.getName()
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)
__bundle_register("core/MythosAreaApi", function(require, _LOADED, __bundle_register, __bundle_modules)
do
local MythosAreaApi = {}
local MYTHOS_AREA_GUID = "9f334f"
-- returns the chaos token metadata (if provided through scenario reference card)
MythosAreaApi.returnTokenData = function()
2023-08-27 21:09:46 -04:00
return getObjectFromGUID(MYTHOS_AREA_GUID).call("returnTokenData")
2023-04-22 16:56:01 -04:00
end
return MythosAreaApi
end
end)
return __bundle_require("__root")