From ee970c1f5604082c46aa67f8da3f5debed3992a2 Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Wed, 28 Sep 2016 18:10:02 -0400 Subject: [PATCH] Fix getPokemon on duplicate fainted If, during effect activation, a Pokemon isn't where it's supposed to be, we search the rest of the player's team for it. For instance, when Destiny Bond activates, Deoxys has already fainted, so it's not where we expect it to be. Normally, we search the player's team for Deoxys, find the Deoxys, and Destiny Bond activates as normal. The problem comes in formats without Species Clause. We might find the wrong Deoxys! Normally, this isn't a big problem. For a Pokemon to match, it has to have the exact same name, so the message displayed is still "Deoxys took the foe's Blaziken down with it!" and everything goes on normally. The problem comes in Doubles formats without Species Clause. Finding the wrong Deoxys overwrites its slot number, which, if it's an active Deoxys, puts it in an inconsistent state, causing crashes later on. This fix changes it so if a Pokemon isn't where it's supposed to be, we search the rest of the player's team, excluding other active Pokemon, for it. This renames the previous isInactive flag in getPokemon to isSwitch, which more accurately describes how its matching works. --- js/battle.js | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/js/battle.js b/js/battle.js index 4474ca365..ad7d5626f 100644 --- a/js/battle.js +++ b/js/battle.js @@ -5768,7 +5768,8 @@ var Battle = (function () { }; Battle.prototype.getPokemon = function (pokemonid, details) { var isNew = false; // if true, don't match any pokemon that already exists (for Team Preview) - var isInactive = false; // if true, don't match an active pokemon (for switching) + var isSwitch = false; // if true, don't match an active, fainted, or immediately-previously switched-out pokemon + var isInactive = false; // if true, don't match an active pokemon var createIfNotFound = false; // if true, create the pokemon if a match wasn't found if (pokemonid === undefined || pokemonid === '??') return null; @@ -5777,9 +5778,9 @@ var Battle = (function () { isNew = true; createIfNotFound = true; // obviously } - if (pokemonid.substr(0, 10) === 'inactive: ') { + if (pokemonid.substr(0, 10) === 'switchin: ') { pokemonid = pokemonid.substr(10); - isInactive = true; + isSwitch = true; createIfNotFound = true; } @@ -5809,6 +5810,7 @@ var Battle = (function () { if (!details) { if (siden < 0) return null; if (this.sides[siden].active[slot]) return this.sides[siden].active[slot]; + isInactive = true; } var searchid = ''; @@ -5816,17 +5818,17 @@ var Battle = (function () { // search p1's pokemon if (siden !== this.p2.n && !isNew) { - if (this.p1.active[slot] && this.p1.active[slot].searchid === searchid && !isInactive) { + if (this.p1.active[slot] && this.p1.active[slot].searchid === searchid && !isSwitch) { this.p1.active[slot].slot = slot; return this.p1.active[slot]; } for (var i = 0; i < this.p1.pokemon.length; i++) { var pokemon = this.p1.pokemon[i]; - if (pokemon.fainted && (isNew || isInactive)) continue; - if (isInactive) { + if (pokemon.fainted && (isNew || isSwitch)) continue; + if (isSwitch || isInactive) { if (this.p1.active.indexOf(pokemon) >= 0) continue; - if (pokemon == this.p1.lastPokemon && !this.p1.active[slot]) continue; } + if (isSwitch && pokemon == this.p1.lastPokemon && !this.p1.active[slot]) continue; if ((searchid && pokemon.searchid === searchid) || // exact match (!pokemon.searchid && pokemon.checkDetails(details)) || // switch-in matches Team Preview entry (!searchid && pokemon.ident === pokemonid)) { // name matched, good enough @@ -5846,17 +5848,17 @@ var Battle = (function () { // search p2's pokemon if (siden !== this.p1.n && !isNew) { - if (this.p2.active[slot] && this.p2.active[slot].searchid === searchid && !isInactive) { + if (this.p2.active[slot] && this.p2.active[slot].searchid === searchid && !isSwitch) { if (slot >= 0) this.p2.active[slot].slot = slot; return this.p2.active[slot]; } for (var i = 0; i < this.p2.pokemon.length; i++) { var pokemon = this.p2.pokemon[i]; - if (pokemon.fainted && (isNew || isInactive)) continue; - if (isInactive) { + if (pokemon.fainted && (isNew || isSwitch)) continue; + if (isSwitch || isInactive) { if (this.p2.active.indexOf(pokemon) >= 0) continue; - if (pokemon == this.p2.lastPokemon && !this.p2.active[slot]) continue; } + if (isSwitch && pokemon == this.p2.lastPokemon && !this.p2.active[slot]) continue; if ((searchid && pokemon.searchid === searchid) || // exact match (!pokemon.searchid && pokemon.checkDetails(details)) || // switch-in matches Team Preview entry (!searchid && pokemon.ident === pokemonid)) { // name matched, good enough @@ -6194,7 +6196,7 @@ var Battle = (function () { case 'replace': this.endLastTurn(); if (this.waitForResult()) return; - var poke = this.getPokemon('inactive: ' + args[1], args[2]); + var poke = this.getPokemon('switchin: ' + args[1], args[2]); var slot = poke.slot; poke.healthParse(args[3]); poke.removeVolatile('itemremoved');