@pkmn/engine 0.1.0-dev.545e0ef3 → 0.1.0-dev.5bbdaae3

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 (57) hide show
  1. package/README.md +98 -59
  2. package/build/pkg/addon.d.ts +7 -0
  3. package/build/pkg/addon.js +112 -0
  4. package/build/pkg/addon.js.map +1 -0
  5. package/build/pkg/data/ids.json +12 -12
  6. package/build/pkg/data/layout.json +25 -3
  7. package/build/pkg/data/protocol.json +69 -12
  8. package/build/pkg/data.d.ts +3 -4
  9. package/build/pkg/data.js +3 -5
  10. package/build/pkg/data.js.map +1 -1
  11. package/build/pkg/gen1.d.ts +5 -4
  12. package/build/pkg/gen1.js +47 -31
  13. package/build/pkg/gen1.js.map +1 -1
  14. package/build/pkg/index.d.ts +15 -12
  15. package/build/pkg/index.js.map +1 -1
  16. package/build/pkg/protocol.d.ts +4 -2
  17. package/build/pkg/protocol.js +60 -39
  18. package/build/pkg/protocol.js.map +1 -1
  19. package/build/test/display.js +400 -0
  20. package/build/test/display.js.map +1 -0
  21. package/build/tools/debug.js +133 -0
  22. package/build/tools/debug.js.map +1 -0
  23. package/build.zig +104 -90
  24. package/package.json +65 -36
  25. package/src/bin/install-pkmn-engine +68 -36
  26. package/src/bin/pkmn-debug +62 -0
  27. package/src/lib/binding/node.zig +47 -142
  28. package/src/lib/binding/wasm.zig +6 -4
  29. package/src/lib/common/battle.zig +17 -0
  30. package/src/lib/common/data.zig +15 -27
  31. package/src/lib/common/debug.zig +21 -11
  32. package/src/lib/common/js.zig +118 -0
  33. package/src/lib/common/optional.zig +67 -0
  34. package/src/lib/common/options.zig +12 -3
  35. package/src/lib/common/protocol.zig +752 -237
  36. package/src/lib/common/rational.zig +353 -0
  37. package/src/lib/common/rng.zig +15 -15
  38. package/src/lib/common/util.zig +12 -0
  39. package/src/lib/gen1/README.md +270 -154
  40. package/src/lib/gen1/calc.zig +426 -0
  41. package/src/lib/gen1/chance.zig +1060 -0
  42. package/src/lib/gen1/data/moves.zig +192 -180
  43. package/src/lib/gen1/data/species.zig +2 -2
  44. package/src/lib/gen1/data/types.zig +5 -5
  45. package/src/lib/gen1/data.zig +78 -64
  46. package/src/lib/gen1/helpers.zig +577 -32
  47. package/src/lib/gen1/mechanics.zig +1019 -654
  48. package/src/lib/gen2/README.md +106 -85
  49. package/src/lib/gen2/data/items.zig +12 -18
  50. package/src/lib/gen2/data/moves.zig +654 -563
  51. package/src/lib/gen2/data/species.zig +1 -1
  52. package/src/lib/gen2/data/types.zig +130 -28
  53. package/src/lib/gen2/data.zig +131 -87
  54. package/src/lib/gen2/helpers.zig +16 -22
  55. package/src/lib/gen2/mechanics.zig +1781 -11
  56. package/src/lib/pkmn.zig +68 -9
  57. package/src/test/display/pkmn.html.tmpl +368 -0
@@ -1,8 +1,9 @@
1
1
  const std = @import("std");
2
2
 
3
+ const pkmn = @import("../pkmn.zig");
4
+
3
5
  const common = @import("../common/data.zig");
4
6
  const DEBUG = @import("../common/debug.zig").print;
5
- const options = @import("../common/options.zig");
6
7
  const protocol = @import("../common/protocol.zig");
7
8
  const rng = @import("../common/rng.zig");
8
9
 
@@ -11,21 +12,27 @@ const data = @import("data.zig");
11
12
  const assert = std.debug.assert;
12
13
 
13
14
  const expectEqual = std.testing.expectEqual;
15
+ const expectEqualSlices = std.testing.expectEqualSlices;
14
16
 
15
17
  const Choice = common.Choice;
16
18
  const ID = common.ID;
17
19
  const Player = common.Player;
18
20
  const Result = common.Result;
19
21
 
20
- const showdown = options.showdown;
22
+ const showdown = pkmn.options.showdown;
21
23
 
24
+ const Activate = protocol.Activate;
25
+ const Cant = protocol.Cant;
22
26
  const Damage = protocol.Damage;
27
+ const End = protocol.End;
23
28
  const Heal = protocol.Heal;
29
+ const Start = protocol.Start;
24
30
 
25
31
  const Gen12 = rng.Gen12;
26
32
 
27
33
  const ActivePokemon = data.ActivePokemon;
28
34
  const Effectiveness = data.Effectiveness;
35
+ const Item = data.Item;
29
36
  const Move = data.Move;
30
37
  const MoveSlot = data.MoveSlot;
31
38
  const Pokemon = data.Pokemon;
@@ -33,21 +40,1784 @@ const Side = data.Side;
33
40
  const Species = data.Species;
34
41
  const Stats = data.Stats;
35
42
  const Status = data.Status;
43
+ const TriAttack = data.TriAttack;
44
+ const Type = data.Type;
45
+ const Types = data.Types;
46
+ const Volatiles = data.Volatiles;
36
47
 
