@pkmn/engine 0.1.0-dev.f3aaee39 → 0.1.0-dev.f4ba6ebd

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 (56) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +46 -19
  3. package/build/pkg/addon.js +6 -7
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/ids.json +12 -12
  6. package/build/pkg/data/layout.json +2 -2
  7. package/build/pkg/data/protocol.json +69 -12
  8. package/build/pkg/data.d.ts +4 -4
  9. package/build/pkg/data.js.map +1 -1
  10. package/build/pkg/gen1.js +2 -2
  11. package/build/pkg/gen1.js.map +1 -1
  12. package/build/pkg/index.d.ts +3 -2
  13. package/build/pkg/index.js.map +1 -1
  14. package/build/pkg/protocol.js +61 -42
  15. package/build/pkg/protocol.js.map +1 -1
  16. package/build/test/display.js +8 -3
  17. package/build/test/display.js.map +1 -1
  18. package/build/tools/debug.js +5 -4
  19. package/build/tools/debug.js.map +1 -1
  20. package/build.zig +227 -96
  21. package/package.json +46 -23
  22. package/src/bin/install-pkmn-engine +29 -19
  23. package/src/lib/common/array.zig +77 -0
  24. package/src/lib/common/data.zig +12 -24
  25. package/src/lib/common/debug.zig +2 -2
  26. package/src/lib/common/js.zig +4 -2
  27. package/src/lib/common/optional.zig +21 -10
  28. package/src/lib/common/options.zig +2 -0
  29. package/src/lib/common/protocol.zig +807 -327
  30. package/src/lib/common/rational.zig +125 -31
  31. package/src/lib/common/rng.zig +22 -13
  32. package/src/lib/common/util.zig +50 -1
  33. package/src/lib/gen1/calc.zig +514 -135
  34. package/src/lib/gen1/chance.zig +407 -284
  35. package/src/lib/gen1/data/moves.zig +192 -180
  36. package/src/lib/gen1/data/species.zig +3 -3
  37. package/src/lib/gen1/data/types.zig +7 -7
  38. package/src/lib/gen1/data.zig +46 -57
  39. package/src/lib/gen1/helpers.zig +95 -550
  40. package/src/lib/gen1/mechanics.zig +571 -516
  41. package/src/lib/gen2/calc.zig +161 -0
  42. package/src/lib/gen2/chance.zig +905 -0
  43. package/src/lib/gen2/data/items.zig +12 -18
  44. package/src/lib/gen2/data/moves.zig +663 -569
  45. package/src/lib/gen2/data/species.zig +1 -1
  46. package/src/lib/gen2/data/types.zig +134 -31
  47. package/src/lib/gen2/data.zig +107 -79
  48. package/src/lib/gen2/generated.zig +806 -0
  49. package/src/lib/gen2/helpers.zig +49 -21
  50. package/src/lib/gen2/mechanics.zig +3563 -9
  51. package/src/lib/{binding/node.zig → node.zig} +10 -13
  52. package/src/lib/pkmn.zig +32 -15
  53. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
  54. package/src/test/display/pkmn.html.tmpl +5 -5
  55. package/src/lib/gen1/README.md +0 -892
  56. package/src/lib/gen2/README.md +0 -308
@@ -1,27 +1,21 @@
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
15
  const Player = common.Player;
21
16
 
22
- const Optional = optional.Optional;
23
-
24
17
  const showdown = options.showdown;
18
+ const chance = options.chance;
25
19
 
26
20
  const PSRNG = rng.PSRNG;
27
21
 
@@ -32,8 +26,9 @@ const Species = data.Species;
32
26
  const Stats = data.Stats;
33
27
  const Status = data.Status;
34
28
 
35
- const Action = chance.Action;
36
- const Actions = chance.Actions;
29
+ const endian = builtin.cpu.arch.endian();
30
+
31
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
37
32
 
38
33
  /// Configuration options controlling the domain of valid Pokémon that can be generated by the
39
34
  /// random generator helpers.
