From 03915524c5912c1c1d4e68406dcac01252ab5291 Mon Sep 17 00:00:00 2001 From: DizzyEggg Date: Mon, 20 Feb 2023 12:42:06 +0100 Subject: [PATCH] Fix switch-in abilities activating on empty field --- include/battle.h | 1 + include/battle_script_commands.h | 1 + src/battle_script_commands.c | 72 +++++++++++++++++++++++--------- src/battle_util.c | 17 +++++++- src/data/trainer_parties.h | 12 +++--- test/ability_intimidate.c | 50 ++++++++++++++++++++-- test/test_runner.c | 2 +- 7 files changed, 124 insertions(+), 31 deletions(-) diff --git a/include/battle.h b/include/battle.h index 8be922384..46c934082 100644 --- a/include/battle.h +++ b/include/battle.h @@ -644,6 +644,7 @@ struct BattleStruct struct StolenItem itemStolen[PARTY_SIZE]; // Player's team that had items stolen (two bytes per party member) u8 blunderPolicy:1; // should blunder policy activate u8 swapDamageCategory:1; // Photon Geyser, Shell Side Arm, Light That Burns the Sky + u8 switchInAbilityPostponed:4; // To not activate against an empty field, each bit for battler u8 ballSpriteIds[2]; // item gfx, window gfx u8 stickyWebUser; u8 appearedInBattle; // Bitfield to track which Pokemon appeared in battle. Used for Burmy's form change diff --git a/include/battle_script_commands.h b/include/battle_script_commands.h index 60fd9b156..c3d6831e9 100644 --- a/include/battle_script_commands.h +++ b/include/battle_script_commands.h @@ -44,6 +44,7 @@ u16 GetSecretPowerMoveEffect(void); void StealTargetItem(u8 battlerStealer, u8 battlerItem); u8 GetCatchingBattler(void); u32 GetHighestStatId(u32 battlerId); +bool32 DoSwitchInAbilitiesItems(u32 battlerId); extern void (* const gBattleScriptingCommandsTable[])(void); extern const u8 gBattlePalaceNatureToMoveGroupLikelihood[NUM_NATURES][4]; diff --git a/src/battle_script_commands.c b/src/battle_script_commands.c index 8b8d4f1e9..9555111b4 100644 --- a/src/battle_script_commands.c +++ b/src/battle_script_commands.c @@ -6900,6 +6900,35 @@ static void SetDmgHazardsBattlescript(u8 battlerId, u8 multistringId) gBattlescriptCurrInstr = BattleScript_DmgHazardsOnFaintedBattler; } +bool32 DoSwitchInAbilitiesItems(u32 battlerId) +{ + return (AbilityBattleEffects(ABILITYEFFECT_ON_SWITCHIN, battlerId, 0, 0, 0) + || (gBattleWeather & B_WEATHER_ANY && WEATHER_HAS_EFFECT && AbilityBattleEffects(ABILITYEFFECT_ON_WEATHER, battlerId, 0, 0, 0)) + || (gFieldStatuses & STATUS_FIELD_TERRAIN_ANY && AbilityBattleEffects(ABILITYEFFECT_ON_TERRAIN, battlerId, 0, 0, 0)) + || ItemBattleEffects(ITEMEFFECT_ON_SWITCH_IN, battlerId, FALSE) + || AbilityBattleEffects(ABILITYEFFECT_TRACE2, 0, 0, 0, 0)); +} + +bool32 ShouldPostponeSwitchInAbilities(u32 battlerId) +{ + bool32 aliveOpposing1 = IsBattlerAlive(BATTLE_OPPOSITE(battlerId)); + bool32 aliveOpposing2 = IsBattlerAlive(BATTLE_PARTNER(BATTLE_OPPOSITE(battlerId))); + // No pokemon on opposing side - postopone. + if (!aliveOpposing1 && !aliveOpposing2) + return TRUE; + + // Checks for double battle, so abilities like Intimidate wait until all battlers are switched-in before activating. + if (IsDoubleBattle()) + { + if (aliveOpposing1 && !aliveOpposing2 && !HasNoMonsToSwitch(BATTLE_OPPOSITE(battlerId), PARTY_SIZE, PARTY_SIZE)) + return TRUE; + if (!aliveOpposing1 && aliveOpposing2 && !HasNoMonsToSwitch(BATTLE_PARTNER(BATTLE_OPPOSITE(battlerId)), PARTY_SIZE, PARTY_SIZE)) + return TRUE; + } + + return FALSE; +} + static void Cmd_switchineffects(void) { CMD_ARGS(u8 battler); @@ -7016,12 +7045,17 @@ static void Cmd_switchineffects(void) gDisableStructs[gActiveBattler].truantSwitchInHack = 0; - if (AbilityBattleEffects(ABILITYEFFECT_ON_SWITCHIN, gActiveBattler, 0, 0, 0) - || (gBattleWeather & B_WEATHER_ANY && WEATHER_HAS_EFFECT && AbilityBattleEffects(ABILITYEFFECT_ON_WEATHER, gActiveBattler, 0, 0, 0)) - || (gFieldStatuses & STATUS_FIELD_TERRAIN_ANY && AbilityBattleEffects(ABILITYEFFECT_ON_TERRAIN, gActiveBattler, 0, 0, 0)) - || ItemBattleEffects(ITEMEFFECT_ON_SWITCH_IN, gActiveBattler, FALSE) - || AbilityBattleEffects(ABILITYEFFECT_TRACE2, 0, 0, 0, 0)) - return; + // Don't activate switch-in abilities if the opposing field is empty. + // This could happen when a mon uses explosion and causes everyone to faint. + if (ShouldPostponeSwitchInAbilities(gActiveBattler) || gBattleStruct->switchInAbilityPostponed) + { + gBattleStruct->switchInAbilityPostponed |= gBitTable[gActiveBattler]; + } + else + { + if (DoSwitchInAbilitiesItems(gActiveBattler)) + return; + } gSideStatuses[GetBattlerSide(gActiveBattler)] &= ~(SIDE_STATUS_SPIKES_DAMAGED | SIDE_STATUS_TOXIC_SPIKES_DAMAGED | SIDE_STATUS_STEALTH_ROCK_DAMAGED | SIDE_STATUS_STICKY_WEB_DAMAGED); @@ -11016,21 +11050,21 @@ static void Cmd_various(void) return; } case VARIOUS_JUMP_IF_NO_VALID_TARGETS: - { - VARIOUS_ARGS(const u8 *jumpInstr); - u32 count = 0; + { + VARIOUS_ARGS(const u8 *jumpInstr); + u32 count = 0; - for (i = 0; i < gBattlersCount; i++) - { - if (GetBattlerSide(i) != GetBattlerSide(gBattlerAttacker) && IsBattlerAlive(i)) - count++; - } - if (count == 0) - gBattlescriptCurrInstr = cmd->jumpInstr; - else - gBattlescriptCurrInstr = cmd->nextInstr; - return; + for (i = 0; i < gBattlersCount; i++) + { + if (GetBattlerSide(i) != GetBattlerSide(gBattlerAttacker) && IsBattlerAlive(i)) + count++; } + if (count == 0) + gBattlescriptCurrInstr = cmd->jumpInstr; + else + gBattlescriptCurrInstr = cmd->nextInstr; + return; + } case VARIOUS_JUMP_IF_EMERGENCY_EXITED: { VARIOUS_ARGS(const u8 *jumpInstr); diff --git a/src/battle_util.c b/src/battle_util.c index 25ba83093..185575411 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -3301,7 +3301,7 @@ bool8 HandleWishPerishSongOnTurnEnd(void) return FALSE; } -#define FAINTED_ACTIONS_MAX_CASE 7 +#define FAINTED_ACTIONS_MAX_CASE 8 bool8 HandleFaintedMonActions(void) { @@ -3386,7 +3386,19 @@ bool8 HandleFaintedMonActions(void) else gBattleStruct->faintedActionsState = 4; break; - case 6: + case 6: // All battlers switch-in abilities happen here to prevent them happening against an empty field. + for (i = 0; i < gBattlersCount; i++) + { + if (gBattleStruct->switchInAbilityPostponed & gBitTable[i]) + { + if (DoSwitchInAbilitiesItems(i)) + return TRUE; + gBattleStruct->switchInAbilityPostponed &= ~(gBitTable[i]); + } + } + gBattleStruct->faintedActionsState++; + break; + case 7: if (ItemBattleEffects(ITEMEFFECT_NORMAL, 0, TRUE)) return TRUE; gBattleStruct->faintedActionsState++; @@ -4749,6 +4761,7 @@ u8 AbilityBattleEffects(u8 caseID, u8 battler, u16 ability, u8 special, u16 move case ABILITY_INTIMIDATE: if (!gSpecialStatuses[battler].switchInAbilityDone) { + gBattlerAttacker = battler; gSpecialStatuses[battler].switchInAbilityDone = TRUE; SET_STATCHANGER(STAT_ATK, 1, TRUE); BattleScriptPushCursorAndCallback(BattleScript_IntimidateActivates); diff --git a/src/data/trainer_parties.h b/src/data/trainer_parties.h index 1759120b7..de2d1e897 100644 --- a/src/data/trainer_parties.h +++ b/src/data/trainer_parties.h @@ -400,17 +400,17 @@ static const struct TrainerMonNoItemDefaultMoves sParty_Rose1[] = { { .iv = 0, .lvl = 14, - .species = SPECIES_ROSELIA, + .species = SPECIES_PORYGON, }, { .iv = 0, .lvl = 14, - .species = SPECIES_SHROOMISH, + .species = SPECIES_PORYGON, }, { .iv = 0, .lvl = 14, - .species = SPECIES_ROSELIA, + .species = SPECIES_PORYGON, } }; @@ -9631,17 +9631,17 @@ static const struct TrainerMonNoItemDefaultMoves sParty_Deandre[] = { { .iv = 0, .lvl = 14, - .species = SPECIES_ZIGZAGOON, + .species = SPECIES_PORYGON, }, { .iv = 0, .lvl = 14, - .species = SPECIES_ARON, + .species = SPECIES_PORYGON, }, { .iv = 0, .lvl = 14, - .species = SPECIES_ELECTRIKE, + .species = SPECIES_PORYGON, } }; diff --git a/test/ability_intimidate.c b/test/ability_intimidate.c index 450ca6402..a0f48776c 100644 --- a/test/ability_intimidate.c +++ b/test/ability_intimidate.c @@ -30,13 +30,12 @@ SINGLE_BATTLE_TEST("Intimidate (opponent) lowers player's attack after switch ou SINGLE_BATTLE_TEST("Intimidate (opponent) lowers player's attack after KO", s16 damage) { u32 ability; - KNOWN_FAILING; PARAMETRIZE { ability = ABILITY_INTIMIDATE; } PARAMETRIZE { ability = ABILITY_RECKLESS; } GIVEN { - PLAYER(SPECIES_WOBBUFFET) { Speed(2); }; + PLAYER(SPECIES_WOBBUFFET) { Speed(2); Attack(120) ; }; OPPONENT(SPECIES_WOBBUFFET) { HP(1); Speed(1); }; - OPPONENT(SPECIES_STARAPTOR) { Ability(ABILITY_INTIMIDATE); Speed(1); }; + OPPONENT(SPECIES_STARAPTOR) { Ability(ability); Speed(1); }; } WHEN { TURN { MOVE(player, MOVE_TACKLE); SEND_OUT(opponent, 1); } TURN { MOVE(player, MOVE_TACKLE); } @@ -48,3 +47,48 @@ SINGLE_BATTLE_TEST("Intimidate (opponent) lowers player's attack after KO", s16 EXPECT_MUL_EQ(results[0].damage, Q_4_12(1.5), results[1].damage); } } + +DOUBLE_BATTLE_TEST("Intimidate doesn't activate on an empty field in a double battle") +{ + GIVEN { + ASSUME(gBattleMoves[MOVE_EXPLOSION].effect == EFFECT_EXPLOSION); + PLAYER(SPECIES_WOBBUFFET) { }; + PLAYER(SPECIES_WOBBUFFET) { HP(1); }; + PLAYER(SPECIES_STARAVIA) { Ability(ABILITY_INTIMIDATE); }; + PLAYER(SPECIES_ABRA); + OPPONENT(SPECIES_WOBBUFFET) { HP(1); }; + OPPONENT(SPECIES_WOBBUFFET) { HP(1); }; + OPPONENT(SPECIES_STARAPTOR) { Ability(ABILITY_INTIMIDATE); }; + OPPONENT(SPECIES_WYNAUT); + } WHEN { + TURN { MOVE(playerLeft, MOVE_EXPLOSION); SEND_OUT(playerLeft, 2); SEND_OUT(opponentLeft, 2); SEND_OUT(playerRight, 3); SEND_OUT(opponentRight, 3); } + TURN { MOVE(playerLeft, MOVE_CELEBRATE);} + } SCENE { + HP_BAR(playerLeft, hp: 0); + ANIMATION(ANIM_TYPE_MOVE, MOVE_EXPLOSION, playerLeft); + // Leaving these messages as they're not that important to the test, to not exceed MAX_QUEUED_EVENTS. + /* + MESSAGE("Foe Wobbuffet fainted!"); + MESSAGE("Wobbuffet fainted!"); + MESSAGE("Foe Wobbuffet fainted!"); + MESSAGE("Wobbuffet fainted!"); + */ + + MESSAGE("Go! Staravia!"); + MESSAGE("2 sent out Staraptor!"); + MESSAGE("Go! Abra!"); + MESSAGE("2 sent out Wynaut!"); + + ABILITY_POPUP(playerLeft, ABILITY_INTIMIDATE); + ANIMATION(ANIM_TYPE_GENERAL, B_ANIM_STATS_CHANGE, opponentLeft); + MESSAGE("Staravia's Intimidate cuts Foe Staraptor's ATTACK!"); + ANIMATION(ANIM_TYPE_GENERAL, B_ANIM_STATS_CHANGE, opponentRight); + MESSAGE("Staravia's Intimidate cuts Foe Wynaut's ATTACK!"); + + ABILITY_POPUP(opponentLeft, ABILITY_INTIMIDATE); + ANIMATION(ANIM_TYPE_GENERAL, B_ANIM_STATS_CHANGE, playerLeft); + MESSAGE("Foe Staraptor's Intimidate cuts Staravia's ATTACK!"); + ANIMATION(ANIM_TYPE_GENERAL, B_ANIM_STATS_CHANGE, playerRight); + MESSAGE("Foe Staraptor's Intimidate cuts Abra's ATTACK!"); + } +} diff --git a/test/test_runner.c b/test/test_runner.c index 3cb1f7c21..c4befb35f 100644 --- a/test/test_runner.c +++ b/test/test_runner.c @@ -7,7 +7,7 @@ #include "test.h" #include "test_runner.h" -#define TIMEOUT_SECONDS 30 +#define TIMEOUT_SECONDS 49 void CB2_TestRunner(void);