@pkmn/engine 0.1.0-dev.e5b5a998 → 0.1.0-dev.f6c1a379

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