@pkmn/engine 0.1.0-dev.d716e940 → 0.1.0-dev.d73cd76c
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.
- package/LICENSE +1 -3
- package/README.md +114 -69
- package/build/pkg/addon/browser.d.ts +3 -0
- package/build/pkg/addon/browser.js +47 -0
- package/build/pkg/addon/browser.js.map +1 -0
- package/build/pkg/addon/node.d.ts +3 -0
- package/build/pkg/addon/node.js +110 -0
- package/build/pkg/addon/node.js.map +1 -0
- package/build/pkg/addon/wasm.d.ts +2 -0
- package/build/pkg/addon/wasm.js +53 -0
- package/build/pkg/addon/wasm.js.map +1 -0
- package/build/pkg/addon.d.ts +26 -4
- package/build/pkg/addon.js +39 -70
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +39 -4
- package/build/pkg/data/protocol.json +84 -13
- package/build/pkg/data.d.ts +27 -5
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +15 -18
- package/build/pkg/gen1.js +43 -37
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +42 -91
- package/build/pkg/index.js +23 -70
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +29 -1
- package/build/pkg/protocol.js +123 -102
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +84 -73
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +66 -0
- package/build/tools/display/debug.jsx.map +1 -0
- package/build/tools/display/index.js +152 -0
- package/build/tools/display/index.js.map +1 -0
- package/build/tools/display/ui.jsx +360 -0
- package/build/tools/display/ui.jsx.map +1 -0
- package/build/tools/display/util.js +157 -0
- package/build/tools/display/util.js.map +1 -0
- package/build.zig +428 -176
- package/package.json +72 -122
- package/src/bin/install-pkmn-engine +56 -25
- package/src/bin/pkmn-debug +1 -1
- package/src/lib/bindings/c.zig +201 -0
- package/src/lib/bindings/node.zig +117 -0
- package/src/lib/bindings/wasm.zig +51 -0
- package/src/lib/common/array.zig +70 -0
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +19 -31
- package/src/lib/common/debug.zig +58 -33
- package/src/lib/common/js.zig +114 -0
- package/src/lib/common/optional.zig +71 -0
- package/src/lib/common/options.zig +15 -0
- package/src/lib/common/protocol.zig +1037 -462
- package/src/lib/common/rational.zig +440 -0
- package/src/lib/common/rng.zig +31 -24
- package/src/lib/common/util.zig +64 -0
- package/src/lib/gen1/calc.zig +975 -0
- package/src/lib/gen1/chance.zig +1246 -0
- package/src/lib/gen1/data/moves.zig +203 -193
- package/src/lib/gen1/data/species.zig +6 -8
- package/src/lib/gen1/data/types.zig +12 -13
- package/src/lib/gen1/data.zig +98 -89
- package/src/lib/gen1/helpers.zig +159 -51
- package/src/lib/gen1/mechanics.zig +1211 -846
- package/src/lib/gen2/calc.zig +150 -0
- package/src/lib/gen2/chance.zig +914 -0
- package/src/lib/gen2/data/items.zig +15 -23
- package/src/lib/gen2/data/moves.zig +665 -573
- package/src/lib/gen2/data/species.zig +3 -5
- package/src/lib/gen2/data/types.zig +141 -36
- package/src/lib/gen2/data.zig +136 -100
- package/src/lib/gen2/generated.zig +801 -0
- package/src/lib/gen2/helpers.zig +81 -58
- package/src/lib/gen2/mechanics.zig +3582 -30
- package/src/lib/napi.h +2 -0
- package/src/lib/node.zig +14 -0
- package/src/lib/pkmn.zig +172 -17
- package/src/lib/wasm.zig +13 -0
- package/src/tools/display/dom.ts +108 -0
- package/src/tools/display/ui.css +354 -0
- package/build/test/display.js +0 -400
- package/build/test/display.js.map +0 -1
- package/src/lib/binding/node.zig +0 -229
- package/src/lib/binding/wasm.zig +0 -10
- package/src/lib/gen1/README.md +0 -812
- package/src/lib/gen2/README.md +0 -309
- package/src/test/display/pkmn.html.tmpl +0 -368
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
const c = @import("napi");
|
|
2
|
+
const js = @import("../common/js.zig");
|
|
3
|
+
const pkmn = @import("../pkmn.zig");
|
|
4
|
+
const std = @import("std");
|
|
5
|
+
|
|
6
|
+
const assert = std.debug.assert;
|
|
7
|
+
|
|
8
|
+
pub fn register(env: c.napi_env) c.napi_value {
|
|
9
|
+
const object = js.Object.init(env);
|
|
10
|
+
const properties = [_]c.napi_property_descriptor{
|
|
11
|
+
js.Property.init("options", .{ .value = options(env) }),
|
|
12
|
+
js.Property.init("bindings", .{ .value = bindings(env) }),
|
|
13
|
+
};
|
|
14
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
15
|
+
return object;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
fn options(env: c.napi_env) c.napi_value {
|
|
19
|
+
const object = js.Object.init(env);
|
|
20
|
+
const properties = [_]c.napi_property_descriptor{
|
|
21
|
+
js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
|
|
22
|
+
js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
|
|
23
|
+
js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
|
|
24
|
+
js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
|
|
25
|
+
};
|
|
26
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
27
|
+
return object;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
fn bindings(env: c.napi_env) c.napi_value {
|
|
31
|
+
const array = js.Array.init(env, .{ .length = 1 });
|
|
32
|
+
js.Array.set(env, array, 0, bind(env, pkmn.gen1));
|
|
33
|
+
return array;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
fn bind(env: c.napi_env, gen: anytype) c.napi_value {
|
|
37
|
+
const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
|
|
38
|
+
const logs_size: u32 = @intCast(gen.LOGS_SIZE);
|
|
39
|
+
const object = js.Object.init(env);
|
|
40
|
+
const properties = [_]c.napi_property_descriptor{
|
|
41
|
+
js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
|
|
42
|
+
js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
|
|
43
|
+
js.Property.init("update", .{ .method = update(gen) }),
|
|
44
|
+
js.Property.init("choices", .{ .method = choices(gen) }),
|
|
45
|
+
};
|
|
46
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
47
|
+
return object;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fn update(gen: anytype) c.napi_callback {
|
|
51
|
+
return struct {
|
|
52
|
+
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.c) c.napi_value {
|
|
53
|
+
var argc: usize = 4;
|
|
54
|
+
var argv: [4]c.napi_value = undefined;
|
|
55
|
+
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
56
|
+
assert(argc == 4);
|
|
57
|
+
|
|
58
|
+
var data: ?*anyopaque = undefined;
|
|
59
|
+
var len: usize = 0;
|
|
60
|
+
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
61
|
+
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
62
|
+
|
|
63
|
+
var battle: *gen.Battle(gen.PRNG) = @ptrCast(@alignCast(data.?));
|
|
64
|
+
const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
|
|
65
|
+
const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
|
|
66
|
+
|
|
67
|
+
var vtype: c.napi_valuetype = undefined;
|
|
68
|
+
assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
|
|
69
|
+
const result = switch (vtype) {
|
|
70
|
+
c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
|
|
71
|
+
else => result: {
|
|
72
|
+
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
73
|
+
assert(len == gen.LOGS_SIZE);
|
|
74
|
+
|
|
75
|
+
const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
|
|
76
|
+
var writer: pkmn.protocol.Writer = .{ .buffer = buf };
|
|
77
|
+
// TODO: extract out
|
|
78
|
+
var opts = pkmn.battle.options(
|
|
79
|
+
pkmn.protocol.FixedLog{ .writer = &writer },
|
|
80
|
+
gen.chance.NULL,
|
|
81
|
+
gen.calc.NULL,
|
|
82
|
+
);
|
|
83
|
+
break :result battle.update(c1, c2, &opts);
|
|
84
|
+
},
|
|
85
|
+
} catch unreachable;
|
|
86
|
+
|
|
87
|
+
return js.Number.init(env, @as(u8, @bitCast(result)));
|
|
88
|
+
}
|
|
89
|
+
}.call;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
fn choices(gen: anytype) c.napi_callback {
|
|
93
|
+
return struct {
|
|
94
|
+
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.c) c.napi_value {
|
|
95
|
+
var argc: usize = 4;
|
|
96
|
+
var argv: [4]c.napi_value = undefined;
|
|
97
|
+
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
98
|
+
assert(argc == 4);
|
|
99
|
+
|
|
100
|
+
var data: ?*anyopaque = undefined;
|
|
101
|
+
var len: usize = 0;
|
|
102
|
+
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
103
|
+
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
104
|
+
|
|
105
|
+
var battle: *gen.Battle(gen.PRNG) = @ptrCast(@alignCast(data.?));
|
|
106
|
+
const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
|
|
107
|
+
const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
|
|
108
|
+
|
|
109
|
+
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
110
|
+
assert(len == gen.CHOICES_SIZE);
|
|
111
|
+
|
|
112
|
+
const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
|
|
113
|
+
const n = battle.choices(player, request, out);
|
|
114
|
+
return js.Number.init(env, @as(u8, @bitCast(n)));
|
|
115
|
+
}
|
|
116
|
+
}.call;
|
|
117
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
const pkmn = @import("../pkmn.zig");
|
|
2
|
+
const std = @import("std");
|
|
3
|
+
const util = @import("../common/util.zig");
|
|
4
|
+
|
|
5
|
+
const assert = std.debug.assert;
|
|
6
|
+
|
|
7
|
+
pub const options = pkmn.options;
|
|
8
|
+
|
|
9
|
+
pub fn gen(comptime num: comptime_int) type {
|
|
10
|
+
const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
|
|
11
|
+
return struct {
|
|
12
|
+
pub const CHOICES_SIZE = size(g.CHOICES_SIZE);
|
|
13
|
+
pub const LOGS_SIZE = size(g.LOGS_SIZE);
|
|
14
|
+
|
|
15
|
+
pub fn update(
|
|
16
|
+
battle: *g.Battle(g.PRNG),
|
|
17
|
+
c1: pkmn.Choice,
|
|
18
|
+
c2: pkmn.Choice,
|
|
19
|
+
options_: ?[*]u8,
|
|
20
|
+
) callconv(.c) pkmn.Result {
|
|
21
|
+
return (if (options_) |o| result: {
|
|
22
|
+
const buf = @as([*]u8, @ptrCast(o))[0..g.LOGS_SIZE];
|
|
23
|
+
var writer: pkmn.protocol.Writer = .{ .buffer = buf };
|
|
24
|
+
// TODO: extract out
|
|
25
|
+
var opts = pkmn.battle.options(
|
|
26
|
+
pkmn.protocol.FixedLog{ .writer = &writer },
|
|
27
|
+
g.chance.NULL,
|
|
28
|
+
g.calc.NULL,
|
|
29
|
+
);
|
|
30
|
+
break :result battle.update(c1, c2, &opts);
|
|
31
|
+
} else battle.update(c1, c2, &g.NULL)) catch unreachable;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
pub fn choices(
|
|
35
|
+
battle: *g.Battle(g.PRNG),
|
|
36
|
+
player: u8,
|
|
37
|
+
request: u8,
|
|
38
|
+
buf: [*]u8,
|
|
39
|
+
) callconv(.c) u8 {
|
|
40
|
+
assert(player <= util.EnumFieldsLen(pkmn.Player));
|
|
41
|
+
assert(request <= util.EnumFieldsLen(pkmn.Choice.Type));
|
|
42
|
+
|
|
43
|
+
const n = CHOICES_SIZE;
|
|
44
|
+
return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(buf[0..n]));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
fn size(n: usize) u32 {
|
|
50
|
+
return std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(n))) catch unreachable;
|
|
51
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const expectEqual = std.testing.expectEqual;
|
|
4
|
+
const assert = std.debug.assert;
|
|
5
|
+
|
|
6
|
+
/// Helpers for working with bit-packed arrays of non-powers-of-2 types.
|
|
7
|
+
/// NOTE: ziglang/zig#12547
|
|
8
|
+
pub fn Array(comptime n: comptime_int, comptime U: type) type {
|
|
9
|
+
return struct {
|
|
10
|
+
const size = @bitSizeOf(U);
|
|
11
|
+
pub const T = @Int(.unsigned, size * n);
|
|
12
|
+
|
|
13
|
+
/// Returns the value stored at index `i` in the array `a`.
|
|
14
|
+
pub fn get(a: T, i: usize) U {
|
|
15
|
+
assert(i < n);
|
|
16
|
+
const shift = i * size;
|
|
17
|
+
const mask: T = (1 << size) - 1;
|
|
18
|
+
const result = (a >> @intCast(shift)) & mask;
|
|
19
|
+
return switch (@typeInfo(U)) {
|
|
20
|
+
.@"enum" => @enumFromInt(result),
|
|
21
|
+
.int => @intCast(result),
|
|
22
|
+
else => unreachable,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/// Sets the value at index `i` to `val` in the array `a`.
|
|
27
|
+
pub fn set(a: T, i: usize, val: U) T {
|
|
28
|
+
assert(i < n);
|
|
29
|
+
const v: T = switch (@typeInfo(U)) {
|
|
30
|
+
.@"enum" => @intFromEnum(val),
|
|
31
|
+
.int => @intCast(val),
|
|
32
|
+
else => unreachable,
|
|
33
|
+
};
|
|
34
|
+
const shift = i * size;
|
|
35
|
+
const mask: T = @as(T, @intCast((1 << size) - 1)) << @intCast(shift);
|
|
36
|
+
const result: T = a & ~mask;
|
|
37
|
+
return result | (v << @intCast(shift));
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
test Array {
|
|
43
|
+
const A = Array(5, u8);
|
|
44
|
+
var a: A.T = 0;
|
|
45
|
+
try expectEqual(u40, @TypeOf(a));
|
|
46
|
+
|
|
47
|
+
a = A.set(a, 4, 241);
|
|
48
|
+
try expectEqual(@as(u8, 241), A.get(a, 4));
|
|
49
|
+
a = A.set(a, 4, 1);
|
|
50
|
+
try expectEqual(@as(u8, 1), A.get(a, 4));
|
|
51
|
+
|
|
52
|
+
for (0..5) |i| a = A.set(a, i, @intCast(i));
|
|
53
|
+
for (0..5) |i| try expectEqual(@as(u8, @intCast(i)), A.get(a, i));
|
|
54
|
+
|
|
55
|
+
const Optional = @import("optional.zig").Optional;
|
|
56
|
+
|
|
57
|
+
const B = Array(4, Optional(bool));
|
|
58
|
+
var b: B.T = 0;
|
|
59
|
+
try expectEqual(u8, @TypeOf(b));
|
|
60
|
+
|
|
61
|
+
b = B.set(b, 2, .true);
|
|
62
|
+
try expectEqual(Optional(bool).None, B.get(b, 1));
|
|
63
|
+
try expectEqual(Optional(bool).true, B.get(b, 2));
|
|
64
|
+
try expectEqual(@as(u8, 0b00100000), b);
|
|
65
|
+
b = B.set(b, 2, .None);
|
|
66
|
+
b = B.set(b, 0, .false);
|
|
67
|
+
try expectEqual(Optional(bool).None, B.get(b, 2));
|
|
68
|
+
try expectEqual(Optional(bool).false, B.get(b, 0));
|
|
69
|
+
try expectEqual(@as(u8, 0b0000001), b);
|
|
70
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// Stores state required by optional features to the engine that can be passed to a generation's
|
|
2
|
+
/// `Battle.update` function. All generations generically support protocol logging (enabled via
|
|
3
|
+
/// `-Dlog`), chance actions and probability tracking (enabled via `-Dchance`), and damage
|
|
4
|
+
/// computation and chance overrides (`-Dcalc`), though the concrete types for these features are
|
|
5
|
+
/// generation dependent.
|
|
6
|
+
pub fn Options(comptime Log: type, comptime Chance: type, comptime Calc: type) type {
|
|
7
|
+
return struct { log: Log, chance: Chance, calc: Calc };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/// Helper to create a battle `Options` object with the provided `log`, `chance`, and `calc`.
|
|
11
|
+
pub fn options(
|
|
12
|
+
log: anytype,
|
|
13
|
+
chance: anytype,
|
|
14
|
+
calc: anytype,
|
|
15
|
+
) Options(@TypeOf(log), @TypeOf(chance), @TypeOf(calc)) {
|
|
16
|
+
return .{ .log = log, .chance = chance, .calc = calc };
|
|
17
|
+
}
|
package/src/lib/common/data.zig
CHANGED
|
@@ -9,12 +9,12 @@ pub const Player = enum(u1) {
|
|
|
9
9
|
P2,
|
|
10
10
|
|
|
11
11
|
/// Returns a player's opponent.
|
|
12
|
-
pub
|
|
13
|
-
return @
|
|
12
|
+
pub fn foe(self: Player) Player {
|
|
13
|
+
return @enumFromInt(~@intFromEnum(self));
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/// Return's an identifier for the player's Pokémon at the one-indexed `id`.
|
|
17
|
-
pub
|
|
17
|
+
pub fn ident(self: Player, id: u3) ID {
|
|
18
18
|
assert(id > 0 and id <= 6);
|
|
19
19
|
return .{ .id = id, .player = self };
|
|
20
20
|
}
|
|
@@ -22,42 +22,38 @@ pub const Player = enum(u1) {
|
|
|
22
22
|
|
|
23
23
|
test Player {
|
|
24
24
|
try expectEqual(Player.P2, Player.P1.foe());
|
|
25
|
-
try expectEqual(@as(u8, 0b0001), @
|
|
26
|
-
try expectEqual(@as(u8, 0b1101), @
|
|
25
|
+
try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(Player.P1.ident(1))));
|
|
26
|
+
try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(Player.P2.ident(5))));
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
/// An identifier for a specific Pokémon in battle.
|
|
30
|
-
pub const ID = packed struct {
|
|
30
|
+
pub const ID = packed struct(u8) {
|
|
31
31
|
/// The one-indexed team slot of the Pokémon
|
|
32
32
|
id: u3 = 0,
|
|
33
33
|
/// The Pokémon's trainer.
|
|
34
34
|
player: Player = .P1,
|
|
35
35
|
_: u4 = 0,
|
|
36
36
|
|
|
37
|
-
comptime {
|
|
38
|
-
assert(@sizeOf(ID) == 1);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
37
|
/// Converts the identifier into a number.
|
|
42
|
-
pub
|
|
43
|
-
return @intCast(
|
|
38
|
+
pub fn int(self: ID) u4 {
|
|
39
|
+
return @intCast(@as(u8, @bitCast(self)));
|
|
44
40
|
}
|
|
45
41
|
|
|
46
42
|
/// Decodes the identifier from a number.
|
|
47
|
-
pub
|
|
48
|
-
return @bitCast(
|
|
43
|
+
pub fn from(id: u4) ID {
|
|
44
|
+
return @bitCast(@as(u8, id));
|
|
49
45
|
}
|
|
50
46
|
};
|
|
51
47
|
|
|
52
48
|
test ID {
|
|
53
|
-
try expectEqual(@as(u8, 0b0001), @
|
|
54
|
-
try expectEqual(@as(u8, 0b1101), @
|
|
55
|
-
const id =
|
|
49
|
+
try expectEqual(@as(u8, 0b0001), @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
|
|
50
|
+
try expectEqual(@as(u8, 0b1101), @as(u8, @bitCast(ID{ .player = .P2, .id = 5 })));
|
|
51
|
+
const id: ID = .{ .player = .P2, .id = 4 };
|
|
56
52
|
try expectEqual(id, ID.from(id.int()));
|
|
57
53
|
}
|
|
58
54
|
|
|
59
55
|
/// A choice made by a player during battle.
|
|
60
|
-
pub const Choice = packed struct {
|
|
56
|
+
pub const Choice = packed struct(u8) {
|
|
61
57
|
/// The choice type.
|
|
62
58
|
type: Choice.Type = .Pass,
|
|
63
59
|
/// The choice data:
|
|
@@ -73,10 +69,6 @@ pub const Choice = packed struct {
|
|
|
73
69
|
Move,
|
|
74
70
|
Switch,
|
|
75
71
|
};
|
|
76
|
-
|
|
77
|
-
comptime {
|
|
78
|
-
assert(@sizeOf(Choice) == 1);
|
|
79
|
-
}
|
|
80
72
|
};
|
|
81
73
|
|
|
82
74
|
test Choice {
|
|
@@ -84,12 +76,12 @@ test Choice {
|
|
|
84
76
|
const p2: Choice = .{ .type = .Switch, .data = 5 };
|
|
85
77
|
try expectEqual(5, p2.data);
|
|
86
78
|
try expectEqual(Choice.Type.Move, p1.type);
|
|
87
|
-
try expectEqual(0b0001_0001, @
|
|
88
|
-
try expectEqual(0b0001_0110, @
|
|
79
|
+
try expectEqual(@as(u8, 0b0001_0001), @as(u8, @bitCast(p1)));
|
|
80
|
+
try expectEqual(@as(u8, 0b0001_0110), @as(u8, @bitCast(p2)));
|
|
89
81
|
}
|
|
90
82
|
|
|
91
83
|
/// The result of the battle - all results other than 'None' should be considered terminal.
|
|
92
|
-
pub const Result = packed struct {
|
|
84
|
+
pub const Result = packed struct(u8) {
|
|
93
85
|
/// The type of result from the perspective of Player 1.
|
|
94
86
|
/// `Error` is not possible when in Pokémon Showdown compatibility mode.
|
|
95
87
|
type: Result.Type = .None,
|
|
@@ -117,13 +109,9 @@ pub const Result = packed struct {
|
|
|
117
109
|
pub const Error: Result = .{ .type = .Error };
|
|
118
110
|
/// The default result.
|
|
119
111
|
pub const Default: Result = .{ .p1 = .Move, .p2 = .Move };
|
|
120
|
-
|
|
121
|
-
comptime {
|
|
122
|
-
assert(@sizeOf(Result) == 1);
|
|
123
|
-
}
|
|
124
112
|
};
|
|
125
113
|
|
|
126
114
|
test Result {
|
|
127
|
-
try expectEqual(0b0101_0000, @
|
|
128
|
-
try expectEqual(0b1000_0000, @
|
|
115
|
+
try expectEqual(@as(u8, 0b0101_0000), @as(u8, @bitCast(Result.Default)));
|
|
116
|
+
try expectEqual(@as(u8, 0b1000_0000), @as(u8, @bitCast(Result{ .p2 = .Switch })));
|
|
129
117
|
}
|
package/src/lib/common/debug.zig
CHANGED
|
@@ -1,65 +1,90 @@
|
|
|
1
|
+
const builtin = @import("builtin");
|
|
1
2
|
const std = @import("std");
|
|
2
3
|
|
|
3
4
|
const debug = std.debug;
|
|
4
|
-
const io = std.io;
|
|
5
5
|
|
|
6
6
|
pub fn print(value: anytype) void {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
if (builtin.target.os.tag == .freestanding) return;
|
|
8
|
+
|
|
9
|
+
var buffer: [4096]u8 = undefined;
|
|
10
|
+
const stderr = debug.lockStderr(&buffer);
|
|
11
|
+
defer debug.unlockStderr();
|
|
12
|
+
var fw = stderr.file_writer;
|
|
10
13
|
|
|
11
14
|
nosuspend {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
15
|
+
fw.interface.writeAll("\x1b[41m") catch return;
|
|
16
|
+
if (@TypeOf(@src()) == @TypeOf(value)) {
|
|
17
|
+
fw.interface.print("{s} ({s}:{d}:{d})", .{
|
|
18
|
+
value.fn_name,
|
|
19
|
+
value.file,
|
|
20
|
+
value.line,
|
|
21
|
+
value.column,
|
|
22
|
+
}) catch return;
|
|
23
|
+
} else {
|
|
24
|
+
switch (@typeInfo(@TypeOf(value))) {
|
|
25
|
+
.@"struct" => |info| {
|
|
26
|
+
if (info.is_tuple) {
|
|
27
|
+
if (@hasField(@TypeOf(info), "fields")) {
|
|
28
|
+
inline for (info.fields, 0..) |f, i| {
|
|
29
|
+
inspect(fw, @field(value, f.name));
|
|
30
|
+
if (i < info.fields.len - 1) {
|
|
31
|
+
fw.interface.writeAll(" ") catch return;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
} else {
|
|
35
|
+
inline for (info.field_names, 0..) |name, i| {
|
|
36
|
+
inspect(fw, @field(value, name));
|
|
37
|
+
if (i < info.field_names.len - 1) {
|
|
38
|
+
fw.interface.writeAll(" ") catch return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
inspect(fw, value);
|
|
19
44
|
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
},
|
|
24
|
-
else => inspect(value),
|
|
45
|
+
},
|
|
46
|
+
else => inspect(fw, value),
|
|
47
|
+
}
|
|
25
48
|
}
|
|
26
|
-
|
|
49
|
+
fw.interface.writeAll("\x1b[K\x1b[0m\n") catch return;
|
|
27
50
|
}
|
|
28
51
|
}
|
|
29
52
|
|
|
30
|
-
fn inspect(value: anytype) void {
|
|
31
|
-
const stderr = io.getStdErr().writer();
|
|
32
|
-
|
|
53
|
+
fn inspect(w: anytype, value: anytype) void {
|
|
33
54
|
nosuspend {
|
|
34
55
|
const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
|
|
35
56
|
switch (@typeInfo(@TypeOf(value))) {
|
|
36
|
-
.
|
|
37
|
-
if (info.child == u8) return
|
|
57
|
+
.array => |info| {
|
|
58
|
+
if (info.child == u8) return w.interface.print("{s}", .{value}) catch return;
|
|
38
59
|
@compileError(err);
|
|
39
60
|
},
|
|
40
|
-
.
|
|
41
|
-
.
|
|
42
|
-
.
|
|
43
|
-
if (info.child == u8)
|
|
61
|
+
.pointer => |ptr_info| switch (ptr_info.size) {
|
|
62
|
+
.one => switch (@typeInfo(ptr_info.child)) {
|
|
63
|
+
.array => |info| {
|
|
64
|
+
if (info.child == u8) {
|
|
65
|
+
return w.interface.print("{s}", .{value}) catch return;
|
|
66
|
+
}
|
|
44
67
|
@compileError(err);
|
|
45
68
|
},
|
|
46
|
-
|
|
69
|
+
.@"enum", .@"union", .@"struct" => return inspect(value.*),
|
|
47
70
|
else => @compileError(err),
|
|
48
71
|
},
|
|
49
|
-
.
|
|
72
|
+
.many, .c => {
|
|
50
73
|
if (ptr_info.sentinel) |_| return inspect(std.mem.span(value));
|
|
51
74
|
if (ptr_info.child == u8) {
|
|
52
|
-
return
|
|
75
|
+
return w.interface.print("{s}", .{std.mem.span(value)}) catch return;
|
|
53
76
|
}
|
|
54
77
|
@compileError(err);
|
|
55
78
|
},
|
|
56
|
-
.
|
|
57
|
-
if (ptr_info.child == u8)
|
|
79
|
+
.slice => {
|
|
80
|
+
if (ptr_info.child == u8) {
|
|
81
|
+
return w.interface.print("{s}", .{value}) catch return;
|
|
82
|
+
}
|
|
58
83
|
@compileError(err);
|
|
59
84
|
},
|
|
60
85
|
},
|
|
61
|
-
.
|
|
62
|
-
else =>
|
|
86
|
+
.optional => w.interface.print("{?}", .{value}) catch return,
|
|
87
|
+
else => w.interface.print("{}", .{value}) catch return,
|
|
63
88
|
}
|
|
64
89
|
}
|
|
65
90
|
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
const c = @import("napi");
|
|
3
|
+
|
|
4
|
+
const assert = std.debug.assert;
|
|
5
|
+
|
|
6
|
+
pub const Array = struct {
|
|
7
|
+
pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
|
|
8
|
+
var result: c.napi_value = undefined;
|
|
9
|
+
assert(c.napi_ok == if (o.length) |n|
|
|
10
|
+
c.napi_create_array_with_length(env, n, &result)
|
|
11
|
+
else
|
|
12
|
+
c.napi_create_array(env, &result));
|
|
13
|
+
return result;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
pub fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
|
|
17
|
+
assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
pub const Boolean = struct {
|
|
22
|
+
pub fn init(env: c.napi_env, value: bool) c.napi_value {
|
|
23
|
+
var result: c.napi_value = undefined;
|
|
24
|
+
assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
pub const Number = struct {
|
|
30
|
+
pub fn init(env: c.napi_env, value: anytype) c.napi_value {
|
|
31
|
+
const T = @TypeOf(value);
|
|
32
|
+
var result: c.napi_value = undefined;
|
|
33
|
+
assert(c.napi_ok == switch (@typeInfo(T)) {
|
|
34
|
+
.int => |info| switch (info.bits) {
|
|
35
|
+
0...32 => switch (info.signedness) {
|
|
36
|
+
.signed => c.napi_create_int32(env, @as(i32, value), &result),
|
|
37
|
+
.unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
|
|
38
|
+
},
|
|
39
|
+
33...52 => c.napi_create_int64(env, @as(i64, value), &result),
|
|
40
|
+
else => @compileError("int can't be represented as JS number"),
|
|
41
|
+
},
|
|
42
|
+
else => @compileError("expected number, got: " ++ @typeName(T)),
|
|
43
|
+
});
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
|
|
48
|
+
switch (@typeInfo(T)) {
|
|
49
|
+
.int => |info| switch (info.bits) {
|
|
50
|
+
0...32 => switch (info.signedness) {
|
|
51
|
+
.signed => {
|
|
52
|
+
var result: i32 = undefined;
|
|
53
|
+
assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
|
|
54
|
+
return if (info.bits == 32) result else @intCast(result);
|
|
55
|
+
},
|
|
56
|
+
.unsigned => {
|
|
57
|
+
var result: u32 = undefined;
|
|
58
|
+
assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
|
|
59
|
+
return if (info.bits == 32) result else @intCast(result);
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
33...63 => {
|
|
63
|
+
var result: i64 = undefined;
|
|
64
|
+
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
|
|
65
|
+
return @intCast(result);
|
|
66
|
+
},
|
|
67
|
+
else => {
|
|
68
|
+
var result: i64 = undefined;
|
|
69
|
+
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
|
|
70
|
+
return switch (info.signedness) {
|
|
71
|
+
.signed => value,
|
|
72
|
+
.unsigned => if (0 <= value) @intCast(value) else unreachable,
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
else => @compileError("expected number, got: " ++ @typeName(T)),
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
pub const Object = struct {
|
|
82
|
+
pub fn init(env: c.napi_env) c.napi_value {
|
|
83
|
+
var result: c.napi_value = undefined;
|
|
84
|
+
assert(c.napi_create_object(env, &result) == c.napi_ok);
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
pub const Property = union(enum) {
|
|
90
|
+
method: c.napi_callback,
|
|
91
|
+
value: c.napi_value,
|
|
92
|
+
|
|
93
|
+
pub fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
|
|
94
|
+
return .{
|
|
95
|
+
.utf8name = name,
|
|
96
|
+
.name = null,
|
|
97
|
+
.method = switch (property) {
|
|
98
|
+
.method => |m| m,
|
|
99
|
+
.value => null,
|
|
100
|
+
},
|
|
101
|
+
.getter = null,
|
|
102
|
+
.setter = null,
|
|
103
|
+
.value = switch (property) {
|
|
104
|
+
.method => null,
|
|
105
|
+
.value => |v| v,
|
|
106
|
+
},
|
|
107
|
+
.attributes = switch (property) {
|
|
108
|
+
.method => c.napi_default,
|
|
109
|
+
.value => c.napi_enumerable,
|
|
110
|
+
},
|
|
111
|
+
.data = null,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const expect = std.testing.expect;
|
|
4
|
+
const assert = std.debug.assert;
|
|
5
|
+
|
|
6
|
+
/// Optimized optional representation which stores the empty None value as a sentinel.
|
|
7
|
+
/// NOTE: ziglang/zig#104
|
|
8
|
+
pub fn Optional(comptime T: type) type {
|
|
9
|
+
const names = if (@hasDecl(std.meta, "fieldNames"))
|
|
10
|
+
std.meta.fieldNames(switch (@typeInfo(T)) {
|
|
11
|
+
.bool => enum { false, true },
|
|
12
|
+
else => T,
|
|
13
|
+
})
|
|
14
|
+
else blk: {
|
|
15
|
+
const fields = std.meta.fields(switch (@typeInfo(T)) {
|
|
16
|
+
.bool => enum { false, true },
|
|
17
|
+
else => T,
|
|
18
|
+
});
|
|
19
|
+
var ns: [fields.len][:0]const u8 = undefined;
|
|
20
|
+
inline for (fields, 0..) |f, i| ns[i] = f.name;
|
|
21
|
+
break :blk &ns;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const TagType = std.math.IntFittingRange(0, names.len);
|
|
25
|
+
var fieldNames: [names.len + 1][]const u8 = undefined;
|
|
26
|
+
var fieldValues: [names.len + 1]TagType = undefined;
|
|
27
|
+
|
|
28
|
+
fieldNames[0] = "None";
|
|
29
|
+
fieldValues[0] = 0;
|
|
30
|
+
|
|
31
|
+
inline for (names, 1..) |name, i| {
|
|
32
|
+
assert(!std.mem.eql(u8, name, "None"));
|
|
33
|
+
fieldNames[i] = name;
|
|
34
|
+
fieldValues[i] = i;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return @Enum(TagType, .exhaustive, &fieldNames, &fieldValues);
|
|
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
|
+
}
|