@pkmn/engine 0.1.0-dev.954f650d → 0.1.0-dev.95e81bfe

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 (47) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +37 -14
  3. package/build/pkg/addon.js.map +1 -1
  4. package/build/pkg/data/ids.json +12 -12
  5. package/build/pkg/data/layout.json +2 -2
  6. package/build/pkg/data/protocol.json +69 -12
  7. package/build/pkg/data.d.ts +3 -2
  8. package/build/pkg/data.js.map +1 -1
  9. package/build/pkg/gen1.js +2 -2
  10. package/build/pkg/gen1.js.map +1 -1
  11. package/build/pkg/index.d.ts +2 -1
  12. package/build/pkg/index.js.map +1 -1
  13. package/build/pkg/protocol.js +57 -38
  14. package/build/pkg/protocol.js.map +1 -1
  15. package/build/test/display.js.map +1 -1
  16. package/build/tools/debug.js +3 -1
  17. package/build/tools/debug.js.map +1 -1
  18. package/build.zig +80 -70
  19. package/package.json +43 -23
  20. package/src/bin/install-pkmn-engine +14 -14
  21. package/src/lib/common/data.zig +9 -9
  22. package/src/lib/common/optional.zig +9 -2
  23. package/src/lib/common/protocol.zig +734 -318
  24. package/src/lib/common/rational.zig +12 -8
  25. package/src/lib/common/rng.zig +25 -16
  26. package/src/lib/gen1/README.md +28 -28
  27. package/src/lib/gen1/calc.zig +9 -9
  28. package/src/lib/gen1/chance.zig +72 -48
  29. package/src/lib/gen1/data/moves.zig +24 -10
  30. package/src/lib/gen1/data/species.zig +3 -3
  31. package/src/lib/gen1/data/types.zig +7 -7
  32. package/src/lib/gen1/data.zig +41 -41
  33. package/src/lib/gen1/helpers.zig +33 -48
  34. package/src/lib/gen1/mechanics.zig +340 -355
  35. package/src/lib/gen2/README.md +18 -72
  36. package/src/lib/gen2/calc.zig +143 -0
  37. package/src/lib/gen2/chance.zig +1314 -0
  38. package/src/lib/gen2/data/items.zig +12 -18
  39. package/src/lib/gen2/data/moves.zig +69 -36
  40. package/src/lib/gen2/data/species.zig +1 -1
  41. package/src/lib/gen2/data/types.zig +134 -31
  42. package/src/lib/gen2/data.zig +52 -44
  43. package/src/lib/gen2/generated.zig +806 -0
  44. package/src/lib/gen2/mechanics.zig +2935 -244
  45. package/src/lib/{binding/node.zig → node.zig} +10 -13
  46. package/src/lib/pkmn.zig +26 -3
  47. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
@@ -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,16 +185,16 @@ 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
- var d = try std.math.mul(T, r.q, q);
196
- var n1 = try std.math.mul(T, r.p, q);
197
- var n2 = try std.math.mul(T, r.q, p);
195
+ const d = try std.math.mul(T, r.q, q);
196
+ const n1 = try std.math.mul(T, r.p, q);
197
+ const n2 = try std.math.mul(T, r.q, p);
198
198
 
199
199
  r.p = try std.math.add(T, n1, n2);
200
200
  r.q = d;
@@ -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
 
