From d9aac4f12a759934570ecf27af6fc9ba0124c697 Mon Sep 17 00:00:00 2001 From: FosterProgramming Date: Sun, 30 Nov 2025 11:49:21 +0100 Subject: [PATCH] Fix ball cycling not working properly when the same ball take multiple bag slots (#8163) --- src/battle_controller_player.c | 52 ++++++++++++++-------------------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/src/battle_controller_player.c b/src/battle_controller_player.c index f2819db600..93fd846a5b 100644 --- a/src/battle_controller_player.c +++ b/src/battle_controller_player.c @@ -197,46 +197,36 @@ static void CompleteOnBattlerSpritePosX_0(u32 battler) static u16 GetPrevBall(u16 ballId) { - u16 ballPrev; - s32 i, j; - CompactItemsInBagPocket(POCKET_POKE_BALLS); - for (i = 0; i < gBagPockets[POCKET_POKE_BALLS].capacity; i++) + s32 i; + s32 index = ItemIdToBallId(ballId); + u32 newBall = 0; + for (i = 0; i < POKEBALL_COUNT; i++) { - if (ballId == GetBagItemId(POCKET_POKE_BALLS, i)) - { - if (i <= 0) - { - for (j = gBagPockets[POCKET_POKE_BALLS].capacity - 1; j >= 0; j--) - { - ballPrev = GetBagItemId(POCKET_POKE_BALLS, j); - if (ballPrev != ITEM_NONE) - return ballPrev; - } - } - i--; - break; - } + index--; + if (index == -1) + index = POKEBALL_COUNT - 1; + newBall = gBallItemIds[index]; + if (CheckBagHasItem(newBall, 1)) + return newBall; } - return GetBagItemId(POCKET_POKE_BALLS, i); + return ballId; } static u32 GetNextBall(u32 ballId) { - u32 ballNext = ITEM_NONE; s32 i; - CompactItemsInBagPocket(POCKET_POKE_BALLS); - for (i = 1; i < gBagPockets[POCKET_POKE_BALLS].capacity; i++) + s32 index = ItemIdToBallId(ballId); + u32 newBall = 0; + for (i = 0; i < POKEBALL_COUNT; i++) { - if (ballId == GetBagItemId(POCKET_POKE_BALLS, i-1)) - { - ballNext = GetBagItemId(POCKET_POKE_BALLS, i); - break; - } + index++; + if (index == POKEBALL_COUNT) + index = 0; + newBall = gBallItemIds[index]; + if (CheckBagHasItem(newBall, 1)) + return newBall; } - if (ballNext == ITEM_NONE) - return GetBagItemId(POCKET_POKE_BALLS, 0); // Zeroth slot - else - return ballNext; + return ballId; } static void HandleInputChooseAction(u32 battler)