forked from smogon/pokemon-showdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
teams.ts
633 lines (579 loc) · 17.1 KB
/
teams.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
/**
* Teams
* Pokemon Showdown - http://pokemonshowdown.com/
*
* Functions for converting and generating teams.
*
* @license MIT
*/
import {Dex, toID} from './dex';
import type {PRNG, PRNGSeed} from './prng';
export interface PokemonSet {
/**
* Nickname. Should be identical to its base species if not specified
* by the player, e.g. "Minior".
*/
name: string;
/**
* Species name (including forme if applicable), e.g. "Minior-Red".
* This should always be converted to an id before use.
*/
species: string;
/**
* This can be an id, e.g. "whiteherb" or a full name, e.g. "White Herb".
* This should always be converted to an id before use.
*/
item: string;
/**
* This can be an id, e.g. "shieldsdown" or a full name,
* e.g. "Shields Down".
* This should always be converted to an id before use.
*/
ability: string;
/**
* Each move can be an id, e.g. "shellsmash" or a full name,
* e.g. "Shell Smash"
* These should always be converted to ids before use.
*/
moves: string[];
/**
* This can be an id, e.g. "adamant" or a full name, e.g. "Adamant".
* This should always be converted to an id before use.
*/
nature: string;
gender: string;
/**
* Effort Values, used in stat calculation.
* These must be between 0 and 255, inclusive.
*
* Also used to store AVs for Let's Go
*/
evs: StatsTable;
/**
* Individual Values, used in stat calculation.
* These must be between 0 and 31, inclusive.
*
* These are also used as DVs, or determinant values, in Gens
* 1 and 2, which are represented as even numbers from 0 to 30.
*
* In Gen 2-6, these must match the Hidden Power type.
*
* In Gen 7+, Bottle Caps means these can either match the
* Hidden Power type or 31.
*/
ivs: StatsTable;
/**
* This is usually between 1 and 100, inclusive,
* but the simulator supports levels up to 9999 for testing purposes.
*/
level: number;
/**
* While having no direct competitive effect, certain Pokemon cannot
* be legally obtained as shiny, either as a whole or with certain
* event-only abilities or moves.
*/
shiny?: boolean;
/**
* This is technically "Friendship", but the community calls this
* "Happiness".
*
* It's used to calculate the power of the moves Return and Frustration.
* This value must be between 0 and 255, inclusive.
*/
happiness?: number;
/**
* The pokeball this Pokemon is in. Like shininess, this property
* has no direct competitive effects, but has implications for
* event legality. For example, any Rayquaza that knows V-Create
* must be sent out from a Cherish Ball.
*
* TODO: actually support this in the validator, switching animations,
* and the teambuilder.
*/
pokeball?: string;
/**
* Hidden Power type. Optional in older gens, but used in Gen 7+
* because `ivs` contain post-Battle-Cap values.
*/
hpType?: string;
/**
* Dynamax Level. Affects the amount of HP gained when Dynamaxed.
* This value must be between 0 and 10, inclusive.
*/
dynamaxLevel?: number;
gigantamax?: boolean;
/**
* Tera Type
*/
teraType?: string;
}
export const Teams = new class Teams {
pack(team: PokemonSet[] | null): string {
if (!team) return '';
function getIv(ivs: StatsTable, s: keyof StatsTable): string {
return ivs[s] === 31 || ivs[s] === undefined ? '' : ivs[s].toString();
}
let buf = '';
for (const set of team) {
if (buf) buf += ']';
// name
buf += (set.name || set.species);
// species
const id = this.packName(set.species || set.name);
buf += '|' + (this.packName(set.name || set.species) === id ? '' : id);
// item
buf += '|' + this.packName(set.item);
// ability
buf += '|' + this.packName(set.ability);
// moves
buf += '|' + set.moves.map(this.packName).join(',');
// nature
buf += '|' + (set.nature || '');
// evs
let evs = '|';
if (set.evs) {
evs = '|' + (set.evs['hp'] || '') + ',' + (set.evs['atk'] || '') + ',' + (set.evs['def'] || '') + ',' + (set.evs['spa'] || '') + ',' + (set.evs['spd'] || '') + ',' + (set.evs['spe'] || '');
}
if (evs === '|,,,,,') {
buf += '|';
} else {
buf += evs;
}
// gender
if (set.gender) {
buf += '|' + set.gender;
} else {
buf += '|';
}
// ivs
let ivs = '|';
if (set.ivs) {
ivs = '|' + getIv(set.ivs, 'hp') + ',' + getIv(set.ivs, 'atk') + ',' + getIv(set.ivs, 'def') +
',' + getIv(set.ivs, 'spa') + ',' + getIv(set.ivs, 'spd') + ',' + getIv(set.ivs, 'spe');
}
if (ivs === '|,,,,,') {
buf += '|';
} else {
buf += ivs;
}
// shiny
if (set.shiny) {
buf += '|S';
} else {
buf += '|';
}
// level
if (set.level && set.level !== 100) {
buf += '|' + set.level;
} else {
buf += '|';
}
// happiness
if (set.happiness !== undefined && set.happiness !== 255) {
buf += '|' + set.happiness;
} else {
buf += '|';
}
if (set.pokeball || set.hpType || set.gigantamax ||
(set.dynamaxLevel !== undefined && set.dynamaxLevel !== 10) || set.teraType) {
buf += ',' + (set.hpType || '');
buf += ',' + this.packName(set.pokeball || '');
buf += ',' + (set.gigantamax ? 'G' : '');
buf += ',' + (set.dynamaxLevel !== undefined && set.dynamaxLevel !== 10 ? set.dynamaxLevel : '');
buf += ',' + (set.teraType || '');
}
}
return buf;
}
unpack(buf: string): PokemonSet[] | null {
if (!buf) return null;
if (typeof buf !== 'string') return buf;
if (buf.startsWith('[') && buf.endsWith(']')) {
try {
buf = this.pack(JSON.parse(buf));
} catch {
return null;
}
}
const team = [];
let i = 0;
let j = 0;
// limit to 24
for (let count = 0; count < 24; count++) {
const set: PokemonSet = {} as PokemonSet;
team.push(set);
// name
j = buf.indexOf('|', i);
if (j < 0) return null;
set.name = buf.substring(i, j);
i = j + 1;
// species
j = buf.indexOf('|', i);
if (j < 0) return null;
set.species = this.unpackName(buf.substring(i, j), Dex.species) || set.name;
i = j + 1;
// item
j = buf.indexOf('|', i);
if (j < 0) return null;
set.item = this.unpackName(buf.substring(i, j), Dex.items);
i = j + 1;
// ability
j = buf.indexOf('|', i);
if (j < 0) return null;
const ability = buf.substring(i, j);
const species = Dex.species.get(set.species);
set.ability = ['', '0', '1', 'H', 'S'].includes(ability) ?
species.abilities[ability as '0' || '0'] || (ability === '' ? '' : '!!!ERROR!!!') :
this.unpackName(ability, Dex.abilities);
i = j + 1;
// moves
j = buf.indexOf('|', i);
if (j < 0) return null;
set.moves = buf.substring(i, j).split(',', 24).map(name => this.unpackName(name, Dex.moves));
i = j + 1;
// nature
j = buf.indexOf('|', i);
if (j < 0) return null;
set.nature = this.unpackName(buf.substring(i, j), Dex.natures);
i = j + 1;
// evs
j = buf.indexOf('|', i);
if (j < 0) return null;
if (j !== i) {
const evs = buf.substring(i, j).split(',', 6);
set.evs = {
hp: Number(evs[0]) || 0,
atk: Number(evs[1]) || 0,
def: Number(evs[2]) || 0,
spa: Number(evs[3]) || 0,
spd: Number(evs[4]) || 0,
spe: Number(evs[5]) || 0,
};
}
i = j + 1;
// gender
j = buf.indexOf('|', i);
if (j < 0) return null;
if (i !== j) set.gender = buf.substring(i, j);
i = j + 1;
// ivs
j = buf.indexOf('|', i);
if (j < 0) return null;
if (j !== i) {
const ivs = buf.substring(i, j).split(',', 6);
set.ivs = {
hp: ivs[0] === '' ? 31 : Number(ivs[0]) || 0,
atk: ivs[1] === '' ? 31 : Number(ivs[1]) || 0,
def: ivs[2] === '' ? 31 : Number(ivs[2]) || 0,
spa: ivs[3] === '' ? 31 : Number(ivs[3]) || 0,
spd: ivs[4] === '' ? 31 : Number(ivs[4]) || 0,
spe: ivs[5] === '' ? 31 : Number(ivs[5]) || 0,
};
}
i = j + 1;
// shiny
j = buf.indexOf('|', i);
if (j < 0) return null;
if (i !== j) set.shiny = true;
i = j + 1;
// level
j = buf.indexOf('|', i);
if (j < 0) return null;
if (i !== j) set.level = parseInt(buf.substring(i, j));
i = j + 1;
// happiness
j = buf.indexOf(']', i);
let misc;
if (j < 0) {
if (i < buf.length) misc = buf.substring(i).split(',', 6);
} else {
if (i !== j) misc = buf.substring(i, j).split(',', 6);
}
if (misc) {
set.happiness = (misc[0] ? Number(misc[0]) : 255);
set.hpType = misc[1] || '';
set.pokeball = this.unpackName(misc[2] || '', Dex.items);
set.gigantamax = !!misc[3];
set.dynamaxLevel = (misc[4] ? Number(misc[4]) : 10);
set.teraType = misc[5];
}
if (j < 0) break;
i = j + 1;
}
return team;
}
/** Very similar to toID but without the lowercase conversion */
packName(name: string | undefined | null) {
if (!name) return '';
return name.replace(/[^A-Za-z0-9]+/g, '');
}
/** Will not entirely recover a packed name, but will be a pretty readable guess */
unpackName(name: string, dexTable?: {get: (name: string) => AnyObject}) {
if (!name) return '';
if (dexTable) {
const obj = dexTable.get(name);
if (obj.exists) return obj.name;
}
return name.replace(/([0-9]+)/g, ' $1 ').replace(/([A-Z])/g, ' $1').replace(/[ ][ ]/g, ' ').trim();
}
/**
* Exports a team in human-readable PS export format
*/
export(team: PokemonSet[], options?: {hideStats?: boolean}) {
let output = '';
for (const set of team) {
output += this.exportSet(set, options) + `\n`;
}
return output;
}
exportSet(set: PokemonSet, {hideStats}: {hideStats?: boolean} = {}) {
let out = ``;
// core
if (set.name && set.name !== set.species) {
out += `${set.name} (${set.species})`;
} else {
out += set.species;
}
if (set.gender === 'M') out += ` (M)`;
if (set.gender === 'F') out += ` (F)`;
if (set.item) out += ` @ ${set.item}`;
out += ` \n`;
if (set.ability) {
out += `Ability: ${set.ability} \n`;
}
// details
if (set.level && set.level !== 100) {
out += `Level: ${set.level} \n`;
}
if (set.shiny) {
out += `Shiny: Yes \n`;
}
if (typeof set.happiness === 'number' && set.happiness !== 255 && !isNaN(set.happiness)) {
out += `Happiness: ${set.happiness} \n`;
}
if (set.pokeball) {
out += `Pokeball: ${set.pokeball} \n`;
}
if (set.hpType) {
out += `Hidden Power: ${set.hpType} \n`;
}
if (typeof set.dynamaxLevel === 'number' && set.dynamaxLevel !== 10 && !isNaN(set.dynamaxLevel)) {
out += `Dynamax Level: ${set.dynamaxLevel} \n`;
}
if (set.gigantamax) {
out += `Gigantamax: Yes \n`;
}
if (set.teraType) {
out += `Tera Type: ${set.teraType} \n`;
}
// stats
if (!hideStats) {
if (set.evs) {
const stats = Dex.stats.ids().map(
stat => set.evs[stat] ?
`${set.evs[stat]} ${Dex.stats.shortNames[stat]}` : ``
).filter(Boolean);
if (stats.length) {
out += `EVs: ${stats.join(" / ")} \n`;
}
}
if (set.nature) {
out += `${set.nature} Nature \n`;
}
if (set.ivs) {
const stats = Dex.stats.ids().map(
stat => (set.ivs[stat] !== 31 && set.ivs[stat] !== undefined) ?
`${set.ivs[stat] || 0} ${Dex.stats.shortNames[stat]}` : ``
).filter(Boolean);
if (stats.length) {
out += `IVs: ${stats.join(" / ")} \n`;
}
}
}
// moves
for (let move of set.moves) {
if (move.startsWith(`Hidden Power `) && move.charAt(13) !== '[') {
move = `Hidden Power [${move.slice(13)}]`;
}
out += `- ${move} \n`;
}
return out;
}
parseExportedTeamLine(line: string, isFirstLine: boolean, set: PokemonSet) {
if (isFirstLine) {
let item;
[line, item] = line.split(' @ ');
if (item) {
set.item = item;
if (toID(set.item) === 'noitem') set.item = '';
}
if (line.endsWith(' (M)')) {
set.gender = 'M';
line = line.slice(0, -4);
}
if (line.endsWith(' (F)')) {
set.gender = 'F';
line = line.slice(0, -4);
}
if (line.endsWith(')') && line.includes('(')) {
const [name, species] = line.slice(0, -1).split('(');
set.species = Dex.species.get(species).name;
set.name = name.trim();
} else {
set.species = Dex.species.get(line).name;
set.name = '';
}
} else if (line.startsWith('Trait: ')) {
line = line.slice(7);
set.ability = line;
} else if (line.startsWith('Ability: ')) {
line = line.slice(9);
set.ability = line;
} else if (line === 'Shiny: Yes') {
set.shiny = true;
} else if (line.startsWith('Level: ')) {
line = line.slice(7);
set.level = +line;
} else if (line.startsWith('Happiness: ')) {
line = line.slice(11);
set.happiness = +line;
} else if (line.startsWith('Pokeball: ')) {
line = line.slice(10);
set.pokeball = line;
} else if (line.startsWith('Hidden Power: ')) {
line = line.slice(14);
set.hpType = line;
} else if (line.startsWith('Tera Type: ')) {
line = line.slice(11);
set.teraType = line;
} else if (line === 'Gigantamax: Yes') {
set.gigantamax = true;
} else if (line.startsWith('EVs: ')) {
line = line.slice(5);
const evLines = line.split('/');
set.evs = {hp: 0, atk: 0, def: 0, spa: 0, spd: 0, spe: 0};
for (const evLine of evLines) {
const [statValue, statName] = evLine.trim().split(' ');
const statid = Dex.stats.getID(statName);
if (!statid) continue;
const value = parseInt(statValue);
set.evs[statid] = value;
}
} else if (line.startsWith('IVs: ')) {
line = line.slice(5);
const ivLines = line.split('/');
set.ivs = {hp: 31, atk: 31, def: 31, spa: 31, spd: 31, spe: 31};
for (const ivLine of ivLines) {
const [statValue, statName] = ivLine.trim().split(' ');
const statid = Dex.stats.getID(statName);
if (!statid) continue;
let value = parseInt(statValue);
if (isNaN(value)) value = 31;
set.ivs[statid] = value;
}
} else if (/^[A-Za-z]+ (N|n)ature/.test(line)) {
let natureIndex = line.indexOf(' Nature');
if (natureIndex === -1) natureIndex = line.indexOf(' nature');
if (natureIndex === -1) return;
line = line.substr(0, natureIndex);
if (line !== 'undefined') set.nature = line;
} else if (line.startsWith('-') || line.startsWith('~')) {
line = line.slice(line.charAt(1) === ' ' ? 2 : 1);
if (line.startsWith('Hidden Power [')) {
const hpType = line.slice(14, -1);
line = 'Hidden Power ' + hpType;
if (!set.ivs && Dex.types.isName(hpType)) {
set.ivs = {hp: 31, atk: 31, def: 31, spa: 31, spd: 31, spe: 31};
const hpIVs = Dex.types.get(hpType).HPivs || {};
for (const statid in hpIVs) {
set.ivs[statid as StatID] = hpIVs[statid as StatID]!;
}
}
}
if (line === 'Frustration' && set.happiness === undefined) {
set.happiness = 0;
}
set.moves.push(line);
}
}
/** Accepts a team in any format (JSON, packed, or exported) */
import(buffer: string): PokemonSet[] | null {
if (buffer.startsWith('[')) {
try {
const team = JSON.parse(buffer);
if (!Array.isArray(team)) throw new Error(`Team should be an Array but isn't`);
for (const set of team) {
set.name = Dex.getName(set.name);
set.species = Dex.getName(set.species);
set.item = Dex.getName(set.item);
set.ability = Dex.getName(set.ability);
set.gender = Dex.getName(set.gender);
set.nature = Dex.getName(set.nature);
const evs = {hp: 0, atk: 0, def: 0, spa: 0, spd: 0, spe: 0};
if (set.evs) {
for (const statid in evs) {
if (typeof set.evs[statid] === 'number') evs[statid as StatID] = set.evs[statid];
}
}
set.evs = evs;
const ivs = {hp: 31, atk: 31, def: 31, spa: 31, spd: 31, spe: 31};
if (set.ivs) {
for (const statid in ivs) {
if (typeof set.ivs[statid] === 'number') ivs[statid as StatID] = set.ivs[statid];
}
}
set.ivs = ivs;
if (!Array.isArray(set.moves)) {
set.moves = [];
} else {
set.moves = set.moves.map(Dex.getName);
}
}
return team;
} catch {}
}
const lines = buffer.split("\n");
const sets: PokemonSet[] = [];
let curSet: PokemonSet | null = null;
while (lines.length && !lines[0]) lines.shift();
while (lines.length && !lines[lines.length - 1]) lines.pop();
if (lines.length === 1 && lines[0].includes('|')) {
return this.unpack(lines[0]);
}
for (let line of lines) {
line = line.trim();
if (line === '' || line === '---') {
curSet = null;
} else if (line.startsWith('===')) {
// team backup format; ignore
} else if (!curSet) {
curSet = {
name: '', species: '', item: '', ability: '', gender: '',
nature: '',
evs: {hp: 0, atk: 0, def: 0, spa: 0, spd: 0, spe: 0},
ivs: {hp: 31, atk: 31, def: 31, spa: 31, spd: 31, spe: 31},
level: 100,
moves: [],
};
sets.push(curSet);
this.parseExportedTeamLine(line, true, curSet);
} else {
this.parseExportedTeamLine(line, false, curSet);
}
}
return sets;
}
getGenerator(format: Format | string, seed: PRNG | PRNGSeed | null = null) {
let TeamGenerator;
if (toID(format).includes('gen9computergeneratedteams')) {
TeamGenerator = require(Dex.forFormat(format).dataDir + '/cg-teams').default;
} else {
TeamGenerator = require(Dex.forFormat(format).dataDir + '/random-teams').default;
}
return new TeamGenerator(format, seed);
}
generate(format: Format | string, options: PlayerOptions | null = null): PokemonSet[] {
return this.getGenerator(format, options?.seed).getTeam(options);
}
};
export default Teams;