37
- pub fn update(battle: anytype, c1: Choice, c2: Choice, log: anytype) !Result {
38
- _ = battle;
39
- _ = c1;
40
- _ = c2;
41
- _ = log;
48
+ // zig fmt: off
49
+ const STAT_BOOSTS = &[_][2]u8{
50
+ .{ 25, 100 }, // -6
51
+ .{ 28, 100 }, // -5
52
+ .{ 33, 100 }, // -4
53
+ .{ 40, 100 }, // -3
54
+ .{ 50, 100 }, // -2
55
+ .{ 66, 100 }, // -1
56
+ .{ 1, 1 }, // 0
57
+ .{ 15, 10 }, // +1
58
+ .{ 2, 1 }, // +2
59
+ .{ 25, 10 }, // +3
60
+ .{ 3, 1 }, // +4
61
+ .{ 35, 10 }, // +5
62
+ .{ 4, 1 }, // +6
63
+ };
42
64
 
43
- return Result.Default;
65
+ const ACCURACY_BOOSTS = &[_][2]u8{
66
+ .{ 33, 100 }, // -6
67
+ .{ 36, 100 }, // -5
68
+ .{ 43, 100 }, // -4
69
+ .{ 50, 100 }, // -3
70
+ .{ 60, 100 }, // -2
71
+ .{ 75, 100 }, // -1
72
+ .{ 1, 1 }, // 0
73
+ .{ 133, 100 }, // +1
74
+ .{ 166, 100 }, // +2
75
+ .{ 2, 1 }, // +3
76
+ .{ 233, 100 }, // +4
77
+ .{ 133, 50 }, // +5
78
+ .{ 3, 1 }, // +6
79
+ };
80
+
81
+ const CRITICAL_HITS = &[_]u8{
82
+ 256 / 15, // 0
83
+ 256 / 8, // +1
84
+ 256 / 4, // +2
85
+ 256 / 3, // +3
86
+ 256 / 2, // +4
87
+ };
88
+ // zig fmt: on
89
+
90
+ const MAX_STAT_VALUE = 999;
91
+
92
+ pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result {
93
+ assert(c1.type != .Pass or c2.type != .Pass or battle.turn == 0);
94
+ if (battle.turn == 0) return start(battle, options);
95
+
96
+ if (try turnOrder(battle, c1, c2, options) == .P1) {
97
+ if (try doTurn(battle, .P1, c1, .P2, c2, options)) |r| return r;
98
+ } else {
99
+ if (try doTurn(battle, .P2, c2, .P1, c1, options)) |r| return r;
100
+ }
101
+
102
+ return endTurn(battle, options);
44
103
  }
45
104
 
46
- pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
105
+ fn start(battle: anytype, options: anytype) !Result {
106
+ const p1 = battle.side(.P1);
107
+ const p2 = battle.side(.P2);
108
+
109
+ var p1_slot = findFirstAlive(p1);
110
+ assert(!showdown or p1_slot == 1);
111
+ if (p1_slot == 0) return if (findFirstAlive(p2) == 0) Result.Tie else Result.Lose;
112
+
113
+ var p2_slot = findFirstAlive(p2);
114
+ assert(!showdown or p2_slot == 1);
115
+ if (p2_slot == 0) return Result.Win;
116
+
117
+ try switchIn(battle, .P1, p1_slot, true, options);
118
+ try switchIn(battle, .P2, p2_slot, true, options);
119
+
120
+ return endTurn(battle, options);
121
+ }
122
+
123
+ fn findFirstAlive(side: *const Side) u8 {
124
+ for (side.pokemon, 0..) |pokemon, i| if (pokemon.hp > 0) return side.pokemon[i].position;
125
+ return 0;
126
+ }
127
+
128
+ fn selectMove(battle: anytype, player: Player, choice: Choice) void {
129
+ if (choice.type == .Pass) return; // FIXME BATON PASS
130
+
131
+ var side = battle.side(player);
132
+ var volatiles = &side.active.volatiles;
133
+
134
+ // pre-battle menu
135
+ const forced = forced: {
136
+ if (volatiles.Recharging) break :forced true;
137
+ // Pokémon Showdown removes Flinch at the end-of-turn in its residual handler
138
+ if (!showdown) {
139
+ volatiles.Flinch = false;
140
+ battle.foe(player).active.volatiles.Flinch = false;
141
+ }
142
+ break :forced (volatiles.Thrashing or volatiles.Charging or volatiles.Rollout);
143
+ };
144
+
145
+ // battle menu
146
+ if (choice.type == .Switch) return;
147
+ assert(choice.type == .Move and (!forced or choice.data == @intFromBool(showdown)));
148
+
149
+ done: {
150
+ if (forced) break :done;
151
+ // pre-move select
152
+ if (volatiles.Encore) {
153
+ // TODO wCurPlayerMove = side.lastMove(true);
154
+ // assert(wCurPlayerMove != .None);
155
+ volatiles.Bide = false; // FIXME wPlayerCharging vs. SUBSTATUS_BIDE?
156
+ const effect = Move.get(.Pound).effect; // TODO
157
+ if (effect != .FuryCutter) volatiles.fury_cutter = false;
158
+ if (effect != .Rage) {
159
+ volatiles.Rage = false;
160
+ volatiles.rage = 0;
161
+ }
162
+ if (effect != .Protect and effect != .Endure) volatiles.protect = 0;
163
+ return;
164
+ }
165
+ if (volatiles.Bide) break :done;
166
+ // move select
167
+ if (choice.data == 0) {
168
+ const struggle = ok: {
169
+ for (side.active.moves, 0..) |move, i| {
170
+ if (move.pp > 0 and volatiles.disabled_move != i + 1) break :ok false;
171
+ }
172
+ break :ok true;
173
+ };
174
+ assert(struggle);
175
+ // TODO wCurPlayerMove = .Struggle;
176
+ } else {
177
+ assert(showdown or side.active.volatiles.disabled_move != choice.data);
178
+ const move = side.active.move(choice.data);
179
+ // You cannot *select* a move with 0 PP
180
+ assert(move.pp != 0);
181
+ // TODO wCurPlayerMove = move.id;
182
+ // battle.lastMove(player).index = @intCast(c.data);
183
+ }
184
+ }
185
+
186
+ volatiles.Rage = false;
187
+ volatiles.rage = 0;
188
+ volatiles.fury_cutter = 0;
189
+ volatiles.protect = 0;
190
+ }
191
+
192
+ fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, options: anytype) !void {
193
+ var side = battle.side(player);
194
+ var foe = battle.foe(player);
195
+
196
+ var active = &side.active;
197
+ const incoming = side.get(slot);
198
+
199
+ assert(incoming.hp != 0);
200
+ assert(slot != 1 or initial);
201
+
202
+ const out = side.pokemon[0].position;
203
+ side.pokemon[0].position = side.pokemon[slot - 1].position;
204
+ side.pokemon[slot - 1].position = out;
205
+
206
+ side.last_used_move = .None;
207
+ foe.active.volatiles.dirty = true;
208
+
209
+ active.volatiles = .{};
210
+ active.stats = incoming.stats;
211
+ active.moves = incoming.moves;
212
+ active.boosts = .{};
213
+ active.types = incoming.types;
214
+ active.species = incoming.species;
215
+
216
+ statusModify(incoming.status, &active.stats);
217
+
218
+ try options.log.switched(battle.active(player), incoming);
219
+ options.chance.switched(player, side.pokemon[0].position, out);
220
+
221
+ if (side.conditions.Spikes and !active.types.includes(.Flying)) {
222
+ incoming.hp -|= @max(incoming.stats.hp / 8, 1);
223
+ try options.log.damage(battle.active(player), incoming, .Spikes);
224
+ }
225
+ }
226
+
227
+ fn turnOrder(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Player {
228
+ assert(c1.type != .Pass or c2.type != .Pass);
229
+
230
+ if (c1.type == .Pass) return .P2;
231
+ if (c2.type == .Pass) return .P1;
232
+
233
+ // Pokémon Showdown always rolls for Quick Claw every turn, even if no Pokémon hold Quick Claw
234
+ // TODO: add to pending and only commit roll on Pokémon Showdown if roll is used
235
+ const qkc = showdown and try Rolls.quickClaw(battle, .P1, options);
236
+
237
+ if ((c1.type == .Switch) != (c2.type == .Switch)) return if (c1.type == .Switch) .P1 else .P2;
238
+
239
+ // https://www.smogon.com/forums/threads/adv-switch-priority.3622189/
240
+ // > In Gen 1 it's irrelevant [which player switches first] because switches happen instantly on
241
+ // > your own screen without waiting for the other player's choice (and their choice will appear
242
+ // > to happen first for them too, unless they attacked in which case your switch happens first)
243
+ // A cartridge-compatible implemention must not advance the RNG so we simply default to P1
244
+ const double_switch = c1.type == .Switch and c2.type == .Switch;
245
+ if (!showdown and double_switch) return .P1;
246
+
247
+ const m1 = .Pound; // TODO battle.side(.P1).last_selected_move;
248
+ const m2 = .Pound; // TODO battle.side(.P2).last_selected_move;
249
+ if (!showdown or !double_switch) {
250
+ const pri1 = Move.get(m1).priority;
251
+ const pri2 = Move.get(m2).priority;
252
+ if (pri1 != pri2) return if (pri1 > pri2) .P1 else .P2;
253
+
254
+ const qkc1 = battle.side(.P1).stored().item == .QuickClaw;
255
+ const qkc2 = battle.side(.P2).stored().item == .QuickClaw;
256
+ if (!showdown and qkc1 and qkc2) {
257
+ // If both Pokémon have Quick Claw the cartridge checks them in order and exits early if
258
+ // the first one procs, meaning the host has a slightly higher chance of going first
259
+ if (try Rolls.quickClaw(battle, .P1, options)) return .P1;
260
+ if (try Rolls.quickClaw(battle, .P2, options)) return .P2;
261
+ } else if (qkc1) {
262
+ const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P1, options);
263
+ if (proc) return .P1;
264
+ } else if (qkc2) {
265
+ const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P2, options);
266
+ if (proc) return .P2;
267
+ }
268
+ }
269
+
270
+ const spe1 = battle.side(.P1).active.stats.spe;
271
+ const spe2 = battle.side(.P2).active.stats.spe;
272
+ if (spe1 == spe2) {
273
+ // Pokémon Showdown's beforeTurnCallback shenanigans
274
+ if (showdown and !double_switch and hasCallback(m1) and hasCallback(m2)) {
275
+ battle.rng.advance(1);
276
+ }
277
+
278
+ const p1 = try Rolls.speedTie(battle, options);
279
+ if (!showdown) return if (p1) .P1 else .P2;
280
+
281
+ // Pokémon Showdown's "lockedmove" volatile's onBeforeTurn uses BattleQueue#changeAction,
282
+ // meaning that if a side is locked into a thrashing move and wins the speed tie, it
283
+ // actually uses its priority to simply insert its actual changed action into the queue,
284
+ // causing it to then execute *after* the side which should go second...
285
+ const t1 = battle.side(.P1).active.volatiles.Thrashing;
286
+ const t2 = battle.side(.P2).active.volatiles.Thrashing;
287
+ // If *both* sides are thrashing it really should be another speed tie, but we've patched
288
+ // that out and enforce host ordering of events, so P1 just goes first regardless of who
289
+ // won the original coin flip
290
+ if (t1 and t2) return .P1;
291
+ return if (p1) if (t1 and !t2) .P2 else .P1 else if (t2 and !t1) .P1 else .P2;
292
+ }
293
+
294
+ return if (spe1 > spe2) .P1 else .P2;
295
+ }
296
+
297
+ fn hasCallback(m: Move) bool {
298
+ return switch (m) {
299
+ .Counter, .MirrorCoat, .Pursuit => true,
300
+ else => false,
301
+ };
302
+ }
303
+
304
+ fn doTurn(
305
+ battle: anytype,
306
+ player: Player,
307
+ player_choice: Choice,
308
+ foe_player: Player,
309
+ foe_choice: Choice,
310
+ options: anytype,
311
+ ) !?Result {
312
+ assert(player_choice.type != .Pass);
313
+
314
+ const volatiles = &battle.side(player).active.volatiles;
315
+ const foe_volatiles = &battle.foe(player).active.volatiles;
316
+
317
+ volatiles.DestinyBond = false;
318
+ if (try executeMove(battle, player, player_choice, options)) |r| return r;
319
+ foe_volatiles.Protect = false;
320
+ foe_volatiles.Endure = false;
321
+ foe_volatiles.DestinyBond = false;
322
+
323
+ // foe_volatiles.DestinyBond = false;
324
+ if (try executeMove(battle, foe_player, foe_choice, options)) |r| return r;
325
+ volatiles.Protect = false;
326
+ volatiles.Endure = false;
327
+ volatiles.DestinyBond = false;
328
+
329
+ return null;
330
+ }
331
+
332
+ fn executeMove(battle: anytype, player: Player, choice: Choice, options: anytype) !?Result {
333
+ if (choice.type == .Switch) {
334
+ try switchIn(battle, player, choice.data, false, options);
335
+ return null;
336
+ }
337
+
338
+ assert(choice.type == .Move);
339
+ var mslot: u4 = @intCast(choice.data);
340
+
341
+ // const move = Move.get(side.last_selected_move);
342
+
343
+ // CheckTurn
344
+
345
+ return doMove(battle, player, options, mslot);
346
+ }
347
+
348
+ fn beforeMove(battle: anytype, player: Player, options: anytype) !bool {
349
+ var log = options.log;
350
+ var side = battle.side(player);
351
+ // const foe = battle.foe(player);
352
+ var active = &side.active;
353
+ var stored = side.stored();
354
+ const ident = battle.active(player);
355
+ var volatiles = &active.volatiles;
356
+
357
+ const move = Move.Pound; // TODO
358
+
359
+ if (volatiles.Recharging) {
360
+ volatiles.Recharging = false;
361
+ cantMove(volatiles);
362
+ try log.cant(ident, .Recharge);
363
+ return false;
364
+ }
365
+
366
+ if (Status.is(stored.status, .SLP)) slp: {
367
+ const before = stored.status;
368
+ // Even if the EXT bit is set this will still correctly modify the sleep duration
369
+ // if (options.calc.modify(player, .sleep)) |extend| {
370
+ // if (!extend) stored.status = 0;
371
+ // } else {
372
+ stored.status -= 1;
373
+ // }
374
+
375
+ const duration = Status.duration(stored.status);
376
+ if (!Status.is(stored.status, .EXT)) {
377
+ try options.chance.sleep(player, duration);
378
+ }
379
+
380
+ if (duration == 0) {
381
+ try log.curestatus(ident, before, .Message);
382
+ stored.status = 0; // clears EXT if present
383
+ cantMove(volatiles);
384
+ volatiles.Nightmare = false;
385
+ } else {
386
+ try log.cant(ident, .Sleep);
387
+ if (move == .Snore or move == .SleepTalk) break :slp;
388
+ cantMove(volatiles);
389
+ return false;
390
+ }
391
+ }
392
+
393
+ if (Status.is(stored.status, .FRZ) and !(move == .FlameWheel or move == .SacredFire)) {
394
+ cantMove(volatiles);
395
+ try log.cant(ident, .Freeze);
396
+ return false;
397
+ }
398
+
399
+ if (volatiles.Flinch) {
400
+ // Pokémon Showdown doesn't clear Flinch until its imaginary "residual" phase, meaning
401
+ // Pokémon can sometimes flinch multiple times from the same original hit
402
+ // if (!showdown) volatiles.Flinch = false;
403
+ volatiles.Flinch = false;
404
+ cantMove(volatiles);
405
+ try log.cant(ident, .Flinch);
406
+ return false;
407
+ }
408
+
409
+ if (volatiles.disabled_duration > 0) {
410
+ // if (options.calc.modify(player, .disable)) |extend| {
411
+ // if (!extend) volatiles.disabled_duration = 0;
412
+ // } else {
413
+ volatiles.disabled_duration -= 1;
414
+ // }
415
+ try options.chance.disable(player, volatiles.disabled_duration);
416
+
417
+ if (volatiles.disabled_duration == 0) {
418
+ volatiles.disabled_move = 0;
419
+ try log.end(ident, .Disable);
420
+ }
421
+ }
422
+
423
+ if (volatiles.Confusion) {
424
+ assert(volatiles.confusion > 0);
425
+
426
+ // if (options.calc.modify(player, .confusion)) |extend| {
427
+ // if (!extend) volatiles.confusion = 0;
428
+ // } else {
429
+ volatiles.confusion -= 1;
430
+ // }
431
+ try options.chance.confusion(player, volatiles.confusion);
432
+
433
+ if (volatiles.confusion == 0) {
434
+ volatiles.Confusion = false;
435
+ try log.end(ident, .Confusion);
436
+ } else {
437
+ try log.activate(ident, .Confusion);
438
+
439
+ if (try Rolls.confused(battle, player, options)) {
440
+ volatiles.BeatUp = false;
441
+ volatiles.Flinch = false;
442
+ cantMove(volatiles);
443
+
444
+ // TODO HitConfusion
445
+
446
+ return false;
447
+ }
448
+ }
449
+ }
450
+
451
+ if (volatiles.Attract) {
452
+ try log.atrract(ident, .Attract);
453
+
454
+ if (try Rolls.attract(battle, player, options)) {
455
+ cantMove(volatiles);
456
+ try log.cant(ident, .Attract);
457
+ return false;
458
+ }
459
+ }
460
+
461
+ if (side.active.volatiles.disabled_move != 0) {
462
+ // A Pokémon that transforms after being disabled may end up with less move slots
463
+ const m = side.active.moves[side.active.volatiles.disabled_move - 1].id;
464
+ if (m != .None and m == move) {
465
+ side.active.volatiles.Charging = false;
466
+ cantMove(volatiles);
467
+ try options.log.disabled(ident, move);
468
+ return false;
469
+ }
470
+ }
471
+
472
+ if (Status.is(stored.status, .PAR) and try Rolls.paralyzed(battle, player, options)) {
473
+ cantMove(volatiles);
474
+ try log.cant(ident, .Paralysis);
475
+ return false;
476
+ }
477
+
478
+ return true;
479
+ }
480
+
481
+ fn cantMove(volatiles: *Volatiles) void {
482
+ volatiles.Bide = false;
483
+ volatiles.Thrashing = false;
484
+ volatiles.Charging = false;
485
+ volatiles.Underground = false;
486
+ volatiles.Flying = false;
487
+ volatiles.Rollout = false;
488
+
489
+ volatiles.fury_cutter = 0;
490
+ }
491
+
492
+ fn canMove(battle: anytype, player: Player, options: anytype) !bool {
47
493
  _ = battle;
48
494
  _ = player;
49
- _ = request;
50
- _ = out;
495
+ _ = options;
496
+ return false;
497
+ }
498
+
499
+ fn decrementPP(side: *Side, mslot: u4) void {
500
+ var active = &side.active;
501
+ const volatiles = &active.volatiles;
502
+
503
+ // TODO assert(side.last_selected_move != .Struggle);
504
+ assert(!volatiles.Charging and !volatiles.BeatUp and !volatiles.Thrashing and !volatiles.Bide);
505
+
506
+ assert(active.move(mslot).pp > 0);
507
+ active.move(mslot).pp = @as(u6, @intCast(active.move(mslot).pp)) -% 1;
508
+ if (volatiles.Transform) return;
509
+
510
+ // FIXME mimic
511
+
512
+ assert(side.stored().move(mslot).pp > 0);
513
+ side.stored().move(mslot).pp = @as(u6, @intCast(side.stored().move(mslot).pp)) -% 1;
514
+ assert(active.move(mslot).pp == side.stored().move(mslot).pp);
515
+ }
51
516
 
