@pkmn/engine 0.1.0-dev.21a51fe4 → 0.1.0-dev.21e728d9

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 (40) hide show
  1. package/README.md +11 -3
  2. package/build/pkg/data/ids.json +12 -12
  3. package/build/pkg/data/layout.json +2 -2
  4. package/build/pkg/data/protocol.json +69 -12
  5. package/build/pkg/data.d.ts +2 -2
  6. package/build/pkg/gen1.js +2 -2
  7. package/build/pkg/gen1.js.map +1 -1
  8. package/build/pkg/protocol.js +56 -37
  9. package/build/pkg/protocol.js.map +1 -1
  10. package/build/tools/debug.js +3 -1
  11. package/build/tools/debug.js.map +1 -1
  12. package/build.zig +72 -57
  13. package/package.json +27 -15
  14. package/src/bin/install-pkmn-engine +1 -1
  15. package/src/lib/binding/node.zig +1 -1
  16. package/src/lib/common/data.zig +8 -20
  17. package/src/lib/common/optional.zig +8 -1
  18. package/src/lib/common/protocol.zig +578 -80
  19. package/src/lib/common/rational.zig +5 -5
  20. package/src/lib/common/rng.zig +15 -6
  21. package/src/lib/gen1/README.md +114 -32
  22. package/src/lib/gen1/calc.zig +4 -5
  23. package/src/lib/gen1/chance.zig +58 -42
  24. package/src/lib/gen1/data/moves.zig +192 -180
  25. package/src/lib/gen1/data/species.zig +2 -2
  26. package/src/lib/gen1/data/types.zig +7 -7
  27. package/src/lib/gen1/data.zig +31 -43
  28. package/src/lib/gen1/helpers.zig +254 -116
  29. package/src/lib/gen1/mechanics.zig +142 -176
  30. package/src/lib/gen2/README.md +93 -72
  31. package/src/lib/gen2/calc.zig +143 -0
  32. package/src/lib/gen2/chance.zig +1314 -0
  33. package/src/lib/gen2/data/items.zig +12 -18
  34. package/src/lib/gen2/data/moves.zig +663 -569
  35. package/src/lib/gen2/data/species.zig +1 -1
  36. package/src/lib/gen2/data/types.zig +134 -31
  37. package/src/lib/gen2/data.zig +100 -73
  38. package/src/lib/gen2/generated.zig +808 -0
  39. package/src/lib/gen2/helpers.zig +8 -12
  40. package/src/lib/gen2/mechanics.zig +3486 -9
