@pkmn/engine 0.1.0-dev.b9fb6eab → 0.1.0-dev.bb8e286d
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 +101 -62
- package/build/pkg/addon.d.ts +7 -0
- package/build/pkg/addon.js +112 -0
- package/build/pkg/addon.js.map +1 -0
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +27 -5
- package/build/pkg/data/protocol.json +69 -12
- package/build/pkg/data.d.ts +4 -4
- package/build/pkg/data.js +3 -5
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +5 -4
- package/build/pkg/gen1.js +49 -33
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +17 -13
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +4 -2
- package/build/pkg/protocol.js +61 -40
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +400 -0
- package/build/test/display.js.map +1 -0
- package/build/tools/debug.js +135 -0
- package/build/tools/debug.js.map +1 -0
- package/build.zig +130 -94
- package/package.json +75 -42
- package/src/bin/install-pkmn-engine +11 -6
- package/src/bin/pkmn-debug +62 -0
- package/src/lib/binding/node.zig +47 -146
- package/src/lib/binding/wasm.zig +6 -4
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +19 -31
- package/src/lib/common/debug.zig +21 -11
- package/src/lib/common/js.zig +118 -0
- package/src/lib/common/optional.zig +67 -0
- package/src/lib/common/options.zig +12 -3
- package/src/lib/common/protocol.zig +931 -387
- package/src/lib/common/rational.zig +357 -0
- package/src/lib/common/rng.zig +24 -15
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/README.md +295 -181
- package/src/lib/gen1/calc.zig +426 -0
- package/src/lib/gen1/chance.zig +1060 -0
- package/src/lib/gen1/data/moves.zig +202 -190
- package/src/lib/gen1/data/species.zig +4 -4
- package/src/lib/gen1/data/types.zig +10 -10
- package/src/lib/gen1/data.zig +90 -76
- package/src/lib/gen1/helpers.zig +577 -32
- package/src/lib/gen1/mechanics.zig +1143 -828
- package/src/lib/gen2/README.md +105 -85
- package/src/lib/gen2/calc.zig +143 -0
- package/src/lib/gen2/chance.zig +1314 -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 +128 -90
- package/src/lib/gen2/generated.zig +806 -0
- package/src/lib/gen2/helpers.zig +16 -22
- package/src/lib/gen2/mechanics.zig +3506 -12
- package/src/lib/pkmn.zig +68 -9
- package/src/test/display/pkmn.html.tmpl +368 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
try {
|
|
5
|
+
require('source-map-support').install();
|
|
6
|
+
} catch (err) {
|
|
7
|
+
if (err.code !== 'MODULE_NOT_FOUND') throw err;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const {execFileSync} = require('child_process');
|
|
11
|
+
const {readFileSync} = require('fs');
|
|
12
|
+
const {join, resolve} = require('path');
|
|
13
|
+
|
|
14
|
+
const ROOT = resolve(__dirname, '..', '..');
|
|
15
|
+
const packageJSON = JSON.parse(readFileSync(join(ROOT, 'package.json'), 'utf8'));
|
|
16
|
+
|
|
17
|
+
const missing = [];
|
|
18
|
+
for (const dep of ['@pkmn/img', 'html-minifier', 'mustache']) {
|
|
19
|
+
try {
|
|
20
|
+
require.resolve(dep);
|
|
21
|
+
} catch (e) {
|
|
22
|
+
if (e.code !== 'MODULE_NOT_FOUND') throw e;
|
|
23
|
+
missing.push(`${dep}@${packageJSON.devDependencies[dep]}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let Dex = undefined;
|
|
28
|
+
try {
|
|
29
|
+
Dex = require('@pkmn/sim').Dex;
|
|
30
|
+
} catch (err) {
|
|
31
|
+
if (err.code !== 'MODULE_NOT_FOUND') throw err;
|
|
32
|
+
try {
|
|
33
|
+
Dex = require('@pkmn/dex').Dex;
|
|
34
|
+
} catch (e) {
|
|
35
|
+
if (e.code !== 'MODULE_NOT_FOUND') throw e;
|
|
36
|
+
missing.push(`@pkmn/dex@${packageJSON.dependencies['@pkmn/data']}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (missing.length) {
|
|
41
|
+
if (+/^v(\d+)/.exec(process.version)[1] < 16) {
|
|
42
|
+
console.error('pkmn-debug requires Node v16+');
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
execFileSync('npm', ['install', ...missing, '--global'], {encoding: 'utf8'});
|
|
47
|
+
|
|
48
|
+
const node_modules = execFileSync('npm', ['root', '--global'], {encoding: 'utf8'}).trim();
|
|
49
|
+
if (!process.env.NODE_PATH || !process.env.NODE_PATH.includes(node_modules)) {
|
|
50
|
+
console.error(`NODE_PATH must include npm's global node_modules directory: '${node_modules}'`);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const {Generations} = require('@pkmn/data');
|
|
56
|
+
|
|
57
|
+
const {run} = require('../../build/tools/debug');
|
|
58
|
+
|
|
59
|
+
run(new Generations(Dex)).catch(err => {
|
|
60
|
+
console.error(err.message);
|
|
61
|
+
process.exit(1);
|
|
62
|
+
});
|
package/src/lib/binding/node.zig
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
2
|
const pkmn = @import("../pkmn.zig");
|
|
3
3
|
|
|
4
|
+
const js = @import("../common/js.zig");
|
|
5
|
+
|
|
4
6
|
const assert = std.debug.assert;
|
|
5
7
|
|
|
6
8
|
const c = @cImport({
|
|
@@ -10,38 +12,49 @@ const c = @cImport({
|
|
|
10
12
|
|
|
11
13
|
export fn napi_register_module_v1(env: c.napi_env, exports: c.napi_value) c.napi_value {
|
|
12
14
|
const properties = [_]c.napi_property_descriptor{
|
|
13
|
-
Property.init("
|
|
14
|
-
Property.init("bindings", .{ .value = bindings(env) }),
|
|
15
|
+
js.Property.init("engine", .{ .value = register(env) }),
|
|
15
16
|
};
|
|
16
17
|
assert(c.napi_define_properties(env, exports, properties.len, &properties) == c.napi_ok);
|
|
17
18
|
return exports;
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
pub fn register(env: c.napi_env) c.napi_value {
|
|
22
|
+
const object = js.Object.init(env);
|
|
23
|
+
const properties = [_]c.napi_property_descriptor{
|
|
24
|
+
js.Property.init("options", .{ .value = options(env) }),
|
|
25
|
+
js.Property.init("bindings", .{ .value = bindings(env) }),
|
|
26
|
+
};
|
|
27
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
28
|
+
return object;
|
|
29
|
+
}
|
|
30
|
+
|
|
20
31
|
fn options(env: c.napi_env) c.napi_value {
|
|
21
|
-
|
|
32
|
+
const object = js.Object.init(env);
|
|
22
33
|
const properties = [_]c.napi_property_descriptor{
|
|
23
|
-
Property.init("showdown", .{ .value = Boolean.init(env, pkmn.options.showdown) }),
|
|
24
|
-
Property.init("
|
|
34
|
+
js.Property.init("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
|
|
35
|
+
js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
|
|
36
|
+
js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
|
|
37
|
+
js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
|
|
25
38
|
};
|
|
26
39
|
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
27
40
|
return object;
|
|
28
41
|
}
|
|
29
42
|
|
|
30
43
|
fn bindings(env: c.napi_env) c.napi_value {
|
|
31
|
-
|
|
32
|
-
Array.set(env, array, 0, bind(env, pkmn.gen1));
|
|
44
|
+
const array = js.Array.init(env, .{ .length = 1 });
|
|
45
|
+
js.Array.set(env, array, 0, bind(env, pkmn.gen1));
|
|
33
46
|
return array;
|
|
34
47
|
}
|
|
35
48
|
|
|
36
49
|
fn bind(env: c.napi_env, gen: anytype) c.napi_value {
|
|
37
|
-
const
|
|
38
|
-
const logs_size = @intCast(
|
|
39
|
-
|
|
50
|
+
const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
|
|
51
|
+
const logs_size: u32 = @intCast(gen.LOGS_SIZE);
|
|
52
|
+
const object = js.Object.init(env);
|
|
40
53
|
const properties = [_]c.napi_property_descriptor{
|
|
41
|
-
Property.init("
|
|
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) }),
|
|
54
|
+
js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
|
|
55
|
+
js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
|
|
56
|
+
js.Property.init("update", .{ .method = update(gen) }),
|
|
57
|
+
js.Property.init("choices", .{ .method = choices(gen) }),
|
|
45
58
|
};
|
|
46
59
|
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
47
60
|
return object;
|
|
@@ -59,30 +72,32 @@ fn update(gen: anytype) c.napi_callback {
|
|
|
59
72
|
var len: usize = 0;
|
|
60
73
|
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
61
74
|
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
62
|
-
assert(data != null);
|
|
63
75
|
|
|
64
|
-
var
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
const c2 = @bitCast(pkmn.Choice, Number.get(env, argv[2], u8));
|
|
76
|
+
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
77
|
+
const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
|
|
78
|
+
const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
|
|
68
79
|
|
|
69
80
|
var vtype: c.napi_valuetype = undefined;
|
|
70
81
|
assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
|
|
71
82
|
const result = switch (vtype) {
|
|
72
|
-
c.napi_undefined, c.napi_null => battle.update(c1, c2,
|
|
83
|
+
c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
|
|
73
84
|
else => result: {
|
|
74
85
|
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
75
86
|
assert(len == gen.LOGS_SIZE);
|
|
76
|
-
assert(data != null);
|
|
77
87
|
|
|
78
|
-
|
|
79
|
-
var stream
|
|
80
|
-
|
|
81
|
-
|
|
88
|
+
const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
|
|
89
|
+
var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
|
|
90
|
+
// TODO: extract out
|
|
91
|
+
var opts = pkmn.battle.options(
|
|
92
|
+
pkmn.protocol.FixedLog{ .writer = stream.writer() },
|
|
93
|
+
gen.chance.NULL,
|
|
94
|
+
gen.calc.NULL,
|
|
95
|
+
);
|
|
96
|
+
break :result battle.update(c1, c2, &opts);
|
|
82
97
|
},
|
|
83
98
|
} catch unreachable;
|
|
84
99
|
|
|
85
|
-
return Number.init(env, @
|
|
100
|
+
return js.Number.init(env, @as(u8, @bitCast(result)));
|
|
86
101
|
}
|
|
87
102
|
}.call;
|
|
88
103
|
}
|
|
@@ -99,131 +114,17 @@ fn choices(gen: anytype) c.napi_callback {
|
|
|
99
114
|
var len: usize = 0;
|
|
100
115
|
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
101
116
|
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
102
|
-
assert(data != null);
|
|
103
117
|
|
|
104
|
-
var
|
|
105
|
-
|
|
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));
|
|
118
|
+
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
119
|
+
const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
|
|
120
|
+
const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
|
|
109
121
|
|
|
110
122
|
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
111
|
-
assert(len == gen.
|
|
112
|
-
assert(data != null);
|
|
123
|
+
assert(len == gen.CHOICES_SIZE);
|
|
113
124
|
|
|
114
|
-
|
|
125
|
+
const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
|
|
115
126
|
const n = battle.choices(player, request, out);
|
|
116
|
-
return Number.init(env, @
|
|
127
|
+
return js.Number.init(env, @as(u8, @bitCast(n)));
|
|
117
128
|
}
|
|
118
129
|
}.call;
|
|
119
130
|
}
|
|
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
CHANGED
|
@@ -2,9 +2,11 @@ const std = @import("std");
|
|
|
2
2
|
const pkmn = @import("../pkmn.zig");
|
|
3
3
|
|
|
4
4
|
export const SHOWDOWN = pkmn.options.showdown;
|
|
5
|
-
export const
|
|
5
|
+
export const LOG = pkmn.options.log;
|
|
6
|
+
export const CHANCE = pkmn.options.chance;
|
|
7
|
+
export const CALC = pkmn.options.calc;
|
|
6
8
|
|
|
7
|
-
export const
|
|
8
|
-
std.math.ceilPowerOfTwo(u32, @
|
|
9
|
+
export const GEN1_CHOICES_SIZE =
|
|
10
|
+
std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.CHOICES_SIZE))) catch unreachable;
|
|
9
11
|
export const GEN1_LOGS_SIZE =
|
|
10
|
-
std.math.ceilPowerOfTwo(u32, @
|
|
12
|
+
std.math.ceilPowerOfTwo(u32, @as(u32, @intCast(pkmn.gen1.LOGS_SIZE))) catch unreachable;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// Stores state required by optional features to the engine that can be passed to a generation's
|
|
2
|
+
/// `Battle.update` function. All generations generically support protocol logging (enabled via
|
|
3
|
+
/// `-Dlog`), chance actions and probability tracking (enabled via `-Dchance`), and damage
|
|
4
|
+
/// computation and chance overrides (`-Dcalc`), though the concrete types for these features are
|
|
5
|
+
/// generation dependent.
|
|
6
|
+
pub fn Options(comptime Log: type, comptime Chance: type, comptime Calc: type) type {
|
|
7
|
+
return struct { log: Log, chance: Chance, calc: Calc };
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/// Helper to create a battle `Options` object with the provided `log`, `chance`, and `calc`.
|
|
11
|
+
pub fn options(
|
|
12
|
+
log: anytype,
|
|
13
|
+
chance: anytype,
|
|
14
|
+
calc: anytype,
|
|
15
|
+
) Options(@TypeOf(log), @TypeOf(chance), @TypeOf(calc)) {
|
|
16
|
+
return .{ .log = log, .chance = chance, .calc = calc };
|
|
17
|
+
}
|
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
|
|
13
|
-
return @
|
|
12
|
+
pub fn foe(self: Player) Player {
|
|
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(@as(u8, 0b0001), @
|
|
26
|
-
try expectEqual(@as(u8, 0b1101), @
|
|
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.
|
|
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
|
|
43
|
-
return @intCast(
|
|
38
|
+
pub fn int(self: ID) u4 {
|
|
39
|
+
return @intCast(@as(u8, @bitCast(self)));
|
|
44
40
|
}
|
|
45
41
|
|
|
46
42
|
/// Decodes the identifier from a number.
|
|
47
|
-
pub
|
|
48
|
-
return @bitCast(
|
|
43
|
+
pub fn from(id: u4) ID {
|
|
44
|
+
return @bitCast(@as(u8, id));
|
|
49
45
|
}
|
|
50
46
|
};
|
|
51
47
|
|
|
52
48
|
test ID {
|
|
53
|
-
try expectEqual(@as(u8, 0b0001), @
|
|
54
|
-
try expectEqual(@as(u8, 0b1101), @
|
|
55
|
-
const id =
|
|
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
|
+
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 {
|
|
@@ -84,12 +76,12 @@ test Choice {
|
|
|
84
76
|
const p2: Choice = .{ .type = .Switch, .data = 5 };
|
|
85
77
|
try expectEqual(5, p2.data);
|
|
86
78
|
try expectEqual(Choice.Type.Move, p1.type);
|
|
87
|
-
try expectEqual(0b0001_0001, @
|
|
88
|
-
try expectEqual(0b0001_0110, @
|
|
79
|
+
try expectEqual(0b0001_0001, @as(u8, @bitCast(p1)));
|
|
80
|
+
try expectEqual(0b0001_0110, @as(u8, @bitCast(p2)));
|
|
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,13 +109,9 @@ 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 {
|
|
127
|
-
try expectEqual(0b0101_0000, @
|
|
128
|
-
try expectEqual(0b1000_0000, @
|
|
115
|
+
try expectEqual(0b0101_0000, @as(u8, @bitCast(Result.Default)));
|
|
116
|
+
try expectEqual(0b1000_0000, @as(u8, @bitCast(Result{ .p2 = .Switch })));
|
|
129
117
|
}
|
package/src/lib/common/debug.zig
CHANGED
|
@@ -10,18 +10,27 @@ pub fn print(value: anytype) void {
|
|
|
10
10
|
|
|
11
11
|
nosuspend {
|
|
12
12
|
stderr.writeAll("\x1b[41m") catch return;
|
|
13
|
-
|
|
14
|
-
.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
13
|
+
if (@TypeOf(@src()) == @TypeOf(value)) {
|
|
14
|
+
stderr.print("{s} ({s}:{d}:{d})", .{
|
|
15
|
+
value.fn_name,
|
|
16
|
+
value.file,
|
|
17
|
+
value.line,
|
|
18
|
+
value.column,
|
|
19
|
+
}) catch return;
|
|
20
|
+
} else {
|
|
21
|
+
switch (@typeInfo(@TypeOf(value))) {
|
|
22
|
+
.Struct => |info| {
|
|
23
|
+
if (info.is_tuple) {
|
|
24
|
+
inline for (info.fields, 0..) |f, i| {
|
|
25
|
+
inspect(@field(value, f.name));
|
|
26
|
+
if (i < info.fields.len - 1) stderr.writeAll(" ") catch return;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
inspect(value);
|
|
19
30
|
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
},
|
|
24
|
-
else => inspect(value),
|
|
31
|
+
},
|
|
32
|
+
else => inspect(value),
|
|
33
|
+
}
|
|
25
34
|
}
|
|
26
35
|
stderr.writeAll("\x1b[K\x1b[0m\n") catch return;
|
|
27
36
|
}
|
|
@@ -58,6 +67,7 @@ fn inspect(value: anytype) void {
|
|
|
58
67
|
@compileError(err);
|
|
59
68
|
},
|
|
60
69
|
},
|
|
70
|
+
.Optional => stderr.print("{?}", .{value}) catch return,
|
|
61
71
|
else => stderr.print("{}", .{value}) catch return,
|
|
62
72
|
}
|
|
63
73
|
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const assert = std.debug.assert;
|
|
4
|
+
|
|
5
|
+
const c = @cImport({
|
|
6
|
+
@cDefine("NAPI_VERSION", "8");
|
|
7
|
+
@cInclude("node_api.h");
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
pub const Array = struct {
|
|
11
|
+
pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
|
|
12
|
+
var result: c.napi_value = undefined;
|
|
13
|
+
assert(c.napi_ok == if (o.length) |n|
|
|
14
|
+
c.napi_create_array_with_length(env, n, &result)
|
|
15
|
+
else
|
|
16
|
+
c.napi_create_array(env, &result));
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
pub fn set(env: c.napi_env, array: c.napi_value, index: u32, value: c.napi_value) void {
|
|
21
|
+
assert(c.napi_set_element(env, array, index, value) == c.napi_ok);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
pub const Boolean = struct {
|
|
26
|
+
pub fn init(env: c.napi_env, value: bool) c.napi_value {
|
|
27
|
+
var result: c.napi_value = undefined;
|
|
28
|
+
assert(c.napi_get_boolean(env, value, &result) == c.napi_ok);
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
pub const Number = struct {
|
|
34
|
+
pub fn init(env: c.napi_env, value: anytype) c.napi_value {
|
|
35
|
+
const T = @TypeOf(value);
|
|
36
|
+
var result: c.napi_value = undefined;
|
|
37
|
+
assert(c.napi_ok == switch (@typeInfo(T)) {
|
|
38
|
+
.Int => |info| switch (info.bits) {
|
|
39
|
+
0...32 => switch (info.signedness) {
|
|
40
|
+
.signed => c.napi_create_int32(env, @as(i32, value), &result),
|
|
41
|
+
.unsigned => c.napi_create_uint32(env, @as(u32, value), &result),
|
|
42
|
+
},
|
|
43
|
+
33...52 => c.napi_create_int64(env, @as(i64, value), &result),
|
|
44
|
+
else => @compileError("int can't be represented as JS number"),
|
|
45
|
+
},
|
|
46
|
+
else => @compileError("expected number, got: " ++ @typeName(T)),
|
|
47
|
+
});
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pub fn get(env: c.napi_env, value: c.napi_value, comptime T: type) T {
|
|
52
|
+
switch (@typeInfo(T)) {
|
|
53
|
+
.Int => |info| switch (info.bits) {
|
|
54
|
+
0...32 => switch (info.signedness) {
|
|
55
|
+
.signed => {
|
|
56
|
+
var result: i32 = undefined;
|
|
57
|
+
assert(c.napi_get_value_int32(env, value, &result) == c.napi_ok);
|
|
58
|
+
return if (info.bits == 32) result else @intCast(result);
|
|
59
|
+
},
|
|
60
|
+
.unsigned => {
|
|
61
|
+
var result: u32 = undefined;
|
|
62
|
+
assert(c.napi_get_value_uint32(env, value, &result) == c.napi_ok);
|
|
63
|
+
return if (info.bits == 32) result else @intCast(result);
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
33...63 => {
|
|
67
|
+
var result: i64 = undefined;
|
|
68
|
+
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
|
|
69
|
+
return @intCast(result);
|
|
70
|
+
},
|
|
71
|
+
else => {
|
|
72
|
+
var result: i64 = undefined;
|
|
73
|
+
assert(c.napi_get_value_int64(env, value, &result) == c.napi_ok);
|
|
74
|
+
return switch (info.signedness) {
|
|
75
|
+
.signed => value,
|
|
76
|
+
.unsigned => if (0 <= value) @intCast(value) else unreachable,
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
else => @compileError("expected number, got: " ++ @typeName(T)),
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
pub const Object = struct {
|
|
86
|
+
pub fn init(env: c.napi_env) c.napi_value {
|
|
87
|
+
var result: c.napi_value = undefined;
|
|
88
|
+
assert(c.napi_create_object(env, &result) == c.napi_ok);
|
|
89
|
+
return result;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
pub const Property = union(enum) {
|
|
94
|
+
method: c.napi_callback,
|
|
95
|
+
value: c.napi_value,
|
|
96
|
+
|
|
97
|
+
pub fn init(comptime name: [:0]const u8, property: Property) c.napi_property_descriptor {
|
|
98
|
+
return .{
|
|
99
|
+
.utf8name = name,
|
|
100
|
+
.name = null,
|
|
101
|
+
.method = switch (property) {
|
|
102
|
+
.method => |m| m,
|
|
103
|
+
.value => null,
|
|
104
|
+
},
|
|
105
|
+
.getter = null,
|
|
106
|
+
.setter = null,
|
|
107
|
+
.value = switch (property) {
|
|
108
|
+
.method => null,
|
|
109
|
+
.value => |v| v,
|
|
110
|
+
},
|
|
111
|
+
.attributes = switch (property) {
|
|
112
|
+
.method => c.napi_default,
|
|
113
|
+
.value => c.napi_enumerable,
|
|
114
|
+
},
|
|
115
|
+
.data = null,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
};
|