@pkmn/engine 0.1.0-dev.deda8593 → 0.1.0-dev.e0a786f8

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 (79) hide show
  1. package/README.md +9 -6
  2. package/build/pkg/addon/browser.d.ts +3 -0
  3. package/build/pkg/addon/browser.js +47 -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 +110 -0
  7. package/build/pkg/addon/node.js.map +1 -0
  8. package/build/pkg/addon/wasm.d.ts +2 -0
  9. package/build/pkg/addon/wasm.js +53 -0
  10. package/build/pkg/addon/wasm.js.map +1 -0
  11. package/build/pkg/addon.d.ts +25 -3
  12. package/build/pkg/addon.js +34 -65
  13. package/build/pkg/addon.js.map +1 -1
  14. package/build/pkg/data/layout.json +15 -2
  15. package/build/pkg/data/protocol.json +23 -9
  16. package/build/pkg/data.d.ts +23 -1
  17. package/build/pkg/data.js.map +1 -1
  18. package/build/pkg/gen1.d.ts +15 -18
  19. package/build/pkg/gen1.js +28 -19
  20. package/build/pkg/gen1.js.map +1 -1
  21. package/build/pkg/index.d.ts +40 -90
  22. package/build/pkg/index.js +23 -70
  23. package/build/pkg/index.js.map +1 -1
  24. package/build/pkg/protocol.d.ts +20 -1
  25. package/build/pkg/protocol.js +105 -103
  26. package/build/pkg/protocol.js.map +1 -1
  27. package/build/tools/debug.js +52 -76
  28. package/build/tools/debug.js.map +1 -1
  29. package/build/tools/display/debug.jsx +152 -0
  30. package/build/tools/display/debug.jsx.map +1 -0
  31. package/build/tools/display/index.js +152 -0
  32. package/build/tools/display/index.js.map +1 -0
  33. package/build/tools/display/ui.jsx +359 -0
  34. package/build/tools/display/ui.jsx.map +1 -0
  35. package/build/tools/display/util.js +33 -0
  36. package/build/tools/display/util.js.map +1 -0
  37. package/build.zig +330 -85
  38. package/package.json +54 -135
  39. package/src/bin/install-pkmn-engine +10 -7
  40. package/src/bin/pkmn-debug +1 -1
  41. package/src/lib/bindings/c.zig +255 -0
  42. package/src/lib/bindings/node.zig +121 -0
  43. package/src/lib/bindings/wasm.zig +59 -0
  44. package/src/lib/common/array.zig +77 -0
  45. package/src/lib/common/data.zig +8 -8
  46. package/src/lib/common/debug.zig +39 -8
  47. package/src/lib/common/js.zig +4 -2
  48. package/src/lib/common/optional.zig +13 -9
  49. package/src/lib/common/options.zig +4 -0
  50. package/src/lib/common/protocol.zig +45 -44
  51. package/src/lib/common/rational.zig +323 -157
  52. package/src/lib/common/rng.zig +5 -7
  53. package/src/lib/common/util.zig +60 -1
  54. package/src/lib/gen1/calc.zig +527 -161
  55. package/src/lib/gen1/chance.zig +366 -240
  56. package/src/lib/gen1/data/moves.zig +2 -4
  57. package/src/lib/gen1/data/species.zig +1 -3
  58. package/src/lib/gen1/data/types.zig +1 -2
  59. package/src/lib/gen1/data.zig +35 -38
  60. package/src/lib/gen1/helpers.zig +17 -554
  61. package/src/lib/gen1/mechanics.zig +280 -193
  62. package/src/lib/gen2/calc.zig +37 -27
  63. package/src/lib/gen2/chance.zig +90 -503
  64. package/src/lib/gen2/data/items.zig +1 -3
  65. package/src/lib/gen2/data/moves.zig +2 -4
  66. package/src/lib/gen2/data/species.zig +1 -3
  67. package/src/lib/gen2/data/types.zig +1 -2
  68. package/src/lib/gen2/data.zig +26 -29
  69. package/src/lib/gen2/generated.zig +18 -23
  70. package/src/lib/gen2/helpers.zig +9 -18
  71. package/src/lib/gen2/mechanics.zig +88 -115
  72. package/src/lib/node.zig +3 -116
  73. package/src/lib/pkmn.zig +14 -7
  74. package/src/lib/wasm.zig +13 -9
  75. package/src/tools/display/dom.ts +93 -0
  76. package/src/tools/display/ui.css +354 -0
  77. package/build/test/display.js +0 -400
  78. package/build/test/display.js.map +0 -1
  79. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -1,43 +1,26 @@
1
- const std = @import("std");
2
1
  const builtin = @import("builtin");
3
-
4
2
  const common = @import("../common/data.zig");
