@pkmn/engine 0.1.0-dev.fb5bf955 → 0.1.0-dev.fb6653d6

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.
Files changed (85) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +115 -76
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +44 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +97 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +27 -5
  13. package/build/pkg/addon.js +42 -73
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/ids.json +12 -12
  16. package/build/pkg/data/layout.json +40 -5
  17. package/build/pkg/data/protocol.json +84 -13
  18. package/build/pkg/data.d.ts +27 -5
  19. package/build/pkg/data.js.map +1 -1
  20. package/build/pkg/gen1.d.ts +16 -19
  21. package/build/pkg/gen1.js +54 -42
  22. package/build/pkg/gen1.js.map +1 -1
  23. package/build/pkg/index.d.ts +55 -103
  24. package/build/pkg/index.js +6 -63
  25. package/build/pkg/index.js.map +1 -1
  26. package/build/pkg/protocol.d.ts +24 -3
  27. package/build/pkg/protocol.js +127 -104
  28. package/build/pkg/protocol.js.map +1 -1
  29. package/build/tools/debug.js +36 -69
  30. package/build/tools/debug.js.map +1 -1
  31. package/build/tools/display/debug.jsx +141 -0
  32. package/build/tools/display/debug.jsx.map +1 -0
  33. package/build/tools/display/index.js +142 -0
  34. package/build/tools/display/index.js.map +1 -0
  35. package/build/tools/display/ui.jsx +349 -0
  36. package/build/tools/display/ui.jsx.map +1 -0
  37. package/build/tools/display/util.js +18 -0
  38. package/build/tools/display/util.js.map +1 -0
  39. package/build.zig +373 -152
  40. package/package.json +68 -120
  41. package/src/bin/install-pkmn-engine +41 -26
  42. package/src/bin/pkmn-debug +29 -6
  43. package/src/lib/bindings/node.zig +121 -0
  44. package/src/lib/bindings/wasm.zig +58 -0
  45. package/src/lib/common/array.zig +77 -0
  46. package/src/lib/common/battle.zig +17 -0
  47. package/src/lib/common/data.zig +19 -31
  48. package/src/lib/common/debug.zig +58 -17
  49. package/src/lib/common/js.zig +120 -0
  50. package/src/lib/common/optional.zig +71 -0
  51. package/src/lib/common/options.zig +18 -3
  52. package/src/lib/common/protocol.zig +947 -407
  53. package/src/lib/common/rational.zig +449 -0
  54. package/src/lib/common/rng.zig +31 -24
  55. package/src/lib/common/util.zig +61 -0
  56. package/src/lib/gen1/calc.zig +953 -0
  57. package/src/lib/gen1/chance.zig +1185 -0
  58. package/src/lib/gen1/data/moves.zig +203 -193
  59. package/src/lib/gen1/data/species.zig +6 -8
  60. package/src/lib/gen1/data/types.zig +11 -12
  61. package/src/lib/gen1/data.zig +100 -89
  62. package/src/lib/gen1/helpers.zig +139 -50
  63. package/src/lib/gen1/mechanics.zig +1344 -907
  64. package/src/lib/gen2/calc.zig +153 -0
  65. package/src/lib/gen2/chance.zig +902 -0
  66. package/src/lib/gen2/data/items.zig +15 -23
  67. package/src/lib/gen2/data/moves.zig +665 -573
  68. package/src/lib/gen2/data/species.zig +3 -5
  69. package/src/lib/gen2/data/types.zig +138 -36
  70. package/src/lib/gen2/data.zig +134 -99
  71. package/src/lib/gen2/generated.zig +801 -0
  72. package/src/lib/gen2/helpers.zig +59 -44
  73. package/src/lib/gen2/mechanics.zig +3582 -30
  74. package/src/lib/node.zig +18 -0
  75. package/src/lib/pkmn.zig +93 -12
  76. package/src/lib/wasm.zig +17 -0
  77. package/src/tools/display/dom.ts +96 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -382
  80. package/build/test/display.js.map +0 -1
  81. package/src/lib/binding/node.zig +0 -229
  82. package/src/lib/binding/wasm.zig +0 -10
  83. package/src/lib/gen1/README.md +0 -784
  84. package/src/lib/gen2/README.md +0 -309
  85. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -0,0 +1,1185 @@
