@pkmn/engine 0.1.0-dev.af1565df → 0.1.0-dev.b01cb48c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -3
- package/README.md +95 -59
- package/build/pkg/addon.js +9 -9
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/layout.json +26 -4
- package/build/pkg/data/protocol.json +69 -12
- package/build/pkg/data.d.ts +4 -4
- package/build/pkg/data.js.map +1 -1
- package/build/pkg/gen1.js +15 -18
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.d.ts +2 -1
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.js +61 -42
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +8 -3
- package/build/test/display.js.map +1 -1
- package/build/tools/debug.js +5 -4
- package/build/tools/debug.js.map +1 -1
- package/build.zig +148 -126
- package/package.json +53 -24
- package/src/bin/install-pkmn-engine +35 -22
- package/src/lib/common/battle.zig +17 -0
- package/src/lib/common/data.zig +19 -31
- package/src/lib/common/debug.zig +22 -13
- package/src/lib/common/js.zig +118 -0
- package/src/lib/common/optional.zig +67 -0
- package/src/lib/common/options.zig +11 -0
- package/src/lib/common/protocol.zig +906 -367
- package/src/lib/common/rational.zig +447 -0
- package/src/lib/common/rng.zig +35 -26
- package/src/lib/common/util.zig +12 -0
- package/src/lib/gen1/calc.zig +831 -0
- package/src/lib/gen1/chance.zig +826 -0
- package/src/lib/gen1/data/moves.zig +202 -190
- package/src/lib/gen1/data/species.zig +5 -5
- package/src/lib/gen1/data/types.zig +10 -10
- package/src/lib/gen1/data.zig +102 -90
- package/src/lib/gen1/helpers.zig +134 -24
- package/src/lib/gen1/mechanics.zig +1051 -739
- package/src/lib/gen2/calc.zig +143 -0
- package/src/lib/gen2/chance.zig +882 -0
- package/src/lib/gen2/data/items.zig +14 -20
- package/src/lib/gen2/data/moves.zig +664 -570
- package/src/lib/gen2/data/species.zig +2 -2
- package/src/lib/gen2/data/types.zig +137 -34
- package/src/lib/gen2/data.zig +130 -92
- package/src/lib/gen2/generated.zig +806 -0
- package/src/lib/gen2/helpers.zig +50 -26
- package/src/lib/gen2/mechanics.zig +3564 -12
- package/src/lib/node.zig +131 -0
- package/src/lib/pkmn.zig +79 -5
- package/src/lib/wasm.zig +13 -0
- package/src/test/display/pkmn.html.tmpl +5 -5
- package/src/lib/binding/node.zig +0 -229
- package/src/lib/binding/wasm.zig +0 -10
- package/src/lib/gen1/README.md +0 -812
- package/src/lib/gen2/README.md +0 -309
|
@@ -0,0 +1,826 @@
|
|
|
1
|
+
const std = @import("std");
|
|
2
|
+
|
|
3
|
+
const assert = std.debug.assert;
|
|
4
|
+
const print = std.debug.print;
|
|
5
|
+
|
|
6
|
+
const expect = std.testing.expect;
|
|
7
|
+
const expectEqual = std.testing.expectEqual;
|
|
8
|
+
|
|
9
|
+
const DEBUG = @import("../common/debug.zig").print;
|
|
10
|
+
const options = @import("../common/options.zig");
|
|
11
|
+
const rational = @import("../common/rational.zig");
|
|
12
|
+
const util = @import("../common/util.zig");
|
|
13
|
+
|
|
14
|
+
const Player = @import("../common/data.zig").Player;
|
|
15
|
+
const Optional = @import("../common/optional.zig").Optional;
|
|
16
|
+
|
|
17
|
+
const data = @import("data.zig");
|
|
18
|
+
|
|
19
|
+
const enabled = options.chance;
|
|
20
|
+
const showdown = options.showdown;
|
|
21
|
+
|
|
22
|
+
const Move = data.Move;
|
|
23
|
+
const MoveSlot = data.MoveSlot;
|
|
24
|
+
|
|
25
|
+
/// Actions taken by a hypothetical "chance player" that convey information about which RNG events
|
|
26
|
+
/// were observed during a Generation I battle `update`. This can additionally be provided as input
|
|
27
|
+
/// to the `update` call to override the normal behavior of the RNG in order to force specific
|
|
28
|
+
/// outcomes.
|
|
29
|
+
pub const Actions = extern struct {
|
|
30
|
+
/// Information about the RNG activity for Player 1.
|
|
31
|
+
p1: Action = .{},
|
|
32
|
+
/// Information about the RNG activity for Player 2.
|
|
33
|
+
p2: Action = .{},
|
|
34
|
+
|
|
35
|
+
comptime {
|
|
36
|
+
assert(@sizeOf(Actions) == 16);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/// Returns the `Action` for the given `player`.
|
|
40
|
+
pub fn get(self: anytype, player: Player) util.PointerType(@TypeOf(self), Action) {
|
|
41
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Actions);
|
|
42
|
+
return if (player == .P1) &self.p1 else &self.p2;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/// Returns true if `a` is equal to `b`.
|
|
46
|
+
pub fn eql(a: Actions, b: Actions) bool {
|
|
47
|
+
return @as(u128, @bitCast(a)) == @as(u128, @bitCast(b));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/// Returns true if `a` has the same "shape" as `b`, where `Actions` are defined to have the
|
|
51
|
+
/// same shape if they have the same fields set (though those fields need not necessarily be
|
|
52
|
+
/// set to the same value).
|
|
53
|
+
pub fn matches(a: Actions, b: Actions) bool {
|
|
54
|
+
inline for (@typeInfo(Actions).Struct.fields) |player| {
|
|
55
|
+
inline for (@typeInfo(Action).Struct.fields) |field| {
|
|
56
|
+
const a_val = @field(@field(a, player.name), field.name);
|
|
57
|
+
const b_val = @field(@field(b, player.name), field.name);
|
|
58
|
+
|
|
59
|
+
switch (@typeInfo(@TypeOf(a_val))) {
|
|
60
|
+
.Enum => if ((@intFromEnum(a_val) > 0) != (@intFromEnum(b_val) > 0))
|
|
61
|
+
return false,
|
|
62
|
+
.Int => if ((a_val > 0) != (b_val > 0)) return false,
|
|
63
|
+
else => unreachable,
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
pub fn fmt(self: Actions, writer: anytype, shape: bool) !void {
|
|
71
|
+
try writer.writeAll("<P1 = ");
|
|
72
|
+
try self.p1.fmt(writer, shape);
|
|
73
|
+
try writer.writeAll(", P2 = ");
|
|
74
|
+
try self.p2.fmt(writer, shape);
|
|
75
|
+
try writer.writeAll(">");
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
pub fn format(a: Actions, comptime f: []const u8, o: std.fmt.FormatOptions, w: anytype) !void {
|
|
79
|
+
_ = .{ f, o };
|
|
80
|
+
try fmt(a, w, false);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
test Actions {
|
|
85
|
+
const a: Actions = .{ .p1 = .{ .hit = .true, .critical_hit = .false, .damage = 245 } };
|
|
86
|
+
const b: Actions = .{ .p1 = .{ .hit = .false, .critical_hit = .true, .damage = 246 } };
|
|
87
|
+
const c: Actions = .{ .p1 = .{ .hit = .true } };
|
|
88
|
+
|
|
89
|
+
try expect(a.eql(a));
|
|
90
|
+
try expect(!a.eql(b));
|
|
91
|
+
try expect(!b.eql(a));
|
|
92
|
+
try expect(!a.eql(c));
|
|
93
|
+
try expect(!c.eql(a));
|
|
94
|
+
|
|
95
|
+
try expect(a.matches(a));
|
|
96
|
+
try expect(a.matches(b));
|
|
97
|
+
try expect(b.matches(a));
|
|
98
|
+
try expect(!a.matches(c));
|
|
99
|
+
try expect(!c.matches(a));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
pub const Observation = enum { started, continuing, ended };
|
|
103
|
+
pub const Confusion = enum { started, continuing, ended, overwritten };
|
|
104
|
+
|
|
105
|
+
/// Information about the RNG that was observed during a Generation I battle `update` for a
|
|
106
|
+
/// single player.
|
|
107
|
+
pub const Action = packed struct(u64) {
|
|
108
|
+
/// If not 0, the roll to be returned Rolls.damage.
|
|
109
|
+
damage: u8 = 0,
|
|
110
|
+
|
|
111
|
+
/// If not None, the value to return for Rolls.hit.
|
|
112
|
+
hit: Optional(bool) = .None,
|
|
113
|
+
/// If not None, the value to be returned by Rolls.criticalHit.
|
|
114
|
+
critical_hit: Optional(bool) = .None,
|
|
115
|
+
/// If not None, the value to be returned for
|
|
116
|
+
/// Rolls.{confusionChance,secondaryChance,poisonChance}.
|
|
117
|
+
secondary_chance: Optional(bool) = .None,
|
|
118
|
+
/// If not None, the Player to be returned by Rolls.speedTie.
|
|
119
|
+
speed_tie: Optional(Player) = .None,
|
|
120
|
+
|
|
121
|
+
/// If not None, the value to return for Rolls.confused.
|
|
122
|
+
confused: Optional(bool) = .None,
|
|
123
|
+
/// If not None, the value to return for Rolls.paralyzed.
|
|
124
|
+
paralyzed: Optional(bool) = .None,
|
|
125
|
+
/// If not 0, the value to be returned by Rolls.distribution in the case of binding moves
|
|
126
|
+
/// or Rolls.{sleepDuration,disableDuration,confusionDuration,attackingDuration} otherwise.
|
|
127
|
+
duration: u4 = 0,
|
|
128
|
+
|
|
129
|
+
// TODO
|
|
130
|
+
sleep: Optional(Observation) = .None,
|
|
131
|
+
confusion: Optional(Confusion) = .None,
|
|
132
|
+
disable: Optional(Observation) = .None,
|
|
133
|
+
bide: Optional(Observation) = .None,
|
|
134
|
+
binding: Optional(Observation) = .None,
|
|
135
|
+
thrashing: Optional(Observation) = .None,
|
|
136
|
+
|
|
137
|
+
_: u3 = 0, // TODO
|
|
138
|
+
|
|
139
|
+
/// If not 0, the move slot (1-4) to return in Rolls.moveSlot. If present as an override,
|
|
140
|
+
/// invalid values (eg. due to empty move slots or 0 PP) will be ignored.
|
|
141
|
+
move_slot: u4 = 0,
|
|
142
|
+
/// If not 0, the value (2-5) to return for Rolls.distribution for multi hit.
|
|
143
|
+
multi_hit: u4 = 0,
|
|
144
|
+
|
|
145
|
+
/// If not 0, psywave - 1 should be returned as the damage roll for Rolls.psywave.
|
|
146
|
+
psywave: u8 = 0,
|
|
147
|
+
|
|
148
|
+
/// If not None, the Move to return for Rolls.metronome.
|
|
149
|
+
metronome: Move = .None,
|
|
150
|
+
|
|
151
|
+
pub const Field = std.meta.FieldEnum(Action);
|
|
152
|
+
|
|
153
|
+
pub fn format(a: Action, comptime f: []const u8, o: std.fmt.FormatOptions, w: anytype) !void {
|
|
154
|
+
_ = .{ f, o };
|
|
155
|
+
try fmt(a, w, false);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
pub fn fmt(self: Action, writer: anytype, shape: bool) !void {
|
|
159
|
+
try writer.writeByte('(');
|
|
160
|
+
var printed = false;
|
|
161
|
+
inline for (@typeInfo(Action).Struct.fields) |field| {
|
|
162
|
+
const val = @field(self, field.name);
|
|
163
|
+
switch (@typeInfo(@TypeOf(val))) {
|
|
164
|
+
.Enum => if (val != .None) {
|
|
165
|
+
if (printed) try writer.writeAll(", ");
|
|
166
|
+
if (shape) {
|
|
167
|
+
try writer.print("{s}:?", .{field.name});
|
|
168
|
+
} else if (@TypeOf(val) == Optional(bool)) {
|
|
169
|
+
try writer.print("{s}{s}", .{
|
|
170
|
+
if (val == .false) "!" else "",
|
|
171
|
+
field.name,
|
|
172
|
+
});
|
|
173
|
+
} else if (@TypeOf(val) == Optional(Observation)) {
|
|
174
|
+
try writer.print("{s}{s}", .{
|
|
175
|
+
switch (val) {
|
|
176
|
+
.started => "+",
|
|
177
|
+
.continuing => "",
|
|
178
|
+
.ended => "-",
|
|
179
|
+
else => unreachable,
|
|
180
|
+
},
|
|
181
|
+
field.name,
|
|
182
|
+
});
|
|
183
|
+
} else if (@TypeOf(val) == Optional(Confusion)) {
|
|
184
|
+
try writer.print("{s}{s}", .{
|
|
185
|
+
switch (val) {
|
|
186
|
+
.started => "+",
|
|
187
|
+
.continuing => "",
|
|
188
|
+
.ended => "-",
|
|
189
|
+
.overwritten => "#",
|
|
190
|
+
else => unreachable,
|
|
191
|
+
},
|
|
192
|
+
field.name,
|
|
193
|
+
});
|
|
194
|
+
} else {
|
|
195
|
+
try writer.print("{s}:{s}", .{ field.name, @tagName(val) });
|
|
196
|
+
}
|
|
197
|
+
printed = true;
|
|
198
|
+
},
|
|
199
|
+
.Int => if (val != 0) {
|
|
200
|
+
if (printed) try writer.writeAll(", ");
|
|
201
|
+
if (shape) {
|
|
202
|
+
try writer.print("{s}:?", .{field.name});
|
|
203
|
+
} else {
|
|
204
|
+
try writer.print("{s}:{d}", .{ field.name, val });
|
|
205
|
+
}
|
|
206
|
+
printed = true;
|
|
207
|
+
},
|
|
208
|
+
else => unreachable,
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
try writer.writeByte(')');
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
/// TODO
|
|
216
|
+
pub const Durations = struct {
|
|
217
|
+
/// TODO
|
|
218
|
+
p1: Duration = .{},
|
|
219
|
+
/// TODO
|
|
220
|
+
p2: Duration = .{},
|
|
221
|
+
|
|
222
|
+
/// TODO
|
|
223
|
+
pub fn get(self: anytype, player: Player) util.PointerType(@TypeOf(self), Duration) {
|
|
224
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Durations);
|
|
225
|
+
return if (player == .P1) &self.p1 else &self.p2;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
pub fn format(
|
|
229
|
+
self: Durations,
|
|
230
|
+
comptime fmt: []const u8,
|
|
231
|
+
opts: std.fmt.FormatOptions,
|
|
232
|
+
writer: anytype,
|
|
233
|
+
) !void {
|
|
234
|
+
_ = .{ fmt, opts };
|
|
235
|
+
try writer.print("[P1 = {}, P2 = {}]", .{ self.p1, self.p2 });
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
/// TODO
|
|
240
|
+
pub const Duration = struct {
|
|
241
|
+
sleeps: [6]u3 = .{0} ** 6,
|
|
242
|
+
confusion: u3 = 0,
|
|
243
|
+
disable: u4 = 0,
|
|
244
|
+
bide: u3 = 0,
|
|
245
|
+
thrashing: u3 = 0,
|
|
246
|
+
binding: u3 = 0,
|
|
247
|
+
|
|
248
|
+
pub fn format(
|
|
249
|
+
self: Duration,
|
|
250
|
+
comptime fmt: []const u8,
|
|
251
|
+
opts: std.fmt.FormatOptions,
|
|
252
|
+
writer: anytype,
|
|
253
|
+
) !void {
|
|
254
|
+
_ = .{ fmt, opts };
|
|
255
|
+
try writer.writeByte('(');
|
|
256
|
+
var printed = false;
|
|
257
|
+
inline for (@typeInfo(Duration).Struct.fields) |field| {
|
|
258
|
+
const val = @field(self, field.name);
|
|
259
|
+
switch (@typeInfo(@TypeOf(val))) {
|
|
260
|
+
.Int => if (val != 0) {
|
|
261
|
+
if (printed) try writer.writeAll(", ");
|
|
262
|
+
try writer.print("{s}:{d}", .{ field.name, val });
|
|
263
|
+
printed = true;
|
|
264
|
+
},
|
|
265
|
+
.Array => {
|
|
266
|
+
var total: usize = 0;
|
|
267
|
+
for (val) |v| total += v;
|
|
268
|
+
if (total > 0) {
|
|
269
|
+
if (printed) try writer.writeAll(", ");
|
|
270
|
+
try writer.print("{s}:[", .{field.name});
|
|
271
|
+
for (val, 0..) |v, i| {
|
|
272
|
+
if (i != 0) try writer.writeByte(',');
|
|
273
|
+
try writer.print("{d}", .{v});
|
|
274
|
+
}
|
|
275
|
+
try writer.writeAll("]");
|
|
276
|
+
printed = true;
|
|
277
|
+
}
|
|
278
|
+
},
|
|
279
|
+
else => unreachable,
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
try writer.writeByte(')');
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
pub const Commit = enum { hit, miss, binding };
|
|
287
|
+
|
|
288
|
+
const uN = if (options.miss) u8 else u9;
|
|
289
|
+
|
|
290
|
+
/// Tracks chance actions and their associated probability during a Generation I battle update when
|
|
291
|
+
/// `options.chance` is enabled.
|
|
292
|
+
pub fn Chance(comptime Rational: type) type {
|
|
293
|
+
return struct {
|
|
294
|
+
const Self = @This();
|
|
295
|
+
|
|
296
|
+
/// The probability of the actions taken by a hypothetical "chance player" occurring.
|
|
297
|
+
probability: Rational,
|
|
298
|
+
/// The actions taken by a hypothetical "chance player" that convey information about which
|
|
299
|
+
/// RNG events were observed during a battle `update`.
|
|
300
|
+
actions: Actions = .{},
|
|
301
|
+
/// TODO
|
|
302
|
+
durations: Durations = .{},
|
|
303
|
+
|
|
304
|
+
// In many cases on both the cartridge and on Pokémon Showdown rolls are made even though
|
|
305
|
+
// later checks render their result irrelevant (missing when the target is actually immune,
|
|
306
|
+
// rolling for damage when the move is later revealed to have missed, etc). We can't reorder
|
|
307
|
+
// the logic to ensure we only ever make rolls when required due to our desire to maintain
|
|
308
|
+
// RNG frame accuracy, so instead we save these "pending" updates to the probability and
|
|
309
|
+
// only commit them once we know they are relevant (a naive solution would be to instead
|
|
310
|
+
// update the probability eagerly and later "undo" the update by multiplying by the inverse
|
|
311
|
+
// but this would be more costly and also error prone in the presence of
|
|
312
|
+
// overflow/normalization).
|
|
313
|
+
//
|
|
314
|
+
// This design deliberately make use of the fact that recursive moves on the cartridge may
|
|
315
|
+
// overwrite critical hit information multiple times as only the last update actually
|
|
316
|
+
// matters for the purposes of the logical results.
|
|
317
|
+
//
|
|
318
|
+
// Finally, because each player's actions are processed sequentially we only need a single
|
|
319
|
+
// shared structure to track this information (though it needs to be cleared appropriately)
|
|
320
|
+
pending: if (showdown) struct {} else struct {
|
|
321
|
+
crit: bool = false,
|
|
322
|
+
crit_probablity: u8 = 0,
|
|
323
|
+
damage_roll: u8 = 0,
|
|
324
|
+
hit: bool = false,
|
|
325
|
+
hit_probablity: uN = 0,
|
|
326
|
+
} = .{},
|
|
327
|
+
|
|
328
|
+
/// Possible error returned by operations tracking chance probability.
|
|
329
|
+
pub const Error = Rational.Error;
|
|
330
|
+
|
|
331
|
+
/// Convenience helper to clear fields which typically should be cleared between updates.
|
|
332
|
+
pub fn reset(self: *Self) void {
|
|
333
|
+
if (!enabled) return;
|
|
334
|
+
|
|
335
|
+
self.probability.reset();
|
|
336
|
+
self.actions = .{};
|
|
337
|
+
self.pending = .{};
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
pub fn commit(self: *Self, player: Player, kind: Commit) Error!void {
|
|
341
|
+
if (!enabled) return;
|
|
342
|
+
|
|
343
|
+
assert(!showdown);
|
|
344
|
+
|
|
345
|
+
var action = self.actions.get(player);
|
|
346
|
+
|
|
347
|
+
// Always commit the hit result if we make it here (commit won't be called at all if the
|
|
348
|
+
// target is immune/behind a sub/etc)
|
|
349
|
+
if (self.pending.hit_probablity != 0) {
|
|
350
|
+
try self.probability.update(self.pending.hit_probablity, 256);
|
|
351
|
+
action.hit = if (self.pending.hit) .true else .false;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// If the move actually lands we can commit any past critical hit / damage rolls. Note
|
|
355
|
+
// we can't rely on the presence or absence of a damage roll to determine whether a
|
|
356
|
+
// "critical hit" was spurious or not as moves that do 1 or less damage don't rol for
|
|
357
|
+
// damage but can still crit (instead, whether or not a roll is deemed to be a no-op is
|
|
358
|
+
// passed to the critical hit helper)
|
|
359
|
+
if (kind != .hit) return assert(!self.pending.crit or self.pending.crit_probablity > 0);
|
|
360
|
+
|
|
361
|
+
if (self.pending.crit_probablity != 0) {
|
|
362
|
+
try self.probability.update(self.pending.crit_probablity, 256);
|
|
363
|
+
action.critical_hit = if (self.pending.crit) .true else .false;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (self.pending.damage_roll > 0) {
|
|
367
|
+
try self.probability.update(1, 39);
|
|
368
|
+
action.damage = self.pending.damage_roll;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
pub fn clearPending(self: *Self) void {
|
|
373
|
+
if (!enabled) return;
|
|
374
|
+
|
|
375
|
+
self.pending = .{};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
pub fn speedTie(self: *Self, p1: bool) Error!void {
|
|
379
|
+
if (!enabled) return;
|
|
380
|
+
|
|
381
|
+
try self.probability.update(1, 2);
|
|
382
|
+
self.actions.p1.speed_tie = if (p1) .P1 else .P2;
|
|
383
|
+
self.actions.p2.speed_tie = self.actions.p1.speed_tie;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
pub fn hit(self: *Self, player: Player, ok: bool, accuracy: uN) Error!void {
|
|
387
|
+
if (!enabled) return;
|
|
388
|
+
|
|
389
|
+
const p = if (ok) accuracy else @as(u8, @intCast(256 - @as(u9, accuracy)));
|
|
390
|
+
if (showdown) {
|
|
391
|
+
try self.probability.update(p, 256);
|
|
392
|
+
self.actions.get(player).hit = if (ok) .true else .false;
|
|
393
|
+
} else {
|
|
394
|
+
self.pending.hit = ok;
|
|
395
|
+
self.pending.hit_probablity = p;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
pub fn criticalHit(self: *Self, player: Player, crit: bool, rate: u8) Error!void {
|
|
400
|
+
if (!enabled) return;
|
|
401
|
+
|
|
402
|
+
const n = if (crit) rate else @as(u8, @intCast(256 - @as(u9, rate)));
|
|
403
|
+
if (showdown) {
|
|
404
|
+
try self.probability.update(n, 256);
|
|
405
|
+
self.actions.get(player).critical_hit = if (crit) .true else .false;
|
|
406
|
+
} else {
|
|
407
|
+
self.pending.crit = crit;
|
|
408
|
+
self.pending.crit_probablity = n;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
pub fn damage(self: *Self, player: Player, roll: u8) Error!void {
|
|
413
|
+
if (!enabled) return;
|
|
414
|
+
|
|
415
|
+
if (showdown) {
|
|
416
|
+
try self.probability.update(1, 39);
|
|
417
|
+
self.actions.get(player).damage = roll;
|
|
418
|
+
} else {
|
|
419
|
+
self.pending.damage_roll = roll;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
pub fn secondaryChance(self: *Self, player: Player, proc: bool, rate: u8) Error!void {
|
|
424
|
+
if (!enabled) return;
|
|
425
|
+
|
|
426
|
+
const n = if (proc) rate else @as(u8, @intCast(256 - @as(u9, rate)));
|
|
427
|
+
try self.probability.update(n, 256);
|
|
428
|
+
self.actions.get(player).secondary_chance = if (proc) .true else .false;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
pub fn confused(self: *Self, player: Player, cfz: bool) Error!void {
|
|
432
|
+
if (!enabled) return;
|
|
433
|
+
|
|
434
|
+
try self.probability.update(1, 2);
|
|
435
|
+
self.actions.get(player).confused = if (cfz) .true else .false;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
pub fn paralyzed(self: *Self, player: Player, par: bool) Error!void {
|
|
439
|
+
if (!enabled) return;
|
|
440
|
+
|
|
441
|
+
try self.probability.update(@as(u8, if (par) 1 else 3), 4);
|
|
442
|
+
self.actions.get(player).paralyzed = if (par) .true else .false;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
pub fn moveSlot(
|
|
446
|
+
self: *Self,
|
|
447
|
+
player: Player,
|
|
448
|
+
slot: u4,
|
|
449
|
+
ms: []const MoveSlot,
|
|
450
|
+
n: u4,
|
|
451
|
+
) Error!void {
|
|
452
|
+
if (!enabled) return;
|
|
453
|
+
|
|
454
|
+
const denominator = if (n != 0) n else denominator: {
|
|
455
|
+
var i: usize = ms.len;
|
|
456
|
+
while (i > 0) {
|
|
457
|
+
i -= 1;
|
|
458
|
+
if (ms[i].id != .None) break :denominator i + 1;
|
|
459
|
+
}
|
|
460
|
+
unreachable;
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
if (denominator != 1) try self.probability.update(1, denominator);
|
|
464
|
+
self.actions.get(player).move_slot = @intCast(slot);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
pub fn multiHit(self: *Self, player: Player, n: u3) Error!void {
|
|
468
|
+
if (!enabled) return;
|
|
469
|
+
|
|
470
|
+
try self.probability.update(@as(u8, if (n > 3) 1 else 3), 8);
|
|
471
|
+
self.actions.get(player).multi_hit = n;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
pub fn duration(self: *Self, player: Player, turns: u4) void {
|
|
475
|
+
if (!enabled) return;
|
|
476
|
+
|
|
477
|
+
self.actions.get(player).duration = if (options.key) 1 else turns;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
pub fn observe(
|
|
481
|
+
self: *Self,
|
|
482
|
+
comptime field: Action.Field,
|
|
483
|
+
player: Player,
|
|
484
|
+
observation: Optional(Confusion),
|
|
485
|
+
) void {
|
|
486
|
+
if (!enabled) return;
|
|
487
|
+
|
|
488
|
+
const val = @field(self.actions.get(player), @tagName(field));
|
|
489
|
+
if (observation == .overwritten) {
|
|
490
|
+
assert(field == .confusion);
|
|
491
|
+
assert(val == .started or val == .continuing);
|
|
492
|
+
assert(self.actions.get(player).duration > 0);
|
|
493
|
+
self.actions.get(player).confusion = observation;
|
|
494
|
+
} else {
|
|
495
|
+
assert(observation == .None or val == .None or
|
|
496
|
+
(val == .started and observation != .started) or
|
|
497
|
+
(val == .ended and observation == .started));
|
|
498
|
+
assert(observation != .started or (if (field == .confusion)
|
|
499
|
+
self.actions.get(player).duration > 0 or
|
|
500
|
+
self.actions.get(player.foe()).duration > 0
|
|
501
|
+
else if (field == .sleep or field == .disable)
|
|
502
|
+
self.actions.get(player.foe()).duration > 0
|
|
503
|
+
else
|
|
504
|
+
self.actions.get(player).duration > 0));
|
|
505
|
+
@field(self.actions.get(player), @tagName(field)) =
|
|
506
|
+
@enumFromInt(@intFromEnum(observation));
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
pub fn psywave(self: *Self, player: Player, power: u8, max: u8) Error!void {
|
|
511
|
+
if (!enabled) return;
|
|
512
|
+
|
|
513
|
+
try self.probability.update(1, max);
|
|
514
|
+
self.actions.get(player).psywave = power + 1;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
pub fn metronome(self: *Self, player: Player, move: Move) Error!void {
|
|
518
|
+
if (!enabled) return;
|
|
519
|
+
|
|
520
|
+
try self.probability.update(1, Move.METRONOME.len);
|
|
521
|
+
self.actions.get(player).metronome = move;
|
|
522
|
+
}
|
|
523
|
+
};
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
test "Chance.speedTie" {
|
|
527
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
528
|
+
|
|
529
|
+
try chance.speedTie(true);
|
|
530
|
+
try expectProbability(&chance.probability, 1, 2);
|
|
531
|
+
try expectValue(Optional(Player).P1, chance.actions.p1.speed_tie);
|
|
532
|
+
try expectValue(chance.actions.p1.speed_tie, chance.actions.p2.speed_tie);
|
|
533
|
+
|
|
534
|
+
chance.reset();
|
|
535
|
+
|
|
536
|
+
try chance.speedTie(false);
|
|
537
|
+
try expectProbability(&chance.probability, 1, 2);
|
|
538
|
+
try expectValue(Optional(Player).P2, chance.actions.p1.speed_tie);
|
|
539
|
+
try expectValue(chance.actions.p1.speed_tie, chance.actions.p2.speed_tie);
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
test "Chance.hit" {
|
|
543
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
544
|
+
|
|
545
|
+
try chance.hit(.P1, true, 229);
|
|
546
|
+
if (!showdown) {
|
|
547
|
+
try expectProbability(&chance.probability, 1, 1);
|
|
548
|
+
try expectValue(Optional(bool).None, chance.actions.p1.hit);
|
|
549
|
+
|
|
550
|
+
try chance.commit(.P1, .hit);
|
|
551
|
+
}
|
|
552
|
+
try expectValue(Optional(bool).true, chance.actions.p1.hit);
|
|
553
|
+
try expectProbability(&chance.probability, 229, 256);
|
|
554
|
+
|
|
555
|
+
chance.reset();
|
|
556
|
+
|
|
557
|
+
try chance.hit(.P2, false, 255);
|
|
558
|
+
if (!showdown) {
|
|
559
|
+
try expectProbability(&chance.probability, 1, 1);
|
|
560
|
+
try expectValue(Optional(bool).None, chance.actions.p2.hit);
|
|
561
|
+
|
|
562
|
+
try chance.commit(.P2, .miss);
|
|
563
|
+
}
|
|
564
|
+
try expectValue(Optional(bool).false, chance.actions.p2.hit);
|
|
565
|
+
try expectProbability(&chance.probability, 1, 256);
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
test "Chance.criticalHit" {
|
|
569
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
570
|
+
|
|
571
|
+
try chance.criticalHit(.P1, true, 17);
|
|
572
|
+
if (showdown) {
|
|
573
|
+
try expectValue(Optional(bool).true, chance.actions.p1.critical_hit);
|
|
574
|
+
try expectProbability(&chance.probability, 17, 256);
|
|
575
|
+
} else {
|
|
576
|
+
try expectProbability(&chance.probability, 1, 1);
|
|
577
|
+
try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
|
|
578
|
+
|
|
579
|
+
try chance.hit(.P1, true, 128);
|
|
580
|
+
|
|
581
|
+
try expectProbability(&chance.probability, 1, 1);
|
|
582
|
+
try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
|
|
583
|
+
|
|
584
|
+
try chance.damage(.P1, 217);
|
|
585
|
+
|
|
586
|
+
try expectProbability(&chance.probability, 1, 1);
|
|
587
|
+
try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
|
|
588
|
+
|
|
589
|
+
try chance.commit(.P1, .hit);
|
|
590
|
+
|
|
591
|
+
try expectProbability(&chance.probability, 17, 19968); // (1/2) * (17/256) * (1/39)
|
|
592
|
+
try expectValue(Optional(bool).true, chance.actions.p1.critical_hit);
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
chance.reset();
|
|
596
|
+
|
|
597
|
+
try chance.criticalHit(.P2, false, 5);
|
|
598
|
+
if (showdown) {
|
|
599
|
+
try expectProbability(&chance.probability, 251, 256);
|
|
600
|
+
try expectValue(Optional(bool).false, chance.actions.p2.critical_hit);
|
|
601
|
+
} else {
|
|
602
|
+
try expectProbability(&chance.probability, 1, 1);
|
|
603
|
+
try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
|
|
604
|
+
|
|
605
|
+
try chance.hit(.P1, true, 128);
|
|
606
|
+
|
|
607
|
+
try expectProbability(&chance.probability, 1, 1);
|
|
608
|
+
try expectValue(Optional(bool).None, chance.actions.p1.critical_hit);
|
|
609
|
+
|
|
610
|
+
try chance.damage(.P1, 217);
|
|
611
|
+
try chance.commit(.P1, .hit);
|
|
612
|
+
|
|
613
|
+
try expectProbability(&chance.probability, 251, 19968); // (1/2) * (251/256) * (1/39)
|
|
614
|
+
try expectValue(Optional(bool).false, chance.actions.p1.critical_hit);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
test "Chance.damage" {
|
|
619
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
620
|
+
|
|
621
|
+
try chance.damage(.P1, 217);
|
|
622
|
+
if (showdown) {
|
|
623
|
+
try expectValue(217, chance.actions.p1.damage);
|
|
624
|
+
try expectProbability(&chance.probability, 1, 39);
|
|
625
|
+
} else {
|
|
626
|
+
try expectProbability(&chance.probability, 1, 1);
|
|
627
|
+
try expectValue(0, chance.actions.p1.damage);
|
|
628
|
+
|
|
629
|
+
try chance.commit(.P1, .hit);
|
|
630
|
+
|
|
631
|
+
try expectProbability(&chance.probability, 1, 39);
|
|
632
|
+
try expectValue(217, chance.actions.p1.damage);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
test "Chance.secondaryChance" {
|
|
637
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
638
|
+
|
|
639
|
+
try chance.secondaryChance(.P1, true, 25);
|
|
640
|
+
try expectProbability(&chance.probability, 25, 256);
|
|
641
|
+
try expectValue(Optional(bool).true, chance.actions.p1.secondary_chance);
|
|
642
|
+
|
|
643
|
+
chance.reset();
|
|
644
|
+
|
|
645
|
+
try chance.secondaryChance(.P2, false, 77);
|
|
646
|
+
try expectProbability(&chance.probability, 179, 256);
|
|
647
|
+
try expectValue(Optional(bool).false, chance.actions.p2.secondary_chance);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
test "Chance.confused" {
|
|
651
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
652
|
+
|
|
653
|
+
try chance.confused(.P1, false);
|
|
654
|
+
try expectProbability(&chance.probability, 1, 2);
|
|
655
|
+
try expectValue(Optional(bool).false, chance.actions.p1.confused);
|
|
656
|
+
|
|
657
|
+
chance.reset();
|
|
658
|
+
|
|
659
|
+
try chance.confused(.P2, true);
|
|
660
|
+
try expectProbability(&chance.probability, 1, 2);
|
|
661
|
+
try expectValue(Optional(bool).true, chance.actions.p2.confused);
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
test "Chance.paralyzed" {
|
|
665
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
666
|
+
|
|
667
|
+
try chance.paralyzed(.P1, false);
|
|
668
|
+
try expectProbability(&chance.probability, 3, 4);
|
|
669
|
+
try expectValue(Optional(bool).false, chance.actions.p1.paralyzed);
|
|
670
|
+
|
|
671
|
+
chance.reset();
|
|
672
|
+
|
|
673
|
+
try chance.paralyzed(.P2, true);
|
|
674
|
+
try expectProbability(&chance.probability, 1, 4);
|
|
675
|
+
try expectValue(Optional(bool).true, chance.actions.p2.paralyzed);
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
test "Chance.moveSlot" {
|
|
679
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
680
|
+
var ms = [_]MoveSlot{ .{ .id = Move.Surf }, .{ .id = Move.Psychic }, .{ .id = Move.Recover } };
|
|
681
|
+
|
|
682
|
+
try chance.moveSlot(.P2, 2, &ms, 2);
|
|
683
|
+
try expectProbability(&chance.probability, 1, 2);
|
|
684
|
+
try expectValue(2, chance.actions.p2.move_slot);
|
|
685
|
+
|
|
686
|
+
chance.reset();
|
|
687
|
+
|
|
688
|
+
try chance.moveSlot(.P1, 1, &ms, 0);
|
|
689
|
+
try expectProbability(&chance.probability, 1, 3);
|
|
690
|
+
try expectValue(1, chance.actions.p1.move_slot);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
test "Chance.multiHit" {
|
|
694
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
695
|
+
|
|
696
|
+
try chance.multiHit(.P1, 3);
|
|
697
|
+
try expectProbability(&chance.probability, 3, 8);
|
|
698
|
+
try expectValue(3, chance.actions.p1.multi_hit);
|
|
699
|
+
|
|
700
|
+
chance.reset();
|
|
701
|
+
|
|
702
|
+
try chance.multiHit(.P2, 5);
|
|
703
|
+
try expectProbability(&chance.probability, 1, 8);
|
|
704
|
+
try expectValue(5, chance.actions.p2.multi_hit);
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
test "Chance.duration" {
|
|
708
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
709
|
+
|
|
710
|
+
chance.duration(.P1, 2);
|
|
711
|
+
try expectValue(2, chance.actions.p1.duration);
|
|
712
|
+
|
|
713
|
+
chance.reset();
|
|
714
|
+
|
|
715
|
+
chance.duration(.P2, 4);
|
|
716
|
+
try expectValue(4, chance.actions.p2.duration);
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
test "Chance.psywave" {
|
|
720
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
721
|
+
|
|
722
|
+
try chance.psywave(.P2, 100, 150);
|
|
723
|
+
try expectProbability(&chance.probability, 1, 150);
|
|
724
|
+
try expectValue(101, chance.actions.p2.psywave);
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
test "Chance.metronome" {
|
|
728
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
729
|
+
|
|
730
|
+
try chance.metronome(.P1, Move.HornAttack);
|
|
731
|
+
try expectProbability(&chance.probability, 1, 163);
|
|
732
|
+
try expectValue(Move.HornAttack, chance.actions.p1.metronome);
|
|
733
|
+
}
|
|
734
|
+
|
|
735
|
+
pub fn expectProbability(r: anytype, p: u64, q: u64) !void {
|
|
736
|
+
if (!enabled) return;
|
|
737
|
+
|
|
738
|
+
r.reduce();
|
|
739
|
+
if (r.p != p or r.q != q) {
|
|
740
|
+
print("expected {d}/{d}, found {}\n", .{ p, q, r });
|
|
741
|
+
return error.TestExpectedEqual;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
pub fn expectValue(a: anytype, b: anytype) !void {
|
|
746
|
+
if (!enabled) return;
|
|
747
|
+
|
|
748
|
+
try expectEqual(a, b);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
/// Null object pattern implementation of Generation I `Chance` which does nothing, though chance
|
|
752
|
+
/// tracking should additionally be turned off entirely via `options.chance`.
|
|
753
|
+
pub const NULL: Null = .{};
|
|
754
|
+
|
|
755
|
+
const Null = struct {
|
|
756
|
+
pub const Error = error{};
|
|
757
|
+
|
|
758
|
+
pub fn commit(self: Null, player: Player, kind: Commit) Error!void {
|
|
759
|
+
_ = .{ self, player, kind };
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
pub fn clearPending(self: Null) void {
|
|
763
|
+
_ = .{self};
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
pub fn switched(self: Null, player: Player, in: u8, out: u8) void {
|
|
767
|
+
_ = .{ self, player, in, out };
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
pub fn speedTie(self: Null, p1: bool) Error!void {
|
|
771
|
+
_ = .{ self, p1 };
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
pub fn hit(self: Null, player: Player, ok: bool, accuracy: uN) Error!void {
|
|
775
|
+
_ = .{ self, player, ok, accuracy };
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
pub fn criticalHit(self: Null, player: Player, crit: bool, rate: u8) Error!void {
|
|
779
|
+
_ = .{ self, player, crit, rate };
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
pub fn damage(self: Null, player: Player, roll: u8) Error!void {
|
|
783
|
+
_ = .{ self, player, roll };
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
pub fn confused(self: Null, player: Player, ok: bool) Error!void {
|
|
787
|
+
_ = .{ self, player, ok };
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
pub fn paralyzed(self: Null, player: Player, ok: bool) Error!void {
|
|
791
|
+
_ = .{ self, player, ok };
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
|
|
795
|
+
_ = .{ self, player, proc, rate };
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
pub fn moveSlot(self: Null, player: Player, slot: u4, ms: []const MoveSlot, n: u4) Error!void {
|
|
799
|
+
_ = .{ self, player, slot, ms, n };
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
pub fn multiHit(self: Null, player: Player, n: u3) Error!void {
|
|
803
|
+
_ = .{ self, player, n };
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
pub fn observe(
|
|
807
|
+
self: Null,
|
|
808
|
+
comptime field: Action.Field,
|
|
809
|
+
player: Player,
|
|
810
|
+
observation: Optional(Confusion),
|
|
811
|
+
) void {
|
|
812
|
+
_ = .{ self, field, player, observation };
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
pub fn duration(self: Null, player: Player, turns: u4) void {
|
|
816
|
+
_ = .{ self, player, turns };
|
|
817
|
+
}
|
|
818
|
+
|
|
819
|
+
pub fn psywave(self: Null, player: Player, power: u8, max: u8) Error!void {
|
|
820
|
+
_ = .{ self, player, power, max };
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
pub fn metronome(self: Null, player: Player, move: Move) Error!void {
|
|
824
|
+
_ = .{ self, player, move };
|
|
825
|
+
}
|
|
826
|
+
};
|