@pkmn/engine 0.1.0-dev.63cef311 → 0.1.0-dev.645b4750

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 (84) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +189 -77
  3. package/build/pkg/addon/browser.d.ts +3 -0
  4. package/build/pkg/addon/browser.js +47 -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 +110 -0
  8. package/build/pkg/addon/node.js.map +1 -0
  9. package/build/pkg/addon/wasm.d.ts +2 -0
  10. package/build/pkg/addon/wasm.js +53 -0
  11. package/build/pkg/addon/wasm.js.map +1 -0
  12. package/build/pkg/addon.d.ts +29 -0
  13. package/build/pkg/addon.js +80 -0
  14. package/build/pkg/addon.js.map +1 -0
  15. package/build/pkg/data/ids.json +12 -12
  16. package/build/pkg/data/layout.json +40 -5
  17. package/build/pkg/data/protocol.json +84 -13
  18. package/build/pkg/data.d.ts +28 -7
  19. package/build/pkg/data.js +3 -5
  20. package/build/pkg/data.js.map +1 -1
  21. package/build/pkg/gen1.d.ts +18 -20
  22. package/build/pkg/gen1.js +76 -51
  23. package/build/pkg/gen1.js.map +1 -1
  24. package/build/pkg/index.d.ts +57 -103
  25. package/build/pkg/index.js +23 -70
  26. package/build/pkg/index.js.map +1 -1
  27. package/build/pkg/protocol.d.ts +24 -3
  28. package/build/pkg/protocol.js +127 -104
  29. package/build/pkg/protocol.js.map +1 -1
  30. package/build/tools/debug.js +110 -0
  31. package/build/tools/debug.js.map +1 -0
  32. package/build/tools/display/debug.jsx +152 -0
  33. package/build/tools/display/debug.jsx.map +1 -0
  34. package/build/tools/display/index.js +152 -0
  35. package/build/tools/display/index.js.map +1 -0
  36. package/build/tools/display/ui.jsx +359 -0
  37. package/build/tools/display/ui.jsx.map +1 -0
  38. package/build/tools/display/util.js +33 -0
  39. package/build/tools/display/util.js.map +1 -0
  40. package/build.zig +417 -160
  41. package/package.json +92 -133
  42. package/src/bin/install-pkmn-engine +85 -42
  43. package/src/bin/pkmn-debug +62 -0
  44. package/src/lib/bindings/c.zig +255 -0
  45. package/src/lib/bindings/node.zig +121 -0
  46. package/src/lib/bindings/wasm.zig +59 -0
  47. package/src/lib/common/array.zig +77 -0
  48. package/src/lib/common/battle.zig +17 -0
  49. package/src/lib/common/data.zig +19 -31
  50. package/src/lib/common/debug.zig +58 -17
  51. package/src/lib/common/js.zig +120 -0
  52. package/src/lib/common/optional.zig +71 -0
  53. package/src/lib/common/options.zig +18 -3
  54. package/src/lib/common/protocol.zig +947 -407
  55. package/src/lib/common/rational.zig +522 -0
  56. package/src/lib/common/rng.zig +31 -24
  57. package/src/lib/common/util.zig +76 -0
  58. package/src/lib/gen1/calc.zig +962 -0
  59. package/src/lib/gen1/chance.zig +1185 -0
  60. package/src/lib/gen1/data/moves.zig +203 -193
  61. package/src/lib/gen1/data/species.zig +6 -8
  62. package/src/lib/gen1/data/types.zig +11 -12
  63. package/src/lib/gen1/data.zig +100 -89
  64. package/src/lib/gen1/helpers.zig +139 -54
  65. package/src/lib/gen1/mechanics.zig +1405 -910
  66. package/src/lib/gen2/calc.zig +153 -0
  67. package/src/lib/gen2/chance.zig +902 -0
  68. package/src/lib/gen2/data/items.zig +15 -23
  69. package/src/lib/gen2/data/moves.zig +665 -573
  70. package/src/lib/gen2/data/species.zig +3 -5
  71. package/src/lib/gen2/data/types.zig +138 -36
  72. package/src/lib/gen2/data.zig +134 -99
  73. package/src/lib/gen2/generated.zig +801 -0
  74. package/src/lib/gen2/helpers.zig +59 -44
  75. package/src/lib/gen2/mechanics.zig +3582 -30
  76. package/src/lib/node.zig +18 -0
  77. package/src/lib/pkmn.zig +93 -12
  78. package/src/lib/wasm.zig +17 -0
  79. package/src/tools/display/dom.ts +93 -0
  80. package/src/tools/display/ui.css +354 -0
  81. package/src/lib/binding/node.zig +0 -229
  82. package/src/lib/binding/wasm.zig +0 -10
  83. package/src/lib/gen1/README.md +0 -778
  84. package/src/lib/gen2/README.md +0 -309
