@pkmn/engine 0.1.0-dev.a324c50f → 0.1.0-dev.a5846b2d

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 (84) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +92 -56
  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 +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 +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 +123 -102
  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 +345 -126
  40. package/package.json +68 -121
  41. package/src/bin/install-pkmn-engine +32 -22
  42. package/src/bin/pkmn-debug +1 -1
  43. package/src/lib/{binding → bindings}/node.zig +9 -22
  44. package/src/lib/bindings/wasm.zig +59 -0
  45. package/src/lib/common/array.zig +77 -0
  46. package/src/lib/common/battle.zig +17 -0
  47. package/src/lib/common/data.zig +12 -24
  48. package/src/lib/common/debug.zig +39 -8
  49. package/src/lib/common/js.zig +120 -0
  50. package/src/lib/common/optional.zig +71 -0
  51. package/src/lib/common/options.zig +6 -0
  52. package/src/lib/common/protocol.zig +811 -332
  53. package/src/lib/common/rational.zig +449 -0
  54. package/src/lib/common/rng.zig +22 -15
  55. package/src/lib/common/util.zig +61 -0
  56. package/src/lib/gen1/calc.zig +953 -0
  57. package/src/lib/gen1/chance.zig +1185 -0
  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 +8 -9
  61. package/src/lib/gen1/data.zig +67 -77
  62. package/src/lib/gen1/helpers.zig +91 -409
  63. package/src/lib/gen1/mechanics.zig +675 -613
  64. package/src/lib/gen2/calc.zig +153 -0
  65. package/src/lib/gen2/chance.zig +902 -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 +135 -33
  70. package/src/lib/gen2/data.zig +115 -91
  71. package/src/lib/gen2/generated.zig +801 -0
  72. package/src/lib/gen2/helpers.zig +54 -37
  73. package/src/lib/gen2/mechanics.zig +3580 -29
  74. package/src/lib/node.zig +18 -0
  75. package/src/lib/pkmn.zig +37 -15
  76. package/src/lib/wasm.zig +17 -0
  77. package/src/tools/display/dom.ts +96 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -400
  80. package/build/test/display.js.map +0 -1
  81. package/src/lib/binding/wasm.zig +0 -12
  82. package/src/lib/gen1/README.md +0 -814
  83. package/src/lib/gen2/README.md +0 -309
  84. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -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
  }
@@ -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
+ }
@@ -0,0 +1,120 @@
1
+ const std = @import("std");
2
+
3
+ const assert = std.debug.assert;
4
+
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
+ pub const Array = struct {
13
+ pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
14
+ var result: c.napi_value = undefined;
15
+ assert(c.napi_ok == if (o.length) |n|
16
+ c.napi_create_array_with_length(env, n, &result)
17
+ else
18
+ c.napi_create_array(env, &result));
19
+ return result;
20
+ }
21
+
22
+ pub fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
23
+ assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
24
+ }
25
+ };
26
+
27
+ pub const Boolean = struct {
28
+ pub fn init(env: c.napi_env, value: bool) c.napi_value {
29
+ var result: c.napi_value = undefined;
30
+ assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
31
+ return result;
32
+ }
33
+ };
34
+
35
+ pub const Number = struct {
36
+ pub fn init(env: c.napi_env, value: anytype) c.napi_value {
37
+ const T = @TypeOf(value);
38
+ var result: c.napi_value = undefined;
39
+ assert(c.napi_ok == switch (@typeInfo(T)) {
40
+ Int => |info| switch (info.bits) {
41
+ 0...32 => switch (info.signedness) {
42
+ .signed => c.napi_create_int32(env, @as(i32, value), &result),
43
+ .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
44
+ },
45
+ 33...52 => c.napi_create_int64(env, @as(i64, value), &result),
46
+ else => @compileError("int can't be represented as JS number"),
47
+ },
48
+ else => @compileError("expected number, got: " ++ @typeName(T)),
49
+ });
50
+ return result;
51
+ }
52
+
53
+ pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
54
+ switch (@typeInfo(T)) {
55
+ Int => |info| switch (info.bits) {
56
+ 0...32 => switch (info.signedness) {
57
+ .signed => {
58
+ var result: i32 = undefined;
59
+ assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
60
+ return if (info.bits == 32) result else @intCast(result);
61
+ },
62
+ .unsigned => {
63
+ var result: u32 = undefined;
64
+ assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
65
+ return if (info.bits == 32) result else @intCast(result);
66
+ },
67
+ },
68
+ 33...63 => {
69
+ var result: i64 = undefined;
70
+ assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
71
+ return @intCast(result);
72
+ },
73
+ else => {
74
+ var result: i64 = undefined;
75
+ assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
76
+ return switch (info.signedness) {
77
+ .signed => value,
78
+ .unsigned => if (0 <= value) @intCast(value) else unreachable,
79
+ };
80
+ },
81
+ },
82
+ else => @compileError("expected number, got: " ++ @typeName(T)),
83
+ }
84
+ }
85
+ };
86
+
87
+ pub const Object = struct {
88
+ pub fn init(env: c.napi_env) c.napi_value {
89
+ var result: c.napi_value = undefined;
90
+ assert(c.napi_create_object(env, &result) == c.napi_ok);
91
+ return result;
92
+ }
93
+ };
94
+
95
+ pub const Property = union(enum) {
96
+ method: c.napi_callback,
97
+ value: c.napi_value,
98
+
99
+ pub fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
100
+ return .{
101
+ .utf8name = name,
102
+ .name = null,
103
+ .method = switch (property) {
104
+ .method => |m| m,
105
+ .value => null,
106
+ },
107
+ .getter = null,
108
+ .setter = null,
109
+ .value = switch (property) {
110
+ .method => null,
111
+ .value => |v| v,
112
+ },
113
+ .attributes = switch (property) {
114
+ .method => c.napi_default,
115
+ .value => c.napi_enumerable,
116
+ },
117
+ .data = null,
118
+ };
119
+ }
120
+ };
@@ -0,0 +1,71 @@
1
+ const std = @import("std");
2
+
3
+ const expect = std.testing.expect;
4
+ const assert = std.debug.assert;
5
+
6
+ const Bool = if (@hasField(std.builtin.Type, "bool")) .bool else .Bool;
7
+
8
+ // TODO: ziglang/zig#104
9
+ 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
+ });
14
+
15
+ var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
16
+ var decls = [_]std.builtin.Type.Declaration{};
17
+
18
+ enumFields[0] = .{ .name = "None", .value = 0 };
19
+
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
+ };
26
+ }
27
+
28
+ const options: std.builtin.Type.Enum = .{
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 });
38
+ }
39
+
40
+ test Optional {
41
+ try expect(@bitSizeOf(Optional(bool)) == 2);
42
+
43
+ const a: Optional(bool) = .true;
44
+ try expect(a != .None);
45
+ try expect(a == .true);
46
+ try expect(a != .false);
47
+
48
+ const b: Optional(bool) = .None;
49
+ try expect(b == .None);
50
+ try expect(b != .true);
51
+ try expect(b != .false);
52
+
53
+ const Player = @import("data.zig").Player;
54
+ try expect(@bitSizeOf(Optional(Player)) == 2);
55
+
56
+ const p: Optional(Player) = .P2;
57
+ try expect(p != .None);
58
+ try expect(p != .P1);
59
+ try expect(p == .P2);
60
+
61
+ const q: Optional(Player) = .None;
62
+ try expect(q == .None);
63
+ try expect(q != .P1);
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);
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 {