@pkmn/engine 0.1.0-dev.6195e056 → 0.1.0-dev.6e63f5e4

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 +101 -62
  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/layout.json +25 -3
  6. package/build/pkg/data/protocol.json +22 -4
  7. package/build/pkg/data.d.ts +1 -2
  8. package/build/pkg/data.js +3 -5
  9. package/build/pkg/data.js.map +1 -1
  10. package/build/pkg/gen1.d.ts +5 -4
  11. package/build/pkg/gen1.js +47 -31
  12. package/build/pkg/gen1.js.map +1 -1
  13. package/build/pkg/index.d.ts +16 -13
  14. package/build/pkg/index.js +1 -1
  15. package/build/pkg/index.js.map +1 -1
  16. package/build/pkg/protocol.d.ts +4 -2
  17. package/build/pkg/protocol.js +5 -3
  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 +57 -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 +74 -0
  32. package/src/lib/common/js.zig +118 -0
  33. package/src/lib/common/optional.zig +60 -0
  34. package/src/lib/common/options.zig +12 -3
  35. package/src/lib/common/protocol.zig +416 -198
  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 +282 -165
  40. package/src/lib/gen1/calc.zig +426 -0
  41. package/src/lib/gen1/chance.zig +1058 -0
  42. package/src/lib/gen1/data/moves.zig +178 -180
  43. package/src/lib/gen1/data/species.zig +2 -2
  44. package/src/lib/gen1/data/types.zig +4 -4
  45. package/src/lib/gen1/data.zig +79 -65
  46. package/src/lib/gen1/helpers.zig +592 -36
  47. package/src/lib/gen1/mechanics.zig +1097 -662
  48. package/src/lib/gen2/README.md +87 -13
  49. package/src/lib/gen2/data/items.zig +3 -3
  50. package/src/lib/gen2/data/moves.zig +635 -563
  51. package/src/lib/gen2/data/species.zig +1 -1
  52. package/src/lib/gen2/data/types.zig +4 -4
  53. package/src/lib/gen2/data.zig +109 -77
  54. package/src/lib/gen2/helpers.zig +16 -22
  55. package/src/lib/gen2/mechanics.zig +1160 -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
 
@@ -17,8 +18,9 @@ const ID = common.ID;
17
18
  const Player = common.Player;
18
19
  const Result = common.Result;
19
20
 
20
- const showdown = options.showdown;
21
+ const showdown = pkmn.options.showdown;
21
22
 
23
+ const Cant = protocol.Cant;
22
24
  const Damage = protocol.Damage;
23
25
  const Heal = protocol.Heal;
24
26
 
@@ -33,21 +35,1168 @@ const Side = data.Side;
33
35
  const Species = data.Species;
34
36
  const Stats = data.Stats;
35
37
  const Status = data.Status;
38
+ const Type = data.Type;
39
+ const Types = data.Types;
40
+ const Volatiles = data.Volatiles;
36
41
 