@@ -43,6 +38,8 @@ pub const Options = struct {
43
38
  /// Whether to generate Pokémon with moves that contain bugs on Pokémon Showdown that are
44
39
  /// unimplementable in the engine.
45
40
  block: bool = showdown,
41
+ // FIXME: remove once durations work with -Dchance
42
+ durations: bool = false,
46
43
  };
47
44
 
48
45
  /// Helpers to simplify initialization of a Generation I battle.
@@ -86,18 +83,38 @@ pub const Battle = struct {
86
83
  };
87
84
 
88
85
  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
86
  return .{
92
87
  .src = .{
93
88
  .seed = if (showdown)
94
89
  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
- },
90
+ else seed: {
91
+ // GLITCH: The initial RNG seed in Gen I link battles is heavily constrained
92
+ // https://www.smogon.com/forums/threads/rng-in-rby-link-battles.3746779
93
+ var seed = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0 };
94
+
95
+ // Bytes in seed can only range from 0-252, not 0-255
96
+ const max: u8 = 253;
97
+ // Instructions take multiples of 4 cycles to execute
98
+ var timer: u16 = 4 * rand.range(u16, 0, 16384);
99
+ var add: u8 = rand.range(u8, 0, 256);
100
+ var carry: u8 = 0;
101
+
102
+ var i: u8 = 0;
103
+ while (i < seed.len) {
104
+ const div: u8 = @intCast(timer >> 8);
105
+ add +%= div +% carry;
106
+ if (add < max) {
107
+ seed[i] = add;
108
+ i += 1;
109
+ timer +%= 472;
110
+ carry = 1;
111
+ } else {
112
+ timer +%= 452;
113
+ carry = 0;
114
+ }
115
+ }
116
+ break :seed seed;
117
+ },
101
118
  },
102
119
  };
103
120
  }
