@pkmn/engine 0.1.0-dev.d716e940 → 0.1.0-dev.d73cd76c

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 (87) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +114 -69
  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 +26 -4
  13. package/build/pkg/addon.js +39 -70
  14. package/build/pkg/addon.js.map +1 -1
  15. package/build/pkg/data/ids.json +12 -12
  16. package/build/pkg/data/layout.json +39 -4
  17. package/build/pkg/data/protocol.json +84 -13
  18. package/build/pkg/data.d.ts +27 -5
  19. package/build/pkg/data.js.map +1 -1
  20. package/build/pkg/gen1.d.ts +15 -18
  21. package/build/pkg/gen1.js +43 -37
  22. package/build/pkg/gen1.js.map +1 -1
  23. package/build/pkg/index.d.ts +42 -91
  24. package/build/pkg/index.js +23 -70
  25. package/build/pkg/index.js.map +1 -1
  26. package/build/pkg/protocol.d.ts +29 -1
  27. package/build/pkg/protocol.js +123 -102
  28. package/build/pkg/protocol.js.map +1 -1
  29. package/build/tools/debug.js +84 -73
  30. package/build/tools/debug.js.map +1 -1
  31. package/build/tools/display/debug.jsx +66 -0
  32. package/build/tools/display/debug.jsx.map +1 -0
  33. package/build/tools/display/index.js +152 -0
  34. package/build/tools/display/index.js.map +1 -0
  35. package/build/tools/display/ui.jsx +360 -0
  36. package/build/tools/display/ui.jsx.map +1 -0
  37. package/build/tools/display/util.js +157 -0
  38. package/build/tools/display/util.js.map +1 -0
  39. package/build.zig +428 -176
  40. package/package.json +72 -122
  41. package/src/bin/install-pkmn-engine +56 -25
  42. package/src/bin/pkmn-debug +1 -1
  43. package/src/lib/bindings/c.zig +201 -0
  44. package/src/lib/bindings/node.zig +117 -0
  45. package/src/lib/bindings/wasm.zig +51 -0
  46. package/src/lib/common/array.zig +70 -0
  47. package/src/lib/common/battle.zig +17 -0
  48. package/src/lib/common/data.zig +19 -31
  49. package/src/lib/common/debug.zig +58 -33
  50. package/src/lib/common/js.zig +114 -0
  51. package/src/lib/common/optional.zig +71 -0
  52. package/src/lib/common/options.zig +15 -0
  53. package/src/lib/common/protocol.zig +1037 -462
  54. package/src/lib/common/rational.zig +440 -0
  55. package/src/lib/common/rng.zig +31 -24
  56. package/src/lib/common/util.zig +64 -0
  57. package/src/lib/gen1/calc.zig +975 -0
  58. package/src/lib/gen1/chance.zig +1246 -0
  59. package/src/lib/gen1/data/moves.zig +203 -193
  60. package/src/lib/gen1/data/species.zig +6 -8
  61. package/src/lib/gen1/data/types.zig +12 -13
  62. package/src/lib/gen1/data.zig +98 -89
  63. package/src/lib/gen1/helpers.zig +159 -51
  64. package/src/lib/gen1/mechanics.zig +1211 -846
  65. package/src/lib/gen2/calc.zig +150 -0
  66. package/src/lib/gen2/chance.zig +914 -0
  67. package/src/lib/gen2/data/items.zig +15 -23
  68. package/src/lib/gen2/data/moves.zig +665 -573
  69. package/src/lib/gen2/data/species.zig +3 -5
  70. package/src/lib/gen2/data/types.zig +141 -36
  71. package/src/lib/gen2/data.zig +136 -100
  72. package/src/lib/gen2/generated.zig +801 -0
  73. package/src/lib/gen2/helpers.zig +81 -58
  74. package/src/lib/gen2/mechanics.zig +3582 -30
  75. package/src/lib/napi.h +2 -0
  76. package/src/lib/node.zig +14 -0
  77. package/src/lib/pkmn.zig +172 -17
  78. package/src/lib/wasm.zig +13 -0
  79. package/src/tools/display/dom.ts +108 -0
  80. package/src/tools/display/ui.css +354 -0
  81. package/build/test/display.js +0 -400
  82. package/build/test/display.js.map +0 -1
  83. package/src/lib/binding/node.zig +0 -229
  84. package/src/lib/binding/wasm.zig +0 -10
  85. package/src/lib/gen1/README.md +0 -812
  86. package/src/lib/gen2/README.md +0 -309
  87. package/src/test/display/pkmn.html.tmpl +0 -368
