diff --git a/source/data_structures/stack.c b/source/data_structures/stack.c index 73f6adfe..7929fbd5 100644 --- a/source/data_structures/stack.c +++ b/source/data_structures/stack.c @@ -14,7 +14,7 @@ bool stack_empty(Stack* stack) int stack_len(Stack* stack) { - + return stack->top + 1; } void stack_push(Stack* stack, void* data) diff --git a/source/game.c b/source/game.c index e3ab4954..2ce3b2b7 100644 --- a/source/game.c +++ b/source/game.c @@ -23,6 +23,7 @@ #include "list.h" #include "random.h" #include "save.h" +#include "stack.h" #include "selection_grid.h" #include "soundbank.h" #include "splash_screen.h" @@ -332,8 +333,7 @@ static int played_top = -1; static Card* deck[MAX_DECK_SIZE] = {NULL}; static int deck_top = -1; -static Card* discard_pile[MAX_DECK_SIZE] = {NULL}; -static int discard_top = -1; +STACK_DEFINE(discard_stack, MAX_DECK_SIZE) // Joker Special Variables static int shortcut_joker_count = 0; @@ -384,20 +384,6 @@ static inline Card* deck_pop() return deck[deck_top--]; } -static inline void discard_push(Card* card) -{ - if (discard_top >= MAX_DECK_SIZE - 1) - return; - discard_pile[++discard_top] = card; -} - -static inline Card* discard_pop() -{ - if (discard_top < 0) - return NULL; - return discard_pile[discard_top--]; -} - static inline void jokers_available_to_shop_init(void) { reset_shop_jokers(); @@ -1040,8 +1026,8 @@ static int deck_get_size(void) static int deck_get_max_size(void) { - // This is the max amount of cards that the player currently has in their possession - return get_hand_top() + played_top + deck_top + discard_top + 4; + // this should eventually be related to size of the deck + return MAX_DECK_SIZE; } static inline void deck_shuffle(void) @@ -1480,7 +1466,7 @@ static inline void card_in_hand_loop_handle_discard_and_shuffling( if (hand[card_idx]->sprite_object->x >= *hand_x) { - discard_push(hand[card_idx]->card); + stack_push(&discard_stack, hand[card_idx]->card); card_object_destroy(&hand[card_idx]); reorder_card_sprites_layers(); @@ -1777,10 +1763,9 @@ static bool play_ended_played_cards_update(int played_idx) // card has exited the screen, now discard it and set it to NULL if (played[played_idx]->sprite_object->x >= int2fx(CARD_DISCARD_PNT.x)) { - discard_push(played[played_idx]->card); // Push the card to the discard pile + stack_push(&discard_stack, played[played_idx]->card); card_object_destroy(&played[played_idx]); - // played_top--; cards_drawn++; // This technically isn't drawing cards, I'm just reusing the variable sound_played = false; // Allow for the sound for the next card to be played @@ -2264,7 +2249,7 @@ static inline void game_playing_process_card_draw() static inline void game_playing_discarded_cards_loop(void) { // Discarded cards loop (mainly for shuffling) - if (hand_nb_held_cards() == 0 && get_hand_state() == HAND_SHUFFLING && discard_top >= -1 && + if (hand_nb_held_cards() == 0 && get_hand_state() == HAND_SHUFFLING && g_game_vars.timer > FRAMES(10)) { // Change the background to the round end background. This is how it works in Balatro, so @@ -2275,7 +2260,7 @@ static inline void game_playing_discarded_cards_loop(void) static CardObject* discarded_card_object = NULL; if (discarded_card_object == NULL) { - discarded_card_object = card_object_new(discard_pop()); + discarded_card_object = card_object_new((Card*)stack_pop(&discard_stack)); // discarded_card_object->sprite = sprite_new(ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF, // ATTR1_SIZE_32, // card_sprite_lut[discarded_card_object->card->suit][discarded_card_object->card->rank], @@ -2309,7 +2294,7 @@ static inline void game_playing_discarded_cards_loop(void) } // If there are no more discarded cards, stop shuffling - if (discard_top == -1 && discarded_card_object == NULL) + if (stack_empty(&discard_stack) && discarded_card_object == NULL) { // After HAND_SHUFFLING the round is over game_playing_handle_round_over();