@pkmn/engine 0.1.0-dev.15f64810 → 0.1.0-dev.1cea12bf

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