@pkmn/engine 0.1.0-dev.1da0f17c → 0.1.0-dev.21a51fe4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +45 -42
- package/build/pkg/addon.js +3 -2
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/layout.json +25 -3
- package/build/pkg/gen1.d.ts +5 -4
- package/build/pkg/gen1.js +46 -30
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +4 -2
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +4 -2
- package/build/pkg/protocol.js +4 -2
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +29 -12
- package/build/test/display.js.map +1 -1
- package/build.zig +36 -21
- package/package.json +27 -17
- package/src/bin/install-pkmn-engine +10 -5
- package/src/lib/binding/node.zig +45 -140
- package/src/lib/binding/wasm.zig +4 -2
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +11 -11
- package/src/lib/common/debug.zig +21 -11
- package/src/lib/common/js.zig +118 -0
- package/src/lib/common/optional.zig +60 -0
- package/src/lib/common/options.zig +9 -0
- package/src/lib/common/protocol.zig +204 -140
- package/src/lib/common/rational.zig +353 -0
- package/src/lib/common/rng.zig +10 -10
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/README.md +169 -148
- package/src/lib/gen1/calc.zig +427 -0
- package/src/lib/gen1/chance.zig +1044 -0
- package/src/lib/gen1/data/moves.zig +10 -10
- package/src/lib/gen1/data/species.zig +2 -2
- package/src/lib/gen1/data/types.zig +3 -3
- package/src/lib/gen1/data.zig +64 -38
- package/src/lib/gen1/helpers.zig +439 -32
- package/src/lib/gen1/mechanics.zig +881 -527
- package/src/lib/gen2/README.md +12 -13
- package/src/lib/gen2/data/items.zig +3 -3
- package/src/lib/gen2/data/moves.zig +1 -1
- package/src/lib/gen2/data/species.zig +1 -1
- package/src/lib/gen2/data/types.zig +3 -3
- package/src/lib/gen2/data.zig +33 -22
- package/src/lib/gen2/helpers.zig +9 -11
- package/src/lib/gen2/mechanics.zig +5 -4
- package/src/lib/pkmn.zig +61 -2
package/README.md
CHANGED
|
@@ -18,7 +18,7 @@ A minimal, complete, Pokémon battle simulation engine optimized for performance
|
|
|
18
18
|
[designed](docs/DESIGN.md) for tooling, embedded systems, and [artificial
|
|
19
19
|
intelligence](https://github.com/pkmn/0-ERROR) use cases. This engine aims to be a frame-accurate and
|
|
20
20
|
[bug-for-bug compatible](http://www.catb.org/jargon/html/B/bug-for-bug-compatible.html)
|
|
21
|
-
implementation of both Pokémon battles as defined by the original game code
|
|
21
|
+
implementation of both Pokémon battles as defined by the original game code and the [Pokémon
|
|
22
22
|
Showdown](https://pokemonshowdown.com/)[^1] simulator which represents Pokémon battling as practically
|
|
23
23
|
interpreted online.
|
|
24
24
|
|
|
@@ -50,7 +50,7 @@ $ zig build --prefix /usr/local -Doptimize=ReleaseFast
|
|
|
50
50
|
|
|
51
51
|
The Zig website has [installation instructions](https://ziglang.org/learn/getting-started/) which
|
|
52
52
|
walk through how to install Zig on each platform - the engine code should work on Zig v0.11.0 dev
|
|
53
|
-
build
|
|
53
|
+
build 3859 or greater, though tracks Zig's master branch so this may change in the future if
|
|
54
54
|
breaking language changes are introduced:
|
|
55
55
|
|
|
56
56
|
`libpkmn` can be built with `-Dshowdown` to instead produce the Pokémon Showdown compatible
|
|
@@ -67,12 +67,12 @@ $ npm install @pkmn/engine
|
|
|
67
67
|
```
|
|
68
68
|
|
|
69
69
|
The driver depends on being able to find compiled Node/WASM addons in
|
|
70
|
-
`node_modules/@pkmn/engine/build/lib`
|
|
71
|
-
|
|
72
|
-
[`install-pkmn-engine`](src/bin/install-pkmn-engine) which
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
70
|
+
`node_modules/@pkmn/engine/build/lib` to be useful. When you install the package a [`postinstall`
|
|
71
|
+
lifecycle script](https://docs.npmjs.com/cli/v8/using-npm/scripts) runs
|
|
72
|
+
[`install-pkmn-engine`](src/bin/install-pkmn-engine) which checks for a compatible `zig` compiler
|
|
73
|
+
(see earlier regarding minimum version) and download one to `node_module/@pkmn/engine/build/bin` if
|
|
74
|
+
it can't find one, as well as looking for (and downloading, if necessary) the required Node headers
|
|
75
|
+
needed to successfully build the addons natively.
|
|
76
76
|
|
|
77
77
|
**If you have configured `npm` to `--ignore-scripts` you must either run `npx install-pkmn-engine`
|
|
78
78
|
directly or build the addons manually and place the artifacts in the expected paths.**
|
|
@@ -88,7 +88,7 @@ adding the following to your `build.zig`:
|
|
|
88
88
|
const std = @import("std");
|
|
89
89
|
const pkmn = @import("lib/pkmn/build.zig");
|
|
90
90
|
|
|
91
|
-
pub fn build(b: *std.
|
|
91
|
+
pub fn build(b: *std.Build) void {
|
|
92
92
|
...
|
|
93
93
|
exe.addModule("pkmn", pkmn.module(b, .{ .showdown = true, .log = true }));
|
|
94
94
|
...
|
|
@@ -100,7 +100,7 @@ configuring whether or not Pokémon Showdown compatibility mode or protocol mess
|
|
|
100
100
|
enabled. Alternatively, you may set options via a `pkmn_options` [root source file
|
|
101
101
|
declaration](https://ziglang.org/documentation/master/#Root-Source-File). There are several
|
|
102
102
|
undocumented internal options that can be tweaked as well via build or root options, though these
|
|
103
|
-
options
|
|
103
|
+
options aren't officially supported, affect correctness, and may change meaning or behavior without
|
|
104
104
|
warning. Use at your own risk.
|
|
105
105
|
|
|
106
106
|
```zig
|
|
@@ -112,11 +112,11 @@ pub const pkmn_options = .{ .showdown = true, .log = true };
|
|
|
112
112
|
For each Pokémon generation, the engine provides a battle structure with two functions - a function
|
|
113
113
|
to `update` the battle's state based on both players' choices and a function that defines which
|
|
114
114
|
`choices` are valid at that decision point (at the beginning of a battle both player's must "pass"
|
|
115
|
-
as their first choice
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
115
|
+
as their first choice to switch in the first member of their party). If `-Dlog` protocol logging is
|
|
116
|
+
enabled each update produces logs which can be decoded into a buffer (`LOGS_SIZE` constants are
|
|
117
|
+
provided to make it easy to allocate a buffer of the correct size). Each `update` returns a result
|
|
118
|
+
to indicate either that the battle has terminated or which types of choices are available for each
|
|
119
|
+
player.
|
|
120
120
|
|
|
121
121
|
Unlike [Pokémon Showdown's
|
|
122
122
|
SIM-PROTOCOL](https://github.com/smogon/pokemon-showdown/blob/master/sim/SIM-PROTOCOL.md#choice-requests)
|
|
@@ -128,7 +128,7 @@ state or cause the engine to crash.**
|
|
|
128
128
|
|
|
129
129
|
Battles may be played out from any point but to freshly initialize a battle which is yet to start
|
|
130
130
|
the turn count and active Pokémon should be zeroed out (driver code should handle this for you).
|
|
131
|
-
Most driver code should also provide helpers to make
|
|
131
|
+
Most driver code should also provide helpers to make initializing battle convenient - check the
|
|
132
132
|
respective documentation and examples for the bindings being used. The engine's [protocol
|
|
133
133
|
documentation](docs/PROTOCOL.md) goes into greater detail on the specifics of updates and the
|
|
134
134
|
potential logs that may result.
|
|
@@ -140,35 +140,38 @@ potential logs that may result.
|
|
|
140
140
|
|
|
141
141
|
[`pkmn.h`](src/include/pkmn.h) exports the C API for `libpkmn`. Symbols are all prefixed with
|
|
142
142
|
`pkmn_` to avoid name collisions. If `-Dlog` is enabled and logging throws an error then the error
|
|
143
|
-
|
|
143
|
+
is encoded in the `pkmn_result` and can be checked with `pkmn_error`.
|
|
144
144
|
|
|
145
145
|
```c
|
|
146
146
|
#include <pkmn.h>
|
|
147
147
|
|
|
148
|
+
pkmn_battle_options options;
|
|
149
|
+
pkmn_gen1_battle_options_set(&options, ...);
|
|
148
150
|
pkmn_battle battle = ...;
|
|
149
|
-
|
|
151
|
+
|
|
150
152
|
pkmn_result result;
|
|
151
153
|
pkmn_choice c1 = 0, c2 = 0;
|
|
152
|
-
while (!pkmn_result_type(result = pkmn_battle_update(&battle, c1, c2,
|
|
154
|
+
while (!pkmn_result_type(result = pkmn_battle_update(&battle, c1, c2, &options))) {
|
|
153
155
|
c1 = choose(PKMN_PLAYER_P1, pkmn_result_p1(result));
|
|
154
156
|
c2 = choose(PKMN_PLAYER_P2, pkmn_result_p2(result));
|
|
157
|
+
pkmn_gen1_battle_options_set(&options, ...);
|
|
155
158
|
}
|
|
156
159
|
if (pkmn_error(result)) exit(1);
|
|
157
160
|
```
|
|
158
161
|
|
|
159
162
|
[(full code)](src/examples/c/example.c)
|
|
160
163
|
|
|
161
|
-
The C API
|
|
164
|
+
The C API doesn't export any helpers for creating or accessing the opaque battle objects - it's
|
|
162
165
|
instead intended to be used as the foundation for more ergonomic bindings in [other
|
|
163
166
|
languages](#other) (the lack of namespaces and bit fields having an implementation-defined layout in
|
|
164
167
|
C are the main contributing factors to the sparseness of what `libpkmn` chooses to expose).
|
|
165
168
|
|
|
166
169
|
### JavaScript / TypeScript
|
|
167
170
|
|
|
168
|
-
`@pkmn/engine` depends on the [`@pkmn/data`](https://www.npmjs.com/package/@pkmn/data) which
|
|
171
|
+
`@pkmn/engine` depends on the [`@pkmn/data`](https://www.npmjs.com/package/@pkmn/data) package which
|
|
169
172
|
requires a `Dex` implementation to be provided as well. The `Battle.create` function can be used to
|
|
170
173
|
initialize a `Battle` from the beginning, or `Battle.restore` can be used to re-instantiate a battle
|
|
171
|
-
which is in
|
|
174
|
+
which is already in progress. If logging is enabled the output can be turned into Pokémon Showdown
|
|
172
175
|
protocol via `Log.parse`.
|
|
173
176
|
|
|
174
177
|
```ts
|
|
@@ -193,18 +196,18 @@ console.log(result);
|
|
|
193
196
|
|
|
194
197
|
[(full code)](src/examples/js/example.ts)
|
|
195
198
|
|
|
196
|
-
By default, the `@pkmn/engine` package
|
|
199
|
+
By default, the `@pkmn/engine` package compiles the engine with `-Dshowdown`, though by running
|
|
197
200
|
`install-pkmn-engine` directly and passing in `--options` you can override this default and build
|
|
198
201
|
different configurations of the extension for the driver to use. The driver can support
|
|
199
202
|
configurations both with and without Pokémon Showdown compatibility simultaneously if present. On
|
|
200
|
-
update, the post-install script
|
|
203
|
+
update, the post-install script attempts to rebuild whichever extensions it finds with the same
|
|
201
204
|
configuration parameters that were originally used meaning updating to the newest version of the
|
|
202
205
|
library should be seamless.
|
|
203
206
|
|
|
204
207
|
Despite relying on the native engine code, the `@pkmn/engine` code is designed to also work in
|
|
205
208
|
browsers which support [WebAssembly](https://webassembly.org/). Running `npm run start:web` from the
|
|
206
|
-
[`examples`](src/examples/js) directory
|
|
207
|
-
|
|
209
|
+
[`examples`](src/examples/js) directory starts a server that can be used to demonstrate the engine
|
|
210
|
+
running in the browser.
|
|
208
211
|
|
|
209
212
|
### Zig
|
|
210
213
|
|
|
@@ -219,13 +222,13 @@ var random = std.rand.DefaultPrng.init(seed).random();
|
|
|
219
222
|
var choices: [pkmn.CHOICES_SIZE]pkmn.Choice = undefined;
|
|
220
223
|
|
|
221
224
|
var battle = ...
|
|
222
|
-
var
|
|
225
|
+
var options = pkmn.battle.options(...);
|
|
223
226
|
|
|
224
227
|
var c1 = pkmn.Choice{};
|
|
225
228
|
var c2 = pkmn.Choice{};
|
|
226
229
|
|
|
227
|
-
var result = try battle.update(c1, c2,
|
|
228
|
-
while (result.type == .None) : (result = try battle.update(c1, c2,
|
|
230
|
+
var result = try battle.update(c1, c2, &options);
|
|
231
|
+
while (result.type == .None) : (result = try battle.update(c1, c2, &options)) {
|
|
229
232
|
c1 = choices[random.uintLessThan(u8, battle.choices(.P1, result.p1, &choices))];
|
|
230
233
|
c2 = choices[random.uintLessThan(u8, battle.choices(.P2, result.p2, &choices))];
|
|
231
234
|
}
|
|
@@ -237,7 +240,7 @@ std.debug.print("{}\n", .{result.type});
|
|
|
237
240
|
|
|
238
241
|
The Zig package also supports some APIs which are difficult to expose elsewhere such as the
|
|
239
242
|
`FixedRNG` which allows you to fully specify the exact RNG frames (which can be useful for ensuring
|
|
240
|
-
certain outcomes/effects always occur) for a battle, though doing so
|
|
243
|
+
certain outcomes/effects always occur) for a battle, though doing so changes the size of the
|
|
241
244
|
`Battle` object.
|
|
242
245
|
|
|
243
246
|
### Other
|
|
@@ -248,7 +251,7 @@ simplify the process even further [`src/data`](src/data) contains JSON dumps of
|
|
|
248
251
|
data, structure sizes and offsets, and protocol information used by the reference driver code. The
|
|
249
252
|
following is a list of known `libpkmn` bindings written by developers outside of the [pkmn
|
|
250
253
|
organization](https://github.com/pkmn) that may be helpful (though note that these projects may not
|
|
251
|
-
necessarily be up-to-date/complete/correct - inclusion in this list
|
|
254
|
+
necessarily be up-to-date/complete/correct - inclusion in this list doesn't imply endorsement):
|
|
252
255
|
|
|
253
256
|
| Language | License | URL |
|
|
254
257
|
| ---------- | --------- | ------------------------------------ |
|
|
@@ -276,7 +279,7 @@ By default, `pkmn-debug` expects to read [binary debug protocol](docs/PROTOCOL.m
|
|
|
276
279
|
$ <cmd> | pkmn-debug > index.html
|
|
277
280
|
```
|
|
278
281
|
|
|
279
|
-
Alternatively, if a single filename argument is passed to the `pkmn-debug` it
|
|
282
|
+
Alternatively, if a single filename argument is passed to the `pkmn-debug` it reads from that
|
|
280
283
|
instead:
|
|
281
284
|
|
|
282
285
|
```sh
|
|
@@ -298,14 +301,14 @@ Currently, most of the foundational work from stage 0 is done:
|
|
|
298
301
|
|
|
299
302
|
- [benchmark and integration testing](src/test) infrastructure
|
|
300
303
|
- [documentation](docs) about design, research, methodology, etc
|
|
301
|
-
- definition and implementation of the [protocol](docs/PROTOCOL.md) that
|
|
304
|
+
- definition and implementation of the [protocol](docs/PROTOCOL.md) that's used by the engine
|
|
302
305
|
|
|
303
|
-
**Stage 1 is currently in progress** and
|
|
306
|
+
**Stage 1 is currently in progress** and involves the implementation of the actual Generation I & II
|
|
304
307
|
battle engines, followed by Generation III & IV in stage 2. The implementation of further Pokémon
|
|
305
|
-
generations is in scope for the project but
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
308
|
+
generations is in scope for the project but shouldn't be considered part of the immediate roadmap
|
|
309
|
+
(i.e. exploring the options for broadening support for old generation APIs is given higher priority
|
|
310
|
+
than implementing more modern generations). Furthermore, implementation of modern generations is
|
|
311
|
+
soft-blocked on the [availability of high quality
|
|
309
312
|
decompilations](https://github.com/orgs/pret/repositories) of the original games in question.
|
|
310
313
|
|
|
311
314
|
Certain features will always be deemed **out of scope**:
|
|
@@ -323,10 +326,10 @@ The pkmn engine is distributed under the terms of the [MIT License](LICENSE).
|
|
|
323
326
|
[^1]: In the case of Pokémon Showdown, only bugs which stem from a misimplementation of specific
|
|
324
327
|
effects are reproduced in the engine, bugs which are the result of a misunderstanding of the
|
|
325
328
|
fundamental mechanics of Pokémon or which simply arise due to specific Pokémon Showdown
|
|
326
|
-
implementation details that
|
|
327
|
-
choices
|
|
329
|
+
implementation details that aren't replicable without making the same (incorrect) architectural
|
|
330
|
+
choices aren't. Furthermore, the "Pokémon Showdown" code referenced by this project includes
|
|
328
331
|
several [patches](docs/TESTING.md#patches) to improve accuracy and smooth over some of the more
|
|
329
332
|
egregious implementation issues. In practical terms, the vast majority of games played out in the
|
|
330
|
-
pkmn engine's compatibility mode and on this patched Pokémon Showdown simulator
|
|
331
|
-
it
|
|
333
|
+
pkmn engine's compatibility mode and on this patched Pokémon Showdown simulator are the same,
|
|
334
|
+
it's only in a well defined and documented set of circumstances where the two implementations
|
|
332
335
|
diverge.
|
package/build/pkg/addon.js
CHANGED
|
@@ -34,11 +34,12 @@ function load() {
|
|
|
34
34
|
let pkmn = undefined;
|
|
35
35
|
let showdown = undefined;
|
|
36
36
|
try {
|
|
37
|
-
pkmn = require(path.join(ROOT, 'build', 'lib', 'pkmn.node'));
|
|
37
|
+
pkmn = require(path.join(ROOT, 'build', 'lib', 'pkmn.node')).engine;
|
|
38
38
|
}
|
|
39
39
|
catch { }
|
|
40
40
|
try {
|
|
41
|
-
showdown =
|
|
41
|
+
showdown =
|
|
42
|
+
require(path.join(ROOT, 'build', 'lib', 'pkmn-showdown.node')).engine;
|
|
42
43
|
}
|
|
43
44
|
catch { }
|
|
44
45
|
if (!pkmn && !showdown) {
|
package/build/pkg/addon.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addon.js","sourceRoot":"","sources":["../../src/pkg/addon.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA6B;AAE7B,wBAAyC;AAEzC,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAE9C,IAAI,KAAK,GAAoD,SAAS,CAAC;AAcvE,SAAS,IAAI;IACX,IAAI,KAAK;QAAE,OAAO,KAAK,CAAC;IACxB,IAAI,IAAI,GAAgC,SAAS,CAAC;IAClD,IAAI,QAAQ,GAA+B,SAAS,CAAC;IACrD,IAAI;QACF,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,CAAC,
|
|
1
|
+
{"version":3,"file":"addon.js","sourceRoot":"","sources":["../../src/pkg/addon.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA6B;AAE7B,wBAAyC;AAEzC,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AAE9C,IAAI,KAAK,GAAoD,SAAS,CAAC;AAcvE,SAAS,IAAI;IACX,IAAI,KAAK;QAAE,OAAO,KAAK,CAAC;IACxB,IAAI,IAAI,GAAgC,SAAS,CAAC;IAClD,IAAI,QAAQ,GAA+B,SAAS,CAAC;IACrD,IAAI;QACF,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,CAAC,CAAC,CAAC,MAAyB,CAAC;KACxF;IAAC,MAAM,GAAG;IACX,IAAI;QACF,QAAQ;YACN,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,EAAE,KAAK,EAAE,oBAAoB,CAAC,CAAC,CAAC,MAAwB,CAAC;KAC3F;IAAC,MAAM,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0,
|
|
12
14
|
"turn": 368,
|
|
13
15
|
"last_damage": 370,
|
|
14
|
-
"
|
|
15
|
-
"rng":
|
|
16
|
+
"last_moves": 372,
|
|
17
|
+
"rng": 374
|
|
16
18
|
},
|
|
17
19
|
"Side": {
|
|
18
20
|
"pokemon": 0,
|
|
@@ -80,6 +82,26 @@
|
|
|
80
82
|
"disabled_duration": 52,
|
|
81
83
|
"disabled_move": 56,
|
|
82
84
|
"toxic": 59
|
|
85
|
+
},
|
|
86
|
+
"Action": {
|
|
87
|
+
"damage": 0,
|
|
88
|
+
"hit": 8,
|
|
89
|
+
"critical_hit": 10,
|
|
90
|
+
"secondary_chance": 12,
|
|
91
|
+
"speed_tie": 14,
|
|
92
|
+
"confused": 16,
|
|
93
|
+
"paralyzed": 18,
|
|
94
|
+
"duration": 20,
|
|
95
|
+
"durations": 24,
|
|
96
|
+
"move_slot": 40,
|
|
97
|
+
"multi_hit": 44,
|
|
98
|
+
"psywave": 48,
|
|
99
|
+
"metronome": 56
|
|
100
|
+
},
|
|
101
|
+
"Damage": {
|
|
102
|
+
"base": 0,
|
|
103
|
+
"final": 2,
|
|
104
|
+
"capped": 4
|
|
83
105
|
}
|
|
84
106
|
}
|
|
85
107
|
}
|
package/build/pkg/gen1.d.ts
CHANGED
|
@@ -39,10 +39,11 @@ export declare class Side implements Gen1.Side {
|
|
|
39
39
|
get(slot: Slot): Pokemon | undefined;
|
|
40
40
|
get lastUsedMove(): ID | undefined;
|
|
41
41
|
get lastSelectedMove(): ID | undefined;
|
|
42
|
-
get
|
|
42
|
+
get lastMoveIndex(): 1 | 2 | 3 | 4 | undefined;
|
|
43
|
+
get lastMoveCounterable(): boolean;
|
|
43
44
|
toJSON(): Gen1.Side;
|
|
44
45
|
static init(gen: Generation, lookup: Lookup, team: Partial<PokemonSet>[], data: DataView, offset: number): void;
|
|
45
|
-
static encode(gen: Generation, lookup: Lookup, battle: Data<Gen1.Battle>, side: Gen1.Side, data: DataView, offset: number): void;
|
|
46
|
+
static encode(gen: Generation, showdown: boolean, lookup: Lookup, battle: Data<Gen1.Battle>, side: Gen1.Side, data: DataView, offset: number): void;
|
|
46
47
|
}
|
|
47
48
|
export declare class Pokemon implements Gen1.Pokemon {
|
|
48
49
|
static Volatiles: {
|
|
@@ -77,11 +78,11 @@ export declare class Pokemon implements Gen1.Pokemon {
|
|
|
77
78
|
toxic: number;
|
|
78
79
|
};
|
|
79
80
|
private get sleep();
|
|
80
|
-
private get
|
|
81
|
+
private get ext();
|
|
81
82
|
private get toxic();
|
|
82
83
|
toJSON(): Gen1.Pokemon;
|
|
83
84
|
static init(gen: Generation, lookup: Lookup, set: Partial<PokemonSet>, data: DataView, offset: number, index: number): void;
|
|
84
|
-
static encodeStored(gen: Generation, lookup: Lookup, pokemon: Gen1.Pokemon, data: DataView, offset: number): void;
|
|
85
|
+
static encodeStored(gen: Generation, showdown: boolean, lookup: Lookup, pokemon: Gen1.Pokemon, data: DataView, offset: number): void;
|
|
85
86
|
static encodeActive(gen: Generation, lookup: Lookup, battle: Data<Gen1.Battle>, pokemon: Gen1.Pokemon | undefined, data: DataView, offset: number): void;
|
|
86
87
|
}
|
|
87
88
|
export declare class StoredPokemon {
|
package/build/pkg/gen1.js
CHANGED
|
@@ -86,7 +86,7 @@ class Battle {
|
|
|
86
86
|
return this.data.getUint16(OFFSETS.Battle.last_damage, data_2.LE);
|
|
87
87
|
}
|
|
88
88
|
get prng() {
|
|
89
|
-
const offset = OFFSETS.Battle.
|
|
89
|
+
const offset = OFFSETS.Battle.last_moves + (this.config.showdown ? 4 : 2);
|
|
90
90
|
// Pokémon Showdown's PRNGSeed is always big-endian
|
|
91
91
|
const seed = [0, 0, 0, 0];
|
|
92
92
|
if (this.config.showdown) {
|
|
@@ -99,10 +99,10 @@ class Battle {
|
|
|
99
99
|
// Gen12RNG seed requires an index, but if we always rotate the seed based on the index when
|
|
100
100
|
// reading we can avoid tracking the index as it will always be zero. NB: the exact bits of
|
|
101
101
|
// the Battle will not roundtrip, but the *logical* Battle state will.
|
|
102
|
-
const index = this.data.getUint8(offset +
|
|
103
|
-
for (let i = 0; i <
|
|
102
|
+
const index = this.data.getUint8(offset + 9);
|
|
103
|
+
for (let i = 0; i < 9; i++) {
|
|
104
104
|
const j = i - index;
|
|
105
|
-
seed[j < 0 ? j +
|
|
105
|
+
seed[j < 0 ? j + 9 : j] = this.data.getUint8(offset + i);
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
return seed;
|
|
@@ -127,20 +127,25 @@ class Battle {
|
|
|
127
127
|
const buf = new ArrayBuffer(SIZES.Battle);
|
|
128
128
|
const data = new DataView(buf);
|
|
129
129
|
let offset = OFFSETS.Battle.p1;
|
|
130
|
-
const
|
|
130
|
+
const lastMoves = [];
|
|
131
131
|
for (const side of battle.sides) {
|
|
132
|
-
Side.encode(gen, lookup, battle, side, data, offset);
|
|
132
|
+
Side.encode(gen, !!options.showdown, lookup, battle, side, data, offset);
|
|
133
133
|
offset += SIZES.Side;
|
|
134
|
-
|
|
134
|
+
if (options.showdown) {
|
|
135
|
+
lastMoves.push(+!!side.lastMoveCounterable);
|
|
136
|
+
lastMoves.push((side.lastMoveIndex ?? 0));
|
|
137
|
+
}
|
|
138
|
+
else {
|
|
139
|
+
lastMoves.push((+!!side.lastMoveCounterable << 4) | (side.lastMoveIndex ?? 0));
|
|
140
|
+
}
|
|
135
141
|
}
|
|
136
142
|
data.setUint16(OFFSETS.Battle.turn, battle.turn, data_2.LE);
|
|
137
143
|
data.setUint16(OFFSETS.Battle.last_damage, battle.lastDamage, data_2.LE);
|
|
144
|
+
data.setUint8(OFFSETS.Battle.last_moves, lastMoves[0]);
|
|
145
|
+
data.setUint8(OFFSETS.Battle.last_moves + 1, lastMoves[1]);
|
|
138
146
|
if (options.showdown) {
|
|
139
|
-
data.
|
|
140
|
-
data.
|
|
141
|
-
}
|
|
142
|
-
else {
|
|
143
|
-
data.setUint8(OFFSETS.Battle.last_selected_indexes, (indexes[1] << 4) | indexes[0]);
|
|
147
|
+
data.setUint8(OFFSETS.Battle.last_moves + 2, lastMoves[2]);
|
|
148
|
+
data.setUint8(OFFSETS.Battle.last_moves + 3, lastMoves[3]);
|
|
144
149
|
}
|
|
145
150
|
encodePRNG(data, battle.prng);
|
|
146
151
|
return new Battle(lookup, data, options);
|
|
@@ -201,25 +206,38 @@ class Side {
|
|
|
201
206
|
const m = this.data.getUint8(this.offset + OFFSETS.Side.last_selected_move);
|
|
202
207
|
return m === 0 ? undefined : this.lookup.moveByNum(m);
|
|
203
208
|
}
|
|
204
|
-
get
|
|
209
|
+
get lastMoveIndex() {
|
|
205
210
|
let m;
|
|
206
211
|
if (this.battle.config.showdown) {
|
|
207
212
|
const off = this.id === 'p1' ? 0 : 2;
|
|
208
|
-
m = this.data.
|
|
213
|
+
m = this.data.getUint8(OFFSETS.Battle.last_moves + off);
|
|
209
214
|
}
|
|
210
215
|
else {
|
|
211
|
-
|
|
212
|
-
m = this.
|
|
216
|
+
const off = this.id === 'p1' ? 0 : 1;
|
|
217
|
+
m = this.data.getUint8(OFFSETS.Battle.last_moves + off) & 0x0F;
|
|
213
218
|
}
|
|
214
219
|
return m === 0 ? undefined : m;
|
|
215
220
|
}
|
|
221
|
+
get lastMoveCounterable() {
|
|
222
|
+
let m;
|
|
223
|
+
if (this.battle.config.showdown) {
|
|
224
|
+
const off = this.id === 'p1' ? 1 : 3;
|
|
225
|
+
m = this.data.getUint8(OFFSETS.Battle.last_moves + off);
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
const off = this.id === 'p1' ? 0 : 1;
|
|
229
|
+
m = this.data.getUint8(OFFSETS.Battle.last_moves + off) & 0xF0;
|
|
230
|
+
}
|
|
231
|
+
return m !== 0;
|
|
232
|
+
}
|
|
216
233
|
toJSON() {
|
|
217
234
|
return {
|
|
218
235
|
active: this.active?.toJSON(),
|
|
219
236
|
pokemon: Array.from(this.pokemon).map(p => p.toJSON()),
|
|
220
237
|
lastSelectedMove: this.lastSelectedMove,
|
|
221
238
|
lastUsedMove: this.lastUsedMove,
|
|
222
|
-
|
|
239
|
+
lastMoveIndex: this.lastMoveIndex,
|
|
240
|
+
lastMoveCounterable: this.lastMoveCounterable,
|
|
223
241
|
};
|
|
224
242
|
}
|
|
225
243
|
static init(gen, lookup, team, data, offset) {
|
|
@@ -230,11 +248,11 @@ class Side {
|
|
|
230
248
|
data.setUint8(offset + OFFSETS.Side.order + i, i + 1);
|
|
231
249
|
}
|
|
232
250
|
}
|
|
233
|
-
static encode(gen, lookup, battle, side, data, offset) {
|
|
251
|
+
static encode(gen, showdown, lookup, battle, side, data, offset) {
|
|
234
252
|
let i = 0;
|
|
235
253
|
for (const pokemon of side.pokemon) {
|
|
236
254
|
const off = offset + OFFSETS.Side.pokemon + SIZES.Pokemon * (pokemon.position - 1);
|
|
237
|
-
Pokemon.encodeStored(gen, lookup, pokemon, data, off);
|
|
255
|
+
Pokemon.encodeStored(gen, showdown, lookup, pokemon, data, off);
|
|
238
256
|
data.setUint8(offset + OFFSETS.Side.order + (pokemon.position - 1), i + 1);
|
|
239
257
|
i++;
|
|
240
258
|
}
|
|
@@ -424,13 +442,14 @@ class Pokemon {
|
|
|
424
442
|
return status === 'psn' && this.toxic ? 'tox' : status;
|
|
425
443
|
}
|
|
426
444
|
get statusData() {
|
|
427
|
-
|
|
445
|
+
const sleep = this.sleep;
|
|
446
|
+
return { sleep, self: !!sleep && this.ext, toxic: this.toxic };
|
|
428
447
|
}
|
|
429
448
|
get sleep() {
|
|
430
449
|
const val = this.data.getUint8(this.offset.stored + OFFSETS.Pokemon.status);
|
|
431
450
|
return val & 0b111;
|
|
432
451
|
}
|
|
433
|
-
get
|
|
452
|
+
get ext() {
|
|
434
453
|
return (this.data.getUint8(this.offset.stored + OFFSETS.Pokemon.status) >> 7) === 1;
|
|
435
454
|
}
|
|
436
455
|
get toxic() {
|
|
@@ -480,7 +499,7 @@ class Pokemon {
|
|
|
480
499
|
data.setUint8(stored + OFFSETS.Pokemon.types, encodeTypes(lookup, species.types));
|
|
481
500
|
data.setUint8(stored + OFFSETS.Pokemon.level, set.level ?? 100);
|
|
482
501
|
}
|
|
483
|
-
static encodeStored(gen, lookup, pokemon, data, offset) {
|
|
502
|
+
static encodeStored(gen, showdown, lookup, pokemon, data, offset) {
|
|
484
503
|
let off = 0;
|
|
485
504
|
const stats = pokemon.stored.stats;
|
|
486
505
|
for (const stat of gen.stats) {
|
|
@@ -499,7 +518,7 @@ class Pokemon {
|
|
|
499
518
|
data.setUint8(offset + OFFSETS.Pokemon.species, species.num);
|
|
500
519
|
data.setUint8(offset + OFFSETS.Pokemon.types, encodeTypes(lookup, species.types));
|
|
501
520
|
data.setUint8(offset + OFFSETS.Pokemon.level, pokemon.level);
|
|
502
|
-
data.setUint8(offset + OFFSETS.Pokemon.status, encodeStatus(pokemon));
|
|
521
|
+
data.setUint8(offset + OFFSETS.Pokemon.status, encodeStatus(pokemon, showdown));
|
|
503
522
|
}
|
|
504
523
|
static encodeActive(gen, lookup, battle, pokemon, data, offset) {
|
|
505
524
|
if (!pokemon)
|
|
@@ -620,7 +639,7 @@ class StoredPokemon {
|
|
|
620
639
|
}
|
|
621
640
|
exports.StoredPokemon = StoredPokemon;
|
|
622
641
|
function encodePRNG(data, seed) {
|
|
623
|
-
const offset = OFFSETS.Battle.last_damage + 2 + (seed.length === 4 ? 4 :
|
|
642
|
+
const offset = OFFSETS.Battle.last_damage + 2 + (seed.length === 4 ? 4 : 2);
|
|
624
643
|
if (seed.length === 4) {
|
|
625
644
|
data.setUint16(offset, seed[data_2.LE ? 3 : 0], data_2.LE);
|
|
626
645
|
data.setUint16(offset + 2, seed[data_2.LE ? 2 : 1], data_2.LE);
|
|
@@ -628,7 +647,7 @@ function encodePRNG(data, seed) {
|
|
|
628
647
|
data.setUint16(offset + 6, seed[data_2.LE ? 0 : 3], data_2.LE);
|
|
629
648
|
}
|
|
630
649
|
else {
|
|
631
|
-
for (let i = 0; i <
|
|
650
|
+
for (let i = 0; i < 9; i++) {
|
|
632
651
|
data.setUint8(offset + i, seed[i]);
|
|
633
652
|
}
|
|
634
653
|
// NB: ArrayBuffer is zero initialized already so we don't need to set the index to zero
|
|
@@ -646,18 +665,15 @@ function decodeSigned(n) {
|
|
|
646
665
|
function encodeSigned(n) {
|
|
647
666
|
return (n < 0) ? 0b10000 + n : n;
|
|
648
667
|
}
|
|
649
|
-
function encodeStatus(pokemon) {
|
|
668
|
+
function encodeStatus(pokemon, showdown) {
|
|
650
669
|
if (pokemon.statusData.sleep) {
|
|
651
670
|
if (pokemon.status !== 'slp') {
|
|
652
671
|
throw new Error('Pokemon is not asleep but has non-zero sleep turns');
|
|
653
672
|
}
|
|
654
673
|
return (pokemon.statusData.self ? 0x80 : 0) | pokemon.statusData.sleep;
|
|
655
674
|
}
|
|
656
|
-
else if (pokemon.statusData.self) {
|
|
657
|
-
throw new Error('Pokemon is not asleep but has self-inflicted sleep bit set');
|
|
658
|
-
}
|
|
659
675
|
switch (pokemon.status) {
|
|
660
|
-
case 'tox':
|
|
676
|
+
case 'tox': return showdown ? 0b10001000 : 1 << 3;
|
|
661
677
|
case 'psn': return 1 << 3;
|
|
662
678
|
case 'brn': return 1 << 4;
|
|
663
679
|
case 'frz': return 1 << 5;
|