@pkmn/engine 0.1.0-dev.d716e940 → 0.1.0-dev.d952b7c9

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 (79) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +97 -59
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +43 -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 +96 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon.d.ts +2 -0
  10. package/build/pkg/addon.js +35 -57
  11. package/build/pkg/addon.js.map +1 -1
  12. package/build/pkg/data/ids.json +12 -12
  13. package/build/pkg/data/layout.json +26 -4
  14. package/build/pkg/data/protocol.json +69 -12
  15. package/build/pkg/data.d.ts +27 -5
  16. package/build/pkg/data.js.map +1 -1
  17. package/build/pkg/gen1.js +16 -19
  18. package/build/pkg/gen1.js.map +1 -1
  19. package/build/pkg/index.d.ts +24 -10
  20. package/build/pkg/index.js +15 -10
  21. package/build/pkg/index.js.map +1 -1
  22. package/build/pkg/protocol.d.ts +20 -1
  23. package/build/pkg/protocol.js +123 -102
  24. package/build/pkg/protocol.js.map +1 -1
  25. package/build/tools/debug.js +36 -68
  26. package/build/tools/debug.js.map +1 -1
  27. package/build/tools/display/debug.jsx +128 -0
  28. package/build/tools/display/debug.jsx.map +1 -0
  29. package/build/tools/display/index.js +132 -0
  30. package/build/tools/display/index.js.map +1 -0
  31. package/build/tools/display/ui.jsx +349 -0
  32. package/build/tools/display/ui.jsx.map +1 -0
  33. package/build/tools/display/util.js +18 -0
  34. package/build/tools/display/util.js.map +1 -0
  35. package/build.zig +286 -131
  36. package/package.json +73 -31
  37. package/src/bin/install-pkmn-engine +39 -24
  38. package/src/bin/pkmn-debug +1 -1
  39. package/src/lib/common/array.zig +77 -0
  40. package/src/lib/common/battle.zig +17 -0
  41. package/src/lib/common/data.zig +19 -31
  42. package/src/lib/common/debug.zig +30 -16
  43. package/src/lib/common/js.zig +120 -0
  44. package/src/lib/common/optional.zig +71 -0
  45. package/src/lib/common/options.zig +13 -0
  46. package/src/lib/common/protocol.zig +911 -367
  47. package/src/lib/common/rational.zig +447 -0
  48. package/src/lib/common/rng.zig +31 -22
  49. package/src/lib/common/util.zig +61 -0
  50. package/src/lib/gen1/calc.zig +950 -0
  51. package/src/lib/gen1/chance.zig +1169 -0
  52. package/src/lib/gen1/data/moves.zig +202 -190
  53. package/src/lib/gen1/data/species.zig +5 -5
  54. package/src/lib/gen1/data/types.zig +10 -10
  55. package/src/lib/gen1/data.zig +92 -79
  56. package/src/lib/gen1/helpers.zig +141 -28
  57. package/src/lib/gen1/mechanics.zig +1118 -773
  58. package/src/lib/gen2/calc.zig +161 -0
  59. package/src/lib/gen2/chance.zig +902 -0
  60. package/src/lib/gen2/data/items.zig +14 -20
  61. package/src/lib/gen2/data/moves.zig +664 -570
  62. package/src/lib/gen2/data/species.zig +2 -2
  63. package/src/lib/gen2/data/types.zig +137 -34
  64. package/src/lib/gen2/data.zig +125 -86
  65. package/src/lib/gen2/generated.zig +806 -0
  66. package/src/lib/gen2/helpers.zig +54 -32
  67. package/src/lib/gen2/mechanics.zig +3564 -15
  68. package/src/lib/node.zig +131 -0
  69. package/src/lib/pkmn.zig +81 -5
  70. package/src/lib/wasm.zig +13 -0
  71. package/src/tools/display/dom.ts +96 -0
  72. package/src/tools/display/ui.css +351 -0
  73. package/build/test/display.js +0 -400
  74. package/build/test/display.js.map +0 -1
  75. package/src/lib/binding/node.zig +0 -229
  76. package/src/lib/binding/wasm.zig +0 -10
  77. package/src/lib/gen1/README.md +0 -812
  78. package/src/lib/gen2/README.md +0 -309
  79. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -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 = .{
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
+ }
@@ -1,11 +1,17 @@
1
1
  const build_options = @import("build_options");
2
2
  const root = @import("root");
3
3
 
4
+ /// Configures the behavior of the pkmn package.
4
5
  pub const Options = struct {
5
6
  showdown: ?bool = null,
6
7
  log: ?bool = null,
8
+ chance: ?bool = null,
9
+ calc: ?bool = null,
7
10
  advance: ?bool = null,
8
11
  ebc: ?bool = null,
12
+ key: ?bool = null,
13
+ miss: ?bool = null,
14
+ overwrite: ?bool = null,
9
15
  internal: ?bool = null,
10
16
  };
11
17
 
@@ -13,9 +19,16 @@ pub const Options = struct {
13
19
  pub const showdown = get("showdown", false);
14
20
  /// Whether or not protocol message logging is enabled.
15
21
  pub const log = get("log", false);
22
+ /// Whether or not update probability tracking is enabled.
23
+ pub const chance = get("chance", false);
24
+ /// Whether or not damage calculator support is enabled.
25
+ pub const calc = get("calc", false);
16
26
 
17
27
  pub const advance = get("advance", true);
18
28
  pub const ebc = get("ebc", true);
29
+ pub const key = get("key", false);
30
+ pub const miss = get("miss", true);
31
+ pub const overwrite = get("overwrite", true);
19
32
  pub const internal = get("internal", false);
20
33
 
21
34
  fn get(comptime name: []const u8, default: bool) bool {