@pkmn/engine 0.1.0-dev.dd5b9081 → 0.1.0-dev.dfd46bbb

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.
@@ -7,21 +7,19 @@ const DEBUG = @import("../common/debug.zig").print;
7
7
  const protocol = @import("../common/protocol.zig");
8
8
  const rng = @import("../common/rng.zig");
9
9
 
10
- const Action = @import("chance.zig").Action;
11
-
12
10
  const data = @import("data.zig");
13
11
 
14
12
  const assert = std.debug.assert;
15
13
 
16
14
  const expectEqual = std.testing.expectEqual;
17
15
 
16
+ const showdown = pkmn.options.showdown;
17
+
18
18
  const Choice = common.Choice;
19
19
  const ID = common.ID;
20
20
  const Player = common.Player;
21
21
  const Result = common.Result;
22
22
 
23
- const showdown = pkmn.options.showdown;
24
-
25
23
  const Boost = protocol.Boost;
26
24
  const Damage = protocol.Damage;
27
25
  const Heal = protocol.Heal;
@@ -59,23 +57,15 @@ const BOOSTS = &[_][2]u8{
59
57
 
60
58
  const MAX_STAT_VALUE = 999;
61
59
 
62
- const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
63
-
64
60
  pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result {
65
- // FIXME
66
- // std.debug.print("\n", .{});
67
- // DEBUG(battle.turn);
68
- // std.debug.print("// ", .{});
69
61
  assert(c1.type != .Pass or c2.type != .Pass or battle.turn == 0);
70
62
  if (battle.turn == 0) return start(battle, options);
71
63
 
72
64
  var s1 = false;
73
65
  var s2 = false;
74
- var f1: ?Move = null;
75
- var f2: ?Move = null;
76
66
 
77
- if (selectMove(battle, .P1, c1, c2, &s1, &f1)) |r| return r;
78
- if (selectMove(battle, .P2, c2, c1, &s2, &f2)) |r| return r;
67
+ if (selectMove(battle, .P1, c1, c2, &s1)) |r| return r;
68
+ if (selectMove(battle, .P2, c2, c1, &s2)) |r| return r;
79
69
 
80
70
  var p1 = battle.side(.P1);
81
71
  var p2 = battle.side(.P2);
@@ -84,9 +74,9 @@ pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result
84
74
  var r2 = showdown and p2.active.volatiles.Binding and c1.type == .Switch;
85
75
 
86
76
  if (try turnOrder(battle, c1, c2, options) == .P1) {
87
- if (try doTurn(battle, .P1, c1, r1, s1, f1, .P2, c2, r2, s2, f2, options)) |r| return r;
77
+ if (try doTurn(battle, .P1, c1, r1, s1, .P2, c2, r2, s2, options)) |r| return r;
88
78
  } else {
89
- if (try doTurn(battle, .P2, c2, r2, s2, f2, .P1, c1, r1, s1, f1, options)) |r| return r;
79
+ if (try doTurn(battle, .P2, c2, r2, s2, .P1, c1, r1, s1, options)) |r| return r;
90
80
  }
91
81
 
92
82
  if (p1.active.volatiles.attacks == 0) p1.active.volatiles.Binding = false;
@@ -124,7 +114,6 @@ fn selectMove(
124
114
  choice: Choice,
125
115
  foe_choice: Choice,
126
116
  skip_turn: *bool,
127
- from: *?Move,
128
117
  ) ?Result {
129
118
  if (choice.type == .Pass) return null;
130
119
 
@@ -141,7 +130,6 @@ fn selectMove(
141
130
  return null;
142
131
  }
143
132
  if (volatiles.Rage) {
144
- from.* = side.last_used_move;
145
133
  if (showdown) {
146
134
  if (battle.foe(player).active.volatiles.Binding) skip_turn.* = true;
147
135
  saveMove(battle, player, null);
@@ -151,7 +139,6 @@ fn selectMove(
151
139
  // Pokémon Showdown removes Flinch at the end-of-turn in its residual handler
152
140
  if (!showdown) volatiles.Flinch = false;
153
141
  if (volatiles.Thrashing or volatiles.Charging) {
154
- from.* = side.last_selected_move;
155
142
  if (showdown) {
156
143
  if (battle.foe(player).active.volatiles.Binding) skip_turn.* = true;
157
144
  saveMove(battle, player, null);
@@ -172,9 +159,7 @@ fn selectMove(
172
159
  return null;
173
160
  }
174
161
  if (volatiles.Binding) {
175
- from.* = side.last_used_move;
176
162
  if (showdown) {
177
- if (foe_choice.type == .Switch) from.* = null;
178
163
  // Pokémon Showdown overwrites Mirror Move with whatever was selected - really this
179
164
  // should set side.last_selected_move = last.id to reuse Mirror Move and fail in order
180
165
  // to satisfy the conditions of the Desync Clause Mod. However, because Binding is still
@@ -210,7 +195,7 @@ fn selectMove(
210
195
  if (choice.data == 0) {
211
196
  const struggle = ok: {
212
197
  for (side.active.moves, 0..) |move, i| {
213
- if (move.pp > 0 and volatiles.disabled_move != i + 1) break :ok false;
198
+ if (move.pp > 0 and volatiles.disable_move != i + 1) break :ok false;
214
199
  }
215
200
  break :ok true;
216
201
  };
@@ -229,7 +214,7 @@ fn saveMove(battle: anytype, player: Player, choice: ?Choice) void {
229
214
  if (c.data == 0) {
230
215
  side.last_selected_move = .Struggle;
231
216
  } else {
232
- assert(showdown or side.active.volatiles.disabled_move != c.data);
217
+ assert(showdown or side.active.volatiles.disable_move != c.data);
233
218
  const move = side.active.move(c.data);
234
219
  // You cannot *select* a move with 0 PP (except on Pokémon Showdown where that is
235
220
  // sometimes required...), but a 0 PP move can be used automatically
@@ -340,12 +325,10 @@ fn doTurn(
340
325
  player_choice: Choice,
341
326
  player_rewrap: bool,
342
327
  player_skip: bool,
343
- player_from: ?Move,
344
328
  foe_player: Player,
345
329
  foe_choice: Choice,
346
330
  foe_rewrap: bool,
347
331
  foe_skip: bool,
348
- foe_from: ?Move,
349
332
  options: anytype,
350
333
  ) !?Result {
351
334
  assert(player_choice.type != .Pass);
@@ -358,7 +341,6 @@ fn doTurn(
358
341
  player_choice,
359
342
  player_rewrap,
360
343
  player_skip,
361
- player_from,
362
344
  &residual,
363
345
  options,
364
346
  )) |r| return r;
@@ -381,7 +363,6 @@ fn doTurn(
381
363
  foe_choice,
382
364
  foe_rewrap,
383
365
  foe_skip,
384
- foe_from,
385
366
  &residual,
386
367
  options,
387
368
  )) |r| return r;
@@ -409,7 +390,6 @@ fn executeMove(
409
390
  choice: Choice,
410
391
  rewrap: bool,
411
392
  skip: bool,
412
- from: ?Move,
413
393
  residual: *bool,
414
394
  options: anytype,
415
395
  ) !?Result {
@@ -444,8 +424,10 @@ fn executeMove(
444
424
  mslot = @intCast(battle.lastMove(player).index);
445
425
  const stored = side.stored();
446
426
  // GLITCH: Struggle bypass PP underflow via Hyper Beam / Trapping-switch auto selection
447
- auto = side.last_selected_move == .HyperBeam or side.active.volatiles.Bide or
448
- from != null or Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP);
427
+ auto = isForced(&side.active) or
428
+ side.active.volatiles.Binding or side.active.volatiles.Bide or
429
+ side.last_selected_move == .HyperBeam or
430
+ Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP);
449
431
  // If it wasn't Hyper Beam or the continuation of a move effect then we must have just
450
432
  // thawed, in which case we will desync unless the last_selected_move happened to be at
451
433
  // index 1 and the current Pokémon has the same move in its first slot.
@@ -467,7 +449,7 @@ fn executeMove(
467
449
 
468
450
  var skip_can = false;
469
451
  var skip_pp = false;
470
- switch (try beforeMove(battle, player, skip, from, residual, options)) {
452
+ switch (try beforeMove(battle, player, skip, residual, options)) {
471
453
  .done => return null,
472
454
  .skip_can => skip_can = true,
473
455
  .skip_pp => skip_pp = true,
@@ -475,9 +457,9 @@ fn executeMove(
475
457
  .err => return @as(?Result, Result.Error),
476
458
  }
477
459
 
478
- if (!skip_can and !try canMove(battle, player, mslot, auto, skip_pp, from, residual, options)) {
479
- return null;
480
- }
460
+ const can = skip_can or
461
+ try canMove(battle, player, mslot, auto, skip_pp, .None, residual, options);
462
+ if (!can) return null;
481
463
 
482
464
  return doMove(battle, player, mslot, rewrap, auto, residual, options);
483
465
  }
@@ -488,7 +470,6 @@ fn beforeMove(
488
470
  battle: anytype,
489
471
  player: Player,
490
472
  skip: bool,
491
- from: ?Move,
492
473
  residual: *bool,
493
474
  options: anytype,
494
475
  ) !BeforeMove {
@@ -549,16 +530,16 @@ fn beforeMove(
549
530
  return .done;
550
531
  }
551
532
 
552
- if (volatiles.disabled_duration > 0) {
533
+ if (volatiles.disable_duration > 0) {
553
534
  if (options.calc.modify(player, .disable)) |extend| {
554
- if (!extend) volatiles.disabled_duration = 0;
535
+ if (!extend) volatiles.disable_duration = 0;
555
536
  } else {
556
- volatiles.disabled_duration -= 1;
537
+ volatiles.disable_duration -= 1;
557
538
  }
558
- try options.chance.disable(player, volatiles.disabled_duration);
539
+ try options.chance.disable(player, volatiles.disable_duration);
559
540
 
560
- if (volatiles.disabled_duration == 0) {
561
- volatiles.disabled_move = 0;
541
+ if (volatiles.disable_duration == 0) {
542
+ volatiles.disable_move = 0;
562
543
  try log.end(ident, .Disable);
563
544
  }
564
545
  }
@@ -695,7 +676,6 @@ fn beforeMove(
695
676
 
696
677
  if (volatiles.Thrashing) {
697
678
  assert(volatiles.attacks > 0);
698
- assert(from != null);
699
679
 
700
680
  if (options.calc.modify(player, .attacking)) |extend| {
701
681
  if (!extend) volatiles.attacks = 0;
@@ -707,7 +687,7 @@ fn beforeMove(
707
687
  if (!showdown and handleThrashing(battle, active, player, options)) {
708
688
  try log.start(battle.active(player), .ConfusionSilent);
709
689
  }
710
- try log.move(ident, side.last_selected_move, battle.active(player.foe()), from);
690
+ try log.move(ident, side.last_selected_move, battle.active(player.foe()));
711
691
  if (showdown) {
712
692
  // This shouldn't actually set last_used_move, but Pokémon Showdown sets last
713
693
  // used in useMove and doesn't have the notion of skipping canMove semantics
@@ -729,12 +709,7 @@ fn beforeMove(
729
709
  }
730
710
  try options.chance.binding(player, volatiles.attacks);
731
711
 
732
- try log.move(
733
- battle.active(player),
734
- side.last_selected_move,
735
- battle.active(player.foe()),
736
- side.last_selected_move,
737
- );
712
+ try log.move(battle.active(player), side.last_selected_move, battle.active(player.foe()));
738
713
  if (showdown or battle.last_damage != 0) {
739
714
  const sub = showdown and foe.active.volatiles.Substitute;
740
715
  _ = try applyDamage(battle, player.foe(), player.foe(), .None, options);
@@ -754,7 +729,7 @@ fn canMove(
754
729
  mslot: u4,
755
730
  auto: bool,
756
731
  skip_pp: bool,
757
- from: ?Move,
732
+ from: Move,
758
733
  residual: *bool,
759
734
  options: anytype,
760
735
  ) !bool {
@@ -762,33 +737,22 @@ fn canMove(
762
737
  const player_ident = battle.active(player);
763
738
  const move = Move.get(side.last_selected_move);
764
739
 
765
- const foe_last = battle.foe(player).last_used_move;
766
- const special = from != null and (from.? == .Metronome and
767
- (from.? == .MirrorMove and !(foe_last == .None or foe_last == .MirrorMove)));
768
-
769
740
  var skip = skip_pp;
770
741
  if (side.active.volatiles.Charging) {
771
742
  side.active.volatiles.Charging = false;
772
743
  side.active.volatiles.Invulnerable = false;
773
744
  } else if (move.effect == .Charge) {
774
- try options.log.move(player_ident, side.last_selected_move, .{}, from);
745
+ try options.log.moveFrom(player_ident, side.last_selected_move, .{}, from);
775
746
  setCounterable(battle, player, side, move);
776
747
  try Effects.charge(battle, player, options);
777
748
  return false;
778
749
  }
779
750
 
780
- if (!showdown or !special) side.last_used_move = side.last_selected_move;
751
+ side.last_used_move = side.last_selected_move;
781
752
  if (!skip) decrementPP(side, mslot, auto);
782
753
 
783
754
  const target = if (move.target == .Self) player else player.foe();
784
- // Pokémon Showdown's protocol for Rage and Binding moves should come with a [from], though we
785
- // don't have space to be able to track it in all circumstances and thus usually try to infer it
786
- // from side.last_used_move. However, this gets reset by the opponent switching or fainting,
787
- // meaning sometimes we lose this information. from is still set because it not being null is
788
- // relevant for control flow, but we don't have anything useful to put in the log. This is
789
- // regrettable, but this information is more "nice to have" than required
790
- const f = if (from != null and from.? == .None) null else from;
791
- try options.log.move(player_ident, side.last_selected_move, battle.active(target), f);
755
+ try options.log.moveFrom(player_ident, side.last_selected_move, battle.active(target), from);
792
756
  setCounterable(battle, player, side, move);
793
757
 
794
758
  if (move.effect.onBegin()) {
@@ -826,12 +790,14 @@ fn decrementPP(side: *Side, mslot: u4, auto: bool) void {
826
790
  assert(!volatiles.Rage and !volatiles.Thrashing and !volatiles.MultiHit);
827
791
  if (volatiles.Bide) return;
828
792
 
829
- assert(active.move(mslot).pp > 0 or auto);
830
- active.move(mslot).pp = @as(u6, @intCast(active.move(mslot).pp)) -% 1;
793
+ var move_slot = active.move(mslot);
794
+ assert(move_slot.pp > 0 or auto);
795
+ move_slot.pp = @as(u6, @intCast(move_slot.pp)) -% 1;
831
796
  if (volatiles.Transform) return;
832
797
 
833
- assert(side.stored().move(mslot).pp > 0 or auto);
834
- side.stored().move(mslot).pp = @as(u6, @intCast(side.stored().move(mslot).pp)) -% 1;
798
+ move_slot = side.stored().move(mslot);
799
+ assert(move_slot.pp > 0 or auto);
800
+ move_slot.pp = @as(u6, @intCast(move_slot.pp)) -% 1;
835
801
  assert(active.move(mslot).pp == side.stored().move(mslot).pp);
836
802
  }
837
803
 
@@ -1474,9 +1440,8 @@ fn checkFaint(
1474
1440
  const foe_fainted = foe.stored().hp == 0;
1475
1441
 
1476
1442
  const player_out = findFirstAlive(side) == 0;
1477
- const foe_out = findFirstAlive(foe) == 0;
1478
- const tie = player_out and foe_out;
1479
- const more = tie or player_out or foe_out;
1443
+ const foe_out = foe_fainted and findFirstAlive(foe) == 0;
1444
+ const more = player_out or foe_out;
1480
1445
 
1481
1446
  if (try faint(battle, player, !(more or foe_fainted), options)) |r| return r;
1482
1447
  if (foe_fainted) if (try faint(battle, player.foe(), !more, options)) |r| return r;
@@ -1484,7 +1449,7 @@ fn checkFaint(
1484
1449
  assert(!side.active.volatiles.MultiHit);
1485
1450
  assert(!foe.active.volatiles.MultiHit);
1486
1451
 
1487
- if (tie) {
1452
+ if (player_out and foe_out) {
1488
1453
  try options.log.tie();
1489
1454
  return Result.Tie;
1490
1455
  } else if (player_out) {
@@ -1496,8 +1461,8 @@ fn checkFaint(
1496
1461
  }
1497
1462
 
1498
1463
  const foe_choice: Choice.Type = if (foe_fainted) .Switch else .Pass;
1499
- if (player == .P1) return Result{ .p1 = .Switch, .p2 = foe_choice };
1500
- return Result{ .p1 = foe_choice, .p2 = .Switch };
1464
+ if (player == .P1) return .{ .p1 = .Switch, .p2 = foe_choice };
1465
+ return .{ .p1 = foe_choice, .p2 = .Switch };
1501
1466
  }
1502
1467
 
1503
1468
  fn faint(battle: anytype, player: Player, done: bool, options: anytype) !?Result {
@@ -1670,9 +1635,9 @@ fn checkEBC(battle: anytype) bool {
1670
1635
  }
1671
1636
 
1672
1637
  fn disabled(side: *Side, ident: ID, options: anytype) !bool {
1673
- if (side.active.volatiles.disabled_move != 0) {
1638
+ if (side.active.volatiles.disable_move != 0) {
1674
1639
  // A Pokémon that transforms after being disabled may end up with less move slots
1675
- const m = side.active.moves[side.active.volatiles.disabled_move - 1].id;
1640
+ const m = side.active.moves[side.active.volatiles.disable_move - 1].id;
1676
1641
  // side.last_selected_move can be Struggle here on Pokemon Showdown we need an extra check
1677
1642
  const last = if (showdown and m == .Bide and side.active.volatiles.Bide)
1678
1643
  m
@@ -1687,7 +1652,7 @@ fn disabled(side: *Side, ident: ID, options: anytype) !bool {
1687
1652
  return false;
1688
1653
  }
1689
1654
 
1690
- inline fn buildRage(battle: anytype, who: Player, options: anytype) !void {
1655
+ fn buildRage(battle: anytype, who: Player, options: anytype) !void {
1691
1656
  const side = battle.side(who);
1692
1657
  if (side.active.volatiles.Rage and side.active.boosts.atk < 6) {
1693
1658
  try Effects.boost(battle, who, Move.get(.Rage), options);
@@ -1705,7 +1670,7 @@ fn handleThrashing(battle: anytype, active: *ActivePokemon, player: Player, opti
1705
1670
  return true;
1706
1671
  }
1707
1672
 
1708
- inline fn onBegin(
1673
+ fn onBegin(
1709
1674
  battle: anytype,
1710
1675
  player: Player,
1711
1676
  move: Move.Data,
@@ -1735,7 +1700,7 @@ inline fn onBegin(
1735
1700
  };
1736
1701
  }
1737
1702
 
1738
- inline fn onEnd(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1703
+ fn onEnd(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1739
1704
  assert(move.effect.onEnd());
1740
1705
  return switch (move.effect) {
1741
1706
  .Bide => Effects.bide(battle, player, options),
@@ -1751,7 +1716,7 @@ inline fn onEnd(battle: anytype, player: Player, move: Move.Data, options: anyty
1751
1716
  };
1752
1717
  }
1753
1718
 
1754
- inline fn alwaysHappens(
1719
+ fn alwaysHappens(
1755
1720
  battle: anytype,
1756
1721
  player: Player,
1757
1722
  move: Move.Data,
@@ -1770,7 +1735,7 @@ inline fn alwaysHappens(
1770
1735
  };
1771
1736
  }
1772
1737
 
1773
- inline fn moveEffect(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1738
+ fn moveEffect(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1774
1739
  return switch (move.effect) {
1775
1740
  .BurnChance1, .BurnChance2 => Effects.burnChance(battle, player, move, options),
1776
1741
  .ConfusionChance => Effects.confusion(battle, player, move, options),
@@ -1900,7 +1865,7 @@ pub const Effects = struct {
1900
1865
  try options.chance.commit(player, .hit);
1901
1866
  }
1902
1867
 
1903
- if (volatiles.disabled_move != 0) {
1868
+ if (volatiles.disable_move != 0) {
1904
1869
  try options.log.fail(foe_ident, .None);
1905
1870
  return null;
1906
1871
  }
@@ -1925,18 +1890,17 @@ pub const Effects = struct {
1925
1890
  return Result.Error;
1926
1891
  }
1927
1892
 
1928
- volatiles.disabled_move =
1893
+ volatiles.disable_move =
1929
1894
  @intCast(try Rolls.moveSlot(battle, player, &foe.active.moves, n, options));
1930
- volatiles.disabled_duration = Rolls.disableDuration(battle, player, options);
1895
+ volatiles.disable_duration = Rolls.disableDuration(battle, player, options);
1931
1896
 
1932
- const id = foe.active.move(volatiles.disabled_move).id;
1897
+ const id = foe.active.move(volatiles.disable_move).id;
1933
1898
  try options.log.startEffect(foe_ident, .Disable, id);
1934
1899
  return null;
1935
1900
  }
1936
1901
 
1937
1902
  fn drainHP(battle: anytype, player: Player, options: anytype) !void {
1938
- var side = battle.side(player);
1939
- var stored = side.stored();
1903
+ var stored = battle.side(player).stored();
1940
1904
 
1941
1905
  if (battle.last_damage == 0) {
1942
1906
  assert(showdown);
@@ -2061,7 +2025,7 @@ pub const Effects = struct {
2061
2025
  // GLITCH: HP recovery move failure glitches
2062
2026
  const delta = stored.stats.hp - stored.hp;
2063
2027
  if (delta == 0 or (delta & 255 == 255 and stored.hp % 256 != 0)) {
2064
- return try options.log.fail(ident, .None);
2028
+ return options.log.fail(ident, .None);
2065
2029
  }
2066
2030
 
2067
2031
  const rest = side.last_selected_move == .Rest;
@@ -2143,7 +2107,7 @@ pub const Effects = struct {
2143
2107
  // Invulnerable foes or 1/256 miss can trigger |-miss| instead of |-fail|
2144
2108
  if (!try checkHit(battle, player, move, options)) return;
2145
2109
  try options.chance.commit(player, .hit);
2146
- return try options.log.fail(battle.active(player.foe()), .None);
2110
+ return options.log.fail(battle.active(player.foe()), .None);
2147
2111
  }
2148
2112
  }
2149
2113
  if (!try checkHit(battle, player, move, options)) return;
@@ -2361,8 +2325,7 @@ pub const Effects = struct {
2361
2325
  fn substitute(battle: anytype, player: Player, residual: *bool, options: anytype) !void {
2362
2326
  var side = battle.side(player);
2363
2327
  if (side.active.volatiles.Substitute) {
2364
- try options.log.fail(battle.active(player), .Substitute);
2365
- return;
2328
+ return options.log.fail(battle.active(player), .Substitute);
2366
2329
  }
2367
2330
 
2368
2331
  assert(side.stored().stats.hp <= 1023);
@@ -2373,10 +2336,7 @@ pub const Effects = struct {
2373
2336
  // the target's HP is exactly divisible by 4 (here we're using an inlined divCeil routine to
2374
2337
  // avoid having to convert to floating point)
2375
2338
  const required_hp = if (showdown) @divFloor(side.stored().stats.hp - 1, 4) + 1 else hp;
2376
- if (side.stored().hp < required_hp) {
2377
- try options.log.fail(battle.active(player), .Weak);
2378
- return;
2379
- }
2339
+ if (side.stored().hp < required_hp) return options.log.fail(battle.active(player), .Weak);
2380
2340
 
2381
2341
  // GLITCH: can leave the user with 0 HP (faints later) because didn't check '<=' above
2382
2342
  side.stored().hp -= hp;
@@ -2468,7 +2428,7 @@ pub const Effects = struct {
2468
2428
  switch (move.effect) {
2469
2429
  .AttackUp1, .AttackUp2, .Rage => {
2470
2430
  assert(boosts.atk >= -6 and boosts.atk <= 6);
2471
- if (boosts.atk == 6) return try log.fail(ident, .None);
2431
+ if (boosts.atk == 6) return log.fail(ident, .None);
2472
2432
  const n: u2 = if (move.effect == .AttackUp2) 2 else 1;
2473
2433
  boosts.atk = @as(i4, @intCast(@min(6, @as(i8, boosts.atk) + n)));
2474
2434
  const reason = if (move.effect == .Rage) Boost.Rage else Boost.Attack;
@@ -2479,7 +2439,7 @@ pub const Effects = struct {
2479
2439
  try log.boost(ident, Boost.Attack, -1);
2480
2440
  if (move.effect == .Rage) return;
2481
2441
  }
2482
- return try log.fail(ident, .None);
2442
+ return log.fail(ident, .None);
2483
2443
  }
2484
2444
  var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.atk) + 6))];
2485
2445
  const stat = unmodifiedStats(battle, side).atk;
@@ -2490,7 +2450,7 @@ pub const Effects = struct {
2490
2450
  },
2491
2451
  .DefenseUp1, .DefenseUp2 => {
2492
2452
  assert(boosts.def >= -6 and boosts.def <= 6);
2493
- if (boosts.def == 6) return try log.fail(ident, .None);
2453
+ if (boosts.def == 6) return log.fail(ident, .None);
2494
2454
  const n: u2 = if (move.effect == .DefenseUp2) 2 else 1;
2495
2455
  boosts.def = @intCast(@min(6, @as(i8, boosts.def) + n));
2496
2456
  if (stats.def == MAX_STAT_VALUE) {
@@ -2499,7 +2459,7 @@ pub const Effects = struct {
2499
2459
  try log.boost(ident, .Defense, n);
2500
2460
  try log.boost(ident, .Defense, -1);
2501
2461
  }
2502
- return try log.fail(ident, .None);
2462
+ return log.fail(ident, .None);
2503
2463
  }
2504
2464
  var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.def) + 6))];
2505
2465
  const stat = unmodifiedStats(battle, side).def;
@@ -2508,7 +2468,7 @@ pub const Effects = struct {
2508
2468
  },
2509
2469
  .SpeedUp2 => {
2510
2470
  assert(boosts.spe >= -6 and boosts.spe <= 6);
2511
- if (boosts.spe == 6) return try log.fail(ident, .None);
2471
+ if (boosts.spe == 6) return log.fail(ident, .None);
2512
2472
  boosts.spe = @intCast(@min(6, @as(i8, boosts.spe) + 2));
2513
2473
  if (stats.spe == MAX_STAT_VALUE) {
2514
2474
  boosts.spe -= 1;
@@ -2516,7 +2476,7 @@ pub const Effects = struct {
2516
2476
  try log.boost(ident, .Speed, 2);
2517
2477
  try log.boost(ident, .Speed, -1);
2518
2478
  }
2519
- return try log.fail(ident, .None);
2479
+ return log.fail(ident, .None);
2520
2480
  }
2521
2481
  var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.spe) + 6))];
2522
2482
  const stat = unmodifiedStats(battle, side).spe;
@@ -2525,7 +2485,7 @@ pub const Effects = struct {
2525
2485
  },
2526
2486
  .SpecialUp1, .SpecialUp2 => {
2527
2487
  assert(boosts.spc >= -6 and boosts.spc <= 6);
2528
- if (boosts.spc == 6) return try log.fail(ident, .None);
2488
+ if (boosts.spc == 6) return log.fail(ident, .None);
2529
2489
  const n: u2 = if (move.effect == .SpecialUp2) 2 else 1;
2530
2490
  boosts.spc = @intCast(@min(6, @as(i8, boosts.spc) + n));
2531
2491
  if (stats.spc == MAX_STAT_VALUE) {
@@ -2536,7 +2496,7 @@ pub const Effects = struct {
2536
2496
  try log.boost(ident, .SpecialDefense, n);
2537
2497
  try log.boost(ident, .SpecialDefense, -1);
2538
2498
  }
2539
- return try log.fail(ident, .None);
2499
+ return log.fail(ident, .None);
2540
2500
  }
2541
2501
  var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.spc) + 6))];
2542
2502
  const stat = unmodifiedStats(battle, side).spc;
@@ -2546,7 +2506,7 @@ pub const Effects = struct {
2546
2506
  },
2547
2507
  .EvasionUp1 => {
2548
2508
  assert(boosts.evasion >= -6 and boosts.evasion <= 6);
2549
- if (boosts.evasion == 6) return try log.fail(ident, .None);
2509
+ if (boosts.evasion == 6) return log.fail(ident, .None);
2550
2510
  boosts.evasion = @intCast(@min(6, @as(i8, boosts.evasion) + 1));
2551
2511
  try log.boost(ident, .Evasion, 1);
2552
2512
  },
@@ -2589,7 +2549,7 @@ pub const Effects = struct {
2589
2549
  try log.boost(foe_ident, .Attack, -1);
2590
2550
  try log.boost(foe_ident, .Attack, 1);
2591
2551
  }
2592
- return try log.fail(foe_ident, .None);
2552
+ return log.fail(foe_ident, .None);
2593
2553
  }
2594
2554
  var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.atk) + 6))];
2595
2555
  const stat = unmodifiedStats(battle, foe).atk;
@@ -2607,7 +2567,7 @@ pub const Effects = struct {
2607
2567
  try log.boost(foe_ident, .Defense, -@as(i8, n));
2608
2568
  try log.boost(foe_ident, .Defense, 1);
2609
2569
  }
2610
- return try log.fail(foe_ident, .None);
2570
+ return log.fail(foe_ident, .None);
2611
2571
  }
2612
2572
  var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.def) + 6))];
2613
2573
  const stat = unmodifiedStats(battle, foe).def;
@@ -2624,7 +2584,7 @@ pub const Effects = struct {
2624
2584
  try log.boost(foe_ident, .Speed, -1);
2625
2585
  try log.boost(foe_ident, .Speed, 1);
2626
2586
  }
2627
- return try log.fail(foe_ident, .None);
2587
+ return log.fail(foe_ident, .None);
2628
2588
  }
2629
2589
  var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.spe) + 6))];
2630
2590
  const stat = unmodifiedStats(battle, foe).spe;
@@ -2644,7 +2604,7 @@ pub const Effects = struct {
2644
2604
  try log.boost(foe_ident, .SpecialDefense, -1);
2645
2605
  try log.boost(foe_ident, .SpecialDefense, 1);
2646
2606
  }
2647
- return try log.fail(foe_ident, .None);
2607
+ return log.fail(foe_ident, .None);
2648
2608
  }
2649
2609
  var mod = BOOSTS[@as(u4, @intCast(@as(i8, boosts.spc) + 6))];
2650
2610
  const stat = unmodifiedStats(battle, foe).spc;
@@ -2680,7 +2640,7 @@ fn statusModify(status: u8, stats: *Stats(u16)) void {
2680
2640
  }
2681
2641
  }
2682
2642
 
2683
- inline fn isForced(active: anytype) bool {
2643
+ fn isForced(active: anytype) bool {
2684
2644
  return active.volatiles.Recharging or active.volatiles.Rage or
2685
2645
  active.volatiles.Thrashing or active.volatiles.Charging;
2686
2646
  }
@@ -2693,9 +2653,9 @@ fn clearVolatiles(battle: anytype, who: Player, clear: bool, options: anytype) !
2693
2653
 
2694
2654
  options.chance.clearDurations(who, clear);
2695
2655
 
2696
- if (volatiles.disabled_move != 0) {
2697
- volatiles.disabled_move = 0;
2698
- volatiles.disabled_duration = 0;
2656
+ if (volatiles.disable_move != 0) {
2657
+ volatiles.disable_move = 0;
2658
+ volatiles.disable_duration = 0;
2699
2659
  try log.end(ident, .DisableSilent);
2700
2660
  }
2701
2661
  if (volatiles.Confusion) {
@@ -2705,15 +2665,15 @@ fn clearVolatiles(battle: anytype, who: Player, clear: bool, options: anytype) !
2705
2665
  }
2706
2666
  if (volatiles.Mist) {
2707
2667
  volatiles.Mist = false;
2708
- try log.end(ident, .Mist);
2668
+ try log.end(ident, .MistSilent);
2709
2669
  }
2710
2670
  if (volatiles.FocusEnergy) {
2711
2671
  volatiles.FocusEnergy = false;
2712
- try log.end(ident, .FocusEnergy);
2672
+ try log.end(ident, .FocusEnergySilent);
2713
2673
  }
2714
2674
  if (volatiles.LeechSeed) {
2715
2675
  volatiles.LeechSeed = false;
2716
- try log.end(ident, .LeechSeed);
2676
+ try log.end(ident, .LeechSeedSilent);
2717
2677
  }
2718
2678
  if (!showdown and volatiles.Toxic) {
2719
2679
  volatiles.Toxic = false;
@@ -2721,22 +2681,22 @@ fn clearVolatiles(battle: anytype, who: Player, clear: bool, options: anytype) !
2721
2681
  }
2722
2682
  if (volatiles.LightScreen) {
2723
2683
  volatiles.LightScreen = false;
2724
- try log.end(ident, .LightScreen);
2684
+ try log.end(ident, .LightScreenSilent);
2725
2685
  }
2726
2686
  if (volatiles.Reflect) {
2727
2687
  volatiles.Reflect = false;
2728
- try log.end(ident, .Reflect);
2688
+ try log.end(ident, .ReflectSilent);
2729
2689
  }
2730
2690
  if (showdown and volatiles.Toxic) {
2731
2691
  volatiles.Toxic = false;
2732
2692
  // Pokémon Showdown erroneously clears the toxic counter
2733
2693
  volatiles.toxic = 0;
2734
- try log.end(ident, .Toxic);
2694
+ try log.end(ident, .ToxicSilent);
2735
2695
  }
2736
2696
  }
2737
2697
 
2738
2698
  pub const Rolls = struct {
2739
- inline fn speedTie(battle: anytype, options: anytype) !bool {
2699
+ fn speedTie(battle: anytype, options: anytype) !bool {
2740
2700
  const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
2741
2701
  player == .P1
2742
2702
  else if (showdown)
@@ -2748,7 +2708,7 @@ pub const Rolls = struct {
2748
2708
  return p1;
2749
2709
  }
2750
2710
 
2751
- inline fn criticalHit(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
2711
+ fn criticalHit(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
2752
2712
  const crit = if (options.calc.overridden(player, .critical_hit)) |val|
2753
2713
  val == .true
2754
2714
  else if (showdown)
@@ -2760,7 +2720,7 @@ pub const Rolls = struct {
2760
2720
  return crit;
2761
2721
  }
2762
2722
 
2763
- inline fn damage(battle: anytype, player: anytype, options: anytype) !u8 {
2723
+ fn damage(battle: anytype, player: Player, options: anytype) !u8 {
2764
2724
  const roll = if (options.calc.overridden(player, .damage)) |val|
2765
2725
  val
2766
2726
  else roll: {
@@ -2776,7 +2736,7 @@ pub const Rolls = struct {
2776
2736
  return roll;
2777
2737
  }
2778
2738
 
2779
- inline fn hit(battle: anytype, player: Player, accuracy: u8, options: anytype) bool {
2739
+ fn hit(battle: anytype, player: Player, accuracy: u8, options: anytype) bool {
2780
2740
  const ok = if (options.calc.overridden(player, .hit)) |val|
2781
2741
  val == .true
2782
2742
  else if (showdown)
@@ -2788,7 +2748,7 @@ pub const Rolls = struct {
2788
2748
  return ok;
2789
2749
  }
2790
2750
 
2791
- inline fn confused(battle: anytype, player: Player, options: anytype) !bool {
2751
+ fn confused(battle: anytype, player: Player, options: anytype) !bool {
2792
2752
  const cfz = if (options.calc.overridden(player, .confused)) |val|
2793
2753
  val == .true
2794
2754
  else if (showdown)
@@ -2800,7 +2760,7 @@ pub const Rolls = struct {
2800
2760
  return cfz;
2801
2761
  }
2802
2762
 
2803
- inline fn paralyzed(battle: anytype, player: Player, options: anytype) !bool {
2763
+ fn paralyzed(battle: anytype, player: Player, options: anytype) !bool {
2804
2764
  const par = if (options.calc.overridden(player, .paralyzed)) |val|
2805
2765
  val == .true
2806
2766
  else if (showdown)
@@ -2812,7 +2772,7 @@ pub const Rolls = struct {
2812
2772
  return par;
2813
2773
  }
2814
2774
 
2815
- inline fn confusionChance(battle: anytype, player: Player, options: anytype) !bool {
2775
+ fn confusionChance(battle: anytype, player: Player, options: anytype) !bool {
2816
2776
  const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
2817
2777
  val == .true
2818
2778
  else if (showdown)
@@ -2824,7 +2784,7 @@ pub const Rolls = struct {
2824
2784
  return proc;
2825
2785
  }
2826
2786
 
2827
- inline fn secondaryChance(battle: anytype, player: Player, low: bool, options: anytype) !bool {
2787
+ fn secondaryChance(battle: anytype, player: Player, low: bool, options: anytype) !bool {
2828
2788
  const rate: u8 = if (low) 26 else 77;
2829
2789
 
2830
2790
  const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
@@ -2838,7 +2798,7 @@ pub const Rolls = struct {
2838
2798
  return proc;
2839
2799
  }
2840
2800
 
2841
- inline fn poisonChance(battle: anytype, player: Player, low: bool, options: anytype) !bool {
2801
+ fn poisonChance(battle: anytype, player: Player, low: bool, options: anytype) !bool {
2842
2802
  const rate: u8 = if (low) 52 else 103;
2843
2803
 
2844
2804
  const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
@@ -2852,7 +2812,7 @@ pub const Rolls = struct {
2852
2812
  return proc;
2853
2813
  }
2854
2814
 
2855
- inline fn unboost(battle: anytype, player: Player, options: anytype) !bool {
2815
+ fn unboost(battle: anytype, player: Player, options: anytype) !bool {
2856
2816
  const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
2857
2817
  val == .true
2858
2818
  else if (showdown)
@@ -2864,14 +2824,12 @@ pub const Rolls = struct {
2864
2824
  return proc;
2865
2825
  }
2866
2826
 
2867
- inline fn metronome(battle: anytype, player: Player, options: anytype) !Move {
2827
+ fn metronome(battle: anytype, player: Player, options: anytype) !Move {
2868
2828
  const move: Move = if (options.calc.overridden(player, .metronome)) |val|
2869
2829
  val
2870
- else if (showdown) move: {
2871
- const r = battle.rng.range(u8, 0, @intFromEnum(Move.Struggle) - 2);
2872
- const mod = @as(u2, (if (r < @intFromEnum(Move.Metronome) - 1) 1 else 2));
2873
- break :move @enumFromInt(r + mod);
2874
- } else move: {
2830
+ else if (showdown)
2831
+ Move.METRONOME[battle.rng.range(u8, 0, Move.METRONOME.len)]
2832
+ else move: {
2875
2833
  while (true) {
2876
2834
  const r = battle.rng.next();
2877
2835
  if (r == 0 or r == @intFromEnum(Move.Metronome)) continue;
@@ -2884,7 +2842,7 @@ pub const Rolls = struct {
2884
2842
  return move;
2885
2843
  }
2886
2844
 
2887
- inline fn psywave(battle: anytype, player: Player, max: u8, options: anytype) !u8 {
2845
+ fn psywave(battle: anytype, player: Player, max: u8, options: anytype) !u8 {
2888
2846
  const power = if (options.calc.overridden(player, .psywave)) |val|
2889
2847
  val - 1
2890
2848
  else if (showdown)
@@ -2901,11 +2859,11 @@ pub const Rolls = struct {
2901
2859
  return power;
2902
2860
  }
2903
2861
 
2904
- inline fn sleepDuration(battle: anytype, player: Player, options: anytype) u3 {
2862
+ fn sleepDuration(battle: anytype, player: Player, options: anytype) u3 {
2905
2863
  const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2906
2864
  @intCast(val)
2907
2865
  else if (showdown)
2908
- @intCast(battle.rng.range(u8, 1, 8))
2866
+ battle.rng.range(u3, 1, 8)
2909
2867
  else duration: {
2910
2868
  while (true) {
2911
2869
  const r = battle.rng.next() & 7;
@@ -2918,11 +2876,11 @@ pub const Rolls = struct {
2918
2876
  return duration;
2919
2877
  }
2920
2878
 
2921
- inline fn disableDuration(battle: anytype, player: Player, options: anytype) u4 {
2879
+ fn disableDuration(battle: anytype, player: Player, options: anytype) u4 {
2922
2880
  const duration: u4 = if (options.calc.overridden(player, .duration)) |val|
2923
2881
  val
2924
2882
  else if (showdown)
2925
- @intCast(battle.rng.range(u8, 1, 9))
2883
+ battle.rng.range(u4, 1, 9)
2926
2884
  else
2927
2885
  @intCast((battle.rng.next() & 7) + 1);
2928
2886
 
@@ -2931,11 +2889,11 @@ pub const Rolls = struct {
2931
2889
  return duration;
2932
2890
  }
2933
2891
 
2934
- inline fn confusionDuration(battle: anytype, player: Player, self: bool, options: anytype) u3 {
2892
+ fn confusionDuration(battle: anytype, player: Player, self: bool, options: anytype) u3 {
2935
2893
  const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2936
2894
  @intCast(val)
2937
2895
  else if (showdown)
2938
- @intCast(battle.rng.range(u8, 2, 6))
2896
+ battle.rng.range(u3, 2, 6)
2939
2897
  else
2940
2898
  @intCast((battle.rng.next() & 3) + 2);
2941
2899
 
@@ -2944,11 +2902,11 @@ pub const Rolls = struct {
2944
2902
  return duration;
2945
2903
  }
2946
2904
 
2947
- inline fn attackingDuration(battle: anytype, player: Player, options: anytype) u3 {
2905
+ fn attackingDuration(battle: anytype, player: Player, options: anytype) u3 {
2948
2906
  const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2949
2907
  @intCast(val)
2950
2908
  else if (showdown)
2951
- @intCast(battle.rng.range(u4, 2, 4))
2909
+ battle.rng.range(u3, 2, 4)
2952
2910
  else
2953
2911
  @intCast((battle.rng.next() & 1) + 2);
2954
2912
 
@@ -2957,7 +2915,9 @@ pub const Rolls = struct {
2957
2915
  return duration;
2958
2916
  }
2959
2917
 
2960
- inline fn distribution(
2918
+ const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
2919
+
2920
+ fn distribution(
2961
2921
  battle: anytype,
2962
2922
  comptime effect: Move.Effect,
2963
2923
  player: Player,
@@ -2967,7 +2927,7 @@ pub const Rolls = struct {
2967
2927
  const n: u3 = if (options.calc.overridden(player, roll)) |val|
2968
2928
  @intCast(val)
2969
2929
  else if (showdown)
2970
- DISTRIBUTION[battle.rng.range(u8, 0, DISTRIBUTION.len)]
2930
+ DISTRIBUTION[battle.rng.range(u3, 0, DISTRIBUTION.len)]
2971
2931
  else n: {
2972
2932
  const r = (battle.rng.next() & 3);
2973
2933
  break :n @intCast((if (r < 2) r else battle.rng.next() & 3) + 2);
@@ -3014,10 +2974,9 @@ pub const Rolls = struct {
3014
2974
  while (i < moves.len and r > 0) : (i += 1) {
3015
2975
  if (moves[i].pp > 0) {
3016
2976
  r -= 1;
3017
- if (r == 0) break;
2977
+ if (r == 0) break :slot @intCast(i + 1);
3018
2978
  }
3019
2979
  }
3020
- break :slot @intCast(i + 1);
3021
2980
  }
3022
2981
  }
3023
2982
 
@@ -3039,11 +2998,11 @@ test "RNG agreement" {
3039
2998
  expected[i] = @intCast(i * 0x1000000);
3040
2999
  }
3041
3000
 
3042
- var spe = rng.FixedRNG(1, expected.len){ .rolls = expected };
3043
- var cfz = rng.FixedRNG(1, expected.len){ .rolls = expected };
3044
- var par = rng.FixedRNG(1, expected.len){ .rolls = expected };
3045
- var brn = rng.FixedRNG(1, expected.len){ .rolls = expected };
3046
- var eff = rng.FixedRNG(1, expected.len){ .rolls = expected };
3001
+ var spe: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3002
+ var cfz: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3003
+ var par: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3004
+ var brn: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3005
+ var eff: rng.FixedRNG(1, expected.len) = .{ .rolls = expected };
3047
3006
 
3048
3007
  for (0..expected.len) |i| {
3049
3008
  try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
@@ -3133,7 +3092,7 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
3133
3092
  if (m.id == if (active.volatiles.Bide) .Bide else side.last_selected_move) {
3134
3093
  // Pokémon Showdown displays Struggle if limited to Bide but unable to pick
3135
3094
  const struggle =
3136
- m.id == .Bide and (m.pp == 0 or active.volatiles.disabled_move == slot);
3095
+ m.id == .Bide and (m.pp == 0 or active.volatiles.disable_move == slot);
3137
3096
  const s = if (struggle) 0 else slot;
3138
3097
  out[n] = .{ .type = .Move, .data = s };
3139
3098
  n += 1;
@@ -3148,7 +3107,7 @@ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Cho
3148
3107
  const m = active.moves[slot - 1];
3149
3108
  if (m.id == .None) break;
3150
3109
  if (m.pp == 0) continue;
3151
- if (active.volatiles.disabled_move == slot) continue;
3110
+ if (active.volatiles.disable_move == slot) continue;
3152
3111
  out[n] = .{ .type = .Move, .data = slot };
3153
3112
  n += 1;
3154
3113
  }