Skip to content

Commit

Permalink
Calculate natures with 16-bit truncation (#7540)
Browse files Browse the repository at this point in the history
Also fixes Let's Go! which wanted to override these but couldn't.
  • Loading branch information
urkerab committed Oct 25, 2020
1 parent 099346e commit b90f03b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
2 changes: 1 addition & 1 deletion data/mods/letsgo/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const Scripts: ModdedBattleScriptsData = {
const stat = baseStats['hp'];
modStats['hp'] = Math.floor(Math.floor(2 * stat + set.ivs['hp'] + 100) * set.level / 100 + 10);
}
return this.dex.natureModify(modStats, set);
return this.natureModify(modStats, set);
},

/**
Expand Down
33 changes: 33 additions & 0 deletions sim/battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1994,6 +1994,39 @@ export class Battle {
return tr((tr(value * modifier) + 2048 - 1) / 4096);
}

/** Given a table of base stats and a pokemon set, return the actual stats. */
spreadModify(baseStats: StatsTable, set: PokemonSet): StatsTable {
const modStats: SparseStatsTable = {atk: 10, def: 10, spa: 10, spd: 10, spe: 10};
const tr = this.trunc;
let statName: keyof StatsTable;
for (statName in modStats) {
const stat = baseStats[statName];
modStats[statName] = tr(tr(2 * stat + set.ivs[statName] + tr(set.evs[statName] / 4)) * set.level / 100 + 5);
}
if ('hp' in baseStats) {
const stat = baseStats['hp'];
modStats['hp'] = tr(tr(2 * stat + set.ivs['hp'] + tr(set.evs['hp'] / 4) + 100) * set.level / 100 + 10);
}
return this.natureModify(modStats as StatsTable, set);
}

natureModify(stats: StatsTable, set: PokemonSet): StatsTable {
// Natures are calculated with 16-bit truncation.
// This only affects Eternatus-Eternmax in Pure Hackmons.
const tr = this.trunc;
const nature = this.dex.getNature(set.nature);
let stat: keyof StatsTable;
if (nature.plus) {
stat = nature.plus;
stats[stat] = tr(tr(stats[stat] * 110, 16) / 100);
}
if (nature.minus) {
stat = nature.minus;
stats[stat] = tr(tr(stats[stat] * 90, 16) / 100);
}
return stats;
}

getCategory(move: string | Move) {
return this.dex.getMove(move).category || 'Physical';
}
Expand Down
30 changes: 0 additions & 30 deletions sim/dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,36 +828,6 @@ export class ModdedDex {
return nature;
}

/** Given a table of base stats and a pokemon set, return the actual stats. */
spreadModify(baseStats: StatsTable, set: PokemonSet): StatsTable {
const modStats: SparseStatsTable = {atk: 10, def: 10, spa: 10, spd: 10, spe: 10};
const tr = this.trunc;
let statName: keyof StatsTable;
for (statName in modStats) {
const stat = baseStats[statName];
modStats[statName] = tr(tr(2 * stat + set.ivs[statName] + tr(set.evs[statName] / 4)) * set.level / 100 + 5);
}
if ('hp' in baseStats) {
const stat = baseStats['hp'];
modStats['hp'] = tr(tr(2 * stat + set.ivs['hp'] + tr(set.evs['hp'] / 4) + 100) * set.level / 100 + 10);
}
return this.natureModify(modStats as StatsTable, set);
}

natureModify(stats: StatsTable, set: PokemonSet): StatsTable {
const nature = this.getNature(set.nature);
let stat: keyof StatsTable;
if (nature.plus) {
stat = nature.plus;
stats[stat] = Math.floor(stats[stat] * 1.1);
}
if (nature.minus) {
stat = nature.minus;
stats[stat] = Math.floor(stats[stat] * 0.9);
}
return stats;
}

getHiddenPower(ivs: AnyObject) {
const hpTypes = [
'Fighting', 'Flying', 'Poison', 'Ground', 'Rock', 'Bug', 'Ghost', 'Steel',
Expand Down
2 changes: 1 addition & 1 deletion sim/pokemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1159,7 +1159,7 @@ export class Pokemon {
this.knownType = true;
this.weighthg = species.weighthg;

const stats = this.battle.dex.spreadModify(this.species.baseStats, this.set);
const stats = this.battle.spreadModify(this.species.baseStats, this.set);
if (this.species.maxHP) stats.hp = this.species.maxHP;

if (!this.maxhp) {
Expand Down

0 comments on commit b90f03b

Please sign in to comment.