@pkmn/engine 0.1.0-dev.46307385 → 0.1.0-dev.4cc7c5c2
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/build/pkg/data/ids.json +12 -12
- package/build/pkg/data/protocol.json +69 -12
- package/build/pkg/data.d.ts +2 -2
- package/build/pkg/protocol.js +56 -37
- package/build/pkg/protocol.js.map +1 -1
- package/build.zig +7 -3
- package/package.json +20 -11
- package/src/lib/binding/node.zig +1 -1
- package/src/lib/common/data.zig +4 -16
- package/src/lib/common/optional.zig +8 -1
- package/src/lib/common/protocol.zig +518 -66
- package/src/lib/common/rational.zig +1 -1
- package/src/lib/common/rng.zig +6 -6
- package/src/lib/gen1/README.md +3 -1
- package/src/lib/gen1/calc.zig +2 -3
- package/src/lib/gen1/chance.zig +51 -35
- package/src/lib/gen1/data/moves.zig +182 -170
- package/src/lib/gen1/data/types.zig +2 -2
- package/src/lib/gen1/data.zig +11 -23
- package/src/lib/gen1/helpers.zig +13 -28
- package/src/lib/gen1/mechanics.zig +43 -52
- package/src/lib/gen2/README.md +94 -72
- package/src/lib/gen2/data/items.zig +10 -16
- package/src/lib/gen2/data/moves.zig +659 -565
- package/src/lib/gen2/data/types.zig +127 -25
- package/src/lib/gen2/data.zig +100 -63
- package/src/lib/gen2/helpers.zig +8 -12
- package/src/lib/gen2/mechanics.zig +2589 -7
|
@@ -275,7 +275,7 @@ pub const BigRational = struct {
|
|
|
275
275
|
/// Create a new BigRational wrapper. A small amount of memory will be allocated on
|
|
276
276
|
/// initialization. Not default initialized to 1 - you must explicitly `reset` first.
|
|
277
277
|
pub fn init(alloc: std.mem.Allocator) !BigRational {
|
|
278
|
-
return
|
|
278
|
+
return .{ .val = try std.math.big.Rational.init(alloc) };
|
|
279
279
|
}
|
|
280
280
|
|
|
281
281
|
/// Frees all memory associated with a rational.
|
package/src/lib/common/rng.zig
CHANGED
|
@@ -64,7 +64,7 @@ pub const PSRNG = extern struct {
|
|
|
64
64
|
};
|
|
65
65
|
|
|
66
66
|
test PSRNG {
|
|
67
|
-
var psrng =
|
|
67
|
+
var psrng: PSRNG = .{ .src = .{ .seed = 0x0001000200030004 } };
|
|
68
68
|
try expectEqual(@as(u8, 121), psrng.range(u8, 0, 256));
|
|
69
69
|
try expectEqual(false, psrng.chance(u8, 128, 256)); // 226 < 128
|
|
70
70
|
}
|
|
@@ -79,7 +79,7 @@ pub const Gen12 = extern struct {
|
|
|
79
79
|
assert(@sizeOf(Gen12) == 10);
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
pub fn percent(p:
|
|
82
|
+
pub fn percent(comptime p: comptime_int) u8 {
|
|
83
83
|
return @intCast((@as(u16, p) * 0xFF) / 100);
|
|
84
84
|
}
|
|
85
85
|
|
|
@@ -93,7 +93,7 @@ pub const Gen12 = extern struct {
|
|
|
93
93
|
|
|
94
94
|
test Gen12 {
|
|
95
95
|
const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 31, 56, 81 };
|
|
96
|
-
var rng =
|
|
96
|
+
var rng: Gen12 = .{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9 } };
|
|
97
97
|
for (expected) |e| {
|
|
98
98
|
try expectEqual(e, rng.next());
|
|
99
99
|
}
|
|
@@ -129,7 +129,7 @@ test Gen34 {
|
|
|
129
129
|
.{ 0x80000000, 5, 0x0E425287 }, .{ 0x80000000, 10, 0x6F2CF4B2 },
|
|
130
130
|
};
|
|
131
131
|
for (data) |d| {
|
|
132
|
-
var rng =
|
|
132
|
+
var rng: Gen34 = .{ .seed = d[0] };
|
|
133
133
|
for (0..d[1]) |_| _ = rng.next();
|
|
134
134
|
try expectEqual(d[2], rng.seed);
|
|
135
135
|
}
|
|
@@ -161,7 +161,7 @@ test Gen56 {
|
|
|
161
161
|
.{ 0x8000000000000000, 10, 0xE7795501267F125A },
|
|
162
162
|
};
|
|
163
163
|
for (data) |d| {
|
|
164
|
-
var rng =
|
|
164
|
+
var rng: Gen56 = .{ .seed = d[0] };
|
|
165
165
|
for (0..d[1]) |_| _ = rng.next();
|
|
166
166
|
try expectEqual(d[2], rng.seed);
|
|
167
167
|
}
|
|
@@ -213,7 +213,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
|
|
|
213
213
|
test FixedRNG {
|
|
214
214
|
const Type = if (showdown) u32 else u8;
|
|
215
215
|
const expected = [_]Type{ 42, 255, 0 };
|
|
216
|
-
var rng
|
|
216
|
+
var rng: FixedRNG(1, expected.len) = .{ .rolls = expected };
|
|
217
217
|
for (expected) |e| {
|
|
218
218
|
try expectEqual(e, rng.next());
|
|
219
219
|
}
|
package/src/lib/gen1/README.md
CHANGED
|
@@ -612,7 +612,8 @@ engine, but the following moves have their broken behavior preserved in `-Dshowd
|
|
|
612
612
|
- **Roar** / **Whirlwind**: these moves can miss on Pokémon Showdown (and advance the RNG when
|
|
613
613
|
checking) which is incorrect (these moves should always fail, but don't check accuracy or advance
|
|
614
614
|
the RNG). More importantly, these moves should *not* cause the tracked last battle damage to be
|
|
615
|
-
zeroed, but on Pokémon Showdown they do.
|
|
615
|
+
zeroed, but on Pokémon Showdown they do. These should also `|-fail|...|[still]` instead of doing
|
|
616
|
+
nothing.
|
|
616
617
|
- **Substitute**: in addition to the [Substitute + Confusion
|
|
617
618
|
glitch](https://pkmn.cc/bulba-glitch-1#Substitute_.2B_Confusion_glitch) not being implemented
|
|
618
619
|
correctly (covered earlier), the [Substitute 1/4
|
|
@@ -634,6 +635,7 @@ only pedantic UI correctness):
|
|
|
634
635
|
|
|
635
636
|
- **Rage**: Rage should report the `|-boost|` before the Disable `|-miss|`, not after.
|
|
636
637
|
- **Haze**: Haze clears the status/volatiles in an incorrect order.
|
|
638
|
+
- **Teleport**: Teleport should `|-fail|...|[still]` instead of doing nothing.
|
|
637
639
|
- **Twineedle**: the `|-hitcount|` ("Hit 2 times") should come before the `|-status|` message, not
|
|
638
640
|
after.
|
|
639
641
|
- **Thrash** / **Petal Dance**: confusion should silently get applied before the `|move|` message,
|
package/src/lib/gen1/calc.zig
CHANGED
|
@@ -29,7 +29,6 @@ const Optional = optional.Optional;
|
|
|
29
29
|
const Rational = rational.Rational;
|
|
30
30
|
|
|
31
31
|
const PointerType = util.PointerType;
|
|
32
|
-
const FieldType = util.FieldType;
|
|
33
32
|
|
|
34
33
|
const Actions = chance.Actions;
|
|
35
34
|
const Action = chance.Action;
|
|
@@ -158,7 +157,7 @@ pub const Calc = struct {
|
|
|
158
157
|
|
|
159
158
|
/// Null object pattern implementation of Generation I `Calc` which does nothing, though damage
|
|
160
159
|
/// calculator support should additionally be turned off entirely via `options.calc`.
|
|
161
|
-
pub const NULL =
|
|
160
|
+
pub const NULL: Null = .{};
|
|
162
161
|
|
|
163
162
|
const Null = struct {
|
|
164
163
|
pub fn overridden(
|
|
@@ -256,7 +255,7 @@ pub fn transitions(
|
|
|
256
255
|
|
|
257
256
|
try debug(writer, template, true, .{ .color = i, .bold = true, .background = true });
|
|
258
257
|
|
|
259
|
-
var a =
|
|
258
|
+
var a: Actions = .{ .p1 = .{ .metronome = p1_move }, .p2 = .{ .metronome = p2_move } };
|
|
260
259
|
|
|
261
260
|
for (Rolls.speedTie(template.p1)) |tie| { a.p1.speed_tie = tie; a.p2.speed_tie = tie;
|
|
262
261
|
for (Rolls.sleep(template.p1, actions.p1)) |p1_slp| { a.p1.durations.sleep = p1_slp;
|
package/src/lib/gen1/chance.zig
CHANGED
|
@@ -14,12 +14,14 @@ const util = @import("../common/util.zig");
|
|
|
14
14
|
const Player = @import("../common/data.zig").Player;
|
|
15
15
|
const Optional = @import("../common/optional.zig").Optional;
|
|
16
16
|
|
|
17
|
-
const
|
|
18
|
-
const MoveSlot = @import("data.zig").MoveSlot;
|
|
17
|
+
const data = @import("./data.zig");
|
|
19
18
|
|
|
20
19
|
const enabled = options.chance;
|
|
21
20
|
const showdown = options.showdown;
|
|
22
21
|
|
|
22
|
+
const Move = data.Move;
|
|
23
|
+
const MoveSlot = data.MoveSlot;
|
|
24
|
+
|
|
23
25
|
/// Actions taken by a hypothetical "chance player" that convey information about which RNG events
|
|
24
26
|
/// were observed during a Generation I battle `update`. This can additionally be provided as input
|
|
25
27
|
/// to the `update` call to override the normal behavior of the RNG in order to force specific
|
|
@@ -115,7 +117,7 @@ test Actions {
|
|
|
115
117
|
|
|
116
118
|
/// Information about the RNG that was observed during a Generation I battle `update` for a
|
|
117
119
|
/// single player.
|
|
118
|
-
pub const Action = packed struct {
|
|
120
|
+
pub const Action = packed struct(u64) {
|
|
119
121
|
/// If not 0, the roll to be returned Rolls.damage.
|
|
120
122
|
damage: u8 = 0,
|
|
121
123
|
|
|
@@ -156,10 +158,6 @@ pub const Action = packed struct {
|
|
|
156
158
|
|
|
157
159
|
pub const Field = std.meta.FieldEnum(Action);
|
|
158
160
|
|
|
159
|
-
comptime {
|
|
160
|
-
assert(@sizeOf(Action) == 8);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
161
|
/// Perform a reset by clearing fields which should not persist across updates.
|
|
164
162
|
pub inline fn reset(self: *Action) void {
|
|
165
163
|
self.* = @bitCast(@as(u64, @bitCast(self.*)) & DURATIONS);
|
|
@@ -219,7 +217,7 @@ pub const Action = packed struct {
|
|
|
219
217
|
};
|
|
220
218
|
|
|
221
219
|
test Action {
|
|
222
|
-
var a =
|
|
220
|
+
var a: Action = .{ .hit = .true, .durations = .{ .sleep = 3 }, .damage = 225 };
|
|
223
221
|
a.reset();
|
|
224
222
|
|
|
225
223
|
try expectEqual(Action{ .hit = .None, .durations = .{ .sleep = 3 }, .damage = 0 }, a);
|
|
@@ -239,7 +237,7 @@ pub const Durations = extern struct {
|
|
|
239
237
|
|
|
240
238
|
/// Observed values for various durations that need to be tracked in order to properly
|
|
241
239
|
/// deduplicate transitions with a primary key. TODO
|
|
242
|
-
pub const Duration = packed struct {
|
|
240
|
+
pub const Duration = packed struct(u16) {
|
|
243
241
|
/// The number of turns a Pokémon has been observed to be sleeping.
|
|
244
242
|
sleep: u3 = 0,
|
|
245
243
|
/// The number of turns a Pokémon has been observed to be confused.
|
|
@@ -252,10 +250,6 @@ pub const Duration = packed struct {
|
|
|
252
250
|
binding: u3 = 0,
|
|
253
251
|
|
|
254
252
|
pub const Field = std.meta.FieldEnum(Duration);
|
|
255
|
-
|
|
256
|
-
comptime {
|
|
257
|
-
assert(@sizeOf(Duration) == 2);
|
|
258
|
-
}
|
|
259
253
|
};
|
|
260
254
|
|
|
261
255
|
pub const Commit = enum { hit, miss, binding };
|
|
@@ -522,22 +516,6 @@ pub fn Chance(comptime Rational: type) type {
|
|
|
522
516
|
}
|
|
523
517
|
}
|
|
524
518
|
|
|
525
|
-
pub fn disable(self: *Self, player: Player, turns: u4) Error!void {
|
|
526
|
-
if (!enabled) return;
|
|
527
|
-
|
|
528
|
-
var durations = &self.actions.get(player).durations;
|
|
529
|
-
const n = durations.disable;
|
|
530
|
-
if (turns == 0) {
|
|
531
|
-
assert(n >= 1 and n <= 8);
|
|
532
|
-
if (n != 8) try self.probability.update(1, 9 - @as(u4, n));
|
|
533
|
-
durations.disable = 0;
|
|
534
|
-
} else {
|
|
535
|
-
assert(n >= 1 and n < 8);
|
|
536
|
-
try self.probability.update(9 - @as(u4, n) - 1, 9 - @as(u4, n));
|
|
537
|
-
durations.disable += 1;
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
|
|
541
519
|
pub fn confusion(self: *Self, player: Player, turns: u4) Error!void {
|
|
542
520
|
if (!enabled) return;
|
|
543
521
|
|
|
@@ -554,6 +532,22 @@ pub fn Chance(comptime Rational: type) type {
|
|
|
554
532
|
}
|
|
555
533
|
}
|
|
556
534
|
|
|
535
|
+
pub fn disable(self: *Self, player: Player, turns: u4) Error!void {
|
|
536
|
+
if (!enabled) return;
|
|
537
|
+
|
|
538
|
+
var durations = &self.actions.get(player).durations;
|
|
539
|
+
const n = durations.disable;
|
|
540
|
+
if (turns == 0) {
|
|
541
|
+
assert(n >= 1 and n <= 8);
|
|
542
|
+
if (n != 8) try self.probability.update(1, 9 - @as(u4, n));
|
|
543
|
+
durations.disable = 0;
|
|
544
|
+
} else {
|
|
545
|
+
assert(n >= 1 and n < 8);
|
|
546
|
+
try self.probability.update(9 - @as(u4, n) - 1, 9 - @as(u4, n));
|
|
547
|
+
durations.disable += 1;
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
557
551
|
pub fn attacking(self: *Self, player: Player, turns: u4) Error!void {
|
|
558
552
|
if (!enabled) return;
|
|
559
553
|
|
|
@@ -601,7 +595,7 @@ pub fn Chance(comptime Rational: type) type {
|
|
|
601
595
|
pub fn metronome(self: *Self, player: Player, move: Move) Error!void {
|
|
602
596
|
if (!enabled) return;
|
|
603
597
|
|
|
604
|
-
try self.probability.update(1, Move.
|
|
598
|
+
try self.probability.update(1, Move.METRONOME.len);
|
|
605
599
|
self.actions.get(player).metronome = move;
|
|
606
600
|
}
|
|
607
601
|
};
|
|
@@ -864,6 +858,28 @@ test "Chance.confusion" {
|
|
|
864
858
|
}
|
|
865
859
|
}
|
|
866
860
|
|
|
861
|
+
test "Chance.disable" {
|
|
862
|
+
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
863
|
+
|
|
864
|
+
for ([_]u8{ 8, 7, 6, 5, 4, 3, 2, 1 }, 1..9) |d, i| {
|
|
865
|
+
chance.actions.p1.durations.disable = @intCast(i);
|
|
866
|
+
try chance.disable(.P1, 0);
|
|
867
|
+
try expectProbability(&chance.probability, 1, d);
|
|
868
|
+
try expectValue(@as(u4, 0), chance.actions.p1.durations.disable);
|
|
869
|
+
|
|
870
|
+
chance.reset();
|
|
871
|
+
|
|
872
|
+
if (i < 8) {
|
|
873
|
+
chance.actions.p1.durations.disable = @intCast(i);
|
|
874
|
+
try chance.disable(.P1, 1);
|
|
875
|
+
try expectProbability(&chance.probability, d - 1, d);
|
|
876
|
+
try expectValue(@as(u4, @intCast(i)) + 1, chance.actions.p1.durations.disable);
|
|
877
|
+
|
|
878
|
+
chance.reset();
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
867
883
|
test "Chance.attacking" {
|
|
868
884
|
var chance: Chance(rational.Rational(u64)) = .{ .probability = .{} };
|
|
869
885
|
|
|
@@ -947,7 +963,7 @@ pub fn expectValue(a: anytype, b: anytype) !void {
|
|
|
947
963
|
|
|
948
964
|
/// Null object pattern implementation of Generation I `Chance` which does nothing, though chance
|
|
949
965
|
/// tracking should additionally be turned off entirely via `options.chance`.
|
|
950
|
-
pub const NULL =
|
|
966
|
+
pub const NULL: Null = .{};
|
|
951
967
|
|
|
952
968
|
const Null = struct {
|
|
953
969
|
pub const Error = error{};
|
|
@@ -984,10 +1000,6 @@ const Null = struct {
|
|
|
984
1000
|
_ = .{ self, player, roll };
|
|
985
1001
|
}
|
|
986
1002
|
|
|
987
|
-
pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
|
|
988
|
-
_ = .{ self, player, proc, rate };
|
|
989
|
-
}
|
|
990
|
-
|
|
991
1003
|
pub fn confused(self: Null, player: Player, ok: bool) Error!void {
|
|
992
1004
|
_ = .{ self, player, ok };
|
|
993
1005
|
}
|
|
@@ -996,6 +1008,10 @@ const Null = struct {
|
|
|
996
1008
|
_ = .{ self, player, ok };
|
|
997
1009
|
}
|
|
998
1010
|
|
|
1011
|
+
pub fn secondaryChance(self: Null, player: Player, proc: bool, rate: u8) Error!void {
|
|
1012
|
+
_ = .{ self, player, proc, rate };
|
|
1013
|
+
}
|
|
1014
|
+
|
|
999
1015
|
pub fn moveSlot(self: Null, player: Player, slot: u4, ms: []const MoveSlot, n: u4) Error!void {
|
|
1000
1016
|
_ = .{ self, player, slot, ms, n };
|
|
1001
1017
|
}
|