@pkmn/engine 0.1.0-dev.526bb2e1 → 0.1.0-dev.53787794

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 (76) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +87 -52
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +43 -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 +96 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon.d.ts +2 -0
  10. package/build/pkg/addon.js +35 -58
  11. package/build/pkg/addon.js.map +1 -1
  12. package/build/pkg/data/ids.json +12 -12
  13. package/build/pkg/data/layout.json +2 -2
  14. package/build/pkg/data/protocol.json +69 -12
  15. package/build/pkg/data.d.ts +27 -5
  16. package/build/pkg/data.js.map +1 -1
  17. package/build/pkg/gen1.js +3 -3
  18. package/build/pkg/gen1.js.map +1 -1
  19. package/build/pkg/index.d.ts +24 -10
  20. package/build/pkg/index.js +15 -10
  21. package/build/pkg/index.js.map +1 -1
  22. package/build/pkg/protocol.d.ts +20 -1
  23. package/build/pkg/protocol.js +123 -102
  24. package/build/pkg/protocol.js.map +1 -1
  25. package/build/tools/debug.js +36 -68
  26. package/build/tools/debug.js.map +1 -1
  27. package/build/tools/display/debug.jsx +128 -0
  28. package/build/tools/display/debug.jsx.map +1 -0
  29. package/build/tools/display/index.js +132 -0
  30. package/build/tools/display/index.js.map +1 -0
  31. package/build/tools/display/ui.jsx +349 -0
  32. package/build/tools/display/ui.jsx.map +1 -0
  33. package/build/tools/display/util.js +18 -0
  34. package/build/tools/display/util.js.map +1 -0
  35. package/build.zig +257 -117
  36. package/package.json +67 -32
  37. package/src/bin/install-pkmn-engine +31 -21
  38. package/src/bin/pkmn-debug +1 -1
  39. package/src/lib/common/array.zig +77 -0
  40. package/src/lib/common/data.zig +12 -24
  41. package/src/lib/common/debug.zig +32 -6
  42. package/src/lib/common/js.zig +4 -2
  43. package/src/lib/common/optional.zig +21 -10
  44. package/src/lib/common/options.zig +4 -0
  45. package/src/lib/common/protocol.zig +810 -327
  46. package/src/lib/common/rational.zig +125 -31
  47. package/src/lib/common/rng.zig +22 -13
  48. package/src/lib/common/util.zig +50 -1
  49. package/src/lib/gen1/calc.zig +665 -137
  50. package/src/lib/gen1/chance.zig +410 -285
  51. package/src/lib/gen1/data/moves.zig +192 -180
  52. package/src/lib/gen1/data/species.zig +3 -3
  53. package/src/lib/gen1/data/types.zig +7 -7
  54. package/src/lib/gen1/data.zig +60 -66
  55. package/src/lib/gen1/helpers.zig +94 -399
  56. package/src/lib/gen1/mechanics.zig +596 -552
  57. package/src/lib/gen2/calc.zig +161 -0
  58. package/src/lib/gen2/chance.zig +902 -0
  59. package/src/lib/gen2/data/items.zig +12 -18
  60. package/src/lib/gen2/data/moves.zig +663 -569
  61. package/src/lib/gen2/data/species.zig +1 -1
  62. package/src/lib/gen2/data/types.zig +134 -31
  63. package/src/lib/gen2/data.zig +107 -79
  64. package/src/lib/gen2/generated.zig +806 -0
  65. package/src/lib/gen2/helpers.zig +49 -25
  66. package/src/lib/gen2/mechanics.zig +3560 -12
  67. package/src/lib/{binding/node.zig → node.zig} +10 -13
  68. package/src/lib/pkmn.zig +32 -15
  69. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
  70. package/src/tools/display/dom.ts +96 -0
  71. package/src/tools/display/ui.css +351 -0
  72. package/build/test/display.js +0 -400
  73. package/build/test/display.js.map +0 -1
  74. package/src/lib/gen1/README.md +0 -814
  75. package/src/lib/gen2/README.md +0 -309
  76. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -1,25 +1,17 @@
