Skip to content

Commit

Permalink
Keep speed tie resolution consistent across the SwitchIn event
Browse files Browse the repository at this point in the history
  • Loading branch information
pyuk-bot committed Dec 27, 2024
1 parent 0d1401a commit e0bdb41
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion sim/battle-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,9 @@ export class BattleActions {
const nextSwitch = this.battle.queue.shift();
switchersIn.push(nextSwitch!.pokemon!);
}
this.battle.prng.shuffle(this.battle.speedTieResolution);
this.battle.fieldEvent('SwitchIn', switchersIn);

if (!pokemon.hp) return false;
pokemon.isStarted = true;
pokemon.draggedIn = null;
Expand Down
13 changes: 13 additions & 0 deletions sim/battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export class Battle {
lastDamage: number;
effectOrder: number;
quickClawRoll: boolean;
speedTieResolution: number[];

teamGenerator: ReturnType<typeof Teams.getGenerator> | null;

Expand Down Expand Up @@ -261,6 +262,10 @@ export class Battle {
this.lastDamage = 0;
this.effectOrder = 0;
this.quickClawRoll = false;
this.speedTieResolution = [];
for (let i = 0; i < this.activePerHalf * 2; i++) {
this.speedTieResolution.push(i / (this.activePerHalf * 2));
}

this.teamGenerator = null;

Expand Down Expand Up @@ -950,6 +955,14 @@ export class Battle {
}
if (handler.effectHolder && (handler.effectHolder as Pokemon).getStat) {
handler.speed = (handler.effectHolder as Pokemon).speed;
if (callbackName.endsWith('SwitchIn')) {
// Pokemon speeds including ties are resolved before all onSwitchIn handlers and aren't re-sorted in-between
// so we add a fractional speed to each Pokemon's respective event handlers by using their unique field position
// to index a randomly shuffled array of sequential numbers
const allSlots = 'abcdef';
const speedTieIndex = allSlots.indexOf((handler.effectHolder as Pokemon).getSlot().charAt(2));
handler.speed += this.speedTieResolution[speedTieIndex];
}
}
return handler;
}
Expand Down
2 changes: 1 addition & 1 deletion test/sim/misc/multi-battle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Free-for-all', function () {
battle.makeChoices();
battle.lose('p2');
assert(battle.p2.activeRequest.wait);
battle.makeChoices('auto', '', 'move uturn', 'auto');
battle.makeChoices('auto', '', 'move uturn 1', 'auto');
battle.lose('p3');
battle.makeChoices();
assert.equal(battle.turn, 4);
Expand Down
6 changes: 3 additions & 3 deletions test/sim/misc/terastal.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ describe("Terastallization", function () {

it('should give STAB correctly to the user\'s old types', function () {
battle = common.createBattle([[
{species: 'Ampharos', ability: 'static', moves: ['shockwave', 'swift'], teraType: 'Electric'},
{species: 'Ampharos', ability: 'shellarmor', moves: ['shockwave', 'swift'], teraType: 'Electric'},
], [
{species: 'Ampharos', ability: 'static', moves: ['shockwave', 'swift'], teraType: 'Normal'},
{species: 'Ampharos', ability: 'shellarmor', moves: ['shockwave', 'swift'], teraType: 'Normal'},
]]);
battle.makeChoices('move shockwave terastallize', 'move shockwave terastallize');
const teraDamage = battle.p2.active[0].maxhp - battle.p2.active[0].hp;
Expand All @@ -47,7 +47,7 @@ describe("Terastallization", function () {
const nonTeraDamage = battle.p1.active[0].maxhp - battle.p1.active[0].hp;
// 0 SpA Ampharos Shock Wave vs. 0 HP / 0 SpD Ampharos: 40-48
assert.bounded(nonTeraDamage, [40, 48],
"Terastallizing did not keep old type's STAB; actual damage: " + teraDamage);
"Terastallizing did not keep old type's STAB; actual damage: " + nonTeraDamage);

battle = common.createBattle([[
{species: 'Mimikyu', ability: 'disguise', item: 'laggingtail', moves: ['shadowclaw', 'waterfall', 'sleeptalk'], teraType: 'Water'},
Expand Down

0 comments on commit e0bdb41

Please sign in to comment.