From f42117a9d8064db3f126e338b9c52864f0cc9467 Mon Sep 17 00:00:00 2001 From: Pawkkie <61265402+Pawkkie@users.noreply.github.com> Date: Sun, 30 Nov 2025 05:56:07 -0500 Subject: [PATCH] Fix switchin KO threshold logic (#8370) Co-authored-by: Alex <93446519+AlexOn1ine@users.noreply.github.com> --- src/battle_ai_switch_items.c | 4 ++-- test/battle/ai/ai_switching.c | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/battle_ai_switch_items.c b/src/battle_ai_switch_items.c index 29b58ed9e6..02cc668d14 100644 --- a/src/battle_ai_switch_items.c +++ b/src/battle_ai_switch_items.c @@ -2240,7 +2240,7 @@ static u32 GetBestMonIntegrated(struct Pokemon *party, int firstId, int lastId, // Check if current mon can revenge kill in some capacity // If AI mon can one shot - if (damageDealt > playerMonHP) + if (damageDealt >= playerMonHP) { if (canSwitchinWin1v1) { @@ -2252,7 +2252,7 @@ static u32 GetBestMonIntegrated(struct Pokemon *party, int firstId, int lastId, } // If AI mon can two shot - if (damageDealt > playerMonHP / 2) + if (damageDealt >= (playerMonHP / 2 + playerMonHP % 2)) // Modulo to handle odd numbers in non-decimal division { if (canSwitchinWin1v1) { diff --git a/test/battle/ai/ai_switching.c b/test/battle/ai/ai_switching.c index 329add5a07..947c9209c2 100644 --- a/test/battle/ai/ai_switching.c +++ b/test/battle/ai/ai_switching.c @@ -1459,3 +1459,23 @@ AI_SINGLE_BATTLE_TEST("AI_FLAG_SMART_MON_CHOICES: AI will consider choice-locked TURN { MOVE(player, MOVE_MIGHTY_CLEAVE); EXPECT_MOVE(opponent, MOVE_TACKLE); item == ITEM_NONE ? EXPECT_SEND_OUT(opponent, 1) : EXPECT_SEND_OUT(opponent, 2); } } } + +AI_SINGLE_BATTLE_TEST("AI_FLAG_SMART_MON_CHOICES: AI considers both meeting and exceeding KO thresholds correctly") +{ + u32 hp; + PARAMETRIZE { hp = 40; } + PARAMETRIZE { hp = 80; } + PARAMETRIZE { hp = 79; } + PARAMETRIZE { hp = 81; } + GIVEN { + ASSUME(GetMoveEffect(MOVE_DRAGON_RAGE) == EFFECT_FIXED_HP_DAMAGE); + ASSUME(GetMoveFixedHPDamage(MOVE_DRAGON_RAGE) == 40); + 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 | AI_FLAG_OMNISCIENT); + PLAYER(SPECIES_ZIGZAGOON) { Speed(5); HP(hp); Moves(MOVE_PROTECT, MOVE_TACKLE); } + OPPONENT(SPECIES_ZIGZAGOON) { Speed(6); Moves(MOVE_EXPLOSION); } + OPPONENT(SPECIES_ZIGZAGOON) { Speed(6); Moves(MOVE_DRAGON_RAGE); } + OPPONENT(SPECIES_BELDUM) { Speed(4); Moves(MOVE_TACKLE); } + } WHEN { + TURN { MOVE(player, MOVE_PROTECT); EXPECT_MOVE(opponent, MOVE_EXPLOSION); hp > 80 ? EXPECT_SEND_OUT(opponent, 2) : EXPECT_SEND_OUT(opponent, 1); } + } +}