@@ -234,6 +234,8 @@ test Rational {
234
234
  var r: Rational(t) = .{};
235
235
 
236
236
  var c: t = 128;
237
+ _ = &c;
238
+
237
239
  try r.update(c, 256);
238
240
  try doTurn(&r);
239
241
 
@@ -275,7 +277,7 @@ pub const BigRational = struct {
275
277
  /// Create a new BigRational wrapper. A small amount of memory will be allocated on
276
278
  /// initialization. Not default initialized to 1 - you must explicitly `reset` first.
277
279
  pub fn init(alloc: std.mem.Allocator) !BigRational {
278
- return BigRational{ .val = try std.math.big.Rational.init(alloc) };
280
+ return .{ .val = try std.math.big.Rational.init(alloc) };
279
281
  }
280
282
 
281
283
  /// Frees all memory associated with a rational.
@@ -320,6 +322,8 @@ test BigRational {
320
322
  try r.reset();
321
323
 
322
324
  var c: u8 = 128;
325
+ _ = &c;
326
+
323
327
  try r.update(c, 256);
324
328
  try doTurn(&r);
325
329
 
@@ -1,6 +1,6 @@
1
1
  const std = @import("std");
2
2
 
3
- const options = @import("./options.zig");
3
+ const options = @import("options.zig");
4
4
 
5
5
  const assert = std.debug.assert;
6
6
  const expectEqual = std.testing.expectEqual;
@@ -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
 
@@ -61,11 +66,15 @@ pub const PSRNG = extern struct {
61
66
  (@as(u64, self.range(u16, 0, 0x10000)) << 16) |
62
67
  (@as(u64, self.range(u16, 0, 0x10000)));
63
68
  }
69
+
70
+ pub fn Bound(comptime T: type) type {
71
+ return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
72
+ }
64
73
  };
65
74
 
66
75
  test PSRNG {
67
- var psrng = PSRNG{ .src = .{ .seed = 0x0001000200030004 } };
68
- try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
76
+ var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
77
+ try expectEqual(121, psrng.range(u8, 0, 256));
69
78
  try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
70
79
  }
71
80
 
@@ -89,18 +98,22 @@ pub const Gen12 = extern struct {
89
98
  self.index = (self.index + 1) % 9;
90
99
  return val;
91
100
  }
101
+
102
+ pub fn advance(self: *Gen12) void {
103
+ _ = self.next();
104
+ }
92
105
  };
93
106
 
94
107
  test Gen12 {
95
108
  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 } };
109
+ var rng: Gen12 = .{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
97
110
  for (expected) |e| {
98
111
  try expectEqual(e, rng.next());
99
112
  }
100
113
 
101
- try expectEqual(@as(u8, 16), Gen12.percent(6) + 1);
102
- try expectEqual(@as(u8, 16), Gen12.percent(7) - 1);
103
- try expectEqual(@as(u8, 128), Gen12.percent(50) + 1);
114
+ try expectEqual(16, Gen12.percent(6) + 1);
115
+ try expectEqual(16, Gen12.percent(7) - 1);
116
+ try expectEqual(128, Gen12.percent(50) + 1);
104
117
  }
105
118
 
106
119
  // https://pkmn.cc/pokeemerald/src/random.c
@@ -129,7 +142,7 @@ test Gen34 {
129
142
  .{ 0x80000000, 5, 0x0E425287 }, .{ 0x80000000, 10, 0x6F2CF4B2 },
130
143
  };
131
144
  for (data) |d| {
132
- var rng = Gen34{ .seed = d[0] };
145
+ var rng: Gen34 = .{ .seed = d[0] };
133
146
  for (0..d[1]) |_| _ = rng.next();
134
147
  try expectEqual(d[2], rng.seed);
135
148
  }
@@ -161,7 +174,7 @@ test Gen56 {
161
174
  .{ 0x8000000000000000, 10, 0xE7795501267F125A },
162
175
  };
163
176
  for (data) |d| {
164
- var rng = Gen56{ .seed = d[0] };
177
+ var rng: Gen56 = .{ .seed = d[0] };
165
178
  for (0..d[1]) |_| _ = rng.next();
166
179
  try expectEqual(d[2], rng.seed);
167
180
  }
@@ -188,7 +201,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
188
201
  for (0..n) |_| _ = self.next();
189
202
  }
190
203
 
