[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
+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;