@pkmn/engine 0.1.0-dev.d97c6e03 → 0.1.0-dev.dc33f678

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 (51) hide show
  1. package/README.md +67 -56
  2. package/build/pkg/addon.d.ts +1 -1
  3. package/build/pkg/addon.js +7 -6
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/ids.json +12 -12
  6. package/build/pkg/data/layout.json +27 -5
  7. package/build/pkg/data/protocol.json +69 -12
  8. package/build/pkg/data.d.ts +2 -2
  9. package/build/pkg/gen1.d.ts +5 -4
  10. package/build/pkg/gen1.js +48 -32
  11. package/build/pkg/gen1.js.map +1 -1
  12. package/build/pkg/index.d.ts +15 -12
  13. package/build/pkg/index.js.map +1 -1
  14. package/build/pkg/protocol.d.ts +4 -2
  15. package/build/pkg/protocol.js +60 -39
  16. package/build/pkg/protocol.js.map +1 -1
  17. package/build/test/display.js +29 -12
  18. package/build/test/display.js.map +1 -1
  19. package/build.zig +92 -68
  20. package/package.json +40 -21
  21. package/src/bin/install-pkmn-engine +11 -6
  22. package/src/lib/binding/node.zig +46 -141
  23. package/src/lib/binding/wasm.zig +5 -3
  24. package/src/lib/common/battle.zig +17 -0
  25. package/src/lib/common/data.zig +15 -27
  26. package/src/lib/common/debug.zig +21 -11
  27. package/src/lib/common/js.zig +118 -0
  28. package/src/lib/common/optional.zig +67 -0
  29. package/src/lib/common/options.zig +12 -3
  30. package/src/lib/common/protocol.zig +771 -245
  31. package/src/lib/common/rational.zig +353 -0
  32. package/src/lib/common/rng.zig +15 -15
  33. package/src/lib/common/util.zig +12 -0
  34. package/src/lib/gen1/README.md +285 -182
  35. package/src/lib/gen1/calc.zig +426 -0
  36. package/src/lib/gen1/chance.zig +1060 -0
  37. package/src/lib/gen1/data/moves.zig +192 -180
  38. package/src/lib/gen1/data/species.zig +2 -2
  39. package/src/lib/gen1/data/types.zig +5 -5
  40. package/src/lib/gen1/data.zig +79 -65
  41. package/src/lib/gen1/helpers.zig +577 -32
  42. package/src/lib/gen1/mechanics.zig +940 -614
  43. package/src/lib/gen2/README.md +106 -85
  44. package/src/lib/gen2/data/items.zig +12 -18
  45. package/src/lib/gen2/data/moves.zig +660 -566
  46. package/src/lib/gen2/data/species.zig +1 -1
  47. package/src/lib/gen2/data/types.zig +131 -28
  48. package/src/lib/gen2/data.zig +132 -84
  49. package/src/lib/gen2/helpers.zig +16 -22
  50. package/src/lib/gen2/mechanics.zig +2426 -12
  51. package/src/lib/pkmn.zig +65 -6