1
1
  const std = @import("std");
2
+ const builtin = @import("builtin");
2
3
 
3
4
  const common = @import("../common/data.zig");
4
- const optional = @import("../common/optional.zig");
5
5
  const options = @import("../common/options.zig");
6
6
  const protocol = @import("../common/protocol.zig");
7
- const rational = @import("../common/rational.zig");
8
7
  const rng = @import("../common/rng.zig");
8
+ const util = @import("../common/util.zig");
9
9
 
10
- const calc = @import("calc.zig");
11
- const chance = @import("chance.zig");
12
10
  const data = @import("data.zig");
13
11
 
14
12
  const assert = std.debug.assert;
15
13
 
16
- const expectEqual = std.testing.expectEqual;
17
- const expectEqualSlices = std.testing.expectEqualSlices;
18
-
19
14
  const Choice = common.Choice;
20
- const Player = common.Player;
21
-
22
- const Optional = optional.Optional;
23
15
 
24
16
  const showdown = options.showdown;
25
17
 
@@ -30,10 +22,10 @@ const Move = data.Move;
30
22
  const MoveSlot = data.MoveSlot;
31
23
  const Species = data.Species;
32
24
  const Stats = data.Stats;
33
- const Status = data.Status;
34
25
 
35
- const Action = chance.Action;
36
- const Actions = chance.Actions;
26
+ const endian = builtin.cpu.arch.endian();
27
+
28
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
37
29
 
38
30
  /// Configuration options controlling the domain of valid Pokémon that can be generated by the
39
31
  /// random generator helpers.
@@ -43,6 +35,8 @@ pub const Options = struct {
43
35
  /// Whether to generate Pokémon with moves that contain bugs on Pokémon Showdown that are
44
36
  /// unimplementable in the engine.
45
37
  block: bool = showdown,
38
+ // FIXME: remove once durations work with -Dchance
39
+ durations: bool = false,
46
40
  };
47
41
 
48
42
  /// Helpers to simplify initialization of a Generation I battle.
@@ -86,18 +80,38 @@ pub const Battle = struct {
86
80
  };
87
81
 
88
82
  fn prng(rand: *PSRNG) data.PRNG {
89
- // GLITCH: initial bytes in seed can only range from 0-252, not 0-255
90
- const max: u8 = 253;
91
83
  return .{
92
84
  .src = .{
93
85
  .seed = if (showdown)
94
86
  rand.newSeed()
95
- else
96
- .{
97
- rand.range(u8, 0, max), rand.range(u8, 0, max), rand.range(u8, 0, max),
98
- rand.range(u8, 0, max), rand.range(u8, 0, max), rand.range(u8, 0, max),
99
- rand.range(u8, 0, max), rand.range(u8, 0, max), rand.range(u8, 0, max),
100
- },
87
+ else seed: {
88
+ // GLITCH: The initial RNG seed in Gen I link battles is heavily constrained
89
+ // https://www.smogon.com/forums/threads/rng-in-rby-link-battles.3746779
90
+ var seed = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0 };
91
+
92
+ // Bytes in seed can only range from 0-252, not 0-255
93
+ const max: u8 = 253;
94
+ // Instructions take multiples of 4 cycles to execute
95
+ var timer: u16 = 4 * rand.range(u16, 0, 16384);
96
+ var add: u8 = rand.range(u8, 0, 256);
97
+ var carry: u8 = 0;
98
+
99
+ var i: u8 = 0;
100
+ while (i < seed.len) {
101
+ const div: u8 = @intCast(timer >> 8);
102
+ add +%= div +% carry;
103
+ if (add < max) {
104
+ seed[i] = add;
105
+ i += 1;
106
+ timer +%= 472;
107
+ carry = 1;
108
+ } else {
109
+ timer +%= 452;
110
+ carry = 0;
111
+ }
112
+ }
113
+ break :seed seed;
114
+ },
101
115
  },
102
116
  };
103
117
  }
