@pkmn/engine 0.1.0-dev.305a932f → 0.1.0-dev.30895c5a

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 (51) hide show
  1. package/LICENSE +1 -3
  2. package/README.md +44 -19
  3. package/build/pkg/addon.js +6 -7
  4. package/build/pkg/addon.js.map +1 -1
  5. package/build/pkg/data/layout.json +2 -2
  6. package/build/pkg/data.d.ts +4 -4
  7. package/build/pkg/data.js.map +1 -1
  8. package/build/pkg/gen1.js +2 -2
  9. package/build/pkg/gen1.js.map +1 -1
  10. package/build/pkg/index.d.ts +3 -2
  11. package/build/pkg/index.js.map +1 -1
  12. package/build/pkg/protocol.js +5 -5
  13. package/build/pkg/protocol.js.map +1 -1
  14. package/build/test/display.js +8 -3
  15. package/build/test/display.js.map +1 -1
  16. package/build/tools/debug.js +5 -4
  17. package/build/tools/debug.js.map +1 -1
  18. package/build.zig +100 -102
  19. package/package.json +34 -23
  20. package/src/bin/install-pkmn-engine +26 -18
  21. package/src/lib/common/data.zig +8 -8
  22. package/src/lib/common/debug.zig +2 -2
  23. package/src/lib/common/optional.zig +9 -2
  24. package/src/lib/common/options.zig +2 -0
  25. package/src/lib/common/protocol.zig +408 -384
  26. package/src/lib/common/rational.zig +119 -18
  27. package/src/lib/common/rng.zig +20 -11
  28. package/src/lib/gen1/calc.zig +506 -130
  29. package/src/lib/gen1/chance.zig +395 -299
  30. package/src/lib/gen1/data/moves.zig +24 -10
  31. package/src/lib/gen1/data/species.zig +3 -3
  32. package/src/lib/gen1/data/types.zig +5 -5
  33. package/src/lib/gen1/data.zig +39 -39
  34. package/src/lib/gen1/helpers.zig +89 -548
  35. package/src/lib/gen1/mechanics.zig +551 -492
  36. package/src/lib/gen2/calc.zig +135 -0
  37. package/src/lib/gen2/chance.zig +882 -0
  38. package/src/lib/gen2/data/items.zig +3 -3
  39. package/src/lib/gen2/data/moves.zig +32 -15
  40. package/src/lib/gen2/data/species.zig +1 -1
  41. package/src/lib/gen2/data/types.zig +75 -28
  42. package/src/lib/gen2/data.zig +37 -25
  43. package/src/lib/gen2/generated.zig +806 -0
  44. package/src/lib/gen2/helpers.zig +37 -7
  45. package/src/lib/gen2/mechanics.zig +2679 -358
  46. package/src/lib/{binding/node.zig → node.zig} +9 -12
  47. package/src/lib/pkmn.zig +27 -15
  48. package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
  49. package/src/test/display/pkmn.html.tmpl +5 -5
  50. package/src/lib/gen1/README.md +0 -894
  51. package/src/lib/gen2/README.md +0 -330
