@pkmn/engine 0.1.0-dev.6195e056 → 0.1.0-dev.62399ed5

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