@pkmn/engine 0.1.0-dev.d716e940 → 0.1.0-dev.da4a0515

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 (57) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +90 -56
  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 +154 -116
  21. package/package.json +53 -24
  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 +19 -31
  25. package/src/lib/common/debug.zig +20 -11
  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 +9 -0
  29. package/src/lib/common/protocol.zig +906 -367
  30. package/src/lib/common/rational.zig +357 -0
  31. package/src/lib/common/rng.zig +35 -26
  32. package/src/lib/common/util.zig +12 -0
  33. package/src/lib/gen1/calc.zig +426 -0
  34. package/src/lib/gen1/chance.zig +1061 -0
  35. package/src/lib/gen1/data/moves.zig +202 -190
  36. package/src/lib/gen1/data/species.zig +5 -5
  37. package/src/lib/gen1/data/types.zig +10 -10
  38. package/src/lib/gen1/data.zig +99 -87
  39. package/src/lib/gen1/helpers.zig +599 -23
  40. package/src/lib/gen1/mechanics.zig +978 -710
  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 +14 -20
  44. package/src/lib/gen2/data/moves.zig +664 -570
  45. package/src/lib/gen2/data/species.zig +2 -2
  46. package/src/lib/gen2/data/types.zig +137 -34
  47. package/src/lib/gen2/data.zig +130 -92
  48. package/src/lib/gen2/generated.zig +806 -0
  49. package/src/lib/gen2/helpers.zig +16 -22
  50. package/src/lib/gen2/mechanics.zig +3591 -12
  51. package/src/lib/node.zig +131 -0
  52. package/src/lib/pkmn.zig +84 -2
  53. package/src/lib/wasm.zig +13 -0
  54. package/src/lib/binding/node.zig +0 -229
  55. package/src/lib/binding/wasm.zig +0 -10
  56. package/src/lib/gen1/README.md +0 -812
  57. package/src/lib/gen2/README.md +0 -309
@@ -1,17 +1,25 @@
1
1
  const std = @import("std");
2
2
 
3
3
  const common = @import("../common/data.zig");
4
+ const optional = @import("../common/optional.zig");
4
5
  const options = @import("../common/options.zig");
5
6
  const protocol = @import("../common/protocol.zig");
7
+ const rational = @import("../common/rational.zig");
6
8
  const rng = @import("../common/rng.zig");
7
9
 
10
+ const calc = @import("calc.zig");
11
+ const chance = @import("chance.zig");
8
12
  const data = @import("data.zig");
9
13
 
10
14
  const assert = std.debug.assert;
11
15
 
12
16
  const expectEqual = std.testing.expectEqual;
17
+ const expectEqualSlices = std.testing.expectEqualSlices;
13
18
 
14
19
  const Choice = common.Choice;
20
+ const Player = common.Player;
21
+
22
+ const Optional = optional.Optional;
15
23
 
16
24
  const showdown = options.showdown;
17
25
 
@@ -24,13 +32,23 @@ const Species = data.Species;
24
32
  const Stats = data.Stats;
25
33
  const Status = data.Status;
26
34
 
27
- /// Options with dictate
35
+ const Action = chance.Action;
36
+ const Actions = chance.Actions;
37
+
38
+ /// Configuration options controlling the domain of valid Pokémon that can be generated by the
39
+ /// random generator helpers.
28
40
  pub const Options = struct {
41
+ /// Whether to generate Pokémon adhering to "Cleric Clause".
29
42
  cleric: bool = showdown,
43
+ /// Whether to generate Pokémon with moves that contain bugs on Pokémon Showdown that are
44
+ /// unimplementable in the engine.
30
45
  block: bool = showdown,
31
46
  };
32
47
 
