@pkmn/engine 0.1.0-dev.f3aaee39 → 0.1.0-dev.f3b63b86

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 (85) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +77 -23
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +26 -4
  13. package/build/pkg/addon.js +39 -71
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/ids.json +12 -12
  16. package/build/pkg/data/layout.json +17 -4
  17. package/build/pkg/data/protocol.json +84 -13
  18. package/build/pkg/data.d.ts +27 -5
  19. package/build/pkg/data.js.map +1 -1
  20. package/build/pkg/gen1.d.ts +15 -18
  21. package/build/pkg/gen1.js +30 -21
  22. package/build/pkg/gen1.js.map +1 -1
  23. package/build/pkg/index.d.ts +42 -91
  24. package/build/pkg/index.js +23 -70
  25. package/build/pkg/index.js.map +1 -1
  26. package/build/pkg/protocol.d.ts +20 -1
  27. package/build/pkg/protocol.js +123 -102
  28. package/build/pkg/protocol.js.map +1 -1
  29. package/build/tools/debug.js +69 -74
  30. package/build/tools/debug.js.map +1 -1
  31. package/build/tools/display/debug.jsx +152 -0
  32. package/build/tools/display/debug.jsx.map +1 -0
  33. package/build/tools/display/index.js +152 -0
  34. package/build/tools/display/index.js.map +1 -0
  35. package/build/tools/display/ui.jsx +359 -0
  36. package/build/tools/display/ui.jsx.map +1 -0
  37. package/build/tools/display/util.js +33 -0
  38. package/build/tools/display/util.js.map +1 -0
  39. package/build.zig +273 -120
  40. package/package.json +64 -123
  41. package/src/bin/install-pkmn-engine +47 -21
  42. package/src/bin/pkmn-debug +1 -1
  43. package/src/lib/bindings/c.zig +200 -0
  44. package/src/lib/{binding → bindings}/node.zig +15 -32
  45. package/src/lib/bindings/wasm.zig +50 -0
  46. package/src/lib/common/array.zig +70 -0
  47. package/src/lib/common/data.zig +12 -24
  48. package/src/lib/common/debug.zig +32 -11
  49. package/src/lib/common/js.zig +3 -7
  50. package/src/lib/common/optional.zig +19 -18
  51. package/src/lib/common/options.zig +6 -0
  52. package/src/lib/common/protocol.zig +919 -430
  53. package/src/lib/common/rational.zig +341 -254
  54. package/src/lib/common/rng.zig +22 -15
  55. package/src/lib/common/util.zig +33 -1
  56. package/src/lib/gen1/calc.zig +706 -177
  57. package/src/lib/gen1/chance.zig +428 -302
  58. package/src/lib/gen1/data/moves.zig +193 -183
  59. package/src/lib/gen1/data/species.zig +4 -6
  60. package/src/lib/gen1/data/types.zig +9 -10
  61. package/src/lib/gen1/data.zig +60 -75
  62. package/src/lib/gen1/helpers.zig +90 -563
  63. package/src/lib/gen1/mechanics.zig +682 -600
  64. package/src/lib/gen2/calc.zig +150 -0
  65. package/src/lib/gen2/chance.zig +896 -0
  66. package/src/lib/gen2/data/items.zig +13 -21
  67. package/src/lib/gen2/data/moves.zig +664 -572
  68. package/src/lib/gen2/data/species.zig +2 -4
  69. package/src/lib/gen2/data/types.zig +139 -34
  70. package/src/lib/gen2/data.zig +121 -96
  71. package/src/lib/gen2/generated.zig +801 -0
  72. package/src/lib/gen2/helpers.zig +53 -38
  73. package/src/lib/gen2/mechanics.zig +3580 -29
  74. package/src/lib/napi.h +2 -0
  75. package/src/lib/node.zig +14 -0
  76. package/src/lib/pkmn.zig +125 -29
  77. package/src/lib/wasm.zig +13 -0
  78. package/src/tools/display/dom.ts +93 -0
  79. package/src/tools/display/ui.css +354 -0
  80. package/build/test/display.js +0 -400
  81. package/build/test/display.js.map +0 -1
  82. package/src/lib/binding/wasm.zig +0 -12
  83. package/src/lib/gen1/README.md +0 -892
  84. package/src/lib/gen2/README.md +0 -308
  85. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -1,25 +1,12 @@
