[Beta][P2] Fix Sketch failing to sketch moves that call other moves virtually (#4823)

* [P2][Beta] Fix Sketch failing to sketch Metronome et al

* Suggested changes to `getLastXMoves()`

* Renamed turnCount to moveCount
This commit is contained in:
PigeonBar 2024-11-09 13:15:24 -05:00 committed by GitHub
parent a763cd173d
commit 2bf8acea06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 70 additions and 54 deletions

View File

@ -6373,10 +6373,17 @@ export class RandomMovesetMoveAttr extends OverrideMoveEffectAttr {
} }
export class RandomMoveAttr extends OverrideMoveEffectAttr { export class RandomMoveAttr extends OverrideMoveEffectAttr {
/**
* This function exists solely to allow tests to override the randomly selected move by mocking this function.
*/
public getMoveOverride(): Moves | null {
return null;
}
apply(user: Pokemon, target: Pokemon, move: Move, args: any[]): Promise<boolean> { apply(user: Pokemon, target: Pokemon, move: Move, args: any[]): Promise<boolean> {
return new Promise(resolve => { return new Promise(resolve => {
const moveIds = Utils.getEnumValues(Moves).filter(m => !allMoves[m].hasFlag(MoveFlags.IGNORE_VIRTUAL) && !allMoves[m].name.endsWith(" (N)")); const moveIds = Utils.getEnumValues(Moves).filter(m => !allMoves[m].hasFlag(MoveFlags.IGNORE_VIRTUAL) && !allMoves[m].name.endsWith(" (N)"));
const moveId = moveIds[user.randSeedInt(moveIds.length)]; const moveId = this.getMoveOverride() ?? moveIds[user.randSeedInt(moveIds.length)];
const moveTargets = getMoveTargets(user, moveId); const moveTargets = getMoveTargets(user, moveId);
if (!moveTargets.targets.length) { if (!moveTargets.targets.length) {
@ -6759,7 +6766,7 @@ export class SketchAttr extends MoveEffectAttr {
return false; return false;
} }
const targetMove = target.getLastXMoves(target.battleSummonData.turnCount) const targetMove = target.getLastXMoves(-1)
.find(m => m.move !== Moves.NONE && m.move !== Moves.STRUGGLE && !m.virtual); .find(m => m.move !== Moves.NONE && m.move !== Moves.STRUGGLE && !m.virtual);
if (!targetMove) { if (!targetMove) {
return false; return false;

View File

@ -3226,9 +3226,21 @@ export default abstract class Pokemon extends Phaser.GameObjects.Container {
this.getMoveHistory().push(turnMove); this.getMoveHistory().push(turnMove);
} }
getLastXMoves(turnCount: integer = 0): TurnMove[] { /**
* Returns a list of the most recent move entries in this Pokemon's move history.
* The retrieved move entries are sorted in order from NEWEST to OLDEST.
* @param moveCount The number of move entries to retrieve.
* If negative, retrieve the Pokemon's entire move history (equivalent to reversing the output of {@linkcode getMoveHistory()}).
* Default is `1`.
* @returns A list of {@linkcode TurnMove}, as specified above.
*/
getLastXMoves(moveCount: number = 1): TurnMove[] {
const moveHistory = this.getMoveHistory(); const moveHistory = this.getMoveHistory();
return moveHistory.slice(turnCount >= 0 ? Math.max(moveHistory.length - (turnCount || 1), 0) : 0, moveHistory.length).reverse(); if (moveCount >= 0) {
return moveHistory.slice(Math.max(moveHistory.length - moveCount, 0)).reverse();
} else {
return moveHistory.slice(0).reverse();
}
} }
getMoveQueue(): QueuedMove[] { getMoveQueue(): QueuedMove[] {

View File

@ -728,10 +728,10 @@ export abstract class PokemonHeldItemModifier extends PersistentModifier {
//Applies to items with chance of activating secondary effects ie Kings Rock //Applies to items with chance of activating secondary effects ie Kings Rock
getSecondaryChanceMultiplier(pokemon: Pokemon): number { getSecondaryChanceMultiplier(pokemon: Pokemon): number {
// Temporary quickfix to stop game from freezing when the opponet uses u-turn while holding on to king's rock // Temporary quickfix to stop game from freezing when the opponet uses u-turn while holding on to king's rock
if (!pokemon.getLastXMoves(0)[0]) { if (!pokemon.getLastXMoves()[0]) {
return 1; return 1;
} }
const sheerForceAffected = allMoves[pokemon.getLastXMoves(0)[0].move].chance >= 0 && pokemon.hasAbility(Abilities.SHEER_FORCE); const sheerForceAffected = allMoves[pokemon.getLastXMoves()[0].move].chance >= 0 && pokemon.hasAbility(Abilities.SHEER_FORCE);
if (sheerForceAffected) { if (sheerForceAffected) {
return 0; return 0;

View File

@ -8,7 +8,8 @@ import { Moves } from "#enums/moves";
import { Species } from "#enums/species"; import { Species } from "#enums/species";
import GameManager from "#test/utils/gameManager"; import GameManager from "#test/utils/gameManager";
import Phaser from "phaser"; import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { allMoves, RandomMoveAttr } from "#app/data/move";
// See also: TypeImmunityAbAttr // See also: TypeImmunityAbAttr
describe("Abilities - Sap Sipper", () => { describe("Abilities - Sap Sipper", () => {
@ -27,20 +28,20 @@ describe("Abilities - Sap Sipper", () => {
beforeEach(() => { beforeEach(() => {
game = new GameManager(phaserGame); game = new GameManager(phaserGame);
game.override.battleType("single"); game.override.battleType("single")
game.override.disableCrits(); .disableCrits()
.ability(Abilities.SAP_SIPPER)
.enemySpecies(Species.RATTATA)
.enemyAbility(Abilities.SAP_SIPPER)
.enemyMoveset(Moves.SPLASH);
}); });
it("raises ATK stat stage by 1 and block effects when activated against a grass attack", async() => { it("raises ATK stat stage by 1 and block effects when activated against a grass attack", async() => {
const moveToUse = Moves.LEAFAGE; const moveToUse = Moves.LEAFAGE;
const enemyAbility = Abilities.SAP_SIPPER;
game.override.moveset([ moveToUse ]); game.override.moveset(moveToUse);
game.override.enemyMoveset(Moves.SPLASH);
game.override.enemySpecies(Species.DUSKULL);
game.override.enemyAbility(enemyAbility);
await game.startBattle(); await game.classicMode.startBattle([ Species.BULBASAUR ]);
const enemyPokemon = game.scene.getEnemyPokemon()!; const enemyPokemon = game.scene.getEnemyPokemon()!;
const initialEnemyHp = enemyPokemon.hp; const initialEnemyHp = enemyPokemon.hp;
@ -55,14 +56,10 @@ describe("Abilities - Sap Sipper", () => {
it("raises ATK stat stage by 1 and block effects when activated against a grass status move", async() => { it("raises ATK stat stage by 1 and block effects when activated against a grass status move", async() => {
const moveToUse = Moves.SPORE; const moveToUse = Moves.SPORE;
const enemyAbility = Abilities.SAP_SIPPER;
game.override.moveset([ moveToUse ]); game.override.moveset(moveToUse);
game.override.enemyMoveset(Moves.SPLASH);
game.override.enemySpecies(Species.RATTATA);
game.override.enemyAbility(enemyAbility);
await game.startBattle(); await game.classicMode.startBattle([ Species.BULBASAUR ]);
const enemyPokemon = game.scene.getEnemyPokemon()!; const enemyPokemon = game.scene.getEnemyPokemon()!;
@ -76,14 +73,10 @@ describe("Abilities - Sap Sipper", () => {
it("do not activate against status moves that target the field", async () => { it("do not activate against status moves that target the field", async () => {
const moveToUse = Moves.GRASSY_TERRAIN; const moveToUse = Moves.GRASSY_TERRAIN;
const enemyAbility = Abilities.SAP_SIPPER;
game.override.moveset([ moveToUse ]); game.override.moveset(moveToUse);
game.override.enemyMoveset(Moves.SPLASH);
game.override.enemySpecies(Species.RATTATA);
game.override.enemyAbility(enemyAbility);
await game.startBattle(); await game.classicMode.startBattle([ Species.BULBASAUR ]);
game.move.select(moveToUse); game.move.select(moveToUse);
@ -96,14 +89,10 @@ describe("Abilities - Sap Sipper", () => {
it("activate once against multi-hit grass attacks", async () => { it("activate once against multi-hit grass attacks", async () => {
const moveToUse = Moves.BULLET_SEED; const moveToUse = Moves.BULLET_SEED;
const enemyAbility = Abilities.SAP_SIPPER;
game.override.moveset([ moveToUse ]); game.override.moveset(moveToUse);
game.override.enemyMoveset(Moves.SPLASH);
game.override.enemySpecies(Species.RATTATA);
game.override.enemyAbility(enemyAbility);
await game.startBattle(); await game.classicMode.startBattle([ Species.BULBASAUR ]);
const enemyPokemon = game.scene.getEnemyPokemon()!; const enemyPokemon = game.scene.getEnemyPokemon()!;
const initialEnemyHp = enemyPokemon.hp; const initialEnemyHp = enemyPokemon.hp;
@ -118,15 +107,10 @@ describe("Abilities - Sap Sipper", () => {
it("do not activate against status moves that target the user", async () => { it("do not activate against status moves that target the user", async () => {
const moveToUse = Moves.SPIKY_SHIELD; const moveToUse = Moves.SPIKY_SHIELD;
const ability = Abilities.SAP_SIPPER;
game.override.moveset([ moveToUse ]); game.override.moveset(moveToUse);
game.override.ability(ability);
game.override.enemyMoveset(Moves.SPLASH);
game.override.enemySpecies(Species.RATTATA);
game.override.enemyAbility(Abilities.NONE);
await game.startBattle(); await game.classicMode.startBattle([ Species.BULBASAUR ]);
const playerPokemon = game.scene.getPlayerPokemon()!; const playerPokemon = game.scene.getPlayerPokemon()!;
@ -142,18 +126,15 @@ describe("Abilities - Sap Sipper", () => {
expect(game.phaseInterceptor.log).not.toContain("ShowAbilityPhase"); expect(game.phaseInterceptor.log).not.toContain("ShowAbilityPhase");
}); });
// TODO Add METRONOME outcome override it("activate once against multi-hit grass attacks (metronome)", async () => {
// To run this testcase, manually modify the METRONOME move to always give SAP_SIPPER, then uncomment
it.todo("activate once against multi-hit grass attacks (metronome)", async () => {
const moveToUse = Moves.METRONOME; const moveToUse = Moves.METRONOME;
const enemyAbility = Abilities.SAP_SIPPER;
game.override.moveset([ moveToUse ]); const randomMoveAttr = allMoves[Moves.METRONOME].findAttr(attr => attr instanceof RandomMoveAttr) as RandomMoveAttr;
game.override.enemyMoveset([ Moves.SPLASH, Moves.NONE, Moves.NONE, Moves.NONE ]); vi.spyOn(randomMoveAttr, "getMoveOverride").mockReturnValue(Moves.BULLET_SEED);
game.override.enemySpecies(Species.RATTATA);
game.override.enemyAbility(enemyAbility);
await game.startBattle(); game.override.moveset(moveToUse);
await game.classicMode.startBattle([ Species.BULBASAUR ]);
const enemyPokemon = game.scene.getEnemyPokemon()!; const enemyPokemon = game.scene.getEnemyPokemon()!;
const initialEnemyHp = enemyPokemon.hp; const initialEnemyHp = enemyPokemon.hp;
@ -168,11 +149,8 @@ describe("Abilities - Sap Sipper", () => {
it("still activates regardless of accuracy check", async () => { it("still activates regardless of accuracy check", async () => {
game.override.moveset(Moves.LEAF_BLADE); game.override.moveset(Moves.LEAF_BLADE);
game.override.enemyMoveset(Moves.SPLASH);
game.override.enemySpecies(Species.MAGIKARP);
game.override.enemyAbility(Abilities.SAP_SIPPER);
await game.classicMode.startBattle(); await game.classicMode.startBattle([ Species.BULBASAUR ]);
const enemyPokemon = game.scene.getEnemyPokemon()!; const enemyPokemon = game.scene.getEnemyPokemon()!;

View File

@ -4,9 +4,10 @@ import { Species } from "#enums/species";
import { MoveResult, PokemonMove } from "#app/field/pokemon"; import { MoveResult, PokemonMove } from "#app/field/pokemon";
import GameManager from "#test/utils/gameManager"; import GameManager from "#test/utils/gameManager";
import Phaser from "phaser"; import Phaser from "phaser";
import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { StatusEffect } from "#app/enums/status-effect"; import { StatusEffect } from "#app/enums/status-effect";
import { BattlerIndex } from "#app/battle"; import { BattlerIndex } from "#app/battle";
import { allMoves, RandomMoveAttr } from "#app/data/move";
describe("Moves - Sketch", () => { describe("Moves - Sketch", () => {
let phaserGame: Phaser.Game; let phaserGame: Phaser.Game;
@ -76,4 +77,22 @@ describe("Moves - Sketch", () => {
expect(playerPokemon.moveset[0]?.moveId).toBe(Moves.SPLASH); expect(playerPokemon.moveset[0]?.moveId).toBe(Moves.SPLASH);
expect(playerPokemon.moveset[1]?.moveId).toBe(Moves.GROWL); expect(playerPokemon.moveset[1]?.moveId).toBe(Moves.GROWL);
}); });
it("should sketch moves that call other moves", async () => {
const randomMoveAttr = allMoves[Moves.METRONOME].findAttr(attr => attr instanceof RandomMoveAttr) as RandomMoveAttr;
vi.spyOn(randomMoveAttr, "getMoveOverride").mockReturnValue(Moves.FALSE_SWIPE);
game.override.enemyMoveset([ Moves.METRONOME ]);
await game.classicMode.startBattle([ Species.REGIELEKI ]);
const playerPokemon = game.scene.getPlayerPokemon()!;
playerPokemon.moveset = [ new PokemonMove(Moves.SKETCH) ];
// Opponent uses Metronome -> False Swipe, then player uses Sketch, which should sketch Metronome
game.move.select(Moves.SKETCH);
await game.setTurnOrder([ BattlerIndex.ENEMY, BattlerIndex.PLAYER ]);
await game.phaseInterceptor.to("TurnEndPhase");
expect(playerPokemon.getLastXMoves()[0].result).toBe(MoveResult.SUCCESS);
expect(playerPokemon.moveset[0]?.moveId).toBe(Moves.METRONOME);
expect(playerPokemon.hp).toBeLessThan(playerPokemon.getMaxHp()); // Make sure opponent actually used False Swipe
});
}); });