@pkmn/engine 0.1.0-dev.a0167a9d → 0.1.0-dev.a0447192

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 (60) 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 +35 -68
  22. package/build/tools/debug.js.map +1 -1
  23. package/build/tools/display/debug.jsx +141 -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 +46 -29
  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/debug.zig +39 -8
  38. package/src/lib/common/js.zig +4 -2
  39. package/src/lib/common/optional.zig +13 -9
  40. package/src/lib/common/options.zig +2 -0
  41. package/src/lib/common/protocol.zig +41 -36
  42. package/src/lib/common/rational.zig +53 -51
  43. package/src/lib/common/rng.zig +4 -4
  44. package/src/lib/common/util.zig +50 -1
  45. package/src/lib/gen1/calc.zig +508 -127
  46. package/src/lib/gen1/chance.zig +316 -173
  47. package/src/lib/gen1/data.zig +28 -27
  48. package/src/lib/gen1/helpers.zig +15 -450
  49. package/src/lib/gen1/mechanics.zig +216 -145
  50. package/src/lib/gen2/calc.zig +34 -16
  51. package/src/lib/gen2/chance.zig +90 -592
  52. package/src/lib/gen2/data.zig +15 -14
  53. package/src/lib/gen2/helpers.zig +4 -6
  54. package/src/lib/gen2/mechanics.zig +47 -77
  55. package/src/lib/pkmn.zig +7 -5
  56. package/src/tools/display/dom.ts +96 -0
  57. package/src/tools/display/ui.css +359 -0
  58. package/build/test/display.js +0 -400
  59. package/build/test/display.js.map +0 -1
  60. 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,14 +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
- const Duration = chance.Duration;
39
25
 
40
26
  const endian = builtin.cpu.arch.endian();
41
27
 
28
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
29
+
42
30
  /// Configuration options controlling the domain of valid Pokémon that can be generated by the
43
31
  /// random generator helpers.
