Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor lastAttackedBy to hurtBy #4894

Merged
merged 6 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions data/moves.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,8 +945,11 @@ let BattleMovedex = {
accuracy: 100,
basePower: 60,
basePowerCallback: function (pokemon, target, move) {
if (target.lastDamage > 0 && pokemon.lastAttackedBy && pokemon.lastAttackedBy.thisTurn && pokemon.lastAttackedBy.pokemon === target) {
this.debug('Boosted for getting hit by ' + pokemon.lastAttackedBy.move);
let hurtByTarget = pokemon.hurtBy.some(p =>
p.source === target && p.damage > 0 && p.thisTurn
);
if (hurtByTarget) {
this.debug('Boosted for getting hit by ' + target);
return move.basePower * 2;
}
return move.basePower;
Expand Down Expand Up @@ -13390,8 +13393,11 @@ let BattleMovedex = {
accuracy: 100,
basePower: 60,
basePowerCallback: function (pokemon, target, move) {
if (target.lastDamage > 0 && pokemon.lastAttackedBy && pokemon.lastAttackedBy.thisTurn && pokemon.lastAttackedBy.pokemon === target) {
this.debug('Boosted for getting hit by ' + pokemon.lastAttackedBy.move);
let hurtByTarget = pokemon.hurtBy.some(p =>
p.source === target && p.damage > 0 && p.thisTurn
);
if (hurtByTarget) {
this.debug('Boosted for getting hit by ' + target);
return move.basePower * 2;
}
return move.basePower;
Expand Down
9 changes: 5 additions & 4 deletions mods/gen1/moves.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,11 +967,12 @@ let BattleMovedex = {
}
this.runEvent('AfterSubDamage', target, source, move, damage);
// Add here counter damage
if (!target.lastAttackedBy) {
target.lastAttackedBy = {pokemon: source, move: move.id, thisTurn: true, damage: damage};
let lastHurtBy = target.getLastHurtBy();
if (!lastHurtBy) {
target.hurtBy.push({source: source, move: move.id, damage: damage, thisTurn: true});
} else {
target.lastAttackedBy.move = move.id;
target.lastAttackedBy.damage = damage;
lastHurtBy.move = move.id;
lastHurtBy.damage = damage;
}
return 0;
},
Expand Down
12 changes: 6 additions & 6 deletions mods/gen2/moves.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ let BattleMovedex = {
inherit: true,
desc: "Deals damage to the opposing Pokemon equal to twice the HP lost by the user from a physical attack this turn. This move considers Hidden Power as Normal type, and only the last hit of a multi-hit attack is counted. Fails if the user moves first, if the user was not hit by a physical attack this turn, or if the user did not lose HP from the attack. If the opposing Pokemon used Fissure or Horn Drill and missed, this move deals 65535 damage.",
damageCallback: function (pokemon, target) {
if (pokemon.lastAttackedBy && pokemon.lastAttackedBy.thisTurn && pokemon.lastAttackedBy.move && (this.getCategory(pokemon.lastAttackedBy.move) === 'Physical' || this.getMove(pokemon.lastAttackedBy.move).id === 'hiddenpower') &&
(!target.lastMove || target.lastMove.id !== 'sleeptalk')) {
return 2 * pokemon.lastAttackedBy.damage;
let lastHurtBy = pokemon.getLastHurtBy();
if (lastHurtBy && lastHurtBy.move && lastHurtBy.thisTurn && (this.getCategory(lastHurtBy.move) === 'Physical' || this.getMove(lastHurtBy.move).id === 'hiddenpower') && (!target.lastMove || target.lastMove.id !== 'sleeptalk')) {
return 2 * lastHurtBy.damage;
}
return false;
},
Expand Down Expand Up @@ -493,9 +493,9 @@ let BattleMovedex = {
inherit: true,
desc: "Deals damage to the opposing Pokemon equal to twice the HP lost by the user from a special attack this turn. This move considers Hidden Power as Normal type, and only the last hit of a multi-hit attack is counted. Fails if the user moves first, if the user was not hit by a special attack this turn, or if the user did not lose HP from the attack.",
damageCallback: function (pokemon, target) {
if (pokemon.lastAttackedBy && pokemon.lastAttackedBy.thisTurn && pokemon.lastAttackedBy.move && this.getCategory(pokemon.lastAttackedBy.move) === 'Special' &&
this.getMove(pokemon.lastAttackedBy.move).id !== 'hiddenpower' && (!target.lastMove || target.lastMove.id !== 'sleeptalk')) {
return 2 * pokemon.lastAttackedBy.damage;
let lastHurtBy = pokemon.getLastHurtBy();
if (lastHurtBy && lastHurtBy.move && lastHurtBy.thisTurn && this.getCategory(lastHurtBy.move) === 'Special' && this.getMove(lastHurtBy.move).id !== 'hiddenpower' && (!target.lastMove || target.lastMove.id !== 'sleeptalk')) {
return 2 * lastHurtBy.damage;
}
return false;
},
Expand Down
10 changes: 6 additions & 4 deletions mods/gen3/moves.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ let BattleMovedex = {
inherit: true,
desc: "Deals damage to the last opposing Pokemon to hit the user with a physical attack this turn equal to twice the HP lost by the user from that attack. If that opposing Pokemon's position is no longer in use and there is another opposing Pokemon on the field, the damage is done to it instead. This move considers Hidden Power as Normal type, and only the last hit of a multi-hit attack is counted. Fails if the user was not hit by an opposing Pokemon's physical attack this turn, or if the user did not lose HP from the attack.",
damageCallback: function (pokemon) {
if (pokemon.lastAttackedBy && pokemon.lastAttackedBy.thisTurn && pokemon.lastAttackedBy.move && (this.getCategory(pokemon.lastAttackedBy.move) === 'Physical' || this.getMove(pokemon.lastAttackedBy.move).id === 'hiddenpower')) {
let lastHurtBy = pokemon.getLastHurtBy();
if (lastHurtBy && lastHurtBy.move && lastHurtBy.thisTurn && (this.getCategory(lastHurtBy.move) === 'Physical' || this.getMove(lastHurtBy.move).id === 'hiddenpower')) {
// @ts-ignore
return 2 * pokemon.lastAttackedBy.damage;
return 2 * lastHurtBy.damage;
}
return false;
},
Expand Down Expand Up @@ -557,10 +558,11 @@ let BattleMovedex = {
onTryHit: function () { },
onHit: function (pokemon) {
let noMirror = ['assist', 'curse', 'doomdesire', 'focuspunch', 'futuresight', 'magiccoat', 'metronome', 'mimic', 'mirrormove', 'naturepower', 'psychup', 'roleplay', 'sketch', 'sleeptalk', 'spikes', 'spitup', 'taunt', 'teeterdance', 'transform'];
if (!pokemon.lastAttackedBy || !pokemon.lastAttackedBy.pokemon.lastMove || !pokemon.lastAttackedBy.move || noMirror.includes(pokemon.lastAttackedBy.move) || !pokemon.lastAttackedBy.pokemon.hasMove(pokemon.lastAttackedBy.move)) {
let lastHurtBy = pokemon.getLastHurtBy();
if (!lastHurtBy || !lastHurtBy.source.lastMove || !lastHurtBy.move || noMirror.includes(lastHurtBy.move) || !lastHurtBy.source.hasMove(lastHurtBy.move)) {
return false;
}
this.useMove(pokemon.lastAttackedBy.move, pokemon);
this.useMove(lastHurtBy.move, pokemon);
},
target: "self",
},
Expand Down
7 changes: 4 additions & 3 deletions mods/gen4/moves.js
Original file line number Diff line number Diff line change
Expand Up @@ -1105,10 +1105,11 @@ let BattleMovedex = {
onTryHit: function () { },
onHit: function (pokemon) {
let noMirror = ['acupressure', 'aromatherapy', 'assist', 'chatter', 'copycat', 'counter', 'curse', 'doomdesire', 'feint', 'focuspunch', 'futuresight', 'gravity', 'hail', 'haze', 'healbell', 'helpinghand', 'lightscreen', 'luckychant', 'magiccoat', 'mefirst', 'metronome', 'mimic', 'mirrorcoat', 'mirrormove', 'mist', 'mudsport', 'naturepower', 'perishsong', 'psychup', 'raindance', 'reflect', 'roleplay', 'safeguard', 'sandstorm', 'sketch', 'sleeptalk', 'snatch', 'spikes', 'spitup', 'stealthrock', 'struggle', 'sunnyday', 'tailwind', 'toxicspikes', 'transform', 'watersport'];
if (!pokemon.lastAttackedBy || !pokemon.lastAttackedBy.pokemon.lastMove || !pokemon.lastAttackedBy.move || noMirror.includes(pokemon.lastAttackedBy.move) || !pokemon.lastAttackedBy.pokemon.hasMove(pokemon.lastAttackedBy.move)) {
return false;
let lastHurtBy = pokemon.getLastHurtBy();
if (!lastHurtBy || !lastHurtBy.source.lastMove || !lastHurtBy.move || noMirror.includes(lastHurtBy.move) || !lastHurtBy.source.hasMove(lastHurtBy.move)) {
return false;
}
this.useMove(pokemon.lastAttackedBy.move, pokemon);
this.useMove(lastHurtBy.move, pokemon);
},
target: "self",
},
Expand Down
9 changes: 6 additions & 3 deletions mods/gennext/moves.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,12 @@ let BattleMovedex = {
avalanche: {
inherit: true,
basePowerCallback: function (pokemon, source) {
if ((source.lastDamage > 0 && pokemon.lastAttackedBy && pokemon.lastAttackedBy.thisTurn)) {
this.debug('Boosted for getting hit by ' + pokemon.lastAttackedBy.move);
return this.isWeather('hail') ? 180 : 120;
let lastHurtBy = pokemon.getLastHurtBy();
if (lastHurtBy) {
if (lastHurtBy.damage > 0 && lastHurtBy.thisTurn) {
this.debug('Boosted for getting hit by ' + lastHurtBy.move);
return this.isWeather('hail') ? 180 : 120;
}
}
return this.isWeather('hail') ? 90 : 60;
},
Expand Down
9 changes: 5 additions & 4 deletions mods/stadium/moves.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ let BattleMovedex = {
}
this.runEvent('AfterSubDamage', target, source, move, damage);
// Add here counter damage
if (!target.lastAttackedBy) {
target.lastAttackedBy = {pokemon: source, move: move.id, thisTurn: true, damage: damage};
let lastHurtBy = target.getLastHurtBy();
if (!lastHurtBy) {
target.hurtBy.push({source: source, move: move.id, damage: damage, thisTurn: true});
} else {
target.lastAttackedBy.move = move.id;
target.lastAttackedBy.damage = damage;
lastHurtBy.move = move.id;
lastHurtBy.damage = damage;
}
return 0;
},
Expand Down
13 changes: 8 additions & 5 deletions sim/battle.js
Original file line number Diff line number Diff line change
Expand Up @@ -1545,13 +1545,16 @@ class Battle extends Dex.ModdedDex {
this.runEvent('DisableMove', pokemon);
if (!pokemon.ateBerry) pokemon.disableMove('belch');

if (pokemon.lastAttackedBy) {
if (pokemon.lastAttackedBy.pokemon.isActive) {
pokemon.lastAttackedBy.thisTurn = false;
// If it was an illusion, it's not any more
if (pokemon.getLastHurtBy() && this.gen >= 7) pokemon.knownType = true;

for (let i = pokemon.hurtBy.length - 1; i >= 0; i--) {
let attack = pokemon.hurtBy[i];
if (attack.source.isActive) {
attack.thisTurn = false;
} else {
pokemon.lastAttackedBy = null;
pokemon.hurtBy.slice(pokemon.hurtBy.indexOf(attack), 1);
}
if (this.gen >= 7) pokemon.knownType = true; // If it was an illusion, it's not any more
}

if (this.gen >= 7) {
Expand Down
16 changes: 11 additions & 5 deletions sim/pokemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ class Pokemon {
this.moveThisTurnResult = undefined;

this.lastDamage = 0;
/**@type {?{pokemon: Pokemon, damage: number, thisTurn: boolean, move?: string}} */
this.lastAttackedBy = null;
/**@type {{source: Pokemon, damage: number, thisTurn: boolean, move?: string}[]} */
this.hurtBy = [];
this.usedItemThisTurn = false;
this.newlySwitched = false;
this.beingCalledBack = false;
Expand Down Expand Up @@ -611,12 +611,18 @@ class Pokemon {
gotAttacked(move, damage, source) {
if (!damage) damage = 0;
move = this.battle.getMove(move);
this.lastAttackedBy = {
pokemon: source,
let lastHurtBy = {
source: source,
damage: damage,
move: move.id,
thisTurn: true,
};
this.hurtBy.push(lastHurtBy);
}

getLastHurtBy() {
if (this.hurtBy.length === 0) return undefined;
return this.hurtBy[this.hurtBy.length - 1];
}

/**
Expand Down Expand Up @@ -1032,7 +1038,7 @@ class Pokemon {
this.moveThisTurn = '';

this.lastDamage = 0;
this.lastAttackedBy = null;
this.hurtBy = [];
this.newlySwitched = true;
this.beingCalledBack = false;

Expand Down