@@ -44,7 +44,7 @@ pub fn Rational(comptime T: type) type {
44
44
  /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
45
45
  pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
46
46
  // FIXME
47
- // std.debug.print("({d}/{d}) * ", .{p, q});
47
+ // std.debug.print("({d}/{d}) * ", .{ p, q });
48
48
  assert(p >= 1);
49
49
  assert(q >= 1);
50
50
 
@@ -185,12 +185,12 @@ pub fn Rational(comptime T: type) type {
185
185
  try writer.print("{d}/{d}", .{ self.p, self.q });
186
186
  }
187
187
 
188
- inline fn multiplication(r: *Self, p: anytype, q: anytype) !void {
188
+ fn multiplication(r: *Self, p: anytype, q: anytype) !void {
189
189
  r.p = try std.math.mul(T, r.p, p);
190
190
  r.q = try std.math.mul(T, r.q, q);
191
191
  }
192
192
 
193
- inline fn addition(r: *Self, p: anytype, q: anytype) !void {
193
+ fn addition(r: *Self, p: anytype, q: anytype) !void {
194
194
  // (a/b) + (c/d) = (ad+bc)/(bd)
195
195
  var d = try std.math.mul(T, r.q, q);
196
196
  var n1 = try std.math.mul(T, r.p, q);
@@ -203,7 +203,7 @@ pub fn Rational(comptime T: type) type {
203
203
  }
204
204
 
205
205
  // https://en.wikipedia.org/wiki/Euclidean_algorithm
206
- inline fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
206
+ fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
207
207
  assert(p >= 1);
208
208
  assert(q >= 1);
209
209
 
@@ -275,7 +275,7 @@ pub const BigRational = struct {
275
275
  /// Create a new BigRational wrapper. A small amount of memory will be allocated on
276
276
  /// initialization. Not default initialized to 1 - you must explicitly `reset` first.
277
277
  pub fn init(alloc: std.mem.Allocator) !BigRational {
278
- return BigRational{ .val = try std.math.big.Rational.init(alloc) };
278
+ return .{ .val = try std.math.big.Rational.init(alloc) };
279
279
  }
280
280
 
281
281
  /// Frees all memory associated with a rational.
@@ -25,6 +25,11 @@ pub fn PRNG(comptime gen: comptime_int) type {
25
25
  pub fn next(self: *Self) Output(gen) {
26
26
  return self.src.next();
27
27
  }
28
+
29
+ pub fn advance(self: *Self, n: usize) void {
30
+ if (!options.advance) return;
31
+ for (0..n) |_| self.src.advance();
32
+ }
28
33
  };
29
34
  }
30
35
 
@@ -64,7 +69,7 @@ pub const PSRNG = extern struct {
64
69
  };
65
70
 
66
71
  test PSRNG {
67
- var psrng = PSRNG{ .src = .{ .seed = 0x0001000200030004 } };
72
+ var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
68
73
  try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
69
74
  try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
70
75
  }
@@ -79,7 +84,7 @@ pub const Gen12 = extern struct {
79
84
  assert(@sizeOf(Gen12) == 10);
80
85
  }
81
86
 
82
- pub fn percent(p: u8) u8 {
87
+ pub fn percent(comptime p: comptime_int) u8 {
83
88
  return @intCast((@as(u16, p) * 0xFF) / 100);
84
89
  }
85
90
 
@@ -89,11 +94,15 @@ pub const Gen12 = extern struct {
89
94
  self.index = (self.index + 1) % 9;
90
95
  return val;
91
96
  }
97
+
98
+ pub fn advance(self: *Gen12) void {
99
+ _ = self.next();
100
+ }
92
101
  };
93
102
 
94
103
  test Gen12 {
95
104
  const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 31, 56, 81 };
96
- var rng = Gen12{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
105
+ var rng: Gen12 = .{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
97
106
  for (expected) |e| {
98
107
  try expectEqual(e, rng.next());
99
108
  }
@@ -129,7 +138,7 @@ test Gen34 {
129
138
  .{ 0x80000000, 5, 0x0E425287 }, .{ 0x80000000, 10, 0x6F2CF4B2 },
130
139
  };
131
140
  for (data) |d| {
132
- var rng = Gen34{ .seed = d[0] };
141
+ var rng: Gen34 = .{ .seed = d[0] };
133
142
  for (0..d[1]) |_| _ = rng.next();
134
143
  try expectEqual(d[2], rng.seed);
135
144
  }
@@ -161,7 +170,7 @@ test Gen56 {
161
170
  .{ 0x8000000000000000, 10, 0xE7795501267F125A },
162
171
  };
163
172
  for (data) |d| {
164
- var rng = Gen56{ .seed = d[0] };
173
+ var rng: Gen56 = .{ .seed = d[0] };
165
174
  for (0..d[1]) |_| _ = rng.next();
166
175
  try expectEqual(d[2], rng.seed);
167
176
  }
@@ -213,7 +222,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
213
222
  test FixedRNG {
214
223
  const Type = if (showdown) u32 else u8;
215
224
  const expected = [_]Type{ 42, 255, 0 };
216
- var rng = FixedRNG(1, expected.len){ .rolls = expected };
225
+ var rng: FixedRNG(1, expected.len) = .{ .rolls = expected };
217
226
  for (expected) |e| {
218
227
  try expectEqual(e, rng.next());
219
228
  }
@@ -6,7 +6,9 @@ document](../../../docs/DESIGN.md). The Generation I engine is implemented and t
6
6
  following files:
7
7
 
8
8
  - [`data.zig`](data.zig) ([`data`](data)): contains definitions of all of the data structures used
9
- by the engine, described in detail [below](#data-structures)
9
+ to implement the core mechanics of the engine, described in detail [below](#data-structures)
10
+ - [`chance.zig`](chance.zig): TODO
11
+ - [`calc.zig`](calc.zig): TODO
10
12
  - [`helpers.zig`](helpers.zig): helpers used to construct complex data types with
11
13
  sensible defaults (internally used by tests and tools)
12
14
  - [`mechanics.zig`](mechanics.zig): code which manipulates the data structures to implement the
@@ -245,9 +247,17 @@ engine maintains a `Type.PRECEDENCE` table with just the match-ups that are rele
245
247
  pruned for efficiency) and only looks up precedence when necessary to minimize expensive searches.
246
248
  Pokémon Showdown doesn't implement type precedence.
247
249
 
250
+ ### `Chance` / `Actions` / `Action` / `Durations` / `Duration`
251
+
252
+ TODO
253
+
254
+ ### `Calc` / `Overrides` / `Summaries` / `Summary`
255
+
256
+ TODO
257
+
248
258
  ## Information
249
259
 
250
- The information of each field (in terms of [bits of
260
+ The information of each field required for mechanics (in terms of [bits of
251
261
  entropy](https://en.wikipedia.org/wiki/Entropy_(information_theory))) is as follows:
252
262
 
253
263
  | Data | Range | Bits | | Data | Range | Bits |
@@ -383,7 +393,7 @@ data in the browser for ease of debugging.
383
393
  - `order` is a 6 byte array where `order[i]` represents the "slot" of `pokemon[i]`, where the slot
384
394
  is usually a unique value from 1 to 6 but can be 0 in situations where a player brings less than six
385
395
  Pokémon to battle (note however that if `order[i]` is 0 than for all j > 0 `order[i+j]` **must**
386
- equal 0).
396
+ equal 0)
387
397
 
388
398
  ### `ActivePokemon`
389
399
 
@@ -417,34 +427,34 @@ data in the browser for ease of debugging.
417
427
 
418
428
  > **NOTE:** The offsets in the following table represent *bits* and **not** bytes.
419
429
 
420
- | Start | End | Data | Description |
421
- | ----- | --- | ------------------- | ----------------------------------------------------------- |
422
- | 0 | 1 | `Bide` | Whether the "Bide" volatile status is present |
423
- | 1 | 2 | `Thrashing` | Whether the "Thrashing" volatile status is present |
424
- | 2 | 3 | `MultiHit` | Whether the "MultiHit" volatile status is present |
425
- | 3 | 4 | `Flinch` | Whether the "Flinch" volatile status is present |
426
- | 4 | 5 | `Charging` | Whether the "Charging" volatile status is present |
427
- | 5 | 6 | `Binding` | Whether the "Binding" volatile status is present |
428
- | 6 | 7 | `Invulnerable` | Whether the "Invulnerable" volatile status is present |
429
- | 7 | 8 | `Confusion` | Whether the "Confusion" volatile status is present |
430
- | 8 | 9 | `Mist` | Whether the "Mist" volatile status is present |
431
- | 9 | 10 | `FocusEnergy` | Whether the "FocusEnergy" volatile status is present |
432
- | 10 | 11 | `Substitute` | Whether the "Substitute" volatile status is present |
433
- | 11 | 12 | `Recharging` | Whether the "Recharging" volatile status is present |
434
- | 12 | 13 | `Rage` | Whether the "Rage" volatile status is present |
435
- | 13 | 14 | `LeechSeed` | Whether the "LeechSeed" volatile status is present |
436
- | 14 | 15 | `Toxic` | Whether the "Toxic" volatile status is present |
437
- | 15 | 16 | `LightScreen` | Whether the "LightScreen" volatile status is present |
438
- | 16 | 17 | `Reflect` | Whether the "Reflect" volatile status is present |
439
- | 17 | 18 | `Transform` | Whether the "Transform" volatile status is present |
440
- | 18 | 21 | `confusion` | The remaining turns of confusion |
441
- | 21 | 24 | `attacks` | The number of attacks remaining |
442
- | 24 | 40 | `state` | A union of either: <ul><li>the total accumulated damage from Bide</li><li>the overwritten accuracy of certain moves</li></ul> |
443
- | 40 | 48 | `substitute` | The remaining HP of the Substitute |
444
- | 48 | 52 | `transform` | The identity of whom the active Pokémon is transformed into |
445
- | 52 | 56 | `disabled_duration` | The remaining turns the move is disabled |
446
- | 56 | 59 | `disabled_move` | The move slot (1-4) that's disabled |
447
- | 59 | 64 | `toxic` | The number of turns toxic damage has been accumulating |
430
+ | Start | End | Data | Description |
431
+ | ----- | --- | -------------------| ----------------------------------------------------------- |
432
+ | 0 | 1 | `Bide` | Whether the "Bide" volatile status is present |
433
+ | 1 | 2 | `Thrashing` | Whether the "Thrashing" volatile status is present |
434
+ | 2 | 3 | `MultiHit` | Whether the "MultiHit" volatile status is present |
435
+ | 3 | 4 | `Flinch` | Whether the "Flinch" volatile status is present |
436
+ | 4 | 5 | `Charging` | Whether the "Charging" volatile status is present |
437
+ | 5 | 6 | `Binding` | Whether the "Binding" volatile status is present |
438
+ | 6 | 7 | `Invulnerable` | Whether the "Invulnerable" volatile status is present |
439
+ | 7 | 8 | `Confusion` | Whether the "Confusion" volatile status is present |
440
+ | 8 | 9 | `Mist` | Whether the "Mist" volatile status is present |
441
+ | 9 | 10 | `FocusEnergy` | Whether the "FocusEnergy" volatile status is present |
442
+ | 10 | 11 | `Substitute` | Whether the "Substitute" volatile status is present |
443
+ | 11 | 12 | `Recharging` | Whether the "Recharging" volatile status is present |
444
+ | 12 | 13 | `Rage` | Whether the "Rage" volatile status is present |
445
+ | 13 | 14 | `LeechSeed` | Whether the "LeechSeed" volatile status is present |
446
+ | 14 | 15 | `Toxic` | Whether the "Toxic" volatile status is present |
447
+ | 15 | 16 | `LightScreen` | Whether the "LightScreen" volatile status is present |
448
+ | 16 | 17 | `Reflect` | Whether the "Reflect" volatile status is present |
449
+ | 17 | 18 | `Transform` | Whether the "Transform" volatile status is present |
450
+ | 18 | 21 | `confusion` | The remaining turns of confusion |
451
+ | 21 | 24 | `attacks` | The number of attacks remaining |
452
+ | 24 | 40 | `state` | A union of either: <ul><li>the total accumulated damage from Bide</li><li>the overwritten accuracy of certain moves</li></ul> |
453
+ | 40 | 48 | `substitute` | The remaining HP of the Substitute |
454
+ | 48 | 52 | `transform` | The identity of whom the active Pokémon is transformed into |
455
+ | 52 | 56 | `disable_duration` | The remaining turns the move is disabled |
456
+ | 56 | 59 | `disable_move` | The move slot (1-4) that's disabled |
457
+ | 59 | 64 | `toxic` | The number of turns toxic damage has been accumulating |
448
458
 
449
459
  ### `Pokemon`
450
460
 
@@ -469,6 +479,76 @@ data in the browser for ease of debugging.
469
479
  | 22 | 23 | `type1`/`type2` | The Pokémon's stored types |
470
480
  | 23 | 24 | `level` | The Pokémon's level |
471
481
 
482
+
483
+ ### `Action`
484
+
485
+ `Actions` consist of an `Action` for Player 1 followed by an `Action` for Player 2.
486
+
487
+ > **NOTE:** The offsets in the following table represent *bits* and **not** bytes.
488
+
489
+ | Start | End | Data | Description |
490
+ | ----- | --- | ------------------------ | ------------------------------------------------------------------------------- |
491
+ | 0 | 8 | `damage` | The roll to be returned damage (217-255). |
492
+ | 8 | 10 | `hit` | The roll to be returned for accuracy (1 for miss, 2 for hit). |
493
+ | 10 | 12 | `critical_hit` | The roll to be returned for critical hits (1 for no, 2 for yes). |
494
+ | 12 | 14 | `secondary_chance` | The roll to be returned for secondary chance rolls (1 for no-proc, 2 for proc). |
495
+ | 14 | 16 | `speed_tie` | The player to return for speed ties (1 for Player 1, 2 for Player 2). |
496
+ | 16 | 18 | `confused` | The roll to be returned for confusion self-hits (1 for no-proc, 2 for proc). |
497
+ | 18 | 20 | `paralyzed` | The roll to be returned for full paralysis (1 for no-proc, 2 for proc). |
498
+ | 20 | 24 | `duration` | The roll to be returned for the duration (including for binding moves). |
499
+ | 24 | 40 | [`durations`](#duration) | Values of various durations. |
500
+ | 40 | 44 | `move_slot` | The roll to be returned for the move slot (1-4, invalid values ignored). |
501
+ | 44 | 48 | `move_slot` | The roll to be returned for a multi-hit move distribution (2-5). |
502
+ | 48 | 56 | `psywave` | One greater than the roll to be returned for Psywave damage. |
503
+ | 56 | 64 | `metronome` | The move to return for Metronome. |
504
+
505
+ - for any field a value of 0 is considered to be unset
506
+ - **both Player 1 and Player 2 must set the same value for `speed_tie`.**
507
+ - `duration` determines the roll for any effect which lasts over multiple turns, whereas `durations` can either be used to
508
+ track how long these effects have lasted (for the purposes of computing probablities with `-Dchance`) or can be used to
509
+ [override](#overrides) (extend or end) the duration of specfic effects with `-Dcalc`.
510
+
511
+ #### `Duration`
512
+
513
+ > **NOTE:** The offsets in the following table represent *bits* and **not** bytes.
514
+
515
+ | Start | End | Data |
516
+ | ----- | --- | ----------- |
517
+ | 0 | 3 | `sleep` |
518
+ | 3 | 6 | `confusion` |
519
+ | 4 | 10 | `disable` |
520
+ | 10 | 13 | `attacking` |
521
+ | 13 | 16 | `binding` |
522
+
523
+ - the meaning of each field depends on the context in which `Duration` appears:
524
+ - within `Chance` `actions`, the durations tracked refer to the number of turns a Pokémon has been
525
+ observed to be under an effect
526
+ - within `Overrides` `actions`, the durations refer to the value the next `Chance` `actions`
527
+ should be made to take on
528
+ - within `Overrides` `durations`, the durations serve as a mask to determine to whether or not the
529
+ durations in `Overrides` `actions` should be considered to be set or not
530
+ ◊
531
+ ### `Overrides`
532
+
533
+ | Start | End | Data | Description |
534
+ | ----- | --- | --------------------------- | ------------------------------------------------------------------------------------- |
535
+ | 0 | 8 | [`action.p1`](#action) | Rolls to force Player 1's RNG to return for specific events. |
536
+ | 8 | 16 | [`action.p2`](#action) | Rolls to force Player 2's RNG to return for specific events. |
537
+ | 16 | 18 | [`durations.p1`](#duration) | Whether or not to modify Player 1's effect durations to match `actions.p1.durations`. |
538
+ | 18 | 20 | [`durations.p2`](#duration) | Whether or not to modify Player 2's effect durations to match `actions.p1.durations`. |
539
+ | 20 | 24 | - | *Zero padding* |
540
+
541
+ ### `Summaries`
542
+
543
+ | Start | End | Data | Description |
544
+ | ----- | --- | ------------------ | ------------------------------------------------------------------------------- |
545
+ | 0 | 2 | `p1.damage.base` | Player 1's base computed damage before the damage roll is applied. |
546
+ | 2 | 4 | `p1.damage.final` | Player 1's final computed damage that gets applied to the Pokémon. |
547
+ | 4 | 6 | `p1.damage.capped` | Whether higher damage from Player 1 will saturate / result in the same outcome. |
548
+ | 6 | 8 | `p2.damage.base` | Player 2's base computed damage before the damage roll is applied. |
549
+ | 8 | 10 | `p2.damage.final` | Player 2's final computed damage that gets applied to the Pokémon. |
550
+ | 10 | 12 | `p2.damage.capped` | Whether higher damage from Player 2 will saturate / result in the same outcome. |
551
+
472
552
  ## Bugs
473
553
 
474
554
  In addition to its alternative [RNG semantics](#rng), Pokémon Showdown's implementation of the first
@@ -532,7 +612,8 @@ engine, but the following moves have their broken behavior preserved in `-Dshowd
532
612
  - **Roar** / **Whirlwind**: these moves can miss on Pokémon Showdown (and advance the RNG when
533
613
  checking) which is incorrect (these moves should always fail, but don't check accuracy or advance
534
614
  the RNG). More importantly, these moves should *not* cause the tracked last battle damage to be
535
- zeroed, but on Pokémon Showdown they do.
615
+ zeroed, but on Pokémon Showdown they do. These should also `|-fail|...|[still]` instead of doing
616
+ nothing.
536
617
  - **Substitute**: in addition to the [Substitute + Confusion
537
618
  glitch](https://pkmn.cc/bulba-glitch-1#Substitute_.2B_Confusion_glitch) not being implemented
538
619
  correctly (covered earlier), the [Substitute 1/4
@@ -554,6 +635,7 @@ only pedantic UI correctness):
554
635
 
555
636
  - **Rage**: Rage should report the `|-boost|` before the Disable `|-miss|`, not after.
556
637
  - **Haze**: Haze clears the status/volatiles in an incorrect order.
638
+ - **Teleport**: Teleport should `|-fail|...|[still]` instead of doing nothing.
557
639
  - **Twineedle**: the `|-hitcount|` ("Hit 2 times") should come before the `|-status|` message, not
558
640
  after.
559
641
  - **Thrash** / **Petal Dance**: confusion should silently get applied before the `|move|` message,
@@ -29,7 +29,6 @@ const Optional = optional.Optional;
29
29
  const Rational = rational.Rational;
30
30
 
31
31
  const PointerType = util.PointerType;
32
- const FieldType = util.FieldType;
33
32
 
34
33
  const Actions = chance.Actions;
35
34
  const Action = chance.Action;
@@ -51,7 +50,7 @@ pub const Summaries = extern struct {
51
50
  }
52
51
 
53
52
  /// Returns the `Summary` for the given `player`.
54
- pub inline fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
53
+ pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
55
54
  assert(@typeInfo(@TypeOf(self)).Pointer.child == Summaries);
56
55
  return if (player == .P1) &self.p1 else &self.p2;
57
56
  }
@@ -158,7 +157,7 @@ pub const Calc = struct {
158
157
 
159
158
  /// Null object pattern implementation of Generation I `Calc` which does nothing, though damage
160
159
  /// calculator support should additionally be turned off entirely via `options.calc`.
161
- pub const NULL = Null{};
160
+ pub const NULL: Null = .{};
162
161
 
163
162
  const Null = struct {
164
163
  pub fn overridden(
@@ -256,7 +255,7 @@ pub fn transitions(
256
255
 
257
256
  try debug(writer, template, true, .{ .color = i, .bold = true, .background = true });
258
257
 
259
- var a = Actions{ .p1 = .{ .metronome = p1_move }, .p2 = .{ .metronome = p2_move } };
258
+ var a: Actions = .{ .p1 = .{ .metronome = p1_move }, .p2 = .{ .metronome = p2_move } };
260
259
 
261
260
  for (Rolls.speedTie(template.p1)) |tie| { a.p1.speed_tie = tie; a.p2.speed_tie = tie;
262
261
  for (Rolls.sleep(template.p1, actions.p1)) |p1_slp| { a.p1.durations.sleep = p1_slp;
@@ -388,7 +387,7 @@ pub fn transitions(
388
387
  return stats;
389
388
  }
390
389
 
391
- inline fn matches(actions: Actions, i: usize, frontier: []Actions) bool {
390
+ fn matches(actions: Actions, i: usize, frontier: []Actions) bool {
392
391
  for (frontier, 0..) |f, j| {
393
392
  // TODO: is skipping this redundant check worth it?
394
393
  if (i == j) continue;
@@ -14,12 +14,14 @@ const util = @import("../common/util.zig");
14
14
  const Player = @import("../common/data.zig").Player;
15
15
  const Optional = @import("../common/optional.zig").Optional;
16
16
 
17
- const Move = @import("data.zig").Move;
18
- const MoveSlot = @import("data.zig").MoveSlot;
17
+ const data = @import("./data.zig");
19
18
 
20
19
  const enabled = options.chance;
21
20
  const showdown = options.showdown;
22
21
 
22
+ const Move = data.Move;
23
+ const MoveSlot = data.MoveSlot;
24
+
23
25
  /// Actions taken by a hypothetical "chance player" that convey information about which RNG events
24
26
  /// were observed during a Generation I battle `update`. This can additionally be provided as input
25
27
  /// to the `update` call to override the normal behavior of the RNG in order to force specific
@@ -35,19 +37,19 @@ pub const Actions = extern struct {
35
37
  }
36
38
 
37
39
  /// Returns the `Action` for the given `player`.
38
- pub inline fn get(self: anytype, player: Player) util.PointerType(@TypeOf(self), Action) {
40
+ pub fn get(self: anytype, player: Player) util.PointerType(@TypeOf(self), Action) {
39
41
  assert(@typeInfo(@TypeOf(self)).Pointer.child == Actions);
40
42
  return if (player == .P1) &self.p1 else &self.p2;
41
43
  }
42
44
 
43
45
  /// Perform a reset by clearing fields which should not persist across updates.
44
- pub inline fn reset(self: *Actions) void {
46
+ pub fn reset(self: *Actions) void {
45
47
  self.p1.reset();
46
48
  self.p2.reset();
47
49
  }
48
50
 
49
51
  /// Returns true if `a` is equal to `b`.
50
- pub inline fn eql(a: Actions, b: Actions) bool {
52
+ pub fn eql(a: Actions, b: Actions) bool {
51
53
  return @as(u128, @bitCast(a)) == @as(u128, @bitCast(b));
52
54
  }
53
55
 
@@ -115,7 +117,7 @@ test Actions {
115
117
 
116
118
  /// Information about the RNG that was observed during a Generation I battle `update` for a
117
119
  /// single player.
118
- pub const Action = packed struct {
120
+ pub const Action = packed struct(u64) {
119
121
  /// If not 0, the roll to be returned Rolls.damage.
120
122
  damage: u8 = 0,
121
123
 
@@ -126,7 +128,7 @@ pub const Action = packed struct {
126
128
  /// If not None, the value to be returned for
127
129
  /// Rolls.{confusionChance,secondaryChance,poisonChance}.
128
130
  secondary_chance: Optional(bool) = .None,
129
- /// If not None, the Playr to be returned by Rolls.speedTie.
131
+ /// If not None, the Player to be returned by Rolls.speedTie.
130
132
  speed_tie: Optional(Player) = .None,
131
133
 
132
134
  /// If not None, the value to return for Rolls.confused.
@@ -156,12 +158,8 @@ pub const Action = packed struct {
156
158
 
157
159
  pub const Field = std.meta.FieldEnum(Action);
158
160
 
159
- comptime {
160
- assert(@sizeOf(Action) == 8);
161
- }
162
-
163
161
  /// Perform a reset by clearing fields which should not persist across updates.
164
- pub inline fn reset(self: *Action) void {
162
+ pub fn reset(self: *Action) void {
165
163
  self.* = @bitCast(@as(u64, @bitCast(self.*)) & DURATIONS);
166
164
  }
167
165
 
@@ -181,9 +179,9 @@ pub const Action = packed struct {
181
179
  if (v != 0) {
182
180
  if (printed) try writer.writeAll(", ");
183
181
  if (shape) {
184
- try writer.print("{s}:?", .{field.name});
182
+ try writer.print("{s}:?", .{f.name});
185
183
  } else {
186
- try writer.print("{s}:{d}", .{ field.name, v });
184
+ try writer.print("{s}:{d}", .{ f.name, v });
187
185
  }
188
186
  printed = true;
189
187
  }
@@ -219,7 +217,7 @@ pub const Action = packed struct {
219
217
  };
220
218
 
221
219
  test Action {
222
- var a = Action{ .hit = .true, .durations = .{ .sleep = 3 }, .damage = 225 };
220
+ var a: Action = .{ .hit = .true, .durations = .{ .sleep = 3 }, .damage = 225 };
223
221
  a.reset();
224
222
 
225
223
  try expectEqual(Action{ .hit = .None, .durations = .{ .sleep = 3 }, .damage = 0 }, a);
@@ -239,7 +237,7 @@ pub const Durations = extern struct {
239
237
 
240
238
  /// Observed values for various durations that need to be tracked in order to properly
241
239
  /// deduplicate transitions with a primary key. TODO
242
- pub const Duration = packed struct {
240
+ pub const Duration = packed struct(u16) {
243
241
  /// The number of turns a Pokémon has been observed to be sleeping.
244
242
  sleep: u3 = 0,
245
243
  /// The number of turns a Pokémon has been observed to be confused.
@@ -252,10 +250,6 @@ pub const Duration = packed struct {
252
250
  binding: u3 = 0,
253
251
 
254
252
  pub const Field = std.meta.FieldEnum(Duration);
255
-
256
- comptime {
257
- assert(@sizeOf(Duration) == 2);
258
- }
259
253
  };
260
254
 
261
255
  pub const Commit = enum { hit, miss, binding };
@@ -522,22 +516,6 @@ pub fn Chance(comptime Rational: type) type {
522
516
  }
523
517
  }
524
518
 
525
- pub fn disable(self: *Self, player: Player, turns: u4) Error!void {
526
- if (!enabled) return;
527
-
528
- var durations = &self.actions.get(player).durations;
529
- const n = durations.disable;
530
- if (turns == 0) {
531
- assert(n >= 1 and n <= 8);
532
- if (n != 8) try self.probability.update(1, 9 - @as(u4, n));
533
- durations.disable = 0;
534
- } else {
535
- assert(n >= 1 and n < 8);
536
- try self.probability.update(9 - @as(u4, n) - 1, 9 - @as(u4, n));
537
- durations.disable += 1;
538
- }
539
- }
540
-
541
519
  pub fn confusion(self: *Self, player: Player, turns: u4) Error!void {
542
520
  if (!enabled) return;
543
521
 
@@ -554,6 +532,22 @@ pub fn Chance(comptime Rational: type) type {
554
532
  }
555
533
  }
556
534
 
535
+ pub fn disable(self: *Self, player: Player, turns: u4) Error!void {
536
+ if (!enabled) return;
537
+
538
+ var durations = &self.actions.get(player).durations;
539
+ const n = durations.disable;
540
+ if (turns == 0) {
541
+ assert(n >= 1 and n <= 8);
542
+ if (n != 8) try self.probability.update(1, 9 - @as(u4, n));
543
+ durations.disable = 0;
544
+ } else {
545
+ assert(n >= 1 and n < 8);
546
+ try self.probability.update(9 - @as(u4, n) - 1, 9 - @as(u4, n));
547
+ durations.disable += 1;
548
+ }
549
+ }
550
+
557
551
  pub fn attacking(self: *Self, player: Player, turns: u4) Error!void {
558
552
  if (!enabled) return;
559
553
 
@@ -601,7 +595,7 @@ pub fn Chance(comptime Rational: type) type {
601
595
  pub fn metronome(self: *Self, player: Player, move: Move) Error!void {
602
596
  if (!enabled) return;
603
597
 
604
- try self.probability.update(1, Move.size - 2);
598
+ try self.probability.update(1, Move.METRONOME.len);
605
599
  self.actions.get(player).metronome = move;
606
600
  }
607
601
  };
@@ -864,6 +858,28 @@ test "Chance.confusion" {
864
858
  }
865
859
  }
866
860
 
861
+ test "Chance.disable" {
862
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
863
+
864
+ for ([_]u8{ 8, 7, 6, 5, 4, 3, 2, 1 }, 1..9) |d, i| {
865
+ chance.actions.p1.durations.disable = @intCast(i);
866
+ try chance.disable(.P1, 0);
867
+ try expectProbability(&chance.probability, 1, d);
868
+ try expectValue(@as(u4, 0), chance.actions.p1.durations.disable);
869
+
870
+ chance.reset();
871
+
872
+ if (i < 8) {
873
+ chance.actions.p1.durations.disable = @intCast(i);
874
+ try chance.disable(.P1, 1);
875
+ try expectProbability(&chance.probability, d - 1, d);
876
+ try expectValue(@as(u4, @intCast(i)) + 1, chance.actions.p1.durations.disable);
877
+
878
+ chance.reset();
879
+ }
880
+ }
881
+ }
882
+
867
883
  test "Chance.attacking" {
868
884
  var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
869
885
 
@@ -947,7 +963,7 @@ pub fn expectValue(a: anytype, b: anytype) !void {
947
963
 
948
964
  /// Null object pattern implementation of Generation I `Chance` which does nothing, though chance
949
965
  /// tracking should additionally be turned off entirely via `options.chance`.
950
- pub const NULL = Null{};
966
+ pub const NULL: Null = .{};
951
967
 
952
968
  const Null = struct {
953
969
  pub const Error = error{};
@@ -984,10 +1000,6 @@ const Null = struct {
984
1000
  _ = .{ self, player, roll };
985
1001
  }
986
1002
 
987
- pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
988
- _ = .{ self, player, proc, rate };
989
- }
990
-
991
1003
  pub fn confused(self: Null, player: Player, ok: bool) Error!void {
992
1004
  _ = .{ self, player, ok };
993
1005
  }
@@ -996,6 +1008,10 @@ const Null = struct {
996
1008
  _ = .{ self, player, ok };
997
1009
  }
998
1010
 
1011
+ pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
1012
+ _ = .{ self, player, proc, rate };
1013
+ }
1014
+
999
1015
  pub fn moveSlot(self: Null, player: Player, slot: u4, ms: []const MoveSlot, n: u4) Error!void {
1000
1016
  _ = .{ self, player, slot, ms, n };
1001
1017
  }