@pkmn/engine 0.1.0-dev.7f37a47c → 0.1.0-dev.7fe22fc0

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.
@@ -198,7 +198,7 @@ above, PP information isn't strictly necessary in Generation I so is dropped. `M
198
198
  a special sentinel value to indicate `null`. Move PP data is only included for testing and is not
199
199
  necessary for the actual engine implementation.
200
200
 
201
- In order to workaround various [Pokémon Showdown bugs](#bugs) and to support its protocol in traces,
201
+ In order to workaround various [Pokémon Showdown bugs](#bugs) and to support its protocol in logs,
202
202
  additional information is stored in `Move.Data` (`targets`) about what Pokémon Showdown believes the
203
203
  `Move.Target` to be (despite the concept of targeting not existing until Generation III when Double
204
204
  battles were introduced). More specifically, a move's "targeting" status is required in various
@@ -331,8 +331,10 @@ does not produce an equivalent to Pokémon Showdown's `|request|` protocol messa
331
331
  must be gleaned through the `Battle` state). Useful size and offset information can be found in
332
332
  the [`layout.json`](../../data/layout.json) which exists to simplify writing driver code.
333
333
 
334
- Documentation of the wire protocol used for logging traces when `-Dtrace` is enabled can be found in
335
- [PROTOCOL.md](../../../docs/PROTOCOL.md).
334
+ Documentation of the wire protocol used for protocol message logging when `-Dlog` is enabled can be
335
+ found in [PROTOCOL.md](../../../docs/PROTOCOL.md). Note that the
336
+ [`pkmn-debug`](../../../README.md#pkmn-debug) tool exists to display the binary protocol and battle
337
+ data in the browser for ease of debugging.
336
338
 
337
339
  ### `Battle`
338
340
 
@@ -467,7 +469,7 @@ Documentation of the wire protocol used for logging traces when `-Dtrace` is ena
467
469
 
468
470
  In addition to its alternative [RNG semantics](#rng), Pokémon Showdown's implementation of the first
469
471
  generation of Pokémon contains a number bugs, [many of which are known and have been documented on
470
- Smogon](https://www.smogon.com/forums/threads/rby-tradebacks-bug-report-thread.3524844/#post-5933177):
472
+ Smogon](https://www.smogon.com/forums/posts/5933177/show):
471
473
 
472
474
  - moves on Pokémon Showdown can do 0 damage instead of failing or causing a [division-by-zero
473
475
  freeze](https://pkmn.cc/bulba-glitch-1#Division_by_0).
@@ -476,23 +478,38 @@ Smogon](https://www.smogon.com/forums/threads/rby-tradebacks-bug-report-thread.3
476
478
  - Confusion self-hits use the wrong damage formula resulting in off-by-one errors (and also fail to
477
479
  account for an opponent's Reflect). Furthermore, the confusion self-hit damage will erroneously be
478
480
  inflicted to the confused user's substitute if the confused user had been attempting to use a
479
- self-targeting move.
481
+ self-targeting move. Finally, Pokémon Showdown erroneously considers the *uncapped* self-hit
482
+ damage for the purposes of tracking the battle's last damage.
480
483
 
481
484
  Beyond the general bugs listed above, several move effects are implemented incorrectly by Pokémon
482
485
  Showdown. Some of these moves are [too fundamentally broken to be implemented](#unimplementable) by
483
486
  the pkmn engine, but the following moves have their broken behavior preserved in `-Dshowdown` mode:
484
487
 
488
+ - **Bide**: If the opponent faints after Bide inflicts damage on Pokémon Showdown residual damage
489
+ incorrectly still gets applied to Bide's user.
490
+ - **Counter**: On Pokémon Showdown choices made while sleeping (which should not have been
491
+ registered) can erroneously cause Counter to trigger Desync Clause Mod behavior.
485
492
  - **Leech Seed**: Leech Seed fails to heal its source side if a seeded target faints due to
486
- recoil/crash damage on Pokémon Showdown.
493
+ recoil/crash damage on Pokémon Showdown. Pokémon Showdown also updates the battle's last damage
494
+ field with the uncapped Leech Seed damage if the residual damage is applied which has consequences
495
+ for Bide and Counter.
487
496
  - **Pay Day**: Pay Day should still scatter coins if it hits (but doesn't break) and opponent's
488
497
  Substitute but doesn't on Pokémon Showdown.
489
498
  - **Flinch**: Flinching does not get cleared during move selection on Pokémon Showdown and is
490
499
  instead cleared in Pokémon Showdown's "residual" phase, meaning the flinch status gets erroneously
491
500
  preserved across fainting (as fainting triggers Pokémon Showdown's "instaswitch" behavior which
492
501
  skips end-of-turn residuals).
502
+ - **Rage**: Rage boosts should still result in burn/paralysis [stat modification
503
+ errors](https://pkmn.cc/bulba-glitch-1#Stat_modification_errors) but don't on Pokémon Showdown.
504
+ Furthermore, Pokémon Showdown only builds Rage for Disable/Explosion (hit/miss) when attacking
505
+ into a Substitute instead of all moves. Pokémon Showdown also implements the [Rage and Thrash /
506
+ Petal Dance accuracy bug]( https://www.youtube.com/watch?v=NC5gbJeExbs) incorrectly, as the
507
+ accuracy only gets written when the volatile is present which can lead to incorrect accuracy
508
+ values on turns where Rage is used on the same turn as a move which modifies accuracy or evasion.
493
509
  - **Thrash** / **Petal Dance**: once a Pokémon is locked into a thrashing move they will lose all
494
510
  speed ties on Pokémon Showdown (due to the fact that if they win the speed tie their action will
495
- then get "changed" and inserted back into the queue after their opponent's action).
511
+ then get "changed" and inserted back into the queue after their opponent's action). Pokémon
512
+ Showdown also implements accuracy incorrectly, as covered above.
496
513
  - **Freeze** / **Sleep**: Pokémon Showdown requires a move to be selected when a Pokémon is frozen
497
514
  or sleeping and uses that in the event that the status is removed while on the cartridge no
498
515
  selection is possible and no turn exists for the thawed/woken Pokémon to act except in the case of
@@ -508,14 +525,17 @@ the pkmn engine, but the following moves have their broken behavior preserved in
508
525
  implement by Pokémon Showdown.
509
526
  - **Roar** / **Whirlwind**: these moves can miss on Pokémon Showdown (and advance the RNG when
510
527
  checking) which is incorrect (these moves should always fail, but do not check accuracy or advance
511
- the RNG).
528
+ the RNG). More importantly, these moves should *not* cause the tracked last battle damage to be
529
+ zeroed, but on Pokémon Showdown they do.
512
530
  - **Substitute**: in addition to the [Substitute + Confusion
513
531
  glitch](https://pkmn.cc/bulba-glitch-1#Substitute_.2B_Confusion_glitch) not being implemented
514
532
  correctly (covered above), the [Substitute 1/4
515
533
  glitch](https://glitchcity.wiki/Substitute_%C2%BC_HP_glitch) also fails in many cases due to
516
534
  Pokémon Showdown implementing the health check based on floating point division instead of integer
517
535
  divison like on the cartridge (meaning the Substitute 1/4 glitch will only occur if the Pokémon's
518
- maximum HP is evenly divisible by 4).
536
+ maximum HP is evenly divisible by 4). Finally, Substitute also incorrectly blocks Dream Eater on
537
+ Pokémon Showdown and will incorrectly still heal 1 HP for any draining moves if the attack does 0
538
+ damage.
519
539
 
520
540
  In addition to numerous cases where Pokémon Showdown uses the wrong type of message (e.g. `|-fail`
521
541
  vs. `|-miss|` vs. `|-immune|`, e.g. in the case of Leech Seed) which are not documented here, Pokémon
@@ -549,7 +569,7 @@ called out in the `|rule|` section at the beginning of a battle's log):
549
569
  `|rule|` section** in Generation I (or II) as the actual order of switches here does not have
550
570
  competitive implications like it does in Generation III, but it is still contrary to how the games
551
571
  work where the [host (Player 1)'s Pokemon would switch
552
- first](https://www.smogon.com/forums/threads/gen-3-on-ps-final-fixes.3527268/post-5989318).
572
+ first](https://www.smogon.com/forums/posts/5989318/show).
553
573
 
554
574
  Pokémon Showdown enforces several clauses *before* the battle: **Cleric Clause** (all Pokémon must
555
575
  have full HP and PP, and not have any status conditions prior to the battle), **Stat Tradeback
@@ -570,13 +590,12 @@ left up to a client to support.
570
590
 
571
591
  Numerous moves on Pokémon Showdown are broken not simply due to local issues in the implementation
572
592
  of their effects but instead due to **global issues** related to fundamental mechanics such as
573
- [broken move
574
- selection](https://www.smogon.com/forums/threads/rby-fight-button-simulation-improvements-fix-partial-trapping-improve-counter.3673280/#post-8655897),
575
- using move name instead of slot to determine move identity, implementing volatile statuses
576
- incorrectly or not understanding how moves which call other moves work means that it isn't possible
577
- to completely implement Pokémon Showdown's behavior for the following moves (the pkmn engine
578
- attempts to reproduce as much of the behavior that can be reproduced without making data structure
579
- changes or dramatically deviating from the correct control flow):
593
+ [broken move selection](https://www.smogon.com/forums/posts/8655897/show), using move name instead
594
+ of slot to determine move identity, implementing volatile statuses incorrectly or not understanding
595
+ how moves which call other moves work means that it isn't possible to completely implement Pokémon
596
+ Showdown's behavior for the following moves (the pkmn engine attempts to reproduce as much of the
597
+ behavior that can be reproduced without making data structure changes or dramatically deviating from
598
+ the correct control flow):
580
599
 
581
600
  - **Wrap**: Binding moves like Wrap are implemented on Pokémon Showdown with an artificial
582
601
  `partialtrappinglock` volatile as opposed to how it works on the cartridge which simply relies on
@@ -586,9 +605,13 @@ changes or dramatically deviating from the correct control flow):
586
605
  still gets a turn under the [trapping sleep
587
606
  glitch](https://glitchcity.wiki/Trapping_move_and_sleep_glitch), Wrap does 0 damage against
588
607
  Ghost-type Pokémon instead of properly respecting immunity, and binding effects are handled in the
589
- wrong order in the code.
590
- - **Counter**: On Pokémon Showdown choices made while sleeping (which should not have been
591
- registered) can erroneously cause Counter to trigger Desync Clause Mod behavior.
608
+ wrong order in the code which results in either out of order messaging or, more consequentially,
609
+ Pokémon using Rage/Bide or Thrashing/Charging moves being incorrectly forced to skip a turn when
610
+ the Pokémon using the Binding move switches. The `partialtrappinglock` variable also separately
611
+ tracks its original damage in order to be able to correctly inflict damage on subsequent turns,
612
+ however, the pkmn engine isn't able to store this state and so must use the battle's last damage
613
+ like the cartridge which results in errors from Leech Seed overwriting the last damage on Pokémon
614
+ Showdown.
592
615
  - **Mimic**: Pokémon Showdown checks that the user of Mimic has Mimic in one of their move slots,
593
616
  which means Mimic legally called via Metronome or Mirror Move will only work if the user also has
594
617
  Mimic (and the moved mimicked by Mimic called via Metronome / Mirror Move will erroneously
@@ -600,15 +623,19 @@ changes or dramatically deviating from the correct control flow):
600
623
  - **Mirror Move**: Binding moves misbehave when used via Mirror Move (though Pokémon Showdown has
601
624
  its own weird behavior and does not implement the [partial trapping move Mirror Move
602
625
  glitch](https://glitchcity.wiki/Partial_trapping_move_Mirror_Move_link_battle_glitch) that exists
603
- on the cartridge).
626
+ on the cartridge). Additionally, Pokémon Showdown sets the last used move every turn a Pokémon
627
+ is Thrashing instead of just on the turn it is actually selected meaning Mirror Move will
628
+ sometimes successfully mirror a Thrashing move when it should fail.
604
629
  - **Metronome**: In addition to the issues with binding moves, Metronome and Mirror Move cannot
605
630
  mutually call each other more than 3 times in a row without causing the Pokémon Showdown simulator
606
631
  to crash due to defensive safety checks that do not exist on the cartridge.
607
- - **Transform**: Transform screws up the effect of Disable, because on Pokémon Showdown, Disable
608
- prevents moves of a given *name* from being used (e.g. "Water Gun") as opposed to moves in a
609
- specific *slot* (e.g. the 2nd move slot), and a Pokémon's moves can change after Transform (this
610
- is not an issue with Disable + Mimic because Mimic happens to replace the same slot). Furthermore,
611
- transforming and then using Mirror Move / Metronome can result in [glitchy behavior and
632
+ - **Transform**: Pokémon Showdown permanently copies the DVs of Transform's target and recomputes
633
+ stats every switch, corrupting the Transform user's stats. Transform also screws up the effect of
634
+ Disable, because on Pokémon Showdown, Disable prevents moves of a given *name* from being used
635
+ (e.g. "Water Gun") as opposed to moves in a specific *slot* (e.g. the 2nd move slot), and a
636
+ Pokémon's moves can change after Transform (this is not an issue with Disable + Mimic because
637
+ Mimic happens to replace the same slot). Furthermore, transforming and then using Mirror Move /
638
+ Metronome can result in [glitchy behavior and
612
639
  softlocks](https://pkmn.cc/bulba/Transform_glitches#Transform_.2B_Mirror_Move.2FMetronome_PP_error)
613
640
  which Pokémon Showdown does not implement.
614
641
 
@@ -665,10 +692,10 @@ compatibility mode, the implications of these differences are outlined below:
665
692
  Finally, the **initial 10-byte seed for link battles on Pokémon Red cannot include bytes larger than
666
693
  the `SERIAL_PREAMBLE_BYTE`**, so must be in the range $\left[0, 252\right]$. This has implications
667
694
  on the first 10 random numbers generated during the battle and has [**non-trivial competitive
668
- implications**](https://www.smogon.com/forums/threads/rby-tradebacks-bug-report-thread.3524844/page-16#post-9068411)
669
- (at the start of the battle move effects become more likely, the ["1/256-miss"
670
- glitch](https://glitchcity.wiki/1/256_miss_glitch) cannot happen, Player 1 is more likely to win
671
- speed ties, etc) that Pokémon Showdown cannot replicate due to everything described above.
695
+ implications**](https://www.smogon.com/forums/posts/9068411/show) (at the start of the battle move
696
+ effects become more likely, the ["1/256-miss" glitch](https://glitchcity.wiki/1/256_miss_glitch)
697
+ cannot happen, Player 1 is more likely to win speed ties, etc) that Pokémon Showdown cannot
698
+ replicate due to everything described above.
672
699
 
673
700
  All of places in the link battle code where randomness is required are outlined below:
674
701
 
@@ -767,11 +794,8 @@ The pkmn engine attempts to adhere to certain naming conventions:
767
794
  ## Resources
768
795
 
769
796
  - [pret/pokered](https://github.com/pret/pokered/) disassembly
770
- - [Gen I Main Battle
771
- Function](https://www.smogon.com/forums/threads/past-gens-research-thread.3506992/#post-5878612) -
772
- Crystal_
797
+ - [Gen I Main Battle Function](https://www.smogon.com/forums/posts/5878612/show) - Crystal_
773
798
  - [Pokemon Showdown!](https://github.com/smogon/pokemon-showdown)
774
799
  - [List of glitches (Generation
775
800
  I)](https://bulbapedia.bulbagarden.net/wiki/List_of_glitches_(Generation_I))
776
- - [Pokémon Showdown RBY
777
- Bugs](https://www.smogon.com/forums/threads/rby-tradebacks-bug-report-thread.3524844/#post-5933177)
801
+ - [Pokémon Showdown RBY Bugs](https://www.smogon.com/forums/posts/5933177/show)
@@ -17,16 +17,16 @@ const expectEqual = std.testing.expectEqual;
17
17
  const expect = std.testing.expect;
18
18
 
19
19
  /// The minimum size in bytes required to hold all Generation I choice options.
20
- pub const MAX_OPTIONS: usize = 9; // move 1..4, switch 2..6
20
+ pub const MAX_CHOICES: usize = 9; // move 1..4, switch 2..6
21
21
  /// The optimal size in bytes required to hold all Generation I choice options.
22
- /// At least as large as MAX_OPTIONS.
22
+ /// At least as large as MAX_CHOICES.
23
23
  pub const MAX_LOGS: usize = 180;
24
24
 
25
25
  /// The maximum number of bytes possibly logged by a single Generation I update.
26
- pub const OPTIONS_SIZE = if (builtin.mode == .ReleaseSmall)
27
- MAX_OPTIONS
26
+ pub const CHOICES_SIZE = if (builtin.mode == .ReleaseSmall)
27
+ MAX_CHOICES
28
28
  else
29
- std.math.ceilPowerOfTwo(usize, MAX_OPTIONS) catch unreachable;
29
+ std.math.ceilPowerOfTwo(usize, MAX_CHOICES) catch unreachable;
30
30
  /// The optimal size in bytes required to hold the largest amount of log data possible from a
31
31
  /// single Generation I update. At least as large as MAX_LOGS.
32
32
  pub const LOGS_SIZE = if (builtin.mode == .ReleaseSmall)
@@ -76,7 +76,7 @@ pub fn Battle(comptime RNG: anytype) type {
76
76
  }
77
77
 
78
78
  /// Returns the result of applying Player 1's choice `c1` and Player 2's choice `c2` to the
79
- /// battle, optionally writing protocol logs to `log` if `options.trace` is enabled.
79
+ /// battle, optionally writing protocol message logs to `log` if `options.log` is enabled.
80
80
  pub fn update(self: *Self, c1: Choice, c2: Choice, log: anytype) !Result {
81
81
  return mechanics.update(self, c1, c2, log);
82
82
  }
@@ -272,7 +272,7 @@ pub const Status = enum(u8) {
272
272
  return num > 0;
273
273
  }
274
274
 
275
- /// Retunrns a human-readable representation of the status `num`.
275
+ /// Returns a human-readable representation of the status `num`.
276
276
  pub fn name(num: u8) []const u8 {
277
277
  if (Status.is(num, .EXT)) return if (num == Status.TOX) "TOX" else "SLF";
278
278
  if (Status.is(num, .SLP)) return "SLP";
@@ -211,20 +211,19 @@ pub const Pokemon = struct {
211
211
  }
212
212
  };
213
213
 
214
- // cat src/test/showdown/blocklist.json | jq '."1".moves'
214
+ // TODO: uncomment once integration tests pass (possibily remove Transform/LeechSeed)
215
+ // fn blocked(m: Move) bool {
216
+ // // Binding moves are borked but only via Mirror Move / Metronome which are already blocked
217
+ // return switch (m) {
218
+ // .Mimic, .Metronome, .MirrorMove, .Transform, .LeechSeed => true,
219
+ // else => false,
220
+ // };
221
+ // }
222
+
215
223
  fn blocked(m: Move) bool {
216
224
  if (Move.get(m).effect == .Binding) return true;
217
225
  return switch (m) {
218
- .Counter,
219
- // FIXME: test and see if this can be removed or document
220
- .Rage,
221
- .Mimic,
222
- // FIXME: test and see if this can be removed or document
223
- .Bide,
224
- .Metronome,
225
- .MirrorMove,
226
- .Transform,
227
- => true,
226
+ .Counter, .Mimic, .Metronome, .MirrorMove, .Transform => true,
228
227
  else => false,
229
228
  };
230
229
  }