From feeccb9198c8396c64c4b75f55c136169824df83 Mon Sep 17 00:00:00 2001 From: ghoulslash <41651341+ghoulslash@users.noreply.github.com> Date: Wed, 9 Oct 2024 03:38:21 -0400 Subject: [PATCH] Simplify BS_FAINTED_MULTIPLE_1 double battle logic in openpartyscreen (#5435) * simplify BS_FAINTED_MULTIPLE_1 doubles logic * simplify openpartyscreen BS_FAINTED_MULTIPLE_2 checks, too * Update src/battle_script_commands.c Co-authored-by: Alex <93446519+AlexOn1ine@users.noreply.github.com> * Update src/battle_script_commands.c Co-authored-by: Alex <93446519+AlexOn1ine@users.noreply.github.com> * Update src/battle_script_commands.c Co-authored-by: Alex <93446519+AlexOn1ine@users.noreply.github.com> * more readable OpponentHandleChoosePokemon * Update src/battle_controller_opponent.c * Update src/battle_controller_opponent.c --------- Co-authored-by: ghoulslash Co-authored-by: Alex <93446519+AlexOn1ine@users.noreply.github.com> --- src/battle_controller_opponent.c | 30 ++--- src/battle_script_commands.c | 194 +++++++++---------------------- 2 files changed, 69 insertions(+), 155 deletions(-) diff --git a/src/battle_controller_opponent.c b/src/battle_controller_opponent.c index 57ebccf7fa..01ec74f9c2 100644 --- a/src/battle_controller_opponent.c +++ b/src/battle_controller_opponent.c @@ -661,7 +661,7 @@ static void OpponentHandleChoosePokemon(u32 battler) chosenMonId = gSelectedMonPartyId = GetFirstFaintedPartyIndex(battler); } // Switching out - else if (*(gBattleStruct->AI_monToSwitchIntoId + battler) == PARTY_SIZE) + else if (gBattleStruct->AI_monToSwitchIntoId[battler] == PARTY_SIZE) { chosenMonId = GetMostSuitableMonToSwitchInto(battler, TRUE); if (chosenMonId == PARTY_SIZE) @@ -680,27 +680,27 @@ static void OpponentHandleChoosePokemon(u32 battler) } GetAIPartyIndexes(battler, &firstId, &lastId); - for (chosenMonId = (lastId-1); chosenMonId >= firstId; chosenMonId--) { - if (IsValidForBattle(&gEnemyParty[chosenMonId]) - && chosenMonId != gBattlerPartyIndexes[battler1] - && chosenMonId != gBattlerPartyIndexes[battler2] - && (!(AI_THINKING_STRUCT->aiFlags[battler] & AI_FLAG_ACE_POKEMON) - || chosenMonId != CalculateEnemyPartyCount() - 1 - || CountAIAliveNonEggMonsExcept(PARTY_SIZE) == pokemonInBattle)) - { - break; - } + if (!IsValidForBattle(&gEnemyParty[chosenMonId])) + continue; + if (chosenMonId == gBattlerPartyIndexes[battler1] + || chosenMonId == gBattlerPartyIndexes[battler2]) + continue; + if ((AI_THINKING_STRUCT->aiFlags[battler] & AI_FLAG_ACE_POKEMON) + && ((chosenMonId != CalculateEnemyPartyCount() - 1) || CountAIAliveNonEggMonsExcept(PARTY_SIZE) == pokemonInBattle)) + continue; + // mon is valid + break; } } - *(gBattleStruct->monToSwitchIntoId + battler) = chosenMonId; + gBattleStruct->monToSwitchIntoId[battler] = chosenMonId; } else { - chosenMonId = *(gBattleStruct->AI_monToSwitchIntoId + battler); - *(gBattleStruct->AI_monToSwitchIntoId + battler) = PARTY_SIZE; - *(gBattleStruct->monToSwitchIntoId + battler) = chosenMonId; + chosenMonId = gBattleStruct->AI_monToSwitchIntoId[battler]; + gBattleStruct->AI_monToSwitchIntoId[battler] = PARTY_SIZE; + gBattleStruct->monToSwitchIntoId[battler] = chosenMonId; } #if TESTING TestRunner_Battle_CheckSwitch(battler, chosenMonId); diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 5c7be23e7d..da1a8754e1 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -6997,126 +6997,53 @@ static void Cmd_openpartyscreen(void) } else if (IsDoubleBattle()) { - bool8 hasReplacement_0, hasReplacement_1, hasReplacement_2, hasReplacement_3; - + bool32 hasReplacement; + hitmarkerFaintBits = gHitMarker >> 28; - - if (1u & hitmarkerFaintBits) + for (i = 0; i < gBattlersCount; i++) { - battler = 0; - if (HasNoMonsToSwitch(battler, PARTY_SIZE, PARTY_SIZE)) + if (((1u << i) & hitmarkerFaintBits)) { - gAbsentBattlerFlags |= 1u << battler; - gHitMarker &= ~HITMARKER_FAINTED(battler); - BtlController_EmitCantSwitch(battler, BUFFER_A); - MarkBattlerForControllerExec(battler); - } - else if (!gSpecialStatuses[battler].faintedHasReplacement) - { - ChooseMonToSendOut(battler, gBattleStruct->monToSwitchIntoId[2]); - gSpecialStatuses[battler].faintedHasReplacement = TRUE; - } - else - { - BtlController_EmitLinkStandbyMsg(battler, BUFFER_A, LINK_STANDBY_MSG_ONLY, FALSE); - MarkBattlerForControllerExec(battler); - flags |= 1; + if (i > 1 && ((1u << BATTLE_PARTNER(i)) & hitmarkerFaintBits)) + continue; + + battler = i; + if (HasNoMonsToSwitch(battler, PARTY_SIZE, PARTY_SIZE)) + { + gAbsentBattlerFlags |= 1u << battler; + gHitMarker &= ~HITMARKER_FAINTED(battler); + BtlController_EmitCantSwitch(battler, BUFFER_A); + MarkBattlerForControllerExec(battler); + } + else if (!gSpecialStatuses[battler].faintedHasReplacement) + { + ChooseMonToSendOut(battler, gBattleStruct->monToSwitchIntoId[BATTLE_PARTNER(battler)]); + gSpecialStatuses[battler].faintedHasReplacement = TRUE; + } + else if (battler < 2 || (battler > 1 && !(flags & BATTLE_PARTNER(battler)))) + { + BtlController_EmitLinkStandbyMsg(battler, BUFFER_A, LINK_STANDBY_MSG_ONLY, FALSE); + MarkBattlerForControllerExec(battler); + flags |= battler; + } } } - if (4u & hitmarkerFaintBits && !(1u & hitmarkerFaintBits)) + + for (i = 0; i < NUM_BATTLE_SIDES; i++) { - battler = 2; - if (HasNoMonsToSwitch(battler, PARTY_SIZE, PARTY_SIZE)) + if (!(gSpecialStatuses[i].faintedHasReplacement)) { - gAbsentBattlerFlags |= 1u << battler; - gHitMarker &= ~HITMARKER_FAINTED(battler); - BtlController_EmitCantSwitch(battler, BUFFER_A); - MarkBattlerForControllerExec(battler); - } - else if (!gSpecialStatuses[battler].faintedHasReplacement) - { - ChooseMonToSendOut(battler, gBattleStruct->monToSwitchIntoId[0]); - gSpecialStatuses[battler].faintedHasReplacement = TRUE; - } - else if (!(flags & 1)) - { - BtlController_EmitLinkStandbyMsg(battler, BUFFER_A, LINK_STANDBY_MSG_ONLY, FALSE); - MarkBattlerForControllerExec(battler); - } - } - if (2 & hitmarkerFaintBits) - { - battler = 1; - if (HasNoMonsToSwitch(battler, PARTY_SIZE, PARTY_SIZE)) - { - gAbsentBattlerFlags |= 1u << battler; - gHitMarker &= ~HITMARKER_FAINTED(battler); - BtlController_EmitCantSwitch(battler, BUFFER_A); - MarkBattlerForControllerExec(battler); - } - else if (!gSpecialStatuses[battler].faintedHasReplacement) - { - ChooseMonToSendOut(battler, gBattleStruct->monToSwitchIntoId[3]); - gSpecialStatuses[battler].faintedHasReplacement = TRUE; - } - else - { - BtlController_EmitLinkStandbyMsg(battler, BUFFER_A, LINK_STANDBY_MSG_ONLY, FALSE); - MarkBattlerForControllerExec(battler); - flags |= 2; - } - } - if (8 & hitmarkerFaintBits && !(2 & hitmarkerFaintBits)) - { - battler = 3; - if (HasNoMonsToSwitch(battler, PARTY_SIZE, PARTY_SIZE)) - { - gAbsentBattlerFlags |= 1u << battler; - gHitMarker &= ~HITMARKER_FAINTED(battler); - BtlController_EmitCantSwitch(battler, BUFFER_A); - MarkBattlerForControllerExec(battler); - } - else if (!gSpecialStatuses[battler].faintedHasReplacement) - { - ChooseMonToSendOut(battler, gBattleStruct->monToSwitchIntoId[1]); - gSpecialStatuses[battler].faintedHasReplacement = TRUE; - } - else if (!(flags & 2)) - { - BtlController_EmitLinkStandbyMsg(battler, BUFFER_A, LINK_STANDBY_MSG_ONLY, FALSE); - MarkBattlerForControllerExec(battler); - } - } + hasReplacement = gSpecialStatuses[BATTLE_PARTNER(i)].faintedHasReplacement; + if (!hasReplacement && hitmarkerFaintBits != 0) + { + if (gAbsentBattlerFlags & (1 << i)) + battler = BATTLE_PARTNER(i); + else + battler = i; - hasReplacement_0 = gSpecialStatuses[0].faintedHasReplacement; - if (!hasReplacement_0) - { - hasReplacement_2 = gSpecialStatuses[2].faintedHasReplacement; - if (!hasReplacement_2 && hitmarkerFaintBits != 0) - { - if (gAbsentBattlerFlags & 1) - battler = 2; - else - battler = 0; - - BtlController_EmitLinkStandbyMsg(battler, BUFFER_A, LINK_STANDBY_MSG_ONLY, FALSE); - MarkBattlerForControllerExec(battler); - } - - } - hasReplacement_1 = gSpecialStatuses[1].faintedHasReplacement; - if (!hasReplacement_1) - { - hasReplacement_3 = gSpecialStatuses[3].faintedHasReplacement; - if (!hasReplacement_3 && hitmarkerFaintBits != 0) - { - if (gAbsentBattlerFlags & 2) - battler = 3; - else - battler = 1; - - BtlController_EmitLinkStandbyMsg(battler, BUFFER_A, LINK_STANDBY_MSG_ONLY, FALSE); - MarkBattlerForControllerExec(battler); + BtlController_EmitLinkStandbyMsg(battler, BUFFER_A, LINK_STANDBY_MSG_ONLY, FALSE); + MarkBattlerForControllerExec(battler); + } } } } @@ -7129,36 +7056,23 @@ static void Cmd_openpartyscreen(void) if (IsDoubleBattle()) { hitmarkerFaintBits = gHitMarker >> 28; - if (4 & hitmarkerFaintBits && 1 & hitmarkerFaintBits) + for (i = 0; i < NUM_BATTLE_SIDES; i++) { - battler = 2; - if (HasNoMonsToSwitch(battler, gBattleResources->bufferB[0][1], PARTY_SIZE)) + if ((1 << BATTLE_PARTNER(i)) & hitmarkerFaintBits && (1 << i) & hitmarkerFaintBits) { - gAbsentBattlerFlags |= 1u << battler; - gHitMarker &= ~HITMARKER_FAINTED(battler); - BtlController_EmitCantSwitch(battler, BUFFER_A); - MarkBattlerForControllerExec(battler); - } - else if (!gSpecialStatuses[battler].faintedHasReplacement) - { - ChooseMonToSendOut(battler, gBattleStruct->monToSwitchIntoId[0]); - gSpecialStatuses[battler].faintedHasReplacement = TRUE; - } - } - if (8u & hitmarkerFaintBits && hitmarkerFaintBits & 2u) - { - battler = 3; - if (HasNoMonsToSwitch(battler, gBattleResources->bufferB[1][1], PARTY_SIZE)) - { - gAbsentBattlerFlags |= 1u << battler; - gHitMarker &= ~HITMARKER_FAINTED(battler); - BtlController_EmitCantSwitch(battler, BUFFER_A); - MarkBattlerForControllerExec(battler); - } - else if (!gSpecialStatuses[battler].faintedHasReplacement) - { - ChooseMonToSendOut(battler, gBattleStruct->monToSwitchIntoId[1]); - gSpecialStatuses[battler].faintedHasReplacement = TRUE; + battler = BATTLE_PARTNER(i); + if (HasNoMonsToSwitch(battler, PARTY_SIZE, PARTY_SIZE)) + { + gAbsentBattlerFlags |= (1u << battler); + gHitMarker &= ~(HITMARKER_FAINTED(battler)); + BtlController_EmitCantSwitch(battler, BUFFER_A); + MarkBattlerForControllerExec(battler); + } + else if (!gSpecialStatuses[battler].faintedHasReplacement) + { + ChooseMonToSendOut(battler, gBattleStruct->monToSwitchIntoId[i]); + gSpecialStatuses[battler].faintedHasReplacement = TRUE; + } } } gBattlescriptCurrInstr = cmd->nextInstr;