@pkmn/engine 0.1.0-dev.d716e940 → 0.1.0-dev.da4a0515
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 +90 -56
- package/build/pkg/addon.js +9 -9
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +26 -4
- 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 +15 -18
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +2 -1
- 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 +2 -2
- 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 +154 -116
- package/package.json +53 -24
- package/src/bin/install-pkmn-engine +35 -22
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +19 -31
- package/src/lib/common/debug.zig +20 -11
- package/src/lib/common/js.zig +118 -0
- package/src/lib/common/optional.zig +67 -0
- package/src/lib/common/options.zig +9 -0
- package/src/lib/common/protocol.zig +906 -367
- package/src/lib/common/rational.zig +357 -0
- package/src/lib/common/rng.zig +35 -26
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/calc.zig +426 -0
- package/src/lib/gen1/chance.zig +1061 -0
- package/src/lib/gen1/data/moves.zig +202 -190
- package/src/lib/gen1/data/species.zig +5 -5
- package/src/lib/gen1/data/types.zig +10 -10
- package/src/lib/gen1/data.zig +99 -87
- package/src/lib/gen1/helpers.zig +599 -23
- package/src/lib/gen1/mechanics.zig +978 -710
- package/src/lib/gen2/calc.zig +143 -0
- package/src/lib/gen2/chance.zig +1315 -0
- package/src/lib/gen2/data/items.zig +14 -20
- package/src/lib/gen2/data/moves.zig +664 -570
- package/src/lib/gen2/data/species.zig +2 -2
- package/src/lib/gen2/data/types.zig +137 -34
- package/src/lib/gen2/data.zig +130 -92
- package/src/lib/gen2/generated.zig +806 -0
- package/src/lib/gen2/helpers.zig +16 -22
- package/src/lib/gen2/mechanics.zig +3591 -12
- package/src/lib/node.zig +131 -0
- package/src/lib/pkmn.zig +84 -2
- package/src/lib/wasm.zig +13 -0
- 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/lib/node.zig
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const pkmn = @import("pkmn.zig");
|
|
4
|
+
|
|
5
|
+
const js = @import("common/js.zig");
|
|
6
|
+
|
|
7
|
+
const assert = std.debug.assert;
|
|
8
|
+
|
|
9
|
+
const c = @cImport({
|
|
10
|
+
@cDefine("NAPI_VERSION", "8");
|
|
11
|
+
@cInclude("node_api.h");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
|
|
15
|
+
const properties = [_]c.napi_property_descriptor{
|
|
16
|
+
js.Property.init("engine", .{ .value = register(env) }),
|
|
17
|
+
};
|
|
18
|
+
assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
|
|
19
|
+
return exports;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
pub fn register(env: c.napi_env) c.napi_value {
|
|
23
|
+
const object = js.Object.init(env);
|
|
24
|
+
const properties = [_]c.napi_property_descriptor{
|
|
25
|
+
js.Property.init("options", .{ .value = options(env) }),
|
|
26
|
+
js.Property.init("bindings", .{ .value = bindings(env) }),
|
|
27
|
+
};
|
|
28
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
29
|
+
return object;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn options(env: c.napi_env) c.napi_value {
|
|
33
|
+
const object = js.Object.init(env);
|
|
34
|
+
const properties = [_]c.napi_property_descriptor{
|
|
35
|
+
js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
|
|
36
|
+
js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
|
|
37
|
+
js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
|
|
38
|
+
js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
|
|
39
|
+
};
|
|
40
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
41
|
+
return object;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
fn bindings(env: c.napi_env) c.napi_value {
|
|
45
|
+
const array = js.Array.init(env, .{ .length = 1 });
|
|
46
|
+
js.Array.set(env, array, 0, bind(env, pkmn.gen1));
|
|
47
|
+
return array;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
fn bind(env: c.napi_env, gen: anytype) c.napi_value {
|
|
51
|
+
const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
|
|
52
|
+
const logs_size: u32 = @intCast(gen.LOGS_SIZE);
|
|
53
|
+
const object = js.Object.init(env);
|
|
54
|
+
const properties = [_]c.napi_property_descriptor{
|
|
55
|
+
js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
|
|
56
|
+
js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
|
|
57
|
+
js.Property.init("update", .{ .method = update(gen) }),
|
|
58
|
+
js.Property.init("choices", .{ .method = choices(gen) }),
|
|
59
|
+
};
|
|
60
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
61
|
+
return object;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
fn update(gen: anytype) c.napi_callback {
|
|
65
|
+
return struct {
|
|
66
|
+
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
|
|
67
|
+
var argc: usize = 4;
|
|
68
|
+
var argv: [4]c.napi_value = undefined;
|
|
69
|
+
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
70
|
+
assert(argc == 4);
|
|
71
|
+
|
|
72
|
+
var data: ?*anyopaque = undefined;
|
|
73
|
+
var len: usize = 0;
|
|
74
|
+
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
75
|
+
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
76
|
+
|
|
77
|
+
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
78
|
+
const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
|
|
79
|
+
const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
|
|
80
|
+
|
|
81
|
+
var vtype: c.napi_valuetype = undefined;
|
|
82
|
+
assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
|
|
83
|
+
const result = switch (vtype) {
|
|
84
|
+
c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
|
|
85
|
+
else => result: {
|
|
86
|
+
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
87
|
+
assert(len == gen.LOGS_SIZE);
|
|
88
|
+
|
|
89
|
+
const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
|
|
90
|
+
var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
|
|
91
|
+
// TODO: extract out
|
|
92
|
+
var opts = pkmn.battle.options(
|
|
93
|
+
pkmn.protocol.FixedLog{ .writer = stream.writer() },
|
|
94
|
+
gen.chance.NULL,
|
|
95
|
+
gen.calc.NULL,
|
|
96
|
+
);
|
|
97
|
+
break :result battle.update(c1, c2, &opts);
|
|
98
|
+
},
|
|
99
|
+
} catch unreachable;
|
|
100
|
+
|
|
101
|
+
return js.Number.init(env, @as(u8, @bitCast(result)));
|
|
102
|
+
}
|
|
103
|
+
}.call;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
fn choices(gen: anytype) c.napi_callback {
|
|
107
|
+
return struct {
|
|
108
|
+
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
|
|
109
|
+
var argc: usize = 4;
|
|
110
|
+
var argv: [4]c.napi_value = undefined;
|
|
111
|
+
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
112
|
+
assert(argc == 4);
|
|
113
|
+
|
|
114
|
+
var data: ?*anyopaque = undefined;
|
|
115
|
+
var len: usize = 0;
|
|
116
|
+
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
117
|
+
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
118
|
+
|
|
119
|
+
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
120
|
+
const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
|
|
121
|
+
const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
|
|
122
|
+
|
|
123
|
+
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
124
|
+
assert(len == gen.CHOICES_SIZE);
|
|
125
|
+
|
|
126
|
+
const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
|
|
127
|
+
const n = battle.choices(player, request, out);
|
|
128
|
+
return js.Number.init(env, @as(u8, @bitCast(n)));
|
|
129
|
+
}
|
|
130
|
+
}.call;
|
|
131
|
+
}
|
package/src/lib/pkmn.zig
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
///
|
|
1
|
+
/// Configured Options for the pkmn package.
|
|
2
2
|
pub const options = @import("common/options.zig");
|
|
3
3
|
/// Configures the behavior of the pkmn package.
|
|
4
4
|
pub const Options = options.Options;
|
|
@@ -46,9 +46,91 @@ pub const protocol = if (options.internal) struct {
|
|
|
46
46
|
pub const NULL = @import("common/protocol.zig").NULL;
|
|
47
47
|
};
|
|
48
48
|
|
|
49
|
-
|
|
49
|
+
pub usingnamespace @import("common/rational.zig");
|
|
50
|
+
|
|
51
|
+
/// Namespace for cross-generation battle-related types.
|
|
52
|
+
pub const battle = @import("common/battle.zig");
|
|
53
|
+
|
|
54
|
+
/// Namespace for Generation I Pokémon.
|
|
50
55
|
pub const gen1 = struct {
|
|
51
56
|
pub usingnamespace @import("gen1/data.zig");
|
|
57
|
+
/// Tracks chance actions and their associated probability during a
|
|
58
|
+
/// Generation I battle update when `options.chance` is enabled.
|
|
59
|
+
pub const Chance = @import("gen1/chance.zig").Chance;
|
|
60
|
+
/// Namespace for types associated with tracking Generation I Pokémon chance outcomes.
|
|
61
|
+
pub const chance = struct {
|
|
62
|
+
/// Actions taken by a hypothetical "chance player" that convey
|
|
63
|
+
/// information about which RNG events were observed during a Generation
|
|
64
|
+
/// I battle `update`. This can additionally be provided as input to the
|
|
65
|
+
/// `update` call via the `Calc` when `options.calc` is enabled to
|
|
66
|
+
/// override the normal behavior of the RNG in order to force specific
|
|
67
|
+
/// outcomes.
|
|
68
|
+
pub const Actions = @import("gen1/chance.zig").Actions;
|
|
69
|
+
/// Information about the RNG that was observed during a Generation I
|
|
70
|
+
/// battle `update` for a single player.
|
|
71
|
+
pub const Action = @import("gen1/chance.zig").Action;
|
|
72
|
+
/// TODO
|
|
73
|
+
pub const Durations = @import("gen1/chance.zig").Durations;
|
|
74
|
+
/// TODO
|
|
75
|
+
pub const Duration = @import("gen1/chance.zig").Duration;
|
|
76
|
+
/// Null object pattern implementation of Generation I `Chance` which
|
|
77
|
+
/// does nothing, though chance tracking should additionally be turned
|
|
78
|
+
/// off entirely via `options.chance`.
|
|
79
|
+
pub const NULL = @import("gen1/chance.zig").NULL;
|
|
80
|
+
};
|
|
81
|
+
/// Allows for forcing the value of specific RNG events during a Generation I battle `update`
|
|
82
|
+
/// via `overrides` and tracks `summaries` of information relevant to damage calculation.
|
|
83
|
+
pub const Calc = @import("gen1/calc.zig").Calc;
|
|
84
|
+
/// Namespace for types associated with supported Generation I Pokémon damage calc features.
|
|
85
|
+
pub const calc = if (options.internal) struct {
|
|
86
|
+
pub usingnamespace @import("gen1/calc.zig");
|
|
87
|
+
} else struct {
|
|
88
|
+
/// TODO
|
|
89
|
+
pub const Overrides = @import("gen1/calc.zig").Overrides;
|
|
90
|
+
/// Information relevant to damage calculation that occured during a Generation I
|
|
91
|
+
/// battle `update`.
|
|
92
|
+
pub const Summaries = @import("gen1/calc.zig").Summaries;
|
|
93
|
+
/// Information relevant to damage calculation that occured during a Generation I
|
|
94
|
+
/// battle `update` for a single player.
|
|
95
|
+
pub const Summary = @import("gen1/calc.zig").Summary;
|
|
96
|
+
/// Null object pattern implementation of Generation I `Calc` which does nothing,
|
|
97
|
+
/// though damage calculator support should additionally be turned off
|
|
98
|
+
/// entirely via `options.calc`.
|
|
99
|
+
pub const NULL = @import("gen1/calc.zig").NULL;
|
|
100
|
+
};
|
|
52
101
|
/// Provides helpers for initializing Generation I Pokémon battles.
|
|
53
102
|
pub const helpers = @import("gen1/helpers.zig");
|
|
54
103
|
};
|
|
104
|
+
|
|
105
|
+
/// TODO
|
|
106
|
+
pub const gen2 = struct {
|
|
107
|
+
pub usingnamespace @import("gen2/data.zig");
|
|
108
|
+
pub const Chance = @import("gen2/chance.zig").Chance;
|
|
109
|
+
pub const chance = struct {
|
|
110
|
+
pub const Actions = @import("gen2/chance.zig").Actions;
|
|
111
|
+
pub const Action = @import("gen2/chance.zig").Action;
|
|
112
|
+
pub const Durations = @import("gen2/chance.zig").Durations;
|
|
113
|
+
pub const Duration = @import("gen2/chance.zig").Duration;
|
|
114
|
+
pub const NULL = @import("gen2/chance.zig").NULL;
|
|
115
|
+
};
|
|
116
|
+
pub const Calc = @import("gen2/calc.zig").Calc;
|
|
117
|
+
pub const calc = if (options.internal) struct {
|
|
118
|
+
pub usingnamespace @import("gen2/calc.zig");
|
|
119
|
+
} else struct {
|
|
120
|
+
pub const Overrides = @import("gen2/calc.zig").Overrides;
|
|
121
|
+
pub const Summaries = @import("gen2/calc.zig").Summaries;
|
|
122
|
+
pub const Summary = @import("gen2/calc.zig").Summary;
|
|
123
|
+
pub const NULL = @import("gen2/calc.zig").NULL;
|
|
124
|
+
};
|
|
125
|
+
pub const helpers = @import("gen2/helpers.zig");
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// Internal APIs used by other pkmn libraries, not actually part of the public API.
|
|
129
|
+
// NOTE: `pub usingnamespace struct { ... }` here results in a (false?) dependency loop
|
|
130
|
+
pub const js = if (options.internal) struct {
|
|
131
|
+
pub usingnamespace @import("common/js.zig");
|
|
132
|
+
} else struct {};
|
|
133
|
+
pub const bindings = if (options.internal) struct {
|
|
134
|
+
pub const node = @import("node.zig");
|
|
135
|
+
pub const wasm = @import("wasm.zig");
|
|
136
|
+
} else struct {};
|
package/src/lib/wasm.zig
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const pkmn = @import("pkmn.zig");
|
|
4
|
+
|
|
5
|
+
export const SHOWDOWN = pkmn.options.showdown;
|
|
6
|
+
export const LOG = pkmn.options.log;
|
|
7
|
+
export const CHANCE = pkmn.options.chance;
|
|
8
|
+
export const CALC = pkmn.options.calc;
|
|
9
|
+
|
|
10
|
+
export const GEN1_CHOICES_SIZE =
|
|
11
|
+
std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.CHOICES_SIZE))) catch unreachable;
|
|
12
|
+
export const GEN1_LOGS_SIZE =
|
|
13
|
+
std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.LOGS_SIZE))) catch unreachable;
|
package/src/lib/binding/node.zig
DELETED
|
@@ -1,229 +0,0 @@
|
|
|
1
|
-
const std = @import("std");
|
|
2
|
-
const pkmn = @import("../pkmn.zig");
|
|
3
|
-
|
|
4
|
-
const assert = std.debug.assert;
|
|
5
|
-
|
|
6
|
-
const c = @cImport({
|
|
7
|
-
@cDefine("NAPI_VERSION", "8");
|
|
8
|
-
@cInclude("node_api.h");
|
|
9
|
-
});
|
|
10
|
-
|
|
11
|
-
export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
|
|
12
|
-
const properties = [_]c.napi_property_descriptor{
|
|
13
|
-
Property.init("options", .{ .value = options(env) }),
|
|
14
|
-
Property.init("bindings", .{ .value = bindings(env) }),
|
|
15
|
-
};
|
|
16
|
-
assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
|
|
17
|
-
return exports;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
fn options(env: c.napi_env) c.napi_value {
|
|
21
|
-
var object = Object.init(env);
|
|
22
|
-
const properties = [_]c.napi_property_descriptor{
|
|
23
|
-
Property.init("showdown", .{ .value = Boolean.init(env, pkmn.options.showdown) }),
|
|
24
|
-
Property.init("log", .{ .value = Boolean.init(env, pkmn.options.log) }),
|
|
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
|
-
var array = Array.init(env, .{ .length = 1 });
|
|
32
|
-
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 = @intCast(u32, gen.CHOICES_SIZE);
|
|
38
|
-
const logs_size = @intCast(u32, gen.LOGS_SIZE);
|
|
39
|
-
var object = Object.init(env);
|
|
40
|
-
const properties = [_]c.napi_property_descriptor{
|
|
41
|
-
Property.init("CHOICES_SIZE", .{ .value = Number.init(env, choices_size) }),
|
|
42
|
-
Property.init("LOGS_SIZE", .{ .value = Number.init(env, logs_size) }),
|
|
43
|
-
Property.init("update", .{ .method = update(gen) }),
|
|
44
|
-
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
|
-
assert(data != null);
|
|
63
|
-
|
|
64
|
-
var aligned = @alignCast(@alignOf(*gen.Battle(gen.PRNG)), data.?);
|
|
65
|
-
var battle = @ptrCast(*gen.Battle(gen.PRNG), aligned);
|
|
66
|
-
const c1 = @bitCast(pkmn.Choice, Number.get(env, argv[1], u8));
|
|
67
|
-
const c2 = @bitCast(pkmn.Choice, Number.get(env, argv[2], u8));
|
|
68
|
-
|
|
69
|
-
var vtype: c.napi_valuetype = undefined;
|
|
70
|
-
assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
|
|
71
|
-
const result = switch (vtype) {
|
|
72
|
-
c.napi_undefined, c.napi_null => battle.update(c1, c2, pkmn.protocol.NULL),
|
|
73
|
-
else => result: {
|
|
74
|
-
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
75
|
-
assert(len == gen.LOGS_SIZE);
|
|
76
|
-
assert(data != null);
|
|
77
|
-
|
|
78
|
-
var buf = @ptrCast([*]u8, data.?)[0..gen.LOGS_SIZE];
|
|
79
|
-
var stream = pkmn.protocol.ByteStream{ .buffer = buf };
|
|
80
|
-
var log = pkmn.protocol.FixedLog{ .writer = stream.writer() };
|
|
81
|
-
break :result battle.update(c1, c2, log);
|
|
82
|
-
},
|
|
83
|
-
} catch unreachable;
|
|
84
|
-
|
|
85
|
-
return Number.init(env, @bitCast(u8, result));
|
|
86
|
-
}
|
|
87
|
-
}.call;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
fn choices(gen: anytype) c.napi_callback {
|
|
91
|
-
return struct {
|
|
92
|
-
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
|
|
93
|
-
var argc: usize = 4;
|
|
94
|
-
var argv: [4]c.napi_value = undefined;
|
|
95
|
-
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
96
|
-
assert(argc == 4);
|
|
97
|
-
|
|
98
|
-
var data: ?*anyopaque = undefined;
|
|
99
|
-
var len: usize = 0;
|
|
100
|
-
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
101
|
-
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
102
|
-
assert(data != null);
|
|
103
|
-
|
|
104
|
-
var aligned = @alignCast(@alignOf(*gen.Battle(gen.PRNG)), data.?);
|
|
105
|
-
var battle = @ptrCast(*gen.Battle(gen.PRNG), aligned);
|
|
106
|
-
|
|
107
|
-
const player = @intToEnum(pkmn.Player, Number.get(env, argv[1], u8));
|
|
108
|
-
const request = @intToEnum(pkmn.Choice.Type, Number.get(env, argv[2], u8));
|
|
109
|
-
|
|
110
|
-
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
111
|
-
assert(len == gen.CHOICES_SIZE);
|
|
112
|
-
assert(data != null);
|
|
113
|
-
|
|
114
|
-
var out = @ptrCast([*]pkmn.Choice, data.?)[0..gen.CHOICES_SIZE];
|
|
115
|
-
const n = battle.choices(player, request, out);
|
|
116
|
-
return Number.init(env, @bitCast(u8, n));
|
|
117
|
-
}
|
|
118
|
-
}.call;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const Array = struct {
|
|
122
|
-
fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
|
|
123
|
-
var result: c.napi_value = undefined;
|
|
124
|
-
assert(c.napi_ok == if (o.length) |n|
|
|
125
|
-
c.napi_create_array_with_length(env, n, &result)
|
|
126
|
-
else
|
|
127
|
-
c.napi_create_array(env, &result));
|
|
128
|
-
return result;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
|
|
132
|
-
assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const Boolean = struct {
|
|
137
|
-
fn init(env: c.napi_env, value: bool) c.napi_value {
|
|
138
|
-
var result: c.napi_value = undefined;
|
|
139
|
-
assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
|
|
140
|
-
return result;
|
|
141
|
-
}
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
const Number = struct {
|
|
145
|
-
fn init(env: c.napi_env, value: anytype) c.napi_value {
|
|
146
|
-
const T = @TypeOf(value);
|
|
147
|
-
var result: c.napi_value = undefined;
|
|
148
|
-
assert(c.napi_ok == switch (@typeInfo(T)) {
|
|
149
|
-
.Int => |info| switch (info.bits) {
|
|
150
|
-
0...32 => switch (info.signedness) {
|
|
151
|
-
.signed => c.napi_create_int32(env, @as(i32, value), &result),
|
|
152
|
-
.unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
|
|
153
|
-
},
|
|
154
|
-
33...52 => c.napi_create_int64(env, @as(i64, value), &result),
|
|
155
|
-
else => @compileError("int can't be represented as JS number"),
|
|
156
|
-
},
|
|
157
|
-
else => @compileError("expected number, got: " ++ @typeName(T)),
|
|
158
|
-
});
|
|
159
|
-
return result;
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
|
|
163
|
-
switch (@typeInfo(T)) {
|
|
164
|
-
.Int => |info| switch (info.bits) {
|
|
165
|
-
0...32 => switch (info.signedness) {
|
|
166
|
-
.signed => {
|
|
167
|
-
var result: i32 = undefined;
|
|
168
|
-
assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
|
|
169
|
-
return if (info.bits == 32) result else @intCast(T, result);
|
|
170
|
-
},
|
|
171
|
-
.unsigned => {
|
|
172
|
-
var result: u32 = undefined;
|
|
173
|
-
assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
|
|
174
|
-
return if (info.bits == 32) result else @intCast(T, result);
|
|
175
|
-
},
|
|
176
|
-
},
|
|
177
|
-
33...63 => {
|
|
178
|
-
var result: i64 = undefined;
|
|
179
|
-
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
|
|
180
|
-
return @intCast(T, result);
|
|
181
|
-
},
|
|
182
|
-
else => {
|
|
183
|
-
var result: i64 = undefined;
|
|
184
|
-
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
|
|
185
|
-
return switch (info.signedness) {
|
|
186
|
-
.signed => @as(T, value),
|
|
187
|
-
.unsigned => if (0 <= value) @intCast(T, value) else unreachable,
|
|
188
|
-
};
|
|
189
|
-
},
|
|
190
|
-
},
|
|
191
|
-
else => @compileError("expected number, got: " ++ @typeName(T)),
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
const Object = struct {
|
|
197
|
-
fn init(env: c.napi_env) c.napi_value {
|
|
198
|
-
var result: c.napi_value = undefined;
|
|
199
|
-
assert(c.napi_create_object(env, &result) == c.napi_ok);
|
|
200
|
-
return result;
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const Property = union(enum) {
|
|
205
|
-
method: c.napi_callback,
|
|
206
|
-
value: c.napi_value,
|
|
207
|
-
|
|
208
|
-
fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
|
|
209
|
-
return .{
|
|
210
|
-
.utf8name = name,
|
|
211
|
-
.name = null,
|
|
212
|
-
.method = switch (property) {
|
|
213
|
-
.method => |m| m,
|
|
214
|
-
.value => null,
|
|
215
|
-
},
|
|
216
|
-
.getter = null,
|
|
217
|
-
.setter = null,
|
|
218
|
-
.value = switch (property) {
|
|
219
|
-
.method => null,
|
|
220
|
-
.value => |v| v,
|
|
221
|
-
},
|
|
222
|
-
.attributes = switch (property) {
|
|
223
|
-
.method => c.napi_default,
|
|
224
|
-
.value => c.napi_enumerable,
|
|
225
|
-
},
|
|
226
|
-
.data = null,
|
|
227
|
-
};
|
|
228
|
-
}
|
|
229
|
-
};
|
package/src/lib/binding/wasm.zig
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
const std = @import("std");
|
|
2
|
-
const pkmn = @import("../pkmn.zig");
|
|
3
|
-
|
|
4
|
-
export const SHOWDOWN = pkmn.options.showdown;
|
|
5
|
-
export const LOG = pkmn.options.log;
|
|
6
|
-
|
|
7
|
-
export const GEN1_CHOICES_SIZE =
|
|
8
|
-
std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.CHOICES_SIZE)) catch unreachable;
|
|
9
|
-
export const GEN1_LOGS_SIZE =
|
|
10
|
-
std.math.ceilPowerOfTwo(u32, @intCast(u32, pkmn.gen1.LOGS_SIZE)) catch unreachable;
|