@@ -0,0 +1,522 @@
1
+ const builtin = @import("builtin");
2
+ const std = @import("std");
3
+
4
+ const assert = std.debug.assert;
5
+ const expect = std.testing.expect;
6
+ const expectEqual = std.testing.expectEqual;
7
+ const expectError = std.testing.expectError;
8
+
9
+ const Int = if (@hasField(std.builtin.Type, "int")) .int else .Int;
10
+ const Float = if (@hasField(std.builtin.Type, "float")) .float else .Float;
11
+ const ComptimeInt =
12
+ if (@hasField(std.builtin.Type, "comptime_int")) .comptime_int else .ComptimeInt;
13
+ const ComptimeFloat =
14
+ if (@hasField(std.builtin.Type, "comptime_float")) .comptime_float else .ComptimeFloat;
15
+
16
+ /// Specialization of a rational number used by the engine to compute probabilties.
17
+ /// For performance reasons the rational is only reduced lazily and thus `reduce` must be
18
+ /// invoked explicitly before reading. Note that this laziness means that this implementation
19
+ /// sometimes overflows in places where an eager reduced implementation would not - to achieve
20
+ /// the same behavior as an eager implementation simply call `reduce` after each operation.
21
+ pub fn Rational(comptime T: type) type {
22
+ // With floats we can't rely on an overflow bit to let us know when to reduce, so we
23
+ // instead start reducing when we get sufficiently close to the limit of the mantissa
24
+ // (in our domain we expect updates to involve numbers < 2**10, so we should be safe
25
+ // not reducing before we are 2**10 away from "overflowing" the mantissa)
26
+ const o = if (@typeInfo(T) == Float)
27
+ std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
28
+ else
29
+ 0;
30
+
31
+ const Err = switch (@typeInfo(T)) {
32
+ Int => error{Overflow},
33
+ Float => error{},
34
+ else => unreachable,
35
+ };
36
+
37
+ // Zig doesn't allow integers > u128 in an extern struct...
38
+ return if (@sizeOf(T) <= 16) extern struct {
39
+ const Self = @This();
40
+
41
+ /// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
42
+ p: T = 1,
43
+ /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
44
+ q: T = 1,
45
+
46
+ /// Possible error returned by operations on the Rational.
47
+ pub const Error = Err;
48
+
49
+ /// Resets the rational back to 1.
50
+ pub fn reset(r: *Self) void {
51
+ r.p = 1;
52
+ r.q = 1;
53
+ }
54
+
55
+ /// Update the rational by multiplying its numerator by p and its denominator by q.
56
+ /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
57
+ pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
58
+ return update_(T, o, r, p, q);
59
+ }
60
+
61
+ /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
62
+ pub fn add(r: *Self, s: anytype) Error!void {
63
+ return add_(T, o, r, s);
64
+ }
65
+
66
+ /// Multiplies two rationals.
67
+ pub fn mul(r: *Self, s: anytype) Error!void {
68
+ return mul_(T, o, r, s);
69
+ }
70
+
71
+ /// Normalize the rational by reducing by the greatest common divisor.
72
+ pub fn reduce(r: *Self) void {
73
+ reduce_(r);
74
+ }
75
+
76
+ pub fn format(
77
+ self: Self,
78
+ comptime fmt: []const u8,
79
+ opts: std.fmt.FormatOptions,
80
+ writer: anytype,
81
+ ) !void {
82
+ _ = .{ fmt, opts };
83
+ try writer.print("{d}/{d}", .{ self.p, self.q });
84
+ }
85
+ } else struct {
86
+ const Self = @This();
87
+
88
+ /// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
89
+ p: T = 1,
90
+ /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
91
+ q: T = 1,
92
+
93
+ /// Possible error returned by operations on the Rational.
94
+ pub const Error = Err;
95
+
96
+ /// Resets the rational back to 1.
97
+ pub fn reset(r: *Self) void {
98
+ r.p = 1;
99
+ r.q = 1;
100
+ }
101
+
102
+ /// Update the rational by multiplying its numerator by p and its denominator by q.
103
+ /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
104
+ pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
105
+ return update_(T, o, r, p, q);
106
+ }
107
+
108
+ /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
109
+ pub fn add(r: *Self, s: anytype) Error!void {
110
+ return add_(T, o, r, s);
111
+ }
112
+
113
+ /// Multiplies two rationals.
114
+ pub fn mul(r: *Self, s: anytype) Error!void {
115
+ return mul_(T, o, r, s);
116
+ }
117
+
118
+ /// Normalize the rational by reducing by the greatest common divisor.
119
+ pub fn reduce(r: *Self) void {
120
+ reduce_(r);
121
+ }
122
+
123
+ pub fn format(
124
+ self: Self,
125
+ comptime fmt: []const u8,
126
+ opts: std.fmt.FormatOptions,
127
+ writer: anytype,
128
+ ) !void {
129
+ _ = .{ fmt, opts };
130
+ try writer.print("{d}/{d}", .{ self.p, self.q });
131
+ }
132
+ };
133
+ }
134
+
135
+ fn update_(comptime T: type, comptime o: comptime_int, r: anytype, p: anytype, q: anytype) !void {
136
+ // std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
137
+ assert(p >= 1);
138
+ assert(q >= 1);
139
+
140
+ // If our parameters are not fully reduced they may prematurely
141
+ // cause overflow/loss of precision after the multiplication below
142
+ assert(switch (@typeInfo(@TypeOf(p, q))) {
143
+ ComptimeInt, ComptimeFloat => comptime gcd(p, q),
144
+ else => 1,
145
+ } == 1);
146
+
147
+ switch (@typeInfo(T)) {
148
+ Int => {
149
+ // Greedily attempt to multiply and if it fails, reduce and try again
150
+ multiplication(T, r, p, q) catch |err| switch (err) {
151
+ error.Overflow => {
152
+ r.reduce();
153
+ try multiplication(T, r, p, q);
154
+ },
155
+ else => unreachable,
156
+ };
157
+ },
158
+ Float => {
159
+ // Reduce in situations where we're likely to start losing precision
160
+ if (r.q > o or r.p > o) r.reduce();
161
+
162
+ r.p *= switch (@typeInfo(@TypeOf(p))) {
163
+ Float, ComptimeFloat => p,
164
+ else => @floatFromInt(p),
165
+ };
166
+ r.q *= switch (@typeInfo(@TypeOf(q))) {
167
+ Float, ComptimeFloat => q,
168
+ else => @floatFromInt(q),
169
+ };
170
+
171
+ // We should always be dealing with whole numbers
172
+ assert(std.math.modf(r.p).fpart == 0);
173
+ assert(std.math.modf(r.q).fpart == 0);
174
+ },
175
+ else => unreachable,
176
+ }
177
+ }
178
+
179
+ fn add_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
180
+ switch (@typeInfo(T)) {
181
+ Int => {
182
+ if (r.q == s.q) {
183
+ r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
184
+ error.Overflow => val: {
185
+ r.reduce();
186
+ s.reduce();
187
+ break :val try std.math.add(T, r.p, s.p);
188
+ },
189
+ else => unreachable,
190
+ };
191
+ } else {
192
+ addition(T, r, s.p, s.q) catch |err| switch (err) {
193
+ error.Overflow => {
194
+ r.reduce();
195
+ s.reduce();
196
+ try addition(T, r, s.p, s.q);
197
+ },
198
+ else => unreachable,
199
+ };
200
+ }
201
+ },
202
+ Float => {
203
+ if (r.q == s.q) {
204
+ if (r.p > o) r.reduce();
205
+ if (s.p > o) s.reduce();
206
+
207
+ r.p += s.p;
208
+ } else {
209
+ // Always reduce to minimize loss of precision from the multiplications
210
+ r.reduce();
211
+ s.reduce();
212
+
213
+ r.p = (r.p * s.q) + (r.q * s.p);
214
+ r.q *= s.q;
215
+ }
216
+
217
+ assert(std.math.modf(r.p).fpart == 0);
218
+ assert(std.math.modf(r.q).fpart == 0);
219
+ },
220
+ else => unreachable,
221
+ }
222
+ }
223
+
224
+ fn mul_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
225
+ switch (@typeInfo(T)) {
226
+ Int => {
227
+ multiplication(T, r, s.p, s.q) catch |err| switch (err) {
228
+ error.Overflow => {
229
+ r.reduce();
230
+ s.reduce();
231
+ try multiplication(T, r, s.p, s.q);
232
+ },
233
+ else => unreachable,
234
+ };
235
+ },
236
+ Float => {
237
+ if (r.q > o or r.p > o) r.reduce();
238
+ if (s.q > o or s.p > o) s.reduce();
239
+
240
+ r.p *= s.p;
241
+ r.q *= s.q;
242
+
243
+ assert(std.math.modf(r.p).fpart == 0);
244
+ assert(std.math.modf(r.q).fpart == 0);
245
+ },
246
+ else => unreachable,
247
+ }
248
+ }
249
+
250
+ fn reduce_(r: anytype) void {
251
+ const d = gcd(r.p, r.q);
252
+ if (d == 1) return;
253
+
254
+ assert(@mod(r.p, d) == 0);
255
+ assert(@mod(r.q, d) == 0);
256
+
257
+ r.p /= d;
258
+ r.q /= d;
259
+
260
+ assert(r.p >= 1);
261
+ assert(r.q >= 1);
262
+ }
263
+
264
+ fn multiplication(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
265
+ r.p = try std.math.mul(T, r.p, p);
266
+ r.q = try std.math.mul(T, r.q, q);
267
+ }
268
+
269
+ fn addition(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
270
+ // (a/b) + (c/d) = (ad+bc)/(bd)
271
+ const d = try std.math.mul(T, r.q, q);
272
+ const n1 = try std.math.mul(T, r.p, q);
273
+ const n2 = try std.math.mul(T, r.q, p);
274
+
275
+ r.p = try std.math.add(T, n1, n2);
276
+ r.q = d;
277
+ }
278
+
279
+ fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
280
+ assert(p >= 1);
281
+ assert(q >= 1);
282
+
283
+ // convert comptime_int to a sized integer within this function so that @ctz will work
284
+ const T = switch (@TypeOf(p, q)) {
285
+ comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
286
+ else => |U| U,
287
+ };
288
+
289
+ switch (@typeInfo(T)) {
290
+ Int => {
291
+ // std.math.gcd but without some checks because we have a stricter range
292
+ var x: T = @intCast(p);
293
+ var y: T = @intCast(q);
294
+
295
+ const xz = @ctz(x);
296
+ const yz = @ctz(y);
297
+ const shift = @min(xz, yz);
298
+ x >>= @intCast(xz);
299
+ y >>= @intCast(yz);
300
+
301
+ var diff = y -% x;
302
+ while (diff != 0) : (diff = y -% x) {
303
+ const zeros = @ctz(diff);
304
+ if (x > y) diff = -%diff;
305
+ y = @min(x, y);
306
+ x = diff >> @intCast(zeros);
307
+ }
308
+
309
+ const result = y << @intCast(shift);
310
+ assert(result > 0);
311
+ return result;
312
+ },
313
+ else => {
314
+ var a = p;
315
+ var b = q;
316
+ var c: T = undefined;
317
+
318
+ while (b != 0) {
319
+ c = b;
320
+ b = @mod(a, b);
321
+ a = c;
322
+ }
323
+
324
+ assert(a > 0);
325
+ return a;
326
+ },
327
+ }
328
+ }
329
+
330
+ test gcd {
331
+ const seed = if (@hasDecl(std.testing, "random_seed")) std.testing.random_seed else 0x12345678;
332
+ var prng = (if (@hasDecl(std, "Random")) std.Random else std.rand).DefaultPrng.init(seed);
333
+ var random = prng.random();
334
+
335
+ for (0..1000) |_| {
336
+ const a = random.int(u32);
337
+ const b = random.int(u32);
338
+ if (a == 0 or b == 0) continue;
339
+
340
+ try expectEqual(
341
+ gcd(a, b),
342
+ @as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
343
+ );
344
+ }
345
+
346
+ try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
347
+ }
348
+
349
+ fn doTurn(r: anytype) !void {
350
+ try r.update(1, 163); // Metronome
351
+ try r.update(33, 256); // Critical Hit (67 Speed)
352
+ try r.update(1, 39); // Damage roll
353
+ try r.update(89, 128); // Thunder accuracy (178/256)
354
+ try r.update(77, 256); // Thunder secondary proc
355
+ }
356
+
357
+ test Rational {
358
+ inline for (.{ u64, u128, u256, f64 }) |t| {
359
+ var r: Rational(t) = .{};
360
+
361
+ var c: t = 128;
362
+ _ = &c;
363
+
364
+ try r.update(c, 256);
365
+ try doTurn(&r);
366
+
367
+ r.reduce();
368
+ try expectEqual(Rational(t){ .p = 75383, .q = 35550920704 }, r);
369
+
370
+ try r.update(1, 4);
371
+ if (t == u64) {
372
+ try expectError(error.Overflow, doTurn(&r));
373
+ } else {
374
+ try doTurn(&r);
375
+ r.reduce();
376
+ try expectEqual(Rational(t){ .p = 5682596689, .q = 2527735925804191711232 }, r);
377
+ }
378
+
379
+ r.reset();
380
+
381
+ var s = Rational(t){ .p = 10, .q = 13 };
382
+ try r.mul(&s);
383
+ s = Rational(t){ .p = 3, .q = 4 };
384
+ try r.mul(&s);
385
+ try expectEqual(Rational(t){ .p = 30, .q = 52 }, r);
386
+
387
+ s = Rational(t){ .p = 1, .q = 3 };
388
+ try r.add(&s);
389
+ r.reduce();
390
+ try expectEqual(Rational(t){ .p = 71, .q = 78 }, r);
391
+ }
392
+ }
393
+
394
+ /// Adapter of std.math.big.Rational to the engines Rational interface.
395
+ pub const BigRational = struct {
396
+ /// The underlying rational number.
397
+ val: std.math.big.Rational,
398
+
399
+ /// Possible error returned by operations on the rational.
400
+ pub const Error = error{OutOfMemory};
401
+
402
+ /// Create a new BigRational wrapper. A small amount of memory will be allocated on
403
+ /// initialization. Not default initialized to 1 - you must explicitly `reset` first.
404
+ pub fn init(alloc: std.mem.Allocator) !BigRational {
405
+ return .{ .val = try std.math.big.Rational.init(alloc) };
406
+ }
407
+
408
+ /// Frees all memory associated with a rational.
409
+ pub fn deinit(r: *BigRational) void {
410
+ r.val.deinit();
411
+ }
412
+
413
+ /// Resets the rational back to 1.
414
+ pub fn reset(r: *BigRational) !void {
415
+ try r.val.setInt(1);
416
+ }
417
+
418
+ /// Update the rational by multiplying its numerator by p and its denominator by q.
419
+ /// Both p and q must be >= 1.
420
+ pub fn update(r: *BigRational, p: anytype, q: anytype) !void {
421
+ assert(p >= 1);
422
+ assert(q >= 1);
423
+
424
+ var s = try std.math.big.Rational.init(r.val.p.allocator);
425
+ defer s.deinit();
426
+ try s.setRatio(p, q);
427
+ try r.val.mul(r.val, s);
428
+ }
429
+
430
+ /// Adds two rationals.
431
+ pub fn add(r: *BigRational, s: anytype) !void {
432
+ if (@hasField(@TypeOf(s.*), "val")) {
433
+ try r.val.add(r.val, s.val);
434
+ } else {
435
+ var t = try std.math.big.Rational.init(r.val.p.allocator);
436
+ defer t.deinit();
437
+ try t.setRatio(s.p, s.q);
438
+ try r.val.add(r.val, t);
439
+ }
440
+ }
441
+
442
+ /// Multiplies two rationals.
443
+ pub fn mul(r: *BigRational, s: *const BigRational) !void {
444
+ try r.val.mul(r.val, s.val);
445
+ }
446
+ };
447
+
448
+ test BigRational {
449
+ var r = try BigRational.init(std.testing.allocator);
450
+ defer r.deinit();
451
+ var s = try std.math.big.Rational.init(std.testing.allocator);
452
+ defer s.deinit();
453
+
454
+ try r.reset();
455
+
456
+ var c: u8 = 128;
457
+ _ = &c;
458
+
459
+ try r.update(c, 256);
460
+ try doTurn(&r);
461
+
462
+ try s.setRatio(75383, 35550920704);
463
+ try expect((try s.order(r.val)) == .eq);
464
+
465
+ try r.update(1, 4);
466
+ try doTurn(&r);
467
+
468
+ try s.setRatio(5682596689, 2527735925804191711232);
469
+ try expect((try s.order(r.val)) == .eq);
470
+
471
+ try r.reset();
472
+
473
+ var t = try BigRational.init(std.testing.allocator);
474
+ defer t.deinit();
475
+
476
+ try t.val.setRatio(10, 13);
477
+ try r.mul(&t);
478
+ try t.val.setRatio(3, 4);
479
+ try r.mul(&t);
480
+
481
+ try s.setRatio(15, 26);
482
+ try expect((try s.order(r.val)) == .eq);
483
+
484
+ try t.val.setRatio(1, 3);
485
+ try r.add(&t);
486
+
487
+ try s.setRatio(71, 78);
488
+ try expect((try s.order(r.val)) == .eq);
489
+ }
490
+
491
+ test "minimum" {
492
+ // TODO: ziglang/zig#21069
493
+ if (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall) {
494
+ return error.SkipZigTest;
495
+ }
496
+ var r = try BigRational.init(std.testing.allocator);
497
+ defer r.deinit();
498
+ var s = try std.math.big.Rational.init(std.testing.allocator);
499
+ defer s.deinit();
500
+
501
+ try r.reset();
502
+
503
+ try r.update(1, 2); // Speed Tie
504
+ for (0..2) |_| {
505
+ try r.update(1, 2); // Confusion
506
+ try r.update(1, 2); // Infatuation
507
+ try r.update(3, 4); // Paralysis
508
+ // Population Bomb
509
+ for (0..10) |_| {
510
+ try r.update(9, 10); // Accuracy
511
+ try r.update(1, 15); // Damage roll
512
+ try r.update(1, 24); // Critical Hit
513
+ try r.update(9, 10); // King's Rock non-proc
514
+ }
515
+ }
516
+
517
+ const ZEROS = std.math.pow(u310, 10, 31);
518
+ const n = 36472996377170786403;
519
+ const d: u310 = 146267071761884981524886149560653378220687632500762654519853056 * ZEROS;
520
+ try s.setRatio(n, d);
521
+ try expect((try s.order(r.val)) == .eq);
522
+ }
@@ -1,10 +1,8 @@
1
+ const options = @import("options.zig");
1
2
  const std = @import("std");
