@pkmn/engine 0.1.0-dev.736f7a8b → 0.1.0-dev.77410aff

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 (45) hide show
  1. package/README.md +36 -14
  2. package/build/pkg/addon.js.map +1 -1
  3. package/build/pkg/data/ids.json +12 -12
  4. package/build/pkg/data/layout.json +2 -2
  5. package/build/pkg/data/protocol.json +69 -12
  6. package/build/pkg/data.d.ts +3 -2
  7. package/build/pkg/data.js.map +1 -1
  8. package/build/pkg/gen1.js +2 -2
  9. package/build/pkg/gen1.js.map +1 -1
  10. package/build/pkg/index.d.ts +2 -1
  11. package/build/pkg/index.js.map +1 -1
  12. package/build/pkg/protocol.js +57 -38
  13. package/build/pkg/protocol.js.map +1 -1
  14. package/build/test/display.js.map +1 -1
  15. package/build/tools/debug.js +3 -1
  16. package/build/tools/debug.js.map +1 -1
  17. package/build.zig +40 -23
  18. package/package.json +30 -17
  19. package/src/bin/install-pkmn-engine +1 -1
  20. package/src/lib/binding/node.zig +7 -11
  21. package/src/lib/common/data.zig +8 -20
  22. package/src/lib/common/optional.zig +8 -1
  23. package/src/lib/common/protocol.zig +725 -299
  24. package/src/lib/common/rational.zig +12 -8
  25. package/src/lib/common/rng.zig +15 -6
  26. package/src/lib/gen1/README.md +31 -29
  27. package/src/lib/gen1/calc.zig +9 -9
  28. package/src/lib/gen1/chance.zig +55 -39
  29. package/src/lib/gen1/data/moves.zig +192 -180
  30. package/src/lib/gen1/data/species.zig +2 -2
  31. package/src/lib/gen1/data/types.zig +7 -7
  32. package/src/lib/gen1/data.zig +31 -43
  33. package/src/lib/gen1/helpers.zig +26 -41
  34. package/src/lib/gen1/mechanics.zig +314 -365
  35. package/src/lib/gen2/README.md +18 -72
  36. package/src/lib/gen2/calc.zig +143 -0
  37. package/src/lib/gen2/chance.zig +1314 -0
  38. package/src/lib/gen2/data/items.zig +12 -18
  39. package/src/lib/gen2/data/moves.zig +649 -568
  40. package/src/lib/gen2/data/species.zig +1 -1
  41. package/src/lib/gen2/data/types.zig +134 -31
  42. package/src/lib/gen2/data.zig +80 -68
  43. package/src/lib/gen2/generated.zig +806 -0
  44. package/src/lib/gen2/helpers.zig +4 -9
  45. package/src/lib/gen2/mechanics.zig +3238 -85
@@ -10,7 +10,7 @@ const path = require('path');
10
10
  const {pipeline} = require('stream/promises');
11
11
  const tty = require('tty');
12
12
 
13
- const ZIG_VERSION = {major: 0, minor: 11, patch: 0};
13
+ const ZIG_VERSION = {major: 0, minor: 12, patch: 0, dev: 1396};
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';
@@ -19,7 +19,7 @@ export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi
19
19
  }
20
20
 
