ah_sce_unpacked/unpacked/Custom_Token Doom Counter 85c4c6.ttslua
2024-02-04 10:51:51 -05:00

417 lines
13 KiB
Plaintext

-- 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("core/DoomCounter")
end)
__bundle_register("core/DoomCounter", function(require, _LOADED, __bundle_register, __bundle_modules)
local guidReferenceApi = require("core/GUIDReferenceApi")
local playAreaApi = require("core/PlayAreaApi")
local searchLib = require("util/SearchLib")
local optionsVisible = false
local options = {
Agenda = true,
Playarea = true,
Playermats = true
}
val = 0
-- save current value and options
function onSave() return JSON.encode({ val, options }) end
function onLoad(savedData)
if savedData ~= "" then
local loadedData = JSON.decode(savedData)
val = loadedData[1]
options = loadedData[2]
-- restore state for option panel
for key, bool in pairs(options) do
self.UI.setAttribute("option" .. key, "isOn", not bool)
end
end
self.createButton({
label = tostring(val),
click_function = "addOrSubtract",
function_owner = self,
position = { 0, 0.06, 0 },
height = 800,
width = 800,
font_size = 650,
scale = { 1.5, 1.5, 1.5 },
font_color = { 1, 1, 1, 95 },
color = { 0, 0, 0, 0 }
})
end
-- called by the invisible button to change displayed value
function addOrSubtract(_, _, isRightClick)
local newVal = math.min(math.max(val + (isRightClick and -1 or 1), 0), 99)
if val ~= newVal then
updateVal(newVal)
end
end
-- adds the provided number to the current count
function addVal(number)
val = val + number
updateVal(val)
end
-- sets the current count to the provided number
function updateVal(number)
val = number or 0
self.editButton({ index = 0, label = tostring(val) })
if number then
broadcastDoom(val)
else
broadcastToAll("0 doom on the agenda", "White")
end
end
-- called by updateVal and addVal to broadcast total doom in play, including doom threshold
function broadcastDoom(val)
local doomInPlayCounter = guidReferenceApi.getObjectByOwnerAndType("Mythos", "DoomInPlayCounter")
local doomInPlay = doomInPlayCounter.call("countDoomInPlay") + val
local doomThreshold = getDoomThreshold()
if doomThreshold then
broadcastToAll(val .. " doom on the agenda (" .. doomInPlay .. "/" .. doomThreshold .. " in play)", "White")
else
broadcastToAll(val .. " doom on the agenda (" .. doomInPlay .. " in play)", "White")
end
end
-- called by "Reset" button to remove doom
function startReset()
if options.Agenda then
-- omitting the number will broadcast a special message just for this case
updateVal()
end
local doomInPlayCounter = guidReferenceApi.getObjectByOwnerAndType("Mythos", "DoomInPlayCounter")
if doomInPlayCounter then
doomInPlayCounter.call("removeDoom", options)
end
end
-- get doom threshold from top card of Agenda deck
function getDoomThreshold()
local agendaPos = { -2.72, 1.6, 0.37 }
local searchResult = searchLib.atPosition(agendaPos, "isCardOrDeck")
if #searchResult == 1 then
local obj = searchResult[1]
if obj.type == "Card" then
return getDoomThresholdFromGMNotes(obj.getGMNotes())
else
-- handle agenda deck
local containedObjects = obj.getData().ContainedObjects
local topCardData = containedObjects[#containedObjects]
return getDoomThresholdFromGMNotes(topCardData.GMNotes)
end
end
return nil
end
-- decodes the gm notes and return the doom treshhold
function getDoomThresholdFromGMNotes(notes)
local metadata = JSON.decode(notes) or {}
if metadata.doomThresholdPerInvestigator then
return metadata.doomThresholdPerInvestigator * playAreaApi.getInvestigatorCount() + metadata.doomThreshold
else
return metadata.doomThreshold
end
end
-- XML UI functions
function optionClick(_, optionName)
options[optionName] = not options[optionName]
printToAll("Doom removal of " .. optionName .. (options[optionName] and " enabled" or " disabled"))
end
function toggleOptions()
optionsVisible = not optionsVisible
if optionsVisible then
self.UI.show("Options")
else
self.UI.hide("Options")
end
end
end)
__bundle_register("core/GUIDReferenceApi", function(require, _LOADED, __bundle_register, __bundle_modules)
do
local GUIDReferenceApi = {}
local function getGuidHandler()
return getObjectFromGUID("123456")
end
-- returns all matching objects as a table with references
---@param owner String Parent object for this search
---@param type String Type of object to search for
GUIDReferenceApi.getObjectByOwnerAndType = function(owner, type)
return getGuidHandler().call("getObjectByOwnerAndType", { owner = owner, type = type })
end
-- returns all matching objects as a table with references
---@param type String Type of object to search for
GUIDReferenceApi.getObjectsByType = function(type)
return getGuidHandler().call("getObjectsByType", type)
end
-- returns all matching objects as a table with references
---@param owner String Parent object for this search
GUIDReferenceApi.getObjectsByOwner = function(owner)
return getGuidHandler().call("getObjectsByOwner", owner)
end
-- sends new information to the reference handler to edit the main index
---@param owner String Parent of the object
---@param type String Type of the object
---@param guid String GUID of the object
GUIDReferenceApi.editIndex = function(owner, type, guid)
return getGuidHandler().call("editIndex", {
owner = owner,
type = type,
guid = guid
})
end
return GUIDReferenceApi
end
end)
__bundle_register("core/PlayAreaApi", function(require, _LOADED, __bundle_register, __bundle_modules)
do
local PlayAreaApi = {}
local guidReferenceApi = require("core/GUIDReferenceApi")
local function getPlayArea()
return guidReferenceApi.getObjectByOwnerAndType("Mythos", "PlayArea")
end
local function getInvestigatorCounter()
return guidReferenceApi.getObjectByOwnerAndType("Mythos", "InvestigatorCounter")
end
-- Returns the current value of the investigator counter from the playmat
---@return Integer. Number of investigators currently set on the counter
PlayAreaApi.getInvestigatorCount = function()
return getInvestigatorCounter().getVar("val")
end
-- Updates the current value of the investigator counter from the playmat
---@param count Number of investigators to set on the counter
PlayAreaApi.setInvestigatorCount = function(count)
getInvestigatorCounter().call("updateVal", count)
end
-- Move all contents on the play area (cards, tokens, etc) one slot in the given direction. Certain
-- fixed objects will be ignored, as will anything the player has tagged with 'displacement_excluded'
---@param playerColor Color Color of the player requesting the shift for messages
PlayAreaApi.shiftContentsUp = function(playerColor)
return getPlayArea().call("shiftContentsUp", playerColor)
end
PlayAreaApi.shiftContentsDown = function(playerColor)
return getPlayArea().call("shiftContentsDown", playerColor)
end
PlayAreaApi.shiftContentsLeft = function(playerColor)
return getPlayArea().call("shiftContentsLeft", playerColor)
end
PlayAreaApi.shiftContentsRight = function(playerColor)
return getPlayArea().call("shiftContentsRight", playerColor)
end
-- Reset the play area's tracking of which cards have had tokens spawned.
PlayAreaApi.resetSpawnedCards = function()
return getPlayArea().call("resetSpawnedCards")
end
-- Sets whether location connections should be drawn
PlayAreaApi.setConnectionDrawState = function(state)
getPlayArea().call("setConnectionDrawState", state)
end
-- Sets the connection color
PlayAreaApi.setConnectionColor = function(color)
getPlayArea().call("setConnectionColor", color)
end
-- Event to be called when the current scenario has changed.
---@param scenarioName Name of the new scenario
PlayAreaApi.onScenarioChanged = function(scenarioName)
getPlayArea().call("onScenarioChanged", scenarioName)
end
-- Sets this playmat's snap points to limit snapping to locations or not.
-- If matchTypes is false, snap points will be reset to snap all cards.
---@param matchCardTypes Boolean Whether snap points should only snap for the matching card types.
PlayAreaApi.setLimitSnapsByType = function(matchCardTypes)
getPlayArea().call("setLimitSnapsByType", matchCardTypes)
end
-- Receiver for the Global tryObjectEnterContainer event. Used to clear vector lines from dragged
-- cards before they're destroyed by entering the container
PlayAreaApi.tryObjectEnterContainer = function(container, object)
getPlayArea().call("tryObjectEnterContainer", { container = container, object = object })
end
-- counts the VP on locations in the play area
PlayAreaApi.countVP = function()
return getPlayArea().call("countVP")
end
-- highlights all locations in the play area without metadata
---@param state Boolean True if highlighting should be enabled
PlayAreaApi.highlightMissingData = function(state)
return getPlayArea().call("highlightMissingData", state)
end
-- highlights all locations in the play area with VP
---@param state Boolean True if highlighting should be enabled
PlayAreaApi.highlightCountedVP = function(state)
return getPlayArea().call("countVP", state)
end
-- Checks if an object is in the play area (returns true or false)
PlayAreaApi.isInPlayArea = function(object)
return getPlayArea().call("isInPlayArea", object)
end
PlayAreaApi.getSurface = function()
return getPlayArea().getCustomObject().image
end
PlayAreaApi.updateSurface = function(url)
return getPlayArea().call("updateSurface", url)
end
-- Called by Custom Data Helpers to push their location data into the Data Helper. This adds the
-- data to the local token manager instance.
---@param args Table Single-value array holding the GUID of the Custom Data Helper making the call
PlayAreaApi.updateLocations = function(args)
getPlayArea().call("updateLocations", args)
end
PlayAreaApi.getCustomDataHelper = function()
return getPlayArea().getVar("customDataHelper")
end
return PlayAreaApi
end
end)
__bundle_register("util/SearchLib", function(require, _LOADED, __bundle_register, __bundle_modules)
do
local SearchLib = {}
local filterFunctions = {
isActionToken = function(x) return x.getDescription() == "Action Token" end,
isCard = function(x) return x.type == "Card" end,
isDeck = function(x) return x.type == "Deck" end,
isCardOrDeck = function(x) return x.type == "Card" or x.type == "Deck" end,
isClue = function(x) return x.memo == "clueDoom" and x.is_face_down == false end,
isTileOrToken = function(x) return x.type == "Tile" end
}
-- performs the actual search and returns a filtered list of object references
---@param pos Table Global position
---@param rot Table Global rotation
---@param size Table Size
---@param filter String Name of the filter function
---@param direction Table Direction (positive is up)
---@param maxDistance Number Distance for the cast
local function returnSearchResult(pos, rot, size, filter, direction, maxDistance)
if filter then filter = filterFunctions[filter] end
local searchResult = Physics.cast({
origin = pos,
direction = direction or { 0, 1, 0 },
orientation = rot or { 0, 0, 0 },
type = 3,
size = size,
max_distance = maxDistance or 0
})
-- filtering the result
local objList = {}
for _, v in ipairs(searchResult) do
if not filter or filter(v.hit_object) then
table.insert(objList, v.hit_object)
end
end
return objList
end
-- searches the specified area
SearchLib.inArea = function(pos, rot, size, filter)
return returnSearchResult(pos, rot, size, filter)
end
-- searches the area on an object
SearchLib.onObject = function(obj, filter)
pos = obj.getPosition()
size = obj.getBounds().size:setAt("y", 1)
return returnSearchResult(pos, _, size, filter)
end
-- searches the specified position (a single point)
SearchLib.atPosition = function(pos, filter)
size = { 0.1, 2, 0.1 }
return returnSearchResult(pos, _, size, filter)
end
-- searches below the specified position (downwards until y = 0)
SearchLib.belowPosition = function(pos, filter)
direction = { 0, -1, 0 }
maxDistance = pos.y
return returnSearchResult(pos, _, size, filter, direction, maxDistance)
end
return SearchLib
end
end)
return __bundle_require("__root")