@pkmn/engine 0.1.0-dev.eaae4aa6 → 0.1.0-dev.f3aab252

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