@pkmn/engine 0.1.0-dev.c544e10e → 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 (46) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +41 -14
  3. package/build/pkg/addon.js +6 -7
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data.d.ts +4 -4
  6. package/build/pkg/data.js.map +1 -1
  7. package/build/pkg/gen1.js.map +1 -1
  8. package/build/pkg/index.d.ts +3 -2
  9. package/build/pkg/index.js.map +1 -1
  10. package/build/pkg/protocol.js +4 -4
  11. package/build/pkg/protocol.js.map +1 -1
  12. package/build/test/display.js +8 -3
  13. package/build/test/display.js.map +1 -1
  14. package/build/tools/debug.js +2 -3
  15. package/build/tools/debug.js.map +1 -1
  16. package/build.zig +208 -81
  17. package/package.json +32 -21
  18. package/src/bin/install-pkmn-engine +25 -17
  19. package/src/lib/common/array.zig +77 -0
  20. package/src/lib/common/data.zig +4 -4
  21. package/src/lib/common/debug.zig +2 -2
  22. package/src/lib/common/js.zig +4 -2
  23. package/src/lib/common/optional.zig +14 -10
  24. package/src/lib/common/options.zig +2 -0
  25. package/src/lib/common/protocol.zig +73 -73
  26. package/src/lib/common/rational.zig +121 -27
  27. package/src/lib/common/rng.zig +7 -7
  28. package/src/lib/common/util.zig +50 -1
  29. package/src/lib/gen1/calc.zig +511 -131
  30. package/src/lib/gen1/chance.zig +374 -267
  31. package/src/lib/gen1/data/species.zig +1 -1
  32. package/src/lib/gen1/data.zig +15 -14
  33. package/src/lib/gen1/helpers.zig +96 -536
  34. package/src/lib/gen1/mechanics.zig +285 -189
  35. package/src/lib/gen2/calc.zig +34 -16
  36. package/src/lib/gen2/chance.zig +85 -492
  37. package/src/lib/gen2/data.zig +8 -7
  38. package/src/lib/gen2/generated.zig +1 -3
  39. package/src/lib/gen2/helpers.zig +41 -9
  40. package/src/lib/gen2/mechanics.zig +178 -121
  41. package/src/lib/{binding/node.zig → node.zig} +9 -12
  42. package/src/lib/pkmn.zig +32 -15
  43. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
  44. package/src/test/display/pkmn.html.tmpl +5 -5
  45. package/src/lib/gen1/README.md +0 -894
  46. package/src/lib/gen2/README.md +0 -329
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pkmn/engine",
3
- "version": "0.1.0-dev.c544e10e",
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",
@@ -62,7 +60,9 @@
62
60
  "src/lib/gen2/generated.zig",
63
61
  "src/lib/gen2/helpers.zig",
64
62
  "src/lib/gen2/mechanics.zig",
63
+ "src/lib/node.zig",
65
64
  "src/lib/pkmn.zig",
65
+ "src/lib/wasm.zig",
66
66
  "src/test/display/pkmn.html.tmpl"
67
67
  ],
68
68
  "engines": {
@@ -76,30 +76,33 @@
76
76
  "access": "public"
77
77
  },
78
78
  "dependencies": {
79
- "@pkmn/data": "^0.7.53",
80
- "@pkmn/protocol": "^0.6.16"
79
+ "@pkmn/data": "^0.9.11",
80
+ "@pkmn/protocol": "^0.6.25"
81
81
  },
82
82
  "devDependencies": {
83
- "@pkmn/eslint-config": "^5.3.0",
84
- "@pkmn/img": "^0.2.18",
85
- "@pkmn/sets": "^5.1.0",
86
- "@pkmn/sim": "0.7.53",
87
- "@types/html-minifier": "^4.0.3",
88
- "@types/jest": "^29.5.5",
89
- "@types/minimist": "^1.2.3",
90
- "@types/mustache": "^4.2.3",
91
- "@types/node": "^20.8.2",
92
- "@vitest/coverage-c8": "^0.33.0",
93
- "binaryen": "^114.0.0",
94
- "eslint": "^8.51.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",
95
96
  "html-minifier": "^4.0.0",
96
- "json-stringify-pretty-compact": "3.0.0",
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",
102
+ "table": "^6.8.2",
100
103
  "trakr": "^0.2.0",
101
- "typescript": "^5.2.2",
102
- "vitest": "^0.34.6"
104
+ "typescript": "^5.5.4",
105
+ "vitest": "^2.0.5"
103
106
  },
104
107
  "scripts": {
105
108
  "lint": "eslint --cache src --ext ts,js && eslint src/bin/*",
@@ -141,6 +144,14 @@
141
144
  ]
142
145
  }
143
146
  },
