@pkmn/engine 0.1.0-dev.cf6f5d2c → 0.1.0-dev.d1b39ff0

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