@pkmn/engine 0.1.0-dev.47dcb76c → 0.1.0-dev.47f842bf
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 +65 -15
- 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 +25 -3
- package/build/pkg/addon.js +39 -71
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/layout.json +15 -2
- package/build/pkg/data/protocol.json +23 -9
- package/build/pkg/data.d.ts +25 -4
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +15 -18
- package/build/pkg/gen1.js +28 -19
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +40 -90
- package/build/pkg/index.js +23 -70
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +20 -1
- package/build/pkg/protocol.js +109 -107
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +53 -78
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +152 -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 +359 -0
- package/build/tools/display/ui.jsx.map +1 -0
- package/build/tools/display/util.js +33 -0
- package/build/tools/display/util.js.map +1 -0
- package/build.zig +309 -79
- package/package.json +55 -135
- package/src/bin/install-pkmn-engine +19 -8
- package/src/bin/pkmn-debug +1 -1
- package/src/lib/bindings/c.zig +255 -0
- package/src/lib/bindings/node.zig +117 -0
- package/src/lib/bindings/wasm.zig +59 -0
- package/src/lib/common/array.zig +77 -0
- package/src/lib/common/data.zig +8 -8
- package/src/lib/common/debug.zig +39 -8
- package/src/lib/common/js.zig +4 -6
- package/src/lib/common/napi.zig +4 -0
- package/src/lib/common/optional.zig +13 -9
- package/src/lib/common/options.zig +6 -0
- package/src/lib/common/protocol.zig +50 -44
- package/src/lib/common/rational.zig +406 -166
- package/src/lib/common/rng.zig +5 -7
- package/src/lib/common/util.zig +65 -1
- package/src/lib/gen1/calc.zig +695 -159
- package/src/lib/gen1/chance.zig +415 -290
- package/src/lib/gen1/data/moves.zig +2 -4
- package/src/lib/gen1/data/species.zig +1 -3
- package/src/lib/gen1/data/types.zig +2 -3
- package/src/lib/gen1/data.zig +38 -41
- package/src/lib/gen1/helpers.zig +92 -548
- package/src/lib/gen1/mechanics.zig +342 -245
- package/src/lib/gen2/calc.zig +37 -27
- package/src/lib/gen2/chance.zig +90 -502
- package/src/lib/gen2/data/items.zig +1 -3
- package/src/lib/gen2/data/moves.zig +2 -4
- package/src/lib/gen2/data/species.zig +1 -3
- package/src/lib/gen2/data/types.zig +7 -5
- package/src/lib/gen2/data.zig +28 -30
- package/src/lib/gen2/generated.zig +18 -23
- package/src/lib/gen2/helpers.zig +46 -25
- package/src/lib/gen2/mechanics.zig +199 -141
- package/src/lib/node.zig +4 -121
- package/src/lib/pkmn.zig +17 -18
- package/src/lib/wasm.zig +13 -9
- package/src/tools/display/dom.ts +93 -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/test/display/pkmn.html.tmpl +0 -368
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
const c = @import("../common/napi.zig");
|
|
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) = @alignCast(@ptrCast(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 stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
|
|
77
|
+
// TODO: extract out
|
|
78
|
+
var opts = pkmn.battle.options(
|
|
79
|
+
pkmn.protocol.FixedLog{ .writer = stream.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) = @alignCast(@ptrCast(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,59 @@
|
|
|
1
|
+
const pkmn = @import("../pkmn.zig");
|
|
2
|
+
const std = @import("std");
|
|
3
|
+
|
|
4
|
+
const assert = std.debug.assert;
|
|
5
|
+
|
|
6
|
+
const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
|
|
7
|
+
const Strong = if (@hasField(std.builtin.GlobalLinkage, "strong")) .strong else .Strong;
|
|
8
|
+
|
|
9
|
+
pub const options = pkmn.options;
|
|
10
|
+
|
|
11
|
+
pub fn gen(comptime num: comptime_int) type {
|
|
12
|
+
const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
|
|
13
|
+
return struct {
|
|
14
|
+
pub const CHOICES_SIZE = size(g.CHOICES_SIZE);
|
|
15
|
+
pub const LOGS_SIZE = size(g.LOGS_SIZE);
|
|
16
|
+
|
|
17
|
+
pub fn update(
|
|
18
|
+
battle: *g.Battle(g.PRNG),
|
|
19
|
+
c1: pkmn.Choice,
|
|
20
|
+
c2: pkmn.Choice,
|
|
21
|
+
options_: ?[*]u8,
|
|
22
|
+
) callconv(.C) pkmn.Result {
|
|
23
|
+
return (if (options_) |o| result: {
|
|
24
|
+
const buf = @as([*]u8, @ptrCast(o))[0..g.LOGS_SIZE];
|
|
25
|
+
var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
|
|
26
|
+
// TODO: extract out
|
|
27
|
+
var opts = pkmn.battle.options(
|
|
28
|
+
pkmn.protocol.FixedLog{ .writer = stream.writer() },
|
|
29
|
+
g.chance.NULL,
|
|
30
|
+
g.calc.NULL,
|
|
31
|
+
);
|
|
32
|
+
break :result battle.update(c1, c2, &opts);
|
|
33
|
+
} else battle.update(c1, c2, &g.NULL)) catch unreachable;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
pub fn choices(
|
|
37
|
+
battle: *g.Battle(g.PRNG),
|
|
38
|
+
player: u8,
|
|
39
|
+
request: u8,
|
|
40
|
+
buf: [*]u8,
|
|
41
|
+
) callconv(.C) u8 {
|
|
42
|
+
assert(player <= @field(@typeInfo(pkmn.Player), @tagName(Enum)).fields.len);
|
|
43
|
+
assert(request <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
|
|
44
|
+
|
|
45
|
+
const n = CHOICES_SIZE;
|
|
46
|
+
return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(buf[0..n]));
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pub fn exports() type {
|
|
52
|
+
@export(gen(1).update, .{ .name = "GEN1_update", .linkage = Strong });
|
|
53
|
+
@export(gen(1).choices, .{ .name = "GEN1_choices", .linkage = Strong });
|
|
54
|
+
return struct {};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
fn size(n: usize) u32 {
|
|
58
|
+
return std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(n))) catch unreachable;
|
|
59
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const expectEqual = std.testing.expectEqual;
|
|
4
|
+
const assert = std.debug.assert;
|
|
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
|
+
/// Helpers for working with bit-packed arrays of non-powers-of-2 types.
|
|
10
|
+
/// NOTE: ziglang/zig#12547
|
|
11
|
+
pub fn Array(comptime n: comptime_int, comptime U: type) type {
|
|
12
|
+
return struct {
|
|
13
|
+
const size = @bitSizeOf(U);
|
|
14
|
+
const options: std.builtin.Type.Int = .{ .signedness = .unsigned, .bits = size * n };
|
|
15
|
+
pub const T = @Type(if (@hasField(std.builtin.Type, "int"))
|
|
16
|
+
.{ .int = options }
|
|
17
|
+
else
|
|
18
|
+
.{ .Int = options });
|
|
19
|
+
|
|
20
|
+
/// Returns the value stored at index `i` in the array `a`.
|
|
21
|
+
pub fn get(a: T, i: usize) U {
|
|
22
|
+
assert(i < n);
|
|
23
|
+
const shift = i * size;
|
|
24
|
+
const mask: T = (1 << size) - 1;
|
|
25
|
+
const result = (a >> @intCast(shift)) & mask;
|
|
26
|
+
return switch (@typeInfo(U)) {
|
|
27
|
+
Enum => @enumFromInt(result),
|
|
28
|
+
Int => @intCast(result),
|
|
29
|
+
else => unreachable,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/// Sets the value at index `i` to `val` in the array `a`.
|
|
34
|
+
pub fn set(a: T, i: usize, val: U) T {
|
|
35
|
+
assert(i < n);
|
|
36
|
+
const v: T = switch (@typeInfo(U)) {
|
|
37
|
+
Enum => @intFromEnum(val),
|
|
38
|
+
Int => @intCast(val),
|
|
39
|
+
else => unreachable,
|
|
40
|
+
};
|
|
41
|
+
const shift = i * size;
|
|
42
|
+
const mask: T = @as(T, @intCast((1 << size) - 1)) << @intCast(shift);
|
|
43
|
+
const result: T = a & ~mask;
|
|
44
|
+
return result | (v << @intCast(shift));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
test Array {
|
|
50
|
+
const A = Array(5, u8);
|
|
51
|
+
var a: A.T = 0;
|
|
52
|
+
try expectEqual(u40, @TypeOf(a));
|
|
53
|
+
|
|
54
|
+
a = A.set(a, 4, 241);
|
|
55
|
+
try expectEqual(@as(u8, 241), A.get(a, 4));
|
|
56
|
+
a = A.set(a, 4, 1);
|
|
57
|
+
try expectEqual(@as(u8, 1), A.get(a, 4));
|
|
58
|
+
|
|
59
|
+
for (0..5) |i| a = A.set(a, i, @intCast(i));
|
|
60
|
+
for (0..5) |i| try expectEqual(@as(u8, @intCast(i)), A.get(a, i));
|
|
61
|
+
|
|
62
|
+
const Optional = @import("optional.zig").Optional;
|
|
63
|
+
|
|
64
|
+
const B = Array(4, Optional(bool));
|
|
65
|
+
var b: B.T = 0;
|
|
66
|
+
try expectEqual(u8, @TypeOf(b));
|
|
67
|
+
|
|
68
|
+
b = B.set(b, 2, .true);
|
|
69
|
+
try expectEqual(Optional(bool).None, B.get(b, 1));
|
|
70
|
+
try expectEqual(Optional(bool).true, B.get(b, 2));
|
|
71
|
+
try expectEqual(@as(u8, 0b00100000), b);
|
|
72
|
+
b = B.set(b, 2, .None);
|
|
73
|
+
b = B.set(b, 0, .false);
|
|
74
|
+
try expectEqual(Optional(bool).None, B.get(b, 2));
|
|
75
|
+
try expectEqual(Optional(bool).false, B.get(b, 0));
|
|
76
|
+
try expectEqual(@as(u8, 0b0000001), b);
|
|
77
|
+
}
|
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(0b0001, @as(u8, @bitCast(Player.P1.ident(1))));
|
|
26
|
-
try expectEqual(0b1101, @as(u8, @bitCast(Player.P2.ident(5))));
|
|
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.
|
|
@@ -46,8 +46,8 @@ pub const ID = packed struct(u8) {
|
|
|
46
46
|
};
|
|
47
47
|
|
|
48
48
|
test 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 })));
|
|
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
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(0b0001_0001, @as(u8, @bitCast(p1)));
|
|
80
|
-
try expectEqual(0b0001_0110, @as(u8, @bitCast(p2)));
|
|
79
|
+
try expectEqual(@as(u8, 0b0001_0001), @as(u8, @bitCast(p1)));
|
|
80
|
+
try expectEqual(@as(u8, 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(0b0101_0000, @as(u8, @bitCast(Result.Default)));
|
|
116
|
-
try expectEqual(0b1000_0000, @as(u8, @bitCast(Result{ .p2 = .Switch })));
|
|
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 })));
|
|
117
117
|
}
|
package/src/lib/common/debug.zig
CHANGED
|
@@ -3,9 +3,19 @@ const std = @import("std");
|
|
|
3
3
|
const debug = std.debug;
|
|
4
4
|
const io = std.io;
|
|
5
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
|
+
|
|
6
13
|
pub fn print(value: anytype) void {
|
|
7
|
-
debug.getStderrMutex().lock();
|
|
8
|
-
defer
|
|
14
|
+
if (@hasDecl(debug, "lockStdErr")) debug.lockStdErr() else debug.getStderrMutex().lock();
|
|
15
|
+
defer if (@hasDecl(debug, "unlockStdErr"))
|
|
16
|
+
debug.unlockStdErr()
|
|
17
|
+
else
|
|
18
|
+
debug.getStderrMutex().unlock();
|
|
9
19
|
const stderr = io.getStdErr().writer();
|
|
10
20
|
|
|
11
21
|
nosuspend {
|
|
@@ -19,7 +29,7 @@ pub fn print(value: anytype) void {
|
|
|
19
29
|
}) catch return;
|
|
20
30
|
} else {
|
|
21
31
|
switch (@typeInfo(@TypeOf(value))) {
|
|
22
|
-
|
|
32
|
+
Struct => |info| {
|
|
23
33
|
if (info.is_tuple) {
|
|
24
34
|
inline for (info.fields, 0..) |f, i| {
|
|
25
35
|
inspect(@field(value, f.name));
|
|
@@ -42,17 +52,17 @@ fn inspect(value: anytype) void {
|
|
|
42
52
|
nosuspend {
|
|
43
53
|
const err = "Unable to format type '" ++ @typeName(@TypeOf(value)) ++ "'";
|
|
44
54
|
switch (@typeInfo(@TypeOf(value))) {
|
|
45
|
-
|
|
55
|
+
Array => |info| {
|
|
46
56
|
if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
|
|
47
57
|
@compileError(err);
|
|
48
58
|
},
|
|
49
|
-
|
|
59
|
+
Pointer => |ptr_info| switch (ptr_info.size) {
|
|
50
60
|
.One => switch (@typeInfo(ptr_info.child)) {
|
|
51
|
-
|
|
61
|
+
Array => |info| {
|
|
52
62
|
if (info.child == u8) return stderr.print("{s}", .{value}) catch return;
|
|
53
63
|
@compileError(err);
|
|
54
64
|
},
|
|
55
|
-
|
|
65
|
+
Enum, Union, Struct => return inspect(value.*),
|
|
56
66
|
else => @compileError(err),
|
|
57
67
|
},
|
|
58
68
|
.Many, .C => {
|
|
@@ -67,8 +77,29 @@ fn inspect(value: anytype) void {
|
|
|
67
77
|
@compileError(err);
|
|
68
78
|
},
|
|
69
79
|
},
|
|
70
|
-
|
|
80
|
+
Optional => stderr.print("{?}", .{value}) catch return,
|
|
71
81
|
else => stderr.print("{}", .{value}) catch return,
|
|
72
82
|
}
|
|
73
83
|
}
|
|
74
84
|
}
|
|
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,11 +1,9 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
|
+
const c = @import("napi.zig");
|
|
2
3
|
|
|
3
4
|
const assert = std.debug.assert;
|
|
4
5
|
|
|
5
|
-
const
|
|
6
|
-
@cDefine("NAPI_VERSION", "8");
|
|
7
|
-
@cInclude("node_api.h");
|
|
8
|
-
});
|
|
6
|
+
const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
|
|
9
7
|
|
|
10
8
|
pub const Array = struct {
|
|
11
9
|
pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
|
|
@@ -35,7 +33,7 @@ pub const Number = struct {
|
|
|
35
33
|
const T = @TypeOf(value);
|
|
36
34
|
var result: c.napi_value = undefined;
|
|
37
35
|
assert(c.napi_ok == switch (@typeInfo(T)) {
|
|
38
|
-
|
|
36
|
+
Int => |info| switch (info.bits) {
|
|
39
37
|
0...32 => switch (info.signedness) {
|
|
40
38
|
.signed => c.napi_create_int32(env, @as(i32, value), &result),
|
|
41
39
|
.unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
|
|
@@ -50,7 +48,7 @@ pub const Number = struct {
|
|
|
50
48
|
|
|
51
49
|
pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
|
|
52
50
|
switch (@typeInfo(T)) {
|
|
53
|
-
|
|
51
|
+
Int => |info| switch (info.bits) {
|
|
54
52
|
0...32 => switch (info.signedness) {
|
|
55
53
|
.signed => {
|
|
56
54
|
var result: i32 = undefined;
|
|
@@ -3,10 +3,12 @@ const std = @import("std");
|
|
|
3
3
|
const expect = std.testing.expect;
|
|
4
4
|
const assert = std.debug.assert;
|
|
5
5
|
|
|
6
|
+
const Bool = if (@hasField(std.builtin.Type, "bool")) .bool else .Bool;
|
|
7
|
+
|
|
6
8
|
// TODO: ziglang/zig#104
|
|
7
9
|
pub fn Optional(comptime T: type) type {
|
|
8
10
|
const fields = std.meta.fields(switch (@typeInfo(T)) {
|
|
9
|
-
|
|
11
|
+
Bool => enum { false, true },
|
|
10
12
|
else => T,
|
|
11
13
|
});
|
|
12
14
|
|
|
@@ -23,14 +25,16 @@ pub fn Optional(comptime T: type) type {
|
|
|
23
25
|
};
|
|
24
26
|
}
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
const options: std.builtin.Type.Enum = .{
|
|
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 });
|
|
34
38
|
}
|
|
35
39
|
|
|
36
40
|
test Optional {
|
|
@@ -7,9 +7,12 @@ pub const Options = struct {
|
|
|
7
7
|
log: ?bool = null,
|
|
8
8
|
chance: ?bool = null,
|
|
9
9
|
calc: ?bool = null,
|
|
10
|
+
mod: ?bool = null,
|
|
10
11
|
advance: ?bool = null,
|
|
11
12
|
ebc: ?bool = null,
|
|
12
13
|
key: ?bool = null,
|
|
14
|
+
miss: ?bool = null,
|
|
15
|
+
overwrite: ?bool = null,
|
|
13
16
|
internal: ?bool = null,
|
|
14
17
|
};
|
|
15
18
|
|
|
@@ -22,9 +25,12 @@ pub const chance = get("chance", false);
|
|
|
22
25
|
/// Whether or not damage calculator support is enabled.
|
|
23
26
|
pub const calc = get("calc", false);
|
|
24
27
|
|
|
28
|
+
pub const mod = showdown or get("mod", false);
|
|
25
29
|
pub const advance = get("advance", true);
|
|
26
30
|
pub const ebc = get("ebc", true);
|
|
27
31
|
pub const key = get("key", false);
|
|
32
|
+
pub const miss = get("miss", true);
|
|
33
|
+
pub const overwrite = get("overwrite", true);
|
|
28
34
|
pub const internal = get("internal", false);
|
|
29
35
|
|
|
30
36
|
fn get(comptime name: []const u8, default: bool) bool {
|