From 126174efe4162693f17fbb44116ce1723bacc0bb Mon Sep 17 00:00:00 2001 From: innerthunder <168692175+innerthunder@users.noreply.github.com> Date: Wed, 3 Jul 2024 11:48:41 -0700 Subject: [PATCH] [Bug] Fix Grip Claw sometimes stealing from the wrong enemy (#2766) * Fix Grip Claw stealing from the wrong enemy * Document held item transfer modifiers --- src/modifier/modifier.ts | 51 +++++++++++++++++++++- src/phases.ts | 2 +- src/test/items/grip_claw.test.ts | 75 ++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/test/items/grip_claw.test.ts diff --git a/src/modifier/modifier.ts b/src/modifier/modifier.ts index f7c23406179..0b339906cc5 100644 --- a/src/modifier/modifier.ts +++ b/src/modifier/modifier.ts @@ -2116,14 +2116,38 @@ export class SwitchEffectTransferModifier extends PokemonHeldItemModifier { } } +/** + * Abstract class for held items that steal other Pokemon's items. + * @see {@linkcode TurnHeldItemTransferModifier} + * @see {@linkcode ContactHeldItemTransferChanceModifier} + */ export abstract class HeldItemTransferModifier extends PokemonHeldItemModifier { constructor(type: ModifierType, pokemonId: integer, stackCount?: integer) { super(type, pokemonId, stackCount); } + /** + * Determines the targets to transfer items from when this applies. + * @param args\[0\] the {@linkcode Pokemon} holding this item + * @returns the opponents of the source {@linkcode Pokemon} + */ + getTargets(args: any[]): Pokemon[] { + const pokemon = args[0]; + + return pokemon instanceof Pokemon + ? pokemon.getOpponents() + : []; + } + + /** + * Steals an item from a set of target Pokemon. + * This prioritizes high-tier held items when selecting the item to steal. + * @param args \[0\] The {@linkcode Pokemon} holding this item + * @returns true if an item was stolen; false otherwise. + */ apply(args: any[]): boolean { const pokemon = args[0] as Pokemon; - const opponents = pokemon.getOpponents(); + const opponents = this.getTargets(args); if (!opponents.length) { return false; @@ -2180,6 +2204,11 @@ export abstract class HeldItemTransferModifier extends PokemonHeldItemModifier { abstract getTransferMessage(pokemon: Pokemon, targetPokemon: Pokemon, item: ModifierTypes.ModifierType): string; } +/** + * Modifier for held items that steal items from the enemy at the end of + * each turn. + * @see {@linkcode modifierTypes[MINI_BLACK_HOLE]} + */ export class TurnHeldItemTransferModifier extends HeldItemTransferModifier { constructor(type: ModifierType, pokemonId: integer, stackCount?: integer) { super(type, pokemonId, stackCount); @@ -2210,6 +2239,12 @@ export class TurnHeldItemTransferModifier extends HeldItemTransferModifier { } } +/** + * Modifier for held items that add a chance to steal items from the target of a + * successful attack. + * @see {@linkcode modifierTypes[GRIP_CLAW]} + * @see {@linkcode HeldItemTransferModifier} + */ export class ContactHeldItemTransferChanceModifier extends HeldItemTransferModifier { private chance: number; @@ -2219,6 +2254,20 @@ export class ContactHeldItemTransferChanceModifier extends HeldItemTransferModif this.chance = chancePercent / 100; } + /** + * Determines the target to steal items from when this applies. + * @param args\[0\] The {@linkcode Pokemon} holding this item + * @param args\[1\] The {@linkcode Pokemon} the holder is targeting with an attack + * @returns The target (args[1]) stored in array format for use in {@linkcode HeldItemTransferModifier.apply} + */ + getTargets(args: any[]): Pokemon[] { + const target = args[1]; + + return target instanceof Pokemon + ? [ target ] + : []; + } + matchType(modifier: Modifier): boolean { return modifier instanceof ContactHeldItemTransferChanceModifier; } diff --git a/src/phases.ts b/src/phases.ts index dd400c22076..df7314e6285 100644 --- a/src/phases.ts +++ b/src/phases.ts @@ -2966,7 +2966,7 @@ export class MoveEffectPhase extends PokemonPhase { })).then(() => { applyPostAttackAbAttrs(PostAttackAbAttr, user, target, this.move.getMove(), hitResult).then(() => { if (this.move.getMove() instanceof AttackMove) { - this.scene.applyModifiers(ContactHeldItemTransferChanceModifier, this.player, user, target.getFieldIndex()); + this.scene.applyModifiers(ContactHeldItemTransferChanceModifier, this.player, user, target); } resolve(); }); diff --git a/src/test/items/grip_claw.test.ts b/src/test/items/grip_claw.test.ts new file mode 100644 index 00000000000..ae621770da6 --- /dev/null +++ b/src/test/items/grip_claw.test.ts @@ -0,0 +1,75 @@ +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; +import Phase from "phaser"; +import GameManager from "#app/test/utils/gameManager"; +import * as overrides from "#app/overrides"; +import { Moves } from "#app/enums/moves.js"; +import { Species } from "#app/enums/species.js"; +import { BerryType } from "#app/enums/berry-type.js"; +import { Abilities } from "#app/enums/abilities.js"; +import { getMovePosition } from "../utils/gameManagerUtils"; +import { CommandPhase, MoveEndPhase, SelectTargetPhase } from "#app/phases.js"; +import { BattlerIndex } from "#app/battle.js"; +import { allMoves } from "#app/data/move.js"; + +const TIMEOUT = 20 * 1000; // 20 seconds + +describe("Items - Grip Claw", () => { + let phaserGame: Phaser.Game; + let game: GameManager; + + beforeAll(() => { + phaserGame = new Phase.Game({ + type: Phaser.HEADLESS, + }); + }); + + afterEach(() => { + game.phaseInterceptor.restoreOg(); + }); + + beforeEach(() => { + game = new GameManager(phaserGame); + + vi.spyOn(overrides, "DOUBLE_BATTLE_OVERRIDE", "get").mockReturnValue(true); + vi.spyOn(overrides, "MOVESET_OVERRIDE", "get").mockReturnValue([ Moves.POPULATION_BOMB, Moves.SPLASH ]); + vi.spyOn(overrides, "STARTING_HELD_ITEMS_OVERRIDE", "get").mockReturnValue([{name: "GRIP_CLAW", count: 5}, {name: "MULTI_LENS", count: 3}]); + vi.spyOn(overrides, "OPP_SPECIES_OVERRIDE", "get").mockReturnValue(Species.SNORLAX); + vi.spyOn(overrides, "ABILITY_OVERRIDE", "get").mockReturnValue(Abilities.KLUTZ); + vi.spyOn(overrides, "OPP_MOVESET_OVERRIDE", "get").mockReturnValue([ Moves.SPLASH, Moves.SPLASH, Moves.SPLASH, Moves.SPLASH ]); + vi.spyOn(overrides, "OPP_HELD_ITEMS_OVERRIDE", "get").mockReturnValue([ + {name: "BERRY", type: BerryType.SITRUS, count: 2}, + {name: "BERRY", type: BerryType.LUM, count: 2} + ]); + vi.spyOn(overrides, "STARTING_LEVEL_OVERRIDE", "get").mockReturnValue(100); + vi.spyOn(overrides, "OPP_LEVEL_OVERRIDE", "get").mockReturnValue(100); + + vi.spyOn(allMoves[Moves.POPULATION_BOMB], "accuracy", "get").mockReturnValue(100); + }); + + it( + "should only steal items from the attack target", + async () => { + await game.startBattle([Species.PANSEAR, Species.ROWLET, Species.PANPOUR, Species.PANSAGE, Species.CHARMANDER, Species.SQUIRTLE]); + + const playerPokemon = game.scene.getPlayerField(); + playerPokemon.forEach(p => expect(p).toBeDefined()); + + const enemyPokemon = game.scene.getEnemyField(); + enemyPokemon.forEach(p => expect(p).toBeDefined()); + + const enemyHeldItemCt = enemyPokemon.map(p => p.getHeldItems.length); + + game.doAttack(getMovePosition(game.scene, 0, Moves.POPULATION_BOMB)); + + await game.phaseInterceptor.to(SelectTargetPhase, false); + game.doSelectTarget(BattlerIndex.ENEMY); + + await game.phaseInterceptor.to(CommandPhase, false); + game.doAttack(getMovePosition(game.scene, 1, Moves.SPLASH)); + + await game.phaseInterceptor.to(MoveEndPhase, false); + + expect(enemyPokemon[1].getHeldItems.length).toBe(enemyHeldItemCt[1]); + }, TIMEOUT + ); +});