@pkmn/engine 0.1.0-dev.a1fcc5d9 → 0.1.0-dev.a27b3ff3
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 +51 -5
- package/build/pkg/addon/browser.d.ts +1 -1
- package/build/pkg/addon/browser.js +6 -2
- package/build/pkg/addon/browser.js.map +1 -1
- package/build/pkg/addon/node.js +22 -8
- package/build/pkg/addon/node.js.map +1 -1
- package/build/pkg/addon/wasm.d.ts +2 -0
- package/build/pkg/addon/wasm.js +53 -0
- package/build/pkg/addon/wasm.js.map +1 -0
- package/build/pkg/addon.d.ts +24 -4
- package/build/pkg/addon.js +8 -17
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/layout.json +15 -2
- package/build/pkg/data/protocol.json +23 -9
- package/build/pkg/gen1.d.ts +15 -18
- package/build/pkg/gen1.js +28 -19
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +37 -100
- package/build/pkg/index.js +23 -75
- package/build/pkg/index.js.map +1 -1
- package/build/tools/debug.js +18 -9
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +18 -7
- package/build/tools/display/debug.jsx.map +1 -1
- package/build/tools/display/index.js +35 -15
- package/build/tools/display/index.js.map +1 -1
- package/build/tools/display/ui.jsx +17 -7
- package/build/tools/display/ui.jsx.map +1 -1
- package/build/tools/display/util.js +16 -1
- package/build/tools/display/util.js.map +1 -1
- package/build.zig +198 -85
- package/package.json +29 -125
- package/src/bin/install-pkmn-engine +3 -2
- package/src/lib/bindings/c.zig +255 -0
- package/src/lib/bindings/node.zig +117 -0
- package/src/lib/bindings/wasm.zig +59 -0
- package/src/lib/common/array.zig +1 -1
- package/src/lib/common/js.zig +1 -5
- package/src/lib/common/napi.zig +4 -0
- package/src/lib/common/optional.zig +1 -1
- package/src/lib/common/options.zig +2 -0
- package/src/lib/common/protocol.zig +4 -8
- package/src/lib/common/rational.zig +281 -270
- package/src/lib/common/rng.zig +1 -3
- package/src/lib/common/util.zig +17 -2
- package/src/lib/gen1/calc.zig +52 -69
- package/src/lib/gen1/chance.zig +34 -18
- package/src/lib/gen1/data/moves.zig +2 -4
- package/src/lib/gen1/data/species.zig +1 -3
- package/src/lib/gen1/data/types.zig +2 -3
- package/src/lib/gen1/data.zig +7 -11
- package/src/lib/gen1/helpers.zig +4 -10
- package/src/lib/gen1/mechanics.zig +76 -51
- package/src/lib/gen2/calc.zig +10 -18
- package/src/lib/gen2/data/items.zig +1 -3
- package/src/lib/gen2/data/moves.zig +2 -4
- package/src/lib/gen2/data/species.zig +1 -3
- package/src/lib/gen2/data/types.zig +7 -5
- package/src/lib/gen2/data.zig +14 -17
- package/src/lib/gen2/generated.zig +18 -23
- package/src/lib/gen2/helpers.zig +5 -12
- package/src/lib/gen2/mechanics.zig +42 -39
- package/src/lib/node.zig +4 -121
- package/src/lib/pkmn.zig +7 -2
- package/src/lib/wasm.zig +13 -9
- package/src/tools/display/dom.ts +5 -8
- package/src/tools/display/ui.css +0 -5
|
@@ -19,7 +19,23 @@ const ComptimeFloat =
|
|
|
19
19
|
/// sometimes overflows in places where an eager reduced implementation would not - to achieve
|
|
20
20
|
/// the same behavior as an eager implementation simply call `reduce` after each operation.
|
|
21
21
|
pub fn Rational(comptime T: type) type {
|
|
22
|
-
|
|
22
|
+
// With floats we can't rely on an overflow bit to let us know when to reduce, so we
|
|
23
|
+
// instead start reducing when we get sufficiently close to the limit of the mantissa
|
|
24
|
+
// (in our domain we expect updates to involve numbers < 2**10, so we should be safe
|
|
25
|
+
// not reducing before we are 2**10 away from "overflowing" the mantissa)
|
|
26
|
+
const o = if (@typeInfo(T) == Float)
|
|
27
|
+
std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
|
|
28
|
+
else
|
|
29
|
+
0;
|
|
30
|
+
|
|
31
|
+
const Err = switch (@typeInfo(T)) {
|
|
32
|
+
Int => error{Overflow},
|
|
33
|
+
Float => error{},
|
|
34
|
+
else => unreachable,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// Zig doesn't allow integers > u128 in an extern struct...
|
|
38
|
+
return if (@sizeOf(T) <= 16) extern struct {
|
|
23
39
|
const Self = @This();
|
|
24
40
|
|
|
25
41
|
/// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
@@ -27,21 +43,8 @@ pub fn Rational(comptime T: type) type {
|
|
|
27
43
|
/// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
28
44
|
q: T = 1,
|
|
29
45
|
|
|
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
46
|
/// Possible error returned by operations on the Rational.
|
|
40
|
-
pub const Error =
|
|
41
|
-
Int => error{Overflow},
|
|
42
|
-
Float => error{},
|
|
43
|
-
else => unreachable,
|
|
44
|
-
};
|
|
47
|
+
pub const Error = Err;
|
|
45
48
|
|
|
46
49
|
/// Resets the rational back to 1.
|
|
47
50
|
pub fn reset(r: *Self) void {
|
|
@@ -52,135 +55,27 @@ pub fn Rational(comptime T: type) type {
|
|
|
52
55
|
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
53
56
|
/// Both p and q must be >= 1, and if computable at comptime must have no common factors.
|
|
54
57
|
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
|
-
}
|
|
58
|
+
return update_(T, o, r, p, q);
|
|
96
59
|
}
|
|
97
60
|
|
|
98
61
|
/// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
|
|
99
|
-
pub fn add(r: *Self, s:
|
|
100
|
-
|
|
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
|
-
}
|
|
62
|
+
pub fn add(r: *Self, s: anytype) Error!void {
|
|
63
|
+
return add_(T, o, r, s);
|
|
142
64
|
}
|
|
143
65
|
|
|
144
66
|
/// Multiplies two rationals.
|
|
145
|
-
pub fn mul(r: *Self, s:
|
|
146
|
-
|
|
147
|
-
|
|
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;
|
|
67
|
+
pub fn mul(r: *Self, s: anytype) Error!void {
|
|
68
|
+
return mul_(T, o, r, s);
|
|
69
|
+
}
|
|
163
70
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
else => unreachable,
|
|
168
|
-
}
|
|
71
|
+
/// Compares two rationals using the identity (a/b) > (c/d) => ad > bc
|
|
72
|
+
pub fn cmp(r: *Self, s: anytype) Error!std.math.Order {
|
|
73
|
+
return cmp_(T, o, r, s);
|
|
169
74
|
}
|
|
170
75
|
|
|
171
76
|
/// Normalize the rational by reducing by the greatest common divisor.
|
|
172
77
|
pub fn reduce(r: *Self) void {
|
|
173
|
-
|
|
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);
|
|
78
|
+
reduce_(r);
|
|
184
79
|
}
|
|
185
80
|
|
|
186
81
|
pub fn format(
|
|
@@ -192,24 +87,240 @@ pub fn Rational(comptime T: type) type {
|
|
|
192
87
|
_ = .{ fmt, opts };
|
|
193
88
|
try writer.print("{d}/{d}", .{ self.p, self.q });
|
|
194
89
|
}
|
|
90
|
+
} else struct {
|
|
91
|
+
const Self = @This();
|
|
92
|
+
|
|
93
|
+
/// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
94
|
+
p: T = 1,
|
|
95
|
+
/// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
96
|
+
q: T = 1,
|
|
195
97
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
98
|
+
/// Possible error returned by operations on the Rational.
|
|
99
|
+
pub const Error = Err;
|
|
100
|
+
|
|
101
|
+
/// Resets the rational back to 1.
|
|
102
|
+
pub fn reset(r: *Self) void {
|
|
103
|
+
r.p = 1;
|
|
104
|
+
r.q = 1;
|
|
199
105
|
}
|
|
200
106
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
107
|
+
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
108
|
+
/// Both p and q must be >= 1, and if computable at comptime must have no common factors.
|
|
109
|
+
pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
|
|
110
|
+
return update_(T, o, r, p, q);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
|
|
114
|
+
pub fn add(r: *Self, s: anytype) Error!void {
|
|
115
|
+
return add_(T, o, r, s);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/// Multiplies two rationals.
|
|
119
|
+
pub fn mul(r: *Self, s: anytype) Error!void {
|
|
120
|
+
return mul_(T, o, r, s);
|
|
121
|
+
}
|
|
206
122
|
|
|
207
|
-
|
|
208
|
-
|
|
123
|
+
/// Compares two rationals using the identity (a/b) > (c/d) => ad > bc
|
|
124
|
+
pub fn cmp(r: *Self, s: anytype) Error!std.math.Order {
|
|
125
|
+
return cmp_(T, o, r, s);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/// Normalize the rational by reducing by the greatest common divisor.
|
|
129
|
+
pub fn reduce(r: *Self) void {
|
|
130
|
+
reduce_(r);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
pub fn format(
|
|
134
|
+
self: Self,
|
|
135
|
+
comptime fmt: []const u8,
|
|
136
|
+
opts: std.fmt.FormatOptions,
|
|
137
|
+
writer: anytype,
|
|
138
|
+
) !void {
|
|
139
|
+
_ = .{ fmt, opts };
|
|
140
|
+
try writer.print("{d}/{d}", .{ self.p, self.q });
|
|
209
141
|
}
|
|
210
142
|
};
|
|
211
143
|
}
|
|
212
144
|
|
|
145
|
+
fn update_(comptime T: type, comptime o: comptime_int, r: anytype, p: anytype, q: anytype) !void {
|
|
146
|
+
// std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
|
|
147
|
+
assert(p >= 1);
|
|
148
|
+
assert(q >= 1);
|
|
149
|
+
|
|
150
|
+
// If our parameters are not fully reduced they may prematurely
|
|
151
|
+
// cause overflow/loss of precision after the multiplication below
|
|
152
|
+
assert(switch (@typeInfo(@TypeOf(p, q))) {
|
|
153
|
+
ComptimeInt, ComptimeFloat => comptime gcd(p, q),
|
|
154
|
+
else => 1,
|
|
155
|
+
} == 1);
|
|
156
|
+
|
|
157
|
+
switch (@typeInfo(T)) {
|
|
158
|
+
Int => {
|
|
159
|
+
// Greedily attempt to multiply and if it fails, reduce and try again
|
|
160
|
+
multiplication(T, r, p, q) catch |err| switch (err) {
|
|
161
|
+
error.Overflow => {
|
|
162
|
+
r.reduce();
|
|
163
|
+
try multiplication(T, r, p, q);
|
|
164
|
+
},
|
|
165
|
+
else => unreachable,
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
Float => {
|
|
169
|
+
// Reduce in situations where we're likely to start losing precision
|
|
170
|
+
if (r.q > o or r.p > o) r.reduce();
|
|
171
|
+
|
|
172
|
+
r.p *= switch (@typeInfo(@TypeOf(p))) {
|
|
173
|
+
Float, ComptimeFloat => p,
|
|
174
|
+
else => @floatFromInt(p),
|
|
175
|
+
};
|
|
176
|
+
r.q *= switch (@typeInfo(@TypeOf(q))) {
|
|
177
|
+
Float, ComptimeFloat => q,
|
|
178
|
+
else => @floatFromInt(q),
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// We should always be dealing with whole numbers
|
|
182
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
183
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
184
|
+
},
|
|
185
|
+
else => unreachable,
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
fn add_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
|
|
190
|
+
switch (@typeInfo(T)) {
|
|
191
|
+
Int => {
|
|
192
|
+
if (r.q == s.q) {
|
|
193
|
+
r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
|
|
194
|
+
error.Overflow => val: {
|
|
195
|
+
r.reduce();
|
|
196
|
+
s.reduce();
|
|
197
|
+
break :val try std.math.add(T, r.p, s.p);
|
|
198
|
+
},
|
|
199
|
+
else => unreachable,
|
|
200
|
+
};
|
|
201
|
+
} else {
|
|
202
|
+
addition(T, r, s.p, s.q) catch |err| switch (err) {
|
|
203
|
+
error.Overflow => {
|
|
204
|
+
r.reduce();
|
|
205
|
+
s.reduce();
|
|
206
|
+
try addition(T, r, s.p, s.q);
|
|
207
|
+
},
|
|
208
|
+
else => unreachable,
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
Float => {
|
|
213
|
+
if (r.q == s.q) {
|
|
214
|
+
if (r.p > o) r.reduce();
|
|
215
|
+
if (s.p > o) s.reduce();
|
|
216
|
+
|
|
217
|
+
r.p += s.p;
|
|
218
|
+
} else {
|
|
219
|
+
// Always reduce to minimize loss of precision from the multiplications
|
|
220
|
+
r.reduce();
|
|
221
|
+
s.reduce();
|
|
222
|
+
|
|
223
|
+
r.p = (r.p * s.q) + (r.q * s.p);
|
|
224
|
+
r.q *= s.q;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
228
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
229
|
+
},
|
|
230
|
+
else => unreachable,
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
fn mul_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
|
|
235
|
+
switch (@typeInfo(T)) {
|
|
236
|
+
Int => {
|
|
237
|
+
multiplication(T, r, s.p, s.q) catch |err| switch (err) {
|
|
238
|
+
error.Overflow => {
|
|
239
|
+
r.reduce();
|
|
240
|
+
s.reduce();
|
|
241
|
+
try multiplication(T, r, s.p, s.q);
|
|
242
|
+
},
|
|
243
|
+
else => unreachable,
|
|
244
|
+
};
|
|
245
|
+
},
|
|
246
|
+
Float => {
|
|
247
|
+
if (r.q > o or r.p > o) r.reduce();
|
|
248
|
+
if (s.q > o or s.p > o) s.reduce();
|
|
249
|
+
|
|
250
|
+
r.p *= s.p;
|
|
251
|
+
r.q *= s.q;
|
|
252
|
+
|
|
253
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
254
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
255
|
+
},
|
|
256
|
+
else => unreachable,
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
fn cmp_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !std.math.Order {
|
|
261
|
+
switch (@typeInfo(T)) {
|
|
262
|
+
Int => {
|
|
263
|
+
const ad = std.math.mul(T, r.p, s.q) catch |err| switch (err) {
|
|
264
|
+
error.Overflow => ad: {
|
|
265
|
+
r.reduce();
|
|
266
|
+
s.reduce();
|
|
267
|
+
break :ad try std.math.mul(T, r.p, s.q);
|
|
268
|
+
},
|
|
269
|
+
else => unreachable,
|
|
270
|
+
};
|
|
271
|
+
const bc = std.math.mul(T, r.q, s.p) catch |err| switch (err) {
|
|
272
|
+
error.Overflow => bc: {
|
|
273
|
+
r.reduce();
|
|
274
|
+
s.reduce();
|
|
275
|
+
break :bc try std.math.mul(T, r.q, s.p);
|
|
276
|
+
},
|
|
277
|
+
else => unreachable,
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
return std.math.order(ad, bc);
|
|
281
|
+
},
|
|
282
|
+
Float => {
|
|
283
|
+
if (r.q > o or r.p > o) r.reduce();
|
|
284
|
+
if (s.q > o or s.p > o) s.reduce();
|
|
285
|
+
|
|
286
|
+
const ad: T = r.p * s.q;
|
|
287
|
+
const bc: T = r.q * s.p;
|
|
288
|
+
|
|
289
|
+
return std.math.order(ad, bc);
|
|
290
|
+
},
|
|
291
|
+
else => unreachable,
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
fn reduce_(r: anytype) void {
|
|
296
|
+
const d = gcd(r.p, r.q);
|
|
297
|
+
if (d == 1) return;
|
|
298
|
+
|
|
299
|
+
assert(@mod(r.p, d) == 0);
|
|
300
|
+
assert(@mod(r.q, d) == 0);
|
|
301
|
+
|
|
302
|
+
r.p /= d;
|
|
303
|
+
r.q /= d;
|
|
304
|
+
|
|
305
|
+
assert(r.p >= 1);
|
|
306
|
+
assert(r.q >= 1);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
fn multiplication(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
|
|
310
|
+
r.p = try std.math.mul(T, r.p, p);
|
|
311
|
+
r.q = try std.math.mul(T, r.q, q);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
fn addition(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
|
|
315
|
+
// (a/b) + (c/d) = (ad+bc)/(bd)
|
|
316
|
+
const bd = try std.math.mul(T, r.q, q);
|
|
317
|
+
const ad = try std.math.mul(T, r.p, q);
|
|
318
|
+
const bc = try std.math.mul(T, r.q, p);
|
|
319
|
+
|
|
320
|
+
r.p = try std.math.add(T, ad, bc);
|
|
321
|
+
r.q = bd;
|
|
322
|
+
}
|
|
323
|
+
|
|
213
324
|
fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
|
|
214
325
|
assert(p >= 1);
|
|
215
326
|
assert(q >= 1);
|
|
@@ -289,8 +400,9 @@ fn doTurn(r: anytype) !void {
|
|
|
289
400
|
}
|
|
290
401
|
|
|
291
402
|
test Rational {
|
|
292
|
-
inline for (.{ u64, u128, f64 }) |t| {
|
|
293
|
-
|
|
403
|
+
inline for (.{ u64, u128, u256, f64 }) |t| {
|
|
404
|
+
const R = Rational(t);
|
|
405
|
+
var r: R = .{};
|
|
294
406
|
|
|
295
407
|
var c: t = 128;
|
|
296
408
|
_ = &c;
|
|
@@ -299,7 +411,7 @@ test Rational {
|
|
|
299
411
|
try doTurn(&r);
|
|
300
412
|
|
|
301
413
|
r.reduce();
|
|
302
|
-
try expectEqual(
|
|
414
|
+
try expectEqual(R{ .p = 75383, .q = 35550920704 }, r);
|
|
303
415
|
|
|
304
416
|
try r.update(1, 4);
|
|
305
417
|
if (t == u64) {
|
|
@@ -307,143 +419,42 @@ test Rational {
|
|
|
307
419
|
} else {
|
|
308
420
|
try doTurn(&r);
|
|
309
421
|
r.reduce();
|
|
310
|
-
try expectEqual(
|
|
422
|
+
try expectEqual(R{ .p = 5682596689, .q = 2527735925804191711232 }, r);
|
|
311
423
|
}
|
|
312
424
|
|
|
313
425
|
r.reset();
|
|
314
426
|
|
|
315
|
-
var s =
|
|
427
|
+
var s = R{ .p = 10, .q = 13 };
|
|
316
428
|
try r.mul(&s);
|
|
317
|
-
s =
|
|
429
|
+
s = R{ .p = 3, .q = 4 };
|
|
318
430
|
try r.mul(&s);
|
|
319
|
-
try expectEqual(
|
|
431
|
+
try expectEqual(R{ .p = 30, .q = 52 }, r);
|
|
320
432
|
|
|
321
|
-
s =
|
|
433
|
+
s = R{ .p = 1, .q = 3 };
|
|
322
434
|
try r.add(&s);
|
|
323
435
|
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
|
-
}
|
|
436
|
+
try expectEqual(R{ .p = 71, .q = 78 }, r);
|
|
363
437
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
try
|
|
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);
|
|
438
|
+
var u = R{ .p = 3, .q = 5 };
|
|
439
|
+
var v = R{ .p = 2, .q = 5 };
|
|
440
|
+
try expectEqual(u.cmp(&v), .gt);
|
|
441
|
+
v = R{ .p = 4, .q = 5 };
|
|
442
|
+
try expectEqual(u.cmp(&v), .lt);
|
|
397
443
|
|
|
398
|
-
|
|
444
|
+
v = R{ .p = 3, .q = 6 };
|
|
445
|
+
try expectEqual(u.cmp(&v), .gt);
|
|
446
|
+
v = R{ .p = 3, .q = 4 };
|
|
447
|
+
try expectEqual(u.cmp(&v), .lt);
|
|
399
448
|
|
|
400
|
-
|
|
401
|
-
|
|
449
|
+
v = R{ .p = 3, .q = 5 };
|
|
450
|
+
try expectEqual(u.cmp(&v), .eq);
|
|
402
451
|
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
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
|
|
452
|
+
try doTurn(&u);
|
|
453
|
+
v = R{ .p = 4567216874, .q = 89124781931235 };
|
|
454
|
+
if (t == u64) {
|
|
455
|
+
try expectEqual(u.cmp(&v), error.Overflow);
|
|
456
|
+
} else {
|
|
457
|
+
try expectEqual(u.cmp(&v), .lt);
|
|
441
458
|
}
|
|
442
459
|
}
|
|
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
460
|
}
|
package/src/lib/common/rng.zig
CHANGED
|
@@ -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 {
|
package/src/lib/common/util.zig
CHANGED
|
@@ -4,6 +4,7 @@ const expect = std.testing.expect;
|
|
|
4
4
|
const expectEqual = std.testing.expectEqual;
|
|
5
5
|
|
|
6
6
|
const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
|
|
7
|
+
const One = if (@hasField(std.builtin.Type.Pointer.Size, "one")) .one else .One;
|
|
7
8
|
|
|
8
9
|
pub fn PointerType(comptime P: type, comptime C: type) type {
|
|
9
10
|
return if (@field(@typeInfo(P), @tagName(Pointer)).is_const) *const C else *C;
|
|
@@ -35,7 +36,7 @@ pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T,
|
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
|
|
38
|
-
return CopyPtrAttrs(B,
|
|
39
|
+
return CopyPtrAttrs(B, One, T);
|
|
39
40
|
}
|
|
40
41
|
|
|
41
42
|
fn CopyPtrAttrs(
|
|
@@ -44,7 +45,7 @@ fn CopyPtrAttrs(
|
|
|
44
45
|
comptime child: type,
|
|
45
46
|
) type {
|
|
46
47
|
const info = @field(@typeInfo(source), @tagName(Pointer));
|
|
47
|
-
const args = .{
|
|
48
|
+
const args: std.builtin.Type.Pointer = if (@hasField(std.builtin.Type.Pointer, "sentinel")) .{
|
|
48
49
|
.size = size,
|
|
49
50
|
.is_const = info.is_const,
|
|
50
51
|
.is_volatile = info.is_volatile,
|
|
@@ -53,9 +54,23 @@ fn CopyPtrAttrs(
|
|
|
53
54
|
.address_space = info.address_space,
|
|
54
55
|
.child = child,
|
|
55
56
|
.sentinel = null,
|
|
57
|
+
} else .{
|
|
58
|
+
.size = size,
|
|
59
|
+
.is_const = info.is_const,
|
|
60
|
+
.is_volatile = info.is_volatile,
|
|
61
|
+
.is_allowzero = info.is_allowzero,
|
|
62
|
+
.alignment = info.alignment,
|
|
63
|
+
.address_space = info.address_space,
|
|
64
|
+
.child = child,
|
|
65
|
+
.sentinel_ptr = null,
|
|
56
66
|
};
|
|
57
67
|
return @Type(if (@hasField(std.builtin.Type, "pointer"))
|
|
58
68
|
.{ .pointer = args }
|
|
59
69
|
else
|
|
60
70
|
.{ .Pointer = args });
|
|
61
71
|
}
|
|
72
|
+
|
|
73
|
+
// NOTE: std.meta.FieldType replaced by @FieldType in 0.14.0
|
|
74
|
+
pub fn FieldType(comptime T: type, comptime field: std.meta.FieldEnum(T)) type {
|
|
75
|
+
return std.meta.fieldInfo(T, field).type;
|
|
76
|
+
}
|