From b0598b1aef16815272187adf84a999b6f190c8ba Mon Sep 17 00:00:00 2001 From: GriffinR Date: Mon, 11 Oct 2021 20:49:56 -0400 Subject: [PATCH] Clean up recorded_battle, add MOVE_IS_PERMANENT --- include/battle.h | 5 +++ include/recorded_battle.h | 6 ++-- src/battle_main.c | 4 +-- src/battle_script_commands.c | 11 +++--- src/battle_util.c | 13 +++---- src/pokemon.c | 8 ++--- src/recorded_battle.c | 69 +++++++++++++++++------------------- 7 files changed, 53 insertions(+), 63 deletions(-) diff --git a/include/battle.h b/include/battle.h index 5ccff8ce3..f793c779f 100644 --- a/include/battle.h +++ b/include/battle.h @@ -17,6 +17,11 @@ #define GET_BATTLER_SIDE(battler) (GetBattlerPosition(battler) & BIT_SIDE) #define GET_BATTLER_SIDE2(battler) (GET_BATTLER_POSITION(battler) & BIT_SIDE) +// Used to exclude moves learned temporarily by Transform or Mimic +#define MOVE_IS_PERMANENT(battler, moveSlot) \ + (!(gBattleMons[battler].status2 & STATUS2_TRANSFORMED) \ + && !(gDisableStructs[battler].mimickedMoves & gBitTable[moveSlot])) + // Battle Actions // These determine what each battler will do in a turn #define B_ACTION_USE_MOVE 0 diff --git a/include/recorded_battle.h b/include/recorded_battle.h index 8ea392774..9b8939403 100644 --- a/include/recorded_battle.h +++ b/include/recorded_battle.h @@ -8,7 +8,7 @@ extern u8 gRecordedBattleMultiplayerId; #define B_RECORD_MODE_RECORDING 1 #define B_RECORD_MODE_PLAYBACK 2 -void RecordedBattle_Init(u8 arg0); +void RecordedBattle_Init(u8 mode); void RecordedBattle_SetTrainerInfo(void); void RecordedBattle_SetBattlerAction(u8 battlerId, u8 action); void RecordedBattle_ClearBattlerAction(u8 battlerId, u8 bytesToClear); @@ -23,12 +23,12 @@ u8 GetRecordedBattleFronterBrainSymbol(void); void RecordedBattle_SaveParties(void); u8 GetActiveBattlerLinkPlayerGender(void); void RecordedBattle_ClearFrontierPassFlag(void); -void RecordedBattle_SetFrontierPassFlagFromHword(u16 arg0); +void RecordedBattle_SetFrontierPassFlagFromHword(u16 flags); u8 RecordedBattle_GetFrontierPassFlag(void); u8 GetBattleSceneInRecordedBattle(void); u8 GetTextSpeedInRecordedBattle(void); void RecordedBattle_CopyBattlerMoves(void); -void sub_818603C(u8 arg0); +void RecordedBattle_CheckMovesetChanges(u8 mode); u32 GetAiScriptsInRecordedBattle(void); void RecordedBattle_SetPlaybackFinished(void); bool8 RecordedBattle_CanStopPlayback(void); diff --git a/src/battle_main.c b/src/battle_main.c index 9b3afb7f3..1d0f3454b 100644 --- a/src/battle_main.c +++ b/src/battle_main.c @@ -4340,7 +4340,7 @@ static void HandleTurnActionSelectionState(void) sub_803CDF8(); return; default: - sub_818603C(2); + RecordedBattle_CheckMovesetChanges(B_RECORD_MODE_PLAYBACK); if ((gBattleBufferB[gActiveBattler][2] | (gBattleBufferB[gActiveBattler][3] << 8)) == 0xFFFF) { gBattleCommunication[gActiveBattler] = STATE_BEFORE_ACTION_CHOSEN; @@ -4508,7 +4508,7 @@ static void HandleTurnActionSelectionState(void) // Check if everyone chose actions. if (gBattleCommunication[ACTIONS_CONFIRMED_COUNT] == gBattlersCount) { - sub_818603C(1); + RecordedBattle_CheckMovesetChanges(B_RECORD_MODE_RECORDING); gBattleMainFunc = SetActionsAndBattlersTurnOrder; if (gBattleTypeFlags & BATTLE_TYPE_INGAME_PARTNER) diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index b564fe680..3e91b64b1 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -1235,8 +1235,7 @@ static void Cmd_ppreduce(void) else gBattleMons[gBattlerAttacker].pp[gCurrMovePos] = 0; - if (!(gBattleMons[gBattlerAttacker].status2 & STATUS2_TRANSFORMED) - && !((gDisableStructs[gBattlerAttacker].mimickedMoves) & gBitTable[gCurrMovePos])) + if (MOVE_IS_PERMANENT(gBattlerAttacker, gCurrMovePos)) { gActiveBattler = gBattlerAttacker; BtlController_EmitSetMonData(0, REQUEST_PPMOVE1_BATTLE + gCurrMovePos, 0, 1, &gBattleMons[gBattlerAttacker].pp[gCurrMovePos]); @@ -5436,17 +5435,14 @@ static void Cmd_yesnoboxlearnmove(void) RemoveMonPPBonus(&gPlayerParty[gBattleStruct->expGetterMonId], movePosition); SetMonMoveSlot(&gPlayerParty[gBattleStruct->expGetterMonId], gMoveToLearn, movePosition); - if (gBattlerPartyIndexes[0] == gBattleStruct->expGetterMonId - && !(gBattleMons[0].status2 & STATUS2_TRANSFORMED) - && !(gDisableStructs[0].mimickedMoves & gBitTable[movePosition])) + if (gBattlerPartyIndexes[0] == gBattleStruct->expGetterMonId && MOVE_IS_PERMANENT(0, movePosition)) { RemoveBattleMonPPBonus(&gBattleMons[0], movePosition); SetBattleMonMoveSlot(&gBattleMons[0], gMoveToLearn, movePosition); } if (gBattleTypeFlags & BATTLE_TYPE_DOUBLE && gBattlerPartyIndexes[2] == gBattleStruct->expGetterMonId - && !(gBattleMons[2].status2 & STATUS2_TRANSFORMED) - && !(gDisableStructs[2].mimickedMoves & gBitTable[movePosition])) + && MOVE_IS_PERMANENT(2, movePosition)) { RemoveBattleMonPPBonus(&gBattleMons[2], movePosition); SetBattleMonMoveSlot(&gBattleMons[2], gMoveToLearn, movePosition); @@ -8241,6 +8237,7 @@ static void Cmd_tryspiteppreduce(void) gBattleMons[gBattlerTarget].pp[i] -= ppToDeduct; gActiveBattler = gBattlerTarget; + // if (MOVE_IS_PERMANENT(gActiveBattler, i)), but backwards if (!(gDisableStructs[gActiveBattler].mimickedMoves & gBitTable[i]) && !(gBattleMons[gActiveBattler].status2 & STATUS2_TRANSFORMED)) { diff --git a/src/battle_util.c b/src/battle_util.c index 11fd31524..ac2fbd34f 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -750,8 +750,7 @@ void PressurePPLose(u8 target, u8 attacker, u16 move) if (gBattleMons[attacker].pp[moveIndex] != 0) gBattleMons[attacker].pp[moveIndex]--; - if (!(gBattleMons[attacker].status2 & STATUS2_TRANSFORMED) - && !(gDisableStructs[attacker].mimickedMoves & gBitTable[moveIndex])) + if (MOVE_IS_PERMANENT(attacker, moveIndex)) { gActiveBattler = attacker; BtlController_EmitSetMonData(0, REQUEST_PPMOVE1_BATTLE + moveIndex, 0, 1, &gBattleMons[gActiveBattler].pp[moveIndex]); @@ -783,9 +782,7 @@ void PressurePPLoseOnUsingImprison(u8 attacker) } } - if (imprisonPos != 4 - && !(gBattleMons[attacker].status2 & STATUS2_TRANSFORMED) - && !(gDisableStructs[attacker].mimickedMoves & gBitTable[imprisonPos])) + if (imprisonPos != MAX_MON_MOVES && MOVE_IS_PERMANENT(attacker, imprisonPos)) { gActiveBattler = attacker; BtlController_EmitSetMonData(0, REQUEST_PPMOVE1_BATTLE + imprisonPos, 0, 1, &gBattleMons[gActiveBattler].pp[imprisonPos]); @@ -816,9 +813,7 @@ void PressurePPLoseOnUsingPerishSong(u8 attacker) } } - if (perishSongPos != MAX_MON_MOVES - && !(gBattleMons[attacker].status2 & STATUS2_TRANSFORMED) - && !(gDisableStructs[attacker].mimickedMoves & gBitTable[perishSongPos])) + if (perishSongPos != MAX_MON_MOVES && MOVE_IS_PERMANENT(attacker, perishSongPos)) { gActiveBattler = attacker; BtlController_EmitSetMonData(0, REQUEST_PPMOVE1_BATTLE + perishSongPos, 0, 1, &gBattleMons[gActiveBattler].pp[perishSongPos]); @@ -3598,7 +3593,7 @@ u8 ItemBattleEffects(u8 caseID, u8 battlerId, bool8 moveTurn) MarkBattlerForControllerExec(gActiveBattler); break; case ITEM_PP_CHANGE: - if (!(gBattleMons[battlerId].status2 & STATUS2_TRANSFORMED) && !(gDisableStructs[battlerId].mimickedMoves & gBitTable[i])) + if (MOVE_IS_PERMANENT(battlerId, i)) gBattleMons[battlerId].pp[i] = changedPP; break; } diff --git a/src/pokemon.c b/src/pokemon.c index 011e288db..f4ca3f4c1 100644 --- a/src/pokemon.c +++ b/src/pokemon.c @@ -5078,9 +5078,7 @@ bool8 PokemonUseItemEffects(struct Pokemon *mon, u16 item, u8 partyIndex, u8 mov SetMonData(mon, MON_DATA_PP1 + temp2, &dataUnsigned); // Heal battler PP too (if applicable) - if (gMain.inBattle - && battlerId != MAX_BATTLERS_COUNT && !(gBattleMons[battlerId].status2 & STATUS2_TRANSFORMED) - && !(gDisableStructs[battlerId].mimickedMoves & gBitTable[temp2])) + if (gMain.inBattle && battlerId != MAX_BATTLERS_COUNT && MOVE_IS_PERMANENT(battlerId, temp2)) gBattleMons[battlerId].pp[temp2] = dataUnsigned; retVal = FALSE; @@ -5106,9 +5104,7 @@ bool8 PokemonUseItemEffects(struct Pokemon *mon, u16 item, u8 partyIndex, u8 mov SetMonData(mon, MON_DATA_PP1 + moveIndex, &dataUnsigned); // Heal battler PP too (if applicable) - if (gMain.inBattle - && battlerId != MAX_BATTLERS_COUNT && !(gBattleMons[battlerId].status2 & STATUS2_TRANSFORMED) - && !(gDisableStructs[battlerId].mimickedMoves & gBitTable[moveIndex])) + if (gMain.inBattle && battlerId != MAX_BATTLERS_COUNT && MOVE_IS_PERMANENT(battlerId, moveIndex)) gBattleMons[battlerId].pp[moveIndex] = dataUnsigned; retVal = FALSE; diff --git a/src/recorded_battle.c b/src/recorded_battle.c index 69ee90210..e6d5b165b 100644 --- a/src/recorded_battle.c +++ b/src/recorded_battle.c @@ -1,6 +1,7 @@ #include "global.h" #include "battle.h" #include "battle_anim.h" +#include "battle_controllers.h" #include "recorded_battle.h" #include "main.h" #include "pokemon.h" @@ -35,12 +36,6 @@ struct PlayerInfo u16 language; }; -struct MovePp -{ - u16 moves[MAX_MON_MOVES]; - u8 pp[MAX_MON_MOVES]; -}; - struct RecordedBattleSave { struct Pokemon playerParty[PARTY_SIZE]; @@ -91,7 +86,7 @@ EWRAM_DATA static u32 sBattleFlags = 0; EWRAM_DATA static u32 sAI_Scripts = 0; EWRAM_DATA static struct Pokemon sSavedPlayerParty[PARTY_SIZE] = {0}; EWRAM_DATA static struct Pokemon sSavedOpponentParty[PARTY_SIZE] = {0}; -EWRAM_DATA static u16 sPlayerMonMoves[2][MAX_MON_MOVES] = {0}; +EWRAM_DATA static u16 sPlayerMonMoves[MAX_BATTLERS_COUNT / 2][MAX_MON_MOVES] = {0}; EWRAM_DATA static struct PlayerInfo sPlayers[MAX_BATTLERS_COUNT] = {0}; EWRAM_DATA static bool8 sIsPlaybackFinished = 0; EWRAM_DATA static u8 sRecordMixFriendName[PLAYER_NAME_LENGTH + 1] = {0}; @@ -674,9 +669,9 @@ void RecordedBattle_ClearFrontierPassFlag(void) } // Set sFrontierPassFlag to received state of FLAG_SYS_FRONTIER_PASS -void RecordedBattle_SetFrontierPassFlagFromHword(u16 arg0) +void RecordedBattle_SetFrontierPassFlagFromHword(u16 flags) { - sFrontierPassFlag |= (arg0 & 0x8000) >> 15; + sFrontierPassFlag |= (flags & (1 << 15)) >> 15; } u8 RecordedBattle_GetFrontierPassFlag(void) @@ -706,14 +701,14 @@ void RecordedBattle_CopyBattlerMoves(void) return; for (i = 0; i < MAX_MON_MOVES; i++) - { sPlayerMonMoves[gActiveBattler / 2][i] = gBattleMons[gActiveBattler].moves[i]; - } } +// This is a special battle action only used by this function +// It shares a value with B_ACTION_SAFARI_POKEBLOCK, which can never occur in a recorded battle. #define ACTION_MOVE_CHANGE 6 -void sub_818603C(u8 arg0) +void RecordedBattle_CheckMovesetChanges(u8 mode) { s32 battlerId, j, k; @@ -725,9 +720,9 @@ void sub_818603C(u8 arg0) // Player's side only if (GetBattlerSide(battlerId) != B_SIDE_OPPONENT) { - if (arg0 == 1) + if (mode == B_RECORD_MODE_RECORDING) { - // Check if any of the battler's moves have changed + // Check if any of the battler's moves have changed. for (j = 0; j < MAX_MON_MOVES; j++) { if (gBattleMons[battlerId].moves[j] != sPlayerMonMoves[battlerId / 2][j]) @@ -750,63 +745,65 @@ void sub_818603C(u8 arg0) } } } - else + else // B_RECORD_MODE_PLAYBACK { if (sBattleRecords[battlerId][sBattlerRecordSizes[battlerId]] == ACTION_MOVE_CHANGE) { u8 ppBonuses[MAX_MON_MOVES]; - u8 array1[MAX_MON_MOVES]; - u8 array2[MAX_MON_MOVES]; - struct MovePp movePp; - u8 array3[(MAX_MON_MOVES * 2)]; - u8 var; + u8 moveSlots[MAX_MON_MOVES]; + u8 mimickedMoveSlots[MAX_MON_MOVES]; + struct ChooseMoveStruct movePp; + u8 ppBonusSet; + // We know the current action is ACTION_MOVE_CHANGE, retrieve + // it without saving it to move on to the next action. RecordedBattle_GetBattlerAction(battlerId); + for (j = 0; j < MAX_MON_MOVES; j++) ppBonuses[j] = ((gBattleMons[battlerId].ppBonuses & (3 << (j << 1))) >> (j << 1)); for (j = 0; j < MAX_MON_MOVES; j++) { - array1[j] = RecordedBattle_GetBattlerAction(battlerId); - movePp.moves[j] = gBattleMons[battlerId].moves[array1[j]]; - movePp.pp[j] = gBattleMons[battlerId].pp[array1[j]]; - array3[j] = ppBonuses[array1[j]]; - array2[j] = (gDisableStructs[battlerId].mimickedMoves & gBitTable[j]) >> j; + moveSlots[j] = RecordedBattle_GetBattlerAction(battlerId); + movePp.moves[j] = gBattleMons[battlerId].moves[moveSlots[j]]; + movePp.currentPp[j] = gBattleMons[battlerId].pp[moveSlots[j]]; + movePp.maxPp[j] = ppBonuses[moveSlots[j]]; + mimickedMoveSlots[j] = (gDisableStructs[battlerId].mimickedMoves & gBitTable[j]) >> j; } for (j = 0; j < MAX_MON_MOVES; j++) { gBattleMons[battlerId].moves[j] = movePp.moves[j]; - gBattleMons[battlerId].pp[j] = movePp.pp[j]; + gBattleMons[battlerId].pp[j] = movePp.currentPp[j]; } gBattleMons[battlerId].ppBonuses = 0; gDisableStructs[battlerId].mimickedMoves = 0; for (j = 0; j < MAX_MON_MOVES; j++) { - gBattleMons[battlerId].ppBonuses |= (array3[j]) << (j << 1); - gDisableStructs[battlerId].mimickedMoves |= (array2[j]) << (j); + gBattleMons[battlerId].ppBonuses |= movePp.maxPp[j] << (j << 1); + gDisableStructs[battlerId].mimickedMoves |= mimickedMoveSlots[j] << j; } if (!(gBattleMons[battlerId].status2 & STATUS2_TRANSFORMED)) { for (j = 0; j < MAX_MON_MOVES; j++) - ppBonuses[j] = ((GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP_BONUSES, NULL) & ((3 << (j << 1)))) >> (j << 1)); + ppBonuses[j] = (GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP_BONUSES, NULL) & ((3 << (j << 1)))) >> (j << 1); for (j = 0; j < MAX_MON_MOVES; j++) { - movePp.moves[j] = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_MOVE1 + array1[j], NULL); - movePp.pp[j] = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP1 + array1[j], NULL); - array3[j] = ppBonuses[array1[j]]; + movePp.moves[j] = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_MOVE1 + moveSlots[j], NULL); + movePp.currentPp[j] = GetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP1 + moveSlots[j], NULL); + movePp.maxPp[j] = ppBonuses[moveSlots[j]]; } for (j = 0; j < MAX_MON_MOVES; j++) { SetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_MOVE1 + j, &movePp.moves[j]); - SetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP1 + j, &movePp.pp[j]); + SetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP1 + j, &movePp.currentPp[j]); } - var = 0; + ppBonusSet = 0; for (j = 0; j < MAX_MON_MOVES; j++) - var |= (array3[j]) << (j << 1); + ppBonusSet |= movePp.maxPp[j] << (j << 1); - SetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP_BONUSES, &var); + SetMonData(&gPlayerParty[gBattlerPartyIndexes[battlerId]], MON_DATA_PP_BONUSES, &ppBonusSet); } gChosenMoveByBattler[battlerId] = gBattleMons[battlerId].moves[*(gBattleStruct->chosenMovePositions + battlerId)]; }