44
32
  pub const Options = struct {
@@ -181,7 +169,7 @@ pub const Pokemon = struct {
181
169
  var pokemon = data.Pokemon{};
182
170
  pokemon.species = p.species;
183
171
  const species = Species.get(p.species);
184
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
172
+ inline for (@field(@typeInfo(@TypeOf(pokemon.stats)), @tagName(Struct)).fields) |field| {
185
173
  const hp = comptime std.mem.eql(u8, field.name, "hp");
186
174
  const spc =
187
175
  comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
@@ -217,7 +205,7 @@ pub const Pokemon = struct {
217
205
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
218
206
  var stats: Stats(u16) = .{};
219
207
  const dvs = DVs.random(rand);
220
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
208
+ inline for (@field(@typeInfo(@TypeOf(stats)), @tagName(Struct)).fields) |field| {
221
209
  @field(stats, field.name) = Stats(u16).calc(
222
210
  field.name,
223
211
  @field(species.stats, field.name),
@@ -236,7 +224,7 @@ pub const Pokemon = struct {
236
224
  var m: Move = .None;
237
225
  sample: while (true) {
238
226
  m = @enumFromInt(rand.range(u8, 1, Move.size - 1 + 1));
239
- if (opt.durations and durationsX(m)) continue :sample;
227
+ if (opt.durations and durations(m)) continue :sample;
240
228
  if (opt.block and blocked(m)) continue :sample;
241
229
  for (0..i) |j| if (ms[j].id == m) continue :sample;
242
230
  break;
@@ -258,6 +246,7 @@ pub const Pokemon = struct {
258
246
  .stats = stats,
259
247
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
260
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
261
250
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
262
251
  else
263
252
  0,
@@ -274,17 +263,10 @@ fn blocked(m: Move) bool {
274
263
  };
275
264
  }
276
265
 
277
- fn durationsX(m: Move) bool {
266
+ fn durations(m: Move) bool {
278
267
  // Any moves that have effects that result in duration tracking with -Dchance
279
268
  return switch (Move.get(m).effect) {
280
- .Confusion,
281
- .Bide,
282
- .Sleep,
283
- .Rage,
284
- .Binding,
285
- .Thrashing,
286
- .ConfusionChance,
287
- .Disable,
269
+ .Haze,
288
270
  .Metronome,
289
271
  => true,
290
272
  else => false,
@@ -301,427 +283,9 @@ pub fn swtch(slot: u4) Choice {
301
283
  return .{ .type = .Switch, .data = slot };
302
284
  }
303
285
 
304
- const NONE = @intFromEnum(Optional(bool).None);
305
-
306
- /// Helper functions that efficiently return valid ranges for various RNG events based on the
307
- /// state of an `Action` and other events to be used to construct a "transitions" function.
308
- pub const Rolls = struct {
309
- const PLAYER_NONE = [_]Optional(Player){.None};
310
- const PLAYERS = [_]Optional(Player){ .P1, .P2 };
311
-
312
- /// Returns a slice with the correct range of values for speed ties given the `action` state.
313
- pub fn speedTie(action: Action) []const Optional(Player) {
314
- return if (action.speed_tie == .None) &PLAYER_NONE else &PLAYERS;
315
- }
316
-
317
- const BOOL_NONE = [_]Optional(bool){.None};
318
- const BOOLS = [_]Optional(bool){ .false, .true };
319
-
320
- /// Returns a slice with the correct range of values for hits given the `action` state
321
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
322
- pub fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
323
- if (parent == .true) return &BOOL_NONE;
324
- return if (action.hit == .None) &BOOL_NONE else &BOOLS;
325
- }
326
-
327
- /// Returns a slice with the correct range of values for critical hits given the `action` state
328
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
329
- pub fn criticalHit(action: Action, parent: Optional(bool)) []const Optional(bool) {
330
- if (parent == .false) return &BOOL_NONE;
331
- return if (action.critical_hit == .None) &BOOL_NONE else &BOOLS;
332
- }
333
-
334
- /// Returns a slice with the correct range of values for secondary chances hits given the
335
- /// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
336
- pub fn secondaryChance(action: Action, parent: Optional(bool)) []const Optional(bool) {
337
- if (parent == .false) return &BOOL_NONE;
338
- return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
339
- }
340
-
341
- /// The min and max bounds on iteration over damage rolls.
342
- pub const Range = struct { min: u9, max: u9 };
343
-
344
- /// Returns the range bounding damage rolls given the `action` state and the state of
345
- /// the `parent` (whether the player's Pokémon's move hit).
346
- pub fn damage(action: Action, parent: Optional(bool)) Range {
347
- return if (parent == .false or action.damage == 0)
348
- .{ .min = 0, .max = 1 }
349
- else
350
- .{ .min = 217, .max = 256 };
351
- }
352
-
353
- /// Returns the max damage roll which will produce the same damage as `roll`
354
- /// given the base damage in `summaries`.
355
- pub fn coalesce(player: Player, roll: u8, summaries: *calc.Summaries, cap: bool) !u8 {
356
- if (roll == 0) return roll;
357
-
358
- const dmg = summaries.get(player.foe()).damage;
359
- if (dmg.base == 0 or (cap and dmg.capped)) return 255;
360
-
361
- // Closed form solution for max damage roll provided by Orion Taylor (taylorott)
362
- return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
363
- }
364
-
365
- /// Returns a slice with the correct range of values for confused given the `action` state
366
- /// and the state of the `parent` (the player's Pokémon's remaining confusion turns).
367
- pub fn confused(action: Action, parent: u4) []const Optional(bool) {
368
- return if (parent == 0 or action.confused == .None) &BOOL_NONE else &BOOLS;
369
- }
370
-
371
- /// Returns a slice with the correct range of values for paralysis given the `action` state
372
- /// and the state of the `parent` (whether the player's Pokémon was confused).
373
- pub fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
374
- if (parent == .true) return &BOOL_NONE;
375
- return if (action.paralyzed == .None) &BOOL_NONE else &BOOLS;
376
- }
377
-
378
- const DURATION_NONE = [_]u1{0};
379
- const DURATION = [_]u1{1};
380
-
381
- /// Returns a slice with a range of values for duration given the `action` state
382
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
383
- pub fn duration(action: Action, parent: Optional(bool)) []const u1 {
384
- if (parent == .false) return &DURATION_NONE;
385
- return if (action.duration == 0) &DURATION_NONE else &DURATION;
386
- }
387
-
388
- const U3_NONE = [_]u3{0};
389
- const U3_FORCED = [_]u3{1};
390
-
391
- /// TODO
392
- pub inline fn sleep(dur: Duration, origin: Duration) []const u3 {
393
- const d = dur.sleep;
394
- const o = origin.sleep;
395
- if (o == d) return if (o == 0) &U3_NONE else &[_]u3{o};
396
- assert(d == 0 or d == o + 1);
397
- return if (o >= 7) &U3_NONE else &[_]u3{ 0, o + 1 };
398
- }
399
-
400
- const DISABLE_NONE = [_]u4{0};
401
-
402
- /// TODO
403
- pub inline fn disable(dur: Duration, origin: Duration, parent: u4) []const u4 {
404
- if (parent > 0) return &DISABLE_NONE;
405
- const a = dur.disable;
406
- const o = origin.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
- /// TODO
413
- pub inline fn confusion(dur: Duration, origin: Duration, parent: u4) []const u3 {
414
- if (parent > 0) return &U3_NONE;
415
- const a = dur.confusion;
416
- const o = origin.confusion;
417
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
418
- assert(a == 0 or a == o + 1);
419
- return if (o >= 5) &U3_NONE else if (o < 2)
420
- if (a == 0) &U3_NONE else &U3_FORCED
421
- else
422
- &[_]u3{ 0, o + 1 };
423
- }
424
-
425
- /// TODO
426
- pub inline fn attacking(dur: Duration, origin: Duration, parent: Optional(bool)) []const u3 {
427
- if (parent == .true) return &U3_NONE;
428
- const a = dur.attacking;
429
- const o = origin.attacking;
430
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
431
- assert(a == 0 or a == o + 1);
432
- return if (o >= 3) &U3_NONE else if (o < 2)
433
- if (a == 0) &U3_NONE else &U3_FORCED
434
- else
435
- &[_]u3{ 0, o + 1 };
436
- }
437
-
438
- /// TODO
439
- pub inline fn binding(dur: Duration, origin: Duration, parent: Optional(bool)) []const u3 {
440
- if (parent == .true) return &U3_NONE;
441
- const a = dur.binding;
442
- const o = origin.binding;
443
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
444
- assert(a == 0 or a == o + 1);
445
- return if (o >= 4) &U3_NONE else &[_]u3{ 0, o + 1 };
446
- }
447
-
448
- const SLOT_NONE = [_]u4{0};
449
- const SLOT = [_]u4{ 1, 2, 3, 4 };
450
-
451
- /// Returns a slice with a range of values for move slots given the `action` state
452
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
453
- ///
454
- /// These slots may or **may not be valid** as slots may be unset / have 0 PP.
455
- pub fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
456
- if (parent == .false) return &SLOT_NONE;
457
- return if (action.move_slot == 0) &SLOT_NONE else &SLOT;
458
- }
459
-
460
- const MULTI_NONE = [_]u4{0};
461
- const MULTI = [_]u4{ 2, 3, 4, 5 };
462
-
463
- /// Returns a slice with the correct range of values for multi hit given the `action` state
464
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
465
- pub fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
466
- if (parent == .false) return &MULTI_NONE;
467
- return if (action.multi_hit == 0) &MULTI_NONE else &MULTI;
468
- }
469
-
470
- const PSYWAVE_NONE = [_]u8{0};
471
- const PSYWAVE = init: {
472
- var rolls: [150]u8 = undefined;
473
- for (0..150) |i| rolls[i] = i + 1;
474
- break :init rolls;
475
- };
476
-
477
- /// Returns a slice with the correct range of values for psywave given the `action` state,
478
- /// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
479
- pub fn psywave(
480
- action: Action,
481
- side: *data.Side,
482
- parent: Optional(bool),
483
- ) []const u8 {
484
- if (parent == .false) return &PSYWAVE_NONE;
485
- return if (action.psywave == 0)
486
- &PSYWAVE_NONE
487
- else
488
- PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
489
- }
490
-
491
- const MOVE_NONE = [_]Move{.None};
492
-
493
- /// Returns a slice with the correct range of values for metronome given the `action` state.
494
- pub fn metronome(action: Action) []const Move {
495
- return if (action.metronome == .None) &MOVE_NONE else &Move.METRONOME;
496
- }
497
- };
498
-
499
- test "Rolls.speedTie" {
500
- const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
501
- try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
502
- try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
503
- }
504
-
505
- test "Rolls.damage" {
506
- const actions: Actions = .{ .p2 = .{ .damage = 221 } };
507
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
508
- try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
509
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
510
- }
511
-
512
- test "Rolls.coalesce" {
513
- var summaries =
514
- calc.Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
515
- try expectEqual(0, try Rolls.coalesce(.P2, 0, &summaries, false));
516
- try expectEqual(241, try Rolls.coalesce(.P2, 238, &summaries, false));
517
- try expectEqual(255, try Rolls.coalesce(.P2, 238, &summaries, true));
518
- summaries.p1.damage.final = 74;
519
- try expectEqual(217, try Rolls.coalesce(.P2, 217, &summaries, false));
520
- try expectEqual(255, try Rolls.coalesce(.P2, 217, &summaries, true));
521
- }
522
-
523
- test "Rolls.hit" {
524
- const actions: Actions = .{ .p2 = .{ .hit = .true } };
525
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
526
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
527
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
528
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
529
- }
530
-
531
- test "Rolls.secondaryChance" {
532
- const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
533
- try expectEqualSlices(
534
- Optional(bool),
535
- &.{ .false, .true },
536
- Rolls.secondaryChance(actions.p1, .None),
537
- );
538
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
539
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
540
- }
541
-
542
- test "Rolls.criticalHit" {
543
- const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
544
- try expectEqualSlices(
545
- Optional(bool),
546
- &.{ .false, .true },
547
- Rolls.criticalHit(actions.p1, .None),
548
- );
549
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
550
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
551
- }
552
-
553
- test "Rolls.confused" {
554
- const actions: Actions = .{ .p2 = .{ .confused = .true } };
555
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, 1));
556
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2, 1));
557
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, 0));
558
- }
559
-
560
- test "Rolls.paralyzed" {
561
- const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
562
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
563
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
564
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
565
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
566
- }
567
-
568
- test "Rolls.duration" {
569
- const actions: Actions = .{ .p2 = .{ .duration = 3 } };
570
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p1, .None));
571
- try expectEqualSlices(u1, &.{1}, Rolls.duration(actions.p2, .None));
572
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p2, .false));
573
- }
574
-
575
- test "Rolls.sleep" {
576
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(.{ .sleep = 0 }, .{ .sleep = 0 }));
577
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.sleep(.{ .sleep = 1 }, .{ .sleep = 0 }));
578
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.sleep(.{ .sleep = 0 }, .{ .sleep = 1 }));
579
- try expectEqualSlices(u3, &.{1}, Rolls.sleep(.{ .sleep = 1 }, .{ .sleep = 1 }));
580
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.sleep(.{ .sleep = 0 }, .{ .sleep = 2 }));
581
- try expectEqualSlices(u3, &.{ 0, 7 }, Rolls.sleep(.{ .sleep = 7 }, .{ .sleep = 6 }));
582
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(.{ .sleep = 0 }, .{ .sleep = 7 }));
583
- }
584
-
585
- test "Rolls.disable" {
586
- try expectEqualSlices(u4, &.{0}, Rolls.disable(.{ .disable = 0 }, .{ .disable = 0 }, 0));
587
- try expectEqualSlices(u4, &.{ 0, 1 }, Rolls.disable(.{ .disable = 1 }, .{ .disable = 0 }, 0));
588
- try expectEqualSlices(u4, &.{ 0, 2 }, Rolls.disable(.{ .disable = 0 }, .{ .disable = 1 }, 0));
589
- try expectEqualSlices(u4, &.{1}, Rolls.disable(.{ .disable = 1 }, .{ .disable = 1 }, 0));
590
- try expectEqualSlices(u4, &.{ 0, 3 }, Rolls.disable(.{ .disable = 0 }, .{ .disable = 2 }, 0));
591
- try expectEqualSlices(u4, &.{0}, Rolls.disable(.{ .disable = 0 }, .{ .disable = 2 }, 1));
592
- try expectEqualSlices(u4, &.{ 0, 8 }, Rolls.disable(.{ .disable = 8 }, .{ .disable = 7 }, 0));
593
- try expectEqualSlices(u4, &.{0}, Rolls.disable(.{ .disable = 0 }, .{ .disable = 8 }, 0));
594
- }
595
-
596
- test "Rolls.confusion" {
597
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(.{ .confusion = 0 }, .{ .confusion = 0 }, 0));
598
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(.{ .confusion = 1 }, .{ .confusion = 0 }, 0));
599
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(.{ .confusion = 0 }, .{ .confusion = 1 }, 0));
600
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(.{ .confusion = 1 }, .{ .confusion = 1 }, 0));
601
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.confusion(
602
- .{ .confusion = 0 },
603
- .{ .confusion = 2 },
604
- 0,
605
- ));
606
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(.{ .confusion = 0 }, .{ .confusion = 2 }, 1));
607
- try expectEqualSlices(u3, &.{ 0, 5 }, Rolls.confusion(
608
- .{ .confusion = 5 },
609
- .{ .confusion = 4 },
610
- 0,
611
- ));
612
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(.{ .confusion = 0 }, .{ .confusion = 5 }, 0));
613
- }
614
-
615
- test "Rolls.attacking" {
616
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
617
- .{ .attacking = 0 },
618
- .{ .attacking = 0 },
619
- .false,
620
- ));
621
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
622
- .{ .attacking = 1 },
623
- .{ .attacking = 0 },
624
- .false,
625
- ));
626
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
627
- .{ .attacking = 0 },
628
- .{ .attacking = 1 },
629
- .false,
630
- ));
631
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
632
- .{ .attacking = 1 },
633
- .{ .attacking = 1 },
634
- .false,
635
- ));
636
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
637
- .{ .attacking = 0 },
638
- .{ .attacking = 2 },
639
- .false,
640
- ));
641
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
642
- .{ .attacking = 0 },
643
- .{ .attacking = 2 },
644
- .true,
645
- ));
646
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
647
- .{ .attacking = 3 },
648
- .{ .attacking = 2 },
649
- .false,
650
- ));
651
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
652
- .{ .attacking = 0 },
653
- .{ .attacking = 3 },
654
- .false,
655
- ));
656
- }
657
-
658
- test "Rolls.binding" {
659
- try expectEqualSlices(u3, &.{0}, Rolls.binding(.{ .binding = 0 }, .{ .binding = 0 }, .false));
660
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.binding(
661
- .{ .binding = 1 },
662
- .{ .binding = 0 },
663
- .false,
664
- ));
665
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.binding(
666
- .{ .binding = 0 },
667
- .{ .binding = 1 },
668
- .false,
669
- ));
670
- try expectEqualSlices(u3, &.{1}, Rolls.binding(.{ .binding = 1 }, .{ .binding = 1 }, .false));
671
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.binding(
672
- .{ .binding = 0 },
673
- .{ .binding = 2 },
674
- .false,
675
- ));
676
- try expectEqualSlices(u3, &.{0}, Rolls.binding(.{ .binding = 0 }, .{ .binding = 2 }, .true));
677
- try expectEqualSlices(u3, &.{ 0, 4 }, Rolls.binding(
678
- .{ .binding = 4 },
679
- .{ .binding = 3 },
680
- .false,
681
- ));
682
- try expectEqualSlices(u3, &.{0}, Rolls.binding(.{ .binding = 0 }, .{ .binding = 4 }, .false));
683
- }
684
-
685
- test "Rolls.moveSlot" {
686
- const actions: Actions = .{ .p2 = .{ .move_slot = 3 } };
687
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
688
- try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
689
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
690
- }
691
-
692
- test "Rolls.multiHit" {
693
- const actions: Actions = .{ .p2 = .{ .multi_hit = 3 } };
694
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
695
- try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
696
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
697
- }
698
-
699
- test "Rolls.metronome" {
700
- const actions: Actions = .{ .p2 = .{ .metronome = .Surf } };
701
- try expectEqualSlices(Move, &.{.None}, Rolls.metronome(actions.p1));
702
- try expectEqual(@as(Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
703
- }
704
-
705
- test "Rolls.psywave" {
706
- const actions: Actions = .{ .p2 = .{ .psywave = 79 } };
707
- var side = Side.init(&[_]Pokemon{.{
708
- .species = .Bulbasaur,
709
- .level = 100,
710
- .moves = &[_]Move{.Tackle},
711
- }});
712
-
713
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
714
- var rolls = Rolls.psywave(actions.p2, &side, .None);
715
- try expectEqual(150, rolls[rolls.len - 1]);
716
- side.stored().level = 81;
717
- rolls = Rolls.psywave(actions.p2, &side, .None);
718
- try expectEqual(121, rolls[rolls.len - 1]);
719
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
720
- }
721
-
722
286
  /// Empirically determined maximum number of bytes possibly required to store the diff between two
723
287
  /// Generation I battles.
724
- pub const MAX_DIFFS: usize = 117;
288
+ pub const MAX_DIFFS: usize = 120;
725
289
 
726
290
  /// The optimal size in bytes required to hold the diff between two Generation I battles.
727
291
  /// At least as large as MAX_DIFFS.
@@ -743,8 +307,8 @@ pub fn diff(
743
307
 
744
308
  var i: usize = 0;
745
309
  while (i < size) : (i += 2) {
746
- const val = std.mem.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
747
- if (val != std.mem.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
310
+ const val = util.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
311
+ if (val != util.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
748
312
  try w.writeByte(@intCast(i / 2));
749
313
  try w.writeInt(u16, val, endian);
750
314
  }
@@ -758,7 +322,8 @@ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
758
322
  var i: usize = 0;
759
323
  while (i < buf.len) : (i += 3) {
760
324
  const off = @as(u16, @intCast(buf[i])) * 2;
761
- const val = std.mem.readInt(u16, @ptrCast(buf[i + 1 .. i + 3]), endian);
762
- std.mem.writeInt(u16, @ptrCast(std.mem.asBytes(battle)[off .. off + 2]), val, endian);
325
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
326
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
327
+ std.mem.writeInt(u16, bytes, val, endian);
763
328
  }
764
329
  }