517
+ fn doMove(battle: anytype, player: Player, options: anytype, mslot: u4) !?Result {
518
+ _ = options;
519
+ var side = battle.side(player);
520
+ const move = Move.get(.Teleport); // side.last_selected_move
521
+ switch (move.effect) {
522
+ .Teleport => {
523
+ // side.useMove();
524
+ decrementPP(side, mslot);
525
+ },
526
+ else => @panic("TODO"),
527
+ }
528
+ return null;
529
+ }
530
+
531
+ fn checkCriticalHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
532
+ if (move.bp == 0) return false;
533
+
534
+ const side = battle.side(player);
535
+ const pokemon = side.stored();
536
+
537
+ var stage: u8 =
538
+ if ((pokemon.species == .Chansey and pokemon.item == .LuckyPunch) or
539
+ (pokemon.species == .Farfetchd and pokemon.item == .Stick)) 2 else 0;
540
+ if (stage == 0) {
541
+ if (side.active.volatiles.FocusEnergy) stage += 1;
542
+ if (move.effect.isHighCritical()) stage += 2;
543
+ if (pokemon.item == .ScopeLens) stage += 1;
544
+ }
545
+ assert(stage <= 4);
546
+
547
+ return Rolls.criticalHit(battle, player, CRITICAL_HITS[stage], options);
548
+ }
549
+
550
+ fn calcDamage(
551
+ battle: anytype,
552
+ player: Player,
553
+ target_player: Player,
554
+ m: ?Move.Data,
555
+ crit: bool,
556
+ ) u16 {
557
+ _ = battle;
558
+ _ = player;
559
+ _ = target_player;
560
+ _ = m;
561
+ _ = crit;
52
562
  return 0;
53
563
  }
