@pkmn/engine 0.1.0-dev.ce4c0b2a → 0.1.0-dev.d127cb34

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 (73) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +23 -15
  3. package/build/pkg/addon/browser.d.ts +1 -1
  4. package/build/pkg/addon/browser.js +6 -2
  5. package/build/pkg/addon/browser.js.map +1 -1
  6. package/build/pkg/addon/node.js +22 -8
  7. package/build/pkg/addon/node.js.map +1 -1
  8. package/build/pkg/addon/wasm.d.ts +2 -0
  9. package/build/pkg/addon/wasm.js +53 -0
  10. package/build/pkg/addon/wasm.js.map +1 -0
  11. package/build/pkg/addon.d.ts +25 -5
  12. package/build/pkg/addon.js +8 -17
  13. package/build/pkg/addon.js.map +1 -1
  14. package/build/pkg/data/layout.json +15 -2
  15. package/build/pkg/data/protocol.json +23 -9
  16. package/build/pkg/data.js.map +1 -1
  17. package/build/pkg/gen1.d.ts +15 -18
  18. package/build/pkg/gen1.js +28 -19
  19. package/build/pkg/gen1.js.map +1 -1
  20. package/build/pkg/index.d.ts +37 -100
  21. package/build/pkg/index.js +23 -75
  22. package/build/pkg/index.js.map +1 -1
  23. package/build/pkg/protocol.d.ts +11 -2
  24. package/build/pkg/protocol.js.map +1 -1
  25. package/build/tools/debug.js +56 -13
  26. package/build/tools/debug.js.map +1 -1
  27. package/build/tools/display/debug.jsx +19 -81
  28. package/build/tools/display/debug.jsx.map +1 -1
  29. package/build/tools/display/index.js +35 -15
  30. package/build/tools/display/index.js.map +1 -1
  31. package/build/tools/display/ui.jsx +27 -16
  32. package/build/tools/display/ui.jsx.map +1 -1
  33. package/build/tools/display/util.js +141 -2
  34. package/build/tools/display/util.js.map +1 -1
  35. package/build.zig +330 -245
  36. package/package.json +36 -128
  37. package/src/bin/install-pkmn-engine +25 -9
  38. package/src/lib/bindings/c.zig +201 -0
  39. package/src/lib/bindings/node.zig +117 -0
  40. package/src/lib/bindings/wasm.zig +51 -0
  41. package/src/lib/common/array.zig +5 -12
  42. package/src/lib/common/debug.zig +45 -34
  43. package/src/lib/common/js.zig +3 -9
  44. package/src/lib/common/optional.zig +26 -26
  45. package/src/lib/common/options.zig +2 -0
  46. package/src/lib/common/protocol.zig +228 -197
  47. package/src/lib/common/rational.zig +278 -285
  48. package/src/lib/common/rng.zig +1 -3
  49. package/src/lib/common/util.zig +34 -31
  50. package/src/lib/gen1/calc.zig +220 -195
  51. package/src/lib/gen1/chance.zig +184 -108
  52. package/src/lib/gen1/data/moves.zig +2 -4
  53. package/src/lib/gen1/data/species.zig +1 -3
  54. package/src/lib/gen1/data/types.zig +3 -4
  55. package/src/lib/gen1/data.zig +12 -16
  56. package/src/lib/gen1/helpers.zig +35 -40
  57. package/src/lib/gen1/mechanics.zig +147 -127
  58. package/src/lib/gen2/calc.zig +12 -23
  59. package/src/lib/gen2/chance.zig +38 -26
  60. package/src/lib/gen2/data/items.zig +1 -3
  61. package/src/lib/gen2/data/moves.zig +2 -4
  62. package/src/lib/gen2/data/species.zig +1 -3
  63. package/src/lib/gen2/data/types.zig +7 -5
  64. package/src/lib/gen2/data.zig +19 -22
  65. package/src/lib/gen2/generated.zig +18 -23
  66. package/src/lib/gen2/helpers.zig +32 -31
  67. package/src/lib/gen2/mechanics.zig +58 -55
  68. package/src/lib/napi.h +2 -0
  69. package/src/lib/node.zig +4 -121
  70. package/src/lib/pkmn.zig +98 -19
  71. package/src/lib/wasm.zig +9 -9
  72. package/src/tools/display/dom.ts +43 -31
  73. package/src/tools/display/ui.css +12 -9
@@ -3,19 +3,12 @@ const std = @import("std");
3
3
  const expectEqual = std.testing.expectEqual;
4
4
  const assert = std.debug.assert;
5
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
6
  /// Helpers for working with bit-packed arrays of non-powers-of-2 types.
10
7
  /// NOTE: ziglang/zig#12547
11
8
  pub fn Array(comptime n: comptime_int, comptime U: type) type {
12
9
  return struct {
13
10
  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 });
11
+ pub const T = @Int(.unsigned, size * n);
19
12
 
20
13
  /// Returns the value stored at index `i` in the array `a`.
