@pkmn/engine 0.1.0-dev.b05836cd → 0.1.0-dev.b3197aa3

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 +91 -18
  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 +25 -3
  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 -5
  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 +42 -91
  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 +109 -107
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +53 -78
  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 +331 -104
  39. package/package.json +61 -134
  40. package/src/bin/install-pkmn-engine +32 -21
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +255 -0
  43. package/src/lib/{binding → bindings}/node.zig +9 -26
  44. package/src/lib/bindings/wasm.zig +59 -0
  45. package/src/lib/common/array.zig +77 -0
  46. package/src/lib/common/data.zig +4 -4
  47. package/src/lib/common/debug.zig +39 -8
  48. package/src/lib/common/js.zig +4 -6
  49. package/src/lib/common/napi.zig +4 -0
  50. package/src/lib/common/optional.zig +14 -10
  51. package/src/lib/common/options.zig +6 -0
  52. package/src/lib/common/protocol.zig +63 -67
  53. package/src/lib/common/rational.zig +355 -248
  54. package/src/lib/common/rng.zig +7 -9
  55. package/src/lib/common/util.zig +65 -1
  56. package/src/lib/gen1/calc.zig +698 -162
  57. package/src/lib/gen1/chance.zig +405 -280
  58. package/src/lib/gen1/data/moves.zig +2 -4
  59. package/src/lib/gen1/data/species.zig +2 -4
  60. package/src/lib/gen1/data/types.zig +2 -3
  61. package/src/lib/gen1/data.zig +24 -27
  62. package/src/lib/gen1/helpers.zig +92 -548
  63. package/src/lib/gen1/mechanics.zig +390 -267
  64. package/src/lib/gen2/calc.zig +37 -27
  65. package/src/lib/gen2/chance.zig +84 -496
  66. package/src/lib/gen2/data/items.zig +1 -3
  67. package/src/lib/gen2/data/moves.zig +2 -4
  68. package/src/lib/gen2/data/species.zig +1 -3
  69. package/src/lib/gen2/data/types.zig +7 -5
  70. package/src/lib/gen2/data.zig +22 -24
  71. package/src/lib/gen2/generated.zig +19 -26
  72. package/src/lib/gen2/helpers.zig +46 -25
  73. package/src/lib/gen2/mechanics.zig +218 -164
  74. package/src/lib/node.zig +14 -0
  75. package/src/lib/pkmn.zig +37 -15
  76. package/src/lib/wasm.zig +17 -0
  77. package/src/tools/display/dom.ts +93 -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
@@ -1,10 +1,8 @@
1
+ const options = @import("options.zig");
1
2
  const std = @import("std");
2
3
 
3
- const options = @import("./options.zig");
4
-
5
4
  const assert = std.debug.assert;
6
5
  const expectEqual = std.testing.expectEqual;
7
-
8
6
  const showdown = options.showdown;
9
7
 
10
8
  pub fn PRNG(comptime gen: comptime_int) type {
@@ -66,6 +64,10 @@ pub const PSRNG = extern struct {
66
64
  (@as(u64, self.range(u16, 0, 0x10000)) << 16) |
67
65
  (@as(u64, self.range(u16, 0, 0x10000)));
68
66
  }
67
+
68
+ pub fn Bound(comptime T: type) type {
69
+ return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
70
+ }
69
71
  };
70
72
 
71
73
  test PSRNG {
@@ -197,7 +199,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
197
199
  for (0..n) |_| _ = self.next();
198
200
  }
199
201
 
200
- pub fn range(self: *Self, comptime T: type, from: T, to: Bound(T)) T {
202
+ pub fn range(self: *Self, comptime T: type, from: T, to: PSRNG.Bound(T)) T {
201
203
  assert(showdown);
202
204
  return @intCast(@as(u64, self.next()) * (to - from) / divisor + from);
203
205
  }
@@ -206,7 +208,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
206
208
  self: *Self,
207
209
  comptime T: type,
208
210
  numerator: T,
209
- denominator: Bound(T),
211
+ denominator: PSRNG.Bound(T),
210
212
  ) bool {
211
213
  assert(showdown);
212
214
  assert(denominator > 0);
@@ -237,7 +239,3 @@ fn Output(comptime gen: comptime_int) type {
237
239
  else => unreachable,
238
240
  };
239
241
  }
240
-
241
- fn Bound(comptime T: type) type {
242
- return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
243
- }
@@ -1,12 +1,76 @@
1
1
  const std = @import("std");
2
2
 
3
+ const expect = std.testing.expect;
3
4
  const expectEqual = std.testing.expectEqual;
4
5
 
6
+ const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
7
+ const One = if (@hasField(std.builtin.Type.Pointer.Size, "one")) .one else .One;
8
+
5
9
  pub fn PointerType(comptime P: type, comptime C: type) type {
6
- return if (@typeInfo(P).Pointer.is_const) *const C else *C;
10
+ return if (@field(@typeInfo(P), @tagName(Pointer)).is_const) *const C else *C;
7
11
  }
8
12
 
9
13
  test PointerType {
10
14
  try expectEqual(*bool, PointerType(*u8, bool));
11
15
  try expectEqual(*const f64, PointerType(*const i32, f64));
12
16
  }
17
+
18
+ pub fn isPointerTo(p: anytype, comptime P: type) bool {
19
+ const info = @typeInfo(@TypeOf(p));
20
+ return switch (info) {
21
+ Pointer => @field(info, @tagName(Pointer)).child == P,
22
+ else => false,
23
+ };
24
+ }
25
+
26
+ test isPointerTo {
27
+ const S = struct {};
28
+ const s: S = .{};
29
+ try expect(!isPointerTo(s, S));
30
+ try expect(isPointerTo(&s, S));
31
+ }
32
+
33
+ // NOTE: std.mem.bytesAsValue backported from ziglang/zig#18061
34
+ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) {
35
+ return @as(BytesAsValueReturnType(T, @TypeOf(bytes)), @ptrCast(bytes));
36
+ }
37
+
38
+ fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
39
+ return CopyPtrAttrs(B, One, T);
40
+ }
41
+
42
+ fn CopyPtrAttrs(
43
+ comptime source: type,
44
+ comptime size: std.builtin.Type.Pointer.Size,
45
+ comptime child: type,
46
+ ) type {
47
+ const info = @field(@typeInfo(source), @tagName(Pointer));
48
+ const args: std.builtin.Type.Pointer = if (@hasField(std.builtin.Type.Pointer, "sentinel")) .{
49
+ .size = size,
50
+ .is_const = info.is_const,
51
+ .is_volatile = info.is_volatile,
52
+ .is_allowzero = info.is_allowzero,
53
+ .alignment = info.alignment,
54
+ .address_space = info.address_space,
55
+ .child = child,
56
+ .sentinel = null,
57
+ } else .{
58
+ .size = size,
59
+ .is_const = info.is_const,
60
+ .is_volatile = info.is_volatile,
61
+ .is_allowzero = info.is_allowzero,
62
+ .alignment = info.alignment,
63
+ .address_space = info.address_space,
64
+ .child = child,
65
+ .sentinel_ptr = null,
66
+ };
67
+ return @Type(if (@hasField(std.builtin.Type, "pointer"))
68
+ .{ .pointer = args }
69
+ else
70
+ .{ .Pointer = args });
71
+ }
72
+
73
+ // NOTE: std.meta.FieldType replaced by @FieldType in 0.14.0
74
+ pub fn FieldType(comptime T: type, comptime field: std.meta.FieldEnum(T)) type {
75
+ return std.meta.fieldInfo(T, field).type;
76
+ }