Skip to content

Commit

Permalink
Fix unintended changes to speed ties in other events
Browse files Browse the repository at this point in the history
  • Loading branch information
pyuk-bot committed Jan 3, 2025
1 parent ea80da6 commit 3b5aa62
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 9 additions & 1 deletion sim/battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ interface EventListener extends EventListenerWithoutPriority {
order: number | false;
priority: number;
subOrder: number;
effectOrder?: number;
speed?: number;
}

Expand Down Expand Up @@ -407,7 +408,7 @@ export class Battle {
((b.priority || 0) - (a.priority || 0)) ||
((b.speed || 0) - (a.speed || 0)) ||
-((b.subOrder || 0) - (a.subOrder || 0)) ||
-((b.state?.effectOrder || 0) - (a.state?.effectOrder || 0)) ||
-((b.effectOrder || 0) - (a.effectOrder || 0)) ||
0;
}

Expand Down Expand Up @@ -953,6 +954,13 @@ export class Battle {
}
}
}
if (callbackName.endsWith('SwitchIn') || callbackName.endsWith('RedirectTarget')) {
// If multiple hazards are present on one side, their event handlers all perfectly tie in speed, priority,
// and subOrder. They should activate in the order they were created, which is where effectOrder comes in.
// This also applies to speed ties for which ability like Lightning Rod redirects moves.
// TODO: In-game, other events are also sorted this way, but that's an implementation for another refactor
handler.effectOrder = handler.state?.effectOrder;
}
if (handler.effectHolder && (handler.effectHolder as Pokemon).getStat) {
const pokemon = (handler.effectHolder as Pokemon);
handler.speed = pokemon.speed;
Expand Down
6 changes: 4 additions & 2 deletions sim/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ export class EffectState {
duration?: number;
[k: string]: any;

constructor(data: AnyObject, battle: Battle) {
constructor(data: AnyObject, battle: Battle, effectOrder?: number) {
this.id = data.id || '';
Object.assign(this, data);
if (this.id && this.target && (!(this.target instanceof Pokemon) || this.target.isActive)) {
if (effectOrder !== undefined) {
this.effectOrder = effectOrder;
} else if (this.id && this.target && (!(this.target instanceof Pokemon) || this.target.isActive)) {
this.effectOrder = battle.effectOrder++;
} else {
this.effectOrder = 0;
Expand Down

0 comments on commit 3b5aa62

Please sign in to comment.