--------------------------------------------------------- -- general setup --------------------------------------------------------- -- set true to enable debug logging DEBUG = false -- we use this to turn off collision handling until onLoad() is complete COLLISION_ENABLED = false -- used for recreating the link to a custom data helper after image change customDataHelper = nil local SHIFT_OFFSETS = { left = { x = 0.00, y = 0, z = 7.67 }, right = { x = 0.00, y = 0, z = -7.67 }, up = { x = 6.54, y = 0, z = 0.00 }, down = { x = -6.54, y = 0, z = 0.00 } } local SHIFT_EXCLUSION = { ["b7b45b"] = true, ["f182ee"] = true, ["721ba2"] = true } local tokenManager = require("core/token/TokenManager") local INVESTIGATOR_COUNTER_GUID = "f182ee" local PLAY_AREA_ZONE_GUID = "a2f932" local lastCollisionObject local currentScenario --------------------------------------------------------- -- general code --------------------------------------------------------- function onSave() return JSON.encode({ currentScenario = currentScenario }) end function onLoad(saveState) -- records locations we have spawned clues for local saveData = JSON.decode(saveState) or {} currentScenario = saveData.currentScenario self.interactable = DEBUG Wait.time(function() COLLISION_ENABLED = true end, 1) end function log(message) if DEBUG then print(message) end 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 function updateLocations(args) customDataHelper = getObjectFromGUID(args[1]) if customDataHelper ~= nil then tokenManager.addLocationData(customDataHelper.getTable("LOCATIONS_DATA")) end end function onCollisionEnter(collision_info) local obj = collision_info.collision_object -- only trigger the following once every 0.1s for each object -- (otherwise TTS fires this event 20 times and that causes 20 JSON decodes) if not COLLISION_ENABLED or lastCollisionObject == obj then return end lastCollisionObject = obj Wait.time(function() lastCollisionObject = nil end, 0.1) -- check if we should spawn clues here and do so according to playercount if shouldSpawnTokens(obj) then tokenManager.spawnForCard(obj) end end function shouldSpawnTokens(card) local metadata = JSON.decode(card.getGMNotes()) if metadata == nil then return tokenManager.hasLocationData(card) end return metadata.type == "Location" or metadata.type == "Enemy" or metadata.type == "Treachery" or metadata.weakness 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 String Color of the player requesting the shift. Used solely to send an error --- message in the unlikely case that the scripting zone has been deleted function shiftContentsUp(playerColor) shiftContents(playerColor, "up") end function shiftContentsDown(playerColor) shiftContents(playerColor, "down") end function shiftContentsLeft(playerColor) shiftContents(playerColor, "left") end function shiftContentsRight(playerColor) shiftContents(playerColor, "right") end function shiftContents(playerColor, direction) local zone = getObjectFromGUID(PLAY_AREA_ZONE_GUID) if not zone then broadcastToColor("Scripting zone couldn't be found.", playerColor, "Red") return end for _, object in ipairs(zone.getObjects()) do if not (SHIFT_EXCLUSION[object.getGUID()] or object.hasTag("displacement_excluded")) then object.translate(SHIFT_OFFSETS[direction]) end end end -- Returns the current value of the investigator counter from the playmat ---@return Integer. Number of investigators currently set on the counter function getInvestigatorCount() local investigatorCounter = getObjectFromGUID("f182ee") return investigatorCounter.getVar("val") end -- Reset the play area's tracking of which cards have had tokens spawned. function resetSpawnedCards() spawnedLocationGUIDs = {} end function onScenarioChanged(scenarioName) currentScenario = scenarioName end