@pkmn/engine 0.1.0-dev.c5fe6d07 → 0.1.0-dev.d1b39ff0
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 +44 -19
- package/build/pkg/addon.js +6 -7
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +2 -2
- package/build/pkg/data/protocol.json +69 -12
- package/build/pkg/data.d.ts +4 -4
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.js +2 -2
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +3 -2
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.js +61 -42
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +8 -3
- package/build/test/display.js.map +1 -1
- package/build/tools/debug.js +5 -4
- package/build/tools/debug.js.map +1 -1
- package/build.zig +105 -107
- package/package.json +43 -23
- package/src/bin/install-pkmn-engine +26 -18
- package/src/lib/common/data.zig +12 -24
- package/src/lib/common/debug.zig +2 -2
- package/src/lib/common/optional.zig +9 -2
- package/src/lib/common/options.zig +2 -0
- package/src/lib/common/protocol.zig +802 -327
- package/src/lib/common/rational.zig +120 -19
- package/src/lib/common/rng.zig +26 -17
- package/src/lib/gen1/calc.zig +507 -132
- package/src/lib/gen1/chance.zig +401 -286
- package/src/lib/gen1/data/moves.zig +192 -180
- package/src/lib/gen1/data/species.zig +3 -3
- package/src/lib/gen1/data/types.zig +7 -7
- package/src/lib/gen1/data.zig +50 -62
- package/src/lib/gen1/helpers.zig +89 -548
- package/src/lib/gen1/mechanics.zig +569 -514
- package/src/lib/gen2/calc.zig +135 -0
- package/src/lib/gen2/chance.zig +895 -0
- package/src/lib/gen2/data/items.zig +12 -18
- package/src/lib/gen2/data/moves.zig +663 -569
- package/src/lib/gen2/data/species.zig +1 -1
- package/src/lib/gen2/data/types.zig +134 -31
- package/src/lib/gen2/data.zig +106 -79
- package/src/lib/gen2/generated.zig +806 -0
- package/src/lib/gen2/helpers.zig +45 -19
- package/src/lib/gen2/mechanics.zig +3564 -9
- package/src/lib/{binding/node.zig → node.zig} +10 -13
- package/src/lib/pkmn.zig +32 -15
- package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
- package/src/test/display/pkmn.html.tmpl +5 -5
- package/src/lib/gen1/README.md +0 -892
- package/src/lib/gen2/README.md +0 -308
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
|
|
12
|
+
pub fn foe(self: Player) Player {
|
|
13
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(
|
|
26
|
-
try expectEqual(
|
|
25
|
+
try expectEqual(0b0001, @as(u8, @bitCast(Player.P1.ident(1))));
|
|
26
|
+
try expectEqual(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
|
|
38
|
+
pub fn int(self: ID) u4 {
|
|
43
39
|
return @intCast(@as(u8, @bitCast(self)));
|
|
44
40
|
}
|
|
45
41
|
|
|
46
42
|
/// Decodes the identifier from a number.
|
|
47
|
-
pub
|
|
43
|
+
pub fn from(id: u4) ID {
|
|
48
44
|
return @bitCast(@as(u8, id));
|
|
49
45
|
}
|
|
50
46
|
};
|
|
51
47
|
|
|
52
48
|
test ID {
|
|
53
|
-
try expectEqual(
|
|
54
|
-
try expectEqual(
|
|
55
|
-
const id =
|
|
49
|
+
try expectEqual(0b0001, @as(u8, @bitCast(ID{ .player = .P1, .id = 1 })));
|
|
50
|
+
try expectEqual(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 {
|
|
@@ -89,7 +81,7 @@ test Choice {
|
|
|
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,10 +109,6 @@ 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 {
|
package/src/lib/common/debug.zig
CHANGED
|
@@ -4,8 +4,8 @@ const debug = std.debug;
|
|
|
4
4
|
const io = std.io;
|
|
5
5
|
|
|
6
6
|
pub fn print(value: anytype) void {
|
|
7
|
-
debug.
|
|
8
|
-
defer debug.
|
|
7
|
+
debug.lockStdErr();
|
|
8
|
+
defer debug.unlockStdErr();
|
|
9
9
|
const stderr = io.getStdErr().writer();
|
|
10
10
|
|
|
11
11
|
nosuspend {
|
|
@@ -14,6 +14,7 @@ pub fn Optional(comptime T: type) type {
|
|
|
14
14
|
var decls = [_]std.builtin.Type.Declaration{};
|
|
15
15
|
|
|
16
16
|
enumFields[0] = .{ .name = "None", .value = 0 };
|
|
17
|
+
|
|
17
18
|
inline for (fields, 1..) |field, i| {
|
|
18
19
|
assert(!std.mem.eql(u8, field.name, "None"));
|
|
19
20
|
enumFields[i] = .{
|
|
@@ -24,7 +25,7 @@ pub fn Optional(comptime T: type) type {
|
|
|
24
25
|
|
|
25
26
|
return @Type(.{
|
|
26
27
|
.Enum = .{
|
|
27
|
-
.tag_type = std.math.IntFittingRange(0, fields.len
|
|
28
|
+
.tag_type = std.math.IntFittingRange(0, fields.len),
|
|
28
29
|
.fields = &enumFields,
|
|
29
30
|
.decls = &decls,
|
|
30
31
|
.is_exhaustive = true,
|
|
@@ -45,7 +46,7 @@ test Optional {
|
|
|
45
46
|
try expect(b != .true);
|
|
46
47
|
try expect(b != .false);
|
|
47
48
|
|
|
48
|
-
const Player = @import("
|
|
49
|
+
const Player = @import("data.zig").Player;
|
|
49
50
|
try expect(@bitSizeOf(Optional(Player)) == 2);
|
|
50
51
|
|
|
51
52
|
const p: Optional(Player) = .P2;
|
|
@@ -57,4 +58,10 @@ test Optional {
|
|
|
57
58
|
try expect(q == .None);
|
|
58
59
|
try expect(q != .P1);
|
|
59
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);
|
|
60
67
|
}
|
|
@@ -10,6 +10,7 @@ pub const Options = struct {
|
|
|
10
10
|
advance: ?bool = null,
|
|
11
11
|
ebc: ?bool = null,
|
|
12
12
|
key: ?bool = null,
|
|
13
|
+
miss: ?bool = null,
|
|
13
14
|
internal: ?bool = null,
|
|
14
15
|
};
|
|
15
16
|
|
|
@@ -25,6 +26,7 @@ pub const calc = get("calc", false);
|
|
|
25
26
|
pub const advance = get("advance", true);
|
|
26
27
|
pub const ebc = get("ebc", true);
|
|
27
28
|
pub const key = get("key", false);
|
|
29
|
+
pub const miss = get("miss", true);
|
|
28
30
|
pub const internal = get("internal", false);
|
|
29
31
|
|
|
30
32
|
fn get(comptime name: []const u8, default: bool) bool {
|