@pkmn/engine 0.1.0-dev.683fba11 → 0.1.0-dev.6b383c80
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 -1
- package/README.md +22 -16
- package/build/pkg/addon/browser.js +6 -3
- package/build/pkg/addon/browser.js.map +1 -1
- package/build/pkg/addon/node.js +23 -10
- package/build/pkg/addon/node.js.map +1 -1
- package/build/pkg/addon/wasm.js +23 -14
- package/build/pkg/addon/wasm.js.map +1 -1
- package/build/pkg/addon.d.ts +5 -5
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +2 -1
- package/build/pkg/gen1.js +22 -9
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +2 -0
- package/build/pkg/index.js +17 -7
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +11 -2
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +54 -11
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +19 -94
- package/build/tools/display/debug.jsx.map +1 -1
- package/build/tools/display/index.js +18 -7
- package/build/tools/display/index.js.map +1 -1
- package/build/tools/display/ui.jsx +20 -9
- package/build/tools/display/ui.jsx.map +1 -1
- package/build/tools/display/util.js +140 -1
- package/build/tools/display/util.js.map +1 -1
- package/build.zig +255 -313
- package/package.json +29 -131
- package/src/bin/install-pkmn-engine +24 -22
- 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 +9 -16
- package/src/lib/common/data.zig +8 -8
- package/src/lib/common/debug.zig +46 -61
- package/src/lib/common/js.zig +3 -9
- package/src/lib/common/optional.zig +20 -25
- package/src/lib/common/protocol.zig +224 -189
- package/src/lib/common/rational.zig +275 -284
- package/src/lib/common/rng.zig +4 -4
- package/src/lib/common/util.zig +34 -31
- package/src/lib/gen1/calc.zig +141 -119
- package/src/lib/gen1/chance.zig +162 -101
- package/src/lib/gen1/data/types.zig +2 -2
- package/src/lib/gen1/data.zig +27 -24
- package/src/lib/gen1/helpers.zig +36 -26
- package/src/lib/gen1/mechanics.zig +45 -49
- package/src/lib/gen2/calc.zig +3 -6
- package/src/lib/gen2/chance.zig +55 -43
- package/src/lib/gen2/data/types.zig +6 -3
- package/src/lib/gen2/data.zig +20 -16
- package/src/lib/gen2/helpers.zig +27 -19
- package/src/lib/gen2/mechanics.zig +17 -17
- package/src/lib/napi.h +2 -0
- package/src/lib/node.zig +3 -118
- package/src/lib/pkmn.zig +94 -19
- package/src/lib/wasm.zig +9 -44
- package/src/tools/display/dom.ts +43 -31
package/src/lib/common/array.zig
CHANGED
|
@@ -3,19 +3,12 @@ const std = @import("std");
|
|
|
3
3
|
const expectEqual = std.testing.expectEqual;
|
|
4
4
|
const assert = std.debug.assert;
|
|
5
5
|
|
|
6
|
-
const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
|
|
7
|
-
const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
|
|
8
|
-
|
|
9
6
|
/// Helpers for working with bit-packed arrays of non-powers-of-2 types.
|
|
10
7
|
/// NOTE: ziglang/zig#12547
|
|
11
8
|
pub fn Array(comptime n: comptime_int, comptime U: type) type {
|
|
12
9
|
return struct {
|
|
13
10
|
const size = @bitSizeOf(U);
|
|
14
|
-
const
|
|
15
|
-
pub const T = @Type(if (@hasField(std.builtin.Type, "int"))
|
|
16
|
-
.{ .int = options }
|
|
17
|
-
else
|
|
18
|
-
.{ .Int = options });
|
|
11
|
+
pub const T = @Int(.unsigned, size * n);
|
|
19
12
|
|
|
20
13
|
/// Returns the value stored at index `i` in the array `a`.
|
|
21
14
|
pub fn get(a: T, i: usize) U {
|
|
@@ -24,8 +17,8 @@ pub fn Array(comptime n: comptime_int, comptime U: type) type {
|
|
|
24
17
|
const mask: T = (1 << size) - 1;
|
|
25
18
|
const result = (a >> @intCast(shift)) & mask;
|
|
26
19
|
return switch (@typeInfo(U)) {
|
|
27
|
-
|
|
28
|
-
|
|
20
|
+
.@"enum" => @enumFromInt(result),
|
|
21
|
+
.int => @intCast(result),
|
|
29
22
|
else => unreachable,
|
|
30
23
|
};
|
|
31
24
|
}
|
|
@@ -34,8 +27,8 @@ pub fn Array(comptime n: comptime_int, comptime U: type) type {
|
|
|
34
27
|
pub fn set(a: T, i: usize, val: U) T {
|
|
35
28
|
assert(i < n);
|
|
36
29
|
const v: T = switch (@typeInfo(U)) {
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
.@"enum" => @intFromEnum(val),
|
|
31
|
+
.int => @intCast(val),
|
|
39
32
|
else => unreachable,
|
|
40
33
|
};
|
|
41
34
|
const shift = i * size;
|
|
@@ -52,9 +45,9 @@ test Array {
|
|
|
52
45
|
try expectEqual(u40, @TypeOf(a));
|
|
53
46
|
|
|
54
47
|
a = A.set(a, 4, 241);
|
|
55
|
-
try expectEqual(
|
|
48
|
+
try expectEqual(241, A.get(a, 4));
|
|
56
49
|
a = A.set(a, 4, 1);
|
|
57
|
-
try expectEqual(
|
|
50
|
+
try expectEqual(1, A.get(a, 4));
|
|
58
51
|
|
|
59
52
|
for (0..5) |i| a = A.set(a, i, @intCast(i));
|
|
60
53
|
for (0..5) |i| try expectEqual(@as(u8, @intCast(i)), A.get(a, i));
|
|
@@ -68,10 +61,10 @@ test Array {
|
|
|
68
61
|
b = B.set(b, 2, .true);
|
|
69
62
|
try expectEqual(Optional(bool).None, B.get(b, 1));
|
|
70
63
|
try expectEqual(Optional(bool).true, B.get(b, 2));
|
|
71
|
-
try expectEqual(
|
|
64
|
+
try expectEqual(0b00100000, b);
|
|
72
65
|
b = B.set(b, 2, .None);
|
|
73
66
|
b = B.set(b, 0, .false);
|
|
74
67
|
try expectEqual(Optional(bool).None, B.get(b, 2));
|
|
75
68
|
try expectEqual(Optional(bool).false, B.get(b, 0));
|
|
76
|
-
try expectEqual(
|
|
69
|
+
try expectEqual(0b0000001, b);
|
|
77
70
|
}
|
package/src/lib/common/data.zig
CHANGED
|
@@ -22,8 +22,8 @@ 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.
|
|
@@ -46,8 +46,8 @@ pub const ID = packed struct(u8) {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
test ID {
|
|
49
|
-
try expectEqual(
|
|
50
|
-
try expectEqual(
|
|
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
51
|
const id: ID = .{ .player = .P2, .id = 4 };
|
|
52
52
|
try expectEqual(id, ID.from(id.int()));
|
|
53
53
|
}
|
|
@@ -76,8 +76,8 @@ test Choice {
|
|
|
76
76
|
const p2: Choice = .{ .type = .Switch, .data = 5 };
|
|
77
77
|
try expectEqual(5, p2.data);
|
|
78
78
|
try expectEqual(Choice.Type.Move, p1.type);
|
|
79
|
-
try expectEqual(
|
|
80
|
-
try expectEqual(
|
|
79
|
+
try expectEqual(0b0001_0001, @as(u8, @bitCast(p1)));
|
|
80
|
+
try expectEqual(0b0001_0110, @as(u8, @bitCast(p2)));
|
|
81
81
|
}
|
|
82
82
|
|
|
83
83
|
/// The result of the battle - all results other than 'None' should be considered terminal.
|
|
@@ -112,6 +112,6 @@ pub const Result = packed struct(u8) {
|
|
|
112
112
|
};
|
|
113
113
|
|
|
114
114
|
test Result {
|
|
115
|
-
try expectEqual(
|
|
116
|
-
try expectEqual(
|
|
115
|
+
try expectEqual(0b0101_0000, @as(u8, @bitCast(Result.Default)));
|
|
116
|
+
try expectEqual(0b1000_0000, @as(u8, @bitCast(Result{ .p2 = .Switch })));
|
|
117
117
|
}
|
package/src/lib/common/debug.zig
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
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
|
-
|
|
6
|
-
const Array = if (@hasField(std.builtin.Type, "array")) .array else .Array;
|
|
7
|
-
const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
|
|
8
|
-
const Optional = if (@hasField(std.builtin.Type, "optional")) .optional else .Optional;
|
|
9
|
-
const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
|
|
10
|
-
const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
|
|
11
|
-
const Union = if (@hasField(std.builtin.Type, "union")) .@"union" else .Union;
|
|
12
5
|
|
|
13
6
|
pub fn print(value: anytype) void {
|
|
14
|
-
if (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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;
|
|
20
13
|
|
|
21
14
|
nosuspend {
|
|
22
|
-
|
|
15
|
+
fw.interface.writeAll("\x1b[41m") catch return;
|
|
23
16
|
if (@TypeOf(@src()) == @TypeOf(value)) {
|
|
24
|
-
|
|
17
|
+
fw.interface.print("{s} ({s}:{d}:{d})", .{
|
|
25
18
|
value.fn_name,
|
|
26
19
|
value.file,
|
|
27
20
|
value.line,
|
|
@@ -29,77 +22,69 @@ pub fn print(value: anytype) void {
|
|
|
29
22
|
}) catch return;
|
|
30
23
|
} else {
|
|
31
24
|
switch (@typeInfo(@TypeOf(value))) {
|
|
32
|
-
|
|
25
|
+
.@"struct" => |info| {
|
|
33
26
|
if (info.is_tuple) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
}
|
|
37
41
|
}
|
|
38
42
|
} else {
|
|
39
|
-
inspect(value);
|
|
43
|
+
inspect(fw, value);
|
|
40
44
|
}
|
|
41
45
|
},
|
|
42
|
-
else => inspect(value),
|
|
46
|
+
else => inspect(fw, value),
|
|
43
47
|
}
|
|
44
48
|
}
|
|
45
|
-
|
|
49
|
+
fw.interface.writeAll("\x1b[K\x1b[0m\n") catch return;
|
|
46
50
|
}
|
|
47
51
|
}
|
|
48
52
|
|
|
49
|
-
fn inspect(value: anytype) void {
|
|
50
|
-
const stderr = io.getStdErr().writer();
|
|
51
|
-
|
|
53
|
+
fn inspect(w: anytype, value: anytype) void {
|
|
52
54
|
nosuspend {
|
|
53
55
|
const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
|
|
54
56
|
switch (@typeInfo(@TypeOf(value))) {
|
|
55
|
-
|
|
56
|
-
if (info.child == u8) return
|
|
57
|
+
.array => |info| {
|
|
58
|
+
if (info.child == u8) return w.interface.print("{s}", .{value}) catch return;
|
|
57
59
|
@compileError(err);
|
|
58
60
|
},
|
|
59
|
-
|
|
60
|
-
.
|
|
61
|
-
|
|
62
|
-
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
|
+
}
|
|
63
67
|
@compileError(err);
|
|
64
68
|
},
|
|
65
|
-
|
|
69
|
+
.@"enum", .@"union", .@"struct" => return inspect(w, value.*),
|
|
66
70
|
else => @compileError(err),
|
|
67
71
|
},
|
|
68
|
-
.
|
|
69
|
-
if (ptr_info.
|
|
72
|
+
.many, .c => {
|
|
73
|
+
if (ptr_info.sentinel_ptr) |_| return inspect(w, std.mem.span(value));
|
|
70
74
|
if (ptr_info.child == u8) {
|
|
71
|
-
return
|
|
75
|
+
return w.interface.print("{s}", .{std.mem.span(value)}) catch return;
|
|
72
76
|
}
|
|
73
77
|
@compileError(err);
|
|
74
78
|
},
|
|
75
|
-
.
|
|
76
|
-
if (ptr_info.child == u8)
|
|
79
|
+
.slice => {
|
|
80
|
+
if (ptr_info.child == u8) {
|
|
81
|
+
return w.interface.print("{s}", .{value}) catch return;
|
|
82
|
+
}
|
|
77
83
|
@compileError(err);
|
|
78
84
|
},
|
|
79
85
|
},
|
|
80
|
-
|
|
81
|
-
else =>
|
|
86
|
+
.optional => w.interface.print("{?}", .{value}) catch return,
|
|
87
|
+
else => w.interface.print("{}", .{value}) catch return,
|
|
82
88
|
}
|
|
83
89
|
}
|
|
84
90
|
}
|
|
85
|
-
|
|
86
|
-
const showdown = @import("./options.zig").showdown;
|
|
87
|
-
const Result = @import("./data.zig").Result;
|
|
88
|
-
const Choice = @import("./data.zig").Choice;
|
|
89
|
-
|
|
90
|
-
pub fn dump(gen: u8, battle: anytype, frame: ?struct { Result, Choice, Choice }) void {
|
|
91
|
-
const file = std.fs.cwd().createFile("logs/dump.bin", .{}) catch return;
|
|
92
|
-
defer file.close();
|
|
93
|
-
var w = file.writer();
|
|
94
|
-
w.writeByte(@intFromBool(showdown)) catch return;
|
|
95
|
-
w.writeByte(gen) catch return;
|
|
96
|
-
w.writeInt(i16, 0, .little) catch return;
|
|
97
|
-
w.writeInt(i32, 0, .little) catch return;
|
|
98
|
-
w.writeStruct(battle) catch return;
|
|
99
|
-
w.writeStruct(battle) catch return;
|
|
100
|
-
if (frame) |f| {
|
|
101
|
-
w.writeStruct(f[0]) catch return;
|
|
102
|
-
w.writeStruct(f[1]) catch return;
|
|
103
|
-
w.writeStruct(f[2]) catch return;
|
|
104
|
-
}
|
|
105
|
-
}
|
package/src/lib/common/js.zig
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
|
+
const c = @import("napi");
|
|
2
3
|
|
|
3
4
|
const assert = std.debug.assert;
|
|
4
5
|
|
|
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
6
|
pub const Array = struct {
|
|
13
7
|
pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
|
|
14
8
|
var result: c.napi_value = undefined;
|
|
@@ -37,7 +31,7 @@ pub const Number = struct {
|
|
|
37
31
|
const T = @TypeOf(value);
|
|
38
32
|
var result: c.napi_value = undefined;
|
|
39
33
|
assert(c.napi_ok == switch (@typeInfo(T)) {
|
|
40
|
-
|
|
34
|
+
.int => |info| switch (info.bits) {
|
|
41
35
|
0...32 => switch (info.signedness) {
|
|
42
36
|
.signed => c.napi_create_int32(env, @as(i32, value), &result),
|
|
43
37
|
.unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
|
|
@@ -52,7 +46,7 @@ pub const Number = struct {
|
|
|
52
46
|
|
|
53
47
|
pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
|
|
54
48
|
switch (@typeInfo(T)) {
|
|
55
|
-
|
|
49
|
+
.int => |info| switch (info.bits) {
|
|
56
50
|
0...32 => switch (info.signedness) {
|
|
57
51
|
.signed => {
|
|
58
52
|
var result: i32 = undefined;
|
|
@@ -3,38 +3,33 @@ const std = @import("std");
|
|
|
3
3
|
const expect = std.testing.expect;
|
|
4
4
|
const assert = std.debug.assert;
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
// TODO: ziglang/zig#104
|
|
6
|
+
/// Optimized optional representation which stores the empty None value as a sentinel.
|
|
7
|
+
/// NOTE: ziglang/zig#104
|
|
9
8
|
pub fn Optional(comptime T: type) type {
|
|
10
|
-
const
|
|
11
|
-
|
|
9
|
+
const info = @typeInfo(switch (@typeInfo(T)) {
|
|
10
|
+
.bool => enum { false, true },
|
|
12
11
|
else => T,
|
|
13
|
-
});
|
|
12
|
+
}).@"enum";
|
|
13
|
+
const names = if (@hasField(@TypeOf(info), "fields")) blk: {
|
|
14
|
+
var ns: [info.fields.len][:0]const u8 = undefined;
|
|
15
|
+
inline for (info.fields, 0..) |f, i| ns[i] = f.name;
|
|
16
|
+
break :blk &ns;
|
|
17
|
+
} else info.field_names;
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
var
|
|
19
|
+
const TagType = std.math.IntFittingRange(0, names.len);
|
|
20
|
+
var fieldNames: [names.len + 1][]const u8 = undefined;
|
|
21
|
+
var fieldValues: [names.len + 1]TagType = undefined;
|
|
17
22
|
|
|
18
|
-
|
|
23
|
+
fieldNames[0] = "None";
|
|
24
|
+
fieldValues[0] = 0;
|
|
19
25
|
|
|
20
|
-
inline for (
|
|
21
|
-
assert(!std.mem.eql(u8,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
.value = i,
|
|
25
|
-
};
|
|
26
|
+
inline for (names, 1..) |name, i| {
|
|
27
|
+
assert(!std.mem.eql(u8, name, "None"));
|
|
28
|
+
fieldNames[i] = name;
|
|
29
|
+
fieldValues[i] = i;
|
|
26
30
|
}
|
|
27
31
|
|
|
28
|
-
|
|
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 });
|
|
32
|
+
return @Enum(TagType, .exhaustive, &fieldNames, &fieldValues);
|
|
38
33
|
}
|
|
39
34
|
|
|
40
35
|
test Optional {
|