Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes target TURN_DAMAGED and MAX_HP to inlines #5822

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions include/battle.h
Original file line number Diff line number Diff line change
Expand Up @@ -869,10 +869,6 @@ STATIC_ASSERT(sizeof(((struct BattleStruct *)0)->palaceFlags) * 8 >= MAX_BATTLER
#define IS_MOVE_STATUS(move)(gMovesInfo[move].category == DAMAGE_CATEGORY_STATUS)
#define IS_MOVE_RECOIL(move)(gMovesInfo[move].recoil > 0 || gMovesInfo[move].effect == EFFECT_RECOIL_IF_MISS)

#define BATTLER_MAX_HP(battlerId)(gBattleMons[battlerId].hp == gBattleMons[battlerId].maxHP)
#define TARGET_TURN_DAMAGED ((gSpecialStatuses[gBattlerTarget].physicalDmg != 0 || gSpecialStatuses[gBattlerTarget].specialDmg != 0) || (gBattleStruct->enduredDamage & (1u << gBattlerTarget)))
#define BATTLER_TURN_DAMAGED(battlerId) ((gSpecialStatuses[battlerId].physicalDmg != 0 || gSpecialStatuses[battlerId].specialDmg != 0) || (gBattleStruct->enduredDamage & (1u << battler)))

/* Checks if 'battlerId' is any of the types.
* Passing multiple types is more efficient than calling this multiple
* times with one type because it shares the 'GetBattlerTypes' result. */
Expand Down Expand Up @@ -1196,6 +1192,18 @@ extern bool8 gLastUsedBallMenuPresent;
extern u8 gPartyCriticalHits[PARTY_SIZE];
extern u8 gCategoryIconSpriteId;

static inline bool32 IsBattlerTurnDamaged(u32 battler)
{
return gSpecialStatuses[battler].physicalDmg != 0
|| gSpecialStatuses[battler].specialDmg != 0
|| gBattleStruct->enduredDamage & (1u << battler);
}

static inline bool32 IsBattlerAtMaxHp(u32 battler)
{
return gBattleMons[battler].hp == gBattleMons[battler].maxHP;
}

static inline u32 GetBattlerPosition(u32 battler)
{
return gBattlerPositions[battler];
Expand Down
2 changes: 1 addition & 1 deletion include/battle_ai_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void RestoreBattlerData(u32 battlerId);
u32 GetAIChosenMove(u32 battlerId);
u32 GetTotalBaseStat(u32 species);
bool32 IsTruantMonVulnerable(u32 battlerAI, u32 opposingBattler);
bool32 AtMaxHp(u32 battler);
bool32 AI_BattlerAtMaxHp(u32 battler);
u32 GetHealthPercentage(u32 battler);
bool32 IsBattlerTrapped(u32 battler, bool32 switching);
s32 AI_WhoStrikesFirst(u32 battlerAI, u32 battler2, u32 moveConsidered);
Expand Down
16 changes: 8 additions & 8 deletions src/battle_ai_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1663,7 +1663,7 @@ static s32 AI_CheckBadMove(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
}
else
{
if (AtMaxHp(battlerAtk))
if (AI_BattlerAtMaxHp(battlerAtk))
ADJUST_SCORE(-10);
else if (aiData->hpPercents[battlerAtk] >= 80)
ADJUST_SCORE(-5); // do it if nothing better
Expand Down Expand Up @@ -1802,7 +1802,7 @@ static s32 AI_CheckBadMove(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
case EFFECT_RESTORE_HP:
case EFFECT_SOFTBOILED:
case EFFECT_ROOST:
if (AtMaxHp(battlerAtk))
if (AI_BattlerAtMaxHp(battlerAtk))
ADJUST_SCORE(-10);
else if (aiData->hpPercents[battlerAtk] >= 90)
ADJUST_SCORE(-9); //No point in healing, but should at least do it if nothing better
Expand All @@ -1812,7 +1812,7 @@ static s32 AI_CheckBadMove(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
case EFFECT_MOONLIGHT:
if ((AI_GetWeather(aiData) & (B_WEATHER_RAIN | B_WEATHER_SANDSTORM | B_WEATHER_HAIL | B_WEATHER_SNOW | B_WEATHER_FOG)))
ADJUST_SCORE(-3);
else if (AtMaxHp(battlerAtk))
else if (AI_BattlerAtMaxHp(battlerAtk))
ADJUST_SCORE(-10);
else if (aiData->hpPercents[battlerAtk] >= 90)
ADJUST_SCORE(-9); //No point in healing, but should at least do it if nothing better
Expand All @@ -1822,7 +1822,7 @@ static s32 AI_CheckBadMove(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
ADJUST_SCORE(-10);
else if (battlerDef == BATTLE_PARTNER(battlerAtk))
break; //Always heal your ally
else if (AtMaxHp(battlerAtk))
else if (AI_BattlerAtMaxHp(battlerAtk))
ADJUST_SCORE(-10);
else if (aiData->hpPercents[battlerAtk] >= 90)
ADJUST_SCORE(-8); //No point in healing, but should at least do it if nothing better
Expand Down Expand Up @@ -2390,7 +2390,7 @@ static s32 AI_CheckBadMove(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
{
if (gStatuses3[battlerDef] & STATUS3_HEAL_BLOCK)
return 0; // cannot even select
if (AtMaxHp(battlerDef))
if (AI_BattlerAtMaxHp(battlerDef))
ADJUST_SCORE(-10);
else if (gBattleMons[battlerDef].hp > gBattleMons[battlerDef].maxHP / 2)
ADJUST_SCORE(-5);
Expand Down Expand Up @@ -2559,8 +2559,8 @@ static s32 AI_CheckBadMove(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
ADJUST_SCORE(-10);
break;
case EFFECT_JUNGLE_HEALING:
if (AtMaxHp(battlerAtk)
&& AtMaxHp(BATTLE_PARTNER(battlerAtk))
if (AI_BattlerAtMaxHp(battlerAtk)
&& AI_BattlerAtMaxHp(BATTLE_PARTNER(battlerAtk))
&& !(gBattleMons[battlerAtk].status1 & STATUS1_ANY)
&& !(gBattleMons[BATTLE_PARTNER(battlerAtk)].status1 & STATUS1_ANY))
ADJUST_SCORE(-10);
Expand Down Expand Up @@ -2685,7 +2685,7 @@ static s32 AI_DoubleBattle(u32 battlerAtk, u32 battlerDef, u32 move, s32 score)
if (IsMoveEffectWeather(move))
ADJUST_SCORE(-10);
break;
}
}
} // check partner move effect

// Adjust for always crit moves
Expand Down
12 changes: 6 additions & 6 deletions src/battle_ai_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ u32 GetHealthPercentage(u32 battlerId)
return (u32)((100 * gBattleMons[battlerId].hp) / gBattleMons[battlerId].maxHP);
}

bool32 AtMaxHp(u32 battlerId)
bool32 AI_BattlerAtMaxHp(u32 battlerId)
{
if (AI_DATA->hpPercents[battlerId] == 100)
return TRUE;
Expand Down Expand Up @@ -1142,7 +1142,7 @@ s32 AI_WhoStrikesFirst(u32 battlerAI, u32 battler, u32 moveConsidered)

static bool32 CanEndureHit(u32 battler, u32 battlerTarget, u32 move)
{
if (!BATTLER_MAX_HP(battlerTarget) || gMovesInfo[move].effect == EFFECT_MULTI_HIT)
if (!AI_BattlerAtMaxHp(battlerTarget) || gMovesInfo[move].effect == EFFECT_MULTI_HIT)
return FALSE;
if (gMovesInfo[move].strikeCount > 1 && !(gMovesInfo[move].effect == EFFECT_DRAGON_DARTS && IsValidDoubleBattle(battlerTarget)))
return FALSE;
Expand Down Expand Up @@ -1564,7 +1564,7 @@ bool32 ShouldTryOHKO(u32 battlerAtk, u32 battlerDef, u32 atkAbility, u32 defAbil
gPotentialItemEffectBattler = battlerDef;
if (holdEffect == HOLD_EFFECT_FOCUS_BAND && (Random() % 100) < AI_DATA->holdEffectParams[battlerDef])
return FALSE; //probabilistically speaking, focus band should activate so dont OHKO
else if (holdEffect == HOLD_EFFECT_FOCUS_SASH && AtMaxHp(battlerDef))
else if (holdEffect == HOLD_EFFECT_FOCUS_SASH && AI_BattlerAtMaxHp(battlerDef))
return FALSE;

if (!DoesBattlerIgnoreAbilityChecks(atkAbility, move) && defAbility == ABILITY_STURDY)
Expand Down Expand Up @@ -2761,15 +2761,15 @@ enum AIPivot ShouldPivot(u32 battlerAtk, u32 battlerDef, u32 defAbility, u32 mov
return SHOULD_PIVOT; // Won't get the two turns, pivot

if (!IS_MOVE_STATUS(move) && ((AI_DATA->shouldSwitch & (1u << battlerAtk))
|| (AtMaxHp(battlerDef) && (AI_DATA->holdEffects[battlerDef] == HOLD_EFFECT_FOCUS_SASH
|| (AI_BattlerAtMaxHp(battlerDef) && (AI_DATA->holdEffects[battlerDef] == HOLD_EFFECT_FOCUS_SASH
|| (B_STURDY >= GEN_5 && defAbility == ABILITY_STURDY)
|| defAbility == ABILITY_MULTISCALE
|| defAbility == ABILITY_SHADOW_SHIELD))))
return SHOULD_PIVOT; // pivot to break sash/sturdy/multiscale
}
else if (!hasStatBoost)
{
if (!IS_MOVE_STATUS(move) && (AtMaxHp(battlerDef) && (AI_DATA->holdEffects[battlerDef] == HOLD_EFFECT_FOCUS_SASH
if (!IS_MOVE_STATUS(move) && (AI_BattlerAtMaxHp(battlerDef) && (AI_DATA->holdEffects[battlerDef] == HOLD_EFFECT_FOCUS_SASH
|| (B_STURDY >= GEN_5 && defAbility == ABILITY_STURDY)
|| defAbility == ABILITY_MULTISCALE
|| defAbility == ABILITY_SHADOW_SHIELD)))
Expand Down Expand Up @@ -2848,7 +2848,7 @@ enum AIPivot ShouldPivot(u32 battlerAtk, u32 battlerDef, u32 defAbility, u32 mov
// can knock out foe in 2 hits
if (IS_MOVE_STATUS(move) && ((AI_DATA->shouldSwitch & (1u << battlerAtk)) //Damaging move
//&& (switchScore >= SWITCHING_INCREASE_RESIST_ALL_MOVES + SWITCHING_INCREASE_KO_FOE //remove hazards
|| (AI_DATA->holdEffects[battlerDef] == HOLD_EFFECT_FOCUS_SASH && AtMaxHp(battlerDef))))
|| (AI_DATA->holdEffects[battlerDef] == HOLD_EFFECT_FOCUS_SASH && AI_BattlerAtMaxHp(battlerDef))))
return DONT_PIVOT; // Pivot to break the sash
else
return CAN_TRY_PIVOT;
Expand Down
2 changes: 1 addition & 1 deletion src/battle_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4854,7 +4854,7 @@ s8 GetMovePriority(u32 battler, u16 move)
return gMovesInfo[MOVE_MAX_GUARD].priority;

if (ability == ABILITY_GALE_WINGS
&& (B_GALE_WINGS < GEN_7 || BATTLER_MAX_HP(battler))
&& (B_GALE_WINGS < GEN_7 || IsBattlerAtMaxHp(battler))
&& gMovesInfo[move].type == TYPE_FLYING)
{
priority++;
Expand Down
30 changes: 15 additions & 15 deletions src/battle_script_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -2208,12 +2208,12 @@ static void Cmd_adjustdamage(void)
RecordItemEffectBattle(battlerDef, holdEffect);
gSpecialStatuses[battlerDef].focusBanded = TRUE;
}
else if (B_STURDY >= GEN_5 && GetBattlerAbility(battlerDef) == ABILITY_STURDY && BATTLER_MAX_HP(battlerDef))
else if (B_STURDY >= GEN_5 && GetBattlerAbility(battlerDef) == ABILITY_STURDY && IsBattlerAtMaxHp(battlerDef))
{
RecordAbilityBattle(battlerDef, ABILITY_STURDY);
gSpecialStatuses[battlerDef].sturdied = TRUE;
}
else if (holdEffect == HOLD_EFFECT_FOCUS_SASH && BATTLER_MAX_HP(battlerDef))
else if (holdEffect == HOLD_EFFECT_FOCUS_SASH && IsBattlerAtMaxHp(battlerDef))
{
RecordItemEffectBattle(battlerDef, holdEffect);
gSpecialStatuses[battlerDef].focusSashed = TRUE;
Expand Down Expand Up @@ -5997,7 +5997,7 @@ static void Cmd_moveend(void)
&& gBattlerAttacker != gBattlerTarget
&& GetBattlerSide(gBattlerAttacker) != GetBattlerSide(gBattlerTarget)
&& MoveResultHasEffect(gBattlerTarget)
&& TARGET_TURN_DAMAGED
&& IsBattlerTurnDamaged(gBattlerTarget)
&& gMovesInfo[gCurrentMove].power != 0
&& CompareStat(gBattlerTarget, STAT_ATK, MAX_STAT_STAGE, CMP_LESS_THAN))
{
Expand Down Expand Up @@ -6282,7 +6282,7 @@ static void Cmd_moveend(void)
if (gBattlerAttacker != gBattlerTarget
&& gMovesInfo[gCurrentMove].category != DAMAGE_CATEGORY_STATUS
&& MoveResultHasEffect(gBattlerTarget)
&& TARGET_TURN_DAMAGED)
&& IsBattlerTurnDamaged(gBattlerTarget))
{
gBattleStruct->timesGotHit[GetBattlerSide(gBattlerTarget)][gBattlerPartyIndexes[gBattlerTarget]]++;
}
Expand Down Expand Up @@ -6563,7 +6563,7 @@ static void Cmd_moveend(void)
&& gBattleMons[gBattlerAttacker].item == ITEM_NONE
&& gBattleMons[gBattlerTarget].item != ITEM_NONE
&& IsBattlerAlive(gBattlerAttacker)
&& TARGET_TURN_DAMAGED
&& IsBattlerTurnDamaged(gBattlerTarget)
&& CanStealItem(gBattlerAttacker, gBattlerTarget, gBattleMons[gBattlerTarget].item)
&& !gSpecialStatuses[gBattlerAttacker].gemBoost // In base game, gems are consumed after magician would activate.
&& !(gWishFutureKnock.knockedOffMons[GetBattlerSide(gBattlerTarget)] & (1u << gBattlerPartyIndexes[gBattlerTarget]))
Expand Down Expand Up @@ -6609,7 +6609,7 @@ static void Cmd_moveend(void)
continue;
// Since we check if battler was damaged, we don't need to check move result.
// In fact, doing so actually prevents multi-target moves from activating eject button properly
if (!BATTLER_TURN_DAMAGED(battler))
if (!IsBattlerTurnDamaged(battler))
continue;
}
else if (ejectPackBattlers & (1u << battler))
Expand Down Expand Up @@ -6704,7 +6704,7 @@ static void Cmd_moveend(void)
if (redCardBattlers & (1u << battler)
&& IsBattlerAlive(battler)
&& !DoesSubstituteBlockMove(gBattlerAttacker, battler, gCurrentMove)
&& BATTLER_TURN_DAMAGED(battler)
&& IsBattlerTurnDamaged(battler)
&& CanBattlerSwitch(gBattlerAttacker))
{
gLastUsedItem = gBattleMons[battler].item;
Expand Down Expand Up @@ -6753,7 +6753,7 @@ static void Cmd_moveend(void)
// Attacker is mon who made contact, battler is mon with pickpocket
if (battler != gBattlerAttacker // Cannot pickpocket yourself
&& GetBattlerAbility(battler) == ABILITY_PICKPOCKET // Target must have pickpocket ability
&& BATTLER_TURN_DAMAGED(battler) // Target needs to have been damaged
&& IsBattlerTurnDamaged(battler) // Target needs to have been damaged
&& !DoesSubstituteBlockMove(gBattlerAttacker, battler, gCurrentMove) // Subsitute unaffected
&& IsBattlerAlive(battler) // Battler must be alive to pickpocket
&& gBattleMons[battler].item == ITEM_NONE // Pickpocketer can't have an item already
Expand Down Expand Up @@ -8514,7 +8514,7 @@ static bool32 TryCheekPouch(u32 battler, u32 itemId)
&& GetBattlerAbility(battler) == ABILITY_CHEEK_POUCH
&& !(gStatuses3[battler] & STATUS3_HEAL_BLOCK)
&& gBattleStruct->ateBerry[GetBattlerSide(battler)] & (1u << gBattlerPartyIndexes[battler])
&& !BATTLER_MAX_HP(battler))
&& !IsBattlerAtMaxHp(battler))
{
gBattleStruct->moveDamage[battler] = GetNonDynamaxMaxHP(battler) / 3;
if (gBattleStruct->moveDamage[battler] == 0)
Expand Down Expand Up @@ -9585,7 +9585,7 @@ static void Cmd_various(void)
case VARIOUS_JUMP_IF_FULL_HP:
{
VARIOUS_ARGS(const u8 *jumpInstr);
if (BATTLER_MAX_HP(battler))
if (IsBattlerAtMaxHp(battler))
gBattlescriptCurrInstr = cmd->jumpInstr;
else
gBattlescriptCurrInstr = cmd->nextInstr;
Expand Down Expand Up @@ -9687,7 +9687,7 @@ static void Cmd_various(void)
{
VARIOUS_ARGS(const u8 *failInstr);
if ((gStatuses3[battler] & (STATUS3_SEMI_INVULNERABLE | STATUS3_HEAL_BLOCK))
|| BATTLER_MAX_HP(battler)
|| IsBattlerAtMaxHp(battler)
|| !gBattleMons[battler].hp
|| !(IsBattlerGrounded(battler)))
{
Expand Down Expand Up @@ -12696,7 +12696,7 @@ static void Cmd_tryKO(void)
gSpecialStatuses[gBattlerTarget].focusBanded = TRUE;
RecordItemEffectBattle(gBattlerTarget, holdEffect);
}
else if (holdEffect == HOLD_EFFECT_FOCUS_SASH && BATTLER_MAX_HP(gBattlerTarget))
else if (holdEffect == HOLD_EFFECT_FOCUS_SASH && IsBattlerAtMaxHp(gBattlerTarget))
{
gSpecialStatuses[gBattlerTarget].focusSashed = TRUE;
RecordItemEffectBattle(gBattlerTarget, holdEffect);
Expand Down Expand Up @@ -17238,7 +17238,7 @@ void BS_TryHealPulse(void)
{
NATIVE_ARGS(const u8 *failInstr);

if (BATTLER_MAX_HP(gBattlerTarget))
if (IsBattlerAtMaxHp(gBattlerTarget))
{
gBattlescriptCurrInstr = cmd->failInstr;
}
Expand Down Expand Up @@ -17495,7 +17495,7 @@ void BS_TryActivateGulpMissile(void)

if (MoveResultHasEffect(gBattlerTarget)
&& !gProtectStructs[gBattlerAttacker].confusionSelfDmg
&& TARGET_TURN_DAMAGED
&& IsBattlerTurnDamaged(gBattlerTarget)
&& gBattleMons[gBattlerTarget].species != SPECIES_CRAMORANT
&& GetBattlerAbility(gBattlerTarget) == ABILITY_GULP_MISSILE)
{
Expand Down Expand Up @@ -17696,7 +17696,7 @@ void BS_TryHitSwitchTarget(void)
if (IsBattlerAlive(gBattlerAttacker)
&& IsBattlerAlive(gBattlerTarget)
&& MoveResultHasEffect(gBattlerTarget)
&& TARGET_TURN_DAMAGED
&& IsBattlerTurnDamaged(gBattlerTarget)
&& gSpecialStatuses[gBattlerAttacker].parentalBondState != PARENTAL_BOND_1ST_HIT
&& GetBattlerAbility(gBattlerTarget) != ABILITY_GUARD_DOG)
{
Expand Down
Loading
Loading