@pkmn/engine 0.1.0-dev.fb5bf955 → 0.1.0-dev.fb6653d6

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