@pkmn/engine 0.1.0-dev.6cc9e067 → 0.1.0-dev.6d566713

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.
@@ -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.
@@ -175,7 +169,7 @@ pub const Pokemon = struct {
175
169
  var pokemon = data.Pokemon{};
176
170
  pokemon.species = p.species;
177
171
  const species = Species.get(p.species);
178
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
172
+ inline for (@field(@typeInfo(@TypeOf(pokemon.stats)), @tagName(Struct)).fields) |field| {
179
173
  const hp = comptime std.mem.eql(u8, field.name, "hp");
180
174
  const spc =
181
175
  comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
@@ -211,7 +205,7 @@ pub const Pokemon = struct {
211
205
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
212
206
  var stats: Stats(u16) = .{};
213
207
  const dvs = DVs.random(rand);
214
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
208
+ inline for (@field(@typeInfo(@TypeOf(stats)), @tagName(Struct)).fields) |field| {
215
209
  @field(stats, field.name) = Stats(u16).calc(
216
210
  field.name,
217
211
  @field(species.stats, field.name),
@@ -230,6 +224,7 @@ pub const Pokemon = struct {
230
224
  var m: Move = .None;
231
225
  sample: while (true) {
232
226
  m = @enumFromInt(rand.range(u8, 1, Move.size - 1 + 1));
227
+ if (opt.durations and durations(m)) continue :sample;
233
228
  if (opt.block and blocked(m)) continue :sample;
234
229
  for (0..i) |j| if (ms[j].id == m) continue :sample;
235
230
  break;
@@ -251,6 +246,7 @@ pub const Pokemon = struct {
251
246
  .stats = stats,
252
247
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
253
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
254
250
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
255
251
  else
256
252
  0,
@@ -267,6 +263,23 @@ fn blocked(m: Move) bool {
267
263
  };
268
264
  }
269
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
+
270
283
  /// Convenience helper to create a move-type choice for the provided move slot.
271
284
  pub fn move(slot: u4) Choice {
272
285
  return .{ .type = .Move, .data = slot };
@@ -277,523 +290,47 @@ pub fn swtch(slot: u4) Choice {
277
290
  return .{ .type = .Switch, .data = slot };
278
291
  }
279
292
 
280
- const NONE = @intFromEnum(Optional(bool).None);
281
-
282
- /// Helper functions that efficiently return valid ranges for various RNG events based on the
283
- /// state of an `Action` and other events to be used to construct a "transitions" function.
284
- pub const Rolls = struct {
285
- const PLAYER_NONE = [_]Optional(Player){.None};
286
- const PLAYERS = [_]Optional(Player){ .P1, .P2 };
287
-
288
- /// Returns a slice with the correct range of values for speed ties given the `action` state.
289
- pub fn speedTie(action: Action) []const Optional(Player) {
290
- return if (@field(action, "speed_tie") == .None) &PLAYER_NONE else &PLAYERS;
291
- }
292
-
293
- const BOOL_NONE = [_]Optional(bool){.None};
294
- const BOOLS = [_]Optional(bool){ .false, .true };
295
-
296
- /// Returns a slice with the correct range of values for hits given the `action` state
297
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
298
- pub fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
299
- if (parent == .true) return &BOOL_NONE;
300
- return if (@field(action, "hit") == .None) &BOOL_NONE else &BOOLS;
301
- }
302
-
303
- /// Returns a slice with the correct range of values for critical hits given the `action` state
304
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
305
- pub fn criticalHit(
306
- action: Action,
307
- parent: Optional(bool),
308
- ) []const Optional(bool) {
309
- if (parent == .false) return &BOOL_NONE;
310
- return if (@field(action, "critical_hit") == .None) &BOOL_NONE else &BOOLS;
311
- }
312
-
313
- /// Returns a slice with the correct range of values for secondary chances hits given the
314
- /// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
315
- pub fn secondaryChance(
316
- action: Action,
317
- parent: Optional(bool),
318
- ) []const Optional(bool) {
319
- if (parent == .false) return &BOOL_NONE;
320
- return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
321
- }
322
-
323
- /// The min and max bounds on iteration over damage rolls.
324
- pub const Range = struct { min: u9, max: u9 };
325
-
326
- /// Returns the range bounding damage rolls given the `action` state and the state of
327
- /// the `parent` (whether the player's Pokémon's move hit).
328
- pub fn damage(action: Action, parent: Optional(bool)) Range {
329
- return if (parent == .false or @field(action, "damage") == 0)
330
- .{ .min = 0, .max = 1 }
331
- else
332
- .{ .min = 217, .max = 256 };
333
- }
334
-
335
- /// Returns the max damage roll which will produce the same damage as `roll`
336
- /// given the base damage in `summaries`.
337
- pub fn coalesce(player: Player, roll: u8, summaries: *calc.Summaries, cap: bool) !u8 {
338
- if (roll == 0) return roll;
339
-
340
- const dmg = summaries.get(player.foe()).damage;
341
- if (dmg.base == 0 or (cap and dmg.capped)) return 255;
342
-
343
- // Closed form solution for max damage roll provided by Orion Taylor (taylorott)
344
- return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
345
- }
346
-
347
- /// Returns a slice with the correct range of values for confused given the `action` state
348
- /// and the state of the `parent` (the player's Pokémon's remaining confusion turns).
349
- pub fn confused(action: Action, parent: u4) []const Optional(bool) {
350
- return if (parent == 0 or @field(action, "confused") == .None) &BOOL_NONE else &BOOLS;
351
- }
352
-
353
- /// Returns a slice with the correct range of values for paralysis given the `action` state
354
- /// and the state of the `parent` (whether the player's Pokémon was confused).
355
- pub fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
356
- if (parent == .true) return &BOOL_NONE;
357
- return if (@field(action, "paralyzed") == .None) &BOOL_NONE else &BOOLS;
358
- }
359
-
360
- const DURATION_NONE = [_]u1{0};
361
- const DURATION = [_]u1{1};
362
-
363
- /// Returns a slice with a range of values for duration given the `action` state
364
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
365
- pub fn duration(action: Action, parent: Optional(bool)) []const u1 {
366
- if (parent == .false) return &DURATION_NONE;
367
- return if (@field(action, "duration") == 0) &DURATION_NONE else &DURATION;
368
- }
369
-
370
- const U3_NONE = [_]u3{0};
371
- const U3_FORCED = [_]u3{1};
372
-
373
- /// Returns a slice with a range of values for sleep given the `action` and `origin` states.
374
- pub inline fn sleep(action: Action, origin: Action) []const u3 {
375
- const a = @field(action.durations, "sleep");
376
- const o = @field(origin.durations, "sleep");
377
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
378
- assert(a == 0 or a == o + 1);
379
- return if (o >= 7) &U3_NONE else &[_]u3{ 0, o + 1 };
380
- }
381
-
382
- const DISABLE_NONE = [_]u4{0};
383
-
384
- /// Returns a slice with a range of values for disable given the `action` and `origin` states
385
- /// and the state of the `parent` (the player's Pokémon's remaining sleep turns).
386
- pub inline fn disable(action: Action, origin: Action, parent: u4) []const u4 {
387
- if (parent > 0) return &DISABLE_NONE;
388
- const a = @field(action.durations, "disable");
389
- const o = @field(origin.durations, "disable");
390
- if (o == a) return if (o == 0) &DISABLE_NONE else &[_]u4{o};
391
- assert(a == 0 or a == o + 1);
392
- return if (o >= 8) &DISABLE_NONE else &[_]u4{ 0, o + 1 };
393
- }
394
-
395
- /// Returns a slice with a range of values for confusion given the `action` and `origin` states
396
- /// and the state of the `parent` (the player's remaining sleep turns).
397
- pub inline fn confusion(action: Action, origin: Action, parent: u4) []const u3 {
398
- if (parent > 0) return &U3_NONE;
399
- const a = @field(action.durations, "confusion");
400
- const o = @field(origin.durations, "confusion");
401
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
402
- assert(a == 0 or a == o + 1);
403
- return if (o >= 5) &U3_NONE else if (o < 2)
404
- if (a == 0) &U3_NONE else &U3_FORCED
405
- else
406
- &[_]u3{ 0, o + 1 };
407
- }
408
-
409
- /// Returns a slice with a range of values for attacking given the `action` and `origin` states
410
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
411
- pub inline fn attacking(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
412
- if (parent == .true) return &U3_NONE;
413
- const a = @field(action.durations, "attacking");
414
- const o = @field(origin.durations, "attacking");
415
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
416
- assert(a == 0 or a == o + 1);
417
- return if (o >= 3) &U3_NONE else if (o < 2)
418
- if (a == 0) &U3_NONE else &U3_FORCED
419
- else
420
- &[_]u3{ 0, o + 1 };
421
- }
422
-
423
- /// Returns a slice with a range of values for binding given the `action` and `origin` states
424
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
425
- pub inline fn binding(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
426
- if (parent == .true) return &U3_NONE;
427
- const a = @field(action.durations, "binding");
428
- const o = @field(origin.durations, "binding");
429
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
430
- assert(a == 0 or a == o + 1);
431
- return if (o >= 4) &U3_NONE else &[_]u3{ 0, o + 1 };
432
- }
433
-
434
- const SLOT_NONE = [_]u4{0};
435
- const SLOT = [_]u4{ 1, 2, 3, 4 };
436
-
437
- /// Returns a slice with a range of values for move slots given the `action` state
438
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
439
- ///
440
- /// These slots may or **may not be valid** as slots may be unset / have 0 PP.
441
- pub fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
442
- if (parent == .false) return &SLOT_NONE;
443
- return if (@field(action, "move_slot") == 0) &SLOT_NONE else &SLOT;
444
- }
445
-
446
- const MULTI_NONE = [_]u4{0};
447
- const MULTI = [_]u4{ 2, 3, 4, 5 };
448
-
449
- /// Returns a slice with the correct range of values for multi hit given the `action` state
450
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
451
- pub fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
452
- if (parent == .false) return &MULTI_NONE;
453
- return if (@field(action, "multi_hit") == 0) &MULTI_NONE else &MULTI;
454
- }
455
-
456
- const PSYWAVE_NONE = [_]u8{0};
457
- const PSYWAVE = init: {
458
- var rolls: [150]u8 = undefined;
459
- for (0..150) |i| rolls[i] = i + 1;
460
- break :init rolls;
461
- };
462
-
463
- /// Returns a slice with the correct range of values for psywave given the `action` state,
464
- /// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
465
- pub fn psywave(
466
- action: Action,
467
- side: *data.Side,
468
- parent: Optional(bool),
469
- ) []const u8 {
470
- if (parent == .false) return &PSYWAVE_NONE;
471
- return if (@field(action, "psywave") == 0)
472
- &PSYWAVE_NONE
473
- else
474
- PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
475
- }
476
-
477
- const MOVE_NONE = [_]Move{.None};
478
-
479
- /// Returns a slice with the correct range of values for metronome given the `action` state.
480
- pub fn metronome(action: Action) []const Move {
481
- return if (@field(action, "metronome") == .None) &MOVE_NONE else &Move.METRONOME;
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);
321
+ }
482
322
  }
483
- };
484
-
485
- test "Rolls.speedTie" {
486
- const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
487
- try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
488
- try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
489
- }
490
-
491
- test "Rolls.damage" {
492
- const actions: Actions = .{ .p2 = .{ .damage = 221 } };
493
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
494
- try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
495
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
496
- }
497
-
498
- test "Rolls.coalesce" {
499
- var summaries =
500
- calc.Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
501
- try expectEqual(0, try Rolls.coalesce(.P2, 0, &summaries, false));
502
- try expectEqual(241, try Rolls.coalesce(.P2, 238, &summaries, false));
503
- try expectEqual(255, try Rolls.coalesce(.P2, 238, &summaries, true));
504
- summaries.p1.damage.final = 74;
505
- try expectEqual(217, try Rolls.coalesce(.P2, 217, &summaries, false));
506
- try expectEqual(255, try Rolls.coalesce(.P2, 217, &summaries, true));
507
- }
508
-
509
- test "Rolls.hit" {
510
- const actions: Actions = .{ .p2 = .{ .hit = .true } };
511
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
512
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
513
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
514
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
515
- }
516
-
517
- test "Rolls.secondaryChance" {
518
- const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
519
- try expectEqualSlices(
520
- Optional(bool),
521
- &.{ .false, .true },
522
- Rolls.secondaryChance(actions.p1, .None),
523
- );
524
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
525
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
526
- }
527
-
528
- test "Rolls.criticalHit" {
529
- const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
530
- try expectEqualSlices(
531
- Optional(bool),
532
- &.{ .false, .true },
533
- Rolls.criticalHit(actions.p1, .None),
534
- );
535
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
536
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
537
- }
538
-
539
- test "Rolls.confused" {
540
- const actions: Actions = .{ .p2 = .{ .confused = .true } };
541
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, 1));
542
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2, 1));
543
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, 0));
544
- }
545
-
546
- test "Rolls.paralyzed" {
547
- const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
548
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
549
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
550
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
551
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
552
- }
553
-
554
- test "Rolls.duration" {
555
- const actions: Actions = .{ .p2 = .{ .duration = 3 } };
556
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p1, .None));
557
- try expectEqualSlices(u1, &.{1}, Rolls.duration(actions.p2, .None));
558
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p2, .false));
559
- }
560
-
561
- test "Rolls.sleep" {
562
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(
563
- .{ .durations = .{ .sleep = 0 } },
564
- .{ .durations = .{ .sleep = 0 } },
565
- ));
566
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.sleep(
567
- .{ .durations = .{ .sleep = 1 } },
568
- .{ .durations = .{ .sleep = 0 } },
569
- ));
570
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.sleep(
571
- .{ .durations = .{ .sleep = 0 } },
572
- .{ .durations = .{ .sleep = 1 } },
573
- ));
574
- try expectEqualSlices(u3, &.{1}, Rolls.sleep(
575
- .{ .durations = .{ .sleep = 1 } },
576
- .{ .durations = .{ .sleep = 1 } },
577
- ));
578
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.sleep(
579
- .{ .durations = .{ .sleep = 0 } },
580
- .{ .durations = .{ .sleep = 2 } },
581
- ));
582
- try expectEqualSlices(u3, &.{ 0, 7 }, Rolls.sleep(
583
- .{ .durations = .{ .sleep = 7 } },
584
- .{ .durations = .{ .sleep = 6 } },
585
- ));
586
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(
587
- .{ .durations = .{ .sleep = 0 } },
588
- .{ .durations = .{ .sleep = 7 } },
589
- ));
590
- }
591
-
592
- test "Rolls.disable" {
593
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
594
- .{ .durations = .{ .disable = 0 } },
595
- .{ .durations = .{ .disable = 0 } },
596
- 0,
597
- ));
598
- try expectEqualSlices(u4, &.{ 0, 1 }, Rolls.disable(
599
- .{ .durations = .{ .disable = 1 } },
600
- .{ .durations = .{ .disable = 0 } },
601
- 0,
602
- ));
603
- try expectEqualSlices(u4, &.{ 0, 2 }, Rolls.disable(
604
- .{ .durations = .{ .disable = 0 } },
605
- .{ .durations = .{ .disable = 1 } },
606
- 0,
607
- ));
608
- try expectEqualSlices(u4, &.{1}, Rolls.disable(
609
- .{ .durations = .{ .disable = 1 } },
610
- .{ .durations = .{ .disable = 1 } },
611
- 0,
612
- ));
613
- try expectEqualSlices(u4, &.{ 0, 3 }, Rolls.disable(
614
- .{ .durations = .{ .disable = 0 } },
615
- .{ .durations = .{ .disable = 2 } },
616
- 0,
617
- ));
618
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
619
- .{ .durations = .{ .disable = 0 } },
620
- .{ .durations = .{ .disable = 2 } },
621
- 1,
622
- ));
623
- try expectEqualSlices(u4, &.{ 0, 8 }, Rolls.disable(
624
- .{ .durations = .{ .disable = 8 } },
625
- .{ .durations = .{ .disable = 7 } },
626
- 0,
627
- ));
628
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
629
- .{ .durations = .{ .disable = 0 } },
630
- .{ .durations = .{ .disable = 8 } },
631
- 0,
632
- ));
633
- }
634
323
 
