@pkmn/engine 0.1.0-dev.c4dec0f7 → 0.1.0-dev.c4ffaf39

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