pokemon-showdown/tools/build-utils.js
ACakeWearingAHat 686a020e2c
Implement Gen 9 Random Battle (#9169)
* Move gen 8 rands code to gen8 folder

* Add test data file

* Remove hidden power from random-teams

* Remove BDSP

* Remove item selection for now

* Remove ability culling for now

* Remove move culling for now

* Obtain allowed pokemon from random-sets.json

* Add tera blast counter

* linting

* Remove Dynamax from random-teams

* Remove unnecessary check

* Add role and tera type + fix data format

* remove gmax

* Disable tier for now

* movePool should have move ids not names

* linting

* Add required moves

* Create an individual function for moveset generation

* Return moves early if <= 4 moves available

* Add counter for individual STABS, including tera

* Tera Blast is tera stab

* Add some moves (help with testing)

* Remove culling moves from moveset

* Add new hazard removing moves

* Test STAB and recovery moves

* Cull movepool each time a move is added

* Black Sludge only if tera type is Poison

* teratype and role to functions

* Test some 'High Priority' items

* Move ability selection to its own function

* Fix ability sorting/selection

* No need to go through the move adding process if number of moves in set + movepool is 4

* lint

* Remove some pokemon that aren't in gen 9 randbats

* Tera STAB example

* Allow for forcing abilities

* Silvally not in gen 9

* Move level calculation to getLevel

* Tier-based levelling and doubles coverage

* Sitrus Berry default item for doubles

* Avoid hard-coding for species frequency

* Linting + replace hail with snow

* Change a comment slightly

* Fix STAB moves: need to check that they do damage

* minor thing

* Comment out tera type requirement

* comments

* Add gen 9 randbats format

* STAB counter

* Remove setupType

* Add cullMutlaExclusive()

* Remove unnecessary functions

* Move CC/HC to the bottom

* Allow movesA and movesB to be overlapping

* Initialization of fill-out

* linting

* Setup moveEnforcementCheckers

* Type/STAB counter only updates for moves that aren't in noSTAB

* Account for type changing abilities earlier

* STAB enforcement

* Setup moves array

* linting

* Revert Setup change

* More fillout

* move tweaks

* Tera STAB enforcement

* Remove rest from recovery moves

* Enforce setup

* Add Facade if Guts

* Misc enforced moves

* Enforce more moves

* Enforce STAB priority

* Setup forced on tera blast user

* check if there are moves of the desired kind before sampling

* move tweaks

* Need to check if setupMoves is empty

* fix brute bonnet

* fix scream tail

* Finish up move incompatibilities

* fix scream tail harder

* Formatting

* Fix some things

* End culling early if movepool gets too small

* Paired moves: end culling early if movepool gets too small

* Remove 'stop cull' for testing purposes

* If 2/4 moves are filled, remove single unpaired move

* Remove now unnecessary hardcodes

* Use MovePairs instead of hardcoding pairs

* addMove function for adding moves to moveset

* Move updates

* Extra check is unnecessary, moves and movePool should never have moves in common

* Mach Punch required on Breloom

* lint

* Remove Synthesis from Brute Bonnet (5 enforced moves)

* adjust oranguru tera types

* finish up moves

* Force abilities

* Update my favorite pokemon

* linting

* Add shouldCullAbility

* fix setup counter

* Move sorting abilities below hardcoding

* minor change

* Add fields

* fix sawsbuck typo

* Improve the species generation + remove Basculegion and Houndstone as possible leads

* Fix Species frequency

* Linting

* veluza can actually run sharpness

* code all ability rejections

* Update random-sets.json

* EVs/IVs tinkering change

* fix capitalization error

* Perform small fixes

* fix moves that don't exist

* linting

* Magnezone hardcode

* Items

* Update random-teams.ts

* linting

* prep for leveling

* long list for ditto

* comment out a line of text

* finish individualizing levels and also melop sucks

* add lokix thing

* revavroom

* updates from testing

* komala probably shouldn't have dark as an option

* tropius fix

* intim mence is ok

* kleavor should not get sheer force

* why did we give hydre earth power lmao

* why did we put leech seed on toed

* forgot to sitrusify cheek pouch mons

* fix enamorus

* Syntax error

* Change Zoroark lastmon condition to level >= 72

* Fix Zoroark-level constraint to allow for level forcing

* Fix singles vs doubles definition

* Use species.id as much as possible for set generation

* updates from testing

* allow flash fire heatran

* additional sash parameters

* reject stakeout if the user has zero attacks

* fixes

* update pawmot abilities

* Change iron fist counter to a number

* Force Own Tempo on Petal Dance Lilligant

* Add T-wave to SD groudon set

* Slush Rush guaranteed with Snowscape + possible if team has snow

* Rework ability sorting: find list of non-culled abilities before sorting and selecting

* Move S/V rands to The Forefront

* fix regidrago

* Small fix to choosing allowed abilities

* update aliases

* Update Arcanines

* Change from forEach to for, since index is no longer being used

* first impression is bug stab now

* set updates from the randscord

* Update /randbats to gen 9

* Add Tera Type to /randbats output

* things to do with steel

* Update data/random-teams.ts

Co-authored-by: Kris Johnson <11083252+KrisXV@users.noreply.github.com>

* Update data/random-teams.ts

Co-authored-by: Kris Johnson <11083252+KrisXV@users.noreply.github.com>

* Update data/random-teams.ts

Co-authored-by: Kris Johnson <11083252+KrisXV@users.noreply.github.com>

* Update data/random-teams.ts

Co-authored-by: Kris Johnson <11083252+KrisXV@users.noreply.github.com>

* Update data/random-teams.ts

Co-authored-by: Kris Johnson <11083252+KrisXV@users.noreply.github.com>

* Apply suggestions from code review

Co-authored-by: Kris Johnson <11083252+KrisXV@users.noreply.github.com>

* Move Battle Factory and BSS Factory to mods/gen8 folder

* Remove gen8 factory sets test

* Copy over random-sets.json when building

* add missing heatran-based incompatibility

* fix sableye not always getting Recover

* set updates

* start movin' cap 1v1

Co-authored-by: Kris Johnson <11083252+KrisXV@users.noreply.github.com>

* finish movin' cap 1v1

* Update random-battles.ts

* Update random-teams.ts

* Update random-teams.ts

Co-authored-by: livid washed <liuc.kelvin9901@hotmail.com>
Co-authored-by: livid washed <115855253+livid-washed@users.noreply.github.com>
Co-authored-by: Alexander B <4866817+MathyFurret@users.noreply.github.com>
Co-authored-by: Kris Johnson <11083252+KrisXV@users.noreply.github.com>
2022-12-13 17:56:29 -07:00

264 lines
7.9 KiB
JavaScript

"use strict";
const {transform} = require("sucrase");
const fs = require("fs");
const path = require("path");
const child_process = require("child_process");
let force = false;
function needsSucrase(source, dest, path = "") {
if (path.endsWith(".ts")) {
if (path.endsWith(".d.ts")) return false;
const sourceStat = fs.lstatSync(source + path);
try {
const destStat = fs.lstatSync(dest + path.slice(0, -2) + "js");
return sourceStat.ctimeMs > destStat.ctimeMs;
} catch (e) {
// dest doesn't exist
return true;
}
}
if (!path.includes(".")) {
// probably dir
if (source === './config' && path) return false;
try {
const sourceFiles = fs.readdirSync(source + path);
for (const file of sourceFiles) {
if (needsSucrase(source, dest, path + "/" + file)) {
return true;
}
}
if (path.endsWith('/chat-plugins') || source === './config' || path.includes('/mods/')) {
const destFiles = fs.readdirSync(dest + path);
for (const file of destFiles) {
if (path.endsWith('/config')) console.log(file);
if (file.endsWith('.js') && !sourceFiles.includes(file.slice(0, -2) + 'ts') && !sourceFiles.includes(file)) {
fs.unlinkSync(dest + path + "/" + file);
}
}
}
if (path.endsWith('/mods')) {
const destFolders = fs.readdirSync(dest + path);
for (const destFolder of destFolders) {
if (!destFolder.includes('.') && !sourceFiles.includes(destFolder)) {
fs.rmSync(dest + path + "/" + destFolder, {recursive: true});
}
}
}
} catch (e) {
// not dir
}
return false;
}
return false;
}
// https://github.com/alangpierce/sucrase/blob/master/src/cli.ts
function findFiles(options) {
const outDirPath = options.outDirPath;
const srcDirPath = options.srcDirPath;
const pathToSource = options.pathToSource || "..";
const extensions = options.sucraseOptions.transforms.includes("typescript") ?
[".ts", ".tsx"] :
[".js", ".jsx"];
const outArr = [];
for (const child of fs.readdirSync(srcDirPath)) {
if (
["node_modules", ".git"].includes(child) ||
options.excludeDirs.includes(child)
) {
continue;
}
const srcChildPath = path.join(srcDirPath, child);
const outChildPath = path.join(outDirPath, child);
const pathToSourceChild = path.join(pathToSource, "..");
if (fs.statSync(srcChildPath).isDirectory()) {
const innerOptions = {...options};
innerOptions.srcDirPath = srcChildPath;
innerOptions.outDirPath = outChildPath;
innerOptions.pathToSource = pathToSourceChild;
const innerFiles = findFiles(innerOptions);
outArr.push(...innerFiles);
} else if (extensions.some((ext) => srcChildPath.endsWith(ext))) {
const outPath = outChildPath.replace(
/\.\w+$/,
`.${options.outExtension}`
);
outArr.push({
srcPath: srcChildPath,
outPath,
pathToSource: pathToSource,
});
}
}
if (!outArr.length) {
return outArr;
}
fs.mkdirSync(path.join(outDirPath, "sourceMaps"), {recursive: true});
return outArr;
}
function sucrase(src, out, opts, excludeDirs = []) {
try {
if (!force && !needsSucrase(src, out) && src !== "./config") {
return false;
}
} catch {}
const sucraseOptions = {
transforms: ["typescript", "imports", "jsx"],
enableLegacyTypeScriptModuleInterop: true,
...opts,
};
const files = findFiles({
outDirPath: out,
srcDirPath: src,
sucraseOptions: sucraseOptions,
excludeDirs: ["sourceMaps", ...excludeDirs],
outExtension: "js",
});
for (const file of files) {
const sucraseOptionsFile = {
...sucraseOptions,
sourceMapOptions: {compiledFilename: file.outPath},
filePath: path.join(file.pathToSource, file.srcPath),
};
const code = fs.readFileSync(file.srcPath, "utf-8");
const transformed = transform(code, sucraseOptionsFile);
transformed.code += `\n //# sourceMappingURL=sourceMaps/${path.basename(
file.outPath
)}.map`;
fs.writeFileSync(
path.join(
path.dirname(file.outPath),
"sourceMaps",
path.basename(`${file.outPath}.map`)
),
JSON.stringify(transformed.sourceMap)
);
fs.writeFileSync(file.outPath, transformed.code);
}
return true;
}
function replace(file, replacements) {
fs.lstat(file, function (err, stats) {
if (err) throw err;
if (stats.isSymbolicLink()) return;
if (stats.isFile()) {
if (!file.endsWith('.js') && !file.endsWith('.d.ts')) return;
fs.readFile(file, "utf-8", function (err, text) {
if (err) throw err;
let anyMatch = false;
for (let i = 0; i < replacements.length; i++) {
anyMatch = anyMatch || text.match(replacements[i].regex);
if (anyMatch) text = text.replace(replacements[i].regex, replacements[i].replace);
}
if (!anyMatch) return;
fs.writeFile(file, text, function (err) {
if (err) throw err;
});
});
} else if (stats.isDirectory()) {
fs.readdir(file, function (err, files) {
if (err) throw err;
for (let i = 0; i < files.length; i++) {
replace(path.join(file, files[i]), replacements);
}
});
}
});
}
function copyOverDataJSON(file) {
const source = './data/' + file;
const dest = './.data-dist/' + file;
fs.readFile(source, function (err, text) {
if (err) throw err;
fs.writeFile(dest, text, function (err) {
if (err) throw err;
});
});
}
exports.transpile = (doForce, decl) => {
if (doForce) force = true;
if (sucrase('./config', './.config-dist')) {
replace('.config-dist', [
{regex: /(require\(.*?)(lib|sim)/g, replace: `$1.$2-dist`},
]);
}
if (sucrase('./data', './.data-dist')) {
replace('.data-dist', [
{regex: /(require\(.*?)(lib|sim)/g, replace: `$1.$2-dist`},
]);
}
if (sucrase('./sim', './.sim-dist')) {
replace('.sim-dist', [
{regex: /(require\(.*?)\/(lib|data|config)/g, replace: `$1/.$2-dist`},
{regex: /(from '.*?)\/(lib|data|config)/g, replace: `$1/.$2-dist`},
]);
}
sucrase('./lib', './.lib-dist');
if (sucrase('./server', './.server-dist')) {
replace('.server-dist', [
{regex: /(require\(.*?)(data|lib|sim)/g, replace: `$1.$2-dist`},
]);
}
sucrase('./translations', './.translations-dist');
if (sucrase('./tools', './tools', null, ['.', 'sets', 'simulate'])) {
replace('tools', [
{regex: /(require\(.*?)(lib|sim|server)/g, replace: `$1.$2-dist`},
]);
}
if (!fs.existsSync('./.data-dist/README.md')) {
const text = '**NOTE**: This folder contains the compiled output of the `data/` directory.\n' +
'You should be editing the `.ts` files there and then running `npm run build` or\n' +
'`./pokemon-showdown` to force these `.js` files to be recreated.\n';
fs.writeFile('./.config-dist/README.md', text.replace('data/', 'config/'), function () {});
fs.writeFile('./.data-dist/README.md', text, function () {});
fs.writeFile('./.sim-dist/README.md', text.replace('data/', 'sim/'), function () {});
fs.writeFile('./.server-dist/README.md', text.replace('data/', 'server/'), function () {});
fs.writeFile('./.translations-dist/README.md', text.replace('data/', 'translations/'), function () {});
fs.writeFile('./.lib-dist/README.md', text.replace('data/', 'lib/'), function () {});
}
// sucrase doesn't copy JSON over, so we'll have to do it ourselves
copyOverDataJSON('random-sets.json');
copyOverDataJSON('mods/gen8/cap-1v1-sets.json');
copyOverDataJSON('mods/gen8/factory-sets.json');
copyOverDataJSON('mods/gen8/bss-factory-sets.json');
copyOverDataJSON('mods/gen7/factory-sets.json');
copyOverDataJSON('mods/gen7/bss-factory-sets.json');
copyOverDataJSON('mods/gen6/factory-sets.json');
// NOTE: replace is asynchronous - add additional replacements for the same path in one call instead of making multiple calls.
if (decl) {
exports.buildDecls();
}
};
exports.buildDecls = () => {
try {
child_process.execSync(`node ./node_modules/typescript/bin/tsc -p sim`, {stdio: 'inherit'});
} catch {}
for (const file of fs.readdirSync(`./.sim-dist/lib/`)) {
fs.renameSync(`./.sim-dist/lib/${file}`, `./.lib-dist/${file}`);
}
for (const file of fs.readdirSync(`./.sim-dist/sim/`)) {
fs.renameSync(`./.sim-dist/sim/${file}`, `./.sim-dist/${file}`);
}
};