@@ -0,0 +1,118 @@
1
+ const std = @import("std");
2
+
3
+ const assert = std.debug.assert;
4
+
5
+ const c = @cImport({
6
+ @cDefine("NAPI_VERSION", "8");
7
+ @cInclude("node_api.h");
8
+ });
9
+
10
+ pub const Array = struct {
11
+ pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
12
+ var result: c.napi_value = undefined;
13
+ assert(c.napi_ok == if (o.length) |n|
14
+ c.napi_create_array_with_length(env, n, &result)
15
+ else
16
+ c.napi_create_array(env, &result));
17
+ return result;
18
+ }
19
+
20
+ pub fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
21
+ assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
22
+ }
23
+ };
24
+
25
+ pub const Boolean = struct {
26
+ pub fn init(env: c.napi_env, value: bool) c.napi_value {
27
+ var result: c.napi_value = undefined;
28
+ assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
29
+ return result;
30
+ }
31
+ };
32
+
33
+ pub const Number = struct {
34
+ pub fn init(env: c.napi_env, value: anytype) c.napi_value {
35
+ const T = @TypeOf(value);
36
+ var result: c.napi_value = undefined;
37
+ assert(c.napi_ok == switch (@typeInfo(T)) {
38
+ .Int => |info| switch (info.bits) {
39
+ 0...32 => switch (info.signedness) {
40
+ .signed => c.napi_create_int32(env, @as(i32, value), &result),
41
+ .unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
42
+ },
43
+ 33...52 => c.napi_create_int64(env, @as(i64, value), &result),
44
+ else => @compileError("int can't be represented as JS number"),
45
+ },
46
+ else => @compileError("expected number, got: " ++ @typeName(T)),
47
+ });
48
+ return result;
49
+ }
50
+
51
+ pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
52
+ switch (@typeInfo(T)) {
53
+ .Int => |info| switch (info.bits) {
54
+ 0...32 => switch (info.signedness) {
55
+ .signed => {
56
+ var result: i32 = undefined;
57
+ assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
58
+ return if (info.bits == 32) result else @intCast(result);
59
+ },
60
+ .unsigned => {
61
+ var result: u32 = undefined;
62
+ assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
63
+ return if (info.bits == 32) result else @intCast(result);
64
+ },
65
+ },
66
+ 33...63 => {
67
+ var result: i64 = undefined;
68
+ assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
69
+ return @intCast(result);
70
+ },
71
+ else => {
72
+ var result: i64 = undefined;
73
+ assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
74
+ return switch (info.signedness) {
75
+ .signed => value,
76
+ .unsigned => if (0 <= value) @intCast(value) else unreachable,
77
+ };
78
+ },
79
+ },
80
+ else => @compileError("expected number, got: " ++ @typeName(T)),
81
+ }
82
+ }
83
+ };
84
+
85
+ pub const Object = struct {
86
+ pub fn init(env: c.napi_env) c.napi_value {
87
+ var result: c.napi_value = undefined;
88
+ assert(c.napi_create_object(env, &result) == c.napi_ok);
89
+ return result;
90
+ }
91
+ };
92
+
93
+ pub const Property = union(enum) {
94
+ method: c.napi_callback,
95
+ value: c.napi_value,
96
+
97
+ pub fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
98
+ return .{
99
+ .utf8name = name,
100
+ .name = null,
101
+ .method = switch (property) {
102
+ .method => |m| m,
103
+ .value => null,
104
+ },
105
+ .getter = null,
106
+ .setter = null,
107
+ .value = switch (property) {
108
+ .method => null,
109
+ .value => |v| v,
110
+ },
111
+ .attributes = switch (property) {
112
+ .method => c.napi_default,
113
+ .value => c.napi_enumerable,
114
+ },
115
+ .data = null,
116
+ };
117
+ }
118
+ };
@@ -0,0 +1,67 @@
1
+ const std = @import("std");
2
+
3
+ const expect = std.testing.expect;
4
+ const assert = std.debug.assert;
5
+
6
+ // TODO: ziglang/zig#104
7
+ 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
+ });
12
+
13
+ var enumFields: [fields.len + 1]std.builtin.Type.EnumField = undefined;
14
+ var decls = [_]std.builtin.Type.Declaration{};
15
+
16
+ enumFields[0] = .{ .name = "None", .value = 0 };
17
+
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
+ };
24
+ }
25
+
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
+ });
34
+ }
35
+
36
+ test Optional {
37
+ try expect(@bitSizeOf(Optional(bool)) == 2);
38
+
39
+ const a: Optional(bool) = .true;
40
+ try expect(a != .None);
41
+ try expect(a == .true);
42
+ try expect(a != .false);
43
+
44
+ const b: Optional(bool) = .None;
45
+ try expect(b == .None);
46
+ try expect(b != .true);
47
+ try expect(b != .false);
48
+
49
+ const Player = @import("./data.zig").Player;
50
+ try expect(@bitSizeOf(Optional(Player)) == 2);
51
+
52
+ const p: Optional(Player) = .P2;
53
+ try expect(p != .None);
54
+ try expect(p != .P1);
55
+ try expect(p == .P2);
56
+
57
+ const q: Optional(Player) = .None;
58
+ try expect(q == .None);
59
+ try expect(q != .P1);
60
+ try expect(q != .P2);
61
+
62
+ const Three = enum(u2) { A, B, C };
63
+ try expect(@bitSizeOf(Optional(Three)) == 2);
64
+
65
+ const Four = enum(u2) { A, B, C, D };
66
+ try expect(@bitSizeOf(Optional(Four)) == 3);
67
+ }
@@ -1,21 +1,30 @@
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
- trace: ?bool = null,
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,
9
13
  internal: ?bool = null,
10
14
  };
11
15
 
12
16
  /// Whether or not Pokémon Showdown compatibility mode is enabled.
13
17
  pub const showdown = get("showdown", false);
14
- /// Whether or not protocol trace logging is enabled.
15
- pub const trace = get("trace", false);
18
+ /// Whether or not protocol message logging is enabled.
19
+ pub const log = get("log", false);
20
+ /// Whether or not update probability tracking is enabled.
21
+ pub const chance = get("chance", false);
22
+ /// Whether or not damage calculator support is enabled.
23
+ pub const calc = get("calc", false);
16
24
 
17
25
  pub const advance = get("advance", true);
18
26
  pub const ebc = get("ebc", true);
27
+ pub const key = get("key", false);
19
28
  pub const internal = get("internal", false);
20
29
 
21
30
  fn get(comptime name: []const u8, default: bool) bool {