@pkmn/engine 0.1.0-dev.3a62fd35 → 0.1.0-dev.3de821b9
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 +13 -11
- package/build/pkg/addon.js +6 -7
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data.d.ts +2 -3
- package/build/pkg/protocol.js +4 -4
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +8 -3
- package/build/test/display.js.map +1 -1
- package/build/tools/debug.js +2 -3
- package/build/tools/debug.js.map +1 -1
- package/build.zig +32 -50
- package/package.json +24 -17
- package/src/bin/install-pkmn-engine +13 -5
- package/src/lib/common/debug.zig +2 -2
- package/src/lib/common/options.zig +2 -0
- package/src/lib/common/protocol.zig +11 -9
- package/src/lib/common/rational.zig +102 -12
- package/src/lib/common/rng.zig +6 -6
- package/src/lib/gen1/calc.zig +480 -92
- package/src/lib/gen1/chance.zig +384 -268
- package/src/lib/gen1/data/species.zig +1 -1
- package/src/lib/gen1/data.zig +3 -3
- package/src/lib/gen1/helpers.zig +90 -534
- package/src/lib/gen1/mechanics.zig +266 -158
- package/src/lib/gen2/chance.zig +8 -440
- package/src/lib/gen2/helpers.zig +37 -7
- package/src/lib/gen2/mechanics.zig +119 -61
- package/src/lib/pkmn.zig +4 -12
- package/src/test/display/pkmn.html.tmpl +5 -5
- package/src/lib/gen1/README.md +0 -894
- package/src/lib/gen2/README.md +0 -329
package/src/lib/gen1/calc.zig
CHANGED
|
@@ -17,6 +17,7 @@ const assert = std.debug.assert;
|
|
|
17
17
|
|
|
18
18
|
const expect = std.testing.expect;
|
|
19
19
|
const expectEqual = std.testing.expectEqual;
|
|
20
|
+
const expectEqualSlices = std.testing.expectEqualSlices;
|
|
20
21
|
|
|
21
22
|
const enabled = pkmn.options.calc;
|
|
22
23
|
|
|
@@ -34,9 +35,9 @@ const Actions = chance.Actions;
|
|
|
34
35
|
const Action = chance.Action;
|
|
35
36
|
const Chance = chance.Chance;
|
|
36
37
|
const Durations = chance.Durations;
|
|
37
|
-
const Duration = chance.Duration;
|
|
38
38
|
|
|
39
|
-
const
|
|
39
|
+
const tty = true; // DEBUG
|
|
40
|
+
const summary = false; // DEBUG
|
|
40
41
|
|
|
41
42
|
/// Information relevant to damage calculation that occured during a Generation I battle `update`.
|
|
42
43
|
pub const Summaries = extern struct {
|
|
@@ -69,8 +70,10 @@ pub const Summary = extern struct {
|
|
|
69
70
|
/// The final computed damage that gets applied to the Pokémon. May exceed the target's HP
|
|
70
71
|
// (to determine the *actual* damage done compare the target's stored HP before and after).
|
|
71
72
|
final: u16 = 0,
|
|
72
|
-
/// Whether higher damage will saturate / result in the same outcome (e.g. additional
|
|
73
|
-
/// gets ignored due to it already breaking a Substitute or causing the target
|
|
73
|
+
/// Whether higher direct damage will saturate / result in the same outcome (e.g. additional
|
|
74
|
+
/// direct damage gets ignored due to it already breaking a Substitute or causing the target
|
|
75
|
+
/// to faint). Note that this field does not get set in scenarios where the target would
|
|
76
|
+
/// only be guaranteed to faint due to some sort of subsequent recoil or residual damage.
|
|
74
77
|
capped: bool = false,
|
|
75
78
|
|
|
76
79
|
// NOTE: 15 bits padding
|
|
@@ -89,13 +92,9 @@ pub const Summary = extern struct {
|
|
|
89
92
|
pub const Overrides = extern struct {
|
|
90
93
|
/// TODO
|
|
91
94
|
actions: Actions = .{},
|
|
92
|
-
/// TODO
|
|
93
|
-
durations: Durations = .{},
|
|
94
|
-
|
|
95
|
-
_: u32 = 0,
|
|
96
95
|
|
|
97
96
|
comptime {
|
|
98
|
-
assert(@sizeOf(Overrides) ==
|
|
97
|
+
assert(@sizeOf(Overrides) == 16);
|
|
99
98
|
}
|
|
100
99
|
};
|
|
101
100
|
|
|
@@ -124,18 +123,6 @@ pub const Calc = struct {
|
|
|
124
123
|
}) val else null;
|
|
125
124
|
}
|
|
126
125
|
|
|
127
|
-
pub fn modify(self: Calc, player: Player, comptime field: Duration.Field) ?bool {
|
|
128
|
-
if (!enabled) return null;
|
|
129
|
-
|
|
130
|
-
const durations = self.overrides.durations;
|
|
131
|
-
const set = @field(if (player == .P1) durations.p1 else durations.p2, @tagName(field));
|
|
132
|
-
if (set == 0) return null;
|
|
133
|
-
|
|
134
|
-
const actions = self.overrides.actions;
|
|
135
|
-
const overrides = if (player == .P1) actions.p1.durations else actions.p2.durations;
|
|
136
|
-
return @field(overrides, @tagName(field)) != 0;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
126
|
pub fn base(self: *Calc, player: Player, val: u16) void {
|
|
140
127
|
if (!enabled) return;
|
|
141
128
|
|
|
@@ -169,11 +156,6 @@ const Null = struct {
|
|
|
169
156
|
return null;
|
|
170
157
|
}
|
|
171
158
|
|
|
172
|
-
pub fn modify(self: Null, player: Player, comptime field: Duration.Field) ?bool {
|
|
173
|
-
_ = .{ self, player, field };
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
|
|
177
159
|
pub fn base(self: Null, player: Player, val: u16) void {
|
|
178
160
|
_ = .{ self, player, val };
|
|
179
161
|
}
|
|
@@ -195,12 +177,14 @@ pub const Stats = struct {
|
|
|
195
177
|
};
|
|
196
178
|
|
|
197
179
|
pub const Options = struct {
|
|
198
|
-
|
|
180
|
+
durations: Durations,
|
|
199
181
|
seed: ?u64 = null,
|
|
200
182
|
cap: bool = false,
|
|
201
183
|
metronome: bool = false,
|
|
202
184
|
};
|
|
203
185
|
|
|
186
|
+
pub const MAX_FRONTIER = 19;
|
|
187
|
+
|
|
204
188
|
pub fn transitions(
|
|
205
189
|
battle: anytype,
|
|
206
190
|
c1: Choice,
|
|
@@ -211,7 +195,6 @@ pub fn transitions(
|
|
|
211
195
|
) !?Stats {
|
|
212
196
|
var stats: Stats = .{};
|
|
213
197
|
|
|
214
|
-
const actions = options.actions;
|
|
215
198
|
const cap = options.cap;
|
|
216
199
|
|
|
217
200
|
var seen = std.AutoHashMap(Actions, void).init(allocator);
|
|
@@ -221,11 +204,8 @@ pub fn transitions(
|
|
|
221
204
|
|
|
222
205
|
var opts = pkmn.battle.options(
|
|
223
206
|
protocol.NULL,
|
|
224
|
-
Chance(Rational(u128)){ .probability = .{}, .
|
|
225
|
-
Calc{
|
|
226
|
-
.p1 = actions.p1.durations,
|
|
227
|
-
.p2 = actions.p2.durations,
|
|
228
|
-
} } },
|
|
207
|
+
Chance(Rational(u128)){ .probability = .{}, .durations = options.durations },
|
|
208
|
+
Calc{},
|
|
229
209
|
);
|
|
230
210
|
|
|
231
211
|
var b = battle;
|
|
@@ -248,32 +228,25 @@ pub fn transitions(
|
|
|
248
228
|
assert(frontier.items.len == 1);
|
|
249
229
|
while (i < frontier.items.len) : (i += 1) {
|
|
250
230
|
const template = frontier.items[i];
|
|
251
|
-
opts.calc.overrides.durations = .{
|
|
252
|
-
.p1 = template.p1.durations,
|
|
253
|
-
.p2 = template.p2.durations,
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
try debug(writer, template, true, .{ .color = i, .bold = true, .background = true });
|
|
257
231
|
|
|
258
|
-
|
|
232
|
+
try debug(writer, template, .{
|
|
233
|
+
.shape = true,
|
|
234
|
+
.color = i,
|
|
235
|
+
.bold = true,
|
|
236
|
+
.background = true,
|
|
237
|
+
.indent = false,
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
var a: Actions = .{
|
|
241
|
+
.p1 = .{ .metronome = p1_move, .duration = template.p1.duration },
|
|
242
|
+
.p2 = .{ .metronome = p2_move, .duration = template.p2.duration },
|
|
243
|
+
};
|
|
259
244
|
|
|
260
245
|
for (Rolls.speedTie(template.p1)) |tie| { a.p1.speed_tie = tie; a.p2.speed_tie = tie;
|
|
261
|
-
for (Rolls.
|
|
262
|
-
for (Rolls.
|
|
263
|
-
for (Rolls.disable(template.p1, actions.p1, p1_slp)) |p1_disable| { a.p1.durations.disable = p1_disable;
|
|
264
|
-
for (Rolls.disable(template.p2, actions.p2, p2_slp)) |p2_disable| { a.p2.durations.disable = p2_disable;
|
|
265
|
-
for (Rolls.confusion(template.p1, actions.p1, p1_slp)) |p1_cfz| { a.p1.durations.confusion = p1_cfz;
|
|
266
|
-
for (Rolls.confusion(template.p2, actions.p2, p1_slp)) |p2_cfz| { a.p2.durations.confusion = p2_cfz;
|
|
267
|
-
for (Rolls.confused(template.p1, p1_cfz)) |p1_cfzd| { a.p1.confused = p1_cfzd;
|
|
268
|
-
for (Rolls.confused(template.p2, p2_cfz)) |p2_cfzd| { a.p2.confused = p2_cfzd;
|
|
246
|
+
for (Rolls.confused(template.p1)) |p1_cfzd| { a.p1.confused = p1_cfzd;
|
|
247
|
+
for (Rolls.confused(template.p2)) |p2_cfzd| { a.p2.confused = p2_cfzd;
|
|
269
248
|
for (Rolls.paralyzed(template.p1, p1_cfzd)) |p1_par| { a.p1.paralyzed = p1_par;
|
|
270
249
|
for (Rolls.paralyzed(template.p2, p2_cfzd)) |p2_par| { a.p2.paralyzed = p2_par;
|
|
271
|
-
for (Rolls.attacking(template.p1, actions.p1, p1_par)) |p1_atk| { a.p1.durations.attacking = p1_atk;
|
|
272
|
-
for (Rolls.attacking(template.p2, actions.p2, p2_par)) |p2_atk| { a.p2.durations.attacking = p2_atk;
|
|
273
|
-
for (Rolls.binding(template.p1, actions.p1, p1_par)) |p1_bind| { a.p1.durations.binding = p1_bind;
|
|
274
|
-
for (Rolls.binding(template.p2, actions.p2, p2_par)) |p2_bind| { a.p2.durations.binding = p2_bind;
|
|
275
|
-
for (Rolls.duration(template.p1, p1_par)) |p1_dur| { a.p1.duration = p1_dur;
|
|
276
|
-
for (Rolls.duration(template.p2, p2_par)) |p2_dur| { a.p2.duration = p2_dur;
|
|
277
250
|
for (Rolls.hit(template.p1, p1_par)) |p1_hit| { a.p1.hit = p1_hit;
|
|
278
251
|
for (Rolls.hit(template.p2, p2_par)) |p2_hit| { a.p2.hit = p2_hit;
|
|
279
252
|
for (Rolls.psywave(template.p1, p1, p1_hit)) |p1_psywave| { a.p1.psywave = p1_psywave;
|
|
@@ -281,7 +254,7 @@ pub fn transitions(
|
|
|
281
254
|
for (Rolls.moveSlot(template.p1, p1_hit)) |p1_slot| { a.p1.move_slot = p1_slot;
|
|
282
255
|
for (Rolls.moveSlot(template.p2, p2_hit)) |p2_slot| { a.p2.move_slot = p2_slot;
|
|
283
256
|
for (Rolls.multiHit(template.p1, p1_hit)) |p1_multi| { a.p1.multi_hit = p1_multi;
|
|
284
|
-
for (Rolls.multiHit(template.p2,
|
|
257
|
+
for (Rolls.multiHit(template.p2, p2_hit)) |p2_multi| { a.p2.multi_hit = p2_multi;
|
|
285
258
|
for (Rolls.secondaryChance(template.p1, p1_hit)) |p1_sec| { a.p1.secondary_chance = p1_sec;
|
|
286
259
|
for (Rolls.secondaryChance(template.p2, p2_hit)) |p2_sec| { a.p2.secondary_chance = p2_sec;
|
|
287
260
|
for (Rolls.criticalHit(template.p1, p1_hit)) |p1_crit| { a.p1.critical_hit = p1_crit;
|
|
@@ -290,42 +263,59 @@ pub fn transitions(
|
|
|
290
263
|
var p1_dmg = Rolls.damage(template.p1, p1_hit);
|
|
291
264
|
while (p1_dmg.min < p1_dmg.max) : (p1_dmg.min += 1) {
|
|
292
265
|
a.p1.damage = @intCast(p1_dmg.min);
|
|
293
|
-
var p1_min: u8 = 0;
|
|
294
266
|
|
|
295
267
|
var p2_dmg = Rolls.damage(template.p2, p2_hit);
|
|
268
|
+
|
|
269
|
+
const p1_min: u9 = p1_dmg.min;
|
|
270
|
+
const p2_min: u9 = p2_dmg.min;
|
|
271
|
+
|
|
296
272
|
while (p2_dmg.min < p2_dmg.max) : (p2_dmg.min += 1) {
|
|
297
273
|
a.p2.damage = @intCast(p2_dmg.min);
|
|
298
274
|
|
|
299
275
|
opts.calc.overrides.actions = a;
|
|
300
|
-
opts.
|
|
276
|
+
opts.calc.summaries = .{};
|
|
277
|
+
opts.chance = .{ .probability = .{}, .durations = options.durations };
|
|
301
278
|
const q = &opts.chance.probability;
|
|
302
279
|
|
|
303
280
|
b = battle;
|
|
304
281
|
_ = try b.update(c1, c2, &opts);
|
|
305
|
-
stats.updates += 1;
|
|
306
282
|
|
|
307
|
-
|
|
308
|
-
const p2_max: u8 = @intCast(p2_dmg.min);
|
|
309
|
-
_ = cap;
|
|
283
|
+
stats.updates += 1;
|
|
310
284
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
285
|
+
const summaries = &opts.calc.summaries;
|
|
286
|
+
const p1_max: u9 = if (p2_dmg.min != p2_min)
|
|
287
|
+
p1_dmg.min
|
|
288
|
+
else
|
|
289
|
+
try Rolls.coalesce(.P1, @as(u8, @intCast(p1_dmg.min)), summaries, cap);
|
|
290
|
+
const p2_max: u9 =
|
|
291
|
+
try Rolls.coalesce(.P2, @as(u8, @intCast(p2_dmg.min)), summaries, cap);
|
|
315
292
|
|
|
316
293
|
if (opts.chance.actions.matches(template)) {
|
|
317
294
|
if (!opts.chance.actions.eql(a)) {
|
|
318
|
-
|
|
295
|
+
if (!summary) {
|
|
296
|
+
try debug(writer, opts.chance.actions, .{
|
|
297
|
+
.p1_max = p1_max,
|
|
298
|
+
.p2_max = p2_max,
|
|
299
|
+
.color = i,
|
|
300
|
+
.dim = true,
|
|
301
|
+
});
|
|
302
|
+
}
|
|
319
303
|
|
|
320
|
-
|
|
304
|
+
p1_dmg.min = p1_max;
|
|
321
305
|
p2_dmg.min = p2_max;
|
|
322
306
|
continue;
|
|
323
307
|
}
|
|
324
308
|
|
|
325
|
-
|
|
309
|
+
if (!summary) {
|
|
310
|
+
try debug(writer, opts.chance.actions, .{
|
|
311
|
+
.p1_max = p1_max,
|
|
312
|
+
.p2_max = p2_max,
|
|
313
|
+
.color = i,
|
|
314
|
+
});
|
|
315
|
+
}
|
|
326
316
|
|
|
327
|
-
for (
|
|
328
|
-
for (p2_dmg.min
|
|
317
|
+
for (p1_min..p1_max + 1) |p1d| {
|
|
318
|
+
for (p2_dmg.min..p2_max + 1) |p2d| {
|
|
329
319
|
var acts = opts.chance.actions;
|
|
330
320
|
acts.p1.damage = @intCast(p1d);
|
|
331
321
|
acts.p2.damage = @intCast(p2d);
|
|
@@ -336,7 +326,7 @@ pub fn transitions(
|
|
|
336
326
|
}
|
|
337
327
|
}
|
|
338
328
|
|
|
339
|
-
if (p1_max !=
|
|
329
|
+
if (p1_max != p1_min) try q.update(p1_max - p1_min + 1, 1);
|
|
340
330
|
if (p2_max != p2_dmg.min) try q.update(p2_max - p2_dmg.min + 1, 1);
|
|
341
331
|
try p.add(q);
|
|
342
332
|
stats.saved += 1;
|
|
@@ -346,27 +336,46 @@ pub fn transitions(
|
|
|
346
336
|
return error.TestUnexpectedResult;
|
|
347
337
|
}
|
|
348
338
|
} else {
|
|
349
|
-
try debug(writer, opts.chance.actions, false, .{ .dim = true });
|
|
350
|
-
|
|
351
339
|
if (!matches(opts.chance.actions, i, frontier.items)) {
|
|
352
340
|
try frontier.append(opts.chance.actions);
|
|
353
341
|
|
|
354
|
-
try debug(writer, opts.chance.actions,
|
|
342
|
+
try debug(writer, opts.chance.actions, .{
|
|
343
|
+
.p1_max = p1_max,
|
|
344
|
+
.p2_max = p2_max,
|
|
345
|
+
.dim = true,
|
|
346
|
+
.newline = false,
|
|
347
|
+
});
|
|
348
|
+
try writer.writeAll(" → ");
|
|
349
|
+
try debug(writer, opts.chance.actions, .{
|
|
350
|
+
.shape = true,
|
|
355
351
|
.color = frontier.items.len - 1,
|
|
356
352
|
.dim = true,
|
|
357
|
-
.background = true
|
|
353
|
+
.background = true,
|
|
354
|
+
.indent = false,
|
|
355
|
+
});
|
|
356
|
+
} else if (!summary) {
|
|
357
|
+
try debug(writer, opts.chance.actions, .{
|
|
358
|
+
.p1_max = p1_max,
|
|
359
|
+
.p2_max = p2_max,
|
|
360
|
+
.dim = true,
|
|
358
361
|
});
|
|
359
362
|
}
|
|
360
363
|
}
|
|
361
364
|
|
|
362
|
-
|
|
365
|
+
p1_dmg.min = p1_max;
|
|
363
366
|
p2_dmg.min = p2_max;
|
|
364
367
|
}
|
|
365
368
|
|
|
366
|
-
|
|
367
|
-
|
|
369
|
+
}}}}}}}}}}}}}}}}}}
|
|
370
|
+
|
|
371
|
+
if (@TypeOf(writer) != @TypeOf(std.io.null_writer)) {
|
|
372
|
+
p.reduce();
|
|
373
|
+
try writer.print(
|
|
374
|
+
" = {s} ({d:.2}%)\n\n",
|
|
375
|
+
.{p, 100 * @as(f128, @floatFromInt(p.p)) / @as(f128, @floatFromInt(p.q))},
|
|
376
|
+
);
|
|
377
|
+
}
|
|
368
378
|
|
|
369
|
-
}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
|
|
370
379
|
}
|
|
371
380
|
|
|
372
381
|
assert(frontier.items.len == i);
|
|
@@ -387,6 +396,114 @@ pub fn transitions(
|
|
|
387
396
|
return stats;
|
|
388
397
|
}
|
|
389
398
|
|
|
399
|
+
pub fn update(
|
|
400
|
+
battle: anytype,
|
|
401
|
+
c1: Choice,
|
|
402
|
+
c2: Choice,
|
|
403
|
+
options: anytype,
|
|
404
|
+
allocator: std.mem.Allocator,
|
|
405
|
+
writer: anytype,
|
|
406
|
+
transition: bool,
|
|
407
|
+
) !Result {
|
|
408
|
+
const durations = options.chance.durations;
|
|
409
|
+
options.chance.reset();
|
|
410
|
+
|
|
411
|
+
if (!pkmn.options.chance or !pkmn.options.calc) return battle.update(c1, c2, options);
|
|
412
|
+
|
|
413
|
+
const original = battle.*;
|
|
414
|
+
|
|
415
|
+
// Perfom the actual update
|
|
416
|
+
const result = battle.update(c1, c2, options);
|
|
417
|
+
var overrides = options.chance.actions;
|
|
418
|
+
const updated = options.chance.durations;
|
|
419
|
+
|
|
420
|
+
if (@TypeOf(battle.rng) == data.PRNG) {
|
|
421
|
+
// Ensure we can encode the diffs in less than MAX_DIFFS bytes.
|
|
422
|
+
var buf: [helpers.MAX_DIFFS]u8 = undefined;
|
|
423
|
+
var stream: protocol.ByteStream = .{ .buffer = &buf };
|
|
424
|
+
const n = try helpers.diff(&original, battle, stream.writer());
|
|
425
|
+
|
|
426
|
+
// Applying the diff to the battle should take us back to the original copy
|
|
427
|
+
// of the battle (ignoring the RNG).
|
|
428
|
+
var patched = battle.*;
|
|
429
|
+
patched.rng = original.rng;
|
|
430
|
+
helpers.patch(&patched, buf[0..n]);
|
|
431
|
+
try expectEqual(original, patched);
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
if (transition) {
|
|
435
|
+
// Ensure we can generate all transitions from the same original state
|
|
436
|
+
// (we must change the battle's RNG from a FixedRNG to a PRNG because
|
|
437
|
+
// the transitions function relies on RNG for discovery of states)
|
|
438
|
+
if (try transitions(unfix(original), c1, c2, allocator, writer, .{
|
|
439
|
+
.durations = durations,
|
|
440
|
+
.cap = true,
|
|
441
|
+
})) |stats| try expect(stats.frontier <= MAX_FRONTIER);
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Demonstrate that we can produce the same state by forcing the RNG to behave the
|
|
445
|
+
// same as we observed.
|
|
446
|
+
var override = pkmn.battle.options(
|
|
447
|
+
protocol.NULL,
|
|
448
|
+
Chance(Rational(u128)){ .probability = .{}, .durations = durations },
|
|
449
|
+
Calc{ .overrides = .{ .actions = overrides } },
|
|
450
|
+
);
|
|
451
|
+
var copy = original;
|
|
452
|
+
var overridden = copy.update(c1, c2, &override);
|
|
453
|
+
try expectEqual(result, overridden);
|
|
454
|
+
try expectEqualActions(overrides, override.chance.actions);
|
|
455
|
+
try expectEqual(updated, override.chance.durations);
|
|
456
|
+
|
|
457
|
+
// The actual battle excluding its RNG field should match a copy updated with
|
|
458
|
+
// overridden RNG (the copy RNG may have advanced because of no-ops)
|
|
459
|
+
copy.rng = battle.rng;
|
|
460
|
+
try expectEqual(battle.*, copy);
|
|
461
|
+
|
|
462
|
+
// Demonstrate that even with the duration rolls changed, the duration
|
|
463
|
+
// fields on their own are sufficient to achieve the same outcomes
|
|
464
|
+
// NB: The actual battle bytes will differ because the durations won't match
|
|
465
|
+
overrides.p1.duration = if (overrides.p1.duration > 0) 2 else 0;
|
|
466
|
+
overrides.p2.duration = if (overrides.p2.duration > 0) 2 else 0;
|
|
467
|
+
|
|
468
|
+
override = pkmn.battle.options(
|
|
469
|
+
protocol.NULL,
|
|
470
|
+
Chance(Rational(u128)){ .probability = .{}, .durations = durations },
|
|
471
|
+
Calc{ .overrides = .{ .actions = overrides } },
|
|
472
|
+
);
|
|
473
|
+
copy = original;
|
|
474
|
+
overridden = copy.update(c1, c2, &override);
|
|
475
|
+
try expectEqual(result, overridden);
|
|
476
|
+
try expectEqualActions(overrides, override.chance.actions);
|
|
477
|
+
try expectEqual(updated, override.chance.durations);
|
|
478
|
+
|
|
479
|
+
return result;
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
fn expectEqualActions(expected: Actions, actual: Actions) !void {
|
|
483
|
+
return expectEqual(expected, actual) catch |e| switch (e) {
|
|
484
|
+
error.TestExpectedEqual => {
|
|
485
|
+
std.debug.print("expected {}, found {}\n", .{ expected, actual });
|
|
486
|
+
return e;
|
|
487
|
+
},
|
|
488
|
+
else => return e,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
fn unfix(actual: anytype) data.Battle(data.PRNG) {
|
|
493
|
+
return .{
|
|
494
|
+
.sides = actual.sides,
|
|
495
|
+
.turn = actual.turn,
|
|
496
|
+
.last_damage = actual.last_damage,
|
|
497
|
+
.last_moves = actual.last_moves,
|
|
498
|
+
.rng = .{ .src = .{
|
|
499
|
+
.seed = if (pkmn.options.showdown)
|
|
500
|
+
0x12345678
|
|
501
|
+
else
|
|
502
|
+
.{ 123, 234, 56, 78, 9, 101, 112, 131, 4 },
|
|
503
|
+
} },
|
|
504
|
+
};
|
|
505
|
+
}
|
|
506
|
+
|
|
390
507
|
fn matches(actions: Actions, i: usize, frontier: []Actions) bool {
|
|
391
508
|
for (frontier, 0..) |f, j| {
|
|
392
509
|
// TODO: is skipping this redundant check worth it?
|
|
@@ -404,23 +521,294 @@ fn err(comptime fmt: []const u8, v: anytype, seed: ?u64) void {
|
|
|
404
521
|
}
|
|
405
522
|
|
|
406
523
|
const Style = struct {
|
|
524
|
+
shape: bool = false,
|
|
525
|
+
p1_max: u9 = 0,
|
|
526
|
+
p2_max: u9 = 0,
|
|
407
527
|
color: ?usize = null,
|
|
408
528
|
bold: bool = false,
|
|
409
529
|
background: bool = false,
|
|
410
530
|
dim: bool = false,
|
|
531
|
+
newline: bool = true,
|
|
532
|
+
indent: bool = true,
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
fn debug(writer: anytype, actions: Actions, style: Style) !void {
|
|
536
|
+
if (style.indent) try writer.writeAll(" ");
|
|
537
|
+
if (tty) {
|
|
538
|
+
const mod: usize = if (style.dim) 2 else 1;
|
|
539
|
+
const background: usize = if (style.background) 4 else 3;
|
|
540
|
+
const color: usize = if (style.color) |c| (c % 6) + 1 else 7;
|
|
541
|
+
|
|
542
|
+
if (style.dim or style.bold) try writer.print("\x1b[{d}m", .{mod});
|
|
543
|
+
try writer.print("\x1b[{d}{d}m", .{ background, color });
|
|
544
|
+
const p1 = if (style.p1_max != 0 and style.p1_max != actions.p1.damage) style.p1_max else 0;
|
|
545
|
+
const p2 = if (style.p2_max != 0 and style.p2_max != actions.p2.damage) style.p2_max else 0;
|
|
546
|
+
if (p1 != 0 or p2 != 0) {
|
|
547
|
+
assert(!style.shape);
|
|
548
|
+
try format(writer, actions, p1, p2);
|
|
549
|
+
} else {
|
|
550
|
+
try actions.fmt(writer, style.shape);
|
|
551
|
+
}
|
|
552
|
+
try writer.writeAll("\x1b[0m");
|
|
553
|
+
} else {
|
|
554
|
+
if (style.dim) try writer.writeAll(" ");
|
|
555
|
+
try actions.fmt(writer, style.shape);
|
|
556
|
+
}
|
|
557
|
+
if (style.newline) try writer.writeByte('\n');
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
fn format(writer: anytype, actions: Actions, p1: u9, p2: u9) !void {
|
|
561
|
+
var input: [1024]u8 = undefined;
|
|
562
|
+
var output: [1024]u8 = undefined;
|
|
563
|
+
|
|
564
|
+
var stream = std.io.fixedBufferStream(&input);
|
|
565
|
+
try actions.fmt(&stream.writer(), false);
|
|
566
|
+
var len = stream.getWritten().len;
|
|
567
|
+
@memcpy(output[0..len], input[0..len]);
|
|
568
|
+
|
|
569
|
+
var find: [16]u8 = undefined;
|
|
570
|
+
var repl: [22]u8 = undefined;
|
|
571
|
+
if (p1 != 0 and actions.p1.damage != 0) {
|
|
572
|
+
const n = try std.fmt.bufPrint(&find, "P1 = (damage:{d}", .{actions.p1.damage});
|
|
573
|
+
const r = try std.fmt.bufPrint(&repl, "P1 = (damage:{d}…{d}", .{ actions.p1.damage, p1 });
|
|
574
|
+
assert(std.mem.replace(u8, input[0..len], n, r, output[0 .. len + 6]) == 1);
|
|
575
|
+
@memcpy(input[0 .. len + 6], output[0 .. len + 6]);
|
|
576
|
+
len += 6;
|
|
577
|
+
}
|
|
578
|
+
if (p2 != 0 and actions.p2.damage != 0) {
|
|
579
|
+
const n = try std.fmt.bufPrint(&find, "P2 = (damage:{d}", .{actions.p2.damage});
|
|
580
|
+
const r = try std.fmt.bufPrint(&repl, "P2 = (damage:{d}…{d}", .{ actions.p2.damage, p2 });
|
|
581
|
+
assert(std.mem.replace(u8, input[0..len], n, r, output[0 .. len + 6]) == 1);
|
|
582
|
+
len += 6;
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
try writer.writeAll(output[0..len]);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
const NONE = @intFromEnum(Optional(bool).None);
|
|
589
|
+
|
|
590
|
+
/// Helper functions that efficiently return valid ranges for various RNG events based on the
|
|
591
|
+
/// state of an `Action` and other events to be used to construct a "transitions" function.
|
|
592
|
+
pub const Rolls = struct {
|
|
593
|
+
const PLAYER_NONE = [_]Optional(Player){.None};
|
|
594
|
+
const PLAYERS = [_]Optional(Player){ .P1, .P2 };
|
|
595
|
+
|
|
596
|
+
/// Returns a slice with the correct range of values for speed ties given the `action` state.
|
|
597
|
+
pub fn speedTie(action: Action) []const Optional(Player) {
|
|
598
|
+
return if (action.speed_tie == .None) &PLAYER_NONE else &PLAYERS;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
const BOOL_NONE = [_]Optional(bool){.None};
|
|
602
|
+
const BOOLS = [_]Optional(bool){ .false, .true };
|
|
603
|
+
|
|
604
|
+
/// Returns a slice with the correct range of values for hits given the `action` state
|
|
605
|
+
/// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
|
|
606
|
+
pub fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
|
|
607
|
+
if (parent == .true) return &BOOL_NONE;
|
|
608
|
+
return if (action.hit == .None) &BOOL_NONE else &BOOLS;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/// Returns a slice with the correct range of values for critical hits given the `action` state
|
|
612
|
+
/// and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
613
|
+
pub fn criticalHit(action: Action, parent: Optional(bool)) []const Optional(bool) {
|
|
614
|
+
if (parent == .false) return &BOOL_NONE;
|
|
615
|
+
return if (action.critical_hit == .None) &BOOL_NONE else &BOOLS;
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
/// Returns a slice with the correct range of values for secondary chances hits given the
|
|
619
|
+
/// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
620
|
+
pub fn secondaryChance(action: Action, parent: Optional(bool)) []const Optional(bool) {
|
|
621
|
+
if (parent == .false) return &BOOL_NONE;
|
|
622
|
+
return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/// The min and max bounds on iteration over damage rolls.
|
|
626
|
+
pub const Range = struct { min: u9, max: u9 };
|
|
627
|
+
|
|
628
|
+
/// Returns the range bounding damage rolls given the `action` state and the state of
|
|
629
|
+
/// the `parent` (whether the player's Pokémon's move hit).
|
|
630
|
+
pub fn damage(action: Action, parent: Optional(bool)) Range {
|
|
631
|
+
return if (parent == .false or action.damage == 0)
|
|
632
|
+
.{ .min = 0, .max = 1 }
|
|
633
|
+
else
|
|
634
|
+
.{ .min = 217, .max = 256 };
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/// Returns the max damage roll which will produce the same damage as `roll`
|
|
638
|
+
/// given the base damage in `summaries`.
|
|
639
|
+
pub fn coalesce(player: Player, roll: u8, summaries: *Summaries, cap: bool) !u8 {
|
|
640
|
+
if (roll == 0) return roll;
|
|
641
|
+
|
|
642
|
+
const dmg = summaries.get(player.foe()).damage;
|
|
643
|
+
if (dmg.base == 0 or (cap and dmg.capped)) return 255;
|
|
644
|
+
|
|
645
|
+
// Closed form solution for max damage roll provided by Orion Taylor (taylorott)
|
|
646
|
+
return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
/// Returns a slice with the correct range of values for confused.
|
|
650
|
+
pub fn confused(action: Action) []const Optional(bool) {
|
|
651
|
+
return if (action.confused == .None) &BOOL_NONE else &BOOLS;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/// Returns a slice with the correct range of values for paralysis given the `action` state
|
|
655
|
+
/// and the state of the `parent` (whether the player's Pokémon was confused).
|
|
656
|
+
pub fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
|
|
657
|
+
if (parent == .true) return &BOOL_NONE;
|
|
658
|
+
return if (action.paralyzed == .None) &BOOL_NONE else &BOOLS;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const SLOT_NONE = [_]u3{0};
|
|
662
|
+
const SLOT = [_]u3{ 1, 2, 3, 4 };
|
|
663
|
+
|
|
664
|
+
/// Returns a slice with a range of values for move slots given the `action` state
|
|
665
|
+
/// and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
666
|
+
///
|
|
667
|
+
/// These slots may or **may not be valid** as slots may be unset / have 0 PP.
|
|
668
|
+
pub fn moveSlot(action: Action, parent: Optional(bool)) []const u3 {
|
|
669
|
+
if (parent == .false) return &SLOT_NONE;
|
|
670
|
+
return if (action.move_slot == 0) &SLOT_NONE else &SLOT;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
const MULTI_NONE = [_]u4{0};
|
|
674
|
+
const MULTI = [_]u4{ 2, 3, 4, 5 };
|
|
675
|
+
|
|
676
|
+
/// Returns a slice with the correct range of values for multi hit given the `action` state
|
|
677
|
+
/// and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
678
|
+
pub fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
|
|
679
|
+
if (parent == .false) return &MULTI_NONE;
|
|
680
|
+
return if (action.multi_hit == 0) &MULTI_NONE else &MULTI;
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
const PSYWAVE_NONE = [_]u8{0};
|
|
684
|
+
const PSYWAVE = init: {
|
|
685
|
+
var rolls: [150]u8 = undefined;
|
|
686
|
+
for (0..150) |i| rolls[i] = i + 1;
|
|
687
|
+
break :init rolls;
|
|
688
|
+
};
|
|
689
|
+
|
|
690
|
+
/// Returns a slice with the correct range of values for psywave given the `action` state,
|
|
691
|
+
/// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
692
|
+
pub fn psywave(
|
|
693
|
+
action: Action,
|
|
694
|
+
side: *data.Side,
|
|
695
|
+
parent: Optional(bool),
|
|
696
|
+
) []const u8 {
|
|
697
|
+
if (parent == .false) return &PSYWAVE_NONE;
|
|
698
|
+
return if (action.psywave == 0)
|
|
699
|
+
&PSYWAVE_NONE
|
|
700
|
+
else
|
|
701
|
+
PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
const MOVE_NONE = [_]data.Move{.None};
|
|
705
|
+
|
|
706
|
+
/// Returns a slice with the correct range of values for metronome given the `action` state.
|
|
707
|
+
pub fn metronome(action: Action) []const data.Move {
|
|
708
|
+
return if (action.metronome == .None) &MOVE_NONE else &data.Move.METRONOME;
|
|
709
|
+
}
|
|
411
710
|
};
|
|
412
711
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
712
|
+
test "Rolls.speedTie" {
|
|
713
|
+
const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
|
|
714
|
+
try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
|
|
715
|
+
try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
test "Rolls.damage" {
|
|
719
|
+
const actions: Actions = .{ .p2 = .{ .damage = 221 } };
|
|
720
|
+
try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
|
|
721
|
+
try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
|
|
722
|
+
try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
test "Rolls.coalesce" {
|
|
726
|
+
var summaries = Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
|
|
727
|
+
try expectEqual(0, try Rolls.coalesce(.P2, 0, &summaries, false));
|
|
728
|
+
try expectEqual(241, try Rolls.coalesce(.P2, 238, &summaries, false));
|
|
729
|
+
try expectEqual(255, try Rolls.coalesce(.P2, 238, &summaries, true));
|
|
730
|
+
summaries.p1.damage.final = 74;
|
|
731
|
+
try expectEqual(217, try Rolls.coalesce(.P2, 217, &summaries, false));
|
|
732
|
+
try expectEqual(255, try Rolls.coalesce(.P2, 217, &summaries, true));
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
test "Rolls.hit" {
|
|
736
|
+
const actions: Actions = .{ .p2 = .{ .hit = .true } };
|
|
737
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
|
|
738
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
|
|
739
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
|
|
740
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
test "Rolls.secondaryChance" {
|
|
744
|
+
const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
|
|
745
|
+
try expectEqualSlices(
|
|
746
|
+
Optional(bool),
|
|
747
|
+
&.{ .false, .true },
|
|
748
|
+
Rolls.secondaryChance(actions.p1, .None),
|
|
749
|
+
);
|
|
750
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
|
|
751
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
test "Rolls.criticalHit" {
|
|
755
|
+
const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
|
|
756
|
+
try expectEqualSlices(
|
|
757
|
+
Optional(bool),
|
|
758
|
+
&.{ .false, .true },
|
|
759
|
+
Rolls.criticalHit(actions.p1, .None),
|
|
760
|
+
);
|
|
761
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
|
|
762
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
test "Rolls.confused" {
|
|
766
|
+
const actions: Actions = .{ .p2 = .{ .confused = .true } };
|
|
767
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1));
|
|
768
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2));
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
test "Rolls.paralyzed" {
|
|
772
|
+
const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
|
|
773
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
|
|
774
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
|
|
775
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
|
|
776
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
test "Rolls.moveSlot" {
|
|
780
|
+
const actions: Actions = .{ .p2 = .{ .move_slot = 3 } };
|
|
781
|
+
try expectEqualSlices(u3, &.{0}, Rolls.moveSlot(actions.p1, .None));
|
|
782
|
+
try expectEqualSlices(u3, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
|
|
783
|
+
try expectEqualSlices(u3, &.{0}, Rolls.moveSlot(actions.p2, .false));
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
test "Rolls.multiHit" {
|
|
787
|
+
const actions: Actions = .{ .p2 = .{ .multi_hit = 3 } };
|
|
788
|
+
try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
|
|
789
|
+
try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
|
|
790
|
+
try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
test "Rolls.metronome" {
|
|
794
|
+
const actions: Actions = .{ .p2 = .{ .metronome = .Surf } };
|
|
795
|
+
try expectEqualSlices(data.Move, &.{.None}, Rolls.metronome(actions.p1));
|
|
796
|
+
try expectEqual(@as(data.Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
test "Rolls.psywave" {
|
|
800
|
+
const actions: Actions = .{ .p2 = .{ .psywave = 79 } };
|
|
801
|
+
var side = helpers.Side.init(&[_]helpers.Pokemon{.{
|
|
802
|
+
.species = .Bulbasaur,
|
|
803
|
+
.level = 100,
|
|
804
|
+
.moves = &[_]data.Move{.Tackle},
|
|
805
|
+
}});
|
|
806
|
+
|
|
807
|
+
try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
|
|
808
|
+
var rolls = Rolls.psywave(actions.p2, &side, .None);
|
|
809
|
+
try expectEqual(150, rolls[rolls.len - 1]);
|
|
810
|
+
side.stored().level = 81;
|
|
811
|
+
rolls = Rolls.psywave(actions.p2, &side, .None);
|
|
812
|
+
try expectEqual(121, rolls[rolls.len - 1]);
|
|
813
|
+
try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
|
|
426
814
|
}
|