191
- pub fn range(self: *Self, comptime T: type, from: T, to: Bound(T)) T {
204
+ pub fn range(self: *Self, comptime T: type, from: T, to: PSRNG.Bound(T)) T {
192
205
  assert(showdown);
193
206
  return @intCast(@as(u64, self.next()) * (to - from) / divisor + from);
194
207
  }
@@ -197,7 +210,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
197
210
  self: *Self,
198
211
  comptime T: type,
199
212
  numerator: T,
200
- denominator: Bound(T),
213
+ denominator: PSRNG.Bound(T),
201
214
  ) bool {
202
215
  assert(showdown);
203
216
  assert(denominator > 0);
@@ -213,7 +226,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
213
226
  test FixedRNG {
214
227
  const Type = if (showdown) u32 else u8;
215
228
  const expected = [_]Type{ 42, 255, 0 };
216
- var rng = FixedRNG(1, expected.len){ .rolls = expected };
229
+ var rng: FixedRNG(1, expected.len) = .{ .rolls = expected };
217
230
  for (expected) |e| {
218
231
  try expectEqual(e, rng.next());
219
232
  }
@@ -228,7 +241,3 @@ fn Output(comptime gen: comptime_int) type {
228
241
  else => unreachable,
229
242
  };
230
243
  }
231
-
232
- fn Bound(comptime T: type) type {
233
- return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
234
- }
@@ -427,34 +427,34 @@ data in the browser for ease of debugging.
427
427
 
428
428
  > **NOTE:** The offsets in the following table represent *bits* and **not** bytes.
429
429
 
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 | `disabled_duration` | The remaining turns the move is disabled |
456
- | 56 | 59 | `disabled_move` | The move slot (1-4) that's disabled |
457
- | 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 |
458
458
 
459
459
  ### `Pokemon`
460
460
 
@@ -50,7 +50,7 @@ pub const Summaries = extern struct {
50
50
  }
51
51
 
52
52
  /// Returns the `Summary` for the given `player`.
53
- pub inline fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
53
+ pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
54
54
  assert(@typeInfo(@TypeOf(self)).Pointer.child == Summaries);
55
55
  return if (player == .P1) &self.p1 else &self.p2;
56
56
  }