564
+
565
+ const FORESIGHT: Types = .{ .type1 = .Normal, .type2 = .Fighting };
566
+
567
+ fn adjustDamage(battle: anytype, player: Player, m: Move, damage: u16, effectiveness: *u16) u16 {
568
+ if (m == .Struggle) return;
569
+
570
+ const side = battle.side(player);
571
+ const foe = battle.foe(player);
572
+ const types = foe.active.types;
573
+ const move = Move.get(side.m);
574
+
575
+ var d = damage;
576
+ if ((move.type == .Water and battle.field.weather == .Rain) or
577
+ (move.type == .Fire and battle.field.weather == .Sun))
578
+ {
579
+ d = d *% @intFromEnum(Effectiveness.Super) / 10;
580
+ } else if ((move.type == .Water and battle.field.weather == .Sun) or
581
+ ((move.type == .Fire or m == .SolarBeam) and battle.field.weather == .Rain))
582
+ {
583
+ d = d *% @intFromEnum(Effectiveness.Resisted) / 10;
584
+ }
585
+
586
+ if (side.active.types.includes(move.type)) d +%= d / 2;
587
+
588
+ const neutral = @intFromEnum(Effectiveness.Neutral);
589
+ const foresight = foe.active.volatiles.Foresight and FORESIGHT.includes(move.type);
590
+ const eff1: u16 = if (foresight and types.type1 == .Ghost)
591
+ 10
592
+ else
593
+ @intFromEnum(move.type.effectiveness(types.type1));
594
+ const eff2: u16 = if (foresight and types.type2 == .Ghost)
595
+ 10
596
+ else
597
+ @intFromEnum(move.type.effectiveness(types.type2));
598
+
599
+ // Type effectiveness matchup precedence only matters with (NVE, SE)
600
+ if (!showdown and (eff1 + eff2) == Effectiveness.mismatch and
601
+ Type.precedence(move.type, types.type1) > Type.precedence(move.type, types.type2))
602
+ {
603
+ assert(eff2 != neutral);
604
+ d = d *% eff2 / 10;
605
+ assert(types.type1 != types.type2);
606
+ assert(eff1 != neutral);
607
+ d = d *% eff1 / 10;
608
+ } else {
609
+ if (eff1 != neutral) d = d *% eff1 / 10;
610
+ if (types.type1 != types.type2 and eff2 != neutral) d = d *% eff2 / 10;
611
+ }
612
+
613
+ effectiveness.* = if (types.type1 == types.type2) eff1 * neutral else eff1 * eff2;
614
+ return d;
615
+ }
616
+
617
+ fn randomizeDamage(battle: anytype, player: Player, damage: u16, options: anytype) !u16 {
618
+ if (damage <= 1) return;
619
+ const random = try Rolls.damage(battle, player, options);
620
+ options.calc.base(player.foe(), damage);
621
+ damage = @intCast(@as(u32, damage) *% random / 255);
622
+ options.calc.final(player.foe(), damage);
623
+ return damage;
624
+ }
625
+
626
+ fn applyDamage(
627
+ battle: anytype,
628
+ player: Player,
629
+ options: anytype,
630
+ ) !bool {
631
+ _ = battle;
632
+ _ = player;
633
+ _ = options;
634
+ return false;
635
+ }
636
+
637
+ fn checkHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
638
+ if (moveHit(battle, player, move, options)) return true;
639
+
640
+ // try options.chance.commit(player, .miss);
641
+ try options.log.lastmiss();
642
+ try options.log.miss(battle.active(player));
643
+
644
+ return false;
645
+ }
646
+
647
+ // TODO: set damage = 0 if missed and not (High) Jump Kick
648
+ fn moveHit(
649
+ battle: anytype,
650
+ player: Player,
651
+ move: Move.Data,
652
+ options: anytype,
653
+ ) !bool {
654
+ const side = battle.side(player);
655
+ const foe = battle.foe(player);
656
+
657
+ if (move.effect == .DreamEater and !Status.is(foe.stored().status, .SLP)) return false;
658
+ if (foe.active.volatiles.Protect) return false;
659
+ const drain = move.effect == .DrainHP or move.effect == .DreamEater;
660
+ if (drain and foe.active.volatiles.Substitute) return false;
661
+ if (side.active.volatiles.LockOn) {
662
+ return !(foe.active.volatiles.Flying and move.extra.undeground);
663
+ }
664
+ assert(!(foe.active.volatiles.Flying and foe.active.volatiles.Underground));
665
+ if (foe.active.volatiles.Flying) {
666
+ if (!move.extra.flying) return false;
667
+ } else if (foe.active.volatiles.Underground) {
668
+ if (!move.extra.underground) return false;
669
+ }
670
+ if (move.effect == .Thunder and battle.field.weather == .Rain) return true;
671
+ if (move.effect == .AlwaysHit) return true;
672
+
673
+ var accuracy: u16 = move.accuracy;
674
+ if (foe.active.boosts.evasion <= side.active.boosts.accuracy or
675
+ !foe.active.volatiles.Foresight)
676
+ {
677
+ var boost = ACCURACY_BOOSTS[@as(u4, @intCast(@as(i8, side.active.boosts.accuracy) + 6))];
678
+ accuracy = accuracy * boost[0] / boost[1];
679
+ boost = ACCURACY_BOOSTS[@as(u4, @intCast(@as(i8, -foe.active.boosts.evasion) + 6))];
680
+ accuracy = accuracy * boost[0] / boost[1];
681
+ accuracy = @min(255, @max(1, accuracy));
682
+ }
683
+
684
+ if (foe.stored().item == .BrightPowder) {
685
+ accuracy -|= 20;
686
+ }
687
+
688
+ // The accuracy roll is skipped entirely if maxed
689
+ if (accuracy == 255) return true;
690
+ return !try Rolls.hit(battle, player, @intCast(accuracy), options);
691
+ }
692
+
693
+ fn checkFaint(
694
+ battle: anytype,
695
+ player: Player,
696
+ options: anytype,
697
+ ) @TypeOf(options.log).Error!?Result {
698
+ _ = battle;
699
+ _ = player;
700
+ return null;
701
+ }
702
+
703
+ fn faint(battle: anytype, player: Player, done: bool, options: anytype) !?Result {
704
+ _ = battle;
705
+ _ = player;
706
+ _ = done;
707
+ _ = options;
708
+ return null;
709
+ }
710
+
711
+ fn handleResidual(battle: anytype, player: Player, options: anytype) !void {
712
+ var side = battle.side(player);
713
+ var stored = side.stored();
714
+ const ident = battle.active(player);
715
+ var volatiles = &side.active.volatiles;
716
+
717
+ // TODO if (stored.hp == 0) return;
718
+ assert(stored.hp > 0);
719
+
720
+ const brn = Status.is(stored.status, .BRN);
721
+ if (brn or Status.is(stored.status, .PSN)) {
722
+ var damage: u16 = undefined;
723
+ if (volatiles.Toxic) {
724
+ volatiles.toxic += 1;
725
+ damage = @max(stored.stats.hp / 16, 1) * volatiles.toxic;
726
+ } else {
727
+ damage = @max(stored.stats.hp / 8, 1);
728
+ }
729
+
730
+ stored.hp -|= damage;
731
+ // Pokémon Showdown uses damageOf here but its not relevant in Generation II
732
+ try options.log.damage(ident, stored, if (brn) Damage.Burn else Damage.Poison);
733
+ if (stored.hp == 0) return;
734
+ }
735
+
736
+ if (volatiles.LeechSeed) {
737
+ var foe = battle.foe(player);
738
+ var foe_stored = foe.stored();
739
+ const foe_ident = battle.active(player.foe());
740
+
741
+ // if (foe_stored.hp == 0) {
742
+ // assert(showdown);
743
+ // return;
744
+ // }
745
+
746
+ const damage = @max(stored.stats.hp / 8, 1);
747
+ stored.hp -|= damage;
748
+ // As above, Pokémon Showdown uses damageOf but its not relevant
749
+ try options.log.damage(ident, stored, .LeechSeed);
750
+
751
+ const before = foe_stored.hp;
752
+ foe_stored.hp = @min(foe_stored.hp + damage, foe_stored.stats.hp);
753
+ // Pokémon Showdown uses the less specific heal here instead of drain... because reasons?
754
+ if (foe_stored.hp > before) try options.log.heal(foe_ident, foe_stored, .Silent);
755
+ if (stored.hp == 0) return;
756
+ }
757
+
758
+ if (volatiles.Nightmare) {
759
+ stored.hp -= @max(stored.stats.hp / 4, 1);
760
+ // ibid
761
+ try options.log.damage(ident, stored, Damage.Nightmare);
762
+ if (stored.hp == 0) return;
763
+ }
764
+
765
+ if (volatiles.Curse) {
766
+ stored.hp -= @max(stored.stats.hp / 4, 1);
767
+ // ibid
768
+ try options.log.damage(ident, stored, Damage.Curse);
769
+ if (stored.hp == 0) return;
770
+ }
771
+ }
772
+
773
+ fn betweenTurns(battle: anytype, mslot: u4, options: anytype) !void {
774
+ const players = std.enums.values(Player);
775
+
776
+ // TODO checkFaintThen(p1, p2);
777
+
778
+ // Future Sight
779
+ inline for (players) |player| {
780
+ var side = battle.side(player);
781
+ var future_sight = &side.conditions.future_sight;
782
+
783
+ if (future_sight.count > 0) {
784
+ future_sight.count -= 1;
785
+ assert(future_sight.count != 0);
786
+ if (future_sight.count == 1) {
787
+ try options.log.end(battle.active(player), .FutureSight);
788
+ // TODO doMove
789
+ }
790
+ }
791
+ }
792
+ // TODO checkFaintThen(p1, p2);
793
+
794
+ // Weather
795
+ if (battle.field.weather != .None) {
796
+ assert(battle.field.weather_duration > 0);
797
+ battle.field.weather_duration -= 1;
798
+
799
+ if (battle.field.weather_duration == 0) {
800
+ battle.field.weather = .None;
801
+ try options.log.weather(battle.field.weather, .None);
802
+ } else {
803
+ try options.log.weather(battle.field.weather, .Upkeep);
804
+ if (battle.field.weather == .Sandstorm) {
805
+ inline for (players) |player| {
806
+ var side = battle.side(player);
807
+ const active = side.active;
808
+ if (!(active.volatiles.underground or active.types.sandstormImmune())) {
809
+ side.stored().hp -|= @max(side.stored().stats.hp / 8, 1);
810
+ try options.log.damage(battle.active(player), side.stored(), .Sandstorm);
811
+ }
812
+ }
813
+ // TODO checkFaintThen(p1, p2);
814
+ }
815
+ }
816
+ }
817
+
818
+ // Binding
819
+ inline for (players) |player| {
820
+ var side = battle.side(player);
821
+ var volatiles = &side.active.volatiles;
822
+
823
+ if (volatiles.bind > 0 and !volatiles.Substitute) {
824
+ volatiles.bind -= 1;
825
+ if (volatiles.bind == 0) {
826
+ const reason = @intFromEnum(End.Bind) + volatiles.bind_reason - 1;
827
+ try options.log.end(battle.active(player), @enumFromInt(reason));
828
+ } else {
829
+ var reason = @intFromEnum(Activate.Bind) + volatiles.bind_reason - 1;
830
+ try options.log.activate(battle.active(player), @enumFromInt(reason));
831
+
832
+ reason = @intFromEnum(Activate.Damage) + volatiles.bind_reason - 1;
833
+ side.stored().hp -|= @max(side.stored().stats.hp / 16, 1);
834
+ try options.log.damage(battle.active(player), side.stored(), reason);
835
+ }
836
+ }
837
+ }
838
+ // TODO checkFaintThen(p1, p2);
839
+
840
+ // Perish Song
841
+ inline for (players) |player| {
842
+ var side = battle.side(player);
843
+ var volatiles = &side.active.volatiles;
844
+
845
+ if (volatiles.PerishSong) {
846
+ assert(volatiles.perish_song > 0);
847
+ volatiles.perish_song -= 1;
848
+
849
+ assert(volatiles.perish_song < 3);
850
+ const reason = @intFromEnum(Start.PerishSong0) + volatiles.perish_song;
851
+ try options.log.start(battle.active(player), reason);
852
+
853
+ if (volatiles.perish_song == 0) {
854
+ volatiles.PerishSong = false;
855
+ side.stored().hp = 0;
856
+ }
857
+ }
858
+ }
859
+ // TODO checkFaintThen(p1, p2);
860
+
861
+ // Leftovers
862
+ inline for (players) |player| {
863
+ var stored = battle.side(player).stored();
864
+
865
+ if (stored.item == .Leftovers) {
866
+ const before = stored.hp;
867
+ stored.hp = @min(stored.hp + @max(stored.stats.hp / 16, 1), stored.stats.hp);
868
+ if (stored.hp > before) try options.log.heal(battle.active(player), stored, .Leftovers);
869
+ }
870
+ }
871
+
872
+ // Mystery Berry
873
+ inline for (players) |player| {
874
+ var stored = battle.side(player).stored();
875
+
876
+ if (stored.item == .MysteryBerry) {
877
+ // TODO stored and active moves, transform, etc
878
+ }
879
+ }
880
+
881
+ // Defrost
882
+ inline for (players) |player| {
883
+ var side = battle.side(player);
884
+
885
+ if (Status.is(side.stored().status, .FRZ) and
886
+ !side.active.volatiles.frozen and
887
+ try Rolls.defrost(battle, player, options))
888
+ {
889
+ try options.log.curestatus(battle.active(player), side.stored().status, .Message);
890
+ side.stored().status = 0;
891
+ }
892
+ }
893
+
894
+ // Side Conditions
895
+ if (showdown) {
896
+ inline for (players) |player| {
897
+ updateSideCondition(battle, player, "Reflect", "reflect");
898
+ }
899
+ inline for (players) |player| {
900
+ updateSideCondition(battle, player, "LightScreen", "light_screen");
901
+ }
902
+ inline for (players) |player| {
903
+ updateSideCondition(battle, player, "Safeguard", "safeguard");
904
+ }
905
+ } else {
906
+ inline for (players) |player| {
907
+ updateSideCondition(battle, player, "Safeguard", "safeguard");
908
+ }
909
+
910
+ inline for (players) |player| {
911
+ updateSideCondition(battle, player, "LightScreen", "light_screen");
912
+ updateSideCondition(battle, player, "Reflect", "reflect");
913
+ }
914
+ }
915
+
916
+ // Healing items
917
+ inline for (players) |player| {
918
+ var side = battle.side(player);
919
+ var stored = side.stored();
920
+ const ident = battle.active(player);
921
+
922
+ const num = @intFromEnum(stored.item);
923
+ if (num > @intFromEnum(Item.Berry)) {
924
+ if (num <= @intFromEnum(Item.GoldBerry)) {
925
+ const proc = if (showdown)
926
+ stored.hp <= stored.stats.hp
927
+ else
928
+ stored.hp < stored.stats.hp;
929
+ if (proc) {
930
+ assert(stored.hp != 0);
931
+ try options.log.enditem(ident, stored.item, .Eat);
932
+ stored.item = .None;
933
+
934
+ const offset = num - @intFromEnum(Item.Berry);
935
+ assert(offset < 3);
936
+ stored.hp = @min(stored.hp + ((offset + 1) * 10), stored.stats.hp);
937
+ const reason: Heal = @enumFromInt(@intFromEnum(Heal.Berry) + offset);
938
+ try options.log.heal(ident, stored, reason);
939
+ }
940
+ } else if (num < @intFromEnum(Item.BitterBerry)) {
941
+ const proc = if (stored.item == .MiracleBerry)
942
+ Status.any(stored.status)
943
+ else
944
+ Status.is(stored.status, @enumFromInt(num - @intFromEnum(Item.MintBerry) + 2));
945
+
946
+ if (proc) {
947
+ assert(Status.any(stored.status));
948
+ try options.log.enditem(ident, stored.item, .Eat);
949
+ stored.item = .None;
950
+
951
+ // FIXME wtf?
952
+ // side.active.volatiles.Toxic = false;
953
+ // side.active.volatiles.NightMare = false;
954
+ if (stored.item == .MiracleBerry) {
955
+ side.active.volatiles.Confusion = false;
956
+ try options.log.end(ident, .Confusion);
957
+ }
958
+ stored.status = 0;
959
+
960
+ // FIXME recalc stats
961
+
962
+ try options.log.curestatus(ident, stored.status, .Message);
963
+ }
964
+ } else if (num == @intFromEnum(Item.BitterBerry)) {
965
+ if (side.active.volatiles.Confusion) {
966
+ try options.log.enditem(ident, stored.item, .Eat);
967
+ stored.item = .None;
968
+
969
+ side.active.volatiles.Confusion = false;
970
+ try options.log.end(ident, .Confusion);
971
+ }
972
+ }
973
+ }
974
+ }
975
+
976
+ // Encore
977
+ inline for (players) |player| {
978
+ var side = battle.side(player);
979
+ var volatiles = &side.active.volatiles;
980
+
981
+ if (volatiles.Encore) {
982
+ assert(volatiles.encore > 0);
983
+ volatiles.encore -= 1;
984
+
985
+ if (volatiles.encore == 0 or side.active.moves(mslot).pp == 0) {
986
+ volatiles.Encore = false;
987
+ try options.log.end(battle.active(player), .Encore);
988
+ }
989
+ }
990
+ }
991
+ }
992
+
993
+ inline fn updateSideCondition(
994
+ battle: anytype,
995
+ player: Player,
996
+ comptime key: []const u8,
997
+ comptime value: []const u8,
998
+ options: anytype,
999
+ ) !void {
1000
+ var side = battle.side(player);
1001
+ if (@field(side.conditions, key)) {
1002
+ assert(@field(side.conditions, value) > 0);
1003
+ @field(side.conditions, value) -= 1;
1004
+
1005
+ if (@field(side.conditions, value) == 0) {
1006
+ @field(side.conditions, key) = false;
1007
+ try options.log.sideend(battle.active(player), @field(protocol.Side, key));
1008
+ }
1009
+ }
1010
+ }
1011
+
1012
+ fn endTurn(battle: anytype, options: anytype) @TypeOf(options.log).Error!Result {
1013
+ battle.turn += 1;
1014
+
1015
+ if (showdown and battle.turn >= 1000) {
1016
+ try options.log.tie();
1017
+ return Result.Tie;
1018
+ } else if (battle.turn >= 65535) {
1019
+ return Result.Error;
1020
+ }
1021
+
1022
+ try options.log.turn(battle.turn);
1023
+
1024
+ return Result.Default;
1025
+ }
1026
+
1027
+ pub const Effects = struct {
1028
+ fn protect(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
1029
+ var side = battle.side(player);
1030
+ var volatiles = &side.active.volatiles;
1031
+
1032
+ // TODO or if we didn't go first
1033
+ if (volatiles.Substitute) return try options.log.fail(battle.active(player), .None);
1034
+
1035
+ // TODO
1036
+
1037
+ if (move.effect == .Endure) {
1038
+ volatiles.Endure = true;
1039
+ } else {
1040
+ volatiles.Protect = true;
1041
+ }
1042
+ try options.log.singleturn(battle.active(player), move.id);
1043
+ }
1044
+
1045
+ const SLOTS: [5]u3 = .{ 0, 0, 0, 0, 0 };
1046
+
1047
+ fn forceSwitch(battle: anytype, player: Player, options: anytype) !void {
1048
+ const foe = battle.foe(player);
1049
+
1050
+ var n: u3 = 0;
1051
+ var i: u3 = 0;
1052
+ for (foe.pokemon, 0..) |pokemon, j| {
1053
+ if (pokemon.species == .None) break;
1054
+ n += 1;
1055
+ if (pokemon.position == 1) continue;
1056
+ if (pokemon.hp > 0) {
1057
+ SLOTS[i] = foe.pokemon[j].position;
1058
+ i += 1;
1059
+ }
1060
+ }
1061
+
1062
+ try Rolls.forceSwitch(battle, player, SLOTS[0..i], n, options);
1063
+ }
1064
+
1065
+ const MAGNITUDE_POWER = [_]u8{ 10, 30, 50, 70, 90, 110, 150 };
1066
+ };
1067
+
1068
+ fn statusModify(status: u8, stats: *Stats(u16)) void {
1069
+ if (Status.is(status, .PAR)) {
1070
+ stats.spe = @max(stats.spe / 4, 1);
1071
+ } else if (Status.is(status, .BRN)) {
1072
+ stats.atk = @max(stats.atk / 2, 1);
1073
+ }
1074
+ }
1075
+
1076
+ inline fn isForced(active: anytype) bool {
1077
+ return active.volatiles.Recharging or active.volatiles.Rage or
1078
+ active.volatiles.Thrashing or active.volatiles.Charging or
1079
+ active.volatiles.Rollout;
1080
+ }
1081
+
1082
+ pub const Rolls = struct {
1083
+ inline fn speedTie(battle: anytype, options: anytype) !bool {
1084
+ const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
1085
+ player == .P1
1086
+ else if (showdown)
1087
+ battle.rng.range(u8, 0, 2) == 0
1088
+ else
1089
+ battle.rng.next() < Gen12.percent(50) + 1;
1090
+
1091
+ try options.chance.speedTie(p1);
1092
+ return p1;
1093
+ }
1094
+
1095
+ inline fn quickClaw(battle: anytype, player: Player, options: anytype) !bool {
1096
+ const qkc = if (options.calc.overridden(player, .quick_claw)) |val|
1097
+ val == .true
1098
+ else if (showdown)
1099
+ battle.rng.chance(u8, 60, 256)
1100
+ else
1101
+ battle.rng.next() < 60;
1102
+
1103
+ try options.chance.quickClaw(player, qkc);
1104
+ return qkc;
1105
+ }
1106
+
1107
+ inline fn hit(battle: anytype, player: Player, accuracy: u8, options: anytype) !bool {
1108
+ const ok = if (options.calc.overridden(player, .hit)) |val|
1109
+ val == .true
1110
+ else if (showdown)
1111
+ battle.rng.chance(u8, accuracy, 256)
1112
+ else
1113
+ battle.rng.next() < accuracy;
1114
+
1115
+ try options.chance.hit(player, ok, accuracy);
1116
+ return ok;
1117
+ }
1118
+
1119
+ inline fn criticalHit(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
1120
+ const crit = if (options.calc.overridden(player, .critical_hit)) |val|
1121
+ val == .true
1122
+ else if (showdown)
1123
+ battle.rng.chance(u8, rate, 256)
1124
+ else
1125
+ battle.rng.next() < rate;
1126
+
1127
+ try options.chance.criticalHit(player, crit, rate);
1128
+ return crit;
1129
+ }
1130
+
1131
+ inline fn damage(battle: anytype, player: anytype, options: anytype) !u8 {
1132
+ const roll = if (options.calc.overridden(player, .damage)) |val|
1133
+ val
1134
+ else roll: {
1135
+ if (showdown) break :roll battle.rng.range(u8, 217, 256);
1136
+ while (true) {
1137
+ const r = std.math.rotr(u8, battle.rng.next(), 1);
1138
+ if (r >= 217) break :roll r;
1139
+ }
1140
+ };
1141
+
1142
+ assert(roll >= 217 and roll <= 255);
1143
+ try options.chance.damage(player, roll);
1144
+ return roll;
1145
+ }
1146
+
1147
+ inline fn confused(battle: anytype, player: Player, options: anytype) !bool {
1148
+ const cfz = if (options.calc.overridden(player, .confused)) |val|
1149
+ val == .true
1150
+ else if (showdown)
1151
+ !battle.rng.chance(u8, 128, 256)
1152
+ else
1153
+ battle.rng.next() >= Gen12.percent(50) + 1;
1154
+
1155
+ try options.chance.confused(player, cfz);
1156
+ return cfz;
1157
+ }
1158
+
1159
+ inline fn attract(battle: anytype, player: Player, options: anytype) !bool {
1160
+ const cant = if (options.calc.overridden(player, .attract)) |val|
1161
+ val == .true
1162
+ else if (showdown)
1163
+ battle.rng.chance(u8, 1, 2)
1164
+ else
1165
+ battle.rng.next() > Gen12.percent(50) + 1;
1166
+
1167
+ try options.chance.attract(player, cant);
1168
+ return cant;
1169
+ }
1170
+
1171
+ inline fn paralyzed(battle: anytype, player: Player, options: anytype) !bool {
1172
+ const par = if (options.calc.overridden(player, .paralyzed)) |val|
1173
+ val == .true
1174
+ else if (showdown)
1175
+ battle.rng.chance(u8, 63, 256)
1176
+ else
1177
+ battle.rng.next() < Gen12.percent(25);
1178
+
1179
+ try options.chance.paralyzed(player, par);
1180
+ return par;
1181
+ }
1182
+
1183
+ inline fn defrost(battle: anytype, player: Player, options: anytype) !bool {
1184
+ const thaw = if (options.calc.overridden(player, .defrost)) |val|
1185
+ val == .true
1186
+ else if (showdown)
1187
+ battle.rng.chance(u8, 25, 256)
1188
+ else
1189
+ battle.rng.next() < Gen12.percent(10);
1190
+
1191
+ try options.chance.defrost(player, thaw);
1192
+ return thaw;
1193
+ }
1194
+
1195
+ inline fn secondaryChance(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
1196
+ // GLITCH: 100% secondary chance will still fail 1/256 of the time
1197
+ const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
1198
+ val == .true
1199
+ else if (showdown)
1200
+ battle.rng.chance(u8, rate, 256)
1201
+ else
1202
+ battle.rng.next() < rate;
1203
+
1204
+ try options.chance.secondaryChance(player, proc, rate);
1205
+ return proc;
1206
+ }
1207
+
1208
+ inline fn item(battle: anytype, player: Player, options: anytype, effect: Item) !bool {
1209
+ assert(effect == .FocusBand or effect == .KingsRock);
1210
+ const roll = if (effect == .FocusBand) .focus_band else .kings_rock;
1211
+
1212
+ const proc = if (options.calc.overridden(player, roll)) |val|
1213
+ val == .true
1214
+ else if (showdown)
1215
+ battle.rng.chance(u8, 30, 256)
1216
+ else
1217
+ battle.rng.next() < 30;
1218
+
1219
+ if (effect == .FocusBand) {
1220
+ try options.chance.focusBand(player, proc);
1221
+ } else {
1222
+ try options.chance.kingsRock(player, proc);
1223
+ }
1224
+ return proc;
1225
+ }
1226
+
1227
+ inline fn triAttack(battle: anytype, player: Player, options: anytype) !u8 {
1228
+ const status: TriAttack = if (options.calc.overridden(player, .tri_attack)) |val|
1229
+ val
1230
+ else if (showdown)
1231
+ @enumFromInt(battle.rng.range(u8, 0, 2))
1232
+ else loop: {
1233
+ while (true) {
1234
+ const r = std.math.rotr(u8, std.battle.rng.next(), 4) & 3;
1235
+ if (r != 0) break :loop @enumFromInt(r - 1);
1236
+ }
1237
+ };
1238
+
1239
+ try options.chance.triAttack(player, status);
1240
+ return status.status();
1241
+ }
1242
+
1243
+ inline fn present(battle: anytype, player: Player, options: anytype) !u8 {
1244
+ const power = if (options.calc.overridden(player, .present)) |val|
1245
+ (val - 1) * 40
1246
+ else if (showdown) power: {
1247
+ const r = battle.rng.range(u8, 0, 10);
1248
+ if (r < 2) break :power 0;
1249
+ if (r < 6) break :power 40;
1250
+ if (r < 9) break :power 80;
1251
+ break :power 120;
1252
+ } else power: {
1253
+ const r = battle.rng.next();
1254
+ if (r <= Gen12.percent(40)) break :power 40;
1255
+ if (r <= Gen12.percent(70) + 1) break :power 80;
1256
+ if (r <= Gen12.percent(80)) break :power 120;
1257
+ break :power 0;
1258
+ };
1259
+
1260
+ assert(power <= 120);
1261
+ try options.chance.present(player, power);
1262
+ return power;
1263
+ }
1264
+
1265
+ inline fn magnitude(battle: anytype, player: Player, options: anytype) !u8 {
1266
+ const num = if (options.calc.overridden(player, .magnitude)) |val|
1267
+ val + 3
1268
+ else if (showdown) num: {
1269
+ const r = battle.rng.range(u8, 0, 100);
1270
+ if (r < 5) break :num 4;
1271
+ if (r < 15) break :num 5;
1272
+ if (r < 35) break :num 6;
1273
+ if (r < 65) break :num 7;
1274
+ if (r < 85) break :num 8;
1275
+ if (r < 95) break :num 9;
1276
+ break :num 10;
1277
+ } else num: {
1278
+ const r = battle.rng.next();
1279
+ if (r <= Gen12.percent(5) + 1) break :num 4;
1280
+ if (r <= Gen12.percent(15)) break :num 5;
1281
+ if (r <= Gen12.percent(35)) break :num 6;
1282
+ if (r <= Gen12.percent(65) + 1) break :num 7;
1283
+ if (r <= Gen12.percent(85) + 1) break :num 8;
1284
+ if (r <= Gen12.percent(95)) break :num 9;
1285
+ break :num 10;
1286
+ };
1287
+
1288
+ assert(num >= 4 and num <= 10);
1289
+ try options.chance.magnitude(player, num);
1290
+ return num;
1291
+ }
1292
+
1293
+ inline fn tripleKick(battle: anytype, player: Player, options: anytype) !u2 {
1294
+ const hits = if (options.calc.overridden(player, .triple_kick)) |val|
1295
+ val
1296
+ else if (showdown)
1297
+ battle.rng.range(u2, 1, 4)
1298
+ else power: {
1299
+ while (true) {
1300
+ const r = battle.rng.next() & 3;
1301
+ if (r != 0) break :power r - 1;
1302
+ }
1303
+ };
1304
+
1305
+ assert(hits >= 1 and hits <= 3);
1306
+ try options.chance.tripleKick(player, hits);
1307
+ return hits;
1308
+ }
1309
+
1310
+ inline fn spite(battle: anytype, player: Player, options: anytype) !u8 {
1311
+ const pp = if (options.calc.overridden(player, .spite)) |val|
1312
+ val + 1
1313
+ else if (showdown)
1314
+ battle.rng.range(u8, 2, 6)
1315
+ else
1316
+ (battle.rng.next() & 3) + 2;
1317
+
1318
+ assert(pp >= 2 and pp <= 5);
1319
+ try options.chance.spite(player, pp);
1320
+ return pp;
1321
+ }
1322
+
1323
+ // Conversion 2 can at most choose between 7 types (vs. a Grass-type attack)
1324
+ const CONVERSION_2: [7]Type = [_]Type{.{}} ** 7;
1325
+
1326
+ inline fn conversion2(battle: anytype, player: Player, mtype: Type, options: anytype) !Type {
1327
+ var i: u8 = 0;
1328
+ const neutral = @intFromEnum(Effectiveness.Neutral);
1329
+ assert(mtype != .@"???");
1330
+ const ty: Type = if (options.calc.overridden(player, .conversion_2)) |val|
1331
+ @enumFromInt(@intFromEnum(val) - 1)
1332
+ else if (showdown) ty: {
1333
+ for (Type.SHOWDOWN) |t| {
1334
+ if (@intFromEnum(mtype.effectiveness(t)) < neutral) {
1335
+ CONVERSION_2[i] = t;
1336
+ i += 1;
1337
+ }
1338
+ }
1339
+ assert(i > 0 and i <= 7);
1340
+ break :ty CONVERSION_2[battle.rng.range(u8, 0, i)];
1341
+ } else ty: {
1342
+ while (true) {
1343
+ const r = battle.rng.next() & 0b11111;
1344
+ if (r == 6) continue; // BIRD
1345
+ if (r > 9 and r < 20) continue; // UNUSED_TYPES
1346
+ if (r > 27) continue; // TYPES_END
1347
+
1348
+ const t = Types.conversion2(r);
1349
+ if (@intFromEnum(mtype.effectiveness(t)) < neutral) {
1350
+ break :ty t;
1351
+ }
1352
+ }
1353
+ };
1354
+
1355
+ assert(ty != .@"???");
1356
+ try options.chance.conversion2(player, ty, mtype, i);
1357
+ return ty;
1358
+ }
1359
+
1360
+ inline fn protect(battle: anytype, player: Player, count: u8, options: anytype) !bool {
1361
+ assert(count <= 255);
1362
+ const num = 255 >> count;
1363
+
1364
+ const ok = if (options.calc.overridden(player, .protect)) |val|
1365
+ val
1366
+ else if (showdown)
1367
+ battle.rng.chance(u8, num, 255)
1368
+ else protected: {
1369
+ while (true) {
1370
+ const r = battle.rng.next();
1371
+ if (r != 0) break :protected !(num <= r - 1);
1372
+ }
1373
+ };
1374
+
1375
+ try options.chance.protect(player, num, ok);
1376
+ return ok;
1377
+ }
1378
+
1379
+ fn conversion(battle: anytype, player: Player, options: anytype) !u4 {
1380
+ _ = battle;
1381
+
1382
+ const n: u4 = 0;
1383
+ const slot: u4 = 0;
1384
+
1385
+ assert(n >= 1 and n <= 4);
1386
+ assert(slot >= 1 and slot <= 4);
1387
+
1388
+ try options.chance.moveSlot(player, slot, n);
1389
+ return slot;
1390
+ }
1391
+
1392
+ fn sleepTalk(battle: anytype, player: Player, options: anytype) !u4 {
1393
+ _ = battle;
1394
+
1395
+ const n: u4 = 0;
1396
+ const slot: u4 = 0;
1397
+
1398
+ assert(n >= 1 and n <= 4);
1399
+ assert(slot >= 1 and slot <= 4);
1400
+ try options.chance.moveSlot(player, slot, n);
1401
+ return slot;
1402
+ }
1403
+
1404
+ // FIXME TODO sleep talk
1405
+ // TODO conversion move slot
1406
+ fn moveSlot(
1407
+ battle: anytype,
1408
+ player: Player,
1409
+ moves: []MoveSlot,
1410
+ check_pp: u4,
1411
+ options: anytype,
1412
+ ) !u4 {
1413
+ // TODO: consider throwing error instead of rerolling?
1414
+ const overridden = if (options.calc.overridden(player, .move_slot)) |val|
1415
+ if (moves[val - 1].id != .None and (check_pp == 0 or moves[val - 1].pp > 0))
1416
+ val
1417
+ else
1418
+ null
1419
+ else
1420
+ null;
1421
+
1422
+ const slot: u4 = overridden orelse slot: {
1423
+ if (showdown) {
1424
+ if (check_pp == 0) {
1425
+ var i: usize = moves.len;
1426
+ while (i > 0) {
1427
+ i -= 1;
1428
+ if (moves[i].id != .None) {
1429
+ break :slot battle.rng.range(u4, 0, @as(u4, @intCast(i + 1))) + 1;
1430
+ }
1431
+ }
1432
+ } else {
1433
+ var r = battle.rng.range(u4, 0, check_pp) + 1;
1434
+ var i: usize = 0;
1435
+ while (i < moves.len and r > 0) : (i += 1) {
1436
+ if (moves[i].pp > 0) {
1437
+ r -= 1;
1438
+ if (r == 0) break;
1439
+ }
1440
+ }
1441
+ break :slot @intCast(i + 1);
1442
+ }
1443
+ }
1444
+
1445
+ while (true) {
1446
+ const r: u4 = @intCast(battle.rng.next() & 3);
1447
+ if (moves[r].id != .None and (check_pp == 0 or moves[r].pp > 0)) break :slot r + 1;
1448
+ }
1449
+ };
1450
+
1451
+ try options.chance.moveSlot(player, slot, moves, check_pp);
1452
+ return slot;
1453
+ }
1454
+
1455
+ inline fn forceSwitch(
1456
+ battle: anytype,
1457
+ player: Player,
1458
+ slots: []u3,
1459
+ n: u3,
1460
+ options: anytype,
1461
+ ) !u3 {
1462
+ const foe = battle.foe(player);
1463
+
1464
+ assert(slots.len < 5);
1465
+ assert(n > 0 and n <= 6);
1466
+
1467
+ // TODO: consider throwing error instead of rerolling?
1468
+ const overridden = if (options.calc.overridden(player, .force_switch)) |val| val: {
1469
+ for (slots) |slot| if (slot == val) break :val if (foe.get(slot).hp > 0) val else null;
1470
+ break :val null;
1471
+ } else null;
1472
+
1473
+ const slot: u3 = overridden orelse slot: {
1474
+ if (showdown) {
1475
+ break :slot slots[battle.rng.range(u3, 0, slots.len)];
1476
+ } else {
1477
+ while (true) {
1478
+ const r = battle.rng.next() & 7;
1479
+ if (r > n or r == 1) continue;
1480
+ if (foe.get(r).hp > 0) break :slot r;
1481
+ }
1482
+ }
1483
+ };
1484
+
1485
+ assert(n >= 1 and n <= 6);
1486
+ assert(slot >= 1 and slot <= 6);
1487
+ try options.chance.forceSwitch(player, slot, slots.len);
1488
+ return slot;
1489
+ }
1490
+
1491
+ const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
1492
+
1493
+ inline fn multiHit(battle: anytype, player: Player, options: anytype) !u3 {
1494
+ const n: u3 = if (options.calc.overridden(player, .multi_hit)) |val|
1495
+ @intCast(val)
1496
+ else if (showdown)
1497
+ DISTRIBUTION[battle.rng.range(u3, 0, DISTRIBUTION.len)]
1498
+ else n: {
1499
+ const r = (battle.rng.next() & 3);
1500
+ break :n @intCast((if (r < 2) r else battle.rng.next() & 3) + 2);
1501
+ };
1502
+
1503
+ assert(n >= 2 and n <= 5);
1504
+ try options.chance.multiHit(player, n);
1505
+ return n;
1506
+ }
1507
+
1508
+ inline fn psywave(battle: anytype, player: Player, max: u8, options: anytype) !u8 {
1509
+ const power = if (options.calc.overridden(player, .psywave)) |val|
1510
+ val - 1
1511
+ else if (showdown)
1512
+ battle.rng.range(u8, 0, max)
1513
+ else power: {
1514
+ while (true) {
1515
+ const r = battle.rng.next();
1516
+ if (r < max) break :power r;
1517
+ }
1518
+ };
1519
+
1520
+ assert(power < max);
1521
+ try options.chance.psywave(player, power, max);
1522
+ return power;
1523
+ }
1524
+
1525
+ inline fn metronome(battle: anytype, player: Player, options: anytype) !Move {
1526
+ const moves = battle.get(player).active.moves;
1527
+
1528
+ var n: u2 = 0;
1529
+ for (moves) |m| {
1530
+ if (m == .None) break;
1531
+ if (Move.get(m).metronome) n += 1;
1532
+ }
1533
+
1534
+ // TODO: consider throwing error instead of rerolling?
1535
+ const overridden = if (options.calc.overridden(player, .metronome)) |val| val: {
1536
+ for (moves) |m| if (m.id == val) break :val null;
1537
+ break :val val;
1538
+ } else null;
1539
+
1540
+ const move: Move = overridden orelse move: {
1541
+ if (showdown) {
1542
+ var r = battle.rng.range(u8, 0, Move.METRONOME.len - n);
1543
+ for (moves) |m| {
1544
+ if (m == .None or @intFromEnum(m) > @intFromEnum(Move.METRONOME[r])) break;
1545
+ if (Move.get(m).metronome) r -= 1;
1546
+ }
1547
+ break :move Move.METRONOME[r];
1548
+ } else {
1549
+ outer: while (true) {
1550
+ const r = battle.rng.next();
1551
+ if (r == 0 or r >= Move.size) continue;
1552
+ const selected: Move = @enumFromInt(r);
1553
+ if (!Move.get(selected).extra.metronome) continue;
1554
+ for (moves) |m| if (m.id == selected) continue :outer;
1555
+ break :move selected;
1556
+ }
1557
+ }
1558
+ };
1559
+
1560
+ assert(Move.get(move).metronome);
1561
+ try options.chance.metronome(player, move, n);
1562
+ return move;
1563
+ }
1564
+
1565
+ inline fn sleepDuration(battle: anytype, player: Player, options: anytype) u3 {
1566
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
1567
+ @intCast(val)
1568
+ else if (showdown)
1569
+ battle.rng.range(u3, 1, 7)
1570
+ else duration: {
1571
+ while (true) {
1572
+ const r = battle.rng.next() & 7;
1573
+ if (r != 0 and r != 7) break :duration @intCast(r);
1574
+ }
1575
+ };
1576
+
1577
+ assert(duration >= 1 and duration <= 6);
1578
+ options.chance.duration(.sleep, player, player.foe(), duration);
1579
+ return duration + 1;
1580
+ }
1581
+
1582
+ inline fn confusionDuration(battle: anytype, player: Player, self: bool, options: anytype) u3 {
1583
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
1584
+ @intCast(val)
1585
+ else if (showdown)
1586
+ // Pokémon Showdown incorrectly uses the same duration for self-confusion
1587
+ battle.rng.range(u3, 2, 6)
1588
+ else
1589
+ @intCast((battle.rng.next() & if (self) 1 else 3) + 2);
1590
+
1591
+ assert(duration >= 2 and duration <= if (!showdown and self) 3 else 5);
1592
+ options.chance.duration(.confusion, player, if (self) player else player.foe(), duration);
1593
+ return duration;
1594
+ }
1595
+
1596
+ inline fn disableDuration(battle: anytype, player: Player, options: anytype) u4 {
1597
+ const duration: u4 = if (options.calc.overridden(player, .duration)) |val|
1598
+ @intCast(val)
1599
+ else if (showdown)
1600
+ // Pokémon Showdown incorrectly inherits Generation III's Disable duration
1601
+ battle.rng.range(u4, 2, 6)
1602
+ else duration: {
1603
+ while (true) {
1604
+ const r = battle.rng.next() & 7;
1605
+ if (r != 0) break :duration @intCast(r + 1);
1606
+ }
1607
+ };
1608
+
1609
+ assert(duration >= 2 and duration <= (if (showdown) 5 else 7));
1610
+ options.chance.duration(.disable, player, player.foe(), duration);
1611
+ return duration;
1612
+ }
1613
+
1614
+ inline fn attackingDuration(battle: anytype, player: Player, options: anytype) u3 {
1615
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
1616
+ @intCast(val)
1617
+ else if (showdown)
1618
+ battle.rng.range(u3, 2, 4)
1619
+ else
1620
+ @intCast((battle.rng.next() & 1) + 2); // BUG: thrash is + 1 not +2
1621
+
1622
+ assert(duration >= 2 and duration <= 3);
1623
+ options.chance.duration(.attacking, player, player, duration);
1624
+ return duration;
1625
+ }
1626
+
1627
+ inline fn bindingDuration(battle: anytype, player: Player, options: anytype) u3 {
1628
+ const duration: u3 = (if (options.calc.overridden(player, .duration)) |val|
1629
+ @intCast(val)
1630
+ else if (showdown)
1631
+ // TODO: PS does range(3, 6) here which after subtracting 1 is actually 2-4 not 2-5!
1632
+ battle.rng.range(u3, 2, 5)
1633
+ else
1634
+ @intCast(battle.rng.next() & 3 + 2));
1635
+
1636
+ assert(duration >= 2 and duration <= 5);
1637
+ options.chance.duration(.binding, player, player.foe(), duration);
1638
+ return duration + 1;
1639
+ }
1640
+
1641
+ // TODO: consider sharing implementation with bindingDuration
1642
+ inline fn encoreDuration(battle: anytype, player: Player, options: anytype) u3 {
1643
+ const duration: u3 = (if (options.calc.overridden(player, .duration)) |val|
1644
+ @intCast(val)
1645
+ else if (showdown)
1646
+ battle.rng.range(u3, 2, 6)
1647
+ else
1648
+ @intCast(battle.rng.next() & 3 + 2));
1649
+
1650
+ assert(duration >= 2 and duration <= 5);
1651
+ options.chance.duration(.encore, player, player.foe(), duration);
1652
+ return duration + 1;
1653
+ }
1654
+ };
1655
+
1656
+ test "RNG agreement" {
1657
+ if (!showdown) return;
1658
+ var expected: [256]u32 = undefined;
1659
+ for (0..expected.len) |i| {
1660
+ expected[i] = @intCast(i * 0x1000000);
1661
+ }
1662
+
1663
+ var spe: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1664
+ var qkc: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1665
+ var cfz: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1666
+ var atr: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1667
+ var frz: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1668
+ var fcb: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1669
+
1670
+ for (0..expected.len) |i| {
1671
+ try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
1672
+ try expectEqual(qkc.chance(u8, 60, 256), i < 60);
1673
+ try expectEqual(!cfz.chance(u8, 128, 256), i >= Gen12.percent(50) + 1);
1674
+ try expectEqual(atr.chance(u8, 1, 2), i < Gen12.percent(50) + 1);
1675
+ try expectEqual(frz.chance(u8, 25, 256), i < Gen12.percent(10));
1676
+ try expectEqual(fcb.chance(u8, 30, 256), !(30 <= i));
1677
+ }
1678
+ }
1679
+
1680
+ test "Roll probabilities" {
1681
+ if (showdown) return;
1682
+
1683
+ var present = [4]u8{ 0, 0, 0, 0 };
1684
+ var magnitude = [7]u8{ 0, 0, 0, 0, 0, 0, 0 };
1685
+ for (0..256) |i| {
1686
+ present[
1687
+ if (i <= Gen12.percent(40))
1688
+ 1
1689
+ else if (i <= Gen12.percent(70) + 1)
1690
+ 2
1691
+ else if (i <= Gen12.percent(80))
1692
+ 3
1693
+ else
1694
+ 0
1695
+ ] += 1;
1696
+ magnitude[
1697
+ if (i <= Gen12.percent(5) + 1)
1698
+ 0
1699
+ else if (i <= Gen12.percent(15) + 1)
1700
+ 1
1701
+ else if (i <= Gen12.percent(35))
1702
+ 2
1703
+ else if (i <= Gen12.percent(65) + 1)
1704
+ 3
1705
+ else if (i <= Gen12.percent(85) + 1)
1706
+ 4
1707
+ else if (i <= Gen12.percent(95))
1708
+ 5
1709
+ else
1710
+ 6
1711
+ ] += 1;
1712
+ }
1713
+
1714
+ try expectEqualSlices(u8, &.{ 51, 103, 77, 25 }, &present);
1715
+ try expectEqualSlices(u8, &.{ 14, 26, 50, 77, 51, 25, 13 }, &magnitude);
1716
+ }
1717
+
1718
+ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
1719
+ var n: u8 = 0;
1720
+ switch (request) {
1721
+ .Pass => {
1722
+ out[n] = .{};
1723
+ n += 1;
1724
+ },
1725
+ .Switch => {
1726
+ const side = battle.side(player);
1727
+ var slot: u4 = 2;
1728
+ while (slot <= 6) : (slot += 1) {
1729
+ const id = side.order[slot - 1];
1730
+ if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
1731
+ out[n] = .{ .type = .Switch, .data = slot };
1732
+ n += 1;
1733
+ }
1734
+ if (n == 0) {
1735
+ out[n] = .{};
1736
+ n += 1;
1737
+ }
1738
+ },
1739
+ .Move => {
1740
+ const side = battle.side(player);
1741
+
1742
+ var active = &side.active;
1743
+ const stored = side.stored();
1744
+
1745
+ // While players are not given any input options on the cartridge in these cases,
1746
+ // Pokémon Showdown instead produces a list with a single move that must be chosen.
1747
+ //
1748
+ // Given that no input is allowed on the cartridge 'Pass' seems like it would be logical
1749
+ // here when not in compatibility mode, but the engine needs to be able to differentiate
1750
+ // between passing while waiting for an opponent's forced switch after fainting and
1751
+ // passing due to being forced into using a move. Instead of introducing another option
1752
+ // we simply repurpose Move with no move slot, even though pedantically this is not
1753
+ // strictly correct as the player would not have been presented the option to move or
1754
+ // switch at all.
1755
+ if (isForced(active)) {
1756
+ out[n] = .{ .type = .Move, .data = @intFromBool(showdown) };
1757
+ n += 1;
1758
+ return n;
1759
+ }
1760
+
1761
+ var slot: u4 = 2;
1762
+ while (!active.volatiles.trapped and slot <= 6) : (slot += 1) {
1763
+ const id = side.order[slot - 1];
1764
+ if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
1765
+ out[n] = .{ .type = .Switch, .data = slot };
1766
+ n += 1;
1767
+ }
1768
+
1769
+ const limited = active.volatiles.Bide or active.volatiles.bind > 0;
1770
+ // On the cartridge, all of these happen after "FIGHT" (indicating you are not
1771
+ // switching) but before you are allowed to select a move. Pokémon Showdown instead
1772
+ // either disables all other moves in the case of limited or requires you to select a
1773
+ // move normally if sleeping/frozen/bound.
1774
+ if (!showdown and (limited or
1775
+ Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP)))
1776
+ {
1777
+ out[n] = .{ .type = .Move, .data = 0 };
1778
+ n += 1;
1779
+ return n;
1780
+ }
1781
+
1782
+ slot = 1;
1783
+ // Pokémon Showdown handles Bide and Binding moves by checking if the move in question
1784
+ // is present in the Pokémon's moveset (which means moves called via Metronome / Mirror
1785
+ // Move will not result in forcing the subsequent use unless the user also had the
1786
+ // proc-ed move in their moveset) and disabling all other moves.
1787
+ if (limited) {
1788
+ assert(showdown);
1789
+ assert(side.last_selected_move != .None);
1790
+ while (slot <= 4) : (slot += 1) {
1791
+ const m = active.moves[slot - 1];
1792
+ if (m.id == .None) break;
1793
+ if (m.id == if (active.volatiles.Bide) .Bide else side.last_selected_move) {
1794
+ // Pokémon Showdown displays Struggle if limited to Bide but unable to pick
1795
+ const struggle =
1796
+ m.id == .Bide and (m.pp == 0 or active.volatiles.disabled_move == slot);
1797
+ const s = if (struggle) 0 else slot;
1798
+ out[n] = .{ .type = .Move, .data = s };
1799
+ n += 1;
1800
+ return n;
1801
+ }
1802
+ }
1803
+ }
1804
+
1805
+ const before = n;
1806
+ slot = 1;
1807
+ while (slot <= 4) : (slot += 1) {
1808
+ const m = active.moves[slot - 1];
1809
+ if (m.id == .None) break;
1810
+ if (m.pp == 0) continue;
1811
+ if (active.volatiles.disabled_move == slot) continue;
1812
+ out[n] = .{ .type = .Move, .data = slot };
1813
+ n += 1;
1814
+ }
1815
+ // Struggle (Pokémon Showdown would use 'move 1' here)
1816
+ if (n == before) {
1817
+ out[n] = .{ .type = .Move, .data = 0 };
1818
+ n += 1;
1819
+ }
1820
+ },
1821
+ }
1822
+ return n;
1823
+ }