5
- const optional = @import("../common/optional.zig");
3
+ const data = @import("data.zig");
6
4
  const options = @import("../common/options.zig");
7
5
  const protocol = @import("../common/protocol.zig");
8
- const rational = @import("../common/rational.zig");
9
6
  const rng = @import("../common/rng.zig");
10
-
11
- const calc = @import("calc.zig");
12
- const chance = @import("chance.zig");
13
- const data = @import("data.zig");
7
+ const std = @import("std");
8
+ const util = @import("../common/util.zig");
14
9
 
15
10
  const assert = std.debug.assert;
16
-
17
- const expectEqual = std.testing.expectEqual;
18
- const expectEqualSlices = std.testing.expectEqualSlices;
19
-
20
11
  const Choice = common.Choice;
21
- const Player = common.Player;
22
-
23
- const Optional = optional.Optional;
24
-
25
- const showdown = options.showdown;
26
-
27
- const PSRNG = rng.PSRNG;
28
-
29
12
  const DVs = data.DVs;
30
13
  const Move = data.Move;
31
14
  const MoveSlot = data.MoveSlot;
15
+ const PSRNG = rng.PSRNG;
16
+ const showdown = options.showdown;
32
17
  const Species = data.Species;
33
18
  const Stats = data.Stats;
34
- const Status = data.Status;
35
-
36
- const Action = chance.Action;
37
- const Actions = chance.Actions;
38
19
 
39
20
  const endian = builtin.cpu.arch.endian();
40
21
 
22
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
23
+
41
24
  /// Configuration options controlling the domain of valid Pokémon that can be generated by the
42
25
  /// random generator helpers.
43
26
  pub const Options = struct {
@@ -180,7 +163,7 @@ pub const Pokemon = struct {
180
163
  var pokemon = data.Pokemon{};
181
164
  pokemon.species = p.species;
182
165
  const species = Species.get(p.species);
183
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
166
+ inline for (@field(@typeInfo(@TypeOf(pokemon.stats)), @tagName(Struct)).fields) |field| {
184
167
  const hp = comptime std.mem.eql(u8, field.name, "hp");
185
168
  const spc =
186
169
  comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
@@ -216,7 +199,7 @@ pub const Pokemon = struct {
216
199
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
217
200
  var stats: Stats(u16) = .{};
218
201
  const dvs = DVs.random(rand);
219
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
202
+ inline for (@field(@typeInfo(@TypeOf(stats)), @tagName(Struct)).fields) |field| {
220
203
  @field(stats, field.name) = Stats(u16).calc(
221
204
  field.name,
222
205
  @field(species.stats, field.name),
@@ -257,6 +240,7 @@ pub const Pokemon = struct {
257
240
  .stats = stats,
258
241
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
259
242
  .status = if (!opt.cleric and rand.chance(u8, 1, 6 + 1))
243
+ // NB: This will only assign sleep as 2 or 4 when rolled
260
244
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
261
245
  else
262
246
  0,
@@ -276,14 +260,7 @@ fn blocked(m: Move) bool {
276
260
  fn durations(m: Move) bool {
277
261
  // Any moves that have effects that result in duration tracking with -Dchance
278
262
  return switch (Move.get(m).effect) {
279
- .Confusion,
280
- .Bide,
281
- .Sleep,
282
- .Rage,
283
- .Binding,
284
- .Thrashing,
285
- .ConfusionChance,
286
- .Disable,
263
+ .Haze,
287
264
  .Metronome,
288
265
  => true,
289
266
  else => false,
@@ -300,524 +277,9 @@ pub fn swtch(slot: u4) Choice {
300
277
  return .{ .type = .Switch, .data = slot };
301
278
  }
302
279
 
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
280
  /// Empirically determined maximum number of bytes possibly required to store the diff between two
819
281
  /// Generation I battles.
820
- pub const MAX_DIFFS: usize = 117;
282
+ pub const MAX_DIFFS: usize = 120;
821
283
 
822
284
  /// The optimal size in bytes required to hold the diff between two Generation I battles.
823
285
  /// At least as large as MAX_DIFFS.
@@ -839,8 +301,8 @@ pub fn diff(
839
301
 
840
302
  var i: usize = 0;
841
303
  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]).*) {
304
+ const val = util.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
305
+ if (val != util.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
844
306
  try w.writeByte(@intCast(i / 2));
845
307
  try w.writeInt(u16, val, endian);
846
308
  }
@@ -854,7 +316,8 @@ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
854
316
  var i: usize = 0;
855
317
  while (i < buf.len) : (i += 3) {
856
318
  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);
319
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
320
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
321
+ std.mem.writeInt(u16, bytes, val, endian);
859
322
  }
860
323
  }