Fixed card drawing with reshuffle

This commit is contained in:
Chr1Z93 2024-10-29 10:39:23 +01:00
parent 5621ed2270
commit fc95bbde56
2 changed files with 98 additions and 65 deletions

View File

@ -225,8 +225,12 @@ function onObjectEnterZone(zone, object)
elseif zone.type == "Hand" and object.type == "Card" then elseif zone.type == "Hand" and object.type == "Card" then
-- make sure the card is face-up -- make sure the card is face-up
if object.is_face_down then if object.is_face_down then
if object.held_by_color then
object.held_flip_index = 0
else
object.flip() object.flip()
end end
end
-- maybe reset data about sealed tokens (if that function exists) -- maybe reset data about sealed tokens (if that function exists)
if object.hasTag("CardThatSeals") then if object.hasTag("CardThatSeals") then

View File

@ -69,7 +69,7 @@ local DECK_DISCARD_AREA = {
upperLeft = { x = -1.62, z = 0.855 }, upperLeft = { x = -1.62, z = 0.855 },
lowerRight = { x = -2.02, z = -0.245 }, lowerRight = { x = -2.02, z = -0.245 },
center = { x = -1.82, y = 0.5, z = 0.305 }, center = { x = -1.82, y = 0.5, z = 0.305 },
size = { x = 0.4, y = 3, z = 1.1 } size = Vector(0.4, 3, 1.1)
} }
-- local positions -- local positions
@ -252,13 +252,7 @@ end
-- searches the area around the draw deck and discard pile -- searches the area around the draw deck and discard pile
function searchDeckAndDiscardArea(filter) function searchDeckAndDiscardArea(filter)
local pos = self.positionToWorld(DECK_DISCARD_AREA.center) local pos = self.positionToWorld(DECK_DISCARD_AREA.center)
local scale = self.getScale() return searchArea(pos, DECK_DISCARD_AREA.size * self.getScale(), filter)
local size = {
x = DECK_DISCARD_AREA.size.x * scale.x,
y = DECK_DISCARD_AREA.size.y,
z = DECK_DISCARD_AREA.size.z * scale.z
}
return searchArea(pos, size, filter)
end end
-- rounds a number to the specified amount of decimal places -- rounds a number to the specified amount of decimal places
@ -518,6 +512,7 @@ end
-- draws the specified amount of cards (and shuffles the discard if necessary) -- draws the specified amount of cards (and shuffles the discard if necessary)
---@param numCards number Number of cards to draw ---@param numCards number Number of cards to draw
function drawCardsWithReshuffle(numCards) function drawCardsWithReshuffle(numCards)
function drawCardsCoroutine()
local deckAreaObjects = getDeckAreaObjects() local deckAreaObjects = getDeckAreaObjects()
-- Norman Withers handling -- Norman Withers handling
@ -531,9 +526,10 @@ function drawCardsWithReshuffle(numCards)
if harbinger then if harbinger then
printToColor("The Harbinger is on top of your deck, not drawing cards", messageColor) printToColor("The Harbinger is on top of your deck, not drawing cards", messageColor)
return return 1
end end
-- draw the top card if possible
local topCardDetected = false local topCardDetected = false
if deckAreaObjects.topCard ~= nil then if deckAreaObjects.topCard ~= nil then
deckAreaObjects.topCard.deal(1, playerColor) deckAreaObjects.topCard.deal(1, playerColor)
@ -541,10 +537,11 @@ function drawCardsWithReshuffle(numCards)
numCards = numCards - 1 numCards = numCards - 1
if numCards == 0 then if numCards == 0 then
flipTopCardFromDeck() flipTopCardFromDeck()
return return 1
end end
end end
-- determine deck size
local deckSize = 1 local deckSize = 1
if deckAreaObjects.draw == nil then if deckAreaObjects.draw == nil then
deckSize = 0 deckSize = 0
@ -552,24 +549,36 @@ function drawCardsWithReshuffle(numCards)
deckSize = #deckAreaObjects.draw.getObjects() deckSize = #deckAreaObjects.draw.getObjects()
end end
-- draw additional cards from existing deck
if deckSize >= numCards then if deckSize >= numCards then
drawCards(numCards) drawCards(deckAreaObjects.draw, numCards)
if topCardDetected then if topCardDetected then
flipTopCardFromDeck() flipTopCardFromDeck()
end end
else return 1
drawCards(deckSize) end
-- draw the full deck, form a new deck and draw the remaining cards
drawCards(deckAreaObjects.draw, deckSize)
if deckAreaObjects.discard ~= nil then if deckAreaObjects.discard ~= nil then
coWaitFrames(30)
shuffleDiscardIntoDeck() shuffleDiscardIntoDeck()
Wait.time(function() if deckAreaObjects.discard.type == "Card" then
drawCards(numCards - deckSize) coWaitSeconds(1 * 0.1 + 0.25)
else
coWaitSeconds(#deckAreaObjects.discard.getObjects() * 0.1 + 0.25)
end
local shuffledObjects = getDeckAreaObjects()
drawCards(shuffledObjects.draw, numCards - deckSize)
if topCardDetected then if topCardDetected then
flipTopCardFromDeck() flipTopCardFromDeck()
end end
end, 1)
end
printToColor("Take 1 horror (drawing card from empty deck)", messageColor) printToColor("Take 1 horror (drawing card from empty deck)", messageColor)
end end
return 1
end
startLuaCoroutine(self, "drawCardsCoroutine")
end end
function isHarbinger(notes) function isHarbinger(notes)
@ -595,11 +604,11 @@ function getDeckAreaObjects()
end end
-- draws the specified number of cards (reshuffling of discard pile is handled separately) -- draws the specified number of cards (reshuffling of discard pile is handled separately)
---@param cardOrDeck tts__Object Card/Deck to draw from
---@param numCards number Number of cards to draw ---@param numCards number Number of cards to draw
function drawCards(numCards) function drawCards(cardOrDeck, numCards)
local deckAreaObjects = getDeckAreaObjects() if cardOrDeck ~= nil then
if deckAreaObjects.draw then cardOrDeck.deal(numCards, playerColor)
deckAreaObjects.draw.deal(numCards, playerColor)
end end
end end
@ -639,7 +648,11 @@ function shuffleDiscardIntoDeck(player, _, elementId)
table.insert(objectsToShuffle, deckAreaObjects.topCard) table.insert(objectsToShuffle, deckAreaObjects.topCard)
-- this will be executed after the deck merging + shuffling -- this will be executed after the deck merging + shuffling
Wait.time(flipTopCardFromDeck, 1) if deckAreaObjects.discard.type == "Card" then
Wait.time(flipTopCardFromDeck, 1 * 0.1 + 0.5)
else
Wait.time(flipTopCardFromDeck, #deckAreaObjects.discard.getObjects() * 0.1 + 0.5)
end
end end
table.insert(objectsToShuffle, deckAreaObjects.discard) table.insert(objectsToShuffle, deckAreaObjects.discard)
@ -651,8 +664,9 @@ end
function flipTopCardFromDeck() function flipTopCardFromDeck()
Wait.time(function() Wait.time(function()
local deckAreaObjects = getDeckAreaObjects() local deckAreaObjects = getDeckAreaObjects()
if deckAreaObjects.topCard then if deckAreaObjects.topCard ~= nil then return end
elseif deckAreaObjects.draw then
if deckAreaObjects.draw ~= nil then
if deckAreaObjects.draw.type == "Card" then if deckAreaObjects.draw.type == "Card" then
deckAreaObjects.draw.flip() deckAreaObjects.draw.flip()
else else
@ -1711,3 +1725,18 @@ end
function getActiveInvestigatorData() return activeInvestigatorData end function getActiveInvestigatorData() return activeInvestigatorData end
function setActiveInvestigatorData(newData) activeInvestigatorData = newData end function setActiveInvestigatorData(newData) activeInvestigatorData = newData end
-- pauses the current coroutine for 'frameCount' frames
function coWaitFrames(frameCount)
for k = 1, frameCount do
coroutine.yield(0)
end
end
-- pauses the current coroutine for 'seconds' seconds
function coWaitSeconds(seconds)
local startTime = os.clock()
while os.clock() - startTime < seconds do
coroutine.yield(0)
end
end