@pkmn/engine 0.1.0-dev.abd68573 → 0.1.0-dev.ac652f4e

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 (79) hide show
  1. package/README.md +17 -12
  2. package/build/pkg/addon/browser.d.ts +3 -0
  3. package/build/pkg/addon/browser.js +47 -0
  4. package/build/pkg/addon/browser.js.map +1 -0
  5. package/build/pkg/addon/node.d.ts +3 -0
  6. package/build/pkg/addon/node.js +110 -0
  7. package/build/pkg/addon/node.js.map +1 -0
  8. package/build/pkg/addon/wasm.d.ts +2 -0
  9. package/build/pkg/addon/wasm.js +53 -0
  10. package/build/pkg/addon/wasm.js.map +1 -0
  11. package/build/pkg/addon.d.ts +25 -3
  12. package/build/pkg/addon.js +34 -65
  13. package/build/pkg/addon.js.map +1 -1
  14. package/build/pkg/data/layout.json +15 -2
  15. package/build/pkg/data/protocol.json +23 -9
  16. package/build/pkg/data.d.ts +23 -1
  17. package/build/pkg/data.js.map +1 -1
  18. package/build/pkg/gen1.d.ts +15 -18
  19. package/build/pkg/gen1.js +28 -19
  20. package/build/pkg/gen1.js.map +1 -1
  21. package/build/pkg/index.d.ts +40 -90
  22. package/build/pkg/index.js +23 -70
  23. package/build/pkg/index.js.map +1 -1
  24. package/build/pkg/protocol.d.ts +20 -1
  25. package/build/pkg/protocol.js +105 -103
  26. package/build/pkg/protocol.js.map +1 -1
  27. package/build/tools/debug.js +52 -76
  28. package/build/tools/debug.js.map +1 -1
  29. package/build/tools/display/debug.jsx +152 -0
  30. package/build/tools/display/debug.jsx.map +1 -0
  31. package/build/tools/display/index.js +152 -0
  32. package/build/tools/display/index.js.map +1 -0
  33. package/build/tools/display/ui.jsx +359 -0
  34. package/build/tools/display/ui.jsx.map +1 -0
  35. package/build/tools/display/util.js +33 -0
  36. package/build/tools/display/util.js.map +1 -0
  37. package/build.zig +307 -78
  38. package/package.json +54 -135
  39. package/src/bin/install-pkmn-engine +10 -7
  40. package/src/bin/pkmn-debug +1 -1
  41. package/src/lib/bindings/c.zig +255 -0
  42. package/src/lib/bindings/node.zig +121 -0
  43. package/src/lib/bindings/wasm.zig +59 -0
  44. package/src/lib/common/array.zig +77 -0
  45. package/src/lib/common/data.zig +8 -8
  46. package/src/lib/common/debug.zig +39 -8
  47. package/src/lib/common/js.zig +4 -2
  48. package/src/lib/common/optional.zig +13 -9
  49. package/src/lib/common/options.zig +6 -0
  50. package/src/lib/common/protocol.zig +45 -44
  51. package/src/lib/common/rational.zig +323 -158
  52. package/src/lib/common/rng.zig +5 -7
  53. package/src/lib/common/util.zig +60 -1
  54. package/src/lib/gen1/calc.zig +693 -157
  55. package/src/lib/gen1/chance.zig +415 -291
  56. package/src/lib/gen1/data/moves.zig +2 -4
  57. package/src/lib/gen1/data/species.zig +1 -3
  58. package/src/lib/gen1/data/types.zig +1 -2
  59. package/src/lib/gen1/data.zig +38 -41
  60. package/src/lib/gen1/helpers.zig +64 -540
  61. package/src/lib/gen1/mechanics.zig +332 -244
  62. package/src/lib/gen2/calc.zig +37 -27
  63. package/src/lib/gen2/chance.zig +90 -503
  64. package/src/lib/gen2/data/items.zig +1 -3
  65. package/src/lib/gen2/data/moves.zig +2 -4
  66. package/src/lib/gen2/data/species.zig +1 -3
  67. package/src/lib/gen2/data/types.zig +1 -2
  68. package/src/lib/gen2/data.zig +26 -29
  69. package/src/lib/gen2/generated.zig +18 -23
  70. package/src/lib/gen2/helpers.zig +46 -25
  71. package/src/lib/gen2/mechanics.zig +88 -115
  72. package/src/lib/node.zig +3 -116
  73. package/src/lib/pkmn.zig +14 -7
  74. package/src/lib/wasm.zig +13 -9
  75. package/src/tools/display/dom.ts +93 -0
  76. package/src/tools/display/ui.css +354 -0
  77. package/build/test/display.js +0 -400
  78. package/build/test/display.js.map +0 -1
  79. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -1,39 +1,25 @@
1
- const std = @import("std");
2
-
1
+ const builtin = @import("builtin");
3
2
  const common = @import("../common/data.zig");
4
- const optional = @import("../common/optional.zig");
3
+ const data = @import("data.zig");
5
4
  const options = @import("../common/options.zig");
6
5
  const protocol = @import("../common/protocol.zig");
7
- const rational = @import("../common/rational.zig");
8
6
  const rng = @import("../common/rng.zig");
9
-
10
- const calc = @import("calc.zig");
11
- const chance = @import("chance.zig");
12
- const data = @import("data.zig");
7
+ const std = @import("std");
8
+ const util = @import("../common/util.zig");
13
9
 
14
10
  const assert = std.debug.assert;
15
-
16
- const expectEqual = std.testing.expectEqual;
17
- const expectEqualSlices = std.testing.expectEqualSlices;
18
-
19
11
  const Choice = common.Choice;
