@pkmn/engine 0.1.0-dev.04c4d944 → 0.1.0-dev.0cabf16e

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 (76) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +46 -19
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +43 -0
  5. package/build/pkg/addon/browser.js.map +1 -0
  6. package/build/pkg/addon/node.d.ts +3 -0
  7. package/build/pkg/addon/node.js +96 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon.d.ts +2 -0
  10. package/build/pkg/addon.js +35 -58
  11. package/build/pkg/addon.js.map +1 -1
  12. package/build/pkg/data/ids.json +12 -12
  13. package/build/pkg/data/layout.json +2 -2
  14. package/build/pkg/data/protocol.json +69 -12
  15. package/build/pkg/data.d.ts +27 -5
  16. package/build/pkg/data.js.map +1 -1
  17. package/build/pkg/gen1.js +3 -3
  18. package/build/pkg/gen1.js.map +1 -1
  19. package/build/pkg/index.d.ts +24 -10
  20. package/build/pkg/index.js +15 -10
  21. package/build/pkg/index.js.map +1 -1
  22. package/build/pkg/protocol.d.ts +20 -1
  23. package/build/pkg/protocol.js +123 -102
  24. package/build/pkg/protocol.js.map +1 -1
  25. package/build/tools/debug.js +36 -68
  26. package/build/tools/debug.js.map +1 -1
  27. package/build/tools/display/debug.jsx +141 -0
  28. package/build/tools/display/debug.jsx.map +1 -0
  29. package/build/tools/display/index.js +132 -0
  30. package/build/tools/display/index.js.map +1 -0
  31. package/build/tools/display/ui.jsx +349 -0
  32. package/build/tools/display/ui.jsx.map +1 -0
  33. package/build/tools/display/util.js +18 -0
  34. package/build/tools/display/util.js.map +1 -0
  35. package/build.zig +233 -98
  36. package/package.json +68 -31
  37. package/src/bin/install-pkmn-engine +30 -20
  38. package/src/bin/pkmn-debug +1 -1
  39. package/src/lib/common/array.zig +77 -0
  40. package/src/lib/common/data.zig +12 -24
  41. package/src/lib/common/debug.zig +39 -8
  42. package/src/lib/common/js.zig +4 -2
  43. package/src/lib/common/optional.zig +21 -10
  44. package/src/lib/common/options.zig +4 -0
  45. package/src/lib/common/protocol.zig +807 -327
  46. package/src/lib/common/rational.zig +125 -31
  47. package/src/lib/common/rng.zig +22 -13
  48. package/src/lib/common/util.zig +50 -1
  49. package/src/lib/gen1/calc.zig +677 -132
  50. package/src/lib/gen1/chance.zig +408 -283
  51. package/src/lib/gen1/data/moves.zig +192 -180
  52. package/src/lib/gen1/data/species.zig +3 -3
  53. package/src/lib/gen1/data/types.zig +7 -7
  54. package/src/lib/gen1/data.zig +48 -59
  55. package/src/lib/gen1/helpers.zig +88 -552
  56. package/src/lib/gen1/mechanics.zig +600 -543
  57. package/src/lib/gen2/calc.zig +161 -0
  58. package/src/lib/gen2/chance.zig +902 -0
  59. package/src/lib/gen2/data/items.zig +12 -18
  60. package/src/lib/gen2/data/moves.zig +663 -569
  61. package/src/lib/gen2/data/species.zig +1 -1
  62. package/src/lib/gen2/data/types.zig +134 -31
  63. package/src/lib/gen2/data.zig +107 -79
  64. package/src/lib/gen2/generated.zig +806 -0
  65. package/src/lib/gen2/helpers.zig +49 -25
  66. package/src/lib/gen2/mechanics.zig +3560 -12
  67. package/src/lib/{binding/node.zig → node.zig} +10 -13
  68. package/src/lib/pkmn.zig +32 -15
  69. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
  70. package/src/tools/display/dom.ts +96 -0
  71. package/src/tools/display/ui.css +359 -0
  72. package/build/test/display.js +0 -400
  73. package/build/test/display.js.map +0 -1
  74. package/src/lib/gen1/README.md +0 -892
  75. package/src/lib/gen2/README.md +0 -308
  76. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -17,6 +17,7 @@ const assert = std.debug.assert;
17
17
 
18
18
  const expect = std.testing.expect;
19
19
  const expectEqual = std.testing.expectEqual;
20
+ const expectEqualSlices = std.testing.expectEqualSlices;
20
21
 
21
22
  const enabled = pkmn.options.calc;
22
23
 
@@ -29,15 +30,22 @@ const Optional = optional.Optional;
29
30
  const Rational = rational.Rational;
30
31
 
31
32
  const PointerType = util.PointerType;
32
- const FieldType = util.FieldType;
33
+ const isPointerTo = util.isPointerTo;
33
34
 
34
35
  const Actions = chance.Actions;
35
36
  const Action = chance.Action;
36
37
  const Chance = chance.Chance;
37
- const Durations = chance.Durations;
38
+ const Confusion = chance.Confusion;
38
39
  const Duration = chance.Duration;
40
+ const Durations = chance.Durations;
41
+ const Observation = chance.Observation;
42
+ const Sleeps = chance.Sleeps;
43
+
44
+ const tty = true; // DEBUG
45
+ const summary = false; // DEBUG
39
46
 
40
- const Rolls = helpers.Rolls;
47
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
48
+ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
41
49
 
42
50
  /// Information relevant to damage calculation that occured during a Generation I battle `update`.
43
51
  pub const Summaries = extern struct {
@@ -51,8 +59,8 @@ pub const Summaries = extern struct {
51
59
  }
52
60
 
53
61
  /// Returns the `Summary` for the given `player`.
54
- pub inline fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
55
- assert(@typeInfo(@TypeOf(self)).Pointer.child == Summaries);
62
+ pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
63
+ assert(isPointerTo(self, Summaries));
56
64
  return if (player == .P1) &self.p1 else &self.p2;
57
65
  }
58
66
  };
@@ -70,8 +78,10 @@ pub const Summary = extern struct {
70
78
  /// The final computed damage that gets applied to the Pokémon. May exceed the target's HP
71
79
  // (to determine the *actual* damage done compare the target's stored HP before and after).
72
80
  final: u16 = 0,
73
- /// Whether higher damage will saturate / result in the same outcome (e.g. additional damage
74
- /// gets ignored due to it already breaking a Substitute or causing the target to faint).
81
+ /// Whether higher direct damage will saturate / result in the same outcome (e.g. additional
82
+ /// direct damage gets ignored due to it already breaking a Substitute or causing the target
83
+ /// to faint). Note that this field does not get set in scenarios where the target would
84
+ /// only be guaranteed to faint due to some sort of subsequent recoil or residual damage.
75
85
  capped: bool = false,
76
86
 
77
87
  // NOTE: 15 bits padding
@@ -86,25 +96,11 @@ pub const Summary = extern struct {
86
96
  }
87
97
  };
