forked from pkmn/randbats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate
More file actions
executable file
·177 lines (160 loc) · 6.08 KB
/
update
File metadata and controls
executable file
·177 lines (160 loc) · 6.08 KB
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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const util = require('util');
const stringify = require('json-stringify-pretty-compact');
const zlib = require('zlib');
const gzip = util.promisify(zlib.gzip);
const {Dex, Teams} = require('./vendor/pokemon-showdown');
Dex.includeModData();
const SEED = [1, 2, 3, 4];
const N = +process.argv[2] || 100_000;
const SKIP = ['factory', 'unrated', 'cup', 'staff', 'cap', 'monotype'];
const ORDER = ['hp', 'atk', 'def', 'spa', 'spd', 'spe'];
const CMP = {
stats: (a, b) => ORDER.indexOf(a[0]) - ORDER.indexOf(b[0]),
str: (a, b) => a[0].localeCompare(b[0]),
};
const getForme = (dex, pool, set) => {
if (set.gigantamax) {
return dex.species.get(set.species.startsWith('Pikachu')
? 'Pikachu-Gmax'
: `${set.species}-Gmax`);
}
const item = dex.items.get(set.item);
if (item.megaStone) return dex.species.get(item.megaStone);
const formes = Object.keys(pool[set.species] || pool[set.name]);
if (formes.length === 1) return dex.species.get(formes[0]);
const matches = [];
for (const f in pool[set.species]) {
const forme = dex.species.get(f);
if (forme.requiredItem) {
if (set.item !== forme.requiredItem) continue;
return forme;
}
if (forme.requiredAbility && set.ability !== forme.requiredAbility) continue;
if (set.moves.every(m => pool[set.species][f].has(m))) {
matches.push(forme);
}
}
// Certain generators perform "Moveset modification" on specific Pokémon or moveset combinations.
// This can be problematic if the species in question has multiple formes, if so we just give up
// and go with the set species (mostly relevant for Aggron/Aggron-Mega in Gen 6/7).
return dex.species.get(matches.length ? matches[0] : set.species);
};
const getMoves = (species, doubles) => {
const moves = new Set();
if (doubles && species.randomDoubleBattleMoves) {
species.randomDoubleBattleMoves.forEach(m => moves.add(m));
} else {
if (species.essentialMove) moves.add(species.essentialMove);
if (species.exclusiveMoves) species.exclusiveMoves.forEach(m => moves.add(m));
if (species.comboMoves) species.comboMoves.forEach(m => moves.add(m));
if (species.randomBattleMoves) species.randomBattleMoves.forEach(m => moves.add(m));
}
return moves;
};
const serialize = async (data, file) => {
const json = stringify(data, {maxLength: 1000});
await fs.promises.writeFile(`data/${file}`, json);
const compressed = await gzip(json);
return [json.length, compressed.length];
};
(async () => {
const index = {};
for (const format of Dex.formats.all()) {
if (!format.team || !['singles', 'doubles'].includes(format.gameType)) continue;
if (SKIP.some(s => format.id.includes(s)) || !format.mod.startsWith('gen')) continue;
// Of course, PS is braindead and Format#gen always reports 0
const gen = Number(format.id.charAt(3));
const dex = Dex.forFormat(format);
const letsgo = format.id.includes('letsgo');
const doubles = format.gameType === 'doubles';
const generator = Teams.getGenerator(format, SEED);
const IVS = gen < 3 ? 30 : 31;
const EVS = gen < 3 ? 255 : 85;
const pool = {};
for (const species of dex.species.all()) {
if (species.gen > gen) continue;
if (letsgo && (species.num < 1 || species.num > 151 && ![808, 809].includes(species.num))) {
continue;
}
const moves = getMoves(species, doubles);
if (!moves.size) continue;
const base = typeof species.battleOnly === 'string' ? species.battleOnly : species.name;
pool[base] = (pool[base] || {});
pool[base][species.name] = moves;
}
const formes = {};
for (let i = 0; i < N; i++) {
for (const set of generator.getTeam()) {
const forme = getForme(dex, pool, set);
const s = formes[forme.name] || (formes[forme.name] = {
level: set.species === 'Zoroark' ? 0 : set.level,
abilities: new Set(),
items: new Set(),
moves: getMoves(forme, doubles),
ivs: {},
evs: {},
});
for (const m of set.moves) s.moves.add(m);
for (const stat in set.ivs) {
if (set.ivs[stat] !== IVS) {
if (!(stat in s.ivs)) {
s.ivs[stat] = set.ivs[stat];
} else if (s.ivs[stat] !== set.ivs[stat]) {
s.ivs[stat] = -1;
}
} else if (s.ivs[stat]) {
s.ivs[stat] = -1;
}
if (set.evs[stat] !== EVS) {
if (!(stat in s.evs)) {
s.evs[stat] = set.evs[stat];
} else if (s.evs[stat] !== set.evs[stat]) {
s.evs[stat] = -1;
}
} else if (s.evs[stat]) {
s.evs[stat] = -1;
}
}
if (set.ability) s.abilities.add(set.ability);
if (set.item) s.items.add(set.item);
}
}
const options = {};
for (const [name, stats] of Object.entries(formes).sort(CMP.str)) {
const opts = {level: stats.level};
if (gen >= 3 && !letsgo) opts.abilities = Array.from(stats.abilities).sort();
if (stats.items.size) opts.items = Array.from(stats.items).sort();
opts.moves = Array.from(stats.moves).map(m => dex.moves.get(m).name).sort();
if (!letsgo) {
for (const [stat, val] of Object.entries(stats.evs).sort((CMP.stats))) {
if (val !== -1) {
opts.evs = opts.evs || {};
opts.evs[stat] = val;
}
}
}
for (const [stat, val] of Object.entries(stats.ivs).sort((CMP.stats))) {
if (val !== -1) {
opts.ivs = opts.ivs || {};
opts.ivs[stat] = val;
}
}
options[name] = opts;
}
index[`${format.id}.json`] = await serialize(options, `${format.id}.json`);
const code = util.inspect(options, {
depth: Infinity,
maxArrayLength: Infinity,
compact: true,
breakLength: 1000,
}).slice(1, -1);
fs.writeFileSync(`data/${format.id}.js`, `var ${format.id.toUpperCase()} = {\n ${code}\n};`);
}
fs.writeFileSync('data/index.json', stringify(index));
})().catch(err => {
console.error(err);
process.exit(1);
});