diff --git a/graphics/link_games/berrycrush_background.bin b/graphics/berry_crush/bg.bin similarity index 100% rename from graphics/link_games/berrycrush_background.bin rename to graphics/berry_crush/bg.bin diff --git a/graphics/link_games/berrycrush_container_cap.bin b/graphics/berry_crush/container_cap.bin similarity index 100% rename from graphics/link_games/berrycrush_container_cap.bin rename to graphics/berry_crush/container_cap.bin diff --git a/graphics/berry_crusher/tiles.bin b/graphics/berry_crush/crusher.bin similarity index 100% rename from graphics/berry_crusher/tiles.bin rename to graphics/berry_crush/crusher.bin diff --git a/graphics/berry_crusher/tiles.pal b/graphics/berry_crush/crusher.pal similarity index 100% rename from graphics/berry_crusher/tiles.pal rename to graphics/berry_crush/crusher.pal diff --git a/graphics/berry_crusher/tiles.png b/graphics/berry_crush/crusher.png similarity index 100% rename from graphics/berry_crusher/tiles.png rename to graphics/berry_crush/crusher.png diff --git a/graphics/link_games/berrycrush_grinder_base.png b/graphics/berry_crush/crusher_base.png similarity index 100% rename from graphics/link_games/berrycrush_grinder_base.png rename to graphics/berry_crush/crusher_base.png diff --git a/graphics/link_games/berrycrush_grinder_top.bin b/graphics/berry_crush/crusher_top.bin similarity index 100% rename from graphics/link_games/berrycrush_grinder_top.bin rename to graphics/berry_crush/crusher_top.bin diff --git a/graphics/link_games/berrycrush_misc.pal b/graphics/berry_crush/effects.pal similarity index 100% rename from graphics/link_games/berrycrush_misc.pal rename to graphics/berry_crush/effects.pal diff --git a/graphics/link_games/berrycrush_btnpress.png b/graphics/berry_crush/impact.png similarity index 100% rename from graphics/link_games/berrycrush_btnpress.png rename to graphics/berry_crush/impact.png diff --git a/graphics/link_games/berrycrush_sparkle.png b/graphics/berry_crush/sparkle.png similarity index 100% rename from graphics/link_games/berrycrush_sparkle.png rename to graphics/berry_crush/sparkle.png diff --git a/graphics/link_games/berrycrush_timerdigits.png b/graphics/berry_crush/timer_digits.png similarity index 100% rename from graphics/link_games/berrycrush_timerdigits.png rename to graphics/berry_crush/timer_digits.png diff --git a/graphics/link_games/321start.png b/graphics/minigame_countdown/321start.png similarity index 100% rename from graphics/link_games/321start.png rename to graphics/minigame_countdown/321start.png diff --git a/graphics/link_games/321start_static.png b/graphics/minigame_countdown/321start_static.png similarity index 100% rename from graphics/link_games/321start_static.png rename to graphics/minigame_countdown/321start_static.png diff --git a/include/berry.h b/include/berry.h index 4a5a46615..22b9158ee 100644 --- a/include/berry.h +++ b/include/berry.h @@ -28,11 +28,11 @@ void SetBerryTreesSeen(void); extern const struct Berry gBerries[]; -struct UnkStruct_0858AB24 { - u8 unk0; - u16 unk1; +struct BerryCrushBerryData { + u8 difficulty; // The number of A presses required to crush it + u16 powder; }; -extern const struct UnkStruct_0858AB24 gUnknown_0858AB24[]; +extern const struct BerryCrushBerryData gBerryCrush_BerryData[]; #endif // GUARD_BERRY_H diff --git a/include/berry_crush.h b/include/berry_crush.h index a08f1f699..02dcce506 100755 --- a/include/berry_crush.h +++ b/include/berry_crush.h @@ -3,6 +3,6 @@ #include "main.h" -void StartBerryCrush(MainCallback callback); +void StartBerryCrush(MainCallback exitCallback); #endif // GUARD_BERRY_CRUSH_H diff --git a/include/constants/game_stat.h b/include/constants/game_stat.h index e302f1345..2acdfd8a3 100644 --- a/include/constants/game_stat.h +++ b/include/constants/game_stat.h @@ -52,7 +52,7 @@ #define GAME_STAT_RODE_CABLE_CAR 48 #define GAME_STAT_ENTERED_HOT_SPRINGS 49 #define GAME_STAT_NUM_UNION_ROOM_BATTLES 50 -#define GAME_STAT_51 51 +#define GAME_STAT_PLAYED_BERRY_CRUSH 51 #define NUM_USED_GAME_STATS 52 #define NUM_GAME_STATS 64 diff --git a/include/global.h b/include/global.h index 1992ff819..7daed8621 100644 --- a/include/global.h +++ b/include/global.h @@ -209,7 +209,7 @@ struct PyramidBag struct BerryCrush { - u16 berryCrushResults[4]; + u16 pressingSpeeds[4]; // For the record with each possible group size, 2-5 players u32 berryPowderAmount; u32 unk; }; diff --git a/include/graphics.h b/include/graphics.h index b1a781fff..9be05f38e 100644 --- a/include/graphics.h +++ b/include/graphics.h @@ -5001,9 +5001,9 @@ extern const u16 gUsePokeblockUpDown_Pal[]; extern const u16 gUsePokeblockCondition_Pal[]; // Berry Crush -extern const u32 gUnknown_08DE34B8[]; -extern const u16 gUnknown_08DE3398[]; -extern const u32 gUnknown_08DE3FD4[]; +extern const u32 gBerryCrush_Crusher_Gfx[]; +extern const u16 gBerryCrush_Crusher_Pal[]; +extern const u32 gBerryCrush_Crusher_Tilemap[]; // Pokenav extern const u32 gPokenavMessageBox_Gfx[]; diff --git a/include/strings.h b/include/strings.h index 294ab8aa6..8880f3a5c 100644 --- a/include/strings.h +++ b/include/strings.h @@ -2904,7 +2904,7 @@ extern const u8 gText_CrushingResults[]; extern const u8 gText_BerryCrush2[]; extern const u8 gText_PressingSpeedRankings[]; extern const u8 gText_Var1Players[]; -extern const u8 gText_ReadyToBerryCrush[]; +extern const u8 gText_ReadyPickBerry[]; extern const u8 gText_WaitForAllChooseBerry[]; extern const u8 gText_EndedWithXUnitsPowder[]; extern const u8 gText_RecordingGameResults[]; diff --git a/src/berry.c b/src/berry.c index ea62deabf..d56ec801c 100644 --- a/src/berry.c +++ b/src/berry.c @@ -889,50 +889,50 @@ const struct Berry gBerries[] = }, }; -const struct UnkStruct_0858AB24 gUnknown_0858AB24[] = { - { 50, 20}, - { 50, 20}, - { 50, 20}, - { 50, 20}, - { 50, 20}, - { 50, 30}, - { 50, 30}, - { 50, 30}, - { 50, 30}, - { 50, 30}, - { 60, 50}, - { 60, 50}, - { 60, 50}, - { 60, 50}, - { 60, 50}, - { 80, 70}, - { 80, 70}, - { 80, 70}, - { 80, 70}, - { 80, 70}, - {100, 100}, - {100, 100}, - {100, 100}, - {100, 100}, - {100, 100}, - {130, 150}, - {130, 150}, - {130, 150}, - {130, 150}, - {130, 150}, - {160, 250}, - {160, 250}, - {160, 250}, - {160, 250}, - {160, 250}, - {180, 500}, - {180, 500}, - {180, 500}, - {180, 500}, - {180, 500}, - {200, 750}, - {200, 750}, - {150, 200} +const struct BerryCrushBerryData gBerryCrush_BerryData[] = { + [ITEM_CHERI_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 20}, + [ITEM_CHESTO_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 20}, + [ITEM_PECHA_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 20}, + [ITEM_RAWST_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 20}, + [ITEM_ASPEAR_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 20}, + [ITEM_LEPPA_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 30}, + [ITEM_ORAN_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 30}, + [ITEM_PERSIM_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 30}, + [ITEM_LUM_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 30}, + [ITEM_SITRUS_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 50, .powder = 30}, + [ITEM_FIGY_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 60, .powder = 50}, + [ITEM_WIKI_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 60, .powder = 50}, + [ITEM_MAGO_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 60, .powder = 50}, + [ITEM_AGUAV_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 60, .powder = 50}, + [ITEM_IAPAPA_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 60, .powder = 50}, + [ITEM_RAZZ_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 80, .powder = 70}, + [ITEM_BLUK_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 80, .powder = 70}, + [ITEM_NANAB_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 80, .powder = 70}, + [ITEM_WEPEAR_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 80, .powder = 70}, + [ITEM_PINAP_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 80, .powder = 70}, + [ITEM_POMEG_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 100, .powder = 100}, + [ITEM_KELPSY_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 100, .powder = 100}, + [ITEM_QUALOT_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 100, .powder = 100}, + [ITEM_HONDEW_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 100, .powder = 100}, + [ITEM_GREPA_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 100, .powder = 100}, + [ITEM_TAMATO_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 130, .powder = 150}, + [ITEM_CORNN_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 130, .powder = 150}, + [ITEM_MAGOST_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 130, .powder = 150}, + [ITEM_RABUTA_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 130, .powder = 150}, + [ITEM_NOMEL_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 130, .powder = 150}, + [ITEM_SPELON_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 160, .powder = 250}, + [ITEM_PAMTRE_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 160, .powder = 250}, + [ITEM_WATMEL_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 160, .powder = 250}, + [ITEM_DURIN_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 160, .powder = 250}, + [ITEM_BELUE_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 160, .powder = 250}, + [ITEM_LIECHI_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 180, .powder = 500}, + [ITEM_GANLON_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 180, .powder = 500}, + [ITEM_SALAC_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 180, .powder = 500}, + [ITEM_PETAYA_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 180, .powder = 500}, + [ITEM_APICOT_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 180, .powder = 500}, + [ITEM_LANSAT_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 200, .powder = 750}, + [ITEM_STARF_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 200, .powder = 750}, + [ITEM_ENIGMA_BERRY - FIRST_BERRY_INDEX] = {.difficulty = 150, .powder = 200} }; const struct BerryTree gBlankBerryTree = {}; diff --git a/src/berry_crush.c b/src/berry_crush.c index 635f2a639..e63eb9f82 100755 --- a/src/berry_crush.c +++ b/src/berry_crush.c @@ -38,204 +38,352 @@ #include "constants/rgb.h" #include "constants/songs.h" +#define MAX_TIME (10 * 60 * 60) // Timer can go up to 9:59:59 + +#define TAG_CRUSHER_BASE 1 +#define PALTAG_EFFECT 2 // The next two gfx tags share this pal tag +#define GFXTAG_IMPACT 2 +#define GFXTAG_SPARKLE 3 +#define TAG_TIMER_DIGITS 4 +#define TAG_PLAYER1_BERRY 5 +#define TAG_PLAYER2_BERRY 6 +#define TAG_PLAYER3_BERRY 7 +#define TAG_PLAYER4_BERRY 8 +#define TAG_PLAYER5_BERRY 9 + #define TAG_COUNTDOWN 0x1000 +#define CRUSHER_START_Y (-104) + +enum { + RUN_CMD, + SCHEDULE_CMD, +}; + +// IDs for the main berry crush game functions +enum { + CMD_NONE, + CMD_FADE, + CMD_WAIT_FADE, + CMD_PRINT_MSG, + CMD_SHOW_GAME, + CMD_HIDE_GAME, + CMD_READY_BEGIN, + CMD_ASK_PICK_BERRY, + CMD_PICK_BERRY, + CMD_WAIT_BERRIES, + CMD_DROP_BERRIES, + CMD_DROP_LID, + CMD_COUNTDOWN, + CMD_PLAY_GAME_LEADER, + CMD_PLAY_GAME_MEMBER, + CMD_FINISH_GAME, + CMD_TIMES_UP, + CMD_CALC_RESULTS, + CMD_SHOW_RESULTS, + CMD_SAVE, + CMD_ASK_PLAY_AGAIN, + CMD_COMM_PLAY_AGAIN, + CMD_PLAY_AGAIN_YES, + CMD_PLAY_AGAIN_NO, + CMD_CLOSE_LINK, + CMD_QUIT, +}; + +enum { + MSG_PICK_BERRY, + MSG_WAIT_PICK, + MSG_POWDER, + MSG_SAVING, + MSG_PLAY_AGAIN, + MSG_NO_BERRIES, + MSG_DROPPED, + MSG_TIMES_UP, + MSG_COMM_STANDBY, +}; + +#define F_MSG_CLEAR (1 << 0) +#define F_MSG_EXPAND (1 << 1) + +// Main states for the game. Many are assigned but never checked +enum { + STATE_INIT = 1, + STATE_RESET, + STATE_PICK_BERRY, + STATE_DROP_BERRIES, + STATE_DROP_LID, + STATE_COUNTDOWN, + STATE_PLAYING, + STATE_FINISHED, + STATE_TIMES_UP, + STATE_10, // Unused + STATE_RESULTS_PRESSES, + STATE_RESULTS_RANDOM, + STATE_RESULTS_CRUSHING, + STATE_14, // Unused + STATE_PLAY_AGAIN, +}; + +#define RESULTS_STATE_START STATE_RESULTS_PRESSES +#define RESULTS_STATE_END STATE_RESULTS_CRUSHING + +// IDs for each results page that shows in succession at the game's end. +// Only 3 pages are shown for a given game. Presses and Crushing are always shown 1st and 3rd. +// The 2nd page is random, and can be rankings for either Neatness, Cooperative, or Power. +enum { + RESULTS_PAGE_PRESSES, + RESULTS_PAGE_RANDOM, + RESULTS_PAGE_CRUSHING, + NUM_RESULTS_PAGES, +}; +// Random pages, see above +// "Neatness" is how many of the player's inputs were at a regular interval +// "Cooperative" is how often the player pressed A at the same time as others +// "Power" is how much of the time the player spent pressing A +enum { + RESULTS_PAGE_NEATNESS, + RESULTS_PAGE_COOPERATIVE, + RESULTS_PAGE_POWER, + NUM_RANDOM_RESULTS_PAGES +}; + +#define PLAY_AGAIN_YES 0 +#define PLAY_AGAIN_NO 1 +#define PLAY_AGAIN_NO_BERRIES 3 + +enum { + COLORID_GREY, + COLORID_BLACK, + COLORID_LIGHT_GREY, + COLORID_BLUE, + COLORID_GREEN, + COLORID_RED, +}; + +// Flags for the inputFlags field +// Field is 16 bits; 3 bits for each player, last bit is unused +// The first two bits are interchangeable +// Needlessly complicated system, the inputState field is sufficient by itself +#define F_INPUT_HIT_A (1 << 0) +#define F_INPUT_HIT_B (1 << 1) +#define F_INPUT_HIT_SYNC (1 << 2) // Input at same time as another player +#define INPUT_FLAGS_PER_PLAYER 3 +#define INPUT_FLAG_MASK ((1 << INPUT_FLAGS_PER_PLAYER) - 1) + +// Values for the inputState field +enum { + INPUT_STATE_NONE, + INPUT_STATE_HIT, // Hit the crusher + INPUT_STATE_HIT_SYNC, // Hit the crusher at same time as another player +}; + +// No reason for this to be 2 +// Simply a flag for whether a given player has sent their data this round +// Data is only sent if the player is the leader or if they pressed A +#define SEND_GAME_STATE 2 + struct BerryCrushGame_Player { - u8 unk0[PLAYER_NAME_LENGTH + 1 + 4]; - u16 unkC; - u16 unkE; - u16 unk10; - u16 unk12; - u16 unk14; - u16 unk16; - u16 unk18; - u16 unk1A; - u8 unk1B; - u8 unk1C; + u8 name[PLAYER_NAME_LENGTH + 1 + 4]; + u16 berryId; + u16 inputTime; + u16 neatInputStreak; + u16 timeSincePrevInput; + u16 maxNeatInputStreak; + u16 numAPresses; + u16 numSyncedAPresses; + u16 timePressingA; + u8 inputFlags; + u8 inputState; }; -struct BerryCrushGame_4E +// This data is set locally and sent to the other players +struct BerryCrushGame_LocalState { - u16 unk0; - u16 unk2; - u8 unk4_0:1; - u8 unk4_1:1; - u8 unk4_2:1; - u8 unk4_3:5; - s8 unk5; - u16 unk6; - u16 unk8; - u16 unkA; - u16 unkC; + u16 sendFlag; + bool8 endGame:1; + bool8 bigSparkle:1; + bool8 pushedAButton:1; + u8 playerPressedAFlags:5; // 1 bit for each player + s8 vibration; + u16 depth; + u16 timer; + u16 inputFlags; + u16 sparkleAmount; }; -struct BerryCrushGame_40 +// This data is read from another player's local state above by casting the recvCmd buffer +// It is identical with exception to the addition of rfuCmd +struct BerryCrushGame_LinkState { - s16 unk0; - s16 unk2; - s16 unk4; - s16 unk6; - s16 unk8; - s16 unkA; - s16 unkC; - s16 unkE; + u16 rfuCmd; + u16 sendFlag; + bool8 endGame:1; + bool8 bigSparkle:1; + bool8 pushedAButton:1; + u8 playerPressedAFlags:5; + s8 vibration; + u16 depth; + u16 timer; + u16 inputFlags; + u16 sparkleAmount; }; -struct BerryCrushGame_5C +struct BerryCrushGame_Results { - u16 unk00; - u8 unk02_0:1; - u8 unk02_1:1; - u8 pushedAButton:1; - u8 unk02_3:5; - s8 unk03; - u16 unk04; - u16 unk06; - u16 unk08; - u16 unk0A; + u32 powder; + u16 time; + u16 targetPressesPerSec; // Never read + u16 silkiness; + u16 totalAPresses; + u16 stats[2][MAX_RFU_PLAYERS]; + u8 playerIdsRanked[2][MAX_RFU_PLAYERS + 3]; }; -struct BerryCrushGame_68 +// playerIdsRanked above has 3 additional elements after the players. +// Only 1 of these 2*3 is ever used, and it stores the id for which +// random results page to show. Its define below is for readability. +#define randomPageId playerIdsRanked[0][7] + +// Holds position data for various player-associated graphics +struct BerryCrushPlayerCoords { - u32 unk00; - u16 unk04; - u16 unk06; - u16 unk08; - u16 unk0A; - // 0: Number of A presses - // 1: Neatness - u16 stats[2][5]; - u8 unk20[2][8]; + u8 playerId; + u8 windowGfxX; + u8 windowGfxY; + s16 impactXOffset; + s16 impactYOffset; + s16 berryXOffset; + s16 berryXDest; }; -struct BerryCrushPlayerSeatCoords +struct BerryCrushGame_Gfx { - u8 unk0; - u8 unk1; - u8 unk2; - s16 unk4; - s16 unk6; - s16 unk8; - s16 unkA; -}; - -struct BerryCrushGame_138 -{ - u8 animBerryIdx; - u8 unk1; - u8 unk2; - u8 unk3; + u8 counter; + u8 vibrationIdx; + u8 numVibrations; + bool8 vibrating; s16 minutes; s16 secondsInt; s16 secondsFrac; - const struct BerryCrushPlayerSeatCoords *seatCoords[5]; + const struct BerryCrushPlayerCoords *playerCoords[MAX_RFU_PLAYERS]; struct Sprite *coreSprite; - struct Sprite *impactSprites[5]; - struct Sprite *berrySprites[5]; + struct Sprite *impactSprites[MAX_RFU_PLAYERS]; + struct Sprite *berrySprites[MAX_RFU_PLAYERS]; struct Sprite *sparkleSprites[11]; struct Sprite *timerSprites[2]; - u8 unk80; - u8 filler81; - u8 unk82; - u8 unk83[5]; + u8 resultsState; + u8 unused; + u8 resultsWindowId; + u8 nameWindowIds[MAX_RFU_PLAYERS]; u16 bgBuffers[4][0x800]; }; struct BerryCrushGame { - MainCallback savedCallback; + MainCallback exitCallback; u32 (*cmdCallback)(struct BerryCrushGame *, u8 *); u8 localId; u8 playerCount; - u8 mainTask; + u8 taskId; u8 textSpeed; u8 cmdState; - u8 unkD; + u8 unused; // Never read u8 nextCmd; u8 afterPalFadeCmd; - u16 unk10; + u16 cmdTimer; u16 gameState; - u16 unk14; + u16 playAgainState; u16 pressingSpeed; - s16 unk18; - s16 unk1A; + s16 targetAPresses; + s16 totalAPresses; s32 powder; - s32 unk20; - u8 unk24; - u8 unk25_0:1; - u8 unk25_1:1; - u8 unk25_2:1; - u8 unk25_3:1; - u8 unk25_4:1; - u8 unk25_5:3; - u16 unk26; + s32 targetDepth; + u8 newDepth; + bool8 noRoomForPowder:1; // Never read + bool8 newRecord:1; + bool8 playedSound:1; + bool8 endGame:1; + bool8 bigSparkle:1; + u8 sparkleAmount:3; + u16 leaderTimer; u16 timer; s16 depth; s16 vibration; - s16 unk2E; - s16 unk30; - s16 unk32; - s16 unk34; - u8 commandParams[0xC]; + s16 bigSparkleCounter; + s16 numBigSparkles; + s16 numBigSparkleChecks; + s16 sparkleCounter; + u8 commandArgs[12]; u16 sendCmd[6]; u16 recvCmd[7]; - struct BerryCrushGame_5C localState; - struct BerryCrushGame_68 unk68; - struct BerryCrushGame_Player unk98[5]; - struct BerryCrushGame_138 unk138; + struct BerryCrushGame_LocalState localState; + struct BerryCrushGame_Results results; + struct BerryCrushGame_Player players[MAX_RFU_PLAYERS]; + struct BerryCrushGame_Gfx gfx; }; static void VBlankCB(void); static void MainCB(void); static void MainTask(u8); -static void ParseName_Options(struct BerryCrushGame *); -static void BerryCrush_RunOrScheduleCommand(u16, u8, u8 *); -static void BerryCrush_SetPaletteFadeParams(u8 *, bool8, u32, s8, u8, u8, u16); -static s32 sub_8021450(struct BerryCrushGame *); -static void sub_8022588(struct BerryCrushGame *); -static void sub_8022600(struct BerryCrushGame *); -static void sub_80226D0(struct BerryCrushGame *); -static void sub_8022730(struct BerryCrushGame *); -static void sub_8022960(struct BerryCrushGame *); -static void BerryCrush_PrintTimeOnSprites(struct BerryCrushGame_138 *, u16); -static void sub_8022B28(struct Sprite *); -static void BerryCrush_HideTimerSprites(struct BerryCrushGame_138 *r0); -static void sub_8024578(struct BerryCrushGame *); -static void BerryCrush_SetShowMessageParams(u8 *params, u8 stringId, u8 flags, u16 waitKeys, u8 followupCmd); -static void SpriteCB_BerryCrushImpact(struct Sprite *sprite); -static u32 BerryCrushCommand_BeginNormalPaletteFade(struct BerryCrushGame *r6, u8 *r1); -static u32 BerryCrushCommand_WaitPaletteFade(struct BerryCrushGame *r4, u8 *r5); -static u32 BerryCrushCommand_PrintMessage(struct BerryCrushGame *r7, u8 *r5); -static u32 BerryCrushCommand_InitGfx(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_TeardownGfx(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_SignalReadyToBegin(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_AskPickBerry(struct BerryCrushGame *r4, u8 *r5); -static u32 BerryCrushCommand_GoToBerryPouch(struct BerryCrushGame *r0, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_WaitForOthersToPickBerries(struct BerryCrushGame *r5, u8 *r2); -static u32 BerryCrushCommand_DropBerriesIntoCrusher(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_DropLid(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_Countdown(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_PlayGame_Master(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_PlayGame_Slave(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_FinishGame(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_HandleTimeUp(struct BerryCrushGame *r5, u8 *r6); -static u32 BerryCrushCommand_TabulateResults(struct BerryCrushGame *r7, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_ShowResults(struct BerryCrushGame *r5, u8 *r6); -static u32 BerryCrushCommand_SaveGame(struct BerryCrushGame *r5, u8 *r4); -static u32 BerryCrushCommand_AskPlayAgain(struct BerryCrushGame *r5, u8 *r6); -static u32 BerryCrushCommand_CommunicatePlayAgainResponses(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_FadeOutToPlayAgain(struct BerryCrushGame *r5, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_PlayAgainFailureMessage(struct BerryCrushGame *r5, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_GracefulExit(struct BerryCrushGame *r5, __attribute__((unused)) u8 *r1); -static u32 BerryCrushCommand_Quit(__attribute__((unused)) struct BerryCrushGame *r0, __attribute__((unused)) u8 *r1); +static void SetNamesAndTextSpeed(struct BerryCrushGame *); +static void RunOrScheduleCommand(u16, u8, u8 *); +static void SetPaletteFadeArgs(u8 *, bool8, u32, s8, u8, u8, u16); +static s32 UpdateGame(struct BerryCrushGame *); +static void CreatePlayerNameWindows(struct BerryCrushGame *); +static void DrawPlayerNameWindows(struct BerryCrushGame *); +static void CopyPlayerNameWindowGfxToBg(struct BerryCrushGame *); +static void CreateGameSprites(struct BerryCrushGame *); +static void DestroyGameSprites(struct BerryCrushGame *); +static void PrintTimer(struct BerryCrushGame_Gfx *, u16); +static void SpriteCB_Sparkle_Init(struct Sprite *); +static void HideTimer(struct BerryCrushGame_Gfx *); +static void ResetGame(struct BerryCrushGame *); +static void SetPrintMessageArgs(u8 *, u8, u8, u16, u8); +static void SpriteCB_Impact(struct Sprite *); +static u32 Cmd_BeginNormalPaletteFade(struct BerryCrushGame *, u8 *); +static u32 Cmd_WaitPaletteFade(struct BerryCrushGame *, u8 *); +static u32 Cmd_PrintMessage(struct BerryCrushGame *, u8 *); +static u32 Cmd_ShowGameDisplay(struct BerryCrushGame *, u8 *); +static u32 Cmd_HideGameDisplay(struct BerryCrushGame *, u8 *); +static u32 Cmd_SignalReadyToBegin(struct BerryCrushGame *, u8 *); +static u32 Cmd_AskPickBerry(struct BerryCrushGame *, u8 *); +static u32 Cmd_GoToBerryPouch(struct BerryCrushGame *, u8 *); +static u32 Cmd_WaitForOthersToPickBerries(struct BerryCrushGame *, u8 *); +static u32 Cmd_DropBerriesIntoCrusher(struct BerryCrushGame *, u8 *); +static u32 Cmd_DropLid(struct BerryCrushGame *, u8 *); +static u32 Cmd_Countdown(struct BerryCrushGame *, u8 *); +static u32 Cmd_PlayGame_Leader(struct BerryCrushGame *, u8 *); +static u32 Cmd_PlayGame_Member(struct BerryCrushGame *, u8 *); +static u32 Cmd_FinishGame(struct BerryCrushGame *, u8 *); +static u32 Cmd_HandleTimeUp(struct BerryCrushGame *, u8 *); +static u32 Cmd_TabulateResults(struct BerryCrushGame *, u8 *); +static u32 Cmd_ShowResults(struct BerryCrushGame *, u8 *); +static u32 Cmd_SaveGame(struct BerryCrushGame *, u8 *); +static u32 Cmd_AskPlayAgain(struct BerryCrushGame *, u8 *); +static u32 Cmd_CommunicatePlayAgainResponses(struct BerryCrushGame *, u8 *); +static u32 Cmd_PlayAgain(struct BerryCrushGame *, u8 *); +static u32 Cmd_StopGame(struct BerryCrushGame *, u8 *); +static u32 Cmd_CloseLink(struct BerryCrushGame *, u8 *); +static u32 Cmd_Quit(struct BerryCrushGame *, u8 *); -static EWRAM_DATA struct BerryCrushGame *sBerryCrushGamePtr = NULL; +static EWRAM_DATA struct BerryCrushGame *sGame = NULL; -static const u8 gUnknown_082F325C[] = { 1, 2, 4, 8, 16, 32, 64, 128 }; -static const u8 gUnknown_082F3264[] = { 0, 1, 2, 3, 5, 0, 0, 0 }; - -static const s8 gUnknown_082F326C[][7] = +static const u8 sBitTable[] = { + 1 << 0, + 1 << 1, + 1 << 2, + 1 << 3, + 1 << 4, + 1 << 5, + 1 << 6, + 1 << 7 +}; +// Additional A presses are counted depending on the number of players +// The bonus of 5 is unobtainable +static const u8 sSyncPressBonus[MAX_RFU_PLAYERS] = { 0, 1, 2, 3, 5 }; +ALIGNED(4) +static const s8 sIntroOutroVibrationData[][7] = { { 4, 1, 0, -1, 0, 0, 0}, { 4, 2, 0, -1, 0, 0, 0}, @@ -244,9 +392,8 @@ static const s8 gUnknown_082F326C[][7] = { 6, 4, 1, -2, -4, -2, 0}, }; -static const u8 sUnusedZero = 0; - -static const u8 gUnknown_082F3290[][4] = +ALIGNED(4) +static const u8 sVibrationData[MAX_RFU_PLAYERS][4] = { {3, 2, 1, 0}, {3, 3, 1, 0}, @@ -255,20 +402,20 @@ static const u8 gUnknown_082F3290[][4] = {3, 5, 3, 0}, }; -static const u8 *const sBerryCrushMessages[] = +static const u8 *const sMessages[] = { - gText_ReadyToBerryCrush, - gText_WaitForAllChooseBerry, - gText_EndedWithXUnitsPowder, - gText_RecordingGameResults, - gText_PlayBerryCrushAgain, - gText_YouHaveNoBerries, - gText_MemberDroppedOut, - gText_TimesUpNoGoodPowder, - gText_CommunicationStandby2, + [MSG_PICK_BERRY] = gText_ReadyPickBerry, + [MSG_WAIT_PICK] = gText_WaitForAllChooseBerry, + [MSG_POWDER] = gText_EndedWithXUnitsPowder, + [MSG_SAVING] = gText_RecordingGameResults, + [MSG_PLAY_AGAIN] = gText_PlayBerryCrushAgain, + [MSG_NO_BERRIES] = gText_YouHaveNoBerries, + [MSG_DROPPED] = gText_MemberDroppedOut, + [MSG_TIMES_UP] = gText_TimesUpNoGoodPowder, + [MSG_COMM_STANDBY] = gText_CommunicationStandby2, }; -static const struct BgTemplate gUnknown_082F32C8[4] = +static const struct BgTemplate sBgTemplates[4] = { { .bg = 0, @@ -308,19 +455,18 @@ static const struct BgTemplate gUnknown_082F32C8[4] = }, }; - -static const u8 sBerryCrushTextColorTable[][3] = +static const u8 sTextColorTable[][3] = { - {TEXT_COLOR_WHITE, TEXT_COLOR_DARK_GREY, TEXT_COLOR_LIGHT_GREY}, - {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_WHITE, TEXT_COLOR_DARK_GREY}, - {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_LIGHT_GREY, TEXT_COLOR_RED}, - {TEXT_COLOR_WHITE, TEXT_COLOR_BLUE, TEXT_COLOR_LIGHT_BLUE}, - {TEXT_COLOR_WHITE, TEXT_COLOR_GREEN, TEXT_COLOR_LIGHT_GREEN}, - {TEXT_COLOR_WHITE, TEXT_COLOR_RED, TEXT_COLOR_LIGHT_RED}, + [COLORID_GREY] = {TEXT_COLOR_WHITE, TEXT_COLOR_DARK_GREY, TEXT_COLOR_LIGHT_GREY}, + [COLORID_BLACK] = {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_WHITE, TEXT_COLOR_DARK_GREY}, + [COLORID_LIGHT_GREY] = {TEXT_COLOR_TRANSPARENT, TEXT_COLOR_LIGHT_GREY, TEXT_COLOR_RED}, + [COLORID_BLUE] = {TEXT_COLOR_WHITE, TEXT_COLOR_BLUE, TEXT_COLOR_LIGHT_BLUE}, + [COLORID_GREEN] = {TEXT_COLOR_WHITE, TEXT_COLOR_GREEN, TEXT_COLOR_LIGHT_GREEN}, + [COLORID_RED] = {TEXT_COLOR_WHITE, TEXT_COLOR_RED, TEXT_COLOR_LIGHT_RED}, }; -static const struct WindowTemplate sWindowTemplate_BerryCrushRankings = +static const struct WindowTemplate sWindowTemplate_Rankings = { .bg = 0, .tilemapLeft = 3, @@ -331,7 +477,7 @@ static const struct WindowTemplate sWindowTemplate_BerryCrushRankings = .baseBlock = 1 }; -static const struct WindowTemplate gUnknown_082F32F4[] = +static const struct WindowTemplate sWindowTemplates_PlayerNames[MAX_RFU_PLAYERS + 1] = { { .bg = 0, @@ -381,9 +527,9 @@ static const struct WindowTemplate gUnknown_082F32F4[] = DUMMY_WIN_TEMPLATE, }; -static const struct WindowTemplate gUnknown_082F3324[] = +static const struct WindowTemplate sWindowTemplates_Results[] = { - { + [STATE_RESULTS_PRESSES - RESULTS_STATE_START] = { .bg = 0, .tilemapLeft = 5, .tilemapTop = 2, @@ -392,7 +538,7 @@ static const struct WindowTemplate gUnknown_082F3324[] = .paletteNum = 15, .baseBlock = 1 }, - { + [STATE_RESULTS_RANDOM - RESULTS_STATE_START] = { .bg = 0, .tilemapLeft = 5, .tilemapTop = 2, @@ -401,7 +547,7 @@ static const struct WindowTemplate gUnknown_082F3324[] = .paletteNum = 15, .baseBlock = 1 }, - { + [STATE_RESULTS_CRUSHING - RESULTS_STATE_START] = { .bg = 0, .tilemapLeft = 4, .tilemapTop = 2, @@ -413,16 +559,16 @@ static const struct WindowTemplate gUnknown_082F3324[] = DUMMY_WIN_TEMPLATE, }; -static const u8 gUnknown_082F3344[][4] = +// The height of the results window depending on the number of players +// 2 players, 3 players, 4 players, or 5 players +static const u8 sResultsWindowHeights[][MAX_RFU_PLAYERS - 1] = { - {6, 8, 9, 11}, - {12, 14, 15, 16}, + {6, 8, 9, 11}, // "Presses" and "Neatness/Cooperative/Power" pages + {12, 14, 15, 16}, // "Crushing" page }; static const u32 sPressingSpeedConversionTable[] = { - // Decimal point is vertically aligned with the pixel - // directly between the >< below. 50000000, // 50 25000000, // 25 12500000, // 12.5 @@ -433,83 +579,85 @@ static const u32 sPressingSpeedConversionTable[] = 390625 // 0.390625 }; -static const u16 gBerryCrushGrinderBasePal[] = INCBIN_U16("graphics/link_games/berrycrush_grinder_base.gbapal"); -static const u16 gBerryCrushMiscSpritesPal[] = INCBIN_U16("graphics/link_games/berrycrush_misc.gbapal"); -static const u16 gBerryCrushTimerDigitsPal[] = INCBIN_U16("graphics/link_games/berrycrush_timerdigits.gbapal"); -static const u32 gBerryCrushGrinderBaseGfx[] = INCBIN_U32("graphics/link_games/berrycrush_grinder_base.4bpp.lz"); -static const u32 gBerryCrushBtnPressGfx[] = INCBIN_U32("graphics/link_games/berrycrush_btnpress.4bpp.lz"); -static const u32 gBerryCrushSparkleGfx[] = INCBIN_U32("graphics/link_games/berrycrush_sparkle.4bpp.lz"); -static const u8 gBerryCrushTimerDigitsGfx[] = INCBIN_U8("graphics/link_games/berrycrush_timerdigits.4bpp.lz"); -static const u8 gBerryCrushGrinderTopTilemap[] = INCBIN_U8("graphics/link_games/berrycrush_grinder_top.bin.lz"); -static const u8 gBerryCrushContainerCapTilemap[] = INCBIN_U8("graphics/link_games/berrycrush_container_cap.bin.lz"); -static const u8 gBerryCrushBackgroundTilemap[] = INCBIN_U8("graphics/link_games/berrycrush_background.bin.lz"); +static const u16 sCrusherBase_Pal[] = INCBIN_U16("graphics/berry_crush/crusher_base.gbapal"); +static const u16 sEffects_Pal[] = INCBIN_U16("graphics/berry_crush/effects.gbapal"); +static const u16 sTimerDigits_Pal[] = INCBIN_U16("graphics/berry_crush/timer_digits.gbapal"); +static const u32 sCrusherBase_Gfx[] = INCBIN_U32("graphics/berry_crush/crusher_base.4bpp.lz"); +static const u32 sImpact_Gfx[] = INCBIN_U32("graphics/berry_crush/impact.4bpp.lz"); +static const u32 sSparkle_Gfx[] = INCBIN_U32("graphics/berry_crush/sparkle.4bpp.lz"); +static const u32 sTimerDigits_Gfx[] = INCBIN_U32("graphics/berry_crush/timer_digits.4bpp.lz"); +static const u8 sCrusherTop_Tilemap[] = INCBIN_U8("graphics/berry_crush/crusher_top.bin.lz"); +static const u8 sContainerCap_Tilemap[] = INCBIN_U8("graphics/berry_crush/container_cap.bin.lz"); +static const u8 sBg_Tilemap[] = INCBIN_U8("graphics/berry_crush/bg.bin.lz"); -static const u8 gUnknown_082F417C[][5] = +// Takes the number of players - 2 and a player id and returns the +// index into sPlayerCoords where that player should be seated +static const u8 sPlayerIdToPosId[MAX_RFU_PLAYERS - 1][MAX_RFU_PLAYERS] = { - {1, 3, 0, 0, 0}, - {0, 1, 3, 0, 0}, - {1, 3, 2, 4, 0}, + {1, 3}, + {0, 1, 3}, + {1, 3, 2, 4}, {0, 1, 3, 2, 4}, }; -static const struct BerryCrushPlayerSeatCoords gUnknown_082F4190[] = +static const struct BerryCrushPlayerCoords sPlayerCoords[MAX_RFU_PLAYERS] = { { - .unk0 = 0, - .unk1 = 0, - .unk2 = 0, - .unk4 = 0, - .unk6 = -16, - .unk8 = 0, - .unkA = 0, + .playerId = 0, + .windowGfxX = 0, + .windowGfxY = 0, + .impactXOffset = 0, + .impactYOffset = -16, + .berryXOffset = 0, + .berryXDest = 0, }, { - .unk0 = 1, - .unk1 = 0, - .unk2 = 3, - .unk4 = -28, - .unk6 = -4, - .unk8 = -24, - .unkA = 16, + .playerId = 1, + .windowGfxX = 0, + .windowGfxY = 3, + .impactXOffset = -28, + .impactYOffset = -4, + .berryXOffset = -24, + .berryXDest = 16, }, { - .unk0 = 2, - .unk1 = 0, - .unk2 = 6, - .unk4 = -16, - .unk6 = 20, - .unk8 = -8, - .unkA = 16, + .playerId = 2, + .windowGfxX = 0, + .windowGfxY = 6, + .impactXOffset = -16, + .impactYOffset = 20, + .berryXOffset = -8, + .berryXDest = 16, }, { - .unk0 = 3, - .unk1 = 20, - .unk2 = 3, - .unk4 = 28, - .unk6 = -4, - .unk8 = 32, - .unkA = -8, + .playerId = 3, + .windowGfxX = 20, + .windowGfxY = 3, + .impactXOffset = 28, + .impactYOffset = -4, + .berryXOffset = 32, + .berryXDest = -8, }, { - .unk0 = 4, - .unk1 = 20, - .unk2 = 6, - .unk4 = 16, - .unk6 = 20, - .unk8 = 16, - .unkA = -8, + .playerId = 4, + .windowGfxX = 20, + .windowGfxY = 6, + .impactXOffset = 16, + .impactYOffset = 20, + .berryXOffset = 16, + .berryXDest = -8, } }; -static const s8 gUnknown_082F41CC[][2] = +static const s8 sImpactCoords[][2] = { { 0, 0}, {-1, 0}, { 1, 1}, }; -static const s8 gUnknown_082F41D2[][2] = +static const s8 sSparkleCoords[][2] = { { 0, 0}, {-16, -4}, @@ -524,37 +672,42 @@ static const s8 gUnknown_082F41D2[][2] = { 40, -16}, }; -static const u16 sPlayerBerrySpriteTags[] = {5, 6, 7, 8, 9, 0}; - -static const struct CompressedSpriteSheet gUnknown_082F41F4[] = +static const u16 sPlayerBerrySpriteTags[MAX_RFU_PLAYERS] = { - { .data = gBerryCrushGrinderBaseGfx, .size = 0x800, .tag = 1 }, - { .data = gBerryCrushBtnPressGfx, .size = 0xE00, .tag = 2 }, - { .data = gBerryCrushSparkleGfx, .size = 0x700, .tag = 3 }, + TAG_PLAYER1_BERRY, + TAG_PLAYER2_BERRY, + TAG_PLAYER3_BERRY, + TAG_PLAYER4_BERRY, + TAG_PLAYER5_BERRY }; -static const struct SpriteSheet gUnknown_082F420C[] = +// sTimerDigits_Gfx is part of this array but is (apparently) uncompressed +// It gets cast to raw uncompressed data when used in sDigitObjTemplates +static const struct CompressedSpriteSheet sSpriteSheets[] = { - { .data = gBerryCrushTimerDigitsGfx, .size = 0x2C0, .tag = 4 }, + { .data = sCrusherBase_Gfx, .size = 0x800, .tag = TAG_CRUSHER_BASE }, + { .data = sImpact_Gfx, .size = 0xE00, .tag = GFXTAG_IMPACT }, + { .data = sSparkle_Gfx, .size = 0x700, .tag = GFXTAG_SPARKLE }, + { .data = sTimerDigits_Gfx, .size = 0x2C0, .tag = TAG_TIMER_DIGITS }, {} }; static const struct SpritePalette sSpritePals[] = { - { .data = gBerryCrushGrinderBasePal, .tag = 1 }, - { .data = gBerryCrushMiscSpritesPal, .tag = 2 }, - { .data = gBerryCrushTimerDigitsPal, .tag = 4 }, + { .data = sCrusherBase_Pal, .tag = TAG_CRUSHER_BASE }, + { .data = sEffects_Pal, .tag = PALTAG_EFFECT }, // For the impact and sparkle effects + { .data = sTimerDigits_Pal, .tag = TAG_TIMER_DIGITS }, {} }; -static const union AnimCmd gUnknown_082F423C[] = +static const union AnimCmd sAnim_CrusherBase[] = { ANIMCMD_FRAME(0, 0), ANIMCMD_END }; -static const union AnimCmd gUnknown_082F4244[] = +static const union AnimCmd sAnim_Impact_Small[] = { ANIMCMD_FRAME(0, 4), ANIMCMD_FRAME(16, 4), @@ -562,7 +715,7 @@ static const union AnimCmd gUnknown_082F4244[] = ANIMCMD_END }; -static const union AnimCmd gUnknown_082F4254[] = +static const union AnimCmd sAnim_Impact_Big[] = { ANIMCMD_FRAME(48, 2), ANIMCMD_FRAME(64, 2), @@ -571,7 +724,7 @@ static const union AnimCmd gUnknown_082F4254[] = ANIMCMD_END }; -static const union AnimCmd gUnknown_082F4268[] = +static const union AnimCmd sAnim_Sparkle_Small[] = { ANIMCMD_FRAME(0, 2), ANIMCMD_FRAME(4, 2), @@ -582,7 +735,7 @@ static const union AnimCmd gUnknown_082F4268[] = ANIMCMD_JUMP(0) }; -static const union AnimCmd gUnknown_082F4284[] = +static const union AnimCmd sAnim_Sparkle_Big[] = { ANIMCMD_FRAME(24, 4), ANIMCMD_FRAME(28, 4), @@ -595,105 +748,105 @@ static const union AnimCmd gUnknown_082F4284[] = ANIMCMD_JUMP(0) }; -static const union AnimCmd gUnknown_082F42A8[] = +static const union AnimCmd sAnim_Timer[] = { ANIMCMD_FRAME(20, 0), ANIMCMD_END }; -static const union AnimCmd gUnknown_082F42B0[] = +static const union AnimCmd sAnim_PlayerBerry[] = { ANIMCMD_FRAME(0, 0), ANIMCMD_END }; -static const union AffineAnimCmd gUnknown_082F42B8[] = +static const union AffineAnimCmd sAffineAnim_PlayerBerry_0[] = { AFFINEANIMCMD_FRAME(256, 256, 0, 0), AFFINEANIMCMD_FRAME(0, 0, 2, 1), AFFINEANIMCMD_JUMP(1) }; -static const union AffineAnimCmd gUnknown_082F42D0[] = +static const union AffineAnimCmd sAffineAnim_PlayerBerry_1[] = { AFFINEANIMCMD_FRAME(256, 256, 0, 0), AFFINEANIMCMD_FRAME(0, 0, -2, 1), AFFINEANIMCMD_JUMP(1) }; -static const union AnimCmd *const sAnimTable_BerryCrushCore[] = +static const union AnimCmd *const sAnims_CrusherBase[] = { - gUnknown_082F423C + sAnim_CrusherBase }; -static const union AnimCmd *const sAnimTable_BerryCrushImpact[] = +static const union AnimCmd *const sAnims_Impact[] = { - gUnknown_082F4244, - gUnknown_082F4254, + sAnim_Impact_Small, + sAnim_Impact_Big, }; -static const union AnimCmd *const sAnimTable_BerryCrushPowderSparkles[] = +static const union AnimCmd *const sAnims_Sparkle[] = { - gUnknown_082F4268, - gUnknown_082F4284, + sAnim_Sparkle_Small, + sAnim_Sparkle_Big, }; -static const union AnimCmd *const sAnimTable_BerryCrushTimer[] = +static const union AnimCmd *const sAnims_Timer[] = { - gUnknown_082F42A8 + sAnim_Timer }; -static const union AnimCmd *const gUnknown_082F4300[] = +static const union AnimCmd *const sAnims_PlayerBerry[] = { - gUnknown_082F42B0 + sAnim_PlayerBerry }; -static const union AffineAnimCmd *const gUnknown_082F4304[] = +static const union AffineAnimCmd *const sAffineAnims_PlayerBerry[] = { - gUnknown_082F42B8, - gUnknown_082F42D0, + sAffineAnim_PlayerBerry_0, + sAffineAnim_PlayerBerry_1, }; -static const struct SpriteTemplate sSpriteTemplate_BerryCrushCore = +static const struct SpriteTemplate sSpriteTemplate_CrusherBase = { - .tileTag = 1, - .paletteTag = 1, + .tileTag = TAG_CRUSHER_BASE, + .paletteTag = TAG_CRUSHER_BASE, .oam = &gOamData_AffineOff_ObjNormal_64x64, - .anims = sAnimTable_BerryCrushCore, + .anims = sAnims_CrusherBase, .images = NULL, .affineAnims = gDummySpriteAffineAnimTable, .callback = SpriteCallbackDummy }; -static const struct SpriteTemplate sSpriteTemplate_BerryCrushImpact = +static const struct SpriteTemplate sSpriteTemplate_Impact = { - .tileTag = 2, - .paletteTag = 2, + .tileTag = GFXTAG_IMPACT, + .paletteTag = PALTAG_EFFECT, .oam = &gOamData_AffineOff_ObjNormal_32x32, - .anims = sAnimTable_BerryCrushImpact, + .anims = sAnims_Impact, .images = NULL, .affineAnims = gDummySpriteAffineAnimTable, - .callback = SpriteCB_BerryCrushImpact + .callback = SpriteCB_Impact }; -static const struct SpriteTemplate sSpriteTemplate_BerryCrushPowderSparkles = +static const struct SpriteTemplate sSpriteTemplate_Sparkle = { - .tileTag = 3, - .paletteTag = 2, + .tileTag = GFXTAG_SPARKLE, + .paletteTag = PALTAG_EFFECT, .oam = &gOamData_AffineOff_ObjNormal_16x16, - .anims = sAnimTable_BerryCrushPowderSparkles, + .anims = sAnims_Sparkle, .images = NULL, .affineAnims = gDummySpriteAffineAnimTable, .callback = SpriteCallbackDummy }; -static const struct SpriteTemplate sSpriteTemplate_BerryCrushTimer = +static const struct SpriteTemplate sSpriteTemplate_Timer = { - .tileTag = 4, - .paletteTag = 4, + .tileTag = TAG_TIMER_DIGITS, + .paletteTag = TAG_TIMER_DIGITS, .oam = &gOamData_AffineOff_ObjNormal_8x16, - .anims = sAnimTable_BerryCrushTimer, + .anims = sAnims_Timer, .images = NULL, .affineAnims = gDummySpriteAffineAnimTable, .callback = SpriteCallbackDummy @@ -701,18 +854,18 @@ static const struct SpriteTemplate sSpriteTemplate_BerryCrushTimer = static const struct SpriteTemplate sSpriteTemplate_PlayerBerry = { - .tileTag = 5, - .paletteTag = 5, + .tileTag = TAG_PLAYER1_BERRY, + .paletteTag = TAG_PLAYER1_BERRY, .oam = &gOamData_AffineDouble_ObjNormal_32x32, - .anims = gUnknown_082F4300, + .anims = sAnims_PlayerBerry, .images = NULL, - .affineAnims = gUnknown_082F4304, + .affineAnims = sAffineAnims_PlayerBerry, .callback = SpriteCallbackDummy }; static const struct DigitObjUtilTemplate sDigitObjTemplates[] = { - { + { // Minutes .strConvMode = 1, .shape = 2, .size = 0, @@ -721,10 +874,10 @@ static const struct DigitObjUtilTemplate sDigitObjTemplates[] = .xDelta = 8, .x = 156, .y = 0, - .spriteSheet = gUnknown_082F420C, + .spriteSheet = (void*) &sSpriteSheets[3], .spritePal = &sSpritePals[2], }, - { + { // Seconds .strConvMode = 0, .shape = 2, .size = 0, @@ -733,10 +886,10 @@ static const struct DigitObjUtilTemplate sDigitObjTemplates[] = .xDelta = 8, .x = 180, .y = 0, - .spriteSheet = gUnknown_082F420C, + .spriteSheet = (void*) &sSpriteSheets[3], .spritePal = &sSpritePals[2], }, - { + { // 1/60ths of a second .strConvMode = 0, .shape = 2, .size = 0, @@ -745,79 +898,83 @@ static const struct DigitObjUtilTemplate sDigitObjTemplates[] = .xDelta = 8, .x = 204, .y = 0, - .spriteSheet = gUnknown_082F420C, + .spriteSheet = (void*) &sSpriteSheets[3], .spritePal = &sSpritePals[2], } }; -static const u8 *const sBCRankingHeaders[] = +static const u8 *const sResultsTexts[] = { - gText_SpaceTimes2, - gText_XDotY, - gText_Var1Berry, - gText_NeatnessRankings, - gText_CoopRankings, - gText_PressingPowerRankings, + [RESULTS_PAGE_PRESSES] = gText_SpaceTimes2, // " times" + [RESULTS_PAGE_RANDOM] = gText_XDotY, // "##.##", for Neatness, Cooperation, or Power value + [RESULTS_PAGE_CRUSHING] = gText_Var1Berry, + + [RESULTS_PAGE_NEATNESS + NUM_RESULTS_PAGES] = gText_NeatnessRankings, + [RESULTS_PAGE_COOPERATIVE + NUM_RESULTS_PAGES] = gText_CoopRankings, + [RESULTS_PAGE_POWER + NUM_RESULTS_PAGES] = gText_PressingPowerRankings, }; -static u32 (*const sBerryCrushCommands[])(struct BerryCrushGame *, u8 *) = +static u32 (*const sBerryCrushCommands[])(struct BerryCrushGame * game, u8 * data) = { - NULL, - BerryCrushCommand_BeginNormalPaletteFade, - BerryCrushCommand_WaitPaletteFade, - BerryCrushCommand_PrintMessage, - BerryCrushCommand_InitGfx, - BerryCrushCommand_TeardownGfx, - BerryCrushCommand_SignalReadyToBegin, - BerryCrushCommand_AskPickBerry, - BerryCrushCommand_GoToBerryPouch, - BerryCrushCommand_WaitForOthersToPickBerries, - BerryCrushCommand_DropBerriesIntoCrusher, - BerryCrushCommand_DropLid, - BerryCrushCommand_Countdown, - BerryCrushCommand_PlayGame_Master, - BerryCrushCommand_PlayGame_Slave, - BerryCrushCommand_FinishGame, - BerryCrushCommand_HandleTimeUp, - BerryCrushCommand_TabulateResults, - BerryCrushCommand_ShowResults, - BerryCrushCommand_SaveGame, - BerryCrushCommand_AskPlayAgain, - BerryCrushCommand_CommunicatePlayAgainResponses, - BerryCrushCommand_FadeOutToPlayAgain, - BerryCrushCommand_PlayAgainFailureMessage, - BerryCrushCommand_GracefulExit, - BerryCrushCommand_Quit, + [CMD_NONE] = NULL, + [CMD_FADE] = Cmd_BeginNormalPaletteFade, + [CMD_WAIT_FADE] = Cmd_WaitPaletteFade, + [CMD_PRINT_MSG] = Cmd_PrintMessage, + [CMD_SHOW_GAME] = Cmd_ShowGameDisplay, + [CMD_HIDE_GAME] = Cmd_HideGameDisplay, + [CMD_READY_BEGIN] = Cmd_SignalReadyToBegin, + [CMD_ASK_PICK_BERRY] = Cmd_AskPickBerry, + [CMD_PICK_BERRY] = Cmd_GoToBerryPouch, + [CMD_WAIT_BERRIES] = Cmd_WaitForOthersToPickBerries, + [CMD_DROP_BERRIES] = Cmd_DropBerriesIntoCrusher, + [CMD_DROP_LID] = Cmd_DropLid, + [CMD_COUNTDOWN] = Cmd_Countdown, + [CMD_PLAY_GAME_LEADER] = Cmd_PlayGame_Leader, + [CMD_PLAY_GAME_MEMBER] = Cmd_PlayGame_Member, + [CMD_FINISH_GAME] = Cmd_FinishGame, + [CMD_TIMES_UP] = Cmd_HandleTimeUp, + [CMD_CALC_RESULTS] = Cmd_TabulateResults, + [CMD_SHOW_RESULTS] = Cmd_ShowResults, + [CMD_SAVE] = Cmd_SaveGame, + [CMD_ASK_PLAY_AGAIN] = Cmd_AskPlayAgain, + [CMD_COMM_PLAY_AGAIN] = Cmd_CommunicatePlayAgainResponses, + [CMD_PLAY_AGAIN_YES] = Cmd_PlayAgain, + [CMD_PLAY_AGAIN_NO] = Cmd_StopGame, + [CMD_CLOSE_LINK] = Cmd_CloseLink, + [CMD_QUIT] = Cmd_Quit, }; -static const u8 gUnknown_082F4434[][4] = +// Per group size, the number of A presses required to increase the number of sparkles. +static const u8 sSparkleThresholds[MAX_RFU_PLAYERS - 1][4] = { - {2, 4, 6, 7}, - {3, 5, 8, 11}, - {3, 7, 11, 15}, - {4, 8, 12, 17}, + {2, 4, 6, 7}, // 2 players + {3, 5, 8, 11}, // 3 players + {3, 7, 11, 15}, // 4 players + {4, 8, 12, 17}, // 5 players }; -static const u8 gUnknown_082F4444[] = {5, 7, 9, 12}; +// Per group size, the number of A presses required to get big sparkles +static const u8 sBigSparkleThresholds[MAX_RFU_PLAYERS - 1] = {5, 7, 9, 12}; + static const u8 sReceivedPlayerBitmasks[] = {0x03, 0x07, 0x0F, 0x1F}; -struct BerryCrushGame * GetBerryCrushGame(void) +static struct BerryCrushGame * GetBerryCrushGame(void) { - return sBerryCrushGamePtr; + return sGame; } -u32 QuitBerryCrush(MainCallback callback) +static u32 QuitBerryCrush(MainCallback exitCallback) { - if (!sBerryCrushGamePtr) + if (!sGame) return 2; - if (!callback) - callback = sBerryCrushGamePtr->savedCallback; + if (!exitCallback) + exitCallback = sGame->exitCallback; - DestroyTask(sBerryCrushGamePtr->mainTask); - FREE_AND_SET_NULL(sBerryCrushGamePtr); - SetMainCallback2(callback); - if (callback == CB2_ReturnToField) + DestroyTask(sGame->taskId); + FREE_AND_SET_NULL(sGame); + SetMainCallback2(exitCallback); + if (exitCallback == CB2_ReturnToField) { gTextFlags.autoScroll = TRUE; PlayNewMapMusic(MUS_POKE_CENTER); @@ -827,17 +984,23 @@ u32 QuitBerryCrush(MainCallback callback) return 0; } -void StartBerryCrush(MainCallback callback) +#define ERROR_EXIT(exitCallback) \ + { \ + SetMainCallback2(exitCallback); \ + Rfu.unk_10 = 0; \ + Rfu.unk_12 = 0; \ + Rfu.errorState = 1; \ + } + +void StartBerryCrush(MainCallback exitCallback) { u8 playerCount = 0; u8 multiplayerId; if (!gReceivedRemoteLinkPlayers || gWirelessCommType == 0) { - SetMainCallback2(callback); - Rfu.unk_10 = 0; - Rfu.unk_12 = 0; - Rfu.errorState = 1; + // Link disconnected + ERROR_EXIT(exitCallback); return; } @@ -845,35 +1008,31 @@ void StartBerryCrush(MainCallback callback) multiplayerId = GetMultiplayerId(); if (playerCount < 2 || multiplayerId >= playerCount) { - SetMainCallback2(callback); - Rfu.unk_10 = 0; - Rfu.unk_12 = 0; - Rfu.errorState = 1; + // Too few players, or invalid id + ERROR_EXIT(exitCallback); return; } - sBerryCrushGamePtr = AllocZeroed(sizeof(struct BerryCrushGame)); - if (!sBerryCrushGamePtr) + sGame = AllocZeroed(sizeof(*sGame)); + if (!sGame) { - SetMainCallback2(callback); - Rfu.unk_10 = 0; - Rfu.unk_12 = 0; - Rfu.errorState = 1; + // Alloc failed + ERROR_EXIT(exitCallback); return; } - sBerryCrushGamePtr->savedCallback = callback; - sBerryCrushGamePtr->localId = multiplayerId; - sBerryCrushGamePtr->playerCount = playerCount; - ParseName_Options(sBerryCrushGamePtr); - sBerryCrushGamePtr->gameState = 1; - sBerryCrushGamePtr->nextCmd = 1; - sBerryCrushGamePtr->afterPalFadeCmd = 6; - BerryCrush_SetPaletteFadeParams(sBerryCrushGamePtr->commandParams, 1, -1, 0, 16, 0, 0); - BerryCrush_RunOrScheduleCommand(4, 1, sBerryCrushGamePtr->commandParams); + sGame->exitCallback = exitCallback; + sGame->localId = multiplayerId; + sGame->playerCount = playerCount; + SetNamesAndTextSpeed(sGame); + sGame->gameState = STATE_INIT; + sGame->nextCmd = CMD_FADE; + sGame->afterPalFadeCmd = CMD_READY_BEGIN; + SetPaletteFadeArgs(sGame->commandArgs, TRUE, PALETTES_ALL, 0, 16, 0, RGB_BLACK); + RunOrScheduleCommand(CMD_SHOW_GAME, 1, sGame->commandArgs); SetMainCallback2(MainCB); - sBerryCrushGamePtr->mainTask = CreateTask(MainTask, 8); - gTextFlags.autoScroll = 0; + sGame->taskId = CreateTask(MainTask, 8); + gTextFlags.autoScroll = FALSE; } static void GetBerryFromBag(void) @@ -883,18 +1042,18 @@ static void GetBerryFromBag(void) else RemoveBagItem(gSpecialVar_ItemId, 1); - sBerryCrushGamePtr->unk98[sBerryCrushGamePtr->localId].unkC = gSpecialVar_ItemId - FIRST_BERRY_INDEX; - sBerryCrushGamePtr->nextCmd = 1; - sBerryCrushGamePtr->afterPalFadeCmd = 9; - BerryCrush_SetPaletteFadeParams(sBerryCrushGamePtr->commandParams, 0, -1, 0, 16, 0, 0); - BerryCrush_RunOrScheduleCommand(4, 1, sBerryCrushGamePtr->commandParams); - sBerryCrushGamePtr->mainTask = CreateTask(MainTask, 8); + sGame->players[sGame->localId].berryId = gSpecialVar_ItemId - FIRST_BERRY_INDEX; + sGame->nextCmd = CMD_FADE; + sGame->afterPalFadeCmd = CMD_WAIT_BERRIES; + SetPaletteFadeArgs(sGame->commandArgs, FALSE, PALETTES_ALL, 0, 16, 0, RGB_BLACK); + RunOrScheduleCommand(CMD_SHOW_GAME, 1, sGame->commandArgs); + sGame->taskId = CreateTask(MainTask, 8); SetMainCallback2(MainCB); } -static void BerryCrush_SetupMainTask(void) +static void ChooseBerry(void) { - DestroyTask(sBerryCrushGamePtr->mainTask); + DestroyTask(sGame->taskId); ChooseBerryForMachine(GetBerryFromBag); } @@ -908,54 +1067,60 @@ static void BerryCrush_InitVBlankCB(void) SetVBlankCallback(NULL); } -static void BerryCrush_SaveResults(void) +static void SaveResults(void) { - u32 var0, var1; + u32 time, presses; - var0 = sBerryCrushGamePtr->unk68.unk04; - var0 = Q_24_8(var0); - var0 = MathUtil_Div32(var0, Q_24_8(60)); - var1 = sBerryCrushGamePtr->unk68.unk0A; - var1 = Q_24_8(var1); - var1 = MathUtil_Div32(var1, var0) & 0xFFFF; - sBerryCrushGamePtr->pressingSpeed = var1; - switch (sBerryCrushGamePtr->playerCount) + // Calculate pressing speed ((time / 60) / presses) + time = sGame->results.time; + time = Q_24_8(time); + time = MathUtil_Div32(time, Q_24_8(60)); + presses = sGame->results.totalAPresses; + presses = Q_24_8(presses); + presses = MathUtil_Div32(presses, time) & 0xFFFF; + sGame->pressingSpeed = presses; + + switch (sGame->playerCount) { case 2: - if (sBerryCrushGamePtr->pressingSpeed > gSaveBlock2Ptr->berryCrush.berryCrushResults[0]) + if (sGame->pressingSpeed > gSaveBlock2Ptr->berryCrush.pressingSpeeds[0]) { - sBerryCrushGamePtr->unk25_1 = 1; - gSaveBlock2Ptr->berryCrush.berryCrushResults[0] = sBerryCrushGamePtr->pressingSpeed; + // New 2-player record + sGame->newRecord = TRUE; + gSaveBlock2Ptr->berryCrush.pressingSpeeds[0] = sGame->pressingSpeed; } break; case 3: - if (sBerryCrushGamePtr->pressingSpeed > gSaveBlock2Ptr->berryCrush.berryCrushResults[1]) + if (sGame->pressingSpeed > gSaveBlock2Ptr->berryCrush.pressingSpeeds[1]) { - sBerryCrushGamePtr->unk25_1 = 1; - gSaveBlock2Ptr->berryCrush.berryCrushResults[1] = sBerryCrushGamePtr->pressingSpeed; + // New 3-player record + sGame->newRecord = TRUE; + gSaveBlock2Ptr->berryCrush.pressingSpeeds[1] = sGame->pressingSpeed; } break; case 4: - if (sBerryCrushGamePtr->pressingSpeed > gSaveBlock2Ptr->berryCrush.berryCrushResults[2]) + if (sGame->pressingSpeed > gSaveBlock2Ptr->berryCrush.pressingSpeeds[2]) { - sBerryCrushGamePtr->unk25_1 = 1; - gSaveBlock2Ptr->berryCrush.berryCrushResults[2] = sBerryCrushGamePtr->pressingSpeed; + // New 4-player record + sGame->newRecord = TRUE; + gSaveBlock2Ptr->berryCrush.pressingSpeeds[2] = sGame->pressingSpeed; } break; case 5: - if (sBerryCrushGamePtr->pressingSpeed > gSaveBlock2Ptr->berryCrush.berryCrushResults[3]) + if (sGame->pressingSpeed > gSaveBlock2Ptr->berryCrush.pressingSpeeds[3]) { - sBerryCrushGamePtr->unk25_1 = 1; - gSaveBlock2Ptr->berryCrush.berryCrushResults[3] = sBerryCrushGamePtr->pressingSpeed; + // New 5-player record + sGame->newRecord = TRUE; + gSaveBlock2Ptr->berryCrush.pressingSpeeds[3] = sGame->pressingSpeed; } break; } - sBerryCrushGamePtr->powder = sBerryCrushGamePtr->unk68.unk00; - if (GiveBerryPowder(sBerryCrushGamePtr->powder)) + sGame->powder = sGame->results.powder; + if (GiveBerryPowder(sGame->powder)) return; - sBerryCrushGamePtr->unk25_0 = 1; + sGame->noRoomForPowder = TRUE; } static void VBlankCB(void) @@ -975,40 +1140,38 @@ static void MainCB(void) static void MainTask(u8 taskId) { - if (sBerryCrushGamePtr->cmdCallback) - sBerryCrushGamePtr->cmdCallback(sBerryCrushGamePtr, sBerryCrushGamePtr->commandParams); + if (sGame->cmdCallback) + sGame->cmdCallback(sGame, sGame->commandArgs); - sub_8021450(sBerryCrushGamePtr); + UpdateGame(sGame); } -static void ParseName_Options(struct BerryCrushGame *arg0) +static void SetNamesAndTextSpeed(struct BerryCrushGame *game) { - u8 i = 0; - - for (; i < arg0->playerCount; i++) - StringCopy(arg0->unk98[i].unk0, gLinkPlayers[i].name); - for (; i < 5; i++) + u8 i; + for (i = 0; i < game->playerCount; i++) + StringCopy(game->players[i].name, gLinkPlayers[i].name); + for (; i < MAX_RFU_PLAYERS; i++) { - memset(arg0->unk98[i].unk0, 1, PLAYER_NAME_LENGTH); - arg0->unk98[i].unk0[PLAYER_NAME_LENGTH] = EOS; + memset(game->players[i].name, 1, PLAYER_NAME_LENGTH); + game->players[i].name[PLAYER_NAME_LENGTH] = EOS; } switch (gSaveBlock2Ptr->optionsTextSpeed) { case OPTIONS_TEXT_SPEED_SLOW: - arg0->textSpeed = 8; + game->textSpeed = 8; break; case OPTIONS_TEXT_SPEED_MID: - arg0->textSpeed = 4; + game->textSpeed = 4; break; case OPTIONS_TEXT_SPEED_FAST: - arg0->textSpeed = 1; + game->textSpeed = 1; break; } } -// TODO: Everything from here on is likely in separate files. -s32 InitBerryCrushDisplay(void) +static s32 ShowGameDisplay(void) { struct BerryCrushGame *game = GetBerryCrushGame(); if (!game) @@ -1035,10 +1198,10 @@ s32 InitBerryCrushDisplay(void) break; case 3: ResetBgsAndClearDma3BusyFlags(0); - InitBgsFromTemplates(0, gUnknown_082F32C8, ARRAY_COUNT(gUnknown_082F32C8)); - SetBgTilemapBuffer(1, game->unk138.bgBuffers[0]); - SetBgTilemapBuffer(2, game->unk138.bgBuffers[2]); - SetBgTilemapBuffer(3, game->unk138.bgBuffers[3]); + InitBgsFromTemplates(0, sBgTemplates, ARRAY_COUNT(sBgTemplates)); + SetBgTilemapBuffer(1, game->gfx.bgBuffers[0]); + SetBgTilemapBuffer(2, game->gfx.bgBuffers[2]); + SetBgTilemapBuffer(3, game->gfx.bgBuffers[3]); ChangeBgX(0, 0, 0); ChangeBgY(0, 0, 0); ChangeBgX(2, 0, 0); @@ -1059,7 +1222,7 @@ s32 InitBerryCrushDisplay(void) CopyBgTilemapBufferToVram(1); CopyBgTilemapBufferToVram(2); CopyBgTilemapBufferToVram(3); - DecompressAndCopyTileDataToVram(1, gUnknown_08DE34B8, 0, 0, 0); + DecompressAndCopyTileDataToVram(1, gBerryCrush_Crusher_Gfx, 0, 0, 0); break; case 6: if (FreeTempTileDataBuffersIfPossible()) @@ -1067,16 +1230,16 @@ s32 InitBerryCrushDisplay(void) InitStandardTextBoxWindows(); InitTextBoxGfxAndPrinters(); - sub_8022588(game); - sub_8022600(game); + CreatePlayerNameWindows(game); + DrawPlayerNameWindows(game); gPaletteFade.bufferTransferDisabled = TRUE; break; case 7: - LoadPalette(gUnknown_08DE3398, 0, 0x180); - CopyToBgTilemapBuffer(1, gBerryCrushGrinderTopTilemap, 0, 0); - CopyToBgTilemapBuffer(2, gBerryCrushContainerCapTilemap, 0, 0); - CopyToBgTilemapBuffer(3, gBerryCrushBackgroundTilemap, 0, 0); - sub_80226D0(game); + LoadPalette(gBerryCrush_Crusher_Pal, 0, 0x180); + CopyToBgTilemapBuffer(1, sCrusherTop_Tilemap, 0, 0); + CopyToBgTilemapBuffer(2, sContainerCap_Tilemap, 0, 0); + CopyToBgTilemapBuffer(3, sBg_Tilemap, 0, 0); + CopyPlayerNameWindowGfxToBg(game); CopyBgTilemapBufferToVram(1); CopyBgTilemapBufferToVram(2); CopyBgTilemapBufferToVram(3); @@ -1084,7 +1247,7 @@ s32 InitBerryCrushDisplay(void) case 8: LoadWirelessStatusIndicatorSpriteGfx(); CreateWirelessStatusIndicatorSprite(0, 0); - sub_8022730(game); + CreateGameSprites(game); SetGpuReg(REG_OFFSET_BG1VOFS, -gSpriteCoordOffsetY); ChangeBgX(1, 0, 0); ChangeBgY(1, 0, 0); @@ -1106,13 +1269,13 @@ s32 InitBerryCrushDisplay(void) return 0; } -static s32 BerryCrush_TeardownBgs(void) +static s32 HideGameDisplay(void) { - struct BerryCrushGame *var0 = GetBerryCrushGame(); - if (!var0) + struct BerryCrushGame *game = GetBerryCrushGame(); + if (!game) return -1; - switch (var0->cmdState) + switch (game->cmdState) { case 0: Rfu_SetLinkStandbyCallback(); @@ -1120,8 +1283,11 @@ static s32 BerryCrush_TeardownBgs(void) case 1: if (!IsLinkTaskFinished()) return 0; - // fall through. The original author forgot to use "break" here - // because this will call BeginNormalPaletteFade() twice. + // fall through + // This will call BeginNormalPaletteFade() twice. +#ifdef BUGFIX + break; +#endif case 2: BeginNormalPaletteFade(PALETTES_ALL, 0, 0, 16, RGB_BLACK); UpdatePaletteFade(); @@ -1154,83 +1320,98 @@ static s32 BerryCrush_TeardownBgs(void) break; case 6: DestroyWirelessStatusIndicatorSprite(); - sub_8022960(var0); + DestroyGameSprites(game); DigitObjUtil_Free(); break; case 7: - var0->cmdState = 0; + game->cmdState = 0; return 1; } - var0->cmdState++; + game->cmdState++; return 0; } -static s32 sub_8021450(struct BerryCrushGame *arg0) +// Handles the crusher vibration and the timer +static s32 UpdateGame(struct BerryCrushGame *game) { - gSpriteCoordOffsetY = arg0->depth + arg0->vibration; + gSpriteCoordOffsetY = game->depth + game->vibration; SetGpuReg(REG_OFFSET_BG1VOFS, -gSpriteCoordOffsetY); - if (arg0->gameState == 7) - { - BerryCrush_PrintTimeOnSprites(&arg0->unk138, arg0->timer); - } + + if (game->gameState == STATE_PLAYING) + PrintTimer(&game->gfx, game->timer); return 0; } -void sub_8021488(struct BerryCrushGame *arg0) +static void ResetCrusherPos(struct BerryCrushGame *game) { - arg0->depth = -104; - arg0->vibration = 0; + game->depth = CRUSHER_START_Y; + game->vibration = 0; gSpriteCoordOffsetX = 0; - gSpriteCoordOffsetY = -104; + gSpriteCoordOffsetY = CRUSHER_START_Y; } -static void BerryCrush_CreateBerrySprites(struct BerryCrushGame *arg0, struct BerryCrushGame_138 *arg1) +// Sprite data for berry sprites. Identical to fields for sparkle sprites +#define sX data[0] +#define sYSpeed data[1] +#define sYAccel data[2] +#define sXSpeed data[3] +#define sSinIdx data[4] +#define sSinSpeed data[5] +#define sAmplitude data[6] +// The last element (data[7]) is a bitfield. +// The first 15 bits are the y coord to stop at. +// The last bit is a flag for whether or not to move horizontally too +#define sBitfield data[7] +#define MASK_TARGET_Y 0x7FFF +#define F_MOVE_HORIZ 0x8000 + +static void CreateBerrySprites(struct BerryCrushGame *game, struct BerryCrushGame_Gfx *gfx) { u8 i; u8 spriteId; - s16 var0, var1; + s16 distance, var1; s16 *data; - s32 var3; - s16 var5; - u32 var6; + s32 amplitude; + s16 speed; + u32 var2; - for (i = 0; i < arg0->playerCount; i++) + for (i = 0; i < game->playerCount; i++) { spriteId = AddCustomItemIconSprite( &sSpriteTemplate_PlayerBerry, sPlayerBerrySpriteTags[i], sPlayerBerrySpriteTags[i], - arg0->unk98[i].unkC + FIRST_BERRY_INDEX); - arg1->berrySprites[i] = &gSprites[spriteId]; - arg1->berrySprites[i]->oam.priority = 3; - arg1->berrySprites[i]->affineAnimPaused = TRUE; - arg1->berrySprites[i]->pos1.x = arg1->seatCoords[i]->unk8 + 120; - arg1->berrySprites[i]->pos1.y = -16; - data = arg1->berrySprites[i]->data; - var5 = 512; - data[1] = var5; - data[2] = 32; - data[7] = 112; - var0 = arg1->seatCoords[i]->unkA - arg1->seatCoords[i]->unk8; - var3 = var0; - if (var0 < 0) - var3 += 3; + game->players[i].berryId + FIRST_BERRY_INDEX); + gfx->berrySprites[i] = &gSprites[spriteId]; + gfx->berrySprites[i]->oam.priority = 3; + gfx->berrySprites[i]->affineAnimPaused = TRUE; + gfx->berrySprites[i]->pos1.x = gfx->playerCoords[i]->berryXOffset + 120; + gfx->berrySprites[i]->pos1.y = -16; + data = gfx->berrySprites[i]->data; + speed = 512; + sYSpeed = speed; + sYAccel = 32; + sBitfield = 112; // Setting bits in MASK_TARGET_Y + distance = gfx->playerCoords[i]->berryXDest - gfx->playerCoords[i]->berryXOffset; + amplitude = distance; + if (distance < 0) + amplitude += 3; - data[6] = var3 >> 2; - var0 *= 128; - var6 = var5 + 32; - var6 = var6 / 2; - var1 = MathUtil_Div16Shift(7, Q_8_8(63.5), var6); - data[0] = (u16)arg1->berrySprites[i]->pos1.x * 128; - data[3] = MathUtil_Div16Shift(7, var0, var1); + sAmplitude = amplitude >> 2; + distance *= 128; + var2 = speed + 32; + var2 = var2 / 2; + var1 = MathUtil_Div16Shift(7, Q_8_8(63.5), var2); + sX = (u16)gfx->berrySprites[i]->pos1.x * 128; + sXSpeed = MathUtil_Div16Shift(7, distance, var1); var1 = MathUtil_Mul16Shift(7, var1, 85); - data[4] = 0; - data[5] = MathUtil_Div16Shift(7, Q_8_8(63.5), var1); - data[7] |= 0x8000; - if (arg1->seatCoords[i]->unk8 < 0) - StartSpriteAffineAnim(arg1->berrySprites[i], 1); + sSinIdx = 0; + sSinSpeed = MathUtil_Div16Shift(7, Q_8_8(63.5), var1); + sBitfield |= F_MOVE_HORIZ; + if (gfx->playerCoords[i]->berryXOffset < 0) + StartSpriteAffineAnim(gfx->berrySprites[i], 1); } } @@ -1238,22 +1419,22 @@ static void SpriteCB_DropBerryIntoCrusher(struct Sprite *sprite) { s16 *data = sprite->data; - data[1] += data[2]; - sprite->pos2.y += data[1] >> 8; - if (data[7] & 0x8000) + sYSpeed += sYAccel; + sprite->pos2.y += sYSpeed >> 8; + if (sBitfield & F_MOVE_HORIZ) { - sprite->data[0] += data[3]; - data[4] += data[5]; - sprite->pos2.x = Sin(data[4] >> 7, data[6]); - if ((data[7] & 0x8000) && (data[4] >> 7) > 126) + sprite->sX += sXSpeed; + sSinIdx += sSinSpeed; + sprite->pos2.x = Sin(sSinIdx >> 7, sAmplitude); + if ((sBitfield & F_MOVE_HORIZ) && (sSinIdx >> 7) > 126) { sprite->pos2.x = 0; - data[7] &= 0x7FFF; + sBitfield &= MASK_TARGET_Y; } } - sprite->pos1.x = data[0] >> 7; - if (sprite->pos1.y + sprite->pos2.y >= (data[7] & 0x7FFF)) + sprite->pos1.x = sX >> 7; + if (sprite->pos1.y + sprite->pos2.y >= (sBitfield & MASK_TARGET_Y)) { sprite->callback = SpriteCallbackDummy; FreeSpriteOamMatrix(sprite); @@ -1261,119 +1442,143 @@ static void SpriteCB_DropBerryIntoCrusher(struct Sprite *sprite) } } -void BerryCrushFreeBerrySpriteGfx(struct BerryCrushGame *arg0, __attribute__((unused)) struct BerryCrushGame_138 *arg1) +#undef sX +#undef sYSpeed +#undef sYAccel +#undef sXSpeed +#undef sSinIdx +#undef sSinSpeed +#undef sAmplitude +#undef sBitfield +#undef MASK_TARGET_Y +#undef F_MOVE_HORIZ + +static void BerryCrushFreeBerrySpriteGfx(struct BerryCrushGame *game, struct BerryCrushGame_Gfx *gfx) { u8 i; - for (i = 0; i < arg0->playerCount; i++) + for (i = 0; i < game->playerCount; i++) { FreeSpritePaletteByTag(sPlayerBerrySpriteTags[i]); FreeSpriteTilesByTag(sPlayerBerrySpriteTags[i]); } } -void sub_80216E0(struct BerryCrushGame *arg0, struct BerryCrushGame_138 *arg1) +static void UpdateInputEffects(struct BerryCrushGame *game, struct BerryCrushGame_Gfx *gfx) { - u8 sp4; - struct BerryCrushGame_4E *var4E; + u8 numPlayersPressed; + struct BerryCrushGame_LinkState *linkState; u8 i; - u16 var, var2; + u16 temp1, xModifier; - sp4 = 0; - var4E = (struct BerryCrushGame_4E *)arg0->recvCmd; - for (i = 0; i < arg0->playerCount; i++) + numPlayersPressed = 0; + linkState = (struct BerryCrushGame_LinkState *)game->recvCmd; + + // Read inputs and update impact effects + for (i = 0; i < game->playerCount; i++) { - var = var4E->unkA >> (i * 3); - var &= 7; - if (var) - { - sp4++; - if (var & 0x4) - StartSpriteAnim(arg1->impactSprites[i], 1); - else - StartSpriteAnim(arg1->impactSprites[i], 0); + #define flags temp1 - arg1->impactSprites[i]->invisible = FALSE; - arg1->impactSprites[i]->animPaused = FALSE; - arg1->impactSprites[i]->pos2.x = gUnknown_082F41CC[(var % 4) - 1][0]; - arg1->impactSprites[i]->pos2.y = gUnknown_082F41CC[(var % 4) - 1][1]; + flags = linkState->inputFlags >> (i * INPUT_FLAGS_PER_PLAYER); + flags &= INPUT_FLAG_MASK; + if (flags) + { + numPlayersPressed++; + if (flags & F_INPUT_HIT_SYNC) + StartSpriteAnim(gfx->impactSprites[i], 1); // Big impact sprite + else + StartSpriteAnim(gfx->impactSprites[i], 0); // Small impact sprite + + gfx->impactSprites[i]->invisible = FALSE; + gfx->impactSprites[i]->animPaused = FALSE; + gfx->impactSprites[i]->pos2.x = sImpactCoords[(flags % (ARRAY_COUNT(sImpactCoords) + 1)) - 1][0]; + gfx->impactSprites[i]->pos2.y = sImpactCoords[(flags % (ARRAY_COUNT(sImpactCoords) + 1)) - 1][1]; } + + #undef flags } - if (sp4 == 0) + if (numPlayersPressed == 0) { - arg0->unk25_2 = 0; + game->playedSound = FALSE; } else { - var = (u8)(arg0->timer % 3); - var2 = var; - for (i = 0; i < var4E->unkC * 2 + 3; i++) - { - if (arg1->sparkleSprites[i]->invisible) - { - arg1->sparkleSprites[i]->callback = sub_8022B28; - arg1->sparkleSprites[i]->pos1.x = gUnknown_082F41D2[i][0] + 120; - arg1->sparkleSprites[i]->pos1.y = gUnknown_082F41D2[i][1] + 136 - (var * 4); - arg1->sparkleSprites[i]->pos2.x = gUnknown_082F41D2[i][0] + (gUnknown_082F41D2[i][0] / (var2 * 4)); - arg1->sparkleSprites[i]->pos2.y = gUnknown_082F41D2[i][1]; - if (var4E->unk4_1) - StartSpriteAnim(arg1->sparkleSprites[i], 1); - else - StartSpriteAnim(arg1->sparkleSprites[i], 0); + // Update sparkle effect + #define yModifier temp1 - var++; - if (var > 3) - var = 0; + yModifier = (u8)(game->timer % 3); + xModifier = yModifier; + for (i = 0; i < linkState->sparkleAmount * 2 + 3; i++) + { + if (gfx->sparkleSprites[i]->invisible) + { + gfx->sparkleSprites[i]->callback = SpriteCB_Sparkle_Init; + gfx->sparkleSprites[i]->pos1.x = sSparkleCoords[i][0] + 120; + gfx->sparkleSprites[i]->pos1.y = sSparkleCoords[i][1] + 136 - (yModifier * 4); + gfx->sparkleSprites[i]->pos2.x = sSparkleCoords[i][0] + (sSparkleCoords[i][0] / (xModifier * 4)); + gfx->sparkleSprites[i]->pos2.y = sSparkleCoords[i][1]; + if (linkState->bigSparkle) + StartSpriteAnim(gfx->sparkleSprites[i], 1); + else + StartSpriteAnim(gfx->sparkleSprites[i], 0); + + yModifier++; + if (yModifier > 3) + yModifier = 0; } } - if (arg0->unk25_2) + #undef yModifier + + if (game->playedSound) { - arg0->unk25_2 = 0; + game->playedSound = FALSE; } else { - if (sp4 == 1) + if (numPlayersPressed == 1) PlaySE(SE_MUD_BALL); else PlaySE(SE_BREAKABLE_DOOR); - arg0->unk25_2 = 1; + game->playedSound = TRUE; } } } -bool32 sub_80218D4(struct BerryCrushGame *arg0, struct BerryCrushGame_138 *arg1) +static bool32 AreEffectsFinished(struct BerryCrushGame *game, struct BerryCrushGame_Gfx *gfx) { u8 i; - for (i = 0; i < arg0->playerCount; i++) + // Are any impact sprites active + for (i = 0; i < game->playerCount; i++) { - if (!arg1->impactSprites[i]->invisible) + if (!gfx->impactSprites[i]->invisible) return FALSE; } - for (i = 0; i < 11; i++) + // Are any sparkle sprites active + for (i = 0; i < ARRAY_COUNT(gfx->sparkleSprites); i++) { - if (!arg1->sparkleSprites[i]->invisible) + if (!gfx->sparkleSprites[i]->invisible) return FALSE; } - if (arg0->vibration != 0) - arg0->vibration = 0; + if (game->vibration != 0) + game->vibration = 0; return TRUE; } -static void FramesToMinSec(struct BerryCrushGame_138 *arg0, u16 arg1) +static void FramesToMinSec(struct BerryCrushGame_Gfx *gfx, u16 frames) { u8 i = 0; u32 fractionalFrames = 0; s16 r3 = 0; - arg0->minutes = arg1 / 3600; - arg0->secondsInt = (arg1 % 3600) / 60; - r3 = MathUtil_Mul16(Q_8_8(arg1 % 60), 4); + gfx->minutes = frames / (60 * 60); + gfx->secondsInt = (frames % (60 * 60)) / 60; + r3 = MathUtil_Mul16(Q_8_8(frames % 60), 4); for (i = 0; i < 8; i++) { @@ -1381,277 +1586,262 @@ static void FramesToMinSec(struct BerryCrushGame_138 *arg0, u16 arg1) fractionalFrames += sPressingSpeedConversionTable[i]; } - arg0->secondsFrac = fractionalFrames / 1000000; + gfx->secondsFrac = fractionalFrames / 1000000; } static void PrintTextCentered(u8 windowId, u8 left, u8 colorId, const u8 *string) { left = (left * 4) - (GetStringWidth(2, string, -1) / 2u); - AddTextPrinterParameterized3(windowId, 2, left, 0, sBerryCrushTextColorTable[colorId], 0, string); + AddTextPrinterParameterized3(windowId, 2, left, 0, sTextColorTable[colorId], 0, string); } -static void PrintBerryCrushResultWindow(struct BerryCrushGame * sp0C, u8 sp10, u8 sp14, u8 sp18) +static void PrintResultsText(struct BerryCrushGame * game, u8 page, u8 sp14, u8 baseY) { - u8 r8; - u8 sp1C = 0; - u8 sp20 = 0; - u8 r2; - s32 r3; - u8 r7; - struct BerryCrushGame_68 * sp24 = &sp0C->unk68; + u8 i, j; + u8 playerId = 0; + u8 ranking = 0; + s32 x; + u8 stat; + struct BerryCrushGame_Results * results = &game->results; u32 xOffset; - s32 r6; + s32 y; - sp18 -= 16; - if (sp10 == 2) - sp18 -= 42; - r6 = sp18 - 14 * sp0C->playerCount; - if (r6 > 0) - r6 = r6 / 2 + 16; + baseY -= 16; + if (page == RESULTS_PAGE_CRUSHING) + baseY -= 42; + + y = baseY - 14 * game->playerCount; + if (y > 0) + y = y / 2 + 16; else - r6 = 16; + y = 16; - for (r8 = 0; r8 < sp0C->playerCount; r6 += 14, ++r8) + for (i = 0; i < game->playerCount; y += 14, i++) { DynamicPlaceholderTextUtil_Reset(); - switch (sp10) + switch (page) { - case 0: - sp1C = sp24->unk20[sp10][r8]; - if (r8 != 0 && sp24->stats[sp10][r8] != sp24->stats[sp10][r8 - 1]) - sp20 = r8; - ConvertIntToDecimalStringN(gStringVar4, sp24->stats[sp10][r8], STR_CONV_MODE_RIGHT_ALIGN, 4); - StringAppend(gStringVar4, sBCRankingHeaders[sp10]); + case RESULTS_PAGE_PRESSES: + playerId = results->playerIdsRanked[page][i]; + if (i != 0 && results->stats[page][i] != results->stats[page][i - 1]) + ranking = i; + ConvertIntToDecimalStringN(gStringVar4, results->stats[page][i], STR_CONV_MODE_RIGHT_ALIGN, 4); + StringAppend(gStringVar4, sResultsTexts[page]); break; - case 1: - sp1C = sp24->unk20[sp10][r8]; - if (r8 != 0 && sp24->stats[sp10][r8] != sp24->stats[sp10][r8 - 1]) - sp20 = r8; - ConvertIntToDecimalStringN(gStringVar1, sp24->stats[sp10][r8] >> 4, STR_CONV_MODE_RIGHT_ALIGN, 3); + case RESULTS_PAGE_RANDOM: + playerId = results->playerIdsRanked[page][i]; + if (i != 0 && results->stats[page][i] != results->stats[page][i - 1]) + ranking = i; + ConvertIntToDecimalStringN(gStringVar1, results->stats[page][i] >> 4, STR_CONV_MODE_RIGHT_ALIGN, 3); xOffset = 0; - r7 = sp24->stats[sp10][r8] & 15; - for (r2 = 0; r2 < 4; ++r2) - if ((r7 >> (3 - r2)) & 1) - xOffset += sPressingSpeedConversionTable[r2]; - r7 = xOffset / 1000000u; - ConvertIntToDecimalStringN(gStringVar2, r7, STR_CONV_MODE_LEADING_ZEROS, 2); - StringExpandPlaceholders(gStringVar4, sBCRankingHeaders[sp10]); + stat = results->stats[page][i] & 15; + for (j = 0; j < 4; j++) + if ((stat >> (3 - j)) & 1) + xOffset += sPressingSpeedConversionTable[j]; + stat = xOffset / 1000000u; + ConvertIntToDecimalStringN(gStringVar2, stat, STR_CONV_MODE_LEADING_ZEROS, 2); + StringExpandPlaceholders(gStringVar4, sResultsTexts[page]); break; - case 2: - sp1C = r8; - sp20 = r8; - r2 = sp0C->unk98[r8].unkC; - if (r2 >= LAST_BERRY_INDEX - FIRST_BERRY_INDEX + 2) - r2 = 0; - StringCopy(gStringVar1, gBerries[r2].name); - StringExpandPlaceholders(gStringVar4, sBCRankingHeaders[sp10]); + case RESULTS_PAGE_CRUSHING: + playerId = i; + ranking = i; + j = game->players[i].berryId; + if (j >= LAST_BERRY_INDEX - FIRST_BERRY_INDEX + 2) + j = 0; + StringCopy(gStringVar1, gBerries[j].name); + StringExpandPlaceholders(gStringVar4, sResultsTexts[page]); break; } - r3 = GetStringRightAlignXOffset(2, gStringVar4, sp14 - 4); - AddTextPrinterParameterized3(sp0C->unk138.unk82, 2, r3, r6, sBerryCrushTextColorTable[0], 0, gStringVar4); - if (sp1C == sp0C->localId) + x = GetStringRightAlignXOffset(2, gStringVar4, sp14 - 4); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gStringVar4); + if (playerId == game->localId) StringCopy(gStringVar3, gText_1DotBlueF700); else StringCopy(gStringVar3, gText_1DotF700); - gStringVar3[0] = sp20 + CHAR_1; - DynamicPlaceholderTextUtil_SetPlaceholderPtr(0, sp0C->unk98[sp1C].unk0); + gStringVar3[0] = ranking + CHAR_1; + DynamicPlaceholderTextUtil_SetPlaceholderPtr(0, game->players[playerId].name); DynamicPlaceholderTextUtil_ExpandPlaceholders(gStringVar4, gStringVar3); - AddTextPrinterParameterized3(sp0C->unk138.unk82, 2, 4, r6, sBerryCrushTextColorTable[0], 0, gStringVar4); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, 4, y, sTextColorTable[COLORID_GREY], 0, gStringVar4); } } -static void sub_8021D34(struct BerryCrushGame *r8) +static void PrintCrushingResults(struct BerryCrushGame *game) { - u8 r10 = 0; - u8 r6 = 0; - u32 sp0C = 0; - struct BerryCrushGame_68 *sp10 = &r8->unk68; - u8 r7 = GetWindowAttribute(r8->unk138.unk82, WINDOW_HEIGHT) * 8 - 42; + u8 i = 0; + u8 x = 0; + u32 pressingSpeedFrac = 0; + struct BerryCrushGame_Results *results = &game->results; + u8 y = GetWindowAttribute(game->gfx.resultsWindowId, WINDOW_HEIGHT) * 8 - 42; - FramesToMinSec(&r8->unk138, sp10->unk04); - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[0], 0, gText_TimeColon); - r6 = 176 - (u8)GetStringWidth(2, gText_SpaceSec, -1); - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[0], 0, gText_SpaceSec); - ConvertIntToDecimalStringN(gStringVar1, r8->unk138.secondsInt, STR_CONV_MODE_LEADING_ZEROS, 2); - ConvertIntToDecimalStringN(gStringVar2, r8->unk138.secondsFrac, STR_CONV_MODE_LEADING_ZEROS, 2); + FramesToMinSec(&game->gfx, results->time); + + // Print time text + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gText_TimeColon); + + // Print seconds text + x = 176 - (u8)GetStringWidth(2, gText_SpaceSec, -1); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gText_SpaceSec); + + // Print seconds value + ConvertIntToDecimalStringN(gStringVar1, game->gfx.secondsInt, STR_CONV_MODE_LEADING_ZEROS, 2); + ConvertIntToDecimalStringN(gStringVar2, game->gfx.secondsFrac, STR_CONV_MODE_LEADING_ZEROS, 2); StringExpandPlaceholders(gStringVar4, gText_XDotY2); - r6 -= GetStringWidth(2, gStringVar4, -1); - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[0], 0, gStringVar4); - r6 -= GetStringWidth(2, gText_SpaceMin, -1); - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[0], 0, gText_SpaceMin); - ConvertIntToDecimalStringN(gStringVar1, r8->unk138.minutes, STR_CONV_MODE_LEADING_ZEROS, 1); + x -= GetStringWidth(2, gStringVar4, -1); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gStringVar4); + + // Print minutes text + x -= GetStringWidth(2, gText_SpaceMin, -1); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gText_SpaceMin); + + // Print minutes value + ConvertIntToDecimalStringN(gStringVar1, game->gfx.minutes, STR_CONV_MODE_LEADING_ZEROS, 1); StringExpandPlaceholders(gStringVar4, gText_StrVar1); - r6 -= GetStringWidth(2, gStringVar4, -1); - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[0], 0, gStringVar4); - r7 += 14; - AddTextPrinterParameterized3(r8->unk138.unk82, 2, 0, r7, sBerryCrushTextColorTable[0], 0, gText_PressingSpeed); - r6 = 176 - (u8)GetStringWidth(2, gText_TimesPerSec, -1); - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[0], 0, gText_TimesPerSec); - for (; r10 < 8; ++r10) - if (((u8)r8->pressingSpeed >> (7 - r10)) & 1) - sp0C += *(r10 + sPressingSpeedConversionTable); // It's accessed in a different way here for unknown reason - ConvertIntToDecimalStringN(gStringVar1, r8->pressingSpeed >> 8, STR_CONV_MODE_RIGHT_ALIGN, 3); - ConvertIntToDecimalStringN(gStringVar2, sp0C / 1000000, STR_CONV_MODE_LEADING_ZEROS, 2); + x -= GetStringWidth(2, gStringVar4, -1); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gStringVar4); + + // Print pressing speed text + y += 14; + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, 0, y, sTextColorTable[COLORID_GREY], 0, gText_PressingSpeed); + x = 176 - (u8)GetStringWidth(2, gText_TimesPerSec, -1); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gText_TimesPerSec); + + // Print pressing speed value + for (i = 0; i < 8; i++) + if (((u8)game->pressingSpeed >> (7 - i)) & 1) + pressingSpeedFrac += *(i + sPressingSpeedConversionTable); // It's accessed in a different way here for unknown reason + ConvertIntToDecimalStringN(gStringVar1, game->pressingSpeed >> 8, STR_CONV_MODE_RIGHT_ALIGN, 3); + ConvertIntToDecimalStringN(gStringVar2, pressingSpeedFrac / 1000000, STR_CONV_MODE_LEADING_ZEROS, 2); StringExpandPlaceholders(gStringVar4, gText_XDotY3); - r6 -= GetStringWidth(2, gStringVar4, -1); - if (r8->unk25_1) - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[5], 0, gStringVar4); + x -= GetStringWidth(2, gStringVar4, -1); + if (game->newRecord) + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_RED], 0, gStringVar4); else - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[0], 0, gStringVar4); - r7 += 14; - AddTextPrinterParameterized3(r8->unk138.unk82, 2, 0, r7, sBerryCrushTextColorTable[0], 0, gText_Silkiness); - ConvertIntToDecimalStringN(gStringVar1, sp10->unk08, STR_CONV_MODE_RIGHT_ALIGN, 3); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gStringVar4); + + // Print silkiness text + y += 14; + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, 0, y, sTextColorTable[COLORID_GREY], 0, gText_Silkiness); + + // Print silkiness value + ConvertIntToDecimalStringN(gStringVar1, results->silkiness, STR_CONV_MODE_RIGHT_ALIGN, 3); StringExpandPlaceholders(gStringVar4, gText_Var1Percent); - r6 = 176 - (u8)GetStringWidth(2, gStringVar4, -1); - AddTextPrinterParameterized3(r8->unk138.unk82, 2, r6, r7, sBerryCrushTextColorTable[0], 0, gStringVar4); + x = 176 - (u8)GetStringWidth(2, gStringVar4, -1); + AddTextPrinterParameterized3(game->gfx.resultsWindowId, 2, x, y, sTextColorTable[COLORID_GREY], 0, gStringVar4); } -static bool32 sub_8022070(struct BerryCrushGame *r4, struct BerryCrushGame_138 *r6) +static bool32 OpenResultsWindow(struct BerryCrushGame *game, struct BerryCrushGame_Gfx *gfx) { - u8 r5; + u8 playerCountIdx; struct WindowTemplate template; - switch (r6->unk80) + switch (gfx->resultsState) { case 0: - r5 = r4->playerCount - 2; - BerryCrush_HideTimerSprites(r6); - memcpy(&template, &gUnknown_082F3324[r4->gameState - 11], sizeof(struct WindowTemplate)); - if (r4->gameState == 13) - template.height = gUnknown_082F3344[1][r5]; + playerCountIdx = game->playerCount - 2; + HideTimer(gfx); + memcpy(&template, &sWindowTemplates_Results[game->gameState - RESULTS_STATE_START], sizeof(struct WindowTemplate)); + if (game->gameState == STATE_RESULTS_CRUSHING) + template.height = sResultsWindowHeights[1][playerCountIdx]; else - template.height = gUnknown_082F3344[0][r5]; - r6->unk82 = AddWindow(&template); + template.height = sResultsWindowHeights[0][playerCountIdx]; + gfx->resultsWindowId = AddWindow(&template); break; case 1: - PutWindowTilemap(r6->unk82); - FillWindowPixelBuffer(r6->unk82, PIXEL_FILL(0)); + PutWindowTilemap(gfx->resultsWindowId); + FillWindowPixelBuffer(gfx->resultsWindowId, PIXEL_FILL(0)); break; case 2: - LoadUserWindowBorderGfx_(r6->unk82, 541, 208); - DrawStdFrameWithCustomTileAndPalette(r6->unk82, 0, 541, 13); + LoadUserWindowBorderGfx_(gfx->resultsWindowId, 541, 208); + DrawStdFrameWithCustomTileAndPalette(gfx->resultsWindowId, 0, 541, 13); break; case 3: - r5 = r4->playerCount - 2; - switch (r4->gameState) + playerCountIdx = game->playerCount - 2; + switch (game->gameState) { - case 11: - PrintTextCentered(r6->unk82, 20, 3, gText_PressesRankings); - PrintBerryCrushResultWindow(r4, 0, 0xA0, 8 * gUnknown_082F3344[0][r5]); - r6->unk80 = 5; + case STATE_RESULTS_PRESSES: + PrintTextCentered(gfx->resultsWindowId, 20, COLORID_BLUE, gText_PressesRankings); + PrintResultsText(game, RESULTS_PAGE_PRESSES, 0xA0, 8 * sResultsWindowHeights[0][playerCountIdx]); + gfx->resultsState = 5; // Skip past Crushing Results text return FALSE; - case 12: - PrintTextCentered(r6->unk82, 20, 4, sBCRankingHeaders[r4->unk68.unk20[0][7] + 3]); - PrintBerryCrushResultWindow(r4, 1, 0xA0, 8 * gUnknown_082F3344[0][r5]); - r6->unk80 = 5; + case STATE_RESULTS_RANDOM: + PrintTextCentered(gfx->resultsWindowId, 20, COLORID_GREEN, sResultsTexts[game->results.randomPageId + NUM_RESULTS_PAGES]); + PrintResultsText(game, RESULTS_PAGE_RANDOM, 0xA0, 8 * sResultsWindowHeights[0][playerCountIdx]); + gfx->resultsState = 5; // Skip past Crushing Results text return FALSE; - case 13: - PrintTextCentered(r6->unk82, 22, 3, gText_CrushingResults); - PrintBerryCrushResultWindow(r4, 2, 0xB0, 8 * gUnknown_082F3344[1][r5]); + case STATE_RESULTS_CRUSHING: + PrintTextCentered(gfx->resultsWindowId, 22, COLORID_BLUE, gText_CrushingResults); + PrintResultsText(game, RESULTS_PAGE_CRUSHING, 0xB0, 8 * sResultsWindowHeights[1][playerCountIdx]); break; } break; case 4: - sub_8021D34(r4); + PrintCrushingResults(game); break; case 5: - CopyWindowToVram(r6->unk82, 3); - r6->unk80 = 0; + CopyWindowToVram(gfx->resultsWindowId, 3); + gfx->resultsState = 0; return TRUE; } - ++r6->unk80; + gfx->resultsState++; return FALSE; } -static void sub_802222C(struct BerryCrushGame *r4) +static void CloseResultsWindow(struct BerryCrushGame *game) { - ClearStdWindowAndFrameToTransparent(r4->unk138.unk82, 1); - RemoveWindow(r4->unk138.unk82); - sub_8022600(r4); + ClearStdWindowAndFrameToTransparent(game->gfx.resultsWindowId, 1); + RemoveWindow(game->gfx.resultsWindowId); + DrawPlayerNameWindows(game); } -static void Task_ShowBerryCrushRankings(u8 taskId) +#define tState data[0] +#define tWindowId data[1] +#define tPressingSpeeds(i) data[2 + (i)] // data[2]-[5], for different group sizes + +static void Task_ShowRankings(u8 taskId) { u8 i = 0, j, xPos, yPos; u32 score = 0; s16 *data = gTasks[taskId].data; - switch (data[0]) + switch (tState) { case 0: - data[1] = AddWindow(&sWindowTemplate_BerryCrushRankings); - PutWindowTilemap(data[1]); - FillWindowPixelBuffer(data[1], PIXEL_FILL(0)); - LoadUserWindowBorderGfx_(data[1], 541, 208); - DrawStdFrameWithCustomTileAndPalette(data[1], 0, 541, 13); + tWindowId = AddWindow(&sWindowTemplate_Rankings); + PutWindowTilemap(tWindowId); + FillWindowPixelBuffer(tWindowId, PIXEL_FILL(0)); + LoadUserWindowBorderGfx_(tWindowId, 541, 208); + DrawStdFrameWithCustomTileAndPalette(tWindowId, 0, 541, 13); break; case 1: + // Print header text xPos = 96 - GetStringWidth(1, gText_BerryCrush2, -1) / 2u; - AddTextPrinterParameterized3( - data[1], - 1, - xPos, - 1, - sBerryCrushTextColorTable[3], - 0, - gText_BerryCrush2 - ); + AddTextPrinterParameterized3(tWindowId, 1, xPos, 1, sTextColorTable[COLORID_BLUE], 0, gText_BerryCrush2); xPos = 96 - GetStringWidth(1, gText_PressingSpeedRankings, -1) / 2u; - AddTextPrinterParameterized3( - data[1], - 1, - xPos, - 17, - sBerryCrushTextColorTable[3], - 0, - gText_PressingSpeedRankings - ); + AddTextPrinterParameterized3(tWindowId, 1, xPos, 17, sTextColorTable[COLORID_BLUE], 0, gText_PressingSpeedRankings); + + // Print pressing speed record for each group size, ranked yPos = 41; - for (i = 0; i < 4; ++i) + for (i = 0; i < MAX_RFU_PLAYERS - 1; i++) { ConvertIntToDecimalStringN(gStringVar1, i + 2, STR_CONV_MODE_LEFT_ALIGN, 1); StringExpandPlaceholders(gStringVar4, gText_Var1Players); - AddTextPrinterParameterized3( - data[1], - 1, - 0, - yPos, - sBerryCrushTextColorTable[0], - 0, - gStringVar4 - ); + AddTextPrinterParameterized3(tWindowId, 1, 0, yPos, sTextColorTable[COLORID_GREY], 0, gStringVar4); xPos = 192 - (u8)GetStringWidth(1, gText_TimesPerSec, -1); - AddTextPrinterParameterized3( - data[1], - 1, - xPos, - yPos, - sBerryCrushTextColorTable[0], - 0, - gText_TimesPerSec - ); - for (j = 0; j < 8; ++j) + AddTextPrinterParameterized3(tWindowId, 1, xPos, yPos, sTextColorTable[COLORID_GREY], 0, gText_TimesPerSec); + for (j = 0; j < 8; j++) { - if (((data[i + 2] & 0xFF) >> (7 - j)) & 1) + if (((tPressingSpeeds(i) & 0xFF) >> (7 - j)) & 1) score += sPressingSpeedConversionTable[j]; } - ConvertIntToDecimalStringN(gStringVar1, (u16)data[i + 2] >> 8, STR_CONV_MODE_RIGHT_ALIGN, 3); + ConvertIntToDecimalStringN(gStringVar1, (u16)tPressingSpeeds(i) >> 8, STR_CONV_MODE_RIGHT_ALIGN, 3); ConvertIntToDecimalStringN(gStringVar2, score / 1000000, STR_CONV_MODE_LEADING_ZEROS, 2); StringExpandPlaceholders(gStringVar4, gText_XDotY3); xPos -= GetStringWidth(1, gStringVar4, -1); - AddTextPrinterParameterized3( - data[1], - 1, - xPos, - yPos, - sBerryCrushTextColorTable[0], - 0, - gStringVar4 - ); + AddTextPrinterParameterized3(tWindowId, 1, xPos, yPos, sTextColorTable[COLORID_GREY], 0, gStringVar4); yPos += 16; score = 0; } - CopyWindowToVram(data[1], 3); + CopyWindowToVram(tWindowId, 3); break; case 2: if (JOY_NEW(A_BUTTON | B_BUTTON)) @@ -1659,16 +1849,16 @@ static void Task_ShowBerryCrushRankings(u8 taskId) else return; case 3: - ClearStdWindowAndFrameToTransparent(data[1], 1); - ClearWindowTilemap(data[1]); - RemoveWindow(data[1]); + ClearStdWindowAndFrameToTransparent(tWindowId, 1); + ClearWindowTilemap(tWindowId); + RemoveWindow(tWindowId); DestroyTask(taskId); EnableBothScriptContexts(); ScriptContext2_Disable(); - data[0] = 0; + tState = 0; return; } - ++data[0]; + tState++; } void ShowBerryCrushRankings(void) @@ -1676,97 +1866,98 @@ void ShowBerryCrushRankings(void) u8 taskId; ScriptContext2_Enable(); - taskId = CreateTask(Task_ShowBerryCrushRankings, 0); - gTasks[taskId].data[2] = gSaveBlock2Ptr->berryCrush.berryCrushResults[0]; - gTasks[taskId].data[3] = gSaveBlock2Ptr->berryCrush.berryCrushResults[1]; - gTasks[taskId].data[4] = gSaveBlock2Ptr->berryCrush.berryCrushResults[2]; - gTasks[taskId].data[5] = gSaveBlock2Ptr->berryCrush.berryCrushResults[3]; + taskId = CreateTask(Task_ShowRankings, 0); + gTasks[taskId].tPressingSpeeds(0) = gSaveBlock2Ptr->berryCrush.pressingSpeeds[0]; + gTasks[taskId].tPressingSpeeds(1) = gSaveBlock2Ptr->berryCrush.pressingSpeeds[1]; + gTasks[taskId].tPressingSpeeds(2) = gSaveBlock2Ptr->berryCrush.pressingSpeeds[2]; + gTasks[taskId].tPressingSpeeds(3) = gSaveBlock2Ptr->berryCrush.pressingSpeeds[3]; } -static void BerryCrush_PrintTimeOnSprites(struct BerryCrushGame_138 *r4, u16 r1) +static void PrintTimer(struct BerryCrushGame_Gfx *gfx, u16 timer) { - FramesToMinSec(r4, r1); - DigitObjUtil_PrintNumOn(0, r4->minutes); - DigitObjUtil_PrintNumOn(1, r4->secondsInt); - DigitObjUtil_PrintNumOn(2, r4->secondsFrac); + FramesToMinSec(gfx, timer); + DigitObjUtil_PrintNumOn(0, gfx->minutes); + DigitObjUtil_PrintNumOn(1, gfx->secondsInt); + DigitObjUtil_PrintNumOn(2, gfx->secondsFrac); } -static void BerryCrush_HideTimerSprites(struct BerryCrushGame_138 *r0) +static void HideTimer(struct BerryCrushGame_Gfx *gfx) { - r0->timerSprites[0]->invisible = TRUE; - r0->timerSprites[1]->invisible = TRUE; + gfx->timerSprites[0]->invisible = TRUE; + gfx->timerSprites[1]->invisible = TRUE; DigitObjUtil_HideOrShow(2, 1); DigitObjUtil_HideOrShow(1, 1); DigitObjUtil_HideOrShow(0, 1); } -static void sub_8022588(struct BerryCrushGame *r5) +static void CreatePlayerNameWindows(struct BerryCrushGame *game) { - u8 r6; - - for (r6 = 0; r6 < r5->playerCount; ++r6) + u8 i; + for (i = 0; i < game->playerCount; i++) { - r5->unk138.seatCoords[r6] = &gUnknown_082F4190[gUnknown_082F417C[r5->playerCount - 2][r6]]; - r5->unk138.unk83[r6] = AddWindow(&gUnknown_082F32F4[r5->unk138.seatCoords[r6]->unk0]); - PutWindowTilemap(r5->unk138.unk83[r6]); - FillWindowPixelBuffer(r5->unk138.unk83[r6], 0); + game->gfx.playerCoords[i] = &sPlayerCoords[sPlayerIdToPosId[game->playerCount - 2][i]]; + game->gfx.nameWindowIds[i] = AddWindow(&sWindowTemplates_PlayerNames[game->gfx.playerCoords[i]->playerId]); + PutWindowTilemap(game->gfx.nameWindowIds[i]); + FillWindowPixelBuffer(game->gfx.nameWindowIds[i], 0); } } -static void sub_8022600(struct BerryCrushGame *r6) +static void DrawPlayerNameWindows(struct BerryCrushGame *game) { - u8 r7; - - for (r7 = 0; r7 < r6->playerCount; ++r7) + u8 i; + for (i = 0; i < game->playerCount; i++) { - PutWindowTilemap(r6->unk138.unk83[r7]); - if (r7 == r6->localId) + PutWindowTilemap(game->gfx.nameWindowIds[i]); + if (i == game->localId) { + // Print the player's name AddTextPrinterParameterized4( - r6->unk138.unk83[r7], + game->gfx.nameWindowIds[i], 2, - 36 - GetStringWidth(2, r6->unk98[r7].unk0, 0) / 2u, + 36 - GetStringWidth(2, game->players[i].name, 0) / 2u, 1, 0, 0, - sBerryCrushTextColorTable[1], + sTextColorTable[COLORID_BLACK], 0, - r6->unk98[r7].unk0 + game->players[i].name ); } else { + // Print a partner's name AddTextPrinterParameterized4( - r6->unk138.unk83[r7], + game->gfx.nameWindowIds[i], 2, - 36 - GetStringWidth(2, r6->unk98[r7].unk0, 0) / 2u, + 36 - GetStringWidth(2, game->players[i].name, 0) / 2u, 1, 0, 0, - sBerryCrushTextColorTable[2], + sTextColorTable[COLORID_LIGHT_GREY], 0, - r6->unk98[r7].unk0 + game->players[i].name ); } - CopyWindowToVram(r6->unk138.unk83[r7], 3); + CopyWindowToVram(game->gfx.nameWindowIds[i], 3); } CopyBgTilemapBufferToVram(0); } -static void sub_80226D0(struct BerryCrushGame *r6) +// Each player name window border uses a color that corresponds to a slot of the crusher lid +static void CopyPlayerNameWindowGfxToBg(struct BerryCrushGame *game) { - u8 r5 = 0; - u8 * r4; + u8 i = 0; + u8 * crusherGfx; - LZ77UnCompWram(gUnknown_08DE3FD4, gDecompressionBuffer); + LZ77UnCompWram(gBerryCrush_Crusher_Tilemap, gDecompressionBuffer); - for (r4 = gDecompressionBuffer; r5 < r6->playerCount; ++r5) + for (crusherGfx = gDecompressionBuffer; i < game->playerCount; i++) { CopyToBgTilemapBufferRect( 3, - &r4[r6->unk138.seatCoords[r5]->unk0 * 40], - r6->unk138.seatCoords[r5]->unk1, - r6->unk138.seatCoords[r5]->unk2, + &crusherGfx[game->gfx.playerCoords[i]->playerId * 40], + game->gfx.playerCoords[i]->windowGfxX, + game->gfx.playerCoords[i]->windowGfxY, 10, 2 ); @@ -1774,96 +1965,99 @@ static void sub_80226D0(struct BerryCrushGame *r6) CopyBgTilemapBufferToVram(3); } -static void sub_8022730(struct BerryCrushGame *r6) +static void CreateGameSprites(struct BerryCrushGame *game) { - u8 r5 = 0; - u8 r2; + u8 i = 0; + u8 spriteId; - r6->depth = -104; - r6->vibration = 0; + game->depth = CRUSHER_START_Y; + game->vibration = 0; gSpriteCoordOffsetX = 0; - gSpriteCoordOffsetY = -104; - for (; r5 < 4; ++r5) - LoadCompressedSpriteSheet(&gUnknown_082F41F4[r5]); + gSpriteCoordOffsetY = CRUSHER_START_Y; + for (i = 0; i < ARRAY_COUNT(sSpriteSheets) - 1; i++) + LoadCompressedSpriteSheet(&sSpriteSheets[i]); LoadSpritePalettes(sSpritePals); - r2 = CreateSprite(&sSpriteTemplate_BerryCrushCore, 120, 88, 5); - r6->unk138.coreSprite = &gSprites[r2]; - r6->unk138.coreSprite->oam.priority = 3; - r6->unk138.coreSprite->coordOffsetEnabled = TRUE; - r6->unk138.coreSprite->animPaused = TRUE; - for (r5 = 0; r5 < r6->playerCount; ++r5) + + // Create sprite for crusher base + spriteId = CreateSprite(&sSpriteTemplate_CrusherBase, 120, 88, 5); + game->gfx.coreSprite = &gSprites[spriteId]; + game->gfx.coreSprite->oam.priority = 3; + game->gfx.coreSprite->coordOffsetEnabled = TRUE; + game->gfx.coreSprite->animPaused = TRUE; + + // Create sprites for the impact effect + for (i = 0; i < game->playerCount; i++) { - r2 = CreateSprite( - &sSpriteTemplate_BerryCrushImpact, - r6->unk138.seatCoords[r5]->unk4 + 120, - r6->unk138.seatCoords[r5]->unk6 + 32, + spriteId = CreateSprite( + &sSpriteTemplate_Impact, + game->gfx.playerCoords[i]->impactXOffset + 120, + game->gfx.playerCoords[i]->impactYOffset + 32, 0 ); - r6->unk138.impactSprites[r5] = &gSprites[r2]; - r6->unk138.impactSprites[r5]->oam.priority = 1; - r6->unk138.impactSprites[r5]->invisible = TRUE; - r6->unk138.impactSprites[r5]->coordOffsetEnabled = TRUE; - r6->unk138.impactSprites[r5]->animPaused = TRUE; + game->gfx.impactSprites[i] = &gSprites[spriteId]; + game->gfx.impactSprites[i]->oam.priority = 1; + game->gfx.impactSprites[i]->invisible = TRUE; + game->gfx.impactSprites[i]->coordOffsetEnabled = TRUE; + game->gfx.impactSprites[i]->animPaused = TRUE; } - for (r5 = 0; r5 < ARRAY_COUNT(r6->unk138.sparkleSprites); ++r5) + + // Create sprites for sparkle effect + for (i = 0; i < ARRAY_COUNT(game->gfx.sparkleSprites); i++) { - r2 = CreateSprite( - &sSpriteTemplate_BerryCrushPowderSparkles, - gUnknown_082F41D2[r5][0] + 120, - gUnknown_082F41D2[r5][1] + 136, + spriteId = CreateSprite( + &sSpriteTemplate_Sparkle, + sSparkleCoords[i][0] + 120, + sSparkleCoords[i][1] + 136, 6 ); - r6->unk138.sparkleSprites[r5] = &gSprites[r2]; - r6->unk138.sparkleSprites[r5]->oam.priority = 3; - r6->unk138.sparkleSprites[r5]->invisible = TRUE; - r6->unk138.sparkleSprites[r5]->animPaused = TRUE; - r6->unk138.sparkleSprites[r5]->data[0] = r5; + game->gfx.sparkleSprites[i] = &gSprites[spriteId]; + game->gfx.sparkleSprites[i]->oam.priority = 3; + game->gfx.sparkleSprites[i]->invisible = TRUE; + game->gfx.sparkleSprites[i]->animPaused = TRUE; + game->gfx.sparkleSprites[i]->data[0] = i; } - for (r5 = 0; r5 < ARRAY_COUNT(r6->unk138.timerSprites); ++r5) + + // Create sprites for timer + for (i = 0; i < ARRAY_COUNT(game->gfx.timerSprites); i++) { - r2 = CreateSprite( - &sSpriteTemplate_BerryCrushTimer, - 24 * r5 + 176, - 8, - 0 - ); - r6->unk138.timerSprites[r5] = &gSprites[r2]; - r6->unk138.timerSprites[r5]->oam.priority = 0; - r6->unk138.timerSprites[r5]->invisible = FALSE; - r6->unk138.timerSprites[r5]->animPaused = FALSE; + spriteId = CreateSprite(&sSpriteTemplate_Timer, 24 * i + 176, 8, 0); + game->gfx.timerSprites[i] = &gSprites[spriteId]; + game->gfx.timerSprites[i]->oam.priority = 0; + game->gfx.timerSprites[i]->invisible = FALSE; + game->gfx.timerSprites[i]->animPaused = FALSE; } DigitObjUtil_CreatePrinter(0, 0, &sDigitObjTemplates[0]); DigitObjUtil_CreatePrinter(1, 0, &sDigitObjTemplates[1]); DigitObjUtil_CreatePrinter(2, 0, &sDigitObjTemplates[2]); - if (r6->gameState == 1) - BerryCrush_HideTimerSprites(&r6->unk138); + + if (game->gameState == STATE_INIT) + HideTimer(&game->gfx); } -static void sub_8022960(struct BerryCrushGame *r5) +static void DestroyGameSprites(struct BerryCrushGame *game) { - u8 r4 = 0; - - FreeSpriteTilesByTag(4); - FreeSpriteTilesByTag(3); - FreeSpriteTilesByTag(2); - FreeSpriteTilesByTag(1); - FreeSpritePaletteByTag(4); - FreeSpritePaletteByTag(2); - FreeSpritePaletteByTag(1); - for (; r4 < ARRAY_COUNT(r5->unk138.timerSprites); ++r4) - DestroySprite(r5->unk138.timerSprites[r4]); + u8 i = 0; + FreeSpriteTilesByTag(TAG_TIMER_DIGITS); + FreeSpriteTilesByTag(GFXTAG_SPARKLE); + FreeSpriteTilesByTag(GFXTAG_IMPACT); + FreeSpriteTilesByTag(TAG_CRUSHER_BASE); + FreeSpritePaletteByTag(TAG_TIMER_DIGITS); + FreeSpritePaletteByTag(PALTAG_EFFECT); + FreeSpritePaletteByTag(TAG_CRUSHER_BASE); + for (i = 0; i < ARRAY_COUNT(game->gfx.timerSprites); i++) + DestroySprite(game->gfx.timerSprites[i]); DigitObjUtil_DeletePrinter(2); DigitObjUtil_DeletePrinter(1); DigitObjUtil_DeletePrinter(0); - for (r4 = 0; r4 < ARRAY_COUNT(r5->unk138.sparkleSprites); ++r4) - DestroySprite(r5->unk138.sparkleSprites[r4]); - for (r4 = 0; r4 < r5->playerCount; ++r4) - DestroySprite(r5->unk138.impactSprites[r4]); - if (r5->unk138.coreSprite->inUse) - DestroySprite(r5->unk138.coreSprite); + for (i = 0; i < ARRAY_COUNT(game->gfx.sparkleSprites); i++) + DestroySprite(game->gfx.sparkleSprites[i]); + for (i = 0; i < game->playerCount; i++) + DestroySprite(game->gfx.impactSprites[i]); + if (game->gfx.coreSprite->inUse) + DestroySprite(game->gfx.coreSprite); } -static void SpriteCB_BerryCrushImpact(struct Sprite *sprite) +static void SpriteCB_Impact(struct Sprite *sprite) { if (sprite->animEnded) { @@ -1872,93 +2066,116 @@ static void SpriteCB_BerryCrushImpact(struct Sprite *sprite) } } -static void sub_8022A4C(struct Sprite *sprite) +static void SpriteCB_Sparkle_End(struct Sprite *sprite) { - u8 r1 = 0; - SpriteCallback r5 = SpriteCallbackDummy; - - for (; r1 < ARRAY_COUNT(sprite->data); ++r1) - sprite->data[r1] = 0; + u8 i; + for (i = 0; i < ARRAY_COUNT(sprite->data); i++) + sprite->data[i] = 0; sprite->pos2.x = 0; sprite->pos2.y = 0; sprite->invisible = TRUE; sprite->animPaused = TRUE; - sprite->callback = r5; + sprite->callback = SpriteCallbackDummy; } -static void sub_8022A94(struct Sprite *sprite) -{ - s16 *r4 = sprite->data; +#define sX data[0] +#define sYSpeed data[1] +#define sYAccel data[2] +#define sXSpeed data[3] +#define sSinIdx data[4] +#define sSinSpeed data[5] +#define sAmplitude data[6] +// The last element (data[7]) is a bitfield. +// The first 15 bits are the y coord to stop at. +// The last bit is a flag for whether or not to move on the x too +#define sBitfield data[7] +#define MASK_TARGET_Y 0x7FFF +#define F_MOVE_HORIZ 0x8000 - r4[1] += r4[2]; - sprite->pos2.y += r4[1] >> 8; - if (r4[7] & 0x8000) +static void SpriteCB_Sparkle(struct Sprite *sprite) +{ + s16 *data = sprite->data; + + sYSpeed += sYAccel; + sprite->pos2.y += sYSpeed >> 8; + if (sBitfield & F_MOVE_HORIZ) { - sprite->data[0] += r4[3]; - r4[4] += r4[5]; - sprite->pos2.x = Sin(r4[4] >> 7, r4[6]); - if (r4[7] & 0x8000 && r4[4] >> 7 > 126) + sprite->sX += sXSpeed; + sSinIdx += sSinSpeed; + sprite->pos2.x = Sin(sSinIdx >> 7, sAmplitude); + if (sBitfield & F_MOVE_HORIZ && sSinIdx >> 7 > 126) { sprite->pos2.x = 0; - r4[7] &= 0x7FFF; + sBitfield &= MASK_TARGET_Y; } } - sprite->pos1.x = r4[0] >> 7; - if (sprite->pos1.y + sprite->pos2.y > (r4[7] & 0x7FFF)) - sprite->callback = sub_8022A4C; + sprite->pos1.x = sX >> 7; + if (sprite->pos1.y + sprite->pos2.y > (sBitfield & MASK_TARGET_Y)) + sprite->callback = SpriteCB_Sparkle_End; } -static void sub_8022B28(struct Sprite *sprite) +static void SpriteCB_Sparkle_Init(struct Sprite *sprite) { - s16 *r7 = sprite->data; - s16 r4, r5; - s32 r2; - u32 r8 = 0; + s16 *data = sprite->data; + s16 xMult, xDiv; + s32 var; + u32 zero = 0; - r2 = 640; - r7[1] = r2; - r7[2] = 32; - r7[7] = 168; - r4 = sprite->pos2.x * 128; - r5 = MathUtil_Div16Shift(7, (168 - sprite->pos1.y) << 7, (r2 + 32) >> 1); - sprite->data[0] = sprite->pos1.x << 7; - r7[3] = MathUtil_Div16Shift(7, r4, r5); - r2 = MathUtil_Mul16Shift(7, r5, 85); - r7[4] = r8; - r7[5] = MathUtil_Div16Shift(7, Q_8_8(63.5), r2); - r7[6] = sprite->pos2.x / 4; - r7[7] |= 0x8000; - sprite->pos2.y = r8; - sprite->pos2.x = r8; - sprite->callback = sub_8022A94; + var = 640; + sYSpeed = var; + sYAccel = 32; + sBitfield = 168; // Setting bits in MASK_TARGET_Y + xMult = sprite->pos2.x * 128; + xDiv = MathUtil_Div16Shift(7, (168 - sprite->pos1.y) << 7, (var + 32) >> 1); + sprite->sX = sprite->pos1.x << 7; + sXSpeed = MathUtil_Div16Shift(7, xMult, xDiv); + var = MathUtil_Mul16Shift(7, xDiv, 85); + sSinIdx = zero; + sSinSpeed = MathUtil_Div16Shift(7, Q_8_8(63.5), var); + sAmplitude = sprite->pos2.x / 4; + sBitfield |= F_MOVE_HORIZ; + sprite->pos2.y = zero; + sprite->pos2.x = zero; + sprite->callback = SpriteCB_Sparkle; sprite->animPaused = FALSE; sprite->invisible = FALSE; } -static void BerryCrush_RunOrScheduleCommand(u16 r5, u8 r4, u8 *r7) -{ - struct BerryCrushGame *r6 = GetBerryCrushGame(); +#undef sX +#undef sYSpeed +#undef sYAccel +#undef sXSpeed +#undef sSinIdx +#undef sSinSpeed +#undef sAmplitude +#undef sBitfield +#undef MASK_TARGET_Y +#undef F_MOVE_HORIZ - if (r5 >= ARRAY_COUNT(sBerryCrushCommands)) - r5 = 0; - switch (r4) +static void RunOrScheduleCommand(u16 cmdId, u8 mode, u8 *args) +{ + struct BerryCrushGame *game = GetBerryCrushGame(); + + if (cmdId >= ARRAY_COUNT(sBerryCrushCommands)) + cmdId = CMD_NONE; + switch (mode) { - case 0: - if (r5 != 0) - sBerryCrushCommands[r5](r6, r7); - if (r6->nextCmd >= ARRAY_COUNT(sBerryCrushCommands)) - r6->nextCmd = r4; - r6->cmdCallback = sBerryCrushCommands[r6->nextCmd]; + case RUN_CMD: + if (cmdId != CMD_NONE) + sBerryCrushCommands[cmdId](game, args); + if (game->nextCmd >= ARRAY_COUNT(sBerryCrushCommands)) + game->nextCmd = CMD_NONE; + game->cmdCallback = sBerryCrushCommands[game->nextCmd]; break; - case 1: - r6->cmdCallback = sBerryCrushCommands[r5]; + case SCHEDULE_CMD: + game->cmdCallback = sBerryCrushCommands[cmdId]; break; } } -static u32 BerryCrushCommand_BeginNormalPaletteFade(struct BerryCrushGame *game, u8 *params) +static u32 Cmd_BeginNormalPaletteFade(struct BerryCrushGame *game, u8 *args) { - // params points to packed values: + // args points to packed values: // bytes 0-3: selectedPals (bitfield) // byte 4: delay // byte 5: startY @@ -1969,126 +2186,128 @@ static u32 BerryCrushCommand_BeginNormalPaletteFade(struct BerryCrushGame *game, u16 color; u32 selectedPals[2]; - selectedPals[0] = (u32)params[0]; - selectedPals[1] = (u32)params[1]; + selectedPals[0] = (u32)args[0]; + selectedPals[1] = (u32)args[1]; selectedPals[1] <<= 8; selectedPals[0] |= selectedPals[1]; - selectedPals[1] = (u32)params[2]; + selectedPals[1] = (u32)args[2]; selectedPals[1] <<= 16; selectedPals[0] |= selectedPals[1]; - selectedPals[1] = (u32)params[3]; + selectedPals[1] = (u32)args[3]; selectedPals[1] <<= 24; selectedPals[0] |= selectedPals[1]; - params[0] = params[9]; + args[0] = args[9]; - color = params[8]; + color = args[8]; color <<= 8; - color |= params[7]; + color |= args[7]; gPaletteFade.bufferTransferDisabled = FALSE; - BeginNormalPaletteFade(selectedPals[0], params[4], params[5], params[6], color); + BeginNormalPaletteFade(selectedPals[0], args[4], args[5], args[6], color); UpdatePaletteFade(); - game->nextCmd = 2; + game->nextCmd = CMD_WAIT_FADE; return 0; } -static u32 BerryCrushCommand_WaitPaletteFade(struct BerryCrushGame *r4, u8 *r5) +static u32 Cmd_WaitPaletteFade(struct BerryCrushGame *game, u8 *args) { - switch (r4->cmdState) + switch (game->cmdState) { case 0: if (UpdatePaletteFade()) return 0; - if(r5[0] != 0) - ++r4->cmdState; + if(args[0] != 0) + game->cmdState++; else - r4->cmdState = 3; + game->cmdState = 3; return 0; case 1: Rfu_SetLinkStandbyCallback(); - ++r4->cmdState; + game->cmdState++; return 0; case 2: if (IsLinkTaskFinished()) { - ++r4->cmdState; + game->cmdState++; return 0; } return 0; case 3: - BerryCrush_RunOrScheduleCommand(r4->afterPalFadeCmd, 1, NULL); - r4->cmdState = 0; + RunOrScheduleCommand(game->afterPalFadeCmd, SCHEDULE_CMD, NULL); + game->cmdState = 0; return 0; default: - ++r4->cmdState; + game->cmdState++; return 0; } } -static u32 BerryCrushCommand_PrintMessage(struct BerryCrushGame *r7, u8 *r5) +static u32 Cmd_PrintMessage(struct BerryCrushGame *game, u8 *args) { - u16 r4 = r5[3]; + u16 keys = args[3]; + keys <<= 8; + keys |= args[2]; - r4 <<= 8; - r4 |= r5[2]; - switch (r7->cmdState) + switch (game->cmdState) { case 0: DrawDialogueFrame(0, 0); - if (r5[1] & 2) + if (args[1] & F_MSG_EXPAND) { - StringExpandPlaceholders(gStringVar4, sBerryCrushMessages[r5[0]]); - AddTextPrinterParameterized2(0, 1, gStringVar4, r7->textSpeed, 0, 2, 1, 3); + StringExpandPlaceholders(gStringVar4, sMessages[args[0]]); + AddTextPrinterParameterized2(0, 1, gStringVar4, game->textSpeed, 0, 2, 1, 3); } else { - AddTextPrinterParameterized2(0, 1, sBerryCrushMessages[r5[0]], r7->textSpeed, 0, 2, 1, 3); + AddTextPrinterParameterized2(0, 1, sMessages[args[0]], game->textSpeed, 0, 2, 1, 3); } CopyWindowToVram(0, 3); break; case 1: if (!IsTextPrinterActive(0)) { - if (r4 == 0) - ++r7->cmdState; + // If no wait keys are given, skip + // waiting state below + if (keys == 0) + game->cmdState++; break; } return 0; case 2: - if (!(r4 & gMain.newKeys)) + if (!JOY_NEW(keys)) return 0; break; case 3: - if (r5[1] & 1) + if (args[1] & F_MSG_CLEAR) ClearDialogWindowAndFrame(0, 1); - BerryCrush_RunOrScheduleCommand(r7->nextCmd, 1, NULL); - r7->cmdState = r5[4]; + RunOrScheduleCommand(game->nextCmd, SCHEDULE_CMD, NULL); + game->cmdState = args[4]; return 0; } - ++r7->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_InitGfx(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_ShowGameDisplay(struct BerryCrushGame *game, u8 *args) { - if (InitBerryCrushDisplay() != 0) - BerryCrush_RunOrScheduleCommand(r4->nextCmd, 0, r4->commandParams); + if (ShowGameDisplay()) + RunOrScheduleCommand(game->nextCmd, RUN_CMD, game->commandArgs); return 0; } -static u32 BerryCrushCommand_TeardownGfx(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_HideGameDisplay(struct BerryCrushGame *game, u8 *args) { - if (BerryCrush_TeardownBgs() != 0) - BerryCrush_RunOrScheduleCommand(r4->nextCmd, 0, r4->commandParams); + if (HideGameDisplay()) + RunOrScheduleCommand(game->nextCmd, RUN_CMD, game->commandArgs); return 0; } -static u32 BerryCrushCommand_SignalReadyToBegin(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_SignalReadyToBegin(struct BerryCrushGame *game, u8 *args) { - switch (r4->cmdState) + switch (game->cmdState) { case 0: Rfu_SetLinkStandbyCallback(); @@ -2097,55 +2316,55 @@ static u32 BerryCrushCommand_SignalReadyToBegin(struct BerryCrushGame *r4, __att if (IsLinkTaskFinished()) { PlayNewMapMusic(MUS_RG_GAME_CORNER); - BerryCrush_RunOrScheduleCommand(7, 1, NULL); - r4->gameState = 3; - r4->cmdState = 0; + RunOrScheduleCommand(CMD_ASK_PICK_BERRY, SCHEDULE_CMD, NULL); + game->gameState = STATE_PICK_BERRY; + game->cmdState = 0; } return 0; } - ++r4->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_AskPickBerry(struct BerryCrushGame *r4, u8 *r5) +static u32 Cmd_AskPickBerry(struct BerryCrushGame *game, u8 *args) { - switch (r4->cmdState) + switch (game->cmdState) { default: - ++r4->cmdState; + game->cmdState++; break; case 0: - sub_8024578(r4); - BerryCrush_SetShowMessageParams(r5, 0, 1, 0, 1); - r4->nextCmd = 7; - BerryCrush_RunOrScheduleCommand(3, 1, NULL); + ResetGame(game); + SetPrintMessageArgs(args, MSG_PICK_BERRY, F_MSG_CLEAR, 0, 1); + game->nextCmd = CMD_ASK_PICK_BERRY; + RunOrScheduleCommand(CMD_PRINT_MSG, SCHEDULE_CMD, NULL); break; case 1: - r4->nextCmd = 8; - BerryCrush_RunOrScheduleCommand(5, 1, NULL); - r4->cmdState = 2; + game->nextCmd = CMD_PICK_BERRY; + RunOrScheduleCommand(CMD_HIDE_GAME, SCHEDULE_CMD, NULL); + game->cmdState = 2; break; } return 0; } -static u32 BerryCrushCommand_GoToBerryPouch(struct BerryCrushGame *r0, __attribute__((unused)) u8 *r1) +static u32 Cmd_GoToBerryPouch(struct BerryCrushGame *game, u8 *args) { - r0->cmdCallback = NULL; - SetMainCallback2(BerryCrush_SetupMainTask); + game->cmdCallback = NULL; + SetMainCallback2(ChooseBerry); return 0; } -static u32 BerryCrushCommand_WaitForOthersToPickBerries(struct BerryCrushGame *r5, u8 *r2) +static u32 Cmd_WaitForOthersToPickBerries(struct BerryCrushGame *game, u8 *args) { - u8 r3; + u8 i; - switch (r5->cmdState) + switch (game->cmdState) { case 0: - BerryCrush_SetShowMessageParams(r2, 1, 0, 0, 1); - r5->nextCmd = 9; - BerryCrush_RunOrScheduleCommand(3, 1, NULL); + SetPrintMessageArgs(args, MSG_WAIT_PICK, 0, 0, 1); + game->nextCmd = CMD_WAIT_BERRIES; + RunOrScheduleCommand(CMD_PRINT_MSG, SCHEDULE_CMD, NULL); return 0; case 1: Rfu_SetLinkStandbyCallback(); @@ -2153,126 +2372,131 @@ static u32 BerryCrushCommand_WaitForOthersToPickBerries(struct BerryCrushGame *r case 2: if (!IsLinkTaskFinished()) return 0; - memset(r5->sendCmd, 0, sizeof(r5->sendCmd)); - r5->sendCmd[0] = r5->unk98[r5->localId].unkC; - SendBlock(0, r5->sendCmd, 2); + + // Send player's chosen berry to partners + memset(game->sendCmd, 0, sizeof(game->sendCmd)); + game->sendCmd[0] = game->players[game->localId].berryId; + SendBlock(0, game->sendCmd, 2); break; case 3: if (!IsLinkTaskFinished()) return 0; - r5->unk10 = 0; + game->cmdTimer = 0; break; case 4: - if (GetBlockReceivedStatus() != sReceivedPlayerBitmasks[r5->playerCount - 2]) + // Wait for partners responses + if (GetBlockReceivedStatus() != sReceivedPlayerBitmasks[game->playerCount - 2]) return 0; - for (r3 = 0; r3 < r5->playerCount; ++r3) + + // Read partners chosen berries + for (i = 0; i < game->playerCount; i++) { - r5->unk98[r3].unkC = gBlockRecvBuffer[r3][0]; - if (r5->unk98[r3].unkC > 0xB0) - r5->unk98[r3].unkC = 0; - r5->unk18 += gUnknown_0858AB24[r5->unk98[r3].unkC].unk0; - r5->powder += gUnknown_0858AB24[r5->unk98[r3].unkC].unk1; + game->players[i].berryId = gBlockRecvBuffer[i][0]; + if (game->players[i].berryId > LAST_BERRY_INDEX + 1) + game->players[i].berryId = 0; + game->targetAPresses += gBerryCrush_BerryData[game->players[i].berryId].difficulty; + game->powder += gBerryCrush_BerryData[game->players[i].berryId].powder; } - r5->unk10 = 0; + game->cmdTimer = 0; ResetBlockReceivedFlags(); - r5->unk20 = MathUtil_Div32(Q_24_8(r5->unk18), Q_24_8(32)); + game->targetDepth = MathUtil_Div32(Q_24_8(game->targetAPresses), Q_24_8(32)); break; case 5: ClearDialogWindowAndFrame(0, 1); - BerryCrush_RunOrScheduleCommand(10, 1, NULL); - r5->gameState = 4; - r5->cmdState = 0; + RunOrScheduleCommand(CMD_DROP_BERRIES, SCHEDULE_CMD, NULL); + game->gameState = STATE_DROP_BERRIES; + game->cmdState = 0; return 0; } - ++r5->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_DropBerriesIntoCrusher(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_DropBerriesIntoCrusher(struct BerryCrushGame *game, u8 *args) { - switch (r4->cmdState) + switch (game->cmdState) { case 0: - BerryCrush_CreateBerrySprites(r4, &r4->unk138); + CreateBerrySprites(game, &game->gfx); Rfu_SetLinkStandbyCallback(); break; case 1: if (!IsLinkTaskFinished()) return 0; - r4->unk138.animBerryIdx = 0; - r4->unk138.unk1 = 0; - r4->unk138.unk2 = 0; - r4->unk138.unk3 = 0; + game->gfx.counter = 0; + game->gfx.vibrationIdx = 0; + game->gfx.numVibrations = 0; + game->gfx.vibrating = FALSE; break; case 2: - r4->unk138.berrySprites[r4->unk138.animBerryIdx]->callback = SpriteCB_DropBerryIntoCrusher; - r4->unk138.berrySprites[r4->unk138.animBerryIdx]->affineAnimPaused = FALSE; + game->gfx.berrySprites[game->gfx.counter]->callback = SpriteCB_DropBerryIntoCrusher; + game->gfx.berrySprites[game->gfx.counter]->affineAnimPaused = FALSE; PlaySE(SE_BALL_THROW); break; case 3: - if (r4->unk138.berrySprites[r4->unk138.animBerryIdx]->callback == SpriteCB_DropBerryIntoCrusher) + if (game->gfx.berrySprites[game->gfx.counter]->callback == SpriteCB_DropBerryIntoCrusher) return 0; - r4->unk138.berrySprites[r4->unk138.animBerryIdx] = NULL; - ++r4->unk138.animBerryIdx; + game->gfx.berrySprites[game->gfx.counter] = NULL; + game->gfx.counter++; Rfu_SetLinkStandbyCallback(); break; case 4: if (!IsLinkTaskFinished()) return 0; - if (r4->unk138.animBerryIdx < r4->playerCount) + if (game->gfx.counter < game->playerCount) { - r4->cmdState = 2; + game->cmdState = 2; return 0; } - r4->unk138.animBerryIdx = 0; + game->gfx.counter = 0; break; case 5: - BerryCrushFreeBerrySpriteGfx(r4, &r4->unk138); + BerryCrushFreeBerrySpriteGfx(game, &game->gfx); Rfu_SetLinkStandbyCallback(); break; case 6: if (!IsLinkTaskFinished()) return 0; PlaySE(SE_FALL); - BerryCrush_RunOrScheduleCommand(11, 1, NULL); - r4->gameState = 5; - r4->cmdState = 0; + RunOrScheduleCommand(CMD_DROP_LID, SCHEDULE_CMD, NULL); + game->gameState = STATE_DROP_LID; + game->cmdState = 0; return 0; } - ++r4->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_DropLid(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_DropLid(struct BerryCrushGame *game, u8 *args) { - switch (r4->cmdState) + switch (game->cmdState) { case 0: - r4->depth += 4; - if (r4->depth < 0) + game->depth += 4; + if (game->depth < 0) return 0; - r4->depth = 0; - r4->unk138.unk1 = 4; - r4->unk138.animBerryIdx = 0; - r4->unk138.unk2 = gUnknown_082F326C[r4->unk138.unk1][0]; + game->depth = 0; + game->gfx.vibrationIdx = 4; + game->gfx.counter = 0; + game->gfx.numVibrations = sIntroOutroVibrationData[game->gfx.vibrationIdx][0]; PlaySE(SE_M_STRENGTH); break; case 1: - r4->vibration = gUnknown_082F326C[r4->unk138.unk1][r4->unk138.animBerryIdx]; - SetGpuReg(REG_OFFSET_BG0VOFS, -r4->vibration); - SetGpuReg(REG_OFFSET_BG2VOFS, -r4->vibration); - SetGpuReg(REG_OFFSET_BG3VOFS, -r4->vibration); - ++r4->unk138.animBerryIdx; - if (r4->unk138.animBerryIdx < r4->unk138.unk2) + game->vibration = sIntroOutroVibrationData[game->gfx.vibrationIdx][game->gfx.counter]; + SetGpuReg(REG_OFFSET_BG0VOFS, -game->vibration); + SetGpuReg(REG_OFFSET_BG2VOFS, -game->vibration); + SetGpuReg(REG_OFFSET_BG3VOFS, -game->vibration); + game->gfx.counter++; + if (game->gfx.counter < game->gfx.numVibrations) return 0; - if (r4->unk138.unk1 == 0) + if (game->gfx.vibrationIdx == 0) break; - --r4->unk138.unk1; - r4->unk138.unk2 = gUnknown_082F326C[r4->unk138.unk1][0]; - r4->unk138.animBerryIdx = 0; + game->gfx.vibrationIdx--; + game->gfx.numVibrations = sIntroOutroVibrationData[game->gfx.vibrationIdx][0]; + game->gfx.counter = 0; return 0; case 2: - r4->vibration = 0; + game->vibration = 0; SetGpuReg(REG_OFFSET_BG0VOFS, 0); SetGpuReg(REG_OFFSET_BG2VOFS, 0); SetGpuReg(REG_OFFSET_BG3VOFS, 0); @@ -2281,18 +2505,18 @@ static u32 BerryCrushCommand_DropLid(struct BerryCrushGame *r4, __attribute__(( case 3: if (!IsLinkTaskFinished()) return 0; - BerryCrush_RunOrScheduleCommand(12, 1, NULL); - r4->gameState = 6; - r4->cmdState = 0; + RunOrScheduleCommand(CMD_COUNTDOWN, SCHEDULE_CMD, NULL); + game->gameState = STATE_COUNTDOWN; + game->cmdState = 0; return 0; } - ++r4->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_Countdown(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_Countdown(struct BerryCrushGame *game, u8 *args) { - switch (r4-> cmdState) + switch (game->cmdState) { case 1: if (!IsLinkTaskFinished()) @@ -2309,405 +2533,442 @@ static u32 BerryCrushCommand_Countdown(struct BerryCrushGame *r4, __attribute__ case 3: if (!IsLinkTaskFinished()) return 0; - r4->unk138.animBerryIdx = 0; - r4->unk138.unk1 = 0; - r4->unk138.unk2 = 0; - r4->unk138.unk3 = 0; - r4->unk10 = 0; - if (r4->localId == 0) - BerryCrush_RunOrScheduleCommand(13, 1, NULL); + game->gfx.counter = 0; + game->gfx.vibrationIdx = 0; + game->gfx.numVibrations = 0; + game->gfx.vibrating = FALSE; + game->cmdTimer = 0; + if (game->localId == 0) + RunOrScheduleCommand(CMD_PLAY_GAME_LEADER, SCHEDULE_CMD, NULL); else - BerryCrush_RunOrScheduleCommand(14, 1, NULL); - r4->gameState = 7; - r4->cmdState = 0; + RunOrScheduleCommand(CMD_PLAY_GAME_MEMBER, SCHEDULE_CMD, NULL); + game->gameState = STATE_PLAYING; + game->cmdState = 0; return 0; } - ++r4->cmdState; + game->cmdState++; return 0; } -void BerryCrush_ProcessGamePartnerInput(struct BerryCrushGame *r4) +// Receive and process data from all players +// Only used by the link leader +static void HandlePartnerInput(struct BerryCrushGame *game) { - u8 r8 = 0; - u8 r7 = 0; - u16 r3; - s32 r2_ = 0; - struct BerryCrushGame_4E *r2; + u8 numPlayersPressed = 0; + u8 i = 0; + u16 timeDiff; + s32 temp = 0; + struct BerryCrushGame_LinkState *linkState; - for (r7 = 0; r7 < r4->playerCount; r7++) + for (i = 0; i < game->playerCount; i++) { - r2 = (struct BerryCrushGame_4E *)gRecvCmds[r7]; - if ((r2->unk0 & 0xFF00) != RFUCMD_SEND_PACKET) + linkState = (struct BerryCrushGame_LinkState *)gRecvCmds[i]; + + // Skip player if we have not received a packet from them + if ((linkState->rfuCmd & 0xFF00) != RFUCMD_SEND_PACKET) continue; - if (r2->unk2 != 2) + if (linkState->sendFlag != SEND_GAME_STATE) continue; - if (r2->unk4_2) + if (linkState->pushedAButton) { - r4->localState.unk02_3 |= gUnknown_082F325C[r7]; - r4->unk98[r7].unk1C = 1; - ++r4->unk98[r7].unk16; - ++r8; - r3 = r4->timer - r4->unk98[r7].unkE; - if (r3 >= r4->unk98[r7].unk12 - 1 && r3 <= r4->unk98[r7].unk12 + 1) + game->localState.playerPressedAFlags |= sBitTable[i]; + game->players[i].inputState = INPUT_STATE_HIT; + game->players[i].numAPresses++; + numPlayersPressed++; + timeDiff = game->timer - game->players[i].inputTime; + + // If the interval between inputs is regular, the input is considered "neat" + // This counts toward the player's neatness score + if (timeDiff >= game->players[i].timeSincePrevInput - 1 + && timeDiff <= game->players[i].timeSincePrevInput + 1) { - ++r4->unk98[r7].unk10; - r4->unk98[r7].unk12 = r3; - if (r4->unk98[r7].unk10 > r4->unk98[r7].unk14) - r4->unk98[r7].unk14 = r4->unk98[r7].unk10; + // On neat input streak + game->players[i].neatInputStreak++; + game->players[i].timeSincePrevInput = timeDiff; + if (game->players[i].neatInputStreak > game->players[i].maxNeatInputStreak) + game->players[i].maxNeatInputStreak = game->players[i].neatInputStreak; } else { - r4->unk98[r7].unk10 = 0; - r4->unk98[r7].unk12 = r3; + // End neat input streak + game->players[i].neatInputStreak = 0; + game->players[i].timeSincePrevInput = timeDiff; } - r4->unk98[r7].unkE = r4->timer; - ++r4->unk98[r7].unk1B; - if (r4->unk98[r7].unk1B > 2) - r4->unk98[r7].unk1B = 0; + + game->players[i].inputTime = game->timer; + game->players[i].inputFlags++; + if (game->players[i].inputFlags > F_INPUT_HIT_B) + game->players[i].inputFlags = 0; } else { - r4->unk98[r7].unk1C = 0; + game->players[i].inputState = INPUT_STATE_NONE; } } - if (r8 > 1) + if (numPlayersPressed > 1) { - for (r7 = 0; r7 < r4->playerCount; ++r7) + // For each player that pressed A, flag their input as synchronous + // This is used to change their impact sprite to a big impact + for (i = 0; i < game->playerCount; i++) { - if (!r4->unk98[r7].unk1C) + if (game->players[i].inputState == INPUT_STATE_NONE) continue; - r4->unk98[r7].unk1C |= 2; - ++r4->unk98[r7].unk18; + game->players[i].inputState |= INPUT_STATE_HIT_SYNC; + game->players[i].numSyncedAPresses++; } } - if (r8 == 0) + if (numPlayersPressed == 0) return; - r4->unk2E += r8; - r8 += gUnknown_082F3264[r8 - 1]; - r4->unk34 += r8; - r4->unk1A += r8; - if (r4->unk18 - r4->unk1A > 0) + game->bigSparkleCounter += numPlayersPressed; + numPlayersPressed += sSyncPressBonus[numPlayersPressed - 1]; + game->sparkleCounter += numPlayersPressed; + game->totalAPresses += numPlayersPressed; + if (game->targetAPresses - game->totalAPresses > 0) { - r2_ = (s32)r4->unk1A; - r2_ <<= 8; - r2_ = MathUtil_Div32(r2_, r4->unk20); - r2_ >>= 8; - r4->unk24 = (u8)r2_; + temp = (s32)game->totalAPresses; + temp = Q_24_8(temp); + temp = MathUtil_Div32(temp, game->targetDepth); + temp = Q_24_8_TO_INT(temp); + game->newDepth = (u8)temp; return; } - r4->unk24 = 32; - r4->localState.unk02_0 = 1; + // Target number of A presses has been reached, game is complete + game->newDepth = 32; + game->localState.endGame = TRUE; } -void BerryCrush_BuildLocalState(struct BerryCrushGame *r3) +// Updates the crusher, input flags, and timer to send to group members +// Only used by the link leader +static void UpdateLeaderGameState(struct BerryCrushGame *game) { - u8 r6 = 0; - u16 r1 = 0; - u16 r2 = 0; - u8 r4 = 0; + u8 numPlayersPressed = 0; + u16 flags = 0; + u16 temp = 0; + u8 i = 0; - for (r4 = 0; r4 < r3->playerCount; ++r4) + for (i = 0; i < game->playerCount; i++) { - if (r3->unk98[r4].unk1C != 0) + if (game->players[i].inputState != INPUT_STATE_NONE) { - ++r6; - r1 = r3->unk98[r4].unk1B + 1; - if (r3->unk98[r4].unk1C & 2) - r1 |= 4; - r1 <<= 3 * r4; - r3->localState.unk08 |= r1; + numPlayersPressed++; + flags = game->players[i].inputFlags + F_INPUT_HIT_A; + if (game->players[i].inputState & INPUT_STATE_HIT_SYNC) + flags |= F_INPUT_HIT_SYNC; + flags <<= INPUT_FLAGS_PER_PLAYER * i; + game->localState.inputFlags |= flags; } } - r2 = (u16)r3->unk24; - r3->localState.unk04 = r2; - if (r6 == 0) + temp = (u16)game->newDepth; + game->localState.depth = temp; + if (numPlayersPressed == 0) { - if (r3->unk138.unk3 != 0) - ++r3->unk138.animBerryIdx; + if (game->gfx.vibrating) + game->gfx.counter++; } - else if (r3->unk138.unk3 != 0) + else if (game->gfx.vibrating) { - if (r6 != r3->unk138.unk1) + if (numPlayersPressed != game->gfx.vibrationIdx) { - r3->unk138.unk1 = r6 - 1; - r3->unk138.unk2 = gUnknown_082F3290[r6 - 1][0]; + game->gfx.vibrationIdx = numPlayersPressed - 1; + game->gfx.numVibrations = sVibrationData[numPlayersPressed - 1][0]; } else { - ++r3->unk138.animBerryIdx; + game->gfx.counter++; } } else { - r3->unk138.animBerryIdx = 0; - r3->unk138.unk1 = r6 - 1; - r3->unk138.unk2 = gUnknown_082F3290[r6 - 1][0]; - r3->unk138.unk3 = 1; + game->gfx.counter = 0; + game->gfx.vibrationIdx = numPlayersPressed - 1; + game->gfx.numVibrations = sVibrationData[numPlayersPressed - 1][0]; + game->gfx.vibrating = TRUE; } - if (r3->unk138.unk3 != 0) + if (game->gfx.vibrating) { - if (r3->unk138.animBerryIdx >= r3->unk138.unk2) + if (game->gfx.counter >= game->gfx.numVibrations) { - r3->unk138.animBerryIdx = 0; - r3->unk138.unk1 = 0; - r3->unk138.unk2 = 0; - r3->unk138.unk3 = 0; - r1 = 0; + game->gfx.counter = 0; + game->gfx.vibrationIdx = 0; + game->gfx.numVibrations = 0; + game->gfx.vibrating = FALSE; + temp = 0; } else { - r1 = gUnknown_082F3290[r3->unk138.unk1][r3->unk138.animBerryIdx + 1]; + temp = sVibrationData[game->gfx.vibrationIdx][game->gfx.counter + 1]; } - r3->localState.unk03 = (u8)r1; + game->localState.vibration = (u8)temp; } else { - r3->localState.unk03 = 0; + game->localState.vibration = 0; } - r3->localState.unk06 = r3->unk26; + game->localState.timer = game->leaderTimer; } -void BerryCrush_HandlePlayerInput(struct BerryCrushGame *r5) +// Checks for input and sends data to group members +static void HandlePlayerInput(struct BerryCrushGame *game) { if (JOY_NEW(A_BUTTON)) - r5->localState.pushedAButton = 1; + game->localState.pushedAButton = TRUE; + if (JOY_HELD(A_BUTTON)) { - if (r5->unk98[r5->localId].unk1A < r5->timer) - ++r5->unk98[r5->localId].unk1A; + if (game->players[game->localId].timePressingA < game->timer) + game->players[game->localId].timePressingA++; } - if (r5->localId != 0 && r5->localState.pushedAButton == 0) + + // Only send data to other players if you are the leader or you pressed A + if (game->localId != 0 && !game->localState.pushedAButton) return; - r5->localState.unk00 = 2; - if (r5->timer % 30 == 0) + game->localState.sendFlag = SEND_GAME_STATE; + + // Every 30 frames, check whether the sparkles produced should be big, + // depending on how many A presses there were in that time + if (game->timer % 30 == 0) { - if (r5->unk2E > gUnknown_082F4444[r5->playerCount - 2]) + if (game->bigSparkleCounter > sBigSparkleThresholds[game->playerCount - 2]) { - ++r5->unk30; - r5->unk25_4 = 1; + game->numBigSparkles++; + game->bigSparkle = TRUE; } else { - r5->unk25_4 = 0; + game->bigSparkle = FALSE; } - r5->unk2E = 0; - ++r5->unk32; + game->bigSparkleCounter = 0; + game->numBigSparkleChecks++; } - if (r5->timer % 15 == 0) + + // Every 15 frames, update the amount of sparkles that should be produced, + // depending on how many A presses there were in that time (including the bonus) + if (game->timer % 15 == 0) { - if (r5->unk34 < gUnknown_082F4434[r5->playerCount - 2][0]) - r5->unk25_5 = 0; - else if (r5->unk34 < gUnknown_082F4434[r5->playerCount - 2][1]) - r5->unk25_5 = 1; - else if (r5->unk34 < gUnknown_082F4434[r5->playerCount - 2][2]) - r5->unk34 = 2; // typo since r5->unk34 will be reset? - else if (r5->unk34 < gUnknown_082F4434[r5->playerCount - 2][3]) - r5->unk34 = 3; // typo since r5->unk34 will be reset? + // BUG: The wrong field is used twice below + // As a result, only a sparkleAmount of 0, 1, or 4 is attainable + #ifdef BUGFIX + #define field sparkleAmount + #else + #define field sparkleCounter + #endif + + if (game->sparkleCounter < sSparkleThresholds[game->playerCount - 2][0]) + game->sparkleAmount = 0; + else if (game->sparkleCounter < sSparkleThresholds[game->playerCount - 2][1]) + game->sparkleAmount = 1; + else if (game->sparkleCounter < sSparkleThresholds[game->playerCount - 2][2]) + game->field = 2; + else if (game->sparkleCounter < sSparkleThresholds[game->playerCount - 2][3]) + game->field = 3; else - r5->unk25_5 = 4; - r5->unk34 = 0; + game->sparkleAmount = 4; + game->sparkleCounter = 0; + + #undef field } else { - ++r5->unk10; - if (r5->unk10 > 60) + game->cmdTimer++; + if (game->cmdTimer > 60) { - if (r5->unk10 > 70) + if (game->cmdTimer > 70) { ClearRecvCommands(); - r5->unk10 = 0; + game->cmdTimer = 0; } - else if (r5->localState.unk02_3 == 0) + else if (game->localState.playerPressedAFlags == 0) { ClearRecvCommands(); - r5->unk10 = 0; + game->cmdTimer = 0; } } } - if (r5->timer >= 36000) - r5->localState.unk02_0 = 1; - r5->localState.unk02_1 = r5->unk25_4; - r5->localState.unk0A = r5->unk25_5; - memcpy(r5->sendCmd, &r5->localState, sizeof(r5->sendCmd)); - Rfu_SendPacket(r5->sendCmd); + if (game->timer >= MAX_TIME) + game->localState.endGame = TRUE; + game->localState.bigSparkle = game->bigSparkle; + game->localState.sparkleAmount = game->sparkleAmount; + memcpy(game->sendCmd, &game->localState, sizeof(game->sendCmd)); + Rfu_SendPacket(game->sendCmd); } -void BerryCrush_UpdateGameState(struct BerryCrushGame *r5) +static void RecvLinkData(struct BerryCrushGame *game) { - u8 r4 = 0; - struct BerryCrushGame_4E *r4_ = NULL; + u8 i = 0; + struct BerryCrushGame_LinkState *linkState = NULL; + + for (i = 0; i < game->playerCount; i++) + game->players[i].inputState = INPUT_STATE_NONE; - for (r4 = 0; r4 < r5->playerCount; r4++) - r5->unk98[r4].unk1C = 0; if ((gRecvCmds[0][0] & 0xFF00) != RFUCMD_SEND_PACKET) { - r5->unk25_2 = 0; + game->playedSound = FALSE; return; } if (gRecvCmds[0][1] != 2) { - r5->unk25_2 = 0; + game->playedSound = FALSE; return; } - memcpy(r5->recvCmd, gRecvCmds[0], 14); - r4_ = (struct BerryCrushGame_4E *)&r5->recvCmd; - r5->depth = r4_->unk6; - r5->vibration = (s16)r4_->unk5; - r5->timer = r4_->unk8; - sub_80216E0(r5, &(r5->unk138)); - if (r4_->unk4_0) - { - r5->unk25_3 = 1; - } + memcpy(game->recvCmd, gRecvCmds[0], sizeof(game->recvCmd)); + linkState = (struct BerryCrushGame_LinkState *)&game->recvCmd; + game->depth = linkState->depth; + game->vibration = (s16)linkState->vibration; + game->timer = linkState->timer; + UpdateInputEffects(game, &(game->gfx)); + + if (linkState->endGame) + game->endGame = TRUE; } -static u32 BerryCrushCommand_PlayGame_Master(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_PlayGame_Leader(struct BerryCrushGame *game, u8 *args) { - memset(&r4->localState, 0, sizeof(r4->localState)); - memset(&r4->recvCmd, 0, sizeof(r4->recvCmd)); - BerryCrush_UpdateGameState(r4); - SetGpuReg(REG_OFFSET_BG0VOFS, -r4->vibration); - SetGpuReg(REG_OFFSET_BG2VOFS, -r4->vibration); - SetGpuReg(REG_OFFSET_BG3VOFS, -r4->vibration); - if (r4->unk25_3) + memset(&game->localState, 0, sizeof(game->localState)); + memset(&game->recvCmd, 0, sizeof(game->recvCmd)); + RecvLinkData(game); + SetGpuReg(REG_OFFSET_BG0VOFS, -game->vibration); + SetGpuReg(REG_OFFSET_BG2VOFS, -game->vibration); + SetGpuReg(REG_OFFSET_BG3VOFS, -game->vibration); + if (game->endGame) { - if (r4->timer >= 36000) + if (game->timer >= MAX_TIME) { - r4->timer = 36000; - BerryCrush_RunOrScheduleCommand(16, 1, NULL); + game->timer = MAX_TIME; + RunOrScheduleCommand(CMD_TIMES_UP, SCHEDULE_CMD, NULL); } else { - BerryCrush_RunOrScheduleCommand(15, 1, NULL); + RunOrScheduleCommand(CMD_FINISH_GAME, SCHEDULE_CMD, NULL); } - r4->unk10 = 0; - r4->cmdState = 0; + game->cmdTimer = 0; + game->cmdState = 0; return 0; } else { - ++r4->unk26; - BerryCrush_ProcessGamePartnerInput(r4); - BerryCrush_BuildLocalState(r4); - BerryCrush_HandlePlayerInput(r4); + game->leaderTimer++; + HandlePartnerInput(game); + UpdateLeaderGameState(game); + HandlePlayerInput(game); return 0; } } -static u32 BerryCrushCommand_PlayGame_Slave(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_PlayGame_Member(struct BerryCrushGame *game, u8 *args) { - memset(&r4->localState, 0, sizeof(r4->localState)); - memset(&r4->recvCmd, 0, sizeof(r4->recvCmd)); - BerryCrush_UpdateGameState(r4); - SetGpuReg(REG_OFFSET_BG0VOFS, -r4->vibration); - SetGpuReg(REG_OFFSET_BG2VOFS, -r4->vibration); - SetGpuReg(REG_OFFSET_BG3VOFS, -r4->vibration); - if (r4->unk25_3) + memset(&game->localState, 0, sizeof(game->localState)); + memset(&game->recvCmd, 0, sizeof(game->recvCmd)); + RecvLinkData(game); + SetGpuReg(REG_OFFSET_BG0VOFS, -game->vibration); + SetGpuReg(REG_OFFSET_BG2VOFS, -game->vibration); + SetGpuReg(REG_OFFSET_BG3VOFS, -game->vibration); + if (game->endGame) { - if (r4->timer >= 36000) + if (game->timer >= MAX_TIME) { - r4->timer = 36000; - BerryCrush_RunOrScheduleCommand(16, 1, NULL); + game->timer = MAX_TIME; + RunOrScheduleCommand(CMD_TIMES_UP, SCHEDULE_CMD, NULL); } else { - BerryCrush_RunOrScheduleCommand(15, 1, NULL); + RunOrScheduleCommand(CMD_FINISH_GAME, SCHEDULE_CMD, NULL); } - r4->unk10 = 0; - r4->cmdState = 0; + game->cmdTimer = 0; + game->cmdState = 0; return 0; } else { - BerryCrush_HandlePlayerInput(r4); + HandlePlayerInput(game); return 0; } } -static u32 BerryCrushCommand_FinishGame(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +// Game was 'won', crusher was pushed down fully before time was up +static u32 Cmd_FinishGame(struct BerryCrushGame *game, u8 *args) { - switch (r4->cmdState) + switch (game->cmdState) { case 0: - r4->gameState = 8; + game->gameState = STATE_FINISHED; PlaySE(SE_M_STRENGTH); BlendPalettes(PALETTES_ALL, 8, RGB(31, 31, 0)); - r4->unk138.animBerryIdx = 2; + game->gfx.counter = 2; break; case 1: - if (--r4->unk138.animBerryIdx != 255) + if (--game->gfx.counter != (u8)-1) return 0; BlendPalettes(PALETTES_ALL, 0, RGB(31, 31, 0)); - r4->unk138.unk1 = 4; - r4->unk138.animBerryIdx = 0; - r4->unk138.unk2 = gUnknown_082F326C[r4->unk138.unk1][0]; + game->gfx.vibrationIdx = 4; + game->gfx.counter = 0; + game->gfx.numVibrations = sIntroOutroVibrationData[game->gfx.vibrationIdx][0]; break; case 2: - r4->vibration = gUnknown_082F326C[r4->unk138.unk1][r4->unk138.animBerryIdx]; - SetGpuReg(REG_OFFSET_BG0VOFS, -r4->vibration); - SetGpuReg(REG_OFFSET_BG2VOFS, -r4->vibration); - SetGpuReg(REG_OFFSET_BG3VOFS, -r4->vibration); - if (++r4->unk138.animBerryIdx < r4->unk138.unk2) + game->vibration = sIntroOutroVibrationData[game->gfx.vibrationIdx][game->gfx.counter]; + SetGpuReg(REG_OFFSET_BG0VOFS, -game->vibration); + SetGpuReg(REG_OFFSET_BG2VOFS, -game->vibration); + SetGpuReg(REG_OFFSET_BG3VOFS, -game->vibration); + if (++game->gfx.counter < game->gfx.numVibrations) return 0; - if (r4->unk138.unk1 != 0) + if (game->gfx.vibrationIdx != 0) { - --r4->unk138.unk1; - r4->unk138.unk2 = gUnknown_082F326C[r4->unk138.unk1][0]; - r4->unk138.animBerryIdx = 0; + game->gfx.vibrationIdx--; + game->gfx.numVibrations = sIntroOutroVibrationData[game->gfx.vibrationIdx][0]; + game->gfx.counter = 0; return 0; } break; case 3: - r4->vibration = 0; + game->vibration = 0; SetGpuReg(REG_OFFSET_BG0VOFS, 0); SetGpuReg(REG_OFFSET_BG2VOFS, 0); SetGpuReg(REG_OFFSET_BG3VOFS, 0); break; case 4: - if (!sub_80218D4(r4, &r4->unk138)) + if (!AreEffectsFinished(game, &game->gfx)) return 0; Rfu_SetLinkStandbyCallback(); - r4->unk10 = 0; + game->cmdTimer = 0; break; case 5: if (!IsLinkTaskFinished()) return 0; - BerryCrush_RunOrScheduleCommand(17, 1, NULL); - r4->unk10 = 0; - r4->cmdState = 0; + RunOrScheduleCommand(CMD_CALC_RESULTS, SCHEDULE_CMD, NULL); + game->cmdTimer = 0; + game->cmdState = 0; return 0; } - ++r4->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_HandleTimeUp(struct BerryCrushGame *r5, u8 *r6) +static u32 Cmd_HandleTimeUp(struct BerryCrushGame *game, u8 *args) { - switch (r5->cmdState) + switch (game->cmdState) { case 0: - r5->gameState = 9; + game->gameState = STATE_TIMES_UP; PlaySE(SE_FAILURE); BlendPalettes(PALETTES_ALL, 8, RGB(31, 0, 0)); - r5->unk138.animBerryIdx = 4; + game->gfx.counter = 4; break; case 1: - if (--r5->unk138.animBerryIdx != 255) + if (--game->gfx.counter != (u8)-1) return 0; BlendPalettes(PALETTES_ALL, 0, RGB(31, 0, 0)); - r5->unk138.animBerryIdx = 0; + game->gfx.counter = 0; break; case 2: - if (!sub_80218D4(r5, &r5->unk138)) + if (!AreEffectsFinished(game, &game->gfx)) return 0; Rfu_SetLinkStandbyCallback(); - r5->unk10 = 0; + game->cmdTimer = 0; SetGpuReg(REG_OFFSET_BG0VOFS, 0); SetGpuReg(REG_OFFSET_BG2VOFS, 0); SetGpuReg(REG_OFFSET_BG3VOFS, 0); @@ -2715,235 +2976,264 @@ static u32 BerryCrushCommand_HandleTimeUp(struct BerryCrushGame *r5, u8 *r6) case 3: if (!IsLinkTaskFinished()) return 0; - ConvertIntToDecimalStringN(gStringVar1, r5->powder, STR_CONV_MODE_LEFT_ALIGN, 6); - BerryCrush_SetShowMessageParams(r6, 7, 1, 0, 0); - r5->nextCmd = 19; - BerryCrush_RunOrScheduleCommand(3, 1, NULL); - r5->unk10 = 0; - r5->cmdState = 0; + ConvertIntToDecimalStringN(gStringVar1, game->powder, STR_CONV_MODE_LEFT_ALIGN, 6); + SetPrintMessageArgs(args, MSG_TIMES_UP, F_MSG_CLEAR, 0, 0); + game->nextCmd = CMD_SAVE; + RunOrScheduleCommand(CMD_PRINT_MSG, SCHEDULE_CMD, NULL); + game->cmdTimer = 0; + game->cmdState = 0; return 0; } - ++r5->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_TabulateResults(struct BerryCrushGame *r7, __attribute__((unused)) u8 *r1) +static u32 Cmd_TabulateResults(struct BerryCrushGame *game, u8 *args) { - u8 r8, r4_, r3; - s32 r2; - s32 r4; - u16 r6; + u8 i, j, tempPlayerId; + s32 temp1, temp2; + u16 tempStat; - switch (r7->cmdState) + switch (game->cmdState) { case 0: - memset(r7->sendCmd, 0, 2 * sizeof(u16)); - if (r7->unk98[r7->localId].unk1A > r7->timer) - r7->unk98[r7->localId].unk1A = r7->timer; - r7->sendCmd[0] = r7->unk98[r7->localId].unk1A; - SendBlock(0, r7->sendCmd, 2); + memset(game->sendCmd, 0, 2 * sizeof(u16)); + if (game->players[game->localId].timePressingA > game->timer) + game->players[game->localId].timePressingA = game->timer; + game->sendCmd[0] = game->players[game->localId].timePressingA; + SendBlock(0, game->sendCmd, 2); break; case 1: if (!IsLinkTaskFinished()) return 0; - r7->unk10 = 0; + game->cmdTimer = 0; break; case 2: - if (GetBlockReceivedStatus() != sReceivedPlayerBitmasks[r7->playerCount - 2]) + if (GetBlockReceivedStatus() != sReceivedPlayerBitmasks[game->playerCount - 2]) return 0; - for (r8 = 0; r8 < r7->playerCount; ++r8) - r7->unk98[r8].unk1A = gBlockRecvBuffer[r8][0]; - r7->unk10 = 0; - r7->sendCmd[0] = 0; + for (i = 0; i < game->playerCount; i++) + game->players[i].timePressingA = gBlockRecvBuffer[i][0]; + + game->cmdTimer = 0; + game->sendCmd[0] = 0; ResetBlockReceivedFlags(); - if (r7->localId == 0) - r7->cmdState = 3; + + // If player is not leader, skip the steps + // where the results are calculated and sent. + // Group members just read the results sent + // to them by the leader. + if (game->localId == 0) + game->cmdState = 3; else - r7->cmdState = 6; + game->cmdState = 6; return 0; case 3: - memset(&r7->unk68, 0, sizeof(struct BerryCrushGame_68)); - r7->unk68.unk04 = r7->timer; - r7->unk68.unk06 = r7->unk18 / (r7->timer / 60); - r2 = MathUtil_Mul32(Q_24_8(r7->unk30), Q_24_8(50)); - r2 = MathUtil_Div32(r2, Q_24_8(r7->unk32)) + Q_24_8(50); - r2 = Q_24_8_TO_INT(r2); - r7->unk68.unk08 = r2 & 0x7F; - r2 = Q_24_8(r2); - r2 = MathUtil_Div32(r2, Q_24_8(100)); - r4 = Q_24_8(r7->powder * r7->playerCount); - r4 = MathUtil_Mul32(r4, r2); - r7->unk68.unk00 = r4 >> 8; - r7->unk68.unk20[0][7] = Random() % 3; - for (r8 = 0; r8 < r7->playerCount; ++r8) + memset(&game->results, 0, sizeof(game->results)); + game->results.time = game->timer; + game->results.targetPressesPerSec = game->targetAPresses / (game->timer / 60); + + // Calculate silkiness + // Silkiness is the percentage of times big sparkles were produced when possible, + // which itself depends on the number of A presses every 30 frames + temp1 = MathUtil_Mul32(Q_24_8(game->numBigSparkles), Q_24_8(50)); + temp1 = MathUtil_Div32(temp1, Q_24_8(game->numBigSparkleChecks)) + Q_24_8(50); + temp1 = Q_24_8_TO_INT(temp1); + game->results.silkiness = temp1 & 0x7F; + + // Calculate amount of powder + temp1 = Q_24_8(temp1); + temp1 = MathUtil_Div32(temp1, Q_24_8(100)); + temp2 = Q_24_8(game->powder * game->playerCount); + temp2 = MathUtil_Mul32(temp2, temp1); + game->results.powder = Q_24_8_TO_INT(temp2); + + // Choose random second results page + game->results.randomPageId = Random() % NUM_RANDOM_RESULTS_PAGES; + + for (i = 0; i < game->playerCount; i++) { - r7->unk68.unk20[0][r8] = r8; - r7->unk68.unk20[1][r8] = r8; - r7->unk68.stats[0][r8] = r7->unk98[r8].unk16; - r7->unk68.unk0A += r7->unk68.stats[0][r8]; - switch (r7->unk68.unk20[0][7]) + game->results.playerIdsRanked[RESULTS_PAGE_PRESSES][i] = i; + game->results.playerIdsRanked[RESULTS_PAGE_RANDOM][i] = i; + game->results.stats[RESULTS_PAGE_PRESSES][i] = game->players[i].numAPresses; + game->results.totalAPresses += game->results.stats[RESULTS_PAGE_PRESSES][i]; + + // Calculate value for random second results page + switch (game->results.randomPageId) { - case 0: - if (r7->unk98[r8].unk16 != 0) + case RESULTS_PAGE_NEATNESS: + if (game->players[i].numAPresses != 0) { - r2 = r7->unk98[r8].unk14; - r2 = Q_24_8(r2); - r2 = MathUtil_Mul32(r2, Q_24_8(100)); - r4 = r7->unk98[r8].unk16; - r4 = Q_24_8(r4); - r4 = MathUtil_Div32(r2, r4); + // Calculate percentage of inputs that were in largest "neat" streak + // "Neat" inputs are those done at a regular interval + temp1 = game->players[i].maxNeatInputStreak; + temp1 = Q_24_8(temp1); + temp1 = MathUtil_Mul32(temp1, Q_24_8(100)); + temp2 = game->players[i].numAPresses; + temp2 = Q_24_8(temp2); + temp2 = MathUtil_Div32(temp1, temp2); } else { - r4 = 0; + temp2 = 0; } break; - case 1: - if (r7->unk98[r8].unk16 != 0) + case RESULTS_PAGE_COOPERATIVE: + if (game->players[i].numAPresses != 0) { - r2 = r7->unk98[r8].unk18; - r2 = Q_24_8(r2); - r2 = MathUtil_Mul32(r2, Q_24_8(100)); - r4 = r7->unk98[r8].unk16; - r4 = Q_24_8(r4); - r4 = MathUtil_Div32(r2, r4); + // Calculate percentage of inputs that were + // done at the same time as another player + temp1 = game->players[i].numSyncedAPresses; + temp1 = Q_24_8(temp1); + temp1 = MathUtil_Mul32(temp1, Q_24_8(100)); + temp2 = game->players[i].numAPresses; + temp2 = Q_24_8(temp2); + temp2 = MathUtil_Div32(temp1, temp2); } else { - r4 = 0; + temp2 = 0; } break; - case 2: - if (r7->unk98[r8].unk16 == 0) + case RESULTS_PAGE_POWER: + if (game->players[i].numAPresses == 0) { - r4 = 0; + temp2 = 0; } - else if (r7->unk98[r8].unk1A >= r7->timer) + else if (game->players[i].timePressingA >= game->timer) { - r4 = 0x6400; + // Spent 100% of the time pressing A + temp2 = Q_24_8(100); } else { - r2 = r7->unk98[r8].unk1A; - r2 = Q_24_8(r2); - r2 = MathUtil_Mul32(r2, Q_24_8(100)); - r4 = r7->timer; - r4 = Q_24_8(r4); - r4 = MathUtil_Div32(r2, r4); + // Calculate percentage of time the + // player spent pressing A + temp1 = game->players[i].timePressingA; + temp1 = Q_24_8(temp1); + temp1 = MathUtil_Mul32(temp1, Q_24_8(100)); + temp2 = game->timer; + temp2 = Q_24_8(temp2); + temp2 = MathUtil_Div32(temp1, temp2); } break; } - r4 >>= 4; - r7->unk68.stats[1][r8] = r4; + temp2 >>= 4; + game->results.stats[RESULTS_PAGE_RANDOM][i] = temp2; } break; case 4: - for (r8 = 0; r8 < r7->playerCount - 1; ++r8) + for (i = 0; i < game->playerCount - 1; i++) { - for (r4_ = r7->playerCount - 1; r4_ > r8; --r4_) + for (j = game->playerCount - 1; j > i; j--) { - if (r7->unk68.stats[0][r4_ - 1] < r7->unk68.stats[0][r4_]) + // Calculate player rankings for "Number of Presses" by sorting arrays + if (game->results.stats[RESULTS_PAGE_PRESSES][j - 1] < game->results.stats[RESULTS_PAGE_PRESSES][j]) { - r6 = r7->unk68.stats[0][r4_]; - r7->unk68.stats[0][r4_] = r7->unk68.stats[0][r4_ - 1]; - r7->unk68.stats[0][r4_ - 1] = r6; - r3 = r7->unk68.unk20[0][r4_]; - r7->unk68.unk20[0][r4_] = r7->unk68.unk20[0][r4_ - 1]; - r7->unk68.unk20[0][r4_ - 1] = r3; + SWAP(game->results.stats[RESULTS_PAGE_PRESSES][j], + game->results.stats[RESULTS_PAGE_PRESSES][j - 1], + tempStat); + SWAP(game->results.playerIdsRanked[RESULTS_PAGE_PRESSES][j], + game->results.playerIdsRanked[RESULTS_PAGE_PRESSES][j - 1], + tempPlayerId); } - if (r7->unk68.stats[1][r4_ - 1] < r7->unk68.stats[1][r4_]) + // Calculate player rankings for random second results page by sorting arrays + if (game->results.stats[RESULTS_PAGE_RANDOM][j - 1] < game->results.stats[RESULTS_PAGE_RANDOM][j]) { - r6 = r7->unk68.stats[1][r4_]; - r7->unk68.stats[1][r4_] = r7->unk68.stats[1][r4_ - 1]; - r7->unk68.stats[1][r4_ - 1] = r6; - r3 = r7->unk68.unk20[1][r4_]; - r7->unk68.unk20[1][r4_] = r7->unk68.unk20[1][r4_ - 1]; - r7->unk68.unk20[1][r4_ - 1] = r3; + SWAP(game->results.stats[RESULTS_PAGE_RANDOM][j], + game->results.stats[RESULTS_PAGE_RANDOM][j - 1], + tempStat); + SWAP(game->results.playerIdsRanked[RESULTS_PAGE_RANDOM][j], + game->results.playerIdsRanked[RESULTS_PAGE_RANDOM][j - 1], + tempPlayerId); } } } - SendBlock(0,&r7->unk68, sizeof(struct BerryCrushGame_68)); + SendBlock(0, &game->results, sizeof(game->results)); break; case 5: if (!IsLinkTaskFinished()) return 0; - r7->unk10 = 0; + game->cmdTimer = 0; break; case 6: if (GetBlockReceivedStatus() != 1) return 0; - memset(&r7->unk68, 0, sizeof(struct BerryCrushGame_68)); - memcpy(&r7->unk68, gBlockRecvBuffer, sizeof(struct BerryCrushGame_68)); + + // Receive results calculated by leader + memset(&game->results, 0, sizeof(game->results)); + memcpy(&game->results, gBlockRecvBuffer, sizeof(game->results)); ResetBlockReceivedFlags(); - r7->unk10 = 0; + game->cmdTimer = 0; break; case 7: - BerryCrush_SaveResults(); - BerryCrush_RunOrScheduleCommand(18, 1, NULL); - r7->gameState = 11; - r7->cmdState = 0; - r7->unk24 = 0; + SaveResults(); + RunOrScheduleCommand(CMD_SHOW_RESULTS, SCHEDULE_CMD, NULL); + game->gameState = STATE_RESULTS_PRESSES; + game->cmdState = 0; + game->newDepth = 0; return 0; } - ++r7->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_ShowResults(struct BerryCrushGame *r5, u8 *r6) +static u32 Cmd_ShowResults(struct BerryCrushGame *game, u8 *args) { - switch (r5->cmdState) + switch (game->cmdState) { case 0: - if (!sub_8022070(r5, &r5->unk138)) + if (!OpenResultsWindow(game, &game->gfx)) return 0; break; case 1: CopyBgTilemapBufferToVram(0); - r5->unk138.animBerryIdx = 30; + game->gfx.counter = 30; break; case 2: - if (r5->unk138.animBerryIdx != 0) + if (game->gfx.counter != 0) { - --r5->unk138.animBerryIdx; + game->gfx.counter--; return 0; } if (!(JOY_NEW(A_BUTTON))) return 0; PlaySE(SE_SELECT); - sub_802222C(r5); + CloseResultsWindow(game); break; case 3: - if (r5->gameState <= 12) + // Progress through each page of the results + if (game->gameState < RESULTS_STATE_END) { - ++r5->gameState; - r5->cmdState = 0; + game->gameState++; + game->cmdState = 0; return 0; } break; case 4: - ConvertIntToDecimalStringN(gStringVar1, r5->powder, STR_CONV_MODE_LEFT_ALIGN, 6); + // Print message showing how much powder was created + ConvertIntToDecimalStringN(gStringVar1, game->powder, STR_CONV_MODE_LEFT_ALIGN, 6); ConvertIntToDecimalStringN(gStringVar2, GetBerryPowder(), STR_CONV_MODE_LEFT_ALIGN, 6); - BerryCrush_SetShowMessageParams(r6, 2, 3, 0, 0); - r5->nextCmd = 19; - BerryCrush_RunOrScheduleCommand(3, 1, NULL); - r5->cmdState = 0; + SetPrintMessageArgs(args, MSG_POWDER, F_MSG_CLEAR | F_MSG_EXPAND, 0, 0); + game->nextCmd = CMD_SAVE; + RunOrScheduleCommand(CMD_PRINT_MSG, SCHEDULE_CMD, NULL); + game->cmdState = 0; return 0; } - ++r5->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_SaveGame(struct BerryCrushGame *r5, u8 *r4) +static u32 Cmd_SaveGame(struct BerryCrushGame *game, u8 *args) { - switch (r5->cmdState) + switch (game->cmdState) { case 0: - if (r5->timer >= 36000) - BerryCrush_HideTimerSprites(&r5->unk138); - BerryCrush_SetShowMessageParams(r4, 8, 0, 0, 1); - r5->nextCmd = 19; - BerryCrush_RunOrScheduleCommand(3, 1, NULL); - r5->cmdState = 0; + if (game->timer >= MAX_TIME) + HideTimer(&game->gfx); + SetPrintMessageArgs(args, MSG_COMM_STANDBY, 0, 0, 1); + game->nextCmd = CMD_SAVE; + RunOrScheduleCommand(CMD_PRINT_MSG, SCHEDULE_CMD, NULL); + game->cmdState = 0; // State is progressed by CMD_PRINT_MSG return 0; case 1: Rfu_SetLinkStandbyCallback(); @@ -2961,63 +3251,67 @@ static u32 BerryCrushCommand_SaveGame(struct BerryCrushGame *r5, u8 *r4) return 0; break; case 4: - BerryCrush_RunOrScheduleCommand(20, 1, NULL); - r5->gameState = 15; - r5->cmdState = 0; + RunOrScheduleCommand(CMD_ASK_PLAY_AGAIN, SCHEDULE_CMD, NULL); + game->gameState = STATE_PLAY_AGAIN; + game->cmdState = 0; return 0; } - ++r5->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_AskPlayAgain(struct BerryCrushGame *r5, u8 *r6) +static u32 Cmd_AskPlayAgain(struct BerryCrushGame *game, u8 *args) { - s8 r4 = 0; + s8 input = 0; - switch (r5->cmdState) + switch (game->cmdState) { case 0: - BerryCrush_SetShowMessageParams(r6, 4, 0, 0, 1); - r5->nextCmd = 20; - BerryCrush_RunOrScheduleCommand(3, 1, NULL); - r5->cmdState = 0; // dunno what it's doing because it's already in case 0 + SetPrintMessageArgs(args, MSG_PLAY_AGAIN, 0, 0, 1); + game->nextCmd = CMD_ASK_PLAY_AGAIN; + RunOrScheduleCommand(CMD_PRINT_MSG, SCHEDULE_CMD, NULL); + game->cmdState = 0; // State is progressed by CMD_PRINT_MSG return 0; case 1: DisplayYesNoMenuDefaultYes(); break; case 2: - r4 = Menu_ProcessInputNoWrapClearOnChoose(); - if (r4 != -2) + input = Menu_ProcessInputNoWrapClearOnChoose(); + if (input != -2) { - memset(r5->sendCmd, 0, sizeof(r5->sendCmd)); - if (r4 == 0) + memset(game->sendCmd, 0, sizeof(game->sendCmd)); + if (input == 0) { + // Selected Yes if (HasAtLeastOneBerry()) - r5->unk14 = 0; + game->playAgainState = PLAY_AGAIN_YES; else - r5->unk14 = 3; + game->playAgainState = PLAY_AGAIN_NO_BERRIES; } else { - r5->unk14 = 1; + // Selected No + game->playAgainState = PLAY_AGAIN_NO; } + + // Close Yes/No and start communication ClearDialogWindowAndFrame(0, 1); - BerryCrush_SetShowMessageParams(r6, 8, 0, 0, 0); - r5->nextCmd = 21; - BerryCrush_RunOrScheduleCommand(3, 1, NULL); - r5->cmdState = 0; + SetPrintMessageArgs(args, MSG_COMM_STANDBY, 0, 0, 0); + game->nextCmd = CMD_COMM_PLAY_AGAIN; + RunOrScheduleCommand(CMD_PRINT_MSG, SCHEDULE_CMD, NULL); + game->cmdState = 0; } return 0; } - ++r5->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_CommunicatePlayAgainResponses(struct BerryCrushGame *r4, __attribute__((unused)) u8 *r1) +static u32 Cmd_CommunicatePlayAgainResponses(struct BerryCrushGame *game, u8 *args) { - u8 r5 = 0; + u8 i = 0; - switch (r4->cmdState) + switch (game->cmdState) { case 0: Rfu_SetLinkStandbyCallback(); @@ -3025,41 +3319,47 @@ static u32 BerryCrushCommand_CommunicatePlayAgainResponses(struct BerryCrushGame case 1: if (!IsLinkTaskFinished()) return 0; - r4->sendCmd[0] = r4->unk14; - r4->recvCmd[0] = 0; - SendBlock(0, r4->sendCmd, sizeof(u16)); + + // Send player's Yes/No response to partners + game->sendCmd[0] = game->playAgainState; + game->recvCmd[0] = 0; + SendBlock(0, game->sendCmd, sizeof(u16)); break; case 2: if (!IsLinkTaskFinished()) return 0; - r4->unk10 = 0; + game->cmdTimer = 0; break; case 3: - if (GetBlockReceivedStatus() != sReceivedPlayerBitmasks[r4->playerCount - 2]) + // Wait for partners responses + if (GetBlockReceivedStatus() != sReceivedPlayerBitmasks[game->playerCount - 2]) return 0; - for (; r5 < r4->playerCount; ++r5) - r4->recvCmd[0] += gBlockRecvBuffer[r5][0]; - if (r4->recvCmd[0] != 0) - BerryCrush_RunOrScheduleCommand(23, 1, NULL); + + // Read partners responses + for (i = 0; i < game->playerCount; i++) + game->recvCmd[0] += gBlockRecvBuffer[i][0]; + + if (game->recvCmd[0] != PLAY_AGAIN_YES) + RunOrScheduleCommand(CMD_PLAY_AGAIN_NO, SCHEDULE_CMD, NULL); else - BerryCrush_RunOrScheduleCommand(22, 1, NULL); + RunOrScheduleCommand(CMD_PLAY_AGAIN_YES, SCHEDULE_CMD, NULL); ResetBlockReceivedFlags(); - r4->sendCmd[0] = 0; - r4->recvCmd[0] = 0; - r4->unk10 = 0; - r4->cmdState = 0; + game->sendCmd[0] = 0; + game->recvCmd[0] = 0; + game->cmdTimer = 0; + game->cmdState = 0; return 0; } - ++r4->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_FadeOutToPlayAgain(struct BerryCrushGame *r5, __attribute__((unused)) u8 *r1) +static u32 Cmd_PlayAgain(struct BerryCrushGame *game, u8 *args) { - switch (r5->cmdState) + switch (game->cmdState) { case 0: - BeginNormalPaletteFade(PALETTES_ALL, 1, 0, 0x10, RGB_BLACK); + BeginNormalPaletteFade(PALETTES_ALL, 1, 0, 16, RGB_BLACK); UpdatePaletteFade(); break; case 1: @@ -3068,56 +3368,56 @@ static u32 BerryCrushCommand_FadeOutToPlayAgain(struct BerryCrushGame *r5, __att break; case 2: ClearDialogWindowAndFrame(0, 1); - sub_8021488(r5); - BeginNormalPaletteFade(PALETTES_ALL, 0, 0x10, 0, RGB_BLACK); + ResetCrusherPos(game); + BeginNormalPaletteFade(PALETTES_ALL, 0, 16, 0, RGB_BLACK); UpdatePaletteFade(); break; case 3: if (UpdatePaletteFade()) return 0; - BerryCrush_RunOrScheduleCommand(7, 1, NULL); - r5->gameState = 3; - r5->cmdState = 0; + RunOrScheduleCommand(CMD_ASK_PICK_BERRY, SCHEDULE_CMD, NULL); + game->gameState = STATE_PICK_BERRY; + game->cmdState = 0; return 0; } - ++r5->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_PlayAgainFailureMessage(struct BerryCrushGame *r5, __attribute__((unused)) u8 *r1) +static u32 Cmd_StopGame(struct BerryCrushGame *game, u8 *args) { - switch (r5->cmdState) + switch (game->cmdState) { case 0: DrawDialogueFrame(0, 0); - if (r5->unk14 == 3) - AddTextPrinterParameterized2(0, 1, sBerryCrushMessages[5], r5->textSpeed, 0, 2, 1, 3); + if (game->playAgainState == PLAY_AGAIN_NO_BERRIES) + AddTextPrinterParameterized2(0, 1, sMessages[MSG_NO_BERRIES], game->textSpeed, 0, 2, 1, 3); else - AddTextPrinterParameterized2(0, 1, sBerryCrushMessages[6], r5->textSpeed, 0, 2, 1, 3); + AddTextPrinterParameterized2(0, 1, sMessages[MSG_DROPPED], game->textSpeed, 0, 2, 1, 3); CopyWindowToVram(0, 3); break; case 1: if (IsTextPrinterActive(0)) return 0; - r5->unk138.animBerryIdx = 120; + game->gfx.counter = 120; break; case 2: - if (r5->unk138.animBerryIdx != 0) - --r5->unk138.animBerryIdx; + if (game->gfx.counter != 0) + game->gfx.counter--; else { - BerryCrush_RunOrScheduleCommand(24, 1, NULL); - r5->cmdState = 0; + RunOrScheduleCommand(CMD_CLOSE_LINK, SCHEDULE_CMD, NULL); + game->cmdState = 0; } return 0; } - ++r5->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_GracefulExit(struct BerryCrushGame *r5, __attribute__((unused)) u8 *r1) +static u32 Cmd_CloseLink(struct BerryCrushGame *game, u8 *args) { - switch (r5->cmdState) + switch (game->cmdState) { case 0: Rfu_SetLinkStandbyCallback(); @@ -3130,81 +3430,81 @@ static u32 BerryCrushCommand_GracefulExit(struct BerryCrushGame *r5, __attribute case 2: if (gReceivedRemoteLinkPlayers != 0) return 0; - r5->nextCmd = 25; - BerryCrush_RunOrScheduleCommand(5, 1, NULL); - r5->cmdState = 2; // ??? + game->nextCmd = CMD_QUIT; + RunOrScheduleCommand(CMD_HIDE_GAME, SCHEDULE_CMD, NULL); + game->cmdState = 2; // ??? return 0; } - ++r5->cmdState; + game->cmdState++; return 0; } -static u32 BerryCrushCommand_Quit(__attribute__((unused)) struct BerryCrushGame *r0, __attribute__((unused)) u8 *r1) +static u32 Cmd_Quit(struct BerryCrushGame *game, u8 *args) { QuitBerryCrush(NULL); return 0; } -static void sub_8024578(struct BerryCrushGame *r4) +static void ResetGame(struct BerryCrushGame *game) { - u8 r5 = 0; + u8 i = 0; - IncrementGameStat(GAME_STAT_51); - r4->unkD = 0; - r4->unk10 = 0; - r4->gameState = 2; - r4->unk14 = 0; - r4->powder = 0; - r4->unk18 = 0; - r4->unk1A = 0; - r4->unk20 = 0; - r4->unk24 = 0; - r4->unk25_0 = 0; - r4->unk25_1 = 0; - r4->unk25_2 = 0; - r4->unk25_3 = 0; - r4->unk25_4 = 0; - r4->unk25_5 = 0; - r4->unk26 = 0; - r4->timer = 0; - r4->unk2E = 0; - r4->unk32 = -1; - r4->unk30 = 0; - r4->unk34 = 0; - for (; r5 < 5; ++r5) + IncrementGameStat(GAME_STAT_PLAYED_BERRY_CRUSH); + game->unused = 0; + game->cmdTimer = 0; + game->gameState = STATE_RESET; + game->playAgainState = 0; + game->powder = 0; + game->targetAPresses = 0; + game->totalAPresses = 0; + game->targetDepth = 0; + game->newDepth = 0; + game->noRoomForPowder = FALSE; + game->newRecord = FALSE; + game->playedSound = FALSE; + game->endGame = FALSE; + game->bigSparkle = FALSE; + game->sparkleAmount = 0; + game->leaderTimer = 0; + game->timer = 0; + game->bigSparkleCounter = 0; + game->numBigSparkleChecks = -1; + game->numBigSparkles = 0; + game->sparkleCounter = 0; + for (i = 0; i < MAX_RFU_PLAYERS; i++) { - r4->unk98[r5].unkC = -1; - r4->unk98[r5].unkE = 0; - r4->unk98[r5].unk10 = 0; - r4->unk98[r5].unk12 = 1; - r4->unk98[r5].unk14 = 0; - r4->unk98[r5].unk16 = 0; - r4->unk98[r5].unk18 = 0; - r4->unk98[r5].unk1A = 0; - r4->unk98[r5].unk1B = 0; - r4->unk98[r5].unk1C = 0; + game->players[i].berryId = -1; + game->players[i].inputTime = 0; + game->players[i].neatInputStreak = 0; + game->players[i].timeSincePrevInput = 1; + game->players[i].maxNeatInputStreak = 0; + game->players[i].numAPresses = 0; + game->players[i].numSyncedAPresses = 0; + game->players[i].timePressingA = 0; + game->players[i].inputFlags = 0; + game->players[i].inputState = INPUT_STATE_NONE; } } -static void BerryCrush_SetPaletteFadeParams(u8 *params, bool8 communicateAfter, u32 selectedPals, s8 delay, u8 startY, u8 targetY, u16 palette) +static void SetPaletteFadeArgs(u8 *args, bool8 communicateAfter, u32 selectedPals, s8 delay, u8 startY, u8 targetY, u16 palette) { - params[0] = ((u8 *)&selectedPals)[0]; - params[1] = ((u8 *)&selectedPals)[1]; - params[2] = ((u8 *)&selectedPals)[2]; - params[3] = ((u8 *)&selectedPals)[3]; - params[4] = delay; - params[5] = startY; - params[6] = targetY; - params[7] = ((u8 *)&palette)[0]; - params[8] = ((u8 *)&palette)[1]; - params[9] = communicateAfter; + args[0] = ((u8 *)&selectedPals)[0]; + args[1] = ((u8 *)&selectedPals)[1]; + args[2] = ((u8 *)&selectedPals)[2]; + args[3] = ((u8 *)&selectedPals)[3]; + args[4] = delay; + args[5] = startY; + args[6] = targetY; + args[7] = ((u8 *)&palette)[0]; + args[8] = ((u8 *)&palette)[1]; + args[9] = communicateAfter; } -static void BerryCrush_SetShowMessageParams(u8 *params, u8 stringId, u8 flags, u16 waitKeys, u8 followupCmd) +static void SetPrintMessageArgs(u8 *args, u8 msgId, u8 flags, u16 waitKeys, u8 followupState) { - params[0] = stringId; - params[1] = flags; - params[2] = ((u8 *)&waitKeys)[0]; - params[3] = ((u8 *)&waitKeys)[1]; - params[4] = followupCmd; + args[0] = msgId; + args[1] = flags; + args[2] = ((u8 *)&waitKeys)[0]; + args[3] = ((u8 *)&waitKeys)[1]; + args[4] = followupState; } diff --git a/src/graphics.c b/src/graphics.c index 1a3423fb6..7a7a11b2b 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -1543,9 +1543,9 @@ const u16 gUnknown_08DE3350[] = INCBIN_U16("graphics/frontier_pass/tilemap1.bin" const u16 gUnknown_08DE3374[] = INCBIN_U16("graphics/frontier_pass/tilemap2.bin"); // Berry Crush -const u16 gUnknown_08DE3398[] = INCBIN_U16("graphics/berry_crusher/tiles.gbapal"); -const u32 gUnknown_08DE34B8[] = INCBIN_U32("graphics/berry_crusher/tiles.4bpp.lz"); -const u32 gUnknown_08DE3FD4[] = INCBIN_U32("graphics/berry_crusher/tiles.bin.lz"); +const u16 gBerryCrush_Crusher_Pal[] = INCBIN_U16("graphics/berry_crush/crusher.gbapal"); +const u32 gBerryCrush_Crusher_Gfx[] = INCBIN_U32("graphics/berry_crush/crusher.4bpp.lz"); +const u32 gBerryCrush_Crusher_Tilemap[] = INCBIN_U32("graphics/berry_crush/crusher.bin.lz"); // random garbage at the end. static const u8 sEmpty3[0x54BAC] = {0}; diff --git a/src/minigame_countdown.c b/src/minigame_countdown.c index f7dad4800..2d4d98138 100644 --- a/src/minigame_countdown.c +++ b/src/minigame_countdown.c @@ -48,8 +48,8 @@ static void Task_StaticCountdown_Free(u8 taskId); static void Task_StaticCountdown_Start(u8 taskId); static void Task_StaticCountdown_Run(u8 taskId); -static const u16 s321Start_Static_Pal[] = INCBIN_U16("graphics/link_games/321start_static.gbapal"); -static const u32 s321Start_Static_Gfx[] = INCBIN_U32("graphics/link_games/321start_static.4bpp.lz"); +static const u16 s321Start_Static_Pal[] = INCBIN_U16("graphics/minigame_countdown/321start_static.gbapal"); +static const u32 s321Start_Static_Gfx[] = INCBIN_U32("graphics/minigame_countdown/321start_static.4bpp.lz"); static const struct CompressedSpriteSheet sSpriteSheet_321Start_Static[] = { @@ -374,8 +374,8 @@ static void CreateStartSprite(u16 tileTag, u16 palTag, s16 x, s16 y, u8 subprior static void InitStartGraphic(u8 spriteId1, u8 spriteId2, u8 spriteId3); static void SpriteCB_Start(struct Sprite *sprite); -static const u16 s321Start_Pal[] = INCBIN_U16("graphics/link_games/321start.gbapal"); -static const u32 s321Start_Gfx[] = INCBIN_U32("graphics/link_games/321start.4bpp.lz"); +static const u16 s321Start_Pal[] = INCBIN_U16("graphics/minigame_countdown/321start.gbapal"); +static const u32 s321Start_Gfx[] = INCBIN_U32("graphics/minigame_countdown/321start.4bpp.lz"); #define tState data[0] #define tTilesTag data[2] diff --git a/src/strings.c b/src/strings.c index fe5051d71..19d0e3cca 100644 --- a/src/strings.c +++ b/src/strings.c @@ -1292,7 +1292,7 @@ const u8 gText_MatchCallMay_Intro1[] = _("My POKéMON and I help"); const u8 gText_MatchCallMay_Intro2[] = _("my father's research."); const u8 gText_HatchedFromEgg[] = _("{STR_VAR_1} hatched from the EGG!"); const u8 gText_NicknameHatchPrompt[] = _("Would you like to nickname the newly\nhatched {STR_VAR_1}?"); -ALIGNED(4) const u8 gText_ReadyToBerryCrush[] = _("Are you ready to BERRY-CRUSH?\nPlease pick a BERRY for use.\p"); +ALIGNED(4) const u8 gText_ReadyPickBerry[] = _("Are you ready to BERRY-CRUSH?\nPlease pick a BERRY for use.\p"); ALIGNED(4) const u8 gText_WaitForAllChooseBerry[] = _("Please wait while each member\nchooses a BERRY."); ALIGNED(4) const u8 gText_EndedWithXUnitsPowder[] = _("{PAUSE_MUSIC}{PLAY_BGM MUS_LEVEL_UP}You ended up with {STR_VAR_1} units of\nsilky-smooth BERRY POWDER.{RESUME_MUSIC}\pYour total amount of BERRY POWDER\nis {STR_VAR_2}.\p"); ALIGNED(4) const u8 gText_RecordingGameResults[] = _("Recording your game results in the\nsave file.\lPlease wait.");