48
+ /// Helpers to simplify initialization of a Generation I battle.
33
49
  pub const Battle = struct {
50
+ /// Initializes a Generation I battle with the teams specified by `p1` and `p2`
51
+ /// and an RNG whose seed is derived from `seed`.
34
52
  pub fn init(
35
53
  seed: u64,
36
54
  p1: []const Pokemon,
@@ -43,6 +61,8 @@ pub const Battle = struct {
43
61
  };
44
62
  }
45
63
 
64
+ /// Initializes a Generation I battle with the teams specified by `p1` and `p2`
65
+ /// and a FixedRNG that returns the provided `rolls`.
46
66
  pub fn fixed(
47
67
  comptime rolls: anytype,
48
68
  p1: []const Pokemon,
@@ -54,71 +74,103 @@ pub const Battle = struct {
54
74
  };
55
75
  }
56
76
 
57
- pub fn random(rand: *PSRNG, opt: Options) data.Battle(data.PRNG) {
77
+ /// Returns a Generation I battle that is randomly generated based on the `rand` and `opts`.
78
+ pub fn random(rand: *PSRNG, opts: Options) data.Battle(data.PRNG) {
58
79
  return .{
59
80
  .rng = prng(rand),
60
81
  .turn = 0,
61
82
  .last_damage = 0,
62
- .sides = .{ Side.random(rand, opt), Side.random(rand, opt) },
83
+ .sides = .{ Side.random(rand, opts), Side.random(rand, opts) },
63
84
  };
64
85
  }
65
86
  };
66
87
 
67
- pub 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;
88
+ fn prng(rand: *PSRNG) data.PRNG {
70
89
  return .{
71
90
  .src = .{
72
91
  .seed = if (showdown)
73
92
  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
- },
93
+ else seed: {
94
+ // GLITCH: The initial RNG seed in Gen I link battles is heavily constrained
95
+ // https://www.smogon.com/forums/threads/rng-in-rby-link-battles.3746779
96
+ var seed = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0 };
97
+
98
+ // Bytes in seed can only range from 0-252, not 0-255
99
+ const max: u8 = 253;
100
+ // Instructions take multiples of 4 cycles to execute
101
+ var timer: u16 = 4 * rand.range(u16, 0, 16384);
102
+ var add: u8 = rand.range(u8, 0, 256);
103
+ var carry: u8 = 0;
104
+
105
+ var i: u8 = 0;
106
+ while (i < seed.len) {
107
+ const div: u8 = @intCast(timer >> 8);
108
+ add +%= div +% carry;
109
+ if (add < max) {
110
+ seed[i] = add;
111
+ i += 1;
112
+ timer +%= 472;
113
+ carry = 1;
114
+ } else {
115
+ timer +%= 452;
116
+ carry = 0;
117
+ }
118
+ }
119
+ break :seed seed;
120
+ },
82
121
  },
83
122
  };
84
123
  }
85
124
 
