@pkmn/engine 0.1.0-dev.77410aff → 0.1.0-dev.78dc891c

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 +40 -28
  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/layout.json +15 -2
  16. package/build/pkg/data/protocol.json +23 -9
  17. package/build/pkg/data.d.ts +27 -6
  18. package/build/pkg/data.js.map +1 -1
  19. package/build/pkg/gen1.d.ts +15 -18
  20. package/build/pkg/gen1.js +28 -19
  21. package/build/pkg/gen1.js.map +1 -1
  22. package/build/pkg/index.d.ts +40 -90
  23. package/build/pkg/index.js +23 -70
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +29 -1
  26. package/build/pkg/protocol.js +109 -107
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +84 -75
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +66 -0
  31. package/build/tools/display/debug.jsx.map +1 -0
  32. package/build/tools/display/index.js +152 -0
  33. package/build/tools/display/index.js.map +1 -0
  34. package/build/tools/display/ui.jsx +360 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +157 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +366 -148
  39. package/package.json +61 -133
  40. package/src/bin/install-pkmn-engine +47 -21
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +201 -0
  43. package/src/lib/{binding → bindings}/node.zig +9 -22
  44. package/src/lib/bindings/wasm.zig +51 -0
  45. package/src/lib/common/array.zig +70 -0
  46. package/src/lib/common/data.zig +4 -4
  47. package/src/lib/common/debug.zig +45 -29
  48. package/src/lib/common/js.zig +3 -7
  49. package/src/lib/common/optional.zig +27 -23
  50. package/src/lib/common/options.zig +6 -0
  51. package/src/lib/common/protocol.zig +205 -174
  52. package/src/lib/common/rational.zig +338 -255
  53. package/src/lib/common/rng.zig +7 -9
  54. package/src/lib/common/util.zig +53 -1
  55. package/src/lib/gen1/calc.zig +721 -172
  56. package/src/lib/gen1/chance.zig +486 -300
  57. package/src/lib/gen1/data/moves.zig +2 -4
  58. package/src/lib/gen1/data/species.zig +2 -4
  59. package/src/lib/gen1/data/types.zig +3 -4
  60. package/src/lib/gen1/data.zig +34 -34
  61. package/src/lib/gen1/helpers.zig +116 -561
  62. package/src/lib/gen1/mechanics.zig +402 -269
  63. package/src/lib/gen2/calc.zig +35 -28
  64. package/src/lib/gen2/chance.zig +111 -511
  65. package/src/lib/gen2/data/items.zig +1 -3
  66. package/src/lib/gen2/data/moves.zig +2 -4
  67. package/src/lib/gen2/data/species.zig +1 -3
  68. package/src/lib/gen2/data/types.zig +7 -5
  69. package/src/lib/gen2/data.zig +31 -30
  70. package/src/lib/gen2/generated.zig +18 -23
  71. package/src/lib/gen2/helpers.zig +68 -39
  72. package/src/lib/gen2/mechanics.zig +215 -157
  73. package/src/lib/napi.h +2 -0
  74. package/src/lib/node.zig +14 -0
  75. package/src/lib/pkmn.zig +125 -29
  76. package/src/lib/wasm.zig +13 -0
  77. package/src/tools/display/dom.ts +108 -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 -894
  83. package/src/lib/gen2/README.md +0 -329
  84. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -0,0 +1,51 @@
1
+ const pkmn = @import("../pkmn.zig");
2
+ const std = @import("std");
3
+ const util = @import("../common/util.zig");
4
+
5
+ const assert = std.debug.assert;
6
+
7
+ pub const options = pkmn.options;
8
+
9
+ pub fn gen(comptime num: comptime_int) type {
10
+ const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
11
+ return struct {
12
+ pub const CHOICES_SIZE = size(g.CHOICES_SIZE);
13
+ pub const LOGS_SIZE = size(g.LOGS_SIZE);
14
+
15
+ pub fn update(
16
+ battle: *g.Battle(g.PRNG),
17
+ c1: pkmn.Choice,
18
+ c2: pkmn.Choice,
19
+ options_: ?[*]u8,
20
+ ) callconv(.c) pkmn.Result {
21
+ return (if (options_) |o| result: {
22
+ const buf = @as([*]u8, @ptrCast(o))[0..g.LOGS_SIZE];
23
+ var writer: pkmn.protocol.Writer = .{ .buffer = buf };
24
+ // TODO: extract out
25
+ var opts = pkmn.battle.options(
26
+ pkmn.protocol.FixedLog{ .writer = &writer },
27
+ g.chance.NULL,
28
+ g.calc.NULL,
29
+ );
30
+ break :result battle.update(c1, c2, &opts);
31
+ } else battle.update(c1, c2, &g.NULL)) catch unreachable;
32
+ }
33
+
34
+ pub fn choices(
35
+ battle: *g.Battle(g.PRNG),
36
+ player: u8,
37
+ request: u8,
38
+ buf: [*]u8,
39
+ ) callconv(.c) u8 {
40
+ assert(player <= util.EnumFieldsLen(pkmn.Player));
41
+ assert(request <= util.EnumFieldsLen(pkmn.Choice.Type));
42
+
43
+ const n = CHOICES_SIZE;
44
+ return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(buf[0..n]));
45
+ }
46
+ };
47
+ }
48
+
49
+ fn size(n: usize) u32 {
50
+ return std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(n))) catch unreachable;
51
+ }
@@ -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
+ }
@@ -76,8 +76,8 @@ test Choice {
76
76
  const p2: Choice = .{ .type = .Switch, .data = 5 };
77
77
  try expectEqual(5, p2.data);
78
78
  try expectEqual(Choice.Type.Move, p1.type);
79
- try expectEqual(0b0001_0001, @as(u8, @bitCast(p1)));
80
- 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)));
81
81
  }