1
- const std = @import("std");
2
- const pkmn = @import("../pkmn.zig");
3
-
1
+ const c = @import("napi");
4
2
  const js = @import("../common/js.zig");
3
+ const pkmn = @import("../pkmn.zig");
4
+ const std = @import("std");
5
5
 
6
6
  const assert = std.debug.assert;
7
7
 
8
- const c = @cImport({
9
- @cDefine("NAPI_VERSION", "8");
10
- @cInclude("node_api.h");
11
- });
12
-
13
- export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
14
- const properties = [_]c.napi_property_descriptor{
15
- js.Property.init("engine", .{ .value = register(env) }),
16
- };
17
- assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
18
- return exports;
19
- }
20
-
21
8
  pub fn register(env: c.napi_env) c.napi_value {
22
- var object = js.Object.init(env);
9
+ const object = js.Object.init(env);
23
10
  const properties = [_]c.napi_property_descriptor{
24
11
  js.Property.init("options", .{ .value = options(env) }),
25
12
  js.Property.init("bindings", .{ .value = bindings(env) }),
@@ -29,7 +16,7 @@ pub fn register(env: c.napi_env) c.napi_value {
29
16
  }
30
17
 
31
18
  fn options(env: c.napi_env) c.napi_value {
32
- var object = js.Object.init(env);
19
+ const object = js.Object.init(env);
33
20
  const properties = [_]c.napi_property_descriptor{
34
21
  js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
35
22
  js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
@@ -41,7 +28,7 @@ fn options(env: c.napi_env) c.napi_value {
41
28
  }
42
29
 
43
30
  fn bindings(env: c.napi_env) c.napi_value {
44
- var array = js.Array.init(env, .{ .length = 1 });
31
+ const array = js.Array.init(env, .{ .length = 1 });
45
32
  js.Array.set(env, array, 0, bind(env, pkmn.gen1));
46
33
  return array;
47
34
  }
@@ -49,7 +36,7 @@ fn bindings(env: c.napi_env) c.napi_value {
49
36
  fn bind(env: c.napi_env, gen: anytype) c.napi_value {
50
37
  const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
51
38
  const logs_size: u32 = @intCast(gen.LOGS_SIZE);
52
- var object = js.Object.init(env);
39
+ const object = js.Object.init(env);
53
40
  const properties = [_]c.napi_property_descriptor{
54
41
  js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
55
42
  js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
@@ -62,7 +49,7 @@ fn bind(env: c.napi_env, gen: anytype) c.napi_value {
62
49
 
63
50
  fn update(gen: anytype) c.napi_callback {
64
51
  return struct {
65
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
52
+ fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.c) c.napi_value {
66
53
  var argc: usize = 4;
67
54
  var argv: [4]c.napi_value = undefined;
68
55
  assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
@@ -72,9 +59,8 @@ fn update(gen: anytype) c.napi_callback {
72
59
  var len: usize = 0;
73
60
  assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
74
61
  assert(len == @sizeOf(gen.Battle(gen.PRNG)));
75
- assert(data != null);
76
62
 
77
- var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
63
+ var battle: *gen.Battle(gen.PRNG) = @ptrCast(@alignCast(data.?));
78
64
  const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
79
65
  const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
80
66
 
@@ -85,13 +71,12 @@ fn update(gen: anytype) c.napi_callback {
85
71
  else => result: {
86
72
  assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
87
73
  assert(len == gen.LOGS_SIZE);
88
- assert(data != null);
89
74
 
90
- var buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
91
- var stream = pkmn.protocol.ByteStream{ .buffer = buf };
75
+ const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
76
+ var writer: pkmn.protocol.Writer = .{ .buffer = buf };
92
77
  // TODO: extract out
93
78
  var opts = pkmn.battle.options(
94
- pkmn.protocol.FixedLog{ .writer = stream.writer() },
79
+ pkmn.protocol.FixedLog{ .writer = &writer },
95
80
  gen.chance.NULL,
96
81
  gen.calc.NULL,
97
82
  );
@@ -106,7 +91,7 @@ fn update(gen: anytype) c.napi_callback {
106
91
 
107
92
  fn choices(gen: anytype) c.napi_callback {
108
93
  return struct {
109
- fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
94
+ fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.c) c.napi_value {
110
95
  var argc: usize = 4;
111
96
  var argv: [4]c.napi_value = undefined;
112
97
  assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
@@ -116,17 +101,15 @@ fn choices(gen: anytype) c.napi_callback {
116
101
  var len: usize = 0;
117
102
  assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
118
103
  assert(len == @sizeOf(gen.Battle(gen.PRNG)));
119
- assert(data != null);
120
104
 
121
- var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
105
+ var battle: *gen.Battle(gen.PRNG) = @ptrCast(@alignCast(data.?));
122
106
  const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
123
107
  const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
124
108
 
125
109
  assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
126
110
  assert(len == gen.CHOICES_SIZE);
127
- assert(data != null);
128
111
 
129
- var out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
112
+ const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
130
113
  const n = battle.choices(player, request, out);
131
114
  return js.Number.init(env, @as(u8, @bitCast(n)));
132
115
  }
@@ -0,0 +1,50 @@
1
+ const pkmn = @import("../pkmn.zig");
2
+ const std = @import("std");
3
+
4
+ const assert = std.debug.assert;
5
+
6
+ pub const options = pkmn.options;
7
+
8
+ pub fn gen(comptime num: comptime_int) type {
9
+ const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
10
+ return struct {
11
+ pub const CHOICES_SIZE = size(g.CHOICES_SIZE);
12
+ pub const LOGS_SIZE = size(g.LOGS_SIZE);
13
+
14
+ pub fn update(
15
+ battle: *g.Battle(g.PRNG),
16
+ c1: pkmn.Choice,
17
+ c2: pkmn.Choice,
18
+ options_: ?[*]u8,
19
+ ) callconv(.c) pkmn.Result {
20
+ return (if (options_) |o| result: {
21
+ const buf = @as([*]u8, @ptrCast(o))[0..g.LOGS_SIZE];
22
+ var writer: pkmn.protocol.Writer = .{ .buffer = buf };
23
+ // TODO: extract out
24
+ var opts = pkmn.battle.options(
25
+ pkmn.protocol.FixedLog{ .writer = &writer },
26
+ g.chance.NULL,
27
+ g.calc.NULL,
28
+ );
29
+ break :result battle.update(c1, c2, &opts);
30
+ } else battle.update(c1, c2, &g.NULL)) catch unreachable;
31
+ }
32
+
33
+ pub fn choices(
34
+ battle: *g.Battle(g.PRNG),
35
+ player: u8,
36
+ request: u8,
37
+ buf: [*]u8,
38
+ ) callconv(.c) u8 {
39
+ assert(player <= @typeInfo(pkmn.Player).@"enum".fields.len);
40
+ assert(request <= @typeInfo(pkmn.Choice.Type).@"enum".fields.len);
41
+
42
+ const n = CHOICES_SIZE;
43
+ return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(buf[0..n]));
44
+ }
45
+ };
46
+ }
47
+
48
+ fn size(n: usize) u32 {
49
+ return std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(n))) catch unreachable;
50
+ }
@@ -0,0 +1,70 @@
1
+ const std = @import("std");
2
+
3
+ const expectEqual = std.testing.expectEqual;
4
+ const assert = std.debug.assert;
5
+
6
+ /// Helpers for working with bit-packed arrays of non-powers-of-2 types.
7
+ /// NOTE: ziglang/zig#12547
8
+ pub fn Array(comptime n: comptime_int, comptime U: type) type {
9
+ return struct {
10
+ const size = @bitSizeOf(U);
11
+ pub const T = @Int(.unsigned, size * n);
12
+
13
+ /// Returns the value stored at index `i` in the array `a`.
14
+ pub fn get(a: T, i: usize) U {
15
+ assert(i < n);
16
+ const shift = i * size;
17
+ const mask: T = (1 << size) - 1;
18
+ const result = (a >> @intCast(shift)) & mask;
19
+ return switch (@typeInfo(U)) {
20
+ .@"enum" => @enumFromInt(result),
21
+ .int => @intCast(result),
22
+ else => unreachable,
23
+ };
24
+ }
25
+
26
+ /// Sets the value at index `i` to `val` in the array `a`.
27
+ pub fn set(a: T, i: usize, val: U) T {
28
+ assert(i < n);
29
+ const v: T = switch (@typeInfo(U)) {
30
+ .@"enum" => @intFromEnum(val),
31
+ .int => @intCast(val),
32
+ else => unreachable,
33
+ };
34
+ const shift = i * size;
35
+ const mask: T = @as(T, @intCast((1 << size) - 1)) << @intCast(shift);
36
+ const result: T = a & ~mask;
37
+ return result | (v << @intCast(shift));
38
+ }
39
+ };
40
+ }
41
+
42
+ test Array {
43
+ const A = Array(5, u8);
44
+ var a: A.T = 0;
45
+ try expectEqual(u40, @TypeOf(a));
46
+
47
+ a = A.set(a, 4, 241);
48
+ try expectEqual(@as(u8, 241), A.get(a, 4));
49
+ a = A.set(a, 4, 1);
50
+ try expectEqual(@as(u8, 1), A.get(a, 4));
51
+
52
+ for (0..5) |i| a = A.set(a, i, @intCast(i));
53
+ for (0..5) |i| try expectEqual(@as(u8, @intCast(i)), A.get(a, i));
54
+
55
+ const Optional = @import("optional.zig").Optional;
56
+
57
+ const B = Array(4, Optional(bool));
58
+ var b: B.T = 0;
59
+ try expectEqual(u8, @TypeOf(b));
60
+
61
+ b = B.set(b, 2, .true);
62
+ try expectEqual(Optional(bool).None, B.get(b, 1));
63
+ try expectEqual(Optional(bool).true, B.get(b, 2));
64
+ try expectEqual(@as(u8, 0b00100000), b);
65
+ b = B.set(b, 2, .None);
66
+ b = B.set(b, 0, .false);
67
+ try expectEqual(Optional(bool).None, B.get(b, 2));
68
+ try expectEqual(Optional(bool).false, B.get(b, 0));
69
+ try expectEqual(@as(u8, 0b0000001), b);
70
+ }
@@ -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 {
@@ -84,12 +76,12 @@ test Choice {
84
76
  const p2: Choice = .{ .type = .Switch, .data = 5 };
85
77
  try expectEqual(5, p2.data);
86
78
  try expectEqual(Choice.Type.Move, p1.type);
87
- try expectEqual(0b0001_0001, @as(u8, @bitCast(p1)));
88
- 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)));
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,13 +109,9 @@ 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 {
127
- try expectEqual(0b0101_0000, @as(u8, @bitCast(Result.Default)));
128
- 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 })));
129
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 {
@@ -19,7 +19,7 @@ pub fn print(value: anytype) void {
19
19
  }) catch return;
20
20
  } else {
21
21
  switch (@typeInfo(@TypeOf(value))) {
22
- .Struct => |info| {
22
+ .@"struct" => |info| {
23
23
  if (info.is_tuple) {
24
24
  inline for (info.fields, 0..) |f, i| {
25
25
  inspect(@field(value, f.name));
@@ -42,33 +42,54 @@ fn inspect(value: anytype) void {
42
42
  nosuspend {
43
43
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
44
44
  switch (@typeInfo(@TypeOf(value))) {
45
- .Array => |info| {
45
+ .array => |info| {
46
46
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
47
47
  @compileError(err);
48
48
  },
49
- .Pointer => |ptr_info| switch (ptr_info.size) {
50
- .One => switch (@typeInfo(ptr_info.child)) {
51
- .Array => |info| {
49
+ .pointer => |ptr_info| switch (ptr_info.size) {
50
+ .one => switch (@typeInfo(ptr_info.child)) {
51
+ .array => |info| {
52
52
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
53
53
  @compileError(err);
54
54
  },
55
- .Enum, .Union, .Struct => return inspect(value.*),
55
+ .@"enum", .@"union", .@"struct" => return inspect(value.*),
56
56
  else => @compileError(err),
57
57
  },
58
- .Many, .C => {
58
+ .many, .c => {
59
59
  if (ptr_info.sentinel) |_| return inspect(std.mem.span(value));
60
60
  if (ptr_info.child == u8) {
61
61
  return stderr.print("{s}", .{std.mem.span(value)}) catch return;
62
62
  }
63
63
  @compileError(err);
64
64
  },
65
- .Slice => {
65
+ .slice => {
66
66
  if (ptr_info.child == u8) return stderr.print("{s}", .{value}) catch return;
67
67
  @compileError(err);
68
68
  },
69
69
  },
70
- .Optional => stderr.print("{?}", .{value}) catch return,
70
+ .optional => stderr.print("{?}", .{value}) catch return,
71
71
  else => stderr.print("{}", .{value}) catch return,
72
72
  }
73
73
  }
74
74
  }
75
+
76
+ const showdown = @import("./options.zig").showdown;
77
+ const Result = @import("./data.zig").Result;
78
+ const Choice = @import("./data.zig").Choice;
79
+
80
+ pub fn dump(gen: u8, battle: anytype, frame: ?struct { Result, Choice, Choice }) void {
81
+ const file = std.fs.cwd().createFile("logs/dump.bin", .{}) catch return;
82
+ defer file.close();
83
+ var w = file.writer();
84
+ w.writeByte(@intFromBool(showdown)) catch return;
85
+ w.writeByte(gen) catch return;
86
+ w.writeInt(i16, 0, .little) catch return;
87
+ w.writeInt(i32, 0, .little) catch return;
88
+ w.writeStruct(battle) catch return;
89
+ w.writeStruct(battle) catch return;
90
+ if (frame) |f| {
91
+ w.writeStruct(f[0]) catch return;
92
+ w.writeStruct(f[1]) catch return;
93
+ w.writeStruct(f[2]) catch return;
94
+ }
95
+ }
@@ -1,12 +1,8 @@
1
1
  const std = @import("std");
2
+ const c = @import("napi");
2
3
 
3
4
  const assert = std.debug.assert;
4
5
 
5
- const c = @cImport({
6
- @cDefine("NAPI_VERSION", "8");
7
- @cInclude("node_api.h");
8
- });
9
-
10
6
  pub const Array = struct {
11
7
  pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
12
8
  var result: c.napi_value = undefined;
@@ -35,7 +31,7 @@ pub const Number = struct {
35
31
  const T = @TypeOf(value);
36
32
  var result: c.napi_value = undefined;
37
33
  assert(c.napi_ok == switch (@typeInfo(T)) {
38
- .Int => |info| switch (info.bits) {
34
+ .int => |info| switch (info.bits) {
39
35
  0...32 => switch (info.signedness) {
40
36
  .signed => c.napi_create_int32(env, @as(i32, value), &result),
41
37
  .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
@@ -50,7 +46,7 @@ pub const Number = struct {
50
46
 
51
47
  pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
52
48
  switch (@typeInfo(T)) {
53
- .Int => |info| switch (info.bits) {
49
+ .int => |info| switch (info.bits) {
54
50
  0...32 => switch (info.signedness) {
55
51
  .signed => {
56
52
  var result: i32 = undefined;
@@ -3,33 +3,28 @@ const std = @import("std");
3
3
  const expect = std.testing.expect;
4
4
  const assert = std.debug.assert;
5
5
 
6
- // TODO: ziglang/zig#104
6
+ /// Optimized optional representation which stores the empty None value as a sentinel.
7
+ /// NOTE: ziglang/zig#104
7
8
  pub fn Optional(comptime T: type) type {
8
9
  const fields = std.meta.fields(switch (@typeInfo(T)) {
9
- .Bool => enum { false, true },
10
+ .bool => enum { false, true },
10
11
  else => T,
11
12
  });
12
13
 
13
- var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
14
- var decls = [_]std.builtin.Type.Declaration{};
14
+ const tagType = std.math.IntFittingRange(0, fields.len);
15
+ var fieldNames: [fields.len + 1][]const u8 = undefined;
16
+ var fieldValues: [fields.len + 1]tagType = undefined;
17
+
18
+ fieldNames[0] = "None";
19
+ fieldValues[0] = 0;
15
20
 
16
- enumFields[0] = .{ .name = "None", .value = 0 };
17
21
  inline for (fields, 1..) |field, i| {
18
22
  assert(!std.mem.eql(u8, field.name, "None"));
19
- enumFields[i] = .{
20
- .name = field.name,
21
- .value = i,
22
- };
23
+ fieldNames[i] = field.name;
24
+ fieldValues[i] = i;
23
25
  }
24
26
 
25
- return @Type(.{
26
- .Enum = .{
27
- .tag_type = std.math.IntFittingRange(0, fields.len + 1),
28
- .fields = &enumFields,
29
- .decls = &decls,
30
- .is_exhaustive = true,
31
- },
32
- });
27
+ return @Enum(tagType, .exhaustive, &fieldNames, &fieldValues);
33
28
  }
34
29
 
35
30
  test Optional {
@@ -45,7 +40,7 @@ test Optional {
45
40
  try expect(b != .true);
46
41
  try expect(b != .false);
47
42
 
48
- const Player = @import("./data.zig").Player;
43
+ const Player = @import("data.zig").Player;
49
44
  try expect(@bitSizeOf(Optional(Player)) == 2);
50
45
 
51
46
  const p: Optional(Player) = .P2;
@@ -57,4 +52,10 @@ test Optional {
57
52
  try expect(q == .None);
58
53
  try expect(q != .P1);
59
54
  try expect(q != .P2);
55
+
56
+ const Three = enum(u2) { A, B, C };
57
+ try expect(@bitSizeOf(Optional(Three)) == 2);
58
+
59
+ const Four = enum(u2) { A, B, C, D };
60
+ try expect(@bitSizeOf(Optional(Four)) == 3);
60
61
  }
@@ -7,9 +7,12 @@ pub const Options = struct {
7
7
  log: ?bool = null,
8
8
  chance: ?bool = null,
9
9
  calc: ?bool = null,
10
+ mod: ?bool = null,
10
11
  advance: ?bool = null,
11
12
  ebc: ?bool = null,
12
13
  key: ?bool = null,
14
+ miss: ?bool = null,
15
+ overwrite: ?bool = null,
13
16
  internal: ?bool = null,
14
17
  };
15
18
 
@@ -22,9 +25,12 @@ pub const chance = get("chance", false);
22
25
  /// Whether or not damage calculator support is enabled.
23
26
  pub const calc = get("calc", false);
24
27
 
28
+ pub const mod = showdown or get("mod", false);
25
29
  pub const advance = get("advance", true);
26
30
  pub const ebc = get("ebc", true);
27
31
  pub const key = get("key", false);
32
+ pub const miss = get("miss", true);
33
+ pub const overwrite = get("overwrite", true);
28
34
  pub const internal = get("internal", false);
29
35
 
30
36
  fn get(comptime name: []const u8, default: bool) bool {