diff --git a/src/data/move.ts b/src/data/move.ts index 28cfc6f2668..d93869dedb9 100644 --- a/src/data/move.ts +++ b/src/data/move.ts @@ -1,5 +1,5 @@ import { ChargeAnim, MoveChargeAnim, initMoveAnim, loadMoveAnimAssets } from "./battle-anims"; -import { BattleEndPhase, MoveEndPhase, MovePhase, NewBattlePhase, PartyStatusCurePhase, PokemonHealPhase, StatChangePhase, SwitchSummonPhase } from "../phases"; +import { BattleEndPhase, MoveEndPhase, MovePhase, NewBattlePhase, PartyStatusCurePhase, PokemonHealPhase, StatChangePhase, SwitchPhase, SwitchSummonPhase } from "../phases"; import { BattleStat, getBattleStatName } from "./battle-stat"; import { EncoreTag, GulpMissileTag, HelpingHandTag, SemiInvulnerableTag, StockpilingTag, TypeBoostTag } from "./battler-tags"; import { getPokemonNameWithAffix } from "../messages"; @@ -10,7 +10,7 @@ import { Constructor } from "#app/utils"; import * as Utils from "../utils"; import { WeatherType } from "./weather"; import { ArenaTagSide, ArenaTrapTag, WeakenMoveTypeTag } from "./arena-tag"; -import { UnswappableAbilityAbAttr, UncopiableAbilityAbAttr, UnsuppressableAbilityAbAttr, BlockRecoilDamageAttr, BlockOneHitKOAbAttr, IgnoreContactAbAttr, MaxMultiHitAbAttr, applyAbAttrs, BlockNonDirectDamageAbAttr, applyPreSwitchOutAbAttrs, PreSwitchOutAbAttr, applyPostDefendAbAttrs, PostDefendContactApplyStatusEffectAbAttr, MoveAbilityBypassAbAttr, ReverseDrainAbAttr, FieldPreventExplosiveMovesAbAttr, ForceSwitchOutImmunityAbAttr, BlockItemTheftAbAttr, applyPostAttackAbAttrs, ConfusionOnStatusEffectAbAttr, HealFromBerryUseAbAttr, IgnoreProtectOnContactAbAttr, IgnoreMoveEffectsAbAttr, applyPreDefendAbAttrs, MoveEffectChanceMultiplierAbAttr, WonderSkinAbAttr, applyPreAttackAbAttrs, MoveTypeChangeAttr, UserFieldMoveTypePowerBoostAbAttr, FieldMoveTypePowerBoostAbAttr, AllyMoveCategoryPowerBoostAbAttr, VariableMovePowerAbAttr } from "./ability"; +import { UnswappableAbilityAbAttr, UncopiableAbilityAbAttr, UnsuppressableAbilityAbAttr, BlockRecoilDamageAttr, BlockOneHitKOAbAttr, IgnoreContactAbAttr, MaxMultiHitAbAttr, applyAbAttrs, BlockNonDirectDamageAbAttr, MoveAbilityBypassAbAttr, ReverseDrainAbAttr, FieldPreventExplosiveMovesAbAttr, ForceSwitchOutImmunityAbAttr, BlockItemTheftAbAttr, applyPostAttackAbAttrs, ConfusionOnStatusEffectAbAttr, HealFromBerryUseAbAttr, IgnoreProtectOnContactAbAttr, IgnoreMoveEffectsAbAttr, applyPreDefendAbAttrs, MoveEffectChanceMultiplierAbAttr, WonderSkinAbAttr, applyPreAttackAbAttrs, MoveTypeChangeAttr, UserFieldMoveTypePowerBoostAbAttr, FieldMoveTypePowerBoostAbAttr, AllyMoveCategoryPowerBoostAbAttr, VariableMovePowerAbAttr } from "./ability"; import { allAbilities } from "./ability"; import { PokemonHeldItemModifier, BerryModifier, PreserveBerryModifier, PokemonMoveAccuracyBoosterModifier, AttackTypeBoosterModifier, PokemonMultiHitModifier } from "../modifier/modifier"; import { BattlerIndex, BattleType } from "../battle"; @@ -4806,13 +4806,15 @@ export class ForceSwitchOutAttr extends MoveEffectAttr { this.batonPass = !!batonPass; } + isBatonPass() { + return this.batonPass; + } + apply(user: Pokemon, target: Pokemon, move: Move, args: any[]): Promise { return new Promise(resolve => { // Check if the move category is not STATUS or if the switch out condition is not met if (!this.getSwitchOutCondition()(user, target, move)) { - //Apply effects before switch out i.e. poison point, flame body, etc - applyPostDefendAbAttrs(PostDefendContactApplyStatusEffectAbAttr, target, user, move, null); return resolve(false); } @@ -4820,10 +4822,11 @@ export class ForceSwitchOutAttr extends MoveEffectAttr { // This ensures that the switch out only happens when the conditions are met const switchOutTarget = this.user ? user : target; if (switchOutTarget instanceof PlayerPokemon) { + switchOutTarget.leaveField(!this.batonPass); + if (switchOutTarget.hp > 0) { - applyPreSwitchOutAbAttrs(PreSwitchOutAbAttr, switchOutTarget); - // switchOut below sets the UI to select party(this is not a separate Phase), then adds a SwitchSummonPhase with selected 'mon - (switchOutTarget as PlayerPokemon).switchOut(this.batonPass).then(() => resolve(true)); + user.scene.prependToPhase(new SwitchPhase(user.scene, switchOutTarget.getFieldIndex(), true, true), MoveEndPhase); + resolve(true); } else { resolve(false); } diff --git a/src/phases.ts b/src/phases.ts index e88f0699918..f561ea6f3fc 100644 --- a/src/phases.ts +++ b/src/phases.ts @@ -4464,11 +4464,24 @@ export class PostGameOverPhase extends Phase { } } +/** + * Opens the party selector UI and transitions into a {@linkcode SwitchSummonPhase} + * for the player (if a switch would be valid for the current battle state). + */ export class SwitchPhase extends BattlePhase { protected fieldIndex: integer; private isModal: boolean; private doReturn: boolean; + /** + * Creates a new SwitchPhase + * @param scene {@linkcode BattleScene} Current battle scene + * @param fieldIndex Field index to switch out + * @param isModal Indicates if the switch should be forced (true) or is + * optional (false). + * @param doReturn Indicates if the party member on the field should be + * recalled to ball or has already left the field. Passed to {@linkcode SwitchSummonPhase}. + */ constructor(scene: BattleScene, fieldIndex: integer, isModal: boolean, doReturn: boolean) { super(scene); @@ -4480,13 +4493,17 @@ export class SwitchPhase extends BattlePhase { start() { super.start(); - // Skip modal switch if impossible + // Skip modal switch if impossible (no remaining party members that aren't in battle) if (this.isModal && !this.scene.getParty().filter(p => p.isAllowedInBattle() && !p.isActive(true)).length) { return super.end(); } - // Skip if the fainted party member has been revived already - if (this.isModal && !this.scene.getParty()[this.fieldIndex].isFainted()) { + // Skip if the fainted party member has been revived already. doReturn is + // only passed as `false` from FaintPhase (as opposed to other usages such + // as ForceSwitchOutAttr or CheckSwitchPhase), so we only want to check this + // if the mon should have already been returned but is still alive and well + // on the field. see also; battle.test.ts + if (this.isModal && !this.doReturn && !this.scene.getParty()[this.fieldIndex].isFainted()) { return super.end(); } diff --git a/src/test/battle/battle.test.ts b/src/test/battle/battle.test.ts index 35eae9b96d2..21b890d9cf0 100644 --- a/src/test/battle/battle.test.ts +++ b/src/test/battle/battle.test.ts @@ -1,9 +1,24 @@ import { allSpecies } from "#app/data/pokemon-species"; +import { TempBattleStat } from "#app/data/temp-battle-stat.js"; import { GameModes } from "#app/game-mode"; import { getGameMode } from "#app/game-mode.js"; -import { CommandPhase, DamagePhase, EncounterPhase, EnemyCommandPhase, LoginPhase, SelectGenderPhase, SelectModifierPhase, SelectStarterPhase, SummonPhase, TitlePhase, TurnInitPhase, VictoryPhase } from "#app/phases"; -import GameManager from "#test/utils/gameManager"; -import { generateStarter, getMovePosition, } from "#test/utils/gameManagerUtils"; +import { + BattleEndPhase, + CommandPhase, DamagePhase, + EncounterPhase, + EnemyCommandPhase, + LoginPhase, + NextEncounterPhase, + SelectGenderPhase, + SelectModifierPhase, + SelectStarterPhase, + SummonPhase, + SwitchPhase, + TitlePhase, + TurnInitPhase, VictoryPhase, +} from "#app/phases"; +import GameManager from "#app/test/utils/gameManager"; +import { generateStarter, getMovePosition, } from "#app/test/utils/gameManagerUtils"; import { Command } from "#app/ui/command-ui-handler"; import { Mode } from "#app/ui/ui"; import { Abilities } from "#enums/abilities"; @@ -12,6 +27,7 @@ import { PlayerGender } from "#enums/player-gender"; import { Species } from "#enums/species"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; +import { SPLASH_ONLY } from "../utils/testUtils"; describe("Test Battle Phase", () => { let phaserGame: Phaser.Game; @@ -312,5 +328,31 @@ describe("Test Battle Phase", () => { await game.toNextWave(); expect(game.scene.currentBattle.waveIndex).toBeGreaterThan(waveIndex); }, 20000); + + it("does not force switch if active pokemon faints at same time as enemy mon and is revived in post-battle", async () => { + const moveToUse = Moves.TAKE_DOWN; + game.override + .battleType("single") + .starterSpecies(Species.SAWK) + .enemySpecies(Species.RATTATA) + .startingWave(1) + .startingLevel(100) + .moveset([moveToUse]) + .enemyMoveset(SPLASH_ONLY) + .startingHeldItems([{ name: "TEMP_STAT_BOOSTER", type: TempBattleStat.ACC }]); + + await game.startBattle(); + game.scene.getPlayerPokemon().hp = 1; + game.doAttack(getMovePosition(game.scene, 0, moveToUse)); + + await game.phaseInterceptor.to(BattleEndPhase); + game.doRevivePokemon(0); // pretend max revive was picked + game.doSelectModifier(); + + game.onNextPrompt("SwitchPhase", Mode.PARTY, () => { + expect.fail("Switch was forced"); + }, () => game.isCurrentPhase(NextEncounterPhase)); + await game.phaseInterceptor.to(SwitchPhase); + }, 20000); }); diff --git a/src/test/moves/baton_pass.test.ts b/src/test/moves/baton_pass.test.ts new file mode 100644 index 00000000000..ba961613998 --- /dev/null +++ b/src/test/moves/baton_pass.test.ts @@ -0,0 +1,93 @@ +import { BattleStat } from "#app/data/battle-stat.js"; +import { PostSummonPhase, TurnEndPhase } from "#app/phases.js"; +import GameManager from "#app/test/utils/gameManager"; +import { getMovePosition } from "#app/test/utils/gameManagerUtils"; +import { Moves } from "#enums/moves"; +import { Species } from "#enums/species"; +import Phaser from "phaser"; +import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; +import { SPLASH_ONLY } from "../utils/testUtils"; + + +describe("Moves - Baton Pass", () => { + let phaserGame: Phaser.Game; + let game: GameManager; + + beforeAll(() => { + phaserGame = new Phaser.Game({ + type: Phaser.HEADLESS, + }); + }); + + afterEach(() => { + game.phaseInterceptor.restoreOg(); + }); + + beforeEach(() => { + game = new GameManager(phaserGame); + game.override + .battleType("single") + .enemySpecies(Species.DUGTRIO) + .startingLevel(1) + .startingWave(97) + .moveset([Moves.BATON_PASS, Moves.NASTY_PLOT, Moves.SPLASH]) + .enemyMoveset(SPLASH_ONLY) + .disableCrits(); + }); + + it("passes stat stage buffs when player uses it", async() => { + // arrange + await game.startBattle([ + Species.RAICHU, + Species.SHUCKLE + ]); + + // round 1 - buff + game.doAttack(getMovePosition(game.scene, 0, Moves.NASTY_PLOT)); + await game.toNextTurn(); + expect(game.scene.getPlayerPokemon().summonData.battleStats[BattleStat.SPATK]).toEqual(2); + + // round 2 - baton pass + game.doAttack(getMovePosition(game.scene, 0, Moves.BATON_PASS)); + game.doSelectPartyPokemon(1); + await game.phaseInterceptor.to(TurnEndPhase); + + // assert + expect(game.scene.getPlayerPokemon().species.speciesId).toEqual(Species.SHUCKLE); + expect(game.scene.getPlayerPokemon().summonData.battleStats[BattleStat.SPATK]).toEqual(2); + }, 20000); + + it("passes stat stage buffs when AI uses it", async() => { + // arrange + game.override + .startingWave(5) + .enemyMoveset(new Array(4).fill([Moves.NASTY_PLOT])); + await game.startBattle([ + Species.RAICHU, + Species.SHUCKLE + ]); + + // round 1 - ai buffs + game.doAttack(getMovePosition(game.scene, 0, Moves.SPLASH)); + await game.toNextTurn(); + + // round 2 - baton pass + game.scene.getEnemyPokemon().hp = 100; + game.override.enemyMoveset(new Array(4).fill(Moves.BATON_PASS)); + game.doAttack(getMovePosition(game.scene, 0, Moves.SPLASH)); + await game.phaseInterceptor.to(PostSummonPhase, false); + + // assert + // check buffs are still there + expect(game.scene.getEnemyPokemon().summonData.battleStats[BattleStat.SPATK]).toEqual(2); + // confirm that a switch actually happened. can't use species because I + // can't find a way to override trainer parties with more than 1 pokemon species + expect(game.scene.getEnemyPokemon().hp).not.toEqual(100); + expect(game.phaseInterceptor.log.slice(-4)).toEqual([ + "MoveEffectPhase", + "SwitchSummonPhase", + "SummonPhase", + "PostSummonPhase" + ]); + }, 20000); +}); diff --git a/src/test/moves/u_turn.test.ts b/src/test/moves/u_turn.test.ts new file mode 100644 index 00000000000..c54a94dde2b --- /dev/null +++ b/src/test/moves/u_turn.test.ts @@ -0,0 +1,98 @@ +import { Abilities } from "#app/enums/abilities.js"; +import { SwitchPhase, TurnEndPhase } from "#app/phases"; +import GameManager from "#app/test/utils/gameManager"; +import { getMovePosition } from "#app/test/utils/gameManagerUtils"; +import { Moves } from "#enums/moves"; +import { Species } from "#enums/species"; +import Phaser from "phaser"; +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import { StatusEffect } from "#app/enums/status-effect.js"; +import { SPLASH_ONLY } from "../utils/testUtils"; + +describe("Moves - U-turn", () => { + let phaserGame: Phaser.Game; + let game: GameManager; + + beforeAll(() => { + phaserGame = new Phaser.Game({ + type: Phaser.HEADLESS, + }); + }); + + afterEach(() => { + game.phaseInterceptor.restoreOg(); + }); + + beforeEach(() => { + game = new GameManager(phaserGame); + game.override + .battleType("single") + .enemySpecies(Species.GENGAR) + .startingLevel(90) + .startingWave(97) + .moveset([Moves.U_TURN]) + .enemyMoveset(SPLASH_ONLY) + .disableCrits(); + }); + + it("triggers regenerator a single time when a regenerator user switches out with u-turn", async() => { + // arrange + const playerHp = 1; + game.override.ability(Abilities.REGENERATOR); + await game.startBattle([ + Species.RAICHU, + Species.SHUCKLE + ]); + game.scene.getPlayerPokemon().hp = playerHp; + + // act + game.doAttack(getMovePosition(game.scene, 0, Moves.U_TURN)); + game.doSelectPartyPokemon(1); + await game.phaseInterceptor.to(TurnEndPhase); + + // assert + expect(game.scene.getParty()[1].hp).toEqual(Math.floor(game.scene.getParty()[1].getMaxHp() * 0.33 + playerHp)); + expect(game.phaseInterceptor.log).toContain("SwitchSummonPhase"); + expect(game.scene.getPlayerPokemon().species.speciesId).toBe(Species.SHUCKLE); + }, 20000); + + it("triggers rough skin on the u-turn user before a new pokemon is switched in", async() => { + // arrange + game.override.enemyAbility(Abilities.ROUGH_SKIN); + await game.startBattle([ + Species.RAICHU, + Species.SHUCKLE + ]); + + // act + game.doAttack(getMovePosition(game.scene, 0, Moves.U_TURN)); + game.doSelectPartyPokemon(1); + await game.phaseInterceptor.to(SwitchPhase, false); + + // assert + expect(game.scene.getPlayerPokemon().hp).not.toEqual(game.scene.getPlayerPokemon().getMaxHp()); + expect(game.scene.getEnemyPokemon().battleData.abilityRevealed).toBe(true); // proxy for asserting ability activated + expect(game.scene.getPlayerPokemon().species.speciesId).toEqual(Species.RAICHU); + expect(game.phaseInterceptor.log).not.toContain("SwitchSummonPhase"); + }, 20000); + + it("triggers contact abilities on the u-turn user (eg poison point) before a new pokemon is switched in", async() => { + // arrange + game.override.enemyAbility(Abilities.POISON_POINT); + await game.startBattle([ + Species.RAICHU, + Species.SHUCKLE + ]); + vi.spyOn(game.scene.getEnemyPokemon(), "randSeedInt").mockReturnValue(0); + + // act + game.doAttack(getMovePosition(game.scene, 0, Moves.U_TURN)); + await game.phaseInterceptor.to(SwitchPhase, false); + + // assert + expect(game.scene.getPlayerPokemon().status?.effect).toEqual(StatusEffect.POISON); + expect(game.scene.getPlayerPokemon().species.speciesId).toEqual(Species.RAICHU); + expect(game.scene.getEnemyPokemon().battleData.abilityRevealed).toBe(true); // proxy for asserting ability activated + expect(game.phaseInterceptor.log).not.toContain("SwitchSummonPhase"); + }, 20000); +}); diff --git a/src/test/utils/gameManager.ts b/src/test/utils/gameManager.ts index ac7f7aea4d2..5540295d341 100644 --- a/src/test/utils/gameManager.ts +++ b/src/test/utils/gameManager.ts @@ -1,7 +1,7 @@ import GameWrapper from "#test/utils/gameWrapper"; import { Mode } from "#app/ui/ui"; import { generateStarter, waitUntil } from "#test/utils/gameManagerUtils"; -import { CommandPhase, EncounterPhase, FaintPhase, LoginPhase, NewBattlePhase, SelectStarterPhase, SelectTargetPhase, TitlePhase, TurnEndPhase, TurnInitPhase, TurnStartPhase } from "#app/phases"; +import { CommandPhase, EncounterPhase, FaintPhase, LoginPhase, MovePhase, NewBattlePhase, SelectStarterPhase, SelectTargetPhase, TitlePhase, TurnEndPhase, TurnInitPhase, TurnStartPhase } from "#app/phases"; import BattleScene from "#app/battle-scene.js"; import PhaseInterceptor from "#test/utils/phaseInterceptor"; import TextInterceptor from "#test/utils/TextInterceptor"; @@ -9,13 +9,12 @@ import { GameModes, getGameMode } from "#app/game-mode"; import fs from "fs"; import { AES, enc } from "crypto-js"; import { updateUserInfo } from "#app/account"; -import InputsHandler from "#test/utils/inputsHandler"; -import ErrorInterceptor from "#test/utils/errorInterceptor"; +import InputsHandler from "#app/test/utils/inputsHandler"; +import ErrorInterceptor from "#app/test/utils/errorInterceptor"; import { EnemyPokemon, PlayerPokemon } from "#app/field/pokemon"; -import { MockClock } from "#test/utils/mocks/mockClock"; -import { Command } from "#app/ui/command-ui-handler"; -import ModifierSelectUiHandler from "#app/ui/modifier-select-ui-handler"; -import PartyUiHandler, { PartyUiMode } from "#app/ui/party-ui-handler"; +import { MockClock } from "#app/test/utils/mocks/mockClock"; +import PartyUiHandler from "#app/ui/party-ui-handler"; +import CommandUiHandler, { Command } from "#app/ui/command-ui-handler"; import Trainer from "#app/field/trainer"; import { ExpNotification } from "#enums/exp-notification"; import { GameDataType } from "#enums/game-data-type"; @@ -28,6 +27,7 @@ import { OverridesHelper } from "./overridesHelper"; import { ModifierTypeOption, modifierTypes } from "#app/modifier/modifier-type.js"; import overrides from "#app/overrides.js"; import { removeEnemyHeldItems } from "./testUtils"; +import ModifierSelectUiHandler from "#app/ui/modifier-select-ui-handler.js"; /** * Class to manage the game state and transitions between phases. @@ -178,7 +178,7 @@ export default class GameManager { if (move.isMultiTarget()) { handler.processInput(Button.ACTION); } - }, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(TurnEndPhase)); + }, () => this.isCurrentPhase(CommandPhase) || this.isCurrentPhase(MovePhase) || this.isCurrentPhase(TurnEndPhase)); } /** @@ -313,20 +313,20 @@ export default class GameManager { } /** - * Switch pokemon and transition to the enemy command phase + * Command an in-battle switch to another Pokemon via the main battle menu. * @param pokemonIndex the index of the pokemon in your party to switch to */ doSwitchPokemon(pokemonIndex: number) { this.onNextPrompt("CommandPhase", Mode.COMMAND, () => { - this.scene.ui.setMode(Mode.PARTY, PartyUiMode.SWITCH, (this.scene.getCurrentPhase() as CommandPhase).getPokemon().getFieldIndex(), null, PartyUiHandler.FilterNonFainted); - }); - this.onNextPrompt("CommandPhase", Mode.PARTY, () => { - (this.scene.getCurrentPhase() as CommandPhase).handleCommand(Command.POKEMON, pokemonIndex, false); + (this.scene.ui.getHandler() as CommandUiHandler).setCursor(2); + (this.scene.ui.getHandler() as CommandUiHandler).processInput(Button.ACTION); }); + + this.doSelectPartyPokemon(pokemonIndex, "CommandPhase"); } /** - * Revive pokemon, currently player's only. + * Revive pokemon, currently players only. * @param pokemonIndex the index of the pokemon in your party to revive */ doRevivePokemon(pokemonIndex: number) { @@ -335,4 +335,23 @@ export default class GameManager { const modifier = candidate.type.newModifier(party[pokemonIndex]); this.scene.addModifier(modifier, false); } + + /** + * Select a pokemon from the party menu. Only really handles the basic cases + * of the party UI, where you just need to navigate to a party slot and press + * Action twice - navigating any menus that come up after you select a party member + * is not supported. + * @param slot the index of the pokemon in your party to switch to + * @param inPhase Which phase to expect the selection to occur in. Typically + * non-command switch actions happen in SwitchPhase. + */ + doSelectPartyPokemon(slot: number, inPhase = "SwitchPhase") { + this.onNextPrompt(inPhase, Mode.PARTY, () => { + const partyHandler = this.scene.ui.getHandler() as PartyUiHandler; + + partyHandler.setCursor(slot); + partyHandler.processInput(Button.ACTION); // select party slot + partyHandler.processInput(Button.ACTION); // send out (or whatever option is at the top) + }); + } } diff --git a/src/test/utils/phaseInterceptor.ts b/src/test/utils/phaseInterceptor.ts index 383624bf298..aabde643aa8 100644 --- a/src/test/utils/phaseInterceptor.ts +++ b/src/test/utils/phaseInterceptor.ts @@ -42,7 +42,7 @@ import { QuietFormChangePhase } from "#app/form-change-phase"; export default class PhaseInterceptor { public scene; public phases = {}; - public log; + public log: string[]; private onHold; private interval; private promptInterval; @@ -104,13 +104,20 @@ export default class PhaseInterceptor { */ constructor(scene) { this.scene = scene; - this.log = []; this.onHold = []; this.prompts = []; + this.clearLogs(); this.startPromptHandler(); this.initPhases(); } + /** + * Clears phase logs + */ + clearLogs() { + this.log = []; + } + rejectAll(error) { if (this.inProgress) { clearInterval(this.promptInterval); diff --git a/src/ui/party-ui-handler.ts b/src/ui/party-ui-handler.ts index 80ce318532b..7b77b71f4ec 100644 --- a/src/ui/party-ui-handler.ts +++ b/src/ui/party-ui-handler.ts @@ -1,13 +1,13 @@ import { CommandPhase, SelectModifierPhase } from "../phases"; import BattleScene from "../battle-scene"; -import { PlayerPokemon, PokemonMove } from "../field/pokemon"; +import { MoveResult, PlayerPokemon, PokemonMove } from "../field/pokemon"; import { addBBCodeTextObject, addTextObject, getTextColor, TextStyle } from "./text"; import { Command } from "./command-ui-handler"; import MessageUiHandler from "./message-ui-handler"; import { Mode } from "./ui"; import * as Utils from "../utils"; import { PokemonBaseStatModifier, PokemonFormChangeItemModifier, PokemonHeldItemModifier, SwitchEffectTransferModifier } from "../modifier/modifier"; -import { allMoves } from "../data/move"; +import { allMoves, ForceSwitchOutAttr } from "../data/move"; import { getGenderColor, getGenderSymbol } from "../data/gender"; import { StatusEffect } from "../data/status-effect"; import PokemonIconAnimHandler, { PokemonIconAnimMode } from "./pokemon-icon-anim-handler"; @@ -25,18 +25,69 @@ import { getPokemonNameWithAffix } from "#app/messages.js"; const defaultMessage = i18next.t("partyUiHandler:choosePokemon"); +/** + * Indicates the reason why the party UI is being opened. + */ export enum PartyUiMode { + /** + * Indicates that the party UI is open because of a user-opted switch. This + * type of switch can be cancelled. + */ SWITCH, + /** + * Indicates that the party UI is open because of a faint or other forced + * switch (eg, move effect). This type of switch cannot be cancelled. + */ FAINT_SWITCH, + /** + * Indicates that the party UI is open because of a start-of-encounter optional + * switch. This type of switch can be cancelled. + */ POST_BATTLE_SWITCH, + /** + * Indicates that the party UI is open because of the move Revival Blessing. + * This selection cannot be cancelled. + */ REVIVAL_BLESSING, + /** + * Indicates that the party UI is open to select a mon to apply a modifier to. + * This type of selection can be cancelled. + */ MODIFIER, + /** + * Indicates that the party UI is open to select a mon to apply a move + * modifier to (such as an Ether or PP Up). This type of selection can be cancelled. + */ MOVE_MODIFIER, + /** + * Indicates that the party UI is open to select a mon to teach a TM. This + * type of selection can be cancelled. + */ TM_MODIFIER, + /** + * Indicates that the party UI is open to select a mon to remember a move. + * This type of selection can be cancelled. + */ REMEMBER_MOVE_MODIFIER, + /** + * Indicates that the party UI is open to transfer items between mons. This + * type of selection can be cancelled. + */ MODIFIER_TRANSFER, + /** + * Indicates that the party UI is open because of a DNA Splicer. This + * type of selection can be cancelled. + */ SPLICE, + /** + * Indicates that the party UI is open to release a party member. This + * type of selection can be cancelled. + */ RELEASE, + /** + * Indicates that the party UI is open to check the team. This + * type of selection can be cancelled. + */ CHECK } @@ -767,10 +818,21 @@ export default class PartyUiHandler extends MessageUiHandler { case PartyUiMode.FAINT_SWITCH: case PartyUiMode.POST_BATTLE_SWITCH: if (this.cursor >= this.scene.currentBattle.getBattlerCount()) { - this.options.push(PartyOption.SEND_OUT); - if (this.partyUiMode !== PartyUiMode.FAINT_SWITCH - && this.scene.findModifier(m => m instanceof SwitchEffectTransferModifier - && (m as SwitchEffectTransferModifier).pokemonId === this.scene.getPlayerField()[this.fieldIndex].id)) { + const allowBatonModifierSwitch = + this.partyUiMode !== PartyUiMode.FAINT_SWITCH + && this.scene.findModifier(m => m instanceof SwitchEffectTransferModifier + && (m as SwitchEffectTransferModifier).pokemonId === this.scene.getPlayerField()[this.fieldIndex].id); + + const moveHistory = this.scene.getPlayerField()[this.fieldIndex].getMoveHistory(); + const isBatonPassMove = this.partyUiMode === PartyUiMode.FAINT_SWITCH && moveHistory.length && allMoves[moveHistory[moveHistory.length - 1].move].getAttrs(ForceSwitchOutAttr)[0]?.isBatonPass() && moveHistory[moveHistory.length - 1].result === MoveResult.SUCCESS; + + // isBatonPassMove and allowBatonModifierSwitch shouldn't ever be true + // at the same time, because they both explicitly check for a mutually + // exclusive partyUiMode. But better safe than sorry. + this.options.push(isBatonPassMove && !allowBatonModifierSwitch ? PartyOption.PASS_BATON : PartyOption.SEND_OUT); + if (allowBatonModifierSwitch && !isBatonPassMove) { + // the BATON modifier gives an extra switch option for + // pokemon-command switches, allowing buffs to be optionally passed this.options.push(PartyOption.PASS_BATON); } }