@pkmn/engine 0.1.0-dev.deda8593 → 0.1.0-dev.e0a786f8
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/README.md +9 -6
- package/build/pkg/addon/browser.d.ts +3 -0
- package/build/pkg/addon/browser.js +47 -0
- package/build/pkg/addon/browser.js.map +1 -0
- package/build/pkg/addon/node.d.ts +3 -0
- package/build/pkg/addon/node.js +110 -0
- package/build/pkg/addon/node.js.map +1 -0
- 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 +25 -3
- package/build/pkg/addon.js +34 -65
- 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/data.d.ts +23 -1
- package/build/pkg/data.js.map +1 -1
- 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 +40 -90
- package/build/pkg/index.js +23 -70
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +20 -1
- package/build/pkg/protocol.js +105 -103
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +52 -76
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +152 -0
- package/build/tools/display/debug.jsx.map +1 -0
- package/build/tools/display/index.js +152 -0
- package/build/tools/display/index.js.map +1 -0
- package/build/tools/display/ui.jsx +359 -0
- package/build/tools/display/ui.jsx.map +1 -0
- package/build/tools/display/util.js +33 -0
- package/build/tools/display/util.js.map +1 -0
- package/build.zig +330 -85
- package/package.json +54 -135
- package/src/bin/install-pkmn-engine +10 -7
- package/src/bin/pkmn-debug +1 -1
- package/src/lib/bindings/c.zig +255 -0
- package/src/lib/bindings/node.zig +121 -0
- package/src/lib/bindings/wasm.zig +59 -0
- package/src/lib/common/array.zig +77 -0
- package/src/lib/common/data.zig +8 -8
- package/src/lib/common/debug.zig +39 -8
- package/src/lib/common/js.zig +4 -2
- package/src/lib/common/optional.zig +13 -9
- package/src/lib/common/options.zig +4 -0
- package/src/lib/common/protocol.zig +45 -44
- package/src/lib/common/rational.zig +323 -157
- package/src/lib/common/rng.zig +5 -7
- package/src/lib/common/util.zig +60 -1
- package/src/lib/gen1/calc.zig +527 -161
- package/src/lib/gen1/chance.zig +366 -240
- 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 +1 -2
- package/src/lib/gen1/data.zig +35 -38
- package/src/lib/gen1/helpers.zig +17 -554
- package/src/lib/gen1/mechanics.zig +280 -193
- package/src/lib/gen2/calc.zig +37 -27
- package/src/lib/gen2/chance.zig +90 -503
- 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 +1 -2
- package/src/lib/gen2/data.zig +26 -29
- package/src/lib/gen2/generated.zig +18 -23
- package/src/lib/gen2/helpers.zig +9 -18
- package/src/lib/gen2/mechanics.zig +88 -115
- package/src/lib/node.zig +3 -116
- package/src/lib/pkmn.zig +14 -7
- package/src/lib/wasm.zig +13 -9
- package/src/tools/display/dom.ts +93 -0
- package/src/tools/display/ui.css +354 -0
- package/build/test/display.js +0 -400
- package/build/test/display.js.map +0 -1
- package/src/test/display/pkmn.html.tmpl +0 -368
|
@@ -6,11 +6,36 @@ 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
|
+
|
|
9
16
|
/// Specialization of a rational number used by the engine to compute probabilties.
|
|
10
17
|
/// For performance reasons the rational is only reduced lazily and thus `reduce` must be
|
|
11
|
-
/// invoked explicitly before reading.
|
|
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.
|
|
12
21
|
pub fn Rational(comptime T: type) type {
|
|
13
|
-
|
|
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 {
|
|
14
39
|
const Self = @This();
|
|
15
40
|
|
|
16
41
|
/// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
@@ -18,21 +43,8 @@ pub fn Rational(comptime T: type) type {
|
|
|
18
43
|
/// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
19
44
|
q: T = 1,
|
|
20
45
|
|
|
21
|
-
// With floats we can't rely on an overflow bit to let us know when to reduce, so we instead
|
|
22
|
-
// start reducing when we get sufficiently close to the limit of the mantissa (in our domain
|
|
23
|
-
// we expect updates to involve numbers < 2**10, so we should be safe not reducing before we
|
|
24
|
-
// are 2**10 away from "overflowing" the mantissa)
|
|
25
|
-
const REDUCE = if (@typeInfo(T) == .Float)
|
|
26
|
-
std.math.pow(T, 2, std.math.floatMantissaBits(T) - 10)
|
|
27
|
-
else
|
|
28
|
-
0;
|
|
29
|
-
|
|
30
46
|
/// Possible error returned by operations on the Rational.
|
|
31
|
-
pub const Error =
|
|
32
|
-
.Int => error{Overflow},
|
|
33
|
-
.Float => error{},
|
|
34
|
-
else => unreachable,
|
|
35
|
-
};
|
|
47
|
+
pub const Error = Err;
|
|
36
48
|
|
|
37
49
|
/// Resets the rational back to 1.
|
|
38
50
|
pub fn reset(r: *Self) void {
|
|
@@ -43,135 +55,69 @@ pub fn Rational(comptime T: type) type {
|
|
|
43
55
|
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
44
56
|
/// Both p and q must be >= 1, and if computable at comptime must have no common factors.
|
|
45
57
|
pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
|
|
46
|
-
|
|
47
|
-
assert(p >= 1);
|
|
48
|
-
assert(q >= 1);
|
|
49
|
-
|
|
50
|
-
// If our parameters are not fully reduced they may prematurely
|
|
51
|
-
// cause overflow/loss of precision after the multiplication below
|
|
52
|
-
assert(switch (@typeInfo(@TypeOf(p, q))) {
|
|
53
|
-
.ComptimeInt, .ComptimeFloat => comptime gcd(p, q),
|
|
54
|
-
else => 1,
|
|
55
|
-
} == 1);
|
|
56
|
-
|
|
57
|
-
switch (@typeInfo(T)) {
|
|
58
|
-
.Int => {
|
|
59
|
-
// Greedily attempt to multiply and if it fails, reduce and try again
|
|
60
|
-
r.multiplication(p, q) catch |err| switch (err) {
|
|
61
|
-
error.Overflow => {
|
|
62
|
-
r.reduce();
|
|
63
|
-
try r.multiplication(p, q);
|
|
64
|
-
},
|
|
65
|
-
else => unreachable,
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
.Float => {
|
|
69
|
-
// Reduce in situations where we're likely to start losing precision
|
|
70
|
-
if (r.q > REDUCE or r.p > REDUCE) r.reduce();
|
|
71
|
-
|
|
72
|
-
r.p *= switch (@typeInfo(@TypeOf(p))) {
|
|
73
|
-
.Float, .ComptimeFloat => p,
|
|
74
|
-
else => @floatFromInt(p),
|
|
75
|
-
};
|
|
76
|
-
r.q *= switch (@typeInfo(@TypeOf(q))) {
|
|
77
|
-
.Float, .ComptimeFloat => q,
|
|
78
|
-
else => @floatFromInt(q),
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
// We should always be dealing with whole numbers
|
|
82
|
-
assert(std.math.modf(r.p).fpart == 0);
|
|
83
|
-
assert(std.math.modf(r.q).fpart == 0);
|
|
84
|
-
},
|
|
85
|
-
else => unreachable,
|
|
86
|
-
}
|
|
58
|
+
return update_(T, o, r, p, q);
|
|
87
59
|
}
|
|
88
60
|
|
|
89
61
|
/// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
|
|
90
|
-
pub fn add(r: *Self, s:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
if (r.q == s.q) {
|
|
94
|
-
r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
|
|
95
|
-
error.Overflow => val: {
|
|
96
|
-
r.reduce();
|
|
97
|
-
s.reduce();
|
|
98
|
-
break :val try std.math.add(T, r.p, s.p);
|
|
99
|
-
},
|
|
100
|
-
else => unreachable,
|
|
101
|
-
};
|
|
102
|
-
} else {
|
|
103
|
-
r.addition(s.p, s.q) catch |err| switch (err) {
|
|
104
|
-
error.Overflow => {
|
|
105
|
-
r.reduce();
|
|
106
|
-
s.reduce();
|
|
107
|
-
try r.addition(s.p, s.q);
|
|
108
|
-
},
|
|
109
|
-
else => unreachable,
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
},
|
|
113
|
-
.Float => {
|
|
114
|
-
if (r.q == s.q) {
|
|
115
|
-
if (r.p > REDUCE) r.reduce();
|
|
116
|
-
if (s.p > REDUCE) s.reduce();
|
|
117
|
-
|
|
118
|
-
r.p += s.p;
|
|
119
|
-
} else {
|
|
120
|
-
// Always reduce to minimize loss of precision from the multiplications
|
|
121
|
-
r.reduce();
|
|
122
|
-
s.reduce();
|
|
62
|
+
pub fn add(r: *Self, s: anytype) Error!void {
|
|
63
|
+
return add_(T, o, r, s);
|
|
64
|
+
}
|
|
123
65
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
66
|
+
/// Multiplies two rationals.
|
|
67
|
+
pub fn mul(r: *Self, s: anytype) Error!void {
|
|
68
|
+
return mul_(T, o, r, s);
|
|
69
|
+
}
|
|
127
70
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
else => unreachable,
|
|
132
|
-
}
|
|
71
|
+
/// Normalize the rational by reducing by the greatest common divisor.
|
|
72
|
+
pub fn reduce(r: *Self) void {
|
|
73
|
+
reduce_(r);
|
|
133
74
|
}
|
|
134
75
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
};
|
|
147
|
-
},
|
|
148
|
-
.Float => {
|
|
149
|
-
if (r.q > REDUCE or r.p > REDUCE) r.reduce();
|
|
150
|
-
if (s.q > REDUCE or s.p > REDUCE) s.reduce();
|
|
76
|
+
pub fn format(
|
|
77
|
+
self: Self,
|
|
78
|
+
comptime fmt: []const u8,
|
|
79
|
+
opts: std.fmt.FormatOptions,
|
|
80
|
+
writer: anytype,
|
|
81
|
+
) !void {
|
|
82
|
+
_ = .{ fmt, opts };
|
|
83
|
+
try writer.print("{d}/{d}", .{ self.p, self.q });
|
|
84
|
+
}
|
|
85
|
+
} else struct {
|
|
86
|
+
const Self = @This();
|
|
151
87
|
|
|
152
|
-
|
|
153
|
-
|
|
88
|
+
/// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
89
|
+
p: T = 1,
|
|
90
|
+
/// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
91
|
+
q: T = 1,
|
|
154
92
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
93
|
+
/// Possible error returned by operations on the Rational.
|
|
94
|
+
pub const Error = Err;
|
|
95
|
+
|
|
96
|
+
/// Resets the rational back to 1.
|
|
97
|
+
pub fn reset(r: *Self) void {
|
|
98
|
+
r.p = 1;
|
|
99
|
+
r.q = 1;
|
|
160
100
|
}
|
|
161
101
|
|
|
162
|
-
///
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
102
|
+
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
103
|
+
/// Both p and q must be >= 1, and if computable at comptime must have no common factors.
|
|
104
|
+
pub fn update(r: *Self, p: anytype, q: anytype) Error!void {
|
|
105
|
+
return update_(T, o, r, p, q);
|
|
106
|
+
}
|
|
166
107
|
|
|
167
|
-
|
|
168
|
-
|
|
108
|
+
/// Add two rationals using the identity (a/b) + (c/d) = (ad+bc)/(bd).
|
|
109
|
+
pub fn add(r: *Self, s: anytype) Error!void {
|
|
110
|
+
return add_(T, o, r, s);
|
|
111
|
+
}
|
|
169
112
|
|
|
170
|
-
|
|
171
|
-
|
|
113
|
+
/// Multiplies two rationals.
|
|
114
|
+
pub fn mul(r: *Self, s: anytype) Error!void {
|
|
115
|
+
return mul_(T, o, r, s);
|
|
116
|
+
}
|
|
172
117
|
|
|
173
|
-
|
|
174
|
-
|
|
118
|
+
/// Normalize the rational by reducing by the greatest common divisor.
|
|
119
|
+
pub fn reduce(r: *Self) void {
|
|
120
|
+
reduce_(r);
|
|
175
121
|
}
|
|
176
122
|
|
|
177
123
|
pub fn format(
|
|
@@ -183,41 +129,221 @@ pub fn Rational(comptime T: type) type {
|
|
|
183
129
|
_ = .{ fmt, opts };
|
|
184
130
|
try writer.print("{d}/{d}", .{ self.p, self.q });
|
|
185
131
|
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
186
134
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
135
|
+
fn update_(comptime T: type, comptime o: comptime_int, r: anytype, p: anytype, q: anytype) !void {
|
|
136
|
+
// std.debug.print("({d}/{d}) * ", .{ p, q }); // DEBUG
|
|
137
|
+
assert(p >= 1);
|
|
138
|
+
assert(q >= 1);
|
|
191
139
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
140
|
+
// If our parameters are not fully reduced they may prematurely
|
|
141
|
+
// cause overflow/loss of precision after the multiplication below
|
|
142
|
+
assert(switch (@typeInfo(@TypeOf(p, q))) {
|
|
143
|
+
ComptimeInt, ComptimeFloat => comptime gcd(p, q),
|
|
144
|
+
else => 1,
|
|
145
|
+
} == 1);
|
|
146
|
+
|
|
147
|
+
switch (@typeInfo(T)) {
|
|
148
|
+
Int => {
|
|
149
|
+
// Greedily attempt to multiply and if it fails, reduce and try again
|
|
150
|
+
multiplication(T, r, p, q) catch |err| switch (err) {
|
|
151
|
+
error.Overflow => {
|
|
152
|
+
r.reduce();
|
|
153
|
+
try multiplication(T, r, p, q);
|
|
154
|
+
},
|
|
155
|
+
else => unreachable,
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
Float => {
|
|
159
|
+
// Reduce in situations where we're likely to start losing precision
|
|
160
|
+
if (r.q > o or r.p > o) r.reduce();
|
|
161
|
+
|
|
162
|
+
r.p *= switch (@typeInfo(@TypeOf(p))) {
|
|
163
|
+
Float, ComptimeFloat => p,
|
|
164
|
+
else => @floatFromInt(p),
|
|
165
|
+
};
|
|
166
|
+
r.q *= switch (@typeInfo(@TypeOf(q))) {
|
|
167
|
+
Float, ComptimeFloat => q,
|
|
168
|
+
else => @floatFromInt(q),
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
// We should always be dealing with whole numbers
|
|
172
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
173
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
174
|
+
},
|
|
175
|
+
else => unreachable,
|
|
176
|
+
}
|
|
177
|
+
}
|
|
197
178
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
179
|
+
fn add_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
|
|
180
|
+
switch (@typeInfo(T)) {
|
|
181
|
+
Int => {
|
|
182
|
+
if (r.q == s.q) {
|
|
183
|
+
r.p = std.math.add(T, r.p, s.p) catch |err| switch (err) {
|
|
184
|
+
error.Overflow => val: {
|
|
185
|
+
r.reduce();
|
|
186
|
+
s.reduce();
|
|
187
|
+
break :val try std.math.add(T, r.p, s.p);
|
|
188
|
+
},
|
|
189
|
+
else => unreachable,
|
|
190
|
+
};
|
|
191
|
+
} else {
|
|
192
|
+
addition(T, r, s.p, s.q) catch |err| switch (err) {
|
|
193
|
+
error.Overflow => {
|
|
194
|
+
r.reduce();
|
|
195
|
+
s.reduce();
|
|
196
|
+
try addition(T, r, s.p, s.q);
|
|
197
|
+
},
|
|
198
|
+
else => unreachable,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
},
|
|
202
|
+
Float => {
|
|
203
|
+
if (r.q == s.q) {
|
|
204
|
+
if (r.p > o) r.reduce();
|
|
205
|
+
if (s.p > o) s.reduce();
|
|
206
|
+
|
|
207
|
+
r.p += s.p;
|
|
208
|
+
} else {
|
|
209
|
+
// Always reduce to minimize loss of precision from the multiplications
|
|
210
|
+
r.reduce();
|
|
211
|
+
s.reduce();
|
|
212
|
+
|
|
213
|
+
r.p = (r.p * s.q) + (r.q * s.p);
|
|
214
|
+
r.q *= s.q;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
218
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
219
|
+
},
|
|
220
|
+
else => unreachable,
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
fn mul_(comptime T: type, comptime o: comptime_int, r: anytype, s: anytype) !void {
|
|
225
|
+
switch (@typeInfo(T)) {
|
|
226
|
+
Int => {
|
|
227
|
+
multiplication(T, r, s.p, s.q) catch |err| switch (err) {
|
|
228
|
+
error.Overflow => {
|
|
229
|
+
r.reduce();
|
|
230
|
+
s.reduce();
|
|
231
|
+
try multiplication(T, r, s.p, s.q);
|
|
232
|
+
},
|
|
233
|
+
else => unreachable,
|
|
234
|
+
};
|
|
235
|
+
},
|
|
236
|
+
Float => {
|
|
237
|
+
if (r.q > o or r.p > o) r.reduce();
|
|
238
|
+
if (s.q > o or s.p > o) s.reduce();
|
|
239
|
+
|
|
240
|
+
r.p *= s.p;
|
|
241
|
+
r.q *= s.q;
|
|
242
|
+
|
|
243
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
244
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
245
|
+
},
|
|
246
|
+
else => unreachable,
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
fn reduce_(r: anytype) void {
|
|
251
|
+
const d = gcd(r.p, r.q);
|
|
252
|
+
if (d == 1) return;
|
|
253
|
+
|
|
254
|
+
assert(@mod(r.p, d) == 0);
|
|
255
|
+
assert(@mod(r.q, d) == 0);
|
|
256
|
+
|
|
257
|
+
r.p /= d;
|
|
258
|
+
r.q /= d;
|
|
259
|
+
|
|
260
|
+
assert(r.p >= 1);
|
|
261
|
+
assert(r.q >= 1);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
fn multiplication(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
|
|
265
|
+
r.p = try std.math.mul(T, r.p, p);
|
|
266
|
+
r.q = try std.math.mul(T, r.q, q);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
fn addition(comptime T: type, r: anytype, p: anytype, q: anytype) !void {
|
|
270
|
+
// (a/b) + (c/d) = (ad+bc)/(bd)
|
|
271
|
+
const d = try std.math.mul(T, r.q, q);
|
|
272
|
+
const n1 = try std.math.mul(T, r.p, q);
|
|
273
|
+
const n2 = try std.math.mul(T, r.q, p);
|
|
274
|
+
|
|
275
|
+
r.p = try std.math.add(T, n1, n2);
|
|
276
|
+
r.q = d;
|
|
202
277
|
}
|
|
203
278
|
|
|
204
|
-
// https://en.wikipedia.org/wiki/Euclidean_algorithm
|
|
205
279
|
fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
|
|
206
280
|
assert(p >= 1);
|
|
207
281
|
assert(q >= 1);
|
|
208
282
|
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
283
|
+
// convert comptime_int to a sized integer within this function so that @ctz will work
|
|
284
|
+
const T = switch (@TypeOf(p, q)) {
|
|
285
|
+
comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
|
|
286
|
+
else => |U| U,
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
switch (@typeInfo(T)) {
|
|
290
|
+
Int => {
|
|
291
|
+
// std.math.gcd but without some checks because we have a stricter range
|
|
292
|
+
var x: T = @intCast(p);
|
|
293
|
+
var y: T = @intCast(q);
|
|
294
|
+
|
|
295
|
+
const xz = @ctz(x);
|
|
296
|
+
const yz = @ctz(y);
|
|
297
|
+
const shift = @min(xz, yz);
|
|
298
|
+
x >>= @intCast(xz);
|
|
299
|
+
y >>= @intCast(yz);
|
|
300
|
+
|
|
301
|
+
var diff = y -% x;
|
|
302
|
+
while (diff != 0) : (diff = y -% x) {
|
|
303
|
+
const zeros = @ctz(diff);
|
|
304
|
+
if (x > y) diff = -%diff;
|
|
305
|
+
y = @min(x, y);
|
|
306
|
+
x = diff >> @intCast(zeros);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const result = y << @intCast(shift);
|
|
310
|
+
assert(result > 0);
|
|
311
|
+
return result;
|
|
312
|
+
},
|
|
313
|
+
else => {
|
|
314
|
+
var a = p;
|
|
315
|
+
var b = q;
|
|
316
|
+
var c: T = undefined;
|
|
317
|
+
|
|
318
|
+
while (b != 0) {
|
|
319
|
+
c = b;
|
|
320
|
+
b = @mod(a, b);
|
|
321
|
+
a = c;
|
|
322
|
+
}
|
|
212
323
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
a = c;
|
|
324
|
+
assert(a > 0);
|
|
325
|
+
return a;
|
|
326
|
+
},
|
|
217
327
|
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
test gcd {
|
|
331
|
+
const seed = if (@hasDecl(std.testing, "random_seed")) std.testing.random_seed else 0x12345678;
|
|
332
|
+
var prng = (if (@hasDecl(std, "Random")) std.Random else std.rand).DefaultPrng.init(seed);
|
|
333
|
+
var random = prng.random();
|
|
218
334
|
|
|
219
|
-
|
|
220
|
-
|
|
335
|
+
for (0..1000) |_| {
|
|
336
|
+
const a = random.int(u32);
|
|
337
|
+
const b = random.int(u32);
|
|
338
|
+
if (a == 0 or b == 0) continue;
|
|
339
|
+
|
|
340
|
+
try expectEqual(
|
|
341
|
+
gcd(a, b),
|
|
342
|
+
@as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
|
|
221
347
|
}
|
|
222
348
|
|
|
223
349
|
fn doTurn(r: anytype) !void {
|
|
@@ -229,7 +355,7 @@ fn doTurn(r: anytype) !void {
|
|
|
229
355
|
}
|
|
230
356
|
|
|
231
357
|
test Rational {
|
|
232
|
-
inline for (.{ u64, u128, f64 }) |t| {
|
|
358
|
+
inline for (.{ u64, u128, u256, f64 }) |t| {
|
|
233
359
|
var r: Rational(t) = .{};
|
|
234
360
|
|
|
235
361
|
var c: t = 128;
|
|
@@ -302,8 +428,15 @@ pub const BigRational = struct {
|
|
|
302
428
|
}
|
|
303
429
|
|
|
304
430
|
/// Adds two rationals.
|
|
305
|
-
pub fn add(r: *BigRational, s:
|
|
306
|
-
|
|
431
|
+
pub fn add(r: *BigRational, s: anytype) !void {
|
|
432
|
+
if (@hasField(@TypeOf(s.*), "val")) {
|
|
433
|
+
try r.val.add(r.val, s.val);
|
|
434
|
+
} else {
|
|
435
|
+
var t = try std.math.big.Rational.init(r.val.p.allocator);
|
|
436
|
+
defer t.deinit();
|
|
437
|
+
try t.setRatio(s.p, s.q);
|
|
438
|
+
try r.val.add(r.val, t);
|
|
439
|
+
}
|
|
307
440
|
}
|
|
308
441
|
|
|
309
442
|
/// Multiplies two rationals.
|
|
@@ -354,3 +487,36 @@ test BigRational {
|
|
|
354
487
|
try s.setRatio(71, 78);
|
|
355
488
|
try expect((try s.order(r.val)) == .eq);
|
|
356
489
|
}
|
|
490
|
+
|
|
491
|
+
test "minimum" {
|
|
492
|
+
// TODO: ziglang/zig#21069
|
|
493
|
+
if (builtin.mode == .ReleaseFast or builtin.mode == .ReleaseSmall) {
|
|
494
|
+
return error.SkipZigTest;
|
|
495
|
+
}
|
|
496
|
+
var r = try BigRational.init(std.testing.allocator);
|
|
497
|
+
defer r.deinit();
|
|
498
|
+
var s = try std.math.big.Rational.init(std.testing.allocator);
|
|
499
|
+
defer s.deinit();
|
|
500
|
+
|
|
501
|
+
try r.reset();
|
|
502
|
+
|
|
503
|
+
try r.update(1, 2); // Speed Tie
|
|
504
|
+
for (0..2) |_| {
|
|
505
|
+
try r.update(1, 2); // Confusion
|
|
506
|
+
try r.update(1, 2); // Infatuation
|
|
507
|
+
try r.update(3, 4); // Paralysis
|
|
508
|
+
// Population Bomb
|
|
509
|
+
for (0..10) |_| {
|
|
510
|
+
try r.update(9, 10); // Accuracy
|
|
511
|
+
try r.update(1, 15); // Damage roll
|
|
512
|
+
try r.update(1, 24); // Critical Hit
|
|
513
|
+
try r.update(9, 10); // King's Rock non-proc
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
const ZEROS = std.math.pow(u310, 10, 31);
|
|
518
|
+
const n = 36472996377170786403;
|
|
519
|
+
const d: u310 = 146267071761884981524886149560653378220687632500762654519853056 * ZEROS;
|
|
520
|
+
try s.setRatio(n, d);
|
|
521
|
+
try expect((try s.order(r.val)) == .eq);
|
|
522
|
+
}
|
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 {
|
|
@@ -74,7 +72,7 @@ pub const PSRNG = extern struct {
|
|
|
74
72
|
|
|
75
73
|
test PSRNG {
|
|
76
74
|
var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
|
|
77
|
-
try expectEqual(121, psrng.range(u8, 0, 256));
|
|
75
|
+
try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
|
|
78
76
|
try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
|
|
79
77
|
}
|
|
80
78
|
|
|
@@ -111,9 +109,9 @@ test Gen12 {
|
|
|
111
109
|
try expectEqual(e, rng.next());
|
|
112
110
|
}
|
|
113
111
|
|
|
114
|
-
try expectEqual(16, Gen12.percent(6) + 1);
|
|
115
|
-
try expectEqual(16, Gen12.percent(7) - 1);
|
|
116
|
-
try expectEqual(128, Gen12.percent(50) + 1);
|
|
112
|
+
try expectEqual(@as(u8, 16), Gen12.percent(6) + 1);
|
|
113
|
+
try expectEqual(@as(u8, 16), Gen12.percent(7) - 1);
|
|
114
|
+
try expectEqual(@as(u8, 128), Gen12.percent(50) + 1);
|
|
117
115
|
}
|
|
118
116
|
|
|
119
117
|
// https://pkmn.cc/pokeemerald/src/random.c
|
package/src/lib/common/util.zig
CHANGED
|
@@ -1,12 +1,71 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
2
|
|
|
3
|
+
const expect = std.testing.expect;
|
|
3
4
|
const expectEqual = std.testing.expectEqual;
|
|
4
5
|
|
|
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;
|
|
8
|
+
|
|
5
9
|
pub fn PointerType(comptime P: type, comptime C: type) type {
|
|
6
|
-
return if (@typeInfo(P)
|
|
10
|
+
return if (@field(@typeInfo(P), @tagName(Pointer)).is_const) *const C else *C;
|
|
7
11
|
}
|
|
8
12
|
|
|
9
13
|
test PointerType {
|
|
10
14
|
try expectEqual(*bool, PointerType(*u8, bool));
|
|
11
15
|
try expectEqual(*const f64, PointerType(*const i32, f64));
|
|
12
16
|
}
|
|
17
|
+
|
|
18
|
+
pub fn isPointerTo(p: anytype, comptime P: type) bool {
|
|
19
|
+
const info = @typeInfo(@TypeOf(p));
|
|
20
|
+
return switch (info) {
|
|
21
|
+
Pointer => @field(info, @tagName(Pointer)).child == P,
|
|
22
|
+
else => false,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
test isPointerTo {
|
|
27
|
+
const S = struct {};
|
|
28
|
+
const s: S = .{};
|
|
29
|
+
try expect(!isPointerTo(s, S));
|
|
30
|
+
try expect(isPointerTo(&s, S));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// NOTE: std.mem.bytesAsValue backported from ziglang/zig#18061
|
|
34
|
+
pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) {
|
|
35
|
+
return @as(BytesAsValueReturnType(T, @TypeOf(bytes)), @ptrCast(bytes));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
|
|
39
|
+
return CopyPtrAttrs(B, One, T);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fn CopyPtrAttrs(
|
|
43
|
+
comptime source: type,
|
|
44
|
+
comptime size: std.builtin.Type.Pointer.Size,
|
|
45
|
+
comptime child: type,
|
|
46
|
+
) type {
|
|
47
|
+
const info = @field(@typeInfo(source), @tagName(Pointer));
|
|
48
|
+
const args: std.builtin.Type.Pointer = if (@hasField(std.builtin.Type.Pointer, "sentinel")) .{
|
|
49
|
+
.size = size,
|
|
50
|
+
.is_const = info.is_const,
|
|
51
|
+
.is_volatile = info.is_volatile,
|
|
52
|
+
.is_allowzero = info.is_allowzero,
|
|
53
|
+
.alignment = info.alignment,
|
|
54
|
+
.address_space = info.address_space,
|
|
55
|
+
.child = child,
|
|
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,
|
|
66
|
+
};
|
|
67
|
+
return @Type(if (@hasField(std.builtin.Type, "pointer"))
|
|
68
|
+
.{ .pointer = args }
|
|
69
|
+
else
|
|
70
|
+
.{ .Pointer = args });
|
|
71
|
+
}
|