@pkmn/engine 0.1.0-dev.b01cb48c → 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 (81) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +54 -6
  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 +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 +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 +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 +52 -76
  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 -85
  39. package/package.json +56 -136
  40. package/src/bin/install-pkmn-engine +10 -7
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +255 -0
  43. package/src/lib/bindings/node.zig +117 -0
  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 +8 -8
  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 +13 -9
  51. package/src/lib/common/options.zig +4 -0
  52. package/src/lib/common/protocol.zig +44 -43
  53. package/src/lib/common/rational.zig +317 -304
  54. package/src/lib/common/rng.zig +5 -7
  55. package/src/lib/common/util.zig +65 -1
  56. package/src/lib/gen1/calc.zig +266 -135
  57. package/src/lib/gen1/chance.zig +469 -110
  58. package/src/lib/gen1/data/moves.zig +2 -4
  59. package/src/lib/gen1/data/species.zig +1 -3
  60. package/src/lib/gen1/data/types.zig +2 -3
  61. package/src/lib/gen1/data.zig +35 -38
  62. package/src/lib/gen1/helpers.zig +17 -27
  63. package/src/lib/gen1/mechanics.zig +213 -160
  64. package/src/lib/gen2/calc.zig +37 -27
  65. package/src/lib/gen2/chance.zig +87 -67
  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 +28 -30
  71. package/src/lib/gen2/generated.zig +18 -23
  72. package/src/lib/gen2/helpers.zig +9 -18
  73. package/src/lib/gen2/mechanics.zig +82 -82
  74. package/src/lib/node.zig +4 -121
  75. package/src/lib/pkmn.zig +14 -7
  76. package/src/lib/wasm.zig +13 -9
  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 -405
  80. package/build/test/display.js.map +0 -1
  81. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -1,10 +1,8 @@
1
- const std = @import("std");
2
-
3
1
  const options = @import("options.zig");
2
+ const std = @import("std");
4
3
 
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 {
@@ -74,7 +72,7 @@ pub const PSRNG = extern struct {
74
72
 
75
73
  test PSRNG {
76
74
  var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
77
- try expectEqual(121, psrng.range(u8, 0, 256));
75
+ try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
78
76
  try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
79
77
  }
80
78
 
@@ -111,9 +109,9 @@ test Gen12 {
111
109
  try expectEqual(e, rng.next());
112
110
  }
113
111
 
114
- try expectEqual(16, Gen12.percent(6) + 1);
115
- try expectEqual(16, Gen12.percent(7) - 1);
116
- try expectEqual(128, Gen12.percent(50) + 1);
112
+ try expectEqual(@as(u8, 16), Gen12.percent(6) + 1);
113
+ try expectEqual(@as(u8, 16), Gen12.percent(7) - 1);
114
+ try expectEqual(@as(u8, 128), Gen12.percent(50) + 1);
117
115
  }
118
116
 
119
117
  // https://pkmn.cc/pokeemerald/src/random.c
@@ -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
+ }