ah_sce_unpacked/unpacked/Bag OptionPanel Source 830bd0/Custom_Token Token Arranger 022907.ttslua

462 lines
13 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 latestLoad = "XXX"
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,
latestLoad = latestLoad,
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
latestLoad = loadedData.latestLoad or "XXX"
percentage = loadedData.percentage
else
loadDefaultValues()
end
createButtonsAndInputs(true)
layout()
-- context menu items
self.addContextMenuItem("Load default values", function()
latestLoad = "XXX"
loadDefaultValues()
updateUI()
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")
end
layout()
end)
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")
layout()
end)
-- grab token metadata from mythos area
Wait.time(function() onTokenDataChanged(mythosAreaApi.returnTokenData()) end, 0.2)
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
-- creates percentage representation buttons
function createPercentageButton(basePercentage, valueCount, tokenName, cumulativePercentage)
local buttonScale, offset, textColor, labelString
if percentage == "cumulative" then
buttonScale = {1.5, 1.5, 1.5}
offset = -2.85
else
buttonScale = {2, 2, 2}
offset = -2.675
end
-- if this is a cumulative button (bottom one of the two created buttons)
if cumulativePercentage then
offset = -2.45
textColor = {1, 1, 1}
-- only display one digit for 100%
if cumulativePercentage == 100 then
labelString = string.format("%s", string.format("%05.1f", cumulativePercentage) .. "%")
else
labelString = string.format("%s", string.format("%05.2f", cumulativePercentage) .. "%")
end
else
labelString = string.format("%s", string.format("%05.2f", basePercentage) .. "%")
if tokenName == "Elder Sign" then
textColor = {0.35, 0.71, 0.85}
elseif tokenName == "Auto-fail" then
textColor = {0.86, 0.1, 0.1}
elseif tokenName then
textColor = {0.68, 0.53, 0.86}
else
textColor = {0.85, 0.67, 0.33}
end
end
self.createButton({
label = labelString,
click_function = "none",
width = 0,
height = 0,
scale = buttonScale,
font_color = textColor,
position = Vector(2.3, -0.05, offset + 0.875 * valueCount)
})
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" }
local spawnedObj = spawnObjectData({
data = obj,
position = { 0.49, 3, 0 }
})
local value = tonumber(obj.Nickname)
local precedence = tokenPrecedence[obj.Nickname]
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
local tokenCount = 0
local valueCount = 1
local cumulativePercentage = 0
local tokenName = false
for _, item in ipairs(data) do
if item.value ~= currentValue then
if percentage then
local basePercentage = math.floor((tokenCount / #data) * 10000) / 100
createPercentageButton(basePercentage, valueCount, tokenName)
if percentage == "cumulative" then
cumulativePercentage = cumulativePercentage + basePercentage
createPercentageButton(basePercentage, valueCount, tokenName, cumulativePercentage)
end
end
location.x = location.x - 1.75
location.z = pos.z
currentValue = item.value
valueCount = valueCount + 1
tokenCount = 0
end
item.token.setPosition(location)
item.token.setRotation(self.getRotation())
location.z = location.z - 1.75
tokenCount = tokenCount + 1
tokenName = item.token.getName()
-- set tokenName to false if it does not contain letters
-- tokenName is used by `createPercentageButton()` to determine the textcolor for percentages
if string.match(tokenName, "%a") == nil then
tokenName = false
end
end
-- this is repeated to create the button for the last token
if percentage then
local basePercentage = math.floor((tokenCount / #data) * 10000) / 100
createPercentageButton(basePercentage, valueCount, tokenName)
if percentage == "cumulative" then
cumulativePercentage = cumulativePercentage + basePercentage
createPercentageButton(basePercentage, valueCount, tokenName, cumulativePercentage)
end
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
-- only update if this data is new
local info = currentScenario .. tostring(useFrontData)
if latestLoad == info then return end
latestLoad = info
-- 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()
return getObjectFromGUID("9f334f").call("returnTokenData")
end
return MythosAreaApi
end
end)
return __bundle_require("__root")