635
- test "Rolls.confusion" {
636
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
637
- .{ .durations = .{ .confusion = 0 } },
638
- .{ .durations = .{ .confusion = 0 } },
639
- 0,
640
- ));
641
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(
642
- .{ .durations = .{ .confusion = 1 } },
643
- .{ .durations = .{ .confusion = 0 } },
644
- 0,
645
- ));
646
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
647
- .{ .durations = .{ .confusion = 0 } },
648
- .{ .durations = .{ .confusion = 1 } },
649
- 0,
650
- ));
651
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(
652
- .{ .durations = .{ .confusion = 1 } },
653
- .{ .durations = .{ .confusion = 1 } },
654
- 0,
655
- ));
656
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.confusion(
657
- .{ .durations = .{ .confusion = 0 } },
658
- .{ .durations = .{ .confusion = 2 } },
659
- 0,
660
- ));
661
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
662
- .{ .durations = .{ .confusion = 0 } },
663
- .{ .durations = .{ .confusion = 2 } },
664
- 1,
665
- ));
666
- try expectEqualSlices(u3, &.{ 0, 5 }, Rolls.confusion(
667
- .{ .durations = .{ .confusion = 5 } },
668
- .{ .durations = .{ .confusion = 4 } },
669
- 0,
670
- ));
671
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
672
- .{ .durations = .{ .confusion = 0 } },
673
- .{ .durations = .{ .confusion = 5 } },
674
- 0,
675
- ));
324
+ return w.stream.pos - pos;
676
325
  }
