@pkmn/engine 0.1.0-dev.f918c3bd → 0.1.0-dev.fb6653d6

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 (83) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +51 -23
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +44 -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 +97 -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 +25 -3
  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 +23 -9
  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 +13 -17
  21. package/build/pkg/gen1.js +8 -12
  22. package/build/pkg/gen1.js.map +1 -1
  23. package/build/pkg/index.d.ts +40 -91
  24. package/build/pkg/index.js +6 -63
  25. package/build/pkg/index.js.map +1 -1
  26. package/build/pkg/protocol.d.ts +20 -1
  27. package/build/pkg/protocol.js +109 -107
  28. package/build/pkg/protocol.js.map +1 -1
  29. package/build/tools/debug.js +36 -69
  30. package/build/tools/debug.js.map +1 -1
  31. package/build/tools/display/debug.jsx +141 -0
  32. package/build/tools/display/debug.jsx.map +1 -0
  33. package/build/tools/display/index.js +142 -0
  34. package/build/tools/display/index.js.map +1 -0
  35. package/build/tools/display/ui.jsx +349 -0
  36. package/build/tools/display/ui.jsx.map +1 -0
  37. package/build/tools/display/util.js +18 -0
  38. package/build/tools/display/util.js.map +1 -0
  39. package/build.zig +303 -101
  40. package/package.json +61 -132
  41. package/src/bin/install-pkmn-engine +30 -20
  42. package/src/bin/pkmn-debug +1 -1
  43. package/src/lib/{binding → bindings}/node.zig +8 -21
  44. package/src/lib/bindings/wasm.zig +58 -0
  45. package/src/lib/common/array.zig +77 -0
  46. package/src/lib/common/data.zig +8 -8
  47. package/src/lib/common/debug.zig +39 -8
  48. package/src/lib/common/js.zig +4 -2
  49. package/src/lib/common/optional.zig +21 -10
  50. package/src/lib/common/options.zig +6 -0
  51. package/src/lib/common/protocol.zig +421 -393
  52. package/src/lib/common/rational.zig +131 -35
  53. package/src/lib/common/rng.zig +16 -9
  54. package/src/lib/common/util.zig +50 -1
  55. package/src/lib/gen1/calc.zig +687 -160
  56. package/src/lib/gen1/chance.zig +434 -307
  57. package/src/lib/gen1/data/moves.zig +26 -14
  58. package/src/lib/gen1/data/species.zig +4 -6
  59. package/src/lib/gen1/data/types.zig +7 -8
  60. package/src/lib/gen1/data.zig +44 -47
  61. package/src/lib/gen1/helpers.zig +91 -562
  62. package/src/lib/gen1/mechanics.zig +641 -557
  63. package/src/lib/gen2/calc.zig +153 -0
  64. package/src/lib/gen2/chance.zig +902 -0
  65. package/src/lib/gen2/data/items.zig +13 -21
  66. package/src/lib/gen2/data/moves.zig +67 -47
  67. package/src/lib/gen2/data/species.zig +2 -4
  68. package/src/lib/gen2/data/types.zig +134 -32
  69. package/src/lib/gen2/data.zig +56 -54
  70. package/src/lib/gen2/generated.zig +801 -0
  71. package/src/lib/gen2/helpers.zig +46 -25
  72. package/src/lib/gen2/mechanics.zig +2778 -362
  73. package/src/lib/node.zig +18 -0
  74. package/src/lib/pkmn.zig +37 -15
  75. package/src/lib/wasm.zig +17 -0
  76. package/src/tools/display/dom.ts +96 -0
  77. package/src/tools/display/ui.css +354 -0
  78. package/build/test/display.js +0 -400
  79. package/build/test/display.js.map +0 -1
  80. package/src/lib/binding/wasm.zig +0 -12
  81. package/src/lib/gen1/README.md +0 -894
  82. package/src/lib/gen2/README.md +0 -328
  83. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -3,9 +3,19 @@ const std = @import("std");
3
3
  const debug = std.debug;
4
4
  const io = std.io;
5
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
+
6
13
  pub fn print(value: anytype) void {
7
- debug.getStderrMutex().lock();
8
- defer debug.getStderrMutex().unlock();
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();
9
19
  const stderr = io.getStdErr().writer();
10
20
 
11
21
  nosuspend {
@@ -19,7 +29,7 @@ pub fn print(value: anytype) void {
19
29
  }) catch return;
20
30
  } else {
21
31
  switch (@typeInfo(@TypeOf(value))) {
22
- .Struct => |info| {
32
+ Struct => |info| {
23
33
  if (info.is_tuple) {
24
34
  inline for (info.fields, 0..) |f, i| {
25
35
  inspect(@field(value, f.name));
@@ -42,17 +52,17 @@ fn inspect(value: anytype) void {
42
52
  nosuspend {
43
53
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
44
54
  switch (@typeInfo(@TypeOf(value))) {
45
- .Array => |info| {
55
+ Array => |info| {
46
56
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
47
57
  @compileError(err);
48
58
  },
49
- .Pointer => |ptr_info| switch (ptr_info.size) {
59
+ Pointer => |ptr_info| switch (ptr_info.size) {
50
60
  .One => switch (@typeInfo(ptr_info.child)) {
51
- .Array => |info| {
61
+ Array => |info| {
52
62
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
53
63
  @compileError(err);
54
64
  },
55
- .Enum, .Union, .Struct => return inspect(value.*),
65
+ Enum, Union, Struct => return inspect(value.*),
56
66
  else => @compileError(err),
57
67
  },
58
68
  .Many, .C => {
@@ -67,8 +77,29 @@ fn inspect(value: anytype) void {
67
77
  @compileError(err);
68
78
  },
69
79
  },
70
- .Optional => stderr.print("{?}", .{value}) catch return,
80
+ Optional => stderr.print("{?}", .{value}) catch return,
71
81
  else => stderr.print("{}", .{value}) catch return,
72
82
  }
73
83
  }
74
84
  }
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
+ }
@@ -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
 
@@ -14,6 +16,7 @@ pub fn Optional(comptime T: type) type {
14
16
  var decls = [_]std.builtin.Type.Declaration{};
15
17
 
16
18
  enumFields[0] = .{ .name = "None", .value = 0 };
19
+
17
20
  inline for (fields, 1..) |field, i| {
18
21
  assert(!std.mem.eql(u8, field.name, "None"));
19
22
  enumFields[i] = .{
@@ -22,14 +25,16 @@ pub fn Optional(comptime T: type) type {
22
25
  };
23
26
  }
24
27
 
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
- });
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 });
33
38
  }
34
39
 
35
40
  test Optional {
@@ -45,7 +50,7 @@ test Optional {
45
50
  try expect(b != .true);
46
51
  try expect(b != .false);
47
52
 
48
- const Player = @import("./data.zig").Player;
53
+ const Player = @import("data.zig").Player;
49
54
  try expect(@bitSizeOf(Optional(Player)) == 2);
50
55
 
51
56
  const p: Optional(Player) = .P2;
@@ -57,4 +62,10 @@ test Optional {
57
62
  try expect(q == .None);
58
63
  try expect(q != .P1);
59
64
  try expect(q != .P2);
65
+
66
+ const Three = enum(u2) { A, B, C };
67
+ try expect(@bitSizeOf(Optional(Three)) == 2);
68
+
69
+ const Four = enum(u2) { A, B, C, D };
70
+ try expect(@bitSizeOf(Optional(Four)) == 3);
60
71
  }
@@ -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 {