@pkmn/engine 0.1.0-dev.9a0c07c3 → 0.1.0-dev.a0167a9d

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