@@ -155,7 +172,7 @@ pub const Pokemon = struct {
155
172
  var pokemon = data.Pokemon{};
156
173
  pokemon.species = p.species;
157
174
  const species = Species.get(p.species);
158
- inline for (@typeInfo(@TypeOf(pokemon.stats)).Struct.fields) |field| {
175
+ inline for (@field(@typeInfo(@TypeOf(pokemon.stats)), @tagName(Struct)).fields) |field| {
159
176
  const hp = comptime std.mem.eql(u8, field.name, "hp");
160
177
  const spc =
161
178
  comptime std.mem.eql(u8, field.name, "spa") or std.mem.eql(u8, field.name, "spd");
@@ -191,7 +208,7 @@ pub const Pokemon = struct {
191
208
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
192
209
  var stats: Stats(u16) = .{};
193
210
  const dvs = DVs.random(rand);
194
- inline for (@typeInfo(@TypeOf(stats)).Struct.fields) |field| {
211
+ inline for (@field(@typeInfo(@TypeOf(stats)), @tagName(Struct)).fields) |field| {
195
212
  @field(stats, field.name) = Stats(u16).calc(
196
213
  field.name,
197
214
  @field(species.stats, field.name),
@@ -210,6 +227,7 @@ pub const Pokemon = struct {
210
227
  var m: Move = .None;
211
228
  sample: while (true) {
212
229
  m = @enumFromInt(rand.range(u8, 1, Move.size - 1 + 1));
230
+ if (opt.durations and durations(m)) continue :sample;
213
231
  if (opt.block and blocked(m)) continue :sample;
214
232
  for (0..i) |j| if (ms[j].id == m) continue :sample;
215
233
  break;
@@ -231,6 +249,7 @@ pub const Pokemon = struct {
231
249
  .stats = stats,
232
250
  .hp = if (opt.cleric) stats.hp else rand.range(u16, 0, stats.hp + 1),
233
251
  .status = if (!opt.cleric and rand.chance(u8, 1, 6 + 1))
252
+ // NB: This will only assign sleep as 2 or 4 when rolled
234
253
  0 | (@as(u8, 1) << rand.range(u3, 1, 6 + 1))
235
254
  else
236
255
  0,
@@ -247,6 +266,23 @@ fn blocked(m: Move) bool {
247
266
  };
248
267
  }
249
268
 
269
+ fn durations(m: Move) bool {
270
+ // Any moves that have effects that result in duration tracking with -Dchance
271
+ return switch (Move.get(m).effect) {
272
+ .Confusion,
273
+ .Bide,
274
+ .Sleep,
275
+ .Rage,
276
+ .Binding,
277
+ .Thrashing,
278
+ .ConfusionChance,
279
+ .Disable,
280
+ .Metronome,
281
+ => true,
282
+ else => false,
283
+ };
284
+ }
285
+
250
286
  /// Convenience helper to create a move-type choice for the provided move slot.
251
287
  pub fn move(slot: u4) Choice {
252
288
  return .{ .type = .Move, .data = slot };
@@ -257,538 +293,47 @@ pub fn swtch(slot: u4) Choice {
257
293
  return .{ .type = .Switch, .data = slot };
258
294
  }
259
295
 
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` and `origin` states.
354
- pub inline fn sleep(action: Action, origin: Action) []const u3 {
355
- const a = @field(action.durations, "sleep");
356
- const o = @field(origin.durations, "sleep");
357
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
358
- assert(a == 0 or a == o + 1);
359
- return if (o >= 7) &U3_NONE else &[_]u3{ 0, o + 1 };
360
- }
361
-
362
- const DISABLE_NONE = [_]u4{0};
363
-
364
- /// Returns a slice with a range of values for disable given the `action` and `origin` states
365
- /// and the state of the `parent` (the player's Pokémon's remaining sleep turns).
366
- pub inline fn disable(action: Action, origin: Action, parent: u4) []const u4 {
367
- if (parent > 0) return &DISABLE_NONE;
368
- const a = @field(action.durations, "disable");
369
- const o = @field(origin.durations, "disable");
370
- if (o == a) return if (o == 0) &DISABLE_NONE else &[_]u4{o};
371
- assert(a == 0 or a == o + 1);
372
- return if (o >= 8) &DISABLE_NONE else &[_]u4{ 0, o + 1 };
373
- }
374
-
375
- /// Returns a slice with a range of values for confusion given the `action` and `origin` states
376
- /// and the state of the `parent` (the player's remaining sleep turns).
377
- pub inline fn confusion(action: Action, origin: Action, parent: u4) []const u3 {
378
- if (parent > 0) return &U3_NONE;
379
- const a = @field(action.durations, "confusion");
380
- const o = @field(origin.durations, "confusion");
381
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
382
- assert(a == 0 or a == o + 1);
383
- return if (o >= 5) &U3_NONE else if (o < 2)
384
- if (a == 0) &U3_NONE else &U3_FORCED
385
- else
386
- &[_]u3{ 0, o + 1 };
387
- }
388
-
389
- /// Returns a slice with a range of values for attacking given the `action` and `origin` states
390
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
391
- pub inline fn attacking(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
392
- if (parent == .true) return &U3_NONE;
393
- const a = @field(action.durations, "attacking");
394
- const o = @field(origin.durations, "attacking");
395
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
396
- assert(a == 0 or a == o + 1);
397
- return if (o >= 3) &U3_NONE else if (o < 2)
398
- if (a == 0) &U3_NONE else &U3_FORCED
399
- else
400
- &[_]u3{ 0, o + 1 };
401
- }
402
-
403
- /// Returns a slice with a range of values for binding given the `action` and `origin` states
404
- /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
405
- pub inline fn binding(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
406
- if (parent == .true) return &U3_NONE;
407
- const a = @field(action.durations, "binding");
408
- const o = @field(origin.durations, "binding");
409
- if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
410
- assert(a == 0 or a == o + 1);
411
- return if (o >= 4) &U3_NONE else &[_]u3{ 0, o + 1 };
412
- }
413
-
414
- const SLOT_NONE = [_]u4{0};
415
- const SLOT = [_]u4{ 1, 2, 3, 4 };
416
-
417
- /// Returns a slice with a range of values for move slots given the `action` state
418
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
419
- ///
420
- /// These slots may or **may not be valid** as slots may be unset / have 0 PP.
421
- pub inline fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
422
- if (parent == .false) return &SLOT_NONE;
423
- return if (@field(action, "move_slot") == 0) &SLOT_NONE else &SLOT;
424
- }
425
-
426
- const MULTI_NONE = [_]u4{0};
427
- const MULTI = [_]u4{ 2, 3, 4, 5 };
428
-
429
- /// Returns a slice with the correct range of values for multi hit given the `action` state
430
- /// and the state of the `parent` (whether the player's Pokémon's move hit).
431
- pub inline fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
432
- if (parent == .false) return &MULTI_NONE;
433
- return if (@field(action, "multi_hit") == 0) &MULTI_NONE else &MULTI;
434
- }
435
-
436
- const PSYWAVE_NONE = [_]u8{0};
437
- const PSYWAVE = init: {
438
- var rolls: [150]u8 = undefined;
439
- for (0..150) |i| rolls[i] = i + 1;
440
- break :init rolls;
441
- };
442
-
443
- /// Returns a slice with the correct range of values for psywave given the `action` state,
444
- /// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
445
- pub inline fn psywave(
446
- action: Action,
447
- side: *data.Side,
448
- parent: Optional(bool),
449
- ) []const u8 {
450
- if (parent == .false) return &PSYWAVE_NONE;
451
- return if (@field(action, "psywave") == 0)
452
- &PSYWAVE_NONE
453
- else
454
- PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
455
- }
456
-
457
- const MOVE_NONE = [_]Move{.None};
458
- const MOVES = init: {
459
- var moves: [Move.size - 2]Move = undefined;
460
- var i: usize = 0;
461
- for (@typeInfo(Move).Enum.fields) |f| {
462
- if (!(std.mem.eql(u8, f.name, "None") or
463
- std.mem.eql(u8, f.name, "Metronome") or
464
- std.mem.eql(u8, f.name, "Struggle") or
465
- std.mem.eql(u8, f.name, "SKIP_TURN")))
466
- {
467
- moves[i] = @field(Move, f.name);
468
- i += 1;
469
- }
296
+ /// Empirically determined maximum number of bytes possibly required to store the diff between two
297
+ /// Generation I battles.
298
+ pub const MAX_DIFFS: usize = 120;
299
+
300
+ /// The optimal size in bytes required to hold the diff between two Generation I battles.
301
+ /// At least as large as MAX_DIFFS.
302
+ pub const DIFFS_SIZE = if (builtin.mode == .ReleaseSmall)
303
+ MAX_DIFFS
304
+ else
305
+ std.math.ceilPowerOfTwo(usize, MAX_DIFFS) catch unreachable;
306
+
307
+ /// TODO
308
+ pub fn diff(
309
+ a: *const data.Battle(data.PRNG),
310
+ b: *const data.Battle(data.PRNG),
311
+ w: protocol.ByteStream.Writer,
312
+ ) !usize {
313
+ const pos = w.stream.pos;
314
+ const size = @sizeOf(data.Battle(data.PRNG)) - @sizeOf(data.PRNG);
315
+ assert(size % 2 == 0);
316
+ assert(size / 2 < 255);
317
+
318
+ var i: usize = 0;
319
+ while (i < size) : (i += 2) {
320
+ const val = util.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
321
+ if (val != util.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
322
+ try w.writeByte(@intCast(i / 2));
323
+ try w.writeInt(u16, val, endian);
470
324
  }
471
- break :init moves;
472
- };
473
-
474
- /// Returns a slice with the correct range of values for metronome given the `action` state.
475
- pub inline fn metronome(action: Action) []const Move {
476
- return if (@field(action, "metronome") == .None) &MOVE_NONE else &MOVES;
477
325
  }
478
- };
479
326
 
480
- test "Rolls.speedTie" {
481
- const actions = Actions{ .p1 = .{ .speed_tie = .P2 } };
482
- try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
483
- try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
327
+ return w.stream.pos - pos;
484
328
  }
485
329
 
486
- test "Rolls.damage" {
487
- const actions = Actions{ .p2 = .{ .damage = 221 } };
488
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
489
- try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
490
- try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
491
- }
492
-
493
- test "Rolls.coalesce" {
494
- var summaries =
495
- calc.Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
496
- try expectEqual(@as(u8, 0), try Rolls.coalesce(.P2, 0, &summaries, false));
497
- try expectEqual(@as(u8, 241), try Rolls.coalesce(.P2, 238, &summaries, false));
498
- try expectEqual(@as(u8, 255), try Rolls.coalesce(.P2, 238, &summaries, true));
499
- summaries.p1.damage.final = 74;
500
- try expectEqual(@as(u8, 217), try Rolls.coalesce(.P2, 217, &summaries, false));
501
- try expectEqual(@as(u8, 255), try Rolls.coalesce(.P2, 217, &summaries, true));
502
- }
503
-
504
- test "Rolls.hit" {
505
- const actions = Actions{ .p2 = .{ .hit = .true } };
506
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
507
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
508
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
509
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
510
- }
511
-
512
- test "Rolls.secondaryChance" {
513
- const actions = Actions{ .p1 = .{ .secondary_chance = .true } };
514
- try expectEqualSlices(
515
- Optional(bool),
516
- &.{ .false, .true },
517
- Rolls.secondaryChance(actions.p1, .None),
518
- );
519
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
520
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
521
- }
522
-
523
- test "Rolls.criticalHit" {
524
- const actions = Actions{ .p1 = .{ .critical_hit = .true } };
525
- try expectEqualSlices(
526
- Optional(bool),
527
- &.{ .false, .true },
528
- Rolls.criticalHit(actions.p1, .None),
529
- );
530
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
531
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
532
- }
533
-
534
- test "Rolls.confused" {
535
- const actions = Actions{ .p2 = .{ .confused = .true } };
536
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, 1));
537
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2, 1));
538
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, 0));
539
- }
540
-
541
- test "Rolls.paralyzed" {
542
- const actions = Actions{ .p2 = .{ .paralyzed = .true } };
543
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
544
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
545
- try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
546
- try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
547
- }
548
-
549
- test "Rolls.duration" {
550
- const actions = Actions{ .p2 = .{ .duration = 3 } };
551
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p1, .None));
552
- try expectEqualSlices(u1, &.{1}, Rolls.duration(actions.p2, .None));
553
- try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p2, .false));
554
- }
555
-
556
- test "Rolls.sleep" {
557
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(
558
- .{ .durations = .{ .sleep = 0 } },
559
- .{ .durations = .{ .sleep = 0 } },
560
- ));
561
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.sleep(
562
- .{ .durations = .{ .sleep = 1 } },
563
- .{ .durations = .{ .sleep = 0 } },
564
- ));
565
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.sleep(
566
- .{ .durations = .{ .sleep = 0 } },
567
- .{ .durations = .{ .sleep = 1 } },
568
- ));
569
- try expectEqualSlices(u3, &.{1}, Rolls.sleep(
570
- .{ .durations = .{ .sleep = 1 } },
571
- .{ .durations = .{ .sleep = 1 } },
572
- ));
573
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.sleep(
574
- .{ .durations = .{ .sleep = 0 } },
575
- .{ .durations = .{ .sleep = 2 } },
576
- ));
577
- try expectEqualSlices(u3, &.{ 0, 7 }, Rolls.sleep(
578
- .{ .durations = .{ .sleep = 7 } },
579
- .{ .durations = .{ .sleep = 6 } },
580
- ));
581
- try expectEqualSlices(u3, &.{0}, Rolls.sleep(
582
- .{ .durations = .{ .sleep = 0 } },
583
- .{ .durations = .{ .sleep = 7 } },
584
- ));
585
- }
586
-
587
- test "Rolls.disable" {
588
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
589
- .{ .durations = .{ .disable = 0 } },
590
- .{ .durations = .{ .disable = 0 } },
591
- 0,
592
- ));
593
- try expectEqualSlices(u4, &.{ 0, 1 }, Rolls.disable(
594
- .{ .durations = .{ .disable = 1 } },
595
- .{ .durations = .{ .disable = 0 } },
596
- 0,
597
- ));
598
- try expectEqualSlices(u4, &.{ 0, 2 }, Rolls.disable(
599
- .{ .durations = .{ .disable = 0 } },
600
- .{ .durations = .{ .disable = 1 } },
601
- 0,
602
- ));
603
- try expectEqualSlices(u4, &.{1}, Rolls.disable(
604
- .{ .durations = .{ .disable = 1 } },
605
- .{ .durations = .{ .disable = 1 } },
606
- 0,
607
- ));
608
- try expectEqualSlices(u4, &.{ 0, 3 }, Rolls.disable(
609
- .{ .durations = .{ .disable = 0 } },
610
- .{ .durations = .{ .disable = 2 } },
611
- 0,
612
- ));
613
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
614
- .{ .durations = .{ .disable = 0 } },
615
- .{ .durations = .{ .disable = 2 } },
616
- 1,
617
- ));
618
- try expectEqualSlices(u4, &.{ 0, 8 }, Rolls.disable(
619
- .{ .durations = .{ .disable = 8 } },
620
- .{ .durations = .{ .disable = 7 } },
621
- 0,
622
- ));
623
- try expectEqualSlices(u4, &.{0}, Rolls.disable(
624
- .{ .durations = .{ .disable = 0 } },
625
- .{ .durations = .{ .disable = 8 } },
626
- 0,
627
- ));
628
- }
629
-
630
- test "Rolls.confusion" {
631
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
632
- .{ .durations = .{ .confusion = 0 } },
633
- .{ .durations = .{ .confusion = 0 } },
634
- 0,
635
- ));
636
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(
637
- .{ .durations = .{ .confusion = 1 } },
638
- .{ .durations = .{ .confusion = 0 } },
639
- 0,
640
- ));
641
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
642
- .{ .durations = .{ .confusion = 0 } },
643
- .{ .durations = .{ .confusion = 1 } },
644
- 0,
645
- ));
646
- try expectEqualSlices(u3, &.{1}, Rolls.confusion(
647
- .{ .durations = .{ .confusion = 1 } },
648
- .{ .durations = .{ .confusion = 1 } },
649
- 0,
650
- ));
651
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.confusion(
652
- .{ .durations = .{ .confusion = 0 } },
653
- .{ .durations = .{ .confusion = 2 } },
654
- 0,
655
- ));
656
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
657
- .{ .durations = .{ .confusion = 0 } },
658
- .{ .durations = .{ .confusion = 2 } },
659
- 1,
660
- ));
661
- try expectEqualSlices(u3, &.{ 0, 5 }, Rolls.confusion(
662
- .{ .durations = .{ .confusion = 5 } },
663
- .{ .durations = .{ .confusion = 4 } },
664
- 0,
665
- ));
666
- try expectEqualSlices(u3, &.{0}, Rolls.confusion(
667
- .{ .durations = .{ .confusion = 0 } },
668
- .{ .durations = .{ .confusion = 5 } },
669
- 0,
670
- ));
671
- }
672
-
673
- test "Rolls.attacking" {
674
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
675
- .{ .durations = .{ .attacking = 0 } },
676
- .{ .durations = .{ .attacking = 0 } },
677
- .false,
678
- ));
679
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
680
- .{ .durations = .{ .attacking = 1 } },
681
- .{ .durations = .{ .attacking = 0 } },
682
- .false,
683
- ));
684
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
685
- .{ .durations = .{ .attacking = 0 } },
686
- .{ .durations = .{ .attacking = 1 } },
687
- .false,
688
- ));
689
- try expectEqualSlices(u3, &.{1}, Rolls.attacking(
690
- .{ .durations = .{ .attacking = 1 } },
691
- .{ .durations = .{ .attacking = 1 } },
692
- .false,
693
- ));
694
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
695
- .{ .durations = .{ .attacking = 0 } },
696
- .{ .durations = .{ .attacking = 2 } },
697
- .false,
698
- ));
699
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
700
- .{ .durations = .{ .attacking = 0 } },
701
- .{ .durations = .{ .attacking = 2 } },
702
- .true,
703
- ));
704
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
705
- .{ .durations = .{ .attacking = 3 } },
706
- .{ .durations = .{ .attacking = 2 } },
707
- .false,
708
- ));
709
- try expectEqualSlices(u3, &.{0}, Rolls.attacking(
710
- .{ .durations = .{ .attacking = 0 } },
711
- .{ .durations = .{ .attacking = 3 } },
712
- .false,
713
- ));
714
- }
715
-
716
- test "Rolls.binding" {
717
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
718
- .{ .durations = .{ .binding = 0 } },
719
- .{ .durations = .{ .binding = 0 } },
720
- .false,
721
- ));
722
- try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.binding(
723
- .{ .durations = .{ .binding = 1 } },
724
- .{ .durations = .{ .binding = 0 } },
725
- .false,
726
- ));
727
- try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.binding(
728
- .{ .durations = .{ .binding = 0 } },
729
- .{ .durations = .{ .binding = 1 } },
730
- .false,
731
- ));
732
- try expectEqualSlices(u3, &.{1}, Rolls.binding(
733
- .{ .durations = .{ .binding = 1 } },
734
- .{ .durations = .{ .binding = 1 } },
735
- .false,
736
- ));
737
- try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.binding(
738
- .{ .durations = .{ .binding = 0 } },
739
- .{ .durations = .{ .binding = 2 } },
740
- .false,
741
- ));
742
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
743
- .{ .durations = .{ .binding = 0 } },
744
- .{ .durations = .{ .binding = 2 } },
745
- .true,
746
- ));
747
- try expectEqualSlices(u3, &.{ 0, 4 }, Rolls.binding(
748
- .{ .durations = .{ .binding = 4 } },
749
- .{ .durations = .{ .binding = 3 } },
750
- .false,
751
- ));
752
- try expectEqualSlices(u3, &.{0}, Rolls.binding(
753
- .{ .durations = .{ .binding = 0 } },
754
- .{ .durations = .{ .binding = 4 } },
755
- .false,
756
- ));
757
- }
758
-
759
- test "Rolls.moveSlot" {
760
- const actions = Actions{ .p2 = .{ .move_slot = 3 } };
761
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
762
- try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
763
- try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
764
- }
765
-
766
- test "Rolls.multiHit" {
767
- const actions = Actions{ .p2 = .{ .multi_hit = 3 } };
768
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
769
- try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
770
- try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
771
- }
772
-
773
- test "Rolls.metronome" {
774
- const actions = Actions{ .p2 = .{ .metronome = .Surf } };
775
- try expectEqualSlices(Move, &.{.None}, Rolls.metronome(actions.p1));
776
- try expectEqual(@as(Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
777
- }
778
-
779
- test "Rolls.psywave" {
780
- const actions = Actions{ .p2 = .{ .psywave = 79 } };
781
- var side = Side.init(&[_]Pokemon{.{
782
- .species = .Bulbasaur,
783
- .level = 100,
784
- .moves = &[_]Move{.Tackle},
785
- }});
786
-
787
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
788
- var rolls = Rolls.psywave(actions.p2, &side, .None);
789
- try expectEqual(@as(u8, 150), rolls[rolls.len - 1]);
790
- side.stored().level = 81;
791
- rolls = Rolls.psywave(actions.p2, &side, .None);
792
- try expectEqual(@as(u8, 121), rolls[rolls.len - 1]);
793
- try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
330
+ /// TODO
331
+ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
332
+ var i: usize = 0;
333
+ while (i < buf.len) : (i += 3) {
334
+ const off = @as(u16, @intCast(buf[i])) * 2;
335
+ const val = std.mem.readInt(u16, @as(*const [2]u8, @ptrCast(buf[i + 1 .. i + 3])), endian);
336
+ const bytes: *[2]u8 = @ptrCast(std.mem.asBytes(battle)[off .. off + 2]);
337
+ std.mem.writeInt(u16, bytes, val, endian);
338
+ }
794
339
  }