discard function and color detection updates

This commit is contained in:
Chr1Z93 2023-02-28 15:00:28 +01:00
parent c68667a2ce
commit 4884899f30
5 changed files with 173 additions and 178 deletions

View File

@ -22,7 +22,7 @@
"ImageURL": "http://cloud-3.steamusercontent.com/ugc/1704036721123215146/E44A3B99EACF310E49E94977151A03C9A3DC7F17/", "ImageURL": "http://cloud-3.steamusercontent.com/ugc/1704036721123215146/E44A3B99EACF310E49E94977151A03C9A3DC7F17/",
"WidthScale": 0 "WidthScale": 0
}, },
"Description": "- Displays the hand size (total or by title for \"Dream Enhancing Serum\"), hover over it to briefly toggle counting method\n\n- Adds a context menu to \"Short Supply\" for the 1st turn\n\n- Allows you to randomly discard a card from your hand\n\nSee context menu for additional information.", "Description": "Displays the hand size (total or by title for \"Dream Enhancing Serum\"), hover over it to briefly toggle counting method.\n\nAllows you to randomly discard a card from your hand.",
"DragSelectable": true, "DragSelectable": true,
"GMNotes": "", "GMNotes": "",
"GUID": "450688", "GUID": "450688",
@ -34,7 +34,7 @@
"LayoutGroupSortIndex": 0, "LayoutGroupSortIndex": 0,
"Locked": false, "Locked": false,
"LuaScript": "require(\"accessories/HandHelper\")", "LuaScript": "require(\"accessories/HandHelper\")",
"LuaScriptState": "[\"Green\",false]", "LuaScriptState": "",
"MeasureMovement": false, "MeasureMovement": false,
"Name": "Custom_Tile", "Name": "Custom_Tile",
"Nickname": "Hand Helper", "Nickname": "Hand Helper",

View File

