From 6ceffaaf3e5c0e981c7e81b03de0bdc7c91cf0da Mon Sep 17 00:00:00 2001 From: kleeenexfeu Date: Tue, 23 Nov 2021 13:46:36 +0100 Subject: [PATCH] recalculate turn order after relevant actions --- include/constants/battle_config.h | 1 - src/battle_util.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/constants/battle_config.h b/include/constants/battle_config.h index 3180212f8..ac3da4dd4 100644 --- a/include/constants/battle_config.h +++ b/include/constants/battle_config.h @@ -134,7 +134,6 @@ #define B_MEGA_EVO_TURN_ORDER GEN_7 // In Gen7, a Pokémon's Speed after Mega Evolution is used to determine turn order, not its Speed before. #define B_RECALC_TURN_AFTER_ACTIONS GEN_8 // In gen 8, switching/using a move have consequences for the turn order of the current turn. - // Move data settings #define B_UPDATED_MOVE_DATA GEN_8 // Updates move data in gBattleMoves, including Power, Accuracy, PP, stat changes, targets, chances of secondary effects, etc. #define B_PHYSICAL_SPECIAL_SPLIT GEN_7 // In Gen3, the move's type determines if it will do physical or special damage. The split icon in the summary will reflect this. diff --git a/src/battle_util.c b/src/battle_util.c index 7e2320500..7953565a0 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -871,6 +871,11 @@ void HandleAction_NothingIsFainted(void) void HandleAction_ActionFinished(void) { + #if B_RECALC_TURN_AFTER_ACTIONS >= GEN_8 + u8 i, j; + u8 battler1 = 0; + u8 battler2 = 0; + #endif *(gBattleStruct->monToSwitchIntoId + gBattlerByTurnOrder[gCurrentTurnActionNumber]) = 6; gCurrentTurnActionNumber++; gCurrentActionFuncId = gActionsByTurnOrder[gCurrentTurnActionNumber]; @@ -894,6 +899,31 @@ void HandleAction_ActionFinished(void) gBattleCommunication[4] = 0; gBattleScripting.multihitMoveEffect = 0; gBattleResources->battleScriptsStack->size = 0; + + #if B_RECALC_TURN_AFTER_ACTIONS >= GEN_8 + // i starts at `gCurrentTurnActionNumber` because we don't want to recalculate turn order for mon that have already + // taken action. It's been previously increased, which we want in order to not recalculate the turn of the mon that just finished its action + for (i = gCurrentTurnActionNumber; i < gBattlersCount - 1; i++) + { + for (j = i + 1; j < gBattlersCount; j++) + { + u8 battler1 = gBattlerByTurnOrder[i]; + u8 battler2 = gBattlerByTurnOrder[j]; + // We recalculate order only for action of the same priority. If any action other than switch/move has been taken, they should + // have been executed before. The only recalculation needed is for moves/switch. Mega evolution is handled in src/battle_main.c/TryChangeOrder + if((gActionsByTurnOrder[i] == B_ACTION_USE_MOVE && gActionsByTurnOrder[j] == B_ACTION_USE_MOVE)) + { + if (GetWhoStrikesFirst(battler1, battler2, FALSE)) + SwapTurnOrder(i, j); + } + else if ((gActionsByTurnOrder[i] == B_ACTION_SWITCH && gActionsByTurnOrder[j] == B_ACTION_SWITCH)) + { + if (GetWhoStrikesFirst(battler1, battler2, TRUE)) // If the actions chosen are switching, we recalc order but ignoring the moves + SwapTurnOrder(i, j); + } + } + } + #endif } // rom const data