@pkmn/engine 0.1.0-dev.942dbfa4 → 0.1.0-dev.954f650d

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.
@@ -1,8 +1,9 @@
1
1
  const std = @import("std");
2
2
 
3
+ const pkmn = @import("../pkmn.zig");
4
+
3
5
  const common = @import("../common/data.zig");
4
6
  const DEBUG = @import("../common/debug.zig").print;
5
- const options = @import("../common/options.zig");
6
7
  const protocol = @import("../common/protocol.zig");
7
8
  const rng = @import("../common/rng.zig");
8
9
 
@@ -17,7 +18,7 @@ const ID = common.ID;
17
18
  const Player = common.Player;
18
19
  const Result = common.Result;
19
20
 
20
- const showdown = options.showdown;
21
+ const showdown = pkmn.options.showdown;
21
22
 
22
23
  const Damage = protocol.Damage;
23
24
  const Heal = protocol.Heal;
@@ -33,21 +34,823 @@ const Side = data.Side;
33
34
  const Species = data.Species;
34
35
  const Stats = data.Stats;
35
36
  const Status = data.Status;
37
+ const Type = data.Type;
38
+ const Types = data.Types;
36
39
 
37
- pub fn update(battle: anytype, c1: Choice, c2: Choice, log: anytype) !Result {
38
- _ = battle;
39
- _ = c1;
40
- _ = c2;
41
- _ = log;
40
+ // zig fmt: off
41
+ const STAT_BOOSTS = &[_][2]u8{
42
+ .{ 25, 100 }, // -6
43
+ .{ 28, 100 }, // -5
44
+ .{ 33, 100 }, // -4
45
+ .{ 40, 100 }, // -3
46
+ .{ 50, 100 }, // -2
47
+ .{ 66, 100 }, // -1
48
+ .{ 1, 1 }, // 0
49
+ .{ 15, 10 }, // +1
50
+ .{ 2, 1 }, // +2
51
+ .{ 25, 10 }, // +3
52
+ .{ 3, 1 }, // +4
53
+ .{ 35, 10 }, // +5
54
+ .{ 4, 1 }, // +6
55
+ };
42
56
 
43
- return Result.Default;
57
+ const ACCURACY_BOOSTS = &[_][2]u8{
58
+ .{ 33, 100 }, // -6
59
+ .{ 36, 100 }, // -5
60
+ .{ 43, 100 }, // -4
61
+ .{ 50, 100 }, // -3
62
+ .{ 60, 100 }, // -2
63
+ .{ 75, 100 }, // -1
64
+ .{ 1, 1 }, // 0
65
+ .{ 133, 100 }, // +1
66
+ .{ 166, 100 }, // +2
67
+ .{ 2, 1 }, // +3
68
+ .{ 233, 100 }, // +4
69
+ .{ 133, 50 }, // +5
70
+ .{ 3, 1 }, // +6
71
+ };
72
+
73
+ const CRITICAL_HITS = &[_]u8{
74
+ 256 / 15, // 0
75
+ 256 / 8, // +1
76
+ 256 / 4, // +2
77
+ 256 / 3, // +3
78
+ 256 / 2, // +4
79
+ 256 / 2, // +5
80
+ 256 / 2, // +6
81
+ };
82
+ // zig fmt: on
83
+
84
+ const MAX_STAT_VALUE = 999;
85
+
86
+ const DISTRIBUTION = [_]u3{ 2, 2, 2, 3, 3, 3, 4, 5 };
87
+
88
+ pub fn update(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Result {
89
+ assert(c1.type != .Pass or c2.type != .Pass or battle.turn == 0);
90
+ if (battle.turn == 0) return start(battle, options);
91
+
92
+ if (try turnOrder(battle, c1, c2, options) == .P1) {
93
+ if (try doTurn(battle, .P1, c1, .P2, c2, options)) |r| return r;
94
+ } else {
95
+ if (try doTurn(battle, .P2, c2, .P1, c1, options)) |r| return r;
96
+ }
97
+
98
+ return endTurn(battle, options);
44
99
  }
45
100
 
46
- pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
101
+ fn start(battle: anytype, options: anytype) !Result {
102
+ const p1 = battle.side(.P1);
103
+ const p2 = battle.side(.P2);
104
+
105
+ var p1_slot = findFirstAlive(p1);
106
+ assert(!showdown or p1_slot == 1);
107
+ if (p1_slot == 0) return if (findFirstAlive(p2) == 0) Result.Tie else Result.Lose;
108
+
109
+ var p2_slot = findFirstAlive(p2);
110
+ assert(!showdown or p2_slot == 1);
111
+ if (p2_slot == 0) return Result.Win;
112
+
113
+ try switchIn(battle, .P1, p1_slot, true, options);
114
+ try switchIn(battle, .P2, p2_slot, true, options);
115
+
116
+ return endTurn(battle, options);
117
+ }
118
+
119
+ fn findFirstAlive(side: *const Side) u8 {
120
+ for (side.pokemon, 0..) |pokemon, i| if (pokemon.hp > 0) return side.pokemon[i].position;
121
+ return 0;
122
+ }
123
+
124
+ fn selectMove(battle: anytype, player: Player, choice: Choice) void {
125
+ if (choice.type == .Pass) return; // FIXME BATON PASS
126
+
127
+ var side = battle.side(player);
128
+ var volatiles = &side.active.volatiles;
129
+
130
+ // pre-battle menu
131
+ const forced = forced: {
132
+ if (volatiles.Recharging) break :forced true;
133
+ // Pokémon Showdown removes Flinch at the end-of-turn in its residual handler
134
+ if (!showdown) {
135
+ volatiles.Flinch = false;
136
+ battle.foe(player).active.volatiles.Flinch = false;
137
+ }
138
+ break :forced (volatiles.Thrashing or volatiles.Charging or volatiles.Rollout);
139
+ };
140
+
141
+ // battle menu
142
+ if (choice.type == .Switch) return;
143
+ assert(choice.type == .Move and (!forced or choice.data == @intFromBool(showdown)));
144
+
145
+ done: {
146
+ if (forced) break :done;
147
+ // pre-move select
148
+ if (volatiles.Encore) {
149
+ // TODO wCurPlayerMove = side.lastMove(true);
150
+ // assert(wCurPlayerMove != .None);
151
+ volatiles.Bide = false; // FIXME wPlayerCharging vs. SUBSTATUS_BIDE?
152
+ const effect = Move.get(.Pound).effect; // TODO
153
+ if (effect != .FuryCutter) volatiles.fury_cutter = false;
154
+ if (effect != .Rage) {
155
+ volatiles.Rage = false;
156
+ volatiles.rage = 0;
157
+ }
158
+ if (effect != .Protect and effect != .Endure) volatiles.protect = 0;
159
+ return;
160
+ }
161
+ if (volatiles.Bide) break :done;
162
+ // move select
163
+ if (choice.data == 0) {
164
+ const struggle = ok: {
165
+ for (side.active.moves, 0..) |move, i| {
166
+ if (move.pp > 0 and volatiles.disabled_move != i + 1) break :ok false;
167
+ }
168
+ break :ok true;
169
+ };
170
+ assert(struggle);
171
+ // TODO wCurPlayerMove = .Struggle;
172
+ } else {
173
+ assert(showdown or side.active.volatiles.disabled_move != choice.data);
174
+ const move = side.active.move(choice.data);
175
+ // You cannot *select* a move with 0 PP
176
+ assert(move.pp != 0);
177
+ // TODO wCurPlayerMove = move.id;
178
+ // battle.lastMove(player).index = @intCast(c.data);
179
+ }
180
+ }
181
+
182
+ volatiles.Rage = false;
183
+ volatiles.rage = 0;
184
+ volatiles.fury_cutter = 0;
185
+ volatiles.protect = 0;
186
+ }
187
+
188
+ fn switchIn(battle: anytype, player: Player, slot: u8, initial: bool, options: anytype) !void {
189
+ var side = battle.side(player);
190
+ var foe = battle.foe(player);
191
+
192
+ var active = &side.active;
193
+ const incoming = side.get(slot);
194
+
195
+ assert(incoming.hp != 0);
196
+ assert(slot != 1 or initial);
197
+
198
+ const out = side.pokemon[0].position;
199
+ side.pokemon[0].position = side.pokemon[slot - 1].position;
200
+ side.pokemon[slot - 1].position = out;
201
+
202
+ side.last_used_move = .None;
203
+ foe.active.volatiles.dirty = true;
204
+
205
+ active.volatiles = .{};
206
+ active.stats = incoming.stats;
207
+ active.moves = incoming.moves;
208
+ active.boosts = .{};
209
+ active.types = incoming.types;
210
+ active.species = incoming.species;
211
+
212
+ statusModify(incoming.status, &active.stats);
213
+
214
+ try options.log.switched(battle.active(player), incoming);
215
+ options.chance.switched(player, side.pokemon[0].position, out);
216
+
217
+ if (side.conditions.Spikes and !active.types.includes(.Flying)) {
218
+ incoming.hp -|= @max(incoming.stats.hp / 8, 1);
219
+ try options.log.damage(battle.active(player), incoming, .Spikes);
220
+ }
221
+ }
222
+
223
+ fn turnOrder(battle: anytype, c1: Choice, c2: Choice, options: anytype) !Player {
224
+ assert(c1.type != .Pass or c2.type != .Pass);
225
+
226
+ if (c1.type == .Pass) return .P2;
227
+ if (c2.type == .Pass) return .P1;
228
+
229
+ // Pokémon Showdown always rolls for Quick Claw every turn, even if no Pokémon hold Quick Claw
230
+ // TODO: add to pending and only commit roll on Pokémon Showdown if roll is used
231
+ const qkc = showdown and try Rolls.quickClaw(battle, .P1, options);
232
+
233
+ if ((c1.type == .Switch) != (c2.type == .Switch)) return if (c1.type == .Switch) .P1 else .P2;
234
+
235
+ // https://www.smogon.com/forums/threads/adv-switch-priority.3622189/
236
+ // > In Gen 1 it's irrelevant [which player switches first] because switches happen instantly on
237
+ // > your own screen without waiting for the other player's choice (and their choice will appear
238
+ // > to happen first for them too, unless they attacked in which case your switch happens first)
239
+ // A cartridge-compatible implemention must not advance the RNG so we simply default to P1
240
+ const double_switch = c1.type == .Switch and c2.type == .Switch;
241
+ if (!showdown and double_switch) return .P1;
242
+
243
+ const m1 = .Pound; // TODO battle.side(.P1).last_selected_move;
244
+ const m2 = .Pound; // TODO battle.side(.P2).last_selected_move;
245
+ if (!showdown or !double_switch) {
246
+ const pri1 = Move.get(m1).priority;
247
+ const pri2 = Move.get(m2).priority;
248
+ if (pri1 != pri2) return if (pri1 > pri2) .P1 else .P2;
249
+
250
+ const qkc1 = battle.side(.P1).stored().item == .QuickClaw;
251
+ const qkc2 = battle.side(.P2).stored().item == .QuickClaw;
252
+ if (!showdown and qkc1 and qkc2) {
253
+ // If both Pokémon have Quick Claw the cartridge checks them in order and exits early if
254
+ // the first one procs, meaning the host has a slightly higher chance of going first
255
+ if (try Rolls.quickClaw(battle, .P1, options)) return .P1;
256
+ if (try Rolls.quickClaw(battle, .P2, options)) return .P2;
257
+ } else if (qkc1) {
258
+ const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P1, options);
259
+ if (proc) return .P1;
260
+ } else if (qkc2) {
261
+ const proc = if (showdown) qkc else try Rolls.quickClaw(battle, .P2, options);
262
+ if (proc) return .P2;
263
+ }
264
+ }
265
+
266
+ const spe1 = battle.side(.P1).active.stats.spe;
267
+ const spe2 = battle.side(.P2).active.stats.spe;
268
+ if (spe1 == spe2) {
269
+ // Pokémon Showdown's beforeTurnCallback shenanigans
270
+ if (showdown and !double_switch and hasCallback(m1) and hasCallback(m2)) {
271
+ battle.rng.advance(1);
272
+ }
273
+
274
+ const p1 = try Rolls.speedTie(battle, options);
275
+ if (!showdown) return if (p1) .P1 else .P2;
276
+
277
+ // Pokémon Showdown's "lockedmove" volatile's onBeforeTurn uses BattleQueue#changeAction,
278
+ // meaning that if a side is locked into a thrashing move and wins the speed tie, it
279
+ // actually uses its priority to simply insert its actual changed action into the queue,
280
+ // causing it to then execute *after* the side which should go second...
281
+ const t1 = battle.side(.P1).active.volatiles.Thrashing;
282
+ const t2 = battle.side(.P2).active.volatiles.Thrashing;
283
+ // If *both* sides are thrashing it really should be another speed tie, but we've patched
284
+ // that out and enforce host ordering of events, so P1 just goes first regardless of who
285
+ // won the original coin flip
286
+ if (t1 and t2) return .P1;
287
+ return if (p1) if (t1 and !t2) .P2 else .P1 else if (t2 and !t1) .P1 else .P2;
288
+ }
289
+
290
+ return if (spe1 > spe2) .P1 else .P2;
291
+ }
292
+
293
+ fn hasCallback(m: Move) bool {
294
+ return switch (m) {
295
+ .Counter, .MirrorCoat, .Pursuit => true,
296
+ else => false,
297
+ };
298
+ }
299
+
300
+ fn doTurn(
301
+ battle: anytype,
302
+ player: Player,
303
+ player_choice: Choice,
304
+ foe_player: Player,
305
+ foe_choice: Choice,
306
+ options: anytype,
307
+ ) !?Result {
308
+ assert(player_choice.type != .Pass);
309
+
310
+ const volatiles = &battle.side(player).active.volatiles;
311
+ const foe_volatiles = &battle.foe(player).active.volatiles;
312
+
313
+ volatiles.DestinyBond = false;
314
+ if (try executeMove(battle, player, player_choice, options)) |r| return r;
315
+ foe_volatiles.Protect = false;
316
+ foe_volatiles.Endure = false;
317
+ foe_volatiles.DestinyBond = false;
318
+
319
+ // foe_volatiles.DestinyBond = false;
320
+ if (try executeMove(battle, foe_player, foe_choice, options)) |r| return r;
321
+ volatiles.Protect = false;
322
+ volatiles.Endure = false;
323
+ volatiles.DestinyBond = false;
324
+
325
+ return null;
326
+ }
327
+
328
+ fn executeMove(battle: anytype, player: Player, choice: Choice, options: anytype) !?Result {
329
+ if (choice.type == .Switch) {
330
+ try switchIn(battle, player, choice.data, false, options);
331
+ return null;
332
+ }
333
+
334
+ assert(choice.type == .Move);
335
+ var mslot: u4 = @intCast(choice.data);
336
+
337
+ // const move = Move.get(side.last_selected_move);
338
+
339
+ // CheckTurn
340
+
341
+ return doMove(battle, player, options, mslot);
342
+ }
343
+
344
+ fn beforeMove(battle: anytype, player: Player, options: anytype) !void {
345
+ _ = battle;
346
+ _ = player;
347
+ _ = options;
348
+ }
349
+
350
+ fn canMove(battle: anytype, player: Player, options: anytype) !bool {
47
351
  _ = battle;
48
352
  _ = player;
49
- _ = request;
50
- _ = out;
353
+ _ = options;
354
+ return false;
355
+ }
356
+
357
+ fn decrementPP(side: *Side, mslot: u4) void {
358
+ var active = &side.active;
359
+ const volatiles = &active.volatiles;
360
+
361
+ // TODO assert(side.last_selected_move != .Struggle);
362
+ assert(!volatiles.Charging and !volatiles.BeatUp and !volatiles.Thrashing and !volatiles.Bide);
363
+
364
+ assert(active.move(mslot).pp > 0);
365
+ active.move(mslot).pp = @as(u6, @intCast(active.move(mslot).pp)) -% 1;
366
+ if (volatiles.Transform) return;
367
+
368
+ // FIXME mimic
51
369
 
370
+ assert(side.stored().move(mslot).pp > 0);
371
+ side.stored().move(mslot).pp = @as(u6, @intCast(side.stored().move(mslot).pp)) -% 1;
372
+ assert(active.move(mslot).pp == side.stored().move(mslot).pp);
373
+ }
374
+
375
+ fn doMove(battle: anytype, player: Player, options: anytype, mslot: u4) !?Result {
376
+ _ = options;
377
+ var side = battle.side(player);
378
+ const move = Move.get(.Teleport); // side.last_selected_move
379
+ switch (move.effect) {
380
+ .Teleport => {
381
+ // side.useMove();
382
+ decrementPP(side, mslot);
383
+ },
384
+ else => @panic("TODO"),
385
+ }
386
+ return null;
387
+ }
388
+
389
+ fn checkCriticalHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
390
+ if (move.bp == 0) return false;
391
+
392
+ const side = battle.side(player);
393
+ const pokemon = side.stored();
394
+
395
+ var stage: u8 =
396
+ if ((pokemon.species == .Chansey and pokemon.item == .LuckyPunch) or
397
+ (pokemon.species == .Farfetchd and pokemon.item == .Stick)) 2 else 0;
398
+ if (side.active.volatiles.FocusEnergy) stage += 1;
399
+ if (move.effect.isHighCritical()) stage += 2;
400
+ if (pokemon.item == .ScopeLens) stage += 1;
401
+ assert(stage <= 5);
402
+
403
+ return Rolls.criticalHit(battle, player, CRITICAL_HITS[stage], options);
404
+ }
405
+
406
+ fn calcDamage(
407
+ battle: anytype,
408
+ player: Player,
409
+ target_player: Player,
410
+ m: ?Move.Data,
411
+ crit: bool,
412
+ ) u16 {
413
+ _ = battle;
414
+ _ = player;
415
+ _ = target_player;
416
+ _ = m;
417
+ _ = crit;
52
418
  return 0;
53
419
  }
