@pkmn/engine 0.1.0-dev.62399ed5 → 0.1.0-dev.6be9a775

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.
@@ -2,27 +2,20 @@ const std = @import("std");
2
2
  const builtin = @import("builtin");
3
3
 
4
4
  const common = @import("../common/data.zig");
5
- const optional = @import("../common/optional.zig");
6
5
  const options = @import("../common/options.zig");
7
6
  const protocol = @import("../common/protocol.zig");
8
- const rational = @import("../common/rational.zig");
9
7
  const rng = @import("../common/rng.zig");
8
+ const util = @import("../common/util.zig");
10
9
 
11
- const calc = @import("calc.zig");
12
- const chance = @import("chance.zig");
13
10
  const data = @import("data.zig");
14
11
 
15
12
  const assert = std.debug.assert;
16
13
 
17
- const expectEqual = std.testing.expectEqual;
18
- const expectEqualSlices = std.testing.expectEqualSlices;
19
-
20
14
  const Choice = common.Choice;
21
15
  const Player = common.Player;
22
16
 
23
- const Optional = optional.Optional;
24
-
25
17
  const showdown = options.showdown;
18
+ const chance = options.chance;
26
19
 
27
20
  const PSRNG = rng.PSRNG;
28
21
 
@@ -33,11 +26,10 @@ const Species = data.Species;
33
26
  const Stats = data.Stats;
34
27
  const Status = data.Status;
35
28
 
36
- const Action = chance.Action;
37
- const Actions = chance.Actions;
38
-
39
29
  const endian = builtin.cpu.arch.endian();
40
30
 
31
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
32
+
41
33
  /// Configuration options controlling the domain of valid Pokémon that can be generated by the
42
34
  /// random generator helpers.