@@ -155,7 +169,7 @@ pub const Pokemon = struct {
155
169
  var pokemon = data.Pokemon{};
156
170
  pokemon.species = p.species;
157
171
  const species = Species.get(p.species);
158
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
172
+ inline for (@field(@typeInfo(@TypeOf(pokemon.stats)), @tagName(Struct)).fields) |field| {
159
173
  const hp = comptime std.mem.eql(u8, field.name, "hp");
160
174
  const spc =
161
175
  comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
@@ -191,7 +205,7 @@ pub const Pokemon = struct {
191
205
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
192
206
  var stats: Stats(u16) = .{};
193
207
  const dvs = DVs.random(rand);
194
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
208
+ inline for (@field(@typeInfo(@TypeOf(stats)), @tagName(Struct)).fields) |field| {
195
209
  @field(stats, field.name) = Stats(u16).calc(
196
210
  field.name,
197
211
  @field(species.stats, field.name),
@@ -210,6 +224,7 @@ pub const Pokemon = struct {
210
224
  var m: Move = .None;
211
225
  sample: while (true) {
212
226
  m = @enumFromInt(rand.range(u8, 1, Move.size - 1 + 1));
227
+ if (opt.durations and durations(m)) continue :sample;
213
228
  if (opt.block and blocked(m)) continue :sample;
214
229
  for (0..i) |j| if (ms[j].id == m) continue :sample;
215
230
  break;
@@ -231,6 +246,7 @@ pub const Pokemon = struct {
231
246
  .stats = stats,
232
247
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
233
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
234
250
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
235
251
  else
236
252
  0,
@@ -247,6 +263,23 @@ fn blocked(m: Move) bool {
247
263
  };
248
264
  }
249
265
 
266
+ fn durations(m: Move) bool {
267
+ // Any moves that have effects that result in duration tracking with -Dchance
268
+ return switch (Move.get(m).effect) {
269
+ .Confusion,
270
+ .Bide,
271
+ .Sleep,
272
+ .Rage,
273
+ .Binding,
274
+ .Thrashing,
275
+ .ConfusionChance,
276
+ .Disable,
277
+ .Metronome,
278
+ => true,
279
+ else => false,
280
+ };
281
+ }
282
+
250
283
  /// Convenience helper to create a move-type choice for the provided move slot.
251
284
  pub fn move(slot: u4) Choice {
252
285
  return .{ .type = .Move, .data = slot };
@@ -257,385 +290,47 @@ pub fn swtch(slot: u4) Choice {
257
290
  return .{ .type = .Switch, .data = slot };
258
291
  }
259
292
 
260
- const NONE = @intFromEnum(Optional(bool).None);
261
-
262
- /// Helper functions that efficiently return valid ranges for various RNG events based on the
263
- /// state of an `Action` and other events to be used to construct a "transitions" function.
264
- pub const Rolls = struct {
265
- const PLAYER_NONE = [_]Optional(Player){.None};
266
- const PLAYERS = [_]Optional(Player){ .P1, .P2 };
267
-
268
- /// Returns a slice with the correct range of values for speed ties given the `action` state.
269
- pub inline fn speedTie(action: Action) []const Optional(Player) {
270
- return if (@field(action, "speed_tie") == .None) &PLAYER_NONE else &PLAYERS;
271
- }
272
-
273
- const BOOL_NONE = [_]Optional(bool){.None};
274
- const BOOLS = [_]Optional(bool){ .false, .true };
275
-
276
- /// Returns a slice with the correct range of values for hits given the `action` state
277
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
278
- pub inline fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
279
- if (parent == .true) return &BOOL_NONE;
280
- return if (@field(action, "hit") == .None) &BOOL_NONE else &BOOLS;
281
- }
282
-
283
- /// Returns a slice with the correct range of values for critical hits given the `action` state
284
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
285
- pub inline fn criticalHit(
286
- action: Action,
287
- parent: Optional(bool),
288
- ) []const Optional(bool) {
289
- if (parent == .false) return &BOOL_NONE;
290
- return if (@field(action, "critical_hit") == .None) &BOOL_NONE else &BOOLS;
291
- }
292
-
293
- /// Returns a slice with the correct range of values for secondary chances hits given the
294
- /// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
295
- pub inline fn secondaryChance(
296
- action: Action,
297
- parent: Optional(bool),
298
- ) []const Optional(bool) {
299
- if (parent == .false) return &BOOL_NONE;
300
- return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
301
- }
302
-
303
- /// The min and max bounds on iteration over damage rolls.
304
- pub const Range = struct { min: u9, max: u9 };
305
-
306
- /// Returns the range bounding damage rolls given the `action` state and the state of
307
- /// the `parent` (whether the player's Pokémon's move hit).
308
- pub inline fn damage(action: Action, parent: Optional(bool)) Range {
309
- return if (parent == .false or @field(action, "damage") == 0)
310
- .{ .min = 0, .max = 1 }
311
- else
312
- .{ .min = 217, .max = 256 };
313
- }
314
-
315
- /// Returns the max damage roll which will produce the same damage as `roll`
316
- /// given the base damage in `summaries`.
317
- pub inline fn coalesce(player: Player, roll: u8, summaries: *calc.Summaries, cap: bool) !u8 {
318
- if (roll == 0) return roll;
319
-
320
- const dmg = summaries.get(player.foe()).damage;
321
- if (dmg.base == 0 or (cap and dmg.capped)) return 255;
322
-
323
- // Closed form solution for max damage roll provided by Orion Taylor (taylorott)
324
- return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
325
- }
326
-
327
- /// Returns a slice with the correct range of values for confused given the `action` state
328
- /// and the state of the `parent` (the player's Pokémon's remaining confusion turns).
329
- pub inline fn confused(action: Action, parent: u4) []const Optional(bool) {
330
- return if (parent == 0 or @field(action, "confused") == .None) &BOOL_NONE else &BOOLS;
331
- }
332
-
333
- /// Returns a slice with the correct range of values for paralysis given the `action` state
334
- /// and the state of the `parent` (whether the player's Pokémon was confused).
335
- pub inline fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
336
- if (parent == .true) return &BOOL_NONE;
337
- return if (@field(action, "paralyzed") == .None) &BOOL_NONE else &BOOLS;
338
- }
339
-
340
- const DURATION_NONE = [_]u1{0};
341
- const DURATION = [_]u1{1};
342
-
343
- /// Returns a slice with a range of values for duration given the `action` state
344
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
345
- pub inline fn duration(action: Action, parent: Optional(bool)) []const u1 {
346
- if (parent == .false) return &DURATION_NONE;
347
- return if (@field(action, "duration") == 0) &DURATION_NONE else &DURATION;
348
- }
349
-
350
- const U3_NONE = [_]u3{0};
351
- const U3_FORCED = [_]u3{1};
352
-
353
- /// Returns a slice with a range of values for sleep given the `action` state.
354
- pub inline fn sleep(action: Action, origin: Action) []const u3 {
355
- const turns = @field(action.durations, "sleep");
356
- _ = origin;
357
- return if (turns < 1 or turns >= 7) &U3_NONE else &[_]u3{ 0, turns };
358
- }
359
-
360
- const DISABLE_NONE = [_]u4{0};
361
-
362
- /// Returns a slice with a range of values for disable given the `action` state
363
- /// and the state of the `parent` (the player's Pokémon's remaining sleep turns).
364
- pub inline fn disable(action: Action, origin: Action, parent: u4) []const u4 {
365
- const turns = @field(action.durations, "disable");
366
- _ = origin;
367
- return if (parent > 0 or turns < 1 or turns >= 8) &DISABLE_NONE else &[_]u4{ 0, turns };
368
- }
369
-
370
- /// Returns a slice with a range of values for confusion given the `action` state
371
- /// and the state of the `parent` (the player's remaining sleep turns).
372
- pub inline fn confusion(action: Action, origin: Action, parent: u4) []const u3 {
373
- const turns = @field(action.durations, "confusion");
374
- _ = origin;
375
- if (parent > 0 or turns < 1 or turns >= 5) return &U3_NONE;
376
- return if (turns < 2) &U3_FORCED else &[_]u3{ 0, turns };
377
- }
378
-
379
- /// Returns a slice with a range of values for attacking given the `action` state
380
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
381
- pub inline fn attacking(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
382
- const turns = @field(action.durations, "attacking");
383
- _ = origin;
384
- if (parent == .true or turns < 1 or turns >= 3) return &U3_NONE;
385
- return if (turns < 2) &U3_FORCED else &[_]u3{ 0, turns };
386
- }
387
-
388
- /// Returns a slice with a range of values for binding given the `action` state
389
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
390
- pub inline fn binding(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
391
- const turns = @field(action.durations, "binding");
392
- _ = origin;
393
- return if (parent == .true or turns < 1 or turns >= 4) &U3_NONE else &[_]u3{ 0, turns };
394
- }
395
-
396
- const SLOT_NONE = [_]u4{0};
397
- const SLOT = [_]u4{ 1, 2, 3, 4 };
398
-
399
- /// Returns a slice with a range of values for move slots given the `action` state
400
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
401
- ///
402
- /// These slots may or **may not be valid** as slots may be unset / have 0 PP.
403
- pub inline fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
404
- if (parent == .false) return &SLOT_NONE;
405
- return if (@field(action, "move_slot") == 0) &SLOT_NONE else &SLOT;
406
- }
407
-
408
- const MULTI_NONE = [_]u4{0};
409
- const MULTI = [_]u4{ 2, 3, 4, 5 };
410
-
411
- /// Returns a slice with the correct range of values for multi hit given the `action` state
412
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
413
- pub inline fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
414
- if (parent == .false) return &MULTI_NONE;
415
- return if (@field(action, "multi_hit") == 0) &MULTI_NONE else &MULTI;
416
- }
417
-
418
- const PSYWAVE_NONE = [_]u8{0};
419
- const PSYWAVE = init: {
420
- var rolls: [150]u8 = undefined;
421
- for (0..150) |i| rolls[i] = i + 1;
422
- break :init rolls;
423
- };
424
-
425
- /// Returns a slice with the correct range of values for psywave given the `action` state,
426
- /// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
427
- pub inline fn psywave(
428
- action: Action,
429
- side: *data.Side,
430
- parent: Optional(bool),
431
- ) []const u8 {
432
- if (parent == .false) return &PSYWAVE_NONE;
433
- return if (@field(action, "psywave") == 0)
434
- &PSYWAVE_NONE
435
- else
436
- PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
437
- }
438
-
439
- const MOVE_NONE = [_]Move{.None};
440
- const MOVES = init: {
441
- var moves: [Move.size - 2]Move = undefined;
442
- var i: usize = 0;
443
- for (@typeInfo(Move).Enum.fields) |f| {
444
- if (!(std.mem.eql(u8, f.name, "None") or
445
- std.mem.eql(u8, f.name, "Metronome") or
446
- std.mem.eql(u8, f.name, "Struggle") or
447
- std.mem.eql(u8, f.name, "SKIP_TURN")))
448
- {
449
- moves[i] = @field(Move, f.name);
450
- i += 1;
451
- }
293
+ /// Empirically determined maximum number of bytes possibly required to store the diff between two
294
+ /// Generation I battles.
295
+ pub const MAX_DIFFS: usize = 120;
296
+
297
+ /// The optimal size in bytes required to hold the diff between two Generation I battles.
298
+ /// At least as large as MAX_DIFFS.
299
+ pub const DIFFS_SIZE = if (builtin.mode == .ReleaseSmall)
300
+ MAX_DIFFS
301
+ else
302
+ std.math.ceilPowerOfTwo(usize, MAX_DIFFS) catch unreachable;
303
+
304
+ /// TODO
305
+ pub fn diff(
306
+ a: *const data.Battle(data.PRNG),
307
+ b: *const data.Battle(data.PRNG),
308
+ w: protocol.ByteStream.Writer,
309
+ ) !usize {
310
+ const pos = w.stream.pos;
311
+ const size = @sizeOf(data.Battle(data.PRNG)) - @sizeOf(data.PRNG);
312
+ assert(size % 2 == 0);
313
+ assert(size / 2 < 255);
314
+
315
+ var i: usize = 0;
316
+ while (i < size) : (i += 2) {
317
+ const val = util.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
318
+ if (val != util.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
319
+ try w.writeByte(@intCast(i / 2));
320
+ try w.writeInt(u16, val, endian);
452
321
  }
453
- break :init moves;
454
- };
455
-
456
- /// Returns a slice with the correct range of values for metronome given the `action` state.
457
- pub inline fn metronome(action: Action) []const Move {
458
- return if (@field(action, "metronome") == .None) &MOVE_NONE else &MOVES;
459
322
  }
460
- };
461
-
462
- test "Rolls.speedTie" {
463
- const actions = Actions{ .p1 = .{ .speed_tie = .P2 } };
464
- try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
465
- try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
466
- }
467
-
468
- test "Rolls.damage" {
469
- const actions = Actions{ .p2 = .{ .damage = 221 } };
470
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
471
- try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
472
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
473
- }
474
-
475
- test "Rolls.coalesce" {
476
- var summaries =
477
- calc.Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
478
- try expectEqual(@as(u8, 0), try Rolls.coalesce(.P2, 0, &summaries, false));
479
- try expectEqual(@as(u8, 241), try Rolls.coalesce(.P2, 238, &summaries, false));
480
- try expectEqual(@as(u8, 255), try Rolls.coalesce(.P2, 238, &summaries, true));
481
- summaries.p1.damage.final = 74;
482
- try expectEqual(@as(u8, 217), try Rolls.coalesce(.P2, 217, &summaries, false));
483
- try expectEqual(@as(u8, 255), try Rolls.coalesce(.P2, 217, &summaries, true));
484
- }
485
-
486
- test "Rolls.hit" {
487
- const actions = Actions{ .p2 = .{ .hit = .true } };
488
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
489
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
490
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
491
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
492
- }
493
-
494
- test "Rolls.secondaryChance" {
495
- const actions = Actions{ .p1 = .{ .secondary_chance = .true } };
496
- try expectEqualSlices(
497
- Optional(bool),
498
- &.{ .false, .true },
499
- Rolls.secondaryChance(actions.p1, .None),
500
- );
501
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
502
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
503
- }
504
-
505
- test "Rolls.criticalHit" {
506
- const actions = Actions{ .p1 = .{ .critical_hit = .true } };
507
- try expectEqualSlices(
508
- Optional(bool),
509
- &.{ .false, .true },
510
- Rolls.criticalHit(actions.p1, .None),
511
- );
512
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
513
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
514
- }
515
-
516
- test "Rolls.confused" {
517
- const actions = Actions{ .p2 = .{ .confused = .true } };
518
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, 1));
519
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2, 1));
520
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, 0));
521
- }
522
-
523
- test "Rolls.paralyzed" {
524
- const actions = Actions{ .p2 = .{ .paralyzed = .true } };
525
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
526
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
527
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
528
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
529
- }
530
-
531
- test "Rolls.duration" {
532
- const actions = Actions{ .p2 = .{ .duration = 3 } };
533
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p1, .None));
534
- try expectEqualSlices(u1, &.{1}, Rolls.duration(actions.p2, .None));
535
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p2, .false));
536
- }
537
-
538
- const FIXME: Action = .{};
539
-
540
- test "Rolls.sleep" {
541
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(.{ .durations = .{ .sleep = 0 } }, FIXME));
542
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(.{ .durations = .{ .sleep = 7 } }, FIXME));
543
- try expectEqualSlices(u3, &.{ 0, 4 }, Rolls.sleep(.{ .durations = .{ .sleep = 4 } }, FIXME));
544
- }
545
-
546
- test "Rolls.disable" {
547
- try expectEqualSlices(u4, &.{0}, Rolls.disable(.{ .durations = .{ .disable = 0 } }, FIXME, 0));
548
- try expectEqualSlices(u4, &.{0}, Rolls.disable(.{ .durations = .{ .disable = 8 } }, FIXME, 0));
549
- try expectEqualSlices(u4, &.{0}, Rolls.disable(.{ .durations = .{ .disable = 4 } }, FIXME, 1));
550
- try expectEqualSlices(u4, &.{ 0, 4 }, Rolls.disable(.{
551
- .durations = .{ .disable = 4 },
552
- }, FIXME, 0));
553
- }
554
323
 
