pokemon-showdown/mods/gen2/scripts.js
Juanma Serrano fb18d41885 Gen 2: Fix Baton Pass mons getting damaged by psn / leech seed
AfterMoveSelf does not happen if the move used was Baton Pass.
2014-12-22 13:23:04 +01:00

410 lines
13 KiB
JavaScript

/**
* Gen 2 scripts.
*/
exports.BattleScripts = {
inherit: 'gen3',
gen: 2,
// BattlePokemon scripts.
pokemon: {
getStat: function (statName, unboosted, unmodified) {
statName = toId(statName);
if (statName === 'hp') return this.maxhp;
// base stat
var stat = this.stats[statName];
// stat boosts
if (!unboosted) {
var boost = this.boosts[statName];
if (boost > 6) boost = 6;
if (boost < -6) boost = -6;
if (boost >= 0) {
var boostTable = [1, 1.5, 2, 2.5, 3, 3.5, 4];
stat = Math.floor(stat * boostTable[boost]);
} else {
var numerators = [100, 66, 50, 40, 33, 28, 25];
stat = Math.floor(stat * numerators[-boost] / 100);
}
}
// Stat modifier effects
if (!unmodified) {
var statTable = {atk:'Atk', def:'Def', spa:'SpA', spd:'SpD', spe:'Spe'};
var statMod = 1;
statMod = this.battle.runEvent('Modify' + statTable[statName], this, null, null, statMod);
stat = this.battle.modify(stat, statMod);
}
// Gen 2 caps stats at 999 and min is 1.
stat = this.battle.clampIntRange(stat, 1, 999);
// Treat here the items.
if ((this.species in {'Cubone':1, 'Marowak':1} && this.item === 'thickclub' && statName === 'atk') || (this.species === 'Pikachu' && this.item === 'lightball' && statName in {'spa':1, 'atk':1})) {
stat *= 2;
} else if (this.species === 'Ditto' && this.item === 'metalpowder' && statName in {'def':1, 'spd':1}) {
// what. the. fuck. stop playing pokémon
stat *= 1.5;
}
return stat;
}
},
// Battle scripts.
runMove: function (move, pokemon, target, sourceEffect) {
if (!sourceEffect && toId(move) !== 'struggle') {
var changedMove = this.runEvent('OverrideDecision', pokemon, target, move);
if (changedMove && changedMove !== true) {
move = changedMove;
target = null;
}
}
move = this.getMove(move);
if (!target && target !== false) target = this.resolveTarget(pokemon, move);
this.setActiveMove(move, pokemon, target);
if (pokemon.moveThisTurn) {
// THIS IS PURELY A SANITY CHECK
// DO NOT TAKE ADVANTAGE OF THIS TO PREVENT A POKEMON FROM MOVING;
// USE this.cancelMove INSTEAD
this.debug('' + pokemon.id + ' INCONSISTENT STATE, ALREADY MOVED: ' + pokemon.moveThisTurn);
this.clearActiveMove(true);
return;
}
if (!this.runEvent('BeforeMove', pokemon, target, move)) {
this.clearActiveMove(true);
// This is only run for sleep and fully paralysed.
this.runEvent('AfterMoveSelf', pokemon, target, move);
return;
}
if (move.beforeMoveCallback) {
if (move.beforeMoveCallback.call(this, pokemon, target, move)) {
this.clearActiveMove(true);
return;
}
}
pokemon.lastDamage = 0;
var lockedMove = this.runEvent('LockMove', pokemon);
if (lockedMove === true) lockedMove = false;
if (!lockedMove) {
if (!pokemon.deductPP(move, null, target) && (move.id !== 'struggle')) {
this.add('cant', pokemon, 'nopp', move);
this.clearActiveMove(true);
return;
}
}
pokemon.moveUsed(move);
this.useMove(move, pokemon, target, sourceEffect);
this.runEvent('AfterMove', target, pokemon, move);
if (!move.selfSwitch) this.runEvent('AfterMoveSelf', pokemon, target, move);
},
getDamage: function (pokemon, target, move, suppressMessages) {
// First of all, we get the move.
if (typeof move === 'string') move = this.getMove(move);
if (typeof move === 'number') move = {
basePower: move,
type: '???',
category: 'Physical'
};
// Let's test for immunities.
if (move.affectedByImmunities) {
if (!target.runImmunity(move.type, true)) {
return false;
}
}
// Is it an OHKO move?
if (move.ohko) {
// If it is, move hits if the Pokémon is more level.
if (target.level > pokemon.level) {
this.add('-failed', target);
return false;
}
return target.maxhp;
}
// We edit the damage through move's damage callback
if (move.damageCallback) {
return move.damageCallback.call(this, pokemon, target);
}
// We take damage from damage=level moves
if (move.damage === 'level') {
return pokemon.level;
}
// If there's a fix move damage, we run it
if (move.damage) {
return move.damage;
}
// We check the category and typing to calculate later on the damage
move.category = this.getCategory(move);
if (!move.defensiveCategory) move.defensiveCategory = move.category;
// '???' is typeless damage: used for Struggle and Confusion etc
if (!move.type) move.type = '???';
var type = move.type;
// We get the base power and apply basePowerCallback if necessary
var basePower = move.basePower;
if (move.basePowerCallback) {
basePower = move.basePowerCallback.call(this, pokemon, target, move);
}
// We check for Base Power
if (!basePower) {
if (basePower === 0) return; // Returning undefined means not dealing damage
return basePower;
}
basePower = this.clampIntRange(basePower, 1);
// Checking for the move's Critical Hit ratio
move.critRatio = this.clampIntRange(move.critRatio, 0, 5);
var critMult = [0, 16, 8, 4, 3, 2];
move.crit = move.willCrit || false;
if (typeof move.willCrit === 'undefined') {
if (move.critRatio) {
move.crit = (this.random(critMult[move.critRatio]) === 0);
}
}
if (move.crit) {
move.crit = this.runEvent('CriticalHit', target, null, move);
}
// Happens after crit calculation
if (basePower) {
basePower = this.runEvent('BasePower', pokemon, target, move, basePower);
if (move.basePowerModifier) {
basePower *= move.basePowerModifier;
}
}
if (!basePower) return 0;
basePower = this.clampIntRange(basePower, 1);
// We now check for attacker and defender
var level = pokemon.level;
var attacker = pokemon;
var defender = target;
if (move.useTargetOffensive) attacker = target;
if (move.useSourceDefensive) defender = pokemon;
var atkType = (move.category === 'Physical')? 'atk' : 'spa';
var defType = (move.defensiveCategory === 'Physical')? 'def' : 'spd';
var attack = attacker.getStat(atkType);
var defense = defender.getStat(defType);
if (move.crit) {
move.ignoreNegativeOffensive = true;
move.ignorePositiveDefensive = true;
level *= 2;
if (!suppressMessages) this.add('-crit', target);
}
if (move.ignoreNegativeOffensive && attack < attacker.getStat(move.category === 'Physical' ? 'atk' : 'spa', true, true)) {
move.ignoreOffensive = true;
}
if (move.ignoreOffensive) {
this.debug('Negating (sp)atk boost/penalty.');
attack = attacker.getStat(move.category === 'Physical' ? 'atk' : 'spa', true, true);
}
if (move.ignorePositiveDefensive && defense > target.getStat(move.defensiveCategory === 'Physical' ? 'def' : 'spd', true, true)) {
move.ignoreDefensive = true;
}
if (move.ignoreDefensive) {
this.debug('Negating (sp)def boost/penalty.');
defense = target.getStat(move.defensiveCategory === 'Physical' ? 'def' : 'spd', true, true);
}
// When either attack or defense are higher than 256, they are both divided by 4 and moded by 256.
// This is what cuases the roll over bugs.
if (attack >= 256 || defense >= 256) {
attack = this.clampIntRange(Math.floor(attack / 4) % 256, 1);
// Defense isn't checked on the cartridge, but we don't want those / 0 bugs on the sim.
defense = this.clampIntRange(Math.floor(defense / 4) % 256, 1);
}
// Self destruct moves halve defense at this point.
if (move.selfdestruct && defType === 'def') {
defense = this.clampIntRange(Math.floor(defense / 2), 1);
}
// Let's go with the calculation now that we have what we need.
// We do it step by step just like the game does.
var damage = level * 2;
damage = Math.floor(damage / 5);
damage += 2;
damage *= basePower;
damage *= attack;
damage = Math.floor(damage / defense);
damage = this.clampIntRange(Math.floor(damage / 50), 1, 997);
damage += 2;
// STAB damage bonus, the "???" type never gets STAB
if (type !== '???' && pokemon.hasType(type)) {
damage += Math.floor(damage / 2);
}
// Type effectiveness
var totalTypeMod = this.getEffectiveness(type, target);
// Super effective attack
if (totalTypeMod > 0) {
if (!suppressMessages) this.add('-supereffective', target);
damage *= 2;
if (totalTypeMod >= 2) {
damage *= 2;
}
}
// Resisted attack
if (totalTypeMod < 0) {
if (!suppressMessages) this.add('-resisted', target);
damage = Math.floor(damage / 2);
if (totalTypeMod <= -2) {
damage = Math.floor(damage / 2);
}
}
// Apply random factor is damage is greater than 1
if (damage > 1) {
damage *= this.random(217, 256);
damage = Math.floor(damage / 255);
if (damage > target.hp) damage = target.hp;
}
// If damage is less than 1, we return 1
if (basePower && !Math.floor(damage)) {
return 1;
}
// We are done, this is the final damage
return damage;
},
faint: function (pokemon, source, effect) {
pokemon.faint(source, effect);
this.queue = [];
},
comparePriority: function (a, b) {
a.priority = a.priority || 0;
a.subPriority = a.subPriority || 0;
a.speed = a.speed || 0;
b.priority = b.priority || 0;
b.subPriority = b.subPriority || 0;
b.speed = b.speed || 0;
if ((typeof a.order === 'number' || typeof b.order === 'number') && a.order !== b.order) {
if (typeof a.order !== 'number') {
return -1;
}
if (typeof b.order !== 'number') {
return 1;
}
if (b.order - a.order) {
return -(b.order - a.order);
}
}
if (b.priority - a.priority) {
return b.priority - a.priority;
}
if (b.speed - a.speed) {
if (b.priority === -1 && a.priority === -1) return a.speed - b.speed;
return b.speed - a.speed;
}
if (b.subOrder - a.subOrder) {
return -(b.subOrder - a.subOrder);
}
return Math.random() - 0.5;
},
getResidualStatuses: function (thing, callbackType) {
var statuses = this.getRelevantEffectsInner(thing || this, callbackType || 'residualCallback', null, null, false, true, 'duration');
statuses.sort(this.comparePriority);
return statuses;
},
residualEvent: function (eventid, relayVar) {
var statuses = this.getRelevantEffectsInner(this, 'on' + eventid, null, null, false, true, 'duration');
statuses.sort(this.comparePriority);
while (statuses.length) {
var statusObj = statuses.shift();
var status = statusObj.status;
if (statusObj.thing.fainted) continue;
if (statusObj.statusData && statusObj.statusData.duration) {
statusObj.statusData.duration--;
if (!statusObj.statusData.duration) {
statusObj.end.call(statusObj.thing, status.id);
continue;
}
}
this.singleEvent(eventid, status, statusObj.statusData, statusObj.thing, relayVar);
}
},
getRelevantEffects: function (thing, callbackType, foeCallbackType, foeThing, checkChildren) {
var statuses = this.getRelevantEffectsInner(thing, callbackType, foeCallbackType, foeThing, true, false);
statuses.sort(this.comparePriority);
return statuses;
},
addQueue: function (decision, noSort, side) {
if (decision) {
if (Array.isArray(decision)) {
for (var i = 0; i < decision.length; i++) {
this.addQueue(decision[i], noSort);
}
return;
}
if (decision.choice === 'pass') return;
if (!decision.side && side) decision.side = side;
if (!decision.side && decision.pokemon) decision.side = decision.pokemon.side;
if (!decision.choice && decision.move) decision.choice = 'move';
if (!decision.priority) {
var priorities = {
'beforeTurn': 100,
'beforeTurnMove': 99,
'switch': 6,
'runSwitch': 6.1,
'residual': -100,
'team': 102,
'start': 101
};
if (priorities[decision.choice]) {
decision.priority = priorities[decision.choice];
}
}
if (decision.choice === 'move') {
if (this.getMove(decision.move).beforeTurnCallback) {
this.addQueue({choice: 'beforeTurnMove', pokemon: decision.pokemon, move: decision.move, targetLoc: decision.targetLoc}, true);
}
} else if (decision.choice === 'switch') {
if (decision.pokemon.switchFlag && decision.pokemon.switchFlag !== true) {
decision.pokemon.switchCopyFlag = decision.pokemon.switchFlag;
}
decision.pokemon.switchFlag = false;
if (!decision.speed && decision.pokemon && decision.pokemon.isActive) decision.speed = decision.pokemon.speed;
}
if (decision.move) {
var target;
if (!decision.targetPosition) {
target = this.resolveTarget(decision.pokemon, decision.move);
decision.targetSide = target.side;
decision.targetPosition = target.position;
}
decision.move = this.getMove(decision.move);
if (!decision.priority) {
var priority = decision.move.priority;
priority = this.runEvent('ModifyPriority', decision.pokemon, target, decision.move, priority);
decision.priority = priority;
}
}
if (!decision.pokemon && !decision.speed) decision.speed = 1;
if (!decision.speed && decision.choice === 'switch' && decision.target) decision.speed = decision.target.speed;
if (!decision.speed) decision.speed = decision.pokemon.speed;
if (decision.choice === 'switch' && !decision.side.pokemon[0].isActive) {
// if there's no actives, switches happen before activations
decision.priority = 6.2;
}
this.queue.push(decision);
}
if (!noSort) {
this.queue.sort(this.comparePriority);
}
}
};