@pkmn/engine 0.1.0-dev.f3aab252 → 0.1.0-dev.f3b63b86

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 (80) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +35 -8
  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 +34 -65
  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 +23 -1
  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 +39 -89
  23. package/build/pkg/index.js +23 -70
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +20 -1
  26. package/build/pkg/protocol.js +105 -103
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +68 -74
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +152 -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 +359 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +33 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +235 -213
  39. package/package.json +59 -141
  40. package/src/bin/install-pkmn-engine +25 -9
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +200 -0
  43. package/src/lib/bindings/node.zig +117 -0
  44. package/src/lib/bindings/wasm.zig +50 -0
  45. package/src/lib/common/array.zig +5 -12
  46. package/src/lib/common/debug.zig +30 -9
  47. package/src/lib/common/js.zig +3 -9
  48. package/src/lib/common/optional.zig +11 -21
  49. package/src/lib/common/options.zig +4 -0
  50. package/src/lib/common/protocol.zig +180 -171
  51. package/src/lib/common/rational.zig +278 -285
  52. package/src/lib/common/rng.zig +1 -3
  53. package/src/lib/common/util.zig +14 -31
  54. package/src/lib/gen1/calc.zig +258 -106
  55. package/src/lib/gen1/chance.zig +57 -54
  56. package/src/lib/gen1/data/moves.zig +2 -4
  57. package/src/lib/gen1/data/species.zig +1 -3
  58. package/src/lib/gen1/data/types.zig +3 -4
  59. package/src/lib/gen1/data.zig +12 -16
  60. package/src/lib/gen1/helpers.zig +13 -28
  61. package/src/lib/gen1/mechanics.zig +131 -101
  62. package/src/lib/gen2/calc.zig +12 -23
  63. package/src/lib/gen2/chance.zig +12 -18
  64. package/src/lib/gen2/data/items.zig +1 -3
  65. package/src/lib/gen2/data/moves.zig +2 -4
  66. package/src/lib/gen2/data/species.zig +1 -3
  67. package/src/lib/gen2/data/types.zig +7 -5
  68. package/src/lib/gen2/data.zig +19 -22
  69. package/src/lib/gen2/generated.zig +18 -23
  70. package/src/lib/gen2/helpers.zig +9 -18
  71. package/src/lib/gen2/mechanics.zig +50 -47
  72. package/src/lib/napi.h +2 -0
  73. package/src/lib/node.zig +4 -121
  74. package/src/lib/pkmn.zig +98 -19
  75. package/src/lib/wasm.zig +9 -9
  76. package/src/tools/display/dom.ts +93 -0
  77. package/src/tools/display/ui.css +354 -0
  78. package/build/test/display.js +0 -405
  79. package/build/test/display.js.map +0 -1
  80. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -0,0 +1,50 @@
1
+ const pkmn = @import("../pkmn.zig");
2
+ const std = @import("std");
3
+
4
+ const assert = std.debug.assert;
5
+
6
+ pub const options = pkmn.options;
7
+
8
+ pub fn gen(comptime num: comptime_int) type {
9
+ const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
10
+ return struct {
11
+ pub const CHOICES_SIZE = size(g.CHOICES_SIZE);
12
+ pub const LOGS_SIZE = size(g.LOGS_SIZE);
13
+
14
+ pub fn update(
15
+ battle: *g.Battle(g.PRNG),
16
+ c1: pkmn.Choice,
17
+ c2: pkmn.Choice,
18
+ options_: ?[*]u8,
19
+ ) callconv(.c) pkmn.Result {
20
+ return (if (options_) |o| result: {
21
+ const buf = @as([*]u8, @ptrCast(o))[0..g.LOGS_SIZE];
22
+ var writer: pkmn.protocol.Writer = .{ .buffer = buf };
23
+ // TODO: extract out
24
+ var opts = pkmn.battle.options(
25
+ pkmn.protocol.FixedLog{ .writer = &writer },
26
+ g.chance.NULL,
27
+ g.calc.NULL,
28
+ );
29
+ break :result battle.update(c1, c2, &opts);
30
+ } else battle.update(c1, c2, &g.NULL)) catch unreachable;
31
+ }
32
+
33
+ pub fn choices(
34
+ battle: *g.Battle(g.PRNG),
35
+ player: u8,
36
+ request: u8,
37
+ buf: [*]u8,
38
+ ) callconv(.c) u8 {
39
+ assert(player <= @typeInfo(pkmn.Player).@"enum".fields.len);
40
+ assert(request <= @typeInfo(pkmn.Choice.Type).@"enum".fields.len);
41
+
42
+ const n = CHOICES_SIZE;
43
+ return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(buf[0..n]));
44
+ }
45
+ };
46
+ }
47
+
48
+ fn size(n: usize) u32 {
49
+ return std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(n))) catch unreachable;
50
+ }
@@ -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;
@@ -19,7 +19,7 @@ pub fn print(value: anytype) void {
19
19
  }) catch return;
