refactor: name the game->menu state machine - #7
Merged
Conversation
game->menu is a stack of screens encoded in base 10 (menu = menu * 10 + N pushes a screen, menu /= 10 pops). Every call site read or wrote that as raw decimal arithmetic (menu % 10 == 6, (menu / 10) % 10 != 6, menu * 10 + 6, ...), which was unreadable and error-prone. Introduce menu.h/menu.c: named state constants (MENU_START, MENU_GAME, MENU_WIN, MENU_PAUSE_*, ...), transition helpers (menu_push, menu_pop, menu_set_tab) and predicate helpers (menu_in_overworld, menu_pause_open, menu_tab_music, menu_won, ...). Each helper reproduces the original expression exactly, so behaviour is unchanged — this is a pure readability pass. The remaining bare comparisons are encoding thresholds inside already-named functions. Verified: build warning-free, full Banana scan still 0 findings, the criterion suite still passes 10/10.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
game->menuis a stack of menu screens encoded in base 10 —menu = menu * 10 + Npushes a screen,
menu /= 10pops. Every one of the ~80 call sites read or wrotethat as raw decimal arithmetic (
menu % 10 == 6,(menu / 10) % 10 != 6,menu * 10 + 6,menu <= 563, ...), which was unreadable and a classic source ofnavigation bugs.
What
New
menu.h/menu.c:MENU_START,MENU_GAME,MENU_WIN,MENU_LOSE,MENU_SETTINGS,MENU_PAUSE_INVENTORY/CHARACTER/QUEST/SETTINGS,SCREEN_*.menu_push,menu_pop,menu_set_tab.menu_is_start,menu_in_overworld,menu_show_hud,menu_pause_open,menu_pause_active,menu_in_pause_tab,menu_tab_inventory/character/quest,menu_in_settings,menu_settings_open,menu_tab_fps/music/keyboard/window,menu_won,menu_lost.Every helper reproduces the exact original expression, so this is a pure
readability pass with no behavioural change. A reverse-engineered map of the
state machine was used to do the translation faithfully.
The decoded state machine is documented in the PR discussion below for reviewers.
Scope
Naming only — the underlying base-10 encoding is preserved. A full re-encoding to
an explicit enum + stack is deliberately left as a follow-up, since it would change
the encoding and can only be validated by playing through the menus.
Verification
make re: warning-free.make tests_run: 10/10 passing.A couple of bare comparisons remain (
menu > 500,menu <= 563): these areencoding thresholds inside already-named transition functions and don't map onto a
clean predicate.