21
14
  pub fn get(a: T, i: usize) U {
@@ -24,8 +17,8 @@ pub fn Array(comptime n: comptime_int, comptime U: type) type {
24
17
  const mask: T = (1 << size) - 1;
25
18
  const result = (a >> @intCast(shift)) & mask;
26
19
  return switch (@typeInfo(U)) {
27
- Enum => @enumFromInt(result),
28
- Int => @intCast(result),
20
+ .@"enum" => @enumFromInt(result),
21
+ .int => @intCast(result),
29
22
  else => unreachable,
30
23
  };
31
24
  }
@@ -34,8 +27,8 @@ pub fn Array(comptime n: comptime_int, comptime U: type) type {
34
27
  pub fn set(a: T, i: usize, val: U) T {
35
28
  assert(i < n);
36
29
  const v: T = switch (@typeInfo(U)) {
37
- Enum => @intFromEnum(val),
38
- Int => @intCast(val),
30
+ .@"enum" => @intFromEnum(val),
31
+ .int => @intCast(val),
39
32
  else => unreachable,
40
33
  };
41
34
  const shift = i * size;
@@ -1,22 +1,20 @@
1
+ const builtin = @import("builtin");
1
2
  const std = @import("std");
2
3
 
3
4
  const debug = std.debug;
4
- const io = std.io;
5
-
6
- const Array = if (@hasField(std.builtin.Type, "array")) .array else .Array;
7
- const Optional = if (@hasField(std.builtin.Type, "optional")) .optional else .Optional;
8
- const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
9
- const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
10
5
 
11
6
  pub fn print(value: anytype) void {
12
- debug.lockStdErr();
13
- defer debug.unlockStdErr();
14
- const stderr = io.getStdErr().writer();
7
+ if (builtin.target.os.tag == .freestanding) return;
8
+
9
+ var buffer: [4096]u8 = undefined;
10
+ const stderr = debug.lockStderr(&buffer);
11
+ defer debug.unlockStderr();
12
+ var fw = stderr.file_writer;
15
13
 
16
14
  nosuspend {
17
- stderr.writeAll("\x1b[41m") catch return;
15
+ fw.interface.writeAll("\x1b[41m") catch return;
18
16
  if (@TypeOf(@src()) == @TypeOf(value)) {
19
- stderr.print("{s} ({s}:{d}:{d})", .{
17
+ fw.interface.print("{s} ({s}:{d}:{d})", .{
20
18
  value.fn_name,
21
19
  value.file,
22
20
  value.line,
@@ -24,56 +22,69 @@ pub fn print(value: anytype) void {
24
22
  }) catch return;
25
23
  } else {
26
24
  switch (@typeInfo(@TypeOf(value))) {
27
- Struct => |info| {
25
+ .@"struct" => |info| {
28
26
  if (info.is_tuple) {
29
- inline for (info.fields, 0..) |f, i| {
30
- inspect(@field(value, f.name));
31
- if (i < info.fields.len - 1) stderr.writeAll(" ") catch return;
27
+ if (@hasField(@TypeOf(info), "fields")) {
28
+ inline for (info.fields, 0..) |f, i| {
29
+ inspect(fw, @field(value, f.name));
30
+ if (i < info.fields.len - 1) {
31
+ fw.interface.writeAll(" ") catch return;
32
+ }
33
+ }
34
+ } else {
35
+ inline for (info.field_names, 0..) |name, i| {
36
+ inspect(fw, @field(value, name));
37
+ if (i < info.field_names.len - 1) {
38
+ fw.interface.writeAll(" ") catch return;
39
+ }
40
+ }
32
41
  }
33
42
  } else {
34
- inspect(value);
43
+ inspect(fw, value);
35
44
  }
36
45
  },
37
- else => inspect(value),
46
+ else => inspect(fw, value),
38
47
  }
39
48
  }
40
- stderr.writeAll("\x1b[K\x1b[0m\n") catch return;
49
+ fw.interface.writeAll("\x1b[K\x1b[0m\n") catch return;
41
50
  }
42
51
  }
43
52
 
44
- fn inspect(value: anytype) void {
45
- const stderr = io.getStdErr().writer();
46
-
53
+ fn inspect(w: anytype, value: anytype) void {
47
54
  nosuspend {
48
55
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
49
56
  switch (@typeInfo(@TypeOf(value))) {
50
- Array => |info| {
51
- if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
57
+ .array => |info| {
58
+ if (info.child == u8) return w.interface.print("{s}", .{value}) catch return;
52
59
  @compileError(err);
53
60
  },
54
- Pointer => |ptr_info| switch (ptr_info.size) {
55
- .One => switch (@typeInfo(ptr_info.child)) {
56
- .Array => |info| {
57
- if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
61
+ .pointer => |ptr_info| switch (ptr_info.size) {
62
+ .one => switch (@typeInfo(ptr_info.child)) {
63
+ .array => |info| {
64
+ if (info.child == u8) {
65
+ return w.interface.print("{s}", .{value}) catch return;
66
+ }
58
67
  @compileError(err);
59
68
  },
60
- .Enum, .Union, .Struct => return inspect(value.*),
69
+ .@"enum", .@"union", .@"struct" => return inspect(value.*),
61
70
  else => @compileError(err),
62
71
  },
63
- .Many, .C => {
72
+ .many, .c => {
64
73
  if (ptr_info.sentinel) |_| return inspect(std.mem.span(value));
65
74
  if (ptr_info.child == u8) {
66
- return stderr.print("{s}", .{std.mem.span(value)}) catch return;
75
+ return w.interface.print("{s}", .{std.mem.span(value)}) catch return;
67
76
  }
68
77
  @compileError(err);
69
78
  },
70
- .Slice => {
71
- if (ptr_info.child == u8) return stderr.print("{s}", .{value}) catch return;
79
+ .slice => {
80
+ if (ptr_info.child == u8) {
81
+ return w.interface.print("{s}", .{value}) catch return;
82
+ }
72
83
  @compileError(err);
73
84
  },
74
85
  },
75
- Optional => stderr.print("{?}", .{value}) catch return,
76
- else => stderr.print("{}", .{value}) catch return,
86
+ .optional => w.interface.print("{?}", .{value}) catch return,
87
+ else => w.interface.print("{}", .{value}) catch return,
77
88
  }
78
89
  }
79
90
  }
@@ -1,14 +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 Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
6
-
7
- const c = @cImport({
8
- @cDefine("NAPI_VERSION", "8");
9
- @cInclude("node_api.h");
10
- });
11
-
12
6
  pub const Array = struct {
13
7
  pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
14
8
  var result: c.napi_value = undefined;
@@ -37,7 +31,7 @@ pub const Number = struct {
37
31
  const T = @TypeOf(value);
38
32
  var result: c.napi_value = undefined;
39
33
  assert(c.napi_ok == switch (@typeInfo(T)) {
40
- Int => |info| switch (info.bits) {
34
+ .int => |info| switch (info.bits) {
41
35
  0...32 => switch (info.signedness) {
42
36
  .signed => c.napi_create_int32(env, @as(i32, value), &result),
43
37
  .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
@@ -52,7 +46,7 @@ pub const Number = struct {
52
46
 
53
47
  pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
54
48
  switch (@typeInfo(T)) {
55
- Int => |info| switch (info.bits) {
49
+ .int => |info| switch (info.bits) {
56
50
  0...32 => switch (info.signedness) {
57
51
  .signed => {
58
52
  var result: i32 = undefined;
@@ -3,38 +3,38 @@ 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
-
8
- // TODO: ziglang/zig#104
6
+ /// Optimized optional representation which stores the empty None value as a sentinel.
7
+ /// NOTE: ziglang/zig#104
9
8
  pub fn Optional(comptime T: type) type {
10
- const fields = std.meta.fields(switch (@typeInfo(T)) {
11
- Bool => enum { false, true },
12
- else => T,
13
- });
9
+ const names = if (@hasDecl(std.meta, "fieldNames"))
10
+ std.meta.fieldNames(switch (@typeInfo(T)) {
11
+ .bool => enum { false, true },
12
+ else => T,
13
+ })
14
+ else blk: {
15
+ const fields = std.meta.fields(switch (@typeInfo(T)) {
16
+ .bool => enum { false, true },
17
+ else => T,
18
+ });
19
+ var ns: [fields.len][:0]const u8 = undefined;
20
+ inline for (fields, 0..) |f, i| ns[i] = f.name;
21
+ break :blk &ns;
22
+ };
14
23
 
15
- var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
16
- var decls = [_]std.builtin.Type.Declaration{};
24
+ const TagType = std.math.IntFittingRange(0, names.len);
25
+ var fieldNames: [names.len + 1][]const u8 = undefined;
26
+ var fieldValues: [names.len + 1]TagType = undefined;
17
27
 
18
- enumFields[0] = .{ .name = "None", .value = 0 };
28
+ fieldNames[0] = "None";
29
+ fieldValues[0] = 0;
19
30
 
20
- inline for (fields, 1..) |field, i| {
21
- assert(!std.mem.eql(u8, field.name, "None"));
22
- enumFields[i] = .{
23
- .name = field.name,
24
- .value = i,
25
- };
31
+ inline for (names, 1..) |name, i| {
32
+ assert(!std.mem.eql(u8, name, "None"));
33
+ fieldNames[i] = name;
34
+ fieldValues[i] = i;
26
35
  }
27
36
 
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 });
37
+ return @Enum(TagType, .exhaustive, &fieldNames, &fieldValues);
38
38
  }
39
39
 
40
40
  test Optional {
@@ -7,6 +7,7 @@ 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,
@@ -24,6 +25,7 @@ pub const chance = get("chance", false);
24
25
  /// Whether or not damage calculator support is enabled.
25
26
  pub const calc = get("calc", false);
26
27
 
28
+ pub const mod = showdown or get("mod", false);
27
29
  pub const advance = get("advance", true);
28
30
  pub const ebc = get("ebc", true);
29
31
  pub const key = get("key", false);