Fix bug occurring when a retriggered card has a non-scoring card to the left (#218)

---------

Co-authored-by: MathisMartin31 <mathis.martin31@gmail.com>
This commit is contained in:
Geralt
2025-11-12 15:06:37 +01:00
committed by GitHub
parent 4183f563ab
commit 1f3d3bffbd
+66 -57
View File
@@ -221,9 +221,12 @@ static int hand_selections = 0;
// Keeping track of cards scored
static int scored_card_index = 0;
static int previous_scored_card_index = 0;
// Keeping track of what Jokers are scored at each step
static ListItr _joker_scored_itr;
static ListItr _joker_card_scored_end_itr;
static ListItr _joker_round_end_itr;
static int selection_x = 0;
static int selection_y = 0;
@@ -2050,7 +2053,7 @@ static void select_highcard_cards_in_played_hand()
card_object_set_selected(played[highest_rank_index], true);
}
static void cards_in_hand_update_loop(bool* discarded_card, int* played_selections, bool* sound_played)
static void cards_in_hand_update_loop(bool* discarded_card, bool* sound_played)
{
// TODO: Break this function up into smaller ones, Gods be good
for (int i = hand_top + 1; i >= 0; i--) // Start from the end of the hand and work backwards because that's how Balatro does it
@@ -2124,7 +2127,7 @@ static void cards_in_hand_update_loop(bool* discarded_card, int* played_selectio
cards_drawn = 0;
hand_selections = 0;
timer = TM_ZERO;
*played_selections = played_top + 1;
scored_card_index = played_top + 1;
switch (hand_type) // select the cards that apply to the hand type
{
@@ -2200,7 +2203,7 @@ static bool check_and_score_joker_for_event(ListItr* starting_joker_itr, CardObj
return false;
}
static void played_cards_update_loop(bool* discarded_card, int* played_selections, bool* sound_played)
static void played_cards_update_loop(bool* discarded_card, bool* sound_played)
{
// TODO: Break this function up into smaller ones.
@@ -2226,11 +2229,11 @@ static void played_cards_update_loop(bool* discarded_card, int* played_selection
{
case PLAY_PLAYING:
if (i == 0 && (timer % FRAMES(10) == 0 || !card_object_is_selected(played[played_top - *played_selections])) && timer > FRAMES(40))
if (i == 0 && (timer % FRAMES(10) == 0 || !card_object_is_selected(played[played_top - scored_card_index])) && timer > FRAMES(40))
{
(*played_selections)--;
scored_card_index--;
if (*played_selections == 0)
if (scored_card_index == 0)
{
_joker_scored_itr = list_itr_create(&_owned_jokers_list);
timer = TM_ZERO;
@@ -2238,7 +2241,7 @@ static void played_cards_update_loop(bool* discarded_card, int* played_selection
}
}
if (card_object_is_selected(played[i]) && played_top - i >= *played_selections)
if (card_object_is_selected(played[i]) && played_top - i >= scored_card_index)
{
played_y -= int2fx(10);
}
@@ -2252,7 +2255,7 @@ static void played_cards_update_loop(bool* discarded_card, int* played_selection
return;
}
if (card_object_is_selected(played[i]) && played_top - i >= *played_selections)
if (card_object_is_selected(played[i]) && played_top - i >= scored_card_index)
{
played_y -= int2fx(10);
}
@@ -2264,67 +2267,76 @@ static void played_cards_update_loop(bool* discarded_card, int* played_selection
if (i == 0 && (timer % FRAMES(30) == 0) && timer > FRAMES(40))
{
// We are about to score played Cards, then Jokers.
// If we need to retrigger, then we have scored a card previously
// and thus have incremented scored_card_index by 1.
// Take out this increment to score the previous card again
// and reset the scored Joker index to 0 to go back to the beginning
// We are about to score played Cards, then Jokers. If we
// need to retrigger, then we have scored a card previously
// and thus have incremented scored_card_index until we found a scoring card.
// Go back to the last scored card to score it again
if (retrigger)
{
retrigger = false;
scored_card_index--;
(*played_selections)--;
_joker_scored_itr = list_itr_create(&_owned_jokers_list);
scored_card_index = previous_scored_card_index;
}
// So pretend "played_selections" is now called "scored_card_index" and it counts the number of cards that have been scored
for (int j = scored_card_index; j <= played_top+1; j++) // allow past played_top to score jokers for last played card
{
tte_erase_rect_wrapper(PLAYED_CARDS_SCORES_RECT);
tte_erase_rect_wrapper(PLAYED_CARDS_SCORES_RECT);
// Trigger all Jokers after each card scored
if (*played_selections > 0)
// Activate Jokers for the previous scored card if any
if (scored_card_index > 0 && card_object_is_selected(played[previous_scored_card_index]))
{
if (check_and_score_joker_for_event(&_joker_scored_itr, played[previous_scored_card_index], JOKER_EVENT_ON_CARD_SCORED))
{
if (check_and_score_joker_for_event(&_joker_scored_itr, played[*played_selections - 1], JOKER_EVENT_ON_CARD_SCORED))
{
return;
}
// Trigger all Jokers that have an effect when a card finishes scoring
// (e.g. retriggers) after activating all the other scored_card Jokers normally
_joker_scored_itr = list_itr_create(&_owned_jokers_list);
if (check_and_score_joker_for_event(&_joker_scored_itr, played[*played_selections - 1], JOKER_EVENT_ON_CARD_SCORED_END))
{
return;
}
return;
}
// Score card
scored_card_index++; // Count the number of cards that have been scored
if (j <= played_top && card_object_is_selected(played[j]))
// Trigger all Jokers that have an effect when a card finishes scoring
// (e.g. retriggers) after activating all the other scored_card Jokers normally
if (check_and_score_joker_for_event(&_joker_card_scored_end_itr, played[previous_scored_card_index], JOKER_EVENT_ON_CARD_SCORED_END))
{
tte_set_pos(fx2int(played[j]->sprite_object->x) + 8, SCORED_CARD_TEXT_Y); // Offset of 16 pixels to center the text on the card
return;
}
}
// Score card
if (scored_card_index <= played_top)
{
CardObject* scored_card_object = played[scored_card_index];
if (card_object_is_selected(scored_card_object))
{
tte_set_pos(fx2int(scored_card_object->sprite_object->x) + TILE_SIZE, SCORED_CARD_TEXT_Y); // Offset of 1 tile to keep the text on the card
tte_set_special(TTE_BLUE_PB * TTE_SPECIAL_PB_MULT_OFFSET); // Set text color to blue from background memory
// Write the score to a character buffer variable
char score_buffer[INT_MAX_DIGITS + 2]; // for '+' and null terminator
snprintf(score_buffer, sizeof(score_buffer), "+%d", card_get_value(played[j]->card));
snprintf(score_buffer, sizeof(score_buffer), "+%d", card_get_value(scored_card_object->card));
tte_write(score_buffer);
*played_selections = scored_card_index;
card_object_shake(played[j], SFX_CARD_SELECT);
card_object_shake(scored_card_object, SFX_CARD_SELECT);
// Relocated card scoring logic here
chips += card_get_value(played[j]->card);
chips += card_get_value(scored_card_object->card);
display_chips();
// Allow Joker scoring
_joker_scored_itr = list_itr_create(&_owned_jokers_list);
return;
_joker_card_scored_end_itr = list_itr_create(&_owned_jokers_list);
}
// store last played card for next Jokers and possible retriggers
// and search the next scoring card
previous_scored_card_index = scored_card_index;
do
{
scored_card_index++;
if (scored_card_index > played_top)
{
return;
}
}
while (!card_object_is_selected(played[scored_card_index]));
return;
}
play_state = PLAY_SCORING_HELD;
@@ -2381,15 +2393,14 @@ static void played_cards_update_loop(bool* discarded_card, int* played_selection
}
// Trigger hand end effect for all jokers once they are done scoring
ListItr round_end_itr = list_itr_create(&_owned_jokers_list);
if (check_and_score_joker_for_event(&round_end_itr, NULL, JOKER_EVENT_ON_HAND_SCORED_END))
if (check_and_score_joker_for_event(&_joker_round_end_itr, NULL, JOKER_EVENT_ON_HAND_SCORED_END))
{
return;
}
play_state = PLAY_ENDING;
timer = TM_ZERO;
*played_selections = played_top + 1; // Reset the played selections to the top of the played stack
scored_card_index = played_top + 1; // Reset the scored card index to the top of the played stack
break;
}
@@ -2401,18 +2412,18 @@ static void played_cards_update_loop(bool* discarded_card, int* played_selection
case PLAY_ENDING: // This is the reverse of PLAY_PLAYING. The cards get reset back to their neutral position sequentially
if (i == 0 && (timer % FRAMES(10) == 0 || !card_object_is_selected(played[played_top - *played_selections])) && timer > FRAMES(40))
if (i == 0 && (timer % FRAMES(10) == 0 || !card_object_is_selected(played[played_top - scored_card_index])) && timer > FRAMES(40))
{
(*played_selections)--;
scored_card_index--;
if (*played_selections == 0)
if (scored_card_index == 0)
{
play_state = PLAY_ENDED;
timer = TM_ZERO;
}
}
if (card_object_is_selected(played[i]) && played_top - i <= *played_selections - 1)
if (card_object_is_selected(played[i]) && played_top - i <= scored_card_index - 1)
{
played_y -= int2fx(10);
}
@@ -2454,7 +2465,6 @@ static void played_cards_update_loop(bool* discarded_card, int* played_selection
play_state = PLAY_PLAYING;
cards_drawn = 0;
hand_selections = 0;
*played_selections = 0;
played_top = -1; // Reset the played stack
scored_card_index = 0;
_joker_scored_itr = list_itr_create(&_owned_jokers_list);
@@ -2544,12 +2554,11 @@ static void game_playing_on_update()
game_playing_discarded_cards_loop();
static int played_selections = 0;
static bool sound_played = false;
bool discarded_card = false;
cards_in_hand_update_loop(&discarded_card, &played_selections, &sound_played);
played_cards_update_loop(&discarded_card, &played_selections, &sound_played);
cards_in_hand_update_loop(&discarded_card, &sound_played);
played_cards_update_loop(&discarded_card, &sound_played);
game_playing_ui_text_update();