@pkmn/engine 0.1.0-dev.a1fcc5d9 → 0.1.0-dev.a27b3ff3
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 +51 -5
- package/build/pkg/addon/browser.d.ts +1 -1
- package/build/pkg/addon/browser.js +6 -2
- package/build/pkg/addon/browser.js.map +1 -1
- package/build/pkg/addon/node.js +22 -8
- package/build/pkg/addon/node.js.map +1 -1
- 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 +24 -4
- 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 +28 -19
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +37 -100
- package/build/pkg/index.js +23 -75
- package/build/pkg/index.js.map +1 -1
- package/build/tools/debug.js +18 -9
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +18 -7
- package/build/tools/display/debug.jsx.map +1 -1
- package/build/tools/display/index.js +35 -15
- package/build/tools/display/index.js.map +1 -1
- package/build/tools/display/ui.jsx +17 -7
- 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 +198 -85
- package/package.json +29 -125
- package/src/bin/install-pkmn-engine +3 -2
- 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 +1 -1
- package/src/lib/common/js.zig +1 -5
- package/src/lib/common/napi.zig +4 -0
- package/src/lib/common/optional.zig +1 -1
- package/src/lib/common/options.zig +2 -0
- package/src/lib/common/protocol.zig +4 -8
- package/src/lib/common/rational.zig +281 -270
- package/src/lib/common/rng.zig +1 -3
- package/src/lib/common/util.zig +17 -2
- package/src/lib/gen1/calc.zig +52 -69
- package/src/lib/gen1/chance.zig +34 -18
- 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 +7 -11
- package/src/lib/gen1/helpers.zig +4 -10
- package/src/lib/gen1/mechanics.zig +76 -51
- package/src/lib/gen2/calc.zig +10 -18
- 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 +14 -17
- package/src/lib/gen2/generated.zig +18 -23
- package/src/lib/gen2/helpers.zig +5 -12
- package/src/lib/gen2/mechanics.zig +42 -39
- package/src/lib/node.zig +4 -121
- package/src/lib/pkmn.zig +7 -2
- package/src/lib/wasm.zig +13 -9
- package/src/tools/display/dom.ts +5 -8
- package/src/tools/display/ui.css +0 -5
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
const pkmn = @import("../pkmn.zig");
|
|
2
|
+
const std = @import("std");
|
|
3
|
+
|
|
4
|
+
const assert = std.debug.assert;
|
|
5
|
+
|
|
6
|
+
const ERROR: u8 = 0b1100;
|
|
7
|
+
|
|
8
|
+
const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
|
|
9
|
+
const Strong = if (@hasField(std.builtin.GlobalLinkage, "strong")) .strong else .Strong;
|
|
10
|
+
|
|
11
|
+
pub const OPTIONS: extern struct {
|
|
12
|
+
showdown: bool,
|
|
13
|
+
log: bool,
|
|
14
|
+
chance: bool,
|
|
15
|
+
calc: bool,
|
|
16
|
+
} = .{
|
|
17
|
+
.showdown = pkmn.options.showdown,
|
|
18
|
+
.log = pkmn.options.log,
|
|
19
|
+
.chance = pkmn.options.chance,
|
|
20
|
+
.calc = pkmn.options.calc,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
pub const MAX_CHOICES = pkmn.MAX_CHOICES;
|
|
24
|
+
pub const CHOICES_SIZE = pkmn.CHOICES_SIZE;
|
|
25
|
+
pub const MAX_LOGS = pkmn.MAX_LOGS;
|
|
26
|
+
pub const LOGS_SIZE = pkmn.LOGS_SIZE;
|
|
27
|
+
|
|
28
|
+
pub fn choice_init(choice: u8, data: u8) callconv(.C) u8 {
|
|
29
|
+
assert(choice <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
|
|
30
|
+
assert(data <= 6);
|
|
31
|
+
return @bitCast(pkmn.Choice{ .type = @enumFromInt(choice), .data = @intCast(data) });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
pub fn choice_type(choice: u8) callconv(.C) u8 {
|
|
35
|
+
return @intFromEnum(@as(pkmn.Choice, @bitCast(choice)).type);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
pub fn choice_data(choice: u8) callconv(.C) u8 {
|
|
39
|
+
return @as(u8, @as(pkmn.Choice, @bitCast(choice)).data);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
pub fn result_type(result: u8) callconv(.C) u8 {
|
|
43
|
+
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).type);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
pub fn result_p1(result: u8) callconv(.C) u8 {
|
|
47
|
+
assert(!err(result));
|
|
48
|
+
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
pub fn result_p2(result: u8) callconv(.C) u8 {
|
|
52
|
+
assert(!err(result));
|
|
53
|
+
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p2);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
pub fn err(result: u8) callconv(.C) bool {
|
|
57
|
+
return result == ERROR;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
pub fn psrng_init(prng: *pkmn.PSRNG, seed: u64) callconv(.C) void {
|
|
61
|
+
prng.src = .{ .seed = seed };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
pub fn psrng_next(prng: *pkmn.PSRNG) callconv(.C) u32 {
|
|
65
|
+
return prng.next();
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
pub fn rational_init(rational: *pkmn.Rational(f64)) callconv(.C) void {
|
|
69
|
+
rational.reset();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
pub fn rational_reduce(rational: *pkmn.Rational(f64)) callconv(.C) void {
|
|
73
|
+
rational.reduce();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
pub fn rational_numerator(rational: *const pkmn.Rational(f64)) callconv(.C) f64 {
|
|
77
|
+
return rational.p;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
pub fn rational_denominator(rational: *const pkmn.Rational(f64)) callconv(.C) f64 {
|
|
81
|
+
return rational.q;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const log_options = extern struct {
|
|
85
|
+
buf: [*]u8,
|
|
86
|
+
len: usize,
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
pub fn gen(comptime num: comptime_int) type {
|
|
90
|
+
const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
|
|
91
|
+
|
|
92
|
+
return struct {
|
|
93
|
+
pub const MAX_CHOICES = g.MAX_CHOICES;
|
|
94
|
+
pub const CHOICES_SIZE = g.CHOICES_SIZE;
|
|
95
|
+
pub const MAX_LOGS = g.MAX_LOGS;
|
|
96
|
+
pub const LOGS_SIZE = g.LOGS_SIZE;
|
|
97
|
+
|
|
98
|
+
const chance_options = extern struct {
|
|
99
|
+
probability: pkmn.Rational(f64),
|
|
100
|
+
actions: g.chance.Actions,
|
|
101
|
+
durations: g.chance.Durations,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const calc_options = extern struct {
|
|
105
|
+
overrides: g.chance.Actions,
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const battle_options = struct {
|
|
109
|
+
stream: pkmn.protocol.ByteStream,
|
|
110
|
+
log: pkmn.protocol.FixedLog,
|
|
111
|
+
chance: g.Chance(pkmn.Rational(f64)),
|
|
112
|
+
calc: g.Calc,
|
|
113
|
+
|
|
114
|
+
comptime {
|
|
115
|
+
assert(@sizeOf(battle_options) <= 128);
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
pub fn battle_options_set(
|
|
120
|
+
options: *battle_options,
|
|
121
|
+
log: ?*const log_options,
|
|
122
|
+
chance: ?*const chance_options,
|
|
123
|
+
calc: ?*const calc_options,
|
|
124
|
+
) callconv(.C) void {
|
|
125
|
+
if (pkmn.options.log) {
|
|
126
|
+
if (log) |l| {
|
|
127
|
+
options.stream = .{ .buffer = l.buf[0..l.len] };
|
|
128
|
+
options.log = .{ .writer = options.stream.writer() };
|
|
129
|
+
} else {
|
|
130
|
+
options.stream.reset();
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (pkmn.options.chance) {
|
|
134
|
+
if (chance) |c| {
|
|
135
|
+
options.chance = .{
|
|
136
|
+
.probability = c.probability,
|
|
137
|
+
.actions = c.actions,
|
|
138
|
+
.durations = c.durations,
|
|
139
|
+
};
|
|
140
|
+
} else {
|
|
141
|
+
options.chance.reset();
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (pkmn.options.calc) {
|
|
145
|
+
if (calc) |c| {
|
|
146
|
+
options.calc = .{ .overrides = c.overrides };
|
|
147
|
+
} else {
|
|
148
|
+
options.calc = .{};
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
pub fn battle_options_chance_probability(
|
|
154
|
+
options: *battle_options,
|
|
155
|
+
) callconv(.C) *pkmn.Rational(f64) {
|
|
156
|
+
return &options.chance.probability;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
pub fn battle_options_chance_actions(
|
|
160
|
+
options: *battle_options,
|
|
161
|
+
) callconv(.C) *g.chance.Actions {
|
|
162
|
+
return &options.chance.actions;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
pub fn battle_options_chance_durations(
|
|
166
|
+
options: *battle_options,
|
|
167
|
+
) callconv(.C) *g.chance.Durations {
|
|
168
|
+
return &options.chance.durations;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
pub fn battle_options_calc_summaries(
|
|
172
|
+
options: *battle_options,
|
|
173
|
+
) callconv(.C) *g.calc.Summaries {
|
|
174
|
+
return &options.calc.summaries;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
pub fn battle_update(
|
|
178
|
+
battle: *g.Battle(g.PRNG),
|
|
179
|
+
c1: pkmn.Choice,
|
|
180
|
+
c2: pkmn.Choice,
|
|
181
|
+
opts: ?*battle_options,
|
|
182
|
+
) callconv(.C) pkmn.Result {
|
|
183
|
+
if ((pkmn.options.log or pkmn.options.chance or pkmn.options.calc) and opts != null) {
|
|
184
|
+
return battle.update(c1, c2, opts.?) catch return @bitCast(ERROR);
|
|
185
|
+
}
|
|
186
|
+
return battle.update(c1, c2, &g.NULL) catch unreachable;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
pub fn battle_choices(
|
|
190
|
+
battle: *const g.Battle(g.PRNG),
|
|
191
|
+
player: u8,
|
|
192
|
+
request: u8,
|
|
193
|
+
out: [*]u8,
|
|
194
|
+
n: usize,
|
|
195
|
+
) callconv(.C) u8 {
|
|
196
|
+
assert(player <= @field(@typeInfo(pkmn.Player), @tagName(Enum)).fields.len);
|
|
197
|
+
assert(request <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
|
|
198
|
+
|
|
199
|
+
assert(!pkmn.options.showdown or n > 0);
|
|
200
|
+
return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(out[0..n]));
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
pub fn exports() type {
|
|
206
|
+
@export(choice_init, .{ .name = "pkmn_choice_init", .linkage = Strong });
|
|
207
|
+
@export(choice_type, .{ .name = "pkmn_choice_type", .linkage = Strong });
|
|
208
|
+
@export(choice_data, .{ .name = "pkmn_choice_data", .linkage = Strong });
|
|
209
|
+
|
|
210
|
+
@export(result_type, .{ .name = "pkmn_result_type", .linkage = Strong });
|
|
211
|
+
@export(result_p1, .{ .name = "pkmn_result_p1", .linkage = Strong });
|
|
212
|
+
@export(result_p2, .{ .name = "pkmn_result_p2", .linkage = Strong });
|
|
213
|
+
|
|
214
|
+
@export(err, .{ .name = "pkmn_error", .linkage = Strong });
|
|
215
|
+
|
|
216
|
+
@export(psrng_init, .{ .name = "pkmn_psrng_init", .linkage = Strong });
|
|
217
|
+
@export(psrng_next, .{ .name = "pkmn_psrng_next", .linkage = Strong });
|
|
218
|
+
|
|
219
|
+
@export(rational_init, .{ .name = "pkmn_rational_init", .linkage = Strong });
|
|
220
|
+
@export(rational_reduce, .{ .name = "pkmn_rational_reduce", .linkage = Strong });
|
|
221
|
+
@export(rational_numerator, .{ .name = "pkmn_rational_numerator", .linkage = Strong });
|
|
222
|
+
@export(rational_denominator, .{ .name = "pkmn_rational_denominator", .linkage = Strong });
|
|
223
|
+
|
|
224
|
+
@export(
|
|
225
|
+
gen(1).battle_options_set,
|
|
226
|
+
.{ .name = "pkmn_gen1_battle_options_set", .linkage = Strong },
|
|
227
|
+
);
|
|
228
|
+
@export(
|
|
229
|
+
gen(1).battle_options_chance_probability,
|
|
230
|
+
.{ .name = "pkmn_gen1_battle_options_chance_probability", .linkage = Strong },
|
|
231
|
+
);
|
|
232
|
+
@export(
|
|
233
|
+
gen(1).battle_options_chance_actions,
|
|
234
|
+
.{ .name = "pkmn_gen1_battle_options_chance_actions", .linkage = Strong },
|
|
235
|
+
);
|
|
236
|
+
@export(
|
|
237
|
+
gen(1).battle_options_chance_durations,
|
|
238
|
+
.{ .name = "pkmn_gen1_battle_options_chance_durations", .linkage = Strong },
|
|
239
|
+
);
|
|
240
|
+
@export(
|
|
241
|
+
gen(1).battle_options_calc_summaries,
|
|
242
|
+
.{ .name = "pkmn_gen1_battle_options_calc_summaries", .linkage = Strong },
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
@export(
|
|
246
|
+
gen(1).battle_update,
|
|
247
|
+
.{ .name = "pkmn_gen1_battle_update", .linkage = Strong },
|
|
248
|
+
);
|
|
249
|
+
@export(
|
|
250
|
+
gen(1).battle_choices,
|
|
251
|
+
.{ .name = "pkmn_gen1_battle_choices", .linkage = Strong },
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
return struct {};
|
|
255
|
+
}
|
|
@@ -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
|
+
}
|
package/src/lib/common/array.zig
CHANGED
|
@@ -11,7 +11,7 @@ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
|
|
|
11
11
|
pub fn Array(comptime n: comptime_int, comptime U: type) type {
|
|
12
12
|
return struct {
|
|
13
13
|
const size = @bitSizeOf(U);
|
|
14
|
-
const options = .{ .signedness = .unsigned, .bits = size * n };
|
|
14
|
+
const options: std.builtin.Type.Int = .{ .signedness = .unsigned, .bits = size * n };
|
|
15
15
|
pub const T = @Type(if (@hasField(std.builtin.Type, "int"))
|
|
16
16
|
.{ .int = options }
|
|
17
17
|
else
|
package/src/lib/common/js.zig
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
|
+
const c = @import("napi.zig");
|
|
2
3
|
|
|
3
4
|
const assert = std.debug.assert;
|
|
4
5
|
|
|
5
6
|
const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
|
|
6
7
|
|
|
7
|
-
const c = @cImport({
|
|
8
|
-
@cDefine("NAPI_VERSION", "8");
|
|
9
|
-
@cInclude("node_api.h");
|
|
10
|
-
});
|
|
11
|
-
|
|
12
8
|
pub const Array = struct {
|
|
13
9
|
pub fn init(env: c.napi_env, o: struct { length: ?usize }) c.napi_value {
|
|
14
10
|
var result: c.napi_value = undefined;
|
|
@@ -7,6 +7,7 @@ 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,
|
|
@@ -24,6 +25,7 @@ pub const chance = get("chance", false);
|
|
|
24
25
|
/// Whether or not damage calculator support is enabled.
|
|
25
26
|
pub const calc = get("calc", false);
|
|
26
27
|
|
|
28
|
+
pub const mod = showdown or get("mod", false);
|
|
27
29
|
pub const advance = get("advance", true);
|
|
28
30
|
pub const ebc = get("ebc", true);
|
|
29
31
|
pub const key = get("key", false);
|
|
@@ -1,18 +1,14 @@
|
|
|
1
|
-
const std = @import("std");
|
|
2
1
|
const builtin = @import("builtin");
|
|
3
|
-
|
|
4
|
-
const pkmn = @import("../pkmn.zig");
|
|
5
|
-
|
|
6
2
|
const data = @import("data.zig");
|
|
7
3
|
const options = @import("options.zig");
|
|
4
|
+
const pkmn = @import("../pkmn.zig");
|
|
5
|
+
const std = @import("std");
|
|
8
6
|
|
|
9
7
|
const assert = std.debug.assert;
|
|
10
|
-
const print = std.debug.print;
|
|
11
|
-
|
|
12
8
|
const enabled = options.log;
|
|
13
|
-
|
|
14
|
-
const Player = data.Player;
|
|
15
9
|
const ID = data.ID;
|
|
10
|
+
const Player = data.Player;
|
|
11
|
+
const print = std.debug.print;
|
|
16
12
|
|
|
17
13
|
const endian = builtin.cpu.arch.endian();
|
|
18
14
|
const Endian = std.builtin.Endian;
|