@pkmn/engine 0.1.0-dev.44a51703 → 0.1.0-dev.44c4a618

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 +93 -57
  3. package/build/pkg/addon.js +9 -9
  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 +26 -4
  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 +15 -18
  11. package/build/pkg/gen1.js.map +1 -1
  12. package/build/pkg/index.d.ts +2 -1
  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 +2 -2
  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 +142 -119
  21. package/package.json +54 -25
  22. package/src/bin/install-pkmn-engine +35 -22
  23. package/src/lib/common/battle.zig +17 -0
  24. package/src/lib/common/data.zig +12 -24
  25. package/src/lib/common/debug.zig +22 -13
  26. package/src/lib/common/js.zig +118 -0
  27. package/src/lib/common/optional.zig +67 -0
  28. package/src/lib/common/options.zig +11 -0
  29. package/src/lib/common/protocol.zig +845 -322
  30. package/src/lib/common/rational.zig +357 -0
  31. package/src/lib/common/rng.zig +30 -21
  32. package/src/lib/common/util.zig +12 -0
  33. package/src/lib/gen1/calc.zig +507 -0
  34. package/src/lib/gen1/chance.zig +1063 -0
  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 +86 -77
  39. package/src/lib/gen1/helpers.zig +658 -15
  40. package/src/lib/gen1/mechanics.zig +940 -675
  41. package/src/lib/gen2/calc.zig +143 -0
  42. package/src/lib/gen2/chance.zig +1315 -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 +121 -84
  48. package/src/lib/gen2/generated.zig +806 -0
  49. package/src/lib/gen2/helpers.zig +45 -21
  50. package/src/lib/gen2/mechanics.zig +3591 -12
  51. package/src/lib/node.zig +131 -0
  52. package/src/lib/pkmn.zig +79 -5
  53. package/src/lib/{binding/wasm.zig → wasm.zig} +4 -1
  54. package/src/lib/binding/node.zig +0 -226
  55. package/src/lib/gen1/README.md +0 -814
  56. package/src/lib/gen2/README.md +0 -309
@@ -1,17 +1,26 @@
1
1
  const std = @import("std");
2
+ const builtin = @import("builtin");
2
3
 
3
4
  const common = @import("../common/data.zig");
5
+ const optional = @import("../common/optional.zig");
4
6
  const options = @import("../common/options.zig");
5
7
  const protocol = @import("../common/protocol.zig");
8
+ const rational = @import("../common/rational.zig");
6
9
  const rng = @import("../common/rng.zig");
7
10
 
11
+ const calc = @import("calc.zig");
12
+ const chance = @import("chance.zig");
8
13
  const data = @import("data.zig");
9
14
 
10
15
  const assert = std.debug.assert;
11
16
 
12
17
  const expectEqual = std.testing.expectEqual;
18
+ const expectEqualSlices = std.testing.expectEqualSlices;
13
19
 
14
20
  const Choice = common.Choice;
21
+ const Player = common.Player;
22
+
23
+ const Optional = optional.Optional;
15
24
 
16
25
  const showdown = options.showdown;
17
26
 
@@ -24,13 +33,27 @@ const Species = data.Species;
24
33
  const Stats = data.Stats;
25
34
  const Status = data.Status;
26
35
 
27
- /// Options with dictate
36
+ const Action = chance.Action;
37
+ const Actions = chance.Actions;
38
+
39
+ const endian = builtin.cpu.arch.endian();
40
+
41
+ /// Configuration options controlling the domain of valid Pokémon that can be generated by the
42
+ /// random generator helpers.
28
43
  pub const Options = struct {
44
+ /// Whether to generate Pokémon adhering to "Cleric Clause".
29
45
  cleric: bool = showdown,
46
+ /// Whether to generate Pokémon with moves that contain bugs on Pokémon Showdown that are
47
+ /// unimplementable in the engine.
30
48
  block: bool = showdown,
49
+ // FIXME: remove once durations work with -Dchance
50
+ durations: bool = false,
31
51
  };
32
52
 
