@pkmn/engine 0.1.0-dev.b9fb6eab → 0.1.0-dev.c42deb42

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