20
20
  } else {
21
21
  switch (@typeInfo(@TypeOf(value))) {
22
- .Struct => |info| {
22
+ .@"struct" => |info| {
23
23
  if (info.is_tuple) {
24
24
  inline for (info.fields, 0..) |f, i| {
25
25
  inspect(@field(value, f.name));
@@ -42,33 +42,54 @@ fn inspect(value: anytype) void {
42
42
  nosuspend {
43
43
  const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
44
44
  switch (@typeInfo(@TypeOf(value))) {
45
- .Array => |info| {
45
+ .array => |info| {
46
46
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
47
47
  @compileError(err);
48
48
  },
49
- .Pointer => |ptr_info| switch (ptr_info.size) {
50
- .One => switch (@typeInfo(ptr_info.child)) {
51
- .Array => |info| {
49
+ .pointer => |ptr_info| switch (ptr_info.size) {
50
+ .one => switch (@typeInfo(ptr_info.child)) {
51
+ .array => |info| {
52
52
  if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
53
53
  @compileError(err);
54
54
  },
55
- .Enum, .Union, .Struct => return inspect(value.*),
55
+ .@"enum", .@"union", .@"struct" => return inspect(value.*),
56
56
  else => @compileError(err),
57
57
  },
58
- .Many, .C => {
58
+ .many, .c => {
59
59
  if (ptr_info.sentinel) |_| return inspect(std.mem.span(value));
60
60
  if (ptr_info.child == u8) {
61
61
  return stderr.print("{s}", .{std.mem.span(value)}) catch return;
62
62
  }
63
63
  @compileError(err);
64
64
  },
65
- .Slice => {
65
+ .slice => {
66
66
  if (ptr_info.child == u8) return stderr.print("{s}", .{value}) catch return;
67
67
  @compileError(err);
68
68
  },
69
69
  },
70
- .Optional => stderr.print("{?}", .{value}) catch return,
70
+ .optional => stderr.print("{?}", .{value}) catch return,
71
71
  else => stderr.print("{}", .{value}) catch return,
72
72
  }
73
73
  }
74
74
  }
75
+
76
+ const showdown = @import("./options.zig").showdown;
77
+ const Result = @import("./data.zig").Result;
78
+ const Choice = @import("./data.zig").Choice;
79
+
80
+ pub fn dump(gen: u8, battle: anytype, frame: ?struct { Result, Choice, Choice }) void {
81
+ const file = std.fs.cwd().createFile("logs/dump.bin", .{}) catch return;
82
+ defer file.close();
83
+ var w = file.writer();
84
+ w.writeByte(@intFromBool(showdown)) catch return;
85
+ w.writeByte(gen) catch return;
86
+ w.writeInt(i16, 0, .little) catch return;
87
+ w.writeInt(i32, 0, .little) catch return;
88
+ w.writeStruct(battle) catch return;
89
+ w.writeStruct(battle) catch return;
90
+ if (frame) |f| {
91
+ w.writeStruct(f[0]) catch return;
92
+ w.writeStruct(f[1]) catch return;
93
+ w.writeStruct(f[2]) catch return;
94
+ }
95
+ }
@@ -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,10 +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,
13
14
  miss: ?bool = null,
15
+ overwrite: ?bool = null,
14
16
  internal: ?bool = null,
15
17
  };
16
18
 
@@ -23,10 +25,12 @@ pub const chance = get("chance", false);
23
25
  /// Whether or not damage calculator support is enabled.
24
26
  pub const calc = get("calc", false);
25
27
 
28
+ pub const mod = showdown or get("mod", false);
26
29
  pub const advance = get("advance", true);
27
30
  pub const ebc = get("ebc", true);
28
31
  pub const key = get("key", false);
29
32
  pub const miss = get("miss", true);
33
+ pub const overwrite = get("overwrite", true);
30
34
  pub const internal = get("internal", false);
31
35
 
32
36
  fn get(comptime name: []const u8, default: bool) bool {