@pkmn/engine 0.1.0-dev.dd5b9081 → 0.1.0-dev.dfd46bbb

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