@pkmn/engine 0.1.0-dev.47feedd9 → 0.1.0-dev.4cc7c5c2

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 (50) hide show
  1. package/README.md +55 -44
  2. package/build/pkg/addon.js +3 -2
  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 +25 -3
  6. package/build/pkg/data/protocol.json +69 -12
  7. package/build/pkg/data.d.ts +2 -2
  8. package/build/pkg/gen1.d.ts +2 -1
  9. package/build/pkg/gen1.js +36 -18
  10. package/build/pkg/gen1.js.map +1 -1
  11. package/build/pkg/index.d.ts +4 -2
  12. package/build/pkg/index.js.map +1 -1
  13. package/build/pkg/protocol.d.ts +4 -2
  14. package/build/pkg/protocol.js +60 -39
  15. package/build/pkg/protocol.js.map +1 -1
  16. package/build/test/display.js +7 -3
  17. package/build/test/display.js.map +1 -1
  18. package/build.zig +89 -65
  19. package/package.json +34 -16
  20. package/src/bin/install-pkmn-engine +10 -5
  21. package/src/lib/binding/node.zig +46 -141
  22. package/src/lib/binding/wasm.zig +4 -2
  23. package/src/lib/common/battle.zig +17 -0
  24. package/src/lib/common/data.zig +15 -27
  25. package/src/lib/common/debug.zig +20 -11
  26. package/src/lib/common/js.zig +118 -0
  27. package/src/lib/common/optional.zig +67 -0
  28. package/src/lib/common/options.zig +9 -0
  29. package/src/lib/common/protocol.zig +715 -199
  30. package/src/lib/common/rational.zig +353 -0
  31. package/src/lib/common/rng.zig +15 -15
  32. package/src/lib/common/util.zig +12 -0
  33. package/src/lib/gen1/README.md +250 -153
  34. package/src/lib/gen1/calc.zig +426 -0
  35. package/src/lib/gen1/chance.zig +1060 -0
  36. package/src/lib/gen1/data/moves.zig +192 -180
  37. package/src/lib/gen1/data/species.zig +2 -2
  38. package/src/lib/gen1/data/types.zig +5 -5
  39. package/src/lib/gen1/data.zig +73 -59
  40. package/src/lib/gen1/helpers.zig +577 -35
  41. package/src/lib/gen1/mechanics.zig +835 -528
  42. package/src/lib/gen2/README.md +106 -85
  43. package/src/lib/gen2/data/items.zig +12 -18
  44. package/src/lib/gen2/data/moves.zig +660 -566
  45. package/src/lib/gen2/data/species.zig +1 -1
  46. package/src/lib/gen2/data/types.zig +130 -28
  47. package/src/lib/gen2/data.zig +131 -83
  48. package/src/lib/gen2/helpers.zig +16 -22
  49. package/src/lib/gen2/mechanics.zig +2594 -11
  50. package/src/lib/pkmn.zig +61 -2
@@ -1,8 +1,9 @@
1
1
  const std = @import("std");
2
2
 
3
+ const pkmn = @import("../pkmn.zig");
4
+
3
5
  const common = @import("../common/data.zig");
4
6
  const DEBUG = @import("../common/debug.zig").print;
5
- const options = @import("../common/options.zig");
6
7
  const protocol = @import("../common/protocol.zig");
7
8
  const rng = @import("../common/rng.zig");
8
9
 
@@ -17,7 +18,7 @@ const ID = common.ID;
17
18
  const Player = common.Player;
18
19
  const Result = common.Result;
19
20
 
20
- const showdown = options.showdown;
21
+ const showdown = pkmn.options.showdown;
21
22
 
22
23
  const Boost = protocol.Boost;
23
24
  const Damage = protocol.Damage;
