From de2b6ab22ec41c24965b439b68692a576c03dcc2 Mon Sep 17 00:00:00 2001 From: Chr1Z93 Date: Tue, 19 Dec 2023 00:48:02 +0100 Subject: [PATCH] mythos area code clean up --- src/core/DoomCounter.ttslua | 74 +++++++++++-------------------- src/core/DoomInPlayCounter.ttslua | 23 +++++----- src/core/MythosArea.ttslua | 39 +++++++--------- 3 files changed, 56 insertions(+), 80 deletions(-) diff --git a/src/core/DoomCounter.ttslua b/src/core/DoomCounter.ttslua index be99c5f3..a8c05a21 100644 --- a/src/core/DoomCounter.ttslua +++ b/src/core/DoomCounter.ttslua @@ -1,6 +1,6 @@ -local searchLib = require("util/SearchLib") local guidReferenceApi = require("core/GUIDReferenceApi") local playAreaApi = require("core/PlayAreaApi") +local searchLib = require("util/SearchLib") local optionsVisible = false local options = { @@ -50,10 +50,8 @@ end -- adds the provided number to the current count function addVal(number) - number = tonumber(number) or 0 val = val + number - self.editButton({ index = 0, label = tostring(val) }) - broadcastDoom(val) + updateVal(val) end -- sets the current count to the provided number @@ -65,18 +63,14 @@ end -- called by updateVal and addVal to broadcast total doom in play, including doom threshold function broadcastDoom(val) - if val ~= 0 then - local doomInPlayCounter = guidReferenceApi.getObjectByOwnerAndType("Mythos", "DoomInPlayCounter") - local otherDoom = doomInPlayCounter.call("countDoom") - local totalDoomThreshold = getDoomThreshold() + local doomInPlayCounter = guidReferenceApi.getObjectByOwnerAndType("Mythos", "DoomInPlayCounter") + local doomInPlay = doomInPlayCounter.call("countDoomInPlay") + val + local doomThreshold = getDoomThreshold() - if totalDoomThreshold ~= nil then - broadcastToAll(val .. " doom on the agenda (" .. otherDoom + val .. "/" .. totalDoomThreshold .. " in play)", "White") - else - broadcastToAll(val .. " doom on the agenda (" .. otherDoom + val .. " in play)", "White") - end + if doomThreshold then + broadcastToAll(val .. " doom on the agenda (" .. doomInPlay .. "/" .. doomThreshold .. " in play)", "White") else - broadcastToAll("Doom on agenda set to " .. val, "White") + broadcastToAll(val .. " doom on the agenda (" .. doomInPlay .. " in play)", "White") end end @@ -95,42 +89,28 @@ end function getDoomThreshold() local agendaPos = { -2.72, 1.6, 0.37 } local searchResult = searchLib.atPosition(agendaPos, "isCardOrDeck") - local metadata = {} - if #searchResult == 0 then - -- handle no agenda found - return nil - elseif #searchResult == 1 then - if searchResult[1].type == "Card" then - -- handle single agenda card - local cardData = searchResult[1].getData() - metadata = JSON.decode(cardData.GMNotes) - if metadata == nil then - return nil - else - if metadata.doomThresholdPerInvestigator then - return metadata.doomThresholdPerInvestigator*playAreaApi.getInvestigatorCount() + metadata.doomThreshold - else - return metadata.doomThreshold - end - end + + if #searchResult == 1 then + local obj = searchResult[1] + if obj.type == "Card" then + return getDoomThresholdFromGMNotes(obj.getGMNotes()) else - -- handle agenda deck - local cardData = searchResult[1].getData().ContainedObjects - local topCardData = cardData[#cardData] - metadata = JSON.decode(topCardData.GMNotes) - if metadata == nil then - return nil - else - if metadata.doomThresholdPerInvestigator then - return metadata.doomThresholdPerInvestigator*playAreaApi.getInvestigatorCount() + metadata.doomThreshold - else - return metadata.doomThreshold - end - end + -- 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 - -- handle multiple cards / decks found - return nil + return metadata.doomThreshold end end diff --git a/src/core/DoomInPlayCounter.ttslua b/src/core/DoomInPlayCounter.ttslua index 63c5fcaa..16ec90fe 100644 --- a/src/core/DoomInPlayCounter.ttslua +++ b/src/core/DoomInPlayCounter.ttslua @@ -32,19 +32,23 @@ function onLoad() TRASH = guidReferenceApi.getObjectByOwnerAndType("Mythos", "Trash") ZONE = guidReferenceApi.getObjectByOwnerAndType("Mythos", "PlayAreaZone") - loopID = Wait.time(countDoom, 2, -1) + loopID = Wait.time(updateCounter, 2, -1) end -- main function -function countDoom() +function updateCounter() + local count = countDoomInPlay() + self.editButton({ index = 0, label = tostring(count) }) +end + +-- get doom in play +function countDoomInPlay() local count = 0 - -- get doom in play for _, obj in ipairs(getObjects()) do count = count + getDoomAmount(obj) end - self.editButton({ index = 0, label = tostring(count) }) return count end @@ -61,19 +65,17 @@ end -- removes doom from playermats / playarea function removeDoom(options) - local count = 0 - if options.Playermats then - count = removeDoomFromList(playmatApi.searchAroundPlaymat("All")) + local count = removeDoomFromList(playmatApi.searchAroundPlaymat("All")) if count > 0 then - broadcastToAll(count .. " doom removed from Playermats.", "White") + broadcastToAll(count .. " doom removed from playermats.", "White") end end if options.Playarea then - count = removeDoomFromList(ZONE.getObjects()) + local count = removeDoomFromList(ZONE.getObjects()) if count > 0 then - broadcastToAll(count .. " doom removed from Playerarea.", "White") + broadcastToAll(count .. " doom removed from play area.", "White") end end end @@ -91,6 +93,7 @@ function removeDoomFromList(objList) return count end +-- helper function to check if a position is inside an area function inArea(point, bounds) return (point.x < bounds.upperLeft.x and point.x > bounds.lowerRight.x diff --git a/src/core/MythosArea.ttslua b/src/core/MythosArea.ttslua index 25492b2a..29db5e1d 100644 --- a/src/core/MythosArea.ttslua +++ b/src/core/MythosArea.ttslua @@ -1,6 +1,6 @@ -local searchLib = require("util/SearchLib") local guidReferenceApi = require("core/GUIDReferenceApi") local playAreaApi = require("core/PlayAreaApi") +local searchLib = require("util/SearchLib") local tokenArrangerApi = require("accessories/TokenArrangerApi") local tokenChecker = require("core/token/TokenChecker") local tokenSpawnTrackerApi = require("core/token/TokenSpawnTrackerApi") @@ -18,16 +18,9 @@ local ENCOUNTER_DISCARD_AREA = { local ENCOUNTER_DECK_POS = { x = -3.93, y = 1, z = 5.76 } local ENCOUNTER_DISCARD_POSITION = { x = -3.85, y = 1, z = 10.38 } local isReshuffling = false - --- scenario metadata local currentScenario, useFrontData, tokenData - --- object references local TRASH, DATA_HELPER --- we use this to turn off collision handling until onLoad() is complete -local collisionEnabled = false - function onLoad(saveState) if saveState ~= nil then local loadedState = JSON.decode(saveState) or {} @@ -37,7 +30,6 @@ function onLoad(saveState) end TRASH = guidReferenceApi.getObjectByOwnerAndType("Mythos", "Trash") DATA_HELPER = guidReferenceApi.getObjectByOwnerAndType("Mythos", "DataHelper") - collisionEnabled = true end function onSave() @@ -48,9 +40,12 @@ function onSave() }) end +--------------------------------------------------------- +-- collison and container event handling +--------------------------------------------------------- + -- TTS event handler. Handles scenario name event triggering and encounter card token resets. function onCollisionEnter(collisionInfo) - if not collisionEnabled then return end local object = collisionInfo.collision_object if object.getName() == "Scenario" then @@ -83,7 +78,6 @@ end -- TTS event handler. Handles scenario name event triggering function onCollisionExit(collisionInfo) - if not collisionEnabled then return end local object = collisionInfo.collision_object -- reset token metadata if scenario reference card is removed @@ -144,7 +138,7 @@ function getEncounterDeck() return obj end end - + -- if no deck was found, return the first hit (a card) if #searchResult > 0 then return searchResult[1] @@ -153,14 +147,14 @@ end -- 'params' contains the position, rotation and a boolean to force a faceup draw function drawEncounterCard(params) - local card - local deck = getEncounterDeck() + local encounterDeck = getEncounterDeck() - if deck then - if deck.type == "Deck" then - card = deck.takeObject() + if encounterDeck then + local card + if encounterDeck.type == "Deck" then + card = encounterDeck.takeObject() else - card = deck + card = encounterDeck end actualEncounterCardDraw(card, params) else @@ -206,11 +200,10 @@ end -- helper functions --------------------------------------------------------- --- Simple method to check if the given point is in a specified area. Local use only, ----@param point Vector. Point to check, only x and z values are relevant ----@param bounds Table. Defined area to see if the point is within. See MAIN_PLAY_AREA for sample --- bounds definition. ----@return Boolean. True if the point is in the area defined by bounds +-- Simple method to check if the given point is in a specified area +---@param point Vector Point to check, only x and z values are relevant +---@param bounds Table Defined area to see if the point is within +---@return Boolean: True if the point is in the area defined by bounds function inArea(point, bounds) return (point.x < bounds.upperLeft.x and point.x > bounds.lowerRight.x