@pkmn/engine 0.1.0-dev.a0447192 → 0.1.0-dev.a15b6c17

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 +22 -12
  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 +55 -12
  26. package/build/tools/debug.js.map +1 -1
  27. package/build/tools/display/debug.jsx +19 -94
  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 +20 -9
  32. package/build/tools/display/ui.jsx.map +1 -1
  33. package/build/tools/display/util.js +140 -1
  34. package/build/tools/display/util.js.map +1 -1
  35. package/build.zig +239 -217
  36. package/package.json +34 -130
  37. package/src/bin/install-pkmn-engine +25 -9
  38. package/src/lib/bindings/c.zig +200 -0
  39. package/src/lib/bindings/node.zig +117 -0
  40. package/src/lib/bindings/wasm.zig +50 -0
  41. package/src/lib/common/array.zig +5 -12
  42. package/src/lib/common/debug.zig +33 -59
  43. package/src/lib/common/js.zig +3 -9
  44. package/src/lib/common/optional.zig +11 -21
  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 +275 -284
  48. package/src/lib/common/rng.zig +1 -3
  49. package/src/lib/common/util.zig +14 -31
  50. package/src/lib/gen1/calc.zig +159 -163
  51. package/src/lib/gen1/chance.zig +146 -93
  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 +12 -20
  57. package/src/lib/gen1/mechanics.zig +103 -78
  58. package/src/lib/gen2/calc.zig +12 -23
  59. package/src/lib/gen2/chance.zig +12 -18
  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 +9 -18
  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 +0 -5
@@ -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,27 +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 Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
8
- const Optional = if (@hasField(std.builtin.Type, "optional")) .optional else .Optional;
9
- const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
10
- const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
11
- const Union = if (@hasField(std.builtin.Type, "union")) .@"union" else .Union;
12
5
 
