From 4dff0362df7bd41d3d05b6168c639932f8280681 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Mon, 11 Sep 2023 00:01:12 +0200 Subject: [PATCH] Fix ai vs ai battles switch issue (#3283) --- src/battle_controller_player_partner.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/battle_controller_player_partner.c b/src/battle_controller_player_partner.c index 44c10c55c0..361460ac72 100644 --- a/src/battle_controller_player_partner.c +++ b/src/battle_controller_player_partner.c @@ -397,19 +397,21 @@ static void PlayerPartnerHandleChoosePokemon(u32 battler) chosenMonId = gSelectedMonPartyId = GetFirstFaintedPartyIndex(battler); } // Switching out - else if (gBattleStruct->monToSwitchIntoId[battler] == PARTY_SIZE) + else if (gBattleStruct->monToSwitchIntoId[battler] >= PARTY_SIZE || !IsValidForBattle(&gPlayerParty[gBattleStruct->monToSwitchIntoId[battler]])) { chosenMonId = GetMostSuitableMonToSwitchInto(battler); - if (chosenMonId == PARTY_SIZE) // just switch to the next mon - { - u8 playerMonIdentity = GetBattlerAtPosition(B_POSITION_PLAYER_LEFT); - u8 selfIdentity = GetBattlerAtPosition(B_POSITION_PLAYER_RIGHT); - for (chosenMonId = PARTY_SIZE / 2; chosenMonId < PARTY_SIZE; chosenMonId++) + if (chosenMonId == PARTY_SIZE || !IsValidForBattle(&gPlayerParty[chosenMonId])) // just switch to the next mon + { + s32 firstId = (IsAiVsAiBattle()) ? 0 : (PARTY_SIZE / 2); + u32 battler1 = GetBattlerAtPosition(B_POSITION_PLAYER_LEFT); + u32 battler2 = IsDoubleBattle() ? GetBattlerAtPosition(B_POSITION_PLAYER_RIGHT) : battler1; + + for (chosenMonId = firstId; chosenMonId < PARTY_SIZE; chosenMonId++) { if (GetMonData(&gPlayerParty[chosenMonId], MON_DATA_HP) != 0 - && chosenMonId != gBattlerPartyIndexes[playerMonIdentity] - && chosenMonId != gBattlerPartyIndexes[selfIdentity]) + && chosenMonId != gBattlerPartyIndexes[battler1] + && chosenMonId != gBattlerPartyIndexes[battler2]) { break; }