147
+ {
148
+ "files": [
149
+ "src/test/display.ts"
150
+ ],
151
+ "rules": {
152
+ "@typescript-eslint/no-unnecessary-type-assertion": "off"
153
+ }
154
+ },
144
155
  {
145
156
  "files": [
146
157
  "src/pkg/index.ts",
@@ -283,19 +283,19 @@ const downloadImportLib = async () => {
283
283
  let error = false;
284
284
  for (let i = 2; i < process.argv.length; i++) {
285
285
  switch (process.argv[i]) {
286
- case "--silent": {
287
- SILENT = true;
288
- break;
289
- }
290
- case "--zig": {
291
- if (options || only) error = true;
292
- only = true;
293
- break;
294
- }
295
- default: {
296
- if (options || only || !process.argv[i].startsWith('--options=')) error = true;
297
- options = process.argv[i].slice(10).split(' ');
298
- }
286
+ case "--silent": {
287
+ SILENT = true;
288
+ break;
289
+ }
290
+ case "--zig": {
291
+ if (options || only) error = true;
292
+ only = true;
293
+ break;
294
+ }
295
+ default: {
296
+ if (options || only || !process.argv[i].startsWith('--options=')) error = true;
297
+ options = process.argv[i].slice(10).split(' ');
298
+ }
299
299
  }
300
300
  }
301
301
  if (error) {
@@ -370,8 +370,10 @@ const downloadImportLib = async () => {
370
370
  const args = ['build', '-p', 'build', ...opts];
371
371
  if (!process.env.DEBUG_PKMN_ENGINE) args.push('-Doptimize=ReleaseFast', '-Dstrip');
372
372
  if (config.type === 'node') {
373
- args.push(`-Dnode-headers=${headers}`);
374
- if (process.platform === 'win32') args.push(`-Dnode-import-library=${lib}`);
373
+ args.push(`-Dnode-headers=${path.relative(ROOT, headers)}`);
374
+ if (process.platform === 'win32') {
375
+ args.push(`-Dnode-import-library=${path.relative(ROOT, lib)}`);
376
+ }
375
377
  } else {
376
378
  args.push('-Dwasm');
377
379
  }
@@ -386,8 +388,14 @@ const downloadImportLib = async () => {
386
388
  const prefix = process.platform === 'win32' ? '' : 'lib';
387
389
  const ext = process.platform === 'darwin'
388
390
  ? '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));
391
+ try {
392
+ const actual = path.join(ROOT, 'build', 'lib', `${prefix}pkmn${suffix}.node.${ext}`);
393
+ fs.renameSync(actual, artifact);
394
+ } catch (err) {
395
+ // ziglang/zig#19743 changed this path
396
+ const actual = path.join(ROOT, 'build', 'bin', `${prefix}pkmn${suffix}.node.${ext}`);
397
+ fs.renameSync(actual, artifact);
398
+ }
391
399
  }
392
400
  }
393
401
  })().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
+ }
@@ -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
  }
@@ -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.getStderrMutex().lock();
8
- defer debug.getStderrMutex().unlock();
7
+ debug.lockStdErr();
8
+ defer debug.unlockStdErr();
9
9
  const stderr = io.getStdErr().writer();
10
10
 
11
11
  nosuspend {
@@ -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
- .Int => |info| switch (info.bits) {
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
- .Int => |info| switch (info.bits) {
55
+ Int => |info| switch (info.bits) {
54
56
  0...32 => switch (info.signedness) {
55
57
  .signed => {
56
58
  var result: i32 = undefined;
@@ -3,10 +3,12 @@ const std = @import("std");
3
3
  const expect = std.testing.expect;
4
4
  const assert = std.debug.assert;
5
5
 
6
+ const Bool = if (@hasField(std.builtin.Type, "bool")) .bool else .Bool;
7
+
6
8
  // TODO: ziglang/zig#104
7
9
  pub fn Optional(comptime T: type) type {
8
10
  const fields = std.meta.fields(switch (@typeInfo(T)) {
9
- .Bool => enum { false, true },
11
+ Bool => enum { false, true },
10
12
  else => T,
11
13
  });
12
14
 
@@ -23,14 +25,16 @@ pub fn Optional(comptime T: type) type {
23
25
  };
24
26
  }
25
27
 
26
- return @Type(.{
27
- .Enum = .{
28
- .tag_type = std.math.IntFittingRange(0, fields.len),
29
- .fields = &enumFields,
30
- .decls = &decls,
31
- .is_exhaustive = true,
32
- },
33
- });
28
+ const options = .{
29
+ .tag_type = std.math.IntFittingRange(0, fields.len),
30
+ .fields = &enumFields,
31
+ .decls = &decls,
32
+ .is_exhaustive = true,
33
+ };
34
+ return @Type(if (@hasField(std.builtin.Type, "enum"))
35
+ .{ .@"enum" = options }
36
+ else
37
+ .{ .Enum = options });
34
38
  }
35
39
 
36
40
  test Optional {
@@ -46,7 +50,7 @@ test Optional {
46
50
  try expect(b != .true);
47
51
  try expect(b != .false);
48
52
 
49
- const Player = @import("./data.zig").Player;
53
+ const Player = @import("data.zig").Player;
50
54
  try expect(@bitSizeOf(Optional(Player)) == 2);
51
55
 
52
56
  const p: Optional(Player) = .P2;
@@ -10,6 +10,7 @@ pub const Options = struct {
10
10
  advance: ?bool = null,
11
11
  ebc: ?bool = null,
12
12
  key: ?bool = null,
13
+ miss: ?bool = null,
13
14
  internal: ?bool = null,
14
15
  };
15
16
 
@@ -25,6 +26,7 @@ pub const calc = get("calc", false);
25
26
  pub const advance = get("advance", true);
26
27
  pub const ebc = get("ebc", true);
27
28
  pub const key = get("key", false);
29
+ pub const miss = get("miss", true);
28
30
  pub const internal = get("internal", false);
29
31
 
30
32
  fn get(comptime name: []const u8, default: bool) bool {