@@ -56,11 +57,9 @@ const BOOSTS = &[_][2]u8{
56
57
 
57
58
  const MAX_STAT_VALUE = 999;
58
59
 
59
- const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
60
-
61
- pub fn update(battle: anytype, c1: Choice, c2: Choice, log: anytype) !Result {
60
+ pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result {
62
61
  assert(c1.type != .Pass or c2.type != .Pass or battle.turn == 0);
63
- if (battle.turn == 0) return start(battle, log);
62
+ if (battle.turn == 0) return start(battle, options);
64
63
 
65
64
  var s1 = false;
66
65
  var s2 = false;
@@ -76,19 +75,19 @@ pub fn update(battle: anytype, c1: Choice, c2: Choice, log: anytype) !Result {
76
75
  var r1 = showdown and p1.active.volatiles.Binding and c2.type == .Switch;
77
76
  var r2 = showdown and p2.active.volatiles.Binding and c1.type == .Switch;
78
77
 
79
- if (turnOrder(battle, c1, c2) == .P1) {
80
- if (try doTurn(battle, .P1, c1, r1, s1, f1, .P2, c2, r2, s2, f2, log)) |r| return r;
78
+ if (try turnOrder(battle, c1, c2, options) == .P1) {
79
+ if (try doTurn(battle, .P1, c1, r1, s1, f1, .P2, c2, r2, s2, f2, options)) |r| return r;
81
80
  } else {
82
- if (try doTurn(battle, .P2, c2, r2, s2, f2, .P1, c1, r1, s1, f1, log)) |r| return r;
81
+ if (try doTurn(battle, .P2, c2, r2, s2, f2, .P1, c1, r1, s1, f1, options)) |r| return r;
83
82
  }
84
83
 
85
84
  if (p1.active.volatiles.attacks == 0) p1.active.volatiles.Binding = false;
86
85
  if (p2.active.volatiles.attacks == 0) p2.active.volatiles.Binding = false;
87
86
 
88
- return endTurn(battle, log);
87
+ return endTurn(battle, options);
89
88
  }
90
89
 
91
- fn start(battle: anytype, log: anytype) !Result {
90
+ fn start(battle: anytype, options: anytype) !Result {
92
91
  const p1 = battle.side(.P1);
93
92
  const p2 = battle.side(.P2);
94
93
 
@@ -100,10 +99,10 @@ fn start(battle: anytype, log: anytype) !Result {
100
99
  assert(!showdown or p2_slot == 1);
101
100
  if (p2_slot == 0) return Result.Win;
102
101
 
103
- try switchIn(battle, .P1, p1_slot, true, log);
104
- try switchIn(battle, .P2, p2_slot, true, log);
102
+ try switchIn(battle, .P1, p1_slot, true, options);
103
+ try switchIn(battle, .P2, p2_slot, true, options);
105
104
 
106
- return endTurn(battle, log);
105
+ return endTurn(battle, options);
107
106
  }
108
107
 
109
108
  fn findFirstAlive(side: *const Side) u8 {
@@ -126,10 +125,13 @@ fn selectMove(
126
125
  const stored = side.stored();
127
126
 
128
127
  assert(!isForced(side.active) or
129
- (choice.type == .Move and choice.data == @boolToInt(showdown)));
128
+ (choice.type == .Move and choice.data == @intFromBool(showdown)));
130
129
 
131
130
  // pre-battle menu
132
- if (volatiles.Recharging) return null;
131
+ if (volatiles.Recharging) {
132
+ if (showdown and battle.foe(player).active.volatiles.Binding) skip_turn.* = true;
133
+ return null;
134
+ }
133
135
  if (volatiles.Rage) {
134
136
  from.* = side.last_used_move;
135
137
  if (showdown) {
@@ -176,11 +178,7 @@ fn selectMove(
176
178
  assert(choice.data == 0);
177
179
  // GLITCH: https://glitchcity.wiki/Partial_trapping_move_Mirror_Move_link_battle_glitch
178
180
  if (foe_choice.type == .Switch) {
179
- const slot = if (player == .P1)
180
- battle.last_selected_indexes.p1
181
- else
182
- battle.last_selected_indexes.p2;
183
- const last = side.active.move(@intCast(u4, slot));
181
+ const last = side.active.move(battle.lastMove(player).index);
184
182
  if (last.id == .Metronome) side.last_selected_move = last.id;
185
183
  if (last.id == .MirrorMove) return Result.Error;
186
184
  }
@@ -208,12 +206,9 @@ fn selectMove(
208
206
  }
209
207
  break :ok true;
210
208
  };
211
-
212
209
  assert(struggle);
213
- saveMove(battle, player, choice);
214
- } else {
215
- saveMove(battle, player, choice);
216
210
  }
211
+ saveMove(battle, player, choice);
217
212
 
218
213
  return null;
219
214
  }
@@ -233,16 +228,12 @@ fn saveMove(battle: anytype, player: Player, choice: ?Choice) void {
233
228
  assert(showdown or move.pp != 0);
234
229
 
235
230
  side.last_selected_move = move.id;
236
- if (player == .P1) {
237
- battle.last_selected_indexes.p1 = @intCast(u4, c.data);
238
- } else {
239
- battle.last_selected_indexes.p2 = @intCast(u4, c.data);
240
- }
231
+ battle.lastMove(player).index = @intCast(c.data);
241
232
  }
242
233
  }
243
234
  }
244
235
 
245
- fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, log: anytype) !void {
236
+ fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, options: anytype) !void {
246
237
  var side = battle.side(player);
247
238
  var foe = battle.foe(player);
248
239
  var active = &side.active;
@@ -255,11 +246,7 @@ fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, log: anyty
255
246
  side.order[0] = side.order[slot - 1];
256
247
  side.order[slot - 1] = out;
257
248
 
258
- if (player == .P1) {
259
- battle.last_selected_indexes.p1 = 1;
260
- } else {
261
- battle.last_selected_indexes.p2 = 1;
262
- }
249
+ battle.lastMove(player).index = 1;
263
250
 
264
251
  side.last_used_move = .None;
265
252
  foe.last_used_move = .None;
@@ -275,17 +262,18 @@ fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, log: anyty
275
262
 
276
263
  foe.active.volatiles.Binding = false;
277
264
 
278
- try log.switched(battle.active(player), incoming);
265
+ try options.log.switched(battle.active(player), incoming);
266
+ options.chance.switched(player, side.order[0], out);
279
267
 
280
268
  if (showdown and incoming.status == Status.TOX) {
281
269
  incoming.status = Status.init(.PSN);
282
270
  // Technically, Pokémon Showdown adds these after *both* Pokémon have switched, but we'd
283
271
  // rather not clutter up turnOrder just for this (incorrect) log message
284
- try log.status(battle.active(player), incoming.status, .Silent);
272
+ try options.log.status(battle.active(player), incoming.status, .Silent);
285
273
  }
286
274
  }
287
275
 
288
- fn turnOrder(battle: anytype, c1: Choice, c2: Choice) Player {
276
+ fn turnOrder(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Player {
289
277
  assert(c1.type != .Pass or c2.type != .Pass);
290
278
 
291
279
  if (c1.type == .Pass) return .P2;
@@ -315,9 +303,11 @@ fn turnOrder(battle: anytype, c1: Choice, c2: Choice) Player {
315
303
  const spe2 = battle.side(.P2).active.stats.spe;
316
304
  if (spe1 == spe2) {
317
305
  // Pokémon Showdown's beforeTurnCallback shenanigans
318
- if (showdown and m1 == .Counter and m2 == .Counter) battle.rng.advance(1);
306
+ if (showdown and !double_switch and m1 == .Counter and m2 == .Counter) {
307
+ battle.rng.advance(1);
308
+ }
319
309
 
320
- const p1 = Rolls.speedTie(battle);
310
+ const p1 = try Rolls.speedTie(battle, options);
321
311
  if (!showdown) return if (p1) .P1 else .P2;
322
312
 
323
313
  // Pokémon Showdown's "lockedmove" volatile's onBeforeTurn uses BattleQueue#changeAction,
@@ -348,7 +338,7 @@ fn doTurn(
348
338
  foe_rewrap: bool,
349
339
  foe_skip: bool,
350
340
  foe_from: ?Move,
351
- log: anytype,
341
+ options: anytype,
352
342
  ) !?Result {
353
343
  assert(player_choice.type != .Pass);
354
344
 
@@ -362,19 +352,22 @@ fn doTurn(
362
352
  player_skip,
363
353
  player_from,
364
354
  &residual,
365
- log,
355
+ options,
366
356
  )) |r| return r;
367
357
  if (!replace) {
368
358
  if (player_choice.type != .Switch) {
369
- if (try checkFaint(battle, foe_player, log)) |r| return r;
359
+ if (try checkFaint(battle, foe_player, options)) |r| return r;
370
360
  }
371
- if (residual) try handleResidual(battle, player, log);
372
- if (try checkFaint(battle, player, log)) |r| return r;
361
+ if (residual) try handleResidual(battle, player, options);
362
+ if (try checkFaint(battle, player, options)) |r| return r;
373
363
  } else if (foe_choice.type == .Pass) return null;
374
364
 
365
+ options.chance.clearPending();
366
+
375
367
  residual = true;
376
368
  replace = battle.side(foe_player).stored().hp == 0;
377
- if (try executeMove(
369
+ const calc = pkmn.options.calc and foe_choice.type == .Pass;
370
+ if (if (calc) null else try executeMove(
378
371
  battle,
379
372
  foe_player,
380
373
  foe_choice,
@@ -382,14 +375,14 @@ fn doTurn(
382
375
  foe_skip,
383
376
  foe_from,
384
377
  &residual,
385
- log,
378
+ options,
386
379
  )) |r| return r;
387
380
  if (!replace) {
388
- if (foe_choice.type != .Switch) {
389
- if (try checkFaint(battle, player, log)) |r| return r;
381
+ if (!calc and foe_choice.type != .Switch) {
382
+ if (try checkFaint(battle, player, options)) |r| return r;
390
383
  }
391
- if (residual) try handleResidual(battle, foe_player, log);
392
- if (try checkFaint(battle, foe_player, log)) |r| return r;
384
+ if (residual) try handleResidual(battle, foe_player, options);
385
+ if (try checkFaint(battle, foe_player, options)) |r| return r;
393
386
  }
394
387
 
395
388
  // Flinch is bugged on Pokémon Showdown because it gets implemented with a duration which causes
@@ -410,12 +403,12 @@ fn executeMove(
410
403
  skip: bool,
411
404
  from: ?Move,
412
405
  residual: *bool,
413
- log: anytype,
406
+ options: anytype,
414
407
  ) !?Result {
415
408
  var side = battle.side(player);
416
409
 
417
410
  if (choice.type == .Switch) {
418
- try switchIn(battle, player, @intCast(u4, choice.data), false, log);
411
+ try switchIn(battle, player, choice.data, false, options);
419
412
  return null;
420
413
  }
421
414
 
@@ -425,13 +418,13 @@ fn executeMove(
425
418
  if (side.last_selected_move == .SKIP_TURN) {
426
419
  assert(!showdown);
427
420
  if (battle.foe(player).active.volatiles.Binding) {
428
- try log.cant(battle.active(player), .Bound);
421
+ try options.log.cant(battle.active(player), .Bound);
429
422
  }
430
423
  return null;
431
424
  }
432
425
 
433
426
  assert(choice.type == .Move);
434
- var mslot = @intCast(u4, choice.data);
427
+ var mslot: u4 = @intCast(choice.data);
435
428
  // Sadly, we can't even check `Move.get(side.last_selected_move).effect == .Binding` here
436
429
  // because Pokémon Showdown's Mirror Move implementation clobbers side.last_selected_move
437
430
  var auto = showdown and side.last_selected_move != .None;
@@ -440,10 +433,7 @@ fn executeMove(
440
433
  if (mslot == 0 and side.last_selected_move != .None and side.last_selected_move != .Struggle) {
441
434
  // choice.data == 0 only happens with Struggle on Pokémon Showdown
442
435
  assert(!showdown);
443
- mslot = @intCast(u4, if (player == .P1)
444
- battle.last_selected_indexes.p1
445
- else
446
- battle.last_selected_indexes.p2);
436
+ mslot = @intCast(battle.lastMove(player).index);
447
437
  const stored = side.stored();
448
438
  // GLITCH: Struggle bypass PP underflow via Hyper Beam / Trapping-switch auto selection
449
439
  auto = side.last_selected_move == .HyperBeam or side.active.volatiles.Bide or
@@ -464,15 +454,12 @@ fn executeMove(
464
454
  } else if (showdown and side.active.volatiles.Charging) {
465
455
  // Incorrect mslot with Pokémon Showdown choice semantics so we need to recover from index
466
456
  assert(mslot == 1);
467
- mslot = @intCast(u4, if (player == .P1)
468
- battle.last_selected_indexes.p1
469
- else
470
- battle.last_selected_indexes.p2);
457
+ mslot = @intCast(battle.lastMove(player).index);
471
458
  }
472
459
 
473
460
  var skip_can = false;
474
461
  var skip_pp = false;
475
- switch (try beforeMove(battle, player, skip, from, residual, log)) {
462
+ switch (try beforeMove(battle, player, skip, from, residual, options)) {
476
463
  .done => return null,
477
464
  .skip_can => skip_can = true,
478
465
  .skip_pp => skip_pp = true,
@@ -480,11 +467,11 @@ fn executeMove(
480
467
  .err => return @as(?Result, Result.Error),
481
468
  }
482
469
 
483
- if (!skip_can and !try canMove(battle, player, mslot, auto, skip_pp, from, residual, log)) {
470
+ if (!skip_can and !try canMove(battle, player, mslot, auto, skip_pp, from, residual, options)) {
484
471
  return null;
485
472
  }
486
473
 
487
- return doMove(battle, player, mslot, rewrap, auto, residual, log);
474
+ return doMove(battle, player, mslot, rewrap, auto, residual, options);
488
475
  }
489
476
 
490
477
  const BeforeMove = enum { done, skip_can, skip_pp, ok, err };
@@ -495,8 +482,9 @@ fn beforeMove(
495
482
  skip: bool,
496
483
  from: ?Move,
497
484
  residual: *bool,
498
- log: anytype,
485
+ options: anytype,
499
486
  ) !BeforeMove {
487
+ var log = options.log;
500
488
  var side = battle.side(player);
501
489
  const foe = battle.foe(player);
502
490
  var active = &side.active;
@@ -505,11 +493,21 @@ fn beforeMove(
505
493
  var volatiles = &active.volatiles;
506
494
 
507
495
  if (Status.is(stored.status, .SLP)) {
508
- const status = stored.status;
496
+ const before = stored.status;
509
497
  // Even if the EXT bit is set this will still correctly modify the sleep duration
510
- stored.status -= 1;
511
- if (Status.duration(stored.status) == 0) {
512
- try log.curestatus(ident, status, .Message);
498
+ if (options.calc.modify(player, .sleep)) |extend| {
499
+ if (!extend) stored.status = 0;
500
+ } else {
501
+ stored.status -= 1;
502
+ }
503
+
504
+ const duration = Status.duration(stored.status);
505
+ if (!Status.is(stored.status, .EXT)) {
506
+ try options.chance.sleep(player, duration);
507
+ }
508
+
509
+ if (duration == 0) {
510
+ try log.curestatus(ident, before, .Message);
513
511
  stored.status = 0; // clears EXT if present
514
512
  } else {
515
513
  try log.cant(ident, .Sleep);
@@ -544,14 +542,20 @@ fn beforeMove(
544
542
  }
545
543
 
546
544
  if (volatiles.disabled_duration > 0) {
547
- volatiles.disabled_duration -= 1;
545
+ if (options.calc.modify(player, .disable)) |extend| {
546
+ if (!extend) volatiles.disabled_duration = 0;
547
+ } else {
548
+ volatiles.disabled_duration -= 1;
549
+ }
550
+ try options.chance.disable(player, volatiles.disabled_duration);
551
+
548
552
  if (volatiles.disabled_duration == 0) {
549
553
  volatiles.disabled_move = 0;
550
554
  try log.end(ident, .Disable);
551
555
  }
552
556
  }
553
557
  // Pokémon Showdown's disable condition has a single onBeforeMove handler
554
- if (showdown and try disabled(side, ident, log)) return .done;
558
+ if (showdown and try disabled(side, ident, options)) return .done;
555
559
 
556
560
  // This can only happen if a Pokémon started the battle frozen/sleeping and was thawed/woken
557
561
  // before the side had a selected a move - we simply need to assume this leads to a desync
@@ -563,14 +567,20 @@ fn beforeMove(
563
567
  if (volatiles.Confusion) {
564
568
  assert(volatiles.confusion > 0);
565
569
 
566
- volatiles.confusion -= 1;
570
+ if (options.calc.modify(player, .confusion)) |extend| {
571
+ if (!extend) volatiles.confusion = 0;
572
+ } else {
573
+ volatiles.confusion -= 1;
574
+ }
575
+ try options.chance.confusion(player, volatiles.confusion);
576
+
567
577
  if (volatiles.confusion == 0) {
568
578
  volatiles.Confusion = false;
569
579
  try log.end(ident, .Confusion);
570
580
  } else {
571
581
  try log.activate(ident, .Confusion);
572
582
 
573
- if (Rolls.confused(battle)) {
583
+ if (try Rolls.confused(battle, player, options)) {
574
584
  assert(!volatiles.MultiHit);
575
585
  if (!volatiles.Rage) volatiles.state = 0;
576
586
  volatiles.Bide = false;
@@ -580,6 +590,7 @@ fn beforeMove(
580
590
  volatiles.Charging = false;
581
591
  volatiles.Binding = false;
582
592
  volatiles.Invulnerable = false;
593
+ options.chance.clearDurations(player, null);
583
594
  {
584
595
  // This feels (and is) disgusting but the cartridge literally just overwrites
585
596
  // the opponent's defense with the user's defense and resets it after. As a
@@ -599,7 +610,7 @@ fn beforeMove(
599
610
 
600
611
  const uncapped = battle.last_damage;
601
612
  // Skipping adjustDamage / randomizeDamage / checkHit
602
- _ = try applyDamage(battle, player, target, .Confusion, log);
613
+ _ = try applyDamage(battle, player, target, .Confusion, options);
603
614
  // Pokémon Showdown thinks that confusion damage is uncapped ¯\_(ツ)_/¯
604
615
  if (showdown) battle.last_damage = uncapped;
605
616
 
@@ -608,14 +619,15 @@ fn beforeMove(
608
619
  }
609
620
  }
610
621
 
611
- if (!showdown and try disabled(side, ident, log)) return .done;
622
+ if (!showdown and try disabled(side, ident, options)) return .done;
612
623
 
613
- if (Status.is(stored.status, .PAR) and Rolls.paralyzed(battle)) {
624
+ if (Status.is(stored.status, .PAR) and try Rolls.paralyzed(battle, player, options)) {
614
625
  if (!volatiles.Rage) volatiles.state = 0;
615
626
  volatiles.Bide = false;
616
627
  volatiles.Thrashing = false;
617
628
  volatiles.Charging = false;
618
629
  volatiles.Binding = false;
630
+ options.chance.clearDurations(player, null);
619
631
  // GLITCH: Invulnerable is not cleared, resulting in permanent Fly/Dig invulnerability
620
632
  try log.cant(ident, .Paralysis);
621
633
  return .done;
@@ -624,11 +636,24 @@ fn beforeMove(
624
636
  if (volatiles.Bide) {
625
637
  assert(!volatiles.Thrashing and !volatiles.Rage);
626
638
 
627
- volatiles.state +%= battle.last_damage;
639
+ if (showdown) {
640
+ // Pokémon Showdown doesn't implement Bide potentially overflowing in the event of
641
+ // OHKO-move damage, but we can fake this incorrect behavior by simply saturating the
642
+ // addition because 65535 is sufficient to faint any Pokémon anyway
643
+ volatiles.state +|= battle.last_damage;
644
+ } else {
645
+ volatiles.state +%= battle.last_damage;
646
+ }
628
647
 
629
648
  assert(volatiles.attacks > 0);
630
649
 
631
- volatiles.attacks -= 1;
650
+ if (options.calc.modify(player, .attacking)) |extend| {
651
+ if (!extend) volatiles.attacks = 0;
652
+ } else {
653
+ volatiles.attacks -= 1;
654
+ }
655
+ try options.chance.attacking(player, volatiles.attacks);
656
+
632
657
  if (volatiles.attacks != 0) {
633
658
  try log.activate(ident, .Bide);
634
659
  return .done;
@@ -646,15 +671,15 @@ fn beforeMove(
646
671
  }
647
672
 
648
673
  const sub = showdown and foe.active.volatiles.Substitute;
649
- _ = try applyDamage(battle, player.foe(), player.foe(), .None, log);
650
- if (foe.stored().hp > 0 and !sub) try buildRage(battle, player.foe(), log);
674
+ _ = try applyDamage(battle, player.foe(), player.foe(), .None, options);
675
+ if (foe.stored().hp > 0 and !sub) try buildRage(battle, player.foe(), options);
651
676
 
652
677
  // For reasons passing understanding, Pokémon Showdown still inflicts residual damage to
653
678
  // Bide's user even if the above damage has caused the foe to faint. It's simpler to always
654
679
  // run residual here regardless of whether the foe fainted and opt-out of the default flow
655
680
  if (showdown) {
656
681
  residual.* = false;
657
- try handleResidual(battle, player, log);
682
+ try handleResidual(battle, player, options);
658
683
  }
659
684
 
660
685
  return .done;
@@ -663,8 +688,15 @@ fn beforeMove(
663
688
  if (volatiles.Thrashing) {
664
689
  assert(volatiles.attacks > 0);
665
690
  assert(from != null);
666
- volatiles.attacks -= 1;
667
- if (!showdown and handleThrashing(battle, active)) {
691
+
692
+ if (options.calc.modify(player, .attacking)) |extend| {
693
+ if (!extend) volatiles.attacks = 0;
694
+ } else {
695
+ volatiles.attacks -= 1;
696
+ }
697
+ try options.chance.attacking(player, volatiles.attacks);
698
+
699
+ if (!showdown and handleThrashing(battle, active, player, options)) {
668
700
  try log.start(battle.active(player), .ConfusionSilent);
669
701
  }
670
702
  try log.move(ident, side.last_selected_move, battle.active(player.foe()), from);
@@ -672,7 +704,7 @@ fn beforeMove(
672
704
  // This shouldn't actually set last_used_move, but Pokémon Showdown sets last
673
705
  // used in useMove and doesn't have the notion of skipping canMove semantics
674
706
  side.last_used_move = side.last_selected_move;
675
- if (handleThrashing(battle, active)) {
707
+ if (handleThrashing(battle, active, player, options)) {
676
708
  try log.start(battle.active(player), .ConfusionSilent);
677
709
  }
678
710
  }
@@ -681,7 +713,14 @@ fn beforeMove(
681
713
 
682
714
  if (volatiles.Binding) {
683
715
  assert(volatiles.attacks > 0);
684
- volatiles.attacks -= 1;
716
+
717
+ if (options.calc.modify(player, .binding)) |extend| {
718
+ if (!extend) volatiles.attacks = 0;
719
+ } else {
720
+ volatiles.attacks -= 1;
721
+ }
722
+ try options.chance.binding(player, volatiles.attacks);
723
+
685
724
  try log.move(
686
725
  battle.active(player),
687
726
  side.last_selected_move,
@@ -690,9 +729,9 @@ fn beforeMove(
690
729
  );
691
730
  if (showdown or battle.last_damage != 0) {
692
731
  const sub = showdown and foe.active.volatiles.Substitute;
693
- _ = try applyDamage(battle, player.foe(), player.foe(), .None, log);
732
+ _ = try applyDamage(battle, player.foe(), player.foe(), .None, options);
694
733
  if (battle.foe(player).stored().hp > 0 and !sub) {
695
- try buildRage(battle, player.foe(), log);
734
+ try buildRage(battle, player.foe(), options);
696
735
  }
697
736
  }
698
737
  return .done;
@@ -709,7 +748,7 @@ fn canMove(
709
748
  skip_pp: bool,
710
749
  from: ?Move,
711
750
  residual: *bool,
712
- log: anytype,
751
+ options: anytype,
713
752
  ) !bool {
714
753
  var side = battle.side(player);
715
754
  const player_ident = battle.active(player);
@@ -724,8 +763,9 @@ fn canMove(
724
763
  side.active.volatiles.Charging = false;
725
764
  side.active.volatiles.Invulnerable = false;
726
765
  } else if (move.effect == .Charge) {
727
- try log.move(player_ident, side.last_selected_move, .{}, from);
728
- try Effects.charge(battle, player, log);
766
+ try options.log.move(player_ident, side.last_selected_move, .{}, from);
767
+ setCounterable(battle, player, side, move);
768
+ try Effects.charge(battle, player, options);
729
769
  return false;
730
770
  }
731
771
 
@@ -740,23 +780,35 @@ fn canMove(
740
780
  // relevant for control flow, but we don't have anything useful to put in the log. This is
741
781
  // regrettable, but this information is more "nice to have" than required
742
782
  const f = if (from != null and from.? == .None) null else from;
743
- try log.move(player_ident, side.last_selected_move, battle.active(target), f);
783
+ try options.log.move(player_ident, side.last_selected_move, battle.active(target), f);
784
+ setCounterable(battle, player, side, move);
744
785
 
745
786
  if (move.effect.onBegin()) {
746
- try onBegin(battle, player, move, mslot, residual, log);
787
+ try onBegin(battle, player, move, mslot, residual, options);
747
788
  return false;
748
789
  }
749
790
 
750
791
  if (move.effect == .Thrashing) {
751
- Effects.thrashing(battle, player);
792
+ Effects.thrashing(battle, player, options);
752
793
  } else if (!showdown and move.effect == .Binding) {
753
794
  // Pokémon Showdown handles this after hit/miss checks and damage calculation
754
- Effects.binding(battle, player, false);
795
+ try Effects.binding(battle, player, false, options);
755
796
  }
756
797
 
757
798
  return true;
758
799
  }
759
800
 
801
+ fn setCounterable(battle: anytype, player: Player, side: *Side, move: Move.Data) void {
802
+ // The Counter desync is caused by the cartridge not calling GetCurrentMove until now, meaning
803
+ // in cases where an early return happens the data for a players last selected move does not get
804
+ // reloaded and HandleCounterMove actually bases its success/failure off of stale information.
805
+ // This boolean state we track here doesn't exist on the cartridge because it instead manifests
806
+ // as the desync results from actually having two separate battle states that subtly disagree
807
+ const counterable = side.last_selected_move != .Counter and move.bp > 0 and
808
+ (move.type == .Normal or move.type == .Fighting);
809
+ battle.lastMove(player).counterable = @intFromBool(counterable);
810
+ }
811
+
760
812
  fn decrementPP(side: *Side, mslot: u4, auto: bool) void {
761
813
  if (side.last_selected_move == .Struggle) return;
762
814
 
@@ -767,11 +819,11 @@ fn decrementPP(side: *Side, mslot: u4, auto: bool) void {
767
819
  if (volatiles.Bide) return;
768
820
 
769
821
  assert(active.move(mslot).pp > 0 or auto);
770
- active.move(mslot).pp = @intCast(u6, active.move(mslot).pp) -% 1;
822
+ active.move(mslot).pp = @as(u6, @intCast(active.move(mslot).pp)) -% 1;
771
823
  if (volatiles.Transform) return;
772
824
 
773
825
  assert(side.stored().move(mslot).pp > 0 or auto);
774
- side.stored().move(mslot).pp = @intCast(u6, side.stored().move(mslot).pp) -% 1;
826
+ side.stored().move(mslot).pp = @as(u6, @intCast(side.stored().move(mslot).pp)) -% 1;
775
827
  assert(active.move(mslot).pp == side.stored().move(mslot).pp);
776
828
  }
777
829
 
@@ -779,12 +831,12 @@ fn incrementPP(side: *Side, mslot: u4) void {
779
831
  var active = &side.active;
780
832
  const volatiles = &active.volatiles;
781
833
 
782
- active.move(mslot).pp = @intCast(u6, active.move(mslot).pp) +% 1;
834
+ active.move(mslot).pp = @as(u6, @intCast(active.move(mslot).pp)) +% 1;
783
835
  // GLITCH: No check for Transform means an empty/incorrect stored slot can get incremented
784
836
  if (showdown and volatiles.Transform) return;
785
837
 
786
838
  assert(mslot > 0 and mslot <= 4);
787
- side.stored().moves[mslot - 1].pp = @intCast(u6, side.stored().moves[mslot - 1].pp) +% 1;
839
+ side.stored().moves[mslot - 1].pp = @as(u6, @intCast(side.stored().moves[mslot - 1].pp)) +% 1;
788
840
  }
789
841
 
790
842
  // Pokémon Showdown does hit/multi/crit/damage instead of crit/damage/hit/multi
@@ -795,8 +847,9 @@ fn doMove(
795
847
  rewrap: bool,
796
848
  auto: bool,
797
849
  residual: *bool,
798
- log: anytype,
850
+ options: anytype,
799
851
  ) !?Result {
852
+ var log = options.log;
800
853
  var side = battle.side(player);
801
854
  const foe = battle.foe(player);
802
855
 
@@ -811,23 +864,27 @@ fn doMove(
811
864
  var hits: u4 = 1;
812
865
  var effectiveness = Effectiveness.neutral;
813
866
 
867
+ // Due to control flow shenanigans we need to clear last_damage for Pokémon Showdown
868
+ if (showdown and !counter) battle.last_damage = 0;
869
+
814
870
  // The cartridge handles set damage moves in applyDamage but we short circuit to simplify things
815
871
  if (move.effect == .SuperFang or move.effect == .SpecialDamage) {
816
- return specialDamage(battle, player, move, log);
872
+ return specialDamage(battle, player, move, options);
817
873
  }
818
874
 
819
875
  // Pokémon Showdown runs invulnerability / immunity checks before checking accuracy - simply
820
876
  // calling moveHit early covers most of that but we also need to check type immunity first
821
877
  var miss = showdown and miss: {
822
878
  immune = move.target != .Self and !status and !counter and
823
- (@enumToInt(move.type.effectiveness(foe.active.types.type1)) == 0 or
824
- @enumToInt(move.type.effectiveness(foe.active.types.type2)) == 0);
879
+ (@intFromEnum(move.type.effectiveness(foe.active.types.type1)) == 0 or
880
+ @intFromEnum(move.type.effectiveness(foe.active.types.type2)) == 0);
825
881
  if (immune and move.effect != .Binding) break :miss true;
826
882
  if (move.effect == .OHKO and side.active.stats.spe < foe.active.stats.spe) {
827
883
  battle.last_damage = 0;
828
884
  break :miss true;
829
885
  }
830
- break :miss move.target != .Self and !moveHit(battle, player, move, &immune, &mist);
886
+ break :miss (move.target != .Self and
887
+ !moveHit(battle, player, move, &immune, &mist, options));
831
888
  };
832
889
  assert(!immune or miss or (showdown and move.effect == .Binding));
833
890
 
@@ -835,16 +892,16 @@ fn doMove(
835
892
  const skip = status or immune;
836
893
  if ((!showdown or (!skip or counter)) and !miss) blk: {
837
894
  if (showdown and move.effect.isMulti()) {
838
- Effects.multiHit(battle, player, move);
895
+ try Effects.multiHit(battle, player, move, options);
839
896
  hits = side.active.volatiles.attacks;
840
897
  late = false;
841
898
  }
842
899
 
843
900
  // Cartridge rolls for crit even for moves that can't crit (Counter/Metronome/status/OHKO)
844
901
  const check = !showdown or (!counter and move.effect != .OHKO);
845
- if (check) crit = checkCriticalHit(battle, player, move);
902
+ if (check) crit = try checkCriticalHit(battle, player, move, options);
846
903
 
847
- if (counter) return counterDamage(battle, player, move, log);
904
+ if (counter) return counterDamage(battle, player, move, options);
848
905
 
849
906
  battle.last_damage = 0;
850
907
 
@@ -866,17 +923,15 @@ fn doMove(
866
923
  effectiveness = adjustDamage(battle, player);
867
924
  immune = effectiveness == 0;
868
925
  }
869
- randomizeDamage(battle);
926
+ try randomizeDamage(battle, player, options);
870
927
  }
871
928
  }
872
929
 
873
- // Due to control flow shenanigans we need to clear last_damage for Pokémon Showdown
874
- if (showdown and skip) battle.last_damage = 0;
875
-
930
+ var zero = battle.last_damage == 0;
876
931
  miss = if (showdown or skip)
877
932
  miss
878
933
  else
879
- (!moveHit(battle, player, move, &immune, &mist) or battle.last_damage == 0);
934
+ (!moveHit(battle, player, move, &immune, &mist, options) or zero);
880
935
 
881
936
  assert(showdown or miss or battle.last_damage > 0 or skip);
882
937
  assert((!showdown and miss) or !(ohko and immune));
@@ -884,11 +939,11 @@ fn doMove(
884
939
 
885
940
  if (!showdown or !miss) {
886
941
  if (move.effect == .MirrorMove) {
887
- return mirrorMove(battle, player, mslot, rewrap, auto, residual, log);
942
+ return mirrorMove(battle, player, mslot, rewrap, auto, residual, options);
888
943
  } else if (move.effect == .Metronome) {
889
- return metronome(battle, player, mslot, rewrap, auto, residual, log);
944
+ return metronome(battle, player, mslot, rewrap, auto, residual, options);
890
945
  } else if (move.effect.onEnd()) {
891
- try onEnd(battle, player, move, log);
946
+ try onEnd(battle, player, move, options);
892
947
  return null;
893
948
  }
894
949
  }
@@ -907,29 +962,34 @@ fn doMove(
907
962
  if (!foe.active.volatiles.Substitute) try log.activate(foe_ident, .Mist);
908
963
  try log.fail(foe_ident, .None);
909
964
  } else {
965
+ if (showdown or !zero) try options.chance.commit(player, .miss);
910
966
  try log.lastmiss();
911
967
  try log.miss(battle.active(player));
912
968
  }
913
969
  if (move.effect == .JumpKick) {
914
970
  // Recoil is supposed to be damage/8 but damage will always be 0 here
915
- assert(battle.last_damage == 0);
971
+ assert(showdown or battle.last_damage == 0);
916
972
  battle.last_damage = 1;
917
- _ = try applyDamage(battle, player, player.foe(), .None, log);
973
+ _ = try applyDamage(battle, player, player.foe(), .None, options);
918
974
  if (showdown and side.stored().hp == 0) residual.* = false;
919
975
  } else if (move.effect == .Explode) {
920
976
  try Effects.explode(battle, player);
921
- try buildRage(battle, player.foe(), log);
977
+ try buildRage(battle, player.foe(), options);
922
978
  } else if (showdown and move.effect == .Disable) {
923
- try buildRage(battle, player.foe(), log);
979
+ try buildRage(battle, player.foe(), options);
980
+ } else if (!showdown and immune and move.effect == .Binding) {
981
+ try options.chance.commit(player, .binding);
924
982
  }
925
983
  return null;
984
+ } else {
985
+ try options.chance.commit(player, .hit);
926
986
  }
927
987
 
928
988
  // On the cartridge MultiHit doesn't get set up until after damage has been applied for the
929
989
  // first time but its more convenient and efficient to set it up here (Pokémon Showdown sets
930
990
  // it up above before damage calculation).
931
991
  if (!showdown and move.effect.isMulti()) {
932
- Effects.multiHit(battle, player, move);
992
+ try Effects.multiHit(battle, player, move, options);
933
993
  hits = side.active.volatiles.attacks;
934
994
  }
935
995
 
@@ -947,11 +1007,13 @@ fn doMove(
947
1007
  try log.resisted(battle.active(player.foe()));
948
1008
  }
949
1009
  }
950
- if (!skip) nullified = try applyDamage(battle, player.foe(), player.foe(), .None, log);
1010
+ if (!skip) nullified = try applyDamage(battle, player.foe(), player.foe(), .None, options);
951
1011
  if (hit == 0 and ohko) try log.ohko();
952
1012
  hit += 1;
953
1013
  if (foe.stored().hp == 0) break;
954
- if (!late and (!sub or move.effect == .Explode)) try buildRage(battle, player.foe(), log);
1014
+ if (!late and (!sub or move.effect == .Explode)) {
1015
+ try buildRage(battle, player.foe(), options);
1016
+ }
955
1017
  // If the substitute breaks during a multi-hit attack, the attack ends
956
1018
  if (nullified) break;
957
1019
  }
@@ -961,17 +1023,23 @@ fn doMove(
961
1023
  assert(nullified or foe.stored().hp == 0 or side.active.volatiles.attacks - hit == 0);
962
1024
  side.active.volatiles.attacks = 0;
963
1025
  if (showdown and move.effect == .Twineedle and !nullified and foe.stored().hp > 0) {
964
- try Effects.poison(battle, player, Move.get(.PoisonSting), log);
1026
+ try Effects.poison(battle, player, Move.get(.PoisonSting), options);
965
1027
  }
966
1028
  try log.hitcount(battle.active(player.foe()), hit);
967
1029
  } else if (showdown) {
968
1030
  // This should be handled much earlier but Pokémon Showdown does it here... ¯\_(ツ)_/¯
969
1031
  if (move.effect == .Binding) {
970
- Effects.binding(battle, player, rewrap);
1032
+ try Effects.binding(battle, player, rewrap, options);
971
1033
  if (immune) {
972
1034
  battle.last_damage = 0;
1035
+ assert(foe.stored().hp > 0);
973
1036
  // Pokémon Showdown logs |-damage| here instead of |-immune| because logic...
974
- try log.damage(battle.active(player.foe()), foe.stored(), .None);
1037
+ if (sub) {
1038
+ try log.activate(battle.active(player.foe()), .Substitute);
1039
+ } else {
1040
+ try log.damage(battle.active(player.foe()), foe.stored(), .None);
1041
+ try buildRage(battle, player.foe(), options);
1042
+ }
975
1043
  return null;
976
1044
  }
977
1045
  }
@@ -984,12 +1052,12 @@ fn doMove(
984
1052
  // On the cartridge, "always happen" effect handlers are called in the applyDamage loop above,
985
1053
  // but this is only done to setup the MultiHit looping in the first place. Moving the MultiHit
986
1054
  // setup before the loop means we can avoid having to waste time doing no-op handler searches.
987
- if (move.effect.alwaysHappens()) try alwaysHappens(battle, player, move, residual, log);
1055
+ if (move.effect.alwaysHappens()) try alwaysHappens(battle, player, move, residual, options);
988
1056
 
989
1057
  if (foe.stored().hp == 0) return null;
990
1058
 
991
1059
  // Pokémon Showdown builds Rage at the wrong time for non-MultiHit move
992
- if (late and !sub and move.effect != .Disable) try buildRage(battle, player.foe(), log);
1060
+ if (late and !sub and move.effect != .Disable) try buildRage(battle, player.foe(), options);
993
1061
 
994
1062
  if (!move.effect.isSpecial()) {
995
1063
  // On the cartridge Rage is not considered to be "special" and thus gets executed for a
@@ -1000,29 +1068,29 @@ fn doMove(
1000
1068
  // Twineedle secondary effect in the MultiHit cleanup block above because it incorrectly
1001
1069
  // puts the |-status| message before |-hitcount| instead of after.
1002
1070
  if (move.effect == .Twineedle) {
1003
- if (!showdown) try Effects.poison(battle, player, Move.get(.PoisonSting), log);
1071
+ if (!showdown) try Effects.poison(battle, player, Move.get(.PoisonSting), options);
1004
1072
  } else if (move.effect == .Disable) {
1005
- const result = try Effects.disable(battle, player, move, log);
1006
- if (showdown) try buildRage(battle, player.foe(), log);
1073
+ const result = try Effects.disable(battle, player, move, options);
1074
+ if (showdown) try buildRage(battle, player.foe(), options);
1007
1075
  return result;
1008
1076
  } else {
1009
- try moveEffect(battle, player, move, log);
1077
+ try moveEffect(battle, player, move, options);
1010
1078
  }
1011
1079
  }
1012
1080
 
1013
1081
  return null;
1014
1082
  }
1015
1083
 
1016
- fn checkCriticalHit(battle: anytype, player: Player, move: Move.Data) bool {
1084
+ fn checkCriticalHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
1017
1085
  const side = battle.side(player);
1018
1086
 
1019
1087
  // Base speed is used for the critical hit calculation, even when Transform-ed
1020
- var chance = @as(u16, Species.chance(side.stored().species));
1021
- // GLITCH: Focus Energy reduces critical hit chance instead of increasing it
1022
- chance = if (side.active.volatiles.FocusEnergy) chance / 2 else @min(chance * 2, 255);
1023
- chance = if (move.effect == .HighCritical) @min(chance * 4, 255) else chance / 2;
1088
+ var rate = @as(u16, Species.chance(side.stored().species));
1089
+ // GLITCH: Focus Energy reduces critical hit rate instead of increasing it
1090
+ rate = if (side.active.volatiles.FocusEnergy) rate / 2 else @min(rate * 2, 255);
1091
+ rate = if (move.effect == .HighCritical) @min(rate * 4, 255) else rate / 2;
1024
1092
 
1025
- return Rolls.criticalHit(battle, chance);
1093
+ return Rolls.criticalHit(battle, player, @intCast(rate), options);
1026
1094
  }
1027
1095
 
1028
1096
  fn calcDamage(
@@ -1088,7 +1156,7 @@ fn calcDamage(
1088
1156
  d = @min(997, d);
1089
1157
  d += 2;
1090
1158
 
1091
- battle.last_damage = @intCast(u16, d);
1159
+ battle.last_damage = @intCast(d);
1092
1160
 
1093
1161
  return true;
1094
1162
  }
@@ -1102,9 +1170,9 @@ fn adjustDamage(battle: anytype, player: Player) u16 {
1102
1170
  var d = battle.last_damage;
1103
1171
  if (side.active.types.includes(move.type)) d +%= d / 2;
1104
1172
 
1105
- const neutral = @enumToInt(Effectiveness.Neutral);
1106
- const eff1: u16 = @enumToInt(move.type.effectiveness(types.type1));
1107
- const eff2: u16 = @enumToInt(move.type.effectiveness(types.type2));
1173
+ const neutral = @intFromEnum(Effectiveness.Neutral);
1174
+ const eff1: u16 = @intFromEnum(move.type.effectiveness(types.type1));
1175
+ const eff2: u16 = @intFromEnum(move.type.effectiveness(types.type2));
1108
1176
 
1109
1177
  // Type effectiveness matchup precedence only matters with (NVE, SE)
1110
1178
  if (!showdown and (eff1 + eff2) == Effectiveness.mismatch and
@@ -1124,17 +1192,20 @@ fn adjustDamage(battle: anytype, player: Player) u16 {
1124
1192
  return if (types.type1 == types.type2) eff1 * neutral else eff1 * eff2;
1125
1193
  }
1126
1194
 
1127
- fn randomizeDamage(battle: anytype) void {
1195
+ fn randomizeDamage(battle: anytype, player: Player, options: anytype) !void {
1128
1196
  if (battle.last_damage <= 1) return;
1129
- const random = Rolls.damage(battle);
1130
- battle.last_damage = @intCast(u16, @as(u32, battle.last_damage) *% random / 255);
1197
+ const random = try Rolls.damage(battle, player, options);
1198
+ options.calc.base(player.foe(), battle.last_damage);
1199
+ battle.last_damage = @intCast(@as(u32, battle.last_damage) *% random / 255);
1200
+ options.calc.final(player.foe(), battle.last_damage);
1131
1201
  }
1132
1202
 
1133
- fn specialDamage(battle: anytype, player: Player, move: Move.Data, log: anytype) !?Result {
1203
+ fn specialDamage(battle: anytype, player: Player, move: Move.Data, options: anytype) !?Result {
1134
1204
  const side = battle.side(player);
1135
1205
  const foe = battle.foe(player);
1136
1206
 
1137
- if (!try checkHit(battle, player, move, log)) return null;
1207
+ if (!try checkHit(battle, player, move, options)) return null;
1208
+ try options.chance.commit(player, .hit);
1138
1209
 
1139
1210
  battle.last_damage = switch (side.last_selected_move) {
1140
1211
  .SuperFang => @max(foe.stored().hp / 2, 1),
@@ -1143,10 +1214,10 @@ fn specialDamage(battle: anytype, player: Player, move: Move.Data, log: anytype)
1143
1214
  .DragonRage => 40,
1144
1215
  // GLITCH: if power = 0 then a desync occurs (or a miss on Pokémon Showdown)
1145
1216
  .Psywave => power: {
1146
- const max = @intCast(u8, @as(u16, side.stored().level) * 3 / 2);
1217
+ const max: u8 = @intCast(@as(u16, side.stored().level) * 3 / 2);
1147
1218
  // GLITCH: Psywave infinite glitch loop
1148
1219
  if (!showdown and max <= 1) return Result.Error;
1149
- break :power Rolls.psywave(battle, max);
1220
+ break :power try Rolls.psywave(battle, player, max, options);
1150
1221
  },
1151
1222
  else => unreachable,
1152
1223
  };
@@ -1154,59 +1225,54 @@ fn specialDamage(battle: anytype, player: Player, move: Move.Data, log: anytype)
1154
1225
  if (battle.last_damage == 0) return if (showdown) null else Result.Error;
1155
1226
 
1156
1227
  const sub = showdown and foe.active.volatiles.Substitute;
1157
- _ = try applyDamage(battle, player.foe(), player.foe(), .None, log);
1158
- if (battle.foe(player).stored().hp > 0 and !sub) try buildRage(battle, player.foe(), log);
1228
+ _ = try applyDamage(battle, player.foe(), player.foe(), .None, options);
1229
+ if (battle.foe(player).stored().hp > 0 and !sub) try buildRage(battle, player.foe(), options);
1159
1230
 
1160
1231
  return null;
1161
1232
  }
1162
1233
 
1163
- fn counterDamage(battle: anytype, player: Player, move: Move.Data, log: anytype) !?Result {
1234
+ fn counterDamage(battle: anytype, player: Player, move: Move.Data, options: anytype) !?Result {
1164
1235
  const foe = battle.foe(player);
1165
1236
 
1166
1237
  if (battle.last_damage == 0) {
1167
- try log.fail(battle.active(player), .None);
1238
+ try options.log.fail(battle.active(player), .None);
1168
1239
  return null;
1169
1240
  }
1170
1241
 
1171
- // Pretend Counter was used as a stand-in when no move has been used to fail below with 0 BP
1172
- const foe_last_used_move =
1173
- Move.get(if (foe.last_used_move == .None) .Counter else foe.last_used_move);
1242
+ // Pretend Splash was used as a stand-in when no move has been used to fail below with 0 BP
1174
1243
  const foe_last_selected_move =
1175
1244
  Move.get(if (foe.last_selected_move == .None or foe.last_selected_move == .SKIP_TURN)
1176
- .Counter
1245
+ .Splash
1177
1246
  else
1178
1247
  foe.last_selected_move);
1179
1248
 
1180
- const used = foe_last_used_move.bp > 0 and
1181
- foe.last_used_move != .Counter and
1182
- (foe_last_used_move.type == .Normal or
1183
- foe_last_used_move.type == .Fighting);
1184
-
1249
+ const used = battle.lastMove(player.foe()).counterable != 0;
1185
1250
  const selected = foe_last_selected_move.bp > 0 and
1186
1251
  foe.last_selected_move != .Counter and
1187
1252
  (foe_last_selected_move.type == .Normal or
1188
1253
  foe_last_selected_move.type == .Fighting);
1189
1254
 
1190
1255
  if (!used and !selected) {
1191
- try log.fail(battle.active(player), .None);
1256
+ try options.log.fail(battle.active(player), .None);
1192
1257
  return null;
1193
1258
  }
1194
1259
 
1195
1260
  if (!used or !selected) {
1196
1261
  // GLITCH: Counter desync (covered by Desync Clause Mod on Pokémon Showdown)
1197
1262
  if (!showdown) return Result.Error;
1198
- try log.fail(battle.active(player), .None);
1263
+ try options.log.fail(battle.active(player), .None);
1199
1264
  return null;
1200
1265
  }
1201
1266
 
1202
- battle.last_damage = if (battle.last_damage > 0x7FFF) 0xFFFF else battle.last_damage * 2;
1267
+ battle.last_damage *|= 2;
1203
1268
 
1204
1269
  // Pokémon Showdown calls checkHit before Counter
1205
- if (!showdown and !try checkHit(battle, player, move, log)) return null;
1270
+ if (!showdown and !try checkHit(battle, player, move, options)) return null;
1271
+ try options.chance.commit(player, .hit);
1206
1272
 
1207
1273
  const sub = showdown and foe.active.volatiles.Substitute;
1208
- _ = try applyDamage(battle, player.foe(), player.foe(), .None, log);
1209
- if (battle.foe(player).stored().hp > 0 and !sub) try buildRage(battle, player.foe(), log);
1274
+ _ = try applyDamage(battle, player.foe(), player.foe(), .None, options);
1275
+ if (battle.foe(player).stored().hp > 0 and !sub) try buildRage(battle, player.foe(), options);
1210
1276
  return null;
1211
1277
  }
1212
1278
 
@@ -1215,7 +1281,7 @@ fn applyDamage(
1215
1281
  target_player: Player,
1216
1282
  sub_player: Player,
1217
1283
  reason: Damage,
1218
- log: anytype,
1284
+ options: anytype,
1219
1285
  ) !bool {
1220
1286
  assert(showdown or battle.last_damage != 0);
1221
1287
 
@@ -1230,19 +1296,20 @@ fn applyDamage(
1230
1296
  subbed.active.volatiles.substitute = 0;
1231
1297
  subbed.active.volatiles.Substitute = false;
1232
1298
  // battle.last_damage is not updated with the amount of HP the Substitute had
1233
- try log.end(battle.active(sub_player), .Substitute);
1299
+ try options.log.end(battle.active(sub_player), .Substitute);
1300
+ options.calc.capped(target_player);
1234
1301
  return true;
1235
1302
  } else {
1236
1303
  // Safe to truncate since less than subbed.volatiles.substitute which is a u8
1237
- subbed.active.volatiles.substitute -= @intCast(u8, battle.last_damage);
1238
- try log.activate(battle.active(sub_player), .Substitute);
1304
+ subbed.active.volatiles.substitute -= @intCast(battle.last_damage);
1305
+ try options.log.activate(battle.active(sub_player), .Substitute);
1239
1306
  return false;
1240
1307
  }
1241
1308
  }
1242
1309
 
1243
1310
  if (battle.last_damage > target.stored().hp) battle.last_damage = target.stored().hp;
1244
1311
  target.stored().hp -= battle.last_damage;
1245
- try log.damage(battle.active(target_player), target.stored(), reason);
1312
+ try options.log.damage(battle.active(target_player), target.stored(), reason);
1246
1313
  return false;
1247
1314
  }
1248
1315
 
@@ -1253,22 +1320,24 @@ fn mirrorMove(
1253
1320
  rewrap: bool,
1254
1321
  auto: bool,
1255
1322
  residual: *bool,
1256
- log: anytype,
1257
- ) !?Result {
1323
+ options: anytype,
1324
+ ) anyerror!?Result {
1258
1325
  var side = battle.side(player);
1259
1326
  const foe = battle.foe(player);
1260
1327
 
1261
1328
  side.last_selected_move = foe.last_used_move;
1262
1329
 
1263
1330
  if (foe.last_used_move == .None or foe.last_used_move == .MirrorMove) {
1264
- try log.fail(battle.active(player), .None);
1331
+ try options.log.fail(battle.active(player), .None);
1265
1332
  return null;
1266
1333
  } else if (!showdown or foe.last_used_move != .Struggle) {
1267
1334
  incrementPP(side, mslot);
1268
1335
  }
1269
1336
 
1270
- if (!try canMove(battle, player, mslot, auto, false, .MirrorMove, residual, log)) return null;
1271
- return doMove(battle, player, mslot, rewrap, auto, residual, log);
1337
+ if (!try canMove(battle, player, mslot, auto, false, .MirrorMove, residual, options)) {
1338
+ return null;
1339
+ }
1340
+ return doMove(battle, player, mslot, rewrap, auto, residual, options);
1272
1341
  }
1273
1342
 
1274
1343
  fn metronome(
@@ -1278,35 +1347,48 @@ fn metronome(
1278
1347
  rewrap: bool,
1279
1348
  auto: bool,
1280
1349
  residual: *bool,
1281
- log: anytype,
1282
- ) !?Result {
1350
+ options: anytype,
1351
+ ) anyerror!?Result {
1283
1352
  var side = battle.side(player);
1284
1353
 
1285
- side.last_selected_move = Rolls.metronome(battle);
1354
+ side.last_selected_move = try Rolls.metronome(battle, player, options);
1286
1355
  incrementPP(side, mslot);
1287
1356
 
1288
- if (!try canMove(battle, player, mslot, auto, false, .Metronome, residual, log)) return null;
1289
- return doMove(battle, player, mslot, rewrap, auto, residual, log);
1357
+ if (!try canMove(battle, player, mslot, auto, false, .Metronome, residual, options)) {
1358
+ return null;
1359
+ }
1360
+ return doMove(battle, player, mslot, rewrap, auto, residual, options);
1290
1361
  }
1291
1362
 
1292
- fn checkHit(battle: anytype, player: Player, move: Move.Data, log: anytype) !bool {
1363
+ fn checkHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
1293
1364
  var immune = false;
1294
1365
  var mist = false;
1295
- if (moveHit(battle, player, move, &immune, &mist)) return true;
1366
+
1367
+ if (moveHit(battle, player, move, &immune, &mist, options)) return true;
1368
+
1296
1369
  assert(!immune);
1297
1370
  if (mist) {
1298
1371
  assert(!showdown);
1299
1372
  const foe_ident = battle.active(player.foe());
1300
- try log.activate(foe_ident, .Mist);
1301
- try log.fail(foe_ident, .None);
1373
+ try options.log.activate(foe_ident, .Mist);
1374
+ try options.log.fail(foe_ident, .None);
1302
1375
  } else {
1303
- try log.lastmiss();
1304
- try log.miss(battle.active(player));
1376
+ try options.chance.commit(player, .miss);
1377
+ try options.log.lastmiss();
1378
+ try options.log.miss(battle.active(player));
1305
1379
  }
1380
+
1306
1381
  return false;
1307
1382
  }
1308
1383
 
1309
- fn moveHit(battle: anytype, player: Player, move: Move.Data, immune: *bool, mist: *bool) bool {
1384
+ fn moveHit(
1385
+ battle: anytype,
1386
+ player: Player,
1387
+ move: Move.Data,
1388
+ immune: *bool,
1389
+ mist: *bool,
1390
+ options: anytype,
1391
+ ) bool {
1310
1392
  var side = battle.side(player);
1311
1393
  const foe = battle.foe(player);
1312
1394
 
@@ -1345,10 +1427,10 @@ fn moveHit(battle: anytype, player: Player, move: Move.Data, immune: *bool, mist
1345
1427
  const overwritten = overwrite and state > 0;
1346
1428
  assert(!overwritten or (0 < state and state <= 255 and !side.active.volatiles.Bide));
1347
1429
 
1348
- var accuracy = if (overwritten) state else @as(u16, Gen12.percent(move.accuracy));
1349
- var boost = BOOSTS[@intCast(u4, @as(i8, side.active.boosts.accuracy) + 6)];
1430
+ var accuracy = if (overwritten) state else @as(u16, move.accuracy);
1431
+ var boost = BOOSTS[@as(u4, @intCast(@as(i8, side.active.boosts.accuracy) + 6))];
1350
1432
  accuracy = accuracy * boost[0] / boost[1];
1351
- boost = BOOSTS[@intCast(u4, @as(i8, -foe.active.boosts.evasion) + 6)];
1433
+ boost = BOOSTS[@as(u4, @intCast(@as(i8, -foe.active.boosts.evasion) + 6))];
1352
1434
  accuracy = accuracy * boost[0] / boost[1];
1353
1435
  accuracy = @min(255, @max(1, accuracy));
1354
1436
 
@@ -1357,7 +1439,7 @@ fn moveHit(battle: anytype, player: Player, move: Move.Data, immune: *bool, mist
1357
1439
  if (overwrite) side.active.volatiles.state = accuracy;
1358
1440
 
1359
1441
  // GLITCH: max accuracy is 255 so 1/256 chance of miss
1360
- break :miss !Rolls.hit(battle, accuracy);
1442
+ break :miss !Rolls.hit(battle, player, @intCast(accuracy), options);
1361
1443
  };
1362
1444
 
1363
1445
  // Pokémon Showdown reports miss instead of fail for moves blocked by Mist that 1/256 miss
@@ -1367,7 +1449,7 @@ fn moveHit(battle: anytype, player: Player, move: Move.Data, immune: *bool, mist
1367
1449
  }
1368
1450
 
1369
1451
  if (!miss) return true;
1370
- battle.last_damage = 0;
1452
+ if (!showdown or !foe.active.volatiles.Invulnerable) battle.last_damage = 0;
1371
1453
  side.active.volatiles.Binding = false;
1372
1454
  return false;
1373
1455
  }
@@ -1375,8 +1457,8 @@ fn moveHit(battle: anytype, player: Player, move: Move.Data, immune: *bool, mist
1375
1457
  fn checkFaint(
1376
1458
  battle: anytype,
1377
1459
  player: Player,
1378
- log: anytype,
1379
- ) @TypeOf(log).Error!?Result {
1460
+ options: anytype,
1461
+ ) @TypeOf(options.log).Error!?Result {
1380
1462
  const side = battle.side(player);
1381
1463
  if (side.stored().hp > 0) return null;
1382
1464
 
@@ -1388,29 +1470,29 @@ fn checkFaint(
1388
1470
  const tie = player_out and foe_out;
1389
1471
  const more = tie or player_out or foe_out;
1390
1472
 
1391
- if (try faint(battle, player, log, !(more or foe_fainted))) |r| return r;
1392
- if (foe_fainted) if (try faint(battle, player.foe(), log, !more)) |r| return r;
1473
+ if (try faint(battle, player, !(more or foe_fainted), options)) |r| return r;
1474
+ if (foe_fainted) if (try faint(battle, player.foe(), !more, options)) |r| return r;
1393
1475
 
1394
1476
  assert(!side.active.volatiles.MultiHit);
1395
1477
  assert(!foe.active.volatiles.MultiHit);
1396
1478
 
1397
1479
  if (tie) {
1398
- try log.tie();
1480
+ try options.log.tie();
1399
1481
  return Result.Tie;
1400
1482
  } else if (player_out) {
1401
- try log.win(player.foe());
1483
+ try options.log.win(player.foe());
1402
1484
  return if (player == .P1) Result.Lose else Result.Win;
1403
1485
  } else if (foe_out) {
1404
- try log.win(player);
1486
+ try options.log.win(player);
1405
1487
  return if (player == .P1) Result.Win else Result.Lose;
1406
1488
  }
1407
1489
 
1408
1490
  const foe_choice: Choice.Type = if (foe_fainted) .Switch else .Pass;
1409
- if (player == .P1) return Result{ .p1 = .Switch, .p2 = foe_choice };
1410
- return Result{ .p1 = foe_choice, .p2 = .Switch };
1491
+ if (player == .P1) return .{ .p1 = .Switch, .p2 = foe_choice };
1492
+ return .{ .p1 = foe_choice, .p2 = .Switch };
1411
1493
  }
1412
1494
 
1413
- fn faint(battle: anytype, player: Player, log: anytype, done: bool) !?Result {
1495
+ fn faint(battle: anytype, player: Player, done: bool, options: anytype) !?Result {
1414
1496
  var side = battle.side(player);
1415
1497
  var foe = battle.foe(player);
1416
1498
  assert(side.stored().hp == 0);
@@ -1443,32 +1525,31 @@ fn faint(battle: anytype, player: Player, log: anytype, done: bool) !?Result {
1443
1525
  side.stored().stats.spe;
1444
1526
  }
1445
1527
 
1446
- try log.faint(battle.active(player), done);
1528
+ try options.log.faint(battle.active(player), done);
1529
+ options.calc.capped(player);
1447
1530
  return null;
1448
1531
  }
1449
1532
 
1450
- fn handleResidual(battle: anytype, player: Player, log: anytype) !void {
1533
+ fn handleResidual(battle: anytype, player: Player, options: anytype) !void {
1451
1534
  var side = battle.side(player);
1452
1535
  var stored = side.stored();
1453
1536
  const ident = battle.active(player);
1454
1537
  var volatiles = &side.active.volatiles;
1455
1538
 
1456
1539
  const brn = Status.is(stored.status, .BRN);
1457
- if (brn or Status.is(stored.status, .PSN)) blk: {
1540
+ if (brn or Status.is(stored.status, .PSN)) {
1458
1541
  var damage = @max(stored.stats.hp / 16, 1);
1459
-
1460
1542
  if (volatiles.Toxic) {
1461
1543
  volatiles.toxic += 1;
1462
1544
  damage *= volatiles.toxic;
1463
1545
  }
1546
+ damage = @min(damage, stored.hp);
1464
1547
 
1465
- const amount = @min(damage, stored.hp);
1466
- if (showdown and amount == 0) break :blk;
1467
-
1468
- stored.hp -= amount;
1469
-
1470
- // Pokémon Showdown uses damageOf here but its not relevant in Generation I
1471
- try log.damage(ident, stored, if (brn) Damage.Burn else Damage.Poison);
1548
+ if (!showdown or damage > 0) {
1549
+ stored.hp -= damage;
1550
+ // Pokémon Showdown uses damageOf here but its not relevant in Generation I
1551
+ try options.log.damage(ident, stored, if (brn) Damage.Burn else Damage.Poison);
1552
+ }
1472
1553
  }
1473
1554
 
1474
1555
  if (volatiles.LeechSeed) {
@@ -1493,42 +1574,42 @@ fn handleResidual(battle: anytype, player: Player, log: anytype) !void {
1493
1574
  stored.hp -= amount;
1494
1575
 
1495
1576
  // As above, Pokémon Showdown uses damageOf but its not relevant
1496
- if (amount > 0) try log.damage(ident, stored, .LeechSeed);
1577
+ if (amount > 0) try options.log.damage(ident, stored, .LeechSeed);
1497
1578
 
1498
1579
  const before = foe_stored.hp;
1499
1580
  // Uncapped damage is added back to the foe
1500
1581
  foe_stored.hp = @min(foe_stored.hp + damage, foe_stored.stats.hp);
1501
1582
  // Pokémon Showdown uses the less specific heal here instead of drain... because reasons?
1502
- if (foe_stored.hp > before) try log.heal(foe_ident, foe_stored, .Silent);
1583
+ if (foe_stored.hp > before) try options.log.heal(foe_ident, foe_stored, .Silent);
1503
1584
  }
1504
1585
  }
1505
1586
 
1506
- fn endTurn(battle: anytype, log: anytype) @TypeOf(log).Error!Result {
1587
+ fn endTurn(battle: anytype, options: anytype) @TypeOf(options.log).Error!Result {
1507
1588
  assert(!battle.side(.P1).active.volatiles.MultiHit);
1508
1589
  assert(!battle.side(.P2).active.volatiles.MultiHit);
1509
1590
 
1510
- if (showdown and options.ebc and checkEBC(battle)) {
1511
- try log.tie();
1591
+ battle.turn += 1;
1592
+
1593
+ if (showdown and pkmn.options.ebc and checkEBC(battle)) {
1594
+ try options.log.tie();
1512
1595
  return Result.Tie;
1513
1596
  }
1514
1597
 
1515
- battle.turn += 1;
1516
-
1517
1598
  if (showdown and battle.turn >= 1000) {
1518
- try log.tie();
1599
+ try options.log.tie();
1519
1600
  return Result.Tie;
1520
1601
  } else if (battle.turn >= 65535) {
1521
1602
  return Result.Error;
1522
1603
  }
1523
1604
 
1524
- try log.turn(battle.turn);
1605
+ try options.log.turn(battle.turn);
1525
1606
 
1526
1607
  return Result.Default;
1527
1608
  }
1528
1609
 
1529
1610
  fn checkEBC(battle: anytype) bool {
1530
1611
  for (battle.sides, 0..) |side, i| {
1531
- const foe = battle.sides[~@intCast(u1, i)];
1612
+ const foe = battle.sides[~@as(u1, @intCast(i))];
1532
1613
 
1533
1614
  var foe_all_ghosts = true;
1534
1615
  var foe_all_transform = true;
@@ -1580,7 +1661,7 @@ fn checkEBC(battle: anytype) bool {
1580
1661
  return false;
1581
1662
  }
1582
1663
 
1583
- fn disabled(side: *Side, ident: ID, log: anytype) !bool {
1664
+ fn disabled(side: *Side, ident: ID, options: anytype) !bool {
1584
1665
  if (side.active.volatiles.disabled_move != 0) {
1585
1666
  // A Pokémon that transforms after being disabled may end up with less move slots
1586
1667
  const m = side.active.moves[side.active.volatiles.disabled_move - 1].id;
@@ -1591,28 +1672,28 @@ fn disabled(side: *Side, ident: ID, log: anytype) !bool {
1591
1672
  side.last_selected_move;
1592
1673
  if (m != .None and m == last) {
1593
1674
  side.active.volatiles.Charging = false;
1594
- try log.disabled(ident, last);
1675
+ try options.log.disabled(ident, last);
1595
1676
  return true;
1596
1677
  }
1597
1678
  }
1598
1679
  return false;
1599
1680
  }
1600
1681
 
1601
- inline fn buildRage(battle: anytype, who: Player, log: anytype) !void {
1682
+ inline fn buildRage(battle: anytype, who: Player, options: anytype) !void {
1602
1683
  const side = battle.side(who);
1603
1684
  if (side.active.volatiles.Rage and side.active.boosts.atk < 6) {
1604
- try Effects.boost(battle, who, Move.get(.Rage), log);
1685
+ try Effects.boost(battle, who, Move.get(.Rage), options);
1605
1686
  }
1606
1687
  }
1607
1688
 
1608
- fn handleThrashing(battle: anytype, active: *ActivePokemon) bool {
1689
+ fn handleThrashing(battle: anytype, active: *ActivePokemon, player: Player, options: anytype) bool {
1609
1690
  var volatiles = &active.volatiles;
1610
1691
  assert(volatiles.Thrashing);
1611
1692
  if (volatiles.attacks > 0) return false;
1612
1693
 
1613
1694
  volatiles.Thrashing = false;
1614
1695
  volatiles.Confusion = true;
1615
- volatiles.confusion = Rolls.confusionDuration(battle);
1696
+ volatiles.confusion = Rolls.confusionDuration(battle, player, true, options);
1616
1697
  return true;
1617
1698
  }
1618
1699
 
@@ -1622,42 +1703,42 @@ inline fn onBegin(
1622
1703
  move: Move.Data,
1623
1704
  mslot: u8,
1624
1705
  residual: *bool,
1625
- log: anytype,
1706
+ options: anytype,
1626
1707
  ) !void {
1627
1708
  assert(move.effect.onBegin());
1628
1709
  return switch (move.effect) {
1629
- .Confusion => Effects.confusion(battle, player, move, log),
1630
- .Conversion => Effects.conversion(battle, player, log),
1631
- .FocusEnergy => Effects.focusEnergy(battle, player, log),
1632
- .Haze => Effects.haze(battle, player, log),
1633
- .Heal => Effects.heal(battle, player, log),
1634
- .LeechSeed => Effects.leechSeed(battle, player, move, log),
1635
- .LightScreen => Effects.lightScreen(battle, player, log),
1636
- .Mimic => Effects.mimic(battle, player, move, mslot, log),
1637
- .Mist => Effects.mist(battle, player, log),
1638
- .Paralyze => Effects.paralyze(battle, player, move, log),
1639
- .Poison => Effects.poison(battle, player, move, log),
1640
- .Reflect => Effects.reflect(battle, player, log),
1641
- .Splash => Effects.splash(battle, player, log),
1642
- .Substitute => Effects.substitute(battle, player, residual, log),
1643
- .SwitchAndTeleport => Effects.switchAndTeleport(battle, player, move, log),
1644
- .Transform => Effects.transform(battle, player, log),
1710
+ .Confusion => Effects.confusion(battle, player, move, options),
1711
+ .Conversion => Effects.conversion(battle, player, options),
1712
+ .FocusEnergy => Effects.focusEnergy(battle, player, options),
1713
+ .Haze => Effects.haze(battle, player, options),
1714
+ .Heal => Effects.heal(battle, player, options),
1715
+ .LeechSeed => Effects.leechSeed(battle, player, move, options),
1716
+ .LightScreen => Effects.lightScreen(battle, player, options),
1717
+ .Mimic => Effects.mimic(battle, player, move, mslot, options),
1718
+ .Mist => Effects.mist(battle, player, options),
1719
+ .Paralyze => Effects.paralyze(battle, player, move, options),
1720
+ .Poison => Effects.poison(battle, player, move, options),
1721
+ .Reflect => Effects.reflect(battle, player, options),
1722
+ .Splash => Effects.splash(battle, player, options),
1723
+ .Substitute => Effects.substitute(battle, player, residual, options),
1724
+ .SwitchAndTeleport => Effects.switchAndTeleport(battle, player, move, options),
1725
+ .Transform => Effects.transform(battle, player, options),
1645
1726
  else => unreachable,
1646
1727
  };
1647
1728
  }
1648
1729
 
1649
- inline fn onEnd(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
1730
+ inline fn onEnd(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1650
1731
  assert(move.effect.onEnd());
1651
1732
  return switch (move.effect) {
1652
- .Bide => Effects.bide(battle, player, log),
1733
+ .Bide => Effects.bide(battle, player, options),
1653
1734
  // zig fmt: off
1654
1735
  .AttackUp1, .AttackUp2, .DefenseUp1, .DefenseUp2,
1655
1736
  .EvasionUp1, .SpecialUp1, .SpecialUp2, .SpeedUp2 =>
1656
- Effects.boost(battle, player, move, log),
1737
+ Effects.boost(battle, player, move, options),
1657
1738
  .AccuracyDown1, .AttackDown1, .DefenseDown1, .DefenseDown2, .SpeedDown1 =>
1658
- Effects.unboost(battle, player, move, log),
1739
+ Effects.unboost(battle, player, move, options),
1659
1740
  // zig fmt: on
1660
- .Sleep => Effects.sleep(battle, player, move, log),
1741
+ .Sleep => Effects.sleep(battle, player, move, options),
1661
1742
  else => unreachable,
1662
1743
  };
1663
1744
  }
@@ -1667,51 +1748,51 @@ inline fn alwaysHappens(
1667
1748
  player: Player,
1668
1749
  move: Move.Data,
1669
1750
  residual: *bool,
1670
- log: anytype,
1751
+ options: anytype,
1671
1752
  ) !void {
1672
1753
  assert(move.effect.alwaysHappens());
1673
1754
  return switch (move.effect) {
1674
- .DrainHP, .DreamEater => Effects.drainHP(battle, player, log),
1755
+ .DrainHP, .DreamEater => Effects.drainHP(battle, player, options),
1675
1756
  .Explode => Effects.explode(battle, player),
1676
- .PayDay => Effects.payDay(battle, player, log),
1757
+ .PayDay => Effects.payDay(battle, player, options),
1677
1758
  .Rage => Effects.rage(battle, player),
1678
- .Recoil => Effects.recoil(battle, player, residual, log),
1759
+ .Recoil => Effects.recoil(battle, player, residual, options),
1679
1760
  .JumpKick, .Binding => {},
1680
1761
  else => unreachable,
1681
1762
  };
1682
1763
  }
1683
1764
 
1684
- inline fn moveEffect(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
1765
+ inline fn moveEffect(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1685
1766
  return switch (move.effect) {
1686
- .BurnChance1, .BurnChance2 => Effects.burnChance(battle, player, move, log),
1687
- .ConfusionChance => Effects.confusion(battle, player, move, log),
1688
- .FlinchChance1, .FlinchChance2 => Effects.flinchChance(battle, player, move),
1689
- .FreezeChance => Effects.freezeChance(battle, player, move, log),
1690
- .HyperBeam => Effects.hyperBeam(battle, player, log),
1767
+ .BurnChance1, .BurnChance2 => Effects.burnChance(battle, player, move, options),
1768
+ .ConfusionChance => Effects.confusion(battle, player, move, options),
1769
+ .FlinchChance1, .FlinchChance2 => Effects.flinchChance(battle, player, move, options),
1770
+ .FreezeChance => Effects.freezeChance(battle, player, move, options),
1771
+ .HyperBeam => Effects.hyperBeam(battle, player, options),
1691
1772
  .MultiHit, .DoubleHit, .Twineedle => unreachable,
1692
- .ParalyzeChance1, .ParalyzeChance2 => Effects.paralyzeChance(battle, player, move, log),
1693
- .PoisonChance1, .PoisonChance2 => Effects.poison(battle, player, move, log),
1773
+ .ParalyzeChance1, .ParalyzeChance2 => Effects.paralyzeChance(battle, player, move, options),
1774
+ .PoisonChance1, .PoisonChance2 => Effects.poison(battle, player, move, options),
1694
1775
  // zig fmt: off
1695
1776
  .AttackDownChance, .DefenseDownChance, .SpecialDownChance, .SpeedDownChance =>
1696
- Effects.unboost(battle, player, move, log),
1777
+ Effects.unboost(battle, player, move, options),
1697
1778
  // zig fmt: on
1698
1779
  else => {},
1699
1780
  };
1700
1781
  }
1701
1782
 
1702
1783
  pub const Effects = struct {
1703
- fn bide(battle: anytype, player: Player, log: anytype) !void {
1784
+ fn bide(battle: anytype, player: Player, options: anytype) !void {
1704
1785
  var side = battle.side(player);
1705
1786
 
1706
1787
  side.active.volatiles.Bide = true;
1707
1788
  assert(!side.active.volatiles.Thrashing and !side.active.volatiles.Rage);
1708
1789
  side.active.volatiles.state = 0;
1709
- side.active.volatiles.attacks = Rolls.bideThrashDuration(battle);
1790
+ side.active.volatiles.attacks = Rolls.attackingDuration(battle, player, options);
1710
1791
 
1711
- try log.start(battle.active(player), .Bide);
1792
+ try options.log.start(battle.active(player), .Bide);
1712
1793
  }
1713
1794
 
1714
- fn burnChance(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
1795
+ fn burnChance(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1715
1796
  var foe = battle.foe(player);
1716
1797
  var foe_stored = foe.stored();
1717
1798
 
@@ -1722,83 +1803,97 @@ pub const Effects = struct {
1722
1803
  // GLITCH: Freeze top move selection desync can occur if thawed Pokémon is slower
1723
1804
  if (Status.is(foe_stored.status, .FRZ)) {
1724
1805
  assert(move.type == .Fire);
1725
- try log.curestatus(battle.active(player.foe()), foe_stored.status, .Message);
1806
+ try options.log.curestatus(
1807
+ battle.active(player.foe()),
1808
+ foe_stored.status,
1809
+ .Message,
1810
+ );
1726
1811
  foe_stored.status = 0;
1727
1812
  }
1728
1813
  return;
1729
1814
  }
1730
1815
 
1731
1816
  if (foe.active.types.includes(move.type)) return;
1732
- if (!Rolls.secondaryChance(battle, move.effect == .BurnChance1)) return;
1817
+ if (!try Rolls.secondaryChance(battle, player, move.effect == .BurnChance1, options)) {
1818
+ return;
1819
+ }
1733
1820
 
1734
1821
  foe_stored.status = Status.init(.BRN);
1735
1822
  foe.active.stats.atk = @max(foe.active.stats.atk / 2, 1);
1736
1823
 
1737
- try log.status(battle.active(player.foe()), foe_stored.status, .None);
1824
+ try options.log.status(battle.active(player.foe()), foe_stored.status, .None);
1738
1825
  }
1739
1826
 
1740
- fn charge(battle: anytype, player: Player, log: anytype) !void {
1827
+ fn charge(battle: anytype, player: Player, options: anytype) !void {
1741
1828
  var side = battle.side(player);
1742
1829
  var volatiles = &side.active.volatiles;
1743
1830
 
1744
1831
  volatiles.Charging = true;
1745
1832
  const move = side.last_selected_move;
1746
1833
  if (move == .Fly or move == .Dig) volatiles.Invulnerable = true;
1747
- try log.laststill();
1748
- try log.prepare(battle.active(player), move);
1834
+ try options.log.laststill();
1835
+ try options.log.prepare(battle.active(player), move);
1749
1836
  }
1750
1837
 
1751
- fn confusion(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
1838
+ fn confusion(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1752
1839
  var foe = battle.foe(player);
1753
1840
  const sub = foe.active.volatiles.Substitute;
1754
1841
 
1755
1842
  if (move.effect == .ConfusionChance) {
1756
- if (!Rolls.confusionChance(battle)) return;
1843
+ if (!try Rolls.confusionChance(battle, player, options)) return;
1757
1844
  } else {
1758
1845
  if (showdown) {
1759
- if (!try checkHit(battle, player, move, log)) {
1846
+ if (!try checkHit(battle, player, move, options)) {
1760
1847
  return;
1761
1848
  } else if (sub) {
1762
- return log.fail(battle.active(player.foe()), .None);
1849
+ return options.log.fail(battle.active(player.foe()), .None);
1763
1850
  }
1764
1851
  } else {
1765
1852
  if (sub) {
1766
- return log.fail(battle.active(player.foe()), .None);
1767
- } else if (!try checkHit(battle, player, move, log)) {
1853
+ return options.log.fail(battle.active(player.foe()), .None);
1854
+ } else if (!try checkHit(battle, player, move, options)) {
1768
1855
  return;
1769
1856
  }
1770
1857
  }
1858
+ try options.chance.commit(player, .hit);
1771
1859
  }
1772
1860
 
1773
1861
  if (foe.active.volatiles.Confusion) return;
1774
1862
  foe.active.volatiles.Confusion = true;
1775
- foe.active.volatiles.confusion = Rolls.confusionDuration(battle);
1863
+ foe.active.volatiles.confusion = Rolls.confusionDuration(battle, player, false, options);
1776
1864
 
1777
- try log.start(battle.active(player.foe()), .Confusion);
1865
+ try options.log.start(battle.active(player.foe()), .Confusion);
1778
1866
  }
1779
1867
 
1780
- fn conversion(battle: anytype, player: Player, log: anytype) !void {
1868
+ fn conversion(battle: anytype, player: Player, options: anytype) !void {
1781
1869
  const foe = battle.foe(player);
1782
1870
 
1783
1871
  if (foe.active.volatiles.Invulnerable) {
1784
- try log.lastmiss();
1785
- return log.miss(battle.active(player));
1872
+ try options.log.lastmiss();
1873
+ return options.log.miss(battle.active(player));
1786
1874
  }
1787
1875
 
1788
1876
  battle.side(player).active.types = foe.active.types;
1789
- return log.typechange(battle.active(player), foe.active.types, battle.active(player.foe()));
1877
+ return options.log.typechange(
1878
+ battle.active(player),
1879
+ foe.active.types,
1880
+ battle.active(player.foe()),
1881
+ );
1790
1882
  }
1791
1883
 
1792
- fn disable(battle: anytype, player: Player, move: Move.Data, log: anytype) !?Result {
1884
+ fn disable(battle: anytype, player: Player, move: Move.Data, options: anytype) !?Result {
1793
1885
  var foe = battle.foe(player);
1794
1886
  var volatiles = &foe.active.volatiles;
1795
1887
  const foe_ident = battle.active(player.foe());
1796
1888
 
1797
1889
  // Pokémon Showdown handles hit/miss earlier in doMove
1798
- if (!showdown and !try checkHit(battle, player, move, log)) return null;
1890
+ if (!showdown) {
1891
+ if (!try checkHit(battle, player, move, options)) return null;
1892
+ try options.chance.commit(player, .hit);
1893
+ }
1799
1894
 
1800
1895
  if (volatiles.disabled_move != 0) {
1801
- try log.fail(foe_ident, .None);
1896
+ try options.log.fail(foe_ident, .None);
1802
1897
  return null;
1803
1898
  }
1804
1899
 
@@ -1814,7 +1909,7 @@ pub const Effects = struct {
1814
1909
  // Technically this is still considered simply a "miss" on the cartridge,
1815
1910
  // but diverging from Pokémon Showdown here would mostly just be pedantic
1816
1911
  if (n == 0) {
1817
- try log.fail(foe_ident, .None);
1912
+ try options.log.fail(foe_ident, .None);
1818
1913
  return null;
1819
1914
  } else if (err) {
1820
1915
  // GLITCH: Transform + Mirror Move / Metronome PP softlock
@@ -1822,14 +1917,16 @@ pub const Effects = struct {
1822
1917
  return Result.Error;
1823
1918
  }
1824
1919
 
1825
- volatiles.disabled_move = @intCast(u3, Rolls.moveSlot(battle, &foe.active.moves, n));
1826
- volatiles.disabled_duration = Rolls.disableDuration(battle);
1920
+ volatiles.disabled_move =
1921
+ @intCast(try Rolls.moveSlot(battle, player, &foe.active.moves, n, options));
1922
+ volatiles.disabled_duration = Rolls.disableDuration(battle, player, options);
1827
1923
 
1828
- try log.startEffect(foe_ident, .Disable, foe.active.move(volatiles.disabled_move).id);
1924
+ const id = foe.active.move(volatiles.disabled_move).id;
1925
+ try options.log.startEffect(foe_ident, .Disable, id);
1829
1926
  return null;
1830
1927
  }
1831
1928
 
1832
- fn drainHP(battle: anytype, player: Player, log: anytype) !void {
1929
+ fn drainHP(battle: anytype, player: Player, options: anytype) !void {
1833
1930
  var side = battle.side(player);
1834
1931
  var stored = side.stored();
1835
1932
 
@@ -1844,7 +1941,7 @@ pub const Effects = struct {
1844
1941
  if (stored.hp == stored.stats.hp) return;
1845
1942
  stored.hp = @min(stored.stats.hp, stored.hp + drain);
1846
1943
 
1847
- try log.drain(battle.active(player), stored, battle.active(player.foe()));
1944
+ try options.log.drain(battle.active(player), stored, battle.active(player.foe()));
1848
1945
  }
1849
1946
 
1850
1947
  fn explode(battle: anytype, player: Player) !void {
@@ -1858,26 +1955,28 @@ pub const Effects = struct {
1858
1955
  side.active.volatiles.LeechSeed = false;
1859
1956
  }
1860
1957
 
1861
- fn flinchChance(battle: anytype, player: Player, move: Move.Data) void {
1958
+ fn flinchChance(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1862
1959
  var volatiles = &battle.foe(player).active.volatiles;
1863
1960
 
1864
1961
  if (volatiles.Substitute) return;
1865
- if (!Rolls.secondaryChance(battle, move.effect == .FlinchChance1)) return;
1962
+ if (!try Rolls.secondaryChance(battle, player, move.effect == .FlinchChance1, options)) {
1963
+ return;
1964
+ }
1866
1965
 
1867
1966
  volatiles.Flinch = true;
1868
1967
  volatiles.Recharging = false;
1869
1968
  }
1870
1969
 
1871
- fn focusEnergy(battle: anytype, player: Player, log: anytype) !void {
1970
+ fn focusEnergy(battle: anytype, player: Player, options: anytype) !void {
1872
1971
  var side = battle.side(player);
1873
1972
 
1874
1973
  if (side.active.volatiles.FocusEnergy) return;
1875
1974
  side.active.volatiles.FocusEnergy = true;
1876
1975
 
1877
- try log.start(battle.active(player), .FocusEnergy);
1976
+ try options.log.start(battle.active(player), .FocusEnergy);
1878
1977
  }
1879
1978
 
1880
- fn freezeChance(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
1979
+ fn freezeChance(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1881
1980
  var foe = battle.foe(player);
1882
1981
  var foe_stored = foe.stored();
1883
1982
  const foe_ident = battle.active(player.foe());
@@ -1889,17 +1988,18 @@ pub const Effects = struct {
1889
1988
  }
1890
1989
 
1891
1990
  if (foe.active.types.includes(move.type)) return;
1892
- if (!Rolls.secondaryChance(battle, true)) return;
1991
+ if (!try Rolls.secondaryChance(battle, player, true, options)) return;
1893
1992
  // Freeze Clause Mod
1894
1993
  if (showdown) for (foe.pokemon) |p| if (Status.is(p.status, .FRZ)) return;
1895
1994
 
1896
1995
  foe_stored.status = Status.init(.FRZ);
1897
1996
  // GLITCH: Hyper Beam recharging status is not cleared
1898
1997
 
1899
- try log.status(foe_ident, foe_stored.status, .None);
1998
+ try options.log.status(foe_ident, foe_stored.status, .None);
1900
1999
  }
1901
2000
 
1902
- fn haze(battle: anytype, player: Player, log: anytype) !void {
2001
+ fn haze(battle: anytype, player: Player, options: anytype) !void {
2002
+ var log = options.log;
1903
2003
  var side = battle.side(player);
1904
2004
  var foe = battle.foe(player);
1905
2005
 
@@ -1921,7 +2021,7 @@ pub const Effects = struct {
1921
2021
  // Pokémon Showdown clears P1 then P2 instead of status -> side -> foe
1922
2022
  if (showdown) {
1923
2023
  for (&battle.sides, 0..) |*s, i| {
1924
- const p = @intToEnum(Player, i);
2024
+ const p: Player = @enumFromInt(i);
1925
2025
  // Pokémon Showdown incorrectly does not prevent sleep/freeze from moving
1926
2026
  if (p != player and Status.any(s.stored().status)) {
1927
2027
  try log.curestatus(foe_ident, foe_stored.status, .Silent);
@@ -1930,7 +2030,7 @@ pub const Effects = struct {
1930
2030
  s.stored().status = Status.init(.PSN);
1931
2031
  try log.status(battle.active(p), s.stored().status, .None);
1932
2032
  }
1933
- try clearVolatiles(battle, p, log);
2033
+ try clearVolatiles(battle, p, p != player, options);
1934
2034
  }
1935
2035
  } else {
1936
2036
  if (Status.any(foe_stored.status)) {
@@ -1940,71 +2040,76 @@ pub const Effects = struct {
1940
2040
  try log.curestatus(foe_ident, foe_stored.status, .Silent);
1941
2041
  foe_stored.status = 0;
1942
2042
  }
1943
- try clearVolatiles(battle, player, log);
1944
- try clearVolatiles(battle, player.foe(), log);
2043
+ try clearVolatiles(battle, player, false, options);
2044
+ try clearVolatiles(battle, player.foe(), true, options);
1945
2045
  }
1946
2046
  }
1947
2047
 
1948
- fn heal(battle: anytype, player: Player, log: anytype) !void {
2048
+ fn heal(battle: anytype, player: Player, options: anytype) !void {
1949
2049
  var side = battle.side(player);
1950
2050
  var stored = side.stored();
1951
2051
  const ident = battle.active(player);
1952
2052
 
1953
2053
  // GLITCH: HP recovery move failure glitches
1954
2054
  const delta = stored.stats.hp - stored.hp;
1955
- if (delta == 0 or delta & 255 == 255) return try log.fail(ident, .None);
2055
+ if (delta == 0 or (delta & 255 == 255 and stored.hp % 256 != 0)) {
2056
+ return try options.log.fail(ident, .None);
2057
+ }
1956
2058
 
1957
2059
  const rest = side.last_selected_move == .Rest;
1958
2060
  if (rest) {
1959
2061
  // Adding the sleep status runs the sleep condition handler to roll duration
1960
2062
  if (showdown) battle.rng.advance(1);
1961
2063
  stored.status = Status.slf(2);
1962
- try log.statusFrom(ident, stored.status, Move.Rest);
2064
+ try options.log.statusFrom(ident, stored.status, Move.Rest);
1963
2065
  stored.hp = stored.stats.hp;
1964
2066
  } else {
1965
2067
  stored.hp = @min(stored.stats.hp, stored.hp + (stored.stats.hp / 2));
1966
2068
  }
1967
- try log.heal(ident, stored, if (rest) Heal.Silent else Heal.None);
2069
+ try options.log.heal(ident, stored, if (rest) Heal.Silent else Heal.None);
1968
2070
  }
1969
2071
 
1970
- fn hyperBeam(battle: anytype, player: Player, log: anytype) !void {
2072
+ fn hyperBeam(battle: anytype, player: Player, options: anytype) !void {
1971
2073
  battle.side(player).active.volatiles.Recharging = true;
1972
- try log.mustrecharge(battle.active(player));
2074
+ try options.log.mustrecharge(battle.active(player));
1973
2075
  }
1974
2076
 
1975
- fn leechSeed(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
2077
+ fn leechSeed(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1976
2078
  var foe = battle.foe(player);
1977
2079
 
1978
2080
  if (showdown) {
1979
2081
  // Invulnerability trumps type immunity on Pokémon Showdown
1980
2082
  if (!foe.active.volatiles.Invulnerable and foe.active.types.includes(.Grass)) {
1981
- return log.immune(battle.active(player.foe()), .None);
2083
+ return options.log.immune(battle.active(player.foe()), .None);
1982
2084
  }
1983
- if (!try checkHit(battle, player, move, log)) return;
2085
+ if (!try checkHit(battle, player, move, options)) return;
1984
2086
  if (foe.active.volatiles.LeechSeed) return;
1985
2087
  } else {
1986
- if (!try checkHit(battle, player, move, log)) return;
2088
+ if (!try checkHit(battle, player, move, options)) return;
1987
2089
  if (foe.active.types.includes(.Grass) or foe.active.volatiles.LeechSeed) {
1988
- try log.lastmiss();
1989
- return log.miss(battle.active(player));
2090
+ try options.log.lastmiss();
2091
+ return options.log.miss(battle.active(player));
1990
2092
  }
1991
2093
  }
1992
2094
 
2095
+ try options.chance.commit(player, .hit);
1993
2096
  foe.active.volatiles.LeechSeed = true;
1994
2097
 
1995
- try log.start(battle.active(player.foe()), .LeechSeed);
2098
+ try options.log.start(battle.active(player.foe()), .LeechSeed);
1996
2099
  }
1997
2100
 
1998
- fn lightScreen(battle: anytype, player: Player, log: anytype) !void {
2101
+ fn lightScreen(battle: anytype, player: Player, options: anytype) !void {
1999
2102
  var side = battle.side(player);
2000
2103
 
2001
- if (side.active.volatiles.LightScreen) return log.fail(battle.active(player), .None);
2104
+ if (side.active.volatiles.LightScreen) {
2105
+ return options.log.fail(battle.active(player), .None);
2106
+ }
2002
2107
  side.active.volatiles.LightScreen = true;
2003
2108
 
2004
- try log.start(battle.active(player), .LightScreen);
2109
+ try options.log.start(battle.active(player), .LightScreen);
2005
2110
  }
2006
2111
 
2007
- fn mimic(battle: anytype, player: Player, move: Move.Data, mslot: u8, log: anytype) !void {
2112
+ fn mimic(battle: anytype, player: Player, move: Move.Data, mslot: u8, options: anytype) !void {
2008
2113
  var side = battle.side(player);
2009
2114
  var foe = battle.foe(player);
2010
2115
 
@@ -2020,7 +2125,7 @@ pub const Effects = struct {
2020
2125
  const has_mimic = has_mimic: {
2021
2126
  for (side.active.moves, 0..) |m, i| {
2022
2127
  if (m.id == .Mimic) {
2023
- oslot = @intCast(u8, i + 1);
2128
+ oslot = @intCast(i + 1);
2024
2129
  break :has_mimic true;
2025
2130
  }
2026
2131
  }
@@ -2028,38 +2133,43 @@ pub const Effects = struct {
2028
2133
  };
2029
2134
  if (!has_mimic) {
2030
2135
  // Invulnerable foes or 1/256 miss can trigger |-miss| instead of |-fail|
2031
- if (!try checkHit(battle, player, move, log)) return;
2032
- return try log.fail(battle.active(player.foe()), .None);
2136
+ if (!try checkHit(battle, player, move, options)) return;
2137
+ try options.chance.commit(player, .hit);
2138
+ return try options.log.fail(battle.active(player.foe()), .None);
2033
2139
  }
2034
2140
  }
2035
- if (!try checkHit(battle, player, move, log)) return;
2141
+ if (!try checkHit(battle, player, move, options)) return;
2142
+ try options.chance.commit(player, .hit);
2036
2143
 
2037
- const rslot = Rolls.moveSlot(battle, &foe.active.moves, 0);
2144
+ const rslot = try Rolls.moveSlot(battle, player, &foe.active.moves, 0, options);
2038
2145
  side.active.move(oslot).id = foe.active.move(rslot).id;
2039
2146
 
2040
- try log.startEffect(battle.active(player), .Mimic, side.active.move(oslot).id);
2147
+ try options.log.startEffect(battle.active(player), .Mimic, side.active.move(oslot).id);
2041
2148
  }
2042
2149
 
2043
- fn mist(battle: anytype, player: Player, log: anytype) !void {
2150
+ fn mist(battle: anytype, player: Player, options: anytype) !void {
2044
2151
  var side = battle.side(player);
2045
2152
 
2046
2153
  if (side.active.volatiles.Mist) return;
2047
2154
  side.active.volatiles.Mist = true;
2048
2155
 
2049
- try log.start(battle.active(player), .Mist);
2156
+ try options.log.start(battle.active(player), .Mist);
2050
2157
  }
2051
2158
 
2052
- fn multiHit(battle: anytype, player: Player, move: Move.Data) void {
2159
+ fn multiHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
2053
2160
  var side = battle.side(player);
2054
2161
 
2055
2162
  assert(!side.active.volatiles.MultiHit);
2056
2163
  side.active.volatiles.MultiHit = true;
2057
2164
 
2058
- side.active.volatiles.attacks =
2059
- if (move.effect == .MultiHit) Rolls.distribution(battle) else 2;
2165
+ side.active.volatiles.attacks = if (move.effect == .MultiHit)
2166
+ try Rolls.distribution(battle, .MultiHit, player, options)
2167
+ else
2168
+ 2;
2060
2169
  }
2061
2170
 
2062
- fn paralyze(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
2171
+ fn paralyze(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
2172
+ var log = options.log;
2063
2173
  var foe = battle.foe(player);
2064
2174
  var foe_stored = foe.stored();
2065
2175
  const foe_ident = battle.active(player.foe());
@@ -2070,7 +2180,7 @@ pub const Effects = struct {
2070
2180
  if (showdown) {
2071
2181
  // Invulnerability trumps type immunity on Pokémon Showdown
2072
2182
  if (immune and !foe.active.volatiles.Invulnerable) return log.immune(foe_ident, .None);
2073
- if (!try checkHit(battle, player, move, log)) return;
2183
+ if (!try checkHit(battle, player, move, options)) return;
2074
2184
  }
2075
2185
  if (Status.any(foe_stored.status)) {
2076
2186
  return log.fail(
@@ -2080,8 +2190,9 @@ pub const Effects = struct {
2080
2190
  }
2081
2191
  if (!showdown) {
2082
2192
  if (immune) return log.immune(foe_ident, .None);
2083
- if (!try checkHit(battle, player, move, log)) return;
2193
+ if (!try checkHit(battle, player, move, options)) return;
2084
2194
  }
2195
+ try options.chance.commit(player, .hit);
2085
2196
 
2086
2197
  foe_stored.status = Status.init(.PAR);
2087
2198
  foe.active.stats.spe = @max(foe.active.stats.spe / 4, 1);
@@ -2089,7 +2200,7 @@ pub const Effects = struct {
2089
2200
  try log.status(foe_ident, foe_stored.status, .None);
2090
2201
  }
2091
2202
 
2092
- fn paralyzeChance(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
2203
+ fn paralyzeChance(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
2093
2204
  var foe = battle.foe(player);
2094
2205
  var foe_stored = foe.stored();
2095
2206
 
@@ -2101,25 +2212,30 @@ pub const Effects = struct {
2101
2212
 
2102
2213
  // Body Slam can't paralyze a Normal type Pokémon
2103
2214
  if (foe.active.types.includes(move.type)) return;
2104
- if (!Rolls.secondaryChance(battle, move.effect == .ParalyzeChance1)) return;
2215
+ if (!try Rolls.secondaryChance(battle, player, move.effect == .ParalyzeChance1, options)) {
2216
+ return;
2217
+ }
2105
2218
 
2106
2219
  foe_stored.status = Status.init(.PAR);
2107
2220
  foe.active.stats.spe = @max(foe.active.stats.spe / 4, 1);
2108
2221
 
2109
- try log.status(battle.active(player.foe()), foe_stored.status, .None);
2222
+ try options.log.status(battle.active(player.foe()), foe_stored.status, .None);
2110
2223
  }
2111
2224
 
2112
- fn payDay(battle: anytype, player: Player, log: anytype) !void {
2113
- if (!showdown or !battle.foe(player).active.volatiles.Substitute) try log.fieldactivate();
2225
+ fn payDay(battle: anytype, player: Player, options: anytype) !void {
2226
+ if (!showdown or !battle.foe(player).active.volatiles.Substitute) {
2227
+ try options.log.fieldactivate();
2228
+ }
2114
2229
  }
2115
2230
 
2116
- fn poison(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
2231
+ fn poison(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
2232
+ var log = options.log;
2117
2233
  var foe = battle.foe(player);
2118
2234
  var foe_stored = foe.stored();
2119
2235
  const foe_ident = battle.active(player.foe());
2120
2236
  const toxic = battle.side(player).last_selected_move == .Toxic;
2121
2237
 
2122
- if (showdown and move.effect == .Poison and !try checkHit(battle, player, move, log)) {
2238
+ if (showdown and move.effect == .Poison and !try checkHit(battle, player, move, options)) {
2123
2239
  return;
2124
2240
  } else if (foe.active.volatiles.Substitute) {
2125
2241
  if (move.effect != .Poison) return;
@@ -2143,8 +2259,13 @@ pub const Effects = struct {
2143
2259
  }
2144
2260
 
2145
2261
  if (move.effect == .Poison) {
2146
- if (!showdown and !try checkHit(battle, player, move, log)) return;
2147
- } else if (!Rolls.poisonChance(battle, move.effect == .PoisonChance1)) return;
2262
+ if (!showdown and !try checkHit(battle, player, move, options)) return;
2263
+ try options.chance.commit(player, .hit);
2264
+ } else {
2265
+ if (!try Rolls.poisonChance(battle, player, move.effect == .PoisonChance1, options)) {
2266
+ return;
2267
+ }
2268
+ }
2148
2269
 
2149
2270
  foe_stored.status = Status.init(.PSN);
2150
2271
  if (toxic) {
@@ -2162,28 +2283,36 @@ pub const Effects = struct {
2162
2283
  volatiles.Rage = true;
2163
2284
  }
2164
2285
 
2165
- fn recoil(battle: anytype, player: Player, residual: *bool, log: anytype) !void {
2286
+ fn recoil(battle: anytype, player: Player, residual: *bool, options: anytype) !void {
2166
2287
  var side = battle.side(player);
2167
2288
  var stored = side.stored();
2168
2289
 
2169
- const damage = @intCast(i16, @max(battle.last_damage /
2290
+ assert(showdown or battle.last_damage > 0);
2291
+ if (showdown and battle.last_damage == 0) return;
2292
+
2293
+ const damage: i16 = @intCast(@max(battle.last_damage /
2170
2294
  @as(u8, if (side.last_selected_move == .Struggle) 2 else 4), 1));
2171
- stored.hp = @intCast(u16, @max(@intCast(i16, stored.hp) - damage, 0));
2295
+ stored.hp = @intCast(@max(@as(i16, @intCast(stored.hp)) - damage, 0));
2172
2296
 
2173
- try log.damageOf(battle.active(player), stored, .RecoilOf, battle.active(player.foe()));
2297
+ try options.log.damageOf(
2298
+ battle.active(player),
2299
+ stored,
2300
+ .RecoilOf,
2301
+ battle.active(player.foe()),
2302
+ );
2174
2303
  if (showdown and stored.hp == 0) residual.* = false;
2175
2304
  }
2176
2305
 
2177
- fn reflect(battle: anytype, player: Player, log: anytype) !void {
2306
+ fn reflect(battle: anytype, player: Player, options: anytype) !void {
2178
2307
  var side = battle.side(player);
2179
2308
 
2180
- if (side.active.volatiles.Reflect) return log.fail(battle.active(player), .None);
2309
+ if (side.active.volatiles.Reflect) return options.log.fail(battle.active(player), .None);
2181
2310
  side.active.volatiles.Reflect = true;
2182
2311
 
2183
- try log.start(battle.active(player), .Reflect);
2312
+ try options.log.start(battle.active(player), .Reflect);
2184
2313
  }
2185
2314
 
2186
- fn sleep(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
2315
+ fn sleep(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
2187
2316
  var foe = battle.foe(player);
2188
2317
  var foe_stored = foe.stored();
2189
2318
  const foe_ident = battle.active(player.foe());
@@ -2194,13 +2323,14 @@ pub const Effects = struct {
2194
2323
  // the Sleep Clause Mod might activate, causing us to not actually bypass
2195
2324
  } else {
2196
2325
  if (Status.any(foe_stored.status)) {
2197
- return log.fail(
2326
+ return options.log.fail(
2198
2327
  foe_ident,
2199
2328
  if (Status.is(foe_stored.status, .SLP)) .Sleep else .None,
2200
2329
  );
2201
2330
  }
2202
2331
  // If checkHit in doMove didn't return true Pokémon Showdown wouldn't be in here
2203
- if (!showdown and !try checkHit(battle, player, move, log)) return;
2332
+ if (!showdown and !try checkHit(battle, player, move, options)) return;
2333
+ try options.chance.commit(player, .hit);
2204
2334
  }
2205
2335
 
2206
2336
  // Sleep Clause Mod
@@ -2211,31 +2341,32 @@ pub const Effects = struct {
2211
2341
  }
2212
2342
  foe.active.volatiles.Recharging = false;
2213
2343
 
2214
- foe_stored.status = Status.slp(Rolls.sleepDuration(battle));
2215
- try log.statusFrom(foe_ident, foe_stored.status, battle.side(player).last_selected_move);
2344
+ foe_stored.status = Status.slp(Rolls.sleepDuration(battle, player, options));
2345
+ const last = battle.side(player).last_selected_move;
2346
+ try options.log.statusFrom(foe_ident, foe_stored.status, last);
2216
2347
  }
2217
2348
 
2218
- fn splash(battle: anytype, player: Player, log: anytype) !void {
2219
- try log.activate(battle.active(player), .Splash);
2349
+ fn splash(battle: anytype, player: Player, options: anytype) !void {
2350
+ try options.log.activate(battle.active(player), .Splash);
2220
2351
  }
2221
2352
 
2222
- fn substitute(battle: anytype, player: Player, residual: *bool, log: anytype) !void {
2353
+ fn substitute(battle: anytype, player: Player, residual: *bool, options: anytype) !void {
2223
2354
  var side = battle.side(player);
2224
2355
  if (side.active.volatiles.Substitute) {
2225
- try log.fail(battle.active(player), .Substitute);
2356
+ try options.log.fail(battle.active(player), .Substitute);
2226
2357
  return;
2227
2358
  }
2228
2359
 
2229
2360
  assert(side.stored().stats.hp <= 1023);
2230
2361
  // Will be 0 if HP is <= 3 meaning that the user gets a 1 HP Substitute for "free"
2231
- const hp = @intCast(u8, side.stored().stats.hp / 4);
2362
+ const hp: u8 = @intCast(side.stored().stats.hp / 4);
2232
2363
  // Pokénon Showdown incorrectly checks for 1/4 HP based on `target.maxhp / 4` which returns
2233
2364
  // a floating point value and thus only correctly implements the Substitute 1/4 glitch when
2234
2365
  // the target's HP is exactly divisible by 4 (here we're using an inlined divCeil routine to
2235
2366
  // avoid having to convert to floating point)
2236
2367
  const required_hp = if (showdown) @divFloor(side.stored().stats.hp - 1, 4) + 1 else hp;
2237
2368
  if (side.stored().hp < required_hp) {
2238
- try log.fail(battle.active(player), .Weak);
2369
+ try options.log.fail(battle.active(player), .Weak);
2239
2370
  return;
2240
2371
  }
2241
2372
 
@@ -2243,31 +2374,37 @@ pub const Effects = struct {
2243
2374
  side.stored().hp -= hp;
2244
2375
  side.active.volatiles.substitute = hp + 1;
2245
2376
  side.active.volatiles.Substitute = true;
2246
- try log.start(battle.active(player), .Substitute);
2377
+ try options.log.start(battle.active(player), .Substitute);
2247
2378
  if (hp > 0) {
2248
- try log.damage(battle.active(player), side.stored(), .None);
2379
+ try options.log.damage(battle.active(player), side.stored(), .None);
2249
2380
  if (showdown and side.stored().hp == 0) residual.* = false;
2250
2381
  }
2251
2382
  }
2252
2383
 
2253
- fn switchAndTeleport(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
2254
- if (!showdown or battle.side(player).last_selected_move == .Teleport) return;
2255
-
2384
+ fn switchAndTeleport(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
2256
2385
  // Whirlwind/Roar should not roll to hit/reset damage but Pokémon Showdown does anyway
2257
- _ = try checkHit(battle, player, move, log);
2258
- battle.last_damage = 0;
2386
+ if (showdown) {
2387
+ if (battle.side(player).last_selected_move == .Teleport) return;
2388
+ if (try checkHit(battle, player, move, options)) {
2389
+ try options.chance.commit(player, .hit);
2390
+ }
2391
+ battle.last_damage = 0;
2392
+ } else {
2393
+ try options.log.fail(battle.active(player), .None);
2394
+ try options.log.laststill();
2395
+ }
2259
2396
  }
2260
2397
 
2261
- fn thrashing(battle: anytype, player: Player) void {
2398
+ fn thrashing(battle: anytype, player: Player, options: anytype) void {
2262
2399
  var volatiles = &battle.side(player).active.volatiles;
2263
2400
  assert(!volatiles.Thrashing);
2264
2401
  assert(!volatiles.Bide);
2265
2402
 
2266
2403
  volatiles.Thrashing = true;
2267
- volatiles.attacks = Rolls.bideThrashDuration(battle);
2404
+ volatiles.attacks = Rolls.attackingDuration(battle, player, options);
2268
2405
  }
2269
2406
 
2270
- fn transform(battle: anytype, player: Player, log: anytype) !void {
2407
+ fn transform(battle: anytype, player: Player, options: anytype) !void {
2271
2408
  var side = battle.side(player);
2272
2409
  const foe = battle.foe(player);
2273
2410
  const foe_ident = battle.active(player.foe());
@@ -2293,10 +2430,10 @@ pub const Effects = struct {
2293
2430
  side.active.moves[i].pp = if (m.id != .None) 5 else 0;
2294
2431
  }
2295
2432
 
2296
- try log.transform(battle.active(player), foe_ident);
2433
+ try options.log.transform(battle.active(player), foe_ident);
2297
2434
  }
2298
2435
 
2299
- fn binding(battle: anytype, player: Player, rewrap: bool) void {
2436
+ fn binding(battle: anytype, player: Player, rewrap: bool, options: anytype) !void {
2300
2437
  var side = battle.side(player);
2301
2438
  var foe = battle.foe(player);
2302
2439
 
@@ -2308,10 +2445,12 @@ pub const Effects = struct {
2308
2445
  foe.active.volatiles.Recharging = false;
2309
2446
  } else if (foe.stored().hp == 0) return if (!rewrap) battle.rng.advance(1);
2310
2447
 
2311
- side.active.volatiles.attacks = Rolls.distribution(battle) - 1;
2448
+ side.active.volatiles.attacks =
2449
+ try Rolls.distribution(battle, .Binding, player, options) - 1;
2312
2450
  }
2313
2451
 
2314
- fn boost(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
2452
+ fn boost(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
2453
+ var log = options.log;
2315
2454
  var side = battle.side(player);
2316
2455
  const ident = battle.active(player);
2317
2456
 
@@ -2323,7 +2462,7 @@ pub const Effects = struct {
2323
2462
  assert(boosts.atk >= -6 and boosts.atk <= 6);
2324
2463
  if (boosts.atk == 6) return try log.fail(ident, .None);
2325
2464
  const n: u2 = if (move.effect == .AttackUp2) 2 else 1;
2326
- boosts.atk = @intCast(i4, @min(6, @as(i8, boosts.atk) + n));
2465
+ boosts.atk = @as(i4, @intCast(@min(6, @as(i8, boosts.atk) + n)));
2327
2466
  const reason = if (move.effect == .Rage) Boost.Rage else Boost.Attack;
2328
2467
  if (stats.atk == MAX_STAT_VALUE) {
2329
2468
  boosts.atk -= 1;
@@ -2334,7 +2473,7 @@ pub const Effects = struct {
2334
2473
  }
2335
2474
  return try log.fail(ident, .None);
2336
2475
  }
2337
- var mod = BOOSTS[@intCast(u4, @as(i8, boosts.atk) + 6)];
2476
+ var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.atk) + 6))];
2338
2477
  const stat = unmodifiedStats(battle, side).atk;
2339
2478
  stats.atk = @min(MAX_STAT_VALUE, stat * mod[0] / mod[1]);
2340
2479
  try log.boost(ident, reason, n);
@@ -2345,7 +2484,7 @@ pub const Effects = struct {
2345
2484
  assert(boosts.def >= -6 and boosts.def <= 6);
2346
2485
  if (boosts.def == 6) return try log.fail(ident, .None);
2347
2486
  const n: u2 = if (move.effect == .DefenseUp2) 2 else 1;
2348
- boosts.def = @intCast(i4, @min(6, @as(i8, boosts.def) + n));
2487
+ boosts.def = @intCast(@min(6, @as(i8, boosts.def) + n));
2349
2488
  if (stats.def == MAX_STAT_VALUE) {
2350
2489
  boosts.def -= 1;
2351
2490
  if (showdown) {
@@ -2354,7 +2493,7 @@ pub const Effects = struct {
2354
2493
  }
2355
2494
  return try log.fail(ident, .None);
2356
2495
  }
2357
- var mod = BOOSTS[@intCast(u4, @as(i8, boosts.def) + 6)];
2496
+ var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.def) + 6))];
2358
2497
  const stat = unmodifiedStats(battle, side).def;
2359
2498
  stats.def = @min(MAX_STAT_VALUE, stat * mod[0] / mod[1]);
2360
2499
  try log.boost(ident, .Defense, n);
@@ -2362,7 +2501,7 @@ pub const Effects = struct {
2362
2501
  .SpeedUp2 => {
2363
2502
  assert(boosts.spe >= -6 and boosts.spe <= 6);
2364
2503
  if (boosts.spe == 6) return try log.fail(ident, .None);
2365
- boosts.spe = @intCast(i4, @min(6, @as(i8, boosts.spe) + 2));
2504
+ boosts.spe = @intCast(@min(6, @as(i8, boosts.spe) + 2));
2366
2505
  if (stats.spe == MAX_STAT_VALUE) {
2367
2506
  boosts.spe -= 1;
2368
2507
  if (showdown) {
@@ -2371,7 +2510,7 @@ pub const Effects = struct {
2371
2510
  }
2372
2511
  return try log.fail(ident, .None);
2373
2512
  }
2374
- var mod = BOOSTS[@intCast(u4, @as(i8, boosts.spe) + 6)];
2513
+ var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.spe) + 6))];
2375
2514
  const stat = unmodifiedStats(battle, side).spe;
2376
2515
  stats.spe = @min(MAX_STAT_VALUE, stat * mod[0] / mod[1]);
2377
2516
  try log.boost(ident, .Speed, 2);
@@ -2380,7 +2519,7 @@ pub const Effects = struct {
2380
2519
  assert(boosts.spc >= -6 and boosts.spc <= 6);
2381
2520
  if (boosts.spc == 6) return try log.fail(ident, .None);
2382
2521
  const n: u2 = if (move.effect == .SpecialUp2) 2 else 1;
2383
- boosts.spc = @intCast(i4, @min(6, @as(i8, boosts.spc) + n));
2522
+ boosts.spc = @intCast(@min(6, @as(i8, boosts.spc) + n));
2384
2523
  if (stats.spc == MAX_STAT_VALUE) {
2385
2524
  boosts.spc -= 1;
2386
2525
  if (showdown) {
@@ -2391,7 +2530,7 @@ pub const Effects = struct {
2391
2530
  }
2392
2531
  return try log.fail(ident, .None);
2393
2532
  }
2394
- var mod = BOOSTS[@intCast(u4, @as(i8, boosts.spc) + 6)];
2533
+ var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.spc) + 6))];
2395
2534
  const stat = unmodifiedStats(battle, side).spc;
2396
2535
  stats.spc = @min(MAX_STAT_VALUE, stat * mod[0] / mod[1]);
2397
2536
  try log.boost(ident, .SpecialAttack, n);
@@ -2400,7 +2539,7 @@ pub const Effects = struct {
2400
2539
  .EvasionUp1 => {
2401
2540
  assert(boosts.evasion >= -6 and boosts.evasion <= 6);
2402
2541
  if (boosts.evasion == 6) return try log.fail(ident, .None);
2403
- boosts.evasion = @intCast(i4, @min(6, @as(i8, boosts.evasion) + 1));
2542
+ boosts.evasion = @intCast(@min(6, @as(i8, boosts.evasion) + 1));
2404
2543
  try log.boost(ident, .Evasion, 1);
2405
2544
  },
2406
2545
  else => unreachable,
@@ -2410,28 +2549,32 @@ pub const Effects = struct {
2410
2549
  statusModify(battle.foe(player).stored().status, &battle.foe(player).active.stats);
2411
2550
  }
2412
2551
 
2413
- fn unboost(battle: anytype, player: Player, move: Move.Data, log: anytype) !void {
2552
+ fn unboost(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
2553
+ var log = options.log;
2414
2554
  var foe = battle.foe(player);
2415
2555
  const foe_ident = battle.active(player.foe());
2556
+ const secondary = move.effect.isStatDownChance();
2416
2557
 
2417
- if (foe.active.volatiles.Substitute) {
2418
- return if (!move.effect.isStatDownChance()) log.fail(foe_ident, .None);
2419
- }
2558
+ if (foe.active.volatiles.Substitute) return if (!secondary) log.fail(foe_ident, .None);
2420
2559
 
2421
- if (move.effect.isStatDownChance()) {
2422
- if (!Rolls.unboost(battle) or foe.active.volatiles.Invulnerable) return;
2423
- } else if (!showdown and !try checkHit(battle, player, move, log)) {
2424
- return; // checkHit already checks for Invulnerable
2560
+ if (secondary) {
2561
+ const proc = try Rolls.unboost(battle, player, options);
2562
+ if (!proc or foe.active.volatiles.Invulnerable) return;
2563
+ } else {
2564
+ // checkHit already checks for Invulnerable
2565
+ if (!showdown and !try checkHit(battle, player, move, options)) return;
2566
+ try options.chance.commit(player, .hit);
2425
2567
  }
2426
2568
 
2427
2569
  var stats = &foe.active.stats;
2428
2570
  var boosts = &foe.active.boosts;
2571
+ const fail = showdown or !secondary;
2429
2572
 
2430
2573
  switch (move.effect) {
2431
2574
  .AttackDown1, .AttackDownChance => {
2432
2575
  assert(boosts.atk >= -6 and boosts.atk <= 6);
2433
- if (boosts.atk == -6) return try log.fail(foe_ident, .None);
2434
- boosts.atk = @intCast(i4, @max(-6, @as(i8, boosts.atk) - 1));
2576
+ if (boosts.atk == -6) return if (fail) try log.fail(foe_ident, .None);
2577
+ boosts.atk = @intCast(@max(-6, @as(i8, boosts.atk) - 1));
2435
2578
  if (stats.atk == 1) {
2436
2579
  boosts.atk += 1;
2437
2580
  if (showdown) {
@@ -2440,16 +2583,16 @@ pub const Effects = struct {
2440
2583
  }
2441
2584
  return try log.fail(foe_ident, .None);
2442
2585
  }
2443
- var mod = BOOSTS[@intCast(u4, @as(i8, boosts.atk) + 6)];
2586
+ var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.atk) + 6))];
2444
2587
  const stat = unmodifiedStats(battle, foe).atk;
2445
2588
  stats.atk = @max(1, stat * mod[0] / mod[1]);
2446
2589
  try log.boost(foe_ident, .Attack, -1);
2447
2590
  },
2448
2591
  .DefenseDown1, .DefenseDown2, .DefenseDownChance => {
2449
2592
  assert(boosts.def >= -6 and boosts.def <= 6);
2450
- if (boosts.def == -6) return try log.fail(foe_ident, .None);
2593
+ if (boosts.def == -6) return if (fail) try log.fail(foe_ident, .None);
2451
2594
  const n: u2 = if (move.effect == .DefenseDown2) 2 else 1;
2452
- boosts.def = @intCast(i4, @max(-6, @as(i8, boosts.def) - n));
2595
+ boosts.def = @intCast(@max(-6, @as(i8, boosts.def) - n));
2453
2596
  if (stats.def == 1) {
2454
2597
  boosts.def += 1;
2455
2598
  if (showdown) {
@@ -2458,15 +2601,15 @@ pub const Effects = struct {
2458
2601
  }
2459
2602
  return try log.fail(foe_ident, .None);
2460
2603
  }
2461
- var mod = BOOSTS[@intCast(u4, @as(i8, boosts.def) + 6)];
2604
+ var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.def) + 6))];
2462
2605
  const stat = unmodifiedStats(battle, foe).def;
2463
2606
  stats.def = @max(1, stat * mod[0] / mod[1]);
2464
2607
  try log.boost(foe_ident, .Defense, -@as(i8, n));
2465
2608
  },
2466
2609
  .SpeedDown1, .SpeedDownChance => {
2467
2610
  assert(boosts.spe >= -6 and boosts.spe <= 6);
2468
- if (boosts.spe == -6) return try log.fail(foe_ident, .None);
2469
- boosts.spe = @intCast(i4, @max(-6, @as(i8, boosts.spe) - 1));
2611
+ if (boosts.spe == -6) return if (fail) try log.fail(foe_ident, .None);
2612
+ boosts.spe = @intCast(@max(-6, @as(i8, boosts.spe) - 1));
2470
2613
  if (stats.spe == 1) {
2471
2614
  boosts.spe += 1;
2472
2615
  if (showdown) {
@@ -2475,7 +2618,7 @@ pub const Effects = struct {
2475
2618
  }
2476
2619
  return try log.fail(foe_ident, .None);
2477
2620
  }
2478
- var mod = BOOSTS[@intCast(u4, @as(i8, boosts.spe) + 6)];
2621
+ var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.spe) + 6))];
2479
2622
  const stat = unmodifiedStats(battle, foe).spe;
2480
2623
  stats.spe = @max(1, stat * mod[0] / mod[1]);
2481
2624
  try log.boost(foe_ident, .Speed, -1);
@@ -2483,8 +2626,8 @@ pub const Effects = struct {
2483
2626
  },
2484
2627
  .SpecialDownChance => {
2485
2628
  assert(boosts.spc >= -6 and boosts.spc <= 6);
2486
- if (boosts.spc == -6) return try log.fail(foe_ident, .None);
2487
- boosts.spc = @intCast(i4, @max(-6, @as(i8, boosts.spc) - 1));
2629
+ if (boosts.spc == -6) return if (fail) try log.fail(foe_ident, .None);
2630
+ boosts.spc = @intCast(@max(-6, @as(i8, boosts.spc) - 1));
2488
2631
  if (stats.spc == 1) {
2489
2632
  boosts.spc += 1;
2490
2633
  if (showdown) {
@@ -2495,7 +2638,7 @@ pub const Effects = struct {
2495
2638
  }
2496
2639
  return try log.fail(foe_ident, .None);
2497
2640
  }
2498
- var mod = BOOSTS[@intCast(u4, @as(i8, boosts.spc) + 6)];
2641
+ var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.spc) + 6))];
2499
2642
  const stat = unmodifiedStats(battle, foe).spc;
2500
2643
  stats.spc = @max(1, stat * mod[0] / mod[1]);
2501
2644
  try log.boost(foe_ident, .SpecialAttack, -1);
@@ -2503,8 +2646,8 @@ pub const Effects = struct {
2503
2646
  },
2504
2647
  .AccuracyDown1 => {
2505
2648
  assert(boosts.accuracy >= -6 and boosts.accuracy <= 6);
2506
- if (boosts.accuracy == -6) return try log.fail(foe_ident, .None);
2507
- boosts.accuracy = @intCast(i4, @max(-6, @as(i8, boosts.accuracy) - 1));
2649
+ if (boosts.accuracy == -6) return if (fail) try log.fail(foe_ident, .None);
2650
+ boosts.accuracy = @intCast(@max(-6, @as(i8, boosts.accuracy) - 1));
2508
2651
  try log.boost(foe_ident, .Accuracy, -1);
2509
2652
  },
2510
2653
  else => unreachable,
@@ -2529,16 +2672,19 @@ fn statusModify(status: u8, stats: *Stats(u16)) void {
2529
2672
  }
2530
2673
  }
2531
2674
 
2532
- inline fn isForced(active: ActivePokemon) bool {
2675
+ inline fn isForced(active: anytype) bool {
2533
2676
  return active.volatiles.Recharging or active.volatiles.Rage or
2534
2677
  active.volatiles.Thrashing or active.volatiles.Charging;
2535
2678
  }
2536
2679
 
2537
- fn clearVolatiles(battle: anytype, who: Player, log: anytype) !void {
2680
+ fn clearVolatiles(battle: anytype, who: Player, clear: bool, options: anytype) !void {
2681
+ var log = options.log;
2538
2682
  var side = battle.side(who);
2539
2683
  var volatiles = &side.active.volatiles;
2540
2684
  const ident = battle.active(who);
2541
2685
 
2686
+ options.chance.clearDurations(who, clear);
2687
+
2542
2688
  if (volatiles.disabled_move != 0) {
2543
2689
  volatiles.disabled_move = 0;
2544
2690
  volatiles.disabled_duration = 0;
@@ -2551,15 +2697,15 @@ fn clearVolatiles(battle: anytype, who: Player, log: anytype) !void {
2551
2697
  }
2552
2698
  if (volatiles.Mist) {
2553
2699
  volatiles.Mist = false;
2554
- try log.end(ident, .Mist);
2700
+ try log.end(ident, .MistSilent);
2555
2701
  }
2556
2702
  if (volatiles.FocusEnergy) {
2557
2703
  volatiles.FocusEnergy = false;
2558
- try log.end(ident, .FocusEnergy);
2704
+ try log.end(ident, .FocusEnergySilent);
2559
2705
  }
2560
2706
  if (volatiles.LeechSeed) {
2561
2707
  volatiles.LeechSeed = false;
2562
- try log.end(ident, .LeechSeed);
2708
+ try log.end(ident, .LeechSeedSilent);
2563
2709
  }
2564
2710
  if (!showdown and volatiles.Toxic) {
2565
2711
  volatiles.Toxic = false;
@@ -2567,152 +2713,313 @@ fn clearVolatiles(battle: anytype, who: Player, log: anytype) !void {
2567
2713
  }
2568
2714
  if (volatiles.LightScreen) {
2569
2715
  volatiles.LightScreen = false;
2570
- try log.end(ident, .LightScreen);
2716
+ try log.end(ident, .LightScreenSilent);
2571
2717
  }
2572
2718
  if (volatiles.Reflect) {
2573
2719
  volatiles.Reflect = false;
2574
- try log.end(ident, .Reflect);
2720
+ try log.end(ident, .ReflectSilent);
2575
2721
  }
2576
2722
  if (showdown and volatiles.Toxic) {
2577
2723
  volatiles.Toxic = false;
2578
2724
  // Pokémon Showdown erroneously clears the toxic counter
2579
2725
  volatiles.toxic = 0;
2580
- try log.end(ident, .Toxic);
2726
+ try log.end(ident, .ToxicSilent);
2581
2727
  }
2582
2728
  }
2583
2729
 
2584
2730
  pub const Rolls = struct {
2585
- inline fn speedTie(battle: anytype) bool {
2586
- if (showdown) return battle.rng.range(u8, 0, 2) == 0;
2587
- return battle.rng.next() < Gen12.percent(50) + 1;
2588
- }
2731
+ inline fn speedTie(battle: anytype, options: anytype) !bool {
2732
+ const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
2733
+ player == .P1
2734
+ else if (showdown)
2735
+ battle.rng.range(u8, 0, 2) == 0
2736
+ else
2737
+ battle.rng.next() < Gen12.percent(50) + 1;
2589
2738
 
2590
- inline fn criticalHit(battle: anytype, chance: u16) bool {
2591
- if (showdown) return battle.rng.chance(u8, @intCast(u8, chance), 256);
2592
- return std.math.rotl(u8, battle.rng.next(), 3) < chance;
2739
+ try options.chance.speedTie(p1);
2740
+ return p1;
2593
2741
  }
2594
2742
 
2595
- inline fn damage(battle: anytype) u8 {
2596
- if (showdown) return battle.rng.range(u8, 217, 256);
2597
- while (true) {
2598
- const r = std.math.rotr(u8, battle.rng.next(), 1);
2599
- if (r >= 217) return r;
2600
- }
2601
- }
2743
+ inline fn criticalHit(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
2744
+ const crit = if (options.calc.overridden(player, .critical_hit)) |val|
2745
+ val == .true
2746
+ else if (showdown)
2747
+ battle.rng.chance(u8, rate, 256)
2748
+ else
2749
+ std.math.rotl(u8, battle.rng.next(), 3) < rate;
2602
2750
 
2603
- inline fn hit(battle: anytype, accuracy: u16) bool {
2604
- if (showdown) return battle.rng.chance(u8, @intCast(u8, accuracy), 256);
2605
- return battle.rng.next() < accuracy;
2751
+ try options.chance.criticalHit(player, crit, rate);
2752
+ return crit;
2606
2753
  }
2607
2754
 
2608
- inline fn confusionDuration(battle: anytype) u3 {
2609
- if (showdown) return @intCast(u3, battle.rng.range(u8, 2, 6));
2610
- return @intCast(u3, (battle.rng.next() & 3) + 2);
2611
- }
2755
+ inline fn damage(battle: anytype, player: anytype, options: anytype) !u8 {
2756
+ const roll = if (options.calc.overridden(player, .damage)) |val|
2757
+ val
2758
+ else roll: {
2759
+ if (showdown) break :roll battle.rng.range(u8, 217, 256);
2760
+ while (true) {
2761
+ const r = std.math.rotr(u8, battle.rng.next(), 1);
2762
+ if (r >= 217) break :roll r;
2763
+ }
2764
+ };
2612
2765
 
2613
- inline fn confused(battle: anytype) bool {
2614
- if (showdown) return !battle.rng.chance(u8, 128, 256);
2615
- return battle.rng.next() >= Gen12.percent(50) + 1;
2766
+ assert(roll >= 217 and roll <= 255);
2767
+ try options.chance.damage(player, roll);
2768
+ return roll;
2616
2769
  }
2617
2770
 
2618
- inline fn paralyzed(battle: anytype) bool {
2619
- if (showdown) return battle.rng.chance(u8, 63, 256);
2620
- return battle.rng.next() < Gen12.percent(25);
2771
+ inline fn hit(battle: anytype, player: Player, accuracy: u8, options: anytype) bool {
2772
+ const ok = if (options.calc.overridden(player, .hit)) |val|
2773
+ val == .true
2774
+ else if (showdown)
2775
+ battle.rng.chance(u8, accuracy, 256)
2776
+ else
2777
+ battle.rng.next() < accuracy;
2778
+
2779
+ options.chance.hit(ok, accuracy);
2780
+ return ok;
2621
2781
  }
2622
2782
 
2623
- inline fn confusionChance(battle: anytype) bool {
2624
- if (showdown) return battle.rng.chance(u8, 25, 256);
2625
- return battle.rng.next() < Gen12.percent(10);
2783
+ inline fn confused(battle: anytype, player: Player, options: anytype) !bool {
2784
+ const cfz = if (options.calc.overridden(player, .confused)) |val|
2785
+ val == .true
2786
+ else if (showdown)
2787
+ !battle.rng.chance(u8, 128, 256)
2788
+ else
2789
+ battle.rng.next() >= Gen12.percent(50) + 1;
2790
+
2791
+ try options.chance.confused(player, cfz);
2792
+ return cfz;
2626
2793
  }
2627
2794
 
2628
- inline fn secondaryChance(battle: anytype, low: bool) bool {
2629
- if (showdown) return battle.rng.chance(u8, @as(u8, if (low) 26 else 77), 256);
2630
- return battle.rng.next() < 1 + (if (low) Gen12.percent(10) else Gen12.percent(30));
2795
+ inline fn paralyzed(battle: anytype, player: Player, options: anytype) !bool {
2796
+ const par = if (options.calc.overridden(player, .paralyzed)) |val|
2797
+ val == .true
2798
+ else if (showdown)
2799
+ battle.rng.chance(u8, 63, 256)
2800
+ else
2801
+ battle.rng.next() < Gen12.percent(25);
2802
+
2803
+ try options.chance.paralyzed(player, par);
2804
+ return par;
2631
2805
  }
2632
2806
 
2633
- inline fn poisonChance(battle: anytype, low: bool) bool {
2634
- if (showdown) return battle.rng.chance(u8, @as(u8, if (low) 52 else 103), 256);
2635
- return battle.rng.next() < 1 + (if (low) Gen12.percent(20) else Gen12.percent(40));
2807
+ inline fn confusionChance(battle: anytype, player: Player, options: anytype) !bool {
2808
+ const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
2809
+ val == .true
2810
+ else if (showdown)
2811
+ battle.rng.chance(u8, 25, 256)
2812
+ else
2813
+ battle.rng.next() < Gen12.percent(10);
2814
+
2815
+ try options.chance.secondaryChance(player, proc, 25);
2816
+ return proc;
2636
2817
  }
2637
2818
 
2638
- inline fn sleepDuration(battle: anytype) u3 {
2639
- if (showdown) return @intCast(u3, battle.rng.range(u8, 1, 8));
2640
- while (true) {
2641
- const r = battle.rng.next() & 7;
2642
- if (r != 0) return @intCast(u3, r);
2643
- }
2819
+ inline fn secondaryChance(battle: anytype, player: Player, low: bool, options: anytype) !bool {
2820
+ const rate: u8 = if (low) 26 else 77;
2821
+
2822
+ const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
2823
+ val == .true
2824
+ else if (showdown)
2825
+ battle.rng.chance(u8, rate, 256)
2826
+ else
2827
+ battle.rng.next() < 1 + (if (low) Gen12.percent(10) else Gen12.percent(30));
2828
+
2829
+ try options.chance.secondaryChance(player, proc, rate);
2830
+ return proc;
2644
2831
  }
2645
2832
 
2646
- inline fn bideThrashDuration(battle: anytype) u3 {
2647
- if (showdown) return @intCast(u3, battle.rng.range(u4, 2, 4));
2648
- return @intCast(u3, (battle.rng.next() & 1) + 2);
2833
+ inline fn poisonChance(battle: anytype, player: Player, low: bool, options: anytype) !bool {
2834
+ const rate: u8 = if (low) 52 else 103;
2835
+
2836
+ const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
2837
+ val == .true
2838
+ else if (showdown)
2839
+ battle.rng.chance(u8, rate, 256)
2840
+ else
2841
+ battle.rng.next() < 1 + (if (low) Gen12.percent(20) else Gen12.percent(40));
2842
+
2843
+ try options.chance.secondaryChance(player, proc, rate);
2844
+ return proc;
2649
2845
  }
2650
2846
 
2651
- fn moveSlot(battle: anytype, moves: []MoveSlot, check_pp: u4) u4 {
2652
- if (showdown) {
2653
- if (check_pp == 0) {
2654
- var i: usize = moves.len;
2655
- while (i > 0) {
2656
- i -= 1;
2657
- if (moves[i].id != .None) {
2658
- return battle.rng.range(u4, 0, @intCast(u4, i + 1)) + 1;
2659
- }
2660
- }
2661
- } else {
2662
- var r = battle.rng.range(u4, 0, @intCast(u4, check_pp)) + 1;
2663
- var i: usize = 0;
2664
- while (i < moves.len and r > 0) : (i += 1) {
2665
- if (moves[i].pp > 0) {
2666
- r -= 1;
2667
- if (r == 0) break;
2668
- }
2669
- }
2670
- return @intCast(u4, i + 1);
2847
+ inline fn unboost(battle: anytype, player: Player, options: anytype) !bool {
2848
+ const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
2849
+ val == .true
2850
+ else if (showdown)
2851
+ battle.rng.chance(u8, 85, 256)
2852
+ else
2853
+ battle.rng.next() < Gen12.percent(33) + 1;
2854
+
2855
+ try options.chance.secondaryChance(player, proc, 85);
2856
+ return proc;
2857
+ }
2858
+
2859
+ inline fn metronome(battle: anytype, player: Player, options: anytype) !Move {
2860
+ const move: Move = if (options.calc.overridden(player, .metronome)) |val|
2861
+ val
2862
+ else if (showdown)
2863
+ Move.METRONOME[battle.rng.range(u8, 0, Move.METRONOME.len)]
2864
+ else move: {
2865
+ while (true) {
2866
+ const r = battle.rng.next();
2867
+ if (r == 0 or r == @intFromEnum(Move.Metronome)) continue;
2868
+ if (r >= @intFromEnum(Move.Struggle)) continue;
2869
+ break :move @enumFromInt(r);
2671
2870
  }
2672
- }
2871
+ };
2673
2872
 
2674
- while (true) {
2675
- const r = @intCast(u4, battle.rng.next() & 3);
2676
- if (moves[r].id != .None and (check_pp == 0 or moves[r].pp > 0)) return r + 1;
2677
- }
2873
+ try options.chance.metronome(player, move);
2874
+ return move;
2678
2875
  }
2679
2876
 
2680
- inline fn disableDuration(battle: anytype) u4 {
2681
- if (showdown) return @intCast(u4, battle.rng.range(u8, 1, 9));
2682
- return @intCast(u4, (battle.rng.next() & 7) + 1);
2877
+ inline fn psywave(battle: anytype, player: Player, max: u8, options: anytype) !u8 {
2878
+ const power = if (options.calc.overridden(player, .psywave)) |val|
2879
+ val - 1
2880
+ else if (showdown)
2881
+ battle.rng.range(u8, 0, max)
2882
+ else power: {
2883
+ while (true) {
2884
+ const r = battle.rng.next();
2885
+ if (r < max) break :power r;
2886
+ }
2887
+ };
2888
+
2889
+ assert(power < max);
2890
+ try options.chance.psywave(player, power, max);
2891
+ return power;
2683
2892
  }
2684
2893
 
2685
- inline fn metronome(battle: anytype) Move {
2686
- if (showdown) {
2687
- const r = battle.rng.range(u8, 0, @enumToInt(Move.Struggle) - 2);
2688
- const mod = @as(u2, (if (r < @enumToInt(Move.Metronome) - 1) 1 else 2));
2689
- return @intToEnum(Move, r + mod);
2690
- }
2691
- while (true) {
2692
- const r = battle.rng.next();
2693
- if (r == 0 or r == @enumToInt(Move.Metronome)) continue;
2694
- if (r >= @enumToInt(Move.Struggle)) continue;
2695
- return @intToEnum(Move, r);
2696
- }
2894
+ inline fn sleepDuration(battle: anytype, player: Player, options: anytype) u3 {
2895
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2896
+ @intCast(val)
2897
+ else if (showdown)
2898
+ battle.rng.range(u3, 1, 8)
2899
+ else duration: {
2900
+ while (true) {
2901
+ const r = battle.rng.next() & 7;
2902
+ if (r != 0) break :duration @intCast(r);
2903
+ }
2904
+ };
2905
+
2906
+ assert(duration >= 1 and duration <= 7);
2907
+ options.chance.duration(.sleep, player, player.foe(), duration);
2908
+ return duration;
2697
2909
  }
2698
2910
 
2699
- inline fn psywave(battle: anytype, max: u8) u8 {
2700
- if (showdown) return battle.rng.range(u8, 0, max);
2701
- while (true) {
2702
- const r = battle.rng.next();
2703
- if (r < max) return r;
2704
- }
2911
+ inline fn disableDuration(battle: anytype, player: Player, options: anytype) u4 {
2912
+ const duration: u4 = if (options.calc.overridden(player, .duration)) |val|
2913
+ val
2914
+ else if (showdown)
2915
+ battle.rng.range(u4, 1, 9)
2916
+ else
2917
+ @intCast((battle.rng.next() & 7) + 1);
2918
+
2919
+ assert(duration >= 1 and duration <= 8);
2920
+ options.chance.duration(.disable, player, player.foe(), duration);
2921
+ return duration;
2705
2922
  }
2706
2923
 
2707
- inline fn distribution(battle: anytype) u3 {
2708
- if (showdown) return DISTRIBUTION[battle.rng.range(u8, 0, DISTRIBUTION.len)];
2709
- const r = (battle.rng.next() & 3);
2710
- return @intCast(u3, (if (r < 2) r else battle.rng.next() & 3) + 2);
2924
+ inline fn confusionDuration(battle: anytype, player: Player, self: bool, options: anytype) u3 {
2925
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2926
+ @intCast(val)
2927
+ else if (showdown)
2928
+ battle.rng.range(u3, 2, 6)
2929
+ else
2930
+ @intCast((battle.rng.next() & 3) + 2);
2931
+
2932
+ assert(duration >= 2 and duration <= 5);
2933
+ options.chance.duration(.confusion, player, if (self) player else player.foe(), duration);
2934
+ return duration;
2711
2935
  }
2712
2936
 
2713
- inline fn unboost(battle: anytype) bool {
2714
- if (showdown) return battle.rng.chance(u8, 85, 256);
2715
- return battle.rng.next() < Gen12.percent(33) + 1;
2937
+ inline fn attackingDuration(battle: anytype, player: Player, options: anytype) u3 {
2938
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2939
+ @intCast(val)
2940
+ else if (showdown)
2941
+ battle.rng.range(u3, 2, 4)
2942
+ else
2943
+ @intCast((battle.rng.next() & 1) + 2);
2944
+
2945
+ assert(duration >= 2 and duration <= 3);
2946
+ options.chance.duration(.attacking, player, player, duration);
2947
+ return duration;
2948
+ }
2949
+
2950
+ const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
2951
+
2952
+ inline fn distribution(
2953
+ battle: anytype,
2954
+ comptime effect: Move.Effect,
2955
+ player: Player,
2956
+ options: anytype,
2957
+ ) !u3 {
2958
+ const roll = if (effect == .Binding) .duration else .multi_hit;
2959
+ const n: u3 = if (options.calc.overridden(player, roll)) |val|
2960
+ @intCast(val)
2961
+ else if (showdown)
2962
+ DISTRIBUTION[battle.rng.range(u3, 0, DISTRIBUTION.len)]
2963
+ else n: {
2964
+ const r = (battle.rng.next() & 3);
2965
+ break :n @intCast((if (r < 2) r else battle.rng.next() & 3) + 2);
2966
+ };
2967
+
2968
+ assert(n >= 2 and n <= 5);
2969
+ if (effect == .Binding) {
2970
+ options.chance.duration(.binding, player, player, n);
2971
+ } else {
2972
+ try options.chance.multiHit(player, n);
2973
+ }
2974
+ return n;
2975
+ }
2976
+
2977
+ fn moveSlot(
2978
+ battle: anytype,
2979
+ player: Player,
2980
+ moves: []MoveSlot,
2981
+ check_pp: u4,
2982
+ options: anytype,
2983
+ ) !u4 {
2984
+ // TODO: consider throwing error instead of rerolling?
2985
+ const overridden = if (options.calc.overridden(player, .move_slot)) |val|
2986
+ if (moves[val - 1].id != .None and (check_pp == 0 or moves[val - 1].pp > 0))
2987
+ val
2988
+ else
2989
+ null
2990
+ else
2991
+ null;
2992
+
2993
+ const slot: u4 = overridden orelse slot: {
2994
+ if (showdown) {
2995
+ if (check_pp == 0) {
2996
+ var i: usize = moves.len;
2997
+ while (i > 0) {
2998
+ i -= 1;
2999
+ if (moves[i].id != .None) {
3000
+ break :slot battle.rng.range(u4, 0, @as(u4, @intCast(i + 1))) + 1;
3001
+ }
3002
+ }
3003
+ } else {
3004
+ var r = battle.rng.range(u4, 0, check_pp) + 1;
3005
+ var i: usize = 0;
3006
+ while (i < moves.len and r > 0) : (i += 1) {
3007
+ if (moves[i].pp > 0) {
3008
+ r -= 1;
3009
+ if (r == 0) break :slot @intCast(i + 1);
3010
+ }
3011
+ }
3012
+ }
3013
+ }
3014
+
3015
+ while (true) {
3016
+ const r: u4 = @intCast(battle.rng.next() & 3);
3017
+ if (moves[r].id != .None and (check_pp == 0 or moves[r].pp > 0)) break :slot r + 1;
3018
+ }
3019
+ };
3020
+
3021
+ try options.chance.moveSlot(player, slot, moves, check_pp);
3022
+ return slot;
2716
3023
  }
2717
3024
  };
2718
3025
 
@@ -2720,14 +3027,14 @@ test "RNG agreement" {
2720
3027
  if (!showdown) return;
2721
3028
  var expected: [256]u32 = undefined;
2722
3029
  for (0..expected.len) |i| {
2723
- expected[i] = @intCast(u32, i * 0x1000000);
3030
+ expected[i] = @intCast(i * 0x1000000);
2724
3031
  }
2725
3032
 
2726
- var spe = rng.FixedRNG(1, expected.len){ .rolls = expected };
2727
- var cfz = rng.FixedRNG(1, expected.len){ .rolls = expected };
2728
- var par = rng.FixedRNG(1, expected.len){ .rolls = expected };
2729
- var brn = rng.FixedRNG(1, expected.len){ .rolls = expected };
2730
- var eff = rng.FixedRNG(1, expected.len){ .rolls = expected };
3033
+ var spe: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3034
+ var cfz: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3035
+ var par: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3036
+ var brn: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3037
+ var eff: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
2731
3038
 
2732
3039
  for (0..expected.len) |i| {
2733
3040
  try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
@@ -2763,7 +3070,7 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
2763
3070
  const side = battle.side(player);
2764
3071
  const foe = battle.foe(player);
2765
3072
 
2766
- var active = side.active;
3073
+ var active = &side.active;
2767
3074
  const stored = side.stored();
2768
3075
 
2769
3076
  // While players are not given any input options on the cartridge in these cases,
@@ -2777,7 +3084,7 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
2777
3084
  // strictly correct as the player would not have been presented the option to move or
2778
3085
  // switch at all.
2779
3086
  if (isForced(active)) {
2780
- out[n] = .{ .type = .Move, .data = @boolToInt(showdown) };
3087
+ out[n] = .{ .type = .Move, .data = @intFromBool(showdown) };
2781
3088
  n += 1;
2782
3089
  return n;
2783
3090
  }