@pkmn/engine 0.1.0-dev.683fba11 → 0.1.0-dev.6b383c80
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.
- package/LICENSE +1 -1
- package/README.md +22 -16
- package/build/pkg/addon/browser.js +6 -3
- package/build/pkg/addon/browser.js.map +1 -1
- package/build/pkg/addon/node.js +23 -10
- package/build/pkg/addon/node.js.map +1 -1
- package/build/pkg/addon/wasm.js +23 -14
- package/build/pkg/addon/wasm.js.map +1 -1
- package/build/pkg/addon.d.ts +5 -5
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +2 -1
- package/build/pkg/gen1.js +22 -9
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +2 -0
- package/build/pkg/index.js +17 -7
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +11 -2
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +54 -11
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +19 -94
- package/build/tools/display/debug.jsx.map +1 -1
- package/build/tools/display/index.js +18 -7
- package/build/tools/display/index.js.map +1 -1
- package/build/tools/display/ui.jsx +20 -9
- package/build/tools/display/ui.jsx.map +1 -1
- package/build/tools/display/util.js +140 -1
- package/build/tools/display/util.js.map +1 -1
- package/build.zig +255 -313
- package/package.json +29 -131
- package/src/bin/install-pkmn-engine +24 -22
- package/src/lib/bindings/c.zig +201 -0
- package/src/lib/bindings/node.zig +117 -0
- package/src/lib/bindings/wasm.zig +51 -0
- package/src/lib/common/array.zig +9 -16
- package/src/lib/common/data.zig +8 -8
- package/src/lib/common/debug.zig +46 -61
- package/src/lib/common/js.zig +3 -9
- package/src/lib/common/optional.zig +20 -25
- package/src/lib/common/protocol.zig +224 -189
- package/src/lib/common/rational.zig +275 -284
- package/src/lib/common/rng.zig +4 -4
- package/src/lib/common/util.zig +34 -31
- package/src/lib/gen1/calc.zig +141 -119
- package/src/lib/gen1/chance.zig +162 -101
- package/src/lib/gen1/data/types.zig +2 -2
- package/src/lib/gen1/data.zig +27 -24
- package/src/lib/gen1/helpers.zig +36 -26
- package/src/lib/gen1/mechanics.zig +45 -49
- package/src/lib/gen2/calc.zig +3 -6
- package/src/lib/gen2/chance.zig +55 -43
- package/src/lib/gen2/data/types.zig +6 -3
- package/src/lib/gen2/data.zig +20 -16
- package/src/lib/gen2/helpers.zig +27 -19
- package/src/lib/gen2/mechanics.zig +17 -17
- package/src/lib/napi.h +2 -0
- package/src/lib/node.zig +3 -118
- package/src/lib/pkmn.zig +94 -19
- package/src/lib/wasm.zig +9 -44
- package/src/tools/display/dom.ts +43 -31
|
@@ -6,20 +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
11
|
/// invoked explicitly before reading. Note that this laziness means that this implementation
|
|
19
12
|
/// sometimes overflows in places where an eager reduced implementation would not - to achieve
|
|
20
13
|
/// the same behavior as an eager implementation simply call `reduce` after each operation.
|
|
21
14
|
pub fn Rational(comptime T: type) type {
|
|
22
|
-
|
|
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 {
|
|
23
32
|
const Self = @This();
|
|
24
33
|
|
|
25
34
|
/// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
@@ -27,21 +36,54 @@ pub fn Rational(comptime T: type) type {
|
|
|
27
36
|
/// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
28
37
|
q: T = 1,
|
|
29
38
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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,
|
|
38
84
|
|
|
39
85
|
/// Possible error returned by operations on the Rational.
|
|
40
|
-
pub const Error =
|
|
41
|
-
Int => error{Overflow},
|
|
42
|
-
Float => error{},
|
|
43
|
-
else => unreachable,
|
|
44
|
-
};
|
|
86
|
+
pub const Error = Err;
|
|
45
87
|
|
|
46
88
|
/// Resets the rational back to 1.
|
|
47
89
|
pub fn reset(r: *Self) void {
|
|
@@ -52,162 +94,212 @@ pub fn Rational(comptime T: type) type {
|
|
|
52
94
|
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
53
95
|
/// Both p and q must be >= 1, and if computable at comptime must have no common factors.
|
|
54
96
|
pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
|
|
55
|
-
|
|
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
|
-
}
|
|
97
|
+
return update_(T, o, r, p, q);
|
|
96
98
|
}
|
|
97
99
|
|
|
98
100
|
/// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
|
|
99
|
-
pub fn add(r: *Self, s:
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
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);
|
|
121
145
|
},
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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 => {
|
|
130
185
|
r.reduce();
|
|
131
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
|
+
}
|
|
132
207
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
208
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
209
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
210
|
+
},
|
|
211
|
+
else => unreachable,
|
|
212
|
+
}
|
|
213
|
+
}
|
|
136
214
|
|
|
137
|
-
|
|
138
|
-
|
|
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);
|
|
139
223
|
},
|
|
140
224
|
else => unreachable,
|
|
141
|
-
}
|
|
142
|
-
}
|
|
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();
|
|
143
230
|
|
|
144
|
-
|
|
145
|
-
|
|
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();
|
|
231
|
+
r.p *= s.p;
|
|
232
|
+
r.q *= s.q;
|
|
160
233
|
|
|
161
|
-
|
|
162
|
-
|
|
234
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
235
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
236
|
+
},
|
|
237
|
+
else => unreachable,
|
|
238
|
+
}
|
|
239
|
+
}
|
|
163
240
|
|
|
164
|
-
|
|
165
|
-
|
|
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);
|
|
166
249
|
},
|
|
167
250
|
else => unreachable,
|
|
168
|
-
}
|
|
169
|
-
|
|
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
|
+
};
|
|
170
260
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
if (
|
|
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();
|
|
175
266
|
|
|
176
|
-
|
|
177
|
-
|
|
267
|
+
const ad: T = r.p * s.q;
|
|
268
|
+
const bc: T = r.q * s.p;
|
|
178
269
|
|
|
179
|
-
|
|
180
|
-
|
|
270
|
+
return std.math.order(ad, bc);
|
|
271
|
+
},
|
|
272
|
+
else => unreachable,
|
|
273
|
+
}
|
|
274
|
+
}
|
|
181
275
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
276
|
+
fn reduce_(r: anytype) void {
|
|
277
|
+
const d = gcd(r.p, r.q);
|
|
278
|
+
if (d == 1) return;
|
|
185
279
|
|
|
186
|
-
|
|
187
|
-
|
|
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
|
-
}
|
|
280
|
+
assert(@mod(r.p, d) == 0);
|
|
281
|
+
assert(@mod(r.q, d) == 0);
|
|
195
282
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
r.q = try std.math.mul(T, r.q, q);
|
|
199
|
-
}
|
|
283
|
+
r.p /= d;
|
|
284
|
+
r.q /= d;
|
|
200
285
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
const n1 = try std.math.mul(T, r.p, q);
|
|
205
|
-
const n2 = try std.math.mul(T, r.q, p);
|
|
286
|
+
assert(r.p >= 1);
|
|
287
|
+
assert(r.q >= 1);
|
|
288
|
+
}
|
|
206
289
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
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;
|
|
211
303
|
}
|
|
212
304
|
|
|
213
305
|
fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
|
|
@@ -221,7 +313,7 @@ fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
|
|
|
221
313
|
};
|
|
222
314
|
|
|
223
315
|
switch (@typeInfo(T)) {
|
|
224
|
-
|
|
316
|
+
.int => {
|
|
225
317
|
// std.math.gcd but without some checks because we have a stricter range
|
|
226
318
|
var x: T = @intCast(p);
|
|
227
319
|
var y: T = @intCast(q);
|
|
@@ -262,8 +354,7 @@ fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
|
|
|
262
354
|
}
|
|
263
355
|
|
|
264
356
|
test gcd {
|
|
265
|
-
|
|
266
|
-
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);
|
|
267
358
|
var random = prng.random();
|
|
268
359
|
|
|
269
360
|
for (0..1000) |_| {
|
|
@@ -289,8 +380,9 @@ fn doTurn(r: anytype) !void {
|
|
|
289
380
|
}
|
|
290
381
|
|
|
291
382
|
test Rational {
|
|
292
|
-
inline for (.{ u64, u128, f64 }) |t| {
|
|
293
|
-
|
|
383
|
+
inline for (.{ u64, u128, u256, f64 }) |t| {
|
|
384
|
+
const R = Rational(t);
|
|
385
|
+
var r: R = .{};
|
|
294
386
|
|
|
295
387
|
var c: t = 128;
|
|
296
388
|
_ = &c;
|
|
@@ -299,7 +391,7 @@ test Rational {
|
|
|
299
391
|
try doTurn(&r);
|
|
300
392
|
|
|
301
393
|
r.reduce();
|
|
302
|
-
try expectEqual(
|
|
394
|
+
try expectEqual(R{ .p = 75383, .q = 35550920704 }, r);
|
|
303
395
|
|
|
304
396
|
try r.update(1, 4);
|
|
305
397
|
if (t == u64) {
|
|
@@ -307,143 +399,42 @@ test Rational {
|
|
|
307
399
|
} else {
|
|
308
400
|
try doTurn(&r);
|
|
309
401
|
r.reduce();
|
|
310
|
-
try expectEqual(
|
|
402
|
+
try expectEqual(R{ .p = 5682596689, .q = 2527735925804191711232 }, r);
|
|
311
403
|
}
|
|
312
404
|
|
|
313
405
|
r.reset();
|
|
314
406
|
|
|
315
|
-
var s =
|
|
407
|
+
var s = R{ .p = 10, .q = 13 };
|
|
316
408
|
try r.mul(&s);
|
|
317
|
-
s =
|
|
409
|
+
s = R{ .p = 3, .q = 4 };
|
|
318
410
|
try r.mul(&s);
|
|
319
|
-
try expectEqual(
|
|
411
|
+
try expectEqual(R{ .p = 30, .q = 52 }, r);
|
|
320
412
|
|
|
321
|
-
s =
|
|
413
|
+
s = R{ .p = 1, .q = 3 };
|
|
322
414
|
try r.add(&s);
|
|
323
415
|
r.reduce();
|
|
324
|
-
try expectEqual(
|
|
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
|
-
}
|
|
416
|
+
try expectEqual(R{ .p = 71, .q = 78 }, r);
|
|
368
417
|
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
try
|
|
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();
|
|
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);
|
|
399
423
|
|
|
400
|
-
|
|
401
|
-
|
|
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);
|
|
402
428
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
try t.val.setRatio(3, 4);
|
|
406
|
-
try r.mul(&t);
|
|
429
|
+
v = R{ .p = 3, .q = 5 };
|
|
430
|
+
try expectEqual(u.cmp(&v), .eq);
|
|
407
431
|
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
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
|
|
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);
|
|
441
438
|
}
|
|
442
439
|
}
|
|
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
440
|
}
|
package/src/lib/common/rng.zig
CHANGED
|
@@ -72,7 +72,7 @@ pub const PSRNG = extern struct {
|
|
|
72
72
|
|
|
73
73
|
test PSRNG {
|
|
74
74
|
var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
|
|
75
|
-
try expectEqual(
|
|
75
|
+
try expectEqual(121, psrng.range(u8, 0, 256));
|
|
76
76
|
try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
|
|
77
77
|
}
|
|
78
78
|
|
|
@@ -109,9 +109,9 @@ test Gen12 {
|
|
|
109
109
|
try expectEqual(e, rng.next());
|
|
110
110
|
}
|
|
111
111
|
|
|
112
|
-
try expectEqual(
|
|
113
|
-
try expectEqual(
|
|
114
|
-
try expectEqual(
|
|
112
|
+
try expectEqual(16, Gen12.percent(6) + 1);
|
|
113
|
+
try expectEqual(16, Gen12.percent(7) - 1);
|
|
114
|
+
try expectEqual(128, Gen12.percent(50) + 1);
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
// https://pkmn.cc/pokeemerald/src/random.c
|