@pkmn/engine 0.1.0-dev.62399ed5 → 0.1.0-dev.62eefbc0

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 (81) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +24 -14
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +26 -4
  13. package/build/pkg/addon.js +34 -65
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/layout.json +15 -2
  16. package/build/pkg/data/protocol.json +23 -9
  17. package/build/pkg/data.d.ts +23 -1
  18. package/build/pkg/data.js.map +1 -1
  19. package/build/pkg/gen1.d.ts +15 -18
  20. package/build/pkg/gen1.js +28 -19
  21. package/build/pkg/gen1.js.map +1 -1
  22. package/build/pkg/index.d.ts +40 -90
  23. package/build/pkg/index.js +23 -70
  24. package/build/pkg/index.js.map +1 -1
  25. package/build/pkg/protocol.d.ts +29 -1
  26. package/build/pkg/protocol.js +105 -103
  27. package/build/pkg/protocol.js.map +1 -1
  28. package/build/tools/debug.js +83 -73
  29. package/build/tools/debug.js.map +1 -1
  30. package/build/tools/display/debug.jsx +66 -0
  31. package/build/tools/display/debug.jsx.map +1 -0
  32. package/build/tools/display/index.js +152 -0
  33. package/build/tools/display/index.js.map +1 -0
  34. package/build/tools/display/ui.jsx +360 -0
  35. package/build/tools/display/ui.jsx.map +1 -0
  36. package/build/tools/display/util.js +157 -0
  37. package/build/tools/display/util.js.map +1 -0
  38. package/build.zig +266 -103
  39. package/package.json +59 -138
  40. package/src/bin/install-pkmn-engine +25 -7
  41. package/src/bin/pkmn-debug +1 -1
  42. package/src/lib/bindings/c.zig +200 -0
  43. package/src/lib/bindings/node.zig +117 -0
  44. package/src/lib/bindings/wasm.zig +50 -0
  45. package/src/lib/common/array.zig +70 -0
  46. package/src/lib/common/data.zig +8 -8
  47. package/src/lib/common/debug.zig +33 -28
  48. package/src/lib/common/js.zig +3 -7
  49. package/src/lib/common/optional.zig +11 -17
  50. package/src/lib/common/options.zig +4 -0
  51. package/src/lib/common/protocol.zig +196 -160
  52. package/src/lib/common/rational.zig +303 -310
  53. package/src/lib/common/rng.zig +5 -7
  54. package/src/lib/common/util.zig +33 -1
  55. package/src/lib/gen1/calc.zig +569 -190
  56. package/src/lib/gen1/chance.zig +412 -249
  57. package/src/lib/gen1/data/moves.zig +2 -4
  58. package/src/lib/gen1/data/species.zig +1 -3
  59. package/src/lib/gen1/data/types.zig +3 -4
  60. package/src/lib/gen1/data.zig +40 -43
  61. package/src/lib/gen1/helpers.zig +17 -556
  62. package/src/lib/gen1/mechanics.zig +317 -221
  63. package/src/lib/gen2/calc.zig +35 -28
  64. package/src/lib/gen2/chance.zig +91 -510
  65. package/src/lib/gen2/data/items.zig +1 -3
  66. package/src/lib/gen2/data/moves.zig +2 -4
  67. package/src/lib/gen2/data/species.zig +1 -3
  68. package/src/lib/gen2/data/types.zig +7 -5
  69. package/src/lib/gen2/data.zig +33 -35
  70. package/src/lib/gen2/generated.zig +18 -23
  71. package/src/lib/gen2/helpers.zig +9 -20
  72. package/src/lib/gen2/mechanics.zig +105 -132
  73. package/src/lib/napi.h +2 -0
  74. package/src/lib/node.zig +4 -121
  75. package/src/lib/pkmn.zig +103 -22
  76. package/src/lib/wasm.zig +9 -9
  77. package/src/tools/display/dom.ts +108 -0
  78. package/src/tools/display/ui.css +354 -0
  79. package/build/test/display.js +0 -400
  80. package/build/test/display.js.map +0 -1
  81. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -1,40 +1,21 @@
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
 
