@pkmn/engine 0.1.0-dev.f3aaee39 → 0.1.0-dev.f4ba6ebd

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 (56) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +46 -19
  3. package/build/pkg/addon.js +6 -7
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/ids.json +12 -12
  6. package/build/pkg/data/layout.json +2 -2
  7. package/build/pkg/data/protocol.json +69 -12
  8. package/build/pkg/data.d.ts +4 -4
  9. package/build/pkg/data.js.map +1 -1
  10. package/build/pkg/gen1.js +2 -2
  11. package/build/pkg/gen1.js.map +1 -1
  12. package/build/pkg/index.d.ts +3 -2
  13. package/build/pkg/index.js.map +1 -1
  14. package/build/pkg/protocol.js +61 -42
  15. package/build/pkg/protocol.js.map +1 -1
  16. package/build/test/display.js +8 -3
  17. package/build/test/display.js.map +1 -1
  18. package/build/tools/debug.js +5 -4
  19. package/build/tools/debug.js.map +1 -1
  20. package/build.zig +227 -96
  21. package/package.json +46 -23
  22. package/src/bin/install-pkmn-engine +29 -19
  23. package/src/lib/common/array.zig +77 -0
  24. package/src/lib/common/data.zig +12 -24
  25. package/src/lib/common/debug.zig +2 -2
  26. package/src/lib/common/js.zig +4 -2
  27. package/src/lib/common/optional.zig +21 -10
  28. package/src/lib/common/options.zig +2 -0
  29. package/src/lib/common/protocol.zig +807 -327
  30. package/src/lib/common/rational.zig +125 -31
  31. package/src/lib/common/rng.zig +22 -13
  32. package/src/lib/common/util.zig +50 -1
  33. package/src/lib/gen1/calc.zig +514 -135
  34. package/src/lib/gen1/chance.zig +407 -284
  35. package/src/lib/gen1/data/moves.zig +192 -180
  36. package/src/lib/gen1/data/species.zig +3 -3
  37. package/src/lib/gen1/data/types.zig +7 -7
  38. package/src/lib/gen1/data.zig +46 -57
  39. package/src/lib/gen1/helpers.zig +95 -550
  40. package/src/lib/gen1/mechanics.zig +571 -516
  41. package/src/lib/gen2/calc.zig +161 -0
  42. package/src/lib/gen2/chance.zig +905 -0
  43. package/src/lib/gen2/data/items.zig +12 -18
  44. package/src/lib/gen2/data/moves.zig +663 -569
  45. package/src/lib/gen2/data/species.zig +1 -1
  46. package/src/lib/gen2/data/types.zig +134 -31
  47. package/src/lib/gen2/data.zig +107 -79
  48. package/src/lib/gen2/generated.zig +806 -0
  49. package/src/lib/gen2/helpers.zig +49 -21
  50. package/src/lib/gen2/mechanics.zig +3563 -9
  51. package/src/lib/{binding/node.zig → node.zig} +10 -13
  52. package/src/lib/pkmn.zig +32 -15
  53. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
  54. package/src/test/display/pkmn.html.tmpl +5 -5
  55. package/src/lib/gen1/README.md +0 -892
  56. package/src/lib/gen2/README.md +0 -308
