SCED/src/core/GameKeyHandler.ttslua
2023-12-31 13:41:53 +01:00

292 lines
10 KiB
Plaintext

local blessCurseManagerApi = require("chaosbag/BlessCurseManagerApi")
local guidReferenceApi = require("core/GUIDReferenceApi")
local optionPanelApi = require("core/OptionPanelApi")
local playmatApi = require("playermat/PlaymatApi")
local searchLib = require("util/SearchLib")
local victoryDisplayApi = require("core/VictoryDisplayApi")
function onLoad()
addHotkey("Add Doom to Agenda", addDoomToAgenda)
addHotkey("Bless/Curse Status", showBlessCurseStatus)
addHotkey("Discard Object", discardObject)
addHotkey("Discard top card", discardTopDeck)
addHotkey("Move card to Victory Display", moveCardToVictoryDisplay)
addHotkey("Remove a use", removeOneUse)
addHotkey("Take clue from location", takeClueFromLocation)
addHotkey("Upkeep", triggerUpkeep)
addHotkey("Upkeep (Multi-handed)", triggerUpkeepMultihanded)
addHotkey("Wendy's Menu", addWendysMenu)
end
-- triggers the "Upkeep" function of the calling player's playmat
function triggerUpkeep(playerColor)
if playerColor == "Black" then
broadcastToColor("Triggering 'Upkeep (Multihanded)' instead", playerColor, "Yellow")
triggerUpkeepMultihanded(playerColor)
return
end
local matColor = playmatApi.getMatColor(playerColor)
playmatApi.doUpkeepFromHotkey(matColor, playerColor)
end
-- triggers the "Upkeep" function of the calling player's playmat AND
-- for all playmats that don't have a seated player, but a investigator card
function triggerUpkeepMultihanded(playerColor)
if playerColor ~= "Black" then
triggerUpkeep(playerColor)
end
local colors = Player.getAvailableColors()
for _, handColor in ipairs(colors) do
local matColor = playmatApi.getMatColor(handColor)
if playmatApi.returnInvestigatorId(matColor) ~= "00000" and Player[handColor].seated == false then
playmatApi.doUpkeepFromHotkey(matColor, playerColor)
end
end
end
-- adds 1 doom to the agenda
function addDoomToAgenda()
local doomCounter = guidReferenceApi.getObjectByOwnerAndType("Mythos", "DoomCounter")
doomCounter.call("addVal", 1)
end
-- discard the hovered object to the respective trashcan and discard tokens on it if it was a card
function discardObject(playerColor, hoveredObject)
-- only continue if an unlocked card, deck or tile was hovered
if hoveredObject == nil
or (hoveredObject.type ~= "Card" and hoveredObject.type ~= "Deck" and hoveredObject.type ~= "Tile")
or hoveredObject.locked then
broadcastToColor("Hover a token/tile or a card/deck and try again.", playerColor, "Yellow")
return
end
-- warning for locations since these are usually not meant to be discarded
if hoveredObject.hasTag("Location") then
broadcastToAll("Watch out: A location was discarded.", "Yellow")
end
-- initialize list of objects to discard
local discardTheseObjects = { hoveredObject }
-- discard tokens / tiles on cards / decks
if hoveredObject.type ~= "Tile" then
for _, obj in ipairs(searchLib.onObject(hoveredObject, "isTileOrToken")) do
table.insert(discardTheseObjects, obj)
end
end
local discardForMatColor = getColorToDiscardFor(hoveredObject, playerColor)
playmatApi.discardListOfObjects(discardForMatColor, discardTheseObjects)
end
-- discard the top card of hovered deck, calling discardObject function
function discardTopDeck(playerColor, hoveredObject)
-- only continue if an unlocked card or deck was hovered
if hoveredObject == nil
or (hoveredObject.type ~= "Card" and hoveredObject.type ~= "Deck")
or hoveredObject.locked then
broadcastToColor("Hover a deck/card and try again.", playerColor, "Yellow")
return
end
if hoveredObject.type == "Deck" then
takenCard = hoveredObject.takeObject({index = 0})
else
takenCard = hoveredObject
end
Wait.frames(function() discardObject(playerColor, takenCard) end, 1)
end
-- helper function to get the player to trigger the discard function for
function getColorToDiscardFor(hoveredObject, playerColor)
local pos = hoveredObject.getPosition()
local closestMatColor = playmatApi.getMatColorByPosition(pos)
-- check if actually on the closest playmat
local closestMat = guidReferenceApi.getObjectByOwnerAndType(closestMatColor, "Playermat")
local bounds = closestMat.getBounds()
-- define the area "near" the playmat
local bufferAroundPlaymat = 2
local areaNearPlaymat = {}
areaNearPlaymat.minX = bounds.center.x - bounds.size.x / 2 - bufferAroundPlaymat
areaNearPlaymat.maxX = bounds.center.x + bounds.size.x / 2 + bufferAroundPlaymat
areaNearPlaymat.minZ = bounds.center.z - bounds.size.z / 2 - bufferAroundPlaymat
areaNearPlaymat.maxZ = bounds.center.z + bounds.size.z / 2 + bufferAroundPlaymat
-- discard to closest mat if near it, use triggering playmat if not
local discardForMatColor
if inArea(pos, areaNearPlaymat) then
return closestMatColor
else
return playmatApi.getMatColor(playerColor)
end
end
-- moves the hovered card to the victory display
function moveCardToVictoryDisplay(_, hoveredObject)
victoryDisplayApi.placeCard(hoveredObject)
end
-- removes a use from a card (or a token if hovered)
function removeOneUse(playerColor, hoveredObject)
-- only continue if an unlocked card or tile was hovered
if hoveredObject == nil
or (hoveredObject.type ~= "Card" and hoveredObject.type ~= "Tile")
or hoveredObject.locked then
broadcastToColor("Hover a token/tile or a card and try again.", playerColor, "Yellow")
return
end
local targetObject = nil
-- discard hovered token / tile
if hoveredObject.type == "Tile" then
targetObject = hoveredObject
elseif hoveredObject.type == "Card" then
-- grab the first use type from the metadata (or nil)
local notes = JSON.decode(hoveredObject.getGMNotes()) or {}
local usesData = notes.uses or {}
local useInfo = usesData[1] or {}
local searchForType = useInfo.type
if searchForType then searchForType = searchForType:lower() end
for _, obj in ipairs(searchLib.onObject(hoveredObject), "isTileOrToken") do
if not obj.locked and obj.memo ~= "resourceCounter" then
-- check for matching object, otherwise use the first hit
if obj.memo == searchForType then
targetObject = obj
break
elseif not targetObject then
targetObject = obj
end
end
end
end
-- error handling
if not targetObject then
broadcastToColor("No tokens found!", playerColor, "Yellow")
return
end
-- handling for stacked tokens
if targetObject.getQuantity() > 1 then
targetObject = targetObject.takeObject()
end
-- feedback message
local tokenName = targetObject.getName()
if tokenName == "" then
if targetObject.memo ~= "" then
-- name handling for clue / doom
if targetObject.memo == "clueDoom" then
if targetObject.is_face_down then
tokenName = "Doom"
else
tokenName = "Clue"
end
else
tokenName = titleCase(targetObject.memo)
end
else
tokenName = "Unknown"
end
end
local playerName = Player[playerColor].steam_name
broadcastToAll(playerName .. " removed a token: " .. tokenName, playerColor)
local discardForMatColor = getColorToDiscardFor(hoveredObject, playerColor)
playmatApi.discardListOfObjects(discardForMatColor, { targetObject })
end
-- takes a clue from a location, player needs to hover the clue directly or the location
function takeClueFromLocation(playerColor, hoveredObject)
local cardName, clue
if hoveredObject == nil then
broadcastToColor("Hover a clue or card with clues and try again.", playerColor, "Yellow")
return
elseif hoveredObject.type == "Card" then
cardName = hoveredObject.getName()
local searchResult = searchLib.onObject(hoveredObject, "isClue")
if #searchResult == 0 then
broadcastToColor("This card does not have any clues on it.", playerColor, "Yellow")
return
else
clue = searchResult[1]
end
elseif hoveredObject.memo == "clueDoom" then
if hoveredObject.is_face_down then
broadcastToColor("This is a doom token and not a clue.", playerColor, "Yellow")
return
end
local searchResult = searchLib.belowPosition(hoveredObject.getPosition(), "isCard")
if #searchResult ~= 0 then
cardName = searchResult[1].getName()
end
else
broadcastToColor("Hover a clue or card with clues and try again.", playerColor, "Yellow")
return
end
local clickableClues = optionPanelApi.getOptions()["useClueClickers"]
local playerName = Player[playerColor].steam_name
local matColor = playmatApi.getMatColor(playerColor)
local pos = nil
if clickableClues then
pos = {x = 0.49, y = 2.66, z = 0.00}
playmatApi.updateCounter(matColor, "ClickableClueCounter", _, 1)
else
pos = playmatApi.transformLocalPosition({x = -1.12, y = 0.05, z = 0.7}, matColor)
end
local rot = playmatApi.returnRotation(matColor)
-- check if found clue is a stack or single token
if clue.getQuantity() > 1 then
clue.takeObject({position = pos, rotation = rot})
else
clue.setPositionSmooth(pos)
clue.setRotation(rot)
end
if cardName then
broadcastToAll(playerName .. " took one clue from " .. cardName .. ".", playerColor)
else
broadcastToAll(playerName .. " took one clue.", "Green")
end
victoryDisplayApi.update()
end
-- broadcasts the bless/curse status to the calling player
function showBlessCurseStatus(playerColor)
blessCurseManagerApi.broadcastStatus(playerColor)
end
-- adds Wendy's menu to the hovered card
function addWendysMenu(playerColor, hoveredObject)
blessCurseManagerApi.addWendysMenu(playerColor, hoveredObject)
end
-- 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
function inArea(point, bounds)
return (point.x > bounds.minX
and point.x < bounds.maxX
and point.z > bounds.minZ
and point.z < bounds.maxZ)
end
-- capitalizes the first letter
function titleCase(str)
local first = str:sub(1, 1)
local rest = str:sub(2)
return first:upper() .. rest:lower()
end