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