@pkmn/engine 0.1.0-dev.3ed876db → 0.1.0-dev.3f3af84c
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/README.md +5 -4
- package/build/pkg/addon/browser.js +4 -1
- package/build/pkg/addon/browser.js.map +1 -1
- package/build/pkg/addon/node.js +3 -0
- package/build/pkg/addon/node.js.map +1 -1
- package/build/pkg/addon/wasm.js +41 -9
- package/build/pkg/addon/wasm.js.map +1 -1
- package/build/pkg/addon.d.ts +5 -5
- package/build/pkg/addon.js +8 -17
- 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/gen1.d.ts +15 -18
- package/build/pkg/gen1.js +11 -12
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +37 -101
- package/build/pkg/index.js +6 -69
- package/build/pkg/index.js.map +1 -1
- package/build/tools/debug.js +0 -1
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/index.js +1 -0
- package/build/tools/display/index.js.map +1 -1
- package/build/tools/display/ui.jsx.map +1 -1
- package/build/tools/display/util.js +16 -1
- package/build/tools/display/util.js.map +1 -1
- package/build.zig +100 -27
- package/package.json +22 -123
- package/src/bin/install-pkmn-engine +3 -2
- package/src/lib/bindings/c.zig +255 -0
- package/src/lib/bindings/node.zig +121 -0
- package/src/lib/bindings/wasm.zig +59 -0
- package/src/lib/common/array.zig +1 -1
- package/src/lib/common/optional.zig +1 -1
- package/src/lib/common/util.zig +1 -1
- package/src/lib/gen1/calc.zig +8 -8
- package/src/lib/gen1/mechanics.zig +8 -3
- package/src/lib/gen2/mechanics.zig +19 -8
- package/src/lib/node.zig +2 -113
- package/src/lib/pkmn.zig +3 -2
- package/src/lib/wasm.zig +14 -9
package/src/lib/node.zig
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const js = @import("common/js.zig");
|
|
2
|
-
const
|
|
2
|
+
const node = @import("bindings/node.zig");
|
|
3
3
|
const std = @import("std");
|
|
4
4
|
|
|
5
5
|
const assert = std.debug.assert;
|
|
@@ -11,119 +11,8 @@ const c = @cImport({
|
|
|
11
11
|
|
|
12
12
|
export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
|
|
13
13
|
const properties = [_]c.napi_property_descriptor{
|
|
14
|
-
js.Property.init("engine", .{ .value = register(env) }),
|
|
14
|
+
js.Property.init("engine", .{ .value = node.register(env) }),
|
|
15
15
|
};
|
|
16
16
|
assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
|
|
17
17
|
return exports;
|
|
18
18
|
}
|
|
19
|
-
|
|
20
|
-
pub fn register(env: c.napi_env) c.napi_value {
|
|
21
|
-
const object = js.Object.init(env);
|
|
22
|
-
const properties = [_]c.napi_property_descriptor{
|
|
23
|
-
js.Property.init("options", .{ .value = options(env) }),
|
|
24
|
-
js.Property.init("bindings", .{ .value = bindings(env) }),
|
|
25
|
-
};
|
|
26
|
-
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
27
|
-
return object;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
fn options(env: c.napi_env) c.napi_value {
|
|
31
|
-
const object = js.Object.init(env);
|
|
32
|
-
const properties = [_]c.napi_property_descriptor{
|
|
33
|
-
js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
|
|
34
|
-
js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
|
|
35
|
-
js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
|
|
36
|
-
js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
|
|
37
|
-
};
|
|
38
|
-
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
39
|
-
return object;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
fn bindings(env: c.napi_env) c.napi_value {
|
|
43
|
-
const array = js.Array.init(env, .{ .length = 1 });
|
|
44
|
-
js.Array.set(env, array, 0, bind(env, pkmn.gen1));
|
|
45
|
-
return array;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
fn bind(env: c.napi_env, gen: anytype) c.napi_value {
|
|
49
|
-
const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
|
|
50
|
-
const logs_size: u32 = @intCast(gen.LOGS_SIZE);
|
|
51
|
-
const object = js.Object.init(env);
|
|
52
|
-
const properties = [_]c.napi_property_descriptor{
|
|
53
|
-
js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
|
|
54
|
-
js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
|
|
55
|
-
js.Property.init("update", .{ .method = update(gen) }),
|
|
56
|
-
js.Property.init("choices", .{ .method = choices(gen) }),
|
|
57
|
-
};
|
|
58
|
-
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
59
|
-
return object;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
fn update(gen: anytype) c.napi_callback {
|
|
63
|
-
return struct {
|
|
64
|
-
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
|
|
65
|
-
var argc: usize = 4;
|
|
66
|
-
var argv: [4]c.napi_value = undefined;
|
|
67
|
-
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
68
|
-
assert(argc == 4);
|
|
69
|
-
|
|
70
|
-
var data: ?*anyopaque = undefined;
|
|
71
|
-
var len: usize = 0;
|
|
72
|
-
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
73
|
-
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
74
|
-
|
|
75
|
-
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
76
|
-
const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
|
|
77
|
-
const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
|
|
78
|
-
|
|
79
|
-
var vtype: c.napi_valuetype = undefined;
|
|
80
|
-
assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
|
|
81
|
-
const result = switch (vtype) {
|
|
82
|
-
c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
|
|
83
|
-
else => result: {
|
|
84
|
-
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
85
|
-
assert(len == gen.LOGS_SIZE);
|
|
86
|
-
|
|
87
|
-
const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
|
|
88
|
-
var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
|
|
89
|
-
// TODO: extract out
|
|
90
|
-
var opts = pkmn.battle.options(
|
|
91
|
-
pkmn.protocol.FixedLog{ .writer = stream.writer() },
|
|
92
|
-
gen.chance.NULL,
|
|
93
|
-
gen.calc.NULL,
|
|
94
|
-
);
|
|
95
|
-
break :result battle.update(c1, c2, &opts);
|
|
96
|
-
},
|
|
97
|
-
} catch unreachable;
|
|
98
|
-
|
|
99
|
-
return js.Number.init(env, @as(u8, @bitCast(result)));
|
|
100
|
-
}
|
|
101
|
-
}.call;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
fn choices(gen: anytype) c.napi_callback {
|
|
105
|
-
return struct {
|
|
106
|
-
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
|
|
107
|
-
var argc: usize = 4;
|
|
108
|
-
var argv: [4]c.napi_value = undefined;
|
|
109
|
-
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
110
|
-
assert(argc == 4);
|
|
111
|
-
|
|
112
|
-
var data: ?*anyopaque = undefined;
|
|
113
|
-
var len: usize = 0;
|
|
114
|
-
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
115
|
-
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
116
|
-
|
|
117
|
-
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
118
|
-
const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
|
|
119
|
-
const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
|
|
120
|
-
|
|
121
|
-
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
122
|
-
assert(len == gen.CHOICES_SIZE);
|
|
123
|
-
|
|
124
|
-
const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
|
|
125
|
-
const n = battle.choices(player, request, out);
|
|
126
|
-
return js.Number.init(env, @as(u8, @bitCast(n)));
|
|
127
|
-
}
|
|
128
|
-
}.call;
|
|
129
|
-
}
|
package/src/lib/pkmn.zig
CHANGED
|
@@ -129,6 +129,7 @@ pub const gen2 = struct {
|
|
|
129
129
|
// NOTE: `pub usingnamespace struct { ... }` here results in a (false?) dependency loop
|
|
130
130
|
pub const js = if (options.internal) @import("common/js.zig") else {};
|
|
131
131
|
pub const bindings = if (options.internal) struct {
|
|
132
|
-
pub const
|
|
133
|
-
pub const
|
|
132
|
+
pub const c = @import("bindings/c.zig");
|
|
133
|
+
pub const node = @import("bindings/node.zig");
|
|
134
|
+
pub const wasm = @import("bindings/wasm.zig");
|
|
134
135
|
} else {};
|
package/src/lib/wasm.zig
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
const pkmn = @import("pkmn.zig");
|
|
2
1
|
const std = @import("std");
|
|
2
|
+
const wasm = @import("bindings/wasm.zig");
|
|
3
3
|
|
|
4
|
-
export const SHOWDOWN =
|
|
5
|
-
export const LOG =
|
|
6
|
-
export const CHANCE =
|
|
7
|
-
export const CALC =
|
|
4
|
+
export const SHOWDOWN = wasm.options.showdown;
|
|
5
|
+
export const LOG = wasm.options.log;
|
|
6
|
+
export const CHANCE = wasm.options.chance;
|
|
7
|
+
export const CALC = wasm.options.calc;
|
|
8
8
|
|
|
9
|
-
export const GEN1_CHOICES_SIZE =
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
export const GEN1_CHOICES_SIZE = wasm.gen(1).CHOICES_SIZE;
|
|
10
|
+
export const GEN1_LOGS_SIZE = wasm.gen(1).LOGS_SIZE;
|
|
11
|
+
|
|
12
|
+
const exportable = @hasDecl(std.zig, "Zir") and !@hasField(std.zig.Zir.Inst.Tag, "export_value");
|
|
13
|
+
|
|
14
|
+
usingnamespace if (exportable) struct {
|
|
15
|
+
export const GEN1_update = wasm.gen(1).update;
|
|
16
|
+
export const GEN1_choices = wasm.gen(1).choices;
|
|
17
|
+
} else wasm.exports();
|