@pkmn/engine 0.1.0-dev.f3aab252 → 0.1.0-dev.f3b63b86

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