125
+ /// Helpers to simplify initialization of a Generation I side.
86
126
  pub const Side = struct {
127
+ /// Initializes a Generation I side with the team specified by `ps`.
87
128
  pub fn init(ps: []const Pokemon) data.Side {
88
129
  assert(ps.len > 0 and ps.len <= 6);
89
130
  var side = data.Side{};
90
131
 
91
132
  for (0..ps.len) |i| {
92
133
  side.pokemon[i] = Pokemon.init(ps[i]);
93
- side.order[i] = @intCast(u4, i) + 1;
134
+ side.order[i] = @as(u4, @intCast(i)) + 1;
94
135
  }
95
136
  return side;
96
137
  }
97
138
 
98
- pub fn random(rand: *PSRNG, opt: Options) data.Side {
139
+ /// Returns a Generation I side that is randomly generated based on the `rand` and `opts`.
140
+ pub fn random(rand: *PSRNG, opts: Options) data.Side {
99
141
  const n = if (rand.chance(u8, 1, 100)) rand.range(u4, 1, 5 + 1) else 6;
100
142
  var side = data.Side{};
101
143
 
102
144
  for (0..n) |i| {
103
- side.pokemon[i] = Pokemon.random(rand, opt);
104
- side.order[i] = @intCast(u4, i) + 1;
145
+ side.pokemon[i] = Pokemon.random(rand, opts);
146
+ side.order[i] = @as(u4, @intCast(i)) + 1;
105
147
  }
106
148
 
107
149
  return side;
108
150
  }
109
151
  };
110
152
 
153
+ /// The maximum stat experience possible in Generation I.
111
154
  pub const EXP = 0xFFFF;
112
155
 
156
+ /// Helpers to simplify initialization of a Generation I Pokémon.
113
157
  pub const Pokemon = struct {
158
+ /// The Pokémon's species.
114
159
  species: Species,
160
+ /// The Pokémon's moves (assumed to all have the max possible PP).
115
161
  moves: []const Move,
162
+ /// The Pokémon's current HP (defaults to 100% if not specified).
116
163
  hp: ?u16 = null,
164
+ /// The Pokémon's current status.
117
165
  status: u8 = 0,
166
+ /// The Pokémon's level.
118
167
  level: u8 = 100,
168
+ /// The Pokémon's DVs.
119
169
  dvs: DVs = .{},
170
+ /// The Pokémon's stat experience.
120
171
  stats: Stats(u16) = .{ .hp = EXP, .atk = EXP, .def = EXP, .spe = EXP, .spc = EXP },
121
172
 
173
+ /// Initializes a Generation I Pokémon based on the information in `p`.
122
174
  pub fn init(p: Pokemon) data.Pokemon {
123
175
  var pokemon = data.Pokemon{};
124
176
  pokemon.species = p.species;
@@ -139,7 +191,7 @@ pub const Pokemon = struct {
139
191
  for (p.moves, 0..) |m, j| {
140
192
  pokemon.moves[j].id = m;
141
193
  // NB: PP can be at most 61 legally (though can overflow to 63)
142
- pokemon.moves[j].pp = @intCast(u8, @min(Move.pp(m) / 5 * 8, 61));
194
+ pokemon.moves[j].pp = @intCast(@min(Move.pp(m) / 5 * 8, 61));
143
195
  }
144
196
  if (p.hp) |hp| {
145
197
  pokemon.hp = hp;
@@ -152,8 +204,9 @@ pub const Pokemon = struct {
152
204
  return pokemon;
153
205
  }
154
206
 
207
+ /// Returns a Generation I Pokémon that is randomly generated based on the `rand` and `opts`.
155
208
  pub fn random(rand: *PSRNG, opt: Options) data.Pokemon {
156
- const s = @intToEnum(Species, rand.range(u8, 1, Species.size + 1));
209
+ const s: Species = @enumFromInt(rand.range(u8, 1, Species.size + 1));
157
210
  const species = Species.get(s);
158
211
  const lvl = if (rand.chance(u8, 1, 20)) rand.range(u8, 1, 99 + 1) else 100;
159
212
  var stats: Stats(u16) = .{};
@@ -176,15 +229,15 @@ pub const Pokemon = struct {
176
229
  for (0..n) |i| {
177
230
  var m: Move = .None;
178
231
  sample: while (true) {
179
- m = @intToEnum(Move, rand.range(u8, 1, Move.size - 1 + 1));
232
+ m = @enumFromInt(rand.range(u8, 1, Move.size - 1 + 1));
180
233
  if (opt.block and blocked(m)) continue :sample;
181
234
  for (0..i) |j| if (ms[j].id == m) continue :sample;
182
235
  break;
183
236
  }
184
- const pp_ups =
237
+ const pp_ups: u8 =
185
238
  if (!opt.cleric and rand.chance(u8, 1, 10)) rand.range(u2, 0, 2 + 1) else 3;
186
239
  // NB: PP can be at most 61 legally (though can overflow to 63)
187
- const max_pp = @intCast(u8, Move.pp(m) + @as(u8, pp_ups) * @min(Move.pp(m) / 5, 7));
240
+ const max_pp: u8 = @intCast(Move.pp(m) + pp_ups * @min(Move.pp(m) / 5, 7));
188
241
  ms[i] = .{
189
242
  .id = m,
190
243
  .pp = if (opt.cleric) max_pp else rand.range(u8, 0, max_pp + 1),
@@ -214,10 +267,533 @@ fn blocked(m: Move) bool {
214
267
  };
215
268
  }
216
269
 
270
+ /// Convenience helper to create a move-type choice for the provided move slot.
217
271
  pub fn move(slot: u4) Choice {
218
272
  return .{ .type = .Move, .data = slot };
219
273
  }
220
274
 
275
+ /// Convenience helper to create a switch-type choice for the provided team slot.
221
276
  pub fn swtch(slot: u4) Choice {
222
277
  return .{ .type = .Switch, .data = slot };
223
278
  }
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;
482
+ }
483
+ };
484
+
485
+ test "Rolls.speedTie" {
486
+ const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
487
+ try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
488
+ try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
489
+ }
490
+
491
+ test "Rolls.damage" {
492
+ const actions: Actions = .{ .p2 = .{ .damage = 221 } };
493
+ try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
494
+ try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
495
+ try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
496
+ }
497
+
498
+ test "Rolls.coalesce" {
499
+ var summaries =
500
+ calc.Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
501
+ try expectEqual(0, try Rolls.coalesce(.P2, 0, &summaries, false));
502
+ try expectEqual(241, try Rolls.coalesce(.P2, 238, &summaries, false));
503
+ try expectEqual(255, try Rolls.coalesce(.P2, 238, &summaries, true));
504
+ summaries.p1.damage.final = 74;
505
+ try expectEqual(217, try Rolls.coalesce(.P2, 217, &summaries, false));
506
+ try expectEqual(255, try Rolls.coalesce(.P2, 217, &summaries, true));
507
+ }
508
+
509
+ test "Rolls.hit" {
510
+ const actions: Actions = .{ .p2 = .{ .hit = .true } };
511
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
512
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
513
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
514
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
515
+ }
516
+
517
+ test "Rolls.secondaryChance" {
518
+ const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
519
+ try expectEqualSlices(
520
+ Optional(bool),
521
+ &.{ .false, .true },
522
+ Rolls.secondaryChance(actions.p1, .None),
523
+ );
524
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
525
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
526
+ }
527
+
528
+ test "Rolls.criticalHit" {
529
+ const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
530
+ try expectEqualSlices(
531
+ Optional(bool),
532
+ &.{ .false, .true },
533
+ Rolls.criticalHit(actions.p1, .None),
534
+ );
535
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
536
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
537
+ }
538
+
539
+ test "Rolls.confused" {
540
+ const actions: Actions = .{ .p2 = .{ .confused = .true } };
541
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, 1));
542
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2, 1));
543
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, 0));
544
+ }
545
+
546
+ test "Rolls.paralyzed" {
547
+ const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
548
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
549
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
550
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
551
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
552
+ }
553
+
554
+ test "Rolls.duration" {
555
+ const actions: Actions = .{ .p2 = .{ .duration = 3 } };
556
+ try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p1, .None));
557
+ try expectEqualSlices(u1, &.{1}, Rolls.duration(actions.p2, .None));
558
+ try expectEqualSlices(u1, &.{0}, Rolls.duration(actions.p2, .false));
559
+ }
560
+
561
+ test "Rolls.sleep" {
562
+ try expectEqualSlices(u3, &.{0}, Rolls.sleep(
563
+ .{ .durations = .{ .sleep = 0 } },
564
+ .{ .durations = .{ .sleep = 0 } },
565
+ ));
566
+ try expectEqualSlices(u3, &.{ 0, 1 }, Rolls.sleep(
567
+ .{ .durations = .{ .sleep = 1 } },
568
+ .{ .durations = .{ .sleep = 0 } },
569
+ ));
570
+ try expectEqualSlices(u3, &.{ 0, 2 }, Rolls.sleep(
571
+ .{ .durations = .{ .sleep = 0 } },
572
+ .{ .durations = .{ .sleep = 1 } },
573
+ ));
574
+ try expectEqualSlices(u3, &.{1}, Rolls.sleep(
575
+ .{ .durations = .{ .sleep = 1 } },
576
+ .{ .durations = .{ .sleep = 1 } },
577
+ ));
578
+ try expectEqualSlices(u3, &.{ 0, 3 }, Rolls.sleep(
579
+ .{ .durations = .{ .sleep = 0 } },
580
+ .{ .durations = .{ .sleep = 2 } },
581
+ ));
582
+ try expectEqualSlices(u3, &.{ 0, 7 }, Rolls.sleep(
583
+ .{ .durations = .{ .sleep = 7 } },
584
+ .{ .durations = .{ .sleep = 6 } },
585
+ ));
586
+ try expectEqualSlices(u3, &.{0}, Rolls.sleep(
587
+ .{ .durations = .{ .sleep = 0 } },
588
+ .{ .durations = .{ .sleep = 7 } },
589
+ ));
590
+ }
591
+
592
+ test "Rolls.disable" {
593
+ try expectEqualSlices(u4, &.{0}, Rolls.disable(
594
+ .{ .durations = .{ .disable = 0 } },
595
+ .{ .durations = .{ .disable = 0 } },
596
+ 0,
597
+ ));
598
+ try expectEqualSlices(u4, &.{ 0, 1 }, Rolls.disable(
599
+ .{ .durations = .{ .disable = 1 } },
600
+ .{ .durations = .{ .disable = 0 } },
601
+ 0,
602
+ ));
603
+ try expectEqualSlices(u4, &.{ 0, 2 }, Rolls.disable(
604
+ .{ .durations = .{ .disable = 0 } },
605
+ .{ .durations = .{ .disable = 1 } },
606
+ 0,
607
+ ));
608
+ try expectEqualSlices(u4, &.{1}, Rolls.disable(
609
+ .{ .durations = .{ .disable = 1 } },
610
+ .{ .durations = .{ .disable = 1 } },
611
+ 0,
612
+ ));
613
+ try expectEqualSlices(u4, &.{ 0, 3 }, Rolls.disable(
614
+ .{ .durations = .{ .disable = 0 } },
615
+ .{ .durations = .{ .disable = 2 } },
616
+ 0,
617
+ ));
618
+ try expectEqualSlices(u4, &.{0}, Rolls.disable(
619
+ .{ .durations = .{ .disable = 0 } },
620
+ .{ .durations = .{ .disable = 2 } },
621
+ 1,
622
+ ));
623
+ try expectEqualSlices(u4, &.{ 0, 8 }, Rolls.disable(
624
+ .{ .durations = .{ .disable = 8 } },
625
+ .{ .durations = .{ .disable = 7 } },
626
+ 0,
627
+ ));
628
+ try expectEqualSlices(u4, &.{0}, Rolls.disable(
629
+ .{ .durations = .{ .disable = 0 } },
630
+ .{ .durations = .{ .disable = 8 } },
631
+ 0,
632
+ ));
633
+ }
634
+
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));
799
+ }