@pkmn/engine 0.1.0-dev.c5fe6d07 → 0.1.0-dev.cbdbb719

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.
Files changed (56) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +46 -19
  3. package/build/pkg/addon.js +6 -7
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/ids.json +12 -12
  6. package/build/pkg/data/layout.json +2 -2
  7. package/build/pkg/data/protocol.json +69 -12
  8. package/build/pkg/data.d.ts +4 -4
  9. package/build/pkg/data.js.map +1 -1
  10. package/build/pkg/gen1.js +2 -2
  11. package/build/pkg/gen1.js.map +1 -1
  12. package/build/pkg/index.d.ts +3 -2
  13. package/build/pkg/index.js.map +1 -1
  14. package/build/pkg/protocol.js +61 -42
  15. package/build/pkg/protocol.js.map +1 -1
  16. package/build/test/display.js +8 -3
  17. package/build/test/display.js.map +1 -1
  18. package/build/tools/debug.js +5 -4
  19. package/build/tools/debug.js.map +1 -1
  20. package/build.zig +232 -99
  21. package/package.json +46 -23
  22. package/src/bin/install-pkmn-engine +25 -17
  23. package/src/lib/common/array.zig +77 -0
  24. package/src/lib/common/data.zig +12 -24
  25. package/src/lib/common/debug.zig +2 -2
  26. package/src/lib/common/js.zig +4 -2
  27. package/src/lib/common/optional.zig +21 -10
  28. package/src/lib/common/options.zig +2 -0
  29. package/src/lib/common/protocol.zig +807 -327
  30. package/src/lib/common/rational.zig +125 -31
  31. package/src/lib/common/rng.zig +22 -13
  32. package/src/lib/common/util.zig +50 -1
  33. package/src/lib/gen1/calc.zig +514 -135
  34. package/src/lib/gen1/chance.zig +407 -284
  35. package/src/lib/gen1/data/moves.zig +192 -180
  36. package/src/lib/gen1/data/species.zig +3 -3
  37. package/src/lib/gen1/data/types.zig +7 -7
  38. package/src/lib/gen1/data.zig +46 -57
  39. package/src/lib/gen1/helpers.zig +95 -550
  40. package/src/lib/gen1/mechanics.zig +571 -516
  41. package/src/lib/gen2/calc.zig +161 -0
  42. package/src/lib/gen2/chance.zig +907 -0
  43. package/src/lib/gen2/data/items.zig +12 -18
  44. package/src/lib/gen2/data/moves.zig +663 -569
  45. package/src/lib/gen2/data/species.zig +1 -1
  46. package/src/lib/gen2/data/types.zig +134 -31
  47. package/src/lib/gen2/data.zig +107 -79
  48. package/src/lib/gen2/generated.zig +806 -0
  49. package/src/lib/gen2/helpers.zig +49 -21
  50. package/src/lib/gen2/mechanics.zig +3563 -9
  51. package/src/lib/{binding/node.zig → node.zig} +10 -13
  52. package/src/lib/pkmn.zig +32 -15
  53. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
  54. package/src/test/display/pkmn.html.tmpl +5 -5
  55. package/src/lib/gen1/README.md +0 -892
  56. package/src/lib/gen2/README.md +0 -308
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
- return b.createModule(.{
19
- .source_file = .{ .path = dirname ++ "/src/lib/pkmn.zig" },
20
- .dependencies = &.{.{ .name = "build_options", .module = build_options.createModule() }},
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") orelse false;
36
- const pic = b.option(bool, "pic", "Force position independent code") orelse false;
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.split(u8, parsed.value.object.get("repository").?.string, ":");
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 lib = b.addSharedLibrary(.{
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
- .main_pkg_path = .{ .path = "." },
70
- .root_source_file = .{ .path = "src/lib/binding/node.zig" },
103
+ .root_source_file = path,
71
104
  .optimize = optimize,
72
105
  .target = target,
106
+ } else .{
107
+ .name = addon,
108
+ .root_source_file = path,
109
+ .optimize = optimize,
110
+ .target = target,
111
+ .strip = strip,
112
+ .pic = pic,
73
113
  });
74
- lib.addOptions("build_options", options);
75
- 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));
76
116
  lib.linkLibC();
