Often, you just need a random item in an array. Throughout Pokemon
Showdown's code, there are many instances of the following pattern:
let randomThing = things[this.random(things.length)];
Make this code easier to read by factoring the indexing into the
PRNG#sample function:
let randomThing = this.sample(things);
Run the following sed script to refactor lots of code to use sample:
s/\([a-zA-Z0-9.]\{1,\}\)\[this\.random(\1\.length)\]/this.sample(\1)/
This commit should not change behaviour. In particular, PRNG#next is
called the same number of times with the same number of parameter as
before this commit, and PRNG#next's results are interpreted in the same
way as before this commit.
According to `git grep getResidualStatuses`, the
Battle#getResidualStatuses function isn't referenced anywhere. Delete
the definition of that unused function.
Mechanically refactor code which uses PRNG#random for booleans to use
PRNG#randomChance instead.
Take advantage of the following properties:
random(x) < y is equivalent to randomChance(y, x)
random(x) <= y is equivalent to random(x) < (y + 1), i.e. randomChance(y + 1, x)
random(x) >= y is equivalent to !(random(x) < y), i.e. !randomChance(y, x)
random(x) > y is equivalent to random(x) >= (y + 1), i.e. !randomChance(y + 1, x)
random(x) === 0 is equivalent to random(x) < 1, i.e. randomChance(1, x)
!random(x) is equivalent to random(x) === 0, i.e. randomChance(1, x)
Boolean(random(x)) is equivalent to random(x) > 0, i.e. !randomChance(1, x)
This commit should not change behaviour. In particular, PRNG#next is
called the same number of times with the same number of parameter as
before this commit, and PRNG#next's results are interpreted in the same
way as before this commit.
Often, you just need a random boolean. Throughout Pokemon Showdown's
code, there are many creative ways of requesting random booleans. For
example:
if (this.random(10) < 3) {
if (this.isWeather(['sunnyday', 'desolateland']) || this.random(2) === 0) {
let shiny = !this.random(1024);
if (uberCount > 1 && this.random(5) >= 1) continue;
if (!this.random(3)) ability = ability1.name;
} else if ((ability === 'Iron Barbs' || ability === 'Rough Skin') && this.random(2)) {
if (typeof secondary.chance === 'undefined' || this.random(256) <= effectChance) {
if (accuracy !== true && this.random(256) > accuracy) {
Enable these methods to converge by introducing the PRNG#randomChance
function. It accepts a probability and returns true with that
probability.
Run the following sed script to refactor many common patterns to use
randomChance:
s/this\.random(\([0-9]\{1,\}\)) >= \([0-9]\{1,\}\)/!this.randomChance(\2, \1)/g
s/this\.random(\([0-9]\{1,\}\)) < \([0-9]\{1,\}\)/this.randomChance(\2, \1)/g
s/this\.random(\([0-9]\{1,\}\)) === 0/this.randomChance(1, \1)/g
s/!this\.random(\([0-9]\{1,\}\))/this.randomChance(1, \1)/g
The sed script takes advantage of the following properties:
random(x) < y is equivalent to randomChance(y, x)
random(x) >= y is equivalent to !(random(x) < y), i.e. !randomChance(y, x)
random(x) === 0 is equivalent to random(x) < 1, i.e. randomChance(1, x)
!random(x) is equivalent to random(x) === 0, i.e. randomChance(1, x)
This commit should not change behaviour. In particular, PRNG#next is
called the same number of times with the same number of parameter as
before this commit, and PRNG#next's results are interpreted in the same
way as before this commit.
Various functions call ModdedDex#getEffect with null, undefined, or an
empty string. For example, in the common case, Battle#getWeather calls
getEffect with an empty string. In these cases, getEffect basically
returns the same object each time: a PureEffect indicating that no
effect with the given name exists.
Creating the dummy PureEffect is expensive and puts pressure on the
JavaScript garbage collector. Memoize the creation in getEffect to
improve performance.
(As a bonus, special-casing null, undefined, and empty strings also lets
us drop some noisy truthiness checks in getEffect.)
On my machine, this commit speeds up Pokemon Showdown's tests by 11%.
Methodology: With and without this commit, I ran mocha four times with
zsh' 'time' builtin, dropped the first result, and averaged the wall
times:
mocha times before this commit:
18.20s user 0.33s system 118% cpu 15.704 total
17.91s user 0.34s system 118% cpu 15.454 total
18.11s user 0.33s system 118% cpu 15.558 total
mocha times after this commit:
16.53s user 0.35s system 120% cpu 13.994 total
16.43s user 0.34s system 120% cpu 13.918 total
16.54s user 0.32s system 120% cpu 14.009 total
Hardware:
Mid 2012 MacBook Pro
2.6 GHz Intel Core i7
Software:
Node v9.0.0
macOS 10.10.5
Battle#getRelevantEffectsInner performs a lookup for the base species of
every Pokemon with ModdedDex#getEffect, then invokes callbacks. The
lookup is expensive, and callbacks very rare. In fact, there are only
ever two callbacks: one for Arceus (SwitchIn) and one for Silvally
(SwitchIn).
Instead of an expensive ModdedDex#getEffect lookup for the callbacks,
put the callbacks directly on the Pokemon's Template object.
On my machine, this commit speeds up Pokemon Showdown's tests by 20%.
Methodology: With and without this commit, I ran mocha four times with
zsh' 'time' builtin, dropped the first result, and averaged the wall
times:
mocha times before this commit:
18.20s user 0.33s system 118% cpu 15.704 total
17.91s user 0.34s system 118% cpu 15.454 total
18.11s user 0.33s system 118% cpu 15.558 total
mocha times after this commit:
15.58s user 0.33s system 122% cpu 13.028 total
15.32s user 0.33s system 121% cpu 12.890 total
15.56s user 0.32s system 121% cpu 13.068 total
Hardware:
Mid 2012 MacBook Pro
2.6 GHz Intel Core i7
Software:
Node v9.0.0
macOS 10.10.5
Also allow passing "HA" or "Hidden" to /learn, to do some basic
checking for Hidden Ability legality. This doesn't currently do full
validation - event abilities aren't checked, for instance.
I originally thought this would have to be hardcoded, but actually this
can be coded slightly less hardly than expected!
Getting a Blissey with Present + Heal Bell in Gen 2 works like this:
- Teach Smeargle Present + Heal Bell
- Breed Present + Heal Bell into Snubbull
- Chainbreed Present + Heal Bell into Blissey
The main issue is that checking chainbreeding is very hard, so PS
mostly just takes the stance of "chainbreeding multiple moves is
probably impossible; hardcode exceptions".
BUT! BUT!!!!
Instead of hardcoding this exact move combination, we can actually
just hardcode the fact "the first step of chainbreeding is always legal
if the first father is Smeargle". Which I did and it works!
A battle's inputLog is now stored separately from the output log. It's
not an exact log of inputs, but rather just a collection of the inputs
that resulted in the battle: a default choice expands to the choice
that was actually used, and the starting seed is logged whether or not
it was explicitly passed into the battle stream.
Fixes#4348Fixes#3201
This contains a lot of minor refactors, but the main thing that's going
on here is that battle stream writes have been streamlined to be a lot
easier for others to use.
We even support:
./pokemon-showdown simulate-battle
which provides a stdio interface for anyone using any programming
language to simulate a battle.
./pokemon-showdown help
will now display all the possible ways to use PS on the command-line,
of which there are several new ones:
./pokemon-showdown validate-team
./pokemon-showdown unpack-team
./pokemon-showdown pack-team
The previous complement of way too many message types:
- update, winupdate, sideupdate, request, score, log
are now only:
- update, sideupdate, end
`score` was removed in the previous commit, and this commit adds a new
message type `end`. `end` replaces the previous `log`, and also
contains the data for `winupdate` and `score`.
`request` was also folded into `sideupdate`.
`Sim.construct` no longer exists. Battles are now constructed directly
with `new Battle()`. Parameters other than formatid are now passed as
`options`.
This is specifically made to fix Nasty Plot + Surf Raichu.
Specifically, prevo moves are no longer compatible with evo event
moves.
Other incompatibilities (specifically, modern prevo moves with old-gen
evo moves) will be harder to fix, unfortunately.
Process Manager is now lib/process-manager.js
It's been entirely rewritten to reflect what I think a process manager
API should look like.
In particular, there are now two Process Managers, QueryProcessManager
and StreamProcessManager.
Pass QueryProcessManager a pure-ish query function (sync or async) that
takes a JSON value and returns a JSON value, and PM.query() will
execute that function in a subprocess, and return a Promise for its
return value.
StreamProcessManager is the same idea: Pass it a function to create an
ObjectReadWriteStream, and PM.createStream() will create a stream in a
subprocess and return a stream connected to it.
It turns out, battle.getFormat() is run once per invocation of
battle.runEvent(), which is a lot.
Especially with the new rule validator.
...It's cached now.
Ladder is now a subclass of Matchmaker, and all the APIs previously
attached to Matchmaker are now directly available in Ladder.
In addition, all functions that take a formatid are now of the form
`Ladders(formatid).function(other arguments)`
TODO: Reverse polarity; it makes more sense for Matchmaker to be
a subclass of Ladder. Fortunately, this wouldn't involve API changes.
The old code, for allowing unreleased pokemon if they were obtainable
by breeding, was a workaround for a very specific case which as far
as I can tell is no longer necessary.
In the future, a template should just not be marked as unreleased if
it's available by breeding.
pokemon.moveset is now pokemon.moveSlots, which is at least slightly
clearer about what it's doing (tracking move state, mainly PP).
Mostly, this gives a consistent naming scheme for `move` (a Move
object) vs `moveSlot` (a MoveSlot object).
This also refactors a lot of existing `moveSlot` accesses to be modern,
including using `for...of`.
We are now tracking source effects for switch actions, if they're
initiated by effects such as U-turn or Baton Pass. This will lead
to better messages client-side.
New functions:
battle.makeChoices([side 1], [side 2]);
Intended to be a replacement for the previous .choose/.commitDecisions
API.
If we can get all the test code on it, we can maybe finally actually
deprecate LEGACY_API_DO_NOT_USE.
It's now used in Healing Wish, where I think it makes a very readable
test.
Previously, if we wanted to test if A was either 'B' or 'C', we would use
the pattern:
A in {B:1, C:1}
I actually don't know how common this pattern is; I just started using
it because I was tired of typing `A === 'B' || A === 'C'` all the time.
I never really liked it, though; the `:1` part made it kind of
blatantly a hack.
I did some testing and `['B', 'C'].includes(A)` is overall faster.
(A switch statement is around 20x faster still, but who wants to type
that much code?)
Anyway, the new standard is
['B', 'C'].includes(A)
Something something progress!
Previously, Berries would not activate after weather damage and before
other residual damage/healing.
According to: https://www.youtube.com/watch?v=FRI5PSekhR4
They should activate then. More research is needed to determine what
other situations Berries should activate but don't.
Rules for custom formats are once again sanitized and can be passed
around more easily by attaching them to the format's id.
Fixes:
- "Rule:" being required to add or remove a rule
- ruleset changes not being shown in battles
- the display of custom rules being affected by user input
- custom formats being broken by /hotpatch formats
Simulator instances still get their scripts included somewhat hackily,
but it's overall much less hacky than before. In particular, the
init code is now an actual constructor, allowing TypeScript to perhaps
validate it one future day.
I might regret supporting this syntax, but it's now possible to make
PS generate teams on the commandline using:
./pokemon-showdown generate-team [optional format name] [optional seed]
The output will be in packed team format (which can be pasted into
a teambuilder import box).
The seed-passing should make it significantly more straightforward to
debug team generation weirdness.
Random team generation scripts are no longer in scripts.js, but instead
in a new file random-teams.js.
The scripts are now also no longer run from inside battles, but in a
new team generator object. This makes it easier for external scripts
to generate random teams by running Dex.generateTeam(format).
PS's rule table has been renamed from banlistTable, and works a bit
differently now. It's a Map instead of an object now, and the keys
work a bit differently.
The original banlistTable was designed to store bans, and later
additions shoved rules and then unbans in there. The new table is
designed to support all of these.
- caused weather effects passed in through ``moreData`` to be marked as simply ``"Effect"``
- as a result, sandstorm and hail will damage Rock/Ground/Steel and Ice types respectively
- this fix makes ``Effect`` inherit the ``effectType`` from ``moreData`` if it doesn't exist in the primary ``data`` variable passed into the constructor
This is a surprisingly minor refactor considering how many files it
touches, but most of this is only renames.
In terms of file renames:
- `tools.js` is now `sim/dex.js`
- `battle-engine.js` is now `sim/index.js` and its three classes are
in `sim/battle.js`, `sim/side.js`, and `sim/pokemon.js`
- `prng.js` is now `sim/prng.js`
In terms of variable renames:
- `Tools` is now `Dex`
- `BattleEngine` is now `Sim`
- `BattleEngine.Battle` is now `Sim.Battle`
- `BattleEngine.BattleSide` is now `Sim.Side`
- `BattleEngine.BattlePokemon` is now `Sim.Pokemon`