88
98
 
89
- /// TODO
90
- pub const Overrides = extern struct {
91
- /// TODO
92
- actions: Actions = .{},
93
- /// TODO
94
- durations: Durations = .{},
95
-
96
- _: u32 = 0,
97
-
98
- comptime {
99
- assert(@sizeOf(Overrides) == 24);
100
- }
101
- };
102
-
103
99
  /// Allows for forcing the value of specific RNG events during a Generation I battle `update` via
104
100
  /// `overrides` and tracks `summaries` of information relevant to damage calculation.
105
101
  pub const Calc = struct {
106
102
  /// Overrides the normal behavior of the RNG during an `update` to force specific outcomes.
107
- overrides: Overrides = .{},
103
+ overrides: Actions = .{},
108
104
  /// Information relevant to damage calculation.
109
105
  summaries: Summaries = .{},
110
106
 
@@ -115,26 +111,20 @@ pub const Calc = struct {
115
111
  ) ?std.meta.FieldType(Action, field) {
116
112
  if (!enabled) return null;
117
113
 
118
- const overrides =
119
- if (player == .P1) self.overrides.actions.p1 else self.overrides.actions.p2;
120
- const val = @field(overrides, @tagName(field));
114
+ const val = @field(self.overrides.get(player), @tagName(field));
121
115
  return if (switch (@typeInfo(@TypeOf(val))) {
122
- .Enum => val != .None,
123
- .Int => val != 0,
116
+ Enum => val != .None,
117
+ Int => val != 0,
124
118
  else => unreachable,
125
119
  }) val else null;
126
120
  }
127
121
 
128
- pub fn modify(self: Calc, player: Player, comptime field: Duration.Field) ?bool {
129
- if (!enabled) return null;
130
-
131
- const durations = self.overrides.durations;
132
- const set = @field(if (player == .P1) durations.p1 else durations.p2, @tagName(field));
133
- if (set == 0) return null;
122
+ pub fn confusion(self: *Calc, player: Player) void {
123
+ if (!enabled) return;
134
124
 
135
- const actions = self.overrides.actions;
136
- const overrides = if (player == .P1) actions.p1.durations else actions.p2.durations;
137
- return @field(overrides, @tagName(field)) != 0;
125
+ if (self.overrides.get(player).confusion == .started) {
126
+ self.overrides.get(player).confusion = .None;
127
+ }
138
128
  }
139
129
 
140
130
  pub fn base(self: *Calc, player: Player, val: u16) void {
@@ -158,7 +148,7 @@ pub const Calc = struct {
158
148
 
159
149
  /// Null object pattern implementation of Generation I `Calc` which does nothing, though damage
160
150
  /// calculator support should additionally be turned off entirely via `options.calc`.
161
- pub const NULL = Null{};
151
+ pub const NULL: Null = .{};
162
152
 
163
153
  const Null = struct {
164
154
  pub fn overridden(
@@ -170,9 +160,8 @@ const Null = struct {
170
160
  return null;
171
161
  }
172
162
 
173
- pub fn modify(self: Null, player: Player, comptime field: Duration.Field) ?bool {
174
- _ = .{ self, player, field };
175
- return null;
163
+ pub fn confusion(self: Null, player: Player) void {
164
+ _ = .{ self, player };
176
165
  }
177
166
 
178
167
  pub fn base(self: Null, player: Player, val: u16) void {
@@ -196,12 +185,14 @@ pub const Stats = struct {
196
185
  };
197
186
 
198
187
  pub const Options = struct {
199
- actions: Actions = .{},
188
+ durations: Durations,
200
189
  seed: ?u64 = null,
201
190
  cap: bool = false,
202
191
  metronome: bool = false,
203
192
  };
204
193
 
194
+ pub const MAX_FRONTIER = 49;
195
+
205
196
  pub fn transitions(
206
197
  battle: anytype,
207
198
  c1: Choice,
@@ -212,29 +203,27 @@ pub fn transitions(
212
203
  ) !?Stats {
213
204
  var stats: Stats = .{};
214
205
 
215
- var actions = options.actions;
216
- var cap = options.cap;
206
+ const cap = options.cap;
217
207
 
218
208
  var seen = std.AutoHashMap(Actions, void).init(allocator);
219
209
  defer seen.deinit();
220
210
  var frontier = std.ArrayList(Actions).init(allocator);
221
211
  defer frontier.deinit();
222
212
 
213
+ const durations = options.durations;
214
+
223
215
  var opts = pkmn.battle.options(
224
216
  protocol.NULL,
225
- Chance(Rational(u128)){ .probability = .{}, .actions = actions },
226
- Calc{ .overrides = .{ .actions = actions, .durations = .{
227
- .p1 = actions.p1.durations,
228
- .p2 = actions.p2.durations,
229
- } } },
217
+ Chance(Rational(u128)){ .probability = .{}, .durations = durations },
218
+ Calc{},
230
219
  );
231
220
 
232
221
  var b = battle;
233
222
  _ = try b.update(c1, c2, &opts);
234
223
  stats.updates += 1;
235
224
 
236
- var p1 = b.side(.P1);
237
- var p2 = b.side(.P2);
225
+ const p1 = b.side(.P1);
226
+ const p2 = b.side(.P2);
238
227
 
239
228
  var p: Rational(u128) = .{ .p = 0, .q = 1 };
240
229
  try frontier.append(opts.chance.actions);
@@ -248,85 +237,106 @@ pub fn transitions(
248
237
  var i: usize = 0;
249
238
  assert(frontier.items.len == 1);
250
239
  while (i < frontier.items.len) : (i += 1) {
251
- var template = frontier.items[i];
252
- opts.calc.overrides.durations = .{
253
- .p1 = template.p1.durations,
254
- .p2 = template.p2.durations,
240
+ const f = frontier.items[i];
241
+ const saved = stats.saved;
242
+
243
+ try debug(writer, f, .{
244
+ .shape = true,
245
+ .color = i,
246
+ .bold = true,
247
+ .background = true,
248
+ .indent = false,
249
+ });
250
+
251
+ var a: Actions = .{
252
+ .p1 = .{ .metronome = p1_move, .pp = f.p1.pp, .duration = f.p1.duration },
253
+ .p2 = .{ .metronome = p2_move, .pp = f.p2.pp, .duration = f.p2.duration },
255
254
  };
256
255
 
257
- try debug(writer, template, true, .{ .color = i, .bold = true, .background = true });
258
-
259
- var a = Actions{ .p1 = .{ .metronome = p1_move }, .p2 = .{ .metronome = p2_move } };
260
-
261
- for (Rolls.speedTie(template.p1)) |tie| { a.p1.speed_tie = tie; a.p2.speed_tie = tie;
262
- for (Rolls.sleep(template.p1, actions.p1)) |p1_slp| { a.p1.durations.sleep = p1_slp;
263
- for (Rolls.sleep(template.p2, actions.p2)) |p2_slp| { a.p2.durations.sleep = p2_slp;
264
- for (Rolls.disable(template.p1, actions.p1, p1_slp)) |p1_disable| { a.p1.durations.disable = p1_disable;
265
- for (Rolls.disable(template.p2, actions.p2, p2_slp)) |p2_disable| { a.p2.durations.disable = p2_disable;
266
- for (Rolls.confusion(template.p1, actions.p1, p1_slp)) |p1_cfz| { a.p1.durations.confusion = p1_cfz;
267
- for (Rolls.confusion(template.p2, actions.p2, p1_slp)) |p2_cfz| { a.p2.durations.confusion = p2_cfz;
268
- for (Rolls.confused(template.p1, p1_cfz)) |p1_cfzd| { a.p1.confused = p1_cfzd;
269
- for (Rolls.confused(template.p2, p2_cfz)) |p2_cfzd| { a.p2.confused = p2_cfzd;
270
- for (Rolls.paralyzed(template.p1, p1_cfzd)) |p1_par| { a.p1.paralyzed = p1_par;
271
- for (Rolls.paralyzed(template.p2, p2_cfzd)) |p2_par| { a.p2.paralyzed = p2_par;
272
- for (Rolls.attacking(template.p1, actions.p1, p1_par)) |p1_atk| { a.p1.durations.attacking = p1_atk;
273
- for (Rolls.attacking(template.p2, actions.p2, p2_par)) |p2_atk| { a.p2.durations.attacking = p2_atk;
274
- for (Rolls.binding(template.p1, actions.p1, p1_par)) |p1_bind| { a.p1.durations.binding = p1_bind;
275
- for (Rolls.binding(template.p2, actions.p2, p2_par)) |p2_bind| { a.p2.durations.binding = p2_bind;
276
- for (Rolls.duration(template.p1, p1_par)) |p1_dur| { a.p1.duration = p1_dur;
277
- for (Rolls.duration(template.p2, p2_par)) |p2_dur| { a.p2.duration = p2_dur;
278
- for (Rolls.hit(template.p1, p1_par)) |p1_hit| { a.p1.hit = p1_hit;
279
- for (Rolls.hit(template.p2, p2_par)) |p2_hit| { a.p2.hit = p2_hit;
280
- for (Rolls.psywave(template.p1, p1, p1_hit)) |p1_psywave| { a.p1.psywave = p1_psywave;
281
- for (Rolls.psywave(template.p2, p2, p2_hit)) |p2_psywave| { a.p2.psywave = p2_psywave;
282
- for (Rolls.moveSlot(template.p1, p1_hit)) |p1_slot| { a.p1.move_slot = p1_slot;
283
- for (Rolls.moveSlot(template.p2, p2_hit)) |p2_slot| { a.p2.move_slot = p2_slot;
284
- for (Rolls.multiHit(template.p1, p1_hit)) |p1_multi| { a.p1.multi_hit = p1_multi;
285
- for (Rolls.multiHit(template.p2, p1_hit)) |p2_multi| { a.p2.multi_hit = p2_multi;
286
- for (Rolls.secondaryChance(template.p1, p1_hit)) |p1_sec| { a.p1.secondary_chance = p1_sec;
287
- for (Rolls.secondaryChance(template.p2, p2_hit)) |p2_sec| { a.p2.secondary_chance = p2_sec;
288
- for (Rolls.criticalHit(template.p1, p1_hit)) |p1_crit| { a.p1.critical_hit = p1_crit;
289
- for (Rolls.criticalHit(template.p2, p2_hit)) |p2_crit| { a.p2.critical_hit = p2_crit;
290
-
291
- var p1_dmg = Rolls.damage(template.p1, p1_hit);
256
+ for (Rolls.speedTie(f.p1)) |tie| { a.p1.speed_tie = tie; a.p2.speed_tie = tie;
257
+ for (Rolls.sleep(f.p1, durations.p1)) |p1_slp| { a.p1.sleep = p1_slp;
258
+ for (Rolls.sleep(f.p2, durations.p2)) |p2_slp| { a.p2.sleep = p2_slp;
259
+ for (Rolls.disable(f.p1, durations.p1, p1_slp)) |p1_dis| { a.p1.disable = p1_dis;
260
+ for (Rolls.disable(f.p2, durations.p2, p2_slp)) |p2_dis| { a.p2.disable = p2_dis;
261
+ for (Rolls.attacking(f.p1, durations.p1)) |p1_atk| { a.p1.attacking = p1_atk;
262
+ for (Rolls.attacking(f.p2, durations.p2)) |p2_atk| { a.p2.attacking = p2_atk;
263
+ for (Rolls.confusion(f.p1, durations.p1, p1_atk, p1_slp)) |p1_cfz| { a.p1.confusion = p1_cfz;
264
+ for (Rolls.confusion(f.p2, durations.p2, p2_atk, p2_slp)) |p2_cfz| { a.p2.confusion = p2_cfz;
265
+ for (Rolls.confused(f.p1, p1_cfz)) |p1_cfzd| { a.p1.confused = p1_cfzd;
266
+ for (Rolls.confused(f.p2, p2_cfz)) |p2_cfzd| { a.p2.confused = p2_cfzd;
267
+ for (Rolls.paralyzed(f.p1, p1_cfzd)) |p1_par| { a.p1.paralyzed = p1_par;
268
+ for (Rolls.paralyzed(f.p2, p2_cfzd)) |p2_par| { a.p2.paralyzed = p2_par;
269
+ for (Rolls.binding(f.p1, durations.p1, p1_par)) |p1_bind| { a.p1.binding = p1_bind;
270
+ for (Rolls.binding(f.p2, durations.p2, p2_par)) |p2_bind| { a.p2.binding = p2_bind;
271
+ for (Rolls.hit(f.p1, p1_par)) |p1_hit| { a.p1.hit = p1_hit;
272
+ for (Rolls.hit(f.p2, p2_par)) |p2_hit| { a.p2.hit = p2_hit;
273
+ for (Rolls.psywave(f.p1, p1, p1_hit)) |p1_psywave| { a.p1.psywave = p1_psywave;
274
+ for (Rolls.psywave(f.p2, p2, p2_hit)) |p2_psywave| { a.p2.psywave = p2_psywave;
275
+ for (Rolls.moveSlot(f.p1, p1_hit)) |p1_slot| { a.p1.move_slot = p1_slot;
276
+ for (Rolls.moveSlot(f.p2, p2_hit)) |p2_slot| { a.p2.move_slot = p2_slot;
277
+ for (Rolls.multiHit(f.p1, p1_hit)) |p1_multi| { a.p1.multi_hit = p1_multi;
278
+ for (Rolls.multiHit(f.p2, p2_hit)) |p2_multi| { a.p2.multi_hit = p2_multi;
279
+ for (Rolls.secondaryChance(f.p1, p1_hit)) |p1_sec| { a.p1.secondary_chance = p1_sec;
280
+ for (Rolls.secondaryChance(f.p2, p2_hit)) |p2_sec| { a.p2.secondary_chance = p2_sec;
281
+ for (Rolls.criticalHit(f.p1, p1_hit)) |p1_crit| { a.p1.critical_hit = p1_crit;
282
+ for (Rolls.criticalHit(f.p2, p2_hit)) |p2_crit| { a.p2.critical_hit = p2_crit;
283
+
284
+ var p1_dmg = Rolls.damage(f.p1, p1_hit);
292
285
  while (p1_dmg.min < p1_dmg.max) : (p1_dmg.min += 1) {
293
286
  a.p1.damage = @intCast(p1_dmg.min);
294
- var p1_min: u8 = 0;
295
287
 
296
- var p2_dmg = Rolls.damage(template.p2, p2_hit);
288
+ var p2_dmg = Rolls.damage(f.p2, p2_hit);
289
+
290
+ const p1_min: u9 = p1_dmg.min;
291
+ const p2_min: u9 = p2_dmg.min;
292
+
297
293
  while (p2_dmg.min < p2_dmg.max) : (p2_dmg.min += 1) {
298
294
  a.p2.damage = @intCast(p2_dmg.min);
299
295
 
300
- opts.calc.overrides.actions = a;
301
- opts.chance = .{ .probability = .{}, .actions = actions };
296
+ opts.calc.overrides = a;
297
+ opts.calc.summaries = .{};
298
+ opts.chance = .{ .probability = .{}, .durations = durations };
302
299
  const q = &opts.chance.probability;
303
300
 
304
301
  b = battle;
305
302
  _ = try b.update(c1, c2, &opts);
306
- stats.updates += 1;
307
-
308
- const p1_max: u8 = @intCast(p1_dmg.min);
309
- const p2_max: u8 = @intCast(p2_dmg.min);
310
- _ = cap;
311
303
 
312
- // var p1_max = if (p1_min != 0) p1_min else
313
- // try Rolls.coalesce(.P1, @intCast(u8, p1_dmg.min), &opts.calc.summaries, cap);
314
- // var p2_max =
315
- // try Rolls.coalesce(.P2, @intCast(u8, p2_dmg.min), &opts.calc.summaries, cap);
304
+ stats.updates += 1;
316
305
 
317
- if (opts.chance.actions.matches(template)) {
318
- if (!opts.chance.actions.eql(a)) {
319
- try debug(writer, opts.chance.actions, false, .{ .color = i, .dim = true });
306
+ const summaries = &opts.calc.summaries;
307
+ const p1_max: u9 = if (p2_dmg.min != p2_min)
308
+ p1_dmg.min
309
+ else
310
+ try Rolls.coalesce(.P1, @as(u8, @intCast(p1_dmg.min)), summaries, cap);
311
+ const p2_max: u9 =
312
+ try Rolls.coalesce(.P2, @as(u8, @intCast(p2_dmg.min)), summaries, cap);
313
+
314
+ if (opts.chance.actions.matches(f)) {
315
+ if (!map(opts.chance.actions).eql(a)) {
316
+ if (!summary) {
317
+ try debug(writer, opts.chance.actions, .{
318
+ .p1_max = p1_max,
319
+ .p2_max = p2_max,
320
+ .color = i,
321
+ .dim = true,
322
+ });
323
+ }
320
324
 
321
- p1_min = p1_max;
325
+ p1_dmg.min = p1_max;
322
326
  p2_dmg.min = p2_max;
323
327
  continue;
324
328
  }
325
329
 
326
- try debug(writer, opts.chance.actions, false, .{ .color = i });
330
+ if (!summary) {
331
+ try debug(writer, opts.chance.actions, .{
332
+ .p1_max = p1_max,
333
+ .p2_max = p2_max,
334
+ .color = i,
335
+ });
336
+ }
327
337
 
328
- for (p1_dmg.min..@as(u9, p1_max) + 1) |p1d| {
329
- for (p2_dmg.min..@as(u9, p2_max) + 1) |p2d| {
338
+ for (p1_min..p1_max + 1) |p1d| {
339
+ for (p2_dmg.min..p2_max + 1) |p2d| {
330
340
  var acts = opts.chance.actions;
331
341
  acts.p1.damage = @intCast(p1d);
332
342
  acts.p2.damage = @intCast(p2d);
@@ -337,7 +347,7 @@ pub fn transitions(
337
347
  }
338
348
  }
339
349
 
340
- if (p1_max != p1_dmg.min) try q.update(p1_max - p1_dmg.min + 1, 1);
350
+ if (p1_max != p1_min) try q.update(p1_max - p1_min + 1, 1);
341
351
  if (p2_max != p2_dmg.min) try q.update(p2_max - p2_dmg.min + 1, 1);
342
352
  try p.add(q);
343
353
  stats.saved += 1;
@@ -347,27 +357,48 @@ pub fn transitions(
347
357
  return error.TestUnexpectedResult;
348
358
  }
349
359
  } else {
350
- try debug(writer, opts.chance.actions, false, .{ .dim = true });
351
-
352
360
  if (!matches(opts.chance.actions, i, frontier.items)) {
353
361
  try frontier.append(opts.chance.actions);
354
362
 
355
- try debug(writer, opts.chance.actions, true, .{
363
+ try debug(writer, opts.chance.actions, .{
364
+ .p1_max = p1_max,
365
+ .p2_max = p2_max,
366
+ .dim = true,
367
+ .newline = false,
368
+ });
369
+ try writer.writeAll(" → ");
370
+ try debug(writer, opts.chance.actions, .{
371
+ .shape = true,
356
372
  .color = frontier.items.len - 1,
357
373
  .dim = true,
358
- .background = true
374
+ .background = true,
375
+ .indent = false,
376
+ });
377
+ } else if (!summary) {
378
+ try debug(writer, opts.chance.actions, .{
379
+ .p1_max = p1_max,
380
+ .p2_max = p2_max,
381
+ .dim = true,
359
382
  });
360
383
  }
361
384
  }
362
385
 
363
- p1_min = p1_max;
386
+ p1_dmg.min = p1_max;
364
387
  p2_dmg.min = p2_max;
365
388
  }
366
389
 
367
- assert(p1_min > 0 or p1_dmg.min == 0);
368
- p1_dmg.min = p1_min;
390
+ }}}}}}}}}}}}}}}}}}}}}}}}}}}}
391
+
392
+ assert(stats.saved > saved);
393
+ const old = @hasDecl(std.fmt, "formatFloatDecimal");
394
+ if (!old and @TypeOf(writer) != @TypeOf(std.io.null_writer)) {
395
+ p.reduce();
396
+ try writer.print(
397
+ " = {s} ({d:.2}%)\n\n",
398
+ .{p, 100 * @as(f128, @floatFromInt(p.p)) / @as(f128, @floatFromInt(p.q))},
399
+ );
400
+ }
369
401
 
370
- }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
371
402
  }
372
403
 
373
404
  assert(frontier.items.len == i);
@@ -388,7 +419,123 @@ pub fn transitions(
388
419
  return stats;
389
420
  }
390
421
 
391
- inline fn matches(actions: Actions, i: usize, frontier: []Actions) bool {
422
+ pub fn update(
423
+ battle: anytype,
424
+ c1: Choice,
425
+ c2: Choice,
426
+ options: anytype,
427
+ allocator: std.mem.Allocator,
428
+ writer: anytype,
429
+ transition: bool,
430
+ ) !Result {
431
+ const durations = options.chance.durations;
432
+ options.chance.reset();
433
+
434
+ if (!pkmn.options.chance or !pkmn.options.calc) return battle.update(c1, c2, options);
435
+
436
+ const original = battle.*;
437
+
438
+ // Perfom the actual update
439
+ const result = battle.update(c1, c2, options);
440
+ var overrides = options.chance.actions;
441
+ const updated = options.chance.durations;
442
+
443
+ if (@TypeOf(battle.rng) == data.PRNG) {
444
+ // Ensure we can encode the diffs in less than MAX_DIFFS bytes.
445
+ var buf: [helpers.MAX_DIFFS]u8 = undefined;
446
+ var stream: protocol.ByteStream = .{ .buffer = &buf };
447
+ const n = try helpers.diff(&original, battle, stream.writer());
448
+
449
+ // Applying the diff to the battle should take us back to the original copy
450
+ // of the battle (ignoring the RNG).
451
+ var patched = battle.*;
452
+ patched.rng = original.rng;
453
+ helpers.patch(&patched, buf[0..n]);
454
+ try expectEqual(original, patched);
455
+ }
456
+
457
+ if (transition) {
458
+ // Ensure we can generate all transitions from the same original state
459
+ // (we must change the battle's RNG from a FixedRNG to a PRNG because
460
+ // the transitions function relies on RNG for discovery of states)
461
+ if (try transitions(unfix(original), c1, c2, allocator, writer, .{
462
+ .durations = durations,
463
+ .cap = true,
464
+ })) |stats| try expect(stats.frontier <= MAX_FRONTIER);
465
+ }
466
+
467
+ // Demonstrate that we can produce the same state by forcing the RNG to behave the
468
+ // same as we observed.
469
+ var override = pkmn.battle.options(
470
+ protocol.NULL,
471
+ Chance(Rational(u128)){ .probability = .{}, .durations = durations },
472
+ Calc{ .overrides = overrides },
473
+ );
474
+ var copy = original;
475
+ var overridden = copy.update(c1, c2, &override);
476
+ try expectEqual(result, overridden);
477
+ try expectEqualActions(overrides, override.chance.actions);
478
+ try expectEqual(updated, override.chance.durations);
479
+
480
+ // The actual battle excluding its RNG field should match a copy updated with
481
+ // overridden RNG (the copy RNG may have advanced because of no-ops)
482
+ copy.rng = battle.rng;
483
+ try expectEqual(battle.*, copy);
484
+
485
+ // Demonstrate that even with the duration rolls changed, the duration
486
+ // fields on their own are sufficient to achieve the same outcomes
487
+ // NB: The actual battle bytes will differ because the durations won't match
488
+ overrides.p1.duration = if (overrides.p1.duration > 0) 2 else 0;
489
+ overrides.p2.duration = if (overrides.p2.duration > 0) 2 else 0;
490
+
491
+ override = pkmn.battle.options(
492
+ protocol.NULL,
493
+ Chance(Rational(u128)){ .probability = .{}, .durations = durations },
494
+ Calc{ .overrides = overrides },
495
+ );
496
+ copy = original;
497
+ overridden = copy.update(c1, c2, &override);
498
+ try expectEqual(result, overridden);
499
+ try expectEqualActions(overrides, override.chance.actions);
500
+ try expectEqual(updated, override.chance.durations);
501
+
502
+ return result;
503
+ }
504
+
505
+ fn expectEqualActions(expected: Actions, actual: Actions) !void {
506
+ return expectEqual(expected, actual) catch |e| switch (e) {
507
+ error.TestExpectedEqual => {
508
+ std.debug.print("expected {}, found {}\n", .{ expected, actual });
509
+ return e;
510
+ },
511
+ else => return e,
512
+ };
513
+ }
514
+
515
+ fn unfix(actual: anytype) data.Battle(data.PRNG) {
516
+ return .{
517
+ .sides = actual.sides,
518
+ .turn = actual.turn,
519
+ .last_damage = actual.last_damage,
520
+ .last_moves = actual.last_moves,
521
+ .rng = .{ .src = .{
522
+ .seed = if (pkmn.options.showdown)
523
+ 0x12345678
524
+ else
525
+ .{ 123, 234, 56, 78, 9, 101, 112, 131, 4 },
526
+ } },
527
+ };
528
+ }
529
+
530
+ fn map(actions: Actions) Actions {
531
+ if (!pkmn.options.overwrite) return actions;
532
+ var a = actions;
533
+ if (a.p1.confusion == .overwritten) a.p1.confusion = .continuing;
534
+ if (a.p2.confusion == .overwritten) a.p2.confusion = .continuing;
535
+ return a;
536
+ }
537
+
538
+ fn matches(actions: Actions, i: usize, frontier: []Actions) bool {
392
539
  for (frontier, 0..) |f, j| {
393
540
  // TODO: is skipping this redundant check worth it?
394
541
  if (i == j) continue;
@@ -405,23 +552,421 @@ fn err(comptime fmt: []const u8, v: anytype, seed: ?u64) void {
405
552
  }
406
553
 
407
554
  const Style = struct {
555
+ shape: bool = false,
556
+ p1_max: u9 = 0,
557
+ p2_max: u9 = 0,
408
558
  color: ?usize = null,
409
559
  bold: bool = false,
410
560
  background: bool = false,
411
561
  dim: bool = false,
562
+ newline: bool = true,
563
+ indent: bool = true,
412
564
  };
413
565
 
414
- fn debug(writer: anytype, actions: Actions, shape: bool, style: Style) !void {
415
- _ = .{ writer, actions, shape, style };
416
- // const mod: usize = if (style.dim) 2 else 1;
417
- // const background: usize = if (style.background) 4 else 3;
418
- // const color: usize = if (style.color) |c| (c % 6) + 1 else 7;
419
- // if (style.dim or style.bold) try writer.print("\x1b[{d}m", .{mod});
420
- // try writer.print("\x1b[{d}{d}m", .{ background, color });
421
- // try actions.fmt(writer, shape);
422
- // try writer.writeAll("\x1b[0m\n");
423
- // DEBUG
424
- // if (style.dim) try writer.writeAll(" ");
425
- // try actions.fmt(writer, shape);
426
- // try writer.writeByte('\n');
566
+ fn debug(writer: anytype, actions: Actions, style: Style) !void {
567
+ if (style.indent) try writer.writeAll(" ");
568
+ if (tty) {
569
+ const mod: usize = if (style.dim) 2 else 1;
570
+ const background: usize = if (style.background) 4 else 3;
571
+ const color: usize = if (style.color) |c| (c % 6) + 1 else 7;
572
+
573
+ if (style.dim or style.bold) try writer.print("\x1b[{d}m", .{mod});
574
+ try writer.print("\x1b[{d}{d}m", .{ background, color });
575
+ const p1 = if (style.p1_max != 0 and style.p1_max != actions.p1.damage) style.p1_max else 0;
576
+ const p2 = if (style.p2_max != 0 and style.p2_max != actions.p2.damage) style.p2_max else 0;
577
+ if (p1 != 0 or p2 != 0) {
578
+ assert(!style.shape);
579
+ try format(writer, actions, p1, p2);
580
+ } else {
581
+ try actions.fmt(writer, style.shape);
582
+ }
583
+ try writer.writeAll("\x1b[0m");
584
+ } else {
585
+ if (style.dim) try writer.writeAll(" ");
586
+ try actions.fmt(writer, style.shape);
587
+ }
588
+ if (style.newline) try writer.writeByte('\n');
589
+ }
590
+
591
+ fn format(writer: anytype, actions: Actions, p1: u9, p2: u9) !void {
592
+ var input: [1024]u8 = undefined;
593
+ var output: [1024]u8 = undefined;
594
+
595
+ var stream = std.io.fixedBufferStream(&input);
596
+ try actions.fmt(&stream.writer(), false);
597
+ var len = stream.getWritten().len;
598
+ @memcpy(output[0..len], input[0..len]);
599
+
600
+ var find: [16]u8 = undefined;
601
+ var repl: [22]u8 = undefined;
602
+ if (p1 != 0 and actions.p1.damage != 0) {
603
+ const n = try std.fmt.bufPrint(&find, "P1 = (damage:{d}", .{actions.p1.damage});
604
+ const r = try std.fmt.bufPrint(&repl, "P1 = (damage:{d}…{d}", .{ actions.p1.damage, p1 });
605
+ assert(std.mem.replace(u8, input[0..len], n, r, output[0 .. len + 6]) == 1);
606
+ @memcpy(input[0 .. len + 6], output[0 .. len + 6]);
607
+ len += 6;
608
+ }
609
+ if (p2 != 0 and actions.p2.damage != 0) {
610
+ const n = try std.fmt.bufPrint(&find, "P2 = (damage:{d}", .{actions.p2.damage});
611
+ const r = try std.fmt.bufPrint(&repl, "P2 = (damage:{d}…{d}", .{ actions.p2.damage, p2 });
612
+ assert(std.mem.replace(u8, input[0..len], n, r, output[0 .. len + 6]) == 1);
613
+ len += 6;
614
+ }
615
+
616
+ try writer.writeAll(output[0..len]);
617
+ }
618
+
619
+ /// Helper functions that efficiently return valid ranges for various RNG events based on the
620
+ /// state of an `Action` and other events to be used to construct a "transitions" function.
621
+ pub const Rolls = struct {
622
+ const PLAYER_NONE = [_]Optional(Player){.None};
623
+ const PLAYERS = [_]Optional(Player){ .P1, .P2 };
624
+
625
+ /// Returns a slice with the correct range of values for speed ties given the `action` state.
626
+ pub fn speedTie(action: Action) []const Optional(Player) {
627
+ return if (action.speed_tie == .None) &PLAYER_NONE else &PLAYERS;
628
+ }
629
+
630
+ const OBS_NONE = [_]Optional(Observation){.None};
631
+ const OBS_STARTED = [_]Optional(Observation){.started};
632
+ const OBS_CONTINUING = [_]Optional(Observation){.continuing};
633
+ const OBS_ENDED = [_]Optional(Observation){.ended};
634
+ const OBS_ALL = [_]Optional(Observation){ .started, .continuing, .ended };
635
+ const OBS = [_]Optional(Observation){ .continuing, .ended };
636
+
637
+ /// Returns a slice with a range of values for sleep given the `action` state and observed
638
+ /// `durations`.
639
+ pub fn sleep(action: Action, duration: Duration) []const Optional(Observation) {
640
+ if (action.sleep == .None) return &OBS_NONE;
641
+ return if (Sleeps.get(duration.sleeps, 0) == 0 and action.speed_tie != .None)
642
+ &OBS_ALL
643
+ else switch (action.sleep) {
644
+ .started => &OBS_STARTED,
645
+ else => if (Sleeps.get(duration.sleeps, 0) >= 7) &OBS_ENDED else &OBS,
646
+ };
647
+ }
648
+
649
+ /// Returns a slice with a range of values for disable given the `action`, observed `durations`,
650
+ /// and the state of the `parent` (observation of the player's Pokémon sleep status).
651
+ pub fn disable(
652
+ action: Action,
653
+ duration: Duration,
654
+ parent: Optional(Observation),
655
+ ) []const Optional(Observation) {
656
+ if (action.disable == .None) return &OBS_NONE;
657
+ return if (duration.disable == 0 and action.speed_tie != .None)
658
+ &OBS_ALL
659
+ else switch (action.disable) {
660
+ .started => &OBS_STARTED,
661
+ else => if (duration.disable >= 8)
662
+ &OBS_ENDED
663
+ else if (parent != .None and parent != .started)
664
+ &OBS_CONTINUING
665
+ else
666
+ &OBS,
667
+ };
668
+ }
669
+
670
+ const CFZ_NONE = [_]Optional(Confusion){.None};
671
+ const CFZ_STARTED = [_]Optional(Confusion){.started};
672
+ const CFZ_OVERWRITTEN = [_]Optional(Confusion){ .started, .overwritten };
673
+ const CFZ_CONTINUING = [_]Optional(Confusion){.continuing};
674
+ const CFZ_ENDED = [_]Optional(Confusion){.ended};
675
+ const CFZ = [_]Optional(Confusion){ .continuing, .ended };
676
+
677
+ /// FIXME
678
+ pub fn confusion(
679
+ action: Action,
680
+ duration: Duration,
681
+ sibling: Optional(Observation),
682
+ parent: Optional(Observation),
683
+ ) []const Optional(Confusion) {
684
+ return switch (action.confusion) {
685
+ .None => &CFZ_NONE,
686
+ .started => if (sibling == .ended)
687
+ &CFZ_OVERWRITTEN
688
+ else
689
+ &CFZ_STARTED,
690
+ else => if (duration.confusion >= 5)
691
+ &CFZ_ENDED
692
+ else if ((parent != .None and parent != .started) or duration.confusion < 2)
693
+ &CFZ_CONTINUING
694
+ else
695
+ &CFZ,
696
+ };
697
+ }
698
+
699
+ /// FIXME
700
+ pub fn attacking(action: Action, duration: Duration) []const Optional(Observation) {
701
+ return switch (action.attacking) {
702
+ .None => &OBS_NONE,
703
+ .started => &OBS_STARTED,
704
+ else => if (duration.attacking >= 3)
705
+ &OBS_ENDED
706
+ else if (duration.attacking < 2)
707
+ &OBS_CONTINUING
708
+ else
709
+ &OBS,
710
+ };
711
+ }
712
+
713
+ /// Returns a slice with a range of values for binding given the `action`, observed `durations`,
714
+ /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
715
+ pub fn binding(
716
+ action: Action,
717
+ duration: Duration,
718
+ parent: Optional(bool),
719
+ ) []const Optional(Observation) {
720
+ if (parent == .true) return &OBS_NONE;
721
+ return switch (action.binding) {
722
+ .None => &OBS_NONE,
723
+ .started => &OBS_STARTED,
724
+ else => if (duration.binding >= 4) &OBS_ENDED else &OBS,
725
+ };
726
+ }
727
+
728
+ const BOOL_NONE = [_]Optional(bool){.None};
729
+ const BOOLS = [_]Optional(bool){ .false, .true };
730
+
731
+ /// Returns a slice with the correct range of values for hits given the `action` state
732
+ /// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
733
+ pub fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
734
+ if (parent == .true) return &BOOL_NONE;
735
+ return if (action.hit == .None) &BOOL_NONE else &BOOLS;
736
+ }
737
+
738
+ /// Returns a slice with the correct range of values for critical hits given the `action` state
739
+ /// and the state of the `parent` (whether the player's Pokémon's move hit).
740
+ pub fn criticalHit(action: Action, parent: Optional(bool)) []const Optional(bool) {
741
+ if (pkmn.options.showdown and parent == .false) return &BOOL_NONE;
742
+ return if (action.critical_hit == .None) &BOOL_NONE else &BOOLS;
743
+ }
744
+
745
+ /// Returns a slice with the correct range of values for secondary chances hits given the
746
+ /// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
747
+ pub fn secondaryChance(action: Action, parent: Optional(bool)) []const Optional(bool) {
748
+ if (parent == .false) return &BOOL_NONE;
749
+ return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
750
+ }
751
+
752
+ /// The min and max bounds on iteration over damage rolls.
753
+ pub const Range = struct { min: u9, max: u9 };
754
+
755
+ /// Returns the range bounding damage rolls given the `action` state and the state of
756
+ /// the `parent` (whether the player's Pokémon's move hit).
757
+ pub fn damage(action: Action, parent: Optional(bool)) Range {
758
+ return if (parent == .false or action.damage == 0)
759
+ .{ .min = 0, .max = 1 }
760
+ else
761
+ .{ .min = 217, .max = 256 };
762
+ }
763
+
764
+ /// Returns the max damage roll which will produce the same damage as `roll`
765
+ /// given the base damage in `summaries`.
766
+ pub fn coalesce(player: Player, roll: u8, summaries: *Summaries, cap: bool) !u8 {
767
+ if (roll == 0) return roll;
768
+
769
+ const dmg = summaries.get(player.foe()).damage;
770
+ if (dmg.base == 0 or (cap and dmg.capped)) return 255;
771
+
772
+ // Closed form solution for max damage roll provided by Orion Taylor (taylorott)
773
+ return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
774
+ }
775
+
776
+ /// Returns a slice with the correct range of values for confused given the `action` state and
777
+ /// the state of the `parent` (observation of the player's Pokémon confusion status).
778
+ pub fn confused(action: Action, parent: Optional(Confusion)) []const Optional(bool) {
779
+ const done = parent == .None or parent == .ended;
780
+ return if (done or action.confused == .None) &BOOL_NONE else &BOOLS;
781
+ }
782
+
783
+ /// Returns a slice with the correct range of values for paralysis given the `action` state
784
+ /// and the state of the `parent` (whether the player's Pokémon was confused).
785
+ pub fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
786
+ if (parent == .true) return &BOOL_NONE;
787
+ return if (action.paralyzed == .None) &BOOL_NONE else &BOOLS;
788
+ }
789
+
790
+ const SLOT_NONE = [_]u4{0};
791
+ const SLOT = [_]u4{ 1, 2, 3, 4 };
792
+
793
+ /// Returns a slice with a range of values for move slots given the `action` state
794
+ /// and the state of the `parent` (whether the player's Pokémon's move hit).
795
+ ///
796
+ /// These slots may or **may not be valid** as slots may be unset / have 0 PP.
797
+ pub fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
798
+ if (parent == .false) return &SLOT_NONE;
799
+ return if (action.move_slot == 0) &SLOT_NONE else &SLOT;
800
+ }
801
+
802
+ const MULTI_NONE = [_]u4{0};
803
+ const MULTI = [_]u4{ 2, 3, 4, 5 };
804
+
805
+ /// Returns a slice with the correct range of values for multi hit given the `action` state
806
+ /// and the state of the `parent` (whether the player's Pokémon's move hit).
807
+ pub fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
808
+ if (parent == .false) return &MULTI_NONE;
809
+ return if (action.multi_hit == 0) &MULTI_NONE else &MULTI;
810
+ }
811
+
812
+ const PSYWAVE_NONE = [_]u8{0};
813
+ const PSYWAVE = init: {
814
+ var rolls: [150]u8 = undefined;
815
+ for (0..150) |i| rolls[i] = i + 1;
816
+ break :init rolls;
817
+ };
818
+
819
+ /// Returns a slice with the correct range of values for psywave given the `action` state,
820
+ /// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
821
+ pub fn psywave(
822
+ action: Action,
823
+ side: *data.Side,
824
+ parent: Optional(bool),
825
+ ) []const u8 {
826
+ if (parent == .false) return &PSYWAVE_NONE;
827
+ return if (action.psywave == 0)
828
+ &PSYWAVE_NONE
829
+ else
830
+ PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
831
+ }
832
+
833
+ const MOVE_NONE = [_]data.Move{.None};
834
+
835
+ /// Returns a slice with the correct range of values for metronome given the `action` state.
836
+ pub fn metronome(action: Action) []const data.Move {
837
+ return if (action.metronome == .None) &MOVE_NONE else &data.Move.METRONOME;
838
+ }
839
+ };
840
+
841
+ test "Rolls.speedTie" {
842
+ const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
843
+ try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
844
+ try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
845
+ }
846
+
847
+ test "Rolls.sleep" {
848
+ return error.SkipZigTest; // TODO
849
+ }
850
+
851
+ test "Rolls.disable" {
852
+ return error.SkipZigTest; // TODO
853
+ }
854
+
855
+ test "Rolls.confusion" {
856
+ return error.SkipZigTest; // TODO
857
+ }
858
+
859
+ test "Rolls.atacking" {
860
+ return error.SkipZigTest; // TODO
861
+ }
862
+
863
+ test "Rolls.binding" {
864
+ return error.SkipZigTest; // TODO
865
+ }
866
+
867
+ test "Rolls.damage" {
868
+ const actions: Actions = .{ .p2 = .{ .damage = 221 } };
869
+ try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
870
+ try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
871
+ try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
872
+ }
873
+
874
+ test "Rolls.coalesce" {
875
+ var summaries = Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
876
+ try expectEqual(@as(u8, 0), try Rolls.coalesce(.P2, 0, &summaries, false));
877
+ try expectEqual(@as(u8, 241), try Rolls.coalesce(.P2, 238, &summaries, false));
878
+ try expectEqual(@as(u8, 255), try Rolls.coalesce(.P2, 238, &summaries, true));
879
+ summaries.p1.damage.final = 74;
880
+ try expectEqual(@as(u8, 217), try Rolls.coalesce(.P2, 217, &summaries, false));
881
+ try expectEqual(@as(u8, 255), try Rolls.coalesce(.P2, 217, &summaries, true));
882
+ }
883
+
884
+ test "Rolls.hit" {
885
+ const actions: Actions = .{ .p2 = .{ .hit = .true } };
886
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
887
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
888
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
889
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
890
+ }
891
+
892
+ test "Rolls.secondaryChance" {
893
+ const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
894
+ try expectEqualSlices(
895
+ Optional(bool),
896
+ &.{ .false, .true },
897
+ Rolls.secondaryChance(actions.p1, .None),
898
+ );
899
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
900
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
901
+ }
902
+
903
+ test "Rolls.criticalHit" {
904
+ const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
905
+ try expectEqualSlices(
906
+ Optional(bool),
907
+ &.{ .false, .true },
908
+ Rolls.criticalHit(actions.p1, .None),
909
+ );
910
+ try expectEqualSlices(
911
+ Optional(bool),
912
+ if (pkmn.options.showdown) &.{.None} else &.{ .false, .true },
913
+ Rolls.criticalHit(actions.p1, .false),
914
+ );
915
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
916
+ }
917
+
918
+ test "Rolls.confused" {
919
+ const actions: Actions = .{ .p2 = .{ .confused = .true } };
920
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1, .None));
921
+ try expectEqualSlices(
922
+ Optional(bool),
923
+ &.{ .false, .true },
924
+ Rolls.confused(actions.p2, .continuing),
925
+ );
926
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p2, .ended));
927
+ }
928
+
929
+ test "Rolls.paralyzed" {
930
+ const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
931
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
932
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
933
+ try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
934
+ try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
935
+ }
936
+
937
+ test "Rolls.moveSlot" {
938
+ const actions: Actions = .{ .p2 = .{ .move_slot = 3 } };
939
+ try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
940
+ try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
941
+ try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
942
+ }
943
+
944
+ test "Rolls.multiHit" {
945
+ const actions: Actions = .{ .p2 = .{ .multi_hit = 3 } };
946
+ try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
947
+ try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
948
+ try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
949
+ }
950
+
951
+ test "Rolls.metronome" {
952
+ const actions: Actions = .{ .p2 = .{ .metronome = .Surf } };
953
+ try expectEqualSlices(data.Move, &.{.None}, Rolls.metronome(actions.p1));
954
+ try expectEqual(@as(data.Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
955
+ }
956
+
957
+ test "Rolls.psywave" {
958
+ const actions: Actions = .{ .p2 = .{ .psywave = 79 } };
959
+ var side = helpers.Side.init(&[_]helpers.Pokemon{.{
960
+ .species = .Bulbasaur,
961
+ .level = 100,
962
+ .moves = &[_]data.Move{.Tackle},
963
+ }});
964
+
965
+ try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
966
+ var rolls = Rolls.psywave(actions.p2, &side, .None);
967
+ try expectEqual(@as(u8, 150), rolls[rolls.len - 1]);
968
+ side.stored().level = 81;
969
+ rolls = Rolls.psywave(actions.p2, &side, .None);
970
+ try expectEqual(@as(u8, 121), rolls[rolls.len - 1]);
971
+ try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
427
972
  }