53
+ /// Helpers to simplify initialization of a Generation I battle.
33
54
  pub const Battle = struct {
55
+ /// Initializes a Generation I battle with the teams specified by `p1` and `p2`
56
+ /// and an RNG whose seed is derived from `seed`.
34
57
  pub fn init(
35
58
  seed: u64,
36
59
  p1: []const Pokemon,
@@ -43,6 +66,8 @@ pub const Battle = struct {
43
66
  };
44
67
  }
45
68
 
69
+ /// Initializes a Generation I battle with the teams specified by `p1` and `p2`
70
+ /// and a FixedRNG that returns the provided `rolls`.
46
71
  pub fn fixed(
47
72
  comptime rolls: anytype,
48
73
  p1: []const Pokemon,
@@ -54,36 +79,57 @@ pub const Battle = struct {
54
79
  };
55
80
  }
56
81
 
57
- pub fn random(rand: *PSRNG, opt: Options) data.Battle(data.PRNG) {
82
+ /// Returns a Generation I battle that is randomly generated based on the `rand` and `opts`.
83
+ pub fn random(rand: *PSRNG, opts: Options) data.Battle(data.PRNG) {
58
84
  return .{
59
85
  .rng = prng(rand),
60
86
  .turn = 0,
61
87
  .last_damage = 0,
62
- .sides = .{ Side.random(rand, opt), Side.random(rand, opt) },
88
+ .sides = .{ Side.random(rand, opts), Side.random(rand, opts) },
63
89
  };
64
90
  }
65
91
  };
66
92
 
67
93
  fn prng(rand: *PSRNG) data.PRNG {
68
- // GLITCH: initial bytes in seed can only range from 0-252, not 0-255
69
- const max: u8 = 253;
70
94
  return .{
71
95
  .src = .{
72
96
  .seed = if (showdown)
73
97
  rand.newSeed()
74
- else
75
- .{
76
- rand.range(u8, 0, max), rand.range(u8, 0, max),
77
- rand.range(u8, 0, max), rand.range(u8, 0, max),
78
- rand.range(u8, 0, max), rand.range(u8, 0, max),
79
- rand.range(u8, 0, max), rand.range(u8, 0, max),
80
- rand.range(u8, 0, max), rand.range(u8, 0, max),
81
- },
98
+ else seed: {
99
+ // GLITCH: The initial RNG seed in Gen I link battles is heavily constrained
100
+ // https://www.smogon.com/forums/threads/rng-in-rby-link-battles.3746779
101
+ var seed = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0 };
102
+
103
+ // Bytes in seed can only range from 0-252, not 0-255
104
+ const max: u8 = 253;
105
+ // Instructions take multiples of 4 cycles to execute
106
+ var timer: u16 = 4 * rand.range(u16, 0, 16384);
107
+ var add: u8 = rand.range(u8, 0, 256);
108
+ var carry: u8 = 0;
109
+
110
+ var i: u8 = 0;
111
+ while (i < seed.len) {
112
+ const div: u8 = @intCast(timer >> 8);
113
+ add +%= div +% carry;
114
+ if (add < max) {
115
+ seed[i] = add;
116
+ i += 1;
117
+ timer +%= 472;
118
+ carry = 1;
119
+ } else {
120
+ timer +%= 452;
121
+ carry = 0;
122
+ }
123
+ }
124
+ break :seed seed;
125
+ },
82
126
  },
83
127
  };
84
128
  }
85
129
 
