@pkmn/engine 0.1.0-dev.d716e940 → 0.1.0-dev.d952b7c9
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 -3
- package/README.md +97 -59
- package/build/pkg/addon/browser.d.ts +3 -0
- package/build/pkg/addon/browser.js +43 -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 +96 -0
- package/build/pkg/addon/node.js.map +1 -0
- package/build/pkg/addon.d.ts +2 -0
- package/build/pkg/addon.js +35 -57
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +26 -4
- package/build/pkg/data/protocol.json +69 -12
- package/build/pkg/data.d.ts +27 -5
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.js +16 -19
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +24 -10
- package/build/pkg/index.js +15 -10
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +20 -1
- package/build/pkg/protocol.js +123 -102
- package/build/pkg/protocol.js.map +1 -1
- package/build/tools/debug.js +36 -68
- package/build/tools/debug.js.map +1 -1
- package/build/tools/display/debug.jsx +128 -0
- package/build/tools/display/debug.jsx.map +1 -0
- package/build/tools/display/index.js +132 -0
- package/build/tools/display/index.js.map +1 -0
- package/build/tools/display/ui.jsx +349 -0
- package/build/tools/display/ui.jsx.map +1 -0
- package/build/tools/display/util.js +18 -0
- package/build/tools/display/util.js.map +1 -0
- package/build.zig +286 -131
- package/package.json +73 -31
- package/src/bin/install-pkmn-engine +39 -24
- package/src/bin/pkmn-debug +1 -1
- package/src/lib/common/array.zig +77 -0
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +19 -31
- package/src/lib/common/debug.zig +30 -16
- package/src/lib/common/js.zig +120 -0
- package/src/lib/common/optional.zig +71 -0
- package/src/lib/common/options.zig +13 -0
- package/src/lib/common/protocol.zig +911 -367
- package/src/lib/common/rational.zig +447 -0
- package/src/lib/common/rng.zig +31 -22
- package/src/lib/common/util.zig +61 -0
- package/src/lib/gen1/calc.zig +950 -0
- package/src/lib/gen1/chance.zig +1169 -0
- package/src/lib/gen1/data/moves.zig +202 -190
- package/src/lib/gen1/data/species.zig +5 -5
- package/src/lib/gen1/data/types.zig +10 -10
- package/src/lib/gen1/data.zig +92 -79
- package/src/lib/gen1/helpers.zig +141 -28
- package/src/lib/gen1/mechanics.zig +1118 -773
- package/src/lib/gen2/calc.zig +161 -0
- package/src/lib/gen2/chance.zig +902 -0
- package/src/lib/gen2/data/items.zig +14 -20
- package/src/lib/gen2/data/moves.zig +664 -570
- package/src/lib/gen2/data/species.zig +2 -2
- package/src/lib/gen2/data/types.zig +137 -34
- package/src/lib/gen2/data.zig +125 -86
- package/src/lib/gen2/generated.zig +806 -0
- package/src/lib/gen2/helpers.zig +54 -32
- package/src/lib/gen2/mechanics.zig +3564 -15
- package/src/lib/node.zig +131 -0
- package/src/lib/pkmn.zig +81 -5
- package/src/lib/wasm.zig +13 -0
- package/src/tools/display/dom.ts +96 -0
- package/src/tools/display/ui.css +351 -0
- package/build/test/display.js +0 -400
- package/build/test/display.js.map +0 -1
- package/src/lib/binding/node.zig +0 -229
- package/src/lib/binding/wasm.zig +0 -10
- package/src/lib/gen1/README.md +0 -812
- package/src/lib/gen2/README.md +0 -309
- package/src/test/display/pkmn.html.tmpl +0 -368
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
const builtin = @import("builtin");
|
|
2
|
+
const std = @import("std");
|
|
3
|
+
|
|
4
|
+
const assert = std.debug.assert;
|
|
5
|
+
const expect = std.testing.expect;
|
|
6
|
+
const expectEqual = std.testing.expectEqual;
|
|
7
|
+
const expectError = std.testing.expectError;
|
|
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
|
+
/// Specialization of a rational number used by the engine to compute probabilties.
|
|
17
|
+
/// For performance reasons the rational is only reduced lazily and thus `reduce` must be
|
|
18
|
+
/// invoked explicitly before reading.
|
|
19
|
+
pub fn Rational(comptime T: type) type {
|
|
20
|
+
return extern struct {
|
|
21
|
+
const Self = @This();
|
|
22
|
+
|
|
23
|
+
/// Numerator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
24
|
+
p: T = 1,
|
|
25
|
+
/// Denominator. Must always be >= 1. Not guaranteed to be reduced in all cases.
|
|
26
|
+
q: T = 1,
|
|
27
|
+
|
|
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;
|
|
36
|
+
|
|
37
|
+
/// 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
|
+
};
|
|
43
|
+
|
|
44
|
+
/// Resets the rational back to 1.
|
|
45
|
+
pub fn reset(r: *Self) void {
|
|
46
|
+
r.p = 1;
|
|
47
|
+
r.q = 1;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// Update the rational by multiplying its numerator by p and its denominator by q.
|
|
51
|
+
/// Both p and q must be >= 1, and if computable at comptime must have no common factors.
|
|
52
|
+
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
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/// 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
|
+
}
|
|
119
|
+
},
|
|
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
|
|
128
|
+
r.reduce();
|
|
129
|
+
s.reduce();
|
|
130
|
+
|
|
131
|
+
r.p = (r.p * s.q) + (r.q * s.p);
|
|
132
|
+
r.q *= s.q;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
136
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
137
|
+
},
|
|
138
|
+
else => unreachable,
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
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();
|
|
158
|
+
|
|
159
|
+
r.p *= s.p;
|
|
160
|
+
r.q *= s.q;
|
|
161
|
+
|
|
162
|
+
assert(std.math.modf(r.p).fpart == 0);
|
|
163
|
+
assert(std.math.modf(r.q).fpart == 0);
|
|
164
|
+
},
|
|
165
|
+
else => unreachable,
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
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;
|
|
173
|
+
|
|
174
|
+
assert(@mod(r.p, d) == 0);
|
|
175
|
+
assert(@mod(r.q, d) == 0);
|
|
176
|
+
|
|
177
|
+
r.p /= d;
|
|
178
|
+
r.q /= d;
|
|
179
|
+
|
|
180
|
+
assert(r.p >= 1);
|
|
181
|
+
assert(r.q >= 1);
|
|
182
|
+
}
|
|
183
|
+
|
|
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
|
+
}
|
|
193
|
+
|
|
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
|
+
}
|
|
198
|
+
|
|
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);
|
|
204
|
+
|
|
205
|
+
r.p = try std.math.add(T, n1, n2);
|
|
206
|
+
r.q = d;
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
fn gcd(p: anytype, q: anytype) @TypeOf(p, q) {
|
|
212
|
+
assert(p >= 1);
|
|
213
|
+
assert(q >= 1);
|
|
214
|
+
|
|
215
|
+
// convert comptime_int to a sized integer within this function so that @ctz will work
|
|
216
|
+
const T = switch (@TypeOf(p, q)) {
|
|
217
|
+
comptime_int => std.math.IntFittingRange(@min(p, q), @max(p, q)),
|
|
218
|
+
else => |U| U,
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
switch (@typeInfo(T)) {
|
|
222
|
+
Int => {
|
|
223
|
+
// std.math.gcd but without some checks because we have a stricter range
|
|
224
|
+
var x: T = @intCast(p);
|
|
225
|
+
var y: T = @intCast(q);
|
|
226
|
+
|
|
227
|
+
const xz = @ctz(x);
|
|
228
|
+
const yz = @ctz(y);
|
|
229
|
+
const shift = @min(xz, yz);
|
|
230
|
+
x >>= @intCast(xz);
|
|
231
|
+
y >>= @intCast(yz);
|
|
232
|
+
|
|
233
|
+
var diff = y -% x;
|
|
234
|
+
while (diff != 0) : (diff = y -% x) {
|
|
235
|
+
const zeros = @ctz(diff);
|
|
236
|
+
if (x > y) diff = -%diff;
|
|
237
|
+
y = @min(x, y);
|
|
238
|
+
x = diff >> @intCast(zeros);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const result = y << @intCast(shift);
|
|
242
|
+
assert(result > 0);
|
|
243
|
+
return result;
|
|
244
|
+
},
|
|
245
|
+
else => {
|
|
246
|
+
var a = p;
|
|
247
|
+
var b = q;
|
|
248
|
+
var c: T = undefined;
|
|
249
|
+
|
|
250
|
+
while (b != 0) {
|
|
251
|
+
c = b;
|
|
252
|
+
b = @mod(a, b);
|
|
253
|
+
a = c;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
assert(a > 0);
|
|
257
|
+
return a;
|
|
258
|
+
},
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
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);
|
|
265
|
+
var random = prng.random();
|
|
266
|
+
|
|
267
|
+
for (0..1000) |_| {
|
|
268
|
+
const a = random.int(u32);
|
|
269
|
+
const b = random.int(u32);
|
|
270
|
+
if (a == 0 or b == 0) continue;
|
|
271
|
+
|
|
272
|
+
try expectEqual(
|
|
273
|
+
gcd(a, b),
|
|
274
|
+
@as(u32, @intFromFloat(gcd(@as(f64, @floatFromInt(a)), @as(f64, @floatFromInt(b))))),
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
try expectEqual(gcd(300_000, @as(u32, 2_300_000)), 100_000); // NB: @ctz requires an @intCast
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
fn doTurn(r: anytype) !void {
|
|
282
|
+
try r.update(1, 163); // Metronome
|
|
283
|
+
try r.update(33, 256); // Critical Hit (67 Speed)
|
|
284
|
+
try r.update(1, 39); // Damage roll
|
|
285
|
+
try r.update(89, 128); // Thunder accuracy (178/256)
|
|
286
|
+
try r.update(77, 256); // Thunder secondary proc
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
test Rational {
|
|
290
|
+
inline for (.{ u64, u128, f64 }) |t| {
|
|
291
|
+
var r: Rational(t) = .{};
|
|
292
|
+
|
|
293
|
+
var c: t = 128;
|
|
294
|
+
_ = &c;
|
|
295
|
+
|
|
296
|
+
try r.update(c, 256);
|
|
297
|
+
try doTurn(&r);
|
|
298
|
+
|
|
299
|
+
r.reduce();
|
|
300
|
+
try expectEqual(Rational(t){ .p = 75383, .q = 35550920704 }, r);
|
|
301
|
+
|
|
302
|
+
try r.update(1, 4);
|
|
303
|
+
if (t == u64) {
|
|
304
|
+
try expectError(error.Overflow, doTurn(&r));
|
|
305
|
+
} else {
|
|
306
|
+
try doTurn(&r);
|
|
307
|
+
r.reduce();
|
|
308
|
+
try expectEqual(Rational(t){ .p = 5682596689, .q = 2527735925804191711232 }, r);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
r.reset();
|
|
312
|
+
|
|
313
|
+
var s = Rational(t){ .p = 10, .q = 13 };
|
|
314
|
+
try r.mul(&s);
|
|
315
|
+
s = Rational(t){ .p = 3, .q = 4 };
|
|
316
|
+
try r.mul(&s);
|
|
317
|
+
try expectEqual(Rational(t){ .p = 30, .q = 52 }, r);
|
|
318
|
+
|
|
319
|
+
s = Rational(t){ .p = 1, .q = 3 };
|
|
320
|
+
try r.add(&s);
|
|
321
|
+
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
|
+
}
|
|
366
|
+
|
|
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();
|
|
397
|
+
|
|
398
|
+
var t = try BigRational.init(std.testing.allocator);
|
|
399
|
+
defer t.deinit();
|
|
400
|
+
|
|
401
|
+
try t.val.setRatio(10, 13);
|
|
402
|
+
try r.mul(&t);
|
|
403
|
+
try t.val.setRatio(3, 4);
|
|
404
|
+
try r.mul(&t);
|
|
405
|
+
|
|
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
|
|
439
|
+
}
|
|
440
|
+
}
|
|
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
|
+
}
|
package/src/lib/common/rng.zig
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
2
|
|
|
3
|
-
const options = @import("
|
|
3
|
+
const options = @import("options.zig");
|
|
4
4
|
|
|
5
5
|
const assert = std.debug.assert;
|
|
6
6
|
const expectEqual = std.testing.expectEqual;
|
|
@@ -25,6 +25,11 @@ pub fn PRNG(comptime gen: comptime_int) type {
|
|
|
25
25
|
pub fn next(self: *Self) Output(gen) {
|
|
26
26
|
return self.src.next();
|
|
27
27
|
}
|
|
28
|
+
|
|
29
|
+
pub fn advance(self: *Self, n: usize) void {
|
|
30
|
+
if (!options.advance) return;
|
|
31
|
+
for (0..n) |_| self.src.advance();
|
|
32
|
+
}
|
|
28
33
|
};
|
|
29
34
|
}
|
|
30
35
|
|
|
@@ -47,7 +52,7 @@ pub const PSRNG = extern struct {
|
|
|
47
52
|
}
|
|
48
53
|
|
|
49
54
|
pub fn range(self: *PSRNG, comptime T: type, from: T, to: Bound(T)) T {
|
|
50
|
-
return @intCast(
|
|
55
|
+
return @intCast(@as(u64, self.src.next()) * (to - from) / divisor + from);
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
pub fn chance(self: *PSRNG, comptime T: type, numerator: T, denominator: Bound(T)) bool {
|
|
@@ -61,10 +66,14 @@ pub const PSRNG = extern struct {
|
|
|
61
66
|
(@as(u64, self.range(u16, 0, 0x10000)) << 16) |
|
|
62
67
|
(@as(u64, self.range(u16, 0, 0x10000)));
|
|
63
68
|
}
|
|
69
|
+
|
|
70
|
+
pub fn Bound(comptime T: type) type {
|
|
71
|
+
return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
|
|
72
|
+
}
|
|
64
73
|
};
|
|
65
74
|
|
|
66
75
|
test PSRNG {
|
|
67
|
-
var psrng =
|
|
76
|
+
var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
|
|
68
77
|
try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
|
|
69
78
|
try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
|
|
70
79
|
}
|
|
@@ -72,28 +81,32 @@ test PSRNG {
|
|
|
72
81
|
// https://pkmn.cc/pokered/engine/battle/core.asm#L6644-L6693
|
|
73
82
|
// https://pkmn.cc/pokecrystal/engine/battle/core.asm#L6922-L6938
|
|
74
83
|
pub const Gen12 = extern struct {
|
|
75
|
-
seed: [
|
|
84
|
+
seed: [9]u8,
|
|
76
85
|
index: u8 = 0,
|
|
77
86
|
|
|
78
87
|
comptime {
|
|
79
|
-
assert(@sizeOf(Gen12) ==
|
|
88
|
+
assert(@sizeOf(Gen12) == 10);
|
|
80
89
|
}
|
|
81
90
|
|
|
82
|
-
pub fn percent(p:
|
|
83
|
-
return @intCast(
|
|
91
|
+
pub fn percent(comptime p: comptime_int) u8 {
|
|
92
|
+
return @intCast((@as(u16, p) * 0xFF) / 100);
|
|
84
93
|
}
|
|
85
94
|
|
|
86
95
|
pub fn next(self: *Gen12) u8 {
|
|
87
96
|
const val = 5 *% self.seed[self.index] +% 1;
|
|
88
97
|
self.seed[self.index] = val;
|
|
89
|
-
self.index = (self.index + 1) %
|
|
98
|
+
self.index = (self.index + 1) % 9;
|
|
90
99
|
return val;
|
|
91
100
|
}
|
|
101
|
+
|
|
102
|
+
pub fn advance(self: *Gen12) void {
|
|
103
|
+
_ = self.next();
|
|
104
|
+
}
|
|
92
105
|
};
|
|
93
106
|
|
|
94
107
|
test Gen12 {
|
|
95
|
-
const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46,
|
|
96
|
-
var rng =
|
|
108
|
+
const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 31, 56, 81 };
|
|
109
|
+
var rng: Gen12 = .{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
|
|
97
110
|
for (expected) |e| {
|
|
98
111
|
try expectEqual(e, rng.next());
|
|
99
112
|
}
|
|
@@ -114,7 +127,7 @@ pub const Gen34 = extern struct {
|
|
|
114
127
|
|
|
115
128
|
pub fn next(self: *Gen34) u16 {
|
|
116
129
|
self.advance();
|
|
117
|
-
return @intCast(
|
|
130
|
+
return @intCast(self.seed >> 16);
|
|
118
131
|
}
|
|
119
132
|
|
|
120
133
|
pub fn advance(self: *Gen34) void {
|
|
@@ -129,7 +142,7 @@ test Gen34 {
|
|
|
129
142
|
.{ 0x80000000, 5, 0x0E425287 }, .{ 0x80000000, 10, 0x6F2CF4B2 },
|
|
130
143
|
};
|
|
131
144
|
for (data) |d| {
|
|
132
|
-
var rng =
|
|
145
|
+
var rng: Gen34 = .{ .seed = d[0] };
|
|
133
146
|
for (0..d[1]) |_| _ = rng.next();
|
|
134
147
|
try expectEqual(d[2], rng.seed);
|
|
135
148
|
}
|
|
@@ -144,7 +157,7 @@ pub const Gen56 = extern struct {
|
|
|
144
157
|
|
|
145
158
|
pub fn next(self: *Gen56) u32 {
|
|
146
159
|
self.advance();
|
|
147
|
-
return @intCast(
|
|
160
|
+
return @intCast(self.seed >> 32);
|
|
148
161
|
}
|
|
149
162
|
|
|
150
163
|
pub fn advance(self: *Gen56) void {
|
|
@@ -161,7 +174,7 @@ test Gen56 {
|
|
|
161
174
|
.{ 0x8000000000000000, 10, 0xE7795501267F125A },
|
|
162
175
|
};
|
|
163
176
|
for (data) |d| {
|
|
164
|
-
var rng =
|
|
177
|
+
var rng: Gen56 = .{ .seed = d[0] };
|
|
165
178
|
for (0..d[1]) |_| _ = rng.next();
|
|
166
179
|
try expectEqual(d[2], rng.seed);
|
|
167
180
|
}
|
|
@@ -188,16 +201,16 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
|
|
|
188
201
|
for (0..n) |_| _ = self.next();
|
|
189
202
|
}
|
|
190
203
|
|
|
191
|
-
pub fn range(self: *Self, comptime T: type, from: T, to: Bound(T)) T {
|
|
204
|
+
pub fn range(self: *Self, comptime T: type, from: T, to: PSRNG.Bound(T)) T {
|
|
192
205
|
assert(showdown);
|
|
193
|
-
return @intCast(
|
|
206
|
+
return @intCast(@as(u64, self.next()) * (to - from) / divisor + from);
|
|
194
207
|
}
|
|
195
208
|
|
|
196
209
|
pub fn chance(
|
|
197
210
|
self: *Self,
|
|
198
211
|
comptime T: type,
|
|
199
212
|
numerator: T,
|
|
200
|
-
denominator: Bound(T),
|
|
213
|
+
denominator: PSRNG.Bound(T),
|
|
201
214
|
) bool {
|
|
202
215
|
assert(showdown);
|
|
203
216
|
assert(denominator > 0);
|
|
@@ -213,7 +226,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
|
|
|
213
226
|
test FixedRNG {
|
|
214
227
|
const Type = if (showdown) u32 else u8;
|
|
215
228
|
const expected = [_]Type{ 42, 255, 0 };
|
|
216
|
-
var rng
|
|
229
|
+
var rng: FixedRNG(1, expected.len) = .{ .rolls = expected };
|
|
217
230
|
for (expected) |e| {
|
|
218
231
|
try expectEqual(e, rng.next());
|
|
219
232
|
}
|
|
@@ -228,7 +241,3 @@ fn Output(comptime gen: comptime_int) type {
|
|
|
228
241
|
else => unreachable,
|
|
229
242
|
};
|
|
230
243
|
}
|
|
231
|
-
|
|
232
|
-
fn Bound(comptime T: type) type {
|
|
233
|
-
return std.math.IntFittingRange(0, std.math.maxInt(T) + 1);
|
|
234
|
-
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const expect = std.testing.expect;
|
|
4
|
+
const expectEqual = std.testing.expectEqual;
|
|
5
|
+
|
|
6
|
+
const Pointer = if (@hasField(std.builtin.Type, "pointer")) .pointer else .Pointer;
|
|
7
|
+
|
|
8
|
+
pub fn PointerType(comptime P: type, comptime C: type) type {
|
|
9
|
+
return if (@field(@typeInfo(P), @tagName(Pointer)).is_const) *const C else *C;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
test PointerType {
|
|
13
|
+
try expectEqual(*bool, PointerType(*u8, bool));
|
|
14
|
+
try expectEqual(*const f64, PointerType(*const i32, f64));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
pub fn isPointerTo(p: anytype, comptime P: type) bool {
|
|
18
|
+
const info = @typeInfo(@TypeOf(p));
|
|
19
|
+
return switch (info) {
|
|
20
|
+
Pointer => @field(info, @tagName(Pointer)).child == P,
|
|
21
|
+
else => false,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
test isPointerTo {
|
|
26
|
+
const S = struct {};
|
|
27
|
+
const s: S = .{};
|
|
28
|
+
try expect(!isPointerTo(s, S));
|
|
29
|
+
try expect(isPointerTo(&s, S));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// NOTE: std.mem.bytesAsValue backported from ziglang/zig#18061
|
|
33
|
+
pub fn bytesAsValue(comptime T: type, bytes: anytype) BytesAsValueReturnType(T, @TypeOf(bytes)) {
|
|
34
|
+
return @as(BytesAsValueReturnType(T, @TypeOf(bytes)), @ptrCast(bytes));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
fn BytesAsValueReturnType(comptime T: type, comptime B: type) type {
|
|
38
|
+
return CopyPtrAttrs(B, .One, T);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fn CopyPtrAttrs(
|
|
42
|
+
comptime source: type,
|
|
43
|
+
comptime size: std.builtin.Type.Pointer.Size,
|
|
44
|
+
comptime child: type,
|
|
45
|
+
) type {
|
|
46
|
+
const info = @field(@typeInfo(source), @tagName(Pointer));
|
|
47
|
+
const args = .{
|
|
48
|
+
.size = size,
|
|
49
|
+
.is_const = info.is_const,
|
|
50
|
+
.is_volatile = info.is_volatile,
|
|
51
|
+
.is_allowzero = info.is_allowzero,
|
|
52
|
+
.alignment = info.alignment,
|
|
53
|
+
.address_space = info.address_space,
|
|
54
|
+
.child = child,
|
|
55
|
+
.sentinel = null,
|
|
56
|
+
};
|
|
57
|
+
return @Type(if (@hasField(std.builtin.Type, "pointer"))
|
|
58
|
+
.{ .pointer = args }
|
|
59
|
+
else
|
|
60
|
+
.{ .Pointer = args });
|
|
61
|
+
}
|