From ffe0936ff160cd3ebea858030fc4c8283d6e2cd3 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Wed, 8 Oct 2025 19:44:31 +0200 Subject: [PATCH] Fix ShouldPivot overwriting random memory (#7882) Co-authored-by: Alex <93446519+AlexOn1ine@users.noreply.github.com> --- src/battle_ai_util.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 5237c4aff2..5913c0842c 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -3174,14 +3174,16 @@ enum AIPivot ShouldPivot(u32 battlerAtk, u32 battlerDef, u32 defAbility, u32 mov u32 battlerToSwitch; u32 predictedMoveSpeedCheck = GetIncomingMoveSpeedCheck(battlerAtk, battlerDef, gAiLogicData); - battlerToSwitch = gBattleStruct->AI_monToSwitchIntoId[battlerAtk]; - // Palafin always wants to activate Zero to Hero if (gBattleMons[battlerAtk].species == SPECIES_PALAFIN_ZERO && gBattleMons[battlerAtk].ability == ABILITY_ZERO_TO_HERO && CountUsablePartyMons(battlerAtk) != 0) return SHOULD_PIVOT; + battlerToSwitch = gAiLogicData->mostSuitableMonId[battlerAtk]; + // This shouldn't ever happen, but it's there to make sure we don't accidentally read past the gParty array. + if (battlerToSwitch >= PARTY_SIZE) + battlerToSwitch = 0; if (PartyBattlerShouldAvoidHazards(battlerAtk, battlerToSwitch)) return DONT_PIVOT;