@pkmn/engine 0.1.0-dev.63cef311 → 0.1.0-dev.7b099504

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,12 +478,15 @@ 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
+ - **Counter**: On Pokémon Showdown choices made while sleeping (which should not have been
489
+ registered) can erroneously cause Counter to trigger Desync Clause Mod behavior.
485
490
  - **Leech Seed**: Leech Seed fails to heal its source side if a seeded target faints due to
486
491
  recoil/crash damage on Pokémon Showdown.
487
492
  - **Pay Day**: Pay Day should still scatter coins if it hits (but doesn't break) and opponent's
@@ -490,9 +495,17 @@ the pkmn engine, but the following moves have their broken behavior preserved in
490
495
  instead cleared in Pokémon Showdown's "residual" phase, meaning the flinch status gets erroneously
491
496
  preserved across fainting (as fainting triggers Pokémon Showdown's "instaswitch" behavior which
492
497
  skips end-of-turn residuals).
498
+ - **Rage**: Rage boosts should still result in burn/paralysis [stat modification
499
+ errors](https://pkmn.cc/bulba-glitch-1#Stat_modification_errors) but don't on Pokémon Showdown.
500
+ Furthermore, Pokémon Showdown only builds Rage for Disable/Explosion (hit/miss) when attacking
501
+ into a Substitute instead of all moves. Pokémon Showdown also implements the [Rage and Thrash /
502
+ Petal Dance accuracy bug]( https://www.youtube.com/watch?v=NC5gbJeExbs) incorrectly, as the
503
+ accuracy only gets written when the volatile is present which can lead to incorrect accuracy
504
+ values on turns where Rage is used on the same turn as a move which modifies accuracy or evasion.
493
505
  - **Thrash** / **Petal Dance**: once a Pokémon is locked into a thrashing move they will lose all
494
506
  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).
507
+ then get "changed" and inserted back into the queue after their opponent's action). Pokémon
508
+ Showdown also implements accuracy incorrectly, as covered above.
496
509
  - **Freeze** / **Sleep**: Pokémon Showdown requires a move to be selected when a Pokémon is frozen
497
510
  or sleeping and uses that in the event that the status is removed while on the cartridge no
498
511
  selection is possible and no turn exists for the thawed/woken Pokémon to act except in the case of
@@ -508,15 +521,21 @@ the pkmn engine, but the following moves have their broken behavior preserved in
508
521
  implement by Pokémon Showdown.
509
522
  - **Roar** / **Whirlwind**: these moves can miss on Pokémon Showdown (and advance the RNG when
510
523
  checking) which is incorrect (these moves should always fail, but do not check accuracy or advance
511
- the RNG).
524
+ the RNG). More importantly, these moves should *not* cause the tracked last battle damage to be
525
+ zeroed, but on Pokémon Showdown they do.
512
526
  - **Substitute**: in addition to the [Substitute + Confusion
513
527
  glitch](https://pkmn.cc/bulba-glitch-1#Substitute_.2B_Confusion_glitch) not being implemented
514
528
  correctly (covered above), the [Substitute 1/4
515
529
  glitch](https://glitchcity.wiki/Substitute_%C2%BC_HP_glitch) also fails in many cases due to
516
530
  Pokémon Showdown implementing the health check based on floating point division instead of integer
517
531
  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). Finally, Substitute also incorrectly blocks Dream Eater on
519
- Pokémon Showdown.
532
+ maximum HP is evenly divisible by 4). Substitute also incorrectly blocks Dream Eater on
533
+ Pokémon Showdown and will incorrectly still heal 1 HP for any draining moves if the attack does 0
534
+ damage. Finally, Pokémon Showdown uses a `subFainted` field to track whether a Substitute was
535
+ broken in order to know when to nullify a move's effect, only it doesn't get cleared at the end of
536
+ the turn and can result in incorrect behavior on subsequent turns with the moves Mirror Move and
537
+ Metronome that invoke `runMove` (which is where `subFainted` gets cleared) on the user but skips
538
+ calling it for the eventual true target.
520
539
 
521
540
  In addition to numerous cases where Pokémon Showdown uses the wrong type of message (e.g. `|-fail`
522
541
  vs. `|-miss|` vs. `|-immune|`, e.g. in the case of Leech Seed) which are not documented here, Pokémon
@@ -550,7 +569,7 @@ called out in the `|rule|` section at the beginning of a battle's log):
550
569
  `|rule|` section** in Generation I (or II) as the actual order of switches here does not have
551
570
  competitive implications like it does in Generation III, but it is still contrary to how the games
552
571
  work where the [host (Player 1)'s Pokemon would switch
553
- 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).
554
573
 
555
574
  Pokémon Showdown enforces several clauses *before* the battle: **Cleric Clause** (all Pokémon must
556
575
  have full HP and PP, and not have any status conditions prior to the battle), **Stat Tradeback
@@ -571,14 +590,17 @@ left up to a client to support.
571
590
 
572
591
  Numerous moves on Pokémon Showdown are broken not simply due to local issues in the implementation
573
592
  of their effects but instead due to **global issues** related to fundamental mechanics such as
574
- [broken move
575
- selection](https://www.smogon.com/forums/threads/rby-fight-button-simulation-improvements-fix-partial-trapping-improve-counter.3673280/#post-8655897),
576
- using move name instead of slot to determine move identity, implementing volatile statuses
577
- incorrectly or not understanding how moves which call other moves work means that it isn't possible
578
- to completely implement Pokémon Showdown's behavior for the following moves (the pkmn engine
579
- attempts to reproduce as much of the behavior that can be reproduced without making data structure
580
- changes or dramatically deviating from the correct control flow):
581
-
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):
599
+
600
+ - **Bide**: Bide's damage can overflow if OHKO moves are involved because OHKO moves work by setting
601
+ the damage to 65535 - Pokémon Showdown doesn't implement this overflow and instead lets Bide's
602
+ damage grow unbounded. Additionally, if the opponent faints after Bide inflicts damage on Pokémon
603
+ Showdown residual damage incorrectly still gets applied to Bide's user.
582
604
  - **Wrap**: Binding moves like Wrap are implemented on Pokémon Showdown with an artificial