43
35
  pub const Options = struct {
@@ -180,7 +172,7 @@ pub const Pokemon = struct {
180
172
  var pokemon = data.Pokemon{};
181
173
  pokemon.species = p.species;
182
174
  const species = Species.get(p.species);
183
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
175
+ inline for (@field(@typeInfo(@TypeOf(pokemon.stats)), @tagName(Struct)).fields) |field| {
184
176
  const hp = comptime std.mem.eql(u8, field.name, "hp");
185
177
  const spc =
186
178
  comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
@@ -216,7 +208,7 @@ pub const Pokemon = struct {
216
208
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
217
209
  var stats: Stats(u16) = .{};
218
210
  const dvs = DVs.random(rand);
219
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
211
+ inline for (@field(@typeInfo(@TypeOf(stats)), @tagName(Struct)).fields) |field| {
220
212
  @field(stats, field.name) = Stats(u16).calc(
221
213
  field.name,
222
214
  @field(species.stats, field.name),
@@ -257,6 +249,7 @@ pub const Pokemon = struct {
257
249
  .stats = stats,
258
250
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
259
251
  .status = if (!opt.cleric and rand.chance(u8, 1, 6 + 1))
252
+ // NB: This will only assign sleep as 2 or 4 when rolled
260
253
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
261
254
  else
262
255
  0,
@@ -300,524 +293,9 @@ pub fn swtch(slot: u4) Choice {
300
293
  return .{ .type = .Switch, .data = slot };
301
294
  }
302
295
 
303
- const NONE = @intFromEnum(Optional(bool).None);
304
-
305
- /// Helper functions that efficiently return valid ranges for various RNG events based on the
306
- /// state of an `Action` and other events to be used to construct a "transitions" function.
307
- pub const Rolls = struct {
308
- const PLAYER_NONE = [_]Optional(Player){.None};
309
- const PLAYERS = [_]Optional(Player){ .P1, .P2 };
310
-
311
- /// Returns a slice with the correct range of values for speed ties given the `action` state.
312
- pub fn speedTie(action: Action) []const Optional(Player) {
313
- return if (action.speed_tie == .None) &PLAYER_NONE else &PLAYERS;
314
- }
315
-
316
- const BOOL_NONE = [_]Optional(bool){.None};
317
- const BOOLS = [_]Optional(bool){ .false, .true };
318
-
319
- /// Returns a slice with the correct range of values for hits given the `action` state
320
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
321
- pub fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
322
- if (parent == .true) return &BOOL_NONE;
323
- return if (action.hit == .None) &BOOL_NONE else &BOOLS;
324
- }
325
-
326
- /// Returns a slice with the correct range of values for critical hits given the `action` state
327
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
328
- pub fn criticalHit(action: Action, parent: Optional(bool)) []const Optional(bool) {
329
- if (parent == .false) return &BOOL_NONE;
330
- return if (action.critical_hit == .None) &BOOL_NONE else &BOOLS;
331
- }
332
-
333
- /// Returns a slice with the correct range of values for secondary chances hits given the
334
- /// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
335
- pub fn secondaryChance(action: Action, parent: Optional(bool)) []const Optional(bool) {
336
- if (parent == .false) return &BOOL_NONE;
337
- return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
338
- }
339
-
340
- /// The min and max bounds on iteration over damage rolls.
341
- pub const Range = struct { min: u9, max: u9 };
342
-
343
- /// Returns the range bounding damage rolls given the `action` state and the state of
344
- /// the `parent` (whether the player's Pokémon's move hit).
345
- pub fn damage(action: Action, parent: Optional(bool)) Range {
346
- return if (parent == .false or action.damage == 0)
347
- .{ .min = 0, .max = 1 }
348
- else
349
- .{ .min = 217, .max = 256 };
350
- }
351
-
352
- /// Returns the max damage roll which will produce the same damage as `roll`
353
- /// given the base damage in `summaries`.
354
- pub fn coalesce(player: Player, roll: u8, summaries: *calc.Summaries, cap: bool) !u8 {
355
- if (roll == 0) return roll;
356
-
357
- const dmg = summaries.get(player.foe()).damage;
358
- if (dmg.base == 0 or (cap and dmg.capped)) return 255;
359
-
360
- // Closed form solution for max damage roll provided by Orion Taylor (taylorott)
361
- return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
362
- }
363
-
364
- /// Returns a slice with the correct range of values for confused given the `action` state
365
- /// and the state of the `parent` (the player's Pokémon's remaining confusion turns).
366
- pub fn confused(action: Action, parent: u4) []const Optional(bool) {
367
- return if (parent == 0 or action.confused == .None) &BOOL_NONE else &BOOLS;
368
- }
369
-
370
- /// Returns a slice with the correct range of values for paralysis given the `action` state
371
- /// and the state of the `parent` (whether the player's Pokémon was confused).
372
- pub fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
373
- if (parent == .true) return &BOOL_NONE;
374
- return if (action.paralyzed == .None) &BOOL_NONE else &BOOLS;
375
- }
376
-
377
- const DURATION_NONE = [_]u1{0};
378
- const DURATION = [_]u1{1};
379
-
380
- /// Returns a slice with a range of values for duration given the `action` state
381
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
382
- pub fn duration(action: Action, parent: Optional(bool)) []const u1 {
383
- if (parent == .false) return &DURATION_NONE;
384
- return if (action.duration == 0) &DURATION_NONE else &DURATION;
385
- }
386
-
387
- const U3_NONE = [_]u3{0};
388
- const U3_FORCED = [_]u3{1};
389
-
390
- /// Returns a slice with a range of values for sleep given the `action` and `origin` states.
391
- pub inline fn sleep(action: Action, origin: Action) []const u3 {
392
- const a = action.durations.sleep;
393
- const o = origin.durations.sleep;
394
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
395
- assert(a == 0 or a == o + 1);
396
- return if (o >= 7) &U3_NONE else &[_]u3{ 0, o + 1 };
397
- }
398
-
399
- const DISABLE_NONE = [_]u4{0};
400
-
401
- /// Returns a slice with a range of values for disable given the `action` and `origin` states
402
- /// and the state of the `parent` (the player's Pokémon's remaining sleep turns).
403
- pub inline fn disable(action: Action, origin: Action, parent: u4) []const u4 {
404
- if (parent > 0) return &DISABLE_NONE;
405
- const a = action.durations.disable;
406
- const o = origin.durations.disable;
407
- if (o == a) return if (o == 0) &DISABLE_NONE else &[_]u4{o};
408
- assert(a == 0 or a == o + 1);
409
- return if (o >= 8) &DISABLE_NONE else &[_]u4{ 0, o + 1 };
410
- }
411
-
412
- /// Returns a slice with a range of values for confusion given the `action` and `origin` states
413
- /// and the state of the `parent` (the player's remaining sleep turns).
414
- pub inline fn confusion(action: Action, origin: Action, parent: u4) []const u3 {
415
- if (parent > 0) return &U3_NONE;
416
- const a = action.durations.confusion;
417
- const o = origin.durations.confusion;
418
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
419
- assert(a == 0 or a == o + 1);
420
- return if (o >= 5) &U3_NONE else if (o < 2)
421
- if (a == 0) &U3_NONE else &U3_FORCED
422
- else
423
- &[_]u3{ 0, o + 1 };
424
- }
425
-
426
- /// Returns a slice with a range of values for attacking given the `action` and `origin` states
427
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
428
- pub inline fn attacking(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
429
- if (parent == .true) return &U3_NONE;
430
- const a = action.durations.attacking;
431
- const o = origin.durations.attacking;
432
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
433
- assert(a == 0 or a == o + 1);
434
- return if (o >= 3) &U3_NONE else if (o < 2)
435
- if (a == 0) &U3_NONE else &U3_FORCED
436
- else
437
- &[_]u3{ 0, o + 1 };
438
- }
439
-
440
- /// Returns a slice with a range of values for binding given the `action` and `origin` states
441
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
442
- pub inline fn binding(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
443
- if (parent == .true) return &U3_NONE;
444
- const a = action.durations.binding;
445
- const o = origin.durations.binding;
446
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
447
- assert(a == 0 or a == o + 1);
448
- return if (o >= 4) &U3_NONE else &[_]u3{ 0, o + 1 };
449
- }
450
-
451
- const SLOT_NONE = [_]u4{0};
452
- const SLOT = [_]u4{ 1, 2, 3, 4 };
453
-
454
- /// Returns a slice with a range of values for move slots given the `action` state
455
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
456
- ///
457
- /// These slots may or **may not be valid** as slots may be unset / have 0 PP.
458
- pub fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
459
- if (parent == .false) return &SLOT_NONE;
460
- return if (action.move_slot == 0) &SLOT_NONE else &SLOT;
461
- }
462
-
463
- const MULTI_NONE = [_]u4{0};
464
- const MULTI = [_]u4{ 2, 3, 4, 5 };
465
-
466
- /// Returns a slice with the correct range of values for multi hit given the `action` state
467
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
468
- pub fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
469
- if (parent == .false) return &MULTI_NONE;
470
- return if (action.multi_hit == 0) &MULTI_NONE else &MULTI;
471
- }
472
-
473
- const PSYWAVE_NONE = [_]u8{0};
474
- const PSYWAVE = init: {
475
- var rolls: [150]u8 = undefined;
476
- for (0..150) |i| rolls[i] = i + 1;
477
- break :init rolls;
478
- };
479
-
480
- /// Returns a slice with the correct range of values for psywave given the `action` state,
481
- /// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
482
- pub fn psywave(
483
- action: Action,
484
- side: *data.Side,
485
- parent: Optional(bool),
486
- ) []const u8 {
487
- if (parent == .false) return &PSYWAVE_NONE;
488
- return if (action.psywave == 0)
489
- &PSYWAVE_NONE
490
- else
491
- PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
492
- }
493
-
494
- const MOVE_NONE = [_]Move{.None};
495
-
496
- /// Returns a slice with the correct range of values for metronome given the `action` state.
497
- pub fn metronome(action: Action) []const Move {
498
- return if (action.metronome == .None) &MOVE_NONE else &Move.METRONOME;
499
- }
500
- };
501
-
502
- test "Rolls.speedTie" {
503
- const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
504
- try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
505
- try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
506
- }
507
-
508
- test "Rolls.damage" {
509
- const actions: Actions = .{ .p2 = .{ .damage = 221 } };
510
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
511
- try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
512
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
513
- }
514
-
515
- test "Rolls.coalesce" {
516
- var summaries =
517
- calc.Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
518
- try expectEqual(0, try Rolls.coalesce(.P2, 0, &summaries, false));
519
- try expectEqual(241, try Rolls.coalesce(.P2, 238, &summaries, false));
520
- try expectEqual(255, try Rolls.coalesce(.P2, 238, &summaries, true));
521
- summaries.p1.damage.final = 74;
522
- try expectEqual(217, try Rolls.coalesce(.P2, 217, &summaries, false));
523
- try expectEqual(255, try Rolls.coalesce(.P2, 217, &summaries, true));
524
- }
525
-
526
- test "Rolls.hit" {
527
- const actions: Actions = .{ .p2 = .{ .hit = .true } };
528
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
529
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
530
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
531
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
532
- }
533
-
534
- test "Rolls.secondaryChance" {
535
- const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
536
- try expectEqualSlices(
537
- Optional(bool),
538
- &.{ .false, .true },
539
- Rolls.secondaryChance(actions.p1, .None),
540
- );
541
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
542
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
543
- }
544
-
545
- test "Rolls.criticalHit" {
546
- const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
547
- try expectEqualSlices(
548
- Optional(bool),
549
- &.{ .false, .true },
550
- Rolls.criticalHit(actions.p1, .None),
551
- );
552
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
553
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
554
- }
555
-
556
- test "Rolls.confused" {
557
- const actions: Actions = .{ .p2 = .{ .confused = .true } };
558
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, 1));
559
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2, 1));
560
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, 0));
561
- }
562
-
563
- test "Rolls.paralyzed" {
564
- const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
565
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
566
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
567
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
568
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
569
- }
570
-
571
- test "Rolls.duration" {
572
- const actions: Actions = .{ .p2 = .{ .duration = 3 } };
573
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p1, .None));
574
- try expectEqualSlices(u1, &.{1}, Rolls.duration(actions.p2, .None));
575
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p2, .false));
576
- }
577
-
578
- test "Rolls.sleep" {
579
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(
580
- .{ .durations = .{ .sleep = 0 } },
581
- .{ .durations = .{ .sleep = 0 } },
582
- ));
583
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.sleep(
584
- .{ .durations = .{ .sleep = 1 } },
585
- .{ .durations = .{ .sleep = 0 } },
586
- ));
587
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.sleep(
588
- .{ .durations = .{ .sleep = 0 } },
589
- .{ .durations = .{ .sleep = 1 } },
590
- ));
591
- try expectEqualSlices(u3, &.{1}, Rolls.sleep(
592
- .{ .durations = .{ .sleep = 1 } },
593
- .{ .durations = .{ .sleep = 1 } },
594
- ));
595
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.sleep(
596
- .{ .durations = .{ .sleep = 0 } },
597
- .{ .durations = .{ .sleep = 2 } },
598
- ));
599
- try expectEqualSlices(u3, &.{ 0, 7 }, Rolls.sleep(
600
- .{ .durations = .{ .sleep = 7 } },
601
- .{ .durations = .{ .sleep = 6 } },
602
- ));
603
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(
604
- .{ .durations = .{ .sleep = 0 } },
605
- .{ .durations = .{ .sleep = 7 } },
606
- ));
607
- }
608
-
609
- test "Rolls.disable" {
610
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
611
- .{ .durations = .{ .disable = 0 } },
612
- .{ .durations = .{ .disable = 0 } },
613
- 0,
614
- ));
615
- try expectEqualSlices(u4, &.{ 0, 1 }, Rolls.disable(
616
- .{ .durations = .{ .disable = 1 } },
617
- .{ .durations = .{ .disable = 0 } },
618
- 0,
619
- ));
620
- try expectEqualSlices(u4, &.{ 0, 2 }, Rolls.disable(
621
- .{ .durations = .{ .disable = 0 } },
622
- .{ .durations = .{ .disable = 1 } },
623
- 0,
624
- ));
625
- try expectEqualSlices(u4, &.{1}, Rolls.disable(
626
- .{ .durations = .{ .disable = 1 } },
627
- .{ .durations = .{ .disable = 1 } },
628
- 0,
629
- ));
630
- try expectEqualSlices(u4, &.{ 0, 3 }, Rolls.disable(
631
- .{ .durations = .{ .disable = 0 } },
632
- .{ .durations = .{ .disable = 2 } },
633
- 0,
634
- ));
635
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
636
- .{ .durations = .{ .disable = 0 } },
637
- .{ .durations = .{ .disable = 2 } },
638
- 1,
639
- ));
640
- try expectEqualSlices(u4, &.{ 0, 8 }, Rolls.disable(
641
- .{ .durations = .{ .disable = 8 } },
642
- .{ .durations = .{ .disable = 7 } },
643
- 0,
644
- ));
645
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
646
- .{ .durations = .{ .disable = 0 } },
647
- .{ .durations = .{ .disable = 8 } },
648
- 0,
649
- ));
650
- }
651
-
652
- test "Rolls.confusion" {
653
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
654
- .{ .durations = .{ .confusion = 0 } },
655
- .{ .durations = .{ .confusion = 0 } },
656
- 0,
657
- ));
658
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(
659
- .{ .durations = .{ .confusion = 1 } },
660
- .{ .durations = .{ .confusion = 0 } },
661
- 0,
662
- ));
663
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
664
- .{ .durations = .{ .confusion = 0 } },
665
- .{ .durations = .{ .confusion = 1 } },
666
- 0,
667
- ));
668
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(
669
- .{ .durations = .{ .confusion = 1 } },
670
- .{ .durations = .{ .confusion = 1 } },
671
- 0,
672
- ));
673
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.confusion(
674
- .{ .durations = .{ .confusion = 0 } },
675
- .{ .durations = .{ .confusion = 2 } },
676
- 0,
677
- ));
678
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
679
- .{ .durations = .{ .confusion = 0 } },
680
- .{ .durations = .{ .confusion = 2 } },
681
- 1,
682
- ));
683
- try expectEqualSlices(u3, &.{ 0, 5 }, Rolls.confusion(
684
- .{ .durations = .{ .confusion = 5 } },
685
- .{ .durations = .{ .confusion = 4 } },
686
- 0,
687
- ));
688
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
689
- .{ .durations = .{ .confusion = 0 } },
690
- .{ .durations = .{ .confusion = 5 } },
691
- 0,
692
- ));
693
- }
694
-
695
- test "Rolls.attacking" {
696
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
697
- .{ .durations = .{ .attacking = 0 } },
698
- .{ .durations = .{ .attacking = 0 } },
699
- .false,
700
- ));
701
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
702
- .{ .durations = .{ .attacking = 1 } },
703
- .{ .durations = .{ .attacking = 0 } },
704
- .false,
705
- ));
706
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
707
- .{ .durations = .{ .attacking = 0 } },
708
- .{ .durations = .{ .attacking = 1 } },
709
- .false,
710
- ));
711
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
712
- .{ .durations = .{ .attacking = 1 } },
713
- .{ .durations = .{ .attacking = 1 } },
714
- .false,
715
- ));
716
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
717
- .{ .durations = .{ .attacking = 0 } },
718
- .{ .durations = .{ .attacking = 2 } },
719
- .false,
720
- ));
721
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
722
- .{ .durations = .{ .attacking = 0 } },
723
- .{ .durations = .{ .attacking = 2 } },
724
- .true,
725
- ));
726
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
727
- .{ .durations = .{ .attacking = 3 } },
728
- .{ .durations = .{ .attacking = 2 } },
729
- .false,
730
- ));
731
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
732
- .{ .durations = .{ .attacking = 0 } },
733
- .{ .durations = .{ .attacking = 3 } },
734
- .false,
735
- ));
736
- }
737
-
738
- test "Rolls.binding" {
739
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
740
- .{ .durations = .{ .binding = 0 } },
741
- .{ .durations = .{ .binding = 0 } },
742
- .false,
743
- ));
744
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.binding(
745
- .{ .durations = .{ .binding = 1 } },
746
- .{ .durations = .{ .binding = 0 } },
747
- .false,
748
- ));
749
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.binding(
750
- .{ .durations = .{ .binding = 0 } },
751
- .{ .durations = .{ .binding = 1 } },
752
- .false,
753
- ));
754
- try expectEqualSlices(u3, &.{1}, Rolls.binding(
755
- .{ .durations = .{ .binding = 1 } },
756
- .{ .durations = .{ .binding = 1 } },
757
- .false,
758
- ));
759
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.binding(
760
- .{ .durations = .{ .binding = 0 } },
761
- .{ .durations = .{ .binding = 2 } },
762
- .false,
763
- ));
764
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
765
- .{ .durations = .{ .binding = 0 } },
766
- .{ .durations = .{ .binding = 2 } },
767
- .true,
768
- ));
769
- try expectEqualSlices(u3, &.{ 0, 4 }, Rolls.binding(
770
- .{ .durations = .{ .binding = 4 } },
771
- .{ .durations = .{ .binding = 3 } },
772
- .false,
773
- ));
774
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
775
- .{ .durations = .{ .binding = 0 } },
776
- .{ .durations = .{ .binding = 4 } },
777
- .false,
778
- ));
779
- }
780
-
781
- test "Rolls.moveSlot" {
782
- const actions: Actions = .{ .p2 = .{ .move_slot = 3 } };
783
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
784
- try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
785
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
786
- }
787
-
788
- test "Rolls.multiHit" {
789
- const actions: Actions = .{ .p2 = .{ .multi_hit = 3 } };
790
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
791
- try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
792
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
793
- }
794
-
795
- test "Rolls.metronome" {
796
- const actions: Actions = .{ .p2 = .{ .metronome = .Surf } };
797
- try expectEqualSlices(Move, &.{.None}, Rolls.metronome(actions.p1));
798
- try expectEqual(@as(Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
799
- }
800
-
801
- test "Rolls.psywave" {
802
- const actions: Actions = .{ .p2 = .{ .psywave = 79 } };
803
- var side = Side.init(&[_]Pokemon{.{
804
- .species = .Bulbasaur,
805
- .level = 100,
806
- .moves = &[_]Move{.Tackle},
807
- }});
808
-
809
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
810
- var rolls = Rolls.psywave(actions.p2, &side, .None);
811
- try expectEqual(150, rolls[rolls.len - 1]);
812
- side.stored().level = 81;
813
- rolls = Rolls.psywave(actions.p2, &side, .None);
814
- try expectEqual(121, rolls[rolls.len - 1]);
815
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
816
- }
817
-
818
296
  /// Empirically determined maximum number of bytes possibly required to store the diff between two
819
297
  /// Generation I battles.
820
- pub const MAX_DIFFS: usize = 117;
298
+ pub const MAX_DIFFS: usize = 120;
821
299
 
822
300
  /// The optimal size in bytes required to hold the diff between two Generation I battles.
823
301
  /// At least as large as MAX_DIFFS.
@@ -839,8 +317,8 @@ pub fn diff(
839
317
 
840
318
  var i: usize = 0;
841
319
  while (i < size) : (i += 2) {
842
- const val = std.mem.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
843
- if (val != std.mem.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
320
+ const val = util.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
321
+ if (val != util.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
844
322
  try w.writeByte(@intCast(i / 2));
845
323
  try w.writeInt(u16, val, endian);
846
324
  }
@@ -854,7 +332,8 @@ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
854
332
  var i: usize = 0;
855
333
  while (i < buf.len) : (i += 3) {
856
334
  const off = @as(u16, @intCast(buf[i])) * 2;
857
- const val = std.mem.readInt(u16, @ptrCast(buf[i + 1 .. i + 3]), endian);
858
- std.mem.writeInt(u16, @ptrCast(std.mem.asBytes(battle)[off .. off + 2]), val, endian);
335
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
336
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
337
+ std.mem.writeInt(u16, bytes, val, endian);
859
338
  }
860
339
  }