677
326
 
678
- test "Rolls.attacking" {
679
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
680
- .{ .durations = .{ .attacking = 0 } },
681
- .{ .durations = .{ .attacking = 0 } },
682
- .false,
683
- ));
684
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
685
- .{ .durations = .{ .attacking = 1 } },
686
- .{ .durations = .{ .attacking = 0 } },
687
- .false,
688
- ));
689
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
690
- .{ .durations = .{ .attacking = 0 } },
691
- .{ .durations = .{ .attacking = 1 } },
692
- .false,
693
- ));
694
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
695
- .{ .durations = .{ .attacking = 1 } },
696
- .{ .durations = .{ .attacking = 1 } },
697
- .false,
698
- ));
699
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
700
- .{ .durations = .{ .attacking = 0 } },
701
- .{ .durations = .{ .attacking = 2 } },
702
- .false,
703
- ));
704
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
705
- .{ .durations = .{ .attacking = 0 } },
706
- .{ .durations = .{ .attacking = 2 } },
707
- .true,
708
- ));
709
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
710
- .{ .durations = .{ .attacking = 3 } },
711
- .{ .durations = .{ .attacking = 2 } },
712
- .false,
713
- ));
714
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
715
- .{ .durations = .{ .attacking = 0 } },
716
- .{ .durations = .{ .attacking = 3 } },
717
- .false,
718
- ));
719
- }
720
-
721
- test "Rolls.binding" {
722
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
723
- .{ .durations = .{ .binding = 0 } },
724
- .{ .durations = .{ .binding = 0 } },
725
- .false,
726
- ));
727
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.binding(
728
- .{ .durations = .{ .binding = 1 } },
729
- .{ .durations = .{ .binding = 0 } },
730
- .false,
731
- ));
732
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.binding(
733
- .{ .durations = .{ .binding = 0 } },
734
- .{ .durations = .{ .binding = 1 } },
735
- .false,
736
- ));
737
- try expectEqualSlices(u3, &.{1}, Rolls.binding(
738
- .{ .durations = .{ .binding = 1 } },
739
- .{ .durations = .{ .binding = 1 } },
740
- .false,
741
- ));
742
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.binding(
743
- .{ .durations = .{ .binding = 0 } },
744
- .{ .durations = .{ .binding = 2 } },
745
- .false,
746
- ));
747
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
748
- .{ .durations = .{ .binding = 0 } },
749
- .{ .durations = .{ .binding = 2 } },
750
- .true,
751
- ));
752
- try expectEqualSlices(u3, &.{ 0, 4 }, Rolls.binding(
753
- .{ .durations = .{ .binding = 4 } },
754
- .{ .durations = .{ .binding = 3 } },
755
- .false,
756
- ));
757
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
758
- .{ .durations = .{ .binding = 0 } },
759
- .{ .durations = .{ .binding = 4 } },
760
- .false,
761
- ));
762
- }
763
-
764
- test "Rolls.moveSlot" {
765
- const actions: Actions = .{ .p2 = .{ .move_slot = 3 } };
766
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
767
- try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
768
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
769
- }
770
-
771
- test "Rolls.multiHit" {
772
- const actions: Actions = .{ .p2 = .{ .multi_hit = 3 } };
773
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
774
- try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
775
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
776
- }
777
-
778
- test "Rolls.metronome" {
779
- const actions: Actions = .{ .p2 = .{ .metronome = .Surf } };
780
- try expectEqualSlices(Move, &.{.None}, Rolls.metronome(actions.p1));
781
- try expectEqual(@as(Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
782
- }
783
-
784
- test "Rolls.psywave" {
785
- const actions: Actions = .{ .p2 = .{ .psywave = 79 } };
786
- var side = Side.init(&[_]Pokemon{.{
787
- .species = .Bulbasaur,
788
- .level = 100,
789
- .moves = &[_]Move{.Tackle},
790
- }});
791
-
792
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
793
- var rolls = Rolls.psywave(actions.p2, &side, .None);
794
- try expectEqual(150, rolls[rolls.len - 1]);
795
- side.stored().level = 81;
796
- rolls = Rolls.psywave(actions.p2, &side, .None);
797
- try expectEqual(121, rolls[rolls.len - 1]);
798
- 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
+ }
799
336
  }