@@ -43,8 +43,7 @@ pub fn Rational(comptime T: type) type {
43
43
  /// Update the rational by multiplying its numerator by p and its denominator by q.
44
44
  /// Both p and q must be >= 1, and if computable at comptime must have no common factors.
45
45
  pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
46
- // FIXME
47
- // std.debug.print("({d}/{d}) * ", .{p, q});
46
+ // std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
48
47
  assert(p >= 1);
49
48
  assert(q >= 1);
50
49
 
@@ -185,16 +184,16 @@ pub fn Rational(comptime T: type) type {
185
184
  try writer.print("{d}/{d}", .{ self.p, self.q });
186
185
  }
187
186
 
188
- inline fn multiplication(r: *Self, p: anytype, q: anytype) !void {
187
+ fn multiplication(r: *Self, p: anytype, q: anytype) !void {
189
188
  r.p = try std.math.mul(T, r.p, p);
190
189
  r.q = try std.math.mul(T, r.q, q);
191
190
  }
192
191
 
193
- inline fn addition(r: *Self, p: anytype, q: anytype) !void {
192
+ fn addition(r: *Self, p: anytype, q: anytype) !void {
194
193
  // (a/b) + (c/d) = (ad+bc)/(bd)
195
- var d = try std.math.mul(T, r.q, q);
196
- var n1 = try std.math.mul(T, r.p, q);
197
- var n2 = try std.math.mul(T, r.q, p);
194
+ const d = try std.math.mul(T, r.q, q);
195
+ const n1 = try std.math.mul(T, r.p, q);
196
+ const n2 = try std.math.mul(T, r.q, p);
198
197
 
199
198
  r.p = try std.math.add(T, n1, n2);
200
199
  r.q = d;
@@ -202,23 +201,88 @@ pub fn Rational(comptime T: type) type {
202
201
  };
203
202
  }
204
203
 
205
- // https://en.wikipedia.org/wiki/Euclidean_algorithm
206
- inline fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
204
+ // https://lemire.me/blog/greatest-common-divisor-the-extended-euclidean-algorithm-and-speed/
205
+ fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
207
206
  assert(p >= 1);
208
207
  assert(q >= 1);
209
208
 
210
- var a = p;
211
- var b = q;
212
- var c: @TypeOf(p, q) = undefined;
209
+ // convert comptime_int to a sized integer within this function so that @ctz will work
210
+ const T = switch (@TypeOf(p, q)) {
211
+ comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
212
+ else => |U| U,
213
+ };
214
+
215
+ switch (@typeInfo(T)) {
216
+ .Int => {
217
+ var u: T = undefined;
218
+ var v: T = undefined;
219
+ if (p < q) {
220
+ u = q;
221
+ v = p;
222
+ } else {
223
+ u = p;
224
+ v = q;
225
+ }
226
+ assert(v <= u);
227
+ u %= v;
228
+ if (u == 0) return v;
229
+
230
+ const zu = @ctz(u);
231
+ const zv = @ctz(v);
232
+ const shift = @min(zu, zv);
233
+ u >>= @intCast(zu);
234
+ v >>= @intCast(zv);
235
+
236
+ while (true) {
237
+ const diff = u -% v;
238
+ if (u > v) {
239
+ u = v;
240
+ v = diff;
241
+ } else {
242
+ v -= u;
243
+ }
244
+ if (diff != 0) v >>= @intCast(@ctz(diff));
245
+ if (v == 0) break;
246
+ }
247
+
248
+ const result = u << @intCast(shift);
249
+ assert(result > 0);
250
+ return result;
251
+ },
252
+ else => {
253
+ var a = p;
254
+ var b = q;
255
+ var c: T = undefined;
256
+
257
+ while (b != 0) {
258
+ c = b;
259
+ b = @mod(a, b);
260
+ a = c;
261
+ }
262
+
263
+ assert(a > 0);
264
+ return a;
265
+ },
266
+ }
267
+ }
268
+
269
+ test gcd {
270
+ const seed = if (@hasDecl(std.testing, "random_seed")) std.testing.random_seed else 0x12345678;
271
+ var prng = std.Random.DefaultPrng.init(seed);
272
+ var random = prng.random();
273
+
274
+ for (0..1000) |_| {
275
+ const a = random.int(u32);
276
+ const b = random.int(u32);
277
+ if (a == 0 or b == 0) continue;
213
278
 
214
- while (b != 0) {
215
- c = b;
216
- b = @mod(a, b);
217
- a = c;
279
+ try expectEqual(
280
+ gcd(a, b),
281
+ @as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
282
+ );
218
283
  }
219
284
 
220
- assert(a > 0);
221
- return a;
285
+ try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
222
286
  }
223
287
 
224
288
  fn doTurn(r: anytype) !void {
@@ -234,6 +298,8 @@ test Rational {
234
298
  var r: Rational(t) = .{};
235
299
 
236
300
  var c: t = 128;
301
+ _ = &c;
302
+
237
303
  try r.update(c, 256);
238
304
  try doTurn(&r);
239
305
 
@@ -320,6 +386,8 @@ test BigRational {
320
386
  try r.reset();
321
387
 
322
388
  var c: u8 = 128;
389
+ _ = &c;
390
+
323
391
  try r.update(c, 256);
324
392
  try doTurn(&r);
325
393
 
@@ -351,3 +419,36 @@ test BigRational {
351
419
  try s.setRatio(71, 78);
352
420
  try expect((try s.order(r.val)) == .eq);
353
421
  }
422
+
423
+ test "minimum" {
424
+ // TODO: ziglang/zig#21069
425
+ if (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall) {
426
+ return error.SkipZigTest;
427
+ }
428
+ var r = try BigRational.init(std.testing.allocator);
429
+ defer r.deinit();
430
+ var s = try std.math.big.Rational.init(std.testing.allocator);
431
+ defer s.deinit();
432
+
433
+ try r.reset();
434
+
435
+ try r.update(1, 2); // Speed Tie
436
+ for (0..2) |_| {
437
+ try r.update(1, 2); // Confusion
438
+ try r.update(1, 2); // Infatuation
439
+ try r.update(3, 4); // Paralysis
440
+ // Population Bomb
441
+ for (0..10) |_| {
442
+ try r.update(9, 10); // Accuracy
443
+ try r.update(1, 15); // Damage roll
444
+ try r.update(1, 24); // Critical Hit
445
+ try r.update(9, 10); // King's Rock non-proc
446
+ }
447
+ }
448
+
449
+ const ZEROS = std.math.pow(u310, 10, 31);
450
+ const n = 36472996377170786403;
451
+ const d: u310 = 146267071761884981524886149560653378220687632500762654519853056 * ZEROS;
452
+ try s.setRatio(n, d);
453
+ try expect((try s.order(r.val)) == .eq);
454
+ }
@@ -1,6 +1,6 @@
1
1
  const std = @import("std");
2
2
 
3
- const options = @import("./options.zig");
3
+ const options = @import("options.zig");
4
4
 
5
5
  const assert = std.debug.assert;
6
6
  const expectEqual = std.testing.expectEqual;
@@ -25,6 +25,11 @@ pub fn PRNG(comptime gen: comptime_int) type {
25
25
  pub fn next(self: *Self) Output(gen) {
26
26
  return self.src.next();
27
27
  }
28
+
29
+ pub fn advance(self: *Self, n: usize) void {
30
+ if (!options.advance) return;
31
+ for (0..n) |_| self.src.advance();
32
+ }
28
33
  };
29
34
  }
30
35
 
@@ -61,11 +66,15 @@ pub const PSRNG = extern struct {
61
66
  (@as(u64, self.range(u16, 0, 0x10000)) << 16) |
62
67
  (@as(u64, self.range(u16, 0, 0x10000)));
63
68
  }
69
+
70
+ pub fn Bound(comptime T: type) type {
71
+ return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
72
+ }
64
73
  };
65
74
 
66
75
  test PSRNG {
67
76
  var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
68
- try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
77
+ try expectEqual(121, psrng.range(u8, 0, 256));
69
78
  try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
70
79
  }
71
80
 
@@ -89,6 +98,10 @@ pub const Gen12 = extern struct {
89
98
  self.index = (self.index + 1) % 9;
90
99
  return val;
91
100
  }
101
+
102
+ pub fn advance(self: *Gen12) void {
103
+ _ = self.next();
104
+ }
92
105
  };
93
106
 
94
107
  test Gen12 {
@@ -98,9 +111,9 @@ test Gen12 {
98
111
  try expectEqual(e, rng.next());
99
112
  }
100
113
 
101
- try expectEqual(@as(u8, 16), Gen12.percent(6) + 1);
102
- try expectEqual(@as(u8, 16), Gen12.percent(7) - 1);
103
- try expectEqual(@as(u8, 128), Gen12.percent(50) + 1);
114
+ try expectEqual(16, Gen12.percent(6) + 1);
115
+ try expectEqual(16, Gen12.percent(7) - 1);
116
+ try expectEqual(128, Gen12.percent(50) + 1);
104
117
  }
105
118
 
106
119
  // https://pkmn.cc/pokeemerald/src/random.c
@@ -188,7 +201,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
188
201
  for (0..n) |_| _ = self.next();
189
202
  }
190
203
 
191
- pub fn range(self: *Self, comptime T: type, from: T, to: Bound(T)) T {
204
+ pub fn range(self: *Self, comptime T: type, from: T, to: PSRNG.Bound(T)) T {
192
205
  assert(showdown);
193
206
  return @intCast(@as(u64, self.next()) * (to - from) / divisor + from);
194
207
  }
@@ -197,7 +210,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
197
210
  self: *Self,
198
211
  comptime T: type,
199
212
  numerator: T,
200
- denominator: Bound(T),
213
+ denominator: PSRNG.Bound(T),
201
214
  ) bool {
202
215
  assert(showdown);
203
216
  assert(denominator > 0);
@@ -228,7 +241,3 @@ fn Output(comptime gen: comptime_int) type {
228
241
  else => unreachable,
229
242
  };
230
243
  }
231
-
232
- fn Bound(comptime T: type) type {
233
- return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
234
- }