From aefe5825964e5cd4d10dae3bad63a4bf5a33b4b2 Mon Sep 17 00:00:00 2001 From: Martin Griffin Date: Sat, 15 Apr 2023 00:52:14 +0100 Subject: [PATCH] GetSideParty/GetBattlerParty Inline GetBattlerPartyData into its callers because its name is confusing and we couldn't think of a better one. This seems to fix a bug in one place where GetBattlerPartyData was being used as if it had GetBattlerParty semantics. --- include/battle.h | 11 ++++++++ include/battle_util.h | 1 - src/battle_ai_util.c | 2 +- src/battle_anim_new.c | 3 -- src/battle_main.c | 7 +++-- src/battle_script_commands.c | 53 +++++++++------------------------- src/battle_util.c | 55 ++++++++---------------------------- 7 files changed, 42 insertions(+), 90 deletions(-) diff --git a/include/battle.h b/include/battle.h index 46975bec3..afa02a8f2 100644 --- a/include/battle.h +++ b/include/battle.h @@ -719,6 +719,17 @@ struct BattleStruct #define SET_STATCHANGER(statId, stage, goesDown)(gBattleScripting.statChanger = (statId) + ((stage) << 3) + (goesDown << 7)) #define SET_STATCHANGER2(dst, statId, stage, goesDown)(dst = (statId) + ((stage) << 3) + (goesDown << 7)) +static inline struct Pokemon *GetSideParty(u32 side) +{ + return side == B_SIDE_PLAYER ? gPlayerParty : gEnemyParty; +} + +static inline struct Pokemon *GetBattlerParty(u32 battlerId) +{ + extern u8 GetBattlerSide(u8 battler); + return GetSideParty(GetBattlerSide(battlerId)); +} + // NOTE: The members of this struct have hard-coded offsets // in include/constants/battle_script_commands.h struct BattleScripting diff --git a/include/battle_util.h b/include/battle_util.h index 0661081e4..308639487 100644 --- a/include/battle_util.h +++ b/include/battle_util.h @@ -188,7 +188,6 @@ bool32 SetIllusionMon(struct Pokemon *mon, u32 battlerId); bool8 ShouldGetStatBadgeBoost(u16 flagId, u8 battlerId); u8 GetBattleMoveSplit(u32 moveId); bool32 TestMoveFlags(u16 move, u32 flag); -struct Pokemon *GetBattlerPartyData(u8 battlerId); bool32 CanFling(u8 battlerId); bool32 IsTelekinesisBannedSpecies(u16 species); bool32 IsHealBlockPreventingMove(u32 battler, u32 move); diff --git a/src/battle_ai_util.c b/src/battle_ai_util.c index 05e7aec58..1128f385b 100644 --- a/src/battle_ai_util.c +++ b/src/battle_ai_util.c @@ -3420,7 +3420,7 @@ bool32 IsPartyFullyHealedExceptBattler(u8 battlerId) bool32 PartyHasMoveSplit(u8 battlerId, u8 split) { u8 firstId, lastId; - struct Pokemon* party = GetBattlerPartyData(battlerId); + struct Pokemon *party = GetBattlerParty(battlerId); u32 i, j; for (i = 0; i < PARTY_SIZE; i++) diff --git a/src/battle_anim_new.c b/src/battle_anim_new.c index b3502fcb9..715e5b3e2 100644 --- a/src/battle_anim_new.c +++ b/src/battle_anim_new.c @@ -7897,9 +7897,6 @@ void AnimTask_TerrainPulse(u8 taskId) void AnimTask_AffectionHangedOn(u8 taskId) { - int side = GetBattlerSide(gBattleAnimTarget); - struct Pokemon *party = (side == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; - gBattleAnimArgs[0] = GetBattlerFriendshipScore(gBattleAnimTarget); DestroyAnimVisualTask(taskId); } diff --git a/src/battle_main.c b/src/battle_main.c index a5a9b5bbf..0babba241 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -3799,7 +3799,9 @@ static void TryDoEventsBeforeFirstTurn(void) { for (i = 0; i < gBattlersCount; i++) { - if (gBattleMons[i].hp == 0 || gBattleMons[i].species == SPECIES_NONE || GetMonData(GetBattlerPartyData(i), MON_DATA_IS_EGG)) + struct Pokemon *party = GetBattlerParty(i); + struct Pokemon *mon = &party[gBattlerPartyIndexes[i]]; + if (gBattleMons[i].hp == 0 || gBattleMons[i].species == SPECIES_NONE || GetMonData(mon, MON_DATA_IS_EGG)) gAbsentBattlerFlags |= gBitTable[i]; } } @@ -5033,7 +5035,8 @@ static void CheckMegaEvolutionBeforeTurn(void) if (gBattleStruct->mega.toEvolve & gBitTable[gActiveBattler] && !(gProtectStructs[gActiveBattler].noValidMoves)) { - struct Pokemon *mon = GetBattlerPartyData(gActiveBattler); + struct Pokemon *party = GetBattlerParty(gActiveBattler); + struct Pokemon *mon = &party[gBattlerPartyIndexes[gActiveBattler]]; gBattleStruct->mega.toEvolve &= ~(gBitTable[gActiveBattler]); gLastUsedItem = gBattleMons[gActiveBattler].item; diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index d313697b0..2a7e2916d 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -6468,10 +6468,7 @@ bool32 CanBattlerSwitch(u32 battlerId) } else if (gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER) { - if (GetBattlerSide(battlerId) == B_SIDE_OPPONENT) - party = gEnemyParty; - else - party = gPlayerParty; + party = GetBattlerParty(battlerId); lastMonId = 0; if (battlerId & 2) @@ -6512,10 +6509,7 @@ bool32 CanBattlerSwitch(u32 battlerId) } else { - if (GetBattlerSide(battlerId) == B_SIDE_OPPONENT) - party = gEnemyParty; - else - party = gPlayerParty; + party = GetBattlerParty(battlerId); lastMonId = 0; if (GetLinkTrainerFlankId(GetBattlerMultiplayerId(battlerId)) == TRUE) @@ -7669,10 +7663,7 @@ static void Cmd_drawpartystatussummary(void) gActiveBattler = GetBattlerForBattleScript(cmd->battler); - if (GetBattlerSide(gActiveBattler) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; + party = GetBattlerParty(gActiveBattler); for (i = 0; i < PARTY_SIZE; i++) { @@ -8746,7 +8737,8 @@ static bool32 CourtChangeSwapSideStatuses(void) static void HandleScriptMegaPrimal(u32 caseId, u32 battlerId, bool32 isMega) { - struct Pokemon *mon = GetBattlerPartyData(battlerId); + struct Pokemon *party = GetBattlerParty(battlerId); + struct Pokemon *mon = &party[gBattlerPartyIndexes[battlerId]]; u32 position = GetBattlerPosition(battlerId); u32 side = GET_BATTLER_SIDE(battlerId); @@ -8814,8 +8806,7 @@ static void HandleScriptMegaPrimal(u32 caseId, u32 battlerId, bool32 isMega) static bool32 CanTeleport(u8 battlerId) { - u8 side = GetBattlerSide(battlerId); - struct Pokemon *party = (side == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetBattlerParty(battlerId); u32 species, count, i; for (i = 0; i < PARTY_SIZE; i++) @@ -11242,7 +11233,7 @@ static void Cmd_various(void) // Battler selected! Revive and go to next instruction. if (gSelectedMonPartyId != PARTY_SIZE) { - struct Pokemon *party = (side == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetSideParty(side); u16 hp = GetMonData(&party[gSelectedMonPartyId], MON_DATA_MAX_HP) / 2; BtlController_EmitSetMonData(BUFFER_A, REQUEST_HP_BATTLE, gBitTable[gSelectedMonPartyId], sizeof(hp), &hp); @@ -12326,10 +12317,7 @@ static void Cmd_forcerandomswitch(void) || redCardForcedSwitch ) { - if (GetBattlerSide(gBattlerTarget) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; + party = GetBattlerParty(gBattlerTarget); if (BATTLE_TWO_VS_ONE_OPPONENT && GetBattlerSide(gBattlerTarget) == B_SIDE_OPPONENT) { @@ -13528,16 +13516,11 @@ static void Cmd_healpartystatus(void) if (gCurrentMove == MOVE_HEAL_BELL) { - struct Pokemon *party; + struct Pokemon *party = GetBattlerParty(gBattlerAttacker); s32 i; gBattleCommunication[MULTISTRING_CHOOSER] = B_MSG_BELL; - if (GetBattlerSide(gBattlerAttacker) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; - if (GetBattlerAbility(gBattlerAttacker) != ABILITY_SOUNDPROOF) { gBattleMons[gBattlerAttacker].status1 = 0; @@ -14165,12 +14148,7 @@ static void Cmd_trydobeatup(void) gBattleStruct->beatUpSlot++; gBattlescriptCurrInstr = cmd->nextInstr; #else - struct Pokemon *party; - - if (GetBattlerSide(gBattlerAttacker) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; + struct Pokemon *party = GetBattlerParty(gBattlerAttacker); if (gBattleMons[gBattlerTarget].hp == 0) { @@ -14864,10 +14842,7 @@ static void Cmd_assistattackselect(void) if (validMoves != NULL) { - if (GET_BATTLER_SIDE(gBattlerAttacker) != B_SIDE_PLAYER) - party = gEnemyParty; - else - party = gPlayerParty; + party = GetBattlerParty(gBattlerAttacker); for (monId = 0; monId < PARTY_SIZE; monId++) { @@ -16396,7 +16371,7 @@ u8 GetFirstFaintedPartyIndex(u8 battlerId) u32 i; u32 start = 0; u32 end = PARTY_SIZE; - struct Pokemon *party = (GetBattlerSide(battlerId) == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetBattlerParty(battlerId); // Check whether partner is separate trainer. if ((GetBattlerSide(battlerId) == B_SIDE_PLAYER && gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER) @@ -16435,7 +16410,7 @@ void BS_ItemRestoreHP(void) { u32 battlerId = MAX_BATTLERS_COUNT; u32 healParam = GetItemEffect(gLastUsedItem)[6]; u32 side = GetBattlerSide(gBattlerAttacker); - struct Pokemon *party = (side == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetSideParty(side); u16 hp = GetMonData(&party[gBattleStruct->itemPartyIndex[gBattlerAttacker]], MON_DATA_HP); u16 maxHP = GetMonData(&party[gBattleStruct->itemPartyIndex[gBattlerAttacker]], MON_DATA_MAX_HP); gBattleCommunication[MULTIUSE_STATE] = 0; @@ -16493,7 +16468,7 @@ void BS_ItemRestoreHP(void) { void BS_ItemCureStatus(void) { NATIVE_ARGS(); - struct Pokemon *party = (GetBattlerSide(gBattlerAttacker) == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetBattlerParty(gBattlerAttacker); // Heal Status1 conditions. HealStatusConditions(&party[gBattleStruct->itemPartyIndex[gBattlerAttacker]], gBattleStruct->itemPartyIndex[gBattlerAttacker], GetItemStatus1Mask(gLastUsedItem), gBattlerAttacker); diff --git a/src/battle_util.c b/src/battle_util.c index ac274bf0f..0a4fd61a1 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -225,14 +225,9 @@ static const u16 sEntrainmentTargetSimpleBeamBannedAbilities[] = static u8 CalcBeatUpPower(void) { - struct Pokemon *party; u8 basePower; u16 species; - - if (GetBattlerSide(gBattlerAttacker) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; + struct Pokemon *party = GetBattlerParty(gBattlerAttacker); // Party slot is incremented by the battle script for Beat Up after this damage calculation species = GetMonData(&party[gBattleStruct->beatUpSlot], MON_DATA_SPECIES); @@ -1988,7 +1983,7 @@ u8 GetImprisonedMovesCount(u8 battlerId, u16 move) u32 GetBattlerFriendshipScore(u8 battlerId) { u8 side = GetBattlerSide(battlerId); - struct Pokemon *party = (side == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetSideParty(side); u16 species = GetMonData(&party[gBattlerPartyIndexes[battlerId]], MON_DATA_SPECIES); if (side != B_SIDE_PLAYER) @@ -3747,14 +3742,9 @@ u8 AtkCanceller_UnableToUseMove(void) #if B_BEAT_UP >= GEN_5 else if (gBattleMoves[gCurrentMove].effect == EFFECT_BEAT_UP) { - struct Pokemon* party; + struct Pokemon* party = GetBattlerParty(gBattlerAttacker); int i; - if (GetBattlerSide(gBattlerAttacker) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; - for (i = 0; i < PARTY_SIZE; i++) { if (GetMonData(&party[i], MON_DATA_HP) @@ -3851,10 +3841,7 @@ bool8 HasNoMonsToSwitch(u8 battler, u8 partyIdBattlerOn1, u8 partyIdBattlerOn2) } else if (gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER) { - if (GetBattlerSide(battler) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; + party = GetBattlerParty(battler); playerId = ((battler & BIT_FLANK) / 2); for (i = playerId * MULTI_PARTY_SIZE; i < playerId * MULTI_PARTY_SIZE + MULTI_PARTY_SIZE; i++) @@ -3888,12 +3875,7 @@ bool8 HasNoMonsToSwitch(u8 battler, u8 partyIdBattlerOn1, u8 partyIdBattlerOn2) else { flankId = GetBattlerMultiplayerId(battler); - - if (GetBattlerSide(battler) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; - + party = GetBattlerParty(battler); playerId = GetLinkTrainerFlankId(flankId); } @@ -4062,7 +4044,7 @@ static void ShouldChangeFormInWeather(u8 battler) { int i; int side = GetBattlerSide(battler); - struct Pokemon *party = (side == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetSideParty(side); for (i = 0; i < PARTY_SIZE; i++) { @@ -4253,8 +4235,7 @@ bool8 ChangeTypeBasedOnTerrain(u8 battlerId) static u16 GetSupremeOverlordModifier(u8 battlerId) { u32 i; - u8 side = GetBattlerSide(battlerId); - struct Pokemon *party = (side == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetBattlerParty(battlerId); u16 modifier = UQ_4_12(1.0); bool8 appliedFirstBoost = FALSE; @@ -7253,11 +7234,11 @@ u8 ItemBattleEffects(u8 caseID, u8 battlerId, bool8 moveTurn) case HOLD_EFFECT_RESTORE_PP: if (!moveTurn) { - struct Pokemon *mon; + struct Pokemon *party = GetBattlerParty(battlerId); + struct Pokemon *mon = &party[gBattlerPartyIndexes[battlerId]]; u8 ppBonuses; u16 move; - mon = GetBattlerPartyData(battlerId); for (i = 0; i < MAX_MON_MOVES; i++) { move = GetMonData(mon, MON_DATA_MOVE1 + i); @@ -10112,7 +10093,7 @@ void UndoMegaEvolution(u32 monId) void UndoFormChange(u32 monId, u32 side, bool32 isSwitchingOut) { u32 i, currSpecies, targetSpecies; - struct Pokemon *party = (side == B_SIDE_PLAYER) ? gPlayerParty : gEnemyParty; + struct Pokemon *party = GetSideParty(side); static const u16 species[][3] = { // Changed Form ID Default Form ID Should change on switch @@ -10247,10 +10228,7 @@ bool32 SetIllusionMon(struct Pokemon *mon, u32 battlerId) if (GetMonAbility(mon) != ABILITY_ILLUSION) return FALSE; - if (GetBattlerSide(battlerId) == B_SIDE_PLAYER) - party = gPlayerParty; - else - party = gEnemyParty; + party = GetBattlerParty(battlerId); if (IsBattlerAlive(BATTLE_PARTNER(battlerId))) partnerMon = &party[gBattlerPartyIndexes[BATTLE_PARTNER(battlerId)]]; @@ -10375,17 +10353,6 @@ bool32 TestMoveFlags(u16 move, u32 flag) return FALSE; } -struct Pokemon *GetBattlerPartyData(u8 battlerId) -{ - struct Pokemon *mon; - if (GetBattlerSide(battlerId) == B_SIDE_PLAYER) - mon = &gPlayerParty[gBattlerPartyIndexes[battlerId]]; - else - mon = &gEnemyParty[gBattlerPartyIndexes[battlerId]]; - - return mon; -} - static u8 GetFlingPowerFromItemId(u16 itemId) { if (itemId >= ITEM_TM01 && itemId <= ITEM_HM08)