Add proper type checking to RedirectMoveAbAttr

This commit is contained in:
Dean 2025-03-01 01:26:52 -08:00
parent 7f226df8d6
commit 85780e1472
2 changed files with 14 additions and 5 deletions

View File

@ -4571,9 +4571,18 @@ export class PostFaintHPDamageAbAttr extends PostFaintAbAttr {
} }
} }
/**
* Redirects a move to the pokemon with this ability if it meets the conditions
*
* @param pokemon - The Pokemon with the redirection ability
* @param args - The args passed to the `AbAttr`:
* - `[0]` - The id of the {@linkcode Move} used
* - `[1]` - The target's battler index (before redirection)
* - `[2]` - The Pokemon that used the move being redirected
*/
export class RedirectMoveAbAttr extends AbAttr { export class RedirectMoveAbAttr extends AbAttr {
apply(pokemon: Pokemon, passive: boolean, simulated: boolean, cancelled: Utils.BooleanHolder, args: any[]): boolean { apply(pokemon: Pokemon, passive: boolean, simulated: boolean, cancelled: Utils.BooleanHolder, args: any[]): boolean {
if (this.canRedirect(args[0] as Moves)) { if (this.canRedirect(args[0] as Moves, args[2] as Pokemon)) {
const target = args[1] as Utils.NumberHolder; const target = args[1] as Utils.NumberHolder;
const newTarget = pokemon.getBattlerIndex(); const newTarget = pokemon.getBattlerIndex();
if (target.value !== newTarget) { if (target.value !== newTarget) {
@ -4585,7 +4594,7 @@ export class RedirectMoveAbAttr extends AbAttr {
return false; return false;
} }
canRedirect(moveId: Moves): boolean { canRedirect(moveId: Moves, user: Pokemon): boolean {
const move = allMoves[moveId]; const move = allMoves[moveId];
return !![ MoveTarget.NEAR_OTHER, MoveTarget.OTHER ].find(t => move.moveTarget === t); return !![ MoveTarget.NEAR_OTHER, MoveTarget.OTHER ].find(t => move.moveTarget === t);
} }
@ -4599,8 +4608,8 @@ export class RedirectTypeMoveAbAttr extends RedirectMoveAbAttr {
this.type = type; this.type = type;
} }
canRedirect(moveId: Moves): boolean { canRedirect(moveId: Moves, user: Pokemon): boolean {
return super.canRedirect(moveId) && allMoves[moveId].type === this.type; return super.canRedirect(moveId, user) && user.getMoveType(allMoves[moveId]) === this.type;
} }
} }

View File

@ -504,7 +504,7 @@ export class MovePhase extends BattlePhase {
globalScene globalScene
.getField(true) .getField(true)
.filter(p => p !== this.pokemon) .filter(p => p !== this.pokemon)
.forEach(p => applyAbAttrs(RedirectMoveAbAttr, p, null, false, this.move.moveId, redirectTarget)); .forEach(p => applyAbAttrs(RedirectMoveAbAttr, p, null, false, this.move.moveId, redirectTarget, this.pokemon));
/** `true` if an Ability is responsible for redirecting the move to another target; `false` otherwise */ /** `true` if an Ability is responsible for redirecting the move to another target; `false` otherwise */
let redirectedByAbility = currentTarget !== redirectTarget.value; let redirectedByAbility = currentTarget !== redirectTarget.value;