82
82
 
83
83
  /// The result of the battle - all results other than 'None' should be considered terminal.
@@ -112,6 +112,6 @@ pub const Result = packed struct(u8) {
112
112
  };
113
113
 
114
114
  test Result {
115
- try expectEqual(0b0101_0000, @as(u8, @bitCast(Result.Default)));
116
- 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 })));
117
117
  }
@@ -1,17 +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
5
 
6
6
  pub fn print(value: anytype) void {
7
- debug.getStderrMutex().lock();
8
- defer debug.getStderrMutex().unlock();
9
- 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;
10
13
 
11
14
  nosuspend {
12
- stderr.writeAll("\x1b[41m") catch return;
15
+ fw.interface.writeAll("\x1b[41m") catch return;
13
16
  if (@TypeOf(@src()) == @TypeOf(value)) {
14
- stderr.print("{s} ({s}:{d}:{d})", .{
17
+ fw.interface.print("{s} ({s}:{d}:{d})", .{
15
18
  value.fn_name,
16
19
  value.file,
17
20
  value.line,
@@ -19,56 +22,69 @@ pub fn print(value: anytype) void {
19
22
  }) catch return;
20
23
  } else {
21
24
  switch (@typeInfo(@TypeOf(value))) {
22
- .Struct => |info| {
25
+ .@"struct" => |info| {
23
26
  if (info.is_tuple) {
24
- inline for (info.fields, 0..) |f, i| {
25
- inspect(@field(value, f.name));
26
- 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
+ }
27
41
  }
28
42
  } else {
29
- inspect(value);
43
+ inspect(fw, value);
30
44
  }
31
45
  },
32
- else => inspect(value),
46
+ else => inspect(fw, value),
33
47
  }
34
48
  }
35
- stderr.writeAll("\x1b[K\x1b[0m\n") catch return;
49
+ fw.interface.writeAll("\x1b[K\x1b[0m\n") catch return;
36
50
  }
37
51
  }
38
52
 
39
- fn inspect(value: anytype) void {
40
- const stderr = io.getStdErr().writer();
41
-
53
+ fn inspect(w: anytype, value: anytype) void {
42
54
  nosuspend {
43
55
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
44
56
  switch (@typeInfo(@TypeOf(value))) {
45
- .Array => |info| {
46
- 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;
47
59
  @compileError(err);
48
60
  },
49
- .Pointer => |ptr_info| switch (ptr_info.size) {
50
- .One => switch (@typeInfo(ptr_info.child)) {
51
- .Array => |info| {
52
- 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
+ }
53
67
  @compileError(err);
54
68
  },
55
- .Enum, .Union, .Struct => return inspect(value.*),
69
+ .@"enum", .@"union", .@"struct" => return inspect(value.*),
56
70
  else => @compileError(err),
57
71
  },
58
- .Many, .C => {
72
+ .many, .c => {
59
73
  if (ptr_info.sentinel) |_| return inspect(std.mem.span(value));
60
74
  if (ptr_info.child == u8) {
61
- return stderr.print("{s}", .{std.mem.span(value)}) catch return;
75
+ return w.interface.print("{s}", .{std.mem.span(value)}) catch return;
62
76
  }
63
77
  @compileError(err);
64
78
  },
65
- .Slice => {
66
- 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
+ }
67
83
  @compileError(err);
68
84
  },
69
85
  },
70
- .Optional => stderr.print("{?}", .{value}) catch return,
71
- else => stderr.print("{}", .{value}) catch return,
86
+ .optional => w.interface.print("{?}", .{value}) catch return,
87
+ else => w.interface.print("{}", .{value}) catch return,
72
88
  }
73
89
  }
74
90
  }
@@ -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,34 +3,38 @@ 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
- const fields = std.meta.fields(switch (@typeInfo(T)) {
9
- .Bool => enum { false, true },
10
- else => T,
11
- });
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
+ };
12
23
 
13
- var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
14
- 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;
15
27
 
16
- enumFields[0] = .{ .name = "None", .value = 0 };
28
+ fieldNames[0] = "None";
29
+ fieldValues[0] = 0;
17
30
 
18
- inline for (fields, 1..) |field, i| {
19
- assert(!std.mem.eql(u8, field.name, "None"));
20
- enumFields[i] = .{
21
- .name = field.name,
22
- .value = i,
23
- };
31
+ inline for (names, 1..) |name, i| {
32
+ assert(!std.mem.eql(u8, name, "None"));
33
+ fieldNames[i] = name;
34
+ fieldValues[i] = i;
24
35
  }
25
36
 
26
- return @Type(.{
27
- .Enum = .{
28
- .tag_type = std.math.IntFittingRange(0, fields.len),
29
- .fields = &enumFields,
30
- .decls = &decls,
31
- .is_exhaustive = true,
32
- },
33
- });
37
+ return @Enum(TagType, .exhaustive, &fieldNames, &fieldValues);
34
38
  }
35
39
 
36
40
  test Optional {
@@ -46,7 +50,7 @@ test Optional {
46
50
  try expect(b != .true);
47
51
  try expect(b != .false);
48
52
 
49
- const Player = @import("./data.zig").Player;
53
+ const Player = @import("data.zig").Player;
50
54
  try expect(@bitSizeOf(Optional(Player)) == 2);
51
55
 
52
56
  const p: Optional(Player) = .P2;
@@ -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 {