37
- pub fn update(battle: anytype, c1: Choice, c2: Choice, log: anytype) !Result {
38
- _ = battle;
39
- _ = c1;
40
- _ = c2;
41
- _ = log;
42
+ // zig fmt: off
43
+ const STAT_BOOSTS = &[_][2]u8{
44
+ .{ 25, 100 }, // -6
45
+ .{ 28, 100 }, // -5
46
+ .{ 33, 100 }, // -4
47
+ .{ 40, 100 }, // -3
48
+ .{ 50, 100 }, // -2
49
+ .{ 66, 100 }, // -1
50
+ .{ 1, 1 }, // 0
51
+ .{ 15, 10 }, // +1
52
+ .{ 2, 1 }, // +2
53
+ .{ 25, 10 }, // +3
54
+ .{ 3, 1 }, // +4
55
+ .{ 35, 10 }, // +5
56
+ .{ 4, 1 }, // +6
57
+ };
42
58
 
43
- return Result.Default;
59
+ const ACCURACY_BOOSTS = &[_][2]u8{
60
+ .{ 33, 100 }, // -6
61
+ .{ 36, 100 }, // -5
62
+ .{ 43, 100 }, // -4
63
+ .{ 50, 100 }, // -3
64
+ .{ 60, 100 }, // -2
65
+ .{ 75, 100 }, // -1
66
+ .{ 1, 1 }, // 0
67
+ .{ 133, 100 }, // +1
68
+ .{ 166, 100 }, // +2
69
+ .{ 2, 1 }, // +3
70
+ .{ 233, 100 }, // +4
71
+ .{ 133, 50 }, // +5
72
+ .{ 3, 1 }, // +6
73
+ };
74
+
75
+ const CRITICAL_HITS = &[_]u8{
76
+ 256 / 15, // 0
77
+ 256 / 8, // +1
78
+ 256 / 4, // +2
79
+ 256 / 3, // +3
80
+ 256 / 2, // +4
81
+ };
82
+ // zig fmt: on
83
+
84
+ const MAX_STAT_VALUE = 999;
85
+
86
+ const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
87
+
88
+ pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result {
89
+ assert(c1.type != .Pass or c2.type != .Pass or battle.turn == 0);
90
+ if (battle.turn == 0) return start(battle, options);
91
+
92
+ if (try turnOrder(battle, c1, c2, options) == .P1) {
93
+ if (try doTurn(battle, .P1, c1, .P2, c2, options)) |r| return r;
94
+ } else {
95
+ if (try doTurn(battle, .P2, c2, .P1, c1, options)) |r| return r;
96
+ }
97
+
98
+ return endTurn(battle, options);
44
99
  }
45
100
 
46
- pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
101
+ fn start(battle: anytype, options: anytype) !Result {
102
+ const p1 = battle.side(.P1);
103
+ const p2 = battle.side(.P2);
104
+
105
+ var p1_slot = findFirstAlive(p1);
106
+ assert(!showdown or p1_slot == 1);
107
+ if (p1_slot == 0) return if (findFirstAlive(p2) == 0) Result.Tie else Result.Lose;
108
+
109
+ var p2_slot = findFirstAlive(p2);
110
+ assert(!showdown or p2_slot == 1);
111
+ if (p2_slot == 0) return Result.Win;
112
+
113
+ try switchIn(battle, .P1, p1_slot, true, options);
114
+ try switchIn(battle, .P2, p2_slot, true, options);
115
+
116
+ return endTurn(battle, options);
117
+ }
118
+
119
+ fn findFirstAlive(side: *const Side) u8 {
120
+ for (side.pokemon, 0..) |pokemon, i| if (pokemon.hp > 0) return side.pokemon[i].position;
121
+ return 0;
122
+ }
123
+
124
+ fn selectMove(battle: anytype, player: Player, choice: Choice) void {
125
+ if (choice.type == .Pass) return; // FIXME BATON PASS
126
+
127
+ var side = battle.side(player);
128
+ var volatiles = &side.active.volatiles;
129
+
130
+ // pre-battle menu
131
+ const forced = forced: {
132
+ if (volatiles.Recharging) break :forced true;
133
+ // Pokémon Showdown removes Flinch at the end-of-turn in its residual handler
134
+ if (!showdown) {
135
+ volatiles.Flinch = false;
136
+ battle.foe(player).active.volatiles.Flinch = false;
137
+ }
138
+ break :forced (volatiles.Thrashing or volatiles.Charging or volatiles.Rollout);
139
+ };
140
+
141
+ // battle menu
142
+ if (choice.type == .Switch) return;
143
+ assert(choice.type == .Move and (!forced or choice.data == @intFromBool(showdown)));
144
+
145
+ done: {
146
+ if (forced) break :done;
147
+ // pre-move select
148
+ if (volatiles.Encore) {
149
+ // TODO wCurPlayerMove = side.lastMove(true);
150
+ // assert(wCurPlayerMove != .None);
151
+ volatiles.Bide = false; // FIXME wPlayerCharging vs. SUBSTATUS_BIDE?
152
+ const effect = Move.get(.Pound).effect; // TODO
153
+ if (effect != .FuryCutter) volatiles.fury_cutter = false;
154
+ if (effect != .Rage) {
155
+ volatiles.Rage = false;
156
+ volatiles.rage = 0;
157
+ }
158
+ if (effect != .Protect and effect != .Endure) volatiles.protect = 0;
159
+ return;
160
+ }
161
+ if (volatiles.Bide) break :done;
162
+ // move select
163
+ if (choice.data == 0) {
164
+ const struggle = ok: {
165
+ for (side.active.moves, 0..) |move, i| {
166
+ if (move.pp > 0 and volatiles.disabled_move != i + 1) break :ok false;
167
+ }
168
+ break :ok true;
169
+ };
170
+ assert(struggle);
171
+ // TODO wCurPlayerMove = .Struggle;
172
+ } else {
173
+ assert(showdown or side.active.volatiles.disabled_move != choice.data);
174
+ const move = side.active.move(choice.data);
175
+ // You cannot *select* a move with 0 PP
176
+ assert(move.pp != 0);
177
+ // TODO wCurPlayerMove = move.id;
178
+ // battle.lastMove(player).index = @intCast(c.data);
179
+ }
180
+ }
181
+
182
+ volatiles.Rage = false;
183
+ volatiles.rage = 0;
184
+ volatiles.fury_cutter = 0;
185
+ volatiles.protect = 0;
186
+ }
187
+
188
+ fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, options: anytype) !void {
189
+ var side = battle.side(player);
190
+ var foe = battle.foe(player);
191
+
192
+ var active = &side.active;
193
+ const incoming = side.get(slot);
194
+
195
+ assert(incoming.hp != 0);
196
+ assert(slot != 1 or initial);
197
+
198
+ const out = side.pokemon[0].position;
199
+ side.pokemon[0].position = side.pokemon[slot - 1].position;
200
+ side.pokemon[slot - 1].position = out;
201
+
202
+ side.last_used_move = .None;
203
+ foe.active.volatiles.dirty = true;
204
+
205
+ active.volatiles = .{};
206
+ active.stats = incoming.stats;
207
+ active.moves = incoming.moves;
208
+ active.boosts = .{};
209
+ active.types = incoming.types;
210
+ active.species = incoming.species;
211
+
212
+ statusModify(incoming.status, &active.stats);
213
+
214
+ try options.log.switched(battle.active(player), incoming);
215
+ options.chance.switched(player, side.pokemon[0].position, out);
216
+
217
+ if (side.conditions.Spikes and !active.types.includes(.Flying)) {
218
+ incoming.hp -|= @max(incoming.stats.hp / 8, 1);
219
+ try options.log.damage(battle.active(player), incoming, .Spikes);
220
+ }
221
+ }
222
+
223
+ fn turnOrder(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Player {
224
+ assert(c1.type != .Pass or c2.type != .Pass);
225
+
226
+ if (c1.type == .Pass) return .P2;
227
+ if (c2.type == .Pass) return .P1;
228
+
229
+ // Pokémon Showdown always rolls for Quick Claw every turn, even if no Pokémon hold Quick Claw
230
+ // TODO: add to pending and only commit roll on Pokémon Showdown if roll is used
231
+ const qkc = showdown and try Rolls.quickClaw(battle, .P1, options);
232
+
233
+ if ((c1.type == .Switch) != (c2.type == .Switch)) return if (c1.type == .Switch) .P1 else .P2;
234
+
235
+ // https://www.smogon.com/forums/threads/adv-switch-priority.3622189/
236
+ // > In Gen 1 it's irrelevant [which player switches first] because switches happen instantly on
237
+ // > your own screen without waiting for the other player's choice (and their choice will appear
238
+ // > to happen first for them too, unless they attacked in which case your switch happens first)
239
+ // A cartridge-compatible implemention must not advance the RNG so we simply default to P1
240
+ const double_switch = c1.type == .Switch and c2.type == .Switch;
241
+ if (!showdown and double_switch) return .P1;
242
+
243
+ const m1 = .Pound; // TODO battle.side(.P1).last_selected_move;
244
+ const m2 = .Pound; // TODO battle.side(.P2).last_selected_move;
245
+ if (!showdown or !double_switch) {
246
+ const pri1 = Move.get(m1).priority;
247
+ const pri2 = Move.get(m2).priority;
248
+ if (pri1 != pri2) return if (pri1 > pri2) .P1 else .P2;
249
+
250
+ const qkc1 = battle.side(.P1).stored().item == .QuickClaw;
251
+ const qkc2 = battle.side(.P2).stored().item == .QuickClaw;
252
+ if (!showdown and qkc1 and qkc2) {
253
+ // If both Pokémon have Quick Claw the cartridge checks them in order and exits early if
254
+ // the first one procs, meaning the host has a slightly higher chance of going first
255
+ if (try Rolls.quickClaw(battle, .P1, options)) return .P1;
256
+ if (try Rolls.quickClaw(battle, .P2, options)) return .P2;
257
+ } else if (qkc1) {
258
+ const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P1, options);
259
+ if (proc) return .P1;
260
+ } else if (qkc2) {
261
+ const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P2, options);
262
+ if (proc) return .P2;
263
+ }
264
+ }
265
+
266
+ const spe1 = battle.side(.P1).active.stats.spe;
267
+ const spe2 = battle.side(.P2).active.stats.spe;
268
+ if (spe1 == spe2) {
269
+ // Pokémon Showdown's beforeTurnCallback shenanigans
270
+ if (showdown and !double_switch and hasCallback(m1) and hasCallback(m2)) {
271
+ battle.rng.advance(1);
272
+ }
273
+
274
+ const p1 = try Rolls.speedTie(battle, options);
275
+ if (!showdown) return if (p1) .P1 else .P2;
276
+
277
+ // Pokémon Showdown's "lockedmove" volatile's onBeforeTurn uses BattleQueue#changeAction,
278
+ // meaning that if a side is locked into a thrashing move and wins the speed tie, it
279
+ // actually uses its priority to simply insert its actual changed action into the queue,
280
+ // causing it to then execute *after* the side which should go second...
281
+ const t1 = battle.side(.P1).active.volatiles.Thrashing;
282
+ const t2 = battle.side(.P2).active.volatiles.Thrashing;
283
+ // If *both* sides are thrashing it really should be another speed tie, but we've patched
284
+ // that out and enforce host ordering of events, so P1 just goes first regardless of who
285
+ // won the original coin flip
286
+ if (t1 and t2) return .P1;
287
+ return if (p1) if (t1 and !t2) .P2 else .P1 else if (t2 and !t1) .P1 else .P2;
288
+ }
289
+
290
+ return if (spe1 > spe2) .P1 else .P2;
291
+ }
292
+
293
+ fn hasCallback(m: Move) bool {
294
+ return switch (m) {
295
+ .Counter, .MirrorCoat, .Pursuit => true,
296
+ else => false,
297
+ };
298
+ }
299
+
300
+ fn doTurn(
301
+ battle: anytype,
302
+ player: Player,
303
+ player_choice: Choice,
304
+ foe_player: Player,
305
+ foe_choice: Choice,
306
+ options: anytype,
307
+ ) !?Result {
308
+ assert(player_choice.type != .Pass);
309
+
310
+ const volatiles = &battle.side(player).active.volatiles;
311
+ const foe_volatiles = &battle.foe(player).active.volatiles;
312
+
313
+ volatiles.DestinyBond = false;
314
+ if (try executeMove(battle, player, player_choice, options)) |r| return r;
315
+ foe_volatiles.Protect = false;
316
+ foe_volatiles.Endure = false;
317
+ foe_volatiles.DestinyBond = false;
318
+
319
+ // foe_volatiles.DestinyBond = false;
320
+ if (try executeMove(battle, foe_player, foe_choice, options)) |r| return r;
321
+ volatiles.Protect = false;
322
+ volatiles.Endure = false;
323
+ volatiles.DestinyBond = false;
324
+
325
+ return null;
326
+ }
327
+
328
+ fn executeMove(battle: anytype, player: Player, choice: Choice, options: anytype) !?Result {
329
+ if (choice.type == .Switch) {
330
+ try switchIn(battle, player, choice.data, false, options);
331
+ return null;
332
+ }
333
+
334
+ assert(choice.type == .Move);
335
+ var mslot: u4 = @intCast(choice.data);
336
+
337
+ // const move = Move.get(side.last_selected_move);
338
+
339
+ // CheckTurn
340
+
341
+ return doMove(battle, player, options, mslot);
342
+ }
343
+
344
+ fn beforeMove(battle: anytype, player: Player, options: anytype) !bool {
345
+ var log = options.log;
346
+ var side = battle.side(player);
347
+ // const foe = battle.foe(player);
348
+ var active = &side.active;
349
+ var stored = side.stored();
350
+ const ident = battle.active(player);
351
+ var volatiles = &active.volatiles;
352
+
353
+ const move = Move.Pound; // TODO
354
+
355
+ if (volatiles.Recharging) {
356
+ volatiles.Recharging = false;
357
+ cantMove(volatiles);
358
+ try log.cant(ident, .Recharge);
359
+ return false;
360
+ }
361
+
362
+ if (Status.is(stored.status, .SLP)) slp: {
363
+ const before = stored.status;
364
+ // Even if the EXT bit is set this will still correctly modify the sleep duration
365
+ // if (options.calc.modify(player, .sleep)) |extend| {
366
+ // if (!extend) stored.status = 0;
367
+ // } else {
368
+ stored.status -= 1;
369
+ // }
370
+
371
+ const duration = Status.duration(stored.status);
372
+ if (!Status.is(stored.status, .EXT)) {
373
+ try options.chance.sleep(player, duration);
374
+ }
375
+
376
+ if (duration == 0) {
377
+ try log.curestatus(ident, before, .Message);
378
+ stored.status = 0; // clears EXT if present
379
+ cantMove(volatiles);
380
+ volatiles.Nightmare = false;
381
+ } else {
382
+ try log.cant(ident, .Sleep);
383
+ if (move == .Snore or move == .SleepTalk) break :slp;
384
+ cantMove(volatiles);
385
+ return false;
386
+ }
387
+ }
388
+
389
+ if (Status.is(stored.status, .FRZ) and !(move == .FlameWheel or move == .SacredFire)) {
390
+ cantMove(volatiles);
391
+ try log.cant(ident, .Freeze);
392
+ return false;
393
+ }
394
+
395
+ if (volatiles.Flinch) {
396
+ // Pokémon Showdown doesn't clear Flinch until its imaginary "residual" phase, meaning
397
+ // Pokémon can sometimes flinch multiple times from the same original hit
398
+ // if (!showdown) volatiles.Flinch = false;
399
+ volatiles.Flinch = false;
400
+ cantMove(volatiles);
401
+ try log.cant(ident, .Flinch);
402
+ return false;
403
+ }
404
+
405
+ if (volatiles.disabled_duration > 0) {
406
+ // if (options.calc.modify(player, .disable)) |extend| {
407
+ // if (!extend) volatiles.disabled_duration = 0;
408
+ // } else {
409
+ volatiles.disabled_duration -= 1;
410
+ // }
411
+ try options.chance.disable(player, volatiles.disabled_duration);
412
+
413
+ if (volatiles.disabled_duration == 0) {
414
+ volatiles.disabled_move = 0;
415
+ try log.end(ident, .Disable);
416
+ }
417
+ }
418
+
419
+ if (volatiles.Confusion) {
420
+ assert(volatiles.confusion > 0);
421
+
422
+ // if (options.calc.modify(player, .confusion)) |extend| {
423
+ // if (!extend) volatiles.confusion = 0;
424
+ // } else {
425
+ volatiles.confusion -= 1;
426
+ // }
427
+ try options.chance.confusion(player, volatiles.confusion);
428
+
429
+ if (volatiles.confusion == 0) {
430
+ volatiles.Confusion = false;
431
+ try log.end(ident, .Confusion);
432
+ } else {
433
+ try log.activate(ident, .Confusion);
434
+
435
+ if (try Rolls.confused(battle, player, options)) {
436
+ // TODO: see if gets merged?
437
+ // volatiles.BeatUp = false;
438
+ // volatiles.Flinch = false;
439
+ // cantMove(volatiles);
440
+
441
+ volatiles.Bide = false;
442
+ volatiles.Thrashing = false;
443
+ volatiles.BeatUp = false;
444
+ volatiles.Flinch = false;
445
+ volatiles.Charging = false;
446
+ volatiles.Underground = false;
447
+ volatiles.Flying = false;
448
+
449
+ volatiles.Rollout = false;
450
+
451
+ volatiles.fury_cutter = 0;
452
+
453
+ // TODO HitConfusion
454
+
455
+ return false;
456
+ }
457
+ }
458
+ }
459
+
460
+ if (volatiles.Attract) {
461
+ try log.activate(ident, .Attract);
462
+
463
+ if (try Rolls.attract(battle, player, options)) {
464
+ cantMove(volatiles);
465
+ try log.cant(ident, .Attract);
466
+ return false;
467
+ }
468
+ }
469
+
470
+ if (side.active.volatiles.disabled_move != 0) {
471
+ // A Pokémon that transforms after being disabled may end up with less move slots
472
+ const m = side.active.moves[side.active.volatiles.disabled_move - 1].id;
473
+ if (m != .None and m == move) {
474
+ side.active.volatiles.Charging = false;
475
+ cantMove(volatiles);
476
+ try options.log.disabled(ident, move);
477
+ return false;
478
+ }
479
+ }
480
+
481
+ if (Status.is(stored.status, .PAR) and try Rolls.paralyzed(battle, player, options)) {
482
+ cantMove(volatiles);
483
+ try log.cant(ident, .Paralysis);
484
+ return false;
485
+ }
486
+
487
+ return true;
488
+ }
489
+
490
+ fn cantMove(volatiles: *Volatiles) void {
491
+ volatiles.Bide = false;
492
+ volatiles.Thrashing = false;
493
+ volatiles.Charging = false;
494
+ volatiles.Underground = false;
495
+ volatiles.Flying = false;
496
+ volatiles.Rollout = false;
497
+
498
+ volatiles.fury_cutter = 0;
499
+ }
500
+
501
+ fn canMove(battle: anytype, player: Player, options: anytype) !bool {
47
502
  _ = battle;
48
503
  _ = player;
49
- _ = request;
50
- _ = out;
504
+ _ = options;
505
+ return false;
506
+ }
507
+
508
+ fn decrementPP(side: *Side, mslot: u4) void {
509
+ var active = &side.active;
510
+ const volatiles = &active.volatiles;
511
+
512
+ // TODO assert(side.last_selected_move != .Struggle);
513
+ assert(!volatiles.Charging and !volatiles.BeatUp and !volatiles.Thrashing and !volatiles.Bide);
514
+
515
+ assert(active.move(mslot).pp > 0);
516
+ active.move(mslot).pp = @as(u6, @intCast(active.move(mslot).pp)) -% 1;
517
+ if (volatiles.Transform) return;
51
518
 
519
+ // FIXME mimic
520
+
521
+ assert(side.stored().move(mslot).pp > 0);
522
+ side.stored().move(mslot).pp = @as(u6, @intCast(side.stored().move(mslot).pp)) -% 1;
523
+ assert(active.move(mslot).pp == side.stored().move(mslot).pp);
524
+ }
525
+
526
+ fn doMove(battle: anytype, player: Player, options: anytype, mslot: u4) !?Result {
527
+ _ = options;
528
+ var side = battle.side(player);
529
+ const move = Move.get(.Teleport); // side.last_selected_move
530
+ switch (move.effect) {
531
+ .Teleport => {
532
+ // side.useMove();
533
+ decrementPP(side, mslot);
534
+ },
535
+ else => @panic("TODO"),
536
+ }
537
+ return null;
538
+ }
539
+
540
+ fn checkCriticalHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
541
+ if (move.bp == 0) return false;
542
+
543
+ const side = battle.side(player);
544
+ const pokemon = side.stored();
545
+
546
+ var stage: u8 =
547
+ if ((pokemon.species == .Chansey and pokemon.item == .LuckyPunch) or
548
+ (pokemon.species == .Farfetchd and pokemon.item == .Stick)) 2 else 0;
549
+ if (stage == 0) {
550
+ if (side.active.volatiles.FocusEnergy) stage += 1;
551
+ if (move.effect.isHighCritical()) stage += 2;
552
+ if (pokemon.item == .ScopeLens) stage += 1;
553
+ }
554
+ assert(stage <= 4);
555
+
556
+ return Rolls.criticalHit(battle, player, CRITICAL_HITS[stage], options);
557
+ }
558
+
559
+ fn calcDamage(
560
+ battle: anytype,
561
+ player: Player,
562
+ target_player: Player,
563
+ m: ?Move.Data,
564
+ crit: bool,
565
+ ) u16 {
566
+ _ = battle;
567
+ _ = player;
568
+ _ = target_player;
569
+ _ = m;
570
+ _ = crit;
52
571
  return 0;
53
572
  }