@@ -157,7 +157,7 @@ pub const Calc = struct {
157
157
 
158
158
  /// Null object pattern implementation of Generation I `Calc` which does nothing, though damage
159
159
  /// calculator support should additionally be turned off entirely via `options.calc`.
160
- pub const NULL = Null{};
160
+ pub const NULL: Null = .{};
161
161
 
162
162
  const Null = struct {
163
163
  pub fn overridden(
@@ -211,8 +211,8 @@ pub fn transitions(
211
211
  ) !?Stats {
212
212
  var stats: Stats = .{};
213
213
 
214
- var actions = options.actions;
215
- var cap = options.cap;
214
+ const actions = options.actions;
215
+ const cap = options.cap;
216
216
 
217
217
  var seen = std.AutoHashMap(Actions, void).init(allocator);
218
218
  defer seen.deinit();
@@ -232,8 +232,8 @@ pub fn transitions(
232
232
  _ = try b.update(c1, c2, &opts);
233
233
  stats.updates += 1;
234
234
 
235
- var p1 = b.side(.P1);
236
- var p2 = b.side(.P2);
235
+ const p1 = b.side(.P1);
236
+ const p2 = b.side(.P2);
237
237
 
238
238
  var p: Rational(u128) = .{ .p = 0, .q = 1 };
239
239
  try frontier.append(opts.chance.actions);
@@ -247,7 +247,7 @@ pub fn transitions(
247
247
  var i: usize = 0;
248
248
  assert(frontier.items.len == 1);
249
249
  while (i < frontier.items.len) : (i += 1) {
250
- var template = frontier.items[i];
250
+ const template = frontier.items[i];
251
251
  opts.calc.overrides.durations = .{
252
252
  .p1 = template.p1.durations,
253
253
  .p2 = template.p2.durations,
@@ -255,7 +255,7 @@ pub fn transitions(
255
255
 
256
256
  try debug(writer, template, true, .{ .color = i, .bold = true, .background = true });
257
257
 
258
- var a = Actions{ .p1 = .{ .metronome = p1_move }, .p2 = .{ .metronome = p2_move } };
258
+ var a: Actions = .{ .p1 = .{ .metronome = p1_move }, .p2 = .{ .metronome = p2_move } };
259
259
 
260
260
  for (Rolls.speedTie(template.p1)) |tie| { a.p1.speed_tie = tie; a.p2.speed_tie = tie;
261
261
  for (Rolls.sleep(template.p1, actions.p1)) |p1_slp| { a.p1.durations.sleep = p1_slp;
@@ -387,7 +387,7 @@ pub fn transitions(
387
387
  return stats;
388
388
  }
389
389
 
390
- inline fn matches(actions: Actions, i: usize, frontier: []Actions) bool {
390
+ fn matches(actions: Actions, i: usize, frontier: []Actions) bool {
391
391
  for (frontier, 0..) |f, j| {
392
392
  // TODO: is skipping this redundant check worth it?
393
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
 
@@ -157,7 +159,7 @@ pub const Action = packed struct(u64) {
157
159
  pub const Field = std.meta.FieldEnum(Action);
158
160
 
159
161
  /// Perform a reset by clearing fields which should not persist across updates.
160
- pub inline fn reset(self: *Action) void {
162
+ pub fn reset(self: *Action) void {
161
163
  self.* = @bitCast(@as(u64, @bitCast(self.*)) & DURATIONS);
162
164
  }
163
165
 
@@ -215,7 +217,7 @@ pub const Action = packed struct(u64) {
215
217
  };
216
218
 
217
219
  test Action {
218
- var a = Action{ .hit = .true, .durations = .{ .sleep = 3 }, .damage = 225 };
220
+ var a: Action = .{ .hit = .true, .durations = .{ .sleep = 3 }, .damage = 225 };
219
221
  a.reset();
220
222
 
221
223
  try expectEqual(Action{ .hit = .None, .durations = .{ .sleep = 3 }, .damage = 0 }, a);
@@ -514,22 +516,6 @@ pub fn Chance(comptime Rational: type) type {
514
516
  }
515
517
  }
516
518
 
517
- pub fn disable(self: *Self, player: Player, turns: u4) Error!void {
518
- if (!enabled) return;
519
-
520
- var durations = &self.actions.get(player).durations;
521
- const n = durations.disable;
522
- if (turns == 0) {
523
- assert(n >= 1 and n <= 8);
524
- if (n != 8) try self.probability.update(1, 9 - @as(u4, n));
525
- durations.disable = 0;
526
- } else {
527
- assert(n >= 1 and n < 8);
528
- try self.probability.update(9 - @as(u4, n) - 1, 9 - @as(u4, n));
529
- durations.disable += 1;
530
- }
531
- }
532
-
533
519
  pub fn confusion(self: *Self, player: Player, turns: u4) Error!void {
534
520
  if (!enabled) return;
535
521
 
@@ -546,6 +532,22 @@ pub fn Chance(comptime Rational: type) type {
546
532
  }
547
533
  }
548
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
+
549
551
  pub fn attacking(self: *Self, player: Player, turns: u4) Error!void {
550
552
  if (!enabled) return;
551
553
 
@@ -593,7 +595,7 @@ pub fn Chance(comptime Rational: type) type {
593
595
  pub fn metronome(self: *Self, player: Player, move: Move) Error!void {
594
596
  if (!enabled) return;
595
597
 
596
- try self.probability.update(1, Move.size - 2);
598
+ try self.probability.update(1, Move.METRONOME.len);
597
599
  self.actions.get(player).metronome = move;
598
600
  }
599
601
  };
@@ -704,19 +706,19 @@ test "Chance.damage" {
704
706
 
705
707
  try chance.damage(.P1, 217);
706
708
  if (showdown) {
707
- try expectValue(@as(u8, 217), chance.actions.p1.damage);
709
+ try expectValue(217, chance.actions.p1.damage);
708
710
  try expectProbability(&chance.probability, 1, 39);
709
711
  } else {
710
712
  try expectProbability(&chance.probability, 1, 1);
711
- try expectValue(@as(u8, 0), chance.actions.p1.damage);
713
+ try expectValue(0, chance.actions.p1.damage);
712
714
 
713
715
  try chance.commit(.P1, .miss);
714
716
  try expectProbability(&chance.probability, 1, 1);
715
- try expectValue(@as(u8, 0), chance.actions.p1.damage);
717
+ try expectValue(0, chance.actions.p1.damage);
716
718
 
717
719
  try chance.commit(.P1, .hit);
718
720
  try expectProbability(&chance.probability, 1, 39);
719
- try expectValue(@as(u8, 217), chance.actions.p1.damage);
721
+ try expectValue(217, chance.actions.p1.damage);
720
722
  }
721
723
  }
722
724
 
@@ -768,13 +770,13 @@ test "Chance.moveSlot" {
768
770
 
769
771
  try chance.moveSlot(.P2, 2, &ms, 2);
770
772
  try expectProbability(&chance.probability, 1, 2);
771
- try expectValue(@as(u4, 2), chance.actions.p2.move_slot);
773
+ try expectValue(2, chance.actions.p2.move_slot);
772
774
 
773
775
  chance.reset();
774
776
 
775
777
  try chance.moveSlot(.P1, 1, &ms, 0);
776
778
  try expectProbability(&chance.probability, 1, 3);
777
- try expectValue(@as(u4, 1), chance.actions.p1.move_slot);
779
+ try expectValue(1, chance.actions.p1.move_slot);
778
780
  }
779
781
 
780
782
  test "Chance.multiHit" {
@@ -782,32 +784,32 @@ test "Chance.multiHit" {
782
784
 
783
785
  try chance.multiHit(.P1, 3);
784
786
  try expectProbability(&chance.probability, 3, 8);
785
- try expectValue(@as(u4, 3), chance.actions.p1.multi_hit);
787
+ try expectValue(3, chance.actions.p1.multi_hit);
786
788
 
787
789
  chance.reset();
788
790
 
789
791
  try chance.multiHit(.P2, 5);
790
792
  try expectProbability(&chance.probability, 1, 8);
791
- try expectValue(@as(u4, 5), chance.actions.p2.multi_hit);
793
+ try expectValue(5, chance.actions.p2.multi_hit);
792
794
  }
793
795
 
794
796
  test "Chance.duration" {
795
797
  var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
796
798
 
797
799
  chance.duration(.sleep, .P1, .P2, 2);
798
- try expectValue(@as(u4, 2), chance.actions.p1.duration);
799
- try expectValue(@as(u3, 1), chance.actions.p2.durations.sleep);
800
+ try expectValue(2, chance.actions.p1.duration);
801
+ try expectValue(1, chance.actions.p2.durations.sleep);
800
802
 
801
803
  chance.reset();
802
804
 
803
805
  chance.duration(.binding, .P2, .P2, 4);
804
806
  if (!showdown) {
805
- try expectValue(@as(u4, 0), chance.actions.p2.duration);
806
- try expectValue(@as(u3, 0), chance.actions.p2.durations.binding);
807
+ try expectValue(0, chance.actions.p2.duration);
808
+ try expectValue(0, chance.actions.p2.durations.binding);
807
809
  try chance.commit(.P2, .hit);
808
810
  }
809
- try expectValue(@as(u4, 4), chance.actions.p2.duration);
810
- try expectValue(@as(u3, 1), chance.actions.p2.durations.binding);
811
+ try expectValue(4, chance.actions.p2.duration);
812
+ try expectValue(1, chance.actions.p2.durations.binding);
811
813
  }
812
814
 
813
815
  test "Chance.sleep" {
@@ -817,7 +819,7 @@ test "Chance.sleep" {
817
819
  chance.actions.p1.durations.sleep = @intCast(i);
818
820
  try chance.sleep(.P1, 0);
819
821
  try expectProbability(&chance.probability, 1, d);
820
- try expectValue(@as(u3, 0), chance.actions.p1.durations.sleep);
822
+ try expectValue(0, chance.actions.p1.durations.sleep);
821
823
 
822
824
  chance.reset();
823
825
 
@@ -840,7 +842,7 @@ test "Chance.confusion" {
840
842
  chance.actions.p2.durations.confusion = @intCast(i);
841
843
  try chance.confusion(.P2, 0);
842
844
  try expectProbability(&chance.probability, 1, d);
843
- try expectValue(@as(u3, 0), chance.actions.p2.durations.confusion);
845
+ try expectValue(0, chance.actions.p2.durations.confusion);
844
846
 
845
847
  chance.reset();
846
848
  }
@@ -856,6 +858,28 @@ test "Chance.confusion" {
856
858
  }
857
859
  }
858
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(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
+
859
883
  test "Chance.attacking" {
860
884
  var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
861
885
 
@@ -864,7 +888,7 @@ test "Chance.attacking" {
864
888
  chance.actions.p2.durations.attacking = @intCast(i);
865
889
  try chance.attacking(.P2, 0);
866
890
  try expectProbability(&chance.probability, 1, d);
867
- try expectValue(@as(u3, 0), chance.actions.p2.durations.attacking);
891
+ try expectValue(0, chance.actions.p2.durations.attacking);
868
892
 
869
893
  chance.reset();
870
894
  }
@@ -890,7 +914,7 @@ test "Chance.binding" {
890
914
  chance.actions.p1.durations.binding = @intCast(i);
891
915
  try chance.binding(.P1, 0);
892
916
  try expectProbability(&chance.probability, p, q);
893
- try expectValue(@as(u3, 0), chance.actions.p1.durations.binding);
917
+ try expectValue(0, chance.actions.p1.durations.binding);
894
918
 
895
919
  chance.reset();
896
920
 
@@ -910,7 +934,7 @@ test "Chance.psywave" {
910
934
 
911
935
  try chance.psywave(.P2, 100, 150);
912
936
  try expectProbability(&chance.probability, 1, 150);
913
- try expectValue(@as(u8, 101), chance.actions.p2.psywave);
937
+ try expectValue(101, chance.actions.p2.psywave);
914
938
  }
915
939
 
916
940
  test "Chance.metronome" {
@@ -939,7 +963,7 @@ pub fn expectValue(a: anytype, b: anytype) !void {
939
963
 
940
964
  /// Null object pattern implementation of Generation I `Chance` which does nothing, though chance
941
965
  /// tracking should additionally be turned off entirely via `options.chance`.
942
- pub const NULL = Null{};
966
+ pub const NULL: Null = .{};
943
967
 
944
968
  const Null = struct {
945
969
  pub const Error = error{};
@@ -976,10 +1000,6 @@ const Null = struct {
976
1000
  _ = .{ self, player, roll };
977
1001
  }
978
1002
 
979
- pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
980
- _ = .{ self, player, proc, rate };
981
- }
982
-
983
1003
  pub fn confused(self: Null, player: Player, ok: bool) Error!void {
984
1004
  _ = .{ self, player, ok };
985
1005
  }
@@ -988,6 +1008,10 @@ const Null = struct {
988
1008
  _ = .{ self, player, ok };
989
1009
  }
990
1010
 
1011
+ pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
1012
+ _ = .{ self, player, proc, rate };
1013
+ }
1014
+
991
1015
  pub fn moveSlot(self: Null, player: Player, slot: u4, ms: []const MoveSlot, n: u4) Error!void {
992
1016
  _ = .{ self, player, slot, ms, n };
993
1017
  }