From d1c2a10af68351cb0c54a5c1851bd4e058007b9b Mon Sep 17 00:00:00 2001 From: Alex <93446519+AlexOn1ine@users.noreply.github.com> Date: Thu, 4 Apr 2024 19:52:45 +0200 Subject: [PATCH] Fixes switch in AI bug (#4338) * Fixes switch in AI bug * fixes fix * test --- src/battle_ai_switch_items.c | 4 ++-- test/battle/ai.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/battle_ai_switch_items.c b/src/battle_ai_switch_items.c index 231ab442a5e0..ae56264d8706 100644 --- a/src/battle_ai_switch_items.c +++ b/src/battle_ai_switch_items.c @@ -1530,7 +1530,7 @@ static u32 GetSwitchinHitsToKO(s32 damageTaken, u32 battler) s32 currentHP = startingHP; // No damage being dealt - if (damageTaken + statusDamage + recurringDamage == 0) + if (damageTaken + statusDamage + recurringDamage < recurringHealing) return startingHP; // Mon fainted to hazards @@ -1589,7 +1589,7 @@ static u32 GetSwitchinHitsToKO(s32 damageTaken, u32 battler) } // Healing from items occurs before status so we can do the rest in one line - if (currentHP != 0) + if (currentHP >= 0) currentHP = currentHP + recurringHealing - recurringDamage - statusDamage; // Recalculate toxic damage if needed diff --git a/test/battle/ai.c b/test/battle/ai.c index b52400acc9f7..c8a64facebde 100644 --- a/test/battle/ai.c +++ b/test/battle/ai.c @@ -526,6 +526,25 @@ AI_SINGLE_BATTLE_TEST("AI will choose either Rock Tomb or Bulldoze if Stat drop } } +AI_SINGLE_BATTLE_TEST("AI_FLAG_SMART_MON_CHOICES: Number of hits to KO calculation checks whether incoming damage is less than recurring healing to avoid an infinite loop") +{ + GIVEN { + AI_FLAGS(AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_TRY_TO_FAINT | AI_FLAG_CHECK_VIABILITY | AI_FLAG_SMART_SWITCHING | AI_FLAG_SMART_MON_CHOICES); + PLAYER(SPECIES_VENUSAUR) { Level(30); Moves(MOVE_TACKLE); } + // Opponent party courtesy of Skolgrahd, who triggered the bug in the first place + OPPONENT(SPECIES_PIKACHU) { Level(100); Moves(MOVE_ZIPPY_ZAP, MOVE_EXTREME_SPEED, MOVE_IRON_TAIL, MOVE_KNOCK_OFF); } + OPPONENT(SPECIES_NINETALES_ALOLAN) { Level(100); Moves(MOVE_AURORA_VEIL, MOVE_BLIZZARD, MOVE_MOONBLAST, MOVE_DISABLE); } + OPPONENT(SPECIES_WEAVILE) { Level(100); Moves(MOVE_NIGHT_SLASH, MOVE_TRIPLE_AXEL, MOVE_ICE_SHARD, MOVE_FAKE_OUT); } + OPPONENT(SPECIES_DITTO) { Level(100); Moves(MOVE_TRANSFORM); } + OPPONENT(SPECIES_TYPHLOSION) { Level(100); Moves(MOVE_ERUPTION, MOVE_HEAT_WAVE, MOVE_FOCUS_BLAST, MOVE_EXTRASENSORY); } + OPPONENT(SPECIES_UMBREON) { Level(100); Item(ITEM_LEFTOVERS); Moves(MOVE_FOUL_PLAY, MOVE_SNARL, MOVE_HELPING_HAND, MOVE_THUNDER_WAVE); } + } WHEN { + TURN { MOVE(player, MOVE_TACKLE); EXPECT_MOVES(opponent, MOVE_ZIPPY_ZAP, MOVE_EXTREME_SPEED, MOVE_IRON_TAIL, MOVE_KNOCK_OFF); } + } SCENE { + MESSAGE("Venusaur fainted!"); + } +} + AI_SINGLE_BATTLE_TEST("AI_FLAG_SMART_MON_CHOICES: AI will not switch in a Pokemon which is slower and gets 1HKOed after fainting") { bool32 alakazamFirst;