diff --git a/sim/battle-queue.ts b/sim/battle-queue.ts index c616023a23..7bdec936f0 100644 --- a/sim/battle-queue.ts +++ b/sim/battle-queue.ts @@ -330,10 +330,13 @@ export class BattleQueue extends Array { this.splice(0); } - debug() { - return this.map( + debug(action?: Action): string { + if (action) { // @ts-ignore - action => `${action.order || ''}:${action.priority || ''}:${action.speed || ''}:${action.subOrder || ''} - ${action.choice}${action.pokemon ? ' ' + action.pokemon : ''}${action.move ? ' ' + action.move : ''}` + return `${action.order || ''}:${action.priority || ''}:${action.speed || ''}:${action.subOrder || ''} - ${action.choice}${action.pokemon ? ' ' + action.pokemon : ''}${action.move ? ' ' + action.move : ''}`; + } + return this.map( + queueAction => this.debug(queueAction) ).join('\n') + '\n'; } diff --git a/sim/battle.ts b/sim/battle.ts index 77514e73c9..acc6cb6495 100644 --- a/sim/battle.ts +++ b/sim/battle.ts @@ -2632,6 +2632,17 @@ export class Battle { return false; } + if (this.gen >= 5) { + this.eachEvent('Update'); + } + + if (action.choice === 'runSwitch') { + const pokemon = action.pokemon; + if (pokemon.hp && pokemon.hp <= pokemon.maxhp / 2 && pokemonOriginalHP! > pokemon.maxhp / 2) { + this.runEvent('EmergencyExit', pokemon); + } + } + const switches = this.sides.map( side => side.active.some(pokemon => pokemon && !!pokemon.switchFlag) ); @@ -2647,22 +2658,12 @@ export class Battle { for (const playerSwitch of switches) { if (playerSwitch) { - if (this.gen >= 5) { - this.eachEvent('Update'); - } this.makeRequest('switch'); return true; } } - this.eachEvent('Update'); - - if (action.choice === 'runSwitch') { - const pokemon = action.pokemon; - if (pokemon.hp && pokemon.hp <= pokemon.maxhp / 2 && pokemonOriginalHP! > pokemon.maxhp / 2) { - this.runEvent('EmergencyExit', pokemon); - } - } + if (this.gen < 5) this.eachEvent('Update'); if (this.gen >= 8 && this.queue.length && this.queue[0].choice === 'move') { // In gen 8, speed is updated dynamically so update the queue's speed properties and sort it. diff --git a/test/sim/abilities/emergencyexit.js b/test/sim/abilities/emergencyexit.js index 3d7e2a71e1..d09f6bfae5 100644 --- a/test/sim/abilities/emergencyexit.js +++ b/test/sim/abilities/emergencyexit.js @@ -45,6 +45,17 @@ describe(`Emergency Exit`, function () { assert.equal(battle.requestState, 'switch'); }); + it(`should request switch-out if brought below half HP by Photon Geyser`, function () { + battle = common.createBattle([[ + {species: "Mew", moves: ['photongeyser']}, + ], [ + {species: "Charmeleon", ability: 'emergencyexit', moves: ['splash']}, + {species: "Shaymin", moves: ['splash']}, + ]]); + battle.makeChoices(); + assert.equal(battle.requestState, 'switch'); + }); + it(`should not request switch-out if attacked and healed by berry`, function () { battle = common.createBattle([ [{species: "Golisopod", ability: 'emergencyexit', moves: ['sleeptalk'], item: 'sitrusberry', ivs: EMPTY_IVS}, {species: "Clefable", ability: 'Unaware', moves: ['metronome']}], @@ -70,6 +81,21 @@ describe(`Emergency Exit`, function () { assert(!battle.p2.activeRequest.forceSwitch); }); + it(`should request switch-out after taking hazard damage`, function () { + battle = common.createBattle([ + [{species: "Golisopod", ability: 'emergencyexit', moves: ['uturn', 'sleeptalk']}, {species: "Magikarp", ability: 'swiftswim', moves: ['splash']}], + [{species: "Arceus-Flying", ability: 'ironbarbs', moves: ['stealthrock', 'spikes', 'dragonascent']}], + ]); + battle.makeChoices('move uturn', 'move stealthrock'); + battle.makeChoices('switch 2', ''); + battle.makeChoices('move splash', 'move spikes'); + battle.makeChoices('move splash', 'move spikes'); + battle.makeChoices('move splash', 'move spikes'); + battle.makeChoices('switch 2', 'move dragonascent'); + assert(battle.p1.active[0].hp); + assert.equal(battle.requestState, 'switch'); + }); + it(`should not request switch-out after taking residual damage and getting healed by berry`, function () { battle = common.createBattle([ [{species: "Golisopod", ability: 'emergencyexit', moves: ['uturn', 'sleeptalk'], item: 'sitrusberry'}, {species: "Magikarp", ability: 'swiftswim', moves: ['splash']}],