@pkmn/engine 0.1.0-dev.47feedd9 → 0.1.0-dev.4c173f03

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