555
- test "Rolls.confusion" {
556
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(.{
557
- .durations = .{ .confusion = 0 },
558
- }, FIXME, 0));
559
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(.{
560
- .durations = .{ .confusion = 5 },
561
- }, FIXME, 0));
562
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(.{
563
- .durations = .{ .confusion = 3 },
564
- }, FIXME, 1));
565
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(.{
566
- .durations = .{ .confusion = 1 },
567
- }, FIXME, 0));
568
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.confusion(.{
569
- .durations = .{ .confusion = 3 },
570
- }, FIXME, 0));
324
+ return w.stream.pos - pos;
571
325
  }
572
326
 
573
- test "Rolls.attacking" {
574
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(.{
575
- .durations = .{ .attacking = 0 },
576
- }, FIXME, .false));
577
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(.{
578
- .durations = .{ .attacking = 3 },
579
- }, FIXME, .false));
580
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(.{
581
- .durations = .{ .attacking = 2 },
582
- }, FIXME, .true));
583
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(.{
584
- .durations = .{ .attacking = 1 },
585
- }, FIXME, .false));
586
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.attacking(.{
587
- .durations = .{ .attacking = 2 },
588
- }, FIXME, .false));
589
- }
590
-
591
- test "Rolls.binding" {
592
- try expectEqualSlices(u3, &.{0}, Rolls.binding(.{
593
- .durations = .{ .binding = 0 },
594
- }, FIXME, .false));
595
- try expectEqualSlices(u3, &.{0}, Rolls.binding(.{
596
- .durations = .{ .binding = 4 },
597
- }, FIXME, .false));
598
- try expectEqualSlices(u3, &.{0}, Rolls.binding(.{
599
- .durations = .{ .binding = 2 },
600
- }, FIXME, .true));
601
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.binding(.{
602
- .durations = .{ .binding = 2 },
603
- }, FIXME, .false));
604
- }
605
-
606
- test "Rolls.moveSlot" {
607
- const actions = Actions{ .p2 = .{ .move_slot = 3 } };
608
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
609
- try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
610
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
611
- }
612
-
613
- test "Rolls.multiHit" {
614
- const actions = Actions{ .p2 = .{ .multi_hit = 3 } };
615
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
616
- try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
617
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
618
- }
619
-
620
- test "Rolls.metronome" {
621
- const actions = Actions{ .p2 = .{ .metronome = .Surf } };
622
- try expectEqualSlices(Move, &.{.None}, Rolls.metronome(actions.p1));
623
- try expectEqual(@as(Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
624
- }
625
-
626
- test "Rolls.psywave" {
627
- const actions = Actions{ .p2 = .{ .psywave = 79 } };
628
- var side = Side.init(&[_]Pokemon{.{
629
- .species = .Bulbasaur,
630
- .level = 100,
631
- .moves = &[_]Move{.Tackle},
632
- }});
633
-
634
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
635
- var rolls = Rolls.psywave(actions.p2, &side, .None);
636
- try expectEqual(@as(u8, 150), rolls[rolls.len - 1]);
637
- side.stored().level = 81;
638
- rolls = Rolls.psywave(actions.p2, &side, .None);
639
- try expectEqual(@as(u8, 121), rolls[rolls.len - 1]);
640
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
327
+ /// TODO
328
+ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
329
+ var i: usize = 0;
330
+ while (i < buf.len) : (i += 3) {
331
+ const off = @as(u16, @intCast(buf[i])) * 2;
332
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
333
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
334
+ std.mem.writeInt(u16, bytes, val, endian);
335
+ }
641
336
  }