@pkmn/engine 0.1.0-dev.24867658 → 0.1.0-dev.2c13d3ec
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/README.md +11 -8
- package/build/pkg/addon.js +3 -2
- package/build/pkg/addon.js.map +1 -1
- package/build/pkg/data/layout.json +24 -2
- package/build/pkg/gen1.js +13 -16
- package/build/pkg/gen1.js.map +1 -1
- package/build/pkg/index.js.map +1 -1
- package/build/pkg/protocol.d.ts +4 -2
- package/build/pkg/protocol.js +4 -2
- package/build/pkg/protocol.js.map +1 -1
- package/build.zig +36 -21
- package/package.json +14 -14
- package/src/bin/install-pkmn-engine +10 -5
- package/src/lib/binding/node.zig +45 -140
- package/src/lib/binding/wasm.zig +4 -2
- package/src/lib/common/data.zig +11 -11
- package/src/lib/common/debug.zig +20 -11
- package/src/lib/common/options.zig +9 -0
- package/src/lib/common/protocol.zig +201 -140
- package/src/lib/common/rng.zig +10 -10
- package/src/lib/gen1/README.md +20 -17
- package/src/lib/gen1/data/moves.zig +10 -10
- package/src/lib/gen1/data/species.zig +2 -2
- package/src/lib/gen1/data/types.zig +3 -3
- package/src/lib/gen1/data.zig +51 -32
- package/src/lib/gen1/helpers.zig +436 -18
- package/src/lib/gen1/mechanics.zig +765 -464
- package/src/lib/gen2/data/items.zig +3 -3
- package/src/lib/gen2/data/moves.zig +1 -1
- package/src/lib/gen2/data/species.zig +1 -1
- package/src/lib/gen2/data/types.zig +3 -3
- package/src/lib/gen2/data.zig +33 -22
- package/src/lib/gen2/helpers.zig +9 -11
- package/src/lib/gen2/mechanics.zig +5 -4
- package/src/lib/pkmn.zig +61 -2
package/src/lib/common/rng.zig
CHANGED
|
@@ -47,7 +47,7 @@ pub const PSRNG = extern struct {
|
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
pub fn range(self: *PSRNG, comptime T: type, from: T, to: Bound(T)) T {
|
|
50
|
-
return @intCast(
|
|
50
|
+
return @intCast(@as(u64, self.src.next()) * (to - from) / divisor + from);
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
pub fn chance(self: *PSRNG, comptime T: type, numerator: T, denominator: Bound(T)) bool {
|
|
@@ -72,28 +72,28 @@ test PSRNG {
|
|
|
72
72
|
// https://pkmn.cc/pokered/engine/battle/core.asm#L6644-L6693
|
|
73
73
|
// https://pkmn.cc/pokecrystal/engine/battle/core.asm#L6922-L6938
|
|
74
74
|
pub const Gen12 = extern struct {
|
|
75
|
-
seed: [
|
|
75
|
+
seed: [9]u8,
|
|
76
76
|
index: u8 = 0,
|
|
77
77
|
|
|
78
78
|
comptime {
|
|
79
|
-
assert(@sizeOf(Gen12) ==
|
|
79
|
+
assert(@sizeOf(Gen12) == 10);
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
pub fn percent(p: u8) u8 {
|
|
83
|
-
return @intCast(
|
|
83
|
+
return @intCast((@as(u16, p) * 0xFF) / 100);
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
pub fn next(self: *Gen12) u8 {
|
|
87
87
|
const val = 5 *% self.seed[self.index] +% 1;
|
|
88
88
|
self.seed[self.index] = val;
|
|
89
|
-
self.index = (self.index + 1) %
|
|
89
|
+
self.index = (self.index + 1) % 9;
|
|
90
90
|
return val;
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
test Gen12 {
|
|
95
|
-
const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46,
|
|
96
|
-
var rng = Gen12{ .seed = .{ 1, 2, 3, 4, 5, 6, 7, 8, 9
|
|
95
|
+
const expected = [_]u8{ 6, 11, 16, 21, 26, 31, 36, 41, 46, 31, 56, 81 };
|
|
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
|
}
|
|
@@ -114,7 +114,7 @@ pub const Gen34 = extern struct {
|
|
|
114
114
|
|
|
115
115
|
pub fn next(self: *Gen34) u16 {
|
|
116
116
|
self.advance();
|
|
117
|
-
return @intCast(
|
|
117
|
+
return @intCast(self.seed >> 16);
|
|
118
118
|
}
|
|
119
119
|
|
|
120
120
|
pub fn advance(self: *Gen34) void {
|
|
@@ -144,7 +144,7 @@ pub const Gen56 = extern struct {
|
|
|
144
144
|
|
|
145
145
|
pub fn next(self: *Gen56) u32 {
|
|
146
146
|
self.advance();
|
|
147
|
-
return @intCast(
|
|
147
|
+
return @intCast(self.seed >> 32);
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
pub fn advance(self: *Gen56) void {
|
|
@@ -190,7 +190,7 @@ pub fn FixedRNG(comptime gen: comptime_int, comptime len: usize) type {
|
|
|
190
190
|
|
|
191
191
|
pub fn range(self: *Self, comptime T: type, from: T, to: Bound(T)) T {
|
|
192
192
|
assert(showdown);
|
|
193
|
-
return @intCast(
|
|
193
|
+
return @intCast(@as(u64, self.next()) * (to - from) / divisor + from);
|
|
194
194
|
}
|
|
195
195
|
|
|
196
196
|
pub fn chance(
|
package/src/lib/gen1/README.md
CHANGED
|
@@ -286,9 +286,9 @@ padding](https://en.wikipedia.org/wiki/Data_structure_alignment) and
|
|
|
286
286
|
- the active Pokémon's level and current and max HP can always be referred to the `Pokemon`
|
|
287
287
|
struct
|
|
288
288
|
- **`Side`**: `ActivePokemon` + 6× `Pokemon` + active (`3`) + last used (`8`) + last selected
|
|
289
|
-
(`8`)
|
|
289
|
+
(`8`) + counterable (`1`) + last move index (`3`)
|
|
290
290
|
- `order` does not need to be stored as the party can always be rearranged as switches occur
|
|
291
|
-
- **`Battle`**: 6× `Side` + seed (
|
|
291
|
+
- **`Battle`**: 6× `Side` + seed (9× `8` + `4`) + turn (`10`) + last damage (`10`)
|
|
292
292
|
- **`Type.CHART`**: attacking types (`15`) × defending types (`15`) × effectiveness (`2`)[^2]
|
|
293
293
|
- **`Type.PRECEDENCE`**: 29× attacking type (`4`) + defending type (`4`) pairs
|
|
294
294
|
- **`Moves.DATA`**: 165× base power (`6`) + effect (`7`) + accuracy (`4`) + type (`4`) + target
|
|
@@ -299,8 +299,8 @@ padding](https://en.wikipedia.org/wiki/Data_structure_alignment) and
|
|
|
299
299
|
| ----------------- | ----------- | ------------ | -------- |
|
|
300
300
|
| `Pokemon` | 192 | 147 | 30.6% |
|
|
301
301
|
| `ActivePokemon` | 256 | 202 | 26.7% |
|
|
302
|
-
| `Side` | 1472 |
|
|
303
|
-
| `Battle` | 3088 |
|
|
302
|
+
| `Side` | 1472 | 1107 | 33.0% |
|
|
303
|
+
| `Battle` | 3088 | 2310 | 33.7% |
|
|
304
304
|
| `Type.CHART` | 1800 | 450 | 300.0% |
|
|
305
305
|
| `Type.PRECEDENCE` | 232 | 232 | 0.0% |
|
|
306
306
|
| `Moves.DATA` | 5280 | 3960 | 33.3% |
|
|
@@ -348,8 +348,8 @@ data in the browser for ease of debugging.
|
|
|
348
348
|
| 184 | 368 | [`sides[1]`](#side) | Player 2's side |
|
|
349
349
|
| 368 | 370 | `turn` | The current turn number |
|
|
350
350
|
| 370 | 372 | `last_damage` | The last damage dealt by either side |
|
|
351
|
-
| 372 |
|
|
352
|
-
|
|
|
351
|
+
| 372 | 374-376 | `last_moves` | The slot index of the last selected moves for each side |
|
|
352
|
+
| 374-376 | 384 | `rng` | The RNG state |
|
|
353
353
|
|
|
354
354
|
- the current `turn` is 2 bytes, written in native-endianness
|
|
355
355
|
- `last_moves` layout depends on whether or not Pokémon Showdown compatibility mode is enabled
|
|
@@ -359,13 +359,13 @@ data in the browser for ease of debugging.
|
|
|
359
359
|
- if `showdown` is enabled byte 372 is used to store the last selected move index of `side[0]` and
|
|
360
360
|
byte 373 is used to store whether or not the side's last executed move would have been considered
|
|
361
361
|
"counterable"; and bytes 374 and 375 store the same information for `side[1]`
|
|
362
|
-
- otherwise the first
|
|
363
|
-
next
|
|
364
|
-
with the same information for `side[1]`'s being stored in the second
|
|
362
|
+
- otherwise the first 4 bits of byte 372 stores the last selected move index of `side[0]` and the
|
|
363
|
+
next 4 bits store whether that side's last executed move would have been considered
|
|
364
|
+
"counterable", with the same information for `side[1]`'s being stored in the second byte
|
|
365
365
|
- the `rng` depends on whether or not Pokémon Showdown compatibility mode is enabled (`-Dshowdown`):
|
|
366
366
|
- if `showdown` is enabled, the RNG state begins on byte 376 and consists of a 64-bit seed,
|
|
367
367
|
written in native-endianness
|
|
368
|
-
- otherwise the RNG state begins on byte
|
|
368
|
+
- otherwise the RNG state begins on byte 374 and consists of the 9 bytes of the seed followed by
|
|
369
369
|
the index pointing to which byte of the seed is currently being used
|
|
370
370
|
|
|
371
371
|
### `Side`
|
|
@@ -497,7 +497,9 @@ the pkmn engine, but the following moves have their broken behavior preserved in
|
|
|
497
497
|
damage grow unbounded. Additionally, if the opponent faints after Bide inflicts damage on Pokémon
|
|
498
498
|
Showdown residual damage incorrectly still gets applied to Bide's user.
|
|
499
499
|
- **Counter**: On Pokémon Showdown choices made while sleeping (which should not have been
|
|
500
|
-
registered) can erroneously cause Counter to trigger Desync Clause Mod behavior.
|
|
500
|
+
registered) can erroneously cause Counter to trigger Desync Clause Mod behavior. Additionally,
|
|
501
|
+
because Pokémon Showdown neglects to zero the last battle damage if a move misses due to immunity
|
|
502
|
+
or invulnerability Counter will occasionally work on Pokémon Showdown when it should fail.
|
|
501
503
|
- **Leech Seed**: Leech Seed fails to heal its source side if a seeded target faints due to
|
|
502
504
|
recoil/crash damage on Pokémon Showdown.
|
|
503
505
|
- **Pay Day**: Pay Day should still scatter coins if it hits (but doesn't break) and opponent's
|
|
@@ -626,7 +628,8 @@ the correct control flow):
|
|
|
626
628
|
because Pokémon Showdown deducts PP based on a move's name instead of slot, if Mimic copies a move
|
|
627
629
|
the Pokémon already knows, PP deduction for using either the original move of the mimicked move
|
|
628
630
|
will instead deduct PP for whichever appears at the lower move slot index and the PP will be
|
|
629
|
-
allowed to go negative (effectively allowing for infinite PP use).
|
|
631
|
+
allowed to go negative (effectively allowing for infinite PP use). Pokémon Showdown also doesn't
|
|
632
|
+
deduct PP from using Transform if it was copied by Mimic.
|
|
630
633
|
- **Mirror Move**: As covered above, both Substitute and Binding moves misbehave when used via
|
|
631
634
|
Mirror Move (though Pokémon Showdown has its own weird behavior and does not implement the
|
|
632
635
|
[partial trapping move Mirror Move
|
|
@@ -664,7 +667,7 @@ the pkmn engine aims to be as compatible with Pokémon Showdown as possible when
|
|
|
664
667
|
compatibility mode, the implications of these differences are outlined below:
|
|
665
668
|
|
|
666
669
|
- **RNG**: Pokémon Showdown uses the RNG from Generation V & VI in every generation, despite
|
|
667
|
-
the seeds and algorithm being different. Pokémon Red uses a simple 8-bit RNG with
|
|
670
|
+
the seeds and algorithm being different. Pokémon Red uses a simple 8-bit RNG with 9 distinct
|
|
668
671
|
seeds generated when the link connection is established, whereas Pokémon Showdown uses a 64-bit
|
|
669
672
|
RNG with a 32-bit output.
|
|
670
673
|
- **Algorithm**: As detailed in the table below, the algorithm used by Pokémon Showdown in the
|
|
@@ -699,9 +702,9 @@ compatibility mode, the implications of these differences are outlined below:
|
|
|
699
702
|
when they are not relevant:
|
|
700
703
|
- Roar / Whirlwind roll to hit and can "miss" as opposed to simply failing
|
|
701
704
|
|
|
702
|
-
Finally, the **initial
|
|
705
|
+
Finally, the **initial 9-byte seed for link battles on Pokémon Red cannot include bytes larger than
|
|
703
706
|
the `SERIAL_PREAMBLE_BYTE`**, so must be in the range $\left[0, 252\right]$. This has implications
|
|
704
|
-
on the first
|
|
707
|
+
on the first 9 random numbers generated during the battle and has [**non-trivial competitive
|
|
705
708
|
implications**](https://www.smogon.com/forums/posts/9068411/show) (at the start of the battle move
|
|
706
709
|
effects become more likely, the ["1/256-miss" glitch](https://glitchcity.wiki/1/256_miss_glitch)
|
|
707
710
|
cannot happen, Player 1 is more likely to win speed ties, etc) that Pokémon Showdown cannot
|
|
@@ -725,7 +728,7 @@ All of places in the link battle code where randomness is required are outlined
|
|
|
725
728
|
| **Paralysis** (full) | `beforeMove` | Trigger if $X < 63$ |
|
|
726
729
|
| **Poison** (chance) | `Effects.poison` | Trigger if $X < 52$ ($103$ for Smog / Sludge) |
|
|
727
730
|
| **Sleep** (duration) | `Effects.sleep` | <dl><dt>Pokémon Red</dt><dd>Continue generating until $X \land 7 \neq 0$</dd><dt>Pokémon Showdown</dt><dd>Generate $X \in \left[1, 8\right)$</dd></dl> |
|
|
728
|
-
| **Bide** (duration) | `Effects.bide` | <dl><dt>Pokémon Red</dt><dd>Last for $\left(X \land 1\right) + 2$ turns</dd><dt>Pokémon Showdown</dt><dd>Last for $X \in \left[2, 4\right)$ turns</dd></dl>
|
|
731
|
+
| **Bide** (duration) | `Effects.bide` | <dl><dt>Pokémon Red</dt><dd>Last for $\left(X \land 1\right) + 2$ turns</dd><dt>Pokémon Showdown</dt><dd>Last for $X \in \left[2, 4\right)$ turns</dd></dl> |
|
|
729
732
|
| **Disable** (move) | `Effects.disable` | <dl><dt>Pokémon Red</dt><dd>Continue generating until $X \land 3$ is the index of a non-empty move slot</dd><dt>Pokémon Showdown</dt><dd>Generate $X \in \left[0, \|movesSlots\|\right)$</dd></dl> |
|
|
730
733
|
| **Disable** (duration) | `Effects.disable` | <dl><dt>Pokémon Red</dt><dd>Last for $\left(X \land 7\right) + 1$ turns</dd><dt>Pokémon Showdown</dt><dd>Last for $X \in \left[1, 9\right)$ turns</dd></dl> |
|
|
731
734
|
| **Metronome** (move) | `metronome` | <dl><dt>Pokémon Red</dt><dd>Continue generating until $X$ matches the index of a move which is not Struggle or Metronome</dd><dt>Pokémon Showdown</dt><dd>Generate $X \in \left[0, \|validMoves\|\right)$, where $validMoves$ is ordered set of moves with Struggle and Metronome removed <i>(indexes of moves > Metronome will be shifted down)</i></dd></dl> |
|
|
@@ -733,7 +736,7 @@ All of places in the link battle code where randomness is required are outlined
|
|
|
733
736
|
| **Psywave** (power) | `specialDamage` | <dl><dt>Pokémon Red</dt><dd>Continue generating until $X < {3\over2} \cdot level$</dd><dt>Pokémon Showdown</dt><dd>Generate $X \in \left[0, {3\over2} \cdot level\right)$</dd></dl> |
|
|
734
737
|
| **Thrash** (rampage) | `Effects.thrash` | <dl><dt>Pokémon Red</dt><dd>Last for $\left(X \land 1\right) + 2$ turns</dd><dt>Pokémon Showdown</dt><dd>Last for $X \in \left[2, 4\right)$ turns</dd></dl> |
|
|
735
738
|
| **Thrash** (confusion) | `beforeMove` | <dl><dt>Pokémon Red</dt><dd>Last for $\left(X \land 3\right) + 2$ turns</dd><dt>Pokémon Showdown</dt><dd>Last for $X \in \left[2, 6\right)$ turns</dd></dl> |
|
|
736
|
-
| **Binding** (duration) | `Effects.binding` | <dl><dt>Pokémon Red</dt><dd>Last for $\left(X \land 3\right) + 1$ further turns if $\left(X \land 3\right) < 2$<br /> otherwise last for $\left(Y \land 3\right) + 1$ further turns (reroll)</dd><dt>Pokémon Showdown</dt><dd>Last for $X \in \{2, 2, 3, 3, 4, 5\} - 1$ further turns</dd></dl> |
|
|
739
|
+
| **Binding** (duration) | `Effects.binding` | <dl><dt>Pokémon Red</dt><dd>Last for $\left(X \land 3\right) + 1$ further turns if $\left(X \land 3\right) < 2$<br /> otherwise last for $\left(Y \land 3\right) + 1$ further turns (reroll)</dd><dt>Pokémon Showdown</dt><dd>Last for $X \in \{2, 2, 2, 3, 3, 3, 4, 5 \} - 1$ further turns</dd></dl> |
|
|
737
740
|
| **Multi-Hit** (hits) | `Effects.multiHit` | *Ibid.* |
|
|
738
741
|
| **Unboost** (chance) | `Effects.unboost` | Trigger if $X < 85$ |
|
|
739
742
|
|
|
@@ -1602,44 +1602,44 @@ pub const Move = enum(u8) {
|
|
|
1602
1602
|
|
|
1603
1603
|
/// Whether this effect activates during the "begin" step of move execution.
|
|
1604
1604
|
pub inline fn onBegin(effect: Effect) bool {
|
|
1605
|
-
return @
|
|
1605
|
+
return @intFromEnum(effect) > 0 and @intFromEnum(effect) <= 16;
|
|
1606
1606
|
}
|
|
1607
1607
|
|
|
1608
1608
|
/// Whether this effect lowers stats.
|
|
1609
1609
|
pub inline fn isStatDown(effect: Effect) bool {
|
|
1610
|
-
return @
|
|
1610
|
+
return @intFromEnum(effect) > 16 and @intFromEnum(effect) <= 21;
|
|
1611
1611
|
}
|
|
1612
1612
|
|
|
1613
1613
|
/// Whether this effect activates during the "end" step of move execution.
|
|
1614
1614
|
pub inline fn onEnd(effect: Effect) bool {
|
|
1615
|
-
return @
|
|
1615
|
+
return @intFromEnum(effect) > 16 and @intFromEnum(effect) <= 31;
|
|
1616
1616
|
}
|
|
1617
1617
|
|
|
1618
1618
|
/// Whether this effect is considered to "always happen".
|
|
1619
1619
|
pub inline fn alwaysHappens(effect: Effect) bool {
|
|
1620
|
-
return @
|
|
1620
|
+
return @intFromEnum(effect) > 31 and @intFromEnum(effect) <= 38;
|
|
1621
1621
|
}
|
|
1622
1622
|
|
|
1623
1623
|
/// Whether this effect is handled specially by the engine.
|
|
1624
1624
|
pub inline fn isSpecial(effect: Effect) bool {
|
|
1625
1625
|
// NB: isSpecial includes isMulti up to Twineedle
|
|
1626
|
-
return @
|
|
1626
|
+
return @intFromEnum(effect) > 31 and @intFromEnum(effect) <= 46;
|
|
1627
1627
|
}
|
|
1628
1628
|
|
|
1629
1629
|
/// Whether this effect is a multi-hit effect.
|
|
1630
1630
|
pub inline fn isMulti(effect: Effect) bool {
|
|
1631
|
-
return @
|
|
1631
|
+
return @intFromEnum(effect) > 44 and @intFromEnum(effect) <= 47;
|
|
1632
1632
|
}
|
|
1633
1633
|
|
|
1634
1634
|
/// Whether this effect is has chance of lowering stats.
|
|
1635
1635
|
pub inline fn isStatDownChance(effect: Effect) bool {
|
|
1636
|
-
return @
|
|
1636
|
+
return @intFromEnum(effect) > 47 and @intFromEnum(effect) <= 51;
|
|
1637
1637
|
}
|
|
1638
1638
|
|
|
1639
1639
|
/// Whether this effect has a secondary chance.
|
|
1640
1640
|
pub inline fn isSecondaryChance(effect: Effect) bool {
|
|
1641
1641
|
// NB: isSecondaryChance includes isStatDownChance as well as Twineedle
|
|
1642
|
-
return (@
|
|
1642
|
+
return (@intFromEnum(effect) > 46 and @intFromEnum(effect) <= 61);
|
|
1643
1643
|
}
|
|
1644
1644
|
};
|
|
1645
1645
|
|
|
@@ -1843,12 +1843,12 @@ pub const Move = enum(u8) {
|
|
|
1843
1843
|
/// Returns the `Data` corresponding to the move.
|
|
1844
1844
|
pub inline fn get(id: Move) Data {
|
|
1845
1845
|
assert(id != .None and id != .SKIP_TURN);
|
|
1846
|
-
return DATA[@
|
|
1846
|
+
return DATA[@intFromEnum(id) - 1];
|
|
1847
1847
|
}
|
|
1848
1848
|
|
|
1849
1849
|
/// Returns the base PP of the move.
|
|
1850
1850
|
pub fn pp(id: Move) u8 {
|
|
1851
1851
|
assert(id != .None);
|
|
1852
|
-
return PP[@
|
|
1852
|
+
return PP[@intFromEnum(id) - 1];
|
|
1853
1853
|
}
|
|
1854
1854
|
};
|
|
@@ -1094,12 +1094,12 @@ pub const Species = enum(u8) {
|
|
|
1094
1094
|
/// The Pokémon's critical hit rate=io out of 256.
|
|
1095
1095
|
pub inline fn chance(id: Species) u8 {
|
|
1096
1096
|
assert(id != .None);
|
|
1097
|
-
return CHANCES[@
|
|
1097
|
+
return CHANCES[@intFromEnum(id) - 1];
|
|
1098
1098
|
}
|
|
1099
1099
|
|
|
1100
1100
|
/// Returns the `Data` corresponding to the species.
|
|
1101
1101
|
pub fn get(id: Species) Data {
|
|
1102
1102
|
assert(id != .None);
|
|
1103
|
-
return DATA[@
|
|
1103
|
+
return DATA[@intFromEnum(id) - 1];
|
|
1104
1104
|
}
|
|
1105
1105
|
};
|
|
@@ -91,18 +91,18 @@ pub const Type = enum(u4) {
|
|
|
91
91
|
|
|
92
92
|
/// Whether or not this type is considered to be special as opposed to physical.
|
|
93
93
|
pub inline fn special(self: Type) bool {
|
|
94
|
-
return @
|
|
94
|
+
return @intFromEnum(self) >= @intFromEnum(Type.Fire);
|
|
95
95
|
}
|
|
96
96
|
|
|
97
97
|
/// The `Effectiveness` of type `t2` vs. type `t1`.
|
|
98
98
|
pub inline fn effectiveness(t1: Type, t2: Type) Effectiveness {
|
|
99
|
-
return CHART[@
|
|
99
|
+
return CHART[@intFromEnum(t1)][@intFromEnum(t2)];
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
/// The precedence order of type `t2` vs. type `t1`.
|
|
103
103
|
pub fn precedence(t1: Type, t2: Type) u8 {
|
|
104
104
|
for (PRECEDENCE, 0..) |matchup, i| {
|
|
105
|
-
if (matchup.type1 == t1 and matchup.type2 == t2) return @intCast(
|
|
105
|
+
if (matchup.type1 == t1 and matchup.type2 == t2) return @intCast(i);
|
|
106
106
|
}
|
|
107
107
|
unreachable;
|
|
108
108
|
}
|
package/src/lib/gen1/data.zig
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
const std = @import("std");
|
|
2
2
|
const builtin = @import("builtin");
|
|
3
3
|
|
|
4
|
+
const pkmn = @import("../pkmn.zig");
|
|
5
|
+
|
|
4
6
|
const data = @import("../common/data.zig");
|
|
5
|
-
const options = @import("../common/options.zig");
|
|
6
7
|
const protocol = @import("../common/protocol.zig");
|
|
7
8
|
const rng = @import("../common/rng.zig");
|
|
9
|
+
const util = @import("../common/util.zig");
|
|
8
10
|
|
|
9
11
|
const moves = @import("data/moves.zig");
|
|
10
12
|
const species = @import("data/species.zig");
|
|
11
13
|
const types = @import("data/types.zig");
|
|
12
14
|
|
|
15
|
+
const calc = @import("calc.zig");
|
|
16
|
+
const chance = @import("chance.zig");
|
|
13
17
|
const mechanics = @import("mechanics.zig");
|
|
14
18
|
|
|
15
19
|
const assert = std.debug.assert;
|
|
@@ -34,12 +38,17 @@ pub const LOGS_SIZE = if (builtin.mode == .ReleaseSmall)
|
|
|
34
38
|
else
|
|
35
39
|
std.math.ceilPowerOfTwo(usize, MAX_LOGS) catch unreachable;
|
|
36
40
|
|
|
41
|
+
/// Null object pattern implementation of pkmn.battle.Options for Generation I.
|
|
42
|
+
pub const NULL = pkmn.battle.options(protocol.NULL, chance.NULL, calc.NULL);
|
|
43
|
+
|
|
37
44
|
const Choice = data.Choice;
|
|
38
45
|
const ID = data.ID;
|
|
39
46
|
const Player = data.Player;
|
|
40
47
|
const Result = data.Result;
|
|
41
48
|
|
|
42
|
-
const
|
|
49
|
+
const PointerType = util.PointerType;
|
|
50
|
+
|
|
51
|
+
const showdown = pkmn.options.showdown;
|
|
43
52
|
|
|
44
53
|
/// The pseudo random number generator used by Generation I.
|
|
45
54
|
pub const PRNG = rng.PRNG(1);
|
|
@@ -61,24 +70,27 @@ pub fn Battle(comptime RNG: anytype) type {
|
|
|
61
70
|
rng: RNG,
|
|
62
71
|
|
|
63
72
|
/// Returns the `Side` for the given `player`.
|
|
64
|
-
pub inline fn side(self:
|
|
65
|
-
|
|
73
|
+
pub inline fn side(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
|
|
74
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
|
|
75
|
+
return &self.sides[@intFromEnum(player)];
|
|
66
76
|
}
|
|
67
77
|
|
|
68
78
|
/// Returns the `Side` of the opponent for the given `player`
|
|
69
|
-
pub inline fn foe(self:
|
|
70
|
-
|
|
79
|
+
pub inline fn foe(self: anytype, player: Player) PointerType(@TypeOf(self), Side) {
|
|
80
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Self);
|
|
81
|
+
return &self.sides[@intFromEnum(player.foe())];
|
|
71
82
|
}
|
|
72
83
|
|
|
73
84
|
/// Returns an identifier for the active Pokémon of `player`.
|
|
74
|
-
pub inline fn active(self: *Self, player: Player) ID {
|
|
75
|
-
return player.ident(@intCast(
|
|
85
|
+
pub inline fn active(self: *const Self, player: Player) ID {
|
|
86
|
+
return player.ident(@intCast(self.side(player).order[0]));
|
|
76
87
|
}
|
|
77
88
|
|
|
78
89
|
/// Returns the result of applying Player 1's choice `c1` and Player 2's choice `c2` to the
|
|
79
|
-
/// battle, optionally writing protocol message logs to `log` if `options.log`
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
/// battle, optionally writing protocol message logs to `options.log` if `pkmn.options.log`
|
|
91
|
+
/// is enabled.
|
|
92
|
+
pub fn update(self: *Self, c1: Choice, c2: Choice, options: anytype) !Result {
|
|
93
|
+
return mechanics.update(self, c1, c2, options);
|
|
82
94
|
}
|
|
83
95
|
|
|
84
96
|
/// Fills in at most `out.len` possible choices for the `player` given the previous `result`
|
|
@@ -90,7 +102,7 @@ pub fn Battle(comptime RNG: anytype) type {
|
|
|
90
102
|
/// interacts with Disable, in which case there are no possible choices for the player to
|
|
91
103
|
/// make (i.e. on the cartridge a soft-lock occurs).
|
|
92
104
|
///
|
|
93
|
-
/// This function will always return
|
|
105
|
+
/// This function will always return choices > 0 if `pkmn.options.showdown` is true.
|
|
94
106
|
pub fn choices(self: *Self, player: Player, request: Choice.Type, out: []Choice) u8 {
|
|
95
107
|
return mechanics.choices(self, player, request, out);
|
|
96
108
|
}
|
|
@@ -120,7 +132,8 @@ pub const Side = extern struct {
|
|
|
120
132
|
}
|
|
121
133
|
|
|
122
134
|
/// Returns the stored `Pokemon` corresponding to the one-indexed party `slot`.
|
|
123
|
-
pub inline fn get(self:
|
|
135
|
+
pub inline fn get(self: anytype, slot: u8) PointerType(@TypeOf(self), Pokemon) {
|
|
136
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
|
|
124
137
|
assert(slot > 0 and slot <= 6);
|
|
125
138
|
const id = self.order[slot - 1];
|
|
126
139
|
assert(id > 0 and id <= 6);
|
|
@@ -128,7 +141,8 @@ pub const Side = extern struct {
|
|
|
128
141
|
}
|
|
129
142
|
|
|
130
143
|
/// Returns the stored `Pokemon` corresponding to the `active` Pokémon (slot 1).
|
|
131
|
-
pub inline fn stored(self:
|
|
144
|
+
pub inline fn stored(self: anytype) PointerType(@TypeOf(self), Pokemon) {
|
|
145
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Side);
|
|
132
146
|
return self.get(1);
|
|
133
147
|
}
|
|
134
148
|
};
|
|
@@ -153,7 +167,8 @@ pub const ActivePokemon = extern struct {
|
|
|
153
167
|
}
|
|
154
168
|
|
|
155
169
|
/// Returns the active Pokémon's current move slot located at the one-indexed `mslot`.
|
|
156
|
-
pub inline fn move(self:
|
|
170
|
+
pub inline fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
|
|
171
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == ActivePokemon);
|
|
157
172
|
assert(mslot > 0 and mslot <= 4);
|
|
158
173
|
assert(self.moves[mslot - 1].id != .None);
|
|
159
174
|
return &self.moves[mslot - 1];
|
|
@@ -182,7 +197,8 @@ pub const Pokemon = extern struct {
|
|
|
182
197
|
}
|
|
183
198
|
|
|
184
199
|
/// Returns the Pokémon's original move slot located at the one-indexed `mslot`.
|
|
185
|
-
pub inline fn move(self:
|
|
200
|
+
pub inline fn move(self: anytype, mslot: u8) PointerType(@TypeOf(self), MoveSlot) {
|
|
201
|
+
assert(@typeInfo(@TypeOf(self)).Pointer.child == Pokemon);
|
|
186
202
|
assert(mslot > 0 and mslot <= 4);
|
|
187
203
|
assert(self.moves[mslot - 1].id != .None);
|
|
188
204
|
return &self.moves[mslot - 1];
|
|
@@ -204,16 +220,16 @@ pub const MoveSlot = extern struct {
|
|
|
204
220
|
/// Details required to detect desyncs based on the move last selected/executed by players.
|
|
205
221
|
pub const MoveDetails = packed struct {
|
|
206
222
|
/// The move slot index of the last move selected in the battle menu by Player 1
|
|
207
|
-
p1_index: if (showdown) u8 else
|
|
223
|
+
p1_index: if (showdown) u8 else u4 = 0,
|
|
208
224
|
/// Whether the last move executed by Player 1 was counterable.
|
|
209
|
-
p1_counterable: if (showdown) u8 else
|
|
225
|
+
p1_counterable: if (showdown) u8 else u4 = 0,
|
|
210
226
|
/// The move slot index of the last move selected in the battle menu by Player 2
|
|
211
|
-
p2_index: if (showdown) u8 else
|
|
227
|
+
p2_index: if (showdown) u8 else u4 = 0,
|
|
212
228
|
/// Whether the last move executed by Player 2 was counterable.
|
|
213
|
-
p2_counterable: if (showdown) u8 else
|
|
229
|
+
p2_counterable: if (showdown) u8 else u4 = 0,
|
|
214
230
|
|
|
215
231
|
comptime {
|
|
216
|
-
assert(@sizeOf(MoveDetails) == if (showdown) 4 else
|
|
232
|
+
assert(@sizeOf(MoveDetails) == if (showdown) 4 else 2);
|
|
217
233
|
}
|
|
218
234
|
};
|
|
219
235
|
|
|
@@ -243,13 +259,13 @@ pub const Status = enum(u8) {
|
|
|
243
259
|
/// Whether or not the status `num` is the same as `status`.
|
|
244
260
|
pub inline fn is(num: u8, status: Status) bool {
|
|
245
261
|
if (status == .SLP) return Status.duration(num) > 0;
|
|
246
|
-
return ((num >> @intCast(
|
|
262
|
+
return ((num >> @intCast(@intFromEnum(status))) & 1) != 0;
|
|
247
263
|
}
|
|
248
264
|
|
|
249
265
|
/// Initializes a non-sleep `status`. Use `slp` or `slf` to initialize a sleep status.
|
|
250
266
|
pub inline fn init(status: Status) u8 {
|
|
251
267
|
assert(status != .SLP and status != .EXT);
|
|
252
|
-
return @as(u8, 1) << @intCast(
|
|
268
|
+
return @as(u8, 1) << @intCast(@intFromEnum(status));
|
|
253
269
|
}
|
|
254
270
|
|
|
255
271
|
/// Initializes a non self-inflicted sleep status with duration `dur`.
|
|
@@ -266,7 +282,7 @@ pub const Status = enum(u8) {
|
|
|
266
282
|
|
|
267
283
|
/// Returns the duration of a sleep status.
|
|
268
284
|
pub inline fn duration(num: u8) u3 {
|
|
269
|
-
return @intCast(
|
|
285
|
+
return @intCast(num & SLP);
|
|
270
286
|
}
|
|
271
287
|
|
|
272
288
|
/// Returns whether `num` reflects any status.
|
|
@@ -413,10 +429,11 @@ pub fn Stats(comptime T: type) type {
|
|
|
413
429
|
/// Computes the value of `stat` given a `base`, `dv`, stat `exp` and `level`.
|
|
414
430
|
pub fn calc(comptime stat: []const u8, base: T, dv: u4, exp: u16, level: u8) T {
|
|
415
431
|
assert(level > 0 and level <= 100);
|
|
416
|
-
const
|
|
432
|
+
const float: f32 = @floatFromInt(exp);
|
|
433
|
+
const evs: u16 = @min(255, @as(u16, @intFromFloat(@ceil(@sqrt(float)))));
|
|
417
434
|
const core: u32 = (2 *% (@as(u32, base) +% dv)) +% (evs / 4);
|
|
418
435
|
const factor: u32 = if (std.mem.eql(u8, stat, "hp")) level + 10 else 5;
|
|
419
|
-
return @intCast(
|
|
436
|
+
return @intCast(core *% @as(u32, level) / 100 +% factor);
|
|
420
437
|
}
|
|
421
438
|
};
|
|
422
439
|
}
|
|
@@ -465,7 +482,7 @@ test Boosts {
|
|
|
465
482
|
pub const Move = moves.Move;
|
|
466
483
|
|
|
467
484
|
test Move {
|
|
468
|
-
try expectEqual(2, @
|
|
485
|
+
try expectEqual(2, @intFromEnum(Move.KarateChop));
|
|
469
486
|
const move = Move.get(.Fissure);
|
|
470
487
|
try expectEqual(Move.Effect.OHKO, move.effect);
|
|
471
488
|
try expectEqual(@as(u8, 30), move.accuracy);
|
|
@@ -526,7 +543,7 @@ test Move {
|
|
|
526
543
|
pub const Species = species.Species;
|
|
527
544
|
|
|
528
545
|
test Species {
|
|
529
|
-
try expectEqual(2, @
|
|
546
|
+
try expectEqual(2, @intFromEnum(Species.Ivysaur));
|
|
530
547
|
try expectEqual(@as(u8, 100), Species.get(.Mew).stats.def);
|
|
531
548
|
}
|
|
532
549
|
|
|
@@ -543,9 +560,11 @@ pub const Effectiveness = enum(u8) {
|
|
|
543
560
|
Super = 20,
|
|
544
561
|
|
|
545
562
|
/// Used to determine the effectiness of a neutral damage hit.
|
|
546
|
-
pub const neutral: u16 =
|
|
563
|
+
pub const neutral: u16 =
|
|
564
|
+
@intFromEnum(Effectiveness.Neutral) * @intFromEnum(Effectiveness.Neutral);
|
|
547
565
|
/// Used to detect whether there is a mismatch in type effectivness (relevant for precedence).
|
|
548
|
-
pub const mismatch: u16 =
|
|
566
|
+
pub const mismatch: u16 =
|
|
567
|
+
@intFromEnum(Effectiveness.Resisted) + @intFromEnum(Effectiveness.Super);
|
|
549
568
|
|
|
550
569
|
comptime {
|
|
551
570
|
assert(@sizeOf(Effectiveness) == 1);
|
|
@@ -553,8 +572,8 @@ pub const Effectiveness = enum(u8) {
|
|
|
553
572
|
};
|
|
554
573
|
|
|
555
574
|
test Types {
|
|
556
|
-
try expectEqual(14, @
|
|
557
|
-
try expectEqual(20, @
|
|
575
|
+
try expectEqual(14, @intFromEnum(Type.Dragon));
|
|
576
|
+
try expectEqual(20, @intFromEnum(Effectiveness.Super));
|
|
558
577
|
|
|
559
578
|
try expect(!Type.Ghost.special());
|
|
560
579
|
try expect(Type.Dragon.special());
|