2
3
 
3
- const options = @import("./options.zig");
4
-
5
4
  const assert = std.debug.assert;
6
5
  const expectEqual = std.testing.expectEqual;
7
-
8
6
  const showdown = options.showdown;
9
7
 
10
8
  pub fn PRNG(comptime gen: comptime_int) type {
@@ -25,6 +23,11 @@ pub fn PRNG(comptime gen: comptime_int) type {
25
23
  pub fn next(self: *Self) Output(gen) {
26
24
  return self.src.next();
27
25
  }
26
+
27
+ pub fn advance(self: *Self, n: usize) void {
28
+ if (!options.advance) return;
29
+ for (0..n) |_| self.src.advance();
30
+ }
28
31
  };
29
32
  }
30
33
 
@@ -47,7 +50,7 @@ pub const PSRNG = extern struct {
47
50
  }
48
51
 
49
52
  pub fn range(self: *PSRNG, comptime T: type, from: T, to: Bound(T)) T {
50
- return @intCast(T, @as(u64, self.src.next()) * (to - from) / divisor + from);
53
+ return @intCast(@as(u64, self.src.next()) * (to - from) / divisor + from);
51
54
  }
52
55
 
53
56
  pub fn chance(self: *PSRNG, comptime T: type, numerator: T, denominator: Bound(T)) bool {
@@ -61,10 +64,14 @@ pub const PSRNG = extern struct {
61
64
  (@as(u64, self.range(u16, 0, 0x10000)) << 16) |
62
65
  (@as(u64, self.range(u16, 0, 0x10000)));
63
66
  }
67
+
68
+ pub fn Bound(comptime T: type) type {
69
+ return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
70
+ }
64
71
  };