@@ -0,0 +1,440 @@
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
+ /// Specialization of a rational number used by the engine to compute probabilties.
10
+ /// For performance reasons the rational is only reduced lazily and thus `reduce` must be
11
+ /// invoked explicitly before reading. Note that this laziness means that this implementation
12
+ /// sometimes overflows in places where an eager reduced implementation would not - to achieve
13
+ /// the same behavior as an eager implementation simply call `reduce` after each operation.
14
+ pub fn Rational(comptime T: type) type {
15
+ // With floats we can't rely on an overflow bit to let us know when to reduce, so we
16
+ // instead start reducing when we get sufficiently close to the limit of the mantissa
17
+ // (in our domain we expect updates to involve numbers < 2**10, so we should be safe
18
+ // not reducing before we are 2**10 away from "overflowing" the mantissa)
19
+ const o = if (@typeInfo(T) == .float)
20
+ std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
21
+ else
22
+ 0;
23
+
24
+ const Err = switch (@typeInfo(T)) {
25
+ .int => error{Overflow},
26
+ .float => error{},
27
+ else => unreachable,
28
+ };
29
+
30
+ // Zig doesn't allow integers > u128 in an extern struct...
31
+ return if (@sizeOf(T) <= 16) extern struct {
32
+ const Self = @This();
33
+
34
+ /// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
35
+ p: T = 1,
36
+ /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
37
+ q: T = 1,
38
+
39
+ /// Possible error returned by operations on the Rational.
40
+ pub const Error = Err;
41
+
42
+ /// Resets the rational back to 1.
43
+ pub fn reset(r: *Self) void {
44
+ r.p = 1;
45
+ r.q = 1;
46
+ }
47
+
48
+ /// Update the rational by multiplying its numerator by p and its denominator by q.
49
+ /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
50
+ pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
51
+ return update_(T, o, r, p, q);
52
+ }
53
+
54
+ /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
55
+ pub fn add(r: *Self, s: anytype) Error!void {
56
+ return add_(T, o, r, s);
57
+ }
58
+
59
+ /// Multiplies two rationals.
60
+ pub fn mul(r: *Self, s: anytype) Error!void {
61
+ return mul_(T, o, r, s);
62
+ }
63
+
64
+ /// Compares two rationals using the identity (a/b) > (c/d) => ad > bc
65
+ pub fn cmp(r: *Self, s: anytype) Error!std.math.Order {
66
+ return cmp_(T, o, r, s);
67
+ }
68
+
69
+ /// Normalize the rational by reducing by the greatest common divisor.
70
+ pub fn reduce(r: *Self) void {
71
+ reduce_(r);
72
+ }
73
+
74
+ pub fn format(self: Self, writer: *std.Io.Writer) !void {
75
+ try writer.print("{d}/{d}", .{ self.p, self.q });
76
+ }
77
+ } else struct {
78
+ const Self = @This();
79
+
80
+ /// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
81
+ p: T = 1,
82
+ /// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
83
+ q: T = 1,
84
+
85
+ /// Possible error returned by operations on the Rational.
86
+ pub const Error = Err;
87
+
88
+ /// Resets the rational back to 1.
89
+ pub fn reset(r: *Self) void {
90
+ r.p = 1;
91
+ r.q = 1;
92
+ }
93
+
94
+ /// Update the rational by multiplying its numerator by p and its denominator by q.
95
+ /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
96
+ pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
97
+ return update_(T, o, r, p, q);
98
+ }
99
+
100
+ /// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
101
+ pub fn add(r: *Self, s: anytype) Error!void {
102
+ return add_(T, o, r, s);
103
+ }
104
+
105
+ /// Multiplies two rationals.
106
+ pub fn mul(r: *Self, s: anytype) Error!void {
107
+ return mul_(T, o, r, s);
108
+ }
109
+
110
+ /// Compares two rationals using the identity (a/b) > (c/d) => ad > bc
111
+ pub fn cmp(r: *Self, s: anytype) Error!std.math.Order {
112
+ return cmp_(T, o, r, s);
113
+ }
114
+
115
+ /// Normalize the rational by reducing by the greatest common divisor.
116
+ pub fn reduce(r: *Self) void {
117
+ reduce_(r);
118
+ }
119
+
120
+ pub fn format(self: Self, writer: *std.Io.Writer) !void {
121
+ try writer.print("{d}/{d}", .{ self.p, self.q });
122
+ }
123
+ };
124
+ }
125
+
126
+ fn update_(comptime T: type, comptime o: comptime_int, r: anytype, p: anytype, q: anytype) !void {
127
+ // std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
128
+ assert(p >= 1);
129
+ assert(q >= 1);
130
+
131
+ // If our parameters are not fully reduced they may prematurely
132
+ // cause overflow/loss of precision after the multiplication below
133
+ assert(switch (@typeInfo(@TypeOf(p, q))) {
134
+ .comptime_int, .comptime_float => comptime gcd(p, q),
135
+ else => 1,
136
+ } == 1);
137
+
138
+ switch (@typeInfo(T)) {
139
+ .int => {
140
+ // Greedily attempt to multiply and if it fails, reduce and try again
141
+ multiplication(T, r, p, q) catch |err| switch (err) {
142
+ error.Overflow => {
143
+ r.reduce();
144
+ try multiplication(T, r, p, q);
145
+ },
146
+ else => unreachable,
147
+ };
148
+ },
149
+ .float => {
150
+ // Reduce in situations where we're likely to start losing precision
151
+ if (r.q > o or r.p > o) r.reduce();
152
+
153
+ r.p *= switch (@typeInfo(@TypeOf(p))) {
154
+ .float, .comptime_float => p,
155
+ else => @floatFromInt(p),
156
+ };
157
+ r.q *= switch (@typeInfo(@TypeOf(q))) {
158
+ .float, .comptime_float => q,
159
+ else => @floatFromInt(q),
160
+ };
161
+
162
+ // We should always be dealing with whole numbers
163
+ assert(std.math.modf(r.p).fpart == 0);
164
+ assert(std.math.modf(r.q).fpart == 0);
165
+ },
166
+ else => unreachable,
167
+ }
168
+ }
169
+
170
+ fn add_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
171
+ switch (@typeInfo(T)) {
172
+ .int => {
173
+ if (r.q == s.q) {
174
+ r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
175
+ error.Overflow => val: {
176
+ r.reduce();
177
+ s.reduce();
178
+ break :val try std.math.add(T, r.p, s.p);
179
+ },
180
+ else => unreachable,
181
+ };
182
+ } else {
183
+ addition(T, r, s.p, s.q) catch |err| switch (err) {
184
+ error.Overflow => {
185
+ r.reduce();
186
+ s.reduce();
187
+ try addition(T, r, s.p, s.q);
188
+ },
189
+ else => unreachable,
190
+ };
191
+ }
192
+ },
193
+ .float => {
194
+ if (r.q == s.q) {
195
+ if (r.p > o) r.reduce();
196
+ if (s.p > o) s.reduce();
197
+
198
+ r.p += s.p;
199
+ } else {
200
+ // Always reduce to minimize loss of precision from the multiplications
201
+ r.reduce();
202
+ s.reduce();
203
+
204
+ r.p = (r.p * s.q) + (r.q * s.p);
205
+ r.q *= s.q;
206
+ }
207
+
208
+ assert(std.math.modf(r.p).fpart == 0);
209
+ assert(std.math.modf(r.q).fpart == 0);
210
+ },
211
+ else => unreachable,
212
+ }
213
+ }
214
+
215
+ fn mul_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
216
+ switch (@typeInfo(T)) {
217
+ .int => {
218
+ multiplication(T, r, s.p, s.q) catch |err| switch (err) {
219
+ error.Overflow => {
220
+ r.reduce();
221
+ s.reduce();
222
+ try multiplication(T, r, s.p, s.q);
223
+ },
224
+ else => unreachable,
225
+ };
226
+ },
227
+ .float => {
228
+ if (r.q > o or r.p > o) r.reduce();
229
+ if (s.q > o or s.p > o) s.reduce();
230
+
231
+ r.p *= s.p;
232
+ r.q *= s.q;
233
+
234
+ assert(std.math.modf(r.p).fpart == 0);
235
+ assert(std.math.modf(r.q).fpart == 0);
236
+ },
237
+ else => unreachable,
238
+ }
239
+ }
240
+
241
+ fn cmp_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !std.math.Order {
242
+ switch (@typeInfo(T)) {
243
+ .int => {
244
+ const ad = std.math.mul(T, r.p, s.q) catch |err| switch (err) {
245
+ error.Overflow => ad: {
246
+ r.reduce();
247
+ s.reduce();
248
+ break :ad try std.math.mul(T, r.p, s.q);
249
+ },
250
+ else => unreachable,
251
+ };
252
+ const bc = std.math.mul(T, r.q, s.p) catch |err| switch (err) {
253
+ error.Overflow => bc: {
254
+ r.reduce();
255
+ s.reduce();
256
+ break :bc try std.math.mul(T, r.q, s.p);
257
+ },
258
+ else => unreachable,
259
+ };
260
+
261
+ return std.math.order(ad, bc);
262
+ },
263
+ .float => {
264
+ if (r.q > o or r.p > o) r.reduce();
265
+ if (s.q > o or s.p > o) s.reduce();
266
+
267
+ const ad: T = r.p * s.q;
268
+ const bc: T = r.q * s.p;
269
+
270
+ return std.math.order(ad, bc);
271
+ },
272
+ else => unreachable,
273
+ }
274
+ }
275
+
276
+ fn reduce_(r: anytype) void {
277
+ const d = gcd(r.p, r.q);
278
+ if (d == 1) return;
279
+
280
+ assert(@mod(r.p, d) == 0);
281
+ assert(@mod(r.q, d) == 0);
282
+
283
+ r.p /= d;
284
+ r.q /= d;
285
+
286
+ assert(r.p >= 1);
287
+ assert(r.q >= 1);
288
+ }
289
+
290
+ fn multiplication(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
291
+ r.p = try std.math.mul(T, r.p, p);
292
+ r.q = try std.math.mul(T, r.q, q);
293
+ }
294
+
295
+ fn addition(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
296
+ // (a/b) + (c/d) = (ad+bc)/(bd)
297
+ const bd = try std.math.mul(T, r.q, q);
298
+ const ad = try std.math.mul(T, r.p, q);
299
+ const bc = try std.math.mul(T, r.q, p);
300
+
301
+ r.p = try std.math.add(T, ad, bc);
302
+ r.q = bd;
303
+ }
304
+
305
+ fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
306
+ assert(p >= 1);
307
+ assert(q >= 1);
308
+
309
+ // convert comptime_int to a sized integer within this function so that @ctz will work
310
+ const T = switch (@TypeOf(p, q)) {
311
+ comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
312
+ else => |U| U,
313
+ };
314
+
315
+ switch (@typeInfo(T)) {
316
+ .int => {
317
+ // std.math.gcd but without some checks because we have a stricter range
318
+ var x: T = @intCast(p);
319
+ var y: T = @intCast(q);
320
+
321
+ const xz = @ctz(x);
322
+ const yz = @ctz(y);
323
+ const shift = @min(xz, yz);
324
+ x >>= @intCast(xz);
325
+ y >>= @intCast(yz);
326
+
327
+ var diff = y -% x;
328
+ while (diff != 0) : (diff = y -% x) {
329
+ const zeros = @ctz(diff);
330
+ if (x > y) diff = -%diff;
331
+ y = @min(x, y);
332
+ x = diff >> @intCast(zeros);
333
+ }
334
+
335
+ const result = y << @intCast(shift);
336
+ assert(result > 0);
337
+ return result;
338
+ },
339
+ else => {
340
+ var a = p;
341
+ var b = q;
342
+ var c: T = undefined;
343
+
344
+ while (b != 0) {
345
+ c = b;
346
+ b = @mod(a, b);
347
+ a = c;
348
+ }
349
+
350
+ assert(a > 0);
351
+ return a;
352
+ },
353
+ }
354
+ }
355
+
356
+ test gcd {
357
+ var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
358
+ var random = prng.random();
359
+
360
+ for (0..1000) |_| {
361
+ const a = random.int(u32);
362
+ const b = random.int(u32);
363
+ if (a == 0 or b == 0) continue;
364
+
365
+ try expectEqual(
366
+ gcd(a, b),
367
+ @as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
368
+ );
369
+ }
370
+
371
+ try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
372
+ }
373
+
374
+ fn doTurn(r: anytype) !void {
375
+ try r.update(1, 163); // Metronome
376
+ try r.update(33, 256); // Critical Hit (67 Speed)
377
+ try r.update(1, 39); // Damage roll
378
+ try r.update(89, 128); // Thunder accuracy (178/256)
379
+ try r.update(77, 256); // Thunder secondary proc
380
+ }
381
+
382
+ test Rational {
383
+ inline for (.{ u64, u128, u256, f64 }) |t| {
384
+ const R = Rational(t);
385
+ var r: R = .{};
386
+
387
+ var c: t = 128;
388
+ _ = &c;
389
+
390
+ try r.update(c, 256);
391
+ try doTurn(&r);
392
+
393
+ r.reduce();
394
+ try expectEqual(R{ .p = 75383, .q = 35550920704 }, r);
395
+
396
+ try r.update(1, 4);
397
+ if (t == u64) {
398
+ try expectError(error.Overflow, doTurn(&r));
399
+ } else {
400
+ try doTurn(&r);
401
+ r.reduce();
402
+ try expectEqual(R{ .p = 5682596689, .q = 2527735925804191711232 }, r);
403
+ }
404
+
405
+ r.reset();
406
+
407
+ var s = R{ .p = 10, .q = 13 };
408
+ try r.mul(&s);
409
+ s = R{ .p = 3, .q = 4 };
410
+ try r.mul(&s);
411
+ try expectEqual(R{ .p = 30, .q = 52 }, r);
412
+
413
+ s = R{ .p = 1, .q = 3 };
414
+ try r.add(&s);
415
+ r.reduce();
416
+ try expectEqual(R{ .p = 71, .q = 78 }, r);
417
+
418
+ var u = R{ .p = 3, .q = 5 };
419
+ var v = R{ .p = 2, .q = 5 };
420
+ try expectEqual(u.cmp(&v), .gt);
421
+ v = R{ .p = 4, .q = 5 };
422
+ try expectEqual(u.cmp(&v), .lt);
423
+
424
+ v = R{ .p = 3, .q = 6 };
425
+ try expectEqual(u.cmp(&v), .gt);
426
+ v = R{ .p = 3, .q = 4 };
427
+ try expectEqual(u.cmp(&v), .lt);
428
+
429
+ v = R{ .p = 3, .q = 5 };
430
+ try expectEqual(u.cmp(&v), .eq);
431
+
432
+ try doTurn(&u);
433
+ v = R{ .p = 4567216874, .q = 89124781931235 };
434
+ if (t == u64) {
435
+ try expectEqual(u.cmp(&v), error.Overflow);
436
+ } else {
437
+ try expectEqual(u.cmp(&v), .lt);
438
+ }
439
+ }
440
+ }
@@ -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
- }
@@ -0,0 +1,64 @@
1
+ const std = @import("std");
2
+
3
+ const expect = std.testing.expect;
4
+ const expectEqual = std.testing.expectEqual;
5
+
6
+ pub fn PointerType(comptime P: type, comptime C: type) type {
7
+ const ptr_info = @typeInfo(P).pointer;
8
+ const is_const = if (@hasField(@TypeOf(ptr_info), "attrs"))
9
+ ptr_info.attrs.@"const"
10
+ else
11
+ ptr_info.is_const;
12
+ return if (is_const) *const C else *C;
13
+ }
14
+
15
+ test PointerType {
16
+ try expectEqual(*bool, PointerType(*u8, bool));
17
+ try expectEqual(*const f64, PointerType(*const i32, f64));
18
+ }
19
+
20
+ pub fn EnumFieldsLen(comptime E: type) usize {
21
+ const info = @typeInfo(E).@"enum";
22
+ return if (@hasField(@TypeOf(info), "fields"))
23
+ info.fields.len
24
+ else
25
+ info.field_names.len;
26
+ }
27
+
28
+ test EnumFieldsLen {
29
+ const E1 = enum { A, B, C };
30
+ const E2 = enum(u8) { A = 0, B = 10, C = 20, D = 30 };
31
+ try expectEqual(3, EnumFieldsLen(E1));
32
+ try expectEqual(4, EnumFieldsLen(E2));
33
+ }
34
+
35
+ pub fn isPointerTo(p: anytype, comptime P: type) bool {
36
+ const info = @typeInfo(@TypeOf(p));
37
+ return switch (info) {
38
+ .pointer => info.pointer.child == P,
39
+ else => false,
40
+ };
41
+ }
42
+
43
+ test isPointerTo {
44
+ const S = struct {};
45
+ const s: S = .{};
46
+ try expect(!isPointerTo(s, S));
47
+ try expect(isPointerTo(&s, S));
48
+ }
49
+
50
+ pub fn repeat(comptime xs: anytype, comptime n: usize) [xs.len * n]@TypeOf(xs[0]) {
51
+ comptime {
52
+ const T = @TypeOf(xs[0]);
53
+ var ts: [xs.len * n]T = undefined;
54
+
55
+ var i: usize = 0;
56
+ while (i < n) : (i += 1) {
57
+ for (xs, 0..) |v, j| {
58
+ ts[i * xs.len + j] = v;
59
+ }
60
+ }
61
+
62
+ return ts;
63
+ }
64
+ }