@pkmn/engine 0.1.0-dev.d97c6e03 → 0.1.0-dev.dc33f678

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 (51) hide show
  1. package/README.md +67 -56
  2. package/build/pkg/addon.d.ts +1 -1
  3. package/build/pkg/addon.js +7 -6
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/ids.json +12 -12
  6. package/build/pkg/data/layout.json +27 -5
  7. package/build/pkg/data/protocol.json +69 -12
  8. package/build/pkg/data.d.ts +2 -2
  9. package/build/pkg/gen1.d.ts +5 -4
  10. package/build/pkg/gen1.js +48 -32
  11. package/build/pkg/gen1.js.map +1 -1
  12. package/build/pkg/index.d.ts +15 -12
  13. package/build/pkg/index.js.map +1 -1
  14. package/build/pkg/protocol.d.ts +4 -2
  15. package/build/pkg/protocol.js +60 -39
  16. package/build/pkg/protocol.js.map +1 -1
  17. package/build/test/display.js +29 -12
  18. package/build/test/display.js.map +1 -1
  19. package/build.zig +92 -68
  20. package/package.json +40 -21
  21. package/src/bin/install-pkmn-engine +11 -6
  22. package/src/lib/binding/node.zig +46 -141
  23. package/src/lib/binding/wasm.zig +5 -3
  24. package/src/lib/common/battle.zig +17 -0
  25. package/src/lib/common/data.zig +15 -27
  26. package/src/lib/common/debug.zig +21 -11
  27. package/src/lib/common/js.zig +118 -0
  28. package/src/lib/common/optional.zig +67 -0
  29. package/src/lib/common/options.zig +12 -3
  30. package/src/lib/common/protocol.zig +771 -245
  31. package/src/lib/common/rational.zig +353 -0
  32. package/src/lib/common/rng.zig +15 -15
  33. package/src/lib/common/util.zig +12 -0
  34. package/src/lib/gen1/README.md +285 -182
  35. package/src/lib/gen1/calc.zig +426 -0
  36. package/src/lib/gen1/chance.zig +1060 -0
  37. package/src/lib/gen1/data/moves.zig +192 -180
  38. package/src/lib/gen1/data/species.zig +2 -2
  39. package/src/lib/gen1/data/types.zig +5 -5
  40. package/src/lib/gen1/data.zig +79 -65
  41. package/src/lib/gen1/helpers.zig +577 -32
  42. package/src/lib/gen1/mechanics.zig +940 -614
  43. package/src/lib/gen2/README.md +106 -85
  44. package/src/lib/gen2/data/items.zig +12 -18
  45. package/src/lib/gen2/data/moves.zig +660 -566
  46. package/src/lib/gen2/data/species.zig +1 -1
  47. package/src/lib/gen2/data/types.zig +131 -28
  48. package/src/lib/gen2/data.zig +132 -84
  49. package/src/lib/gen2/helpers.zig +16 -22
  50. package/src/lib/gen2/mechanics.zig +2426 -12
  51. package/src/lib/pkmn.zig +65 -6
@@ -1,31 +1,42 @@
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");
9
+ const util = @import("../common/util.zig");
8
10
 
9
11
  const data = @import("data.zig");
10
12
 
11
13
  const assert = std.debug.assert;
12
14
 
13
15
  const expectEqual = std.testing.expectEqual;
16
+ const expectEqualSlices = std.testing.expectEqualSlices;
17
+ const expectEqualStrings = std.testing.expectEqualStrings;
18
+
19
+ const showdown = pkmn.options.showdown;
14
20
 
15
21
  const Choice = common.Choice;
16
22
  const ID = common.ID;
17
23
  const Player = common.Player;
18
24
  const Result = common.Result;
19
25
 
20
- const showdown = options.showdown;
21
-
26
+ const Activate = protocol.Activate;
27
+ const Cant = protocol.Cant;
22
28
  const Damage = protocol.Damage;
29
+ const End = protocol.End;
23
30
  const Heal = protocol.Heal;
31
+ const Start = protocol.Start;
24
32
 
25
33
  const Gen12 = rng.Gen12;
26
34
 
35
+ const PointerType = util.PointerType;
36
+
27
37
  const ActivePokemon = data.ActivePokemon;
28
38
  const Effectiveness = data.Effectiveness;
39
+ const Item = data.Item;
29
40
  const Move = data.Move;
30
41
  const MoveSlot = data.MoveSlot;
31
42
  const Pokemon = data.Pokemon;
@@ -33,21 +44,2424 @@ const Side = data.Side;
33
44
  const Species = data.Species;
34
45
  const Stats = data.Stats;
35
46
  const Status = data.Status;
