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