@pkmn/engine 0.1.0-dev.44a51703 → 0.1.0-dev.46307385
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 +53 -42
- package/build/pkg/addon.js +3 -2
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/layout.json +24 -2
- package/build/pkg/gen1.js +13 -16
- package/build/pkg/gen1.js.map +1 -1
- package/build.zig +75 -54
- package/package.json +23 -14
- package/src/bin/install-pkmn-engine +10 -5
- package/src/lib/binding/node.zig +39 -131
- package/src/lib/binding/wasm.zig +2 -0
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/debug.zig +20 -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 +48 -0
- package/src/lib/common/rational.zig +353 -0
- package/src/lib/common/rng.zig +5 -5
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/README.md +254 -176
- package/src/lib/gen1/calc.zig +427 -0
- package/src/lib/gen1/chance.zig +1044 -0
- package/src/lib/gen1/data.zig +45 -24
- package/src/lib/gen1/helpers.zig +581 -10
- package/src/lib/gen1/mechanics.zig +726 -448
- package/src/lib/gen2/README.md +12 -13
- package/src/lib/gen2/data.zig +22 -12
- package/src/lib/gen2/helpers.zig +3 -5
- package/src/lib/gen2/mechanics.zig +5 -4
- package/src/lib/pkmn.zig +61 -2
package/build.zig
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
2
|
const builtin = @import("builtin");
|
|
3
3
|
|
|
4
|
-
pub const Options = struct {
|
|
4
|
+
pub const Options = struct {
|
|
5
|
+
showdown: ?bool = null,
|
|
6
|
+
log: ?bool = null,
|
|
7
|
+
chance: ?bool = null,
|
|
8
|
+
calc: ?bool = null,
|
|
9
|
+
};
|
|
5
10
|
|
|
6
11
|
pub fn module(b: *std.Build, options: Options) *std.Build.Module {
|
|
7
12
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
8
13
|
const build_options = b.addOptions();
|
|
9
14
|
build_options.addOption(?bool, "showdown", options.showdown);
|
|
10
15
|
build_options.addOption(?bool, "log", options.log);
|
|
16
|
+
build_options.addOption(?bool, "chance", options.chance);
|
|
17
|
+
build_options.addOption(?bool, "calc", options.calc);
|
|
11
18
|
return b.createModule(.{
|
|
12
19
|
.source_file = .{ .path = dirname ++ "/src/lib/pkmn.zig" },
|
|
13
20
|
.dependencies = &.{.{ .name = "build_options", .module = build_options.createModule() }},
|
|
@@ -28,6 +35,7 @@ pub fn build(b: *std.Build) !void {
|
|
|
28
35
|
const strip = b.option(bool, "strip", "Strip debugging symbols from binary") orelse false;
|
|
29
36
|
const pic = b.option(bool, "pic", "Force position independent code") orelse false;
|
|
30
37
|
const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
|
|
38
|
+
const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
|
|
31
39
|
|
|
32
40
|
const cmd = b.findProgram(&.{"strip"}, &.{}) catch null;
|
|
33
41
|
|
|
@@ -42,10 +50,14 @@ pub fn build(b: *std.Build) !void {
|
|
|
42
50
|
const showdown =
|
|
43
51
|
b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
|
|
44
52
|
const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
|
|
53
|
+
const chance = b.option(bool, "chance", "Enable update probability tracking") orelse false;
|
|
54
|
+
const calc = b.option(bool, "calc", "Enable damage calculator support") orelse false;
|
|
45
55
|
|
|
46
56
|
const options = b.addOptions();
|
|
47
57
|
options.addOption(?bool, "showdown", showdown);
|
|
48
58
|
options.addOption(?bool, "log", log);
|
|
59
|
+
options.addOption(?bool, "chance", chance);
|
|
60
|
+
options.addOption(?bool, "calc", calc);
|
|
49
61
|
|
|
50
62
|
const name = if (showdown) "pkmn-showdown" else "pkmn";
|
|
51
63
|
|
|
@@ -54,16 +66,16 @@ pub fn build(b: *std.Build) !void {
|
|
|
54
66
|
const addon = b.fmt("{s}.node", .{name});
|
|
55
67
|
const lib = b.addSharedLibrary(.{
|
|
56
68
|
.name = addon,
|
|
69
|
+
.main_pkg_path = .{ .path = "." },
|
|
57
70
|
.root_source_file = .{ .path = "src/lib/binding/node.zig" },
|
|
58
71
|
.optimize = optimize,
|
|
59
72
|
.target = target,
|
|
60
73
|
});
|
|
61
74
|
lib.addOptions("build_options", options);
|
|
62
|
-
lib.
|
|
63
|
-
lib.addSystemIncludePath(headers);
|
|
75
|
+
lib.addSystemIncludePath(.{ .path = headers });
|
|
64
76
|
lib.linkLibC();
|
|
65
77
|
if (node_import_lib) |il| {
|
|
66
|
-
lib.addObjectFile(il);
|
|
78
|
+
lib.addObjectFile(.{ .path = il });
|
|
67
79
|
} else if ((try std.zig.system.NativeTargetInfo.detect(target)).target.os.tag == .windows) {
|
|
68
80
|
try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
|
|
69
81
|
std.process.exit(1);
|
|
@@ -83,6 +95,7 @@ pub fn build(b: *std.Build) !void {
|
|
|
83
95
|
} else if (wasm) {
|
|
84
96
|
const lib = b.addSharedLibrary(.{
|
|
85
97
|
.name = name,
|
|
98
|
+
.main_pkg_path = .{ .path = "." },
|
|
86
99
|
.root_source_file = .{ .path = "src/lib/binding/wasm.zig" },
|
|
87
100
|
.optimize = switch (optimize) {
|
|
88
101
|
.ReleaseFast, .ReleaseSafe => .ReleaseSmall,
|
|
@@ -91,7 +104,6 @@ pub fn build(b: *std.Build) !void {
|
|
|
91
104
|
.target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
|
|
92
105
|
});
|
|
93
106
|
lib.addOptions("build_options", options);
|
|
94
|
-
lib.setMainPkgPath("./");
|
|
95
107
|
lib.stack_size = wasm_stack_size;
|
|
96
108
|
lib.rdynamic = true;
|
|
97
109
|
lib.strip = strip;
|
|
@@ -109,14 +121,14 @@ pub fn build(b: *std.Build) !void {
|
|
|
109
121
|
} else if (dynamic) {
|
|
110
122
|
const lib = b.addSharedLibrary(.{
|
|
111
123
|
.name = name,
|
|
124
|
+
.main_pkg_path = .{ .path = "." },
|
|
112
125
|
.root_source_file = .{ .path = "src/lib/binding/c.zig" },
|
|
113
126
|
.version = try std.SemanticVersion.parse(version),
|
|
114
127
|
.optimize = optimize,
|
|
115
128
|
.target = target,
|
|
116
129
|
});
|
|
117
130
|
lib.addOptions("build_options", options);
|
|
118
|
-
lib.
|
|
119
|
-
lib.addIncludePath("src/include");
|
|
131
|
+
lib.addIncludePath(.{ .path = "src/include" });
|
|
120
132
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
121
133
|
if (pic) lib.force_pic = pic;
|
|
122
134
|
b.installArtifact(lib);
|
|
@@ -124,17 +136,30 @@ pub fn build(b: *std.Build) !void {
|
|
|
124
136
|
} else {
|
|
125
137
|
const lib = b.addStaticLibrary(.{
|
|
126
138
|
.name = name,
|
|
139
|
+
.main_pkg_path = .{ .path = "." },
|
|
127
140
|
.root_source_file = .{ .path = "src/lib/binding/c.zig" },
|
|
128
141
|
.optimize = optimize,
|
|
129
142
|
.target = target,
|
|
130
143
|
});
|
|
131
144
|
lib.addOptions("build_options", options);
|
|
132
|
-
lib.
|
|
133
|
-
lib.addIncludePath("src/include");
|
|
145
|
+
lib.addIncludePath(.{ .path = "src/include" });
|
|
134
146
|
lib.bundle_compiler_rt = true;
|
|
135
147
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
136
148
|
if (pic) lib.force_pic = pic;
|
|
137
|
-
if (emit_asm)
|
|
149
|
+
if (emit_asm) {
|
|
150
|
+
b.getInstallStep().dependOn(&b.addInstallFileWithDir(
|
|
151
|
+
lib.getEmittedAsm(),
|
|
152
|
+
.prefix,
|
|
153
|
+
b.fmt("{s}.s", .{name}),
|
|
154
|
+
).step);
|
|
155
|
+
}
|
|
156
|
+
if (emit_ll) {
|
|
157
|
+
b.getInstallStep().dependOn(&b.addInstallFileWithDir(
|
|
158
|
+
lib.getEmittedLlvmIr(),
|
|
159
|
+
.prefix,
|
|
160
|
+
b.fmt("{s}.ll", .{name}),
|
|
161
|
+
).step);
|
|
162
|
+
}
|
|
138
163
|
b.installArtifact(lib);
|
|
139
164
|
c = true;
|
|
140
165
|
}
|
|
@@ -177,14 +202,20 @@ pub fn build(b: *std.Build) !void {
|
|
|
177
202
|
.strip = strip,
|
|
178
203
|
.cmd = cmd,
|
|
179
204
|
};
|
|
205
|
+
|
|
206
|
+
// TODO: tests can be run multiple times due to @imports
|
|
180
207
|
const tests = TestStep.create(b, options, config);
|
|
181
208
|
|
|
182
209
|
var exes = std.ArrayList(*std.Build.CompileStep).init(b.allocator);
|
|
183
210
|
const tools: ToolConfig = .{
|
|
184
|
-
.
|
|
185
|
-
.tool = .{
|
|
211
|
+
.options = .{
|
|
186
212
|
.showdown = showdown,
|
|
187
213
|
.log = log,
|
|
214
|
+
.chance = chance,
|
|
215
|
+
.calc = calc,
|
|
216
|
+
},
|
|
217
|
+
.general = config,
|
|
218
|
+
.tool = .{
|
|
188
219
|
.tests = if (tests.build) tests else null,
|
|
189
220
|
.exes = &exes,
|
|
190
221
|
},
|
|
@@ -198,23 +229,21 @@ pub fn build(b: *std.Build) !void {
|
|
|
198
229
|
var fuzz_config = tools;
|
|
199
230
|
fuzz_config.general.strip = false;
|
|
200
231
|
fuzz_config.tool.name = "fuzz";
|
|
201
|
-
const fuzz = tool(b, "src/test/
|
|
232
|
+
const fuzz = tool(b, "src/test/fuzz.zig", fuzz_config);
|
|
202
233
|
|
|
234
|
+
const analyze = tool(b, "src/tools/analyze.zig", tools);
|
|
235
|
+
const dump = tool(b, "src/tools/dump.zig", tools);
|
|
203
236
|
const serde = tool(b, "src/tools/serde.zig", tools);
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
const lint_exe =
|
|
207
|
-
b.addExecutable(.{ .name = "lint", .root_source_file = .{ .path = "src/tools/lint.zig" } });
|
|
208
|
-
if (tests.build) tests.step.dependOn(&lint_exe.step);
|
|
209
|
-
const lint = b.addRunArtifact(lint_exe);
|
|
237
|
+
const transitions = tool(b, "src/tools/transitions.zig", tools);
|
|
210
238
|
|
|
239
|
+
b.step("analyze", "Run LLVM analysis tool").dependOn(&analyze.step);
|
|
211
240
|
b.step("benchmark", "Run benchmark code").dependOn(&benchmark.step);
|
|
241
|
+
b.step("dump", "Run protocol dump tool").dependOn(&dump.step);
|
|
212
242
|
b.step("fuzz", "Run fuzz tester").dependOn(&fuzz.step);
|
|
213
|
-
b.step("lint", "Lint source files").dependOn(&lint.step);
|
|
214
|
-
b.step("protocol", "Run protocol dump tool").dependOn(&protocol.step);
|
|
215
243
|
b.step("serde", "Run serialization/deserialization tool").dependOn(&serde.step);
|
|
216
244
|
b.step("test", "Run all tests").dependOn(&tests.step);
|
|
217
245
|
b.step("tools", "Install tools").dependOn(&ToolsStep.create(b, &exes).step);
|
|
246
|
+
b.step("transitions", "Visualize transitions algorithm search").dependOn(&transitions.step);
|
|
218
247
|
}
|
|
219
248
|
|
|
220
249
|
fn maybeStrip(
|
|
@@ -244,12 +273,6 @@ const Config = struct {
|
|
|
244
273
|
cmd: ?[]const u8,
|
|
245
274
|
};
|
|
246
275
|
|
|
247
|
-
const TESTS = .{
|
|
248
|
-
"src/lib/common/test.zig",
|
|
249
|
-
"src/lib/gen1/test.zig",
|
|
250
|
-
"src/lib/gen2/test.zig",
|
|
251
|
-
};
|
|
252
|
-
|
|
253
276
|
const TestStep = struct {
|
|
254
277
|
step: std.Build.Step,
|
|
255
278
|
build: bool,
|
|
@@ -264,35 +287,35 @@ const TestStep = struct {
|
|
|
264
287
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
|
|
265
288
|
self.* = TestStep{ .step = step, .build = test_filter == null };
|
|
266
289
|
|
|
267
|
-
const
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
}
|
|
285
|
-
self.step.dependOn(&b.addRunArtifact(tests).step);
|
|
290
|
+
const path = test_file orelse "src/lib/test.zig";
|
|
291
|
+
const tests = b.addTest(.{
|
|
292
|
+
.name = b.fmt("{s}-{s}", .{
|
|
293
|
+
std.fs.path.basename(std.fs.path.dirname(path).?),
|
|
294
|
+
std.fs.path.stem(std.fs.path.basename(path)),
|
|
295
|
+
}),
|
|
296
|
+
.main_pkg_path = .{ .path = "." },
|
|
297
|
+
.root_source_file = .{ .path = path },
|
|
298
|
+
.optimize = config.optimize,
|
|
299
|
+
.target = config.target,
|
|
300
|
+
.filter = test_filter,
|
|
301
|
+
});
|
|
302
|
+
tests.addOptions("build_options", options);
|
|
303
|
+
tests.single_threaded = true;
|
|
304
|
+
maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
|
|
305
|
+
if (config.pic) tests.force_pic = config.pic;
|
|
306
|
+
if (coverage) |c| {
|
|
307
|
+
tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
|
|
286
308
|
}
|
|
309
|
+
self.step.dependOn(&b.addRunArtifact(tests).step);
|
|
310
|
+
|
|
287
311
|
return self;
|
|
288
312
|
}
|
|
289
313
|
};
|
|
290
314
|
|
|
291
315
|
const ToolConfig = struct {
|
|
316
|
+
options: Options,
|
|
292
317
|
general: Config,
|
|
293
318
|
tool: struct {
|
|
294
|
-
showdown: bool,
|
|
295
|
-
log: bool,
|
|
296
319
|
tests: ?*TestStep,
|
|
297
320
|
name: ?[]const u8 = null,
|
|
298
321
|
exes: *std.ArrayList(*std.Build.CompileStep),
|
|
@@ -303,7 +326,7 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) *std.Build.RunStep
|
|
|
303
326
|
var name = config.tool.name orelse std.fs.path.basename(path);
|
|
304
327
|
const index = std.mem.lastIndexOfScalar(u8, name, '.');
|
|
305
328
|
if (index) |i| name = name[0..i];
|
|
306
|
-
if (config.
|
|
329
|
+
if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
|
|
307
330
|
|
|
308
331
|
const exe = b.addExecutable(.{
|
|
309
332
|
.name = name,
|
|
@@ -311,10 +334,8 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) *std.Build.RunStep
|
|
|
311
334
|
.target = config.general.target,
|
|
312
335
|
.optimize = config.general.optimize,
|
|
313
336
|
});
|
|
314
|
-
exe.addModule("pkmn", module(b, .
|
|
315
|
-
|
|
316
|
-
.log = config.tool.log,
|
|
317
|
-
}));
|
|
337
|
+
exe.addModule("pkmn", module(b, config.options));
|
|
338
|
+
|
|
318
339
|
exe.single_threaded = true;
|
|
319
340
|
if (config.general.pic) exe.force_pic = config.general.pic;
|
|
320
341
|
if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
|
|
@@ -334,7 +355,7 @@ const ToolsStep = struct {
|
|
|
334
355
|
const self = b.allocator.create(ToolsStep) catch @panic("OOM");
|
|
335
356
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
|
|
336
357
|
self.* = ToolsStep{ .step = step };
|
|
337
|
-
for (exes.items) |t| self.step.dependOn(&b.addInstallArtifact(t).step);
|
|
358
|
+
for (exes.items) |t| self.step.dependOn(&b.addInstallArtifact(t, .{}).step);
|
|
338
359
|
return self;
|
|
339
360
|
}
|
|
340
361
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pkmn/engine",
|
|
3
|
-
"version": "0.1.0-dev.
|
|
3
|
+
"version": "0.1.0-dev.46307385",
|
|
4
4
|
"description": "A minimal, complete, Pokémon battle simulation engine optimized for performance",
|
|
5
5
|
"repository": "github:pkmn/engine",
|
|
6
6
|
"license": "MIT",
|
|
@@ -34,11 +34,18 @@
|
|
|
34
34
|
"src/bin/install-pkmn-engine",
|
|
35
35
|
"src/lib/binding/node.zig",
|
|
36
36
|
"src/lib/binding/wasm.zig",
|
|
37
|
+
"src/lib/common/battle.zig",
|
|
37
38
|
"src/lib/common/data.zig",
|
|
38
39
|
"src/lib/common/debug.zig",
|
|
40
|
+
"src/lib/common/js.zig",
|
|
41
|
+
"src/lib/common/optional.zig",
|
|
39
42
|
"src/lib/common/options.zig",
|
|
40
43
|
"src/lib/common/protocol.zig",
|
|
44
|
+
"src/lib/common/rational.zig",
|
|
41
45
|
"src/lib/common/rng.zig",
|
|
46
|
+
"src/lib/common/util.zig",
|
|
47
|
+
"src/lib/gen1/calc.zig",
|
|
48
|
+
"src/lib/gen1/chance.zig",
|
|
42
49
|
"src/lib/gen1/data.zig",
|
|
43
50
|
"src/lib/gen1/data/moves.zig",
|
|
44
51
|
"src/lib/gen1/data/species.zig",
|
|
@@ -66,30 +73,30 @@
|
|
|
66
73
|
"access": "public"
|
|
67
74
|
},
|
|
68
75
|
"dependencies": {
|
|
69
|
-
"@pkmn/data": "^0.7.
|
|
76
|
+
"@pkmn/data": "^0.7.46",
|
|
70
77
|
"@pkmn/protocol": "^0.6.12"
|
|
71
78
|
},
|
|
72
79
|
"devDependencies": {
|
|
73
|
-
"@pkmn/eslint-config": "^
|
|
74
|
-
"@pkmn/img": "^0.2.
|
|
80
|
+
"@pkmn/eslint-config": "^5.0.0",
|
|
81
|
+
"@pkmn/img": "^0.2.13",
|
|
75
82
|
"@pkmn/sets": "^5.0.4",
|
|
76
|
-
"@pkmn/sim": "0.7.
|
|
83
|
+
"@pkmn/sim": "0.7.46",
|
|
77
84
|
"@types/html-minifier": "^4.0.2",
|
|
78
|
-
"@types/jest": "^29.5.
|
|
85
|
+
"@types/jest": "^29.5.3",
|
|
79
86
|
"@types/minimist": "^1.2.2",
|
|
80
87
|
"@types/mustache": "^4.2.2",
|
|
81
|
-
"@types/node": "^20.
|
|
82
|
-
"@vitest/coverage-c8": "^0.
|
|
83
|
-
"binaryen": "^
|
|
84
|
-
"eslint": "^8.
|
|
88
|
+
"@types/node": "^20.4.2",
|
|
89
|
+
"@vitest/coverage-c8": "^0.33.0",
|
|
90
|
+
"binaryen": "^114.0.0",
|
|
91
|
+
"eslint": "^8.44.0",
|
|
85
92
|
"html-minifier": "^4.0.0",
|
|
86
93
|
"json-stringify-pretty-compact": "3.0.0",
|
|
87
94
|
"minimist": "^1.2.8",
|
|
88
95
|
"mustache": "^4.2.0",
|
|
89
96
|
"source-map-support": "^0.5.21",
|
|
90
97
|
"trakr": "^0.2.0",
|
|
91
|
-
"typescript": "^5.1.
|
|
92
|
-
"vitest": "^0.
|
|
98
|
+
"typescript": "^5.1.6",
|
|
99
|
+
"vitest": "^0.33.0"
|
|
93
100
|
},
|
|
94
101
|
"scripts": {
|
|
95
102
|
"lint": "eslint --cache src --ext ts,js && eslint src/bin/*",
|
|
@@ -140,7 +147,8 @@
|
|
|
140
147
|
"rules": {
|
|
141
148
|
"no-control-regex": "off",
|
|
142
149
|
"@typescript-eslint/no-empty-interface": "off",
|
|
143
|
-
"@typescript-eslint/no-shadow": "off"
|
|
150
|
+
"@typescript-eslint/no-shadow": "off",
|
|
151
|
+
"@typescript-eslint/no-unsafe-declaration-merging": "off"
|
|
144
152
|
}
|
|
145
153
|
},
|
|
146
154
|
{
|
|
@@ -156,7 +164,8 @@
|
|
|
156
164
|
"src/test/benchmark*.ts"
|
|
157
165
|
],
|
|
158
166
|
"rules": {
|
|
159
|
-
"@typescript-eslint/no-unused-vars": "off"
|
|
167
|
+
"@typescript-eslint/no-unused-vars": "off",
|
|
168
|
+
"@typescript-eslint/no-redundant-type-constituents": "off"
|
|
160
169
|
}
|
|
161
170
|
},
|
|
162
171
|
{
|
|
@@ -10,7 +10,7 @@ const path = require('path');
|
|
|
10
10
|
const {pipeline} = require('stream/promises');
|
|
11
11
|
const tty = require('tty');
|
|
12
12
|
|
|
13
|
-
const ZIG_VERSION = {major: 0, minor: 11, patch: 0
|
|
13
|
+
const ZIG_VERSION = {major: 0, minor: 11, patch: 0};
|
|
14
14
|
const DEV = ZIG_VERSION.dev ? `-dev.${ZIG_VERSION.dev}` : '';
|
|
15
15
|
const VERSION = `${ZIG_VERSION.major}.${ZIG_VERSION.minor}.${ZIG_VERSION.patch}${DEV}`;
|
|
16
16
|
const INDEX = 'https://ziglang.org/download/index.json';
|
|
@@ -336,14 +336,18 @@ const downloadImportLib = async () => {
|
|
|
336
336
|
node = true;
|
|
337
337
|
if (!options) {
|
|
338
338
|
const addon = require(copy);
|
|
339
|
-
if (addon.options.showdown) opts.push('-Dshowdown');
|
|
340
|
-
if (addon.options.log) opts.push('-Dlog');
|
|
339
|
+
if (addon.engine.options.showdown) opts.push('-Dshowdown');
|
|
340
|
+
if (addon.engine.options.log) opts.push('-Dlog');
|
|
341
|
+
if (addon.engine.options.chance) opts.push('-Dchance');
|
|
342
|
+
if (addon.engine.options.calc) opts.push('-Dcalc');
|
|
341
343
|
}
|
|
342
344
|
} else if (!options) {
|
|
343
345
|
const addon = await WebAssembly.instantiate(fs.readFileSync(copy));
|
|
344
346
|
const values = new Uint8Array(addon.instance.exports.memory.buffer);
|
|
345
347
|
if (values[addon.instance.exports.SHOWDOWN.value]) opts.push('-Dshowdown');
|
|
346
348
|
if (values[addon.instance.exports.LOG.value]) opts.push('-Dlog');
|
|
349
|
+
if (values[addon.instance.exports.CHANCE.value]) opts.push('-Dchance');
|
|
350
|
+
if (values[addon.instance.exports.CALC.value]) opts.push('-Dcalc');
|
|
347
351
|
}
|
|
348
352
|
const key = `${type} ${opts.sort().join(' ')}`;
|
|
349
353
|
if (!configs.has(key)) configs.set(key, {type, options: opts});
|
|
@@ -354,8 +358,9 @@ const downloadImportLib = async () => {
|
|
|
354
358
|
// either the default options or whatever was passed in
|
|
355
359
|
if (!configs.size) {
|
|
356
360
|
node = true;
|
|
357
|
-
|
|
358
|
-
configs.set('
|
|
361
|
+
const DEFAULT = ['-Dshowdown'];
|
|
362
|
+
configs.set('node', {type: 'node', options: options || DEFAULT});
|
|
363
|
+
configs.set('wasm', {type: 'wasm', options: options || DEFAULT});
|
|
359
364
|
}
|
|
360
365
|
|
|
361
366
|
const headers = node ? await downloadHeaders() : undefined;
|
package/src/lib/binding/node.zig
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
2
|
const pkmn = @import("../pkmn.zig");
|
|
3
3
|
|
|
4
|
+
const js = @import("../common/js.zig");
|
|
5
|
+
|
|
4
6
|
const assert = std.debug.assert;
|
|
5
7
|
|
|
6
8
|
const c = @cImport({
|
|
@@ -10,38 +12,49 @@ const c = @cImport({
|
|
|
10
12
|
|
|
11
13
|
export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
|
|
12
14
|
const properties = [_]c.napi_property_descriptor{
|
|
13
|
-
Property.init("
|
|
14
|
-
Property.init("bindings", .{ .value = bindings(env) }),
|
|
15
|
+
js.Property.init("engine", .{ .value = register(env) }),
|
|
15
16
|
};
|
|
16
17
|
assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
|
|
17
18
|
return exports;
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
pub fn register(env: c.napi_env) c.napi_value {
|
|
22
|
+
var object = js.Object.init(env);
|
|
23
|
+
const properties = [_]c.napi_property_descriptor{
|
|
24
|
+
js.Property.init("options", .{ .value = options(env) }),
|
|
25
|
+
js.Property.init("bindings", .{ .value = bindings(env) }),
|
|
26
|
+
};
|
|
27
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
28
|
+
return object;
|
|
29
|
+
}
|
|
30
|
+
|
|
20
31
|
fn options(env: c.napi_env) c.napi_value {
|
|
21
|
-
var object = Object.init(env);
|
|
32
|
+
var object = js.Object.init(env);
|
|
22
33
|
const properties = [_]c.napi_property_descriptor{
|
|
23
|
-
Property.init("showdown", .{ .value = Boolean.init(env, pkmn.options.showdown) }),
|
|
24
|
-
Property.init("log", .{ .value = Boolean.init(env, pkmn.options.log) }),
|
|
34
|
+
js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
|
|
35
|
+
js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
|
|
36
|
+
js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
|
|
37
|
+
js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
|
|
25
38
|
};
|
|
26
39
|
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
27
40
|
return object;
|
|
28
41
|
}
|
|
29
42
|
|
|
30
43
|
fn bindings(env: c.napi_env) c.napi_value {
|
|
31
|
-
var array = Array.init(env, .{ .length = 1 });
|
|
32
|
-
Array.set(env, array, 0, bind(env, pkmn.gen1));
|
|
44
|
+
var array = js.Array.init(env, .{ .length = 1 });
|
|
45
|
+
js.Array.set(env, array, 0, bind(env, pkmn.gen1));
|
|
33
46
|
return array;
|
|
34
47
|
}
|
|
35
48
|
|
|
36
49
|
fn bind(env: c.napi_env, gen: anytype) c.napi_value {
|
|
37
50
|
const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
|
|
38
51
|
const logs_size: u32 = @intCast(gen.LOGS_SIZE);
|
|
39
|
-
var object = Object.init(env);
|
|
52
|
+
var object = js.Object.init(env);
|
|
40
53
|
const properties = [_]c.napi_property_descriptor{
|
|
41
|
-
Property.init("CHOICES_SIZE", .{ .value = Number.init(env, choices_size) }),
|
|
42
|
-
Property.init("LOGS_SIZE", .{ .value = Number.init(env, logs_size) }),
|
|
43
|
-
Property.init("update", .{ .method = update(gen) }),
|
|
44
|
-
Property.init("choices", .{ .method = choices(gen) }),
|
|
54
|
+
js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
|
|
55
|
+
js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
|
|
56
|
+
js.Property.init("update", .{ .method = update(gen) }),
|
|
57
|
+
js.Property.init("choices", .{ .method = choices(gen) }),
|
|
45
58
|
};
|
|
46
59
|
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
47
60
|
return object;
|
|
@@ -62,13 +75,13 @@ fn update(gen: anytype) c.napi_callback {
|
|
|
62
75
|
assert(data != null);
|
|
63
76
|
|
|
64
77
|
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
65
|
-
const c1: pkmn.Choice = @bitCast(Number.get(env, argv[1], u8));
|
|
66
|
-
const c2: pkmn.Choice = @bitCast(Number.get(env, argv[2], u8));
|
|
78
|
+
const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
|
|
79
|
+
const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
|
|
67
80
|
|
|
68
81
|
var vtype: c.napi_valuetype = undefined;
|
|
69
82
|
assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
|
|
70
83
|
const result = switch (vtype) {
|
|
71
|
-
c.napi_undefined, c.napi_null => battle.update(c1, c2,
|
|
84
|
+
c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
|
|
72
85
|
else => result: {
|
|
73
86
|
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
74
87
|
assert(len == gen.LOGS_SIZE);
|
|
@@ -76,12 +89,17 @@ fn update(gen: anytype) c.napi_callback {
|
|
|
76
89
|
|
|
77
90
|
var buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
|
|
78
91
|
var stream = pkmn.protocol.ByteStream{ .buffer = buf };
|
|
79
|
-
|
|
80
|
-
|
|
92
|
+
// TODO: extract out
|
|
93
|
+
var opts = pkmn.battle.options(
|
|
94
|
+
pkmn.protocol.FixedLog{ .writer = stream.writer() },
|
|
95
|
+
gen.chance.NULL,
|
|
96
|
+
gen.calc.NULL,
|
|
97
|
+
);
|
|
98
|
+
break :result battle.update(c1, c2, &opts);
|
|
81
99
|
},
|
|
82
100
|
} catch unreachable;
|
|
83
101
|
|
|
84
|
-
return Number.init(env, @as(u8, @bitCast(result)));
|
|
102
|
+
return js.Number.init(env, @as(u8, @bitCast(result)));
|
|
85
103
|
}
|
|
86
104
|
}.call;
|
|
87
105
|
}
|
|
@@ -101,8 +119,8 @@ fn choices(gen: anytype) c.napi_callback {
|
|
|
101
119
|
assert(data != null);
|
|
102
120
|
|
|
103
121
|
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
104
|
-
const player: pkmn.Player = @enumFromInt(Number.get(env, argv[1], u8));
|
|
105
|
-
const request: pkmn.Choice.Type = @enumFromInt(Number.get(env, argv[2], u8));
|
|
122
|
+
const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
|
|
123
|
+
const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
|
|
106
124
|
|
|
107
125
|
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
108
126
|
assert(len == gen.CHOICES_SIZE);
|
|
@@ -110,117 +128,7 @@ fn choices(gen: anytype) c.napi_callback {
|
|
|
110
128
|
|
|
111
129
|
var out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
|
|
112
130
|
const n = battle.choices(player, request, out);
|
|
113
|
-
return Number.init(env, @as(u8, @bitCast(n)));
|
|
131
|
+
return js.Number.init(env, @as(u8, @bitCast(n)));
|
|
114
132
|
}
|
|
115
133
|
}.call;
|
|
116
134
|
}
|
|
117
|
-
|
|
118
|
-
const Array = struct {
|
|
119
|
-
fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
|
|
120
|
-
var result: c.napi_value = undefined;
|
|
121
|
-
assert(c.napi_ok == if (o.length) |n|
|
|
122
|
-
c.napi_create_array_with_length(env, n, &result)
|
|
123
|
-
else
|
|
124
|
-
c.napi_create_array(env, &result));
|
|
125
|
-
return result;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
|
|
129
|
-
assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const Boolean = struct {
|
|
134
|
-
fn init(env: c.napi_env, value: bool) c.napi_value {
|
|
135
|
-
var result: c.napi_value = undefined;
|
|
136
|
-
assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
};
|
|
140
|
-
|
|
141
|
-
const Number = struct {
|
|
142
|
-
fn init(env: c.napi_env, value: anytype) c.napi_value {
|
|
143
|
-
const T = @TypeOf(value);
|
|
144
|
-
var result: c.napi_value = undefined;
|
|
145
|
-
assert(c.napi_ok == switch (@typeInfo(T)) {
|
|
146
|
-
.Int => |info| switch (info.bits) {
|
|
147
|
-
0...32 => switch (info.signedness) {
|
|
148
|
-
.signed => c.napi_create_int32(env, @as(i32, value), &result),
|
|
149
|
-
.unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
|
|
150
|
-
},
|
|
151
|
-
33...52 => c.napi_create_int64(env, @as(i64, value), &result),
|
|
152
|
-
else => @compileError("int can't be represented as JS number"),
|
|
153
|
-
},
|
|
154
|
-
else => @compileError("expected number, got: " ++ @typeName(T)),
|
|
155
|
-
});
|
|
156
|
-
return result;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
|
|
160
|
-
switch (@typeInfo(T)) {
|
|
161
|
-
.Int => |info| switch (info.bits) {
|
|
162
|
-
0...32 => switch (info.signedness) {
|
|
163
|
-
.signed => {
|
|
164
|
-
var result: i32 = undefined;
|
|
165
|
-
assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
|
|
166
|
-
return if (info.bits == 32) result else @intCast(result);
|
|
167
|
-
},
|
|
168
|
-
.unsigned => {
|
|
169
|
-
var result: u32 = undefined;
|
|
170
|
-
assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
|
|
171
|
-
return if (info.bits == 32) result else @intCast(result);
|
|
172
|
-
},
|
|
173
|
-
},
|
|
174
|
-
33...63 => {
|
|
175
|
-
var result: i64 = undefined;
|
|
176
|
-
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
|
|
177
|
-
return @intCast(result);
|
|
178
|
-
},
|
|
179
|
-
else => {
|
|
180
|
-
var result: i64 = undefined;
|
|
181
|
-
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
|
|
182
|
-
return switch (info.signedness) {
|
|
183
|
-
.signed => value,
|
|
184
|
-
.unsigned => if (0 <= value) @intCast(value) else unreachable,
|
|
185
|
-
};
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
else => @compileError("expected number, got: " ++ @typeName(T)),
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
const Object = struct {
|
|
194
|
-
fn init(env: c.napi_env) c.napi_value {
|
|
195
|
-
var result: c.napi_value = undefined;
|
|
196
|
-
assert(c.napi_create_object(env, &result) == c.napi_ok);
|
|
197
|
-
return result;
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
|
|
201
|
-
const Property = union(enum) {
|
|
202
|
-
method: c.napi_callback,
|
|
203
|
-
value: c.napi_value,
|
|
204
|
-
|
|
205
|
-
fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
|
|
206
|
-
return .{
|
|
207
|
-
.utf8name = name,
|
|
208
|
-
.name = null,
|
|
209
|
-
.method = switch (property) {
|
|
210
|
-
.method => |m| m,
|
|
211
|
-
.value => null,
|
|
212
|
-
},
|
|
213
|
-
.getter = null,
|
|
214
|
-
.setter = null,
|
|
215
|
-
.value = switch (property) {
|
|
216
|
-
.method => null,
|
|
217
|
-
.value => |v| v,
|
|
218
|
-
},
|
|
219
|
-
.attributes = switch (property) {
|
|
220
|
-
.method => c.napi_default,
|
|
221
|
-
.value => c.napi_enumerable,
|
|
222
|
-
},
|
|
223
|
-
.data = null,
|
|
224
|
-
};
|
|
225
|
-
}
|
|
226
|
-
};
|
package/src/lib/binding/wasm.zig
CHANGED
|
@@ -3,6 +3,8 @@ const pkmn = @import("../pkmn.zig");
|
|
|
3
3
|
|
|
4
4
|
export const SHOWDOWN = pkmn.options.showdown;
|
|
5
5
|
export const LOG = pkmn.options.log;
|
|
6
|
+
export const CHANCE = pkmn.options.chance;
|
|
7
|
+
export const CALC = pkmn.options.calc;
|
|
6
8
|
|
|
7
9
|
export const GEN1_CHOICES_SIZE =
|
|
8
10
|
std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.CHOICES_SIZE))) catch unreachable;
|