47
+ const TriAttack = data.TriAttack;
48
+ const Type = data.Type;
49
+ const Types = data.Types;
50
+ const Volatiles = data.Volatiles;
51
+
52
+ // zig fmt: off
53
+ const STAT_BOOSTS = &[_][2]u8{
54
+ .{ 25, 100 }, // -6
55
+ .{ 28, 100 }, // -5
56
+ .{ 33, 100 }, // -4
57
+ .{ 40, 100 }, // -3
58
+ .{ 50, 100 }, // -2
59
+ .{ 66, 100 }, // -1
60
+ .{ 1, 1 }, // 0
61
+ .{ 15, 10 }, // +1
62
+ .{ 2, 1 }, // +2
63
+ .{ 25, 10 }, // +3
64
+ .{ 3, 1 }, // +4
65
+ .{ 35, 10 }, // +5
66
+ .{ 4, 1 }, // +6
67
+ };
68
+
69
+ const ACCURACY_BOOSTS = &[_][2]u8{
70
+ .{ 33, 100 }, // -6
71
+ .{ 36, 100 }, // -5
72
+ .{ 43, 100 }, // -4
73
+ .{ 50, 100 }, // -3
74
+ .{ 60, 100 }, // -2
75
+ .{ 75, 100 }, // -1
76
+ .{ 1, 1 }, // 0
77
+ .{ 133, 100 }, // +1
78
+ .{ 166, 100 }, // +2
79
+ .{ 2, 1 }, // +3
80
+ .{ 233, 100 }, // +4
81
+ .{ 133, 50 }, // +5
82
+ .{ 3, 1 }, // +6
83
+ };
84
+
85
+ const CRITICAL_HITS = &[_]u8{
86
+ 256 / 15, // 0
87
+ 256 / 8, // +1
88
+ 256 / 4, // +2
89
+ 256 / 3, // +3
90
+ 256 / 2, // +4
91
+ };
92
+ // zig fmt: on
93
+
94
+ const MAX_STAT_VALUE = 999;
95
+
96
+ const State = struct {
97
+ damage: u16 = 0,
98
+ effectiveness: u16 = Effectiveness.neutral,
99
+ bp: u8 = 0,
100
+ move: Move = .None,
101
+ mslot: u4 = 0,
102
+ first: bool = false,
103
+ miss: bool = false,
104
+ crit: bool = false,
105
+
106
+ comptime {
107
+ assert(@sizeOf(State) == 10);
108
+ }
109
+
110
+ inline fn immune(self: *State) bool {
111
+ return self.effectiveness == 0;
112
+ }
113
+ };
114
+
115
+ pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result {
116
+ assert(c1.type != .Pass or c2.type != .Pass or battle.turn == 0);
117
+ if (battle.turn == 0) return start(battle, options);
118
+
119
+ if (try turnOrder(battle, c1, c2, options) == .P1) {
120
+ if (try doTurn(battle, .P1, c1, .P2, c2, options)) |r| return r;
121
+ } else {
122
+ if (try doTurn(battle, .P2, c2, .P1, c1, options)) |r| return r;
123
+ }
124
+
125
+ return endTurn(battle, options);
126
+ }
127
+
128
+ fn start(battle: anytype, options: anytype) !Result {
129
+ const p1 = battle.side(.P1);
130
+ const p2 = battle.side(.P2);
131
+
132
+ var p1_slot = findFirstAlive(p1);
133
+ assert(!showdown or p1_slot == 1);
134
+ if (p1_slot == 0) return if (findFirstAlive(p2) == 0) Result.Tie else Result.Lose;
135
+
136
+ var p2_slot = findFirstAlive(p2);
137
+ assert(!showdown or p2_slot == 1);
138
+ if (p2_slot == 0) return Result.Win;
139
+
140
+ try switchIn(battle, .P1, p1_slot, true, options);
141
+ try switchIn(battle, .P2, p2_slot, true, options);
142
+
143
+ return endTurn(battle, options);
144
+ }
145
+
146
+ fn findFirstAlive(side: *const Side) u8 {
147
+ for (side.pokemon, 0..) |pokemon, i| if (pokemon.hp > 0) return side.pokemon[i].position;
148
+ return 0;
149
+ }
150
+
151
+ fn selectMove(battle: anytype, player: Player, choice: Choice) void {
152
+ if (choice.type == .Pass) return; // FIXME BATON PASS
153
+
154
+ var side = battle.side(player);
155
+ var volatiles = &side.active.volatiles;
156
+
157
+ // pre-battle menu
158
+ const forced = forced: {
159
+ if (volatiles.Recharging) break :forced true;
160
+ // Pokémon Showdown removes Flinch at the end-of-turn in its residual handler
161
+ if (!showdown) {
162
+ volatiles.Flinch = false;
163
+ battle.foe(player).active.volatiles.Flinch = false;
164
+ }
165
+ break :forced (volatiles.Thrashing or volatiles.Charging or volatiles.Rollout);
166
+ };
167
+
168
+ // battle menu
169
+ if (choice.type == .Switch) return;
170
+ assert(choice.type == .Move and (!forced or choice.data == @intFromBool(showdown)));
171
+
172
+ done: {
173
+ if (forced) break :done;
174
+ // pre-move select
175
+ if (volatiles.Encore) {
176
+ // TODO wCurPlayerMove = side.lastMove(true);
177
+ // assert(wCurPlayerMove != .None);
178
+ volatiles.Bide = false; // FIXME wPlayerCharging vs. SUBSTATUS_BIDE?
179
+ const effect = Move.get(.Pound).effect; // TODO
180
+ if (effect != .FuryCutter) volatiles.fury_cutter = false;
181
+ if (effect != .Rage) {
182
+ volatiles.Rage = false;
183
+ volatiles.rage = 0;
184
+ }
185
+ if (effect != .Protect and effect != .Endure) volatiles.protect = 0;
186
+ return;
187
+ }
188
+ if (volatiles.Bide) break :done;
189
+ // move select
190
+ if (choice.data == 0) {
191
+ const struggle = ok: {
192
+ for (side.active.moves, 0..) |move, i| {
193
+ if (move.pp > 0 and volatiles.disable.move != i + 1) break :ok false;
194
+ }
195
+ break :ok true;
196
+ };
197
+ assert(struggle);
198
+ // TODO wCurPlayerMove = .Struggle;
199
+ } else {
200
+ assert(showdown or side.active.volatiles.disable.move != choice.data);
201
+ const move = side.active.move(choice.data);
202
+ // You cannot *select* a move with 0 PP
203
+ assert(move.pp != 0);
204
+ // TODO wCurPlayerMove = move.id;
205
+ // battle.lastMove(player).index = @intCast(c.data);
206
+ }
207
+ }
208
+
209
+ volatiles.Rage = false;
210
+ volatiles.rage = 0;
211
+ volatiles.fury_cutter = 0;
212
+ volatiles.protect = 0;
213
+ }
214
+
215
+ fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, options: anytype) !void {
216
+ var side = battle.side(player);
217
+ var foe = battle.foe(player);
218
+
219
+ var active = &side.active;
220
+ const incoming = side.get(slot);
221
+
222
+ assert(incoming.hp != 0);
223
+ assert(slot != 1 or initial);
224
+
225
+ const out = side.pokemon[0].position;
226
+ side.pokemon[0].position = side.pokemon[slot - 1].position;
227
+ side.pokemon[slot - 1].position = out;
228
+
229
+ side.last_used_move = .None;
230
+ foe.active.volatiles.dirty = true;
231
+
232
+ active.volatiles = .{};
233
+ active.stats = incoming.stats;
234
+ active.moves = incoming.moves;
235
+ active.boosts = .{};
236
+ active.types = incoming.types;
237
+ active.species = incoming.species;
238
+
239
+ statusModify(incoming.status, &active.stats);
240
+
241
+ try options.log.switched(battle.active(player), incoming);
242
+ options.chance.switched(player, side.pokemon[0].position, out);
243
+
244
+ if (side.conditions.Spikes and !active.types.includes(.Flying)) {
245
+ incoming.hp -|= @max(incoming.stats.hp / 8, 1);
246
+ try options.log.damage(battle.active(player), incoming, .Spikes);
247
+ }
248
+ }
249
+
250
+ fn turnOrder(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Player {
251
+ assert(c1.type != .Pass or c2.type != .Pass);
252
+
253
+ if (c1.type == .Pass) return .P2;
254
+ if (c2.type == .Pass) return .P1;
255
+
256
+ // Pokémon Showdown always rolls for Quick Claw every turn, even if no Pokémon hold Quick Claw
257
+ // TODO: add to pending and only commit roll on Pokémon Showdown if roll is used
258
+ const qkc = showdown and try Rolls.quickClaw(battle, .P1, options);
259
+
260
+ if ((c1.type == .Switch) != (c2.type == .Switch)) return if (c1.type == .Switch) .P1 else .P2;
261
+
262
+ // https://www.smogon.com/forums/threads/adv-switch-priority.3622189/
263
+ // > In Gen 1 it's irrelevant [which player switches first] because switches happen instantly on
264
+ // > your own screen without waiting for the other player's choice (and their choice will appear
265
+ // > to happen first for them too, unless they attacked in which case your switch happens first)
266
+ // A cartridge-compatible implemention must not advance the RNG so we simply default to P1
267
+ const double_switch = c1.type == .Switch and c2.type == .Switch;
268
+ if (!showdown and double_switch) return .P1;
269
+
270
+ const m1 = .Pound; // TODO battle.side(.P1).last_selected_move;
271
+ const m2 = .Pound; // TODO battle.side(.P2).last_selected_move;
272
+ if (!showdown or !double_switch) {
273
+ const pri1 = Move.get(m1).priority;
274
+ const pri2 = Move.get(m2).priority;
275
+ if (pri1 != pri2) return if (pri1 > pri2) .P1 else .P2;
276
+
277
+ const qkc1 = battle.side(.P1).stored().item == .QuickClaw;
278
+ const qkc2 = battle.side(.P2).stored().item == .QuickClaw;
279
+ if (!showdown and qkc1 and qkc2) {
280
+ // If both Pokémon have Quick Claw the cartridge checks them in order and exits early if
281
+ // the first one procs, meaning the host has a slightly higher chance of going first
282
+ if (try Rolls.quickClaw(battle, .P1, options)) return .P1;
283
+ if (try Rolls.quickClaw(battle, .P2, options)) return .P2;
284
+ } else if (qkc1) {
285
+ const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P1, options);
286
+ if (proc) return .P1;
287
+ } else if (qkc2) {
288
+ const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P2, options);
289
+ if (proc) return .P2;
290
+ }
291
+ }
292
+
293
+ const spe1 = battle.side(.P1).active.stats.spe;
294
+ const spe2 = battle.side(.P2).active.stats.spe;
295
+ if (spe1 == spe2) {
296
+ // Pokémon Showdown's beforeTurnCallback shenanigans
297
+ if (showdown and !double_switch and hasCallback(m1) and hasCallback(m2)) {
298
+ battle.rng.advance(1);
299
+ }
300
+
301
+ const p1 = try Rolls.speedTie(battle, options);
302
+ if (!showdown) return if (p1) .P1 else .P2;
303
+
304
+ // Pokémon Showdown's "lockedmove" volatile's onBeforeTurn uses BattleQueue#changeAction,
305
+ // meaning that if a side is locked into a thrashing move and wins the speed tie, it
306
+ // actually uses its priority to simply insert its actual changed action into the queue,
307
+ // causing it to then execute *after* the side which should go second...
308
+ const t1 = battle.side(.P1).active.volatiles.Thrashing;
309
+ const t2 = battle.side(.P2).active.volatiles.Thrashing;
310
+ // If *both* sides are thrashing it really should be another speed tie, but we've patched
311
+ // that out and enforce host ordering of events, so P1 just goes first regardless of who
312
+ // won the original coin flip
313
+ if (t1 and t2) return .P1;
314
+ return if (p1) if (t1 and !t2) .P2 else .P1 else if (t2 and !t1) .P1 else .P2;
315
+ }
316
+
317
+ return if (spe1 > spe2) .P1 else .P2;
318
+ }
319
+
320
+ fn hasCallback(m: Move) bool {
321
+ return switch (m) {
322
+ .Counter, .MirrorCoat, .Pursuit => true,
323
+ else => false,
324
+ };
325
+ }
326
+
327
+ fn doTurn(
328
+ battle: anytype,
329
+ player: Player,
330
+ player_choice: Choice,
331
+ foe_player: Player,
332
+ foe_choice: Choice,
333
+ options: anytype,
334
+ ) !?Result {
335
+ assert(player_choice.type != .Pass);
336
+
337
+ const volatiles = &battle.side(player).active.volatiles;
338
+ const foe_volatiles = &battle.foe(player).active.volatiles;
339
+
340
+ volatiles.DestinyBond = false;
341
+ if (try executeMove(battle, player, player_choice, true, options)) |r| return r;
342
+ foe_volatiles.Protect = false;
343
+ foe_volatiles.Endure = false;
344
+ foe_volatiles.DestinyBond = false;
345
+
346
+ // foe_volatiles.DestinyBond = false;
347
+ if (try executeMove(battle, foe_player, foe_choice, false, options)) |r| return r;
348
+ volatiles.Protect = false;
349
+ volatiles.Endure = false;
350
+ volatiles.DestinyBond = false;
351
+
352
+ // FIXME
353
+ try handleResidual(battle, player, options);
354
+
355
+ // FIXME
356
+ try betweenTurns(battle, 0, options);
357
+
358
+ return null;
359
+ }
360
+
361
+ fn executeMove(
362
+ battle: anytype,
363
+ player: Player,
364
+ choice: Choice,
365
+ first: bool,
366
+ options: anytype,
367
+ ) !?Result {
368
+ if (choice.type == .Switch) {
369
+ try switchIn(battle, player, choice.data, false, options);
370
+ return null;
371
+ }
372
+
373
+ assert(choice.type == .Move);
374
+ var mslot: u4 = @intCast(choice.data);
375
+ var move = Move.Pound; // TODO
376
+
377
+ if (!try beforeMove(battle, player, move, options)) return null;
378
+
379
+ var state: State = .{ .move = move, .mslot = mslot, .first = first };
380
+ return doMove(battle, player, &state, options);
381
+ }
382
+
383
+ fn beforeMove(battle: anytype, player: Player, move: Move, options: anytype) !bool {
384
+ var log = options.log;
385
+ var side = battle.side(player);
386
+ // const foe = battle.foe(player);
387
+ var active = &side.active;
388
+ var stored = side.stored();
389
+ const ident = battle.active(player);
390
+ var volatiles = &active.volatiles;
391
+
392
+ if (volatiles.Recharging) {
393
+ volatiles.Recharging = false;
394
+ cantMove(volatiles);
395
+ try log.cant(ident, .Recharge);
396
+ return false;
397
+ }
398
+
399
+ if (Status.is(stored.status, .SLP)) slp: {
400
+ const before = stored.status;
401
+ // Even if the EXT bit is set this will still correctly modify the sleep duration
402
+ // if (options.calc.modify(player, .sleep)) |extend| {
403
+ // if (!extend) stored.status = 0;
404
+ // } else {
405
+ stored.status -= 1;
406
+ // }
407
+
408
+ const duration = Status.duration(stored.status);
409
+ if (!Status.is(stored.status, .EXT)) {
410
+ try options.chance.sleep(player, duration);
411
+ }
412
+
413
+ if (duration == 0) {
414
+ try log.curestatus(ident, before, .Message);
415
+ stored.status = 0; // clears EXT if present
416
+ cantMove(volatiles);
417
+ volatiles.Nightmare = false;
418
+ } else {
419
+ try log.cant(ident, .Sleep);
420
+ if (move == .Snore or move == .SleepTalk) break :slp;
421
+ cantMove(volatiles);
422
+ return false;
423
+ }
424
+ }
425
+
426
+ if (Status.is(stored.status, .FRZ) and !(move == .FlameWheel or move == .SacredFire)) {
427
+ cantMove(volatiles);
428
+ try log.cant(ident, .Freeze);
429
+ return false;
430
+ }
431
+
432
+ if (volatiles.Flinch) {
433
+ // Pokémon Showdown doesn't clear Flinch until its imaginary "residual" phase, meaning
434
+ // Pokémon can sometimes flinch multiple times from the same original hit
435
+ // if (!showdown) volatiles.Flinch = false;
436
+ volatiles.Flinch = false;
437
+ cantMove(volatiles);
438
+ try log.cant(ident, .Flinch);
439
+ return false;
440
+ }
441
+
442
+ if (volatiles.disable.duration > 0) {
443
+ // if (options.calc.modify(player, .disable)) |extend| {
444
+ // if (!extend) volatiles.disable.duration = 0;
445
+ // } else {
446
+ volatiles.disable.duration -= 1;
447
+ // }
448
+ try options.chance.disable(player, volatiles.disable.duration);
449
+
450
+ if (volatiles.disable.duration == 0) {
451
+ volatiles.disable.move = 0;
452
+ try log.end(ident, .Disable);
453
+ }
454
+ }
455
+
456
+ if (volatiles.Confusion) {
457
+ assert(volatiles.confusion > 0);
458
+
459
+ // if (options.calc.modify(player, .confusion)) |extend| {
460
+ // if (!extend) volatiles.confusion = 0;
461
+ // } else {
462
+ volatiles.confusion -= 1;
463
+ // }
464
+ try options.chance.confusion(player, volatiles.confusion);
465
+
466
+ if (volatiles.confusion == 0) {
467
+ volatiles.Confusion = false;
468
+ try log.end(ident, .Confusion);
469
+ } else {
470
+ try log.activate(ident, .Confusion);
471
+
472
+ if (try Rolls.confused(battle, player, options)) {
473
+ volatiles.BeatUp = false;
474
+ volatiles.Flinch = false;
475
+ cantMove(volatiles);
36
476
 
37
- pub fn update(battle: anytype, c1: Choice, c2: Choice, log: anytype) !Result {
477
+ // TODO HitConfusion
478
+
479
+ return false;
480
+ }
481
+ }
482
+ }
483
+
484
+ if (volatiles.Attract) {
485
+ try log.activate(ident, .Attract);
486
+
487
+ if (try Rolls.attract(battle, player, options)) {
488
+ cantMove(volatiles);
489
+ try log.cant(ident, .Attract);
490
+ return false;
491
+ }
492
+ }
493
+
494
+ if (side.active.volatiles.disable.move != 0) {
495
+ // A Pokémon that transforms after being disable may end up with less move slots
496
+ const m = side.active.moves[side.active.volatiles.disable.move - 1].id;
497
+ if (m != .None and m == move) {
498
+ side.active.volatiles.Charging = false;
499
+ cantMove(volatiles);
500
+ try options.log.disabled(ident, move);
501
+ return false;
502
+ }
503
+ }
504
+
505
+ if (Status.is(stored.status, .PAR) and try Rolls.paralyzed(battle, player, options)) {
506
+ cantMove(volatiles);
507
+ try log.cant(ident, .Paralysis);
508
+ return false;
509
+ }
510
+
511
+ return true;
512
+ }
513
+
514
+ fn cantMove(volatiles: *align(8) Volatiles) void {
515
+ volatiles.Bide = false;
516
+ volatiles.Thrashing = false;
517
+ volatiles.Charging = false;
518
+ volatiles.Underground = false;
519
+ volatiles.Flying = false;
520
+ volatiles.Rollout = false;
521
+
522
+ volatiles.fury_cutter = 0;
523
+ }
524
+
525
+ fn canMove(battle: anytype, player: Player, options: anytype) !void {
38
526
  _ = battle;
39
- _ = c1;
40
- _ = c2;
41
- _ = log;
527
+ _ = player;
528
+ _ = options;
529
+ return false;
530
+ }
42
531
 
43
- return Result.Default;
532
+ fn decrementPP(side: *Side, move: Move, mslot: u4) bool {
533
+ var active = &side.active;
534
+ const volatiles = &active.volatiles;
535
+
536
+ assert(move != .Struggle);
537
+ assert(!volatiles.Charging and !volatiles.BeatUp and !volatiles.Thrashing and !volatiles.Bide);
538
+
539
+ var move_slot = active.move(mslot);
540
+ if (move_slot.pp == 0) return false;
541
+
542
+ move_slot.pp = @as(u6, @intCast(move_slot.pp)) -% 1;
543
+ if (volatiles.Transform) return true;
544
+
545
+ move_slot = side.stored().move(mslot);
546
+ assert(move_slot.pp > 0);
547
+
548
+ if (move_slot.id == .Mimic and move != .Mimic) return true;
549
+
550
+ move_slot.pp = @as(u6, @intCast(move_slot.pp)) -% 1;
551
+ assert(active.move(mslot).pp == side.stored().move(mslot).pp);
552
+
553
+ return true;
44
554
  }
45
555
 
46
- pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
556
+ fn doMove(battle: anytype, player: Player, state: *State, options: anytype) !?Result {
557
+ try checkCriticalHit(battle, player, state, options);
558
+ try calcDamage(battle, player, state, options);
559
+ try adjustDamage(battle, player, state, options);
560
+ try randomizeDamage(battle, player, state, options);
561
+ try applyDamage(battle, player, state, options);
562
+ try buildRage(battle, player, state, options);
563
+ try kingsRock(battle, player, state, options);
564
+
565
+ try Effects.attract(battle, player, state, options);
566
+ try Effects.conversion(battle, player, state, options);
567
+ try Effects.conversion2(battle, player, state, options);
568
+ try Effects.destinyBond(battle, player, state, options);
569
+ try Effects.disable(battle, player, state, options);
570
+ try Effects.encore(battle, player, state, options);
571
+ try Effects.falseSwipe(battle, player, state, options);
572
+ try Effects.focusEnergy(battle, player, state, options);
573
+ try Effects.forceSwitch(battle, player, state, options);
574
+ try Effects.foresight(battle, player, state, options);
575
+ try Effects.happiness(battle, player, state, options);
576
+ try Effects.protect(battle, player, state, options);
577
+
578
+ return null;
579
+ }
580
+
581
+ fn checkCriticalHit(battle: anytype, player: Player, state: *State, options: anytype) !void {
582
+ const move = Move.get(state.move);
583
+ if (move.bp == 0) return;
584
+
585
+ const side = battle.side(player);
586
+ const pokemon = side.stored();
587
+
588
+ var stage: u8 =
589
+ if ((pokemon.species == .Chansey and pokemon.item == .LuckyPunch) or
590
+ (pokemon.species == .Farfetchd and pokemon.item == .Stick)) 2 else 0;
591
+ if (stage == 0) {
592
+ if (side.active.volatiles.FocusEnergy) stage += 1;
593
+ if (move.effect.isHighCritical()) stage += 2;
594
+ if (pokemon.item == .ScopeLens) stage += 1;
595
+ }
596
+ assert(stage <= 4);
597
+
598
+ state.crit = try Rolls.criticalHit(battle, player, CRITICAL_HITS[stage], options);
599
+ }
600
+
601
+ fn calcDamage(
602
+ battle: anytype,
603
+ player: Player,
604
+ state: *State,
605
+ options: anytype,
606
+ ) !void {
47
607
  _ = battle;
48
608
  _ = player;
49
- _ = request;
50
- _ = out;
609
+ _ = state;
610
+ _ = options;
611
+ }
51
612
 
52
- return 0;
613
+ const FORESIGHT: Types = .{ .type1 = .Normal, .type2 = .Fighting };
614
+
615
+ fn adjustDamage(battle: anytype, player: Player, state: *State, _: anytype) !void {
616
+ if (state.move == .Struggle) return;
617
+
618
+ const side = battle.side(player);
619
+ const foe = battle.foe(player);
620
+ const types = foe.active.types;
621
+ const move = Move.get(state.move);
622
+
623
+ var d = state.damage;
624
+ if ((move.type == .Water and battle.field.weather == .Rain) or
625
+ (move.type == .Fire and battle.field.weather == .Sun))
626
+ {
627
+ d = d *% @intFromEnum(Effectiveness.Super) / 10;
628
+ } else if ((move.type == .Water and battle.field.weather == .Sun) or
629
+ ((move.type == .Fire or state.move == .SolarBeam) and battle.field.weather == .Rain))
630
+ {
631
+ d = d *% @intFromEnum(Effectiveness.Resisted) / 10;
632
+ }
633
+
634
+ if (side.active.types.includes(move.type)) d +%= d / 2;
635
+
636
+ const neutral = @intFromEnum(Effectiveness.Neutral);
637
+ const foresight = foe.active.volatiles.Foresight and FORESIGHT.includes(move.type);
638
+ const eff1: u16 = if (foresight and types.type1 == .Ghost)
639
+ neutral
640
+ else
641
+ @intFromEnum(move.type.effectiveness(types.type1));
642
+ const eff2: u16 = if (foresight and types.type2 == .Ghost)
643
+ neutral
644
+ else
645
+ @intFromEnum(move.type.effectiveness(types.type2));
646
+
647
+ // Type effectiveness matchup precedence only matters with (NVE, SE)
648
+ if (!showdown and (eff1 + eff2) == Effectiveness.mismatch and
649
+ types.type1.precedence() > types.type2.precedence())
650
+ {
651
+ assert(eff2 != neutral);
652
+ d = d *% eff2 / 10;
653
+ assert(types.type1 != types.type2);
654
+ assert(eff1 != neutral);
655
+ d = d *% eff1 / 10;
656
+ } else {
657
+ if (eff1 != neutral) d = d *% eff1 / 10;
658
+ if (types.type1 != types.type2 and eff2 != neutral) d = d *% eff2 / 10;
659
+ }
660
+
661
+ state.effectiveness = if (types.type1 == types.type2) eff1 * neutral else eff1 * eff2;
662
+ state.damage = d;
663
+ }
664
+
665
+ fn randomizeDamage(battle: anytype, player: Player, state: *State, options: anytype) !void {
666
+ if (state.damage <= 1) return;
667
+ const random = try Rolls.damage(battle, player, options);
668
+ options.calc.base(player.foe(), state.damage);
669
+ state.damage = @intCast(@as(u32, state.damage) *% random / 255);
670
+ options.calc.final(player.foe(), state.damage);
671
+ }
672
+
673
+ fn applyDamage(
674
+ battle: anytype,
675
+ player: Player,
676
+ state: *State,
677
+ options: anytype,
678
+ ) !void {
679
+ _ = battle;
680
+ _ = player;
681
+ _ = state;
682
+ _ = options;
683
+ }
684
+
685
+ // TODO: set damage = 0 if missed and not (High) Jump Kick
686
+ fn checkHit(
687
+ battle: anytype,
688
+ player: Player,
689
+ state: *State,
690
+ options: anytype,
691
+ ) !void {
692
+ const side = battle.side(player);
693
+ const foe = battle.foe(player);
694
+
695
+ const move = Move.get(state.move);
696
+
697
+ if (move.effect == .DreamEater and !Status.is(foe.stored().status, .SLP)) {
698
+ state.miss = true;
699
+ return;
700
+ }
701
+ if (foe.active.volatiles.Protect) {
702
+ state.miss = true;
703
+ return;
704
+ }
705
+ const drain = move.effect == .DrainHP or move.effect == .DreamEater;
706
+ if (drain and foe.active.volatiles.Substitute) {
707
+ state.miss = true;
708
+ return;
709
+ }
710
+ if (side.active.volatiles.LockOn) {
711
+ state.miss = !(foe.active.volatiles.Flying and move.extra.undeground);
712
+ return;
713
+ }
714
+ assert(!(foe.active.volatiles.Flying and foe.active.volatiles.Underground));
715
+ if (foe.active.volatiles.Flying) {
716
+ if (!move.extra.flying) {
717
+ state.miss = true;
718
+ return;
719
+ }
720
+ } else if (foe.active.volatiles.Underground) {
721
+ if (!move.extra.underground) {
722
+ state.miss = true;
723
+ return;
724
+ }
725
+ }
726
+ if (move.effect == .Thunder and battle.field.weather == .Rain) {
727
+ state.miss = false;
728
+ return;
729
+ }
730
+ if (move.effect == .AlwaysHit) {
731
+ state.miss = false;
732
+ return;
733
+ }
734
+
735
+ var accuracy: u16 = move.accuracy;
736
+ if (foe.active.boosts.evasion <= side.active.boosts.accuracy or
737
+ !foe.active.volatiles.Foresight)
738
+ {
739
+ var boost = ACCURACY_BOOSTS[@as(u4, @intCast(@as(i8, side.active.boosts.accuracy) + 6))];
740
+ accuracy = accuracy * boost[0] / boost[1];
741
+ boost = ACCURACY_BOOSTS[@as(u4, @intCast(@as(i8, -foe.active.boosts.evasion) + 6))];
742
+ accuracy = accuracy * boost[0] / boost[1];
743
+ accuracy = @min(255, @max(1, accuracy));
744
+ }
745
+
746
+ if (foe.stored().item == .BrightPowder) {
747
+ accuracy -|= 20;
748
+ }
749
+
750
+ // The accuracy roll is skipped entirely if maxed
751
+ state.miss = if (accuracy == 255)
752
+ false
753
+ else
754
+ !try Rolls.hit(battle, player, @intCast(accuracy), options);
755
+ }
756
+
757
+ fn checkFaint(
758
+ battle: anytype,
759
+ player: Player,
760
+ state: *State,
761
+ options: anytype,
762
+ ) @TypeOf(options.log).Error!?Result {
763
+ _ = battle;
764
+ _ = player;
765
+ _ = state;
766
+ return null;
767
+ }
768
+
769
+ fn faint(battle: anytype, player: Player, done: bool, options: anytype) !?Result {
770
+ _ = battle;
771
+ _ = player;
772
+ _ = done;
773
+ _ = options;
774
+ return null;
775
+ }
776
+
777
+ fn handleResidual(battle: anytype, player: Player, options: anytype) !void {
778
+ var side = battle.side(player);
779
+ var stored = side.stored();
780
+ const ident = battle.active(player);
781
+ var volatiles = &side.active.volatiles;
782
+
783
+ // TODO if (stored.hp == 0) return;
784
+ assert(stored.hp > 0);
785
+
786
+ const brn = Status.is(stored.status, .BRN);
787
+ if (brn or Status.is(stored.status, .PSN)) {
788
+ var damage: u16 = undefined;
789
+ if (volatiles.Toxic) {
790
+ volatiles.toxic += 1;
791
+ damage = @max(stored.stats.hp / 16, 1) * volatiles.toxic;
792
+ } else {
793
+ damage = @max(stored.stats.hp / 8, 1);
794
+ }
795
+
796
+ stored.hp -|= damage;
797
+ // Pokémon Showdown uses damageOf here but its not relevant in Generation II
798
+ try options.log.damage(ident, stored, if (brn) Damage.Burn else Damage.Poison);
799
+ if (stored.hp == 0) return;
800
+ }
801
+
802
+ if (volatiles.LeechSeed) {
803
+ var foe = battle.foe(player);
804
+ var foe_stored = foe.stored();
805
+ const foe_ident = battle.active(player.foe());
806
+
807
+ // if (foe_stored.hp == 0) {
808
+ // assert(showdown);
809
+ // return;
810
+ // }
811
+
812
+ const damage = @max(stored.stats.hp / 8, 1);
813
+ stored.hp -|= damage;
814
+ // As above, Pokémon Showdown uses damageOf but its not relevant
815
+ try options.log.damage(ident, stored, .LeechSeed);
816
+
817
+ const before = foe_stored.hp;
818
+ foe_stored.hp = @min(foe_stored.hp + damage, foe_stored.stats.hp);
819
+ // Pokémon Showdown uses the less specific heal here instead of drain... because reasons?
820
+ if (foe_stored.hp > before) try options.log.heal(foe_ident, foe_stored, .Silent);
821
+ if (stored.hp == 0) return;
822
+ }
823
+
824
+ if (volatiles.Nightmare) {
825
+ stored.hp -= @max(stored.stats.hp / 4, 1);
826
+ // ibid
827
+ try options.log.damage(ident, stored, .Nightmare);
828
+ if (stored.hp == 0) return;
829
+ }
830
+
831
+ if (volatiles.Curse) {
832
+ stored.hp -= @max(stored.stats.hp / 4, 1);
833
+ // ibid
834
+ try options.log.damage(ident, stored, .Curse);
835
+ if (stored.hp == 0) return;
836
+ }
837
+ }
838
+
839
+ fn betweenTurns(battle: anytype, mslot: u4, options: anytype) !void {
840
+ const players = comptime std.enums.values(Player);
841
+
842
+ // TODO checkFaintThen(p1, p2);
843
+
844
+ // Future Sight
845
+ inline for (players) |player| {
846
+ var side = battle.side(player);
847
+ var future_sight = &side.conditions.future_sight;
848
+
849
+ if (future_sight.count > 0) {
850
+ future_sight.count -= 1;
851
+ assert(future_sight.count != 0);
852
+ if (future_sight.count == 1) {
853
+ try options.log.end(battle.active(player), .FutureSight);
854
+ // TODO doMove
855
+ }
856
+ }
857
+ }
858
+ // TODO checkFaintThen(p1, p2);
859
+
860
+ // Weather
861
+ if (battle.field.weather != .None) {
862
+ assert(battle.field.weather_duration > 0);
863
+ battle.field.weather_duration -= 1;
864
+
865
+ if (battle.field.weather_duration == 0) {
866
+ battle.field.weather = .None;
867
+ try options.log.weather(battle.field.weather, .None);
868
+ } else {
869
+ try options.log.weather(battle.field.weather, .Upkeep);
870
+ if (battle.field.weather == .Sandstorm) {
871
+ inline for (players) |player| {
872
+ var side = battle.side(player);
873
+ const active = side.active;
874
+ if (!(active.volatiles.Underground or active.types.sandstormImmune())) {
875
+ side.stored().hp -|= @max(side.stored().stats.hp / 8, 1);
876
+ try options.log.damage(battle.active(player), side.stored(), .Sandstorm);
877
+ }
878
+ }
879
+ // TODO checkFaintThen(p1, p2);
880
+ }
881
+ }
882
+ }
883
+
884
+ // Binding
885
+ inline for (players) |player| {
886
+ var side = battle.side(player);
887
+ var volatiles = &side.active.volatiles;
888
+
889
+ if (volatiles.bind.duration > 0 and !volatiles.Substitute) {
890
+ volatiles.bind.duration -= 1;
891
+ if (volatiles.bind.duration == 0) {
892
+ const reason = @intFromEnum(End.Bind) + volatiles.bind.reason - 1;
893
+ try options.log.end(battle.active(player), @enumFromInt(reason));
894
+ } else {
895
+ var reason = @intFromEnum(Activate.Bind) + volatiles.bind.reason - 1;
896
+ try options.log.activate(battle.active(player), @enumFromInt(reason));
897
+
898
+ reason = @intFromEnum(Damage.Bind) + volatiles.bind.reason - 1;
899
+ side.stored().hp -|= @max(side.stored().stats.hp / 16, 1);
900
+ try options.log.damage(battle.active(player), side.stored(), @enumFromInt(reason));
901
+ }
902
+ }
903
+ }
904
+ // TODO checkFaintThen(p1, p2);
905
+
906
+ // Perish Song
907
+ inline for (players) |player| {
908
+ var side = battle.side(player);
909
+ var volatiles = &side.active.volatiles;
910
+
911
+ if (volatiles.PerishSong) {
912
+ assert(volatiles.perish_song > 0);
913
+ volatiles.perish_song -= 1;
914
+
915
+ assert(volatiles.perish_song < 3);
916
+ const reason = @intFromEnum(Start.PerishSong0) + volatiles.perish_song;
917
+ try options.log.start(battle.active(player), @enumFromInt(reason));
918
+
919
+ if (volatiles.perish_song == 0) {
920
+ volatiles.PerishSong = false;
921
+ side.stored().hp = 0;
922
+ }
923
+ }
924
+ }
925
+ // TODO checkFaintThen(p1, p2);
926
+
927
+ // Leftovers
928
+ inline for (players) |player| {
929
+ var stored = battle.side(player).stored();
930
+
931
+ if (stored.item == .Leftovers) {
932
+ const before = stored.hp;
933
+ stored.hp = @min(stored.hp + @max(stored.stats.hp / 16, 1), stored.stats.hp);
934
+ if (stored.hp > before) try options.log.heal(battle.active(player), stored, .Leftovers);
935
+ }
936
+ }
937
+
938
+ // Mystery Berry
939
+ inline for (players) |player| {
940
+ var stored = battle.side(player).stored();
941
+
942
+ if (stored.item == .MysteryBerry) {
943
+ // TODO stored and active moves, transform, etc
944
+ }
945
+ }
946
+
947
+ // Defrost
948
+ inline for (players) |player| {
949
+ var side = battle.side(player);
950
+
951
+ if (Status.is(side.stored().status, .FRZ) and
952
+ !side.active.volatiles.frozen and
953
+ try Rolls.defrost(battle, player, options))
954
+ {
955
+ try options.log.curestatus(battle.active(player), side.stored().status, .Message);
956
+ side.stored().status = 0;
957
+ }
958
+ }
959
+
960
+ // Side Conditions
961
+ if (showdown) {
962
+ inline for (players) |player| {
963
+ try updateSideCondition(battle, player, "Reflect", "reflect", options);
964
+ }
965
+ inline for (players) |player| {
966
+ try updateSideCondition(battle, player, "LightScreen", "light_screen", options);
967
+ }
968
+ inline for (players) |player| {
969
+ try updateSideCondition(battle, player, "Safeguard", "safeguard", options);
970
+ }
971
+ } else {
972
+ inline for (players) |player| {
973
+ try updateSideCondition(battle, player, "Safeguard", "safeguard", options);
974
+ }
975
+ inline for (players) |player| {
976
+ try updateSideCondition(battle, player, "LightScreen", "light_screen", options);
977
+ try updateSideCondition(battle, player, "Reflect", "reflect", options);
978
+ }
979
+ }
980
+
981
+ // Healing items
982
+ inline for (players) |player| {
983
+ var side = battle.side(player);
984
+ var stored = side.stored();
985
+ const ident = battle.active(player);
986
+
987
+ const num = @intFromEnum(stored.item);
988
+ if (num > @intFromEnum(Item.Berry)) {
989
+ if (num <= @intFromEnum(Item.GoldBerry)) {
990
+ const proc = if (showdown)
991
+ stored.hp <= stored.stats.hp
992
+ else
993
+ stored.hp < stored.stats.hp;
994
+ if (proc) {
995
+ assert(stored.hp != 0);
996
+ try options.log.enditem(ident, stored.item, .Eat);
997
+ stored.item = .None;
998
+
999
+ const offset = num - @intFromEnum(Item.Berry);
1000
+ assert(offset < 3);
1001
+ stored.hp = @min(stored.hp + ((offset + 1) * 10), stored.stats.hp);
1002
+ const reason: Heal = @enumFromInt(@intFromEnum(Heal.Berry) + offset);
1003
+ try options.log.heal(ident, stored, reason);
1004
+ }
1005
+ } else if (num < @intFromEnum(Item.BitterBerry)) {
1006
+ const proc = if (stored.item == .MiracleBerry)
1007
+ Status.any(stored.status)
1008
+ else
1009
+ Status.is(stored.status, @enumFromInt(num - @intFromEnum(Item.MintBerry) + 2));
1010
+
1011
+ if (proc) {
1012
+ assert(Status.any(stored.status));
1013
+ try options.log.enditem(ident, stored.item, .Eat);
1014
+ stored.item = .None;
1015
+
1016
+ // FIXME wtf?
1017
+ // side.active.volatiles.Toxic = false;
1018
+ // side.active.volatiles.NightMare = false;
1019
+ if (stored.item == .MiracleBerry) {
1020
+ side.active.volatiles.Confusion = false;
1021
+ try options.log.end(ident, .Confusion);
1022
+ }
1023
+ stored.status = 0;
1024
+
1025
+ // FIXME recalc stats
1026
+
1027
+ try options.log.curestatus(ident, stored.status, .Message);
1028
+ }
1029
+ } else if (num == @intFromEnum(Item.BitterBerry)) {
1030
+ if (side.active.volatiles.Confusion) {
1031
+ try options.log.enditem(ident, stored.item, .Eat);
1032
+ stored.item = .None;
1033
+
1034
+ side.active.volatiles.Confusion = false;
1035
+ try options.log.end(ident, .Confusion);
1036
+ }
1037
+ }
1038
+ }
1039
+ }
1040
+
1041
+ // Encore
1042
+ inline for (players) |player| {
1043
+ var side = battle.side(player);
1044
+ var volatiles = &side.active.volatiles;
1045
+
1046
+ if (volatiles.Encore) {
1047
+ assert(volatiles.encore > 0);
1048
+ volatiles.encore -= 1;
1049
+
1050
+ if (volatiles.encore == 0 or side.active.move(mslot).pp == 0) {
1051
+ volatiles.Encore = false;
1052
+ try options.log.end(battle.active(player), .Encore);
1053
+ }
1054
+ }
1055
+ }
1056
+ }
1057
+
1058
+ inline fn updateSideCondition(
1059
+ battle: anytype,
1060
+ player: Player,
1061
+ comptime key: []const u8,
1062
+ comptime value: []const u8,
1063
+ options: anytype,
1064
+ ) !void {
1065
+ var side = battle.side(player);
1066
+ if (@field(side.conditions, key)) {
1067
+ assert(@field(side.conditions, value) > 0);
1068
+ @field(side.conditions, value) -= 1;
1069
+
1070
+ if (@field(side.conditions, value) == 0) {
1071
+ @field(side.conditions, key) = false;
1072
+ try options.log.sideend(player, @field(protocol.Side, key));
1073
+ }
1074
+ }
1075
+ }
1076
+
1077
+ fn endTurn(battle: anytype, options: anytype) @TypeOf(options.log).Error!Result {
1078
+ battle.turn += 1;
1079
+
1080
+ if (showdown and battle.turn >= 1000) {
1081
+ try options.log.tie();
1082
+ return Result.Tie;
1083
+ } else if (battle.turn >= 65535) {
1084
+ return Result.Error;
1085
+ }
1086
+
1087
+ try options.log.turn(battle.turn);
1088
+
1089
+ return Result.Default;
1090
+ }
1091
+
1092
+ fn buildRage(battle: anytype, player: Player, state: *State, options: anytype) !void {
1093
+ var foe = battle.foe(player);
1094
+ if (foe.active.volatiles.Rage) {
1095
+ if (showdown) {
1096
+ // TODO if showdown boost
1097
+ if (foe.active.boosts.atk < 6) {
1098
+ try Effects.boost(battle, player, state, options);
1099
+ }
1100
+ } else {
1101
+ foe.active.volatiles.rage +|= 1;
1102
+ try options.log.activate(battle.active(player), .Rage);
1103
+ }
1104
+ }
1105
+ }
1106
+
1107
+ fn kingsRock(battle: anytype, player: Player, _: *State, options: anytype) !void {
1108
+ var foe = battle.foe(player);
1109
+ const flinch = battle.side(player).stored().item == .KingsRock and
1110
+ !foe.active.volatiles.Substitute and
1111
+ try Rolls.item(battle, player, options);
1112
+ if (flinch) {
1113
+ foe.active.volatiles.Recharging = false;
1114
+ foe.active.volatiles.Flinch = true;
1115
+ }
1116
+ }
1117
+
1118
+ pub const Effects = struct {
1119
+ fn alwaysHit(battle: anytype, player: Player, state: *State, options: anytype) !void {
1120
+ _ = .{ battle, player, state, options }; // TODO
1121
+ }
1122
+
1123
+ fn attract(battle: anytype, player: Player, _: *State, options: anytype) !void {
1124
+ const side = battle.side(player);
1125
+ var foe = battle.foe(player);
1126
+
1127
+ const gender: u8 = @intFromEnum(side.stored().dvs.gender);
1128
+ const foe_gender: u8 = @intFromEnum(foe.stored().dvs.gender);
1129
+ if (gender + foe_gender != 1) {
1130
+ try options.log.immune(battle.active(player.foe()), .None);
1131
+ return;
1132
+ } else if (foe.active.volatiles.Flying or foe.active.volatiles.Underground) {
1133
+ try options.log.lastmiss();
1134
+ try options.log.miss(battle.active(player));
1135
+ return;
1136
+ }
1137
+
1138
+ foe.active.volatiles.Attract = true;
1139
+ try options.log.start(battle.active(player.foe()), .Attract);
1140
+ }
1141
+
1142
+ fn batonPass(battle: anytype, player: Player, state: *State, options: anytype) !void {
1143
+ _ = .{ battle, player, state, options }; // TODO
1144
+ }
1145
+
1146
+ fn beatUp(battle: anytype, player: Player, state: *State, options: anytype) !void {
1147
+ _ = .{ battle, player, state, options }; // TODO
1148
+ }
1149
+
1150
+ fn bellyDrum(battle: anytype, player: Player, state: *State, options: anytype) !void {
1151
+ _ = .{ battle, player, state, options }; // TODO
1152
+ }
1153
+
1154
+ fn bide(battle: anytype, player: Player, state: *State, options: anytype) !void {
1155
+ _ = .{ battle, player, state, options }; // TODO
1156
+ }
1157
+
1158
+ fn binding(battle: anytype, player: Player, state: *State, options: anytype) !void {
1159
+ _ = .{ battle, player, state, options }; // TODO
1160
+ }
1161
+
1162
+ fn burnChance(battle: anytype, player: Player, state: *State, options: anytype) !void {
1163
+ _ = .{ battle, player, state, options }; // TODO
1164
+ }
1165
+
1166
+ fn confusion(battle: anytype, player: Player, state: *State, options: anytype) !void {
1167
+ _ = .{ battle, player, state, options }; // TODO
1168
+ }
1169
+
1170
+ fn conversion(battle: anytype, player: Player, _: *State, options: anytype) !void {
1171
+ var side = battle.side(player);
1172
+
1173
+ var n: u3 = 0;
1174
+ for (side.active.moves) |m| {
1175
+ if (convertible(side.active, m.id)) n += 1;
1176
+ }
1177
+ if (n == 0) {
1178
+ try options.log.fail(battle.active(player), .None);
1179
+ return;
1180
+ }
1181
+
1182
+ side.active.types.type1 = try Rolls.conversion(battle, player, n, options);
1183
+ side.active.types.type2 = side.active.types.type1;
1184
+ }
1185
+
1186
+ fn conversion2(battle: anytype, player: Player, _: *State, options: anytype) !void {
1187
+ var side = battle.side(player);
1188
+
1189
+ const last = battle.foe(player).lastMove(false);
1190
+ if (last == .None) {
1191
+ try options.log.fail(battle.active(player), .None);
1192
+ return;
1193
+ }
1194
+ const move = Move.get(last);
1195
+ if (move.type == .@"???") {
1196
+ try options.log.fail(battle.active(player), .None);
1197
+ return;
1198
+ }
1199
+
1200
+ side.active.types.type1 = try Rolls.conversion2(battle, player, move.type, options);
1201
+ side.active.types.type2 = side.active.types.type1;
1202
+ }
1203
+
1204
+ fn counter(battle: anytype, player: Player, state: *State, options: anytype) !void {
1205
+ _ = .{ battle, player, state, options }; // TODO
1206
+ }
1207
+
1208
+ fn curse(battle: anytype, player: Player, state: *State, options: anytype) !void {
1209
+ _ = .{ battle, player, state, options }; // TODO
1210
+ }
1211
+
1212
+ fn defenseCurl(battle: anytype, player: Player, state: *State, options: anytype) !void {
1213
+ _ = .{ battle, player, state, options }; // TODO
1214
+ }
1215
+
1216
+ fn destinyBond(battle: anytype, player: Player, _: *State, options: anytype) !void {
1217
+ battle.side(player).active.volatiles.DestinyBond = true;
1218
+ try options.log.singlemove(battle.active(player), Move.DestinyBond);
1219
+ }
1220
+
1221
+ fn disable(battle: anytype, player: Player, _: *State, options: anytype) !void {
1222
+ var foe = battle.foe(player);
1223
+ var volatiles = &foe.active.volatiles;
1224
+ const foe_ident = battle.active(player.foe());
1225
+
1226
+ if (volatiles.disable.move != 0) {
1227
+ try options.log.fail(foe_ident, .None);
1228
+ return;
1229
+ }
1230
+
1231
+ const last = battle.foe(player).lastMove(false);
1232
+ if (last == .None or last == .Struggle) {
1233
+ try options.log.fail(foe_ident, .None);
1234
+ return;
1235
+ }
1236
+
1237
+ var slot: u3 = 0;
1238
+ for (foe.active.moves) |m| {
1239
+ assert(m.id != .None);
1240
+ slot += 1;
1241
+ if (m.id == last) break;
1242
+ }
1243
+ const move = foe.active.move(slot);
1244
+ if (move.pp == 0) {
1245
+ try options.log.fail(foe_ident, .None);
1246
+ return;
1247
+ }
1248
+
1249
+ volatiles.disable.move = slot;
1250
+ volatiles.disable.duration = Rolls.disableDuration(battle, player, options);
1251
+
1252
+ try options.log.startEffect(foe_ident, .Disable, move.id);
1253
+ }
1254
+
1255
+ fn doubleHit(battle: anytype, player: Player, state: *State, options: anytype) !void {
1256
+ _ = .{ battle, player, state, options }; // TODO
1257
+ }
1258
+
1259
+ fn drainHP(battle: anytype, player: Player, state: *State, options: anytype) !void {
1260
+ _ = .{ battle, player, state, options }; // TODO
1261
+ }
1262
+
1263
+ fn encore(battle: anytype, player: Player, state: *State, options: anytype) !void {
1264
+ var foe = battle.foe(player);
1265
+ var volatiles = &foe.active.volatiles;
1266
+ const foe_ident = battle.active(player.foe());
1267
+
1268
+ const last = battle.foe(player).lastMove(true);
1269
+ const failed = volatiles.Encore or
1270
+ (last == .None or last == .Struggle or
1271
+ last == .Encore or .last == .MirrorMove);
1272
+ if (failed) {
1273
+ try options.log.fail(foe_ident, .None);
1274
+ return;
1275
+ }
1276
+
1277
+ var slot: u3 = 0;
1278
+ for (foe.active.moves) |m| {
1279
+ assert(m.id != .None);
1280
+ slot += 1;
1281
+ if (m.id == last) break;
1282
+ }
1283
+ const move = foe.active.move(slot);
1284
+ if (move.pp == 0) {
1285
+ try options.log.fail(foe_ident, .None);
1286
+ return;
1287
+ }
1288
+
1289
+ volatiles.Encore = true;
1290
+ volatiles.encore = Rolls.encoreDuration(battle, player, options);
1291
+
1292
+ // TODO ???
1293
+ _ = state;
1294
+
1295
+ try options.log.startEffect(foe_ident, .Encore, move.id);
1296
+ }
1297
+
1298
+ fn explode(battle: anytype, player: Player, state: *State, options: anytype) !void {
1299
+ _ = .{ battle, player, state, options }; // TODO
1300
+ }
1301
+
1302
+ fn falseSwipe(battle: anytype, player: Player, state: *State, _: anytype) !void {
1303
+ const foe = battle.foe(player);
1304
+ assert(foe.stored().hp > 0);
1305
+ // FIXME crit?
1306
+ if (state.damage > foe.stored().hp) state.damage = foe.stored().hp - 1;
1307
+ }
1308
+
1309
+ fn fixedDamage(battle: anytype, player: Player, state: *State, options: anytype) !void {
1310
+ _ = .{ battle, player, state, options }; // TODO
1311
+ }
1312
+
1313
+ fn flameWheel(battle: anytype, player: Player, state: *State, options: anytype) !void {
1314
+ _ = .{ battle, player, state, options }; // TODO
1315
+ }
1316
+
1317
+ fn flinchChance(battle: anytype, player: Player, state: *State, options: anytype) !void {
1318
+ _ = .{ battle, player, state, options }; // TODO
1319
+ }
1320
+
1321
+ fn flyDig(battle: anytype, player: Player, state: *State, options: anytype) !void {
1322
+ _ = .{ battle, player, state, options }; // TODO
1323
+ }
1324
+
1325
+ fn focusEnergy(battle: anytype, player: Player, _: *State, options: anytype) !void {
1326
+ var side = battle.side(player);
1327
+
1328
+ if (side.active.volatiles.FocusEnergy) {
1329
+ if (!showdown) try options.log.fail(battle.active(player), .None);
1330
+ return;
1331
+ }
1332
+
1333
+ side.active.volatiles.FocusEnergy = true;
1334
+ try options.log.start(battle.active(player), .FocusEnergy);
1335
+ }
1336
+
1337
+ var SLOTS: [5]u4 = .{ 0, 0, 0, 0, 0 };
1338
+
1339
+ fn forceSwitch(battle: anytype, player: Player, _: *State, options: anytype) !void {
1340
+ const foe = battle.foe(player);
1341
+
1342
+ var n: u4 = 0;
1343
+ var i: u4 = 0;
1344
+ for (foe.pokemon, 0..) |pokemon, j| {
1345
+ if (pokemon.species == .None) break;
1346
+ n += 1;
1347
+ if (pokemon.position == 1) continue;
1348
+ if (pokemon.hp > 0) {
1349
+ SLOTS[i] = @intCast(foe.pokemon[j].position);
1350
+ i += 1;
1351
+ }
1352
+ }
1353
+
1354
+ if (n == 0) {
1355
+ try options.log.fail(battle.active(player), .None);
1356
+ return;
1357
+ }
1358
+
1359
+ _ = try Rolls.forceSwitch(battle, player, SLOTS[0..i], n, options); // TODO
1360
+ }
1361
+
1362
+ fn foresight(battle: anytype, player: Player, _: *State, options: anytype) !void {
1363
+ var foe = battle.foe(player);
1364
+
1365
+ if (foe.active.volatiles.Flying or foe.active.volatiles.Underground) {
1366
+ try options.log.lastmiss();
1367
+ try options.log.miss(battle.active(player));
1368
+ return;
1369
+ } else if (foe.active.volatiles.Foresight) {
1370
+ try options.log.fail(battle.active(player), .None);
1371
+ return;
1372
+ }
1373
+
1374
+ foe.active.volatiles.Foresight = true;
1375
+ try options.log.start(battle.active(player.foe()), .FocusEnergy);
1376
+ }
1377
+
1378
+ fn freezeChance(battle: anytype, player: Player, state: *State, options: anytype) !void {
1379
+ _ = .{ battle, player, state, options }; // TODO
1380
+ }
1381
+
1382
+ fn furyCutter(battle: anytype, player: Player, state: *State, options: anytype) !void {
1383
+ _ = .{ battle, player, state, options }; // TODO
1384
+ }
1385
+
1386
+ fn futureSight(battle: anytype, player: Player, state: *State, options: anytype) !void {
1387
+ _ = .{ battle, player, state, options }; // TODO
1388
+ }
1389
+
1390
+ fn happiness(battle: anytype, player: Player, state: *State, _: anytype) !void {
1391
+ const val = battle.side(player).stored().happiness;
1392
+ const frustration = Move.get(state.move).effect == .Frustration;
1393
+ state.bp = ((if (frustration) 255 - val else val) * 10) / 25;
1394
+ }
1395
+
1396
+ fn haze(battle: anytype, player: Player, state: *State, options: anytype) !void {
1397
+ _ = .{ battle, player, state, options }; // TODO
1398
+ }
1399
+
1400
+ fn heal(battle: anytype, player: Player, state: *State, options: anytype) !void {
1401
+ _ = .{ battle, player, state, options }; // TODO
1402
+ }
1403
+
1404
+ fn healBell(battle: anytype, player: Player, state: *State, options: anytype) !void {
1405
+ _ = .{ battle, player, state, options }; // TODO
1406
+ }
1407
+
1408
+ fn hiddenPower(battle: anytype, player: Player, state: *State, options: anytype) !void {
1409
+ _ = .{ battle, player, state, options }; // TODO
1410
+ }
1411
+
1412
+ fn hyperBeam(battle: anytype, player: Player, state: *State, options: anytype) !void {
1413
+ _ = .{ battle, player, state, options }; // TODO
1414
+ }
1415
+
1416
+ fn jumpKick(battle: anytype, player: Player, state: *State, options: anytype) !void {
1417
+ _ = .{ battle, player, state, options }; // TODO
1418
+ }
1419
+
1420
+ fn leechSeed(battle: anytype, player: Player, state: *State, options: anytype) !void {
1421
+ _ = .{ battle, player, state, options }; // TODO
1422
+ }
1423
+
1424
+ fn levelDamage(battle: anytype, player: Player, state: *State, options: anytype) !void {
1425
+ _ = .{ battle, player, state, options }; // TODO
1426
+ }
1427
+
1428
+ fn lockOn(battle: anytype, player: Player, state: *State, options: anytype) !void {
1429
+ _ = .{ battle, player, state, options }; // TODO
1430
+ }
1431
+
1432
+ const MAGNITUDE_POWER = [_]u8{ 10, 30, 50, 70, 90, 110, 150 };
1433
+
1434
+ fn magnitude(battle: anytype, player: Player, state: *State, options: anytype) !void {
1435
+ _ = .{ battle, player, state, options }; // TODO
1436
+ }
1437
+
1438
+ fn meanLook(battle: anytype, player: Player, state: *State, options: anytype) !void {
1439
+ _ = .{ battle, player, state, options }; // TODO
1440
+ }
1441
+
1442
+ fn metronome(battle: anytype, player: Player, state: *State, options: anytype) !void {
1443
+ _ = .{ battle, player, state, options }; // TODO
1444
+ }
1445
+
1446
+ fn mimic(battle: anytype, player: Player, state: *State, options: anytype) !void {
1447
+ _ = .{ battle, player, state, options }; // TODO
1448
+ }
1449
+
1450
+ fn mirrorCoat(battle: anytype, player: Player, state: *State, options: anytype) !void {
1451
+ _ = .{ battle, player, state, options }; // TODO
1452
+ }
1453
+
1454
+ fn mirrorMove(battle: anytype, player: Player, state: *State, options: anytype) !void {
1455
+ _ = .{ battle, player, state, options }; // TODO
1456
+ }
1457
+
1458
+ fn mist(battle: anytype, player: Player, state: *State, options: anytype) !void {
1459
+ _ = .{ battle, player, state, options }; // TODO
1460
+ }
1461
+
1462
+ fn weatherHeal(battle: anytype, player: Player, state: *State, options: anytype) !void {
1463
+ _ = .{ battle, player, state, options }; // TODO
1464
+ }
1465
+
1466
+ fn multiHit(battle: anytype, player: Player, state: *State, options: anytype) !void {
1467
+ _ = .{ battle, player, state, options }; // TODO
1468
+ }
1469
+
1470
+ fn nightmare(battle: anytype, player: Player, state: *State, options: anytype) !void {
1471
+ _ = .{ battle, player, state, options }; // TODO
1472
+ }
1473
+
1474
+ fn ohko(battle: anytype, player: Player, state: *State, options: anytype) !void {
1475
+ _ = .{ battle, player, state, options }; // TODO
1476
+ }
1477
+
1478
+ fn painSplit(battle: anytype, player: Player, state: *State, options: anytype) !void {
1479
+ _ = .{ battle, player, state, options }; // TODO
1480
+ }
1481
+
1482
+ fn paralyze(battle: anytype, player: Player, state: *State, options: anytype) !void {
1483
+ _ = .{ battle, player, state, options }; // TODO
1484
+ }
1485
+
1486
+ fn paralyzeChance(battle: anytype, player: Player, state: *State, options: anytype) !void {
1487
+ // TODO: Thunder does acc/checkHit/effectChance before stab + damage variation
1488
+ _ = .{ battle, player, state, options }; // TODO
1489
+ }
1490
+
1491
+ fn payDay(battle: anytype, player: Player, state: *State, options: anytype) !void {
1492
+ _ = .{ battle, player, state, options }; // TODO
1493
+ }
1494
+
1495
+ fn perishSong(battle: anytype, player: Player, state: *State, options: anytype) !void {
1496
+ _ = .{ battle, player, state, options }; // TODO
1497
+ }
1498
+
1499
+ fn poison(battle: anytype, player: Player, state: *State, options: anytype) !void {
1500
+ _ = .{ battle, player, state, options }; // TODO
1501
+ }
1502
+
1503
+ fn present(battle: anytype, player: Player, state: *State, options: anytype) !void {
1504
+ _ = .{ battle, player, state, options }; // TODO
1505
+ }
1506
+
1507
+ fn protect(battle: anytype, player: Player, state: *State, options: anytype) !void {
1508
+ var side = battle.side(player);
1509
+ var volatiles = &side.active.volatiles;
1510
+
1511
+ const failed = !state.first or volatiles.Substitute or
1512
+ !try Rolls.protect(battle, player, volatiles.protect, options);
1513
+ if (failed) {
1514
+ try options.log.fail(battle.active(player), .None);
1515
+ return;
1516
+ }
1517
+
1518
+ volatiles.protect = @max(8, volatiles.protect + 1);
1519
+ if (state.move == .Endure) {
1520
+ volatiles.Endure = true;
1521
+ } else {
1522
+ volatiles.Protect = true;
1523
+ }
1524
+ try options.log.singleturn(battle.active(player), state.move);
1525
+ }
1526
+
1527
+ fn psychUp(battle: anytype, player: Player, state: *State, options: anytype) !void {
1528
+ _ = .{ battle, player, state, options }; // TODO
1529
+ }
1530
+
1531
+ fn pursuit(battle: anytype, player: Player, state: *State, options: anytype) !void {
1532
+ _ = .{ battle, player, state, options }; // TODO
1533
+ }
1534
+
1535
+ fn rage(battle: anytype, player: Player, state: *State, options: anytype) !void {
1536
+ _ = .{ battle, player, state, options }; // TODO
1537
+ }
1538
+
1539
+ fn rainDance(battle: anytype, player: Player, state: *State, options: anytype) !void {
1540
+ _ = .{ battle, player, state, options }; // TODO
1541
+ }
1542
+
1543
+ fn rapidSpin(battle: anytype, player: Player, state: *State, options: anytype) !void {
1544
+ _ = .{ battle, player, state, options }; // TODO
1545
+ }
1546
+
1547
+ fn razorWind(battle: anytype, player: Player, state: *State, options: anytype) !void {
1548
+ _ = .{ battle, player, state, options }; // TODO
1549
+ }
1550
+
1551
+ fn recoil(battle: anytype, player: Player, state: *State, options: anytype) !void {
1552
+ _ = .{ battle, player, state, options }; // TODO
1553
+ }
1554
+
1555
+ fn reversal(battle: anytype, player: Player, state: *State, options: anytype) !void {
1556
+ _ = .{ battle, player, state, options }; // TODO
1557
+ }
1558
+
1559
+ fn rollout(battle: anytype, player: Player, state: *State, options: anytype) !void {
1560
+ _ = .{ battle, player, state, options }; // TODO
1561
+ }
1562
+
1563
+ fn sacredFire(battle: anytype, player: Player, state: *State, options: anytype) !void {
1564
+ _ = .{ battle, player, state, options }; // TODO
1565
+ }
1566
+
1567
+ fn safeguard(battle: anytype, player: Player, state: *State, options: anytype) !void {
1568
+ _ = .{ battle, player, state, options }; // TODO
1569
+ }
1570
+
1571
+ fn sandstorm(battle: anytype, player: Player, state: *State, options: anytype) !void {
1572
+ _ = .{ battle, player, state, options }; // TODO
1573
+ }
1574
+
1575
+ fn screens(battle: anytype, player: Player, state: *State, options: anytype) !void {
1576
+ _ = .{ battle, player, state, options }; // TODO
1577
+ }
1578
+
1579
+ fn sketch(battle: anytype, player: Player, state: *State, options: anytype) !void {
1580
+ _ = .{ battle, player, state, options }; // TODO
1581
+ }
1582
+
1583
+ fn skullBash(battle: anytype, player: Player, state: *State, options: anytype) !void {
1584
+ _ = .{ battle, player, state, options }; // TODO
1585
+ }
1586
+
1587
+ fn skyAttack(battle: anytype, player: Player, state: *State, options: anytype) !void {
1588
+ _ = .{ battle, player, state, options }; // TODO
1589
+ }
1590
+
1591
+ fn sleep(battle: anytype, player: Player, state: *State, options: anytype) !void {
1592
+ _ = .{ battle, player, state, options }; // TODO
1593
+ }
1594
+
1595
+ fn sleepTalk(battle: anytype, player: Player, state: *State, options: anytype) !void {
1596
+ const side = battle.side(player);
1597
+
1598
+ if (!Status.is(side.stored().status, .SLP)) {
1599
+ if (!showdown) try options.log.fail(battle.active(player), .None);
1600
+ return;
1601
+ }
1602
+
1603
+ var n: u3 = 0;
1604
+ for (side.active.moves, 0..) |m, i| {
1605
+ if (state.mslot - 1 != i and Move.get(m.id).extra.sleep_talk) n += 1;
1606
+ }
1607
+ if (n == 0) {
1608
+ try options.log.fail(battle.active(player), .None);
1609
+ return;
1610
+ }
1611
+
1612
+ _ = try Rolls.sleepTalk(battle, player, n, state.mslot, options); // TODO
1613
+ }
1614
+
1615
+ fn snore(battle: anytype, player: Player, state: *State, options: anytype) !void {
1616
+ _ = .{ battle, player, state, options }; // TODO
1617
+ }
1618
+
1619
+ fn solarBeam(battle: anytype, player: Player, state: *State, options: anytype) !void {
1620
+ _ = .{ battle, player, state, options }; // TODO
1621
+ }
1622
+
1623
+ fn spikes(battle: anytype, player: Player, state: *State, options: anytype) !void {
1624
+ _ = .{ battle, player, state, options }; // TODO
1625
+ }
1626
+
1627
+ fn spite(battle: anytype, player: Player, state: *State, options: anytype) !void {
1628
+ _ = .{ battle, player, state, options }; // TODO
1629
+ }
1630
+
1631
+ fn splash(battle: anytype, player: Player, state: *State, options: anytype) !void {
1632
+ _ = .{ battle, player, state, options }; // TODO
1633
+ }
1634
+
1635
+ fn stomp(battle: anytype, player: Player, state: *State, options: anytype) !void {
1636
+ _ = .{ battle, player, state, options }; // TODO
1637
+ }
1638
+
1639
+ fn substitute(battle: anytype, player: Player, state: *State, options: anytype) !void {
1640
+ _ = .{ battle, player, state, options }; // TODO
1641
+ }
1642
+
1643
+ fn sunnyDay(battle: anytype, player: Player, state: *State, options: anytype) !void {
1644
+ _ = .{ battle, player, state, options }; // TODO
1645
+ }
1646
+
1647
+ fn swagger(battle: anytype, player: Player, state: *State, options: anytype) !void {
1648
+ _ = .{ battle, player, state, options }; // TODO
1649
+ }
1650
+
1651
+ fn synthesis(battle: anytype, player: Player, state: *State, options: anytype) !void {
1652
+ _ = .{ battle, player, state, options }; // TODO
1653
+ }
1654
+
1655
+ fn teleport(battle: anytype, player: Player, state: *State, options: anytype) !void {
1656
+ _ = .{ battle, player, state, options }; // TODO
1657
+ }
1658
+
1659
+ fn thief(battle: anytype, player: Player, state: *State, options: anytype) !void {
1660
+ _ = .{ battle, player, state, options }; // TODO
1661
+ }
1662
+
1663
+ fn thrashing(battle: anytype, player: Player, state: *State, options: anytype) !void {
1664
+ _ = .{ battle, player, state, options }; // TODO
1665
+ }
1666
+
1667
+ fn toxic(battle: anytype, player: Player, state: *State, options: anytype) !void {
1668
+ _ = .{ battle, player, state, options }; // TODO
1669
+ }
1670
+
1671
+ fn transform(battle: anytype, player: Player, state: *State, options: anytype) !void {
1672
+ _ = .{ battle, player, state, options }; // TODO
1673
+ }
1674
+
1675
+ fn tripleKick(battle: anytype, player: Player, state: *State, options: anytype) !void {
1676
+ _ = .{ battle, player, state, options }; // TODO
1677
+ }
1678
+
1679
+ fn twister(battle: anytype, player: Player, state: *State, options: anytype) !void {
1680
+ _ = .{ battle, player, state, options }; // TODO
1681
+ }
1682
+
1683
+ fn boost(battle: anytype, player: Player, state: *State, options: anytype) !void {
1684
+ _ = .{ battle, player, state, options }; // TODO
1685
+ }
1686
+
1687
+ fn unboost(battle: anytype, player: Player, state: *State, options: anytype) !void {
1688
+ _ = .{ battle, player, state, options }; // TODO
1689
+ }
1690
+ };
1691
+
1692
+ fn statusModify(status: u8, stats: *Stats(u16)) void {
1693
+ if (Status.is(status, .PAR)) {
1694
+ stats.spe = @max(stats.spe / 4, 1);
1695
+ } else if (Status.is(status, .BRN)) {
1696
+ stats.atk = @max(stats.atk / 2, 1);
1697
+ }
1698
+ }
1699
+
1700
+ inline fn isForced(active: anytype) bool {
1701
+ return active.volatiles.Recharging or active.volatiles.Rage or
1702
+ active.volatiles.Thrashing or active.volatiles.Charging or
1703
+ active.volatiles.Rollout;
1704
+ }
1705
+
1706
+ inline fn convertible(active: ActivePokemon, m: Move) bool {
1707
+ if (m == .None) return false;
1708
+ const t = Move.get(m).type;
1709
+ return t != .@"???" and !active.types.includes(t);
1710
+ }
1711
+
1712
+ pub const Rolls = struct {
1713
+ inline fn speedTie(battle: anytype, options: anytype) !bool {
1714
+ const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
1715
+ player == .P1
1716
+ else if (showdown)
1717
+ battle.rng.range(u8, 0, 2) == 0
1718
+ else
1719
+ battle.rng.next() < Gen12.percent(50) + 1;
1720
+
1721
+ try options.chance.speedTie(p1);
1722
+ return p1;
1723
+ }
1724
+
1725
+ inline fn quickClaw(battle: anytype, player: Player, options: anytype) !bool {
1726
+ const qkc = if (options.calc.overridden(player, .quick_claw)) |val|
1727
+ val == .true
1728
+ else if (showdown)
1729
+ battle.rng.chance(u8, 60, 256)
1730
+ else
1731
+ battle.rng.next() < 60;
1732
+
1733
+ try options.chance.quickClaw(player, qkc);
1734
+ return qkc;
1735
+ }
1736
+
1737
+ inline fn hit(battle: anytype, player: Player, accuracy: u8, options: anytype) !bool {
1738
+ const ok = if (options.calc.overridden(player, .hit)) |val|
1739
+ val == .true
1740
+ else if (showdown)
1741
+ battle.rng.chance(u8, accuracy, 256)
1742
+ else
1743
+ battle.rng.next() < accuracy;
1744
+
1745
+ try options.chance.hit(player, ok, accuracy);
1746
+ return ok;
1747
+ }
1748
+
1749
+ inline fn criticalHit(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
1750
+ const crit = if (options.calc.overridden(player, .critical_hit)) |val|
1751
+ val == .true
1752
+ else if (showdown)
1753
+ battle.rng.chance(u8, rate, 256)
1754
+ else
1755
+ battle.rng.next() < rate;
1756
+
1757
+ try options.chance.criticalHit(player, crit, rate);
1758
+ return crit;
1759
+ }
1760
+
1761
+ inline fn damage(battle: anytype, player: anytype, options: anytype) !u8 {
1762
+ const roll = if (options.calc.overridden(player, .damage)) |val|
1763
+ val
1764
+ else roll: {
1765
+ if (showdown) break :roll battle.rng.range(u8, 217, 256);
1766
+ while (true) {
1767
+ const r = std.math.rotr(u8, battle.rng.next(), 1);
1768
+ if (r >= 217) break :roll r;
1769
+ }
1770
+ };
1771
+
1772
+ assert(roll >= 217 and roll <= 255);
1773
+ try options.chance.damage(player, roll);
1774
+ return roll;
1775
+ }
1776
+
1777
+ inline fn confused(battle: anytype, player: Player, options: anytype) !bool {
1778
+ const cfz = if (options.calc.overridden(player, .confused)) |val|
1779
+ val == .true
1780
+ else if (showdown)
1781
+ !battle.rng.chance(u8, 128, 256)
1782
+ else
1783
+ battle.rng.next() >= Gen12.percent(50) + 1;
1784
+
1785
+ try options.chance.confused(player, cfz);
1786
+ return cfz;
1787
+ }
1788
+
1789
+ inline fn attract(battle: anytype, player: Player, options: anytype) !bool {
1790
+ const cant = if (options.calc.overridden(player, .attract)) |val|
1791
+ val == .true
1792
+ else if (showdown)
1793
+ battle.rng.chance(u8, 1, 2)
1794
+ else
1795
+ battle.rng.next() > Gen12.percent(50) + 1;
1796
+
1797
+ try options.chance.attract(player, cant);
1798
+ return cant;
1799
+ }
1800
+
1801
+ inline fn paralyzed(battle: anytype, player: Player, options: anytype) !bool {
1802
+ const par = if (options.calc.overridden(player, .paralyzed)) |val|
1803
+ val == .true
1804
+ else if (showdown)
1805
+ battle.rng.chance(u8, 63, 256)
1806
+ else
1807
+ battle.rng.next() < Gen12.percent(25);
1808
+
1809
+ try options.chance.paralyzed(player, par);
1810
+ return par;
1811
+ }
1812
+
1813
+ inline fn defrost(battle: anytype, player: Player, options: anytype) !bool {
1814
+ const thaw = if (options.calc.overridden(player, .defrost)) |val|
1815
+ val == .true
1816
+ else if (showdown)
1817
+ battle.rng.chance(u8, 25, 256)
1818
+ else
1819
+ battle.rng.next() < Gen12.percent(10);
1820
+
1821
+ try options.chance.defrost(player, thaw);
1822
+ return thaw;
1823
+ }
1824
+
1825
+ inline fn secondaryChance(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
1826
+ // GLITCH: 100% secondary chance will still fail 1/256 of the time
1827
+ const proc = if (options.calc.overridden(player, .secondary_chance)) |val|
1828
+ val == .true
1829
+ else if (showdown)
1830
+ battle.rng.chance(u8, rate, 256)
1831
+ else
1832
+ battle.rng.next() < rate;
1833
+
1834
+ try options.chance.secondaryChance(player, proc, rate);
1835
+ return proc;
1836
+ }
1837
+
1838
+ inline fn item(battle: anytype, player: Player, options: anytype) !bool {
1839
+ const proc = if (options.calc.overridden(player, .item)) |val|
1840
+ val == .true
1841
+ else if (showdown)
1842
+ battle.rng.chance(u8, 30, 256)
1843
+ else
1844
+ battle.rng.next() < 30;
1845
+
1846
+ try options.chance.item(player, proc);
1847
+ return proc;
1848
+ }
1849
+
1850
+ inline fn triAttack(battle: anytype, player: Player, options: anytype) !u8 {
1851
+ const status: TriAttack = if (options.calc.overridden(player, .tri_attack)) |val|
1852
+ val
1853
+ else if (showdown)
1854
+ @enumFromInt(battle.rng.range(u8, 0, 2))
1855
+ else loop: {
1856
+ while (true) {
1857
+ const r = std.math.rotr(u8, std.battle.rng.next(), 4) & 3;
1858
+ if (r != 0) break :loop @enumFromInt(r - 1);
1859
+ }
1860
+ };
1861
+
1862
+ try options.chance.triAttack(player, status);
1863
+ return status.status();
1864
+ }
1865
+
1866
+ inline fn present(battle: anytype, player: Player, options: anytype) !u8 {
1867
+ const power = if (options.calc.overridden(player, .present)) |val|
1868
+ (val - 1) * 40
1869
+ else if (showdown) power: {
1870
+ const r = battle.rng.range(u8, 0, 10);
1871
+ if (r < 2) break :power 0;
1872
+ if (r < 6) break :power 40;
1873
+ if (r < 9) break :power 80;
1874
+ break :power 120;
1875
+ } else power: {
1876
+ const r = battle.rng.next();
1877
+ if (r <= Gen12.percent(40)) break :power 40;
1878
+ if (r <= Gen12.percent(70) + 1) break :power 80;
1879
+ if (r <= Gen12.percent(80)) break :power 120;
1880
+ break :power 0;
1881
+ };
1882
+
1883
+ assert(power <= 120);
1884
+ try options.chance.present(player, power);
1885
+ return power;
1886
+ }
1887
+
1888
+ inline fn magnitude(battle: anytype, player: Player, options: anytype) !u8 {
1889
+ const num = if (options.calc.overridden(player, .magnitude)) |val|
1890
+ val + 3
1891
+ else if (showdown) num: {
1892
+ const r = battle.rng.range(u8, 0, 100);
1893
+ if (r < 5) break :num 4;
1894
+ if (r < 15) break :num 5;
1895
+ if (r < 35) break :num 6;
1896
+ if (r < 65) break :num 7;
1897
+ if (r < 85) break :num 8;
1898
+ if (r < 95) break :num 9;
1899
+ break :num 10;
1900
+ } else num: {
1901
+ const r = battle.rng.next();
1902
+ if (r <= Gen12.percent(5) + 1) break :num 4;
1903
+ if (r <= Gen12.percent(15)) break :num 5;
1904
+ if (r <= Gen12.percent(35)) break :num 6;
1905
+ if (r <= Gen12.percent(65) + 1) break :num 7;
1906
+ if (r <= Gen12.percent(85) + 1) break :num 8;
1907
+ if (r <= Gen12.percent(95)) break :num 9;
1908
+ break :num 10;
1909
+ };
1910
+
1911
+ assert(num >= 4 and num <= 10);
1912
+ try options.chance.magnitude(player, num);
1913
+ return num;
1914
+ }
1915
+
1916
+ inline fn tripleKick(battle: anytype, player: Player, options: anytype) !u2 {
1917
+ const hits = if (options.calc.overridden(player, .triple_kick)) |val|
1918
+ val
1919
+ else if (showdown)
1920
+ battle.rng.range(u2, 1, 4)
1921
+ else power: {
1922
+ while (true) {
1923
+ const r = battle.rng.next() & 3;
1924
+ if (r != 0) break :power r - 1;
1925
+ }
1926
+ };
1927
+
1928
+ assert(hits >= 1 and hits <= 3);
1929
+ try options.chance.tripleKick(player, hits);
1930
+ return hits;
1931
+ }
1932
+
1933
+ inline fn spite(battle: anytype, player: Player, options: anytype) !u8 {
1934
+ const pp = if (options.calc.overridden(player, .spite)) |val|
1935
+ val + 1
1936
+ else if (showdown)
1937
+ battle.rng.range(u8, 2, 6)
1938
+ else
1939
+ (battle.rng.next() & 3) + 2;
1940
+
1941
+ assert(pp >= 2 and pp <= 5);
1942
+ try options.chance.spite(player, pp);
1943
+ return pp;
1944
+ }
1945
+
1946
+ // Conversion 2 can at most choose between 7 types (vs. a Grass-type attack)
1947
+ var CONVERSION_2: [7]Type = [_]Type{.@"???"} ** 7;
1948
+
1949
+ inline fn conversion2(battle: anytype, player: Player, mtype: Type, options: anytype) !Type {
1950
+ var i: u8 = 0;
1951
+ const neutral = @intFromEnum(Effectiveness.Neutral);
1952
+ assert(mtype != .@"???");
1953
+ const ty: Type = if (options.calc.overridden(player, .conversion_2)) |val|
1954
+ @enumFromInt(@intFromEnum(val) - 1)
1955
+ else if (showdown) ty: {
1956
+ for (Type.SHOWDOWN) |t| {
1957
+ if (@intFromEnum(mtype.effectiveness(t)) < neutral) {
1958
+ CONVERSION_2[i] = t;
1959
+ i += 1;
1960
+ }
1961
+ }
1962
+ assert(i > 0 and i <= 7);
1963
+ break :ty CONVERSION_2[battle.rng.range(u8, 0, i)];
1964
+ } else ty: {
1965
+ while (true) {
1966
+ const r = battle.rng.next() & 0b11111;
1967
+ if (r == 6) continue; // BIRD
1968
+ if (r > 9 and r < 20) continue; // UNUSED_TYPES
1969
+ if (r > 27) continue; // TYPES_END
1970
+
1971
+ const t = Type.conversion2(r);
1972
+ if (@intFromEnum(mtype.effectiveness(t)) < neutral) {
1973
+ break :ty t;
1974
+ }
1975
+ }
1976
+ };
1977
+
1978
+ assert(ty != .@"???");
1979
+ try options.chance.conversion2(player, ty, mtype, i);
1980
+ return ty;
1981
+ }
1982
+
1983
+ inline fn protect(battle: anytype, player: Player, count: u8, options: anytype) !bool {
1984
+ assert(count <= 8);
1985
+ const num = if (count == 8) 0 else @as(u8, 255) >> @intCast(count);
1986
+
1987
+ const ok = if (options.calc.overridden(player, .protect)) |val|
1988
+ val == .true
1989
+ else if (showdown)
1990
+ battle.rng.chance(u8, num, 255)
1991
+ else protected: {
1992
+ while (true) {
1993
+ const r = battle.rng.next();
1994
+ if (r != 0) break :protected !(num <= r - 1);
1995
+ }
1996
+ };
1997
+
1998
+ try options.chance.protect(player, num, ok);
1999
+ return ok;
2000
+ }
2001
+
2002
+ fn conversion(battle: anytype, player: Player, n: u4, options: anytype) !Type {
2003
+ assert(n > 0);
2004
+
2005
+ const active = battle.side(player).active;
2006
+ const moves = active.moves;
2007
+
2008
+ // TODO: consider throwing error instead of rerolling?
2009
+ const overridden = if (options.calc.overridden(player, .move_slot)) |val|
2010
+ if (convertible(active, moves[val - 1].id)) val else null
2011
+ else
2012
+ null;
2013
+
2014
+ const slot: u4 = overridden orelse slot: {
2015
+ if (showdown) {
2016
+ var r = battle.rng.range(u4, 0, n) + 1;
2017
+ var i: usize = 0;
2018
+ while (i < moves.len and r > 0) : (i += 1) {
2019
+ if (convertible(active, moves[i].id)) {
2020
+ r -= 1;
2021
+ if (r == 0) break :slot @intCast(i + 1);
2022
+ }
2023
+ }
2024
+ break :slot @intCast(i + 1);
2025
+ } else {
2026
+ while (true) {
2027
+ const r: u4 = @intCast(battle.rng.next() & 3);
2028
+ if (convertible(active, moves[r].id)) break :slot @intCast(r + 1);
2029
+ }
2030
+ }
2031
+ };
2032
+
2033
+ assert(n >= 1 and n <= 4);
2034
+ assert(slot >= 1 and slot <= 4);
2035
+ try options.chance.moveSlot(player, slot, n);
2036
+ return Move.get(active.move(slot).id).type;
2037
+ }
2038
+
2039
+ fn sleepTalk(battle: anytype, player: Player, n: u4, mslot: u4, options: anytype) !u4 {
2040
+ assert(n > 0);
2041
+
2042
+ const active = battle.side(player).active;
2043
+ const moves = active.moves;
2044
+
2045
+ // TODO: consider throwing error instead of rerolling?
2046
+ const overridden = if (options.calc.overridden(player, .move_slot)) |val|
2047
+ if (val != mslot and Move.get(moves[val - 1].id).extra.sleep_talk) val else null
2048
+ else
2049
+ null;
2050
+
2051
+ const slot: u4 = overridden orelse slot: {
2052
+ if (showdown) {
2053
+ var r = battle.rng.range(u4, 0, n) + 1;
2054
+ var i: usize = 0;
2055
+ while (i < moves.len and r > 0) : (i += 1) {
2056
+ if (i + 1 != mslot and Move.get(moves[i].id).extra.sleep_talk) {
2057
+ r -= 1;
2058
+ if (r == 0) break :slot @intCast(i + 1);
2059
+ }
2060
+ }
2061
+ break :slot @intCast(i + 1);
2062
+ } else {
2063
+ while (true) {
2064
+ const r: u4 = @intCast(battle.rng.next() & 3);
2065
+ if (r + 1 == mslot or !Move.get(moves[r].id).extra.sleep_talk) continue;
2066
+ break :slot @intCast(r + 1);
2067
+ }
2068
+ }
2069
+ };
2070
+
2071
+ assert(n >= 1 and n <= 4);
2072
+ assert(slot >= 1 and slot <= 4);
2073
+ try options.chance.moveSlot(player, slot, n);
2074
+ return slot;
2075
+ }
2076
+
2077
+ inline fn forceSwitch(
2078
+ battle: anytype,
2079
+ player: Player,
2080
+ slots: []u4,
2081
+ n: u4,
2082
+ options: anytype,
2083
+ ) !u4 {
2084
+ const foe = battle.foe(player);
2085
+
2086
+ assert(slots.len < 5);
2087
+ assert(n > 0 and n <= 6);
2088
+
2089
+ // TODO: consider throwing error instead of rerolling?
2090
+ const overridden = if (options.calc.overridden(player, .force_switch)) |val| val: {
2091
+ for (slots) |slot| if (slot == val) break :val if (foe.get(slot).hp > 0) val else null;
2092
+ break :val null;
2093
+ } else null;
2094
+
2095
+ const slot: u4 = overridden orelse slot: {
2096
+ if (showdown) {
2097
+ break :slot slots[battle.rng.range(u4, 0, @as(u4, @intCast(slots.len)))];
2098
+ } else {
2099
+ while (true) {
2100
+ const r = battle.rng.next() & 7;
2101
+ if (r > n or r == 1) continue;
2102
+ if (foe.get(r).hp > 0) break :slot @intCast(r);
2103
+ }
2104
+ }
2105
+ };
2106
+
2107
+ assert(n >= 1 and n <= 6);
2108
+ assert(slot >= 1 and slot <= 6);
2109
+ try options.chance.forceSwitch(player, slot, n);
2110
+ return slot;
2111
+ }
2112
+
2113
+ const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
2114
+
2115
+ inline fn multiHit(battle: anytype, player: Player, options: anytype) !u3 {
2116
+ const n: u3 = if (options.calc.overridden(player, .multi_hit)) |val|
2117
+ @intCast(val)
2118
+ else if (showdown)
2119
+ DISTRIBUTION[battle.rng.range(u3, 0, DISTRIBUTION.len)]
2120
+ else n: {
2121
+ const r = (battle.rng.next() & 3);
2122
+ break :n @intCast((if (r < 2) r else battle.rng.next() & 3) + 2);
2123
+ };
2124
+
2125
+ assert(n >= 2 and n <= 5);
2126
+ try options.chance.multiHit(player, n);
2127
+ return n;
2128
+ }
2129
+
2130
+ inline fn psywave(battle: anytype, player: Player, max: u8, options: anytype) !u8 {
2131
+ const power = if (options.calc.overridden(player, .psywave)) |val|
2132
+ val - 1
2133
+ else if (showdown)
2134
+ battle.rng.range(u8, 0, max)
2135
+ else power: {
2136
+ while (true) {
2137
+ const r = battle.rng.next();
2138
+ if (r < max) break :power r;
2139
+ }
2140
+ };
2141
+
2142
+ assert(power < max);
2143
+ try options.chance.psywave(player, power, max);
2144
+ return power;
2145
+ }
2146
+
2147
+ inline fn metronome(battle: anytype, player: Player, options: anytype) !Move {
2148
+ const moves = battle.get(player).active.moves;
2149
+
2150
+ var n: u2 = 0;
2151
+ for (moves) |m| {
2152
+ if (m == .None) break;
2153
+ if (Move.get(m).metronome) n += 1;
2154
+ }
2155
+
2156
+ // TODO: consider throwing error instead of rerolling?
2157
+ const overridden = if (options.calc.overridden(player, .metronome)) |val| val: {
2158
+ for (moves) |m| if (m.id == val) break :val null;
2159
+ break :val val;
2160
+ } else null;
2161
+
2162
+ const move: Move = overridden orelse move: {
2163
+ if (showdown) {
2164
+ var r = battle.rng.range(u8, 0, Move.METRONOME.len - n);
2165
+ for (moves) |m| {
2166
+ if (m == .None or @intFromEnum(m) > @intFromEnum(Move.METRONOME[r])) break;
2167
+ if (Move.get(m).metronome) r -= 1;
2168
+ }
2169
+ break :move Move.METRONOME[r];
2170
+ } else {
2171
+ outer: while (true) {
2172
+ const r = battle.rng.next();
2173
+ if (r == 0 or r >= Move.size) continue;
2174
+ const selected: Move = @enumFromInt(r);
2175
+ if (!Move.get(selected).extra.metronome) continue;
2176
+ for (moves) |m| if (m.id == selected) continue :outer;
2177
+ break :move selected;
2178
+ }
2179
+ }
2180
+ };
2181
+
2182
+ assert(Move.get(move).metronome);
2183
+ try options.chance.metronome(player, move, n);
2184
+ return move;
2185
+ }
2186
+
2187
+ inline fn sleepDuration(battle: anytype, player: Player, options: anytype) u3 {
2188
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2189
+ @intCast(val)
2190
+ else if (showdown)
2191
+ battle.rng.range(u3, 1, 7)
2192
+ else duration: {
2193
+ while (true) {
2194
+ const r = battle.rng.next() & 7;
2195
+ if (r != 0 and r != 7) break :duration @intCast(r);
2196
+ }
2197
+ };
2198
+
2199
+ assert(duration >= 1 and duration <= 6);
2200
+ options.chance.duration(.sleep, player, player.foe(), duration);
2201
+ return duration + 1;
2202
+ }
2203
+
2204
+ inline fn confusionDuration(battle: anytype, player: Player, self: bool, options: anytype) u3 {
2205
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2206
+ @intCast(val)
2207
+ else if (showdown)
2208
+ // Pokémon Showdown incorrectly uses the same duration for self-confusion
2209
+ battle.rng.range(u3, 2, 6)
2210
+ else
2211
+ @intCast((battle.rng.next() & if (self) 1 else 3) + 2);
2212
+
2213
+ assert(duration >= 2 and duration <= if (!showdown and self) 3 else 5);
2214
+ options.chance.duration(.confusion, player, if (self) player else player.foe(), duration);
2215
+ return duration;
2216
+ }
2217
+
2218
+ inline fn disableDuration(battle: anytype, player: Player, options: anytype) u4 {
2219
+ const duration: u4 = if (options.calc.overridden(player, .duration)) |val|
2220
+ @intCast(val)
2221
+ else if (showdown)
2222
+ // Pokémon Showdown incorrectly inherits Generation III's Disable duration
2223
+ battle.rng.range(u4, 2, 6)
2224
+ else duration: {
2225
+ while (true) {
2226
+ const r = battle.rng.next() & 7;
2227
+ if (r != 0) break :duration @intCast(r + 1);
2228
+ }
2229
+ };
2230
+
2231
+ assert(duration >= 2 and duration <= (if (showdown) 5 else 7));
2232
+ options.chance.duration(.disable, player, player.foe(), duration);
2233
+ return duration;
2234
+ }
2235
+
2236
+ inline fn attackingDuration(battle: anytype, player: Player, options: anytype) u3 {
2237
+ const duration: u3 = if (options.calc.overridden(player, .duration)) |val|
2238
+ @intCast(val)
2239
+ else if (showdown)
2240
+ battle.rng.range(u3, 2, 4)
2241
+ else
2242
+ @intCast((battle.rng.next() & 1) + 2); // BUG: thrash is + 1 not +2
2243
+
2244
+ assert(duration >= 2 and duration <= 3);
2245
+ options.chance.duration(.attacking, player, player, duration);
2246
+ return duration;
2247
+ }
2248
+
2249
+ inline fn bindingDuration(battle: anytype, player: Player, options: anytype) u3 {
2250
+ const duration: u3 = (if (options.calc.overridden(player, .duration)) |val|
2251
+ @intCast(val)
2252
+ else if (showdown)
2253
+ // TODO: PS does range(3, 6) here which after subtracting 1 is actually 2-4 not 2-5!
2254
+ battle.rng.range(u3, 2, 5)
2255
+ else
2256
+ @intCast(battle.rng.next() & 3 + 2));
2257
+
2258
+ assert(duration >= 2 and duration <= 5);
2259
+ options.chance.duration(.binding, player, player.foe(), duration);
2260
+ return duration + 1;
2261
+ }
2262
+
2263
+ // TODO: consider sharing implementation with bindingDuration
2264
+ inline fn encoreDuration(battle: anytype, player: Player, options: anytype) u3 {
2265
+ const duration: u3 = (if (options.calc.overridden(player, .duration)) |val|
2266
+ @intCast(val)
2267
+ else if (showdown)
2268
+ battle.rng.range(u3, 2, 6)
2269
+ else
2270
+ @intCast(battle.rng.next() & 3 + 2));
2271
+
2272
+ assert(duration >= 2 and duration <= 5);
2273
+ options.chance.duration(.encore, player, player.foe(), duration);
2274
+ return duration + 1;
2275
+ }
2276
+ };
2277
+
2278
+ test "RNG agreement" {
2279
+ if (!showdown) return;
2280
+ var expected: [256]u32 = undefined;
2281
+ for (0..expected.len) |i| {
2282
+ expected[i] = @intCast(i * 0x1000000);
2283
+ }
2284
+
2285
+ var spe: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
2286
+ var qkc: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
2287
+ var cfz: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
2288
+ var atr: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
2289
+ var frz: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
2290
+ var fcb: rng.FixedRNG(2, expected.len) = .{ .rolls = expected };
2291
+
2292
+ for (0..expected.len) |i| {
2293
+ try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
2294
+ try expectEqual(qkc.chance(u8, 60, 256), i < 60);
2295
+ try expectEqual(!cfz.chance(u8, 128, 256), i >= Gen12.percent(50) + 1);
2296
+ try expectEqual(atr.chance(u8, 1, 2), i < Gen12.percent(50) + 1);
2297
+ try expectEqual(frz.chance(u8, 25, 256), i < Gen12.percent(10));
2298
+ try expectEqual(fcb.chance(u8, 30, 256), !(30 <= i));
2299
+ }
2300
+ }
2301
+
2302
+ test "Roll probabilities" {
2303
+ if (showdown) return;
2304
+
2305
+ var present = [4]u8{ 0, 0, 0, 0 };
2306
+ var magnitude = [7]u8{ 0, 0, 0, 0, 0, 0, 0 };
2307
+ for (0..256) |i| {
2308
+ present[
2309
+ if (i <= Gen12.percent(40))
2310
+ 1
2311
+ else if (i <= Gen12.percent(70) + 1)
2312
+ 2
2313
+ else if (i <= Gen12.percent(80))
2314
+ 3
2315
+ else
2316
+ 0
2317
+ ] += 1;
2318
+ magnitude[
2319
+ if (i <= Gen12.percent(5) + 1)
2320
+ 0
2321
+ else if (i <= Gen12.percent(15) + 1)
2322
+ 1
2323
+ else if (i <= Gen12.percent(35))
2324
+ 2
2325
+ else if (i <= Gen12.percent(65) + 1)
2326
+ 3
2327
+ else if (i <= Gen12.percent(85) + 1)
2328
+ 4
2329
+ else if (i <= Gen12.percent(95))
2330
+ 5
2331
+ else
2332
+ 6
2333
+ ] += 1;
2334
+ }
2335
+
2336
+ try expectEqualSlices(u8, &.{ 51, 103, 77, 25 }, &present);
2337
+ try expectEqualSlices(u8, &.{ 14, 26, 50, 77, 51, 25, 13 }, &magnitude);
2338
+ }
2339
+
2340
+ test "Reasons" {
2341
+ const moves = [_]Move{ .Bind, .Wrap, .FireSpin, .Clamp, .Whirlpool };
2342
+ for (moves) |move| {
2343
+ const reason = Move.get(move).extra.protocol - 1;
2344
+
2345
+ const activate: Activate = @enumFromInt(@intFromEnum(Activate.Bind) + reason);
2346
+ try expectEqualStrings(@tagName(move), @tagName(activate));
2347
+
2348
+ const damage: Damage = @enumFromInt(@intFromEnum(Damage.Bind) + reason);
2349
+ try expectEqualStrings(@tagName(move), @tagName(damage));
2350
+
2351
+ const end: End = @enumFromInt(@intFromEnum(End.Bind) + reason);
2352
+ try expectEqualStrings(@tagName(move), @tagName(end));
2353
+ }
2354
+
2355
+ const perish_song = [_]Start{ .PerishSong0, .PerishSong1, .PerishSong2, .PerishSong3 };
2356
+ for (perish_song, 0..) |ps, i| {
2357
+ const reason: Start = @enumFromInt(@intFromEnum(Start.PerishSong0) + i);
2358
+ try expectEqualStrings(@tagName(ps), @tagName(reason));
2359
+ }
2360
+ }
2361
+
2362
+ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
2363
+ var n: u8 = 0;
2364
+ switch (request) {
2365
+ .Pass => {
2366
+ out[n] = .{};
2367
+ n += 1;
2368
+ },
2369
+ .Switch => {
2370
+ const side = battle.side(player);
2371
+ var slot: u4 = 2;
2372
+ while (slot <= 6) : (slot += 1) {
2373
+ const id = side.order[slot - 1];
2374
+ if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
2375
+ out[n] = .{ .type = .Switch, .data = slot };
2376
+ n += 1;
2377
+ }
2378
+ if (n == 0) {
2379
+ out[n] = .{};
2380
+ n += 1;
2381
+ }
2382
+ },
2383
+ .Move => {
2384
+ const side = battle.side(player);
2385
+
2386
+ var active = &side.active;
2387
+ const stored = side.stored();
2388
+
2389
+ // While players are not given any input options on the cartridge in these cases,
2390
+ // Pokémon Showdown instead produces a list with a single move that must be chosen.
2391
+ //
2392
+ // Given that no input is allowed on the cartridge 'Pass' seems like it would be logical
2393
+ // here when not in compatibility mode, but the engine needs to be able to differentiate
2394
+ // between passing while waiting for an opponent's forced switch after fainting and
2395
+ // passing due to being forced into using a move. Instead of introducing another option
2396
+ // we simply repurpose Move with no move slot, even though pedantically this is not
2397
+ // strictly correct as the player would not have been presented the option to move or
2398
+ // switch at all.
2399
+ if (isForced(active)) {
2400
+ out[n] = .{ .type = .Move, .data = @intFromBool(showdown) };
2401
+ n += 1;
2402
+ return n;
2403
+ }
2404
+
2405
+ var slot: u4 = 2;
2406
+ while (!active.volatiles.trapped and slot <= 6) : (slot += 1) {
2407
+ const id = side.order[slot - 1];
2408
+ if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
2409
+ out[n] = .{ .type = .Switch, .data = slot };
2410
+ n += 1;
2411
+ }
2412
+
2413
+ const limited = active.volatiles.Bide or active.volatiles.bind > 0;
2414
+ // On the cartridge, all of these happen after "FIGHT" (indicating you are not
2415
+ // switching) but before you are allowed to select a move. Pokémon Showdown instead
2416
+ // either disables all other moves in the case of limited or requires you to select a
2417
+ // move normally if sleeping/frozen/bound.
2418
+ if (!showdown and (limited or
2419
+ Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP)))
2420
+ {
2421
+ out[n] = .{ .type = .Move, .data = 0 };
2422
+ n += 1;
2423
+ return n;
2424
+ }
2425
+
2426
+ slot = 1;
2427
+ // Pokémon Showdown handles Bide and Binding moves by checking if the move in question
2428
+ // is present in the Pokémon's moveset (which means moves called via Metronome / Mirror
2429
+ // Move will not result in forcing the subsequent use unless the user also had the
2430
+ // proc-ed move in their moveset) and disabling all other moves.
2431
+ if (limited) {
2432
+ assert(showdown);
2433
+ assert(side.last_selected_move != .None);
2434
+ while (slot <= 4) : (slot += 1) {
2435
+ const m = active.moves[slot - 1];
2436
+ if (m.id == .None) break;
2437
+ if (m.id == if (active.volatiles.Bide) .Bide else side.last_selected_move) {
2438
+ // Pokémon Showdown displays Struggle if limited to Bide but unable to pick
2439
+ const struggle =
2440
+ m.id == .Bide and (m.pp == 0 or active.volatiles.disable.move == slot);
2441
+ const s = if (struggle) 0 else slot;
2442
+ out[n] = .{ .type = .Move, .data = s };
2443
+ n += 1;
2444
+ return n;
2445
+ }
2446
+ }
2447
+ }
2448
+
2449
+ const before = n;
2450
+ slot = 1;
2451
+ while (slot <= 4) : (slot += 1) {
2452
+ const m = active.moves[slot - 1];
2453
+ if (m.id == .None) break;
2454
+ if (m.pp == 0) continue;
2455
+ if (active.volatiles.disable.move == slot) continue;
2456
+ out[n] = .{ .type = .Move, .data = slot };
2457
+ n += 1;
2458
+ }
2459
+ // Struggle (Pokémon Showdown would use 'move 1' here)
2460
+ if (n == before) {
2461
+ out[n] = .{ .type = .Move, .data = 0 };
2462
+ n += 1;
2463
+ }
2464
+ },
2465
+ }
2466
+ return n;
53
2467
  }