From 55e0d65ac8055b9b3fb6748289ae44796956b32c Mon Sep 17 00:00:00 2001 From: Mumble <171087428+frutescens@users.noreply.github.com> Date: Sun, 1 Sep 2024 15:23:25 -0700 Subject: [PATCH] [Refactor] Improvements on getOrder() (#3547) * Moved getOrder() into TurnStartPhase * Cleaned up bypass speed checks * Revert "Cleaned up bypass speed checks" This reverts commit 11150254f568b8575211693919c9c5deeb3d8dcd. * Added comments. * Fixed up some inconsistencies * changed isSameBracket check * changed isSameBracket check p2 * changed isSameBracket check p3 * changed isSameBracket check p3 * Fixed up conditionals + stall/M.m * Seems OK * Update battle-spec.ts * Updated tests to use new functions introduced. Less intuitive, but faster. * Update src/phases.ts Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> * Moved getOrder() into TurnStartPhase * Cleaned up bypass speed checks * Revert "Cleaned up bypass speed checks" This reverts commit 11150254f568b8575211693919c9c5deeb3d8dcd. * Added comments. * Fixed up some inconsistencies * changed isSameBracket check * changed isSameBracket check p2 * changed isSameBracket check p3 * changed isSameBracket check p3 * Fixed up conditionals + stall/M.m * Seems OK * Update battle-spec.ts * Updated tests to use new functions introduced. Less intuitive, but faster. * Removed import * i hate git * Moved getOrder() into TurnStartPhase * Seems OK * missing import * Added Snooze's review * Added test fixes and removed unwanted edit. * fixed dynamax cannon test * typedocs fixes * Updating battle-order.test.ts * merge fixes * ughhh * Update src/test/abilities/mycelium_might.test.ts Co-authored-by: innerthunder <168692175+innerthunder@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: innerthunder <168692175+innerthunder@users.noreply.github.com> * tsdocs :) * Fixed tests * Update src/phases/turn-start-phase.ts Co-authored-by: innerthunder <168692175+innerthunder@users.noreply.github.com> * Update src/phases/turn-start-phase.ts Co-authored-by: innerthunder <168692175+innerthunder@users.noreply.github.com> * innerthunder's fixes * commas * Mocked stats instead of directly changing them --------- Co-authored-by: Frutescens Co-authored-by: NightKev <34855794+DayKev@users.noreply.github.com> Co-authored-by: innerthunder <168692175+innerthunder@users.noreply.github.com> --- src/data/ability.ts | 4 +- src/phases/field-phase.ts | 35 +------ src/phases/turn-start-phase.ts | 110 ++++++++++++++++------ src/test/abilities/mycelium_might.test.ts | 51 +++++----- src/test/abilities/stall.test.ts | 48 +++++----- src/test/battle/battle-order.test.ts | 105 +++++++++++++-------- src/test/utils/gameManager.ts | 4 +- 7 files changed, 203 insertions(+), 154 deletions(-) diff --git a/src/data/ability.ts b/src/data/ability.ts index 8b7a7772efe..3d5b32f4ce8 100644 --- a/src/data/ability.ts +++ b/src/data/ability.ts @@ -5025,7 +5025,7 @@ export function initAbilities() { .attr(AlwaysHitAbAttr) .attr(DoubleBattleChanceAbAttr), new Ability(Abilities.STALL, 4) - .attr(ChangeMovePriorityAbAttr, (pokemon, move: Move) => true, -0.5), + .attr(ChangeMovePriorityAbAttr, (pokemon, move: Move) => true, -0.2), new Ability(Abilities.TECHNICIAN, 4) .attr(MovePowerBoostAbAttr, (user, target, move) => { const power = new Utils.NumberHolder(move.power); @@ -5713,7 +5713,7 @@ export function initAbilities() { .partial() // Healing not blocked by Heal Block .ignorable(), new Ability(Abilities.MYCELIUM_MIGHT, 9) - .attr(ChangeMovePriorityAbAttr, (pokemon, move) => move.category === MoveCategory.STATUS, -0.5) + .attr(ChangeMovePriorityAbAttr, (pokemon, move) => move.category === MoveCategory.STATUS, -0.2) .attr(PreventBypassSpeedChanceAbAttr, (pokemon, move) => move.category === MoveCategory.STATUS) .attr(MoveAbilityBypassAbAttr, (pokemon, move: Move) => move.category === MoveCategory.STATUS), new Ability(Abilities.MINDS_EYE, 9) diff --git a/src/phases/field-phase.ts b/src/phases/field-phase.ts index a9622271f14..02d1f1395d3 100644 --- a/src/phases/field-phase.ts +++ b/src/phases/field-phase.ts @@ -1,42 +1,9 @@ -import { BattlerIndex } from "#app/battle.js"; -import { TrickRoomTag } from "#app/data/arena-tag.js"; -import { Stat } from "#app/enums/stat.js"; -import Pokemon from "#app/field/pokemon.js"; import { BattlePhase } from "./battle-phase"; -import * as Utils from "#app/utils.js"; +import Pokemon from "#app/field/pokemon"; type PokemonFunc = (pokemon: Pokemon) => void; export abstract class FieldPhase extends BattlePhase { - getOrder(): BattlerIndex[] { - const playerField = this.scene.getPlayerField().filter(p => p.isActive()) as Pokemon[]; - const enemyField = this.scene.getEnemyField().filter(p => p.isActive()) as Pokemon[]; - - // We shuffle the list before sorting so speed ties produce random results - let orderedTargets: Pokemon[] = playerField.concat(enemyField); - // We seed it with the current turn to prevent an inconsistency where it - // was varying based on how long since you last reloaded - this.scene.executeWithSeedOffset(() => { - orderedTargets = Utils.randSeedShuffle(orderedTargets); - }, this.scene.currentBattle.turn, this.scene.waveSeed); - - orderedTargets.sort((a: Pokemon, b: Pokemon) => { - const aSpeed = a?.getBattleStat(Stat.SPD) || 0; - const bSpeed = b?.getBattleStat(Stat.SPD) || 0; - - return bSpeed - aSpeed; - }); - - const speedReversed = new Utils.BooleanHolder(false); - this.scene.arena.applyTags(TrickRoomTag, speedReversed); - - if (speedReversed.value) { - orderedTargets = orderedTargets.reverse(); - } - - return orderedTargets.map(t => t.getFieldIndex() + (!t.isPlayer() ? BattlerIndex.ENEMY : 0)); - } - executeForAll(func: PokemonFunc): void { const field = this.scene.getField(true).filter(p => p.summonData); field.forEach(pokemon => func(pokemon)); diff --git a/src/phases/turn-start-phase.ts b/src/phases/turn-start-phase.ts index e4064fc784a..b2545e9ee30 100644 --- a/src/phases/turn-start-phase.ts +++ b/src/phases/turn-start-phase.ts @@ -1,12 +1,12 @@ -import BattleScene from "#app/battle-scene.js"; -import { applyAbAttrs, BypassSpeedChanceAbAttr, PreventBypassSpeedChanceAbAttr, ChangeMovePriorityAbAttr } from "#app/data/ability.js"; -import { allMoves, applyMoveAttrs, IncrementMovePriorityAttr, MoveHeaderAttr } from "#app/data/move.js"; -import { Abilities } from "#app/enums/abilities.js"; -import { Stat } from "#app/enums/stat.js"; -import { PokemonMove } from "#app/field/pokemon.js"; -import { BypassSpeedChanceModifier } from "#app/modifier/modifier.js"; -import { Command } from "#app/ui/command-ui-handler.js"; -import * as Utils from "#app/utils.js"; +import BattleScene from "#app/battle-scene"; +import { applyAbAttrs, BypassSpeedChanceAbAttr, PreventBypassSpeedChanceAbAttr, ChangeMovePriorityAbAttr } from "#app/data/ability"; +import { allMoves, applyMoveAttrs, IncrementMovePriorityAttr, MoveHeaderAttr } from "#app/data/move"; +import { Abilities } from "#app/enums/abilities"; +import { Stat } from "#app/enums/stat"; +import Pokemon, { PokemonMove } from "#app/field/pokemon"; +import { BypassSpeedChanceModifier } from "#app/modifier/modifier"; +import { Command } from "#app/ui/command-ui-handler"; +import * as Utils from "#app/utils"; import { AttemptCapturePhase } from "./attempt-capture-phase"; import { AttemptRunPhase } from "./attempt-run-phase"; import { BerryPhase } from "./berry-phase"; @@ -17,18 +17,59 @@ import { PostTurnStatusEffectPhase } from "./post-turn-status-effect-phase"; import { SwitchSummonPhase } from "./switch-summon-phase"; import { TurnEndPhase } from "./turn-end-phase"; import { WeatherEffectPhase } from "./weather-effect-phase"; +import { BattlerIndex } from "#app/battle"; +import { TrickRoomTag } from "#app/data/arena-tag"; export class TurnStartPhase extends FieldPhase { constructor(scene: BattleScene) { super(scene); } - start() { - super.start(); + /** + * This orders the active Pokemon on the field by speed into an BattlerIndex array and returns that array. + * It also checks for Trick Room and reverses the array if it is present. + * @returns {@linkcode BattlerIndex[]} the battle indices of all pokemon on the field ordered by speed + */ + getSpeedOrder(): BattlerIndex[] { + const playerField = this.scene.getPlayerField().filter(p => p.isActive()) as Pokemon[]; + const enemyField = this.scene.getEnemyField().filter(p => p.isActive()) as Pokemon[]; - const field = this.scene.getField(); - const order = this.getOrder(); + // We shuffle the list before sorting so speed ties produce random results + let orderedTargets: Pokemon[] = playerField.concat(enemyField); + // We seed it with the current turn to prevent an inconsistency where it + // was varying based on how long since you last reloaded + this.scene.executeWithSeedOffset(() => { + orderedTargets = Utils.randSeedShuffle(orderedTargets); + }, this.scene.currentBattle.turn, this.scene.waveSeed); + orderedTargets.sort((a: Pokemon, b: Pokemon) => { + const aSpeed = a?.getBattleStat(Stat.SPD) || 0; + const bSpeed = b?.getBattleStat(Stat.SPD) || 0; + + return bSpeed - aSpeed; + }); + + // Next, a check for Trick Room is applied. If Trick Room is present, the order is reversed. + const speedReversed = new Utils.BooleanHolder(false); + this.scene.arena.applyTags(TrickRoomTag, speedReversed); + + if (speedReversed.value) { + orderedTargets = orderedTargets.reverse(); + } + + return orderedTargets.map(t => t.getFieldIndex() + (!t.isPlayer() ? BattlerIndex.ENEMY : BattlerIndex.PLAYER)); + } + + /** + * This takes the result of getSpeedOrder and applies priority / bypass speed attributes to it. + * This also considers the priority levels of various commands and changes the result of getSpeedOrder based on such. + * @returns {@linkcode BattlerIndex[]} the final sequence of commands for this turn + */ + getCommandOrder(): BattlerIndex[] { + let moveOrder = this.getSpeedOrder(); + // The creation of the battlerBypassSpeed object contains checks for the ability Quick Draw and the held item Quick Claw + // The ability Mycelium Might disables Quick Claw's activation when using a status move + // This occurs before the main loop because of battles with more than two Pokemon const battlerBypassSpeed = {}; this.scene.getField(true).filter(p => p.summonData).map(p => { @@ -42,8 +83,9 @@ export class TurnStartPhase extends FieldPhase { battlerBypassSpeed[p.getBattlerIndex()] = bypassSpeed; }); - const moveOrder = order.slice(0); - + // The function begins sorting orderedTargets based on command priority, move priority, and possible speed bypasses. + // Non-FIGHT commands (SWITCH, BALL, RUN) have a higher command priority and will always occur before any FIGHT commands. + moveOrder = moveOrder.slice(0); moveOrder.sort((a, b) => { const aCommand = this.scene.currentBattle.turnCommands[a]; const bCommand = this.scene.currentBattle.turnCommands[b]; @@ -55,37 +97,50 @@ export class TurnStartPhase extends FieldPhase { return -1; } } else if (aCommand?.command === Command.FIGHT) { - const aMove = allMoves[aCommand.move!.move];//TODO: is the bang correct here? - const bMove = allMoves[bCommand!.move!.move];//TODO: is the bang correct here? + const aMove = allMoves[aCommand.move!.move]; + const bMove = allMoves[bCommand!.move!.move]; + // The game now considers priority and applies the relevant move and ability attributes const aPriority = new Utils.IntegerHolder(aMove.priority); const bPriority = new Utils.IntegerHolder(bMove.priority); - applyMoveAttrs(IncrementMovePriorityAttr, this.scene.getField().find(p => p?.isActive() && p.getBattlerIndex() === a)!, null, aMove, aPriority); //TODO: is the bang correct here? - applyMoveAttrs(IncrementMovePriorityAttr, this.scene.getField().find(p => p?.isActive() && p.getBattlerIndex() === b)!, null, bMove, bPriority); //TODO: is the bang correct here? + applyMoveAttrs(IncrementMovePriorityAttr, this.scene.getField().find(p => p?.isActive() && p.getBattlerIndex() === a)!, null, aMove, aPriority); + applyMoveAttrs(IncrementMovePriorityAttr, this.scene.getField().find(p => p?.isActive() && p.getBattlerIndex() === b)!, null, bMove, bPriority); - applyAbAttrs(ChangeMovePriorityAbAttr, this.scene.getField().find(p => p?.isActive() && p.getBattlerIndex() === a)!, null, false, aMove, aPriority); //TODO: is the bang correct here? - applyAbAttrs(ChangeMovePriorityAbAttr, this.scene.getField().find(p => p?.isActive() && p.getBattlerIndex() === b)!, null, false, bMove, bPriority); //TODO: is the bang correct here? + applyAbAttrs(ChangeMovePriorityAbAttr, this.scene.getField().find(p => p?.isActive() && p.getBattlerIndex() === a)!, null, false, aMove, aPriority); + applyAbAttrs(ChangeMovePriorityAbAttr, this.scene.getField().find(p => p?.isActive() && p.getBattlerIndex() === b)!, null, false, bMove, bPriority); + // The game now checks for differences in priority levels. + // If the moves share the same original priority bracket, it can check for differences in battlerBypassSpeed and return the result. + // This conditional is used to ensure that Quick Claw can still activate with abilities like Stall and Mycelium Might (attack moves only) + // Otherwise, the game returns the user of the move with the highest priority. + const isSameBracket = Math.ceil(aPriority.value) - Math.ceil(bPriority.value) === 0; if (aPriority.value !== bPriority.value) { - const bracketDifference = Math.ceil(aPriority.value) - Math.ceil(bPriority.value); - const hasSpeedDifference = battlerBypassSpeed[a].value !== battlerBypassSpeed[b].value; - if (bracketDifference === 0 && hasSpeedDifference) { + if (isSameBracket && battlerBypassSpeed[a].value !== battlerBypassSpeed[b].value) { return battlerBypassSpeed[a].value ? -1 : 1; } return aPriority.value < bPriority.value ? 1 : -1; } } + // If there is no difference between the move's calculated priorities, the game checks for differences in battlerBypassSpeed and returns the result. if (battlerBypassSpeed[a].value !== battlerBypassSpeed[b].value) { return battlerBypassSpeed[a].value ? -1 : 1; } - const aIndex = order.indexOf(a); - const bIndex = order.indexOf(b); + const aIndex = moveOrder.indexOf(a); + const bIndex = moveOrder.indexOf(b); return aIndex < bIndex ? -1 : aIndex > bIndex ? 1 : 0; }); + return moveOrder; + } + + start() { + super.start(); + + const field = this.scene.getField(); + const moveOrder = this.getCommandOrder(); let orderIndex = 0; @@ -150,10 +205,9 @@ export class TurnStartPhase extends FieldPhase { } } - this.scene.pushPhase(new WeatherEffectPhase(this.scene)); - for (const o of order) { + for (const o of moveOrder) { if (field[o].status && field[o].status.isPostTurn()) { this.scene.pushPhase(new PostTurnStatusEffectPhase(this.scene, o)); } diff --git a/src/test/abilities/mycelium_might.test.ts b/src/test/abilities/mycelium_might.test.ts index 83396f7950f..d5bea185f59 100644 --- a/src/test/abilities/mycelium_might.test.ts +++ b/src/test/abilities/mycelium_might.test.ts @@ -1,6 +1,6 @@ import { BattleStat } from "#app/data/battle-stat"; -import { MovePhase } from "#app/phases/move-phase"; import { TurnEndPhase } from "#app/phases/turn-end-phase"; +import { TurnStartPhase } from "#app/phases/turn-start-phase"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; import { Species } from "#enums/species"; @@ -8,7 +8,6 @@ import GameManager from "#test/utils/gameManager"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; - describe("Abilities - Mycelium Might", () => { let phaserGame: Phaser.Game; let game: GameManager; @@ -35,7 +34,7 @@ describe("Abilities - Mycelium Might", () => { }); /** - * Bulbapedia References: + * References: * https://bulbapedia.bulbagarden.net/wiki/Mycelium_Might_(Ability) * https://bulbapedia.bulbagarden.net/wiki/Priority * https://www.smogon.com/forums/threads/scarlet-violet-battle-mechanics-research.3709545/page-24 @@ -44,22 +43,22 @@ describe("Abilities - Mycelium Might", () => { it("will move last in its priority bracket and ignore protective abilities", async () => { await game.startBattle([Species.REGIELEKI]); - const leadIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); const enemyPokemon = game.scene.getEnemyPokemon(); + const playerIndex = game.scene.getPlayerPokemon()?.getBattlerIndex(); const enemyIndex = enemyPokemon?.getBattlerIndex(); game.move.select(Moves.BABY_DOLL_EYES); - await game.phaseInterceptor.to(MovePhase, false); + await game.phaseInterceptor.to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; + const speedOrder = phase.getSpeedOrder(); + const commandOrder = phase.getCommandOrder(); // The opponent Pokemon (without Mycelium Might) goes first despite having lower speed than the player Pokemon. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(enemyIndex); - - await game.phaseInterceptor.run(MovePhase); - await game.phaseInterceptor.to(MovePhase, false); - // The player Pokemon (with Mycelium Might) goes last despite having higher speed than the opponent. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(leadIndex); + expect(speedOrder).toEqual([playerIndex, enemyIndex]); + expect(commandOrder).toEqual([enemyIndex, playerIndex]); await game.phaseInterceptor.to(TurnEndPhase); + // Despite the opponent's ability (Clear Body), its attack stat is still reduced. expect(enemyPokemon?.summonData.battleStats[BattleStat.ATK]).toBe(-1); }, 20000); @@ -67,39 +66,41 @@ describe("Abilities - Mycelium Might", () => { game.override.enemyMoveset([Moves.TACKLE, Moves.TACKLE, Moves.TACKLE, Moves.TACKLE]); await game.startBattle([Species.REGIELEKI]); - const leadIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); const enemyPokemon = game.scene.getEnemyPokemon(); + const playerIndex = game.scene.getPlayerPokemon()?.getBattlerIndex(); const enemyIndex = enemyPokemon?.getBattlerIndex(); game.move.select(Moves.BABY_DOLL_EYES); - await game.phaseInterceptor.to(MovePhase, false); + await game.phaseInterceptor.to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; + const speedOrder = phase.getSpeedOrder(); + const commandOrder = phase.getCommandOrder(); // The player Pokemon (with M.M.) goes first because its move is still within a higher priority bracket than its opponent. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(leadIndex); - - await game.phaseInterceptor.run(MovePhase); - await game.phaseInterceptor.to(MovePhase, false); // The enemy Pokemon goes second because its move is in a lower priority bracket. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(enemyIndex); + expect(speedOrder).toEqual([playerIndex, enemyIndex]); + expect(commandOrder).toEqual([playerIndex, enemyIndex]); await game.phaseInterceptor.to(TurnEndPhase); + // Despite the opponent's ability (Clear Body), its attack stat is still reduced. expect(enemyPokemon?.summonData.battleStats[BattleStat.ATK]).toBe(-1); }, 20000); it("will not affect non-status moves", async () => { await game.startBattle([Species.REGIELEKI]); - const leadIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); + const playerIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); const enemyIndex = game.scene.getEnemyPokemon()!.getBattlerIndex(); game.move.select(Moves.QUICK_ATTACK); - await game.phaseInterceptor.to(MovePhase, false); + await game.phaseInterceptor.to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; + const speedOrder = phase.getSpeedOrder(); + const commandOrder = phase.getCommandOrder(); // The player Pokemon (with M.M.) goes first because it has a higher speed and did not use a status move. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(leadIndex); - - await game.phaseInterceptor.run(MovePhase); - await game.phaseInterceptor.to(MovePhase, false); // The enemy Pokemon (without M.M.) goes second because its speed is lower. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(enemyIndex); + // This means that the commandOrder should be identical to the speedOrder + expect(speedOrder).toEqual([playerIndex, enemyIndex]); + expect(commandOrder).toEqual([playerIndex, enemyIndex]); }, 20000); }); diff --git a/src/test/abilities/stall.test.ts b/src/test/abilities/stall.test.ts index d8dbe9d0e06..7baf7c846f0 100644 --- a/src/test/abilities/stall.test.ts +++ b/src/test/abilities/stall.test.ts @@ -1,11 +1,10 @@ -import { MovePhase } from "#app/phases/move-phase"; import { Abilities } from "#enums/abilities"; import { Moves } from "#enums/moves"; import { Species } from "#enums/species"; import GameManager from "#test/utils/gameManager"; import Phaser from "phaser"; import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; - +import { TurnStartPhase } from "#app/phases/turn-start-phase"; describe("Abilities - Stall", () => { let phaserGame: Phaser.Game; @@ -32,7 +31,7 @@ describe("Abilities - Stall", () => { }); /** - * Bulbapedia References: + * References: * https://bulbapedia.bulbagarden.net/wiki/Stall_(Ability) * https://bulbapedia.bulbagarden.net/wiki/Priority **/ @@ -40,55 +39,56 @@ describe("Abilities - Stall", () => { it("Pokemon with Stall should move last in its priority bracket regardless of speed", async () => { await game.startBattle([Species.SHUCKLE]); - const leadIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); + const playerIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); const enemyIndex = game.scene.getEnemyPokemon()!.getBattlerIndex(); game.move.select(Moves.QUICK_ATTACK); - await game.phaseInterceptor.to(MovePhase, false); + await game.phaseInterceptor.to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; + const speedOrder = phase.getSpeedOrder(); + const commandOrder = phase.getCommandOrder(); // The player Pokemon (without Stall) goes first despite having lower speed than the opponent. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(leadIndex); - - await game.phaseInterceptor.run(MovePhase); - await game.phaseInterceptor.to(MovePhase, false); // The opponent Pokemon (with Stall) goes last despite having higher speed than the player Pokemon. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(enemyIndex); + expect(speedOrder).toEqual([enemyIndex, playerIndex]); + expect(commandOrder).toEqual([playerIndex, enemyIndex]); }, 20000); it("Pokemon with Stall will go first if a move that is in a higher priority bracket than the opponent's move is used", async () => { await game.startBattle([Species.SHUCKLE]); - const leadIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); + const playerIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); const enemyIndex = game.scene.getEnemyPokemon()!.getBattlerIndex(); game.move.select(Moves.TACKLE); - await game.phaseInterceptor.to(MovePhase, false); + await game.phaseInterceptor.to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; + const speedOrder = phase.getSpeedOrder(); + const commandOrder = phase.getCommandOrder(); // The opponent Pokemon (with Stall) goes first because its move is still within a higher priority bracket than its opponent. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(enemyIndex); - - await game.phaseInterceptor.run(MovePhase); - await game.phaseInterceptor.to(MovePhase, false); // The player Pokemon goes second because its move is in a lower priority bracket. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(leadIndex); + expect(speedOrder).toEqual([enemyIndex, playerIndex]); + expect(commandOrder).toEqual([enemyIndex, playerIndex]); }, 20000); it("If both Pokemon have stall and use the same move, speed is used to determine who goes first.", async () => { game.override.ability(Abilities.STALL); await game.startBattle([Species.SHUCKLE]); - const leadIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); + const playerIndex = game.scene.getPlayerPokemon()!.getBattlerIndex(); const enemyIndex = game.scene.getEnemyPokemon()!.getBattlerIndex(); game.move.select(Moves.TACKLE); - await game.phaseInterceptor.to(MovePhase, false); - // The opponent Pokemon (with Stall) goes first because it has a higher speed. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(enemyIndex); + await game.phaseInterceptor.to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; + const speedOrder = phase.getSpeedOrder(); + const commandOrder = phase.getCommandOrder(); - await game.phaseInterceptor.run(MovePhase); - await game.phaseInterceptor.to(MovePhase, false); + // The opponent Pokemon (with Stall) goes first because it has a higher speed. // The player Pokemon (with Stall) goes second because its speed is lower. - expect((game.scene.getCurrentPhase() as MovePhase).pokemon.getBattlerIndex()).toBe(leadIndex); + expect(speedOrder).toEqual([enemyIndex, playerIndex]); + expect(commandOrder).toEqual([enemyIndex, playerIndex]); }, 20000); }); diff --git a/src/test/battle/battle-order.test.ts b/src/test/battle/battle-order.test.ts index 0129ecad254..e19168962dc 100644 --- a/src/test/battle/battle-order.test.ts +++ b/src/test/battle/battle-order.test.ts @@ -1,4 +1,3 @@ -import { Stat } from "#app/data/pokemon-stat"; import { EnemyCommandPhase } from "#app/phases/enemy-command-phase"; import { SelectTargetPhase } from "#app/phases/select-target-phase"; import { TurnStartPhase } from "#app/phases/turn-start-phase"; @@ -7,8 +6,7 @@ import { Moves } from "#enums/moves"; import { Species } from "#enums/species"; import GameManager from "#test/utils/gameManager"; import Phaser from "phaser"; -import { afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; - +import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; describe("Battle order", () => { let phaserGame: Phaser.Game; @@ -37,30 +35,42 @@ describe("Battle order", () => { await game.startBattle([ Species.BULBASAUR, ]); - game.scene.getParty()[0].stats[Stat.SPD] = 50; - game.scene.currentBattle.enemyParty[0].stats[Stat.SPD] = 150; + + const playerPokemon = game.scene.getPlayerPokemon()!; + const enemyPokemon = game.scene.getEnemyPokemon()!; + vi.spyOn(playerPokemon, "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 50]); // set playerPokemon's speed to 50 + vi.spyOn(enemyPokemon, "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 150]); // set enemyPokemon's speed to 150 game.move.select(Moves.TACKLE); await game.phaseInterceptor.run(EnemyCommandPhase); + + const playerPokemonIndex = playerPokemon.getBattlerIndex(); + const enemyPokemonIndex = enemyPokemon.getBattlerIndex(); const phase = game.scene.getCurrentPhase() as TurnStartPhase; - const order = phase.getOrder(); - expect(order[0]).toBe(2); - expect(order[1]).toBe(0); + const order = phase.getCommandOrder(); + expect(order[0]).toBe(enemyPokemonIndex); + expect(order[1]).toBe(playerPokemonIndex); }, 20000); it("Player faster than opponent 150 vs 50", async () => { await game.startBattle([ Species.BULBASAUR, ]); - game.scene.getParty()[0].stats[Stat.SPD] = 150; - game.scene.currentBattle.enemyParty[0].stats[Stat.SPD] = 50; + + const playerPokemon = game.scene.getPlayerPokemon()!; + const enemyPokemon = game.scene.getEnemyPokemon()!; + vi.spyOn(playerPokemon, "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 150]); // set playerPokemon's speed to 150 + vi.spyOn(enemyPokemon, "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 50]); // set enemyPokemon's speed to 50 game.move.select(Moves.TACKLE); await game.phaseInterceptor.run(EnemyCommandPhase); + + const playerPokemonIndex = playerPokemon.getBattlerIndex(); + const enemyPokemonIndex = enemyPokemon.getBattlerIndex(); const phase = game.scene.getCurrentPhase() as TurnStartPhase; - const order = phase.getOrder(); - expect(order[0]).toBe(0); - expect(order[1]).toBe(2); + const order = phase.getCommandOrder(); + expect(order[0]).toBe(playerPokemonIndex); + expect(order[1]).toBe(enemyPokemonIndex); }, 20000); it("double - both opponents faster than player 50/50 vs 150/150", async () => { @@ -69,20 +79,25 @@ describe("Battle order", () => { Species.BULBASAUR, Species.BLASTOISE, ]); - game.scene.getParty()[0].stats[Stat.SPD] = 50; - game.scene.getParty()[1].stats[Stat.SPD] = 50; - game.scene.currentBattle.enemyParty[0].stats[Stat.SPD] = 150; - game.scene.currentBattle.enemyParty[1].stats[Stat.SPD] = 150; + + const playerPokemon = game.scene.getPlayerField(); + const enemyPokemon = game.scene.getEnemyField(); + + playerPokemon.forEach(p => vi.spyOn(p, "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 50])); // set both playerPokemons' speed to 50 + enemyPokemon.forEach(p => vi.spyOn(p, "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 150])); // set both enemyPokemons' speed to 150 + const playerIndices = playerPokemon.map(p => p?.getBattlerIndex()); + const enemyIndices = enemyPokemon.map(p => p?.getBattlerIndex()); game.move.select(Moves.TACKLE); game.move.select(Moves.TACKLE, 1); await game.phaseInterceptor.runFrom(SelectTargetPhase).to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; - const order = phase.getOrder(); - expect(order.indexOf(0)).toBeGreaterThan(order.indexOf(2)); - expect(order.indexOf(0)).toBeGreaterThan(order.indexOf(3)); - expect(order.indexOf(1)).toBeGreaterThan(order.indexOf(2)); - expect(order.indexOf(1)).toBeGreaterThan(order.indexOf(3)); + const order = phase.getCommandOrder(); + expect(order.slice(0, 2).includes(enemyIndices[0])).toBe(true); + expect(order.slice(0, 2).includes(enemyIndices[1])).toBe(true); + expect(order.slice(2, 4).includes(playerIndices[0])).toBe(true); + expect(order.slice(2, 4).includes(playerIndices[1])).toBe(true); }, 20000); it("double - speed tie except 1 - 100/100 vs 100/150", async () => { @@ -91,19 +106,25 @@ describe("Battle order", () => { Species.BULBASAUR, Species.BLASTOISE, ]); - game.scene.getParty()[0].stats[Stat.SPD] = 100; - game.scene.getParty()[1].stats[Stat.SPD] = 100; - game.scene.currentBattle.enemyParty[0].stats[Stat.SPD] = 100; - game.scene.currentBattle.enemyParty[1].stats[Stat.SPD] = 150; + + const playerPokemon = game.scene.getPlayerField(); + const enemyPokemon = game.scene.getEnemyField(); + playerPokemon.forEach(p => vi.spyOn(p, "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 100])); //set both playerPokemons' speed to 100 + vi.spyOn(enemyPokemon[0], "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 100]); // set enemyPokemon's speed to 100 + vi.spyOn(enemyPokemon[1], "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 150]); // set enemyPokemon's speed to 150 + const playerIndices = playerPokemon.map(p => p?.getBattlerIndex()); + const enemyIndices = enemyPokemon.map(p => p?.getBattlerIndex()); game.move.select(Moves.TACKLE); game.move.select(Moves.TACKLE, 1); await game.phaseInterceptor.runFrom(SelectTargetPhase).to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; - const order = phase.getOrder(); - expect(order.indexOf(3)).toBeLessThan(order.indexOf(0)); - expect(order.indexOf(3)).toBeLessThan(order.indexOf(1)); - expect(order.indexOf(3)).toBeLessThan(order.indexOf(2)); + const order = phase.getCommandOrder(); + expect(order[0]).toBe(enemyIndices[1]); + expect(order.slice(1, 4).includes(enemyIndices[0])).toBe(true); + expect(order.slice(1, 4).includes(playerIndices[0])).toBe(true); + expect(order.slice(1, 4).includes(playerIndices[1])).toBe(true); }, 20000); it("double - speed tie 100/150 vs 100/150", async () => { @@ -112,19 +133,25 @@ describe("Battle order", () => { Species.BULBASAUR, Species.BLASTOISE, ]); - game.scene.getParty()[0].stats[Stat.SPD] = 100; - game.scene.getParty()[1].stats[Stat.SPD] = 150; - game.scene.currentBattle.enemyParty[0].stats[Stat.SPD] = 100; - game.scene.currentBattle.enemyParty[1].stats[Stat.SPD] = 150; + + const playerPokemon = game.scene.getPlayerField(); + const enemyPokemon = game.scene.getEnemyField(); + vi.spyOn(playerPokemon[0], "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 100]); // set one playerPokemon's speed to 100 + vi.spyOn(playerPokemon[1], "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 150]); // set other playerPokemon's speed to 150 + vi.spyOn(enemyPokemon[0], "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 100]); // set one enemyPokemon's speed to 100 + vi.spyOn(enemyPokemon[1], "stats", "get").mockReturnValue([20, 20, 20, 20, 20, 150]); // set other enemyPokemon's speed to 150 + const playerIndices = playerPokemon.map(p => p?.getBattlerIndex()); + const enemyIndices = enemyPokemon.map(p => p?.getBattlerIndex()); game.move.select(Moves.TACKLE); game.move.select(Moves.TACKLE, 1); await game.phaseInterceptor.runFrom(SelectTargetPhase).to(TurnStartPhase, false); + const phase = game.scene.getCurrentPhase() as TurnStartPhase; - const order = phase.getOrder(); - expect(order.indexOf(1)).toBeLessThan(order.indexOf(0)); - expect(order.indexOf(1)).toBeLessThan(order.indexOf(2)); - expect(order.indexOf(3)).toBeLessThan(order.indexOf(0)); - expect(order.indexOf(3)).toBeLessThan(order.indexOf(2)); + const order = phase.getCommandOrder(); + expect(order.slice(0, 2).includes(playerIndices[1])).toBe(true); + expect(order.slice(0, 2).includes(enemyIndices[1])).toBe(true); + expect(order.slice(2, 4).includes(playerIndices[0])).toBe(true); + expect(order.slice(2, 4).includes(enemyIndices[0])).toBe(true); }, 20000); }); diff --git a/src/test/utils/gameManager.ts b/src/test/utils/gameManager.ts index 60d07065090..5818244db8f 100644 --- a/src/test/utils/gameManager.ts +++ b/src/test/utils/gameManager.ts @@ -381,7 +381,7 @@ export default class GameManager { } /** - * Intercepts `TurnStartPhase` and mocks the getOrder's return value {@linkcode TurnStartPhase.getOrder} + * Intercepts `TurnStartPhase` and mocks the getSpeedOrder's return value {@linkcode TurnStartPhase.getSpeedOrder} * Used to modify the turn order. * @param {BattlerIndex[]} order The turn order to set * @example @@ -392,7 +392,7 @@ export default class GameManager { async setTurnOrder(order: BattlerIndex[]): Promise { await this.phaseInterceptor.to(TurnStartPhase, false); - vi.spyOn(this.scene.getCurrentPhase() as TurnStartPhase, "getOrder").mockReturnValue(order); + vi.spyOn(this.scene.getCurrentPhase() as TurnStartPhase, "getSpeedOrder").mockReturnValue(order); } /**