13
6
  pub fn print(value: anytype) void {
14
- if (@hasDecl(debug, "lockStdErr")) debug.lockStdErr() else debug.getStderrMutex().lock();
15
- defer if (@hasDecl(debug, "unlockStdErr"))
16
- debug.unlockStdErr()
17
- else
18
- debug.getStderrMutex().unlock();
19
- 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;
20
13
 
21
14
  nosuspend {
22
- stderr.writeAll("\x1b[41m") catch return;
15
+ fw.interface.writeAll("\x1b[41m") catch return;
23
16
  if (@TypeOf(@src()) == @TypeOf(value)) {
24
- stderr.print("{s} ({s}:{d}:{d})", .{
17
+ fw.interface.print("{s} ({s}:{d}:{d})", .{
25
18
  value.fn_name,
26
19
  value.file,
27
20
  value.line,
@@ -29,77 +22,58 @@ pub fn print(value: anytype) void {
29
22
  }) catch return;
30
23
  } else {
31
24
  switch (@typeInfo(@TypeOf(value))) {
32
- Struct => |info| {
25
+ .@"struct" => |info| {
33
26
  if (info.is_tuple) {
34
27
  inline for (info.fields, 0..) |f, i| {
35
- inspect(@field(value, f.name));
36
- if (i < info.fields.len - 1) stderr.writeAll(" ") catch return;
28
+ inspect(fw, @field(value, f.name));
29
+ if (i < info.fields.len - 1) fw.interface.writeAll(" ") catch return;
37
30
  }
38
31
  } else {
39
- inspect(value);
32
+ inspect(fw, value);
40
33
  }
41
34
  },
42
- else => inspect(value),
35
+ else => inspect(fw, value),
43
36
  }
44
37
  }
45
- stderr.writeAll("\x1b[K\x1b[0m\n") catch return;
38
+ fw.interface.writeAll("\x1b[K\x1b[0m\n") catch return;
46
39
  }
47
40
  }
48
41
 
49
- fn inspect(value: anytype) void {
50
- const stderr = io.getStdErr().writer();
51
-
42
+ fn inspect(w: anytype, value: anytype) void {
52
43
  nosuspend {
53
44
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
54
45
  switch (@typeInfo(@TypeOf(value))) {
55
- Array => |info| {
56
- if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
46
+ .array => |info| {
47
+ if (info.child == u8) return w.interface.print("{s}", .{value}) catch return;
57
48
  @compileError(err);
58
49
  },
59
- Pointer => |ptr_info| switch (ptr_info.size) {
60
- .One => switch (@typeInfo(ptr_info.child)) {
61
- Array => |info| {
62
- if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
50
+ .pointer => |ptr_info| switch (ptr_info.size) {
51
+ .one => switch (@typeInfo(ptr_info.child)) {
52
+ .array => |info| {
53
+ if (info.child == u8) {
54
+ return w.interface.print("{s}", .{value}) catch return;
55
+ }
63
56
  @compileError(err);
64
57
  },
65
- Enum, Union, Struct => return inspect(value.*),
58
+ .@"enum", .@"union", .@"struct" => return inspect(value.*),
66
59
  else => @compileError(err),
67
60
  },
68
- .Many, .C => {
61
+ .many, .c => {
69
62
  if (ptr_info.sentinel) |_| return inspect(std.mem.span(value));
70
63
  if (ptr_info.child == u8) {
71
- return stderr.print("{s}", .{std.mem.span(value)}) catch return;
64
+ return w.interface.print("{s}", .{std.mem.span(value)}) catch return;
72
65
  }
73
66
  @compileError(err);
74
67
  },
75
- .Slice => {
76
- if (ptr_info.child == u8) return stderr.print("{s}", .{value}) catch return;
68
+ .slice => {
69
+ if (ptr_info.child == u8) {
70
+ return w.interface.print("{s}", .{value}) catch return;
71
+ }
77
72
  @compileError(err);
78
73
  },
79
74
  },
80
- Optional => stderr.print("{?}", .{value}) catch return,
81
- else => stderr.print("{}", .{value}) catch return,
75
+ .optional => w.interface.print("{?}", .{value}) catch return,
76
+ else => w.interface.print("{}", .{value}) catch return,
82
77
  }
83
78
  }
84
79
  }
85
-
86
- const showdown = @import("./options.zig").showdown;
87
- const Result = @import("./data.zig").Result;
88
- const Choice = @import("./data.zig").Choice;
89
-
90
- pub fn dump(gen: u8, battle: anytype, frame: ?struct { Result, Choice, Choice }) void {
91
- const file = std.fs.cwd().createFile("logs/dump.bin", .{}) catch return;
92
- defer file.close();
93
- var w = file.writer();
94
- w.writeByte(@intFromBool(showdown)) catch return;
95
- w.writeByte(gen) catch return;
96
- w.writeInt(i16, 0, .little) catch return;
97
- w.writeInt(i32, 0, .little) catch return;
98
- w.writeStruct(battle) catch return;
99
- w.writeStruct(battle) catch return;
100
- if (frame) |f| {
101
- w.writeStruct(f[0]) catch return;
102
- w.writeStruct(f[1]) catch return;
103
- w.writeStruct(f[2]) catch return;
104
- }
105
- }
@@ -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,28 @@ 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
9
  const fields = std.meta.fields(switch (@typeInfo(T)) {
11
- Bool => enum { false, true },
10
+ .bool => enum { false, true },
12
11
  else => T,
13
12
  });
14
13
 
15
- var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
16
- 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
17
 
18
- enumFields[0] = .{ .name = "None", .value = 0 };
18
+ fieldNames[0] = "None";
19
+ fieldValues[0] = 0;
19
20
 
20
21
  inline for (fields, 1..) |field, i| {
21
22
  assert(!std.mem.eql(u8, field.name, "None"));
22
- enumFields[i] = .{
23
- .name = field.name,
24
- .value = i,
25
- };
23
+ fieldNames[i] = field.name;
24
+ fieldValues[i] = i;
26
25
  }
27
26
 
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 });
27
+ return @Enum(TagType, .exhaustive, &fieldNames, &fieldValues);
38
28
  }
39
29
 
40
30
  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);