Fix Emergency Exit on switch-in

This commit is contained in:
Guangcong Luo 2020-03-08 20:13:29 -07:00
parent efa98a08b9
commit 38e4af504c
3 changed files with 44 additions and 14 deletions

View File

@ -330,10 +330,13 @@ export class BattleQueue extends Array<Action> {
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';
}

View File

@ -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.

View File

@ -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']}],