@pkmn/engine 0.1.0-dev.f3aaee39 → 0.1.0-dev.f3b63b86
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/LICENSE +1 -3
- package/README.md +77 -23
- package/build/pkg/addon/browser.d.ts +3 -0
- package/build/pkg/addon/browser.js +47 -0
- package/build/pkg/addon/browser.js.map +1 -0
- package/build/pkg/addon/node.d.ts +3 -0
- package/build/pkg/addon/node.js +110 -0
- package/build/pkg/addon/node.js.map +1 -0
- package/build/pkg/addon/wasm.d.ts +2 -0
- package/build/pkg/addon/wasm.js +53 -0
- package/build/pkg/addon/wasm.js.map +1 -0
- package/build/pkg/addon.d.ts +26 -4
- package/build/pkg/addon.js +39 -71
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +17 -4
- package/build/pkg/data/protocol.json +84 -13
- package/build/pkg/data.d.ts +27 -5
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +15 -18
- package/build/pkg/gen1.js +30 -21
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +42 -91
- package/build/pkg/index.js +23 -70
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +20 -1
- package/build/pkg/protocol.js +123 -102
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +69 -74
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +152 -0
- package/build/tools/display/debug.jsx.map +1 -0
- package/build/tools/display/index.js +152 -0
- package/build/tools/display/index.js.map +1 -0
- package/build/tools/display/ui.jsx +359 -0
- package/build/tools/display/ui.jsx.map +1 -0
- package/build/tools/display/util.js +33 -0
- package/build/tools/display/util.js.map +1 -0
- package/build.zig +273 -120
- package/package.json +64 -123
- package/src/bin/install-pkmn-engine +47 -21
- package/src/bin/pkmn-debug +1 -1
- package/src/lib/bindings/c.zig +200 -0
- package/src/lib/{binding → bindings}/node.zig +15 -32
- package/src/lib/bindings/wasm.zig +50 -0
- package/src/lib/common/array.zig +70 -0
- package/src/lib/common/data.zig +12 -24
- package/src/lib/common/debug.zig +32 -11
- package/src/lib/common/js.zig +3 -7
- package/src/lib/common/optional.zig +19 -18
- package/src/lib/common/options.zig +6 -0
- package/src/lib/common/protocol.zig +919 -430
- package/src/lib/common/rational.zig +341 -254
- package/src/lib/common/rng.zig +22 -15
- package/src/lib/common/util.zig +33 -1
- package/src/lib/gen1/calc.zig +706 -177
- package/src/lib/gen1/chance.zig +428 -302
- package/src/lib/gen1/data/moves.zig +193 -183
- package/src/lib/gen1/data/species.zig +4 -6
- package/src/lib/gen1/data/types.zig +9 -10
- package/src/lib/gen1/data.zig +60 -75
- package/src/lib/gen1/helpers.zig +90 -563
- package/src/lib/gen1/mechanics.zig +682 -600
- package/src/lib/gen2/calc.zig +150 -0
- package/src/lib/gen2/chance.zig +896 -0
- package/src/lib/gen2/data/items.zig +13 -21
- package/src/lib/gen2/data/moves.zig +664 -572
- package/src/lib/gen2/data/species.zig +2 -4
- package/src/lib/gen2/data/types.zig +139 -34
- package/src/lib/gen2/data.zig +121 -96
- package/src/lib/gen2/generated.zig +801 -0
- package/src/lib/gen2/helpers.zig +53 -38
- package/src/lib/gen2/mechanics.zig +3580 -29
- package/src/lib/napi.h +2 -0
- package/src/lib/node.zig +14 -0
- package/src/lib/pkmn.zig +125 -29
- package/src/lib/wasm.zig +13 -0
- package/src/tools/display/dom.ts +93 -0
- package/src/tools/display/ui.css +354 -0
- package/build/test/display.js +0 -400
- package/build/test/display.js.map +0 -1
- package/src/lib/binding/wasm.zig +0 -12
- package/src/lib/gen1/README.md +0 -892
- package/src/lib/gen2/README.md +0 -308
- package/src/test/display/pkmn.html.tmpl +0 -368
package/LICENSE
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2021-2023 pkmn contributors
|
|
1
|
+
Copyright (c) 2021-2026 pkmn contributors
|
|
4
2
|
|
|
5
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
6
4
|
this software and associated documentation files (the "Software"), to deal in
|
package/README.md
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
<br />
|
|
4
4
|
<br />
|
|
5
5
|
<a href="https://github.com/pkmn/engine/actions/workflows/test.yml">
|
|
6
|
-
<img alt="Test Status" src="https://github.com/pkmn/engine/workflows/
|
|
6
|
+
<img alt="Test Status" src="https://github.com/pkmn/engine/actions/workflows/test.yml/badge.svg" />
|
|
7
7
|
</a>
|
|
8
8
|
<a href="#status">
|
|
9
9
|
<img alt="WIP" src="https://img.shields.io/badge/status-WIP-red.svg" />
|
|
@@ -20,6 +20,10 @@
|
|
|
20
20
|
> [`old`](https://github.com/pkmn/engine/releases/tag/old) tag can also be used to experiment with a
|
|
21
21
|
> more stable version of the codebase).
|
|
22
22
|
|
|
23
|
+
> [!IMPORTANT]
|
|
24
|
+
> While this project aims to support the most recent stable Zig release and the Zig master branch,
|
|
25
|
+
> **only the Zig master branch is currently supported** due to breaking changes upstream.
|
|
26
|
+
|
|
23
27
|
<hr />
|
|
24
28
|
|
|
25
29
|
A minimal, complete, Pokémon battle simulation engine optimized for performance and
|
|
@@ -57,9 +61,14 @@ $ zig build --prefix /usr/local -Doptimize=ReleaseFast
|
|
|
57
61
|
```
|
|
58
62
|
|
|
59
63
|
The Zig website has [installation instructions](https://ziglang.org/learn/getting-started/) which
|
|
60
|
-
walk through how to install Zig on each platform - the engine code should work on Zig v0.
|
|
61
|
-
|
|
62
|
-
|
|
64
|
+
walk through how to install Zig on each platform - ~~the engine code should work on Zig v0.14.0,
|
|
65
|
+
though tracks Zig's master branch so this may change in the future if breaking language changes are
|
|
66
|
+
introduced~~ the engine code currently only works on the Zig master branch due to breaking language
|
|
67
|
+
changes that have been introduced. Note that **due to a [bug in the Zig
|
|
68
|
+
compiler](https://github.com/ziglang/zig/issues/17768), building a version of [Zig from
|
|
69
|
+
source](https://codeberg.org/ziglang/zig#building-from-source) after having applied a
|
|
70
|
+
[patch](zig.patch) to revert [ziglang/zig#17391](https://github.com/ziglang/zig/pull/17391) is
|
|
71
|
+
recommended for performance**.
|
|
63
72
|
|
|
64
73
|
`libpkmn` can be built with `-Dshowdown` to instead produce the Pokémon Showdown compatible
|
|
65
74
|
`libpkmn-showdown` library. Furthermore, protocol message logging can be enabled through `-Dlog`.
|
|
@@ -87,25 +96,69 @@ directly or build the addons manually and place the artifacts in the expected pa
|
|
|
87
96
|
|
|
88
97
|
### `pkmn`
|
|
89
98
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
99
|
+
To include `pkmn`, add it as a dependency in your project's
|
|
100
|
+
[`build.zig.zon`](https://github.com/ziglang/zig/blob/master/doc/build.zig.zon.md) using [Zig's
|
|
101
|
+
Package Management
|
|
102
|
+
system](https://ziglang.org/download/0.11.0/release-notes.html#Package-Management):
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
```zig
|
|
106
|
+
.dependencies = .{
|
|
107
|
+
.pkmn = .{
|
|
108
|
+
.url = "https://github.com/pkmn/engine/archive/RELEASE.tar.gz",
|
|
109
|
+
},
|
|
110
|
+
},
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Replace `RELEASE` with the tag for the [release](https://github.com/pkmn/engine/releases) you wish
|
|
114
|
+
to use (e.g. `0.1.0` or `nightly`) or the specific hash of any commit. Next, run `zig build` and
|
|
115
|
+
copy the expected hash from the error message into your `build.zig.zon` alongside the `.url`:
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
$ zig build
|
|
119
|
+
error: dependency is missing hash field
|
|
120
|
+
.url = "https://github.com/pkmn/engine/archive/nightly.tar.gz",
|
|
121
|
+
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
122
|
+
note: expected .hash = "122056b93b403033cb7d9bed96a02c2eb8cc1275515976167726ada6eb4207e8ef8a",
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
You must add `build.zig.zon` to the engine package to be able to import it in your `build.zig`:
|
|
126
|
+
|
|
127
|
+
```zig
|
|
128
|
+
.{
|
|
129
|
+
.name = .pkmn,
|
|
130
|
+
.version = "0.1.0-dev",
|
|
131
|
+
.minimum_zig_version = "0.16.0",
|
|
132
|
+
.paths = .{
|
|
133
|
+
"build.zig",
|
|
134
|
+
"docs",
|
|
135
|
+
"LICENSE",
|
|
136
|
+
"README.md",
|
|
137
|
+
"src/include",
|
|
138
|
+
"src/lib",
|
|
139
|
+
},
|
|
140
|
+
.fingerprint = 0x74a002f44f200a47,
|
|
141
|
+
}
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Note that this hash is **not** going to be the same as the commit hash. After adding the hash to
|
|
145
|
+
your `build.zig.zon` you will be able to import the `pkmn` package's build helpers into your
|
|
146
|
+
`build.zig`:
|
|
94
147
|
|
|
95
148
|
```zig
|
|
96
149
|
const std = @import("std");
|
|
97
|
-
const pkmn = @import("lib/pkmn/build.zig");
|
|
98
150
|
|
|
99
151
|
pub fn build(b: *std.Build) void {
|
|
100
152
|
...
|
|
101
|
-
|
|
153
|
+
const pkmn = b.dependency("pkmn", .{ .showdown = true, .log = true });
|
|
154
|
+
exe.root_module.addImport("pkmn", pkmn.module("pkmn"));
|
|
102
155
|
...
|
|
103
156
|
}
|
|
104
157
|
```
|
|
105
158
|
|
|
106
|
-
The
|
|
107
|
-
|
|
108
|
-
|
|
159
|
+
The `pkmn` package can be configured with various options such as whether or not Pokémon Showdown
|
|
160
|
+
compatibility mode or protocol message logging should be enabled. Alternatively, you may set options
|
|
161
|
+
via a `pkmn_options` [root source file
|
|
109
162
|
declaration](https://ziglang.org/documentation/master/#Root-Source-File). There are several
|
|
110
163
|
undocumented internal options that can be tweaked as well via build or root options, though these
|
|
111
164
|
options aren't officially supported, affect correctness, and may change meaning or behavior without
|
|
@@ -142,7 +195,7 @@ documentation](docs/PROTOCOL.md) goes into greater detail on the specifics of up
|
|
|
142
195
|
potential logs that may result.
|
|
143
196
|
|
|
144
197
|
*The snippets below are meant to merely illustrate in broad strokes how the pkmn engine can be used
|
|
145
|
-
\- the [`examples`](
|
|
198
|
+
\- the [`examples`](examples) directory contains fully commented and runnable code.*
|
|
146
199
|
|
|
147
200
|
### C
|
|
148
201
|
|
|
@@ -167,7 +220,7 @@ while (!pkmn_result_type(result = pkmn_battle_update(&battle, c1, c2, &options))
|
|
|
167
220
|
if (pkmn_error(result)) exit(1);
|
|
168
221
|
```
|
|
169
222
|
|
|
170
|
-
[(full code)](
|
|
223
|
+
[(full code)](examples/c/example.c)
|
|
171
224
|
|
|
172
225
|
The C API doesn't export any helpers for creating or accessing the opaque battle objects - it's
|
|
173
226
|
instead intended to be used as the foundation for more ergonomic bindings in [other
|
|
@@ -185,24 +238,25 @@ protocol via `Log.parse`.
|
|
|
185
238
|
```ts
|
|
186
239
|
import {Dex} from '@pkmn/dex';
|
|
187
240
|
import {Generations} from '@pkmn/data';
|
|
188
|
-
import {Battle} from '@pkmn/engine';
|
|
241
|
+
import {Battle, Choice} from '@pkmn/engine';
|
|
189
242
|
|
|
190
243
|
const gens = new Generations(Dex);
|
|
191
244
|
const battle = Battle.create(...);
|
|
192
245
|
|
|
193
|
-
const choose = (
|
|
246
|
+
const choose = (choices: Choice[]) =>
|
|
247
|
+
choices[Math.floor(Math.random() * choices.length)];
|
|
194
248
|
|
|
195
249
|
let result: Result;
|
|
196
250
|
let c1: Choice, c2: Choice;
|
|
197
251
|
while (!(result = battle.update(c1, c2)).type) {
|
|
198
|
-
c1 = battle.
|
|
199
|
-
c2 = battle.
|
|
252
|
+
c1 = choose(battle.choices('p1', result));
|
|
253
|
+
c2 = choose(battle.choices('p2', result));
|
|
200
254
|
}
|
|
201
255
|
|
|
202
256
|
console.log(result);
|
|
203
257
|
```
|
|
204
258
|
|
|
205
|
-
[(full code)](
|
|
259
|
+
[(full code)](examples/js/example.ts)
|
|
206
260
|
|
|
207
261
|
By default, the `@pkmn/engine` package compiles the engine with `-Dshowdown`, though by running
|
|
208
262
|
`install-pkmn-engine` directly and passing in `--options` you can override this default and build
|
|
@@ -214,7 +268,7 @@ library should be seamless.
|
|
|
214
268
|
|
|
215
269
|
Despite relying on the native engine code, the `@pkmn/engine` code is designed to also work in
|
|
216
270
|
browsers which support [WebAssembly](https://webassembly.org/). Running `npm run start:web` from the
|
|
217
|
-
[`examples`](
|
|
271
|
+
[`examples`](examples/js) directory starts a server that can be used to demonstrate the engine
|
|
218
272
|
running in the browser.
|
|
219
273
|
|
|
220
274
|
### Zig
|
|
@@ -226,7 +280,7 @@ be used when logging is enabled to allow for easily printing e.g. to standard ou
|
|
|
226
280
|
const std = @import("std");
|
|
227
281
|
const pkmn = @import("pkmn");
|
|
228
282
|
|
|
229
|
-
var random = std.
|
|
283
|
+
var random = std.Random.DefaultPrng.init(seed).random();
|
|
230
284
|
var choices: [pkmn.CHOICES_SIZE]pkmn.Choice = undefined;
|
|
231
285
|
|
|
232
286
|
var battle = ...
|
|
@@ -244,7 +298,7 @@ while (result.type == .None) : (result = try battle.update(c1, c2, &options)) {
|
|
|
244
298
|
std.debug.print("{}\n", .{result.type});
|
|
245
299
|
```
|
|
246
300
|
|
|
247
|
-
[(full code)](
|
|
301
|
+
[(full code)](examples/zig/example.zig)
|
|
248
302
|
|
|
249
303
|
The Zig package also supports some APIs which are difficult to expose elsewhere such as the
|
|
250
304
|
`FixedRNG` which allows you to fully specify the exact RNG frames (which can be useful for ensuring
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.load = load;
|
|
4
|
+
exports.loadSync = loadSync;
|
|
5
|
+
const wasm_1 = require("./wasm");
|
|
6
|
+
async function load(showdown, addon) {
|
|
7
|
+
if (typeof addon === 'string' && addon !== 'wasm') {
|
|
8
|
+
throw new Error('Unable to load native addons in the browser!');
|
|
9
|
+
}
|
|
10
|
+
let wasm;
|
|
11
|
+
if (addon instanceof WebAssembly.Instance) {
|
|
12
|
+
wasm = addon;
|
|
13
|
+
}
|
|
14
|
+
else if (addon instanceof WebAssembly.Module) {
|
|
15
|
+
try {
|
|
16
|
+
wasm = (await WebAssembly.instantiate(addon));
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
if (!(err instanceof Error))
|
|
20
|
+
throw err;
|
|
21
|
+
throw new Error(`Could not instantiate WASM module!\n${err.message}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
const name = `pkmn${showdown ? '-showdown' : ''}.wasm`;
|
|
26
|
+
try {
|
|
27
|
+
const response = !addon || addon === 'wasm' ? fetch(name)
|
|
28
|
+
: addon instanceof URL ? fetch(addon) : addon;
|
|
29
|
+
wasm = (await WebAssembly.instantiateStreaming(response)).instance;
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
if (!(err instanceof Error))
|
|
33
|
+
throw err;
|
|
34
|
+
const message = !addon || addon === 'wasm'
|
|
35
|
+
? `Could not find ${name} - did you run \`npx install-pkmn-engine\`?`
|
|
36
|
+
: addon instanceof URL
|
|
37
|
+
? `Could not fetch WASM module from '${addon.href}'!`
|
|
38
|
+
: 'Could not instantiate WASM module!';
|
|
39
|
+
throw new Error(`${message}\n${err.message}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return (0, wasm_1.toBindings)(wasm.exports);
|
|
43
|
+
}
|
|
44
|
+
function loadSync() {
|
|
45
|
+
throw new Error('You must `initialize` the WASM addon before using the engine!');
|
|
46
|
+
}
|
|
47
|
+
//# sourceMappingURL=browser.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser.js","sourceRoot":"","sources":["../../../src/pkg/addon/browser.ts"],"names":[],"mappings":";;AAKA,oBAiCC;AAED,4BAEC;AAvCD,iCAAkC;AAE3B,KAAK,UAAU,IAAI,CAAC,QAAiB,EAA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AK,MAAM;gBACxC,CAAC,CAAC,kBAAkB,IAAI,6CAA6C;gBACrE,CAAC,CAAE,KAAiC,YAAY,GAAG;oBACjD,CAAC,CAAC,qCAAsC,KAAa,CAAC,IAAI,IAAI;oBAC9D,CAAC,CAAC,oCAAoC,CAAC;YAC3C,MAAM,IAAI,KAAK,CAAC,GAAG,OAAO,KAAK,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;QAChD,CAAC;IACH,CAAC;IAED,OAAO,IAAA,iBAAU,EAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAClC,CAAC;AAED,SAAgB,QAAQ;IACtB,MAAM,IAAI,KAAK,CAAC,+DAA+D,CAAC,CAAC;AACnF,CAAC"}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.load = load;
|
|
37
|
+
exports.loadSync = loadSync;
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
39
|
+
const path = __importStar(require("path"));
|
|
40
|
+
const wasm_1 = require("./wasm");
|
|
41
|
+
const ROOT = path.join(__dirname, '..', '..', '..');
|
|
42
|
+
const LIB = path.join(ROOT, 'build', 'lib');
|
|
43
|
+
const NODE = [path.join(LIB, 'pkmn.node'), path.join(LIB, 'pkmn-showdown.node')];
|
|
44
|
+
const WASM = [path.join(LIB, 'pkmn.wasm'), path.join(LIB, 'pkmn-showdown.wasm')];
|
|
45
|
+
async function load(showdown, addon) {
|
|
46
|
+
if (addon === undefined || typeof addon === 'string' && addon !== 'wasm') {
|
|
47
|
+
try {
|
|
48
|
+
return require(!addon || addon === 'node' ? NODE[+showdown] : addon).engine;
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (!(err instanceof Error))
|
|
52
|
+
return err;
|
|
53
|
+
if (addon && addon !== 'node') {
|
|
54
|
+
throw new Error(`Unable to load native addon: '${addon}'\n${err.message}`);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
throw error(NODE[+showdown], err);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
let wasm;
|
|
62
|
+
if (addon === 'wasm') {
|
|
63
|
+
try {
|
|
64
|
+
wasm = (await WebAssembly.instantiate(fs.readFileSync(WASM[+showdown]))).instance;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
throw error(WASM[+showdown], err);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (addon instanceof WebAssembly.Instance) {
|
|
71
|
+
wasm = addon;
|
|
72
|
+
}
|
|
73
|
+
else if (addon instanceof WebAssembly.Module) {
|
|
74
|
+
try {
|
|
75
|
+
wasm = (await WebAssembly.instantiate(addon));
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
if (!(err instanceof Error))
|
|
79
|
+
throw err;
|
|
80
|
+
throw new Error(`Could not instantiate WASM module!\n${err.message}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
try {
|
|
85
|
+
const response = addon instanceof Promise ? addon : fetch(addon);
|
|
86
|
+
wasm = (await WebAssembly.instantiateStreaming(response)).instance;
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
if (!(err instanceof Error))
|
|
90
|
+
throw err;
|
|
91
|
+
throw new Error(`Could not instantiate WASM module!\n${err.message}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return (0, wasm_1.toBindings)(wasm.exports);
|
|
95
|
+
}
|
|
96
|
+
function loadSync(showdown) {
|
|
97
|
+
try {
|
|
98
|
+
return require(NODE[+showdown]).engine;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
return undefined;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
function error(file, err) {
|
|
105
|
+
if (!(err instanceof Error))
|
|
106
|
+
return err;
|
|
107
|
+
const message = `Could not find ${path.basename(file)} addon`;
|
|
108
|
+
return new Error(`${message} - did you run \`npx install-pkmn-engine\`?\n${err.message}`);
|
|
109
|
+
}
|
|
110
|
+
//# sourceMappingURL=node.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"node.js","sourceRoot":"","sources":["../../../src/pkg/addon/node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AAI,CAAC,CAAC,GAAG,YAAY,KAAK,CAAC;gBAAE,MAAM,GAAG,CAAC;YACvC,MAAM,IAAI,KAAK,CAAC,uCAAuC,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;QACxE,CAAC;IACH,CAAC;IAED,OAAO,IAAA,iBAAU,EAAC,IAAI,CAAC,OAAO,CAAC,CAAC;AAClC,CAAC;AAED,SAAgB,QAAQ,CAAC,QAAiB;IACxC,IAAI,CAAC;QACH,OAAO,OAAO,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC;IACzC,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED,SAAS,KAAK,CAAC,IAAY,EAAE,GAAY;IACvC,IAAI,CAAC,CAAC,GAAG,YAAY,KAAK,CAAC;QAAE,OAAO,GAAG,CAAC;IACxC,MAAM,OAAO,GAAG,kBAAkB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC;IAC9D,OAAO,IAAI,KAAK,CAAC,GAAG,OAAO,gDAAgD,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;AAC5F,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toBindings = toBindings;
|
|
4
|
+
const data_1 = require("../data");
|
|
5
|
+
function toBindings(w) {
|
|
6
|
+
const memory = new Uint8Array(w.memory.buffer);
|
|
7
|
+
return {
|
|
8
|
+
options: {
|
|
9
|
+
showdown: !!memory[w.SHOWDOWN.valueOf()],
|
|
10
|
+
log: !!memory[w.LOG.valueOf()],
|
|
11
|
+
chance: !!memory[w.CHANCE.valueOf()],
|
|
12
|
+
calc: !!memory[w.CALC.valueOf()],
|
|
13
|
+
},
|
|
14
|
+
bindings: [toBinding(1, w)],
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
function toBinding(gen, w) {
|
|
18
|
+
const prefix = `GEN${gen}`;
|
|
19
|
+
const buf = w.memory.buffer;
|
|
20
|
+
const constants = new Uint32Array(buf);
|
|
21
|
+
const CHOICES_SIZE = constants[w[`${prefix}_CHOICES_SIZE`].valueOf() / 4];
|
|
22
|
+
const LOGS_SIZE = constants[w[`${prefix}_LOGS_SIZE`].valueOf() / 4];
|
|
23
|
+
const size = data_1.LAYOUT[gen - 1].sizes.Battle;
|
|
24
|
+
const update = w[`${prefix}_update`];
|
|
25
|
+
const choices = w[`${prefix}_choices`];
|
|
26
|
+
const memory = new Uint8Array(buf);
|
|
27
|
+
return {
|
|
28
|
+
CHOICES_SIZE,
|
|
29
|
+
LOGS_SIZE,
|
|
30
|
+
update(battle, c1, c2, log) {
|
|
31
|
+
const bytes = new Uint8Array(battle);
|
|
32
|
+
memory.set(bytes, 0);
|
|
33
|
+
let result;
|
|
34
|
+
if (log) {
|
|
35
|
+
result = update(0, c1, c2, size);
|
|
36
|
+
new Uint8Array(log).set(memory.subarray(size, size + LOGS_SIZE));
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
result = update(0, c1, c2, 0);
|
|
40
|
+
}
|
|
41
|
+
bytes.set(memory.subarray(0, size));
|
|
42
|
+
return result;
|
|
43
|
+
},
|
|
44
|
+
choices(battle, player, request, options) {
|
|
45
|
+
const opts = new Uint8Array(options);
|
|
46
|
+
memory.set(new Uint8Array(battle));
|
|
47
|
+
const n = choices(0, player, request, size);
|
|
48
|
+
opts.set(memory.subarray(size, size + n));
|
|
49
|
+
return n;
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
//# sourceMappingURL=wasm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wasm.js","sourceRoot":"","sources":["../../../src/pkg/addon/wasm.ts"],"names":[],"mappings":";;AAGA,gCAWC;AAbD,kCAA+B;AAE/B,SAAgB,UAAU,CAAoB,CAAsB;IAClE,MAAM,MAAM,G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
CHANGED
|
@@ -1,7 +1,29 @@
|
|
|
1
|
-
import { Choice, Player, Result } from '
|
|
1
|
+
import { Choice, Player, Result } from './common';
|
|
2
|
+
export type Argument = string | URL | WebAssembly.Module | WebAssembly.Instance | Promise<Response>;
|
|
3
|
+
export interface Bindings<T extends boolean> {
|
|
4
|
+
/**
|
|
5
|
+
* The compile-time options the bindings were built with. showdown is special
|
|
6
|
+
* cased because it changes the name of addon.
|
|
7
|
+
*/
|
|
8
|
+
options: {
|
|
9
|
+
showdown: T;
|
|
10
|
+
log: boolean;
|
|
11
|
+
chance: boolean;
|
|
12
|
+
calc: boolean;
|
|
13
|
+
};
|
|
14
|
+
/** Bindings are per-generation, Generation I is index 0. */
|
|
15
|
+
bindings: Binding[];
|
|
16
|
+
}
|
|
17
|
+
export interface Binding {
|
|
18
|
+
CHOICES_SIZE: number;
|
|
19
|
+
LOGS_SIZE: number;
|
|
20
|
+
update(battle: ArrayBufferLike, c1: number, c2: number, log: ArrayBufferLike | undefined): number;
|
|
21
|
+
choices(battle: ArrayBufferLike, player: number, request: number, out: ArrayBufferLike): number;
|
|
22
|
+
}
|
|
23
|
+
/** TODO */
|
|
24
|
+
export declare function initialize(showdown: boolean, addon?: Argument): Promise<void | undefined>;
|
|
2
25
|
export declare function check(showdown: boolean): void;
|
|
3
26
|
export declare function supports(showdown: boolean, log?: boolean): boolean;
|
|
4
|
-
export declare function update(index: number, showdown: boolean, battle:
|
|
5
|
-
export declare function choices(index: number, showdown: boolean, battle:
|
|
6
|
-
export declare function choose(index: number, showdown: boolean, battle: ArrayBuffer, player: Player, choice: Choice['type'], buf: ArrayBuffer, fn: (n: number) => number): Choice;
|
|
27
|
+
export declare function update(index: number, showdown: boolean, battle: ArrayBufferLike, c1?: Choice, c2?: Choice, log?: ArrayBufferLike): Result;
|
|
28
|
+
export declare function choices(index: number, showdown: boolean, battle: ArrayBufferLike, player: Player, choice: Choice['type'], out: Uint8Array): Choice[];
|
|
7
29
|
export declare function size(index: number, type: 'choices' | 'log'): number;
|
package/build/pkg/addon.js
CHANGED
|
@@ -1,78 +1,58 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
showdown =
|
|
42
|
-
require(path.join(ROOT, 'build', 'lib', 'pkmn-showdown.node')).engine;
|
|
43
|
-
}
|
|
44
|
-
catch { }
|
|
45
|
-
if (!pkmn && !showdown) {
|
|
46
|
-
throw new Error('Could not find native addons - did you run `npx install-pkmn-engine`?');
|
|
3
|
+
exports.initialize = initialize;
|
|
4
|
+
exports.check = check;
|
|
5
|
+
exports.supports = supports;
|
|
6
|
+
exports.update = update;
|
|
7
|
+
exports.choices = choices;
|
|
8
|
+
exports.size = size;
|
|
9
|
+
const node_1 = require("./addon/node");
|
|
10
|
+
const common_1 = require("./common");
|
|
11
|
+
const ADDONS = [];
|
|
12
|
+
const loading = [];
|
|
13
|
+
/** TODO */
|
|
14
|
+
async function initialize(showdown, addon) {
|
|
15
|
+
if (loading[+showdown]) {
|
|
16
|
+
throw new Error(`Cannot call initialize more than once with showdown=${showdown}`);
|
|
47
17
|
}
|
|
48
|
-
|
|
18
|
+
loading[+showdown] = (0, node_1.load)(showdown, addon);
|
|
19
|
+
loading[+showdown].then(a => {
|
|
20
|
+
ADDONS[+showdown] = a;
|
|
21
|
+
}).catch(() => {
|
|
22
|
+
loading[+showdown] = undefined;
|
|
23
|
+
});
|
|
24
|
+
return loading[+showdown]?.then(() => { });
|
|
49
25
|
}
|
|
50
26
|
function check(showdown) {
|
|
51
|
-
if (!
|
|
52
|
-
const opts =
|
|
27
|
+
if (!addons(showdown)[+showdown]) {
|
|
28
|
+
const opts = ADDONS[+!showdown].options.log ? ['-log'] : [];
|
|
53
29
|
if (showdown)
|
|
54
30
|
opts.push('-Dshowdown');
|
|
55
31
|
throw new Error(`@pkmn/engine has ${showdown ? 'not' : 'only'} been configured to support Pokémon Showdown.` +
|
|
56
32
|
`\n(running \`npx install-pkmn-engine --options='${opts.join(' ')}'\` can fix this issue).`);
|
|
57
33
|
}
|
|
58
34
|
}
|
|
59
|
-
exports.check = check;
|
|
60
35
|
function supports(showdown, log) {
|
|
61
|
-
if (!
|
|
36
|
+
if (!addons(showdown)[+showdown])
|
|
62
37
|
return false;
|
|
63
38
|
if (log === undefined)
|
|
64
39
|
return true;
|
|
65
|
-
return
|
|
40
|
+
return ADDONS[+showdown].options.log === log;
|
|
41
|
+
}
|
|
42
|
+
function addons(showdown) {
|
|
43
|
+
if (ADDONS[+showdown])
|
|
44
|
+
return ADDONS;
|
|
45
|
+
// If we havem't been initialized attempt to autoload if we're on Node
|
|
46
|
+
ADDONS[+showdown] = (0, node_1.loadSync)(showdown);
|
|
47
|
+
return ADDONS;
|
|
66
48
|
}
|
|
67
|
-
exports.supports = supports;
|
|
68
49
|
function update(index, showdown, battle, c1, c2, log) {
|
|
69
|
-
return
|
|
70
|
-
.update(battle,
|
|
50
|
+
return common_1.Result.decode(ADDONS[+showdown].bindings[index]
|
|
51
|
+
.update(battle, common_1.Choice.encode(c1), common_1.Choice.encode(c2), log));
|
|
71
52
|
}
|
|
72
|
-
|
|
73
|
-
function choices(index, showdown, battle, player, choice, buf) {
|
|
53
|
+
function choices(index, showdown, battle, player, choice, out) {
|
|
74
54
|
const request = choice[0] === 'p' ? 0 : choice[0] === 'm' ? 1 : 2;
|
|
75
|
-
const n =
|
|
55
|
+
const n = ADDONS[+showdown].bindings[index].choices(battle, +(player !== 'p1'), request, out.buffer);
|
|
76
56
|
// The top-level API signature means our hands our tied with respect to
|
|
77
57
|
// writing really fast bindings here. The simplest approach would be to return
|
|
78
58
|
// the ArrayBuffer the bindings populate as well as its size and only decode a
|
|
@@ -87,26 +67,14 @@ function choices(index, showdown, battle, player, choice, buf) {
|
|
|
87
67
|
// choices function for each generation within node.zig. We could do something
|
|
88
68
|
// really galaxy-brained and return some sort of frankenstein subclass of
|
|
89
69
|
// Array backed by ArrayBuffer which would lazily decode the Choice on access,
|
|
90
|
-
// but thats ultimately not worth the effort.
|
|
91
|
-
// idiomatic API and performance here, hence why the choose function below
|
|
92
|
-
// exists.
|
|
70
|
+
// but thats ultimately not worth the effort.
|
|
93
71
|
const options = new Array(n);
|
|
94
|
-
const data = new Uint8Array(buf);
|
|
95
72
|
for (let i = 0; i < n; i++)
|
|
96
|
-
options[i] =
|
|
73
|
+
options[i] = common_1.Choice.decode(out[i]);
|
|
97
74
|
return options;
|
|
98
75
|
}
|
|
99
|
-
exports.choices = choices;
|
|
100
|
-
function choose(index, showdown, battle, player, choice, buf, fn) {
|
|
101
|
-
const request = choice[0] === 'p' ? 0 : choice[0] === 'm' ? 1 : 2;
|
|
102
|
-
const n = ADDON[+showdown].bindings[index].choices(battle, +(player !== 'p1'), request, buf);
|
|
103
|
-
const data = new Uint8Array(buf);
|
|
104
|
-
return _1.Choice.decode(data[fn(n)]);
|
|
105
|
-
}
|
|
106
|
-
exports.choose = choose;
|
|
107
76
|
function size(index, type) {
|
|
108
|
-
const bindings = (
|
|
77
|
+
const bindings = (ADDONS[1] ?? ADDONS[0]).bindings[index];
|
|
109
78
|
return type[0] === 'c' ? bindings.CHOICES_SIZE : bindings.LOGS_SIZE;
|
|
110
79
|
}
|
|
111
|
-
exports.size = size;
|
|
112
80
|
//# sourceMappingURL=addon.js.map
|
package/build/pkg/addon.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addon.js","sourceRoot":"","sources":["../../src/pkg/addon.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"addon.js","sourceRoot":"","sources":["../../src/pkg/addon.ts"],"names":[],"mappings":";;AA0BA,gCAWC;AAED,sBASC;AAED,4BAIC;AASD,wBAUC;AAED,0BA6BC;AAED,oBAGC;AA7GD,uCAA4C;AAC5C,qCAAgD;AAqBhD,MAAM,MAAM,GAAwC,EAAE,CAAC;AACvD,MAAM,OAAO,GAA0D,EAAE,CAAC;AAE1E,WAAW;AACJ,KAAK,UAAU,UAAU,CAAC,QAAiB,EAAE,KAAgB;IAClE,IAAI,OAAO,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CAAC,uDAAuD,QAAQ,EAAE,CAAC,CAAC;IACrF,CAAC;IACD,OAAO,CAAC,CAAC,QAAQ,CAAC,GAAG,IAAA,WAAI,EAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;I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