Refactor/state machine (#470)

* initial additions

* Move current_blind to game vars

* tmp

* tmp

* tmp

* tmp

* tmp

* tmp

* tmp

* Compile but is broken

* temp fix for token location

* tmp

* Working, lets clean up

* remove todo

* Remove odd reroll blind variable

* fix skipping blind screen

* fix reroll

* remove magic for blind token locations

* clang format

* Cleanup button highlight in blind menu

* some cleanup

* Add layout file for global UI rects

* remove dead code

* Clang format

* clang format it up

* Move reset top left funct to gfx utils

* make note of soon-to-be removed vars

* Cleanup gamevars declaration

* Cleanup layout file

* Apply suggestions from code review

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Add include for stdbool.h in common_ui.h

* Cleanup rect declaration

* Start moving state machine stuff to new file

* quick tmp

* oops, add new files

* working call list

* fixed pointer stuff

* working as per norm

* working replacement

* move defines to static const

* fix compile issue

* Add some missing "void"s

* Working new substates

* add statemachine to blind select

* Fix game restart statemachine leak

* clang-format

* Clang format

* Rework shop exit logic

* Use new state machine in shop

* Remove substates entirely

* Remove debug stuff

* Clang format

* document state machine better

* update list tests

* clang-format

* Update for PR

* Claaaaang format

* another clang-format

* Remove extra state in round_end

* cleanup docs for removals in lists

* clang-format... again

* update doxygen format

* Clang format

* Update list docs

* Update lists tests

* Fix bad conflict resolution

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Rickey
2026-05-22 18:06:13 -07:00
committed by GitHub
co-authored by Copilot Autofix powered by AI
parent 2329c4c681
commit 92cb9f9883
11 changed files with 428 additions and 165 deletions
+31 -30
View File
@@ -5,6 +5,7 @@
#include "game.h"
#include "game_variables.h"
#include "layout.h"
#include "state_machine.h"
#include "timer.h"
#include "util.h"
@@ -19,7 +20,7 @@ enum GameRoundEndStates
DISPLAY_REWARDS,
DISPLAY_CASHOUT,
DISMISS_ROUND_END_PANEL,
ROUND_END_EXIT
ROUND_END_STATES_MAX
};
static const u32 TM_RESET_STATIC_VARS = 30;
@@ -46,7 +47,6 @@ static const Rect ROUND_END_MENU_RECT = {9, 7, 24, 20
static const BG_POINT CASHOUT_SRC_3X3_RECT_POS = {5, 29};
// clang-format on
static int substate;
static int blind_reward = 0;
static int hand_reward = 0;
static int interest_reward = 0;
@@ -67,16 +67,21 @@ static void game_round_end_dismiss_round_end_panel(void);
static void game_round_end_extend_black_panel_down(int black_panel_bottom);
static const SubStateActionFn round_end_state_actions[] = {
game_round_end_start,
game_round_end_start_expand_popup,
game_round_end_display_finished_blind,
game_round_end_display_score_min,
game_round_end_update_blind_reward,
game_round_end_panel_exit,
game_round_end_display_rewards,
game_round_end_display_cashout,
game_round_end_dismiss_round_end_panel
static StateInfo state_info[] = {
STATE_INFO_UPDATE_FN_ONLY(game_round_end_start),
STATE_INFO_UPDATE_FN_ONLY(game_round_end_start_expand_popup),
STATE_INFO_UPDATE_FN_ONLY(game_round_end_display_finished_blind),
STATE_INFO_UPDATE_FN_ONLY(game_round_end_display_score_min),
STATE_INFO_UPDATE_FN_ONLY(game_round_end_update_blind_reward),
STATE_INFO_UPDATE_FN_ONLY(game_round_end_panel_exit),
STATE_INFO_UPDATE_FN_ONLY(game_round_end_display_rewards),
STATE_INFO_UPDATE_FN_ONLY(game_round_end_display_cashout),
STATE_INFO_UPDATE_FN_ONLY(game_round_end_dismiss_round_end_panel),
};
static StateMachine round_end_sm = {
.state_infos = &state_info[0],
.num_infos = ROUND_END_STATES_MAX,
};
static int calculate_interest_reward(void)
@@ -93,8 +98,8 @@ static void game_round_end_start(void)
if (g_game_vars.timer == TM_RESET_STATIC_VARS)
{
change_background(BG_ROUND_END, false); // Change the background to the round end background
substate = START_EXPAND_POPUP; // Change the state to the next one
g_game_vars.timer = TM_ZERO; // Reset the timer
state_machine_change_state(&round_end_sm, START_EXPAND_POPUP);
g_game_vars.timer = TM_ZERO; // Reset the timer
blind_reward = blind_get_reward(g_game_vars.current_blind);
hand_reward = g_game_vars.hands;
interest_reward = calculate_interest_reward();
@@ -109,7 +114,7 @@ static void game_round_end_start_expand_popup(void)
if (g_game_vars.timer == TM_END_POP_MENU_ANIM)
{
substate = DISPLAY_FINISHED_BLIND;
state_machine_change_state(&round_end_sm, DISPLAY_FINISHED_BLIND);
g_game_vars.timer = TM_ZERO;
}
}
@@ -151,7 +156,7 @@ static void game_round_end_display_finished_blind(void)
if (g_game_vars.timer >= TM_END_DISPLAY_FIN_BLIND)
{
substate = DISPLAY_SCORE_MIN;
state_machine_change_state(&round_end_sm, DISPLAY_SCORE_MIN);
g_game_vars.timer = TM_ZERO;
}
}
@@ -172,7 +177,7 @@ static void game_round_end_display_score_min(void)
if (g_game_vars.timer >= TM_END_DISPLAY_SCORE_MIN)
{
substate = UPDATE_BLIND_REWARD;
state_machine_change_state(&round_end_sm, UPDATE_BLIND_REWARD);
g_game_vars.timer = TM_ZERO;
}
}
@@ -208,7 +213,7 @@ static void game_round_end_update_blind_reward(void)
tte_erase_rect_wrapper(BLIND_REQ_TEXT_RECT);
obj_hide(g_game_vars.playing_blind_token->obj);
affine_background_load_palette(affine_background_gfxPal);
substate = BLIND_PANEL_EXIT;
state_machine_change_state(&round_end_sm, BLIND_PANEL_EXIT);
g_game_vars.timer = TM_ZERO;
}
}
@@ -236,7 +241,7 @@ static void game_round_end_panel_exit(void)
else if (g_game_vars.timer > FRAMES(20))
{
memset16(&pal_bg_mem[REWARD_PANEL_BORDER_PID], 0x1483, 1);
substate = DISPLAY_REWARDS;
state_machine_change_state(&round_end_sm, DISPLAY_REWARDS);
g_game_vars.timer = TM_ZERO;
}
}
@@ -342,7 +347,7 @@ static void game_round_end_display_rewards(void)
if (hand_reward <= 0 && interest_to_count <= 0)
{
g_game_vars.timer = TM_ZERO;
substate = DISPLAY_CASHOUT;
state_machine_change_state(&round_end_sm, DISPLAY_CASHOUT);
}
else if (g_game_vars.timer == TM_START_ROUND_END_REWARDS_ANIM)
{
@@ -406,7 +411,7 @@ static void game_round_end_display_cashout()
{
game_round_end_cashout();
substate = DISMISS_ROUND_END_PANEL; // Go to the next state
state_machine_change_state(&round_end_sm, DISMISS_ROUND_END_PANEL);
g_game_vars.timer = TM_ZERO;
obj_hide(g_game_vars.round_end_blind_token->obj); // Hide the blind token object
@@ -423,7 +428,7 @@ static void game_round_end_dismiss_round_end_panel(void)
if (g_game_vars.timer >= TM_DISMISS_ROUND_END_TM)
{
g_game_vars.timer = TM_ZERO;
substate = ROUND_END_EXIT;
game_change_state(GAME_STATE_SHOP);
}
}
@@ -446,19 +451,14 @@ void game_round_end_change_background(void)
void game_round_end_on_init(void)
{
substate = ROUND_END_START;
g_game_vars.timer = 0;
state_machine_register(&round_end_sm);
state_machine_change_state(&round_end_sm, ROUND_END_START);
}
void game_round_end_on_update(void)
{
if (substate == ROUND_END_EXIT)
{
game_change_state(GAME_STATE_SHOP);
return;
}
round_end_state_actions[substate]();
// Substate logic only
}
void game_round_end_on_exit(void)
@@ -470,5 +470,6 @@ void game_round_end_on_exit(void)
interest_reward = 0;
sprite_destroy(&g_game_vars.playing_blind_token);
sprite_destroy(&g_game_vars.round_end_blind_token);
state_machine_remove(&round_end_sm);
// TODO: Reuse sprites for blind selection?
}