[Refactor] Separate Round logic into its own file (#570)

* Everything works, need to clean up

* cleanup

* fix rebase issue

* clang format

* docs

* clang format

* clang format

* address part of review

* Protect against edge case when discarding at end of hand

* Protect undiscard against popping a discarded card that doesn't exist

* Protect against out of bounds access to the `played` cards stack

* fix condition in `select_four_of_a_kind_cards_in_played_hand`

* change `get_played_top() + 1` to `get_played_size()`

* move `can_play/discard_hand` functions to round.c

---------

Co-authored-by: MathisMartin31 <mathis.martin31@gmail.com>
This commit is contained in:
Geralt
2026-06-27 16:57:37 -07:00
committed by GitHub
co-authored by MathisMartin31
parent f722b9eef4
commit 45eac259a5
12 changed files with 2207 additions and 2015 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ DEF_STATE_INFO(GAME_STATE_MAIN_MENU, game_main_menu_on_init, game_main_me
DEF_STATE_INFO(GAME_STATE_OPTIONS_MENU, game_options_menu_on_init, game_options_menu_on_update, game_options_menu_on_exit)
DEF_STATE_INFO(GAME_STATE_RUN_SETUP , game_run_setup_on_init, game_run_setup_on_update, game_run_setup_on_exit )
DEF_STATE_INFO(GAME_STATE_GAME_START, noop, game_start, noop )
DEF_STATE_INFO(GAME_STATE_PLAYING, game_round_on_init, game_playing_on_update, noop )
DEF_STATE_INFO(GAME_STATE_ROUND, game_round_on_init, game_round_on_update, noop )
DEF_STATE_INFO(GAME_STATE_ROUND_END, game_round_end_on_init, game_round_end_on_update, game_round_end_on_exit )
DEF_STATE_INFO(GAME_STATE_SHOP, game_shop_on_init, game_shop_on_update, game_shop_on_exit )
DEF_STATE_INFO(GAME_STATE_BLIND_SELECT, game_blind_select_on_init, game_blind_select_on_update, game_blind_select_on_exit)
+7 -28
View File
@@ -51,19 +51,6 @@ enum GameState
GAME_STATE_UNDEFINED
};
enum PlayState
{
PLAY_STARTING,
PLAY_BEFORE_SCORING,
PLAY_SCORING_CARDS,
PLAY_SCORING_CARD_JOKERS,
PLAY_SCORING_HELD_CARDS,
PLAY_SCORING_INDEPENDENT_JOKERS,
PLAY_SCORING_HAND_SCORED_END,
PLAY_ENDING,
PLAY_ENDED
};
// Game functions
void game_init(void);
@@ -81,9 +68,6 @@ void game_update(void);
void game_change_state(enum GameState new_game_state);
enum GameState game_get_state(void);
CardObject** get_played_array(void);
int get_played_top(void);
int get_scored_card_index(void);
bool is_joker_owned(int joker_id);
bool card_is_face(Card* card);
void add_joker(JokerObject* joker_object);
@@ -92,19 +76,19 @@ List* get_jokers_list(void);
List* get_expired_jokers_list(void);
List* get_discarded_jokers_list(void);
int deck_get_size(void);
int get_deck_top(void);
void deck_push(Card* card);
Card* deck_pop(void);
void deck_shuffle(void);
int get_num_discards_remaining(void);
int get_num_hands_remaining(void);
void display_deck_size_max(void);
u32 get_chips(void);
void set_chips(u32 new_chips);
void display_chips(void);
u32 get_mult(void);
void set_mult(u32 new_mult);
void display_mult(void);
void display_money(void);
void set_retrigger(bool new_retrigger);
void display_ante(void);
// joker specific functions
bool is_shortcut_joker_active(void);
@@ -112,16 +96,11 @@ int get_straight_and_flush_size(void);
void game_start(void);
// Temporary change for Refactor. Currently this compatibility binder is to allow
// simultaneous integration of the new system in `common_ui` with the the existing
// old system incrementally and without losing functionality.
void change_background_legacy(enum BackgroundId id);
void display_round(void);
void reset_background(void);
void display_hands(void);
void display_discards(void);
void display_temp_score(u32 value);
void erase_temp_score(void);
void display_score(u32 value);
#endif // GAME_H
+2
View File
@@ -24,6 +24,8 @@ enum BackgroundId
BG_MAIN_MENU
};
enum BackgroundId get_current_background(void);
/**
* Change the background
*
+72
View File
@@ -0,0 +1,72 @@
/**
* @file round.h
* @brief API relative to the Rounds we play
*/
#ifndef GAME_ROUND_H
#define GAME_ROUND_H
#include <stdbool.h>
/**
* @brief Checks whether the score that would result from the current Chips and Mult exceeds the
* current Blind's score requirement, and applies the flaming effect if needed.
*/
void toggle_flaming_score(void);
/**
* @brief Get the index of the last played card
*
* @return int
*/
int get_played_top(void);
/**
* @brief Get the number of cards played
*
* @return int
*/
int get_played_size(void);
/**
* @brief Get the index of the last discarded card
*
* @return int
*/
int get_discard_top(void);
/**
* @brief Get the index of the card that is currently being scored
*
* @return int
*/
int get_scored_card_index(void);
/**
* @brief Set whether the current card should get triggered again
*
* @param new_retrigger true if the card should trigger again, false if not
*/
void set_retrigger(bool new_retrigger);
/**
* @brief Change to the round card selection background
*/
void game_round_change_background_selecting(void);
/**
* @brief Change to the round card playing background
*/
void game_round_change_background_playing(void);
/**
* @brief Round state initialization
*/
void game_round_on_init(void);
/**
* @brief Round state update
*/
void game_round_on_update(void);
#endif // GAME_ROUND_H
+2
View File
@@ -58,6 +58,8 @@ typedef struct
s32 hands;
s32 discards;
u32 score;
u32 chips;
u32 mult;
Sprite* playing_blind_token;
Sprite* round_end_blind_token;
+28 -1931
View File
File diff suppressed because it is too large Load Diff
+3 -3
View File
@@ -309,8 +309,8 @@ static void game_blind_select_display_blind_panel()
static void game_blind_select_exit(void)
{
reset_background();
game_change_state(GAME_STATE_PLAYING);
change_background(BG_NONE, false);
game_change_state(GAME_STATE_ROUND);
}
static Rect game_blind_select_get_req_score_rect(enum BlindTokens blind)
@@ -484,7 +484,7 @@ void game_blind_select_on_exit(void)
sprite_destroy(&blind_select_tokens[BIG_BLIND]);
sprite_destroy(&blind_select_tokens[BOSS_BLIND]);
reset_background();
change_background(BG_NONE, false);
selection_y = 0;
state_machine_remove(&blind_select_sm);
+8 -6
View File
@@ -4,6 +4,7 @@
#include "game.h"
#include "game/main_menu.h"
#include "game/options_menu.h"
#include "game/round.h"
#include "game/round_end.h"
#include "game/run_setup.h"
#include "game/shop.h"
@@ -15,8 +16,8 @@ static enum BackgroundId background = BG_NONE;
// Map to fill in for refactor
static const BackgroundRenderCallback bgCallbacks[] = {
[BG_NONE] = NULL,
[BG_CARD_SELECTING] = NULL,
[BG_CARD_PLAYING] = NULL,
[BG_CARD_SELECTING] = game_round_change_background_selecting,
[BG_CARD_PLAYING] = game_round_change_background_playing,
[BG_ROUND_END] = game_round_end_change_background,
[BG_SHOP] = game_shop_change_background,
[BG_BLIND_SELECT] = game_blind_select_change_background,
@@ -25,19 +26,20 @@ static const BackgroundRenderCallback bgCallbacks[] = {
[BG_MAIN_MENU] = game_main_menu_change_background,
};
enum BackgroundId get_current_background(void)
{
return background;
}
void change_background(enum BackgroundId id, bool force_redraw)
{
if (force_redraw)
{
background = BG_NONE;
reset_background();
}
if (id != background && bgCallbacks[id] != NULL)
{
bgCallbacks[id]();
}
background = id;
// Can be removed once all states have their own "change_background" func
change_background_legacy(id);
}
+2074
View File
File diff suppressed because it is too large Load Diff
+3 -32
View File
@@ -9,6 +9,7 @@
#include "audio_utils.h"
#include "card.h"
#include "game.h"
#include "game/round.h"
#include "game_variables.h"
#include "graphic_utils.h"
#include "soundbank.h"
@@ -150,8 +151,8 @@ void compute_hand_value_info(void)
HandValues hand_values = hand_base_values[hand.hand_type];
set_chips(hand_values.chips);
set_mult(hand_values.mult);
g_game_vars.chips = hand_values.chips;
g_game_vars.mult = hand_values.mult;
print_hand_type(hand_values.display_name);
display_chips();
@@ -356,36 +357,6 @@ static void get_hand_distribution(u8 ranks_out[NUM_RANKS], u8 suits_out[NUM_SUIT
}
}
/**
* @brief Outputs the distribution of ranks and suits in the played stack
* @param ranks_out output - updated such as ranks_out[rank] is the number of cards of rank in the
* played stack. Must be of size NUM_RANKS.
* @param suits_out output - updated such as suits_out[suit] is the number of cards if suit in the
* played stack. Must be of size NUM_SUITS
*/
GBAL_UNUSED
static void get_played_distribution(u8 ranks_out[NUM_RANKS], u8 suits_out[NUM_SUITS])
{
for (int i = 0; i < NUM_RANKS; i++)
ranks_out[i] = 0;
for (int i = 0; i < NUM_SUITS; i++)
suits_out[i] = 0;
CardObject** played = get_played_array();
int top = get_played_top();
for (int i = 0; i <= top; i++)
{
/* The difference from get_hand_distribution() (not checking if card is selected)
* is in line Balatro behavior,
* see https://github.com/GBALATRO/balatro-gba/issues/341#issuecomment-3691363488
*/
if (!played[i])
continue;
ranks_out[played[i]->card->rank]++;
suits_out[played[i]->card->suit]++;
}
}
// Returns the highest N of a kind. So a full-house would return 3.
static u8 hand_contains_n_of_a_kind(u8* ranks)
{
+5 -13
View File
@@ -1,6 +1,7 @@
#include "joker.h"
#include "card.h"
#include "game/round.h"
#include "game_variables.h"
#include "graphic_utils.h"
#include "pool.h"
@@ -291,10 +292,6 @@ bool joker_object_score(
return false;
}
u32 chips = get_chips();
u32 mult = get_mult();
int money = g_game_vars.money;
if (effect_flags_ret & JOKER_EFFECT_FLAG_RETRIGGER)
{
set_retrigger(joker_effect->retrigger);
@@ -320,7 +317,7 @@ bool joker_object_score(
mm_word sfx_id;
if (effect_flags_ret & JOKER_EFFECT_FLAG_CHIPS)
{
chips = u32_protected_add(chips, joker_effect->chips);
g_game_vars.chips = u32_protected_add(g_game_vars.chips, joker_effect->chips);
char score_buffer[INT_MAX_DIGITS + 2]; // For '+' and null terminator
snprintf(score_buffer, sizeof(score_buffer), "+%lu", joker_effect->chips);
set_and_shift_text(score_buffer, &cursorPosX, &cursorPosY, TTE_BLUE_PB);
@@ -328,7 +325,7 @@ bool joker_object_score(
}
if (effect_flags_ret & JOKER_EFFECT_FLAG_MULT)
{
mult = u32_protected_add(mult, joker_effect->mult);
g_game_vars.mult = u32_protected_add(g_game_vars.mult, joker_effect->mult);
char score_buffer[INT_MAX_DIGITS + 2];
snprintf(score_buffer, sizeof(score_buffer), "+%lu", joker_effect->mult);
set_and_shift_text(score_buffer, &cursorPosX, &cursorPosY, TTE_RED_PB);
@@ -337,7 +334,7 @@ bool joker_object_score(
// if xmult is zero, DO NOT multiply by it
if (effect_flags_ret & JOKER_EFFECT_FLAG_XMULT && joker_effect->xmult > 0)
{
mult = u32_protected_mult(mult, joker_effect->xmult);
g_game_vars.mult = u32_protected_mult(g_game_vars.mult, joker_effect->xmult);
char score_buffer[INT_MAX_DIGITS + 2];
snprintf(score_buffer, sizeof(score_buffer), "X%lu", joker_effect->xmult);
set_and_shift_text(score_buffer, &cursorPosX, &cursorPosY, TTE_RED_PB);
@@ -345,7 +342,7 @@ bool joker_object_score(
}
if (effect_flags_ret & JOKER_EFFECT_FLAG_MONEY)
{
money += joker_effect->money;
g_game_vars.money += joker_effect->money;
char score_buffer[INT_MAX_DIGITS + 2];
snprintf(score_buffer, sizeof(score_buffer), "%d$", joker_effect->money);
set_and_shift_text(score_buffer, &cursorPosX, &cursorPosY, TTE_YELLOW_PB);
@@ -364,11 +361,6 @@ bool joker_object_score(
list_push_back(get_expired_jokers_list(), joker_object);
}
// Update values
set_chips(chips);
set_mult(mult);
g_game_vars.money = money;
// Update displays
display_chips();
display_mult();
+2 -1
View File
@@ -1,4 +1,5 @@
#include "game.h"
#include "game/round.h"
#include "game_variables.h"
#include "hand.h"
#include "joker.h"
@@ -1034,7 +1035,7 @@ static u32 half_joker_effect(
u32 effect_flags_ret = JOKER_EFFECT_FLAG_NONE;
int played_size = get_played_top() + 1;
int played_size = get_played_size();
if (played_size <= 3)
{
*joker_effect = &shared_joker_effect;