@pkmn/engine 0.1.0-dev.15f64810 → 0.1.0-dev.18478e7d

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 +30 -17
  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 +346 -128
  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 +201 -0
  43. package/src/lib/bindings/node.zig +117 -0
  44. package/src/lib/bindings/wasm.zig +51 -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 +45 -29
  48. package/src/lib/common/js.zig +3 -7
  49. package/src/lib/common/optional.zig +26 -22
  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 +338 -254
  53. package/src/lib/common/rng.zig +5 -7
  54. package/src/lib/common/util.zig +53 -1
  55. package/src/lib/gen1/calc.zig +651 -205
  56. package/src/lib/gen1/chance.zig +443 -257
  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 +39 -574
  62. package/src/lib/gen1/mechanics.zig +328 -229
  63. package/src/lib/gen2/calc.zig +35 -28
  64. package/src/lib/gen2/chance.zig +117 -518
  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 +32 -33
  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,15 +161,20 @@ 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| {
184
- const hp = comptime std.mem.eql(u8, field.name, "hp");
164
+ inline for (if (@hasField(@TypeOf(@typeInfo(@TypeOf(pokemon.stats)).@"struct"), "fields"))
165
+ @typeInfo(@TypeOf(pokemon.stats)).@"struct".fields
166
+ else
167
+ @typeInfo(@TypeOf(pokemon.stats)).@"struct".field_names) |field|
168
+ {
169
+ const field_name = if (@hasField(@TypeOf(field), "name")) field.name else field;
170
+ const hp = comptime std.mem.eql(u8, field_name, "hp");
185
171
  const spc =
186
- comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
187
- @field(pokemon.stats, field.name) = Stats(u16).calc(
188
- field.name,
189
- @field(species.stats, field.name),
190
- if (hp) p.dvs.hp() else if (spc) p.dvs.spc else @field(p.dvs, field.name),
191
- @field(p.stats, field.name),
172
+ comptime std.mem.eql(u8, field_name, "spa") or std.mem.eql(u8, field_name, "spd");
173
+ @field(pokemon.stats, field_name) = Stats(u16).calc(
174
+ field_name,
175
+ @field(species.stats, field_name),
176
+ if (hp) p.dvs.hp() else if (spc) p.dvs.spc else @field(p.dvs, field_name),
177
+ @field(p.stats, field_name),
192
178
  p.level,
193
179
  );
194
180
  }
@@ -216,20 +202,25 @@ pub const Pokemon = struct {
216
202
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
217
203
  var stats: Stats(u16) = .{};
218
204
  const dvs = DVs.random(rand);
219
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
220
- @field(stats, field.name) = Stats(u16).calc(
221
- field.name,
222
- @field(species.stats, field.name),
223
- if (comptime std.mem.eql(u8, field.name, "hp"))
205
+ inline for (if (@hasField(@TypeOf(@typeInfo(@TypeOf(stats)).@"struct"), "fields"))
206
+ @typeInfo(@TypeOf(stats)).@"struct".fields
207
+ else
208
+ @typeInfo(@TypeOf(stats)).@"struct".field_names) |field|
209
+ {
210
+ const field_name = if (@hasField(@TypeOf(field), "name")) field.name else field;
211
+ @field(stats, field_name) = Stats(u16).calc(
212
+ field_name,
213
+ @field(species.stats, field_name),
214
+ if (comptime std.mem.eql(u8, field_name, "hp"))
224
215
  dvs.hp()
225
216
  else
226
- @field(dvs, field.name),
217
+ @field(dvs, field_name),
227
218
  if (rand.chance(u8, 1, 20)) rand.range(u16, 0, EXP + 1) else EXP,
228
219
  lvl,
229
220
  );
230
221
  }
231
222
 
232
- var ms = [_]MoveSlot{.{}} ** 4;
223
+ var ms: [4]MoveSlot = @splat(.{});
233
224
  const n = if (rand.chance(u8, 1, 100)) rand.range(u4, 1, 3 + 1) else 4;
234
225
  for (0..n) |i| {
235
226
  var m: Move = .None;
@@ -257,6 +248,7 @@ pub const Pokemon = struct {
257
248
  .stats = stats,
258
249
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
259
250
  .status = if (!opt.cleric and rand.chance(u8, 1, 6 + 1))
251
+ // NB: This will only assign sleep as 2 or 4 when rolled
260
252
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
261
253
  else
262
254
  0,
@@ -276,14 +268,7 @@ fn blocked(m: Move) bool {
276
268
  fn durations(m: Move) bool {
277
269
  // Any moves that have effects that result in duration tracking with -Dchance
278
270
  return switch (Move.get(m).effect) {
279
- .Confusion,
280
- .Bide,
281
- .Sleep,
282
- .Rage,
283
- .Binding,
284
- .Thrashing,
285
- .ConfusionChance,
286
- .Disable,
271
+ .Haze,
287
272
  .Metronome,
288
273
  => true,
289
274
  else => false,
@@ -300,530 +285,9 @@ pub fn swtch(slot: u4) Choice {
300
285
  return .{ .type = .Switch, .data = slot };
301
286
  }
302
287
 
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
288
  /// Empirically determined maximum number of bytes possibly required to store the diff between two
825
289
  /// Generation I battles.
826
- pub const MAX_DIFFS: usize = 117;
290
+ pub const MAX_DIFFS: usize = 120;
827
291
 
828
292
  /// The optimal size in bytes required to hold the diff between two Generation I battles.
829
293
  /// At least as large as MAX_DIFFS.
@@ -836,9 +300,9 @@ else
836
300
  pub fn diff(
837
301
  a: *const data.Battle(data.PRNG),
838
302
  b: *const data.Battle(data.PRNG),
839
- w: protocol.ByteStream.Writer,
303
+ w: *protocol.Writer,
840
304
  ) !usize {
841
- const pos = w.stream.pos;
305
+ const pos = w.pos;
842
306
  const size = @sizeOf(data.Battle(data.PRNG)) - @sizeOf(data.PRNG);
843
307
  assert(size % 2 == 0);
844
308
  assert(size / 2 < 255);
@@ -852,7 +316,7 @@ pub fn diff(
852
316
  }
853
317
  }
854
318
 
855
- return w.stream.pos - pos;
319
+ return w.pos - pos;
856
320
  }
857
321
 
858
322
  /// TODO
@@ -860,7 +324,8 @@ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
860
324
  var i: usize = 0;
861
325
  while (i < buf.len) : (i += 3) {
862
326
  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);
327
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
328
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
329
+ std.mem.writeInt(u16, bytes, val, endian);
865
330
  }
866
331
  }