573
+ const FORESIGHT: Types = .{ .type1 = .Normal, .type2 = .Fighting };
574
+
575
+ fn adjustDamage(battle: anytype, player: Player, m: Move, damage: u16, effectiveness: *u16) u16 {
576
+ if (m == .Struggle) return;
577
+
578
+ const side = battle.side(player);
579
+ const foe = battle.foe(player);
580
+ const types = foe.active.types;
581
+ const move = Move.get(side.m);
582
+
583
+ var d = damage;
584
+ if ((move.type == .Water and battle.field.weather == .Rain) or
585
+ (move.type == .Fire and battle.field.weather == .Sun))
586
+ {
587
+ d = d *% @intFromEnum(Effectiveness.Super) / 10;
588
+ } else if ((move.type == .Water and battle.field.weather == .Sun) or
589
+ ((move.type == .Fire or m == .SolarBeam) and battle.field.weather == .Rain))
590
+ {
591
+ d = d *% @intFromEnum(Effectiveness.Resisted) / 10;
592
+ }
593
+
594
+ if (side.active.types.includes(move.type)) d +%= d / 2;
595
+
596
+ const neutral = @intFromEnum(Effectiveness.Neutral);
597
+ const foresight = foe.active.volatiles.Foresight and FORESIGHT.includes(move.type);
598
+ const eff1: u16 = if (foresight and types.type1 == .Ghost)
599
+ 10
600
+ else
601
+ @intFromEnum(move.type.effectiveness(types.type1));
602
+ const eff2: u16 = if (foresight and types.type2 == .Ghost)
603
+ 10
604
+ else
605
+ @intFromEnum(move.type.effectiveness(types.type2));
606
+
607
+ // Type effectiveness matchup precedence only matters with (NVE, SE)
608
+ if (!showdown and (eff1 + eff2) == Effectiveness.mismatch and
609
+ Type.precedence(move.type, types.type1) > Type.precedence(move.type, types.type2))
610
+ {
611
+ assert(eff2 != neutral);
612
+ d = d *% eff2 / 10;
613
+ assert(types.type1 != types.type2);
614
+ assert(eff1 != neutral);
615
+ d = d *% eff1 / 10;
616
+ } else {
617
+ if (eff1 != neutral) d = d *% eff1 / 10;
618
+ if (types.type1 != types.type2 and eff2 != neutral) d = d *% eff2 / 10;
619
+ }
620
+
621
+ effectiveness.* = if (types.type1 == types.type2) eff1 * neutral else eff1 * eff2;
622
+ return d;
623
+ }
624
+
625
+ fn randomizeDamage(battle: anytype, player: Player, damage: u16, options: anytype) !u16 {
626
+ if (damage <= 1) return;
627
+ const random = try Rolls.damage(battle, player, options);
628
+ options.calc.base(player.foe(), damage);
629
+ damage = @intCast(@as(u32, damage) *% random / 255);
630
+ options.calc.final(player.foe(), damage);
631
+ return damage;
632
+ }
633
+
634
+ fn applyDamage(
635
+ battle: anytype,
636
+ player: Player,
637
+ options: anytype,
638
+ ) !bool {
639
+ _ = battle;
640
+ _ = player;
641
+ _ = options;
642
+ return false;
643
+ }
644
+
645
+ fn checkHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
646
+ if (moveHit(battle, player, move, options)) return true;
647
+
648
+ // try options.chance.commit(player, .miss);
649
+ try options.log.lastmiss();
650
+ try options.log.miss(battle.active(player));
651
+
652
+ return false;
653
+ }
654
+
655
+ // TODO: set damage = 0 if missed and not (High) Jump Kick
656
+ fn moveHit(
657
+ battle: anytype,
658
+ player: Player,
659
+ move: Move.Data,
660
+ options: anytype,
661
+ ) !bool {
662
+ const side = battle.side(player);
663
+ const foe = battle.foe(player);
664
+
665
+ if (move.effect == .DreamEater and !Status.is(foe.stored().status, .SLP)) return false;
666
+ if (foe.active.volatiles.Protect) return false;
667
+ const drain = move.effect == .DrainHP or move.effect == .DreamEater;
668
+ if (drain and foe.active.volatiles.Substitute) return false;
669
+ if (side.active.volatiles.LockOn) {
670
+ return !(foe.active.volatiles.Flying and move.flags.undeground);
671
+ }
672
+ assert(!(foe.active.volatiles.Flying and foe.active.volatiles.Underground));
673
+ if (foe.active.volatiles.Flying) {
674
+ if (!move.flags.flying) return false;
675
+ } else if (foe.active.volatiles.Underground) {
676
+ if (!move.flags.underground) return false;
677
+ }
678
+ if (move.effect == .Thunder and battle.field.weather == .Rain) return true;
679
+ if (move.effect == .AlwaysHit) return true;
680
+
681
+ var accuracy: u16 = move.accuracy;
682
+ if (foe.active.boosts.evasion <= side.active.boosts.accuracy or
683
+ !foe.active.volatiles.Foresight)
684
+ {
685
+ var boost = ACCURACY_BOOSTS[@as(u4, @intCast(@as(i8, side.active.boosts.accuracy) + 6))];
686
+ accuracy = accuracy * boost[0] / boost[1];
687
+ boost = ACCURACY_BOOSTS[@as(u4, @intCast(@as(i8, -foe.active.boosts.evasion) + 6))];
688
+ accuracy = accuracy * boost[0] / boost[1];
689
+ accuracy = @min(255, @max(1, accuracy));
690
+ }
691
+
692
+ if (foe.stored().item == .BrightPowder) {
693
+ accuracy = @min(0, accuracy -% 20);
694
+ }
695
+
696
+ // The accuracy roll is skipped entirely if maxed
697
+ if (accuracy == 255) return true;
698
+ return !try Rolls.hit(battle, player, @intCast(accuracy), options);
699
+ }
700
+
701
+ fn checkFaint(
702
+ battle: anytype,
703
+ player: Player,
704
+ options: anytype,
705
+ ) @TypeOf(options.log).Error!?Result {
706
+ _ = battle;
707
+ _ = player;
708
+ return null;
709
+ }
710
+
711
+ fn faint(battle: anytype, player: Player, done: bool, options: anytype) !?Result {
712
+ _ = battle;
713
+ _ = player;
714
+ _ = done;
715
+ _ = options;
716
+ return null;
717
+ }
718
+
719
+ fn handleResidual(battle: anytype, player: Player, options: anytype) !void {
720
+ var side = battle.side(player);
721
+ var stored = side.stored();
722
+ const ident = battle.active(player);
723
+ var volatiles = &side.active.volatiles;
724
+
725
+ // TODO if (stored.hp == 0) return;
726
+ assert(stored.hp > 0);
727
+
728
+ const brn = Status.is(stored.status, .BRN);
729
+ if (brn or Status.is(stored.status, .PSN)) {
730
+ var damage: u16 = undefined;
731
+ if (volatiles.Toxic) {
732
+ volatiles.toxic += 1;
733
+ damage = @max(stored.stats.hp / 16, 1) * volatiles.toxic;
734
+ } else {
735
+ damage = @max(stored.stats.hp / 8, 1);
736
+ }
737
+ damage = @min(damage, stored.hp);
738
+
739
+ if (!showdown or damage > 0) {
740
+ stored.hp -= damage;
741
+ // Pokémon Showdown uses damageOf here but its not relevant in Generation II
742
+ try options.log.damage(ident, stored, if (brn) Damage.Burn else Damage.Poison);
743
+ if (stored.hp == 0) return;
744
+ }
745
+ }
746
+
747
+ if (volatiles.LeechSeed) {
748
+ var foe = battle.foe(player);
749
+ var foe_stored = foe.stored();
750
+ const foe_ident = battle.active(player.foe());
751
+
752
+ // if (foe_stored.hp == 0) {
753
+ // assert(showdown);
754
+ // return;
755
+ // }
756
+
757
+ const damage = @min(@max(stored.stats.hp / 8, 1), stored.hp);
758
+ stored.hp -= damage;
759
+ // As above, Pokémon Showdown uses damageOf but its not relevant
760
+ if (damage > 0) try options.log.damage(ident, stored, .LeechSeed);
761
+
762
+ const before = foe_stored.hp;
763
+ foe_stored.hp = @min(foe_stored.hp + damage, foe_stored.stats.hp);
764
+ // Pokémon Showdown uses the less specific heal here instead of drain... because reasons?
765
+ if (foe_stored.hp > before) try options.log.heal(foe_ident, foe_stored, .Silent);
766
+ if (stored.hp == 0) return;
767
+ }
768
+
769
+ if (volatiles.Nightmare) {
770
+ const damage = @min(@max(stored.stats.hp / 4, 1), stored.hp);
771
+ if (!showdown or damage > 0) {
772
+ stored.hp -= damage;
773
+ // ibid
774
+ try options.log.damage(ident, stored, Damage.Nightmare);
775
+ if (stored.hp == 0) return;
776
+ }
777
+ }
778
+
779
+ if (volatiles.Curse) {
780
+ const damage = @min(@max(stored.stats.hp / 4, 1), stored.hp);
781
+ if (!showdown or damage > 0) {
782
+ stored.hp -= damage;
783
+ // ibid
784
+ try options.log.damage(ident, stored, Damage.Curse);
785
+ if (stored.hp == 0) return;
786
+ }
787
+ }
788
+ }
789
+
790
+ fn betweenTurn(battle: anytype, mslot: u4, options: anytype) !void {
791
+ const players = std.enums.values(Player);
792
+
793
+ // TODO checkFaintThen(p1, p2);
794
+
795
+ // Future Sight
796
+ inline for (players) |player| {
797
+ var side = battle.side(player);
798
+ if (side.conditions.future_sight.count > 0) {
799
+ side.conditions.future_sight.count -= 1;
800
+ assert(side.conditions.future_sight.count != 0);
801
+ if (side.conditions.future_sight.count == 1) {
802
+ try options.log.end(battle.active(player), .FutureSight);
803
+ // TODO doMove
804
+
805
+ }
806
+ }
807
+ }
808
+ // TODO checkFaintThen(p1, p2);
809
+
810
+ // Weather
811
+ inline for (players) |player| {
812
+ var side = battle.side(player);
813
+ _ = side; // TODO
814
+
815
+ }
816
+ // TODO checkFaintThen(p1, p2);
817
+
818
+ // Binding
819
+ inline for (players) |player| {
820
+ var side = battle.side(player);
821
+ _ = side; // TODO
822
+
823
+ }
824
+ // TODO checkFaintThen(p1, p2);
825
+
826
+ // Perish Song
827
+ inline for (players) |player| {
828
+ var side = battle.side(player);
829
+ _ = side; // TODO
830
+
831
+ }
832
+ // TODO checkFaintThen(p1, p2);
833
+
834
+ // Leftovers
835
+ inline for (players) |player| {
836
+ var stored = battle.side(player).stored();
837
+ if (stored.item == .Leftovers) {
838
+ const before = stored.hp;
839
+ const heal = @min(@max(stored.stats.hp / 16, 1), stored.hp);
840
+ stored.hp = @min(stored.hp + heal, stored.stats.hp);
841
+ if (stored.hp > before) try options.log.heal(battle.active(player), stored, .Leftovers);
842
+ }
843
+ }
844
+
845
+ // Mystery Berry
846
+ inline for (players) |player| {
847
+ var stored = battle.side(player).stored();
848
+ if (stored.item == .MysteryBerry) {
849
+ // TODO stored and active moves, transform, etc
850
+ }
851
+ }
852
+
853
+ // Defrost
854
+ inline for (players) |player| {
855
+ var stored = battle.side(player).stored();
856
+ const just_frozen = false; // FIXME TODO
857
+ if (Status.is(stored.status, .FRZ) and !just_frozen and
858
+ try Rolls.defrost(battle, player, options))
859
+ {
860
+ try options.log.curestatus(battle.active(player), stored.status, .Message);
861
+ stored.status = 0;
862
+ }
863
+ }
864
+
865
+ // Safeguard
866
+ inline for (players) |player| {
867
+ var side = battle.side(player);
868
+ if (side.conditions.Safeguard) {
869
+ assert(side.conditions.safeguard > 0);
870
+ side.conditions.safeguard -= 1;
871
+
872
+ if (side.conditions.safeguard == 0) {
873
+ side.conditions.Safeguard = false;
874
+ try options.log.sideend(battle.active(player), .Safeguard);
875
+ }
876
+ }
877
+ }
878
+
879
+ // Reflect / Light Screen
880
+ inline for (players) |player| {
881
+ var side = battle.side(player);
882
+ _ = side; // TODO
883
+ }
884
+
885
+ // Stat boosting items
886
+ inline for (players) |player| {
887
+ var side = battle.side(player);
888
+ _ = side; // TODO
889
+ }
890
+
891
+ // Healing items
892
+ inline for (players) |player| {
893
+ var side = battle.side(player);
894
+ _ = side; // TODO
895
+ }
896
+
897
+ // Encore
898
+ inline for (players) |player| {
899
+ var side = battle.side(player);
900
+ if (side.active.volatiles.Encore) {
901
+ assert(side.active.volatiles.encore > 0);
902
+ side.active.volatiles.encore -= 1;
903
+
904
+ if (side.active.volatiles.encore == 0 or side.active.moves(mslot).pp == 0) {
905
+ side.active.volatiles.Encore = false;
906
+ try options.log.end(battle.active(player), .Encore);
907
+ }
908
+ }
909
+ }
910
+ }
911
+
912
+ fn endTurn(battle: anytype, options: anytype) @TypeOf(options.log).Error!Result {
913
+ battle.turn += 1;
914
+
915
+ if (showdown and battle.turn >= 1000) {
916
+ try options.log.tie();
917
+ return Result.Tie;
918
+ } else if (battle.turn >= 65535) {
919
+ return Result.Error;
920
+ }
921
+
922
+ try options.log.turn(battle.turn);
923
+
924
+ return Result.Default;
925
+ }
926
+
927
+ pub const Effects = struct {
928
+ fn protect(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
929
+ var side = battle.side(player);
930
+ var volatiles = &side.active.volatiles;
931
+
932
+ // TODO or if we didn't go first
933
+ if (volatiles.Substitute) return try options.log.fail(battle.active(player), .None);
934
+
935
+ // TODO
936
+
937
+ if (move.effect == .Endure) {
938
+ volatiles.Endure = true;
939
+ } else {
940
+ volatiles.Protect = true;
941
+ }
942
+ try options.log.singleturn(battle.active(player), move.id);
943
+ }
944
+ };
945
+
946
+ fn statusModify(status: u8, stats: *Stats(u16)) void {
947
+ if (Status.is(status, .PAR)) {
948
+ stats.spe = @max(stats.spe / 4, 1);
949
+ } else if (Status.is(status, .BRN)) {
950
+ stats.atk = @max(stats.atk / 2, 1);
951
+ }
952
+ }
953
+
954
+ inline fn isForced(active: anytype) bool {
955
+ return active.volatiles.Recharging or active.volatiles.Rage or
956
+ active.volatiles.Thrashing or active.volatiles.Charging or
957
+ active.volatiles.Rollout;
958
+ }
959
+
960
+ pub const Rolls = struct {
961
+ inline fn speedTie(battle: anytype, options: anytype) !bool {
962
+ const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
963
+ player == .P1
964
+ else if (showdown)
965
+ battle.rng.range(u8, 0, 2) == 0
966
+ else
967
+ battle.rng.next() < Gen12.percent(50) + 1;
968
+
969
+ try options.chance.speedTie(p1);
970
+ return p1;
971
+ }
972
+
973
+ inline fn quickClaw(battle: anytype, player: Player, options: anytype) !bool {
974
+ const qkc = if (options.calc.overridden(player, .quick_claw)) |val|
975
+ val == .true
976
+ else if (showdown)
977
+ battle.rng.chance(u8, 60, 256)
978
+ else
979
+ battle.rng.next() < 60;
980
+
981
+ try options.chance.quickClaw(player, qkc);
982
+ return qkc;
983
+ }
984
+
985
+ inline fn criticalHit(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
986
+ const crit = if (options.calc.overridden(player, .critical_hit)) |val|
987
+ val == .true
988
+ else if (showdown)
989
+ battle.rng.chance(u8, rate, 256)
990
+ else
991
+ battle.rng.next() < rate;
992
+
993
+ try options.chance.criticalHit(player, crit, rate);
994
+ return crit;
995
+ }
996
+
997
+ inline fn damage(battle: anytype, player: anytype, options: anytype) !u8 {
998
+ const roll = if (options.calc.overridden(player, .damage)) |val|
999
+ val
1000
+ else roll: {
1001
+ if (showdown) break :roll battle.rng.range(u8, 217, 256);
1002
+ while (true) {
1003
+ const r = std.math.rotr(u8, battle.rng.next(), 1);
1004
+ if (r >= 217) break :roll r;
1005
+ }
1006
+ };
1007
+
1008
+ assert(roll >= 217 and roll <= 255);
1009
+ try options.chance.damage(player, roll);
1010
+ return roll;
1011
+ }
1012
+
1013
+ inline fn hit(battle: anytype, player: Player, accuracy: u8, options: anytype) !bool {
1014
+ const ok = if (options.calc.overridden(player, .hit)) |val|
1015
+ val == .true
1016
+ else if (showdown)
1017
+ battle.rng.chance(u8, accuracy, 256)
1018
+ else
1019
+ battle.rng.next() < accuracy;
1020
+
1021
+ try options.chance.hit(player, ok, accuracy);
1022
+ return ok;
1023
+ }
1024
+
1025
+ inline fn confused(battle: anytype, player: Player, options: anytype) !bool {
1026
+ const cfz = if (options.calc.overridden(player, .confused)) |val|
1027
+ val == .true
1028
+ else if (showdown)
1029
+ !battle.rng.chance(u8, 128, 256)
1030
+ else
1031
+ battle.rng.next() >= Gen12.percent(50) + 1;
1032
+
1033
+ try options.chance.confused(player, cfz);
1034
+ return cfz;
1035
+ }
1036
+
1037
+ inline fn attract(battle: anytype, player: Player, options: anytype) !bool {
1038
+ const cant = if (options.calc.overridden(player, .attract)) |val|
1039
+ val == .true
1040
+ else if (showdown)
1041
+ battle.rng.chance(u8, 1, 2)
1042
+ else
1043
+ battle.rng.next() > Gen12.percent(50) + 1;
1044
+
1045
+ try options.chance.attract(player, cant);
1046
+ return cant;
1047
+ }
1048
+
1049
+ inline fn defrost(battle: anytype, player: Player, options: anytype) !bool {
1050
+ const thaw = if (options.calc.overridden(player, .defrost)) |val|
1051
+ val == .true
1052
+ else if (showdown)
1053
+ battle.rng.chance(u8, 25, 256)
1054
+ else
1055
+ battle.rng.next() < Gen12.percent(10);
1056
+
1057
+ try options.chance.defrost(player, thaw);
1058
+ return thaw;
1059
+ }
1060
+
1061
+ const METRONOME = init: {
1062
+ var num = 0;
1063
+ var moves: [238]Move = undefined;
1064
+ for (1..Move.size) |i| {
1065
+ if (Move.get(@enumFromInt(i)).flags.metronome) {
1066
+ moves[num] = @enumFromInt(i);
1067
+ num += 1;
1068
+ }
1069
+ }
1070
+ assert(num == moves.len);
1071
+ break :init moves;
1072
+ };
1073
+ };
1074
+
1075
+ test "RNG agreement" {
1076
+ if (!showdown) return;
1077
+ var expected: [256]u32 = undefined;
1078
+ for (0..expected.len) |i| {
1079
+ expected[i] = @intCast(i * 0x1000000);
1080
+ }
1081
+
1082
+ var spe: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1083
+ var qkc: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1084
+ var cfz: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1085
+ var atr: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1086
+ var frz: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
1087
+
1088
+ for (0..expected.len) |i| {
1089
+ try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
1090
+ try expectEqual(qkc.chance(u8, 60, 256), i < 60);
1091
+ try expectEqual(!cfz.chance(u8, 128, 256), i >= Gen12.percent(50) + 1);
1092
+ try expectEqual(atr.chance(u8, 1, 2), i < Gen12.percent(50) + 1);
1093
+ try expectEqual(frz.chance(u8, 25, 256), i < Gen12.percent(10));
1094
+ }
1095
+ }
1096
+
1097
+ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
1098
+ var n: u8 = 0;
1099
+ switch (request) {
1100
+ .Pass => {
1101
+ out[n] = .{};
1102
+ n += 1;
1103
+ },
1104
+ .Switch => {
1105
+ const side = battle.side(player);
1106
+ var slot: u4 = 2;
1107
+ while (slot <= 6) : (slot += 1) {
1108
+ const id = side.order[slot - 1];
1109
+ if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
1110
+ out[n] = .{ .type = .Switch, .data = slot };
1111
+ n += 1;
1112
+ }
1113
+ if (n == 0) {
1114
+ out[n] = .{};
1115
+ n += 1;
1116
+ }
1117
+ },
1118
+ .Move => {
1119
+ const side = battle.side(player);
1120
+
1121
+ var active = &side.active;
1122
+ const stored = side.stored();
1123
+
1124
+ // While players are not given any input options on the cartridge in these cases,
1125
+ // Pokémon Showdown instead produces a list with a single move that must be chosen.
1126
+ //
1127
+ // Given that no input is allowed on the cartridge 'Pass' seems like it would be logical
1128
+ // here when not in compatibility mode, but the engine needs to be able to differentiate
1129
+ // between passing while waiting for an opponent's forced switch after fainting and
1130
+ // passing due to being forced into using a move. Instead of introducing another option
1131
+ // we simply repurpose Move with no move slot, even though pedantically this is not
1132
+ // strictly correct as the player would not have been presented the option to move or
1133
+ // switch at all.
1134
+ if (isForced(active)) {
1135
+ out[n] = .{ .type = .Move, .data = @intFromBool(showdown) };
1136
+ n += 1;
1137
+ return n;
1138
+ }
1139
+
1140
+ var slot: u4 = 2;
1141
+ while (!active.volatiles.trapped and slot <= 6) : (slot += 1) {
1142
+ const id = side.order[slot - 1];
1143
+ if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
1144
+ out[n] = .{ .type = .Switch, .data = slot };
1145
+ n += 1;
1146
+ }
1147
+
1148
+ const limited = active.volatiles.Bide or active.volatiles.bind > 0;
1149
+ // On the cartridge, all of these happen after "FIGHT" (indicating you are not
1150
+ // switching) but before you are allowed to select a move. Pokémon Showdown instead
1151
+ // either disables all other moves in the case of limited or requires you to select a
1152
+ // move normally if sleeping/frozen/bound.
1153
+ if (!showdown and (limited or
1154
+ Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP)))
1155
+ {
1156
+ out[n] = .{ .type = .Move, .data = 0 };
1157
+ n += 1;
1158
+ return n;
1159
+ }
1160
+
1161
+ slot = 1;
1162
+ // Pokémon Showdown handles Bide and Binding moves by checking if the move in question
1163
+ // is present in the Pokémon's moveset (which means moves called via Metronome / Mirror
1164
+ // Move will not result in forcing the subsequent use unless the user also had the
1165
+ // proc-ed move in their moveset) and disabling all other moves.
1166
+ if (limited) {
1167
+ assert(showdown);
1168
+ assert(side.last_selected_move != .None);
1169
+ while (slot <= 4) : (slot += 1) {
1170
+ const m = active.moves[slot - 1];
1171
+ if (m.id == .None) break;
1172
+ if (m.id == if (active.volatiles.Bide) .Bide else side.last_selected_move) {
1173
+ // Pokémon Showdown displays Struggle if limited to Bide but unable to pick
1174
+ const struggle =
1175
+ m.id == .Bide and (m.pp == 0 or active.volatiles.disabled_move == slot);
1176
+ const s = if (struggle) 0 else slot;
1177
+ out[n] = .{ .type = .Move, .data = s };
1178
+ n += 1;
1179
+ return n;
1180
+ }
1181
+ }
1182
+ }
1183
+
1184
+ const before = n;
1185
+ slot = 1;
1186
+ while (slot <= 4) : (slot += 1) {
1187
+ const m = active.moves[slot - 1];
1188
+ if (m.id == .None) break;
1189
+ if (m.pp == 0) continue;
1190
+ if (active.volatiles.disabled_move == slot) continue;
1191
+ out[n] = .{ .type = .Move, .data = slot };
1192
+ n += 1;
1193
+ }
1194
+ // Struggle (Pokémon Showdown would use 'move 1' here)
1195
+ if (n == before) {
1196
+ out[n] = .{ .type = .Move, .data = 0 };
1197
+ n += 1;
1198
+ }
1199
+ },
1200
+ }
1201
+ return n;
1202
+ }