diff --git a/sim/battle-actions.ts b/sim/battle-actions.ts index 9df9dceb52..5a16ebbc97 100644 --- a/sim/battle-actions.ts +++ b/sim/battle-actions.ts @@ -180,7 +180,7 @@ export class BattleActions { } const allActive = this.battle.getAllActive(true); this.battle.speedSort(allActive); - this.battle.speedOrder = allActive.map(a => a.side.n * a.battle.sides.length + a.position); + this.battle.speedOrder = allActive.map(a => a.getFieldPositionValue()); this.battle.fieldEvent('SwitchIn', switchersIn); for (const poke of switchersIn) { diff --git a/sim/battle.ts b/sim/battle.ts index f06c223980..81617c5bb6 100644 --- a/sim/battle.ts +++ b/sim/battle.ts @@ -1009,8 +1009,7 @@ export class Battle { // Pokemon speeds including ties are resolved before all onSwitchIn handlers and aren't re-sorted in-between // so we subtract a fractional speed from each Pokemon's respective event handlers by using the index of their // unique field position in a pre-sorted-by-speed array - const fieldPositionValue = pokemon.side.n * this.sides.length + pokemon.position; - handler.speed -= this.speedOrder.indexOf(fieldPositionValue) / (this.activePerHalf * 2); + handler.speed -= this.speedOrder.indexOf(pokemon.getFieldPositionValue()) / (this.activePerHalf * 2); } } return handler; diff --git a/sim/pokemon.ts b/sim/pokemon.ts index 6ff69ec153..cda757180f 100644 --- a/sim/pokemon.ts +++ b/sim/pokemon.ts @@ -518,6 +518,11 @@ export class Pokemon { return (this.side.id + positionLetter) as PokemonSlot; } + getFieldPositionValue() { + // p1a, p2a, p3a, ..., p1b, p2b, p3b, ... + return this.side.n + this.battle.sides.length * this.position; + } + toString() { const fullname = (this.illusion) ? this.illusion.fullname : this.fullname; return this.isActive ? this.getSlot() + fullname.slice(2) : fullname;