-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.ts
845 lines (757 loc) · 29.5 KB
/
state.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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
/* eslint-disable no-shadow */
import type {
BoostsTable,
Move as DMove,
GameType,
GenderName,
Generation,
ID,
NatureName,
PokemonSet,
Specie,
StatID,
StatsTable,
StatusName,
Type,
TypeName,
} from '@pkmn/data';
import {ConditionKind, Conditions, TerrainName, WeatherName} from './conditions';
import {floor, round} from './math';
import {DeepPartial, extend, has, is, toID} from './utils';
type OverriddenFields = 'item' | 'ability' | 'nature' | 'status' | 'volatiles' | 'ivs' | 'evs' | 'boosts';
export interface PokemonOptions extends Partial<Omit<State.Pokemon, OverriddenFields>> {
name?: string;
weightkg?: number;
item?: string;
ability?: string;
nature?: string;
status?: string;
hpPercent?: number;
volatiles?: string[] | State.Pokemon['volatiles'];
evs?: Partial<StatsTable & {spc: number}>;
ivs?: Partial<StatsTable & {spc: number}>;
dvs?: Partial<StatsTable & {spc: number}>;
boosts?: Partial<BoostsTable & {spc: number}>;
teraType?: TypeName;
}
export interface MoveOptions {
name?: string;
basePower?: number;
crit?: boolean;
hits?: number;
magnitude?: number;
consecutive?: number;
spread?: boolean;
useZ?: boolean;
}
export interface FieldOptions {
weather?: string;
terrain?: string;
pseudoWeather?: string[] | State.Field['pseudoWeather'];
}
export interface SideOptions {
sideConditions?: string[] | State.Side['sideConditions'];
abilities?: string[];
atks?: number[];
}
// Moves can include a base power override in their name, eg. "Present 80" or a "Z-" prefix
export const MOVE_SUGAR = /^\s*(Z\s*-?\s*)?(\D*)(\d+)?\s*$/i;
/**
* The external API representing the state of the battle used as input to the calculator. Internally
* this gets transformed into a `Context` during computation, but all interfaces map fairly closely
* to the shape of these interfaces. These interfaces were constructed to map closely to the same
* concepts in Pokémon Showdown for easy interoperability. Most users should use the `create` helper
* methods or simply `parse` the `State` from a string representation.
*/
export class State {
readonly gameType: GameType;
readonly gen: Generation;
readonly p1: State.Side;
readonly p2: State.Side;
readonly move: State.Move;
readonly field: State.Field;
constructor(
gen: Generation,
attacker: State.Side | State.Pokemon,
defender: State.Side | State.Pokemon,
move: State.Move,
field: State.Field = {pseudoWeather: {}},
gameType: GameType = 'singles'
) {
this.gameType = gameType;
this.gen = gen;
this.p1 = 'pokemon' in attacker ? attacker : {pokemon: attacker, sideConditions: {}};
this.p2 = 'pokemon' in defender ? defender : {pokemon: defender, sideConditions: {}};
this.move = move;
this.field = field;
}
/** Serializes State, collapsing immutable data structures that have circular references. */
static toJSON(s: State) {
const move = {} as any;
const base = s.gen.moves.get(s.move.name);
for (const key in s.move) {
if (Object.prototype.hasOwnProperty.call(s.move, key)) {
const val = s.move[key as keyof State.Move];
if (key === 'name' || !base || !(key in base) || val !== base[key as keyof DMove]) {
move[key] = val;
}
}
}
return {
gen: s.gen.num,
gameType: s.gameType,
p1: {
...s.p1,
pokemon: {...s.p1.pokemon, species: s.p1.pokemon.species.name},
},
p2: {
...s.p2,
pokemon: {...s.p2.pokemon, species: s.p2.pokemon.species.name},
},
move,
field: s.field,
};
}
/** Factory method helper for creating `State.Field`. */
static createField(gen: Generation, options: FieldOptions = {}) {
const field: Partial<State.Field> = {};
if (options.weather) {
const c = Conditions.get(gen, options.weather);
if (!c) invalid(gen, 'weather', options.weather);
field.weather = c[0] as WeatherName;
}
if (options.terrain) {
const c = Conditions.get(gen, options.terrain);
if (!c) invalid(gen, 'terrain', options.terrain);
field.terrain = c[0] as TerrainName;
}
field.pseudoWeather = setConditions(gen, 'Pseudo Weather', options.pseudoWeather);
return field as State.Field;
}
/** Factory method helper for creating `State.Side`. */
static createSide(gen: Generation, pokemon: State.Pokemon, options: SideOptions = {}) {
return {
sideConditions: setConditions(gen, 'Side Condition', options.sideConditions),
pokemon,
active: options.abilities?.map((name, i) => {
const ability = gen.abilities.get(name);
if (!ability) invalid(gen, 'ability', name);
return {ability: ability.id, position: i};
}),
team: options.atks?.map((atk, i) => ({
species: {baseStats: {atk: bounded('stat', atk)}},
position: i,
})),
} as State.Side;
}
/** Factory method helper for creating `State.Pokemon`. */
static createPokemon(gen: Generation, name: string, options: PokemonOptions = {}, move: string | {name?: string} = '') {
const pokemon: Partial<State.Pokemon> = {};
// Species
const species = gen.species.get(name);
if (!species) invalid(gen, 'species', name);
if (options.species && options.species !== species) {
throw new Error(`Species mismatch: ${options.species.name} does not match ${species.name}`);
}
pokemon.species = species;
// Level
pokemon.level = 100;
if (typeof options.level === 'number') {
pokemon.level = bounded('level', options.level);
}
// Weight
pokemon.weighthg =
typeof options.weighthg === 'number' ? options.weighthg : typeof options.weightkg === 'number' ? options.weightkg * 10 : species.weighthg;
if (pokemon.weighthg < 1) throw new Error(`weighthg of ${pokemon.weighthg} must be at least 1`);
// Item
pokemon.item = undefined;
setItem(gen, pokemon, options.item);
// Ability
pokemon.ability = undefined;
setAbility(gen, pokemon as {species: Specie; ability?: ID}, options.ability);
// Happiness
pokemon.happiness = typeof options.happiness === 'undefined' ? undefined : bounded('happiness', options.happiness);
// Status
pokemon.status = undefined;
pokemon.statusState = undefined;
if (options.status) {
const condition = Conditions.get(gen, options.status);
if (!condition) invalid(gen, 'status', options.status);
const [status, kind] = condition;
if (kind !== 'Status') {
throw new Error(`'${status} is a ${kind} not a Status in generation ${gen.num}`);
}
pokemon.status = status as StatusName;
if (pokemon.status === 'tox') pokemon.statusState = {toxicTurns: 0};
}
// Status Data
if (options.statusState) {
if (options.statusState.toxicTurns) {
const turns = options.statusState.toxicTurns;
bounded('toxicCounter', turns);
if (pokemon.status !== 'tox') {
throw new Error(`toxicTurns set to ${turns} but the Pokemon's status is not 'tox'`);
}
}
pokemon.statusState = options.statusState;
}
// Volatiles
pokemon.volatiles = setConditions(gen, 'Volatile Status', options.volatiles);
// Types
pokemon.types = options.types || pokemon.species.types;
pokemon.addedType = options.addedType;
// Nature
pokemon.nature = undefined;
setNature(gen, pokemon, options.nature);
// EVs
setValues(gen, pokemon, 'evs', options.evs);
// IVs / DVs
setValues(gen, pokemon, 'ivs', options.ivs);
for (const stat of gen.stats) {
const val = options.dvs?.[stat];
if (typeof val === 'number') {
const dv = bounded('dvs', val);
if (typeof options.ivs?.[stat] === 'number' && gen.stats.toDV(options.ivs[stat]) !== dv) {
throw new Error(`${stat} DV of '${dv}' does not match IV of '${options.ivs[stat]}'`);
}
pokemon.ivs![stat] = gen.stats.toIV(dv);
}
}
setSpc(gen, pokemon.ivs!, 'ivs', options.dvs, gen.stats.toIV.bind(gen.stats));
if (move) {
move = typeof move === 'string' ? move : move.name || '';
setHiddenPowerIVs(gen, pokemon as {level: number; ivs: StatsTable}, [move]);
}
// Stats
if (options.stats) pokemon.stats = extend({}, options.stats);
// Boosts
pokemon.boosts = {};
if (options.boosts) {
for (const b in options.boosts) {
if (b === 'spc') continue;
const boost = b as keyof BoostsTable;
const val = options.boosts[boost];
if (typeof val === 'number') pokemon.boosts[boost] = bounded('boosts', val);
}
}
setSpc(gen, pokemon.boosts, 'boosts', options.boosts);
// Gender (depends on DVs)
const setAtkDV = typeof (options.dvs?.atk ?? options.ivs?.atk) === 'number';
setGender(gen, pokemon as {species: Specie; ivs: StatsTable; gender?: GenderName}, options.gender, setAtkDV);
// HP (depends on stats)
const setHPDV = typeof (options.dvs?.hp ?? options.ivs?.hp) === 'number';
correctHPDV(gen, pokemon as {species: Specie; ivs: StatsTable}, setHPDV);
pokemon.maxhp = gen.stats.calc('hp', species.baseStats.hp, pokemon.ivs!.hp, pokemon.evs!.hp, pokemon.level);
if (options.maxhp) {
if (options.maxhp < pokemon.maxhp) {
throw new RangeError(`maxhp ${options.maxhp} less than calculated max HP ${pokemon.maxhp}`);
}
pokemon.maxhp = options.maxhp;
}
//Tera Type
pokemon.teraType = pokemon.types[0];
const computed = typeof options.hpPercent === 'number' ? round((options.hpPercent * pokemon.maxhp) / 100) : undefined;
pokemon.hp = typeof options.hp === 'number' ? options.hp : typeof computed === 'number' ? computed : pokemon.maxhp;
if (!(pokemon.hp >= 0 && pokemon.hp <= pokemon.maxhp)) {
throw new RangeError(`hp ${pokemon.hp} is not within [0,${pokemon.maxhp}]`);
}
if (typeof options.hp === 'number' && typeof computed === 'number') {
if (pokemon.hp !== computed) {
throw new Error(`hp mismatch: '${computed}' does not match '${pokemon.hp}'`);
}
}
// Miscellaneous
pokemon.position = options.position;
pokemon.switching = options.switching;
pokemon.moveLastTurnResult = options.moveLastTurnResult;
pokemon.hurtThisTurn = options.hurtThisTurn;
return validateStats(gen, pokemon as State.Pokemon);
}
/** Factory method helper for creating `State.Move`. */
static createMove(
gen: Generation,
name: string,
options: MoveOptions = {},
pokemon:
| string
| {
species?: string | Specie;
item?: string;
ability?: string;
volatiles?: {[id: string]: object};
} = {}
) {
let base = gen.moves.get(name);
if (!base) {
const m = MOVE_SUGAR.exec(name);
if (m) {
base = gen.moves.get(m[2]);
if (m[1]) {
const useZ = !!m[1];
if (options.useZ !== undefined && options.useZ !== useZ) {
throw new Error(`useZ mismatch: '${options.useZ}' does not match '${useZ}'`);
}
options.useZ = useZ;
}
if (m[3]) {
const n = Number(m[3]);
if (base?.id === 'conversion' && n === 2) {
base = gen.moves.get('Conversion 2');
} else if (base?.id === 'magnitude' && n >= 4 && n <= 10) {
if (options.magnitude && options.magnitude !== n) {
throw new Error(`Magnitude mismatch: '${options.magnitude}' does not match '${n}'`);
}
options.magnitude = n;
} else {
if (options.basePower && options.basePower !== n) {
throw new Error(`Base Power mismatch: '${options.basePower}' does not match '${n}'`);
}
options.basePower = n;
}
}
}
if (!base) invalid(gen, 'move', name);
}
if (options.name && !(toID(options.name) === base.id || toID(options.name) === toID(name))) {
throw new Error(`Move mismatch: '${options.name}' does not match '${base.name}'`);
}
// Avoid overwriting the actual move when we extend below
options.name = base.name;
const move: Partial<State.Move> = {hits: 1};
if (typeof pokemon === 'string') {
const species = gen.species.get(pokemon);
if (!species) invalid(gen, 'species', pokemon);
pokemon = {species};
} else if (typeof pokemon.species === 'string') {
const species = gen.species.get(pokemon.species);
if (!species) invalid(gen, 'species', pokemon.species);
pokemon.species = species;
}
const useMax = pokemon.volatiles?.dynamax;
if (useMax && options.useZ) {
throw new Error(`Cannot use ${base.name} as both a Z-Move and a Max Move simulataneously`);
}
if (useMax || move.isMax) {
if (options.hits && options.hits > 1) {
throw new Error(`'${options.hits}' hits requested but Max Moves cannot be multi-hit`);
}
} else if (options.useZ || move.isZ) {
if (options.hits && options.hits > 1) {
throw new Error(`'${options.hits}' hits requested but Z-Moves cannot be multi-hit`);
}
} else {
if (base.multihit) {
if (typeof base.multihit === 'number') {
move.hits = base.multihit;
} else if (options.hits) {
move.hits = options.hits;
} else {
move.hits = pokemon?.ability === 'Skill Link' || pokemon?.item === 'Grip Claw' ? base.multihit[1] : base.multihit[0] + 1;
}
} else if (options.hits && options.hits !== 1) {
throw new Error(`'${options.hits}' hits requested but ${base.name} is not multi-hit`);
}
}
extend(move, base, options); // whatever, there are too many move fields
// If @pkmn/sim is used as the backing Dex, extend might copy over the simulators handlers which
// conflict with our own so we iterate through the top level keys (which are the only handlers
// which could conflict) and yeet any which are functions
for (const k in move) {
if (typeof k === 'function') delete move[k];
}
move.crit = options.crit ?? base.willCrit;
if (typeof options.magnitude === 'number') {
if (move.id !== 'magnitude') {
throw new Error(`magnitude ${options.magnitude} incorrectly set on move '${base.name}'`);
}
move.magnitude = bounded('magnitude', options.magnitude);
}
if (move.id === 'magnitude' && !(move.magnitude || options.useZ)) {
throw new Error('The move Magnitude must have a magnitude specified');
}
if (gen.num < 3 && options.spread) {
throw new Error(`Spread moves do not exist in generation ${gen.num}`);
} else {
move.spread = options.spread;
}
if (options.consecutive && toID(pokemon.item) !== 'metronome') {
throw new Error('consecutive has no meaning unless the Pokemon is holding a Metronome');
}
move.consecutive = options.consecutive;
return move as State.Move;
}
/** Mutates `pokemon` by merging in the details from the set from `sets` which best matches. */
static mergeSet(gen: Generation, pokemon: State.Pokemon, move: string | DeepPartial<PokemonSet>, ...sets: DeepPartial<PokemonSet>[]) {
const set = bestMatch(pokemon, move, ...sets);
pokemon.level = bounded('level', set.level || 100);
setItem(gen, pokemon, set.item);
setAbility(gen, pokemon, set.ability);
if (set.happiness !== undefined) pokemon.happiness = bounded('happiness', set.happiness);
// Nature is required on PokemonSet, so we just ignore it for generations 1 and 2
setNature(gen, pokemon, set.nature, gen.num >= 3);
if (set.evs) setValues(gen, pokemon, 'evs', gen.stats.fill(set.evs, gen.num <= 2 ? 252 : 0));
if (set.ivs) setValues(gen, pokemon, 'ivs', gen.stats.fill(set.ivs, 31));
setHiddenPowerIVs(gen, pokemon as {level: number; ivs: StatsTable}, [typeof move === 'string' ? move : '', ...(set.moves ?? [])], true);
if (
// Marowak hack, cribbed from Pokémon Showdown's sim/team-validator.ts
gen.num === 2 &&
pokemon.species.id === 'marowak' &&
is(pokemon.item, 'thickclub') &&
has(set.moves?.map(toID), 'swordsdance') &&
pokemon.level === 100
) {
const ivs = (pokemon.ivs!.atk = gen.stats.toDV(pokemon.ivs!.atk!) * 2);
while (pokemon.evs!.atk! > 0 && 2 * 80 + ivs + floor(pokemon.evs!.atk! / 4) + 5 > 255) {
pokemon.evs!.atk! -= 4;
}
}
// Shiny
const dv = (stat: StatID) => gen.stats.toDV(pokemon.ivs![stat]!);
const shiny = !!(dv('def') === 10 && dv('spe') === 10 && dv('spa') === 10 && dv('atk') % 4 >= 2);
if (gen.num === 2 && shiny !== !!set.shiny) {
throw new Error(`${pokemon.species.name} is required to ${shiny ? '' : 'not '}be ` + 'shiny in generation 2 given its DVs.');
}
setGender(gen, pokemon as {species: Specie; ivs: StatsTable; gender?: GenderName}, set.gender as GenderName, typeof set.ivs?.atk === 'number');
correctHPDV(gen, pokemon as {species: Specie; ivs: StatsTable}, typeof set.ivs?.hp === 'number');
// We can't validate HP here, but we can attempt to preserve the same percentage
// of health while adjusting the HP values to be legal.
const maxhp = gen.stats.calc('hp', pokemon.species.baseStats.hp, pokemon.ivs!.hp, pokemon.evs!.hp, pokemon.level);
pokemon.hp = floor((maxhp * pokemon.hp) / pokemon.maxhp);
pokemon.maxhp = maxhp;
return validateStats(gen, pokemon);
}
}
export namespace State {
export interface Field {
weather?: WeatherName;
terrain?: TerrainName;
pseudoWeather: {[id: string]: {level?: number}};
}
export interface Side {
pokemon: Pokemon;
sideConditions: {[id: string]: {level?: number}};
// Rarely useful, but required for ally-affecting abilities like Plus/Minus or Fairy Aura etc.
// Must be a subset of State.Pokemon so that a State.Pokemon object could be used in this array
active?: Array<{
ability?: ID;
// Used to differentiate the attacker/defender from their allies if they are also
// included in the active array
position?: number;
// Used to exclude allies which are fainted (alternatively they can just be left
// out of the active array)
fainted?: boolean;
} | null>;
// Similarly niche, Beat Up etc requires information about the entire team. Must be a subset
// of State.Pokemon so that a State.Pokemon object could be used in this array
team?: Array<{
// Fields required for Beat Up mechanics
species: {baseStats: {atk: number}};
// Fields used to exclude certain team members per the mechanics
status?: StatusName;
fainted?: boolean;
// Fields used to exclude the attacker/defender if they are included in the array
position?: number;
}>;
}
export interface Pokemon {
species: Specie;
level: number;
// Hectograms is stored instead of the more user-friendly and intuitive kilograms because the
// game considers weights down to hectogram precision when modified
weighthg: number;
item?: ID;
ability?: ID;
gender?: GenderName;
// Only relevant for submaximal happiness based move calculations - if excluded the move will be
// treated as max power (eg. 0 happiness for Frustration, 255 for Return etc)
happiness?: number;
status?: StatusName;
statusState?: {toxicTurns?: number};
// Level is used to track layers/stockpiles/etc
volatiles: {[id: string]: {level?: number}};
types: [TypeName] | [TypeName, TypeName];
// Type added by Trick-or-Treat/Forest's Curse etc
addedType?: TypeName;
teraType?: TypeName;
// Base max HP is stats.hp, but max HP may change due to Dynamaxing or Power Contruct etc
maxhp: number;
hp: number;
nature?: NatureName;
evs?: Partial<StatsTable>;
ivs?: Partial<StatsTable>;
// Computed stats can be provided directly, but without spread information the result
// description will be limited. This option exists primarly as an optimization for @pkmn/gmd
// and other programmatic use cases.
stats?: StatsTable;
boosts: Partial<BoostsTable>;
// Use to disambiguate this Pokemon from its allies if included in the Side's active or party
position?: number;
// Required for certain moves which effect switches, the most obvious being Pursuit
switching?: 'in' | 'out';
// Required for Stomping Tantrum
moveLastTurnResult?: unknown;
// Required for Assurance
hurtThisTurn?: unknown;
}
export interface Move extends DMove {
crit?: boolean;
hits?: number;
magnitude?: number;
spread?: boolean;
consecutive?: number;
useZ?: boolean;
}
}
const BOUNDS: {[key: string]: [number, number]} = {
level: [1, 100],
stat: [0, 255],
evs: [0, 255],
ivs: [0, 31],
dvs: [0, 15],
gen: [1, 8],
boosts: [-6, 6],
toxicCounter: [0, 15],
happiness: [0, 255],
magnitude: [4, 10],
};
function invalid(gen: Generation, k: string, v: any): never {
throw new Error(`Unsupported or invalid ${k} '${v}' for generation ${gen.num}`);
}
/** Ensures `val` falls within the range of the attribute indicated by `key`. */
export function bounded(key: keyof typeof BOUNDS, val: number, die = true) {
const ok = val >= BOUNDS[key][0] && val <= BOUNDS[key][1];
if (!ok && die) throw new RangeError(`${key} ${val} is not within [${BOUNDS[key].join(',')}]`);
return val;
}
// Naive algorithm for determing the 'best' matching set to the `pokemon` (with optional `move`) -
// iterate through each set and assign it a score based on how many fields match vs. how many
// conflict (with the score being unaffected if the field is not set) and then return the highest
// scoring set, breaking ties by using the ordering of the sets provided. This works well enough
// in a pinch, but could be improved by valuing certain fields more than others or eg. relying on
// a set clustering similarity metric.
function bestMatch(pokemon: State.Pokemon, move: string | DeepPartial<PokemonSet>, ...sets: DeepPartial<PokemonSet>[]) {
if (typeof move !== 'string') {
sets.unshift(move);
move = '';
}
const match = (a?: string, b?: string) => toID(a) === toID(b);
const scored: Array<[number, DeepPartial<PokemonSet>]> = [];
for (const set of sets) {
let score = 0;
if (!match(set.species, pokemon.species.name)) {
throw new Error(`Received invalid ${set.species} set for ${pokemon.species.name}`);
}
if (set.level !== pokemon.level) score--;
if (!match(set.item, pokemon.item)) {
score--;
} else if (pokemon.item) {
score++;
}
if (!match(set.ability, pokemon.ability)) {
score--;
} else if (pokemon.ability) {
score++;
}
if (!match(set.nature, pokemon.nature)) {
score--;
} else if (pokemon.nature) {
score++;
}
if (move && !set.moves?.some(m => match(m, move))) {
score--;
} else if (move) {
score++;
}
scored.push([score, set]);
}
if (!scored.length) throw new Error(`Received no sets for ${pokemon.species.name}`);
let best: [number, DeepPartial<PokemonSet>] = scored[0];
for (let i = 1; i < scored.length; i++) {
if (scored[i][0] > best[0]) best = scored[i];
}
return best[1];
}
function setItem(gen: Generation, pokemon: Partial<State.Pokemon>, name?: string) {
if (name) {
const item = gen.items.get(name);
if (!item) invalid(gen, 'item', name);
pokemon.item = item.id;
}
}
function setAbility(gen: Generation, pokemon: {species: Specie; ability?: ID}, name?: string) {
if (name) {
const ability = gen.abilities.get(name);
if (!ability) invalid(gen, 'ability', name);
pokemon.ability = ability.id;
} else if (gen.num >= 3) {
pokemon.ability = toID(pokemon.species.abilities[0]);
}
}
function setNature(gen: Generation, pokemon: Partial<State.Pokemon>, name?: string, die = true) {
if (name) {
const nature = gen.natures.get(name);
if (!nature) {
if (die) invalid(gen, 'nature', name);
} else {
pokemon.nature = nature.name;
}
}
}
function setValues(gen: Generation, pokemon: Partial<State.Pokemon>, type: 'evs' | 'ivs', vals?: Partial<StatsTable & {spc: number}>) {
pokemon[type] = pokemon[type] || {};
for (const stat of gen.stats) {
pokemon[type][stat] = pokemon[type][stat] ?? (type === 'evs' ? (gen.num <= 2 ? 252 : 0) : 31);
const val = vals?.[stat];
if (typeof val === 'number') pokemon[type][stat] = bounded(type, val);
}
setSpc(gen, pokemon[type], type, vals);
}
function setSpc(
gen: Generation,
existing: Partial<{spc: number; spa: number; spd: number}>,
type: 'evs' | 'ivs' | 'boosts',
vals?: Partial<{spc: number; spa: number; spd: number}>,
fn?: (n: number) => number
) {
const spc = vals?.spc;
if (typeof spc === 'number') {
if (gen.num >= 2) throw new Error('Spc does not exist after generation 1');
if (typeof vals!.spa === 'number' && vals!.spa !== spc) {
throw new Error(`Spc and SpA ${type} mismatch: ${spc} vs. ${vals!.spa}`);
}
if (typeof vals!.spd === 'number' && vals!.spd !== spc) {
throw new Error(`Spc and SpD ${type} mismatch: ${spc} vs. ${vals!.spd}`);
}
existing.spa = existing.spd = bounded(type, fn ? fn(spc) : spc);
}
if (gen.num <= 2 && existing.spa !== existing.spd) {
throw new Error(`SpA and SpD ${type} must match before generation 3`);
}
}
export function setGender(gen: Generation, pokemon: {species: Specie; ivs: StatsTable; gender?: GenderName}, name?: GenderName, setAtkDV = false) {
const ivs = pokemon.ivs;
const species = pokemon.species;
const atkDV = gen.stats.toDV(ivs.atk);
// AtkDV determing gender is only at thing in generation 2, but we can use it as the default
const gender = gen.num === 1 ? undefined : atkDV >= species.genderRatio.F * 16 ? 'M' : 'F';
if (name) {
if (gen.num === 1) throw new Error('Gender does not exist in generation 1');
if (species.gender && name !== species.gender) {
throw new Error(`${species.name} must be '${species.gender}' in generation ${gen.num}`);
}
if (gen.num === 2) {
if (setAtkDV && name !== gender) {
throw new Error(`A ${species.name} with ${atkDV} Atk DVs must be '${gender}' in gen 2`);
}
pokemon.gender = gender;
return;
}
}
pokemon.gender = name || species.gender || gender;
}
function correctHPDV(gen: Generation, pokemon: {species: Specie; ivs: StatsTable}, setHPDV = false) {
const expectedHPDV = gen.stats.getHPDV(pokemon.ivs);
const actualHPDV = gen.stats.toDV(pokemon.ivs.hp);
if (gen.num <= 2 && expectedHPDV !== actualHPDV) {
if (setHPDV) {
throw new Error(`${pokemon.species.name} is required to have an HP DV of ` + `${expectedHPDV} in generations 1 and 2 but it is ${actualHPDV}`);
}
pokemon.ivs.hp = gen.stats.toIV(expectedHPDV);
}
}
function setHiddenPowerIVs(gen: Generation, pokemon: {level: number; ivs: StatsTable}, moves: string[], override = false) {
let hpType: Type | 'infer' | undefined = undefined;
for (const move of moves) {
const id = toID(move);
if (id.startsWith('hiddenpower')) {
if (hpType) throw new Error('Cannot have more than one Hidden Power on a set');
const type = gen.types.get(id.slice(11));
if (gen.num === 1 || gen.num === 8 || !type || is(type.name, '???', 'Normal', 'Fairy')) {
if (id === 'hiddenpower') {
hpType = 'infer';
} else {
invalid(gen, 'Hidden Power', type);
}
} else {
hpType = type;
}
}
}
if (!hpType || hpType === 'infer') return;
if (gen.num >= 7 && pokemon.level === 100) return;
if (gen.types.getHiddenPower(pokemon.ivs).type === hpType.name) return;
let ivs = hpType.HPivs;
if (gen.num <= 2) {
ivs = {};
for (const stat in hpType.HPdvs) {
ivs[stat as StatID] = gen.stats.toIV(hpType.HPdvs[stat as StatID]!);
}
}
if (override) {
for (const stat of gen.stats) {
pokemon.ivs[stat] = ivs[stat] || 31;
}
} else {
const max = gen.num <= 2 ? 30 : 31;
let maxed = true;
for (const stat of gen.stats) {
if (!(pokemon.ivs[stat] >= max) && pokemon.ivs[stat] !== ivs[stat]) {
maxed = false;
break;
}
}
if (maxed) {
for (const stat of gen.stats) {
pokemon.ivs[stat] = ivs[stat] || 31;
}
} else {
throw new Error('Cannot set Hidden Power IVs over non-default IVs');
}
}
}
function setConditions(gen: Generation, kind: ConditionKind, data: string[] | {[id: string]: unknown} | undefined) {
const obj: {[id: string]: {level?: number}} = {};
if (data) {
if (Array.isArray(data)) {
for (const d of data) {
const condition = Conditions.get(gen, d);
if (!condition) invalid(gen, kind, d);
const [name, k] = condition;
if (k !== kind) {
throw new Error(`'${name} is a ${k} not a ${kind} in generation ${gen.num}`);
}
obj[toID(name)] = {};
}
} else {
for (const d in data) {
const condition = Conditions.get(gen, d);
if (!condition) invalid(gen, kind, d);
const [name, k] = condition;
if (k !== kind) {
throw new Error(`'${name} is a ${k} not a ${kind} in generation ${gen.num}`);
}
obj[toID(name)] = data[d] as {level?: number}; // TODO: verify layers?
}
}
}
return obj;
}
function validateStats(gen: Generation, pokemon: State.Pokemon) {
if (!pokemon.stats) return pokemon;
const nature = pokemon.nature && gen.natures.get(pokemon.nature);
for (const stat of gen.stats) {
const actual = gen.stats.calc(
stat,
pokemon.species.baseStats[stat],
pokemon.ivs?.[stat] ?? 31,
pokemon.evs?.[stat] ?? (gen.num <= 2 ? 252 : 0),
pokemon.level,
nature
);
if (actual !== pokemon.stats[stat]) {
const s = gen.stats.display(stat);
throw new Error(`Expected a ${s} stat of ${actual}, received: ${pokemon.stats[stat]}`);
}
}
return pokemon;
}