77
117
  if (node_import_lib) |il| {
78
- lib.addObjectFile(.{ .path = il });
79
- } else if ((try std.zig.system.NativeTargetInfo.detect(target)).target.os.tag == .windows) {
118
+ lib.addObjectFile(if (has_path) .{ .path = il } else b.path(il));
119
+ } else if (detect(target).os.tag == .windows) {
80
120
  try std.io.getStdErr().writeAll("Must provide --node-import-library path on Windows\n");
81
121
  std.process.exit(1);
82
122
  }
83
123
  lib.linker_allow_shlib_undefined = true;
84
124
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
85
- if (pic) lib.force_pic = pic;
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(ziglang/zig#2231): using the following used to work (though was hacky):
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,59 +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 lib = b.addSharedLibrary(.{
97
- .name = name,
98
- .main_pkg_path = .{ .path = "." },
99
- .root_source_file = .{ .path = "src/lib/binding/wasm.zig" },
100
- .optimize = switch (optimize) {
101
- .ReleaseFast, .ReleaseSafe => .ReleaseSmall,
102
- else => optimize,
103
- },
104
- .target = .{ .cpu_arch = .wasm32, .os_tag = .freestanding },
105
- });
106
- lib.addOptions("build_options", options);
107
- lib.stack_size = wasm_stack_size;
108
- lib.rdynamic = true;
109
- lib.strip = strip;
110
- if (pic) lib.force_pic = pic;
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;
179
+ };
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
+
111
185
  const opt = b.findProgram(&.{"wasm-opt"}, &.{"./node_modules/.bin"}) catch null;
112
186
  if (optimize != .Debug and opt != null) {
113
187
  const out = b.fmt("build/lib/{s}.wasm", .{name});
114
188
  const sh = b.addSystemCommand(&.{ opt.?, "-O4" });
115
- sh.addArtifactArg(lib);
189
+ sh.addArtifactArg(bin);
116
190
  sh.addArg("-o");
117
- sh.addFileSourceArg(.{ .path = out });
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
+ }
118
196
  b.getInstallStep().dependOn(&sh.step);
197
+ if (!entry) b.installArtifact(bin);
198
+ } else if (entry) {
199
+ b.getInstallStep().dependOn(&b.addInstallArtifact(bin, .{
200
+ .dest_dir = .{ .override = std.Build.InstallDir{ .lib = {} } },
201
+ }).step);
119
202
  }