420
+ const FORESIGHT: Types = .{ .type1 = .Normal, .type2 = .Fighting };
421
+
422
+ fn adjustDamage(battle: anytype, player: Player, m: Move, damage: u16, effectiveness: *u16) u16 {
423
+ if (m == .Struggle) return;
424
+
425
+ const side = battle.side(player);
426
+ const foe = battle.foe(player);
427
+ const types = foe.active.types;
428
+ const move = Move.get(side.m);
429
+
430
+ var d = damage;
431
+ if ((move.type == .Water and battle.field.weather == .Rain) or
432
+ (move.type == .Fire and battle.field.weather == .Sun))
433
+ {
434
+ d = d *% @intFromEnum(Effectiveness.Super) / 10;
435
+ } else if ((move.type == .Water and battle.field.weather == .Sun) or
436
+ ((move.type == .Fire or m == .SolarBeam) and battle.field.weather == .Rain))
437
+ {
438
+ d = d *% @intFromEnum(Effectiveness.Resisted) / 10;
439
+ }
440
+
441
+ if (side.active.types.includes(move.type)) d +%= d / 2;
442
+
443
+ const neutral = @intFromEnum(Effectiveness.Neutral);
444
+ const foresight = foe.active.volatiles.Foresight and FORESIGHT.includes(move.type);
445
+ const eff1: u16 = if (foresight and types.type1 == .Ghost)
446
+ 10
447
+ else
448
+ @intFromEnum(move.type.effectiveness(types.type1));
449
+ const eff2: u16 = if (foresight and types.type2 == .Ghost)
450
+ 10
451
+ else
452
+ @intFromEnum(move.type.effectiveness(types.type2));
453
+
454
+ // Type effectiveness matchup precedence only matters with (NVE, SE)
455
+ if (!showdown and (eff1 + eff2) == Effectiveness.mismatch and
456
+ Type.precedence(move.type, types.type1) > Type.precedence(move.type, types.type2))
457
+ {
458
+ assert(eff2 != neutral);
459
+ d = d *% eff2 / 10;
460
+ assert(types.type1 != types.type2);
461
+ assert(eff1 != neutral);
462
+ d = d *% eff1 / 10;
463
+ } else {
464
+ if (eff1 != neutral) d = d *% eff1 / 10;
465
+ if (types.type1 != types.type2 and eff2 != neutral) d = d *% eff2 / 10;
466
+ }
467
+
468
+ effectiveness.* = if (types.type1 == types.type2) eff1 * neutral else eff1 * eff2;
469
+ return d;
470
+ }
471
+
472
+ fn randomizeDamage(battle: anytype, player: Player, damage: u16, options: anytype) !u16 {
473
+ if (damage <= 1) return;
474
+ const random = try Rolls.damage(battle, player, options);
475
+ options.calc.base(player.foe(), damage);
476
+ damage = @intCast(@as(u32, damage) *% random / 255);
477
+ options.calc.final(player.foe(), damage);
478
+ return damage;
479
+ }
480
+
481
+ fn applyDamage(
482
+ battle: anytype,
483
+ player: Player,
484
+ options: anytype,
485
+ ) !bool {
486
+ _ = battle;
487
+ _ = player;
488
+ _ = options;
489
+ return false;
490
+ }
491
+
492
+ fn checkHit(battle: anytype, player: Player, move: Move.Data, options: anytype) !bool {
493
+ if (moveHit(battle, player, move, options)) return true;
494
+
495
+ try options.chance.commit(player, .miss);
496
+ try options.log.lastmiss();
497
+ try options.log.miss(battle.active(player));
498
+
499
+ return false;
500
+ }
501
+
502
+ fn moveHit(
503
+ battle: anytype,
504
+ player: Player,
505
+ move: Move.Data,
506
+ options: anytype,
507
+ ) bool {
508
+ var side = battle.side(player);
509
+ const foe = battle.foe(player);
510
+ _ = move;
511
+ _ = side;
512
+ _ = foe;
513
+ _ = options;
514
+ return false;
515
+ }
516
+
517
+ fn checkFaint(
518
+ battle: anytype,
519
+ player: Player,
520
+ options: anytype,
521
+ ) @TypeOf(options.log).Error!?Result {
522
+ _ = battle;
523
+ _ = player;
524
+ return null;
525
+ }
526
+
527
+ fn faint(battle: anytype, player: Player, done: bool, options: anytype) !?Result {
528
+ _ = battle;
529
+ _ = player;
530
+ _ = done;
531
+ _ = options;
532
+ return null;
533
+ }
534
+
535
+ fn handleResidual(battle: anytype, player: Player, options: anytype) !void {
536
+ var side = battle.side(player);
537
+ var stored = side.stored();
538
+ const ident = battle.active(player);
539
+ var volatiles = &side.active.volatiles;
540
+
541
+ // TODO if (stored.hp == 0) return;
542
+ assert(stored.hp > 0);
543
+
544
+ const brn = Status.is(stored.status, .BRN);
545
+ if (brn or Status.is(stored.status, .PSN)) {
546
+ var damage: u16 = undefined;
547
+ if (volatiles.Toxic) {
548
+ volatiles.toxic += 1;
549
+ damage = @max(stored.stats.hp / 16, 1) * volatiles.toxic;
550
+ } else {
551
+ damage = @max(stored.stats.hp / 8, 1);
552
+ }
553
+ damage = @min(damage, stored.hp);
554
+
555
+ if (!showdown or damage > 0) {
556
+ stored.hp -= damage;
557
+ // Pokémon Showdown uses damageOf here but its not relevant in Generation II
558
+ try options.log.damage(ident, stored, if (brn) Damage.Burn else Damage.Poison);
559
+ if (stored.hp == 0) return;
560
+ }
561
+ }
562
+
563
+ if (volatiles.LeechSeed) {
564
+ var foe = battle.foe(player);
565
+ var foe_stored = foe.stored();
566
+ const foe_ident = battle.active(player.foe());
567
+
568
+ // if (foe_stored.hp == 0) {
569
+ // assert(showdown);
570
+ // return;
571
+ // }
572
+
573
+ const damage = @min(@max(stored.stats.hp / 8, 1), stored.hp);
574
+ stored.hp -= damage;
575
+ // As above, Pokémon Showdown uses damageOf but its not relevant
576
+ if (damage > 0) try options.log.damage(ident, stored, .LeechSeed);
577
+
578
+ const before = foe_stored.hp;
579
+ foe_stored.hp = @min(foe_stored.hp + damage, foe_stored.stats.hp);
580
+ // Pokémon Showdown uses the less specific heal here instead of drain... because reasons?
581
+ if (foe_stored.hp > before) try options.log.heal(foe_ident, foe_stored, .Silent);
582
+ if (stored.hp == 0) return;
583
+ }
584
+
585
+ if (volatiles.Nightmare) {
586
+ const damage = @min(@max(stored.stats.hp / 4, 1), stored.hp);
587
+ if (!showdown or damage > 0) {
588
+ stored.hp -= damage;
589
+ // ibid
590
+ try options.log.damage(ident, stored, Damage.Nightmare);
591
+ if (stored.hp == 0) return;
592
+ }
593
+ }
594
+
595
+ if (volatiles.Curse) {
596
+ const damage = @min(@max(stored.stats.hp / 4, 1), stored.hp);
597
+ if (!showdown or damage > 0) {
598
+ stored.hp -= damage;
599
+ // ibid
600
+ try options.log.damage(ident, stored, Damage.Curse);
601
+ if (stored.hp == 0) return;
602
+ }
603
+ }
604
+ }
605
+
606
+ fn betweenTurn(battle: anytype, options: anytype) !void {
607
+ var p1 = battle.side(.P1);
608
+ var p2 = battle.side(.P2);
609
+
610
+ _ = p1;
611
+ _ = p2;
612
+ _ = options;
613
+
614
+ // TODO checkFaintThen(p1, p2);
615
+
616
+ }
617
+
618
+ fn endTurn(battle: anytype, options: anytype) @TypeOf(options.log).Error!Result {
619
+ battle.turn += 1;
620
+
621
+ if (showdown and battle.turn >= 1000) {
622
+ try options.log.tie();
623
+ return Result.Tie;
624
+ } else if (battle.turn >= 65535) {
625
+ return Result.Error;
626
+ }
627
+
628
+ try options.log.turn(battle.turn);
629
+
630
+ return Result.Default;
631
+ }
632
+
633
+ pub const Effects = struct {
634
+ fn protect(battle: anytype, player: Player, move: Move.Data, options: anytype) !void {
635
+ var side = battle.side(player);
636
+ var volatiles = &side.active.volatiles;
637
+
638
+ // TODO or if we didn't go first
639
+ if (volatiles.Substitute) return try options.log.fail(battle.active(player), .None);
640
+
641
+ // TODO
642
+
643
+ if (move.effect == .Endure) {
644
+ volatiles.Endure = true;
645
+ } else {
646
+ volatiles.Protect = true;
647
+ }
648
+ try options.log.singleturn(battle.active(player), move.id);
649
+ }
650
+ };
651
+
652
+ fn statusModify(status: u8, stats: *Stats(u16)) void {
653
+ if (Status.is(status, .PAR)) {
654
+ stats.spe = @max(stats.spe / 4, 1);
655
+ } else if (Status.is(status, .BRN)) {
656
+ stats.atk = @max(stats.atk / 2, 1);
657
+ }
658
+ }
659
+
660
+ inline fn isForced(active: anytype) bool {
661
+ return active.volatiles.Recharging or active.volatiles.Rage or
662
+ active.volatiles.Thrashing or active.volatiles.Charging or
663
+ // FIXME TODO Underground or Flying?
664
+ active.volatiles.Underground or active.volatiles.Flying or
665
+ active.volatiles.Rollout;
666
+ }
667
+
668
+ pub const Rolls = struct {
669
+ inline fn speedTie(battle: anytype, options: anytype) !bool {
670
+ const p1 = if (options.calc.overridden(.P1, .speed_tie)) |player|
671
+ player == .P1
672
+ else if (showdown)
673
+ battle.rng.range(u8, 0, 2) == 0
674
+ else
675
+ battle.rng.next() < Gen12.percent(50) + 1;
676
+
677
+ try options.chance.speedTie(p1);
678
+ return p1;
679
+ }
680
+
681
+ inline fn quickClaw(battle: anytype, player: Player, options: anytype) !bool {
682
+ const qkc = if (options.calc.overridden(player, .quick_claw)) |val|
683
+ val == .true
684
+ else if (showdown)
685
+ battle.rng.chance(u8, 60, 256)
686
+ else
687
+ battle.rng.next() < 60;
688
+
689
+ try options.chance.quickClaw(player, qkc);
690
+ return qkc;
691
+ }
692
+
693
+ inline fn criticalHit(battle: anytype, player: Player, rate: u8, options: anytype) !bool {
694
+ const crit = if (options.calc.overridden(player, .critical_hit)) |val|
695
+ val == .true
696
+ else if (showdown)
697
+ battle.rng.chance(u8, rate, 256)
698
+ else
699
+ battle.rng.next() < rate;
700
+
701
+ try options.chance.criticalHit(player, crit, rate);
702
+ return crit;
703
+ }
704
+
705
+ inline fn damage(battle: anytype, player: anytype, options: anytype) !u8 {
706
+ const roll = if (options.calc.overridden(player, .damage)) |val|
707
+ val
708
+ else roll: {
709
+ if (showdown) break :roll battle.rng.range(u8, 217, 256);
710
+ while (true) {
711
+ const r = std.math.rotr(u8, battle.rng.next(), 1);
712
+ if (r >= 217) break :roll r;
713
+ }
714
+ };
715
+
716
+ assert(roll >= 217 and roll <= 255);
717
+ try options.chance.damage(player, roll);
718
+ return roll;
719
+ }
720
+
721
+ const METRONOME = init: {
722
+ var num = 0;
723
+ var moves: [238]Move = undefined;
724
+ for (1..Move.size) |i| {
725
+ if (Move.get(@enumFromInt(i)).flags.metronome) {
726
+ moves[num] = @enumFromInt(i);
727
+ num += 1;
728
+ }
729
+ }
730
+ assert(num == moves.len);
731
+ break :init moves;
732
+ };
733
+ };
734
+
735
+ test "RNG agreement" {
736
+ if (!showdown) return;
737
+ var expected: [256]u32 = undefined;
738
+ for (0..expected.len) |i| {
739
+ expected[i] = @intCast(i * 0x1000000);
740
+ }
741
+
742
+ var spe = rng.FixedRNG(2, expected.len){ .rolls = expected };
743
+ var qkc = rng.FixedRNG(2, expected.len){ .rolls = expected };
744
+
745
+ for (0..expected.len) |i| {
746
+ try expectEqual(spe.range(u8, 0, 2) == 0, i < Gen12.percent(50) + 1);
747
+ try expectEqual(qkc.chance(u8, 60, 256), i < 60);
748
+ }
749
+ }
750
+
751
+ pub fn choices(battle: anytype, player: Player, request: Choice.Type, out: []Choice) u8 {
752
+ var n: u8 = 0;
753
+ switch (request) {
754
+ .Pass => {
755
+ out[n] = .{};
756
+ n += 1;
757
+ },
758
+ .Switch => {
759
+ const side = battle.side(player);
760
+ var slot: u4 = 2;
761
+ while (slot <= 6) : (slot += 1) {
762
+ const id = side.order[slot - 1];
763
+ if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
764
+ out[n] = .{ .type = .Switch, .data = slot };
765
+ n += 1;
766
+ }
767
+ if (n == 0) {
768
+ out[n] = .{};
769
+ n += 1;
770
+ }
771
+ },
772
+ .Move => {
773
+ const side = battle.side(player);
774
+
775
+ var active = &side.active;
776
+ const stored = side.stored();
777
+
778
+ // While players are not given any input options on the cartridge in these cases,
779
+ // Pokémon Showdown instead produces a list with a single move that must be chosen.
780
+ //
781
+ // Given that no input is allowed on the cartridge 'Pass' seems like it would be logical
782
+ // here when not in compatibility mode, but the engine needs to be able to differentiate
783
+ // between passing while waiting for an opponent's forced switch after fainting and
784
+ // passing due to being forced into using a move. Instead of introducing another option
785
+ // we simply repurpose Move with no move slot, even though pedantically this is not
786
+ // strictly correct as the player would not have been presented the option to move or
787
+ // switch at all.
788
+ if (isForced(active)) {
789
+ out[n] = .{ .type = .Move, .data = @intFromBool(showdown) };
790
+ n += 1;
791
+ return n;
792
+ }
793
+
794
+ var slot: u4 = 2;
795
+ while (!active.volatiles.trapped and slot <= 6) : (slot += 1) {
796
+ const id = side.order[slot - 1];
797
+ if (id == 0 or side.pokemon[id - 1].hp == 0) continue;
798
+ out[n] = .{ .type = .Switch, .data = slot };
799
+ n += 1;
800
+ }
801
+
802
+ const limited = active.volatiles.Bide or active.volatiles.bind > 0;
803
+ // On the cartridge, all of these happen after "FIGHT" (indicating you are not
804
+ // switching) but before you are allowed to select a move. Pokémon Showdown instead
805
+ // either disables all other moves in the case of limited or requires you to select a
806
+ // move normally if sleeping/frozen/bound.
807
+ if (!showdown and (limited or
808
+ Status.is(stored.status, .FRZ) or Status.is(stored.status, .SLP)))
809
+ {
810
+ out[n] = .{ .type = .Move, .data = 0 };
811
+ n += 1;
812
+ return n;
813
+ }
814
+
815
+ slot = 1;
816
+ // Pokémon Showdown handles Bide and Binding moves by checking if the move in question
817
+ // is present in the Pokémon's moveset (which means moves called via Metronome / Mirror
818
+ // Move will not result in forcing the subsequent use unless the user also had the
819
+ // proc-ed move in their moveset) and disabling all other moves.
820
+ if (limited) {
821
+ assert(showdown);
822
+ assert(side.last_selected_move != .None);
823
+ while (slot <= 4) : (slot += 1) {
824
+ const m = active.moves[slot - 1];
825
+ if (m.id == .None) break;
826
+ if (m.id == if (active.volatiles.Bide) .Bide else side.last_selected_move) {
827
+ // Pokémon Showdown displays Struggle if limited to Bide but unable to pick
828
+ const struggle =
829
+ m.id == .Bide and (m.pp == 0 or active.volatiles.disabled_move == slot);
830
+ const s = if (struggle) 0 else slot;
831
+ out[n] = .{ .type = .Move, .data = s };
832
+ n += 1;
833
+ return n;
834
+ }
835
+ }
836
+ }
837
+
838
+ const before = n;
839
+ slot = 1;
840
+ while (slot <= 4) : (slot += 1) {
841
+ const m = active.moves[slot - 1];
842
+ if (m.id == .None) break;
843
+ if (m.pp == 0) continue;
844
+ if (active.volatiles.disabled_move == slot) continue;
845
+ out[n] = .{ .type = .Move, .data = slot };
846
+ n += 1;
847
+ }
848
+ // Struggle (Pokémon Showdown would use 'move 1' here)
849
+ if (n == before) {
850
+ out[n] = .{ .type = .Move, .data = 0 };
851
+ n += 1;
852
+ }
853
+ },
854
+ }
855
+ return n;
856
+ }