@@ -0,0 +1,905 @@
1
+ const std = @import("std");
2
+
3
+ const assert = std.debug.assert;
4
+ const print = std.debug.print;
5
+
6
+ const expect = std.testing.expect;
7
+ const expectEqual = std.testing.expectEqual;
8
+
9
+ const options = @import("../common/options.zig");
10
+ const rational = @import("../common/rational.zig");
11
+ const rng = @import("../common/rng.zig");
12
+ const util = @import("../common/util.zig");
13
+
14
+ const Array = @import("../common/array.zig").Array;
15
+ const Player = @import("../common/data.zig").Player;
16
+ const Optional = @import("../common/optional.zig").Optional;
17
+
18
+ const data = @import("data.zig");
19
+
20
+ const enabled = options.chance;
21
+ const showdown = options.showdown;
22
+
23
+ const Gen12 = rng.Gen12;
24
+
25
+ const PointerType = util.PointerType;
26
+ const isPointerTo = util.isPointerTo;
27
+
28
+ const Move = data.Move;
29
+ const Type = data.Type;
30
+ const Effectiveness = data.Effectiveness;
31
+ const TriAttack = data.TriAttack;
32
+
33
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
34
+ const Enum = if (@hasField(std.builtin.Type, "enum")) .@"enum" else .Enum;
35
+ const Struct = if (@hasField(std.builtin.Type, "struct")) .@"struct" else .Struct;
36
+
37
+ /// Actions taken by a hypothetical "chance player" that convey information about which RNG events
38
+ /// were observed during a Generation II battle `update`. This can additionally be provided as input
39
+ /// to the `update` call to override the normal behavior of the RNG in order to force specific
40
+ /// outcomes.
41
+ pub const Actions = extern struct {
42
+ /// Information about the RNG activity for Player 1.
43
+ p1: Action = .{},
44
+ /// Information about the RNG activity for Player 2.
45
+ p2: Action = .{},
46
+
47
+ /// Returns the `Action` for the given `player`.
48
+ pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Action) {
49
+ assert(isPointerTo(self, Actions));
50
+ return if (player == .P1) &self.p1 else &self.p2;
51
+ }
52
+
53
+ /// Returns true if `a` is equal to `b`.
54
+ pub fn eql(a: Actions, b: Actions) bool {
55
+ return @as(u128, @bitCast(a.p1)) == @as(u128, @bitCast(b.p1)) and
56
+ @as(u128, @bitCast(a.p2)) == @as(u128, @bitCast(b.p2));
57
+ }
58
+
59
+ /// Returns true if `a` has the same "shape" as `b`, where `Actions` are defined to have the
60
+ /// same shape if they have the same fields set (though those fields need not necessarily be
61
+ /// set to the same value).
62
+ pub fn matches(a: Actions, b: Actions) bool {
63
+ inline for (@field(@typeInfo(Actions), @tagName(Struct)).fields) |player| {
64
+ inline for (@field(@typeInfo(Action), @tagName(Struct)).fields) |field| {
65
+ const a_val = @field(@field(a, player.name), field.name);
66
+ const b_val = @field(@field(b, player.name), field.name);
67
+
68
+ switch (@typeInfo(@TypeOf(a_val))) {
69
+ Enum => if ((@intFromEnum(a_val) > 0) != (@intFromEnum(b_val) > 0))
70
+ return false,
71
+ Int => if ((a_val > 0) != (b_val > 0)) return false,
72
+ else => unreachable,
73
+ }
74
+ }
75
+ }
76
+ return true;
77
+ }
78
+
79
+ pub fn fmt(self: Actions, writer: anytype, shape: bool) !void {
80
+ try writer.writeAll("<P1 = ");
81
+ try self.p1.fmt(writer, shape);
82
+ try writer.writeAll(", P2 = ");
83
+ try self.p2.fmt(writer, shape);
84
+ try writer.writeAll(">");
85
+ }
86
+
87
+ pub fn format(a: Actions, comptime f: []const u8, o: std.fmt.FormatOptions, w: anytype) !void {
88
+ _ = .{ f, o };
89
+ try fmt(a, w, false);
90
+ }
91
+ };
92
+
93
+ test Actions {
94
+ const a: Actions = .{ .p1 = .{ .hit = .true, .confused = .false, .damages = 245 } };
95
+ const b: Actions = .{ .p1 = .{ .hit = .false, .confused = .true, .damages = 246 } };
96
+ const c: Actions = .{ .p1 = .{ .hit = .true } };
97
+
98
+ try expect(a.eql(a));
99
+ try expect(!a.eql(b));
100
+ try expect(!b.eql(a));
101
+ try expect(!a.eql(c));
102
+ try expect(!c.eql(a));
103
+
104
+ try expect(a.matches(a));
105
+ try expect(a.matches(b));
106
+ try expect(b.matches(a));
107
+ try expect(!a.matches(c));
108
+ try expect(!c.matches(a));
109
+ }
110
+
111
+ /// Observation made about a duration - whether the duration has started, been continued, or ended.
112
+ pub const Observation = enum { started, continuing, ended };
113
+
114
+ pub const Damages = Array(5, u8);
115
+ pub const Criticals = Array(5, Optional(bool));
116
+
117
+ /// Information about the RNG that was observed during a Generation II battle `update` for a
118
+ /// single player.
119
+ pub const Action = packed struct(u128) {
120
+ /// TODO
121
+ damages: Damages.T = 0,
122
+ // TODO
123
+ critical_hits: Criticals.T = 0,
124
+
125
+ /// If not None, the Player to be returned by Rolls.speedTie.
126
+ speed_tie: Optional(Player) = .None,
127
+ /// If not None, the value to be returned for Rolls.quickClaw.
128
+ quick_claw: Optional(bool) = .None,
129
+ /// If not None, the value to return for Rolls.hit.
130
+ hit: Optional(bool) = .None,
131
+ /// If not None, the value to return for Rolls.confused.
132
+ confused: Optional(bool) = .None,
133
+
134
+ /// If not None, the value to return for Rolls.attract.
135
+ attract: Optional(bool) = .None,
136
+ /// If not None, the value to return for Rolls.paralyzed.
137
+ paralyzed: Optional(bool) = .None,
138
+ /// If not None, the value to return for Rolls.defrost.
139
+ defrost: Optional(bool) = .None,
140
+ /// If not None, the value to be returned for Rolls.secondaryChance.
141
+ secondary_chance: Optional(bool) = .None,
142
+
143
+ /// If not None, the value to be returned for Rolls.item (Focus Band / King's Rock).
144
+ item: Optional(bool) = .None,
145
+ /// If not None, the value to be returned for Rolls.protect.
146
+ protect: Optional(bool) = .None,
147
+ /// If not None, the value to return for Rolls.triAttack.
148
+ tri_attack: Optional(TriAttack) = .None,
149
+ /// If not 0, the value to return for Rolls.tripleKick.
150
+ triple_kick: u2 = 0,
151
+
152
+ /// If not 0, (present - 1) * 40 should be returned as the base power for Rolls.present.
153
+ present: u3 = 0,
154
+ /// If not 0, magnitude + 3 should be returned as the number for Rolls.magnitude.
155
+ magnitude: u3 = 0,
156
+ /// If not 0, the amount of PP to deduct for Rolls.spite.
157
+ spite: u3 = 0,
158
+ /// If not 0, the move slot (1-4) to return in Rolls.moveSlot. If present as an override,
159
+ /// invalid values (eg. due to empty move slots or 0 PP) will be ignored.
160
+ move_slot: u3 = 0,
161
+ /// If not 0, the party slot (1-6) to return in Rolls.forceSwitch. If present as an override,
162
+ /// invalid values (eg. due to empty party slots or fainted members) will be ignored.
163
+ force_switch: u3 = 0,
164
+ /// If not 0, the value (2-5) to return for Rolls.distribution for multi hit.
165
+ multi_hit: u3 = 0,
166
+
167
+ sleep: Optional(Observation) = .None,
168
+ /// TODO
169
+ confusion: Optional(Observation) = .None,
170
+ /// TODO
171
+ disable: Optional(Observation) = .None,
172
+ /// TODO
173
+ attacking: Optional(Observation) = .None,
174
+ /// TODO
175
+ binding: Optional(Observation) = .None,
176
+ /// TODO
177
+ encore: Optional(Observation) = .None,
178
+
179
+ /// If not 0, the value to by one of the Rolls.*Duration rolls.
180
+ duration: u3 = 0,
181
+ /// If not None, the value to return for Rolls.conversion2.
182
+ conversion_2: Optional(Type) = .None,
183
+
184
+ /// If not 0, psywave should be returned as the damage roll for Rolls.psywave.
185
+ psywave: u8 = 0,
186
+
187
+ /// If not None, the Move to return for Rolls.metronome.
188
+ metronome: Move = .None,
189
+
190
+ pub const Field = std.meta.FieldEnum(Action);
191
+
192
+ pub fn format(a: Action, comptime f: []const u8, o: std.fmt.FormatOptions, w: anytype) !void {
193
+ _ = .{ f, o };
194
+ try fmt(a, w, false);
195
+ }
196
+
197
+ pub fn fmt(self: Action, writer: anytype, shape: bool) !void {
198
+ try writer.writeByte('(');
199
+ var printed = false;
200
+ inline for (@field(@typeInfo(Action), @tagName(Struct)).fields) |field| {
201
+ const val = @field(self, field.name);
202
+ switch (@typeInfo(@TypeOf(val))) {
203
+ Enum => if (val != .None) {
204
+ if (printed) try writer.writeAll(", ");
205
+ if (shape) {
206
+ try writer.print("{s}:?", .{field.name});
207
+ } else if (@TypeOf(val) == Optional(bool)) {
208
+ try writer.print("{s}{s}", .{
209
+ if (val == .false) "!" else "",
210
+ field.name,
211
+ });
212
+ } else {
213
+ try writer.print("{s}:{s}", .{ field.name, @tagName(val) });
214
+ }
215
+ printed = true;
216
+ },
217
+ Int => if (val != 0) {
218
+ if (printed) try writer.writeAll(", ");
219
+ if (shape) {
220
+ try writer.print("{s}:?", .{field.name});
221
+ } else {
222
+ try writer.print("{s}:{d}", .{ field.name, val });
223
+ }
224
+ printed = true;
225
+ },
226
+ else => unreachable,
227
+ }
228
+ }
229
+ try writer.writeByte(')');
230
+ }
231
+ };
232
+
233
+ /// Tracks chance actions and their associated probability during a Generation II battle update when
234
+ /// `options.chance` is enabled.
235
+ pub fn Chance(comptime Rational: type) type {
236
+ return struct {
237
+ const Self = @This();
238
+
239
+ /// The probability of the actions taken by a hypothetical "chance player" occurring.
240
+ probability: Rational,
241
+ /// The actions taken by a hypothetical "chance player" that convey information about which
242
+ /// RNG events were observed during a battle `update`.
243
+ actions: Actions = .{},
244
+
245
+ /// Possible error returned by operations tracking chance probability.
246
+ pub const Error = Rational.Error;
247
+
248
+ /// Convenience helper to clear fields which typically should be cleared between updates.
249
+ pub fn reset(self: *Self) void {
250
+ if (!enabled) return;
251
+
252
+ self.probability.reset();
253
+ self.actions = .{};
254
+ }
255
+
256
+ pub fn speedTie(self: *Self, p1: bool) Error!void {
257
+ if (!enabled) return;
258
+
259
+ try self.probability.update(1, 2);
260
+ self.actions.p1.speed_tie = if (p1) .P1 else .P2;
261
+ self.actions.p2.speed_tie = self.actions.p1.speed_tie;
262
+ }
263
+
264
+ pub fn quickClaw(self: *Self, player: Player, proc: bool) Error!void {
265
+ if (!enabled) return;
266
+
267
+ try self.probability.update(@as(u8, if (proc) 60 else 196), 256);
268
+ self.actions.get(player).quick_claw = if (proc) .true else .false;
269
+ }
270
+
271
+ pub fn hit(self: *Self, player: Player, ok: bool, accuracy: u8) Error!void {
272
+ if (!enabled) return;
273
+
274
+ const p = if (ok) accuracy else @as(u8, @intCast(256 - @as(u9, accuracy)));
275
+ try self.probability.update(p, 256);
276
+ self.actions.get(player).hit = if (ok) .true else .false;
277
+ }
278
+
279
+ pub fn criticalHit(self: *Self, player: Player, i: u3, crit: bool, rate: u8) Error!void {
280
+ if (!enabled) return;
281
+
282
+ const n = if (crit) rate else @as(u8, @intCast(256 - @as(u9, rate)));
283
+ try self.probability.update(n, 256);
284
+ var a = self.actions.get(player);
285
+ a.critical_hits = Criticals.set(a.critical_hits, i, if (crit) .true else .false);
286
+ }
287
+
288
+ pub fn damage(self: *Self, player: Player, i: u3, roll: u8) Error!void {
289
+ if (!enabled) return;
290
+
291
+ try self.probability.update(1, 39);
292
+ var a = self.actions.get(player);
293
+ a.damages = Damages.set(a.damages, i, roll);
294
+ }
295
+
296
+ pub fn confused(self: *Self, player: Player, cfz: bool) Error!void {
297
+ if (!enabled) return;
298
+
299
+ try self.probability.update(1, 2);
300
+ self.actions.get(player).confused = if (cfz) .true else .false;
301
+ }
302
+
303
+ pub fn attract(self: *Self, player: Player, cant: bool) Error!void {
304
+ if (!enabled) return;
305
+
306
+ try self.probability.update(1, 2);
307
+ self.actions.get(player).attract = if (cant) .true else .false;
308
+ }
309
+
310
+ pub fn paralyzed(self: *Self, player: Player, par: bool) Error!void {
311
+ if (!enabled) return;
312
+
313
+ try self.probability.update(@as(u8, if (par) 1 else 3), 4);
314
+ self.actions.get(player).paralyzed = if (par) .true else .false;
315
+ }
316
+
317
+ pub fn defrost(self: *Self, player: Player, thaw: bool) Error!void {
318
+ if (!enabled) return;
319
+
320
+ try self.probability.update(@as(u8, if (thaw) 25 else 231), 256);
321
+ self.actions.get(player).defrost = if (thaw) .true else .false;
322
+ }
323
+
324
+ pub fn secondaryChance(self: *Self, player: Player, proc: bool, rate: u8) Error!void {
325
+ if (!enabled) return;
326
+
327
+ const n = if (proc) rate else @as(u8, @intCast(256 - @as(u9, rate)));
328
+ try self.probability.update(n, 256);
329
+ self.actions.get(player).secondary_chance = if (proc) .true else .false;
330
+ }
331
+
332
+ pub fn item(self: *Self, player: Player, proc: bool) Error!void {
333
+ if (!enabled) return;
334
+
335
+ try self.probability.update(@as(u8, if (proc) 30 else 226), 256);
336
+ self.actions.get(player).item = if (proc) .true else .false;
337
+ }
338
+
339
+ pub fn protect(self: *Self, player: Player, num: u8, ok: bool) Error!void {
340
+ if (!enabled) return;
341
+
342
+ try self.probability.update(@as(u8, if (ok) num + 1 else 255 - num - 1), 255);
343
+ self.actions.get(player).protect = if (ok) .true else .false;
344
+ }
345
+
346
+ pub fn triAttack(self: *Self, player: Player, status: TriAttack) Error!void {
347
+ if (!enabled) return;
348
+
349
+ try self.probability.update(1, 3);
350
+ self.actions.get(player).tri_attack = @enumFromInt(@intFromEnum(status) + 1);
351
+ }
352
+
353
+ const PRESENT: [4]u8 = if (showdown) .{ 2, 4, 3, 1 } else .{ 51, 103, 77, 25 };
354
+
355
+ pub fn present(self: *Self, player: Player, power: u8) Error!void {
356
+ if (!enabled) return;
357
+
358
+ const index = power / 40;
359
+ try self.probability.update(PRESENT[index], if (showdown) 10 else 256);
360
+ self.actions.get(player).present = @intCast(index + 1);
361
+ }
362
+
363
+ const MAGNITUDE: [7]u8 = if (showdown)
364
+ .{ 5, 10, 20, 30, 20, 10, 5 }
365
+ else
366
+ .{ 14, 26, 50, 77, 51, 25, 13 };
367
+
368
+ pub fn magnitude(self: *Self, player: Player, num: u8) Error!void {
369
+ if (!enabled) return;
370
+
371
+ const index = num - 4;
372
+ try self.probability.update(MAGNITUDE[index], if (showdown) 100 else 256);
373
+ self.actions.get(player).magnitude = @intCast(index + 1);
374
+ }
375
+
376
+ pub fn tripleKick(self: *Self, player: Player, hits: u2) Error!void {
377
+ if (!enabled) return;
378
+
379
+ try self.probability.update(1, 3);
380
+ self.actions.get(player).triple_kick = hits;
381
+ }
382
+
383
+ pub fn spite(self: *Self, player: Player, pp: u3) Error!void {
384
+ if (!enabled) return;
385
+
386
+ try self.probability.update(1, 4);
387
+ self.actions.get(player).spite = pp;
388
+ }
389
+
390
+ pub fn conversion2(self: *Self, player: Player, ty: Type, mtype: Type, num: u8) Error!void {
391
+ if (!enabled) return;
392
+
393
+ assert(showdown or num == 0);
394
+ const n = if (num != 0)
395
+ num
396
+ else n: {
397
+ const neutral = @intFromEnum(Effectiveness.Neutral);
398
+ var i: u8 = 0;
399
+ for (0..Type.size) |t| {
400
+ if (@intFromEnum(mtype.effectiveness(@enumFromInt(t))) < neutral) i += 1;
401
+ }
402
+ assert(i > 0 and i <= 7);
403
+ break :n i;
404
+ };
405
+
406
+ try self.probability.update(1, n);
407
+ self.actions.get(player).conversion_2 = @enumFromInt(@intFromEnum(ty) + 1);
408
+ }
409
+
410
+ pub fn moveSlot(self: *Self, player: Player, slot: u4, n: u4) Error!void {
411
+ if (!enabled) return;
412
+
413
+ if (n != 1) try self.probability.update(1, n);
414
+ self.actions.get(player).move_slot = @intCast(slot);
415
+ }
416
+
417
+ pub fn forceSwitch(self: *Self, player: Player, slot: u4, n: u4) Error!void {
418
+ if (!enabled) return;
419
+
420
+ if (n != 1) try self.probability.update(1, n);
421
+ self.actions.get(player).force_switch = @intCast(slot);
422
+ }
423
+
424
+ pub fn psywave(self: *Self, player: Player, power: u8, max: u8) Error!void {
425
+ if (!enabled) return;
426
+
427
+ try self.probability.update(1, max);
428
+ self.actions.get(player).psywave = power;
429
+ }
430
+
431
+ pub fn metronome(self: *Self, player: Player, move: Move, n: u2) Error!void {
432
+ if (!enabled) return;
433
+
434
+ try self.probability.update(1, Move.METRONOME.len - @as(u8, n));
435
+ self.actions.get(player).metronome = move;
436
+ }
437
+
438
+ pub fn multiHit(self: *Self, player: Player, n: u3) Error!void {
439
+ if (!enabled) return;
440
+
441
+ try self.probability.update(@as(u8, if (n > 3) 1 else 3), 8);
442
+ self.actions.get(player).multi_hit = n;
443
+ }
444
+
445
+ pub fn duration(self: *Self, player: Player, turns: u3) void {
446
+ if (!enabled) return;
447
+
448
+ self.actions.get(player).duration = if (options.key) 2 else turns;
449
+ }
450
+ };
451
+ }
452
+
453
+ test "Chance.speedTie" {
454
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
455
+
456
+ try chance.speedTie(true);
457
+ try expectProbability(&chance.probability, 1, 2);
458
+ try expectValue(Optional(Player).P1, chance.actions.p1.speed_tie);
459
+ try expectValue(chance.actions.p1.speed_tie, chance.actions.p2.speed_tie);
460
+
461
+ chance.reset();
462
+
463
+ try chance.speedTie(false);
464
+ try expectProbability(&chance.probability, 1, 2);
465
+ try expectValue(Optional(Player).P2, chance.actions.p1.speed_tie);
466
+ try expectValue(chance.actions.p1.speed_tie, chance.actions.p2.speed_tie);
467
+ }
468
+
469
+ test "Chance.quickClaw" {
470
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
471
+
472
+ try chance.quickClaw(.P1, false);
473
+ try expectProbability(&chance.probability, 49, 64);
474
+ try expectValue(Optional(bool).false, chance.actions.p1.quick_claw);
475
+
476
+ chance.reset();
477
+
478
+ try chance.quickClaw(.P2, true);
479
+ try expectProbability(&chance.probability, 15, 64);
480
+ try expectValue(Optional(bool).true, chance.actions.p2.quick_claw);
481
+ }
482
+
483
+ test "Chance.hit" {
484
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
485
+
486
+ try chance.hit(.P1, true, 229);
487
+ try expectValue(Optional(bool).true, chance.actions.p1.hit);
488
+ try expectProbability(&chance.probability, 229, 256);
489
+
490
+ chance.reset();
491
+
492
+ try chance.hit(.P2, false, 229);
493
+ try expectValue(Optional(bool).false, chance.actions.p2.hit);
494
+ try expectProbability(&chance.probability, 27, 256);
495
+ }
496
+
497
+ test "Chance.criticalHit" {
498
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
499
+
500
+ try chance.criticalHit(.P1, 0, true, 17);
501
+ try expectValue(Optional(bool).true, Criticals.get(chance.actions.p1.critical_hits, 0));
502
+ try expectProbability(&chance.probability, 17, 256);
503
+
504
+ chance.reset();
505
+
506
+ try chance.criticalHit(.P2, 1, false, 5);
507
+ try expectProbability(&chance.probability, 251, 256);
508
+ try expectValue(Optional(bool).false, Criticals.get(chance.actions.p2.critical_hits, 1));
509
+ }
510
+
511
+ test "Chance.damage" {
512
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
513
+
514
+ try chance.damage(.P1, 0, 219);
515
+ try expectValue(@as(u8, 219), Damages.get(chance.actions.p1.damages, 0));
516
+ try expectProbability(&chance.probability, 1, 39);
517
+ }
518
+
519
+ test "Chance.confused" {
520
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
521
+
522
+ try chance.confused(.P1, false);
523
+ try expectProbability(&chance.probability, 1, 2);
524
+ try expectValue(Optional(bool).false, chance.actions.p1.confused);
525
+
526
+ chance.reset();
527
+
528
+ try chance.confused(.P2, true);
529
+ try expectProbability(&chance.probability, 1, 2);
530
+ try expectValue(Optional(bool).true, chance.actions.p2.confused);
531
+ }
532
+
533
+ test "Chance.attract" {
534
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
535
+
536
+ try chance.attract(.P1, false);
537
+ try expectProbability(&chance.probability, 1, 2);
538
+ try expectValue(Optional(bool).false, chance.actions.p1.attract);
539
+
540
+ chance.reset();
541
+
542
+ try chance.attract(.P2, true);
543
+ try expectProbability(&chance.probability, 1, 2);
544
+ try expectValue(Optional(bool).true, chance.actions.p2.attract);
545
+ }
546
+
547
+ test "Chance.paralyzed" {
548
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
549
+
550
+ try chance.paralyzed(.P1, false);
551
+ try expectProbability(&chance.probability, 3, 4);
552
+ try expectValue(Optional(bool).false, chance.actions.p1.paralyzed);
553
+
554
+ chance.reset();
555
+
556
+ try chance.paralyzed(.P2, true);
557
+ try expectProbability(&chance.probability, 1, 4);
558
+ try expectValue(Optional(bool).true, chance.actions.p2.paralyzed);
559
+ }
560
+
561
+ test "Chance.defrost" {
562
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
563
+
564
+ try chance.defrost(.P1, false);
565
+ try expectProbability(&chance.probability, 231, 256);
566
+ try expectValue(Optional(bool).false, chance.actions.p1.defrost);
567
+
568
+ chance.reset();
569
+
570
+ try chance.defrost(.P2, true);
571
+ try expectProbability(&chance.probability, 25, 256);
572
+ try expectValue(Optional(bool).true, chance.actions.p2.defrost);
573
+ }
574
+
575
+ test "Chance.secondaryChance" {
576
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
577
+
578
+ try chance.secondaryChance(.P1, true, 25);
579
+ try expectProbability(&chance.probability, 25, 256);
580
+ try expectValue(Optional(bool).true, chance.actions.p1.secondary_chance);
581
+
582
+ chance.reset();
583
+
584
+ try chance.secondaryChance(.P2, false, 77);
585
+ try expectProbability(&chance.probability, 179, 256);
586
+ try expectValue(Optional(bool).false, chance.actions.p2.secondary_chance);
587
+ }
588
+
589
+ test "Chance.item" {
590
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
591
+
592
+ try chance.item(.P1, false);
593
+ try expectProbability(&chance.probability, 113, 128);
594
+ try expectValue(Optional(bool).false, chance.actions.p1.item);
595
+
596
+ chance.reset();
597
+
598
+ try chance.item(.P2, true);
599
+ try expectProbability(&chance.probability, 15, 128);
600
+ try expectValue(Optional(bool).true, chance.actions.p2.item);
601
+ }
602
+
603
+ test "Chance.protect" {
604
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
605
+
606
+ try chance.protect(.P1, 15, false);
607
+ try expectProbability(&chance.probability, 239, 255);
608
+ try expectValue(Optional(bool).false, chance.actions.p1.protect);
609
+
610
+ chance.reset();
611
+
612
+ try chance.protect(.P2, 63, true);
613
+ try expectProbability(&chance.probability, 64, 255);
614
+ try expectValue(Optional(bool).true, chance.actions.p2.protect);
615
+ }
616
+
617
+ test "Chance.triAttack" {
618
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
619
+
620
+ try chance.triAttack(.P1, .BRN);
621
+ try expectProbability(&chance.probability, 1, 3);
622
+ try expectValue(Optional(TriAttack).BRN, chance.actions.p1.tri_attack);
623
+
624
+ chance.reset();
625
+
626
+ try chance.triAttack(.P2, .FRZ);
627
+ try expectProbability(&chance.probability, 1, 3);
628
+ try expectValue(Optional(TriAttack).FRZ, chance.actions.p2.tri_attack);
629
+ }
630
+
631
+ test "Chance.present" {
632
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
633
+
634
+ try chance.present(.P2, 120);
635
+ if (showdown) {
636
+ try expectProbability(&chance.probability, 1, 10);
637
+ } else {
638
+ try expectProbability(&chance.probability, 25, 256);
639
+ }
640
+ try expectValue(@as(u3, 4), chance.actions.p2.present);
641
+
642
+ chance.reset();
643
+
644
+ try chance.present(.P1, 0);
645
+ if (showdown) {
646
+ try expectProbability(&chance.probability, 1, 5);
647
+ } else {
648
+ try expectProbability(&chance.probability, 51, 256);
649
+ }
650
+ try expectValue(@as(u3, 1), chance.actions.p1.present);
651
+ }
652
+
653
+ test "Chance.magnitude" {
654
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
655
+
656
+ try chance.magnitude(.P2, 10);
657
+ if (showdown) {
658
+ try expectProbability(&chance.probability, 1, 20);
659
+ } else {
660
+ try expectProbability(&chance.probability, 13, 256);
661
+ }
662
+ try expectValue(@as(u3, 7), chance.actions.p2.magnitude);
663
+
664
+ chance.reset();
665
+
666
+ try chance.magnitude(.P1, 8);
667
+ if (showdown) {
668
+ try expectProbability(&chance.probability, 1, 5);
669
+ } else {
670
+ try expectProbability(&chance.probability, 51, 256);
671
+ }
672
+ try expectValue(@as(u3, 5), chance.actions.p1.magnitude);
673
+ }
674
+
675
+ test "Chance.tripleKick" {
676
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
677
+
678
+ try chance.tripleKick(.P1, 2);
679
+ try expectProbability(&chance.probability, 1, 3);
680
+ try expectValue(@as(u2, 2), chance.actions.p1.triple_kick);
681
+ }
682
+
683
+ test "Chance.spite" {
684
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
685
+
686
+ try chance.spite(.P1, 3);
687
+ try expectProbability(&chance.probability, 1, 4);
688
+ try expectValue(@as(u3, 3), chance.actions.p1.spite);
689
+ }
690
+
691
+ test "Chance.conversion2" {
692
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
693
+
694
+ if (showdown) {
695
+ try chance.conversion2(.P1, .Normal, .Ghost, 3);
696
+ try expectProbability(&chance.probability, 1, 3);
697
+ try expectValue(Optional(Type).Normal, chance.actions.p1.conversion_2);
698
+ } else {
699
+ try chance.conversion2(.P2, .Normal, .Ghost, 0);
700
+ try expectProbability(&chance.probability, 1, 3);
701
+ try expectValue(Optional(Type).Normal, chance.actions.p2.conversion_2);
702
+ }
703
+ chance.reset();
704
+
705
+ try chance.conversion2(.P2, .Fire, .Bug, 0);
706
+ try expectProbability(&chance.probability, 1, 6);
707
+ try expectValue(Optional(Type).Fire, chance.actions.p2.conversion_2);
708
+ }
709
+
710
+ test "Chance.psywave" {
711
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
712
+
713
+ try chance.psywave(.P2, 100, 150);
714
+ try expectProbability(&chance.probability, 1, 150);
715
+ try expectValue(@as(u8, 100), chance.actions.p2.psywave);
716
+ }
717
+
718
+ test "Chance.metronome" {
719
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
720
+
721
+ try chance.metronome(.P1, Move.HornAttack, 1);
722
+ try expectProbability(&chance.probability, 1, 238);
723
+ try expectValue(Move.HornAttack, chance.actions.p1.metronome);
724
+ }
725
+
726
+ test "Chance.moveSlot" {
727
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
728
+
729
+ try chance.moveSlot(.P2, 2, 2);
730
+ try expectProbability(&chance.probability, 1, 2);
731
+ try expectValue(@as(u3, 2), chance.actions.p2.move_slot);
732
+
733
+ chance.reset();
734
+
735
+ try chance.moveSlot(.P1, 1, 3);
736
+ try expectProbability(&chance.probability, 1, 3);
737
+ try expectValue(@as(u3, 1), chance.actions.p1.move_slot);
738
+
739
+ chance.reset();
740
+
741
+ try chance.moveSlot(.P1, 4, 1);
742
+ try expectProbability(&chance.probability, 1, 1);
743
+ try expectValue(@as(u3, 4), chance.actions.p1.move_slot);
744
+ }
745
+
746
+ test "Chance.forceSwitch" {
747
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
748
+
749
+ try chance.forceSwitch(.P2, 2, 2);
750
+ try expectProbability(&chance.probability, 1, 2);
751
+ try expectValue(@as(u3, 2), chance.actions.p2.force_switch);
752
+
753
+ chance.reset();
754
+
755
+ try chance.forceSwitch(.P1, 1, 3);
756
+ try expectProbability(&chance.probability, 1, 3);
757
+ try expectValue(@as(u3, 1), chance.actions.p1.force_switch);
758
+
759
+ chance.reset();
760
+
761
+ try chance.forceSwitch(.P1, 4, 1);
762
+ try expectProbability(&chance.probability, 1, 1);
763
+ try expectValue(@as(u3, 4), chance.actions.p1.force_switch);
764
+ }
765
+
766
+ test "Chance.multiHit" {
767
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
768
+
769
+ try chance.multiHit(.P1, 3);
770
+ try expectProbability(&chance.probability, 3, 8);
771
+ try expectValue(@as(u3, 3), chance.actions.p1.multi_hit);
772
+
773
+ chance.reset();
774
+
775
+ try chance.multiHit(.P2, 5);
776
+ try expectProbability(&chance.probability, 1, 8);
777
+ try expectValue(@as(u3, 5), chance.actions.p2.multi_hit);
778
+ }
779
+
780
+ test "Chance.duration" {
781
+ var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
782
+
783
+ chance.duration(.P1, 2);
784
+ try expectValue(@as(u3, 2), chance.actions.p1.duration);
785
+ }
786
+
787
+ pub fn expectProbability(r: anytype, p: u64, q: u64) !void {
788
+ if (!enabled) return;
789
+
790
+ r.reduce();
791
+ if (r.p != p or r.q != q) {
792
+ print("expected {d}/{d}, found {}\n", .{ p, q, r });
793
+ return error.TestExpectedEqual;
794
+ }
795
+ }
796
+
797
+ pub fn expectValue(a: anytype, b: anytype) !void {
798
+ if (!enabled) return;
799
+
800
+ try expectEqual(a, b);
801
+ }
802
+
803
+ /// Null object pattern implementation of Generation II `Chance` which does nothing, though chance
804
+ /// tracking should additionally be turned off entirely via `options.chance`.
805
+ pub const NULL: Null = .{};
806
+
807
+ const Null = struct {
808
+ pub const Error = error{};
809
+
810
+ pub fn switched(self: Null, player: Player, in: u8, out: u8) void {
811
+ _ = .{ self, player, in, out };
812
+ }
813
+
814
+ pub fn speedTie(self: Null, p1: bool) Error!void {
815
+ _ = .{ self, p1 };
816
+ }
817
+
818
+ pub fn quickClaw(self: Null, player: Player, proc: bool) Error!void {
819
+ _ = .{ self, player, proc };
820
+ }
821
+
822
+ pub fn hit(self: Null, player: Player, ok: bool, accuracy: u8) Error!void {
823
+ _ = .{ self, player, ok, accuracy };
824
+ }
825
+
826
+ pub fn criticalHit(self: Null, player: Player, i: u3, crit: bool, rate: u8) Error!void {
827
+ _ = .{ self, player, i, crit, rate };
828
+ }
829
+
830
+ pub fn damage(self: Null, player: Player, i: u3, roll: u8) Error!void {
831
+ _ = .{ self, player, i, roll };
832
+ }
833
+
834
+ pub fn confused(self: Null, player: Player, ok: bool) Error!void {
835
+ _ = .{ self, player, ok };
836
+ }
837
+
838
+ pub fn attract(self: Null, player: Player, cant: bool) Error!void {
839
+ _ = .{ self, player, cant };
840
+ }
841
+
842
+ pub fn paralyzed(self: Null, player: Player, par: bool) Error!void {
843
+ _ = .{ self, player, par };
844
+ }
845
+
846
+ pub fn defrost(self: Null, player: Player, thaw: bool) Error!void {
847
+ _ = .{ self, player, thaw };
848
+ }
849
+
850
+ pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
851
+ _ = .{ self, player, proc, rate };
852
+ }
853
+
854
+ pub fn item(self: Null, player: Player, proc: bool) Error!void {
855
+ _ = .{ self, player, proc };
856
+ }
857
+
858
+ pub fn protect(self: Null, player: Player, num: u8, ok: bool) Error!void {
859
+ _ = .{ self, player, num, ok };
860
+ }
861
+
862
+ pub fn triAttack(self: Null, player: Player, status: TriAttack) Error!void {
863
+ _ = .{ self, player, status };
864
+ }
865
+
866
+ pub fn present(self: Null, player: Player, power: u8) Error!void {
867
+ _ = .{ self, player, power };
868
+ }
869
+
870
+ pub fn magnitude(self: Null, player: Player, num: u8) Error!void {
871
+ _ = .{ self, player, num };
872
+ }
873
+
874
+ pub fn spite(self: Null, player: Player, pp: u3) Error!void {
875
+ _ = .{ self, player, pp };
876
+ }
877
+
878
+ pub fn conversion2(self: Null, player: Player, ty: Type, mtype: Type, num: u8) Error!void {
879
+ _ = .{ self, player, ty, mtype, num };
880
+ }
881
+
882
+ pub fn moveSlot(self: Null, player: Player, slot: u4, n: u4) Error!void {
883
+ _ = .{ self, player, slot, n };
884
+ }
885
+
886
+ pub fn forceSwitch(self: Null, player: Player, slot: u4, n: u4) Error!void {
887
+ _ = .{ self, player, slot, n };
888
+ }
889
+
890
+ pub fn multiHit(self: Null, player: Player, n: u3) Error!void {
891
+ _ = .{ self, player, n };
892
+ }
893
+
894
+ pub fn psywave(self: Null, player: Player, power: u8, max: u8) Error!void {
895
+ _ = .{ self, player, power, max };
896
+ }
897
+
898
+ pub fn metronome(self: Null, player: Player, move: Move, n: u2) Error!void {
899
+ _ = .{ self, player, move, n };
900
+ }
901
+
902
+ pub fn duration(self: Null, player: Player, turns: u3) void {
903
+ _ = .{ self, player, turns };
904
+ }
905
+ };