@pkmn/engine 0.1.0-dev.cf6f5d2c → 0.1.0-dev.d1b39ff0
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 +95 -59
- package/build/pkg/addon.js +9 -9
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +27 -5
- package/build/pkg/data/protocol.json +69 -12
- package/build/pkg/data.d.ts +4 -4
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.d.ts +2 -1
- package/build/pkg/gen1.js +38 -20
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +7 -4
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +4 -2
- package/build/pkg/protocol.js +65 -44
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +15 -6
- package/build/test/display.js.map +1 -1
- package/build/tools/debug.js +5 -4
- package/build/tools/debug.js.map +1 -1
- package/build.zig +148 -126
- package/package.json +53 -24
- package/src/bin/install-pkmn-engine +35 -22
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +19 -31
- package/src/lib/common/debug.zig +22 -13
- package/src/lib/common/js.zig +118 -0
- package/src/lib/common/optional.zig +67 -0
- package/src/lib/common/options.zig +11 -0
- package/src/lib/common/protocol.zig +906 -367
- package/src/lib/common/rational.zig +454 -0
- package/src/lib/common/rng.zig +35 -26
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/calc.zig +802 -0
- package/src/lib/gen1/chance.zig +1159 -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 +104 -90
- package/src/lib/gen1/helpers.zig +140 -42
- package/src/lib/gen1/mechanics.zig +1135 -773
- package/src/lib/gen2/calc.zig +135 -0
- package/src/lib/gen2/chance.zig +895 -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 +130 -92
- package/src/lib/gen2/generated.zig +806 -0
- package/src/lib/gen2/helpers.zig +50 -26
- package/src/lib/gen2/mechanics.zig +3568 -12
- package/src/lib/node.zig +131 -0
- package/src/lib/pkmn.zig +81 -5
- package/src/lib/wasm.zig +13 -0
- package/src/test/display/pkmn.html.tmpl +5 -5
- package/src/lib/binding/node.zig +0 -229
- package/src/lib/binding/wasm.zig +0 -10
- package/src/lib/gen1/README.md +0 -804
- package/src/lib/gen2/README.md +0 -309
|
@@ -0,0 +1,802 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const pkmn = @import("../pkmn.zig");
|
|
4
|
+
|
|
5
|
+
const common = @import("../common/data.zig");
|
|
6
|
+
const DEBUG = @import("../common/debug.zig").print;
|
|
7
|
+
const optional = @import("../common/optional.zig");
|
|
8
|
+
const protocol = @import("../common/protocol.zig");
|
|
9
|
+
const rational = @import("../common/rational.zig");
|
|
10
|
+
const util = @import("../common/util.zig");
|
|
11
|
+
|
|
12
|
+
const chance = @import("chance.zig");
|
|
13
|
+
const data = @import("data.zig");
|
|
14
|
+
const helpers = @import("helpers.zig");
|
|
15
|
+
|
|
16
|
+
const assert = std.debug.assert;
|
|
17
|
+
|
|
18
|
+
const expect = std.testing.expect;
|
|
19
|
+
const expectEqual = std.testing.expectEqual;
|
|
20
|
+
const expectEqualSlices = std.testing.expectEqualSlices;
|
|
21
|
+
|
|
22
|
+
const enabled = pkmn.options.calc;
|
|
23
|
+
|
|
24
|
+
const Player = common.Player;
|
|
25
|
+
const Result = common.Result;
|
|
26
|
+
const Choice = common.Choice;
|
|
27
|
+
|
|
28
|
+
const Optional = optional.Optional;
|
|
29
|
+
|
|
30
|
+
const Rational = rational.Rational;
|
|
31
|
+
|
|
32
|
+
const PointerType = util.PointerType;
|
|
33
|
+
|
|
34
|
+
const Actions = chance.Actions;
|
|
35
|
+
const Action = chance.Action;
|
|
36
|
+
const Chance = chance.Chance;
|
|
37
|
+
const Durations = chance.Durations;
|
|
38
|
+
|
|
39
|
+
const tty = true; // DEBUG
|
|
40
|
+
const summary = false; // DEBUG
|
|
41
|
+
|
|
42
|
+
/// Information relevant to damage calculation that occured during a Generation I battle `update`.
|
|
43
|
+
pub const Summaries = extern struct {
|
|
44
|
+
/// Relevant information for Player 1.
|
|
45
|
+
p1: Summary = .{},
|
|
46
|
+
/// Relevant information for Player 2.
|
|
47
|
+
p2: Summary = .{},
|
|
48
|
+
|
|
49
|
+
comptime {
|
|
50
|
+
assert(@sizeOf(Summaries) == 12);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// Returns the `Summary` for the given `player`.
|
|
54
|
+
pub fn get(self: anytype, player: Player) PointerType(@TypeOf(self), Summary) {
|
|
55
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Summaries);
|
|
56
|
+
return if (player == .P1) &self.p1 else &self.p2;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/// Information relevant to damage calculation that occured during a Generation I battle `update`
|
|
61
|
+
/// for a single player.
|
|
62
|
+
pub const Summary = extern struct {
|
|
63
|
+
/// The computed raw damage values.
|
|
64
|
+
damage: Damage = .{},
|
|
65
|
+
|
|
66
|
+
/// Intermediate raw damage values computed during a calculation.
|
|
67
|
+
pub const Damage = extern struct {
|
|
68
|
+
/// The base computed damage before the damage roll is applied.
|
|
69
|
+
base: u16 = 0,
|
|
70
|
+
/// The final computed damage that gets applied to the Pokémon. May exceed the target's HP
|
|
71
|
+
// (to determine the *actual* damage done compare the target's stored HP before and after).
|
|
72
|
+
final: u16 = 0,
|
|
73
|
+
/// Whether higher direct damage will saturate / result in the same outcome (e.g. additional
|
|
74
|
+
/// direct damage gets ignored due to it already breaking a Substitute or causing the target
|
|
75
|
+
/// to faint). Note that this field does not get set in scenarios where the target would
|
|
76
|
+
/// only be guaranteed to faint due to some sort of subsequent recoil or residual damage.
|
|
77
|
+
capped: bool = false,
|
|
78
|
+
|
|
79
|
+
// NOTE: 15 bits padding
|
|
80
|
+
|
|
81
|
+
comptime {
|
|
82
|
+
assert(@sizeOf(Damage) == 6);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
comptime {
|
|
87
|
+
assert(@sizeOf(Summary) == 6);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
/// Allows for forcing the value of specific RNG events during a Generation I battle `update` via
|
|
92
|
+
/// `overrides` and tracks `summaries` of information relevant to damage calculation.
|
|
93
|
+
pub const Calc = struct {
|
|
94
|
+
/// Overrides the normal behavior of the RNG during an `update` to force specific outcomes.
|
|
95
|
+
overrides: Actions = .{},
|
|
96
|
+
/// Information relevant to damage calculation.
|
|
97
|
+
summaries: Summaries = .{},
|
|
98
|
+
|
|
99
|
+
pub fn overridden(
|
|
100
|
+
self: Calc,
|
|
101
|
+
player: Player,
|
|
102
|
+
comptime field: Action.Field,
|
|
103
|
+
) ?std.meta.FieldType(Action, field) {
|
|
104
|
+
if (!enabled) return null;
|
|
105
|
+
|
|
106
|
+
const val = @field(self.overrides.get(player), @tagName(field));
|
|
107
|
+
return if (switch (@typeInfo(@TypeOf(val))) {
|
|
108
|
+
.Enum => val != .None,
|
|
109
|
+
.Int => val != 0,
|
|
110
|
+
else => unreachable,
|
|
111
|
+
}) val else null;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
pub fn base(self: *Calc, player: Player, val: u16) void {
|
|
115
|
+
if (!enabled) return;
|
|
116
|
+
|
|
117
|
+
self.summaries.get(player).damage.base = val;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
pub fn final(self: *Calc, player: Player, val: u16) void {
|
|
121
|
+
if (!enabled) return;
|
|
122
|
+
|
|
123
|
+
self.summaries.get(player).damage.final = val;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
pub fn capped(self: *Calc, player: Player) void {
|
|
127
|
+
if (!enabled) return;
|
|
128
|
+
|
|
129
|
+
self.summaries.get(player).damage.capped = true;
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
/// Null object pattern implementation of Generation I `Calc` which does nothing, though damage
|
|
134
|
+
/// calculator support should additionally be turned off entirely via `options.calc`.
|
|
135
|
+
pub const NULL: Null = .{};
|
|
136
|
+
|
|
137
|
+
const Null = struct {
|
|
138
|
+
pub fn overridden(
|
|
139
|
+
self: Null,
|
|
140
|
+
player: Player,
|
|
141
|
+
comptime field: Action.Field,
|
|
142
|
+
) ?std.meta.FieldType(Action, field) {
|
|
143
|
+
_ = .{ self, player };
|
|
144
|
+
return null;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
pub fn base(self: Null, player: Player, val: u16) void {
|
|
148
|
+
_ = .{ self, player, val };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
pub fn final(self: Null, player: Player, val: u16) void {
|
|
152
|
+
_ = .{ self, player, val };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
pub fn capped(self: Null, player: Player) void {
|
|
156
|
+
_ = .{ self, player };
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
pub const Stats = struct {
|
|
161
|
+
frontier: usize = 0,
|
|
162
|
+
updates: usize = 0,
|
|
163
|
+
seen: usize = 0,
|
|
164
|
+
saved: usize = 0,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
pub const Options = struct {
|
|
168
|
+
durations: Durations,
|
|
169
|
+
seed: ?u64 = null,
|
|
170
|
+
cap: bool = false,
|
|
171
|
+
metronome: bool = false,
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
pub const MAX_FRONTIER = 19;
|
|
175
|
+
|
|
176
|
+
pub fn transitions(
|
|
177
|
+
battle: anytype,
|
|
178
|
+
c1: Choice,
|
|
179
|
+
c2: Choice,
|
|
180
|
+
allocator: std.mem.Allocator,
|
|
181
|
+
writer: anytype,
|
|
182
|
+
options: Options,
|
|
183
|
+
) !?Stats {
|
|
184
|
+
var stats: Stats = .{};
|
|
185
|
+
|
|
186
|
+
const cap = options.cap;
|
|
187
|
+
|
|
188
|
+
var seen = std.AutoHashMap(Actions, void).init(allocator);
|
|
189
|
+
defer seen.deinit();
|
|
190
|
+
var frontier = std.ArrayList(Actions).init(allocator);
|
|
191
|
+
defer frontier.deinit();
|
|
192
|
+
|
|
193
|
+
var opts = pkmn.battle.options(
|
|
194
|
+
protocol.NULL,
|
|
195
|
+
Chance(Rational(u128)){ .probability = .{}, .durations = options.durations },
|
|
196
|
+
Calc{},
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
var b = battle;
|
|
200
|
+
_ = try b.update(c1, c2, &opts);
|
|
201
|
+
stats.updates += 1;
|
|
202
|
+
|
|
203
|
+
const p1 = b.side(.P1);
|
|
204
|
+
const p2 = b.side(.P2);
|
|
205
|
+
|
|
206
|
+
var p: Rational(u128) = .{ .p = 0, .q = 1 };
|
|
207
|
+
try frontier.append(opts.chance.actions);
|
|
208
|
+
|
|
209
|
+
// zig fmt: off
|
|
210
|
+
for (Rolls.metronome(frontier.items[0].p1)) |p1_move| {
|
|
211
|
+
for (Rolls.metronome(frontier.items[0].p2)) |p2_move| {
|
|
212
|
+
|
|
213
|
+
if (!options.metronome and (p1_move != .None or p2_move != .None)) return null;
|
|
214
|
+
|
|
215
|
+
var i: usize = 0;
|
|
216
|
+
assert(frontier.items.len == 1);
|
|
217
|
+
while (i < frontier.items.len) : (i += 1) {
|
|
218
|
+
const f = frontier.items[i];
|
|
219
|
+
|
|
220
|
+
try debug(writer, f, .{
|
|
221
|
+
.shape = true,
|
|
222
|
+
.color = i,
|
|
223
|
+
.bold = true,
|
|
224
|
+
.background = true,
|
|
225
|
+
.indent = false,
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
var a: Actions = .{
|
|
229
|
+
.p1 = .{ .metronome = p1_move, .pp = f.p1.pp, .duration = f.p1.duration },
|
|
230
|
+
.p2 = .{ .metronome = p2_move, .pp = f.p2.pp, .duration = f.p2.duration },
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
for (Rolls.speedTie(f.p1)) |tie| { a.p1.speed_tie = tie; a.p2.speed_tie = tie;
|
|
234
|
+
for (Rolls.confused(f.p1)) |p1_cfzd| { a.p1.confused = p1_cfzd;
|
|
235
|
+
for (Rolls.confused(f.p2)) |p2_cfzd| { a.p2.confused = p2_cfzd;
|
|
236
|
+
for (Rolls.paralyzed(f.p1, p1_cfzd)) |p1_par| { a.p1.paralyzed = p1_par;
|
|
237
|
+
for (Rolls.paralyzed(f.p2, p2_cfzd)) |p2_par| { a.p2.paralyzed = p2_par;
|
|
238
|
+
for (Rolls.hit(f.p1, p1_par)) |p1_hit| { a.p1.hit = p1_hit;
|
|
239
|
+
for (Rolls.hit(f.p2, p2_par)) |p2_hit| { a.p2.hit = p2_hit;
|
|
240
|
+
for (Rolls.psywave(f.p1, p1, p1_hit)) |p1_psywave| { a.p1.psywave = p1_psywave;
|
|
241
|
+
for (Rolls.psywave(f.p2, p2, p2_hit)) |p2_psywave| { a.p2.psywave = p2_psywave;
|
|
242
|
+
for (Rolls.moveSlot(f.p1, p1_hit)) |p1_slot| { a.p1.move_slot = p1_slot;
|
|
243
|
+
for (Rolls.moveSlot(f.p2, p2_hit)) |p2_slot| { a.p2.move_slot = p2_slot;
|
|
244
|
+
for (Rolls.multiHit(f.p1, p1_hit)) |p1_multi| { a.p1.multi_hit = p1_multi;
|
|
245
|
+
for (Rolls.multiHit(f.p2, p2_hit)) |p2_multi| { a.p2.multi_hit = p2_multi;
|
|
246
|
+
for (Rolls.secondaryChance(f.p1, p1_hit)) |p1_sec| { a.p1.secondary_chance = p1_sec;
|
|
247
|
+
for (Rolls.secondaryChance(f.p2, p2_hit)) |p2_sec| { a.p2.secondary_chance = p2_sec;
|
|
248
|
+
for (Rolls.criticalHit(f.p1, p1_hit)) |p1_crit| { a.p1.critical_hit = p1_crit;
|
|
249
|
+
for (Rolls.criticalHit(f.p2, p2_hit)) |p2_crit| { a.p2.critical_hit = p2_crit;
|
|
250
|
+
|
|
251
|
+
var p1_dmg = Rolls.damage(f.p1, p1_hit);
|
|
252
|
+
while (p1_dmg.min < p1_dmg.max) : (p1_dmg.min += 1) {
|
|
253
|
+
a.p1.damage = @intCast(p1_dmg.min);
|
|
254
|
+
|
|
255
|
+
var p2_dmg = Rolls.damage(f.p2, p2_hit);
|
|
256
|
+
|
|
257
|
+
const p1_min: u9 = p1_dmg.min;
|
|
258
|
+
const p2_min: u9 = p2_dmg.min;
|
|
259
|
+
|
|
260
|
+
while (p2_dmg.min < p2_dmg.max) : (p2_dmg.min += 1) {
|
|
261
|
+
a.p2.damage = @intCast(p2_dmg.min);
|
|
262
|
+
|
|
263
|
+
opts.calc.overrides = a;
|
|
264
|
+
opts.calc.summaries = .{};
|
|
265
|
+
opts.chance = .{ .probability = .{}, .durations = options.durations };
|
|
266
|
+
const q = &opts.chance.probability;
|
|
267
|
+
|
|
268
|
+
b = battle;
|
|
269
|
+
_ = try b.update(c1, c2, &opts);
|
|
270
|
+
|
|
271
|
+
stats.updates += 1;
|
|
272
|
+
|
|
273
|
+
const summaries = &opts.calc.summaries;
|
|
274
|
+
const p1_max: u9 = if (p2_dmg.min != p2_min)
|
|
275
|
+
p1_dmg.min
|
|
276
|
+
else
|
|
277
|
+
try Rolls.coalesce(.P1, @as(u8, @intCast(p1_dmg.min)), summaries, cap);
|
|
278
|
+
const p2_max: u9 =
|
|
279
|
+
try Rolls.coalesce(.P2, @as(u8, @intCast(p2_dmg.min)), summaries, cap);
|
|
280
|
+
|
|
281
|
+
if (opts.chance.actions.matches(f)) {
|
|
282
|
+
if (!opts.chance.actions.eql(a)) {
|
|
283
|
+
if (!summary) {
|
|
284
|
+
try debug(writer, opts.chance.actions, .{
|
|
285
|
+
.p1_max = p1_max,
|
|
286
|
+
.p2_max = p2_max,
|
|
287
|
+
.color = i,
|
|
288
|
+
.dim = true,
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
p1_dmg.min = p1_max;
|
|
293
|
+
p2_dmg.min = p2_max;
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if (!summary) {
|
|
298
|
+
try debug(writer, opts.chance.actions, .{
|
|
299
|
+
.p1_max = p1_max,
|
|
300
|
+
.p2_max = p2_max,
|
|
301
|
+
.color = i,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
for (p1_min..p1_max + 1) |p1d| {
|
|
306
|
+
for (p2_dmg.min..p2_max + 1) |p2d| {
|
|
307
|
+
var acts = opts.chance.actions;
|
|
308
|
+
acts.p1.damage = @intCast(p1d);
|
|
309
|
+
acts.p2.damage = @intCast(p2d);
|
|
310
|
+
if ((try seen.getOrPut(acts)).found_existing) {
|
|
311
|
+
err("already seen {}", .{acts}, options.seed);
|
|
312
|
+
return error.TestUnexpectedResult;
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (p1_max != p1_min) try q.update(p1_max - p1_min + 1, 1);
|
|
318
|
+
if (p2_max != p2_dmg.min) try q.update(p2_max - p2_dmg.min + 1, 1);
|
|
319
|
+
try p.add(q);
|
|
320
|
+
stats.saved += 1;
|
|
321
|
+
|
|
322
|
+
if (p.q < p.p) {
|
|
323
|
+
err("improper fraction {}", .{p}, options.seed);
|
|
324
|
+
return error.TestUnexpectedResult;
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
if (!matches(opts.chance.actions, i, frontier.items)) {
|
|
328
|
+
try frontier.append(opts.chance.actions);
|
|
329
|
+
|
|
330
|
+
try debug(writer, opts.chance.actions, .{
|
|
331
|
+
.p1_max = p1_max,
|
|
332
|
+
.p2_max = p2_max,
|
|
333
|
+
.dim = true,
|
|
334
|
+
.newline = false,
|
|
335
|
+
});
|
|
336
|
+
try writer.writeAll(" → ");
|
|
337
|
+
try debug(writer, opts.chance.actions, .{
|
|
338
|
+
.shape = true,
|
|
339
|
+
.color = frontier.items.len - 1,
|
|
340
|
+
.dim = true,
|
|
341
|
+
.background = true,
|
|
342
|
+
.indent = false,
|
|
343
|
+
});
|
|
344
|
+
} else if (!summary) {
|
|
345
|
+
try debug(writer, opts.chance.actions, .{
|
|
346
|
+
.p1_max = p1_max,
|
|
347
|
+
.p2_max = p2_max,
|
|
348
|
+
.dim = true,
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
p1_dmg.min = p1_max;
|
|
354
|
+
p2_dmg.min = p2_max;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
}}}}}}}}}}}}}}}}}}
|
|
358
|
+
|
|
359
|
+
if (@TypeOf(writer) != @TypeOf(std.io.null_writer)) {
|
|
360
|
+
p.reduce();
|
|
361
|
+
try writer.print(
|
|
362
|
+
" = {s} ({d:.2}%)\n\n",
|
|
363
|
+
.{p, 100 * @as(f128, @floatFromInt(p.p)) / @as(f128, @floatFromInt(p.q))},
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
assert(frontier.items.len == i);
|
|
370
|
+
stats.frontier = @max(stats.frontier, i);
|
|
371
|
+
frontier.shrinkRetainingCapacity(1);
|
|
372
|
+
|
|
373
|
+
}}
|
|
374
|
+
// zig fmt: on
|
|
375
|
+
|
|
376
|
+
stats.seen = seen.count();
|
|
377
|
+
|
|
378
|
+
p.reduce();
|
|
379
|
+
if (p.p != 1 or p.q != 1) {
|
|
380
|
+
err("expected 1, found {}", .{p}, options.seed);
|
|
381
|
+
return error.TestExpectedEqual;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return stats;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
pub fn update(
|
|
388
|
+
battle: anytype,
|
|
389
|
+
c1: Choice,
|
|
390
|
+
c2: Choice,
|
|
391
|
+
options: anytype,
|
|
392
|
+
allocator: std.mem.Allocator,
|
|
393
|
+
writer: anytype,
|
|
394
|
+
transition: bool,
|
|
395
|
+
) !Result {
|
|
396
|
+
const durations = options.chance.durations;
|
|
397
|
+
options.chance.reset();
|
|
398
|
+
|
|
399
|
+
if (!pkmn.options.chance or !pkmn.options.calc) return battle.update(c1, c2, options);
|
|
400
|
+
|
|
401
|
+
const original = battle.*;
|
|
402
|
+
|
|
403
|
+
// Perfom the actual update
|
|
404
|
+
const result = battle.update(c1, c2, options);
|
|
405
|
+
var overrides = options.chance.actions;
|
|
406
|
+
const updated = options.chance.durations;
|
|
407
|
+
|
|
408
|
+
if (@TypeOf(battle.rng) == data.PRNG) {
|
|
409
|
+
// Ensure we can encode the diffs in less than MAX_DIFFS bytes.
|
|
410
|
+
var buf: [helpers.MAX_DIFFS]u8 = undefined;
|
|
411
|
+
var stream: protocol.ByteStream = .{ .buffer = &buf };
|
|
412
|
+
const n = try helpers.diff(&original, battle, stream.writer());
|
|
413
|
+
|
|
414
|
+
// Applying the diff to the battle should take us back to the original copy
|
|
415
|
+
// of the battle (ignoring the RNG).
|
|
416
|
+
var patched = battle.*;
|
|
417
|
+
patched.rng = original.rng;
|
|
418
|
+
helpers.patch(&patched, buf[0..n]);
|
|
419
|
+
try expectEqual(original, patched);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (transition) {
|
|
423
|
+
// Ensure we can generate all transitions from the same original state
|
|
424
|
+
// (we must change the battle's RNG from a FixedRNG to a PRNG because
|
|
425
|
+
// the transitions function relies on RNG for discovery of states)
|
|
426
|
+
if (try transitions(unfix(original), c1, c2, allocator, writer, .{
|
|
427
|
+
.durations = durations,
|
|
428
|
+
.cap = true,
|
|
429
|
+
})) |stats| try expect(stats.frontier <= MAX_FRONTIER);
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Demonstrate that we can produce the same state by forcing the RNG to behave the
|
|
433
|
+
// same as we observed.
|
|
434
|
+
var override = pkmn.battle.options(
|
|
435
|
+
protocol.NULL,
|
|
436
|
+
Chance(Rational(u128)){ .probability = .{}, .durations = durations },
|
|
437
|
+
Calc{ .overrides = overrides },
|
|
438
|
+
);
|
|
439
|
+
var copy = original;
|
|
440
|
+
var overridden = copy.update(c1, c2, &override);
|
|
441
|
+
try expectEqual(result, overridden);
|
|
442
|
+
try expectEqualActions(overrides, override.chance.actions);
|
|
443
|
+
try expectEqual(updated, override.chance.durations);
|
|
444
|
+
|
|
445
|
+
// The actual battle excluding its RNG field should match a copy updated with
|
|
446
|
+
// overridden RNG (the copy RNG may have advanced because of no-ops)
|
|
447
|
+
copy.rng = battle.rng;
|
|
448
|
+
try expectEqual(battle.*, copy);
|
|
449
|
+
|
|
450
|
+
// Demonstrate that even with the duration rolls changed, the duration
|
|
451
|
+
// fields on their own are sufficient to achieve the same outcomes
|
|
452
|
+
// NB: The actual battle bytes will differ because the durations won't match
|
|
453
|
+
overrides.p1.duration = if (overrides.p1.duration > 0) 2 else 0;
|
|
454
|
+
overrides.p2.duration = if (overrides.p2.duration > 0) 2 else 0;
|
|
455
|
+
|
|
456
|
+
override = pkmn.battle.options(
|
|
457
|
+
protocol.NULL,
|
|
458
|
+
Chance(Rational(u128)){ .probability = .{}, .durations = durations },
|
|
459
|
+
Calc{ .overrides = overrides },
|
|
460
|
+
);
|
|
461
|
+
copy = original;
|
|
462
|
+
overridden = copy.update(c1, c2, &override);
|
|
463
|
+
try expectEqual(result, overridden);
|
|
464
|
+
try expectEqualActions(overrides, override.chance.actions);
|
|
465
|
+
try expectEqual(updated, override.chance.durations);
|
|
466
|
+
|
|
467
|
+
return result;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
fn expectEqualActions(expected: Actions, actual: Actions) !void {
|
|
471
|
+
return expectEqual(expected, actual) catch |e| switch (e) {
|
|
472
|
+
error.TestExpectedEqual => {
|
|
473
|
+
std.debug.print("expected {}, found {}\n", .{ expected, actual });
|
|
474
|
+
return e;
|
|
475
|
+
},
|
|
476
|
+
else => return e,
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
fn unfix(actual: anytype) data.Battle(data.PRNG) {
|
|
481
|
+
return .{
|
|
482
|
+
.sides = actual.sides,
|
|
483
|
+
.turn = actual.turn,
|
|
484
|
+
.last_damage = actual.last_damage,
|
|
485
|
+
.last_moves = actual.last_moves,
|
|
486
|
+
.rng = .{ .src = .{
|
|
487
|
+
.seed = if (pkmn.options.showdown)
|
|
488
|
+
0x12345678
|
|
489
|
+
else
|
|
490
|
+
.{ 123, 234, 56, 78, 9, 101, 112, 131, 4 },
|
|
491
|
+
} },
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
fn matches(actions: Actions, i: usize, frontier: []Actions) bool {
|
|
496
|
+
for (frontier, 0..) |f, j| {
|
|
497
|
+
// TODO: is skipping this redundant check worth it?
|
|
498
|
+
if (i == j) continue;
|
|
499
|
+
if (f.matches(actions)) return true;
|
|
500
|
+
}
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
fn err(comptime fmt: []const u8, v: anytype, seed: ?u64) void {
|
|
505
|
+
const w = std.io.getStdErr().writer();
|
|
506
|
+
w.print(fmt, v) catch return;
|
|
507
|
+
if (seed) |s| return w.print("{}\n", .{s}) catch return;
|
|
508
|
+
return w.writeByte('\n') catch return;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
const Style = struct {
|
|
512
|
+
shape: bool = false,
|
|
513
|
+
p1_max: u9 = 0,
|
|
514
|
+
p2_max: u9 = 0,
|
|
515
|
+
color: ?usize = null,
|
|
516
|
+
bold: bool = false,
|
|
517
|
+
background: bool = false,
|
|
518
|
+
dim: bool = false,
|
|
519
|
+
newline: bool = true,
|
|
520
|
+
indent: bool = true,
|
|
521
|
+
};
|
|
522
|
+
|
|
523
|
+
fn debug(writer: anytype, actions: Actions, style: Style) !void {
|
|
524
|
+
if (style.indent) try writer.writeAll(" ");
|
|
525
|
+
if (tty) {
|
|
526
|
+
const mod: usize = if (style.dim) 2 else 1;
|
|
527
|
+
const background: usize = if (style.background) 4 else 3;
|
|
528
|
+
const color: usize = if (style.color) |c| (c % 6) + 1 else 7;
|
|
529
|
+
|
|
530
|
+
if (style.dim or style.bold) try writer.print("\x1b[{d}m", .{mod});
|
|
531
|
+
try writer.print("\x1b[{d}{d}m", .{ background, color });
|
|
532
|
+
const p1 = if (style.p1_max != 0 and style.p1_max != actions.p1.damage) style.p1_max else 0;
|
|
533
|
+
const p2 = if (style.p2_max != 0 and style.p2_max != actions.p2.damage) style.p2_max else 0;
|
|
534
|
+
if (p1 != 0 or p2 != 0) {
|
|
535
|
+
assert(!style.shape);
|
|
536
|
+
try format(writer, actions, p1, p2);
|
|
537
|
+
} else {
|
|
538
|
+
try actions.fmt(writer, style.shape);
|
|
539
|
+
}
|
|
540
|
+
try writer.writeAll("\x1b[0m");
|
|
541
|
+
} else {
|
|
542
|
+
if (style.dim) try writer.writeAll(" ");
|
|
543
|
+
try actions.fmt(writer, style.shape);
|
|
544
|
+
}
|
|
545
|
+
if (style.newline) try writer.writeByte('\n');
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
fn format(writer: anytype, actions: Actions, p1: u9, p2: u9) !void {
|
|
549
|
+
var input: [1024]u8 = undefined;
|
|
550
|
+
var output: [1024]u8 = undefined;
|
|
551
|
+
|
|
552
|
+
var stream = std.io.fixedBufferStream(&input);
|
|
553
|
+
try actions.fmt(&stream.writer(), false);
|
|
554
|
+
var len = stream.getWritten().len;
|
|
555
|
+
@memcpy(output[0..len], input[0..len]);
|
|
556
|
+
|
|
557
|
+
var find: [16]u8 = undefined;
|
|
558
|
+
var repl: [22]u8 = undefined;
|
|
559
|
+
if (p1 != 0 and actions.p1.damage != 0) {
|
|
560
|
+
const n = try std.fmt.bufPrint(&find, "P1 = (damage:{d}", .{actions.p1.damage});
|
|
561
|
+
const r = try std.fmt.bufPrint(&repl, "P1 = (damage:{d}…{d}", .{ actions.p1.damage, p1 });
|
|
562
|
+
assert(std.mem.replace(u8, input[0..len], n, r, output[0 .. len + 6]) == 1);
|
|
563
|
+
@memcpy(input[0 .. len + 6], output[0 .. len + 6]);
|
|
564
|
+
len += 6;
|
|
565
|
+
}
|
|
566
|
+
if (p2 != 0 and actions.p2.damage != 0) {
|
|
567
|
+
const n = try std.fmt.bufPrint(&find, "P2 = (damage:{d}", .{actions.p2.damage});
|
|
568
|
+
const r = try std.fmt.bufPrint(&repl, "P2 = (damage:{d}…{d}", .{ actions.p2.damage, p2 });
|
|
569
|
+
assert(std.mem.replace(u8, input[0..len], n, r, output[0 .. len + 6]) == 1);
|
|
570
|
+
len += 6;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
try writer.writeAll(output[0..len]);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const NONE = @intFromEnum(Optional(bool).None);
|
|
577
|
+
|
|
578
|
+
/// Helper functions that efficiently return valid ranges for various RNG events based on the
|
|
579
|
+
/// state of an `Action` and other events to be used to construct a "transitions" function.
|
|
580
|
+
pub const Rolls = struct {
|
|
581
|
+
const PLAYER_NONE = [_]Optional(Player){.None};
|
|
582
|
+
const PLAYERS = [_]Optional(Player){ .P1, .P2 };
|
|
583
|
+
|
|
584
|
+
/// Returns a slice with the correct range of values for speed ties given the `action` state.
|
|
585
|
+
pub fn speedTie(action: Action) []const Optional(Player) {
|
|
586
|
+
return if (action.speed_tie == .None) &PLAYER_NONE else &PLAYERS;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
const BOOL_NONE = [_]Optional(bool){.None};
|
|
590
|
+
const BOOLS = [_]Optional(bool){ .false, .true };
|
|
591
|
+
|
|
592
|
+
/// Returns a slice with the correct range of values for hits given the `action` state
|
|
593
|
+
/// and the state of the `parent` (whether the player's Pokémon was fully paralyzed).
|
|
594
|
+
pub fn hit(action: Action, parent: Optional(bool)) []const Optional(bool) {
|
|
595
|
+
if (parent == .true) return &BOOL_NONE;
|
|
596
|
+
return if (action.hit == .None) &BOOL_NONE else &BOOLS;
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
/// Returns a slice with the correct range of values for critical hits given the `action` state
|
|
600
|
+
/// and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
601
|
+
pub fn criticalHit(action: Action, parent: Optional(bool)) []const Optional(bool) {
|
|
602
|
+
if (parent == .false) return &BOOL_NONE;
|
|
603
|
+
return if (action.critical_hit == .None) &BOOL_NONE else &BOOLS;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
/// Returns a slice with the correct range of values for secondary chances hits given the
|
|
607
|
+
/// `action` state and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
608
|
+
pub fn secondaryChance(action: Action, parent: Optional(bool)) []const Optional(bool) {
|
|
609
|
+
if (parent == .false) return &BOOL_NONE;
|
|
610
|
+
return if (@field(action, "secondary_chance") == .None) &BOOL_NONE else &BOOLS;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/// The min and max bounds on iteration over damage rolls.
|
|
614
|
+
pub const Range = struct { min: u9, max: u9 };
|
|
615
|
+
|
|
616
|
+
/// Returns the range bounding damage rolls given the `action` state and the state of
|
|
617
|
+
/// the `parent` (whether the player's Pokémon's move hit).
|
|
618
|
+
pub fn damage(action: Action, parent: Optional(bool)) Range {
|
|
619
|
+
return if (parent == .false or action.damage == 0)
|
|
620
|
+
.{ .min = 0, .max = 1 }
|
|
621
|
+
else
|
|
622
|
+
.{ .min = 217, .max = 256 };
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/// Returns the max damage roll which will produce the same damage as `roll`
|
|
626
|
+
/// given the base damage in `summaries`.
|
|
627
|
+
pub fn coalesce(player: Player, roll: u8, summaries: *Summaries, cap: bool) !u8 {
|
|
628
|
+
if (roll == 0) return roll;
|
|
629
|
+
|
|
630
|
+
const dmg = summaries.get(player.foe()).damage;
|
|
631
|
+
if (dmg.base == 0 or (cap and dmg.capped)) return 255;
|
|
632
|
+
|
|
633
|
+
// Closed form solution for max damage roll provided by Orion Taylor (taylorott)
|
|
634
|
+
return @min(255, roll + ((254 - ((@as(u32, dmg.base) * roll) % 255)) / dmg.base));
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
/// Returns a slice with the correct range of values for confused.
|
|
638
|
+
pub fn confused(action: Action) []const Optional(bool) {
|
|
639
|
+
return if (action.confused == .None) &BOOL_NONE else &BOOLS;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
/// Returns a slice with the correct range of values for paralysis given the `action` state
|
|
643
|
+
/// and the state of the `parent` (whether the player's Pokémon was confused).
|
|
644
|
+
pub fn paralyzed(action: Action, parent: Optional(bool)) []const Optional(bool) {
|
|
645
|
+
if (parent == .true) return &BOOL_NONE;
|
|
646
|
+
return if (action.paralyzed == .None) &BOOL_NONE else &BOOLS;
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
const SLOT_NONE = [_]u4{0};
|
|
650
|
+
const SLOT = [_]u4{ 1, 2, 3, 4 };
|
|
651
|
+
|
|
652
|
+
/// Returns a slice with a range of values for move slots given the `action` state
|
|
653
|
+
/// and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
654
|
+
///
|
|
655
|
+
/// These slots may or **may not be valid** as slots may be unset / have 0 PP.
|
|
656
|
+
pub fn moveSlot(action: Action, parent: Optional(bool)) []const u4 {
|
|
657
|
+
if (parent == .false) return &SLOT_NONE;
|
|
658
|
+
return if (action.move_slot == 0) &SLOT_NONE else &SLOT;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
const MULTI_NONE = [_]u4{0};
|
|
662
|
+
const MULTI = [_]u4{ 2, 3, 4, 5 };
|
|
663
|
+
|
|
664
|
+
/// Returns a slice with the correct range of values for multi hit given the `action` state
|
|
665
|
+
/// and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
666
|
+
pub fn multiHit(action: Action, parent: Optional(bool)) []const u4 {
|
|
667
|
+
if (parent == .false) return &MULTI_NONE;
|
|
668
|
+
return if (action.multi_hit == 0) &MULTI_NONE else &MULTI;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
const PSYWAVE_NONE = [_]u8{0};
|
|
672
|
+
const PSYWAVE = init: {
|
|
673
|
+
var rolls: [150]u8 = undefined;
|
|
674
|
+
for (0..150) |i| rolls[i] = i + 1;
|
|
675
|
+
break :init rolls;
|
|
676
|
+
};
|
|
677
|
+
|
|
678
|
+
/// Returns a slice with the correct range of values for psywave given the `action` state,
|
|
679
|
+
/// the `side`, and the state of the `parent` (whether the player's Pokémon's move hit).
|
|
680
|
+
pub fn psywave(
|
|
681
|
+
action: Action,
|
|
682
|
+
side: *data.Side,
|
|
683
|
+
parent: Optional(bool),
|
|
684
|
+
) []const u8 {
|
|
685
|
+
if (parent == .false) return &PSYWAVE_NONE;
|
|
686
|
+
return if (action.psywave == 0)
|
|
687
|
+
&PSYWAVE_NONE
|
|
688
|
+
else
|
|
689
|
+
PSYWAVE[0 .. @as(u16, side.stored().level) * 3 / 2];
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
const MOVE_NONE = [_]data.Move{.None};
|
|
693
|
+
|
|
694
|
+
/// Returns a slice with the correct range of values for metronome given the `action` state.
|
|
695
|
+
pub fn metronome(action: Action) []const data.Move {
|
|
696
|
+
return if (action.metronome == .None) &MOVE_NONE else &data.Move.METRONOME;
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
|
|
700
|
+
test "Rolls.speedTie" {
|
|
701
|
+
const actions: Actions = .{ .p1 = .{ .speed_tie = .P2 } };
|
|
702
|
+
try expectEqualSlices(Optional(Player), &.{ .P1, .P2 }, Rolls.speedTie(actions.p1));
|
|
703
|
+
try expectEqualSlices(Optional(Player), &.{.None}, Rolls.speedTie(actions.p2));
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
test "Rolls.damage" {
|
|
707
|
+
const actions: Actions = .{ .p2 = .{ .damage = 221 } };
|
|
708
|
+
try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p1, .None));
|
|
709
|
+
try expectEqual(Rolls.Range{ .min = 217, .max = 256 }, Rolls.damage(actions.p2, .None));
|
|
710
|
+
try expectEqual(Rolls.Range{ .min = 0, .max = 1 }, Rolls.damage(actions.p2, .false));
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
test "Rolls.coalesce" {
|
|
714
|
+
var summaries = Summaries{ .p1 = .{ .damage = .{ .base = 74, .final = 69, .capped = true } } };
|
|
715
|
+
try expectEqual(0, try Rolls.coalesce(.P2, 0, &summaries, false));
|
|
716
|
+
try expectEqual(241, try Rolls.coalesce(.P2, 238, &summaries, false));
|
|
717
|
+
try expectEqual(255, try Rolls.coalesce(.P2, 238, &summaries, true));
|
|
718
|
+
summaries.p1.damage.final = 74;
|
|
719
|
+
try expectEqual(217, try Rolls.coalesce(.P2, 217, &summaries, false));
|
|
720
|
+
try expectEqual(255, try Rolls.coalesce(.P2, 217, &summaries, true));
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
test "Rolls.hit" {
|
|
724
|
+
const actions: Actions = .{ .p2 = .{ .hit = .true } };
|
|
725
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p1, .None));
|
|
726
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .None));
|
|
727
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.hit(actions.p2, .true));
|
|
728
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.hit(actions.p2, .false));
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
test "Rolls.secondaryChance" {
|
|
732
|
+
const actions: Actions = .{ .p1 = .{ .secondary_chance = .true } };
|
|
733
|
+
try expectEqualSlices(
|
|
734
|
+
Optional(bool),
|
|
735
|
+
&.{ .false, .true },
|
|
736
|
+
Rolls.secondaryChance(actions.p1, .None),
|
|
737
|
+
);
|
|
738
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p1, .false));
|
|
739
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.secondaryChance(actions.p2, .None));
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
test "Rolls.criticalHit" {
|
|
743
|
+
const actions: Actions = .{ .p1 = .{ .critical_hit = .true } };
|
|
744
|
+
try expectEqualSlices(
|
|
745
|
+
Optional(bool),
|
|
746
|
+
&.{ .false, .true },
|
|
747
|
+
Rolls.criticalHit(actions.p1, .None),
|
|
748
|
+
);
|
|
749
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p1, .false));
|
|
750
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.criticalHit(actions.p2, .None));
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
test "Rolls.confused" {
|
|
754
|
+
const actions: Actions = .{ .p2 = .{ .confused = .true } };
|
|
755
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.confused(actions.p1));
|
|
756
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.confused(actions.p2));
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
test "Rolls.paralyzed" {
|
|
760
|
+
const actions: Actions = .{ .p2 = .{ .paralyzed = .true } };
|
|
761
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p1, .None));
|
|
762
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .None));
|
|
763
|
+
try expectEqualSlices(Optional(bool), &.{ .false, .true }, Rolls.paralyzed(actions.p2, .false));
|
|
764
|
+
try expectEqualSlices(Optional(bool), &.{.None}, Rolls.paralyzed(actions.p2, .true));
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
test "Rolls.moveSlot" {
|
|
768
|
+
const actions: Actions = .{ .p2 = .{ .move_slot = 3 } };
|
|
769
|
+
try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p1, .None));
|
|
770
|
+
try expectEqualSlices(u4, &.{ 1, 2, 3, 4 }, Rolls.moveSlot(actions.p2, .None));
|
|
771
|
+
try expectEqualSlices(u4, &.{0}, Rolls.moveSlot(actions.p2, .false));
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
test "Rolls.multiHit" {
|
|
775
|
+
const actions: Actions = .{ .p2 = .{ .multi_hit = 3 } };
|
|
776
|
+
try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p1, .None));
|
|
777
|
+
try expectEqualSlices(u4, &.{ 2, 3, 4, 5 }, Rolls.multiHit(actions.p2, .None));
|
|
778
|
+
try expectEqualSlices(u4, &.{0}, Rolls.multiHit(actions.p2, .false));
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
test "Rolls.metronome" {
|
|
782
|
+
const actions: Actions = .{ .p2 = .{ .metronome = .Surf } };
|
|
783
|
+
try expectEqualSlices(data.Move, &.{.None}, Rolls.metronome(actions.p1));
|
|
784
|
+
try expectEqual(@as(data.Move, @enumFromInt(24)), Rolls.metronome(actions.p2)[23]);
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
test "Rolls.psywave" {
|
|
788
|
+
const actions: Actions = .{ .p2 = .{ .psywave = 79 } };
|
|
789
|
+
var side = helpers.Side.init(&[_]helpers.Pokemon{.{
|
|
790
|
+
.species = .Bulbasaur,
|
|
791
|
+
.level = 100,
|
|
792
|
+
.moves = &[_]data.Move{.Tackle},
|
|
793
|
+
}});
|
|
794
|
+
|
|
795
|
+
try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p1, &side, .None));
|
|
796
|
+
var rolls = Rolls.psywave(actions.p2, &side, .None);
|
|
797
|
+
try expectEqual(150, rolls[rolls.len - 1]);
|
|
798
|
+
side.stored().level = 81;
|
|
799
|
+
rolls = Rolls.psywave(actions.p2, &side, .None);
|
|
800
|
+
try expectEqual(121, rolls[rolls.len - 1]);
|
|
801
|
+
try expectEqualSlices(u8, &.{0}, Rolls.psywave(actions.p2, &side, .false));
|
|
802
|
+
}
|