130
+ /// Helpers to simplify initialization of a Generation I side.
86
131
  pub const Side = struct {
132
+ /// Initializes a Generation I side with the team specified by `ps`.
87
133
  pub fn init(ps: []const Pokemon) data.Side {
88
134
  assert(ps.len > 0 and ps.len <= 6);
89
135
  var side = data.Side{};
@@ -95,12 +141,13 @@ pub const Side = struct {
95
141
  return side;
96
142
  }
97
143
 
98
- pub fn random(rand: *PSRNG, opt: Options) data.Side {
144
+ /// Returns a Generation I side that is randomly generated based on the `rand` and `opts`.
145
+ pub fn random(rand: *PSRNG, opts: Options) data.Side {
99
146
  const n = if (rand.chance(u8, 1, 100)) rand.range(u4, 1, 5 + 1) else 6;
100
147
  var side = data.Side{};
101
148
 
102
149
  for (0..n) |i| {
103
- side.pokemon[i] = Pokemon.random(rand, opt);
150
+ side.pokemon[i] = Pokemon.random(rand, opts);
104
151
  side.order[i] = @as(u4, @intCast(i)) + 1;
105
152
  }
106
153
 
@@ -108,17 +155,27 @@ pub const Side = struct {
108
155
  }
109
156
  };
110
157
 
158
+ /// The maximum stat experience possible in Generation I.
111
159
  pub const EXP = 0xFFFF;
112
160
 
161
+ /// Helpers to simplify initialization of a Generation I Pokémon.
113
162
  pub const Pokemon = struct {
163
+ /// The Pokémon's species.
114
164
  species: Species,
165
+ /// The Pokémon's moves (assumed to all have the max possible PP).
115
166
  moves: []const Move,
167
+ /// The Pokémon's current HP (defaults to 100% if not specified).
116
168
  hp: ?u16 = null,
169
+ /// The Pokémon's current status.
117
170
  status: u8 = 0,
171
+ /// The Pokémon's level.
118
172
  level: u8 = 100,
173
+ /// The Pokémon's DVs.
119
174
  dvs: DVs = .{},
175
+ /// The Pokémon's stat experience.
120
176
  stats: Stats(u16) = .{ .hp = EXP, .atk = EXP, .def = EXP, .spe = EXP, .spc = EXP },
121
177
 
178
+ /// Initializes a Generation I Pokémon based on the information in `p`.
122
179
  pub fn init(p: Pokemon) data.Pokemon {
123
180
  var pokemon = data.Pokemon{};
124
181
  pokemon.species = p.species;
@@ -152,6 +209,7 @@ pub const Pokemon = struct {
152
209
  return pokemon;
153
210
  }
154
211
 
212
+ /// Returns a Generation I Pokémon that is randomly generated based on the `rand` and `opts`.
155
213
  pub fn random(rand: *PSRNG, opt: Options) data.Pokemon {
156
214
  const s: Species = @enumFromInt(rand.range(u8, 1, Species.size + 1));
157
215
  const species = Species.get(s);
@@ -177,6 +235,7 @@ pub const Pokemon = struct {
177
235
  var m: Move = .None;
178
236
  sample: while (true) {
179
237
  m = @enumFromInt(rand.range(u8, 1, Move.size - 1 + 1));
238
+ if (opt.durations and durations(m)) continue :sample;
180
239
  if (opt.block and blocked(m)) continue :sample;
181
240
  for (0..i) |j| if (ms[j].id == m) continue :sample;
182
241
  break;
@@ -214,10 +273,594 @@ fn blocked(m: Move) bool {
214
273
  };
215
274
  }
216
275
 
276
+ fn durations(m: Move) bool {
277
+ // Any moves that have effects that result in duration tracking with -Dchance
278
+ return switch (Move.get(m).effect) {
279
+ .Confusion,
280
+ .Bide,
281
+ .Sleep,
282
+ .Rage,
283
+ .Binding,
284
+ .Thrashing,
285
+ .ConfusionChance,
286
+ .Disable,
287
+ .Metronome,
288
+ => true,
289
+ else => false,
290
+ };
291
+ }
292
+
293
+ /// Convenience helper to create a move-type choice for the provided move slot.
217
294
  pub fn move(slot: u4) Choice {
218
295
  return .{ .type = .Move, .data = slot };
219
296
  }
220
297
 
298
+ /// Convenience helper to create a switch-type choice for the provided team slot.
221
299
  pub fn swtch(slot: u4) Choice {
222
300
  return .{ .type = .Switch, .data = slot };
223
301
  }
302
+
303
+ const NONE = @intFromEnum(Optional(bool).None);
304
+
305
+ /// Helper functions that efficiently return valid ranges for various RNG events based on the
306
+ /// state of an `Action` and other events to be used to construct a "transitions" function.
307
+ pub const Rolls = struct {
308
+ const PLAYER_NONE = [_]Optional(Player){.None};
309
+ const PLAYERS = [_]Optional(Player){ .P1, .P2 };
310
+
311
+ /// Returns a slice with the correct range of values for speed ties given the `action` state.
312
+ pub fn speedTie(action: Action) []const Optional(Player) {
313
+ return if (@field(action, "speed_tie") == .None) &PLAYER_NONE else &PLAYERS;
314
+ }
315
+
316
+ const BOOL_NONE = [_]Optional(bool){.None};
317
+ const BOOLS = [_]Optional(bool){ .false, .true };
318
+
319
+ /// Returns a slice with the correct range of values for hits given the `action` state
320
+ /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
321
+ pub fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
322
+ if (parent == .true) return &BOOL_NONE;
323
+ return if (@field(action, "hit") == .None) &BOOL_NONE else &BOOLS;
324
+ }
325
+
326
+ /// Returns a slice with the correct range of values for critical hits given the `action` state
327
+ /// and the state of the `parent` (whether the player's Pokémon's move hit).
328
+ pub fn criticalHit(
329
+ action: Action,
330
+ parent: Optional(bool),
331
+ ) []const Optional(bool) {
332
+ if (parent == .false) return &BOOL_NONE;
333
+ return if (@field(action, "critical_hit") == .None) &BOOL_NONE else &BOOLS;
334
+ }
335
+
336
+ /// Returns a slice with the correct range of values for secondary chances hits given the
337
+ /// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
338
+ pub fn secondaryChance(
339
+ action: Action,
340
+ parent: Optional(bool),
341
+ ) []const Optional(bool) {
342
+ if (parent == .false) return &BOOL_NONE;
343
+ return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
344
+ }
345
+
346
+ /// The min and max bounds on iteration over damage rolls.
347
+ pub const Range = struct { min: u9, max: u9 };
348
+
349
+ /// Returns the range bounding damage rolls given the `action` state and the state of
350
+ /// the `parent` (whether the player's Pokémon's move hit).
351
+ pub fn damage(action: Action, parent: Optional(bool)) Range {
352
+ return if (parent == .false or @field(action, "damage") == 0)
353
+ .{ .min = 0, .max = 1 }
354
+ else
355
+ .{ .min = 217, .max = 256 };
356
+ }
357
+
358
+ /// Returns the max damage roll which will produce the same damage as `roll`
359
+ /// given the base damage in `summaries`.
360
+ pub fn coalesce(player: Player, roll: u8, summaries: *calc.Summaries, cap: bool) !u8 {
361
+ if (roll == 0) return roll;
362
+
363
+ const dmg = summaries.get(player.foe()).damage;
364
+ if (dmg.base == 0 or (cap and dmg.capped)) return 255;
365
+
366
+ // Closed form solution for max damage roll provided by Orion Taylor (taylorott)
367
+ return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
368
+ }
369
+
370
+ /// Returns a slice with the correct range of values for confused given the `action` state
371
+ /// and the state of the `parent` (the player's Pokémon's remaining confusion turns).
372
+ pub fn confused(action: Action, parent: u4) []const Optional(bool) {
373
+ return if (parent == 0 or @field(action, "confused") == .None) &BOOL_NONE else &BOOLS;
374
+ }
375
+
376
+ /// Returns a slice with the correct range of values for paralysis given the `action` state
377
+ /// and the state of the `parent` (whether the player's Pokémon was confused).
378
+ pub fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
379
+ if (parent == .true) return &BOOL_NONE;
380
+ return if (@field(action, "paralyzed") == .None) &BOOL_NONE else &BOOLS;
381
+ }
382
+
383
+ const DURATION_NONE = [_]u1{0};
384
+ const DURATION = [_]u1{1};
385
+
386
+ /// Returns a slice with a range of values for duration given the `action` state
387
+ /// and the state of the `parent` (whether the player's Pokémon's move hit).
388
+ pub fn duration(action: Action, parent: Optional(bool)) []const u1 {
389
+ if (parent == .false) return &DURATION_NONE;
390
+ return if (@field(action, "duration") == 0) &DURATION_NONE else &DURATION;
391
+ }
392
+
393
+ const U3_NONE = [_]u3{0};
394
+ const U3_FORCED = [_]u3{1};
395
+
396
+ /// Returns a slice with a range of values for sleep given the `action` and `origin` states.
397
+ pub inline fn sleep(action: Action, origin: Action) []const u3 {
398
+ const a = @field(action.durations, "sleep");
399
+ const o = @field(origin.durations, "sleep");
400
+ if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
401
+ assert(a == 0 or a == o + 1);
402
+ return if (o >= 7) &U3_NONE else &[_]u3{ 0, o + 1 };
403
+ }
404
+
405
+ const DISABLE_NONE = [_]u4{0};
406
+
407
+ /// Returns a slice with a range of values for disable given the `action` and `origin` states
408
+ /// and the state of the `parent` (the player's Pokémon's remaining sleep turns).
409
+ pub inline fn disable(action: Action, origin: Action, parent: u4) []const u4 {
410
+ if (parent > 0) return &DISABLE_NONE;
411
+ const a = @field(action.durations, "disable");
412
+ const o = @field(origin.durations, "disable");
413
+ if (o == a) return if (o == 0) &DISABLE_NONE else &[_]u4{o};
414
+ assert(a == 0 or a == o + 1);
415
+ return if (o >= 8) &DISABLE_NONE else &[_]u4{ 0, o + 1 };
416
+ }
417
+
418
+ /// Returns a slice with a range of values for confusion given the `action` and `origin` states
419
+ /// and the state of the `parent` (the player's remaining sleep turns).
420
+ pub inline fn confusion(action: Action, origin: Action, parent: u4) []const u3 {
421
+ if (parent > 0) return &U3_NONE;
422
+ const a = @field(action.durations, "confusion");
423
+ const o = @field(origin.durations, "confusion");
424
+ if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
425
+ assert(a == 0 or a == o + 1);
426
+ return if (o >= 5) &U3_NONE else if (o < 2)
427
+ if (a == 0) &U3_NONE else &U3_FORCED
428
+ else
429
+ &[_]u3{ 0, o + 1 };
430
+ }
431
+
432
+ /// Returns a slice with a range of values for attacking given the `action` and `origin` states
433
+ /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
434
+ pub inline fn attacking(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
435
+ if (parent == .true) return &U3_NONE;
436
+ const a = @field(action.durations, "attacking");
437
+ const o = @field(origin.durations, "attacking");
438
+ if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
439
+ assert(a == 0 or a == o + 1);
440
+ return if (o >= 3) &U3_NONE else if (o < 2)
441
+ if (a == 0) &U3_NONE else &U3_FORCED
442
+ else
443
+ &[_]u3{ 0, o + 1 };
444
+ }
445
+
446
+ /// Returns a slice with a range of values for binding given the `action` and `origin` states
447
+ /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
448
+ pub inline fn binding(action: Action, origin: Action, parent: Optional(bool)) []const u3 {
449
+ if (parent == .true) return &U3_NONE;
450
+ const a = @field(action.durations, "binding");
451
+ const o = @field(origin.durations, "binding");
452
+ if (o == a) return if (o == 0) &U3_NONE else &[_]u3{o};
453
+ assert(a == 0 or a == o + 1);
454
+ return if (o >= 4) &U3_NONE else &[_]u3{ 0, o + 1 };
455
+ }
456
+
457
+ const SLOT_NONE = [_]u4{0};
458
+ const SLOT = [_]u4{ 1, 2, 3, 4 };
459
+
460
+ /// Returns a slice with a range of values for move slots given the `action` state
461
+ /// and the state of the `parent` (whether the player's Pokémon's move hit).
462
+ ///
463
+ /// These slots may or **may not be valid** as slots may be unset / have 0 PP.
464
+ pub fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
465
+ if (parent == .false) return &SLOT_NONE;
466
+ return if (@field(action, "move_slot") == 0) &SLOT_NONE else &SLOT;
467
+ }
468
+
469
+ const MULTI_NONE = [_]u4{0};
470
+ const MULTI = [_]u4{ 2, 3, 4, 5 };
471
+
472
+ /// Returns a slice with the correct range of values for multi hit given the `action` state
473
+ /// and the state of the `parent` (whether the player's Pokémon's move hit).
474
+ pub fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
475
+ if (parent == .false) return &MULTI_NONE;
476
+ return if (@field(action, "multi_hit") == 0) &MULTI_NONE else &MULTI;
477
+ }
478
+
479
+ const PSYWAVE_NONE = [_]u8{0};
480
+ const PSYWAVE = init: {
481
+ var rolls: [150]u8 = undefined;
482
+ for (0..150) |i| rolls[i] = i + 1;
483
+ break :init rolls;
484
+ };
485
+
486
+ /// Returns a slice with the correct range of values for psywave given the `action` state,
487
+ /// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
488
+ pub fn psywave(
489
+ action: Action,
490
+ side: *data.Side,
491
+ parent: Optional(bool),
492
+ ) []const u8 {
493
+ if (parent == .false) return &PSYWAVE_NONE;
494
+ return if (@field(action, "psywave") == 0)
495
+ &PSYWAVE_NONE
496
+ else
497
+ PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
498
+ }
499
+
500
+ const MOVE_NONE = [_]Move{.None};
501
+
502
+ /// Returns a slice with the correct range of values for metronome given the `action` state.
503
+ pub fn metronome(action: Action) []const Move {
504
+ return if (@field(action, "metronome") == .None) &MOVE_NONE else &Move.METRONOME;
505
+ }
506
+ };
507
+
508
+ test "Rolls.speedTie" {
509
+ const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
510
+ try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
511
+ try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
512
+ }
513
+
514
+ test "Rolls.damage" {
515
+ const actions: Actions = .{ .p2 = .{ .damage = 221 } };
516
+ try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
517
+ try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
518
+ try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
519
+ }
520
+
521
+ test "Rolls.coalesce" {
522
+ var summaries =
523
+ calc.Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
524
+ try expectEqual(0, try Rolls.coalesce(.P2, 0, &summaries, false));
525
+ try expectEqual(241, try Rolls.coalesce(.P2, 238, &summaries, false));
526
+ try expectEqual(255, try Rolls.coalesce(.P2, 238, &summaries, true));
527
+ summaries.p1.damage.final = 74;
528
+ try expectEqual(217, try Rolls.coalesce(.P2, 217, &summaries, false));
529
+ try expectEqual(255, try Rolls.coalesce(.P2, 217, &summaries, true));
530
+ }
531
+
532
+ test "Rolls.hit" {
533
+ const actions: Actions = .{ .p2 = .{ .hit = .true } };
534
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
535
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
536
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
537
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
538
+ }
539
+
540
+ test "Rolls.secondaryChance" {
541
+ const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
542
+ try expectEqualSlices(
543
+ Optional(bool),
544
+ &.{ .false, .true },
545
+ Rolls.secondaryChance(actions.p1, .None),
546
+ );
547
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
548
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
549
+ }
550
+
551
+ test "Rolls.criticalHit" {
552
+ const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
553
+ try expectEqualSlices(
554
+ Optional(bool),
555
+ &.{ .false, .true },
556
+ Rolls.criticalHit(actions.p1, .None),
557
+ );
558
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
559
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
560
+ }
561
+
562
+ test "Rolls.confused" {
563
+ const actions: Actions = .{ .p2 = .{ .confused = .true } };
564
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, 1));
565
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2, 1));
566
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, 0));
567
+ }
568
+
569
+ test "Rolls.paralyzed" {
570
+ const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
571
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
572
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
573
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
574
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
575
+ }
576
+
577
+ test "Rolls.duration" {
578
+ const actions: Actions = .{ .p2 = .{ .duration = 3 } };
579
+ try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p1, .None));
580
+ try expectEqualSlices(u1, &.{1}, Rolls.duration(actions.p2, .None));
581
+ try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p2, .false));
582
+ }
583
+
584
+ test "Rolls.sleep" {
585
+ try expectEqualSlices(u3, &.{0}, Rolls.sleep(
586
+ .{ .durations = .{ .sleep = 0 } },
587
+ .{ .durations = .{ .sleep = 0 } },
588
+ ));
589
+ try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.sleep(
590
+ .{ .durations = .{ .sleep = 1 } },
591
+ .{ .durations = .{ .sleep = 0 } },
592
+ ));
593
+ try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.sleep(
594
+ .{ .durations = .{ .sleep = 0 } },
595
+ .{ .durations = .{ .sleep = 1 } },
596
+ ));
597
+ try expectEqualSlices(u3, &.{1}, Rolls.sleep(
598
+ .{ .durations = .{ .sleep = 1 } },
599
+ .{ .durations = .{ .sleep = 1 } },
600
+ ));
601
+ try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.sleep(
602
+ .{ .durations = .{ .sleep = 0 } },
603
+ .{ .durations = .{ .sleep = 2 } },
604
+ ));
605
+ try expectEqualSlices(u3, &.{ 0, 7 }, Rolls.sleep(
606
+ .{ .durations = .{ .sleep = 7 } },
607
+ .{ .durations = .{ .sleep = 6 } },
608
+ ));
609
+ try expectEqualSlices(u3, &.{0}, Rolls.sleep(
610
+ .{ .durations = .{ .sleep = 0 } },
611
+ .{ .durations = .{ .sleep = 7 } },
612
+ ));
613
+ }
614
+
615
+ test "Rolls.disable" {
616
+ try expectEqualSlices(u4, &.{0}, Rolls.disable(
617
+ .{ .durations = .{ .disable = 0 } },
618
+ .{ .durations = .{ .disable = 0 } },
619
+ 0,
620
+ ));
621
+ try expectEqualSlices(u4, &.{ 0, 1 }, Rolls.disable(
622
+ .{ .durations = .{ .disable = 1 } },
623
+ .{ .durations = .{ .disable = 0 } },
624
+ 0,
625
+ ));
626
+ try expectEqualSlices(u4, &.{ 0, 2 }, Rolls.disable(
627
+ .{ .durations = .{ .disable = 0 } },
628
+ .{ .durations = .{ .disable = 1 } },
629
+ 0,
630
+ ));
631
+ try expectEqualSlices(u4, &.{1}, Rolls.disable(
632
+ .{ .durations = .{ .disable = 1 } },
633
+ .{ .durations = .{ .disable = 1 } },
634
+ 0,
635
+ ));
636
+ try expectEqualSlices(u4, &.{ 0, 3 }, Rolls.disable(
637
+ .{ .durations = .{ .disable = 0 } },
638
+ .{ .durations = .{ .disable = 2 } },
639
+ 0,
640
+ ));
641
+ try expectEqualSlices(u4, &.{0}, Rolls.disable(
642
+ .{ .durations = .{ .disable = 0 } },
643
+ .{ .durations = .{ .disable = 2 } },
644
+ 1,
645
+ ));
646
+ try expectEqualSlices(u4, &.{ 0, 8 }, Rolls.disable(
647
+ .{ .durations = .{ .disable = 8 } },
648
+ .{ .durations = .{ .disable = 7 } },
649
+ 0,
650
+ ));
651
+ try expectEqualSlices(u4, &.{0}, Rolls.disable(
652
+ .{ .durations = .{ .disable = 0 } },
653
+ .{ .durations = .{ .disable = 8 } },
654
+ 0,
655
+ ));
656
+ }
657
+
658
+ test "Rolls.confusion" {
659
+ try expectEqualSlices(u3, &.{0}, Rolls.confusion(
660
+ .{ .durations = .{ .confusion = 0 } },
661
+ .{ .durations = .{ .confusion = 0 } },
662
+ 0,
663
+ ));
664
+ try expectEqualSlices(u3, &.{1}, Rolls.confusion(
665
+ .{ .durations = .{ .confusion = 1 } },
666
+ .{ .durations = .{ .confusion = 0 } },
667
+ 0,
668
+ ));
669
+ try expectEqualSlices(u3, &.{0}, Rolls.confusion(
670
+ .{ .durations = .{ .confusion = 0 } },
671
+ .{ .durations = .{ .confusion = 1 } },
672
+ 0,
673
+ ));
674
+ try expectEqualSlices(u3, &.{1}, Rolls.confusion(
675
+ .{ .durations = .{ .confusion = 1 } },
676
+ .{ .durations = .{ .confusion = 1 } },
677
+ 0,
678
+ ));
679
+ try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.confusion(
680
+ .{ .durations = .{ .confusion = 0 } },
681
+ .{ .durations = .{ .confusion = 2 } },
682
+ 0,
683
+ ));
684
+ try expectEqualSlices(u3, &.{0}, Rolls.confusion(
685
+ .{ .durations = .{ .confusion = 0 } },
686
+ .{ .durations = .{ .confusion = 2 } },
687
+ 1,
688
+ ));
689
+ try expectEqualSlices(u3, &.{ 0, 5 }, Rolls.confusion(
690
+ .{ .durations = .{ .confusion = 5 } },
691
+ .{ .durations = .{ .confusion = 4 } },
692
+ 0,
693
+ ));
694
+ try expectEqualSlices(u3, &.{0}, Rolls.confusion(
695
+ .{ .durations = .{ .confusion = 0 } },
696
+ .{ .durations = .{ .confusion = 5 } },
697
+ 0,
698
+ ));
699
+ }
700
+
701
+ test "Rolls.attacking" {
702
+ try expectEqualSlices(u3, &.{0}, Rolls.attacking(
703
+ .{ .durations = .{ .attacking = 0 } },
704
+ .{ .durations = .{ .attacking = 0 } },
705
+ .false,
706
+ ));
707
+ try expectEqualSlices(u3, &.{1}, Rolls.attacking(
708
+ .{ .durations = .{ .attacking = 1 } },
709
+ .{ .durations = .{ .attacking = 0 } },
710
+ .false,
711
+ ));
712
+ try expectEqualSlices(u3, &.{0}, Rolls.attacking(
713
+ .{ .durations = .{ .attacking = 0 } },
714
+ .{ .durations = .{ .attacking = 1 } },
715
+ .false,
716
+ ));
717
+ try expectEqualSlices(u3, &.{1}, Rolls.attacking(
718
+ .{ .durations = .{ .attacking = 1 } },
719
+ .{ .durations = .{ .attacking = 1 } },
720
+ .false,
721
+ ));
722
+ try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
723
+ .{ .durations = .{ .attacking = 0 } },
724
+ .{ .durations = .{ .attacking = 2 } },
725
+ .false,
726
+ ));
727
+ try expectEqualSlices(u3, &.{0}, Rolls.attacking(
728
+ .{ .durations = .{ .attacking = 0 } },
729
+ .{ .durations = .{ .attacking = 2 } },
730
+ .true,
731
+ ));
732
+ try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.attacking(
733
+ .{ .durations = .{ .attacking = 3 } },
734
+ .{ .durations = .{ .attacking = 2 } },
735
+ .false,
736
+ ));
737
+ try expectEqualSlices(u3, &.{0}, Rolls.attacking(
738
+ .{ .durations = .{ .attacking = 0 } },
739
+ .{ .durations = .{ .attacking = 3 } },
740
+ .false,
741
+ ));
742
+ }
743
+
744
+ test "Rolls.binding" {
745
+ try expectEqualSlices(u3, &.{0}, Rolls.binding(
746
+ .{ .durations = .{ .binding = 0 } },
747
+ .{ .durations = .{ .binding = 0 } },
748
+ .false,
749
+ ));
750
+ try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.binding(
751
+ .{ .durations = .{ .binding = 1 } },
752
+ .{ .durations = .{ .binding = 0 } },
753
+ .false,
754
+ ));
755
+ try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.binding(
756
+ .{ .durations = .{ .binding = 0 } },
757
+ .{ .durations = .{ .binding = 1 } },
758
+ .false,
759
+ ));
760
+ try expectEqualSlices(u3, &.{1}, Rolls.binding(
761
+ .{ .durations = .{ .binding = 1 } },
762
+ .{ .durations = .{ .binding = 1 } },
763
+ .false,
764
+ ));
765
+ try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.binding(
766
+ .{ .durations = .{ .binding = 0 } },
767
+ .{ .durations = .{ .binding = 2 } },
768
+ .false,
769
+ ));
770
+ try expectEqualSlices(u3, &.{0}, Rolls.binding(
771
+ .{ .durations = .{ .binding = 0 } },
772
+ .{ .durations = .{ .binding = 2 } },
773
+ .true,
774
+ ));
775
+ try expectEqualSlices(u3, &.{ 0, 4 }, Rolls.binding(
776
+ .{ .durations = .{ .binding = 4 } },
777
+ .{ .durations = .{ .binding = 3 } },
778
+ .false,
779
+ ));
780
+ try expectEqualSlices(u3, &.{0}, Rolls.binding(
781
+ .{ .durations = .{ .binding = 0 } },
782
+ .{ .durations = .{ .binding = 4 } },
783
+ .false,
784
+ ));
785
+ }
786
+
787
+ test "Rolls.moveSlot" {
788
+ const actions: Actions = .{ .p2 = .{ .move_slot = 3 } };
789
+ try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
790
+ try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
791
+ try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
792
+ }
793
+
794
+ test "Rolls.multiHit" {
795
+ const actions: Actions = .{ .p2 = .{ .multi_hit = 3 } };
796
+ try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
797
+ try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
798
+ try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
799
+ }
800
+
801
+ test "Rolls.metronome" {
802
+ const actions: Actions = .{ .p2 = .{ .metronome = .Surf } };
803
+ try expectEqualSlices(Move, &.{.None}, Rolls.metronome(actions.p1));
804
+ try expectEqual(@as(Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
805
+ }
806
+
807
+ test "Rolls.psywave" {
808
+ const actions: Actions = .{ .p2 = .{ .psywave = 79 } };
809
+ var side = Side.init(&[_]Pokemon{.{
810
+ .species = .Bulbasaur,
811
+ .level = 100,
812
+ .moves = &[_]Move{.Tackle},
813
+ }});
814
+
815
+ try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
816
+ var rolls = Rolls.psywave(actions.p2, &side, .None);
817
+ try expectEqual(150, rolls[rolls.len - 1]);
818
+ side.stored().level = 81;
819
+ rolls = Rolls.psywave(actions.p2, &side, .None);
820
+ try expectEqual(121, rolls[rolls.len - 1]);
821
+ try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
822
+ }
823
+
824
+ /// Empirically determined maximum number of bytes possibly required to store the diff between two
825
+ /// Generation I battles.
826
+ pub const MAX_DIFFS: usize = 117;
827
+
828
+ /// The optimal size in bytes required to hold the diff between two Generation I battles.
829
+ /// At least as large as MAX_DIFFS.
830
+ pub const DIFFS_SIZE = if (builtin.mode == .ReleaseSmall)
831
+ MAX_DIFFS
832
+ else
833
+ std.math.ceilPowerOfTwo(usize, MAX_DIFFS) catch unreachable;
834
+
835
+ /// TODO
836
+ pub fn diff(
837
+ a: *const data.Battle(data.PRNG),
838
+ b: *const data.Battle(data.PRNG),
839
+ w: protocol.ByteStream.Writer,
840
+ ) !usize {
841
+ const pos = w.stream.pos;
842
+ const size = @sizeOf(data.Battle(data.PRNG)) - @sizeOf(data.PRNG);
843
+ assert(size % 2 == 0);
844
+ assert(size / 2 < 255);
845
+
846
+ var i: usize = 0;
847
+ while (i < size) : (i += 2) {
848
+ const val = std.mem.bytesAsValue(u16, std.mem.asBytes(a)[i .. i + 2]).*;
849
+ if (val != std.mem.bytesAsValue(u16, std.mem.asBytes(b)[i .. i + 2]).*) {
850
+ try w.writeByte(@intCast(i / 2));
851
+ try w.writeInt(u16, val, endian);
852
+ }
853
+ }
854
+
855
+ return w.stream.pos - pos;
856
+ }
857
+
858
+ /// TODO
859
+ pub fn patch(battle: *data.Battle(data.PRNG), buf: []u8) void {
860
+ var i: usize = 0;
861
+ while (i < buf.len) : (i += 3) {
862
+ const off = @as(u16, @intCast(buf[i])) * 2;
863
+ const val = std.mem.readInt(u16, @ptrCast(buf[i + 1 .. i + 3]), endian);
864
+ std.mem.writeInt(u16, @ptrCast(std.mem.asBytes(battle)[off .. off + 2]), val, endian);
865
+ }
866
+ }