1
+ const DEBUG = @import("../common/debug.zig").print;
2
+
3
+ const array = @import("../common/array.zig");
4
+ const common = @import("../common/data.zig");
5
+ const data = @import("data.zig");
6
+ const optional = @import("../common/optional.zig");
7
+ const options = @import("../common/options.zig");
8
+ const rational = @import("../common/rational.zig");
9
+ const std = @import("std");
10
+ const util = @import("../common/util.zig");
11
+
12
+ const Array = array.Array;
13
+ const assert = std.debug.assert;
14
+ const enabled = options.chance;
15
+ const expect = std.testing.expect;
16
+ const expectEqual = std.testing.expectEqual;
17
+ const isPointerTo = util.isPointerTo;
18
+ const Move = data.Move;
19
+ const MoveSlot = data.MoveSlot;
20
+ const Optional = optional.Optional;
21
+ const Player = common.Player;
22
+ const PointerType = util.PointerType;
23
+ const print = std.debug.print;
24
+ const showdown = options.showdown;
25
+
26
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
27
+ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
28
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
29
+
30
+ /// Actions taken by a hypothetical "chance player" that convey information about which RNG events
31
+ /// were observed during a Generation I battle `update`. This can additionally be provided as input
32
+ /// to the `update` call to override the normal behavior of the RNG in order to force specific
33
+ /// outcomes.
34
+ pub const Actions = extern struct {
35
+ /// Information about the RNG activity for Player 1.
36
+ p1: Action = .{},
37
+ /// Information about the RNG activity for Player 2.
38
+ p2: Action = .{},
39
+
40
+ comptime {
41
+ assert(@sizeOf(Actions) == 16);
42
+ }
43
+
44
+ /// Returns the `Action` for the given `player`.
45
+ pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Action) {
46
+ assert(isPointerTo(self, Actions));
47
+ return if (player == .P1) &self.p1 else &self.p2;
48
+ }
49
+
50
+ /// Returns true if `a` is equal to `b`.
51
+ pub fn eql(a: Actions, b: Actions) bool {
52
+ return @as(u128, @bitCast(a)) == @as(u128, @bitCast(b));
53
+ }
54
+
55
+ /// Returns true if `a` has the same "shape" as `b`, where `Actions` are defined to have the
56
+ /// same shape if they have the same fields set (though those fields need not necessarily be
57
+ /// set to the same value).
58
+ pub fn matches(a: Actions, b: Actions) bool {
59
+ inline for (@field(@typeInfo(Actions), @tagName(Struct)).fields) |player| {
60
+ inline for (@field(@typeInfo(Action), @tagName(Struct)).fields) |field| {
61
+ const a_val = @field(@field(a, player.name), field.name);
62
+ const b_val = @field(@field(b, player.name), field.name);
63
+
64
+ switch (@typeInfo(@TypeOf(a_val))) {
65
+ Enum => if ((@intFromEnum(a_val) > 0) != (@intFromEnum(b_val) > 0))
66
+ return false,
67
+ Int => if ((a_val > 0) != (b_val > 0)) return false,
68
+ else => unreachable,
69
+ }
70
+ }
71
+ }
72
+ return true;
73
+ }
74
+
75
+ /// TODO
76
+ pub fn matchesAny(a: Actions, bs: []Actions, i: usize) bool {
77
+ for (bs, 0..) |b, j| {
78
+ // TODO: is skipping this redundant check worth it?
79
+ if (i == j) continue;
80
+ if (b.matches(a)) return true;
81
+ }
82
+ return false;
83
+ }
84
+
85
+ /// TODO
86
+ pub fn relax(actions: Actions) Actions {
87
+ if (!options.overwrite) return actions;
88
+ var a = actions;
89
+ if (a.p1.confusion == .overwritten) a.p1.confusion = .continuing;
90
+ if (a.p2.confusion == .overwritten) a.p2.confusion = .continuing;
91
+ return a;
92
+ }
93
+
94
+ pub fn fmt(self: Actions, writer: anytype, shape: bool) !void {
95
+ try writer.writeAll("<P1 = ");
96
+ try self.p1.fmt(writer, shape);
97
+ try writer.writeAll(", P2 = ");
98
+ try self.p2.fmt(writer, shape);
99
+ try writer.writeAll(">");
100
+ }
101
+
102
+ pub fn format(a: Actions, comptime f: []const u8, o: std.fmt.FormatOptions, w: anytype) !void {
103
+ _ = .{ f, o };
104
+ try fmt(a, w, false);
105
+ }
106
+ };
107
+
108
+ test Actions {
109
+ const a: Actions = .{ .p1 = .{ .hit = .true, .critical_hit = .false, .damage = 245 } };
110
+ const b: Actions = .{ .p1 = .{ .hit = .false, .critical_hit = .true, .damage = 246 } };
111
+ const c: Actions = .{ .p1 = .{ .hit = .true } };
112
+
113
+ try expect(a.eql(a));
114
+ try expect(!a.eql(b));
115
+ try expect(!b.eql(a));
116
+ try expect(!a.eql(c));
117
+ try expect(!c.eql(a));
118
+
119
+ try expect(a.matches(a));
120
+ try expect(a.matches(b));
121
+ try expect(b.matches(a));
122
+ try expect(!a.matches(c));
123
+ try expect(!c.matches(a));
124
+ }
125
+
126
+ /// Observation made about a duration - whether the duration has started, been continued, or ended.
127
+ pub const Observation = enum { started, continuing, ended };
128
+ /// An extension of Observation to account for "hidden" confusion.
129
+ pub const Confusion = enum {
130
+ started,
131
+ continuing,
132
+ ended,
133
+ /// An indicator that an existing confusion duration has been overwritten by a new confusion
134
+ /// duration due to a Thrashing move ending. This is not immediately observable to an opponent,
135
+ /// to compute probabilities from an opponent's point of view based solely on public information
136
+ /// extra work must be done to correct for the information leak
137
+ overwritten,
138
+ };
139
+
140
+ /// Information about the RNG that was observed during a Generation I battle `update` for a
141
+ /// single player.
142
+ pub const Action = packed struct(u64) {
143
+ /// If not 0, the roll to be returned Rolls.damage.
144
+ damage: u8 = 0,
145
+
146
+ /// If not None, the value to return for Rolls.hit.
147
+ hit: Optional(bool) = .None,
148
+ /// If not None, the value to be returned by Rolls.criticalHit.
149
+ critical_hit: Optional(bool) = .None,
150
+ /// If not None, the value to be returned for
151
+ /// Rolls.{confusionChance,secondaryChance,poisonChance}.
152
+ secondary_chance: Optional(bool) = .None,
153
+ /// If not None, the Player to be returned by Rolls.speedTie.
154
+ speed_tie: Optional(Player) = .None,
155
+
156
+ /// If not None, the value to return for Rolls.confused.
157
+ confused: Optional(bool) = .None,
158
+ /// If not None, the value to return for Rolls.paralyzed.
159
+ paralyzed: Optional(bool) = .None,
160
+ /// If not 0, the value to be returned by Rolls.distribution in the case of binding moves
161
+ /// or Rolls.{sleepDuration,disableDuration,confusionDuration,attackingDuration} otherwise.
162
+ duration: u4 = 0,
163
+
164
+ /// TODO
165
+ sleep: Optional(Observation) = .None,
166
+ /// TODO
167
+ confusion: Optional(Confusion) = .None,
168
+ /// TODO
169
+ disable: Optional(Observation) = .None,
170
+ /// TODO
171
+ attacking: Optional(Observation) = .None,
172
+ /// TODO
173
+ binding: Optional(Observation) = .None,
174
+
175
+ _: u1 = 0,
176
+
177
+ /// If not 0, the move slot (1-4) to return in Rolls.moveSlot. If present as an override,
178
+ /// invalid values (eg. due to empty move slots or 0 PP) will be ignored.
179
+ move_slot: u4 = 0,
180
+
181
+ /// TODO
182
+ pp: u4 = 0,
183
+ /// If not 0, the value (2-5) to return for Rolls.distribution for multi hit.
184
+ multi_hit: u4 = 0,
185
+
186
+ /// If not 0, psywave - 1 should be returned as the damage roll for Rolls.psywave.
187
+ psywave: u8 = 0,
188
+
189
+ /// If not None, the Move to return for Rolls.metronome.
190
+ metronome: Move = .None,
191
+
192
+ pub const Field = std.meta.FieldEnum(Action);
193
+
194
+ pub fn format(a: Action, comptime f: []const u8, o: std.fmt.FormatOptions, w: anytype) !void {
195
+ _ = .{ f, o };
196
+ try fmt(a, w, false);
197
+ }
198
+
199
+ pub fn fmt(self: Action, writer: anytype, shape: bool) !void {
200
+ try writer.writeByte('(');
201
+ var printed = false;
202
+ inline for (@field(@typeInfo(Action), @tagName(Struct)).fields) |field| {
203
+ const val = @field(self, field.name);
204
+ switch (@typeInfo(@TypeOf(val))) {
205
+ Enum => if (val != .None) {
206
+ if (printed) try writer.writeAll(", ");
207
+ if (shape) {
208
+ try writer.print("{s}:?", .{field.name});
209
+ } else if (@TypeOf(val) == Optional(bool)) {
210
+ try writer.print("{s}{s}", .{
211
+ if (val == .false) "!" else "",
212
+ field.name,
213
+ });
214
+ } else if (@TypeOf(val) == Optional(Observation) or
215
+ @TypeOf(val) == Optional(Confusion))
216
+ {
217
+ try writer.print("{s}{s}", .{
218
+ ([_][]const u8{ "+", "", "-", "#" })[@intFromEnum(val) - 1],
219
+ field.name,
220
+ });
221
+ } else {
222
+ try writer.print("{s}:{s}", .{ field.name, @tagName(val) });
223
+ }
224
+ printed = true;
225
+ },
226
+ Int => if (val != 0) {
227
+ if (printed) try writer.writeAll(", ");
228
+ if (shape) {
229
+ try writer.print("{s}:?", .{field.name});
230
+ } else {
231
+ try writer.print("{s}:{d}", .{ field.name, val });
232
+ }
233
+ printed = true;
234
+ },
235
+ else => unreachable,
236
+ }
237
+ }
238
+ try writer.writeByte(')');
239
+ }
240
+ };
241
+
242
+ /// TODO
243
+ pub const Durations = extern struct {
244
+ /// TODO
245
+ p1: Duration = .{},
246
+ /// TODO
247
+ p2: Duration = .{},
248
+
249
+ comptime {
250
+ assert(@sizeOf(Durations) == 8);
251
+ }
252
+
253
+ /// TODO
254
+ pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Duration) {
255
+ assert(isPointerTo(self, Durations));
256
+ return if (player == .P1) &self.p1 else &self.p2;
257
+ }
258
+
259
+ pub fn format(
260
+ self: Durations,
261
+ comptime fmt: []const u8,
262
+ opts: std.fmt.FormatOptions,
263
+ writer: anytype,
264
+ ) !void {
265
+ _ = .{ fmt, opts };
266
+ try writer.print("[P1 = {}, P2 = {}]", .{ self.p1, self.p2 });
267
+ }
268
+ };
269
+
270
+ pub const Sleeps = Array(6, u3);
271
+
272
+ /// TODO
273
+ pub const Duration = packed struct(u32) {
274
+ sleeps: Sleeps.T = 0,
275
+ confusion: u3 = 0,
276
+ disable: u4 = 0,
277
+ attacking: u3 = 0,
278
+ binding: u3 = 0,
279
+
280
+ _: u1 = 0,
281
+
282
+ pub fn format(
283
+ self: Duration,
284
+ comptime fmt: []const u8,
285
+ opts: std.fmt.FormatOptions,
286
+ writer: anytype,
287
+ ) !void {
288
+ _ = .{ fmt, opts };
289
+ try writer.writeByte('(');
290
+ var printed = false;
291
+ inline for (@field(@typeInfo(Duration), @tagName(Struct)).fields) |field| {
292
+ const val = @field(self, field.name);
293
+ switch (@typeInfo(@TypeOf(val))) {
294
+ Int => if (val != 0) {
295
+ if (printed) try writer.writeAll(", ");
296
+ if (comptime std.mem.eql(u8, field.name, "sleeps")) {
297
+ try writer.print("{s}:[", .{field.name});
298
+ for (0..6) |i| {
299
+ if (i != 0) try writer.writeByte(',');
300
+ try writer.print("{d}", .{Sleeps.get(val, i)});
301
+ }
302
+ try writer.writeAll("]");
303
+ } else {
304
+ try writer.print("{s}:{d}", .{ field.name, val });
305
+ }
306
+ printed = true;
307
+ },
308
+ else => unreachable,
309
+ }
310
+ }
311
+ try writer.writeByte(')');
312
+ }
313
+ };
314
+
315
+ pub const Commit = enum { hit, miss, binding, err, glitch };
316
+
317
+ const uN = if (options.miss) u8 else u9;
318
+
319
+ /// Tracks chance actions and their associated probability during a Generation I battle update when
320
+ /// `options.chance` is enabled.
321
+ pub fn Chance(comptime Rational: type) type {
322
+ return struct {
323
+ const Self = @This();
324
+
325
+ /// The probability of the actions taken by a hypothetical "chance player" occurring.
326
+ probability: Rational,
327
+ /// The actions taken by a hypothetical "chance player" that convey information about which
328
+ /// RNG events were observed during a battle `update`.
329
+ actions: Actions = .{},
330
+ /// TODO
331
+ durations: Durations = .{},
332
+
333
+ // In many cases on both the cartridge and on Pokémon Showdown rolls are made even though
334
+ // later checks render their result irrelevant (missing when the target is actually immune,
335
+ // rolling for damage when the move is later revealed to have missed, etc). We can't reorder
336
+ // the logic to ensure we only ever make rolls when required due to our desire to maintain
337
+ // RNG frame accuracy, so instead we save these "pending" updates to the probability and
338
+ // only commit them once we know they are relevant (a naive solution would be to instead
339
+ // update the probability eagerly and later "undo" the update by multiplying by the inverse
340
+ // but this would be more costly and also error prone in the presence of
341
+ // overflow/normalization).
342
+ //
343
+ // This design deliberately make use of the fact that recursive moves on the cartridge may
344
+ // overwrite critical hit information multiple times as only the last update actually
345
+ // matters for the purposes of the logical results.
346
+ //
347
+ // Finally, because each player's actions are processed sequentially we only need a single
348
+ // shared structure to track this information (though it needs to be cleared appropriately)
349
+ pending: if (showdown) struct {} else struct {
350
+ crit: bool = false,
351
+ crit_probablity: u8 = 0,
352
+ damage_roll: u8 = 0,
353
+ hit: bool = false,
354
+ hit_probablity: uN = 0,
355
+ glitch: bool = false,
356
+ } = .{},
357
+
358
+ /// Possible error returned by operations tracking chance probability.
359
+ pub const Error = Rational.Error;
360
+
361
+ /// Convenience helper to clear fields which typically should be cleared between updates.
362
+ pub fn reset(self: *Self) void {
363
+ if (!enabled) return;
364
+
365
+ self.probability.reset();
366
+ self.actions = .{};
367
+ self.pending = .{};
368
+ }
369
+
370
+ pub fn commit(self: *Self, player: Player, kind: Commit) Error!void {
371
+ if (!enabled) return;
372
+
373
+ assert(!showdown);
374
+
375
+ if (kind == .glitch and !self.pending.glitch) return;
376
+
377
+ var action = self.actions.get(player);
378
+
379
+ if (kind == .err and self.pending.crit_probablity != 0) {
380
+ try self.probability.update(self.pending.crit_probablity, 256);
381
+ action.critical_hit = if (self.pending.crit) .true else .false;
382
+ return;
383
+ }
384
+
385
+ // Always commit the hit result if we make it here (commit won't be called at all if the
386
+ // target is immune*/behind a sub/etc)
387
+ if (self.pending.hit_probablity != 0) {
388
+ try self.probability.update(self.pending.hit_probablity, 256);
389
+ action.hit = if (self.pending.hit) .true else .false;
390
+ }
391
+
392
+ // If the move actually lands we can commit any past critical hit / damage rolls. Note
393
+ // we can't rely on the presence or absence of a damage roll to determine whether a
394
+ // "critical hit" was spurious or not as moves that do 1 or less damage don't roll for
395
+ // damage but can still crit (instead, whether or not a roll is deemed to be a no-op is
396
+ // passed to the critical hit helper).
397
+ //
398
+ // There is a special edge case with the division-by-zero freeze where we need to commit
399
+ // the status of critical hit roll the occured before the move missed to avoid missing
400
+ // out on the possibility that the alternative branch encountered an error
401
+ if (kind != .hit and !self.pending.glitch) {
402
+ return assert(!self.pending.crit or self.pending.crit_probablity > 0);
403
+ }
404
+
405
+ if (self.pending.crit_probablity != 0) {
406
+ try self.probability.update(self.pending.crit_probablity, 256);
407
+ action.critical_hit = if (self.pending.crit) .true else .false;
408
+ } else assert(!self.pending.glitch);
409
+
410
+ if (self.pending.damage_roll > 0 and (!self.pending.glitch or self.pending.hit)) {
411
+ try self.probability.update(1, 39);
412
+ action.damage = self.pending.damage_roll;
413
+ }
414
+ }
415
+
416
+ pub fn glitch(self: *Self) void {
417
+ if (!enabled) return;
418
+
419
+ assert(!showdown);
420
+ self.pending.glitch = true;
421
+ }
422
+
423
+ pub fn clearPending(self: *Self) void {
424
+ if (!enabled) return;
425
+
426
+ assert(!showdown);
427
+ self.pending = .{};
428
+ }
429
+
430
+ pub fn switched(self: *Self, player: Player, slot: u8) void {
431
+ if (!enabled) return;
432
+
433
+ assert(slot >= 1 and slot <= 6);
434
+
435
+ var d = self.durations.get(player);
436
+
437
+ const out = Sleeps.get(d.sleeps, 0);
438
+ d.sleeps = Sleeps.set(d.sleeps, 0, Sleeps.get(d.sleeps, slot - 1));
439
+ d.sleeps = Sleeps.set(d.sleeps, slot - 1, out);
440
+
441
+ d.confusion = 0;
442
+ d.disable = 0;
443
+ d.attacking = 0;
444
+ d.binding = 0;
445
+
446
+ self.durations.get(player.foe()).binding = 0;
447
+ }
448
+
449
+ pub fn speedTie(self: *Self, p1: bool) Error!void {
450
+ if (!enabled) return;
451
+
452
+ try self.probability.update(1, 2);
453
+ self.actions.p1.speed_tie = if (p1) .P1 else .P2;
454
+ self.actions.p2.speed_tie = self.actions.p1.speed_tie;
455
+ }
456
+
457
+ pub fn hit(self: *Self, player: Player, ok: bool, accuracy: uN) Error!void {
458
+ if (!enabled) return;
459
+
460
+ const p = if (ok) accuracy else @as(u8, @intCast(256 - @as(u9, accuracy)));
461
+ if (showdown) {
462
+ try self.probability.update(p, 256);
463
+ self.actions.get(player).hit = if (ok) .true else .false;
464
+ } else {
465
+ self.pending.hit = ok;
466
+ self.pending.hit_probablity = p;
467
+ }
468
+ }
469
+
470
+ pub fn criticalHit(self: *Self, player: Player, crit: bool, rate: u8) Error!void {
471
+ if (!enabled) return;
472
+
473
+ const n = if (crit) rate else @as(u8, @intCast(256 - @as(u9, rate)));
474
+ if (showdown) {
475
+ try self.probability.update(n, 256);
476
+ self.actions.get(player).critical_hit = if (crit) .true else .false;
477
+ } else {
478
+ self.pending.crit = crit;
479
+ self.pending.crit_probablity = n;
480
+ }
481
+ }
482
+
483
+ pub fn damage(self: *Self, player: Player, roll: u8) Error!void {
484
+ if (!enabled) return;
485
+
486
+ if (showdown) {
487
+ try self.probability.update(1, 39);
488
+ self.actions.get(player).damage = roll;
489
+ } else {
490
+ self.pending.damage_roll = roll;
491
+ }
492
+ }
493
+
494
+ pub fn secondaryChance(self: *Self, player: Player, proc: bool, rate: u8) Error!void {
495
+ if (!enabled) return;
496
+
497
+ const n = if (proc) rate else @as(u8, @intCast(256 - @as(u9, rate)));
498
+ try self.probability.update(n, 256);
499
+ self.actions.get(player).secondary_chance = if (proc) .true else .false;
500
+ }
501
+
502
+ pub fn confused(self: *Self, player: Player, cfz: bool) Error!void {
503
+ if (!enabled) return;
504
+
505
+ try self.probability.update(1, 2);
506
+ self.actions.get(player).confused = if (cfz) .true else .false;
507
+ }
508
+
509
+ pub fn paralyzed(self: *Self, player: Player, par: bool) Error!void {
510
+ if (!enabled) return;
511
+
512
+ try self.probability.update(@as(u8, if (par) 1 else 3), 4);
513
+ self.actions.get(player).paralyzed = if (par) .true else .false;
514
+ }
515
+
516
+ pub fn moveSlot(
517
+ self: *Self,
518
+ player: Player,
519
+ slot: u4,
520
+ ms: []const MoveSlot,
521
+ n: u4,
522
+ ) Error!void {
523
+ if (!enabled) return;
524
+
525
+ const denominator = if (n != 0) n else denominator: {
526
+ var i: usize = ms.len;
527
+ while (i > 0) {
528
+ i -= 1;
529
+ if (ms[i].id != .None) break :denominator i + 1;
530
+ }
531
+ unreachable;
532
+ };
533
+
534
+ if (denominator != 1) try self.probability.update(1, denominator);
535
+ self.actions.get(player).move_slot = @intCast(slot);
536
+ }
537
+
538
+ pub fn multiHit(self: *Self, player: Player, n: u3) Error!void {
539
+ if (!enabled) return;
540
+
541
+ try self.probability.update(@as(u8, if (n > 3) 1 else 3), 8);
542
+ self.actions.get(player).multi_hit = n;
543
+ }
544
+
545
+ pub fn duration(self: *Self, player: Player, turns: u4) void {
546
+ if (!enabled) return;
547
+
548
+ self.actions.get(player).duration = if (options.key) 2 else turns;
549
+ }
550
+
551
+ pub fn observe(
552
+ self: *Self,
553
+ comptime field: Action.Field,
554
+ player: Player,
555
+ obs: Optional(Confusion),
556
+ ) void {
557
+ if (!enabled) return;
558
+
559
+ var a = self.actions.get(player);
560
+ const val = @field(a, @tagName(field));
561
+ switch (obs) {
562
+ .overwritten => {
563
+ assert(field == .confusion);
564
+ assert(val == .started or val == .continuing);
565
+ assert(a.duration > 0);
566
+ a.confusion = obs;
567
+ self.durations.get(player).confusion = 1;
568
+ },
569
+ .None => {
570
+ @field(a, @tagName(field)) = .None;
571
+ if (field == .sleep) {
572
+ var d = self.durations.get(player);
573
+ d.sleeps = Sleeps.set(d.sleeps, 0, 0);
574
+ } else {
575
+ @field(self.durations.get(player), @tagName(field)) = 0;
576
+ }
577
+ },
578
+ .started => {
579
+ assert(val == .None or val == .ended);
580
+ assert(switch (field) {
581
+ .confusion => a.duration > 0 or self.actions.get(player.foe()).duration > 0,
582
+ .sleep, .disable => self.actions.get(player.foe()).duration > 0,
583
+ else => a.duration > 0,
584
+ });
585
+ @field(a, @tagName(field)) = .started;
586
+ if (field == .sleep) {
587
+ var d = self.durations.get(player);
588
+ d.sleeps = Sleeps.set(d.sleeps, 0, 1);
589
+ } else {
590
+ @field(self.durations.get(player), @tagName(field)) = 1;
591
+ }
592
+ },
593
+ else => unreachable,
594
+ }
595
+ }
596
+
597
+ pub fn sleep(self: *Self, player: Player, obs: Optional(Observation)) Error!void {
598
+ if (!enabled) return;
599
+
600
+ var a = self.actions.get(player);
601
+ assert(a.sleep == .None or a.sleep == .started);
602
+ a.sleep = obs;
603
+
604
+ var d = self.durations.get(player);
605
+ const n = Sleeps.get(d.sleeps, 0);
606
+
607
+ if (obs == .None) {
608
+ d.sleeps = Sleeps.set(d.sleeps, 0, 0);
609
+ } else if (obs == .ended) {
610
+ assert(n >= 1 and n <= 7);
611
+ if (n != 7) try self.probability.update(1, 8 - @as(u4, n));
612
+ d.sleeps = Sleeps.set(d.sleeps, 0, 0);
613
+ } else {
614
+ assert(obs == .continuing);
615
+ assert(n >= 1 and n < 7);
616
+ try self.probability.update(8 - @as(u4, n) - 1, 8 - @as(u4, n));
617
+ d.sleeps = Sleeps.set(d.sleeps, 0, n + 1);
618
+ }
619
+ }
620
+
621
+ pub fn confusion(self: *Self, player: Player, obs: Optional(Confusion)) Error!void {
622
+ if (!enabled) return;
623
+
624
+ var a = self.actions.get(player);
625
+ assert(a.confusion == .None or a.confusion == .started);
626
+ a.confusion = obs;
627
+
628
+ var d = self.durations.get(player);
629
+ const n = d.confusion;
630
+
631
+ if (obs == .ended) {
632
+ assert(n >= 2 and n <= 5);
633
+ if (n != 5) try self.probability.update(1, 6 - @as(u4, n));
634
+ d.confusion = 0;
635
+ } else {
636
+ assert(obs == .continuing);
637
+ assert(n >= 1 and n < 5);
638
+ if (n > 1) try self.probability.update(6 - @as(u4, n) - 1, 6 - @as(u4, n));
639
+ d.confusion += 1;
640
+ }
641
+ }
642
+
643
+ pub fn disable(self: *Self, player: Player, obs: Optional(Observation)) Error!void {
644
+ if (!enabled) return;
645
+
646
+ var a = self.actions.get(player);
647
+ assert(a.disable == .None or a.disable == .started);
648
+ a.disable = obs;
649
+
650
+ var d = self.durations.get(player);
651
+ const n = d.disable;
652
+
653
+ if (obs == .ended) {
654
+ assert(n >= 1 and n <= 8);
655
+ if (n != 8) try self.probability.update(1, 9 - @as(u4, n));
656
+ d.disable = 0;
657
+ } else {
658
+ assert(obs == .continuing);
659
+ assert(n >= 1 and n < 8);
660
+ try self.probability.update(9 - @as(u4, n) - 1, 9 - @as(u4, n));
661
+ d.disable += 1;
662
+ }
663
+ }
664
+
665
+ pub fn attacking(self: *Self, player: Player, obs: Optional(Observation)) Error!void {
666
+ if (!enabled) return;
667
+
668
+ var a = self.actions.get(player);
669
+ assert(a.attacking == .None or a.attacking == .started);
670
+ a.attacking = obs;
671
+
672
+ var d = self.durations.get(player);
673
+ const n = d.attacking;
674
+ if (obs == .ended) {
675
+ // "3-4 turns" includes the turn for the initial attack *plus* the forced attacks
676
+ assert(n >= 2 and n <= 3);
677
+ if (n != 3) try self.probability.update(1, 4 - @as(u4, n));
678
+ d.attacking = 0;
679
+ } else {
680
+ assert(obs == .continuing);
681
+ assert(n >= 1 and n < 3);
682
+ if (n > 1) try self.probability.update(4 - @as(u4, n) - 1, 4 - @as(u4, n));
683
+ d.attacking += 1;
684
+ }
685
+ }
686
+
687
+ pub fn binding(self: *Self, player: Player, obs: Optional(Observation)) Error!void {
688
+ if (!enabled) return;
689
+
690
+ var a = self.actions.get(player);
691
+ assert(a.binding == .None or a.binding == .started);
692
+ a.binding = obs;
693
+
694
+ var d = self.durations.get(player);
695
+ const n = d.binding;
696
+
697
+ assert(n > 0);
698
+ const p: u4 = if (n < 3) 3 else 1;
699
+ const q: u4 = if (n < 3) 8 - ((n - 1) * p) else 2;
700
+
701
+ if (obs == .ended) {
702
+ // "2-5 turns" includes the turn for the initial attack *plus* the forced attacks
703
+ assert(n >= 1 and n <= 4);
704
+ if (n != 4) try self.probability.update(p, q);
705
+ d.binding = 0;
706
+ } else {
707
+ assert(obs == .continuing);
708
+ assert(n >= 1 and n < 4);
709
+ try self.probability.update(q - p, q);
710
+ d.binding += 1;
711
+ }
712
+ }
713
+
714
+ pub fn psywave(self: *Self, player: Player, power: u8, max: u8) Error!void {
715
+ if (!enabled) return;
716
+
717
+ try self.probability.update(1, max);
718
+ self.actions.get(player).psywave = power + 1;
719
+ }
720
+
721
+ pub fn metronome(self: *Self, player: Player, move: Move) Error!void {
722
+ if (!enabled) return;
723
+
724
+ try self.probability.update(1, Move.METRONOME.len);
725
+ self.actions.get(player).metronome = move;
726
+ }
727
+
728
+ pub fn pp(self: *Self, player: Player, n: u4) bool {
729
+ if (!enabled) return true;
730
+
731
+ var a = self.actions.get(player);
732
+ if (n > 1 and a.pp == n) return false;
733
+ if (a.metronome != .None) a.pp += n;
734
+ return true;
735
+ }
736
+ };
737
+ }
738
+
739
+ test "Chance.speedTie" {
740
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
741
+
742
+ try chance.speedTie(true);
743
+ try expectProbability(&chance.probability, 1, 2);
744
+ try expectValue(Optional(Player).P1, chance.actions.p1.speed_tie);
745
+ try expectValue(chance.actions.p1.speed_tie, chance.actions.p2.speed_tie);
746
+
747
+ chance.reset();
748
+
749
+ try chance.speedTie(false);
750
+ try expectProbability(&chance.probability, 1, 2);
751
+ try expectValue(Optional(Player).P2, chance.actions.p1.speed_tie);
752
+ try expectValue(chance.actions.p1.speed_tie, chance.actions.p2.speed_tie);
753
+ }
754
+
755
+ test "Chance.hit" {
756
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
757
+
758
+ try chance.hit(.P1, true, 229);
759
+ if (!showdown) {
760
+ try expectProbability(&chance.probability, 1, 1);
761
+ try expectValue(Optional(bool).None, chance.actions.p1.hit);
762
+
763
+ try chance.commit(.P1, .hit);
764
+ }
765
+ try expectValue(Optional(bool).true, chance.actions.p1.hit);
766
+ try expectProbability(&chance.probability, 229, 256);
767
+
768
+ chance.reset();
769
+
770
+ try chance.hit(.P2, false, 255);
771
+ if (!showdown) {
772
+ try expectProbability(&chance.probability, 1, 1);
773
+ try expectValue(Optional(bool).None, chance.actions.p2.hit);
774
+
775
+ try chance.commit(.P2, .miss);
776
+ }
777
+ try expectValue(Optional(bool).false, chance.actions.p2.hit);
778
+ try expectProbability(&chance.probability, 1, 256);
779
+ }
780
+
781
+ test "Chance.criticalHit" {
782
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
783
+
784
+ try chance.criticalHit(.P1, true, 17);
785
+ if (showdown) {
786
+ try expectValue(Optional(bool).true, chance.actions.p1.critical_hit);
787
+ try expectProbability(&chance.probability, 17, 256);
788
+ } else {
789
+ try expectProbability(&chance.probability, 1, 1);
790
+ try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
791
+
792
+ try chance.hit(.P1, true, 128);
793
+
794
+ try expectProbability(&chance.probability, 1, 1);
795
+ try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
796
+
797
+ try chance.damage(.P1, 217);
798
+
799
+ try expectProbability(&chance.probability, 1, 1);
800
+ try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
801
+
802
+ try chance.commit(.P1, .hit);
803
+
804
+ try expectProbability(&chance.probability, 17, 19968); // (1/2) * (17/256) * (1/39)
805
+ try expectValue(Optional(bool).true, chance.actions.p1.critical_hit);
806
+ }
807
+
808
+ chance.reset();
809
+
810
+ try chance.criticalHit(.P2, false, 5);
811
+ if (showdown) {
812
+ try expectProbability(&chance.probability, 251, 256);
813
+ try expectValue(Optional(bool).false, chance.actions.p2.critical_hit);
814
+ } else {
815
+ try expectProbability(&chance.probability, 1, 1);
816
+ try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
817
+
818
+ try chance.hit(.P1, true, 128);
819
+
820
+ try expectProbability(&chance.probability, 1, 1);
821
+ try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
822
+
823
+ try chance.damage(.P1, 217);
824
+ try chance.commit(.P1, .hit);
825
+
826
+ try expectProbability(&chance.probability, 251, 19968); // (1/2) * (251/256) * (1/39)
827
+ try expectValue(Optional(bool).false, chance.actions.p1.critical_hit);
828
+ }
829
+ }
830
+
831
+ test "Chance.damage" {
832
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
833
+
834
+ try chance.damage(.P1, 217);
835
+ if (showdown) {
836
+ try expectValue(@as(u8, 217), chance.actions.p1.damage);
837
+ try expectProbability(&chance.probability, 1, 39);
838
+ } else {
839
+ try expectProbability(&chance.probability, 1, 1);
840
+ try expectValue(@as(u8, 0), chance.actions.p1.damage);
841
+
842
+ try chance.commit(.P1, .hit);
843
+
844
+ try expectProbability(&chance.probability, 1, 39);
845
+ try expectValue(@as(u8, 217), chance.actions.p1.damage);
846
+ }
847
+ }
848
+
849
+ test "Chance.secondaryChance" {
850
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
851
+
852
+ try chance.secondaryChance(.P1, true, 25);
853
+ try expectProbability(&chance.probability, 25, 256);
854
+ try expectValue(Optional(bool).true, chance.actions.p1.secondary_chance);
855
+
856
+ chance.reset();
857
+
858
+ try chance.secondaryChance(.P2, false, 77);
859
+ try expectProbability(&chance.probability, 179, 256);
860
+ try expectValue(Optional(bool).false, chance.actions.p2.secondary_chance);
861
+ }
862
+
863
+ test "Chance.confused" {
864
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
865
+
866
+ try chance.confused(.P1, false);
867
+ try expectProbability(&chance.probability, 1, 2);
868
+ try expectValue(Optional(bool).false, chance.actions.p1.confused);
869
+
870
+ chance.reset();
871
+
872
+ try chance.confused(.P2, true);
873
+ try expectProbability(&chance.probability, 1, 2);
874
+ try expectValue(Optional(bool).true, chance.actions.p2.confused);
875
+ }
876
+
877
+ test "Chance.paralyzed" {
878
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
879
+
880
+ try chance.paralyzed(.P1, false);
881
+ try expectProbability(&chance.probability, 3, 4);
882
+ try expectValue(Optional(bool).false, chance.actions.p1.paralyzed);
883
+
884
+ chance.reset();
885
+
886
+ try chance.paralyzed(.P2, true);
887
+ try expectProbability(&chance.probability, 1, 4);
888
+ try expectValue(Optional(bool).true, chance.actions.p2.paralyzed);
889
+ }
890
+
891
+ test "Chance.moveSlot" {
892
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
893
+ var ms = [_]MoveSlot{ .{ .id = Move.Surf }, .{ .id = Move.Psychic }, .{ .id = Move.Recover } };
894
+
895
+ try chance.moveSlot(.P2, 2, &ms, 2);
896
+ try expectProbability(&chance.probability, 1, 2);
897
+ try expectValue(@as(u4, 2), chance.actions.p2.move_slot);
898
+
899
+ chance.reset();
900
+
901
+ try chance.moveSlot(.P1, 1, &ms, 0);
902
+ try expectProbability(&chance.probability, 1, 3);
903
+ try expectValue(@as(u4, 1), chance.actions.p1.move_slot);
904
+ }
905
+
906
+ test "Chance.multiHit" {
907
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
908
+
909
+ try chance.multiHit(.P1, 3);
910
+ try expectProbability(&chance.probability, 3, 8);
911
+ try expectValue(@as(u4, 3), chance.actions.p1.multi_hit);
912
+
913
+ chance.reset();
914
+
915
+ try chance.multiHit(.P2, 5);
916
+ try expectProbability(&chance.probability, 1, 8);
917
+ try expectValue(@as(u4, 5), chance.actions.p2.multi_hit);
918
+ }
919
+
920
+ test "Chance.duration" {
921
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
922
+
923
+ chance.duration(.P1, 2);
924
+ try expectValue(@as(u4, 2), chance.actions.p1.duration);
925
+
926
+ chance.reset();
927
+
928
+ chance.duration(.P2, 4);
929
+ try expectValue(@as(u4, 4), chance.actions.p2.duration);
930
+ }
931
+
932
+ test "Chance.sleep" {
933
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
934
+
935
+ for ([_]u8{ 7, 6, 5, 4, 3, 2, 1 }, 1..8) |d, i| {
936
+ chance.durations.p1.sleeps = Sleeps.set(chance.durations.p1.sleeps, 0, @intCast(i));
937
+ try chance.sleep(.P1, .ended);
938
+ try expectProbability(&chance.probability, 1, d);
939
+ try expectValue(@as(u3, 0), Sleeps.get(chance.durations.p1.sleeps, 0));
940
+
941
+ chance.reset();
942
+
943
+ if (i < 7) {
944
+ chance.durations.p1.sleeps = Sleeps.set(chance.durations.p1.sleeps, 0, @intCast(i));
945
+ try chance.sleep(.P1, .continuing);
946
+ try expectProbability(&chance.probability, d - 1, d);
947
+ try expectValue(@as(u3, @intCast(i)) + 1, Sleeps.get(chance.durations.p1.sleeps, 0));
948
+
949
+ chance.reset();
950
+ }
951
+ }
952
+ }
953
+
954
+ test "Chance.confusion" {
955
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
956
+
957
+ for ([_]u8{ 1, 4, 3, 2, 1 }, 1..6) |d, i| {
958
+ if (i > 1) {
959
+ chance.durations.p2.confusion = @intCast(i);
960
+ try chance.confusion(.P2, .ended);
961
+ try expectProbability(&chance.probability, 1, d);
962
+ try expectValue(@as(u3, 0), chance.durations.p2.confusion);
963
+
964
+ chance.reset();
965
+ }
966
+
967
+ if (i < 5) {
968
+ chance.durations.p2.confusion = @intCast(i);
969
+ try chance.confusion(.P2, .continuing);
970
+ try expectProbability(&chance.probability, if (d > 1) d - 1 else d, d);
971
+ try expectValue(@as(u3, @intCast(i)) + 1, chance.durations.p2.confusion);
972
+
973
+ chance.reset();
974
+ }
975
+ }
976
+ }
977
+
978
+ test "Chance.disable" {
979
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
980
+
981
+ for ([_]u8{ 8, 7, 6, 5, 4, 3, 2, 1 }, 1..9) |d, i| {
982
+ chance.durations.p1.disable = @intCast(i);
983
+ try chance.disable(.P1, .ended);
984
+ try expectProbability(&chance.probability, 1, d);
985
+ try expectValue(@as(u4, 0), chance.durations.p1.disable);
986
+
987
+ chance.reset();
988
+
989
+ if (i < 8) {
990
+ chance.durations.p1.disable = @intCast(i);
991
+ try chance.disable(.P1, .continuing);
992
+ try expectProbability(&chance.probability, d - 1, d);
993
+ try expectValue(@as(u4, @intCast(i)) + 1, chance.durations.p1.disable);
994
+
995
+ chance.reset();
996
+ }
997
+ }
998
+ }
999
+
1000
+ test "Chance.attacking" {
1001
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
1002
+
1003
+ for ([_]u8{ 1, 2, 1 }, 1..4) |d, i| {
1004
+ if (i > 1) {
1005
+ chance.durations.p2.attacking = @intCast(i);
1006
+ try chance.attacking(.P2, .ended);
1007
+ try expectProbability(&chance.probability, 1, d);
1008
+ try expectValue(@as(u3, 0), chance.durations.p2.attacking);
1009
+
1010
+ chance.reset();
1011
+ }
1012
+
1013
+ if (i < 3) {
1014
+ chance.durations.p2.attacking = @intCast(i);
1015
+ try chance.attacking(.P2, .continuing);
1016
+ try expectProbability(&chance.probability, if (d > 1) d - 1 else d, d);
1017
+ try expectValue(@as(u3, @intCast(i)) + 1, chance.durations.p2.attacking);
1018
+
1019
+ chance.reset();
1020
+ }
1021
+ }
1022
+ }
1023
+
1024
+ test "Chance.binding" {
1025
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
1026
+
1027
+ const ps = [_]u8{ 3, 3, 1, 1 };
1028
+ const qs = [_]u8{ 8, 5, 2, 1 };
1029
+
1030
+ for (ps, qs, 1..5) |p, q, i| {
1031
+ chance.durations.p1.binding = @intCast(i);
1032
+ try chance.binding(.P1, .ended);
1033
+ try expectProbability(&chance.probability, p, q);
1034
+ try expectValue(@as(u3, 0), chance.durations.p1.binding);
1035
+
1036
+ chance.reset();
1037
+
1038
+ if (i < 4) {
1039
+ chance.durations.p1.binding = @intCast(i);
1040
+ try chance.binding(.P1, .continuing);
1041
+ try expectProbability(&chance.probability, q - p, q);
1042
+ try expectValue(@as(u3, @intCast(i)) + 1, chance.durations.p1.binding);
1043
+
1044
+ chance.reset();
1045
+ }
1046
+ }
1047
+ }
1048
+
1049
+ test "Chance.psywave" {
1050
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
1051
+
1052
+ try chance.psywave(.P2, 100, 150);
1053
+ try expectProbability(&chance.probability, 1, 150);
1054
+ try expectValue(@as(u8, 101), chance.actions.p2.psywave);
1055
+ }
1056
+
1057
+ test "Chance.metronome" {
1058
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
1059
+
1060
+ try chance.metronome(.P1, Move.HornAttack);
1061
+ try expectProbability(&chance.probability, 1, 163);
1062
+ try expectValue(Move.HornAttack, chance.actions.p1.metronome);
1063
+ }
1064
+
1065
+ pub fn expectProbability(r: anytype, p: u64, q: u64) !void {
1066
+ if (!enabled) return;
1067
+
1068
+ r.reduce();
1069
+ if (r.p != p or r.q != q) {
1070
+ print("expected {d}/{d}, found {}\n", .{ p, q, r });
1071
+ return error.TestExpectedEqual;
1072
+ }
1073
+ }
1074
+
1075
+ pub fn expectValue(a: anytype, b: anytype) !void {
1076
+ if (!enabled) return;
1077
+
1078
+ try expectEqual(a, b);
1079
+ }
1080
+
1081
+ /// Null object pattern implementation of Generation I `Chance` which does nothing, though chance
1082
+ /// tracking should additionally be turned off entirely via `options.chance`.
1083
+ pub const NULL: Null = .{};
1084
+
1085
+ const Null = struct {
1086
+ pub const Error = error{};
1087
+
1088
+ pub fn commit(self: Null, player: Player, kind: Commit) Error!void {
1089
+ _ = .{ self, player, kind };
1090
+ }
1091
+
1092
+ pub fn glitch(self: Null) void {
1093
+ _ = .{self};
1094
+ }
1095
+
1096
+ pub fn clearPending(self: Null) void {
1097
+ _ = .{self};
1098
+ }
1099
+
1100
+ pub fn switched(self: Null, player: Player, slot: u8) void {
1101
+ _ = .{ self, player, slot };
1102
+ }
1103
+
1104
+ pub fn speedTie(self: Null, p1: bool) Error!void {
1105
+ _ = .{ self, p1 };
1106
+ }
1107
+
1108
+ pub fn hit(self: Null, player: Player, ok: bool, accuracy: uN) Error!void {
1109
+ _ = .{ self, player, ok, accuracy };
1110
+ }
1111
+
1112
+ pub fn criticalHit(self: Null, player: Player, crit: bool, rate: u8) Error!void {
1113
+ _ = .{ self, player, crit, rate };
1114
+ }
1115
+
1116
+ pub fn damage(self: Null, player: Player, roll: u8) Error!void {
1117
+ _ = .{ self, player, roll };
1118
+ }
1119
+
1120
+ pub fn confused(self: Null, player: Player, ok: bool) Error!void {
1121
+ _ = .{ self, player, ok };
1122
+ }
1123
+
1124
+ pub fn paralyzed(self: Null, player: Player, ok: bool) Error!void {
1125
+ _ = .{ self, player, ok };
1126
+ }
1127
+
1128
+ pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
1129
+ _ = .{ self, player, proc, rate };
1130
+ }
1131
+
1132
+ pub fn moveSlot(self: Null, player: Player, slot: u4, ms: []const MoveSlot, n: u4) Error!void {
1133
+ _ = .{ self, player, slot, ms, n };
1134
+ }
1135
+
1136
+ pub fn multiHit(self: Null, player: Player, n: u3) Error!void {
1137
+ _ = .{ self, player, n };
1138
+ }
1139
+
1140
+ pub fn duration(self: Null, player: Player, turns: u4) void {
1141
+ _ = .{ self, player, turns };
1142
+ }
1143
+
1144
+ pub fn observe(
1145
+ self: Null,
1146
+ comptime field: Action.Field,
1147
+ player: Player,
1148
+ obs: Optional(Confusion),
1149
+ ) void {
1150
+ _ = .{ self, field, player, obs };
1151
+ }
1152
+
1153
+ pub fn sleep(self: Null, player: Player, obs: Optional(Observation)) Error!void {
1154
+ _ = .{ self, player, obs };
1155
+ }
1156
+
1157
+ pub fn confusion(self: Null, player: Player, obs: Optional(Confusion)) Error!void {
1158
+ _ = .{ self, player, obs };
1159
+ }
1160
+
1161
+ pub fn disable(self: Null, player: Player, obs: Optional(Observation)) Error!void {
1162
+ _ = .{ self, player, obs };
1163
+ }
1164
+
1165
+ pub fn attacking(self: Null, player: Player, obs: Optional(Observation)) Error!void {
1166
+ _ = .{ self, player, obs };
1167
+ }
1168
+
1169
+ pub fn binding(self: Null, player: Player, obs: Optional(Observation)) Error!void {
1170
+ _ = .{ self, player, obs };
1171
+ }
1172
+
1173
+ pub fn psywave(self: Null, player: Player, power: u8, max: u8) Error!void {
1174
+ _ = .{ self, player, power, max };
1175
+ }
1176
+
1177
+ pub fn metronome(self: Null, player: Player, move: Move) Error!void {
1178
+ _ = .{ self, player, move };
1179
+ }
1180
+
1181
+ pub fn pp(self: Null, player: Player, n: u4) bool {
1182
+ _ = .{ self, player, n };
1183
+ return true;
1184
+ }
1185
+ };