From 70ca5060f9c281e2916b3f45badce27d69d1b47f Mon Sep 17 00:00:00 2001 From: Alex <93446519+AlexOn1ine@users.noreply.github.com> Date: Mon, 18 Nov 2024 23:18:26 +0100 Subject: [PATCH] Fixes wrong Id when AI chooses mon to switch in (#5684) --- src/battle_controller_opponent.c | 35 ++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/battle_controller_opponent.c b/src/battle_controller_opponent.c index ef12589681..b6ffd67b75 100644 --- a/src/battle_controller_opponent.c +++ b/src/battle_controller_opponent.c @@ -648,6 +648,22 @@ static void OpponentHandleChooseItem(u32 battler) OpponentBufferExecCompleted(battler); } +static inline bool32 IsAcePokemon(u32 chosenMonId, u32 pokemonInBattle, u32 battler) +{ + return AI_THINKING_STRUCT->aiFlags[battler] & AI_FLAG_ACE_POKEMON + && (chosenMonId == CalculateEnemyPartyCountInSide(battler) - 1) + && CountAIAliveNonEggMonsExcept(PARTY_SIZE) != pokemonInBattle; +} + +static inline bool32 IsDoubleAcePokemon(u32 chosenMonId, u32 pokemonInBattle, u32 battler) +{ + return AI_THINKING_STRUCT->aiFlags[battler] & AI_FLAG_DOUBLE_ACE_POKEMON + && (chosenMonId == CalculateEnemyPartyCountInSide(battler) - 1) + && (chosenMonId == CalculateEnemyPartyCountInSide(battler) - 2) + && CountAIAliveNonEggMonsExcept(PARTY_SIZE) != pokemonInBattle + && CountAIAliveNonEggMonsExcept(PARTY_SIZE-1) != pokemonInBattle; +} + static void OpponentHandleChoosePokemon(u32 battler) { s32 chosenMonId; @@ -680,20 +696,14 @@ static void OpponentHandleChoosePokemon(u32 battler) GetAIPartyIndexes(battler, &firstId, &lastId); for (chosenMonId = (lastId-1); chosenMonId >= firstId; chosenMonId--) { - if (!IsValidForBattle(&gEnemyParty[chosenMonId])) - continue; - if (chosenMonId == gBattlerPartyIndexes[battler1] + if (!IsValidForBattle(&gEnemyParty[chosenMonId]) + || chosenMonId == gBattlerPartyIndexes[battler1] || chosenMonId == gBattlerPartyIndexes[battler2]) continue; - if ((AI_THINKING_STRUCT->aiFlags[battler] & AI_FLAG_ACE_POKEMON) - && ((chosenMonId == CalculateEnemyPartyCountInSide(battler) - 1) || CountAIAliveNonEggMonsExcept(PARTY_SIZE) == pokemonInBattle)) - continue; - if ((AI_THINKING_STRUCT->aiFlags[battler] & AI_FLAG_DOUBLE_ACE_POKEMON) - && (((chosenMonId == CalculateEnemyPartyCountInSide(battler) - 1) || (chosenMonId == CalculateEnemyPartyCountInSide(battler) - 2)) - || (CountAIAliveNonEggMonsExcept(PARTY_SIZE) == pokemonInBattle || CountAIAliveNonEggMonsExcept(PARTY_SIZE-1) == pokemonInBattle))) - continue; - // mon is valid - break; + + if (!IsAcePokemon(chosenMonId, pokemonInBattle, battler) + && !IsDoubleAcePokemon(chosenMonId, pokemonInBattle, battler)) + break; } } gBattleStruct->monToSwitchIntoId[battler] = chosenMonId; @@ -709,7 +719,6 @@ static void OpponentHandleChoosePokemon(u32 battler) #endif // TESTING BtlController_EmitChosenMonReturnValue(battler, BUFFER_B, chosenMonId, NULL); OpponentBufferExecCompleted(battler); - } static u8 CountAIAliveNonEggMonsExcept(u8 slotToIgnore)