65
72
 
66
73
  test PSRNG {
67
- var psrng = PSRNG{ .src = .{ .seed = 0x0001000200030004 } };
74
+ var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
68
75
  try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
69
76
  try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
70
77
  }
@@ -72,28 +79,32 @@ test PSRNG {
72
79
  // https://pkmn.cc/pokered/engine/battle/core.asm#L6644-L6693
73
80
  // https://pkmn.cc/pokecrystal/engine/battle/core.asm#L6922-L6938
74
81
  pub const Gen12 = extern struct {
75
- seed: [10]u8,
82
+ seed: [9]u8,
76
83
  index: u8 = 0,
77
84
 
78
85
  comptime {
79
- assert(@sizeOf(Gen12) == 11);
86
+ assert(@sizeOf(Gen12) == 10);
80
87
  }
81
88
 
82
- pub fn percent(p: u8) u8 {
83
- return @intCast(u8, (@as(u16, p) * 0xFF) / 100);
89
+ pub fn percent(comptime p: comptime_int) u8 {
90
+ return @intCast((@as(u16, p) * 0xFF) / 100);
84
91
  }
85
92
 
86
93
  pub fn next(self: *Gen12) u8 {
87
94
  const val = 5 *% self.seed[self.index] +% 1;
88
95
  self.seed[self.index] = val;
89
- self.index = (self.index + 1) % 10;
96
+ self.index = (self.index + 1) % 9;
90
97
  return val;
91
98
  }
99
+
100
+ pub fn advance(self: *Gen12) void {
101
+ _ = self.next();
102
+ }
92
103
  };
93
104
 
94
105
  test Gen12 {
95
- const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 31, 56, 81 };
96
- var rng = Gen12{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } };
106
+ const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 31, 56, 81 };
107
+ var rng: Gen12 = .{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
97
108
  for (expected) |e| {
98
109
  try expectEqual(e, rng.next());
99
110
  }
@@ -114,7 +125,7 @@ pub const Gen34 = extern struct {
114
125
 
115
126
  pub fn next(self: *Gen34) u16 {
116
127
  self.advance();
117
- return @intCast(u16, self.seed >> 16);
128
+ return @intCast(self.seed >> 16);
118
129
  }
119
130
 
120
131
  pub fn advance(self: *Gen34) void {
@@ -129,7 +140,7 @@ test Gen34 {
129
140
  .{ 0x80000000, 5, 0x0E425287 }, .{ 0x80000000, 10, 0x6F2CF4B2 },
130
141
  };
131
142
  for (data) |d| {
132
- var rng = Gen34{ .seed = d[0] };
143
+ var rng: Gen34 = .{ .seed = d[0] };
133
144
  for (0..d[1]) |_| _ = rng.next();
134
145
  try expectEqual(d[2], rng.seed);
135
146
  }
@@ -144,7 +155,7 @@ pub const Gen56 = extern struct {
144
155
 
145
156
  pub fn next(self: *Gen56) u32 {
146
157
  self.advance();
147
- return @intCast(u32, self.seed >> 32);
158
+ return @intCast(self.seed >> 32);
148
159
  }
149
160
 
150
161
  pub fn advance(self: *Gen56) void {
@@ -161,7 +172,7 @@ test Gen56 {
161
172
  .{ 0x8000000000000000, 10, 0xE7795501267F125A },
162
173
  };
163
174
  for (data) |d| {
164
- var rng = Gen56{ .seed = d[0] };
175
+ var rng: Gen56 = .{ .seed = d[0] };
165
176
  for (0..d[1]) |_| _ = rng.next();
166
177
  try expectEqual(d[2], rng.seed);
167
178
  }
@@ -188,16 +199,16 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
188
199
  for (0..n) |_| _ = self.next();
189
200
  }
190
201
 
191
- pub fn range(self: *Self, comptime T: type, from: T, to: Bound(T)) T {
202
+ pub fn range(self: *Self, comptime T: type, from: T, to: PSRNG.Bound(T)) T {
192
203
  assert(showdown);
193
- return @intCast(T, @as(u64, self.next()) * (to - from) / divisor + from);
204
+ return @intCast(@as(u64, self.next()) * (to - from) / divisor + from);
194
205
  }
195
206
 
196
207
  pub fn chance(
197
208
  self: *Self,
198
209
  comptime T: type,
199
210
  numerator: T,
200
- denominator: Bound(T),
211
+ denominator: PSRNG.Bound(T),
201
212
  ) bool {
202
213
  assert(showdown);
203
214
  assert(denominator > 0);
@@ -213,7 +224,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
213
224
  test FixedRNG {
214
225
  const Type = if (showdown) u32 else u8;
215
226
  const expected = [_]Type{ 42, 255, 0 };
216
- var rng = FixedRNG(1, expected.len){ .rolls = expected };
227
+ var rng: FixedRNG(1, expected.len) = .{ .rolls = expected };
217
228
  for (expected) |e| {
218
229
  try expectEqual(e, rng.next());
219
230
  }
@@ -228,7 +239,3 @@ fn Output(comptime gen: comptime_int) type {
228
239
  else => unreachable,
229
240
  };
230
241
  }
231
-
232
- fn Bound(comptime T: type) type {
233
- return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
234
- }