diff --git a/src/data/moves/move.ts b/src/data/moves/move.ts index c11b0188991..01e9b276833 100644 --- a/src/data/moves/move.ts +++ b/src/data/moves/move.ts @@ -7781,17 +7781,27 @@ export class StatusIfBoostedAttr extends MoveEffectAttr { } } +/** + * Attribute to fail move usage unless all of the user's other moves have been used at least once. + * Used by {@linkcode Moves.LAST_RESORT}. + */ export class LastResortAttr extends MoveAttr { + // TODO: Verify behavior as Bulbapedia page is _extremely_ poorly documented getCondition(): MoveConditionFunc { - return (user: Pokemon, target: Pokemon, move: Move) => { - const uniqueUsedMoveIds = new Set(); - const movesetMoveIds = user.getMoveset().map(m => m.moveId); - user.getMoveHistory().map(m => { - if (m.move !== move.id && movesetMoveIds.find(mm => mm === m.move)) { - uniqueUsedMoveIds.add(m.move); - } - }); - return uniqueUsedMoveIds.size >= movesetMoveIds.length - 1; + return (user: Pokemon, _target: Pokemon, move: Move) => { + const movesInMoveset = new Set(user.getMoveset().map(m => m.moveId)); + if (!movesInMoveset.delete(move.id) || !movesInMoveset.size) { + return false; // Last resort fails if used when not in user's moveset or no other moves exist + } + + const movesInHistory = new Set( + user.getMoveHistory() + .filter(m => !m.virtual) // TODO: Change to (m) => m < MoveUseType.INDIRECT after Dancer PR refactors virtual into enum + .map(m => m.move) + ); + + // Since `Set.intersection()` is only present in ESNext, we have to coerce it to an array to check inclusion + return [...movesInMoveset].every(m => movesInHistory.has(m)) }; } } @@ -9009,6 +9019,7 @@ export function initMoves() { .soundBased() .target(MoveTarget.RANDOM_NEAR_ENEMY) .partial(), // Does not lock the user, does not stop Pokemon from sleeping + // Likely can make use of FrenzyAttr and an ArenaTag (just without the FrenzyMissFunc) new SelfStatusMove(Moves.STOCKPILE, PokemonType.NORMAL, -1, 20, -1, 0, 3) .condition(user => (user.getTag(StockpilingTag)?.stockpiledCount ?? 0) < 3) .attr(AddBattlerTagAttr, BattlerTagType.STOCKPILING, true), @@ -9444,7 +9455,8 @@ export function initMoves() { .makesContact(true) .attr(PunishmentPowerAttr), new AttackMove(Moves.LAST_RESORT, PokemonType.NORMAL, MoveCategory.PHYSICAL, 140, 100, 5, -1, 0, 4) - .attr(LastResortAttr), + .attr(LastResortAttr) + .edgeCase(), // May or may not need to ignore remotely called moves depending on how it works new StatusMove(Moves.WORRY_SEED, PokemonType.GRASS, 100, 10, -1, 0, 4) .attr(AbilityChangeAttr, Abilities.INSOMNIA) .reflectable(), diff --git a/src/field/pokemon.ts b/src/field/pokemon.ts index 65481c8a9f5..2a3abba01a0 100644 --- a/src/field/pokemon.ts +++ b/src/field/pokemon.ts @@ -7768,16 +7768,26 @@ export class PokemonSummonData { /** Array containing all berries eaten in the last turn; used by {@linkcode Abilities.CUD_CHEW} */ public berriesEatenLast: BerryType[] = []; - /** The number of turns the pokemon has passed since entering the battle */ + /** + * The number of turns this pokemon has spent in the active position since entering the battle. + * Currently exclusively used for positioning the battle cursor. + */ public turnCount = 1; - /** The number of turns the pokemon has passed since the start of the wave */ + /** + * The number of turns this pokemon has spent in the active position since starting a new wave. + * Used for most "first turn only" conditions ({@linkcode Moves.FAKE_OUT | Fake Out}, {@linkcode Moves.FIRST_IMPRESSION | First Impression}, etc.) + */ public waveTurnCount = 1; - /** The list of moves the pokemon has used since entering the battle */ + /** + * An array of all moves this pokemon has used since entering the battle. + * Used for most moves and abilities that check prior move usage or copy already-used moves. + */ public moveHistory: TurnMove[] = []; constructor(source?: PokemonSummonData | Partial) { if (!isNullOrUndefined(source)) { Object.assign(this, source) + // TODO: Do we need these mapping statements? this.moveset &&= this.moveset.map(m => PokemonMove.loadMove(m)) this.tags &&= this.tags.map(t => loadBattlerTag(t)) } diff --git a/src/phases/switch-summon-phase.ts b/src/phases/switch-summon-phase.ts index 35d83ac3922..a2feea55c3c 100644 --- a/src/phases/switch-summon-phase.ts +++ b/src/phases/switch-summon-phase.ts @@ -67,7 +67,8 @@ export class SwitchSummonPhase extends SummonPhase { !(this.player ? globalScene.getPlayerParty() : globalScene.getEnemyParty())[this.slotIndex]) ) { if (this.player) { - return this.switchAndSummon(); + this.switchAndSummon(); + return; } globalScene.time.delayedCall(750, () => this.switchAndSummon()); return; diff --git a/test/moves/fake_out.test.ts b/test/moves/fake_out.test.ts index cbce16270e0..404473c8fa0 100644 --- a/test/moves/fake_out.test.ts +++ b/test/moves/fake_out.test.ts @@ -26,64 +26,71 @@ describe("Moves - Fake Out", () => { .moveset([Moves.FAKE_OUT, Moves.SPLASH]) .enemyMoveset(Moves.SPLASH) .enemyLevel(10) - .startingLevel(10) // prevent LevelUpPhase from happening + .startingLevel(1) // prevent LevelUpPhase from happening .disableCrits(); }); - it("can only be used on the first turn a pokemon is sent out in a battle", async () => { + it("should only work the first turn a pokemon is sent out in a battle", async () => { await game.classicMode.startBattle([Species.FEEBAS]); - const enemy = game.scene.getEnemyPokemon()!; + const corv = game.scene.getEnemyPokemon()!; game.move.select(Moves.FAKE_OUT); await game.toNextTurn(); - expect(enemy.hp).toBeLessThan(enemy.getMaxHp()); - const postTurnOneHp = enemy.hp; + expect(corv.hp).toBeLessThan(corv.getMaxHp()); + const postTurnOneHp = corv.hp; game.move.select(Moves.FAKE_OUT); await game.toNextTurn(); - expect(enemy.hp).toBe(postTurnOneHp); - }, 20000); + expect(corv.hp).toBe(postTurnOneHp); + }); // This is a PokeRogue buff to Fake Out - it("can be used at the start of every wave even if the pokemon wasn't recalled", async () => { + it("should succeed at the start of each new wave, even if user wasn't recalled", async () => { await game.classicMode.startBattle([Species.FEEBAS]); - const enemy = game.scene.getEnemyPokemon()!; - enemy.damageAndUpdate(enemy.getMaxHp() - 1); - + // set hp to 1 for easy knockout + game.scene.getEnemyPokemon()!.hp = 1; game.move.select(Moves.FAKE_OUT); await game.toNextWave(); game.move.select(Moves.FAKE_OUT); await game.toNextTurn(); - expect(game.scene.getEnemyPokemon()!.isFullHp()).toBe(false); - }, 20000); + const corv = game.scene.getEnemyPokemon()!; + expect(corv).toBeDefined(); + expect(corv?.hp).toBeLessThan(corv?.getMaxHp()); + }); - it("can be used again if recalled and sent back out", async () => { - game.override.startingWave(4); + // This is a PokeRogue buff to Fake Out + it("should succeed at the start of each new wave, even if user wasn't recalled", async () => { + await game.classicMode.startBattle([Species.FEEBAS]); + + // set hp to 1 for easy knockout + game.scene.getEnemyPokemon()!.hp = 1; + game.move.select(Moves.FAKE_OUT); + await game.toNextWave(); + + game.move.select(Moves.FAKE_OUT); + await game.toNextTurn(); + + const corv = game.scene.getEnemyPokemon()!; + expect(corv).toBeDefined(); + expect(corv.hp).toBeLessThan(corv.getMaxHp()); + }); + + it("should succeed if recalled and sent back out", async () => { await game.classicMode.startBattle([Species.FEEBAS, Species.MAGIKARP]); - const enemy1 = game.scene.getEnemyPokemon()!; - - game.move.select(Moves.FAKE_OUT); - await game.phaseInterceptor.to("MoveEndPhase"); - - expect(enemy1.hp).toBeLessThan(enemy1.getMaxHp()); - - await game.doKillOpponents(); - await game.toNextWave(); - game.move.select(Moves.FAKE_OUT); await game.toNextTurn(); - const enemy2 = game.scene.getEnemyPokemon()!; + const corv = game.scene.getEnemyPokemon()!; - expect(enemy2.hp).toBeLessThan(enemy2.getMaxHp()); - enemy2.hp = enemy2.getMaxHp(); + expect(corv.hp).toBeLessThan(corv.getMaxHp()); + corv.hp = corv.getMaxHp(); game.doSwitchPokemon(1); await game.toNextTurn(); @@ -94,6 +101,6 @@ describe("Moves - Fake Out", () => { game.move.select(Moves.FAKE_OUT); await game.toNextTurn(); - expect(enemy2.hp).toBeLessThan(enemy2.getMaxHp()); - }, 20000); + expect(corv.hp).toBeLessThan(corv.getMaxHp()); + }); }); diff --git a/test/moves/last-resort.test.ts b/test/moves/last-resort.test.ts new file mode 100644 index 00000000000..a7b462f3ca4 --- /dev/null +++ b/test/moves/last-resort.test.ts @@ -0,0 +1,166 @@ +import { BattlerIndex } from "#app/battle"; +import { MoveResult } from "#app/field/pokemon"; +import { Abilities } from "#enums/abilities"; +import { Moves } from "#enums/moves"; +import { Species } from "#enums/species"; +import GameManager from "#test/testUtils/gameManager"; +import Phaser from "phaser"; +import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; + +describe("Moves - Last Resort", () => { + let phaserGame: Phaser.Game; + let game: GameManager; + + function expectLastResortFail() { + expect(game.scene.getPlayerPokemon()?.getLastXMoves()[0]).toEqual( + expect.objectContaining({ + move: Moves.LAST_RESORT, + result: MoveResult.FAIL, + }), + ); + } + beforeAll(() => { + phaserGame = new Phaser.Game({ + type: Phaser.HEADLESS, + }); + }); + + afterEach(() => { + game.phaseInterceptor.restoreOg(); + }); + + beforeEach(() => { + game = new GameManager(phaserGame); + game.override + .ability(Abilities.BALL_FETCH) + .battleStyle("single") + .disableCrits() + .enemySpecies(Species.MAGIKARP) + .enemyAbility(Abilities.BALL_FETCH) + .enemyMoveset(Moves.SPLASH); + }); + + it("should fail unless all other moves (excluding itself) has been used at least once", async () => { + game.override.moveset([Moves.LAST_RESORT, Moves.SPLASH, Moves.GROWL, Moves.GROWTH]); + await game.classicMode.startBattle([Species.BLISSEY]); + + const blissey = game.scene.getPlayerPokemon()!; + expect(blissey).toBeDefined(); + + // Last resort by itself + game.move.select(Moves.LAST_RESORT); + await game.phaseInterceptor.to("TurnEndPhase"); + expectLastResortFail(); + + // Splash (1/3) + blissey.pushMoveHistory({ move: Moves.SPLASH, targets: [BattlerIndex.PLAYER] }); + game.move.select(Moves.LAST_RESORT); + await game.phaseInterceptor.to("TurnEndPhase"); + expectLastResortFail(); + + // Growl (2/3) + blissey.pushMoveHistory({ move: Moves.GROWL, targets: [BattlerIndex.ENEMY] }); + game.move.select(Moves.LAST_RESORT); + await game.phaseInterceptor.to("TurnEndPhase"); + expectLastResortFail(); // Were last resort itself counted, it would error here + + // Growth (3/3) + blissey.pushMoveHistory({ move: Moves.GROWTH, targets: [BattlerIndex.PLAYER] }); + game.move.select(Moves.LAST_RESORT); + await game.phaseInterceptor.to("TurnEndPhase"); + expect(game.scene.getPlayerPokemon()?.getLastXMoves()[0]).toEqual( + expect.objectContaining({ + move: Moves.LAST_RESORT, + result: MoveResult.SUCCESS, + }), + ); + }); + + it("should disregard virtually invoked moves", async () => { + game.override + .moveset([Moves.LAST_RESORT, Moves.SWORDS_DANCE, Moves.ABSORB, Moves.MIRROR_MOVE]) + .enemyMoveset([Moves.SWORDS_DANCE, Moves.ABSORB]) + .ability(Abilities.DANCER) + .enemySpecies(Species.ABOMASNOW); // magikarp has 50% chance to be okho'd on absorb crit + await game.classicMode.startBattle([Species.BLISSEY]); + + // use mirror move normally to trigger absorb virtually + game.move.select(Moves.MIRROR_MOVE); + await game.forceEnemyMove(Moves.ABSORB); + await game.toNextTurn(); + + game.move.select(Moves.LAST_RESORT); + await game.forceEnemyMove(Moves.SWORDS_DANCE); // goes first to proc dancer ahead of time + await game.setTurnOrder([BattlerIndex.ENEMY, BattlerIndex.PLAYER]); + await game.phaseInterceptor.to("TurnEndPhase"); + expectLastResortFail(); + }); + + it("should fail if no other moves in moveset", async () => { + game.override.moveset(Moves.LAST_RESORT); + await game.classicMode.startBattle([Species.BLISSEY]); + + game.move.select(Moves.LAST_RESORT); + await game.phaseInterceptor.to("TurnEndPhase"); + + expectLastResortFail(); + }); + + it("should work if invoked virtually when all other moves have been used", async () => { + game.override.moveset([Moves.LAST_RESORT, Moves.SLEEP_TALK]).ability(Abilities.COMATOSE); + await game.classicMode.startBattle([Species.KOMALA]); + + game.move.select(Moves.SLEEP_TALK); + await game.phaseInterceptor.to("TurnEndPhase"); + + expect(game.scene.getPlayerPokemon()?.getLastXMoves(-1)).toEqual([ + expect.objectContaining({ + move: Moves.LAST_RESORT, + result: MoveResult.SUCCESS, + virtual: true, + }), + expect.objectContaining({ + move: Moves.SLEEP_TALK, + result: MoveResult.SUCCESS, + }), + ]); + }); + + it("should preserve usability status on reload", async () => { + game.override.moveset([Moves.LAST_RESORT, Moves.SPLASH]).ability(Abilities.COMATOSE); + await game.classicMode.startBattle([Species.BLISSEY]); + + game.move.select(Moves.SPLASH); + await game.doKillOpponents(); + await game.toNextWave(); + + const oldMoveHistory = game.scene.getPlayerPokemon()?.summonData.moveHistory; + await game.reload.reloadSession(); + + const newMoveHistory = game.scene.getPlayerPokemon()?.summonData.moveHistory; + expect(oldMoveHistory).toEqual(newMoveHistory); + + // use last resort and it should kill the karp just fine + game.move.select(Moves.LAST_RESORT); + game.scene.getEnemyPokemon()!.hp = 1; + await game.phaseInterceptor.to("TurnEndPhase"); + + expect(game.isVictory()).toBe(true); + }); + + it("should fail if used while not in moveset", async () => { + game.override.moveset(Moves.MIRROR_MOVE).enemyMoveset([Moves.ABSORB, Moves.LAST_RESORT]); + await game.classicMode.startBattle([Species.BLISSEY]); + + // ensure enemy last resort succeeds + game.move.select(Moves.MIRROR_MOVE); + await game.forceEnemyMove(Moves.ABSORB); + await game.phaseInterceptor.to("TurnEndPhase"); + game.move.select(Moves.MIRROR_MOVE); + await game.forceEnemyMove(Moves.LAST_RESORT); + await game.setTurnOrder([BattlerIndex.ENEMY, BattlerIndex.PLAYER]); + await game.phaseInterceptor.to("TurnEndPhase"); + + expectLastResortFail(); + }); +});