@@ -180,7 +161,7 @@ pub const Pokemon = struct {
180
161
  var pokemon = data.Pokemon{};
181
162
  pokemon.species = p.species;
182
163
  const species = Species.get(p.species);
183
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
164
+ inline for (@typeInfo(@TypeOf(pokemon.stats)).@"struct".fields) |field| {
184
165
  const hp = comptime std.mem.eql(u8, field.name, "hp");
185
166
  const spc =
186
167
  comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
@@ -216,7 +197,7 @@ pub const Pokemon = struct {
216
197
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
217
198
  var stats: Stats(u16) = .{};
218
199
  const dvs = DVs.random(rand);
219
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
200
+ inline for (@typeInfo(@TypeOf(stats)).@"struct".fields) |field| {
220
201
  @field(stats, field.name) = Stats(u16).calc(
221
202
  field.name,
222
203
  @field(species.stats, field.name),
@@ -229,7 +210,7 @@ pub const Pokemon = struct {
229
210
  );
230
211
  }
231
212
 
232
- var ms = [_]MoveSlot{.{}} ** 4;
213
+ var ms: [4]MoveSlot = @splat(.{});
233
214
  const n = if (rand.chance(u8, 1, 100)) rand.range(u4, 1, 3 + 1) else 4;
234
215
  for (0..n) |i| {
235
216
  var m: Move = .None;
@@ -257,6 +238,7 @@ pub const Pokemon = struct {
257
238
  .stats = stats,
258
239
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
259
240
  .status = if (!opt.cleric and rand.chance(u8, 1, 6 + 1))
241
+ // NB: This will only assign sleep as 2 or 4 when rolled
260
242
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
261
243
  else
262
244
  0,
@@ -276,14 +258,7 @@ fn blocked(m: Move) bool {
276
258
  fn durations(m: Move) bool {
277
259
  // Any moves that have effects that result in duration tracking with -Dchance
278
260
  return switch (Move.get(m).effect) {
279
- .Confusion,
280
- .Bide,
281
- .Sleep,
282
- .Rage,
283
- .Binding,
284
- .Thrashing,
285
- .ConfusionChance,
286
- .Disable,
261
+ .Haze,
287
262
  .Metronome,
288
263
  => true,
289
264
  else => false,
@@ -300,524 +275,9 @@ pub fn swtch(slot: u4) Choice {
300
275
  return .{ .type = .Switch, .data = slot };
301
276
  }
302
277
 
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
278
  /// Empirically determined maximum number of bytes possibly required to store the diff between two
819
279
  /// Generation I battles.
820
- pub const MAX_DIFFS: usize = 117;
280
+ pub const MAX_DIFFS: usize = 120;
821
281
 
822
282
  /// The optimal size in bytes required to hold the diff between two Generation I battles.
823
283
  /// At least as large as MAX_DIFFS.
@@ -830,9 +290,9 @@ else
830
290
  pub fn diff(
831
291
  a: *const data.Battle(data.PRNG),
832
292
  b: *const data.Battle(data.PRNG),
833
- w: protocol.ByteStream.Writer,
293
+ w: *protocol.Writer,
834
294
  ) !usize {
835
- const pos = w.stream.pos;
295
+ const pos = w.pos;
836
296
  const size = @sizeOf(data.Battle(data.PRNG)) - @sizeOf(data.PRNG);
837
297
  assert(size % 2 == 0);
838
298
  assert(size / 2 < 255);
@@ -846,7 +306,7 @@ pub fn diff(
846
306
  }
847
307
  }
848
308
 
849
- return w.stream.pos - pos;
309
+ return w.pos - pos;
850
310
  }
851
311
 
852
312
  /// TODO
@@ -854,7 +314,8 @@ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
854
314
  var i: usize = 0;
855
315
  while (i < buf.len) : (i += 3) {
856
316
  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);
317
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
318
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
319
+ std.mem.writeInt(u16, bytes, val, endian);
859
320
  }
860
321
  }