Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
psytp2 committed Sep 13, 2024
2 parents fa907f3 + 9bdc9e5 commit 0194db1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/battle_ai_switch_items.c
Original file line number Diff line number Diff line change
Expand Up @@ -1610,7 +1610,7 @@ static u32 GetSwitchinHitsToKO(s32 damageTaken, u32 battler)
u8 weatherDuration = gWishFutureKnock.weatherDuration, holdEffectParam = ItemId_GetHoldEffectParam(item);
u32 opposingBattler = GetBattlerAtPosition(BATTLE_OPPOSITE(GetBattlerPosition(battler)));
u32 opposingAbility = gBattleMons[opposingBattler].ability, ability = AI_DATA->switchinCandidate.battleMon.ability;
bool32 usedSingleUseHealingItem = FALSE;
bool32 usedSingleUseHealingItem = FALSE, opponentCanBreakMold = IsMoldBreakerTypeAbility(opposingBattler, opposingAbility);
s32 currentHP = startingHP;

// No damage being dealt
Expand All @@ -1632,7 +1632,7 @@ static u32 GetSwitchinHitsToKO(s32 damageTaken, u32 battler)
currentHP = currentHP - damageTaken;

// One shot prevention effects
if (damageTaken >= maxHP && currentHP == maxHP && (heldItemEffect == HOLD_EFFECT_FOCUS_SASH || (B_STURDY >= GEN_5 && ability == ABILITY_STURDY)))
if (damageTaken >= maxHP && currentHP == maxHP && (heldItemEffect == HOLD_EFFECT_FOCUS_SASH || (!opponentCanBreakMold && B_STURDY >= GEN_5 && ability == ABILITY_STURDY)))
currentHP = 1;

// If mon is still alive, apply weather impact first, as it might KO the mon before it can heal with its item (order is weather -> item -> status)
Expand Down Expand Up @@ -1697,6 +1697,10 @@ static u32 GetSwitchinHitsToKO(s32 damageTaken, u32 battler)
hitsToKO++;
}

// Disguise will always add an extra hit to KO
if (opponentCanBreakMold && AI_DATA->switchinCandidate.battleMon.species == SPECIES_MIMIKYU_DISGUISED)
hitsToKO++;

// If mon had a hypothetical status from TSpikes, clear it
if (AI_DATA->switchinCandidate.hypotheticalStatus == TRUE)
{
Expand Down
34 changes: 31 additions & 3 deletions src/battle_ai_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,26 @@ s32 AI_WhoStrikesFirst(u32 battlerAI, u32 battler, u32 moveConsidered)
return AI_IS_SLOWER;
}

static bool32 CanEndureHit(u32 battler, u32 battlerTarget, u32 move)
{
if (!BATTLER_MAX_HP(battlerTarget) || gMovesInfo[move].effect == EFFECT_MULTI_HIT)
return FALSE;
if (gMovesInfo[move].strikeCount > 1 && !(gMovesInfo[move].effect == EFFECT_DRAGON_DARTS && IsValidDoubleBattle(battlerTarget)))
return FALSE;
if (AI_DATA->holdEffects[battlerTarget] == HOLD_EFFECT_FOCUS_SASH)
return TRUE;

if (!DoesBattlerIgnoreAbilityChecks(AI_DATA->abilities[battler], move))
{
if (B_STURDY >= GEN_5 && AI_DATA->abilities[battlerTarget] == ABILITY_STURDY)
return TRUE;
if (gBattleMons[battlerTarget].species == SPECIES_MIMIKYU_DISGUISED)
return TRUE;
}

return FALSE;
}

// Check if target has means to faint ai mon.
bool32 CanTargetFaintAi(u32 battlerDef, u32 battlerAtk)
{
Expand All @@ -1123,7 +1143,8 @@ bool32 CanTargetFaintAi(u32 battlerDef, u32 battlerAtk)
for (i = 0; i < MAX_MON_MOVES; i++)
{
if (moves[i] != MOVE_NONE && moves[i] != MOVE_UNAVAILABLE && !(unusable & (1u << i))
&& AI_DATA->simulatedDmg[battlerDef][battlerAtk][i].expected >= gBattleMons[battlerAtk].hp)
&& AI_DATA->simulatedDmg[battlerDef][battlerAtk][i].expected >= gBattleMons[battlerAtk].hp
&& !CanEndureHit(battlerDef, battlerAtk, moves[i]))
{
return TRUE;
}
Expand Down Expand Up @@ -1210,7 +1231,13 @@ bool32 CanAIFaintTarget(u32 battlerAtk, u32 battlerDef, u32 numHits)
dmg *= numHits;

if (gBattleMons[battlerDef].hp <= dmg)
return TRUE;
{
if (numHits > 1)
return TRUE;

if (!CanEndureHit(battlerAtk, battlerDef, moves[i]))
return TRUE;
}
}
}

Expand Down Expand Up @@ -1922,13 +1949,14 @@ bool32 ShouldLowerEvasion(u32 battlerAtk, u32 battlerDef, u32 defAbility)
bool32 CanIndexMoveFaintTarget(u32 battlerAtk, u32 battlerDef, u32 index, u32 numHits)
{
s32 dmg;
u16 *moves = gBattleMons[battlerAtk].moves;

if (numHits)
dmg = AI_DATA->simulatedDmg[battlerAtk][battlerDef][index].expected * numHits;
else
dmg = AI_DATA->simulatedDmg[battlerAtk][battlerDef][index].minimum;

if (gBattleMons[battlerDef].hp <= dmg)
if (gBattleMons[battlerDef].hp <= dmg && !CanEndureHit(battlerAtk, battlerDef, moves[index]))
return TRUE;
return FALSE;
}
Expand Down

0 comments on commit 0194db1

Please sign in to comment.