120
- b.installArtifact(lib);
121
203
  } else if (dynamic) {
122
- const lib = b.addSharedLibrary(.{
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) .{
206
+ .name = name,
207
+ .root_source_file = path,
208
+ .version = try std.SemanticVersion.parse(version),
209
+ .optimize = optimize,
210
+ .target = target,
211
+ } else .{
123
212
  .name = name,
124
- .main_pkg_path = .{ .path = "." },
125
- .root_source_file = .{ .path = "src/lib/binding/c.zig" },
213
+ .root_source_file = path,
126
214
  .version = try std.SemanticVersion.parse(version),
127
215
  .optimize = optimize,
128
216
  .target = target,
217
+ .strip = strip,
218
+ .pic = pic,
129
219
  });
130
- lib.addOptions("build_options", options);
131
- 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"));
132
222
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
133
- if (pic) lib.force_pic = pic;
223
+ if (force_pic and pic orelse false) lib.force_pic = pic;
134
224
  b.installArtifact(lib);
135
225
  c = true;
136
226
  } else {
137
- const lib = b.addStaticLibrary(.{
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) .{
138
229
  .name = name,
139
- .main_pkg_path = .{ .path = "." },
140
- .root_source_file = .{ .path = "src/lib/binding/c.zig" },
230
+ .root_source_file = path,
141
231
  .optimize = optimize,
142
232
  .target = target,
233
+ } else .{
234
+ .name = name,
235
+ .root_source_file = path,
236
+ .optimize = optimize,
237
+ .target = target,
238
+ .strip = strip,
239
+ .pic = pic,
143
240
  });
144
- lib.addOptions("build_options", options);
145
- 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"));
146
243
  lib.bundle_compiler_rt = true;
147
244
  maybeStrip(b, lib, b.getInstallStep(), strip, cmd);
148
- if (pic) lib.force_pic = pic;
245
+ if (force_pic and pic orelse false) lib.force_pic = pic;
149
246
  if (emit_asm) {
150
247
  b.getInstallStep().dependOn(&b.addInstallFileWithDir(
151
248
  lib.getEmittedAsm(),
@@ -165,15 +262,17 @@ pub fn build(b: *std.Build) !void {
165
262
  }
166
263
 
167
264
  if (c) {
168
- const header = b.addInstallFileWithDir(
169
- .{ .path = "src/include/pkmn.h" },
170
- .header,
171
- "pkmn.h",
172
- );
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");
173
268
  b.getInstallStep().dependOn(&header.step);
174
269
 
175
270
  const pc = b.fmt("lib{s}.pc", .{name});
176
- const file = try b.cache_root.join(b.allocator, &.{pc});
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
+ );
177
276
  const pkgconfig_file = try std.fs.cwd().createFile(file, .{});
178
277
 
179
278
  const dirname = comptime std.fs.path.dirname(@src().file) orelse ".";
@@ -206,7 +305,7 @@ pub fn build(b: *std.Build) !void {
206
305
  // TODO: tests can be run multiple times due to @imports
207
306
  const tests = TestStep.create(b, options, config);
208
307
 
209
- var exes = std.ArrayList(*std.Build.CompileStep).init(b.allocator);
308
+ var exes = std.ArrayList(*Step.Compile).init(b.allocator);
210
309
  const tools: ToolConfig = .{
211
310
  .options = .{
212
311
  .showdown = showdown,
@@ -214,6 +313,7 @@ pub fn build(b: *std.Build) !void {
214
313
  .chance = chance,
215
314
  .calc = calc,
216
315
  },
316
+ .module = pkmn,
217
317
  .general = config,
218
318
  .tool = .{
219
319
  .tests = if (tests.build) tests else null,
@@ -224,41 +324,43 @@ pub fn build(b: *std.Build) !void {
224
324
  var benchmark_config = tools;
225
325
  benchmark_config.general.optimize = .ReleaseFast;
226
326
  benchmark_config.general.strip = true;
227
- const benchmark = tool(b, "src/test/benchmark.zig", benchmark_config);
327
+ const benchmark = try tool(b, "src/test/benchmark.zig", benchmark_config);
228
328
 
229
329
  var fuzz_config = tools;
230
330
  fuzz_config.general.strip = false;
231
331
  fuzz_config.tool.name = "fuzz";
232
- const fuzz = tool(b, "src/test/fuzz.zig", fuzz_config);
332
+ const fuzz = try tool(b, "src/test/fuzz.zig", fuzz_config);
233
333
 
234
- const analyze = tool(b, "src/tools/analyze.zig", tools);
235
- const dump = tool(b, "src/tools/dump.zig", tools);
236
- const transitions = tool(b, "src/tools/transitions.zig", tools);
334
+ const analyze = try tool(b, "src/tools/analyze.zig", tools);
335
+ const dump = try tool(b, "src/tools/dump.zig", tools);
336
+ const transitions = try tool(b, "src/tools/transitions.zig", tools);
237
337
 
238
- // FIXME: serde randomly fails to build in recent Windows release configurations...
338
+ // FIXME: serde randomly fails to build in some release configurations
239
339
  var hack = tools;
240
- if (builtin.os.tag == .windows and optimize != .Debug) hack.tool.tests = null;
241
- const serde = tool(b, "src/tools/serde.zig", hack);
242
-
243
- b.step("analyze", "Run LLVM analysis tool").dependOn(&analyze.step);
244
- b.step("benchmark", "Run benchmark code").dependOn(&benchmark.step);
245
- b.step("dump", "Run protocol dump tool").dependOn(&dump.step);
246
- b.step("fuzz", "Run fuzz tester").dependOn(&fuzz.step);
247
- b.step("serde", "Run serialization/deserialization tool").dependOn(&serde.step);
340
+ if (optimize != .Debug) hack.tool.tests = null;
341
+ const serde = try tool(b, "src/tools/serde.zig", hack);
342
+
343
+ if (analyze) |t| b.step("analyze", "Run LLVM analysis tool").dependOn(&t.step);
344
+ if (benchmark) |t| b.step("benchmark", "Run benchmark code").dependOn(&t.step);
345
+ if (dump) |t| b.step("dump", "Run protocol dump tool").dependOn(&t.step);
346
+ if (fuzz) |t| b.step("fuzz", "Run fuzz tester").dependOn(&t.step);
347
+ if (serde) |t| b.step("serde", "Run serialization/deserialization tool").dependOn(&t.step);
248
348
  b.step("test", "Run all tests").dependOn(&tests.step);
249
349
  b.step("tools", "Install tools").dependOn(&ToolsStep.create(b, &exes).step);
250
- b.step("transitions", "Visualize transitions algorithm search").dependOn(&transitions.step);
350
+ if (transitions) |t| {
351
+ b.step("transitions", "Visualize transitions algorithm search").dependOn(&t.step);
352
+ }
251
353
  }
252
354
 
253
355
  fn maybeStrip(
254
356
  b: *std.Build,
255
- artifact: *std.Build.CompileStep,
357
+ artifact: *Step.Compile,
256
358
  step: *std.Build.Step,
257
- strip: bool,
359
+ strip: ?bool,
258
360
  cmd: ?[]const u8,
259
361
  ) void {
260
- artifact.strip = strip;
261
- if (!strip or cmd == null) return;
362
+ if (@hasDecl(@TypeOf(artifact.*), "strip")) artifact.strip = strip orelse false;
363
+ if (!(strip orelse false) or cmd == null) return;
262
364
  // Using `strip -r -u` for dynamic libraries is supposed to work on macOS but doesn't...
263
365
  const mac = builtin.os.tag == .macos;
264
366
  if (mac and artifact.isDynamicLibrary()) return;
@@ -270,10 +372,10 @@ fn maybeStrip(
270
372
  }
271
373
 
272
374
  const Config = struct {
273
- target: std.zig.CrossTarget,
375
+ target: ResolvedTarget,
274
376
  optimize: std.builtin.OptimizeMode,
275
- pic: bool,
276
- strip: bool,
377
+ pic: ?bool,
378
+ strip: ?bool,
277
379
  cmd: ?[]const u8,
278
380
  };
279
381
 
@@ -281,32 +383,37 @@ const TestStep = struct {
281
383
  step: std.Build.Step,
282
384
  build: bool,
283
385
 
284
- pub fn create(b: *std.Build, options: *std.Build.OptionsStep, config: Config) *TestStep {
386
+ pub fn create(b: *std.Build, options: *Step.Options, config: Config) *TestStep {
285
387
  const coverage = b.option([]const u8, "test-coverage", "Generate test coverage");
286
- const test_file = b.option([]const u8, "test-file", "Input file for test");
287
388
  const test_filter =
288
389
  b.option([]const u8, "test-filter", "Skip tests that do not match filter");
289
390
 
290
391
  const self = b.allocator.create(TestStep) catch @panic("OOM");
291
392
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Run all tests", .owner = b });
292
- self.* = TestStep{ .step = step, .build = test_filter == null };
293
-
294
- const path = test_file orelse "src/lib/test.zig";
295
- const tests = b.addTest(.{
296
- .name = b.fmt("{s}-{s}", .{
297
- std.fs.path.basename(std.fs.path.dirname(path).?),
298
- std.fs.path.stem(std.fs.path.basename(path)),
299
- }),
300
- .main_pkg_path = .{ .path = "." },
301
- .root_source_file = .{ .path = path },
393
+ self.* = .{ .step = step, .build = test_filter == null };
394
+
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,
302
398
  .optimize = config.optimize,
303
399
  .target = config.target,
304
400
  .filter = test_filter,
401
+ } else .{
402
+ .root_source_file = path,
403
+ .optimize = config.optimize,
404
+ .target = config.target,
405
+ .filter = test_filter,
406
+ .single_threaded = true,
407
+ .strip = config.strip,
408
+ .pic = config.pic,
305
409
  });
306
- tests.addOptions("build_options", options);
307
- tests.single_threaded = true;
410
+ (if (root_module) tests.root_module else tests).addOptions("build_options", options);
411
+
308
412
  maybeStrip(b, tests, &tests.step, config.strip, config.cmd);
309
- if (config.pic) tests.force_pic = config.pic;
413
+ if (force_pic) {
414
+ tests.single_threaded = true;
415
+ if (config.pic orelse false) tests.force_pic = config.pic;
416
+ }
310
417
  if (coverage) |c| {
311
418
  tests.setExecCmd(&.{ "kcov", "--include-pattern=src/lib", c, null });
312
419
  }
@@ -318,30 +425,51 @@ const TestStep = struct {
318
425
 
319
426
  const ToolConfig = struct {
320
427
  options: Options,
428
+ module: ?*std.Build.Module,
321
429
  general: Config,
322
430
  tool: struct {
323
431
  tests: ?*TestStep,
324
432
  name: ?[]const u8 = null,
325
- exes: *std.ArrayList(*std.Build.CompileStep),
433
+ exes: *std.ArrayList(*Step.Compile),
326
434
  },
327
435
  };
328
436
 
329
- fn tool(b: *std.Build, path: []const u8, config: ToolConfig) *std.Build.RunStep {
437
+ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) !?*Step.Run {
438
+ std.fs.cwd().access(path, .{}) catch |err| switch (err) {
439
+ error.FileNotFound => return null,
440
+ else => |e| return e,
441
+ };
330
442
  var name = config.tool.name orelse std.fs.path.basename(path);
331
443
  const index = std.mem.lastIndexOfScalar(u8, name, '.');
332
444
  if (index) |i| name = name[0..i];
333
445
  if (config.options.showdown orelse false) name = b.fmt("{s}-showdown", .{name});
334
446
 
335
- const exe = b.addExecutable(.{
447
+ const exe = b.addExecutable(if (force_pic) .{
448
+ .name = name,
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 .{
336
453
  .name = name,
337
- .root_source_file = .{ .path = path },
454
+ .root_source_file = if (has_path) .{ .path = path } else b.path(path),
338
455
  .target = config.general.target,
339
456
  .optimize = config.general.optimize,
457
+ .single_threaded = true,
458
+ .strip = config.general.strip,
459
+ .pic = config.general.pic,
340
460
  });
341
- exe.addModule("pkmn", module(b, config.options));
342
461
 
343
- exe.single_threaded = true;
344
- if (config.general.pic) exe.force_pic = config.general.pic;
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
+ }
345
473
  if (config.tool.tests) |ts| ts.step.dependOn(&exe.step);
346
474
  config.tool.exes.append(exe) catch @panic("OOM");
347
475
 
@@ -355,11 +483,16 @@ fn tool(b: *std.Build, path: []const u8, config: ToolConfig) *std.Build.RunStep
355
483
  const ToolsStep = struct {
356
484
  step: std.Build.Step,
357
485
 
358
- pub fn create(b: *std.Build, exes: *std.ArrayList(*std.Build.CompileStep)) *ToolsStep {
486
+ pub fn create(b: *std.Build, exes: *std.ArrayList(*Step.Compile)) *ToolsStep {
359
487
  const self = b.allocator.create(ToolsStep) catch @panic("OOM");
360
488
  const step = std.Build.Step.init(.{ .id = .custom, .name = "Install tools", .owner = b });
361
- self.* = ToolsStep{ .step = step };
489
+ self.* = .{ .step = step };
362
490
  for (exes.items) |t| self.step.dependOn(&b.addInstallArtifact(t, .{}).step);
363
491
  return self;
364
492
  }
365
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.c5fe6d07",
3
+ "version": "0.1.0-dev.cbdbb719",
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,13 +31,11 @@
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
- "src/lib/binding/node.zig",
36
- "src/lib/binding/wasm.zig",
37
34
  "src/lib/common/battle.zig",
38
35
  "src/lib/common/data.zig",
39
36
  "src/lib/common/debug.zig",
40
37
  "src/lib/common/js.zig",
38
+ "src/lib/common/array.zig",
41
39
  "src/lib/common/optional.zig",
42
40
  "src/lib/common/options.zig",
43
41
  "src/lib/common/protocol.zig",
@@ -52,14 +50,19 @@
52
50
  "src/lib/gen1/data/types.zig",
53
51
  "src/lib/gen1/helpers.zig",
54
52
  "src/lib/gen1/mechanics.zig",
53
+ "src/lib/gen2/calc.zig",
54
+ "src/lib/gen2/chance.zig",
55
55
  "src/lib/gen2/data.zig",
56
56
  "src/lib/gen2/data/items.zig",
57
57
  "src/lib/gen2/data/moves.zig",
58
58
  "src/lib/gen2/data/species.zig",
59
59
  "src/lib/gen2/data/types.zig",
60
+ "src/lib/gen2/generated.zig",
60
61
  "src/lib/gen2/helpers.zig",
61
62
  "src/lib/gen2/mechanics.zig",
63
+ "src/lib/node.zig",
62
64
  "src/lib/pkmn.zig",
65
+ "src/lib/wasm.zig",
63
66
  "src/test/display/pkmn.html.tmpl"
64
67
  ],
65
68
  "engines": {
@@ -73,30 +76,33 @@
73
76
  "access": "public"
74
77
  },
75
78
  "dependencies": {
76
- "@pkmn/data": "^0.7.46",
77
- "@pkmn/protocol": "^0.6.12"
79
+ "@pkmn/data": "^0.9.11",
80
+ "@pkmn/protocol": "^0.6.25"
78
81
  },
79
82
  "devDependencies": {
80
- "@pkmn/eslint-config": "^5.0.0",
81
- "@pkmn/img": "^0.2.13",
82
- "@pkmn/sets": "^5.0.4",
83
- "@pkmn/sim": "0.7.46",
84
- "@types/html-minifier": "^4.0.2",
85
- "@types/jest": "^29.5.3",
86
- "@types/minimist": "^1.2.2",
87
- "@types/mustache": "^4.2.2",
88
- "@types/node": "^20.4.2",
89
- "@vitest/coverage-c8": "^0.33.0",
90
- "binaryen": "^114.0.0",
91
- "eslint": "^8.44.0",
83
+ "@pkmn/eslint-config": "^8.1.0",
84
+ "@pkmn/img": "^0.2.31",
85
+ "@pkmn/sets": "^5.1.2",
86
+ "@pkmn/sim": "0.9.11",
87
+ "@types/html-minifier": "^4.0.5",
88
+ "@types/jest": "^29.5.12",
89
+ "@types/minimist": "^1.2.5",
90
+ "@types/mustache": "^4.2.5",
91
+ "@types/node": "^22.0.2",
92
+ "@types/semver": "^7.5.8",
93
+ "@vitest/coverage-v8": "^2.0.5",
94
+ "binaryen": "^118.0.0",
95
+ "eslint": "<=8",
92
96
  "html-minifier": "^4.0.0",
93
- "json-stringify-pretty-compact": "3.0.0",
97
+ "json-stringify-pretty-compact": "4.0.0",
94
98
  "minimist": "^1.2.8",
95
99
  "mustache": "^4.2.0",
100
+ "semver": "^7.6.3",
96
101
  "source-map-support": "^0.5.21",
102
+ "table": "^6.8.2",
97
103
  "trakr": "^0.2.0",
98
- "typescript": "^5.1.6",
99
- "vitest": "^0.33.0"
104
+ "typescript": "^5.5.4",
105
+ "vitest": "^2.0.5"
100
106
  },
101
107
  "scripts": {
102
108
  "lint": "eslint --cache src --ext ts,js && eslint src/bin/*",
@@ -118,7 +124,7 @@
118
124
  "ignorePatterns": [
119
125
  "dist/",
120
126
  "node_modules/",
121
- "src/examples/zig",
127
+ "examples/zig",
122
128
  "build/"
123
129
  ],
124
130
  "env": {
@@ -134,10 +140,18 @@
134
140
  "parserOptions": {
135
141
  "project": [
136
142
  "./tsconfig.json",
137
- "./src/examples/js/tsconfig.json"
143
+ "./examples/js/tsconfig.json"
138
144
  ]
139
145
  }
140
146
  },
147
+ {
148
+ "files": [
149
+ "src/test/display.ts"
150
+ ],
151
+ "rules": {
152
+ "@typescript-eslint/no-unnecessary-type-assertion": "off"
153
+ }
154
+ },
141
155
  {
142
156
  "files": [
143
157
  "src/pkg/index.ts",
@@ -168,6 +182,15 @@
168
182
  "@typescript-eslint/no-redundant-type-constituents": "off"
169
183
  }
170
184
  },
185
+ {
186
+ "files": [
187
+ "src/pkg/protocol.test.ts"
188
+ ],
189
+ "rules": {
190
+ "jest/no-standalone-expect": "off",
191
+ "jest/no-conditional-expect": "off"
192
+ }
193
+ },
171
194
  {
172
195
  "files": [
173
196
  "src/test/*.test.ts",