@pkmn/engine 0.1.0-dev.04a96f24 → 0.1.0-dev.059a0591
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 -100
- package/build/pkg/index.js +6 -68
- 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 +118 -27
- package/package.json +23 -124
- package/src/bin/install-pkmn-engine +3 -2
- package/src/lib/bindings/c.zig +254 -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/options.zig +2 -0
- package/src/lib/common/util.zig +1 -1
- package/src/lib/gen1/calc.zig +9 -9
- package/src/lib/gen1/mechanics.zig +52 -21
- package/src/lib/gen2/mechanics.zig +22 -11
- package/src/lib/node.zig +2 -113
- package/src/lib/pkmn.zig +3 -2
- package/src/lib/wasm.zig +14 -9
|
@@ -0,0 +1,254 @@
|
|
|
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
|
+
|
|
10
|
+
pub const OPTIONS: extern struct {
|
|
11
|
+
showdown: bool,
|
|
12
|
+
log: bool,
|
|
13
|
+
chance: bool,
|
|
14
|
+
calc: bool,
|
|
15
|
+
} = .{
|
|
16
|
+
.showdown = pkmn.options.showdown,
|
|
17
|
+
.log = pkmn.options.log,
|
|
18
|
+
.chance = pkmn.options.chance,
|
|
19
|
+
.calc = pkmn.options.calc,
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
pub const MAX_CHOICES = pkmn.MAX_CHOICES;
|
|
23
|
+
pub const CHOICES_SIZE = pkmn.CHOICES_SIZE;
|
|
24
|
+
pub const MAX_LOGS = pkmn.MAX_LOGS;
|
|
25
|
+
pub const LOGS_SIZE = pkmn.LOGS_SIZE;
|
|
26
|
+
|
|
27
|
+
pub fn choice_init(choice: u8, data: u8) callconv(.C) u8 {
|
|
28
|
+
assert(choice <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
|
|
29
|
+
assert(data <= 6);
|
|
30
|
+
return @bitCast(pkmn.Choice{ .type = @enumFromInt(choice), .data = @intCast(data) });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
pub fn choice_type(choice: u8) callconv(.C) u8 {
|
|
34
|
+
return @intFromEnum(@as(pkmn.Choice, @bitCast(choice)).type);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
pub fn choice_data(choice: u8) callconv(.C) u8 {
|
|
38
|
+
return @as(u8, @as(pkmn.Choice, @bitCast(choice)).data);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
pub fn result_type(result: u8) callconv(.C) u8 {
|
|
42
|
+
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).type);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
pub fn result_p1(result: u8) callconv(.C) u8 {
|
|
46
|
+
assert(!err(result));
|
|
47
|
+
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p1);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
pub fn result_p2(result: u8) callconv(.C) u8 {
|
|
51
|
+
assert(!err(result));
|
|
52
|
+
return @intFromEnum(@as(pkmn.Result, @bitCast(result)).p2);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
pub fn err(result: u8) callconv(.C) bool {
|
|
56
|
+
return result == ERROR;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
pub fn psrng_init(prng: *pkmn.PSRNG, seed: u64) callconv(.C) void {
|
|
60
|
+
prng.src = .{ .seed = seed };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
pub fn psrng_next(prng: *pkmn.PSRNG) callconv(.C) u32 {
|
|
64
|
+
return prng.next();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
pub fn rational_init(rational: *pkmn.Rational(f64)) callconv(.C) void {
|
|
68
|
+
rational.reset();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
pub fn rational_reduce(rational: *pkmn.Rational(f64)) callconv(.C) void {
|
|
72
|
+
rational.reduce();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
pub fn rational_numerator(rational: *const pkmn.Rational(f64)) callconv(.C) f64 {
|
|
76
|
+
return rational.p;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
pub fn rational_denominator(rational: *const pkmn.Rational(f64)) callconv(.C) f64 {
|
|
80
|
+
return rational.q;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const log_options = extern struct {
|
|
84
|
+
buf: [*]u8,
|
|
85
|
+
len: usize,
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
pub fn gen(comptime num: comptime_int) type {
|
|
89
|
+
const g = @field(pkmn, "gen" ++ std.fmt.comptimePrint("{d}", .{num}));
|
|
90
|
+
|
|
91
|
+
return struct {
|
|
92
|
+
pub const MAX_CHOICES = g.MAX_CHOICES;
|
|
93
|
+
pub const CHOICES_SIZE = g.CHOICES_SIZE;
|
|
94
|
+
pub const MAX_LOGS = g.MAX_LOGS;
|
|
95
|
+
pub const LOGS_SIZE = g.LOGS_SIZE;
|
|
96
|
+
|
|
97
|
+
const chance_options = extern struct {
|
|
98
|
+
probability: pkmn.Rational(f64),
|
|
99
|
+
actions: g.chance.Actions,
|
|
100
|
+
durations: g.chance.Durations,
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const calc_options = extern struct {
|
|
104
|
+
overrides: g.chance.Actions,
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const battle_options = struct {
|
|
108
|
+
stream: pkmn.protocol.ByteStream,
|
|
109
|
+
log: pkmn.protocol.FixedLog,
|
|
110
|
+
chance: g.Chance(pkmn.Rational(f64)),
|
|
111
|
+
calc: g.Calc,
|
|
112
|
+
|
|
113
|
+
comptime {
|
|
114
|
+
assert(@sizeOf(battle_options) <= 128);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
pub fn battle_options_set(
|
|
119
|
+
options: *battle_options,
|
|
120
|
+
log: ?*const log_options,
|
|
121
|
+
chance: ?*const chance_options,
|
|
122
|
+
calc: ?*const calc_options,
|
|
123
|
+
) callconv(.C) void {
|
|
124
|
+
if (pkmn.options.log) {
|
|
125
|
+
if (log) |l| {
|
|
126
|
+
options.stream = .{ .buffer = l.buf[0..l.len] };
|
|
127
|
+
options.log = .{ .writer = options.stream.writer() };
|
|
128
|
+
} else {
|
|
129
|
+
options.stream.reset();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (pkmn.options.chance) {
|
|
133
|
+
if (chance) |c| {
|
|
134
|
+
options.chance = .{
|
|
135
|
+
.probability = c.probability,
|
|
136
|
+
.actions = c.actions,
|
|
137
|
+
.durations = c.durations,
|
|
138
|
+
};
|
|
139
|
+
} else {
|
|
140
|
+
options.chance.reset();
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
if (pkmn.options.calc) {
|
|
144
|
+
if (calc) |c| {
|
|
145
|
+
options.calc = .{ .overrides = c.overrides };
|
|
146
|
+
} else {
|
|
147
|
+
options.calc = .{};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
pub fn battle_options_chance_probability(
|
|
153
|
+
options: *battle_options,
|
|
154
|
+
) callconv(.C) *pkmn.Rational(f64) {
|
|
155
|
+
return &options.chance.probability;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
pub fn battle_options_chance_actions(
|
|
159
|
+
options: *battle_options,
|
|
160
|
+
) callconv(.C) *g.chance.Actions {
|
|
161
|
+
return &options.chance.actions;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
pub fn battle_options_chance_durations(
|
|
165
|
+
options: *battle_options,
|
|
166
|
+
) callconv(.C) *g.chance.Durations {
|
|
167
|
+
return &options.chance.durations;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
pub fn battle_options_calc_summaries(
|
|
171
|
+
options: *battle_options,
|
|
172
|
+
) callconv(.C) *g.calc.Summaries {
|
|
173
|
+
return &options.calc.summaries;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
pub fn battle_update(
|
|
177
|
+
battle: *g.Battle(g.PRNG),
|
|
178
|
+
c1: pkmn.Choice,
|
|
179
|
+
c2: pkmn.Choice,
|
|
180
|
+
opts: ?*battle_options,
|
|
181
|
+
) callconv(.C) pkmn.Result {
|
|
182
|
+
if ((pkmn.options.log or pkmn.options.chance or pkmn.options.calc) and opts != null) {
|
|
183
|
+
return battle.update(c1, c2, opts.?) catch return @bitCast(ERROR);
|
|
184
|
+
}
|
|
185
|
+
return battle.update(c1, c2, &g.NULL) catch unreachable;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
pub fn battle_choices(
|
|
189
|
+
battle: *const g.Battle(g.PRNG),
|
|
190
|
+
player: u8,
|
|
191
|
+
request: u8,
|
|
192
|
+
out: [*]u8,
|
|
193
|
+
n: usize,
|
|
194
|
+
) callconv(.C) u8 {
|
|
195
|
+
assert(player <= @field(@typeInfo(pkmn.Player), @tagName(Enum)).fields.len);
|
|
196
|
+
assert(request <= @field(@typeInfo(pkmn.Choice.Type), @tagName(Enum)).fields.len);
|
|
197
|
+
|
|
198
|
+
assert(!pkmn.options.showdown or n > 0);
|
|
199
|
+
return battle.choices(@enumFromInt(player), @enumFromInt(request), @ptrCast(out[0..n]));
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
pub fn exports() type {
|
|
205
|
+
@export(choice_init, .{ .name = "pkmn_choice_init", .linkage = .Strong });
|
|
206
|
+
@export(choice_type, .{ .name = "pkmn_choice_type", .linkage = .Strong });
|
|
207
|
+
@export(choice_data, .{ .name = "pkmn_choice_data", .linkage = .Strong });
|
|
208
|
+
|
|
209
|
+
@export(result_type, .{ .name = "pkmn_result_type", .linkage = .Strong });
|
|
210
|
+
@export(result_p1, .{ .name = "pkmn_result_p1", .linkage = .Strong });
|
|
211
|
+
@export(result_p2, .{ .name = "pkmn_result_p2", .linkage = .Strong });
|
|
212
|
+
|
|
213
|
+
@export(err, .{ .name = "pkmn_error", .linkage = .Strong });
|
|
214
|
+
|
|
215
|
+
@export(psrng_init, .{ .name = "pkmn_psrng_init", .linkage = .Strong });
|
|
216
|
+
@export(psrng_next, .{ .name = "pkmn_psrng_next", .linkage = .Strong });
|
|
217
|
+
|
|
218
|
+
@export(rational_init, .{ .name = "pkmn_rational_init", .linkage = .Strong });
|
|
219
|
+
@export(rational_reduce, .{ .name = "pkmn_rational_reduce", .linkage = .Strong });
|
|
220
|
+
@export(rational_numerator, .{ .name = "pkmn_rational_numerator", .linkage = .Strong });
|
|
221
|
+
@export(rational_denominator, .{ .name = "pkmn_rational_denominator", .linkage = .Strong });
|
|
222
|
+
|
|
223
|
+
@export(
|
|
224
|
+
gen(1).battle_options_set,
|
|
225
|
+
.{ .name = "pkmn_gen1_battle_options_set", .linkage = .Strong },
|
|
226
|
+
);
|
|
227
|
+
@export(
|
|
228
|
+
gen(1).battle_options_chance_probability,
|
|
229
|
+
.{ .name = "pkmn_gen1_battle_options_chance_probability", .linkage = .Strong },
|
|
230
|
+
);
|
|
231
|
+
@export(
|
|
232
|
+
gen(1).battle_options_chance_actions,
|
|
233
|
+
.{ .name = "pkmn_gen1_battle_options_chance_actions", .linkage = .Strong },
|
|
234
|
+
);
|
|
235
|
+
@export(
|
|
236
|
+
gen(1).battle_options_chance_durations,
|
|
237
|
+
.{ .name = "pkmn_gen1_battle_options_chance_durations", .linkage = .Strong },
|
|
238
|
+
);
|
|
239
|
+
@export(
|
|
240
|
+
gen(1).battle_options_calc_summaries,
|
|
241
|
+
.{ .name = "pkmn_gen1_battle_options_calc_summaries", .linkage = .Strong },
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
@export(
|
|
245
|
+
gen(1).battle_update,
|
|
246
|
+
.{ .name = "pkmn_gen1_battle_update", .linkage = .Strong },
|
|
247
|
+
);
|
|
248
|
+
@export(
|
|
249
|
+
gen(1).battle_choices,
|
|
250
|
+
.{ .name = "pkmn_gen1_battle_choices", .linkage = .Strong },
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
return struct {};
|
|
254
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
const js = @import("../common/js.zig");
|
|
2
|
+
const pkmn = @import("../pkmn.zig");
|
|
3
|
+
const std = @import("std");
|
|
4
|
+
|
|
5
|
+
const assert = std.debug.assert;
|
|
6
|
+
|
|
7
|
+
const c = @cImport({
|
|
8
|
+
@cDefine("NAPI_VERSION", "8");
|
|
9
|
+
@cInclude("node_api.h");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
pub fn register(env: c.napi_env) c.napi_value {
|
|
13
|
+
const object = js.Object.init(env);
|
|
14
|
+
const properties = [_]c.napi_property_descriptor{
|
|
15
|
+
js.Property.init("options", .{ .value = options(env) }),
|
|
16
|
+
js.Property.init("bindings", .{ .value = bindings(env) }),
|
|
17
|
+
};
|
|
18
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
19
|
+
return object;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
fn options(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("showdown", .{ .value = js.Boolean.init(env, pkmn.options.showdown) }),
|
|
26
|
+
js.Property.init("log", .{ .value = js.Boolean.init(env, pkmn.options.log) }),
|
|
27
|
+
js.Property.init("chance", .{ .value = js.Boolean.init(env, pkmn.options.chance) }),
|
|
28
|
+
js.Property.init("calc", .{ .value = js.Boolean.init(env, pkmn.options.calc) }),
|
|
29
|
+
};
|
|
30
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
31
|
+
return object;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fn bindings(env: c.napi_env) c.napi_value {
|
|
35
|
+
const array = js.Array.init(env, .{ .length = 1 });
|
|
36
|
+
js.Array.set(env, array, 0, bind(env, pkmn.gen1));
|
|
37
|
+
return array;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
fn bind(env: c.napi_env, gen: anytype) c.napi_value {
|
|
41
|
+
const choices_size: u32 = @intCast(gen.CHOICES_SIZE);
|
|
42
|
+
const logs_size: u32 = @intCast(gen.LOGS_SIZE);
|
|
43
|
+
const object = js.Object.init(env);
|
|
44
|
+
const properties = [_]c.napi_property_descriptor{
|
|
45
|
+
js.Property.init("CHOICES_SIZE", .{ .value = js.Number.init(env, choices_size) }),
|
|
46
|
+
js.Property.init("LOGS_SIZE", .{ .value = js.Number.init(env, logs_size) }),
|
|
47
|
+
js.Property.init("update", .{ .method = update(gen) }),
|
|
48
|
+
js.Property.init("choices", .{ .method = choices(gen) }),
|
|
49
|
+
};
|
|
50
|
+
assert(c.napi_define_properties(env, object, properties.len, &properties) == c.napi_ok);
|
|
51
|
+
return object;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
fn update(gen: anytype) c.napi_callback {
|
|
55
|
+
return struct {
|
|
56
|
+
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
|
|
57
|
+
var argc: usize = 4;
|
|
58
|
+
var argv: [4]c.napi_value = undefined;
|
|
59
|
+
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
60
|
+
assert(argc == 4);
|
|
61
|
+
|
|
62
|
+
var data: ?*anyopaque = undefined;
|
|
63
|
+
var len: usize = 0;
|
|
64
|
+
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
65
|
+
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
66
|
+
|
|
67
|
+
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
68
|
+
const c1: pkmn.Choice = @bitCast(js.Number.get(env, argv[1], u8));
|
|
69
|
+
const c2: pkmn.Choice = @bitCast(js.Number.get(env, argv[2], u8));
|
|
70
|
+
|
|
71
|
+
var vtype: c.napi_valuetype = undefined;
|
|
72
|
+
assert(c.napi_typeof(env, argv[3], &vtype) == c.napi_ok);
|
|
73
|
+
const result = switch (vtype) {
|
|
74
|
+
c.napi_undefined, c.napi_null => battle.update(c1, c2, &gen.NULL),
|
|
75
|
+
else => result: {
|
|
76
|
+
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
77
|
+
assert(len == gen.LOGS_SIZE);
|
|
78
|
+
|
|
79
|
+
const buf = @as([*]u8, @ptrCast(data.?))[0..gen.LOGS_SIZE];
|
|
80
|
+
var stream: pkmn.protocol.ByteStream = .{ .buffer = buf };
|
|
81
|
+
// TODO: extract out
|
|
82
|
+
var opts = pkmn.battle.options(
|
|
83
|
+
pkmn.protocol.FixedLog{ .writer = stream.writer() },
|
|
84
|
+
gen.chance.NULL,
|
|
85
|
+
gen.calc.NULL,
|
|
86
|
+
);
|
|
87
|
+
break :result battle.update(c1, c2, &opts);
|
|
88
|
+
},
|
|
89
|
+
} catch unreachable;
|
|
90
|
+
|
|
91
|
+
return js.Number.init(env, @as(u8, @bitCast(result)));
|
|
92
|
+
}
|
|
93
|
+
}.call;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
fn choices(gen: anytype) c.napi_callback {
|
|
97
|
+
return struct {
|
|
98
|
+
fn call(env: c.napi_env, info: c.napi_callback_info) callconv(.C) c.napi_value {
|
|
99
|
+
var argc: usize = 4;
|
|
100
|
+
var argv: [4]c.napi_value = undefined;
|
|
101
|
+
assert(c.napi_get_cb_info(env, info, &argc, &argv, null, null) == c.napi_ok);
|
|
102
|
+
assert(argc == 4);
|
|
103
|
+
|
|
104
|
+
var data: ?*anyopaque = undefined;
|
|
105
|
+
var len: usize = 0;
|
|
106
|
+
assert(c.napi_get_arraybuffer_info(env, argv[0], &data, &len) == c.napi_ok);
|
|
107
|
+
assert(len == @sizeOf(gen.Battle(gen.PRNG)));
|
|
108
|
+
|
|
109
|
+
var battle: *gen.Battle(gen.PRNG) = @alignCast(@ptrCast(data.?));
|
|
110
|
+
const player: pkmn.Player = @enumFromInt(js.Number.get(env, argv[1], u8));
|
|
111
|
+
const request: pkmn.Choice.Type = @enumFromInt(js.Number.get(env, argv[2], u8));
|
|
112
|
+
|
|
113
|
+
assert(c.napi_get_arraybuffer_info(env, argv[3], &data, &len) == c.napi_ok);
|
|
114
|
+
assert(len == gen.CHOICES_SIZE);
|
|
115
|
+
|
|
116
|
+
const out = @as([*]pkmn.Choice, @ptrCast(data.?))[0..gen.CHOICES_SIZE];
|
|
117
|
+
const n = battle.choices(player, request, out);
|
|
118
|
+
return js.Number.init(env, @as(u8, @bitCast(n)));
|
|
119
|
+
}
|
|
120
|
+
}.call;
|
|
121
|
+
}
|
|
@@ -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
|
|
@@ -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);
|
package/src/lib/common/util.zig
CHANGED
|
@@ -44,7 +44,7 @@ fn CopyPtrAttrs(
|
|
|
44
44
|
comptime child: type,
|
|
45
45
|
) type {
|
|
46
46
|
const info = @field(@typeInfo(source), @tagName(Pointer));
|
|
47
|
-
const args = .{
|
|
47
|
+
const args: std.builtin.Type.Pointer = .{
|
|
48
48
|
.size = size,
|
|
49
49
|
.is_const = info.is_const,
|
|
50
50
|
.is_volatile = info.is_volatile,
|
package/src/lib/gen1/calc.zig
CHANGED
|
@@ -182,7 +182,7 @@ pub const Options = struct {
|
|
|
182
182
|
metronome: bool = false,
|
|
183
183
|
};
|
|
184
184
|
|
|
185
|
-
pub const MAX_FRONTIER =
|
|
185
|
+
pub const MAX_FRONTIER = 81;
|
|
186
186
|
|
|
187
187
|
pub fn transitions(
|
|
188
188
|
battle: anytype,
|
|
@@ -298,9 +298,9 @@ pub fn transitions(
|
|
|
298
298
|
const p1_max: u9 = if (p2_dmg.min != p2_min)
|
|
299
299
|
p1_dmg.min
|
|
300
300
|
else
|
|
301
|
-
|
|
301
|
+
Rolls.coalesce(.P1, @as(u8, @intCast(p1_dmg.min)), summaries, cap);
|
|
302
302
|
const p2_max: u9 =
|
|
303
|
-
|
|
303
|
+
Rolls.coalesce(.P2, @as(u8, @intCast(p2_dmg.min)), summaries, cap);
|
|
304
304
|
|
|
305
305
|
if (opts.chance.actions.matches(f)) {
|
|
306
306
|
if (!opts.chance.actions.relax().eql(a)) {
|
|
@@ -744,7 +744,7 @@ pub const Rolls = struct {
|
|
|
744
744
|
|
|
745
745
|
/// Returns the max damage roll which will produce the same damage as `roll`
|
|
746
746
|
/// given the base damage in `summaries`.
|
|
747
|
-
pub fn coalesce(player: Player, roll: u8, summaries: *Summaries, cap: bool)
|
|
747
|
+
pub fn coalesce(player: Player, roll: u8, summaries: *Summaries, cap: bool) u8 {
|
|
748
748
|
if (roll == 0) return roll;
|
|
749
749
|
|
|
750
750
|
const dmg = summaries.get(player.foe()).damage;
|
|
@@ -854,12 +854,12 @@ test "Rolls.damage" {
|
|
|
854
854
|
|
|
855
855
|
test "Rolls.coalesce" {
|
|
856
856
|
var summaries = Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
|
|
857
|
-
try expectEqual(@as(u8, 0),
|
|
858
|
-
try expectEqual(@as(u8, 241),
|
|
859
|
-
try expectEqual(@as(u8, 255),
|
|
857
|
+
try expectEqual(@as(u8, 0), Rolls.coalesce(.P2, 0, &summaries, false));
|
|
858
|
+
try expectEqual(@as(u8, 241), Rolls.coalesce(.P2, 238, &summaries, false));
|
|
859
|
+
try expectEqual(@as(u8, 255), Rolls.coalesce(.P2, 238, &summaries, true));
|
|
860
860
|
summaries.p1.damage.final = 74;
|
|
861
|
-
try expectEqual(@as(u8, 217),
|
|
862
|
-
try expectEqual(@as(u8, 255),
|
|
861
|
+
try expectEqual(@as(u8, 217), Rolls.coalesce(.P2, 217, &summaries, false));
|
|
862
|
+
try expectEqual(@as(u8, 255), Rolls.coalesce(.P2, 217, &summaries, true));
|
|
863
863
|
}
|
|
864
864
|
|
|
865
865
|
test "Rolls.hit" {
|