pokemon-showdown/test/common.js
Guangcong Luo 71498d451d
New validator (#5840)
* Refactor validator

This is a major refactor intended to make the default rules easier to
understand, and also easier for OMs to bypass.

Removed rules:
- `Pokemon`: This is now `-Nonexistent`. Its previous name was intended
  to be interpreted as "simulate the Pokémon games exactly, and only
  allow what they allow". The new name should make it clearer that it
  mainly bans CAPs and other nonexistent Pokémon and functionality.
- `-Illegal`: This is now `Obtainable` (see below).
- `Allow CAP`: This is now `+CAP`. Instead of having a hardcoded rule,
  OMs can now be manually whitelist any pokemon/item/etc or group of
  them, overriding `-Nonexistent`.
- `Ignore Illegal Abilities`: This is now `!Obtainable Abilities` (see
  below).

`Obtainable` was previously `-Illegal`, and does the same thing: Makes
sure you have a regular Pokémon game with only Pokémon that can be
obtained without hacking.

But instead of being a ban, it's now a rule that does nothing by
itself, except contain more rules:
- `Obtainable Moves`
- `Obtainable Abilities`
- `Obtainable Formes`
- `Obtainable Misc`
- `-Nonexistent`
- `-Unreleased`

This allows OMs to piecemeal repeal and unban any of these individual
rules, instead of the previous approach of unbanning them all and
manually reimplementing every single validation you wanted to keep.

* Refactor PokemonSources into a class

This mostly just makes a lot of the weirder checks in the validator
substantially more readable.

This also renames `lsetData` to `setSources`, which should also help
readability.

* Validate Bottle Cap HP types

Fixes an issue reported here:

https://github.com/Zarel/Pokemon-Showdown/issues/5742#issuecomment-533850288

* Fix several move validation issues

Fixes #5742

We have a new MoveSource type: R for Restricted. R moves work like
level-up/tutor/TM moves, except you're limited to one R move.

- Shedinja move stolen from Ninjask in Gen 3-4 are now R moves instead
  of event moves. This allows them to coexist with Nincada egg moves.

- Necrozma-DW/DM now inherit moves/events from Necrozma (like Rotom,
  but with event validation). This allows them to be shiny.

- Pokemon can now get egg moves from their own evolutions. This fixes
  some Tyrogue, Charmander, and Treecko sets mentioned in #5742

- Some more C moves were added, fixing some Hitmontop and Chatot sets
  mentioned in #5742

* Improve ability/move compatibility validator
2019-10-06 04:21:30 +11:00

133 lines
3.7 KiB
JavaScript

'use strict';
const assert = require('assert');
const Dex = require('./../.sim-dist/dex').Dex;
const Sim = require('./../.sim-dist');
const cache = new Map();
const RULE_FLAGS = {
pokemon: 1,
legality: 2,
preview: 4,
sleepClause: 8,
cancel: 16,
endlessBattleClause: 32,
};
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}
/**
* The default random number generator seed used if one is not given.
*/
const DEFAULT_SEED = [0x09917, 0x06924, 0x0e1c8, 0x06af0];
class TestTools {
constructor(options) {
if (!options) options = {};
const mod = options.mod || 'base';
this.baseFormat = options.baseFormat || {effectType: 'Format', mod: mod};
this.dex = Dex.mod(mod);
this.modPrefix = this.baseFormat.name ? `[${this.baseFormat.name}]` : '';
if (!this.modPrefix && !this.dex.isBase) {
this.modPrefix = (/^gen\d$/.test(mod) ? `[Gen ${this.dex.gen}]` : `[${mod}]`);
}
// Handle caches
this.formats = new Map([['singles', new Map()], ['doubles', new Map()], ['triples', new Map()]]);
cache.set(this.baseFormat.id || mod, this);
}
mod(mod) {
if (cache.has(mod)) return cache.get(mod);
if (Dex.dexes[mod]) return new TestTools({mod: mod});
const baseFormat = Dex.getFormat(mod);
if (baseFormat.effectType === 'Format') return new TestTools({mod: baseFormat.mod, baseFormat});
throw new Error(`Mod ${mod} does not exist`);
}
gen(genNum) {
return this.mod('gen' + genNum);
}
getFormat(options) {
let mask = 0;
for (let property in options) {
if (property === 'gameType' || !options[property]) continue;
mask |= RULE_FLAGS[property];
}
const gameType = Dex.getId(options.gameType || 'singles');
if (this.formats.get(gameType).has(mask)) return this.formats.get(gameType).get(mask);
const gameTypePrefix = gameType === 'singles' ? '' : capitalize(gameType);
const formatName = [this.modPrefix, gameTypePrefix, "Custom Game", '' + mask].filter(part => part).join(" ");
const formatId = Dex.getId(formatName);
const format = Object.assign(Object.assign({}, this.baseFormat), {
id: formatId,
name: formatName,
mask: mask,
gameType: options.gameType || 'singles',
isCustomGameFormat: true,
rated: false,
});
if (!format.ruleset) format.ruleset = [];
if (!format.banlist) format.banlist = [];
if (options.pokemon) format.ruleset.push('-Nonexistent');
if (options.legality) format.ruleset.push('Obtainable');
if (options.preview) format.ruleset.push('Team Preview');
if (options.sleepClause) format.ruleset.push('Sleep Clause Mod');
if (options.cancel) format.ruleset.push('Cancel Mod');
if (options.endlessBattleClause) format.ruleset.push('Endless Battle Clause');
this.dex.installFormat(formatId, format);
return format;
}
/**
* Creates a new Battle and returns it.
*
* @param {Object} [options]
* @param {Team[]} [teams]
* @returns {Sim.Battle} A battle.
*/
createBattle(options, teams) {
if (Array.isArray(options)) {
teams = options;
options = {};
}
if (!options) options = {};
const format = this.getFormat(options);
const battleOptions = {
formatid: format.id,
// If a seed for the pseudo-random number generator is not provided,
// a default seed (guaranteed to be the same across test executions)
// will be used.
seed: options.seed || DEFAULT_SEED,
strictChoices: options.strictChoices !== false,
};
if (!teams) return new Sim.Battle(battleOptions);
for (let i = 0; i < teams.length; i++) {
assert(Array.isArray(teams[i]), `Team provided is not an array`);
const playerSlot = `p${i + 1}`;
battleOptions[playerSlot] = {team: teams[i]};
}
return new Sim.Battle(battleOptions);
}
}
const common = exports = module.exports = new TestTools();
cache.set('base', common);
cache.set('gen7', common);