@pkmn/engine 0.1.0-dev.44b2e6ec → 0.1.0-dev.44c4a618
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 +13 -11
- package/build/pkg/addon.js +6 -7
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data.d.ts +4 -5
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.js +4 -4
- package/build/pkg/protocol.js.map +1 -1
- package/build/test/display.js +2 -2
- package/build/test/display.js.map +1 -1
- package/build/tools/debug.js +2 -3
- package/build/tools/debug.js.map +1 -1
- package/build.zig +67 -86
- package/package.json +29 -22
- package/src/bin/install-pkmn-engine +26 -18
- package/src/lib/common/data.zig +4 -4
- package/src/lib/common/debug.zig +2 -2
- package/src/lib/common/optional.zig +1 -1
- package/src/lib/common/options.zig +2 -0
- package/src/lib/common/protocol.zig +15 -20
- package/src/lib/common/rng.zig +11 -11
- package/src/lib/gen1/calc.zig +81 -0
- package/src/lib/gen1/chance.zig +33 -30
- package/src/lib/gen1/data/species.zig +1 -1
- package/src/lib/gen1/data.zig +21 -21
- package/src/lib/gen1/helpers.zig +102 -15
- package/src/lib/gen1/mechanics.zig +46 -8
- package/src/lib/gen2/chance.zig +38 -37
- package/src/lib/gen2/data.zig +8 -8
- package/src/lib/gen2/helpers.zig +37 -7
- package/src/lib/gen2/mechanics.zig +112 -27
- package/src/lib/{binding/node.zig → node.zig} +3 -2
- package/src/lib/pkmn.zig +27 -12
- package/src/lib/{binding/wasm.zig → wasm.zig} +2 -1
- package/src/lib/gen1/README.md +0 -894
- package/src/lib/gen2/README.md +0 -329
package/src/lib/gen2/data.zig
CHANGED
|
@@ -248,12 +248,12 @@ test DVs {
|
|
|
248
248
|
var dvs = DVs.from(.Mewtwo, .{ .def = 13 });
|
|
249
249
|
try expectEqual(Gender.Unknown, dvs.gender);
|
|
250
250
|
try expectEqual(Type.Ice, dvs.type);
|
|
251
|
-
try expectEqual(
|
|
251
|
+
try expectEqual(70, dvs.power());
|
|
252
252
|
|
|
253
253
|
dvs = DVs.from(.Pikachu, .{ .spc = 14 });
|
|
254
254
|
try expectEqual(Gender.Male, dvs.gender);
|
|
255
255
|
try expectEqual(Type.Dark, dvs.type);
|
|
256
|
-
try expectEqual(
|
|
256
|
+
try expectEqual(69, dvs.power());
|
|
257
257
|
|
|
258
258
|
dvs = DVs.from(.Cyndaquil, .{ .atk = 1, .def = 3, .spe = 10, .spc = 9 });
|
|
259
259
|
try expectEqual(Gender.Female, dvs.gender);
|
|
@@ -380,9 +380,9 @@ test Stats {
|
|
|
380
380
|
try expectEqual(0, stats.def);
|
|
381
381
|
|
|
382
382
|
var base = Species.get(.Gyarados).stats;
|
|
383
|
-
try expectEqual(
|
|
383
|
+
try expectEqual(127, Stats(u16).calc("hp", base.hp, 0, 5120, 38));
|
|
384
384
|
base = Species.get(.Pidgeot).stats;
|
|
385
|
-
try expectEqual(
|
|
385
|
+
try expectEqual(279, Stats(u16).calc("spe", base.spe, 15, 63001, 100));
|
|
386
386
|
}
|
|
387
387
|
|
|
388
388
|
pub const Boosts = packed struct(u32) {
|
|
@@ -428,8 +428,8 @@ test Move {
|
|
|
428
428
|
try expectEqual(Move.Effect.ConfusionChance, move.effect);
|
|
429
429
|
try expectEqual(rng.Gen12.percent(50), move.accuracy);
|
|
430
430
|
try expectEqual(rng.Gen12.percent(100), move.chance);
|
|
431
|
-
try expectEqual(
|
|
432
|
-
try expectEqual(
|
|
431
|
+
try expectEqual(5, Move.pp(.DynamicPunch));
|
|
432
|
+
try expectEqual(-1, Move.get(.Counter).priority);
|
|
433
433
|
try expect(!move.effect.isHighCritical());
|
|
434
434
|
try expect(Move.get(.KarateChop).effect.isHighCritical());
|
|
435
435
|
try expect(Move.get(.RazorWind).effect.isHighCritical());
|
|
@@ -439,7 +439,7 @@ pub const Species = species.Species;
|
|
|
439
439
|
|
|
440
440
|
test Species {
|
|
441
441
|
try expectEqual(152, @intFromEnum(Species.Chikorita));
|
|
442
|
-
try expectEqual(
|
|
442
|
+
try expectEqual(100, Species.get(.Celebi).stats.spd);
|
|
443
443
|
}
|
|
444
444
|
|
|
445
445
|
pub const Type = types.Type;
|
|
@@ -452,7 +452,7 @@ test Types {
|
|
|
452
452
|
try expect(!Type.Steel.special());
|
|
453
453
|
try expect(Type.Dark.special());
|
|
454
454
|
|
|
455
|
-
try expectEqual(
|
|
455
|
+
try expectEqual(3, Type.Water.precedence());
|
|
456
456
|
try expect(Type.Bug.precedence() > Type.Poison.precedence());
|
|
457
457
|
|
|
458
458
|
try expectEqual(Effectiveness.Super, Type.effectiveness(.Ghost, .Psychic));
|
package/src/lib/gen2/helpers.zig
CHANGED
|
@@ -65,17 +65,47 @@ pub const Battle = struct {
|
|
|
65
65
|
};
|
|
66
66
|
|
|
67
67
|
pub fn prng(rand: *PSRNG) data.PRNG {
|
|
68
|
-
// FIXME: what is max range in gen2?
|
|
69
68
|
return .{
|
|
70
69
|
.src = .{
|
|
71
70
|
.seed = if (showdown)
|
|
72
71
|
rand.newSeed()
|
|
73
|
-
else
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
72
|
+
else seed: {
|
|
73
|
+
// GLITCH: The initial RNG seed in Gen II link battles is heavily constrained
|
|
74
|
+
// https://www.smogon.com/forums/threads/rng-in-rby-link-battles.3746779
|
|
75
|
+
var seed = [_]u8{ 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
76
|
+
|
|
77
|
+
// Bytes in seed can only range from 0-252, not 0-255
|
|
78
|
+
const max: u8 = 253;
|
|
79
|
+
// Instructions take multiples of 4 cycles to execute
|
|
80
|
+
var timer: u16 = 4 * rand.range(u16, 0, 16384);
|
|
81
|
+
var add: u8 = rand.range(u8, 0, 256);
|
|
82
|
+
var sub: u8 = rand.range(u8, 0, 256);
|
|
83
|
+
var carry: u8 = 0;
|
|
84
|
+
var div: u8 = 0;
|
|
85
|
+
|
|
86
|
+
var i: u8 = 0;
|
|
87
|
+
while (i < seed.len) {
|
|
88
|
+
div = @intCast(timer >> 8);
|
|
89
|
+
// This is effectively @addWithOverflow but with three operands
|
|
90
|
+
const result = @as(u16, add) + @as(u16, div) + @as(u16, carry);
|
|
91
|
+
add = @intCast(result & 255);
|
|
92
|
+
carry = @intFromBool(result > 255);
|
|
93
|
+
timer +%= 44;
|
|
94
|
+
div = @intCast(timer >> 8);
|
|
95
|
+
sub -%= div +% carry;
|
|
96
|
+
|
|
97
|
+
if (sub < max) {
|
|
98
|
+
seed[i] = sub;
|
|
99
|
+
i += 1;
|
|
100
|
+
timer +%= 152;
|
|
101
|
+
carry = 1;
|
|
102
|
+
} else {
|
|
103
|
+
timer +%= 132;
|
|
104
|
+
carry = 0;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
break :seed seed;
|
|
108
|
+
},
|
|
79
109
|
},
|
|
80
110
|
};
|
|
81
111
|
}
|
|
@@ -1304,14 +1304,32 @@ pub const Effects = struct {
|
|
|
1304
1304
|
}
|
|
1305
1305
|
|
|
1306
1306
|
pub fn bellyDrum(battle: anytype, player: Player, state: *State, options: anytype) !void {
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1307
|
+
assert(!state.miss);
|
|
1308
|
+
var side = battle.side(player);
|
|
1309
|
+
|
|
1310
|
+
const ident = battle.active(player);
|
|
1311
|
+
|
|
1312
|
+
assert(side.active.boosts.atk >= -6 and side.active.boosts.atk <= 6);
|
|
1313
|
+
if (side.active.boosts.atk == 6 or side.active.boosts.atk == MAX_STAT_VALUE) {
|
|
1314
|
+
return options.log.fail(.{ ident, .None });
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
const hp = @max(side.stored().stats.hp / 2, 1);
|
|
1318
|
+
if (side.stored().hp <= hp) {
|
|
1319
|
+
try boostStat(battle, side, &side.active.stats, &side.active.boosts, "atk", 2);
|
|
1320
|
+
try options.log.boost(.{ ident, .Attack, @as(i8, 2), .Silent });
|
|
1321
|
+
return options.log.fail(.{ ident, .None });
|
|
1322
|
+
} else {
|
|
1323
|
+
side.stored().hp -= hp;
|
|
1324
|
+
try options.log.damage(.{ ident, side.stored(), .None });
|
|
1325
|
+
|
|
1326
|
+
const before = side.active.boosts.atk;
|
|
1327
|
+
var i: usize = 0;
|
|
1328
|
+
while (i < 6 and side.active.stats.atk < MAX_STAT_VALUE) : (i += 1) {
|
|
1329
|
+
try boostStat(battle, side, &side.active.stats, &side.active.boosts, "atk", 2);
|
|
1330
|
+
}
|
|
1331
|
+
try options.log.boost(.{ ident, .Attack, @as(i8, side.active.boosts.atk - before) });
|
|
1332
|
+
}
|
|
1315
1333
|
}
|
|
1316
1334
|
|
|
1317
1335
|
pub const bide = struct {
|
|
@@ -1596,8 +1614,16 @@ pub const Effects = struct {
|
|
|
1596
1614
|
try options.log.start(.{ foe_ident, .Encore, move.id });
|
|
1597
1615
|
}
|
|
1598
1616
|
|
|
1599
|
-
pub fn explode(battle: anytype, player: Player,
|
|
1600
|
-
|
|
1617
|
+
pub fn explode(battle: anytype, player: Player, _: *State, _: anytype) !void {
|
|
1618
|
+
var side = battle.side(player);
|
|
1619
|
+
var stored = side.stored();
|
|
1620
|
+
|
|
1621
|
+
stored.hp = 0;
|
|
1622
|
+
// Pokémon Showdown sets the status to 0 on faint(), and we need to do the same to be able
|
|
1623
|
+
// to correctly implement Pokémon Showdown's dumb speed-based switch mechanics
|
|
1624
|
+
if (!showdown) stored.status = 0;
|
|
1625
|
+
side.active.volatiles.LeechSeed = false;
|
|
1626
|
+
side.active.volatiles.DestinyBond = false;
|
|
1601
1627
|
}
|
|
1602
1628
|
|
|
1603
1629
|
pub fn falseSwipe(battle: anytype, player: Player, state: *State, _: anytype) !void {
|
|
@@ -1723,8 +1749,26 @@ pub const Effects = struct {
|
|
|
1723
1749
|
foe.active.volatiles.frozen = true;
|
|
1724
1750
|
}
|
|
1725
1751
|
|
|
1726
|
-
|
|
1727
|
-
|
|
1752
|
+
// TODO FIXME showdown calls this before calcDamage (after checkHit) and modifies state.bp!
|
|
1753
|
+
pub fn furyCutter(battle: anytype, player: Player, state: *State, _: anytype) !void {
|
|
1754
|
+
var side = battle.side(player);
|
|
1755
|
+
var volatiles = &side.active.volatiles;
|
|
1756
|
+
|
|
1757
|
+
if (state.miss) {
|
|
1758
|
+
volatiles.fury_cutter = 0;
|
|
1759
|
+
return;
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
assert(volatiles.fury_cutter >= 0 and volatiles.fury_cutter <= 5);
|
|
1763
|
+
if (volatiles.fury_cutter == 5) return;
|
|
1764
|
+
volatiles.fury_cutter += 1;
|
|
1765
|
+
|
|
1766
|
+
if (showdown) {
|
|
1767
|
+
state.bp *= 2;
|
|
1768
|
+
assert(state.bp <= 160);
|
|
1769
|
+
} else {
|
|
1770
|
+
state.damage *|= 2;
|
|
1771
|
+
}
|
|
1728
1772
|
}
|
|
1729
1773
|
|
|
1730
1774
|
pub fn futureSight(battle: anytype, player: Player, state: *State, options: anytype) !void {
|
|
@@ -1885,7 +1929,29 @@ pub const Effects = struct {
|
|
|
1885
1929
|
}
|
|
1886
1930
|
|
|
1887
1931
|
pub fn mimic(battle: anytype, player: Player, state: *State, options: anytype) !void {
|
|
1888
|
-
|
|
1932
|
+
var side = battle.side(player);
|
|
1933
|
+
|
|
1934
|
+
if (state.miss) {
|
|
1935
|
+
if (!showdown) return options.log.fail(.{ battle.active(player.foe()), .None });
|
|
1936
|
+
try options.log.lastmiss(.{});
|
|
1937
|
+
return options.log.miss(.{battle.active(player)});
|
|
1938
|
+
}
|
|
1939
|
+
|
|
1940
|
+
const move = battle.foe(player).lastMove(false);
|
|
1941
|
+
if (move == .None or move == .Struggle) {
|
|
1942
|
+
return options.log.fail(.{ battle.active(player.foe()), .None });
|
|
1943
|
+
}
|
|
1944
|
+
|
|
1945
|
+
var slot: u4 = 0;
|
|
1946
|
+
for (side.active.moves, 0..) |m, i| {
|
|
1947
|
+
if (m.id == move) return options.log.fail(.{ battle.active(player.foe()), .None });
|
|
1948
|
+
if (m.id == .Mimic) slot = @intCast(i + 1);
|
|
1949
|
+
}
|
|
1950
|
+
assert(slot != 0 and slot == state.mslot);
|
|
1951
|
+
|
|
1952
|
+
side.active.move(slot).id = move;
|
|
1953
|
+
side.active.move(slot).pp = 5;
|
|
1954
|
+
try options.log.start(.{ battle.active(player), .Mimic, move });
|
|
1889
1955
|
}
|
|
1890
1956
|
|
|
1891
1957
|
pub fn mirrorMove(
|
|
@@ -2393,17 +2459,37 @@ pub const Effects = struct {
|
|
|
2393
2459
|
}
|
|
2394
2460
|
|
|
2395
2461
|
pub fn spite(battle: anytype, player: Player, state: *State, options: anytype) !void {
|
|
2396
|
-
|
|
2397
|
-
// var foe = battle.foe(player);
|
|
2462
|
+
var foe = battle.foe(player);
|
|
2398
2463
|
|
|
2399
|
-
|
|
2464
|
+
const last = foe.lastMove(false);
|
|
2465
|
+
if (state.miss or last == .None or last == .Struggle) {
|
|
2466
|
+
return options.log.fail(.{ battle.active(player.foe()), .None });
|
|
2467
|
+
}
|
|
2400
2468
|
|
|
2401
|
-
|
|
2402
|
-
|
|
2403
|
-
|
|
2404
|
-
|
|
2405
|
-
//
|
|
2406
|
-
|
|
2469
|
+
var slot: u4 = 0;
|
|
2470
|
+
for (foe.active.moves, 0..) |m, i| {
|
|
2471
|
+
if (m.id == last) slot = @intCast(i + 1);
|
|
2472
|
+
}
|
|
2473
|
+
// If the move was called via Metronome it might not exist
|
|
2474
|
+
if (slot == 0 or foe.active.move(slot).pp == 0) {
|
|
2475
|
+
return options.log.fail(.{ battle.active(player.foe()), .None });
|
|
2476
|
+
}
|
|
2477
|
+
|
|
2478
|
+
// Pokémon Showdown always deducts 4 instead of randomizing
|
|
2479
|
+
const pp = @min(
|
|
2480
|
+
foe.active.move(slot).pp,
|
|
2481
|
+
if (showdown) 4 else try Rolls.spite(battle, player, options),
|
|
2482
|
+
);
|
|
2483
|
+
foe.active.move(slot).pp -= pp;
|
|
2484
|
+
if (!foe.active.volatiles.Transform) {
|
|
2485
|
+
var move_slot = foe.stored().move(slot);
|
|
2486
|
+
assert(move_slot.pp > 0);
|
|
2487
|
+
|
|
2488
|
+
move_slot.pp = @as(u6, @intCast(move_slot.pp)) -% 1;
|
|
2489
|
+
assert(foe.active.move(slot).pp == foe.stored().move(slot).pp);
|
|
2490
|
+
}
|
|
2491
|
+
|
|
2492
|
+
try options.log.activate(.{ battle.active(player.foe()), .Spite, last, pp });
|
|
2407
2493
|
}
|
|
2408
2494
|
|
|
2409
2495
|
pub fn splash(battle: anytype, player: Player, _: *State, options: anytype) !void {
|
|
@@ -3009,13 +3095,12 @@ pub const Rolls = struct {
|
|
|
3009
3095
|
return hits;
|
|
3010
3096
|
}
|
|
3011
3097
|
|
|
3012
|
-
fn spite(battle: anytype, player: Player, options: anytype) !
|
|
3013
|
-
|
|
3098
|
+
fn spite(battle: anytype, player: Player, options: anytype) !u3 {
|
|
3099
|
+
assert(!showdown);
|
|
3100
|
+
const pp: u3 = if (options.calc.overridden(player, .spite)) |val|
|
|
3014
3101
|
val + 1
|
|
3015
|
-
else if (showdown)
|
|
3016
|
-
battle.rng.range(u8, 2, 6)
|
|
3017
3102
|
else
|
|
3018
|
-
(battle.rng.next() & 3) + 2;
|
|
3103
|
+
@intCast((battle.rng.next() & 3) + 2);
|
|
3019
3104
|
|
|
3020
3105
|
assert(pp >= 2 and pp <= 5);
|
|
3021
3106
|
try options.chance.spite(player, pp);
|
package/src/lib/pkmn.zig
CHANGED
|
@@ -26,9 +26,7 @@ pub const Result = @import("common/data.zig").Result;
|
|
|
26
26
|
pub const PSRNG = @import("common/rng.zig").PSRNG;
|
|
27
27
|
|
|
28
28
|
/// Namespace for helpers related to protocol message logging.
|
|
29
|
-
pub const protocol = if (options.internal) struct {
|
|
30
|
-
pub usingnamespace @import("common/protocol.zig");
|
|
31
|
-
} else struct {
|
|
29
|
+
pub const protocol = if (options.internal) @import("common/protocol.zig") else struct {
|
|
32
30
|
/// Logs protocol information to its `Writer` during a battle update when
|
|
33
31
|
/// `options.log` is enabled.
|
|
34
32
|
pub const Log = @import("common/protocol.zig").Log;
|
|
@@ -82,16 +80,14 @@ pub const gen1 = struct {
|
|
|
82
80
|
/// via `overrides` and tracks `summaries` of information relevant to damage calculation.
|
|
83
81
|
pub const Calc = @import("gen1/calc.zig").Calc;
|
|
84
82
|
/// Namespace for types associated with supported Generation I Pokémon damage calc features.
|
|
85
|
-
pub const calc = if (options.internal) struct {
|
|
86
|
-
pub usingnamespace @import("gen1/calc.zig");
|
|
87
|
-
} else struct {
|
|
83
|
+
pub const calc = if (options.internal) @import("gen1/calc.zig") else struct {
|
|
88
84
|
/// TODO
|
|
89
85
|
pub const Overrides = @import("gen1/calc.zig").Overrides;
|
|
90
86
|
/// Information relevant to damage calculation that occured during a Generation I
|
|
91
87
|
/// battle `update`.
|
|
92
88
|
pub const Summaries = @import("gen1/calc.zig").Summaries;
|
|
93
89
|
/// Information relevant to damage calculation that occured during a Generation I
|
|
94
|
-
///
|
|
90
|
+
/// battle `update` for a single player.
|
|
95
91
|
pub const Summary = @import("gen1/calc.zig").Summary;
|
|
96
92
|
/// Null object pattern implementation of Generation I `Calc` which does nothing,
|
|
97
93
|
/// though damage calculator support should additionally be turned off
|
|
@@ -102,12 +98,31 @@ pub const gen1 = struct {
|
|
|
102
98
|
pub const helpers = @import("gen1/helpers.zig");
|
|
103
99
|
};
|
|
104
100
|
|
|
101
|
+
/// TODO
|
|
102
|
+
pub const gen2 = struct {
|
|
103
|
+
pub usingnamespace @import("gen2/data.zig");
|
|
104
|
+
pub const Chance = @import("gen2/chance.zig").Chance;
|
|
105
|
+
pub const chance = struct {
|
|
106
|
+
pub const Actions = @import("gen2/chance.zig").Actions;
|
|
107
|
+
pub const Action = @import("gen2/chance.zig").Action;
|
|
108
|
+
pub const Durations = @import("gen2/chance.zig").Durations;
|
|
109
|
+
pub const Duration = @import("gen2/chance.zig").Duration;
|
|
110
|
+
pub const NULL = @import("gen2/chance.zig").NULL;
|
|
111
|
+
};
|
|
112
|
+
pub const Calc = @import("gen2/calc.zig").Calc;
|
|
113
|
+
pub const calc = if (options.internal) @import("gen2/calc.zig") else struct {
|
|
114
|
+
pub const Overrides = @import("gen2/calc.zig").Overrides;
|
|
115
|
+
pub const Summaries = @import("gen2/calc.zig").Summaries;
|
|
116
|
+
pub const Summary = @import("gen2/calc.zig").Summary;
|
|
117
|
+
pub const NULL = @import("gen2/calc.zig").NULL;
|
|
118
|
+
};
|
|
119
|
+
pub const helpers = @import("gen2/helpers.zig");
|
|
120
|
+
};
|
|
121
|
+
|
|
105
122
|
// Internal APIs used by other pkmn libraries, not actually part of the public API.
|
|
106
123
|
// NOTE: `pub usingnamespace struct { ... }` here results in a (false?) dependency loop
|
|
107
|
-
pub const js = if (options.internal) struct {
|
|
108
|
-
pub usingnamespace @import("common/js.zig");
|
|
109
|
-
} else struct {};
|
|
124
|
+
pub const js = if (options.internal) @import("common/js.zig") else struct {};
|
|
110
125
|
pub const bindings = if (options.internal) struct {
|
|
111
|
-
pub const node = @import("
|
|
112
|
-
pub const wasm = @import("
|
|
126
|
+
pub const node = @import("node.zig");
|
|
127
|
+
pub const wasm = @import("wasm.zig");
|
|
113
128
|
} else struct {};
|