@ -1,16 +1,11 @@
local playmatAPI = require("playermat/PlaymatApi") local playmatAPI = require("playermat/PlaymatApi")
local matColor
local handColor
local loopId
local buttonParamaters = {} function onLoad()
buttonParamaters.function_owner = self local buttonParamaters = {}
buttonParamaters.function_owner = self
-- saving "playerColor" and "des"
function onSave() return JSON.encode({ playerColor, des}) end
function onLoad(saved_data)
-- loading saved data
local loaded_data = JSON.decode(saved_data)
playerColor = loaded_data[1] or Player.getAvailableColors()[1]
des = loaded_data[2] or false
-- index 0: button as hand size label -- index 0: button as hand size label
buttonParamaters.hover_color = "White" buttonParamaters.hover_color = "White"
@ -23,77 +18,68 @@ function onLoad(saved_data)
self.createButton(buttonParamaters) self.createButton(buttonParamaters)
-- index 1: button to toggle "des" -- index 1: button to toggle "des"
buttonParamaters.label = "DES: " .. (des and "✓" or "✗") buttonParamaters.label = "DES: ✗"
buttonParamaters.click_function = "toggleDES" buttonParamaters.click_function = "none"
buttonParamaters.position = { 0.475, 0.11, 0.25 } buttonParamaters.position = { 0, 0.11, 0.25 }
buttonParamaters.height = 175 buttonParamaters.height = 0
buttonParamaters.width = 440 buttonParamaters.width = 0
buttonParamaters.font_size = 90 buttonParamaters.font_size = 120
buttonParamaters.font_color = "Black"
self.createButton(buttonParamaters) self.createButton(buttonParamaters)
-- index 2: button to discard a card -- index 2: button to discard a card
buttonParamaters.label = "discard random card" buttonParamaters.label = "discard random card"
buttonParamaters.click_function = "discardRandom" buttonParamaters.click_function = "discardRandom"
buttonParamaters.position = { 0, 0.11, 0.7 } buttonParamaters.position = { 0, 0.11, 0.7 }
buttonParamaters.height = 175
buttonParamaters.width = 900 buttonParamaters.width = 900
buttonParamaters.font_size = 90
buttonParamaters.font_color = "Black"
self.createButton(buttonParamaters) self.createButton(buttonParamaters)
-- index 3: button to select color updateColors()
buttonParamaters.label = playerColor
buttonParamaters.color = playerColor
buttonParamaters.hover_color = playerColor
buttonParamaters.click_function = "changeColor"
buttonParamaters.tooltip = "change color"
buttonParamaters.position = { -0.475, 0.11, 0.25 }
buttonParamaters.width = 440
self.createButton(buttonParamaters)
-- start loop to update card count -- start loop to update card count
loopId = Wait.time(||updateValue(), 1, -1) loopId = Wait.time(updateValue, 1, -1)
-- context menu to quickly bind color
self.addContextMenuItem("Bind to my color", function(color)
changeColor(_, _, _, color)
end)
-- context menu to display additional information
self.addContextMenuItem("More Information", function()
printToAll("------------------------------", "White")
printToAll("Hand Helper by Chr1Z", "Orange")
printToAll("original by Tikatoy", "White")
printToAll("Note: 'Hidden' cards can't be randomly discarded.", "Yellow")
printToAll("Set them aside beforehand!", "Yellow")
end)
-- initialize the pseudo random number generator
math.randomseed(os.time())
end end
-- updates colors when object is dropped somewhere
function onDrop() updateColors() end
-- toggles counting method briefly
function onObjectHover(hover_color, obj) function onObjectHover(hover_color, obj)
-- only continue if correct player hovers over "self" -- only continue if correct player hovers over "self"
if obj ~= self or hover_color ~= playerColor then return end if obj ~= self or hover_color ~= handColor then return end
-- stop loop, toggle "des" and displayed value briefly, then start new loop -- stop loop, toggle "des" and displayed value briefly, then start new loop after 2s
Wait.stop(loopId) Wait.stop(loopId)
des = not des updateValue(true)
updateValue() Wait.time(function() loopId = Wait.time(updateValue, 1, -1) end, 2)
des = not des
loopId = Wait.time(||updateValue(), 1, -1)
end end
-- toggle "des" and update button label -- updates the matcolor and handcolor variable
function toggleDES() function updateColors()
des = not des matColor = playmatAPI.getMatColorByPosition(self.getPosition())
self.editButton({index = 1, label = "DES: " .. (des and "✓" or "✗")}) handColor = playmatAPI.getHandColor(matColor)
updateValue() self.setName(handColor .. " Hand Helper")
end end
-- count cards in hand (by name for DES) -- count cards in hand (by name for DES)
function updateValue() function updateValue(toggle)
if not playerExists(playerColor) then return end -- update colors if handColor doesn't own a handzone
if Player[handColor].getHandCount() == 0 then
updateColors()
end
local hand = Player[playerColor].getHandObjects() -- if there is still no handzone, then end here
if Player[handColor].getHandCount() == 0 then return end
-- get state of "Dream-Enhancing Serum" from playermat and update button label
local des = playmatAPI.isDES(matColor)
if toggle then des = not des end
self.editButton({ index = 1, label = "DES: " .. (des and "✓" or "✗") })
-- count cards in hand
local hand = Player[handColor].getHandObjects()
local size = 0 local size = 0
if des then if des then
@ -102,10 +88,10 @@ function updateValue()
if obj.tag == "Card" then if obj.tag == "Card" then
local name = obj.getName() local name = obj.getName()
local title = string.match(name, '(.+)(%s%(%d+%))') or name local title = string.match(name, '(.+)(%s%(%d+%))') or name
cardHash[title] = obj cardHash[title] = true
end end
end end
for _, obj in pairs(cardHash) do for _, title in pairs(cardHash) do
size = size + 1 size = size + 1
end end
else else
@ -113,82 +99,12 @@ function updateValue()
if obj.tag == "Card" then size = size + 1 end if obj.tag == "Card" then size = size + 1 end
end end
end end
-- change button label and color
self.editButton({index = 0, font_color = des and "Green" or "White", label = size}) -- update button label and color
self.editButton({ index = 0, font_color = des and "Green" or "White", label = size })
end end
-- allows change of color via external call -- discards a random non-hidden card from hand
function externalColorChange(newColor)
changeColor(_, _, _, newColor)
end
-- get index of current color and move up one step (or down for right-click)
function changeColor(_, _, isRightClick, color)
if color then
playerColor = color
else
local COLORS = Player.getAvailableColors()
local pos = indexOf(COLORS, playerColor)
if isRightClick then
if pos == nil or pos == 1 then pos = #COLORS
else pos = pos - 1 end
else
if pos == nil or pos == #COLORS then pos = 1
else pos = pos + 1 end
end
-- update playerColor
playerColor = COLORS[pos]
end
-- update "change color" button (note: remove and create instantly updates hover_color)
buttonParamaters.label = playerColor
buttonParamaters.color = playerColor
buttonParamaters.hover_color = playerColor
self.removeButton(3)
self.createButton(buttonParamaters)
end
---------------------------------------------------------
-- discards a random card from hand
---------------------------------------------------------
function discardRandom() function discardRandom()
if not playerExists(playerColor) then return end playmatAPI.doDiscardOne(matColor)
-- error handling: hand is empty
local hand = Player[playerColor].getHandObjects()
if #hand == 0 then
broadcastToAll("Cannot discard from empty hand!", "Red")
else
local searchPos = Player[playerColor].getHandTransform().position
local discardPos = playmatAPI.getDiscardPosition(playmatAPI.getMatColorByPosition(searchPos))
if discardPos == nil then
broadcastToAll("Couldn't retrieve discard position from playermat!", "Red")
return
end
local num = math.random(1, #hand)
hand[num].setPosition(discardPos)
broadcastToAll(playerColor .. " randomly discarded card " .. num .. "/" .. #hand .. ".", "White")
end
end
---------------------------------------------------------
-- helper functions
---------------------------------------------------------
-- helper to search array
function indexOf(array, value)
for i, v in ipairs(array) do
if v == value then return i end
end
end
-- helper to check if player exists
function playerExists(color)
local COLORS = Player.getAvailableColors()
return indexOf(COLORS, color) and true or false
end end

View File

@ -925,9 +925,7 @@ function spawnHelperObject(name, position, rotation, color)
local spawnTable = { local spawnTable = {
position = position, position = position,
callback_function = function(object) callback_function = function(object)
if name == "Hand Helper" then if name == "Token Arranger" then
Wait.time(function() object.call("externalColorChange", color) end, 0.1)
elseif name == "Token Arranger" then
Wait.time(function() object.call("layout") end, 0.1) Wait.time(function() object.call("layout") end, 0.1)
end end
end end

View File

@ -5,7 +5,7 @@ local tokenChecker = require("core/token/TokenChecker")
local DEBUG = false local DEBUG = false
-- we use this to turn off collision handling until onLoad() is complete -- we use this to turn off collision handling until onLoad() is complete
local COLLISION_ENABLED = false local collisionEnabled = false
-- position offsets relative to mat [x, y, z] -- position offsets relative to mat [x, y, z]
local DRAWN_ENCOUNTER_CARD_OFFSET = {1.365, 0.5, -0.635} local DRAWN_ENCOUNTER_CARD_OFFSET = {1.365, 0.5, -0.635}
@ -62,6 +62,9 @@ local RESOURCE_COUNTER
activeInvestigatorId = "00000" activeInvestigatorId = "00000"
local isDrawButtonVisible = false local isDrawButtonVisible = false
-- global variable to report "Dream-Enhancing Serum" status
isDES = false
function onSave() function onSave()
return JSON.encode({ return JSON.encode({
zoneID = zoneID, zoneID = zoneID,
@ -122,7 +125,9 @@ function onLoad(save_state)
showDrawButton(isDrawButtonVisible) showDrawButton(isDrawButtonVisible)
if getObjectFromGUID(zoneID) == nil then spawnDeckZone() end if getObjectFromGUID(zoneID) == nil then spawnDeckZone() end
COLLISION_ENABLED = true collisionEnabled = true
math.randomseed(os.time())
end end
--------------------------------------------------------- ---------------------------------------------------------
@ -409,6 +414,51 @@ function shuffleDiscardIntoDeck()
discardPile = nil discardPile = nil
end end
-- discard a random non-hidden card from hand
function doDiscardOne()
local handColor = getHandColor()
local hand = Player[handColor].getHandObjects()
if #hand == 0 then
broadcastToAll("Cannot discard from empty hand!", "Red")
else
local choices = {}
for i = 1, #hand do
local notes = JSON.decode(hand[i].getGMNotes())
if notes ~= nil then
if notes.hidden ~= true then
table.insert(choices, i)
end
else
table.insert(choices, i)
end
end
if #choices == 0 then
broadcastToAll("Hidden cards can't be randomly discarded.", "Orange")
return
end
-- get a random non-hidden card (from the "choices" table)
local num = math.random(1, #choices)
hand[choices[num]].setPosition(returnGlobalDiscardPosition())
broadcastToAll(handColor .. " randomly discarded card " .. choices[num] .. "/" .. #hand .. ".", "White")
end
end
-- gets the hand color of the closest seated player (by roughly cutting the table into quarters)
function getHandColor()
for _, handColor in ipairs(Player.getAvailableColors()) do
local handPosition = Player[handColor].getHandTransform().position
if (PLAYER_COLOR == "White" and handPosition.x < -42 and handPosition.z > 0)
or (PLAYER_COLOR == "Orange" and handPosition.x < -42 and handPosition.z < 0)
or (PLAYER_COLOR == "Green" and handPosition.x > -42 and handPosition.z > 0)
or (PLAYER_COLOR == "Red" and handPosition.x > -42 and handPosition.z < 0) then
return handColor
end
end
end
--------------------------------------------------------- ---------------------------------------------------------
-- playmat token spawning -- playmat token spawning
--------------------------------------------------------- ---------------------------------------------------------
@ -493,9 +543,14 @@ function spawnTokensFor(object)
end end
function onCollisionEnter(collision_info) function onCollisionEnter(collision_info)
if not COLLISION_ENABLED then return end
local object = collision_info.collision_object local object = collision_info.collision_object
-- detect if "Dream-Enhancing Serum" is placed
if object.getName() == "Dream-Enhancing Serum" then isDES = true end
-- only continue if loading is completed
if not collisionEnabled then return end
-- only continue for cards -- only continue for cards
if object.name ~= "Card" and object.name ~= "CardCustom" then return end if object.name ~= "Card" and object.name ~= "CardCustom" then return end
@ -509,6 +564,11 @@ function onCollisionEnter(collision_info)
end end
end end
-- detect if "Dream-Enhancing Serum" is removed
function onCollisionExit(collision_info)
if collision_info.collision_object.getName() == "Dream-Enhancing Serum" then isDES = false end
end
function shouldSpawnTokens(card) function shouldSpawnTokens(card)
if card.is_face_down then if card.is_face_down then
return false return false

View File

@ -41,6 +41,20 @@ do
end end
end end
-- Returns the color of the player's hand that is seated next to the playermat
---@param matColor String Color of the playermat
PlaymatApi.getHandColor = function(matColor)
local mat = getObjectFromGUID(MAT_IDS[matColor])
return mat.call("getHandColor")
end
-- Returns if there is the card"Dream-Enhancing Serum" on the requested playermat
---@param matColor String Color of the playermat
PlaymatApi.isDES = function(matColor)
local mat = getObjectFromGUID(MAT_IDS[matColor])
return mat.getVar("isDES")
end
-- Returns the draw deck of the requested playmat -- Returns the draw deck of the requested playmat
---@param matColor String Color of the playermat ---@param matColor String Color of the playermat
PlaymatApi.getDrawDeck = function(matColor) PlaymatApi.getDrawDeck = function(matColor)
@ -116,6 +130,13 @@ do
end end
end end
-- Discard a non-hidden card from the corresponding player's hand
PlaymatApi.doDiscardOne = function(matColor)
for _, mat in ipairs(internal.getMatForColor(matColor)) do
mat.call("doDiscardOne")
end
end
-- Convenience function to look up a mat's object by color, or get all mats. -- Convenience function to look up a mat's object by color, or get all mats.
---@param matColor String for one of the active player colors - White, Orange, Green, Red. Also ---@param matColor String for one of the active player colors - White, Orange, Green, Red. Also
-- accepts "All" as a special value which will return all four mats. -- accepts "All" as a special value which will return all four mats.