21
21
  pub fn register(env: c.napi_env) c.napi_value {
22
- var object = js.Object.init(env);
22
+ const object = js.Object.init(env);
23
23
  const properties = [_]c.napi_property_descriptor{
24
24
  js.Property.init("options", .{ .value = options(env) }),
25
25
  js.Property.init("bindings", .{ .value = bindings(env) }),
@@ -29,7 +29,7 @@ pub fn register(env: c.napi_env) c.napi_value {
29
29
  }
30
30
 
31
31
  fn options(env: c.napi_env) c.napi_value {
32
- var object = js.Object.init(env);
32
+ const object = js.Object.init(env);
33
33
  const properties = [_]c.napi_property_descriptor{
34
34
  js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
35
35
  js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
@@ -41,7 +41,7 @@ fn options(env: c.napi_env) c.napi_value {
41
41
  }
42
42
 
43
43
  fn bindings(env: c.napi_env) c.napi_value {
44
- var array = js.Array.init(env, .{ .length = 1 });
44
+ const array = js.Array.init(env, .{ .length = 1 });
45
45
  js.Array.set(env, array, 0, bind(env, pkmn.gen1));
46
46
  return array;
47
47
  }
@@ -49,7 +49,7 @@ fn bindings(env: c.napi_env) c.napi_value {
49
49
  fn bind(env: c.napi_env, gen: anytype) c.napi_value {
50
50
  const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
51
51
  const logs_size: u32 = @intCast(gen.LOGS_SIZE);
52
- var object = js.Object.init(env);
52
+ const object = js.Object.init(env);
53
53
  const properties = [_]c.napi_property_descriptor{
54
54
  js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
55
55
  js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
@@ -72,7 +72,6 @@ fn update(gen: anytype) c.napi_callback {
72
72
  var len: usize = 0;
73
73
  assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
74
74
  assert(len == @sizeOf(gen.Battle(gen.PRNG)));
75
- assert(data != null);
76
75
 
77
76
  var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
78
77
  const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
@@ -85,10 +84,9 @@ fn update(gen: anytype) c.napi_callback {
85
84
  else => result: {
86
85
  assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
87
86
  assert(len == gen.LOGS_SIZE);
88
- assert(data != null);
89
87
 
90
- var buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
91
- var stream = pkmn.protocol.ByteStream{ .buffer = buf };
88
+ const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
89
+ var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
92
90
  // TODO: extract out
93
91
  var opts = pkmn.battle.options(
94
92
  pkmn.protocol.FixedLog{ .writer = stream.writer() },
@@ -116,7 +114,6 @@ fn choices(gen: anytype) c.napi_callback {
116
114
  var len: usize = 0;
117
115
  assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
118
116
  assert(len == @sizeOf(gen.Battle(gen.PRNG)));
119
- assert(data != null);
120
117
 
121
118
  var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
122
119
  const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
@@ -124,9 +121,8 @@ fn choices(gen: anytype) c.napi_callback {
124
121
 
125
122
  assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
126
123
  assert(len == gen.CHOICES_SIZE);
127
- assert(data != null);
128
124
 
129
- var out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
125
+ const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
130
126
  const n = battle.choices(player, request, out);
131
127
  return js.Number.init(env, @as(u8, @bitCast(n)));
132
128
  }
@@ -9,12 +9,12 @@ pub const Player = enum(u1) {
9
9
  P2,
10
10
 
11
11
  /// Returns a player's opponent.
12
- pub inline fn foe(self: Player) Player {
12
+ pub fn foe(self: Player) Player {
13
13
  return @enumFromInt(~@intFromEnum(self));
14
14
  }
15
15
 
16
16
  /// Return's an identifier for the player's Pokémon at the one-indexed `id`.
17
- pub inline fn ident(self: Player, id: u3) ID {
17
+ pub fn ident(self: Player, id: u3) ID {
18
18
  assert(id > 0 and id <= 6);
19
19
  return .{ .id = id, .player = self };
20
20
  }
@@ -27,24 +27,20 @@ test Player {
27
27
  }
28
28
 
29
29
  /// An identifier for a specific Pokémon in battle.
30
- pub const ID = packed struct {
30
+ pub const ID = packed struct(u8) {
31
31
  /// The one-indexed team slot of the Pokémon
32
32
  id: u3 = 0,
33
33
  /// The Pokémon's trainer.
34
34
  player: Player = .P1,
35
35
  _: u4 = 0,
36
36
 
37
- comptime {
38
- assert(@sizeOf(ID) == 1);
39
- }
40
-
41
37
  /// Converts the identifier into a number.
42
- pub inline fn int(self: ID) u4 {
38
+ pub fn int(self: ID) u4 {
43
39
  return @intCast(@as(u8, @bitCast(self)));
44
40
  }
45
41
 
46
42
  /// Decodes the identifier from a number.
47
- pub inline fn from(id: u4) ID {
43
+ pub fn from(id: u4) ID {
48
44
  return @bitCast(@as(u8, id));
49
45
  }
50
46
  };
@@ -52,12 +48,12 @@ pub const ID = packed struct {
52
48
  test ID {
53
49
  try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
54
50
  try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(ID{ .player = .P2, .id = 5 })));
55
- const id = ID{ .player = .P2, .id = 4 };
51
+ const id: ID = .{ .player = .P2, .id = 4 };
56
52
  try expectEqual(id, ID.from(id.int()));
57
53
  }
58
54
 
59
55
  /// A choice made by a player during battle.
60
- pub const Choice = packed struct {
56
+ pub const Choice = packed struct(u8) {
61
57
  /// The choice type.
62
58
  type: Choice.Type = .Pass,
63
59
  /// The choice data:
@@ -73,10 +69,6 @@ pub const Choice = packed struct {
73
69
  Move,
74
70
  Switch,
75
71
  };
76
-
77
- comptime {
78
- assert(@sizeOf(Choice) == 1);
79
- }
80
72
  };
81
73
 
82
74
  test Choice {
@@ -89,7 +81,7 @@ test Choice {
89
81
  }
90
82
 
91
83
  /// The result of the battle - all results other than 'None' should be considered terminal.
92
- pub const Result = packed struct {
84
+ pub const Result = packed struct(u8) {
93
85
  /// The type of result from the perspective of Player 1.
94
86
  /// `Error` is not possible when in Pokémon Showdown compatibility mode.
95
87
  type: Result.Type = .None,
@@ -117,10 +109,6 @@ pub const Result = packed struct {
117
109
  pub const Error: Result = .{ .type = .Error };
118
110
  /// The default result.
119
111
  pub const Default: Result = .{ .p1 = .Move, .p2 = .Move };
120
-
121
- comptime {
122
- assert(@sizeOf(Result) == 1);
123
- }
124
112
  };
125
113
 
126
114
  test Result {
@@ -14,6 +14,7 @@ pub fn Optional(comptime T: type) type {
14
14
  var decls = [_]std.builtin.Type.Declaration{};
15
15
 
16
16
  enumFields[0] = .{ .name = "None", .value = 0 };
17
+
17
18
  inline for (fields, 1..) |field, i| {
18
19
  assert(!std.mem.eql(u8, field.name, "None"));
19
20
  enumFields[i] = .{
@@ -24,7 +25,7 @@ pub fn Optional(comptime T: type) type {
24
25
 
25
26
  return @Type(.{
26
27
  .Enum = .{
27
- .tag_type = std.math.IntFittingRange(0, fields.len + 1),
28
+ .tag_type = std.math.IntFittingRange(0, fields.len),
28
29
  .fields = &enumFields,
29
30
  .decls = &decls,
30
31
  .is_exhaustive = true,
@@ -57,4 +58,10 @@ test Optional {
57
58
  try expect(q == .None);
58
59
  try expect(q != .P1);
59
60
  try expect(q != .P2);
61
+
62
+ const Three = enum(u2) { A, B, C };
63
+ try expect(@bitSizeOf(Optional(Three)) == 2);
64
+
65
+ const Four = enum(u2) { A, B, C, D };
66
+ try expect(@bitSizeOf(Optional(Four)) == 3);
60
67
  }