583
605
  `partialtrappinglock` volatile as opposed to how it works on the cartridge which simply relies on
584
606
  the `Binding` volatile of the opponent. This mistake results in choice locking not being reported
@@ -587,9 +609,9 @@ changes or dramatically deviating from the correct control flow):
587
609
  still gets a turn under the [trapping sleep
588
610
  glitch](https://glitchcity.wiki/Trapping_move_and_sleep_glitch), Wrap does 0 damage against
589
611
  Ghost-type Pokémon instead of properly respecting immunity, and binding effects are handled in the
590
- wrong order in the code.
591
- - **Counter**: On Pokémon Showdown choices made while sleeping (which should not have been
592
- registered) can erroneously cause Counter to trigger Desync Clause Mod behavior.
612
+ wrong order in the code which results in either out of order messaging or, more consequentially,
613
+ Pokémon using Rage/Bide or Thrashing/Charging moves being incorrectly forced to skip a turn when
614
+ the Pokémon using the Binding move switches.
593
615
  - **Mimic**: Pokémon Showdown checks that the user of Mimic has Mimic in one of their move slots,
594
616
  which means Mimic legally called via Metronome or Mirror Move will only work if the user also has
595
617
  Mimic (and the moved mimicked by Mimic called via Metronome / Mirror Move will erroneously
@@ -598,18 +620,25 @@ changes or dramatically deviating from the correct control flow):
598
620
  the Pokémon already knows, PP deduction for using either the original move of the mimicked move
599
621
  will instead deduct PP for whichever appears at the lower move slot index and the PP will be
600
622
  allowed to go negative (effectively allowing for infinite PP use).
601
- - **Mirror Move**: Binding moves misbehave when used via Mirror Move (though Pokémon Showdown has
602
- its own weird behavior and does not implement the [partial trapping move Mirror Move
623
+ - **Mirror Move**: As covered above, both Substitute and Binding moves misbehave when used via
624
+ Mirror Move (though Pokémon Showdown has its own weird behavior and does not implement the
625
+ [partial trapping move Mirror Move
603
626
  glitch](https://glitchcity.wiki/Partial_trapping_move_Mirror_Move_link_battle_glitch) that exists
604
- on the cartridge).
605
- - **Metronome**: In addition to the issues with binding moves, Metronome and Mirror Move cannot
606
- mutually call each other more than 3 times in a row without causing the Pokémon Showdown simulator
607
- to crash due to defensive safety checks that do not exist on the cartridge.
608
- - **Transform**: Transform screws up the effect of Disable, because on Pokémon Showdown, Disable
627
+ on the cartridge). Additionally, Pokémon Showdown sets the last used move every turn a Pokémon is
628
+ Thrashing instead of just on the turn it is actually selected meaning Mirror Move will sometimes
629
+ successfully mirror a Thrashing move when it should fail. Finally, if Mirror Move copies
630
+ Struggle it should not deduct PP but on Pokémon Showdown it does.
631
+ - **Metronome**: In addition to the issues with binding moves and Substitute, Metronome and Mirror
632
+ Move cannot mutually call each other more than 3 times in a row without causing the Pokémon
633
+ Showdown simulator to crash due to defensive safety checks that do not exist on the cartridge.
634
+ - **Transform**: Transform screws up the effect of Disable, because on Pokémon Showdown, Disable
609
635
  prevents moves of a given *name* from being used (e.g. "Water Gun") as opposed to moves in a
610
636
  specific *slot* (e.g. the 2nd move slot), and a Pokémon's moves can change after Transform (this
611
- is not an issue with Disable + Mimic because Mimic happens to replace the same slot). Furthermore,
612
- transforming and then using Mirror Move / Metronome can result in [glitchy behavior and
637
+ is not an issue with Disable + Mimic because Mimic happens to replace the same slot). Transform
638
+ also has a bad interaction with Pokémon Showdown's buggy Haze implementation, as Haze on Pokémon
639
+ Showdown does not copy unmodified stats, leaving the Transformed Pokémon with inccorect stats.
640
+ Furthermore, transforming and then using Mirror Move / Metronome can result in [glitchy behavior
641
+ and
613
642
  softlocks](https://pkmn.cc/bulba/Transform_glitches#Transform_.2B_Mirror_Move.2FMetronome_PP_error)
614
643
  which Pokémon Showdown does not implement.
615
644
 
@@ -666,10 +695,10 @@ compatibility mode, the implications of these differences are outlined below:
666
695
  Finally, the **initial 10-byte seed for link battles on Pokémon Red cannot include bytes larger than
667
696
  the `SERIAL_PREAMBLE_BYTE`**, so must be in the range $\left[0, 252\right]$. This has implications
668
697
  on the first 10 random numbers generated during the battle and has [**non-trivial competitive
669
- implications**](https://www.smogon.com/forums/threads/rby-tradebacks-bug-report-thread.3524844/page-16#post-9068411)
670
- (at the start of the battle move effects become more likely, the ["1/256-miss"
671
- glitch](https://glitchcity.wiki/1/256_miss_glitch) cannot happen, Player 1 is more likely to win
672
- speed ties, etc) that Pokémon Showdown cannot replicate due to everything described above.
698
+ implications**](https://www.smogon.com/forums/posts/9068411/show) (at the start of the battle move
699
+ effects become more likely, the ["1/256-miss" glitch](https://glitchcity.wiki/1/256_miss_glitch)
700
+ cannot happen, Player 1 is more likely to win speed ties, etc) that Pokémon Showdown cannot
701
+ replicate due to everything described above.
673
702
 
674
703
  All of places in the link battle code where randomness is required are outlined below:
675
704
 
@@ -768,11 +797,8 @@ The pkmn engine attempts to adhere to certain naming conventions:
768
797
  ## Resources
769
798
 
770
799
  - [pret/pokered](https://github.com/pret/pokered/) disassembly
771
- - [Gen I Main Battle
772
- Function](https://www.smogon.com/forums/threads/past-gens-research-thread.3506992/#post-5878612) -
773
- Crystal_
800
+ - [Gen I Main Battle Function](https://www.smogon.com/forums/posts/5878612/show) - Crystal_
774
801
  - [Pokemon Showdown!](https://github.com/smogon/pokemon-showdown)
775
802
  - [List of glitches (Generation
776
803
  I)](https://bulbapedia.bulbagarden.net/wiki/List_of_glitches_(Generation_I))
777
- - [Pokémon Showdown RBY
778
- Bugs](https://www.smogon.com/forums/threads/rby-tradebacks-bug-report-thread.3524844/#post-5933177)
804
+ - [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
  }
@@ -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
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 => 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, .Bide, .Metronome, .MirrorMove, .Transform => true,
228
227
  else => false,
229
228
  };
230
229
  }