forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dex.ts
579 lines (515 loc) · 17.8 KB
/
dex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/**
* Dex
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Handles getting data about pokemon, items, etc. Also contains some useful
* helper functions for using dex data.
*
* By default, nothing is loaded until you call Dex.mod(mod) or
* Dex.forFormat(format).
*
* You may choose to preload some things:
* - Dex.includeMods() ~10ms
* This will preload `Dex.dexes`, giving you a list of possible mods.
* - Dex.includeFormats() ~30ms
* As above, but will also preload `Dex.formats.all()`.
* - Dex.includeData() ~500ms
* As above, but will also preload all of Dex.data for Gen 8, so
* functions like `Dex.species.get`, etc will be instantly usable.
* - Dex.includeModData() ~1500ms
* As above, but will also preload `Dex.dexes[...].data` for all mods.
*
* Note that preloading is never necessary. All the data will be
* automatically preloaded when needed, preloading will just spend time
* now so you don't need to spend time later.
*
* @license MIT
*/
import * as fs from 'fs';
import * as path from 'path';
import * as Data from './dex-data';
import {Condition, DexConditions} from './dex-conditions';
import {DataMove, DexMoves} from './dex-moves';
import {Item, DexItems} from './dex-items';
import {Ability, DexAbilities} from './dex-abilities';
import {Species, DexSpecies} from './dex-species';
import {Format, DexFormats} from './dex-formats';
import {Utils} from '../lib';
const BASE_MOD = 'gen9' as ID;
const DATA_DIR = path.resolve(__dirname, '../data');
const MODS_DIR = path.resolve(DATA_DIR, './mods');
const dexes: {[mod: string]: ModdedDex} = Object.create(null);
type DataType =
'Abilities' | 'Rulesets' | 'FormatsData' | 'Items' | 'Learnsets' | 'Moves' |
'Natures' | 'Pokedex' | 'Scripts' | 'Conditions' | 'TypeChart' | 'PokemonGoData';
const DATA_TYPES: (DataType | 'Aliases')[] = [
'Abilities', 'Rulesets', 'FormatsData', 'Items', 'Learnsets', 'Moves',
'Natures', 'Pokedex', 'Scripts', 'Conditions', 'TypeChart', 'PokemonGoData',
];
const DATA_FILES = {
Abilities: 'abilities',
Aliases: 'aliases',
Rulesets: 'rulesets',
FormatsData: 'formats-data',
Items: 'items',
Learnsets: 'learnsets',
Moves: 'moves',
Natures: 'natures',
Pokedex: 'pokedex',
PokemonGoData: 'pokemongo',
Scripts: 'scripts',
Conditions: 'conditions',
TypeChart: 'typechart',
};
/** Unfortunately we do for..in too much to want to deal with the casts */
export interface DexTable<T> {[id: string]: T}
export interface AliasesTable {[id: IDEntry]: string}
interface DexTableData {
Abilities: DexTable<import('./dex-abilities').AbilityData>;
Aliases: DexTable<string>;
Rulesets: DexTable<import('./dex-formats').FormatData>;
Items: DexTable<import('./dex-items').ItemData>;
Learnsets: DexTable<import('./dex-species').LearnsetData>;
Moves: DexTable<import('./dex-moves').MoveData>;
Natures: DexTable<import('./dex-data').NatureData>;
Pokedex: DexTable<import('./dex-species').SpeciesData>;
FormatsData: DexTable<import('./dex-species').SpeciesFormatsData>;
PokemonGoData: DexTable<import('./dex-species').PokemonGoData>;
Scripts: DexTable<AnyObject>;
Conditions: DexTable<import('./dex-conditions').ConditionData>;
TypeChart: DexTable<import('./dex-data').TypeData>;
}
interface TextTableData {
Abilities: DexTable<AbilityText>;
Items: DexTable<ItemText>;
Moves: DexTable<MoveText>;
Pokedex: DexTable<PokedexText>;
Default: DexTable<DefaultText>;
}
export const toID = Data.toID;
export class ModdedDex {
readonly Data = Data;
readonly Condition = Condition;
readonly Ability = Ability;
readonly Item = Item;
readonly Move = DataMove;
readonly Species = Species;
readonly Format = Format;
readonly ModdedDex = ModdedDex;
readonly name = "[ModdedDex]";
readonly isBase: boolean;
readonly currentMod: string;
readonly dataDir: string;
readonly toID = Data.toID;
gen = 0;
parentMod = '';
modsLoaded = false;
dataCache: DexTableData | null;
textCache: TextTableData | null;
deepClone = Utils.deepClone;
deepFreeze = Utils.deepFreeze;
Multiset = Utils.Multiset;
readonly formats: DexFormats;
readonly abilities: DexAbilities;
readonly items: DexItems;
readonly moves: DexMoves;
readonly species: DexSpecies;
readonly conditions: DexConditions;
readonly natures: Data.DexNatures;
readonly types: Data.DexTypes;
readonly stats: Data.DexStats;
constructor(mod = 'base') {
this.isBase = (mod === 'base');
this.currentMod = mod;
this.dataDir = (this.isBase ? DATA_DIR : MODS_DIR + '/' + this.currentMod);
this.dataCache = null;
this.textCache = null;
this.formats = new DexFormats(this);
this.abilities = new DexAbilities(this);
this.items = new DexItems(this);
this.moves = new DexMoves(this);
this.species = new DexSpecies(this);
this.conditions = new DexConditions(this);
this.natures = new Data.DexNatures(this);
this.types = new Data.DexTypes(this);
this.stats = new Data.DexStats(this);
}
get data(): DexTableData {
return this.loadData();
}
get dexes(): {[mod: string]: ModdedDex} {
this.includeMods();
return dexes;
}
mod(mod: string | undefined): ModdedDex {
if (!dexes['base'].modsLoaded) dexes['base'].includeMods();
return dexes[mod || 'base'].includeData();
}
forGen(gen: number) {
if (!gen) return this;
return this.mod(`gen${gen}`);
}
forFormat(format: Format | string): ModdedDex {
if (!this.modsLoaded) this.includeMods();
const mod = this.formats.get(format).mod;
return dexes[mod || BASE_MOD].includeData();
}
modData(dataType: DataType, id: string) {
if (this.isBase) return this.data[dataType][id];
if (this.data[dataType][id] !== dexes[this.parentMod].data[dataType][id]) return this.data[dataType][id];
return (this.data[dataType][id] = Utils.deepClone(this.data[dataType][id]));
}
effectToString() {
return this.name;
}
/**
* Sanitizes a username or Pokemon nickname
*
* Returns the passed name, sanitized for safe use as a name in the PS
* protocol.
*
* Such a string must uphold these guarantees:
* - must not contain any ASCII whitespace character other than a space
* - must not start or end with a space character
* - must not contain any of: | , [ ]
* - must not be the empty string
* - must not contain Unicode RTL control characters
*
* If no such string can be found, returns the empty string. Calling
* functions are expected to check for that condition and deal with it
* accordingly.
*
* getName also enforces that there are not multiple consecutive space
* characters in the name, although this is not strictly necessary for
* safety.
*/
getName(name: any): string {
if (typeof name !== 'string' && typeof name !== 'number') return '';
name = ('' + name).replace(/[|\s[\],\u202e]+/g, ' ').trim();
if (name.length > 18) name = name.substr(0, 18).trim();
// remove zalgo
name = name.replace(
/[\u0300-\u036f\u0483-\u0489\u0610-\u0615\u064B-\u065F\u0670\u06D6-\u06DC\u06DF-\u06ED\u0E31\u0E34-\u0E3A\u0E47-\u0E4E]{3,}/g,
''
);
name = name.replace(/[\u239b-\u23b9]/g, '');
return name;
}
/**
* Returns false if the target is immune; true otherwise.
* Also checks immunity to some statuses.
*/
getImmunity(
source: {type: string} | string,
target: {getTypes: () => string[]} | {types: string[]} | string[] | string
): boolean {
const sourceType: string = typeof source !== 'string' ? source.type : source;
// @ts-ignore
const targetTyping: string[] | string = target.getTypes?.() || target.types || target;
if (Array.isArray(targetTyping)) {
for (const type of targetTyping) {
if (!this.getImmunity(sourceType, type)) return false;
}
return true;
}
const typeData = this.types.get(targetTyping);
if (typeData && typeData.damageTaken[sourceType] === 3) return false;
return true;
}
getEffectiveness(
source: {type: string} | string,
target: {getTypes: () => string[]} | {types: string[]} | string[] | string
): number {
const sourceType: string = typeof source !== 'string' ? source.type : source;
// @ts-ignore
const targetTyping: string[] | string = target.getTypes?.() || target.types || target;
let totalTypeMod = 0;
if (Array.isArray(targetTyping)) {
for (const type of targetTyping) {
totalTypeMod += this.getEffectiveness(sourceType, type);
}
return totalTypeMod;
}
const typeData = this.types.get(targetTyping);
if (!typeData) return 0;
switch (typeData.damageTaken[sourceType]) {
case 1: return 1; // super-effective
case 2: return -1; // resist
// in case of weird situations like Gravity, immunity is handled elsewhere
default: return 0;
}
}
getDescs(table: keyof TextTableData, id: ID, dataEntry: AnyObject) {
if (dataEntry.shortDesc) {
return {
desc: dataEntry.desc,
shortDesc: dataEntry.shortDesc,
};
}
const entry = this.loadTextData()[table][id];
if (!entry) return null;
const descs = {
desc: '',
shortDesc: '',
};
for (let i = this.gen; i < dexes['base'].gen; i++) {
const curDesc = entry[`gen${i}` as keyof typeof entry]?.desc;
const curShortDesc = entry[`gen${i}` as keyof typeof entry]?.shortDesc;
if (!descs.desc && curDesc) {
descs.desc = curDesc;
}
if (!descs.shortDesc && curShortDesc) {
descs.shortDesc = curShortDesc;
}
if (descs.desc && descs.shortDesc) break;
}
if (!descs.shortDesc) descs.shortDesc = entry.shortDesc || '';
if (!descs.desc) descs.desc = entry.desc || descs.shortDesc;
return descs;
}
/**
* Ensure we're working on a copy of a move (and make a copy if we aren't)
*
* Remember: "ensure" - by default, it won't make a copy of a copy:
* moveCopy === Dex.getActiveMove(moveCopy)
*
* If you really want to, use:
* moveCopyCopy = Dex.getActiveMove(moveCopy.id)
*/
getActiveMove(move: Move | string): ActiveMove {
if (move && typeof (move as ActiveMove).hit === 'number') return move as ActiveMove;
move = this.moves.get(move);
const moveCopy: ActiveMove = this.deepClone(move);
moveCopy.hit = 0;
return moveCopy;
}
getHiddenPower(ivs: StatsTable) {
const hpTypes = [
'Fighting', 'Flying', 'Poison', 'Ground', 'Rock', 'Bug', 'Ghost', 'Steel',
'Fire', 'Water', 'Grass', 'Electric', 'Psychic', 'Ice', 'Dragon', 'Dark',
];
const tr = this.trunc;
const stats = {hp: 31, atk: 31, def: 31, spe: 31, spa: 31, spd: 31};
if (this.gen <= 2) {
// Gen 2 specific Hidden Power check. IVs are still treated 0-31 so we get them 0-15
const atkDV = tr(ivs.atk / 2);
const defDV = tr(ivs.def / 2);
const speDV = tr(ivs.spe / 2);
const spcDV = tr(ivs.spa / 2);
return {
type: hpTypes[4 * (atkDV % 4) + (defDV % 4)],
power: tr(
(5 * ((spcDV >> 3) + (2 * (speDV >> 3)) + (4 * (defDV >> 3)) + (8 * (atkDV >> 3))) + (spcDV % 4)) / 2 + 31
),
};
} else {
// Hidden Power check for Gen 3 onwards
let hpTypeX = 0;
let hpPowerX = 0;
let i = 1;
for (const s in stats) {
hpTypeX += i * (ivs[s as StatID] % 2);
hpPowerX += i * (tr(ivs[s as StatID] / 2) % 2);
i *= 2;
}
return {
type: hpTypes[tr(hpTypeX * 15 / 63)],
// After Gen 6, Hidden Power is always 60 base power
power: (this.gen && this.gen < 6) ? tr(hpPowerX * 40 / 63) + 30 : 60,
};
}
}
/**
* Truncate a number into an unsigned 32-bit integer, for
* compatibility with the cartridge games' math systems.
*/
trunc(num: number, bits = 0) {
if (bits) return (num >>> 0) % (2 ** bits);
return num >>> 0;
}
dataSearch(
target: string, searchIn?: ('Pokedex' | 'Moves' | 'Abilities' | 'Items' | 'Natures')[] | null, isInexact?: boolean
): AnyObject[] | null {
if (!target) return null;
searchIn = searchIn || ['Pokedex', 'Moves', 'Abilities', 'Items', 'Natures'];
const searchObjects = {
Pokedex: 'species', Moves: 'moves', Abilities: 'abilities', Items: 'items', Natures: 'natures',
} as const;
const searchTypes = {
Pokedex: 'pokemon', Moves: 'move', Abilities: 'ability', Items: 'item', Natures: 'nature',
} as const;
let searchResults: AnyObject[] | null = [];
for (const table of searchIn) {
const res = this[searchObjects[table]].get(target);
if (res.exists && res.gen <= this.gen) {
searchResults.push({
isInexact,
searchType: searchTypes[table],
name: res.name,
});
}
}
if (searchResults.length) return searchResults;
if (isInexact) return null; // prevent infinite loop
const cmpTarget = toID(target);
let maxLd = 3;
if (cmpTarget.length <= 1) {
return null;
} else if (cmpTarget.length <= 4) {
maxLd = 1;
} else if (cmpTarget.length <= 6) {
maxLd = 2;
}
searchResults = null;
for (const table of [...searchIn, 'Aliases'] as const) {
const searchObj = this.data[table] as DexTable<any>;
if (!searchObj) continue;
for (const j in searchObj) {
const ld = Utils.levenshtein(cmpTarget, j, maxLd);
if (ld <= maxLd) {
const word = searchObj[j].name || j;
const results = this.dataSearch(word, searchIn, word);
if (results) {
searchResults = results;
maxLd = ld;
}
}
}
}
return searchResults;
}
loadDataFile(basePath: string, dataType: DataType | 'Aliases'): AnyObject | void {
try {
const filePath = basePath + DATA_FILES[dataType];
const dataObject = require(filePath);
if (!dataObject || typeof dataObject !== 'object') {
throw new TypeError(`${filePath}, if it exists, must export a non-null object`);
}
if (dataObject[dataType]?.constructor?.name !== 'Object') {
throw new TypeError(`${filePath}, if it exists, must export an object whose '${dataType}' property is an Object`);
}
return dataObject[dataType];
} catch (e: any) {
if (e.code !== 'MODULE_NOT_FOUND' && e.code !== 'ENOENT') {
throw e;
}
}
}
loadTextFile(
name: string, exportName: string
): DexTable<MoveText | ItemText | AbilityText | PokedexText | DefaultText> {
return require(`${DATA_DIR}/text/${name}`)[exportName];
}
includeMods(): this {
if (!this.isBase) throw new Error(`This must be called on the base Dex`);
if (this.modsLoaded) return this;
for (const mod of fs.readdirSync(MODS_DIR)) {
dexes[mod] = new ModdedDex(mod);
}
this.modsLoaded = true;
return this;
}
includeModData(): this {
for (const mod in this.dexes) {
dexes[mod].includeData();
}
return this;
}
includeData(): this {
this.loadData();
return this;
}
loadTextData() {
if (dexes['base'].textCache) return dexes['base'].textCache;
dexes['base'].textCache = {
Pokedex: this.loadTextFile('pokedex', 'PokedexText') as DexTable<PokedexText>,
Moves: this.loadTextFile('moves', 'MovesText') as DexTable<MoveText>,
Abilities: this.loadTextFile('abilities', 'AbilitiesText') as DexTable<AbilityText>,
Items: this.loadTextFile('items', 'ItemsText') as DexTable<ItemText>,
Default: this.loadTextFile('default', 'DefaultText') as DexTable<DefaultText>,
};
return dexes['base'].textCache;
}
loadData(): DexTableData {
if (this.dataCache) return this.dataCache;
dexes['base'].includeMods();
const dataCache: {[k in keyof DexTableData]?: any} = {};
const basePath = this.dataDir + '/';
const Scripts = this.loadDataFile(basePath, 'Scripts') || {};
// We want to inherit most of Scripts but not this.
const init = Scripts.init;
this.parentMod = this.isBase ? '' : (Scripts.inherit || 'base');
let parentDex;
if (this.parentMod) {
parentDex = dexes[this.parentMod];
if (!parentDex || parentDex === this) {
throw new Error(
`Unable to load ${this.currentMod}. 'inherit' in scripts.ts should specify a parent mod from which to inherit data, or must be not specified.`
);
}
}
if (!parentDex) {
// Formats are inherited by mods and used by Rulesets
this.includeFormats();
}
for (const dataType of DATA_TYPES.concat('Aliases')) {
dataCache[dataType] = this.loadDataFile(basePath, dataType);
if (dataType === 'Rulesets' && !parentDex) {
for (const format of this.formats.all()) {
dataCache.Rulesets[format.id] = {...format, ruleTable: null};
}
}
}
if (parentDex) {
for (const dataType of DATA_TYPES) {
const parentTypedData: DexTable<any> = parentDex.data[dataType];
if (!dataCache[dataType] && !init) {
dataCache[dataType] = parentTypedData;
continue;
}
const childTypedData: DexTable<any> = dataCache[dataType] || (dataCache[dataType] = {});
for (const entryId in parentTypedData) {
if (childTypedData[entryId] === null) {
// null means don't inherit
delete childTypedData[entryId];
} else if (!(entryId in childTypedData)) {
// If it doesn't exist it's inherited from the parent data
childTypedData[entryId] = parentTypedData[entryId];
} else if (childTypedData[entryId] && childTypedData[entryId].inherit) {
// {inherit: true} can be used to modify only parts of the parent data,
// instead of overwriting entirely
delete childTypedData[entryId].inherit;
// Merge parent into children entry, preserving existing childs' properties.
childTypedData[entryId] = {...parentTypedData[entryId], ...childTypedData[entryId]};
}
}
}
dataCache['Aliases'] = parentDex.data['Aliases'];
}
// Flag the generation. Required for team validator.
this.gen = dataCache.Scripts.gen;
if (!this.gen) throw new Error(`Mod ${this.currentMod} needs a generation number in scripts.js`);
this.dataCache = dataCache as DexTableData;
// Execute initialization script.
if (init) init.call(this);
return this.dataCache;
}
includeFormats(): this {
this.formats.load();
return this;
}
}
dexes['base'] = new ModdedDex();
// "gen9" is an alias for the current base data
dexes[BASE_MOD] = dexes['base'];
export const Dex = dexes['base'];
export namespace Dex {
export type Species = import('./dex-species').Species;
export type Item = import('./dex-items').Item;
export type Move = import('./dex-moves').Move;
export type Ability = import('./dex-abilities').Ability;
export type HitEffect = import('./dex-moves').HitEffect;
export type SecondaryEffect = import('./dex-moves').SecondaryEffect;
export type RuleTable = import('./dex-formats').RuleTable;
}
export default Dex;