diff --git a/src/battle_ai_switch_items.c b/src/battle_ai_switch_items.c index fe3bb5a5ed..d6ac5d025e 100644 --- a/src/battle_ai_switch_items.c +++ b/src/battle_ai_switch_items.c @@ -1444,33 +1444,18 @@ static u32 GetBestMonBatonPass(struct Pokemon *party, int firstId, int lastId, u static u32 GetBestMonTypeMatchup(struct Pokemon *party, int firstId, int lastId, u8 invalidMons, u32 battler, u32 opposingBattler) { int i, bits = 0; - while (bits != 0x3F) // All mons were checked. { - uq4_12_t bestResist = UQ_4_12(1.0); + u32 bestResist = UQ_4_12(2.0); int bestMonId = PARTY_SIZE; // Find the mon whose type is the most suitable defensively. for (i = firstId; i < lastId; i++) { if (!((1u << i) & invalidMons) && !((1u << i) & bits)) { - u16 species = GetMonData(&party[i], MON_DATA_SPECIES); - uq4_12_t typeEffectiveness = UQ_4_12(1.0); + InitializeSwitchinCandidate(&party[i]); - u8 atkType1 = gBattleMons[opposingBattler].types[0]; - u8 atkType2 = gBattleMons[opposingBattler].types[1]; - u8 defType1 = GetSpeciesType(species, 0); - u8 defType2 = GetSpeciesType(species, 1); - - typeEffectiveness = uq4_12_multiply(typeEffectiveness, (GetTypeModifier(atkType1, defType1))); - if (atkType2 != atkType1) - typeEffectiveness = uq4_12_multiply(typeEffectiveness, (GetTypeModifier(atkType2, defType1))); - if (defType2 != defType1) - { - typeEffectiveness = uq4_12_multiply(typeEffectiveness, (GetTypeModifier(atkType1, defType2))); - if (atkType2 != atkType1) - typeEffectiveness = uq4_12_multiply(typeEffectiveness, (GetTypeModifier(atkType2, defType2))); - } + u32 typeEffectiveness = GetBattleMonTypeMatchup(gBattleMons[opposingBattler], gAiLogicData->switchinCandidate.battleMon); if (typeEffectiveness < bestResist) { bestResist = typeEffectiveness; diff --git a/test/battle/ai/ai_switching.c b/test/battle/ai/ai_switching.c index 45b50b7638..a858c33ded 100644 --- a/test/battle/ai/ai_switching.c +++ b/test/battle/ai/ai_switching.c @@ -1316,6 +1316,21 @@ AI_SINGLE_BATTLE_TEST("AI_FLAG_SMART_MON_CHOICES: AI will properly consider immu } } +AI_DOUBLE_BATTLE_TEST("AI_FLAG_SMART_MON_CHOICES: AI will properly consider immunities when determining switchin type matchup (Doubles)") +{ + GIVEN { + AI_FLAGS(AI_FLAG_CHECK_BAD_MOVE | AI_FLAG_CHECK_VIABILITY | AI_FLAG_TRY_TO_FAINT | AI_FLAG_SMART_MON_CHOICES | AI_FLAG_OMNISCIENT); + PLAYER(SPECIES_POLIWRATH) { Moves(MOVE_WATER_GUN, MOVE_KARATE_CHOP); } + PLAYER(SPECIES_ZIGZAGOON) { Moves(MOVE_CELEBRATE); } + OPPONENT(SPECIES_ZIGZAGOON) { Level(1); Moves(MOVE_CELEBRATE); } + OPPONENT(SPECIES_ZIGZAGOON) { Level(1); Moves(MOVE_CELEBRATE); } + OPPONENT(SPECIES_CERULEDGE) { Moves(MOVE_SPARK); } + OPPONENT(SPECIES_WHIMSICOTT) { Moves(MOVE_MEGA_DRAIN); } + } WHEN { + TURN { MOVE(playerLeft, MOVE_KARATE_CHOP, target:opponentLeft); MOVE(playerRight, MOVE_CELEBRATE); EXPECT_MOVE(opponentLeft, MOVE_CELEBRATE); EXPECT_MOVE(opponentRight, MOVE_CELEBRATE); EXPECT_SEND_OUT(opponentLeft, 3); } + } +} + AI_SINGLE_BATTLE_TEST("AI_FLAG_SMART_SWITCHING: AI won't switch out due to bad odds if it can OHKO with a priority move") { PASSES_RANDOMLY(100, 100, RNG_AI_SWITCH_HASBADODDS);