@pkmn/engine 0.1.0-dev.eaae4aa6 → 0.1.0-dev.f34af416
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 +0 -2
- package/README.md +15 -11
- package/build/pkg/addon.js +6 -7
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data.d.ts +2 -3
- package/build/pkg/index.d.ts +1 -1
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.js +4 -4
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +8 -3
- package/build/test/display.js.map +1 -1
- package/build/tools/debug.js +2 -3
- package/build/tools/debug.js.map +1 -1
- package/build.zig +189 -72
- package/package.json +17 -16
- package/src/bin/install-pkmn-engine +17 -7
- package/src/lib/common/data.zig +8 -8
- package/src/lib/common/debug.zig +2 -2
- package/src/lib/common/js.zig +4 -2
- package/src/lib/common/optional.zig +13 -9
- package/src/lib/common/options.zig +2 -0
- package/src/lib/common/protocol.zig +51 -44
- package/src/lib/common/rational.zig +114 -24
- package/src/lib/common/rng.zig +10 -10
- package/src/lib/common/util.zig +50 -1
- package/src/lib/gen1/calc.zig +508 -128
- package/src/lib/gen1/chance.zig +383 -276
- package/src/lib/gen1/data/species.zig +1 -1
- package/src/lib/gen1/data.zig +29 -28
- package/src/lib/gen1/helpers.zig +96 -536
- package/src/lib/gen1/mechanics.zig +266 -160
- package/src/lib/gen2/calc.zig +34 -16
- package/src/lib/gen2/chance.zig +91 -500
- package/src/lib/gen2/data.zig +15 -14
- package/src/lib/gen2/helpers.zig +41 -9
- package/src/lib/gen2/mechanics.zig +158 -97
- package/src/lib/pkmn.zig +10 -16
- package/src/test/display/pkmn.html.tmpl +5 -5
- package/src/lib/gen1/README.md +0 -894
- package/src/lib/gen2/README.md +0 -329
package/build.zig
CHANGED
|
@@ -8,23 +8,40 @@ pub const Options = struct {
|
|
|
8
8
|
calc: ?bool = null,
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
const Step = if (@hasDecl(std.Build, "Step")) std.Build.Step else .{
|
|
12
|
+
.Compile = std.Build.CompileStep,
|
|
13
|
+
.Options = std.Build.OptionsStep,
|
|
14
|
+
.Run = std.Build.RunStep,
|
|
15
|
+
};
|
|
16
|
+
const has_path = @hasField(std.Build.LazyPath, "path");
|
|
17
|
+
const root_module = @hasField(Step.Compile, "root_module");
|
|
18
|
+
const force_pic = !@hasField(std.Build.SharedLibraryOptions, "pic");
|
|
19
|
+
const ResolvedTarget =
|
|
20
|
+
if (@hasDecl(std.Build, "ResolvedTarget")) std.Build.ResolvedTarget else std.zig.CrossTarget;
|
|
21
|
+
|
|
11
22
|
pub fn module(b: *std.Build, options: Options) *std.Build.Module {
|
|
23
|
+
if (!has_path) @panic("Use build.zig.zon package management");
|
|
24
|
+
|
|
12
25
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
13
26
|
const build_options = b.addOptions();
|
|
14
27
|
build_options.addOption(?bool, "showdown", options.showdown);
|
|
15
28
|
build_options.addOption(?bool, "log", options.log);
|
|
16
29
|
build_options.addOption(?bool, "chance", options.chance);
|
|
17
30
|
build_options.addOption(?bool, "calc", options.calc);
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
31
|
+
|
|
32
|
+
const root_source_file = .{ .cwd_relative = dirname ++ "/src/lib/pkmn.zig" };
|
|
33
|
+
const imports = &.{.{ .name = "build_options", .module = build_options.createModule() }};
|
|
34
|
+
return b.createModule(if (@hasDecl(std.Build, "CreateModuleOptions"))
|
|
35
|
+
.{ .source_file = root_source_file, .dependencies = imports }
|
|
36
|
+
else
|
|
37
|
+
.{ .root_source_file = root_source_file, .imports = imports });
|
|
22
38
|
}
|
|
23
39
|
|
|
24
40
|
pub fn build(b: *std.Build) !void {
|
|
25
41
|
const target = b.standardTargetOptions(.{});
|
|
26
42
|
const optimize = b.standardOptimizeOption(.{});
|
|
27
43
|
|
|
44
|
+
const default: ?bool = if (force_pic) false else null;
|
|
28
45
|
const node_headers = b.option([]const u8, "node-headers", "Path to node-headers");
|
|
29
46
|
const node_import_lib =
|
|
30
47
|
b.option([]const u8, "node-import-library", "Path to node import library (Windows)");
|
|
@@ -32,8 +49,8 @@ pub fn build(b: *std.Build) !void {
|
|
|
32
49
|
const wasm_stack_size =
|
|
33
50
|
b.option(u64, "wasm-stack-size", "The size of WASM stack") orelse std.wasm.page_size;
|
|
34
51
|
const dynamic = b.option(bool, "dynamic", "Build a dynamic library") orelse false;
|
|
35
|
-
const strip = b.option(bool, "strip", "Strip debugging symbols from binary");
|
|
36
|
-
const pic = b.option(bool, "pic", "Force position independent code");
|
|
52
|
+
const strip = b.option(bool, "strip", "Strip debugging symbols from binary") orelse default;
|
|
53
|
+
const pic = b.option(bool, "pic", "Force position independent code") orelse default;
|
|
37
54
|
const emit_asm = b.option(bool, "emit-asm", "Output .s (assembly code)") orelse false;
|
|
38
55
|
const emit_ll = b.option(bool, "emit-ll", "Output .ll (LLVM IR)") orelse false;
|
|
39
56
|
|
|
@@ -44,9 +61,15 @@ pub fn build(b: *std.Build) !void {
|
|
|
44
61
|
defer parsed.deinit();
|
|
45
62
|
const version = parsed.value.object.get("version").?.string;
|
|
46
63
|
const description = parsed.value.object.get("description").?.string;
|
|
47
|
-
var repository = std.mem.
|
|
64
|
+
var repository = std.mem.splitSequence(u8, parsed.value.object.get("repository").?.string, ":");
|
|
48
65
|
std.debug.assert(std.mem.eql(u8, repository.first(), "github"));
|
|
49
66
|
|
|
67
|
+
const fast = try std.SemanticVersion.parse("0.12.0-dev.866+3a47bc715");
|
|
68
|
+
if (optimize != .Debug and fast.order(builtin.zig_version) == .lt) {
|
|
69
|
+
std.log.warn("Release builds are ~10-20% slower than before Zig " ++
|
|
70
|
+
"v0.12.0-dev.866+3a47bc715 due to ziglang/zig#17768", .{});
|
|
71
|
+
}
|
|
72
|
+
|
|
50
73
|
const showdown =
|
|
51
74
|
b.option(bool, "showdown", "Enable Pokémon Showdown compatibility mode") orelse false;
|
|
52
75
|
const log = b.option(bool, "log", "Enable protocol message logging") orelse false;
|
|
@@ -61,30 +84,47 @@ pub fn build(b: *std.Build) !void {
|
|
|
61
84
|
|
|
62
85
|
const name = if (showdown) "pkmn-showdown" else "pkmn";
|
|
63
86
|
|
|
87
|
+
const pkmn = if (@hasDecl(std.Build, "CreateModuleOptions")) null else b.addModule("pkmn", .{
|
|
88
|
+
.root_source_file = if (has_path)
|
|
89
|
+
.{ .path = "src/lib/pkmn.zig" }
|
|
90
|
+
else
|
|
91
|
+
b.path("src/lib/pkmn.zig"),
|
|
92
|
+
.optimize = optimize,
|
|
93
|
+
.target = target,
|
|
94
|
+
.imports = &.{.{ .name = "build_options", .module = options.createModule() }},
|
|
95
|
+
});
|
|
96
|
+
|
|
64
97
|
var c = false;
|
|
65
98
|
if (node_headers) |headers| {
|
|
66
99
|
const addon = b.fmt("{s}.node", .{name});
|
|
67
|
-
const
|
|
100
|
+
const path = if (has_path) .{ .path = "src/lib/node.zig" } else b.path("src/lib/node.zig");
|
|
101
|
+
const lib = b.addSharedLibrary(if (force_pic) .{
|
|
68
102
|
.name = addon,
|
|
69
|
-
.root_source_file =
|
|
103
|
+
.root_source_file = path,
|
|
104
|
+
.optimize = optimize,
|
|
105
|
+
.target = target,
|
|
106
|
+
} else .{
|
|
107
|
+
.name = addon,
|
|
108
|
+
.root_source_file = path,
|
|
70
109
|
.optimize = optimize,
|
|
71
110
|
.target = target,
|
|
72
111
|
.strip = strip,
|
|
73
112
|
.pic = pic,
|
|
74
113
|
});
|
|
75
|
-
lib.root_module.addOptions("build_options", options);
|
|
76
|
-
lib.addSystemIncludePath(.{ .path = headers });
|
|
114
|
+
(if (root_module) lib.root_module else lib).addOptions("build_options", options);
|
|
115
|
+
lib.addSystemIncludePath(if (has_path) .{ .path = headers } else b.path(headers));
|
|
77
116
|
lib.linkLibC();
|
|
78
117
|
if (node_import_lib) |il| {
|
|
79
|
-
lib.addObjectFile(.{ .path = il });
|
|
80
|
-
} else if (target.
|
|
118
|
+
lib.addObjectFile(if (has_path) .{ .path = il } else b.path(il));
|
|
119
|
+
} else if (detect(target).os.tag == .windows) {
|
|
81
120
|
try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
|
|
82
121
|
std.process.exit(1);
|
|
83
122
|
}
|
|
84
123
|
lib.linker_allow_shlib_undefined = true;
|
|
85
124
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
125
|
+
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
86
126
|
// Always emit to build/lib because this is where the driver code expects to find it
|
|
87
|
-
// TODO
|
|
127
|
+
// TODO: ziglang/zig#2231 - using the following used to work (though was hacky):
|
|
88
128
|
//
|
|
89
129
|
// lib.emit_bin = .{ .emit_to = b.fmt("build/lib/{s}", .{addon}) };
|
|
90
130
|
// b.getInstallStep().dependOn(&lib.step);
|
|
@@ -93,69 +133,116 @@ pub fn build(b: *std.Build) !void {
|
|
|
93
133
|
// rename the file ourself in install-pkmn-engine
|
|
94
134
|
b.installArtifact(lib);
|
|
95
135
|
} else if (wasm) {
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
.
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
.
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
136
|
+
const entry = @hasDecl(Step.Compile, "Entry");
|
|
137
|
+
const mode = switch (optimize) {
|
|
138
|
+
.ReleaseFast, .ReleaseSafe => .ReleaseSmall,
|
|
139
|
+
else => optimize,
|
|
140
|
+
};
|
|
141
|
+
const freestanding = if (@hasDecl(std.Build, "resolveTargetQuery"))
|
|
142
|
+
b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding })
|
|
143
|
+
else
|
|
144
|
+
ResolvedTarget{ .cpu_arch = .wasm32, .os_tag = .freestanding };
|
|
145
|
+
const path = if (has_path) .{ .path = "src/lib/wasm.zig" } else b.path("src/lib/wasm.zig");
|
|
146
|
+
const bin = if (entry) bin: {
|
|
147
|
+
const exe = b.addExecutable(if (force_pic) .{
|
|
148
|
+
.name = name,
|
|
149
|
+
.root_source_file = path,
|
|
150
|
+
.optimize = mode,
|
|
151
|
+
.target = freestanding,
|
|
152
|
+
} else .{
|
|
153
|
+
.name = name,
|
|
154
|
+
.root_source_file = path,
|
|
155
|
+
.optimize = mode,
|
|
156
|
+
.target = freestanding,
|
|
157
|
+
.strip = strip,
|
|
158
|
+
.pic = pic,
|
|
159
|
+
});
|
|
160
|
+
(if (root_module) exe.root_module else exe).export_symbol_names = &[_][]const u8{
|
|
161
|
+
"SHOWDOWN",
|
|
162
|
+
"LOG",
|
|
163
|
+
"CHANCE",
|
|
164
|
+
"CALC",
|
|
165
|
+
"GEN1_CHOICES_SIZE",
|
|
166
|
+
"GEN1_LOGS_SIZE",
|
|
167
|
+
};
|
|
168
|
+
exe.entry = .disabled;
|
|
169
|
+
break :bin exe;
|
|
170
|
+
} else bin: {
|
|
171
|
+
const lib = b.addSharedLibrary(.{
|
|
172
|
+
.name = name,
|
|
173
|
+
.root_source_file = path,
|
|
174
|
+
.optimize = mode,
|
|
175
|
+
.target = freestanding,
|
|
176
|
+
});
|
|
177
|
+
lib.rdynamic = true;
|
|
178
|
+
break :bin lib;
|
|
115
179
|
};
|
|
116
|
-
|
|
117
|
-
|
|
180
|
+
bin.stack_size = wasm_stack_size;
|
|
181
|
+
if (@hasDecl(@TypeOf(bin.*), "strip")) bin.strip = strip orelse false;
|
|
182
|
+
if (force_pic and pic orelse false) bin.force_pic = pic;
|
|
183
|
+
(if (root_module) bin.root_module else bin).addOptions("build_options", options);
|
|
184
|
+
|
|
118
185
|
const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
|
|
119
186
|
if (optimize != .Debug and opt != null) {
|
|
120
187
|
const out = b.fmt("build/lib/{s}.wasm", .{name});
|
|
121
188
|
const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
|
|
122
|
-
sh.addArtifactArg(
|
|
189
|
+
sh.addArtifactArg(bin);
|
|
123
190
|
sh.addArg("-o");
|
|
124
|
-
sh
|
|
191
|
+
if (@hasDecl(@TypeOf(sh.*), "addFileSourceArg")) {
|
|
192
|
+
sh.addFileSourceArg(if (has_path) .{ .path = out } else b.path(out));
|
|
193
|
+
} else {
|
|
194
|
+
sh.addFileArg(if (has_path) .{ .path = out } else b.path(out));
|
|
195
|
+
}
|
|
125
196
|
b.getInstallStep().dependOn(&sh.step);
|
|
126
|
-
|
|
127
|
-
|
|
197
|
+
if (!entry) b.installArtifact(bin);
|
|
198
|
+
} else if (entry) {
|
|
199
|
+
b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
|
|
128
200
|
.dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
|
|
129
201
|
}).step);
|
|
130
202
|
}
|
|
131
203
|
} else if (dynamic) {
|
|
132
|
-
const
|
|
204
|
+
const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
|
|
205
|
+
const lib = b.addSharedLibrary(if (force_pic) .{
|
|
133
206
|
.name = name,
|
|
134
|
-
.root_source_file =
|
|
207
|
+
.root_source_file = path,
|
|
208
|
+
.version = try std.SemanticVersion.parse(version),
|
|
209
|
+
.optimize = optimize,
|
|
210
|
+
.target = target,
|
|
211
|
+
} else .{
|
|
212
|
+
.name = name,
|
|
213
|
+
.root_source_file = path,
|
|
135
214
|
.version = try std.SemanticVersion.parse(version),
|
|
136
215
|
.optimize = optimize,
|
|
137
216
|
.target = target,
|
|
138
217
|
.strip = strip,
|
|
139
218
|
.pic = pic,
|
|
140
219
|
});
|
|
141
|
-
lib.root_module.addOptions("build_options", options);
|
|
142
|
-
lib.addIncludePath(.{ .path = "src/include" });
|
|
220
|
+
(if (root_module) lib.root_module else lib).addOptions("build_options", options);
|
|
221
|
+
lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
|
|
143
222
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
223
|
+
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
144
224
|
b.installArtifact(lib);
|
|
145
225
|
c = true;
|
|
146
226
|
} else {
|
|
147
|
-
const
|
|
227
|
+
const path = if (has_path) .{ .path = "src/lib/c.zig" } else b.path("src/lib/c.zig");
|
|
228
|
+
const lib = b.addStaticLibrary(if (force_pic) .{
|
|
229
|
+
.name = name,
|
|
230
|
+
.root_source_file = path,
|
|
231
|
+
.optimize = optimize,
|
|
232
|
+
.target = target,
|
|
233
|
+
} else .{
|
|
148
234
|
.name = name,
|
|
149
|
-
.root_source_file =
|
|
235
|
+
.root_source_file = path,
|
|
150
236
|
.optimize = optimize,
|
|
151
237
|
.target = target,
|
|
152
238
|
.strip = strip,
|
|
153
239
|
.pic = pic,
|
|
154
240
|
});
|
|
155
|
-
lib.root_module.addOptions("build_options", options);
|
|
156
|
-
lib.addIncludePath(.{ .path = "src/include" });
|
|
241
|
+
(if (root_module) lib.root_module else lib).addOptions("build_options", options);
|
|
242
|
+
lib.addIncludePath(if (has_path) .{ .path = "src/include" } else b.path("src/include"));
|
|
157
243
|
lib.bundle_compiler_rt = true;
|
|
158
244
|
maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
|
|
245
|
+
if (force_pic and pic orelse false) lib.force_pic = pic;
|
|
159
246
|
if (emit_asm) {
|
|
160
247
|
b.getInstallStep().dependOn(&b.addInstallFileWithDir(
|
|
161
248
|
lib.getEmittedAsm(),
|
|
@@ -175,15 +262,17 @@ pub fn build(b: *std.Build) !void {
|
|
|
175
262
|
}
|
|
176
263
|
|
|
177
264
|
if (c) {
|
|
178
|
-
const
|
|
179
|
-
.{ .path = "src/include/pkmn.h" }
|
|
180
|
-
|
|
181
|
-
"pkmn.h",
|
|
182
|
-
);
|
|
265
|
+
const path =
|
|
266
|
+
if (has_path) .{ .path = "src/include/pkmn.h" } else b.path("src/include/pkmn.h");
|
|
267
|
+
const header = b.addInstallFileWithDir(path, .header, "pkmn.h");
|
|
183
268
|
b.getInstallStep().dependOn(&header.step);
|
|
184
269
|
|
|
185
270
|
const pc = b.fmt("lib{s}.pc", .{name});
|
|
186
|
-
const file = try
|
|
271
|
+
const file = try std.fs.path.relative(
|
|
272
|
+
b.allocator,
|
|
273
|
+
try std.process.getCwdAlloc(b.allocator),
|
|
274
|
+
try b.cache_root.join(b.allocator, &.{pc}),
|
|
275
|
+
);
|
|
187
276
|
const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
|
|
188
277
|
|
|
189
278
|
const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
|
|
@@ -216,7 +305,7 @@ pub fn build(b: *std.Build) !void {
|
|
|
216
305
|
// TODO: tests can be run multiple times due to @imports
|
|
217
306
|
const tests = TestStep.create(b, options, config);
|
|
218
307
|
|
|
219
|
-
var exes = std.ArrayList(*
|
|
308
|
+
var exes = std.ArrayList(*Step.Compile).init(b.allocator);
|
|
220
309
|
const tools: ToolConfig = .{
|
|
221
310
|
.options = .{
|
|
222
311
|
.showdown = showdown,
|
|
@@ -224,6 +313,7 @@ pub fn build(b: *std.Build) !void {
|
|
|
224
313
|
.chance = chance,
|
|
225
314
|
.calc = calc,
|
|
226
315
|
},
|
|
316
|
+
.module = pkmn,
|
|
227
317
|
.general = config,
|
|
228
318
|
.tool = .{
|
|
229
319
|
.tests = if (tests.build) tests else null,
|
|
@@ -264,11 +354,12 @@ pub fn build(b: *std.Build) !void {
|
|
|
264
354
|
|
|
265
355
|
fn maybeStrip(
|
|
266
356
|
b: *std.Build,
|
|
267
|
-
artifact: *
|
|
357
|
+
artifact: *Step.Compile,
|
|
268
358
|
step: *std.Build.Step,
|
|
269
359
|
strip: ?bool,
|
|
270
360
|
cmd: ?[]const u8,
|
|
271
361
|
) void {
|
|
362
|
+
if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
|
|
272
363
|
if (!(strip orelse false) or cmd == null) return;
|
|
273
364
|
// Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
|
|
274
365
|
const mac = builtin.os.tag == .macos;
|
|
@@ -281,7 +372,7 @@ fn maybeStrip(
|
|
|
281
372
|
}
|
|
282
373
|
|
|
283
374
|
const Config = struct {
|
|
284
|
-
target:
|
|
375
|
+
target: ResolvedTarget,
|
|
285
376
|
optimize: std.builtin.OptimizeMode,
|
|
286
377
|
pic: ?bool,
|
|
287
378
|
strip: ?bool,
|
|
@@ -292,9 +383,8 @@ const TestStep = struct {
|
|
|
292
383
|
step: std.Build.Step,
|
|
293
384
|
build: bool,
|
|
294
385
|
|
|
295
|
-
pub fn create(b: *std.Build, options: *
|
|
386
|
+
pub fn create(b: *std.Build, options: *Step.Options, config: Config) *TestStep {
|
|
296
387
|
const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
|
|
297
|
-
const test_file = b.option([]const u8, "test-file", "Input file for test");
|
|
298
388
|
const test_filter =
|
|
299
389
|
b.option([]const u8, "test-filter", "Skip tests that do not match filter");
|
|
300
390
|
|
|
@@ -302,13 +392,14 @@ const TestStep = struct {
|
|
|
302
392
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
|
|
303
393
|
self.* = .{ .step = step, .build = test_filter == null };
|
|
304
394
|
|
|
305
|
-
const path =
|
|
306
|
-
const tests = b.addTest(.{
|
|
307
|
-
.
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
395
|
+
const path = if (has_path) .{ .path = "src/lib/test.zig" } else b.path("src/lib/test.zig");
|
|
396
|
+
const tests = b.addTest(if (force_pic) .{
|
|
397
|
+
.root_source_file = path,
|
|
398
|
+
.optimize = config.optimize,
|
|
399
|
+
.target = config.target,
|
|
400
|
+
.filter = test_filter,
|
|
401
|
+
} else .{
|
|
402
|
+
.root_source_file = path,
|
|
312
403
|
.optimize = config.optimize,
|
|
313
404
|
.target = config.target,
|
|
314
405
|
.filter = test_filter,
|
|
@@ -316,8 +407,13 @@ const TestStep = struct {
|
|
|
316
407
|
.strip = config.strip,
|
|
317
408
|
.pic = config.pic,
|
|
318
409
|
});
|
|
319
|
-
tests.root_module.addOptions("build_options", options);
|
|
410
|
+
(if (root_module) tests.root_module else tests).addOptions("build_options", options);
|
|
411
|
+
|
|
320
412
|
maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
|
|
413
|
+
if (force_pic) {
|
|
414
|
+
tests.single_threaded = true;
|
|
415
|
+
if (config.pic orelse false) tests.force_pic = config.pic;
|
|
416
|
+
}
|
|
321
417
|
if (coverage) |c| {
|
|
322
418
|
tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
|
|
323
419
|
}
|
|
@@ -329,15 +425,16 @@ const TestStep = struct {
|
|
|
329
425
|
|
|
330
426
|
const ToolConfig = struct {
|
|
331
427
|
options: Options,
|
|
428
|
+
module: ?*std.Build.Module,
|
|
332
429
|
general: Config,
|
|
333
430
|
tool: struct {
|
|
334
431
|
tests: ?*TestStep,
|
|
335
432
|
name: ?[]const u8 = null,
|
|
336
|
-
exes: *std.ArrayList(*
|
|
433
|
+
exes: *std.ArrayList(*Step.Compile),
|
|
337
434
|
},
|
|
338
435
|
};
|
|
339
436
|
|
|
340
|
-
fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*
|
|
437
|
+
fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
|
|
341
438
|
std.fs.cwd().access(path, .{}) catch |err| switch (err) {
|
|
342
439
|
error.FileNotFound => return null,
|
|
343
440
|
else => |e| return e,
|
|
@@ -347,17 +444,32 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.R
|
|
|
347
444
|
if (index) |i| name = name[0..i];
|
|
348
445
|
if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
|
|
349
446
|
|
|
350
|
-
const exe = b.addExecutable(.{
|
|
447
|
+
const exe = b.addExecutable(if (force_pic) .{
|
|
351
448
|
.name = name,
|
|
352
|
-
.root_source_file = .{ .path = path },
|
|
449
|
+
.root_source_file = if (has_path) .{ .path = path } else b.path(path),
|
|
450
|
+
.target = config.general.target,
|
|
451
|
+
.optimize = config.general.optimize,
|
|
452
|
+
} else .{
|
|
453
|
+
.name = name,
|
|
454
|
+
.root_source_file = if (has_path) .{ .path = path } else b.path(path),
|
|
353
455
|
.target = config.general.target,
|
|
354
456
|
.optimize = config.general.optimize,
|
|
355
457
|
.single_threaded = true,
|
|
356
458
|
.strip = config.general.strip,
|
|
357
459
|
.pic = config.general.pic,
|
|
358
460
|
});
|
|
359
|
-
exe.root_module.addImport("pkmn", module(b, config.options));
|
|
360
461
|
|
|
462
|
+
if (root_module) {
|
|
463
|
+
const import = if (config.module) |m| m else module(b, config.options);
|
|
464
|
+
exe.root_module.addImport("pkmn", import);
|
|
465
|
+
} else {
|
|
466
|
+
exe.addModule("pkmn", module(b, config.options));
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
if (force_pic) {
|
|
470
|
+
exe.single_threaded = true;
|
|
471
|
+
if (config.general.pic orelse false) exe.force_pic = config.general.pic;
|
|
472
|
+
}
|
|
361
473
|
if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
|
|
362
474
|
config.tool.exes.append(exe) catch @panic("OOM");
|
|
363
475
|
|
|
@@ -371,7 +483,7 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*std.Build.Step.R
|
|
|
371
483
|
const ToolsStep = struct {
|
|
372
484
|
step: std.Build.Step,
|
|
373
485
|
|
|
374
|
-
pub fn create(b: *std.Build, exes: *std.ArrayList(*
|
|
486
|
+
pub fn create(b: *std.Build, exes: *std.ArrayList(*Step.Compile)) *ToolsStep {
|
|
375
487
|
const self = b.allocator.create(ToolsStep) catch @panic("OOM");
|
|
376
488
|
const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
|
|
377
489
|
self.* = .{ .step = step };
|
|
@@ -379,3 +491,8 @@ const ToolsStep = struct {
|
|
|
379
491
|
return self;
|
|
380
492
|
}
|
|
381
493
|
};
|
|
494
|
+
|
|
495
|
+
fn detect(target: anytype) std.Target {
|
|
496
|
+
if (@hasField(@TypeOf(target), "result")) return target.result;
|
|
497
|
+
return (std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target;
|
|
498
|
+
}
|
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.f34af416",
|
|
4
4
|
"description": "A minimal, complete, Pokémon battle simulation engine optimized for performance",
|
|
5
5
|
"repository": "github:pkmn/engine",
|
|
6
6
|
"license": "MIT",
|
|
@@ -31,7 +31,6 @@
|
|
|
31
31
|
"build/test/display.js",
|
|
32
32
|
"build/tools/debug.js.map",
|
|
33
33
|
"build/tools/debug.js",
|
|
34
|
-
"src/bin/install-pkmn-engine",
|
|
35
34
|
"src/lib/common/battle.zig",
|
|
36
35
|
"src/lib/common/data.zig",
|
|
37
36
|
"src/lib/common/debug.zig",
|
|
@@ -76,31 +75,33 @@
|
|
|
76
75
|
"access": "public"
|
|
77
76
|
},
|
|
78
77
|
"dependencies": {
|
|
79
|
-
"@pkmn/data": "^0.
|
|
80
|
-
"@pkmn/protocol": "^0.6.
|
|
78
|
+
"@pkmn/data": "^0.9.11",
|
|
79
|
+
"@pkmn/protocol": "^0.6.25"
|
|
81
80
|
},
|
|
82
81
|
"devDependencies": {
|
|
83
|
-
"@pkmn/eslint-config": "^
|
|
84
|
-
"@pkmn/img": "^0.2.
|
|
85
|
-
"@pkmn/sets": "^5.1.
|
|
86
|
-
"@pkmn/sim": "0.
|
|
82
|
+
"@pkmn/eslint-config": "^8.1.0",
|
|
83
|
+
"@pkmn/img": "^0.2.31",
|
|
84
|
+
"@pkmn/sets": "^5.1.2",
|
|
85
|
+
"@pkmn/sim": "0.9.11",
|
|
87
86
|
"@types/html-minifier": "^4.0.5",
|
|
88
|
-
"@types/jest": "^29.5.
|
|
87
|
+
"@types/jest": "^29.5.12",
|
|
89
88
|
"@types/minimist": "^1.2.5",
|
|
90
89
|
"@types/mustache": "^4.2.5",
|
|
91
|
-
"@types/node": "^
|
|
92
|
-
"@
|
|
93
|
-
"
|
|
94
|
-
"
|
|
90
|
+
"@types/node": "^22.0.2",
|
|
91
|
+
"@types/semver": "^7.5.8",
|
|
92
|
+
"@vitest/coverage-v8": "^2.0.5",
|
|
93
|
+
"binaryen": "^118.0.0",
|
|
94
|
+
"eslint": "<=8",
|
|
95
95
|
"html-minifier": "^4.0.0",
|
|
96
96
|
"json-stringify-pretty-compact": "4.0.0",
|
|
97
97
|
"minimist": "^1.2.8",
|
|
98
98
|
"mustache": "^4.2.0",
|
|
99
|
+
"semver": "^7.6.3",
|
|
99
100
|
"source-map-support": "^0.5.21",
|
|
100
|
-
"table": "^6.8.
|
|
101
|
+
"table": "^6.8.2",
|
|
101
102
|
"trakr": "^0.2.0",
|
|
102
|
-
"typescript": "^5.
|
|
103
|
-
"vitest": "^
|
|
103
|
+
"typescript": "^5.5.4",
|
|
104
|
+
"vitest": "^2.0.5"
|
|
104
105
|
},
|
|
105
106
|
"scripts": {
|
|
106
107
|
"lint": "eslint --cache src --ext ts,js && eslint src/bin/*",
|
|
@@ -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:
|
|
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';
|
|
@@ -163,8 +163,10 @@ const install = async () => {
|
|
|
163
163
|
}
|
|
164
164
|
|
|
165
165
|
const index = JSON.parse(await fetch(INDEX));
|
|
166
|
-
|
|
167
|
-
const
|
|
166
|
+
// TODO: ziglang/zig#17768
|
|
167
|
+
// const version = Object.keys(index)[+!ZIG_VERSION.dev];
|
|
168
|
+
const version = VERSION;
|
|
169
|
+
const release = index[VERSION][`${arch}-${platform}`];
|
|
168
170
|
const url = release.tarball;
|
|
169
171
|
|
|
170
172
|
let base = path.basename(url);
|
|
@@ -370,8 +372,10 @@ const downloadImportLib = async () => {
|
|
|
370
372
|
const args = ['build', '-p', 'build', ...opts];
|
|
371
373
|
if (!process.env.DEBUG_PKMN_ENGINE) args.push('-Doptimize=ReleaseFast', '-Dstrip');
|
|
372
374
|
if (config.type === 'node') {
|
|
373
|
-
args.push(`-Dnode-headers=${headers}`);
|
|
374
|
-
if (process.platform === 'win32')
|
|
375
|
+
args.push(`-Dnode-headers=${path.relative(ROOT, headers)}`);
|
|
376
|
+
if (process.platform === 'win32') {
|
|
377
|
+
args.push(`-Dnode-import-library=${path.relative(ROOT, lib)}`);
|
|
378
|
+
}
|
|
375
379
|
} else {
|
|
376
380
|
args.push('-Dwasm');
|
|
377
381
|
}
|
|
@@ -386,8 +390,14 @@ const downloadImportLib = async () => {
|
|
|
386
390
|
const prefix = process.platform === 'win32' ? '' : 'lib';
|
|
387
391
|
const ext = process.platform === 'darwin'
|
|
388
392
|
? 'dylib' : process.platform === 'win32' ? 'dll' : 'so';
|
|
389
|
-
|
|
390
|
-
|
|
393
|
+
try {
|
|
394
|
+
const actual = path.join(ROOT, 'build', 'lib', `${prefix}pkmn${suffix}.node.${ext}`);
|
|
395
|
+
fs.renameSync(actual, artifact);
|
|
396
|
+
} catch (err) {
|
|
397
|
+
// ziglang/zig#19743 changed this path
|
|
398
|
+
const actual = path.join(ROOT, 'build', 'bin', `${prefix}pkmn${suffix}.node.${ext}`);
|
|
399
|
+
fs.renameSync(actual, artifact);
|
|
400
|
+
}
|
|
391
401
|
}
|
|
392
402
|
}
|
|
393
403
|
})().catch(e => {
|
package/src/lib/common/data.zig
CHANGED
|
@@ -22,8 +22,8 @@ pub const Player = enum(u1) {
|
|
|
22
22
|
|
|
23
23
|
test Player {
|
|
24
24
|
try expectEqual(Player.P2, Player.P1.foe());
|
|
25
|
-
try expectEqual(0b0001, @as(u8, @bitCast(Player.P1.ident(1))));
|
|
26
|
-
try expectEqual(0b1101, @as(u8, @bitCast(Player.P2.ident(5))));
|
|
25
|
+
try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(Player.P1.ident(1))));
|
|
26
|
+
try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(Player.P2.ident(5))));
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/// An identifier for a specific Pokémon in battle.
|
|
@@ -46,8 +46,8 @@ pub const ID = packed struct(u8) {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
test ID {
|
|
49
|
-
try expectEqual(0b0001, @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
|
|
50
|
-
try expectEqual(0b1101, @as(u8, @bitCast(ID{ .player = .P2, .id = 5 })));
|
|
49
|
+
try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
|
|
50
|
+
try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(ID{ .player = .P2, .id = 5 })));
|
|
51
51
|
const id: ID = .{ .player = .P2, .id = 4 };
|
|
52
52
|
try expectEqual(id, ID.from(id.int()));
|
|
53
53
|
}
|
|
@@ -76,8 +76,8 @@ test Choice {
|
|
|
76
76
|
const p2: Choice = .{ .type = .Switch, .data = 5 };
|
|
77
77
|
try expectEqual(5, p2.data);
|
|
78
78
|
try expectEqual(Choice.Type.Move, p1.type);
|
|
79
|
-
try expectEqual(0b0001_0001, @as(u8, @bitCast(p1)));
|
|
80
|
-
try expectEqual(0b0001_0110, @as(u8, @bitCast(p2)));
|
|
79
|
+
try expectEqual(@as(u8, 0b0001_0001), @as(u8, @bitCast(p1)));
|
|
80
|
+
try expectEqual(@as(u8, 0b0001_0110), @as(u8, @bitCast(p2)));
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/// The result of the battle - all results other than 'None' should be considered terminal.
|
|
@@ -112,6 +112,6 @@ pub const Result = packed struct(u8) {
|
|
|
112
112
|
};
|
|
113
113
|
|
|
114
114
|
test Result {
|
|
115
|
-
try expectEqual(0b0101_0000, @as(u8, @bitCast(Result.Default)));
|
|
116
|
-
try expectEqual(0b1000_0000, @as(u8, @bitCast(Result{ .p2 = .Switch })));
|
|
115
|
+
try expectEqual(@as(u8, 0b0101_0000), @as(u8, @bitCast(Result.Default)));
|
|
116
|
+
try expectEqual(@as(u8, 0b1000_0000), @as(u8, @bitCast(Result{ .p2 = .Switch })));
|
|
117
117
|
}
|
package/src/lib/common/debug.zig
CHANGED
|
@@ -4,8 +4,8 @@ const debug = std.debug;
|
|
|
4
4
|
const io = std.io;
|
|
5
5
|
|
|
6
6
|
pub fn print(value: anytype) void {
|
|
7
|
-
debug.
|
|
8
|
-
defer debug.
|
|
7
|
+
debug.lockStdErr();
|
|
8
|
+
defer debug.unlockStdErr();
|
|
9
9
|
const stderr = io.getStdErr().writer();
|
|
10
10
|
|
|
11
11
|
nosuspend {
|
package/src/lib/common/js.zig
CHANGED
|
@@ -2,6 +2,8 @@ const std = @import("std");
|
|
|
2
2
|
|
|
3
3
|
const assert = std.debug.assert;
|
|
4
4
|
|
|
5
|
+
const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
|
|
6
|
+
|
|
5
7
|
const c = @cImport({
|
|
6
8
|
@cDefine("NAPI_VERSION", "8");
|
|
7
9
|
@cInclude("node_api.h");
|
|
@@ -35,7 +37,7 @@ pub const Number = struct {
|
|
|
35
37
|
const T = @TypeOf(value);
|
|
36
38
|
var result: c.napi_value = undefined;
|
|
37
39
|
assert(c.napi_ok == switch (@typeInfo(T)) {
|
|
38
|
-
|
|
40
|
+
Int => |info| switch (info.bits) {
|
|
39
41
|
0...32 => switch (info.signedness) {
|
|
40
42
|
.signed => c.napi_create_int32(env, @as(i32, value), &result),
|
|
41
43
|
.unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
|
|
@@ -50,7 +52,7 @@ pub const Number = struct {
|
|
|
50
52
|
|
|
51
53
|
pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
|
|
52
54
|
switch (@typeInfo(T)) {
|
|
53
|
-
|
|
55
|
+
Int => |info| switch (info.bits) {
|
|
54
56
|
0...32 => switch (info.signedness) {
|
|
55
57
|
.signed => {
|
|
56
58
|
var result: i32 = undefined;
|