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