2022-11-11 02:59:55 -05:00
|
|
|
-- names of tokens in order
|
2022-12-16 20:49:34 -05:00
|
|
|
local TOKEN_NAMES = {
|
2022-11-11 02:59:55 -05:00
|
|
|
"Elder Sign",
|
|
|
|
"Skull",
|
|
|
|
"Cultist",
|
|
|
|
"Tablet",
|
|
|
|
"Elder Thing",
|
|
|
|
"Auto-fail",
|
|
|
|
"Bless",
|
|
|
|
"Curse",
|
|
|
|
"Frost",
|
|
|
|
""
|
|
|
|
}
|
|
|
|
|
2022-11-13 06:11:21 -05:00
|
|
|
-- token modifiers for sorting (and order for same modifier)
|
|
|
|
-- order starts at 2 because there is a "+1" token
|
2022-12-16 20:49:34 -05:00
|
|
|
local TOKEN_PRECEDENCE = {
|
2022-11-13 06:11:21 -05:00
|
|
|
["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 }
|
|
|
|
}
|
|
|
|
|
2022-11-11 02:59:55 -05:00
|
|
|
-- common parameters
|
2022-11-13 06:11:21 -05:00
|
|
|
local buttonParameters = {}
|
|
|
|
buttonParameters.function_owner = self
|
|
|
|
buttonParameters.label = ""
|
|
|
|
buttonParameters.tooltip = "Add / Remove"
|
|
|
|
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
|
2022-11-11 02:59:55 -05:00
|
|
|
|
2022-11-13 06:11:21 -05:00
|
|
|
updating = false
|
|
|
|
|
2022-12-16 20:49:34 -05:00
|
|
|
function onSave() return JSON.encode(TOKEN_PRECEDENCE) end
|
2022-11-11 02:59:55 -05:00
|
|
|
|
2022-11-13 06:11:21 -05:00
|
|
|
function onLoad(save_state)
|
2022-11-11 02:59:55 -05:00
|
|
|
if save_state ~= nil then
|
2022-12-16 20:49:34 -05:00
|
|
|
TOKEN_PRECEDENCE = JSON.decode(save_state)
|
2022-11-11 02:59:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
-- create UI
|
|
|
|
local offset = 0.725
|
2022-11-13 06:11:21 -05:00
|
|
|
local pos = { x = { -1.067, 0.377 }, z = -2.175 }
|
2022-11-11 02:59:55 -05:00
|
|
|
|
|
|
|
-- button and inputs index 1-10
|
|
|
|
for i = 1, 10 do
|
|
|
|
if i < 6 then
|
2022-11-13 06:11:21 -05:00
|
|
|
buttonParameters.position = { pos.x[1], 0, pos.z + i * offset }
|
|
|
|
inputParameters.position = { pos.x[1] + offset, 0.1, pos.z + i * offset }
|
2022-11-11 02:59:55 -05:00
|
|
|
else
|
2022-11-13 06:11:21 -05:00
|
|
|
buttonParameters.position = { pos.x[2], 0, pos.z + (i - 5) * offset }
|
|
|
|
inputParameters.position = { pos.x[2] + offset, 0.1, pos.z + (i - 5) * offset }
|
2022-11-11 02:59:55 -05:00
|
|
|
end
|
|
|
|
|
2022-11-13 06:11:21 -05:00
|
|
|
buttonParameters.click_function = attachIndex("tokenClick", i)
|
|
|
|
inputParameters.input_function = attachIndex2("tokenInput", i)
|
2022-12-16 20:49:34 -05:00
|
|
|
inputParameters.value = TOKEN_PRECEDENCE[TOKEN_NAMES[i]][1]
|
2022-11-11 02:59:55 -05:00
|
|
|
|
2022-11-13 06:11:21 -05:00
|
|
|
self.createButton(buttonParameters)
|
|
|
|
self.createInput(inputParameters)
|
2022-11-11 02:59:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
-- index 11: "Update / Hide" button
|
2022-11-13 06:11:21 -05:00
|
|
|
buttonParameters.label = "Update / Hide"
|
|
|
|
buttonParameters.click_function = "layout"
|
|
|
|
buttonParameters.tooltip = "Left-Click: Update!\nRight-Click: Hide Tokens!"
|
|
|
|
buttonParameters.position = { 0.725, 0.1, 2.025 }
|
|
|
|
buttonParameters.color = { 1, 1, 1 }
|
|
|
|
buttonParameters.width = 675
|
|
|
|
buttonParameters.height = 175
|
|
|
|
self.createButton(buttonParameters)
|
2022-11-11 02:59:55 -05:00
|
|
|
|
|
|
|
self.addContextMenuItem("More Information", function()
|
|
|
|
printToAll("------------------------------", "White")
|
2022-12-16 20:49:34 -05:00
|
|
|
printToAll("Token Arranger by Chr1Z", "Orange")
|
2022-11-11 02:59:55 -05:00
|
|
|
printToAll("original concept by Whimsical", "White")
|
|
|
|
end)
|
2022-11-13 06:11:21 -05:00
|
|
|
|
|
|
|
-- send object reference to bless/curse manager
|
2022-12-19 08:39:32 -05:00
|
|
|
Wait.time(function() getObjectFromGUID("5933fb").setVar("tokenArranger", self) end, 1)
|
2022-11-13 06:11:21 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
function onDestroy()
|
2022-12-19 08:39:32 -05:00
|
|
|
deleteCopiedTokens()
|
2022-11-13 06:11:21 -05:00
|
|
|
-- remove object reference from bless/curse manager
|
|
|
|
getObjectFromGUID("5933fb").setVar("tokenArranger", nil)
|
2022-11-11 02:59:55 -05:00
|
|
|
end
|
|
|
|
|
2022-12-19 08:39:32 -05:00
|
|
|
function onPickUp()
|
|
|
|
deleteCopiedTokens()
|
|
|
|
end
|
|
|
|
|
2022-11-11 02:59:55 -05:00
|
|
|
-- helper functions to carry index
|
|
|
|
function attachIndex(click_function, index)
|
|
|
|
local fn_name = click_function .. index
|
2022-12-19 08:39:32 -05:00
|
|
|
_G[fn_name] = function(obj, player_color, isRightClick)
|
|
|
|
_G[click_function](obj, player_color, isRightClick, index)
|
2022-11-11 02:59:55 -05:00
|
|
|
end
|
|
|
|
return fn_name
|
|
|
|
end
|
|
|
|
|
|
|
|
function attachIndex2(input_function, index)
|
|
|
|
local fn_name = input_function .. index
|
|
|
|
_G[fn_name] = function(obj, player_color, input, selected)
|
|
|
|
_G[input_function](obj, player_color, input, selected, index)
|
|
|
|
end
|
|
|
|
return fn_name
|
|
|
|
end
|
|
|
|
|
|
|
|
-- click_function for buttons on chaos tokens
|
2022-12-19 08:39:32 -05:00
|
|
|
function tokenClick(_, _, isRightClick, index)
|
2022-11-11 02:59:55 -05:00
|
|
|
if not updating then
|
|
|
|
updating = true
|
2022-12-19 08:39:32 -05:00
|
|
|
local change = tonumber(isRightClick and "-1" or "1")
|
|
|
|
TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] = TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] + change
|
2022-12-16 20:49:34 -05:00
|
|
|
self.editInput({ index = index - 1, value = TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] })
|
2022-11-11 02:59:55 -05:00
|
|
|
layout()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- input_function for input_boxes
|
2022-12-19 08:39:32 -05:00
|
|
|
function tokenInput(_, _, input, selected, index)
|
2022-11-11 02:59:55 -05:00
|
|
|
if selected == false and not updating then
|
|
|
|
updating = true
|
|
|
|
local num = tonumber(input)
|
|
|
|
if num ~= nil then
|
2022-12-16 20:49:34 -05:00
|
|
|
TOKEN_PRECEDENCE[TOKEN_NAMES[index]][1] = num
|
2022-11-11 02:59:55 -05:00
|
|
|
end
|
|
|
|
layout()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- order function for data sorting
|
|
|
|
function token_value_comparator(left, right)
|
|
|
|
if left.value > right.value then return true
|
|
|
|
elseif right.value > left.value then return false
|
|
|
|
elseif left.order < right.order then return true
|
|
|
|
elseif right.order < left.order then return false
|
|
|
|
else return left.token.getGUID() > right.token.getGUID()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- get chaos bag from scripting zone and description
|
|
|
|
function getChaosBag()
|
|
|
|
local chaosbag = nil
|
|
|
|
local chaosbag_zone = getObjectFromGUID("83ef06")
|
|
|
|
|
|
|
|
-- error handling: scripting zone not found
|
|
|
|
if chaosbag_zone == nil then
|
|
|
|
printToAll("Zone for chaos bag detection couldn't be found.", "Red")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, v in ipairs(chaosbag_zone.getObjects()) do
|
|
|
|
if v.getDescription() == "Chaos Bag" then
|
|
|
|
chaosbag = getObjectFromGUID(v.getGUID())
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- error handling: chaos bag not found
|
|
|
|
if chaosbag == nil then
|
|
|
|
printToAll("Chaos bag couldn't be found.", "Red")
|
|
|
|
end
|
|
|
|
return chaosbag
|
|
|
|
end
|
|
|
|
|
2022-12-19 08:39:32 -05:00
|
|
|
-- deletes previously placed tokens
|
|
|
|
function deleteCopiedTokens()
|
|
|
|
for _, token in ipairs(getObjectsWithTag("to_be_deleted")) do token.destruct() end
|
|
|
|
end
|
|
|
|
|
2022-11-11 02:59:55 -05:00
|
|
|
-- main function (delete old tokens, clone chaos bag content, sort it and position it)
|
|
|
|
function layout(_, _, isRightClick)
|
2022-12-19 08:39:32 -05:00
|
|
|
deleteCopiedTokens()
|
2022-11-11 02:59:55 -05:00
|
|
|
|
|
|
|
-- stop here if right-clicked
|
|
|
|
if isRightClick then return end
|
|
|
|
|
|
|
|
local chaos_bag = getChaosBag()
|
|
|
|
local chaos_bag_objects = chaos_bag.getObjects()
|
|
|
|
|
|
|
|
-- take each token out and clone it
|
|
|
|
for _, data in ipairs(chaos_bag_objects) do
|
|
|
|
chaos_bag.takeObject {
|
|
|
|
guid = data.guid,
|
|
|
|
smooth = false,
|
|
|
|
callback_function = function(tok)
|
|
|
|
chaos_bag.putObject(tok.clone())
|
2022-12-16 20:49:34 -05:00
|
|
|
tok.addTag("to_be_deleted")
|
2022-11-11 02:59:55 -05:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
-- wait until all tokens have finished spawning
|
2022-12-19 08:39:32 -05:00
|
|
|
Wait.condition(function() placeTokens() end, function() return #chaos_bag_objects == #getObjectsWithTag("to_be_deleted") end)
|
2022-11-11 02:59:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
-- position tokens sorted by value
|
2022-12-19 08:39:32 -05:00
|
|
|
function placeTokens()
|
2022-11-11 02:59:55 -05:00
|
|
|
local data = {}
|
|
|
|
|
|
|
|
-- create table with tokens
|
2022-12-16 20:49:34 -05:00
|
|
|
for i, token in ipairs(getObjectsWithTag("to_be_deleted")) do
|
2022-11-13 06:11:21 -05:00
|
|
|
local name = token.getName() or ""
|
2022-11-11 02:59:55 -05:00
|
|
|
local value = tonumber(name)
|
2022-12-16 20:49:34 -05:00
|
|
|
local precedence = TOKEN_PRECEDENCE[name]
|
2022-11-11 02:59:55 -05:00
|
|
|
|
|
|
|
data[i] = {
|
|
|
|
token = token,
|
|
|
|
value = value or precedence[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
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, token_value_comparator)
|
|
|
|
|
|
|
|
-- laying out the tokens
|
|
|
|
local pos = self.getPosition() + Vector(3.55, -0.05, -3.95)
|
|
|
|
local location = { x = pos.x, y = pos.y, z = pos.z }
|
|
|
|
local current_value = data[1].value
|
|
|
|
|
|
|
|
for _, item in ipairs(data) do
|
|
|
|
if item.value ~= current_value then
|
|
|
|
location.x = location.x - 1.75
|
|
|
|
location.z = pos.z
|
|
|
|
current_value = item.value
|
|
|
|
end
|
|
|
|
item.token.setPosition(location)
|
|
|
|
item.token.setRotation(self.getRotation())
|
|
|
|
location.z = location.z - 1.75
|
|
|
|
end
|
|
|
|
updating = false
|
|
|
|
end
|