20
- const Player = common.Player;
21
-
22
- const Optional = optional.Optional;
23
-
24
- const showdown = options.showdown;
25
-
26
- const PSRNG = rng.PSRNG;
27
-
28
12
  const DVs = data.DVs;
29
13
  const Move = data.Move;
30
14
  const MoveSlot = data.MoveSlot;
15
+ const PSRNG = rng.PSRNG;
16
+ const showdown = options.showdown;
31
17
  const Species = data.Species;
32
18
  const Stats = data.Stats;
33
- const Status = data.Status;
34
19
 
35
- const Action = chance.Action;
36
- const Actions = chance.Actions;
20
+ const endian = builtin.cpu.arch.endian();
21
+
22
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
37
23
 
38
24
  /// Configuration options controlling the domain of valid Pokémon that can be generated by the
39
25
  /// random generator helpers.
@@ -43,6 +29,8 @@ pub const Options = struct {
43
29
  /// Whether to generate Pokémon with moves that contain bugs on Pokémon Showdown that are
44
30
  /// unimplementable in the engine.
45
31
  block: bool = showdown,
32
+ // FIXME: remove once durations work with -Dchance
33
+ durations: bool = false,
46
34
  };
47
35
 
48
36
  /// Helpers to simplify initialization of a Generation I battle.
@@ -175,7 +163,7 @@ pub const Pokemon = struct {
175
163
  var pokemon = data.Pokemon{};
176
164
  pokemon.species = p.species;
177
165
  const species = Species.get(p.species);
178
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
166
+ inline for (@field(@typeInfo(@TypeOf(pokemon.stats)), @tagName(Struct)).fields) |field| {
179
167
  const hp = comptime std.mem.eql(u8, field.name, "hp");
180
168
  const spc =
181
169
  comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
@@ -211,7 +199,7 @@ pub const Pokemon = struct {
211
199
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
212
200
  var stats: Stats(u16) = .{};
213
201
  const dvs = DVs.random(rand);
214
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
202
+ inline for (@field(@typeInfo(@TypeOf(stats)), @tagName(Struct)).fields) |field| {
215
203
  @field(stats, field.name) = Stats(u16).calc(
216
204
  field.name,
217
205
  @field(species.stats, field.name),
@@ -230,6 +218,7 @@ pub const Pokemon = struct {
230
218
  var m: Move = .None;
231
219
  sample: while (true) {
232
220
  m = @enumFromInt(rand.range(u8, 1, Move.size - 1 + 1));
221
+ if (opt.durations and durations(m)) continue :sample;
233
222
  if (opt.block and blocked(m)) continue :sample;
234
223
  for (0..i) |j| if (ms[j].id == m) continue :sample;
235
224
  break;
@@ -251,6 +240,7 @@ pub const Pokemon = struct {
251
240
  .stats = stats,
252
241
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
253
242
  .status = if (!opt.cleric and rand.chance(u8, 1, 6 + 1))
243
+ // NB: This will only assign sleep as 2 or 4 when rolled
254
244
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
255
245
  else
256
246
  0,
@@ -267,6 +257,16 @@ fn blocked(m: Move) bool {
267
257
  };
268
258
  }
269
259
 
260
+ fn durations(m: Move) bool {
261
+ // Any moves that have effects that result in duration tracking with -Dchance
262
+ return switch (Move.get(m).effect) {
263
+ .Haze,
264
+ .Metronome,
265
+ => true,
266
+ else => false,
267
+ };
268
+ }
269
+
270
270
  /// Convenience helper to create a move-type choice for the provided move slot.
271
271
  pub fn move(slot: u4) Choice {
272
272
  return .{ .type = .Move, .data = slot };
@@ -277,523 +277,47 @@ pub fn swtch(slot: u4) Choice {
277
277
  return .{ .type = .Switch, .data = slot };
278
278
  }
279
279
 
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;
280
+ /// Empirically determined maximum number of bytes possibly required to store the diff between two
281
+ /// Generation I battles.
282
+ pub const MAX_DIFFS: usize = 120;
283
+
284
+ /// The optimal size in bytes required to hold the diff between two Generation I battles.
285
+ /// At least as large as MAX_DIFFS.
286
+ pub const DIFFS_SIZE = if (builtin.mode == .ReleaseSmall)
287
+ MAX_DIFFS
288
+ else
289
+ std.math.ceilPowerOfTwo(usize, MAX_DIFFS) catch unreachable;
290
+
291
+ /// TODO
292
+ pub fn diff(
293
+ a: *const data.Battle(data.PRNG),
294
+ b: *const data.Battle(data.PRNG),
295
+ w: protocol.ByteStream.Writer,
296
+ ) !usize {
297
+ const pos = w.stream.pos;
298
+ const size = @sizeOf(data.Battle(data.PRNG)) - @sizeOf(data.PRNG);
299
+ assert(size % 2 == 0);
300
+ assert(size / 2 < 255);
301
+
302
+ var i: usize = 0;
303
+ while (i < size) : (i += 2) {
304
+ const val = util.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
305
+ if (val != util.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
306
+ try w.writeByte(@intCast(i / 2));
307
+ try w.writeInt(u16, val, endian);
308
+ }
482
309
  }
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
310
 
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));
311
+ return w.stream.pos - pos;
526
312
  }
527
313
 
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
-
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
- ));
676
- }
677
-
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));
314
+ /// TODO
315
+ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
316
+ var i: usize = 0;
317
+ while (i < buf.len) : (i += 3) {
318
+ const off = @as(u16, @intCast(buf[i])) * 2;
319
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
320
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
321
+ std.mem.writeInt(u16, bytes, val, endian);
322
+ }
799
323
  }