Small code deduplication in battle_ai_switch.c (#8790)

This commit is contained in:
Alex 2026-01-04 21:15:48 +01:00 committed by GitHub
commit 52455d2c59
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 31 deletions

View File

@ -1074,9 +1074,9 @@ extern u8 gCategoryIconSpriteId;
static inline bool32 IsBattlerAlive(u32 battler)
{
if (gBattleMons[battler].hp == 0)
if (battler >= gBattlersCount)
return FALSE;
else if (battler >= gBattlersCount)
else if (gBattleMons[battler].hp == 0)
return FALSE;
else if (gAbsentBattlerFlags & (1u << battler))
return FALSE;

View File

@ -923,42 +923,35 @@ static bool32 ShouldSwitchIfAbilityBenefit(u32 battler)
return SetSwitchinAndSwitch(battler, PARTY_SIZE);
}
static bool32 CanUseSuperEffectiveMoveAgainstOpponents(u32 battler)
static bool32 CanUseSuperEffectiveMoveAgainstOpponent(u32 battler, u32 opposingBattler)
{
enum Move move;
if (!IsBattlerAlive(opposingBattler))
return FALSE;
for (u32 moveIndex = 0; moveIndex < MAX_MON_MOVES; moveIndex++)
{
move = gBattleMons[battler].moves[moveIndex];
if (move == MOVE_NONE || AI_DoesChoiceEffectBlockMove(battler, move))
continue;
if (gAiLogicData->effectiveness[battler][opposingBattler][moveIndex] >= UQ_4_12(2.0))
return TRUE;
}
return FALSE;
}
static bool32 CanUseSuperEffectiveMoveAgainstOpponents(u32 battler)
{
u32 opposingPosition = BATTLE_OPPOSITE(GetBattlerPosition(battler));
u32 opposingBattler = GetBattlerAtPosition(opposingPosition);
if (!(gAbsentBattlerFlags & (1u << opposingBattler)))
{
for (u32 moveIndex = 0; moveIndex < MAX_MON_MOVES; moveIndex++)
{
move = gBattleMons[battler].moves[moveIndex];
if (move == MOVE_NONE || AI_DoesChoiceEffectBlockMove(battler, move))
continue;
if (CanUseSuperEffectiveMoveAgainstOpponent(battler, opposingBattler))
return TRUE;
if (gAiLogicData->effectiveness[battler][opposingBattler][moveIndex] >= UQ_4_12(2.0))
return TRUE;
}
}
if (!IsDoubleBattle())
return FALSE;
opposingBattler = BATTLE_PARTNER(opposingPosition);
if (!(gAbsentBattlerFlags & (1u << opposingBattler)))
{
for (u32 moveIndex = 0; moveIndex < MAX_MON_MOVES; moveIndex++)
{
move = gBattleMons[battler].moves[moveIndex];
if (move == MOVE_NONE || AI_DoesChoiceEffectBlockMove(battler, move))
continue;
if (gAiLogicData->effectiveness[battler][opposingBattler][moveIndex] >= UQ_4_12(2.0))
return TRUE;
}
}
if (IsDoubleBattle() && CanUseSuperEffectiveMoveAgainstOpponent(battler, BATTLE_PARTNER(opposingPosition)))
return TRUE;
return FALSE;
}