Skip to content

Instantly share code, notes, and snippets.

@Lusamine
Last active June 30, 2022 17:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lusamine/c9c6be1287daa3aff2c5010a01d662f7 to your computer and use it in GitHub Desktop.
Save Lusamine/c9c6be1287daa3aff2c5010a01d662f7 to your computer and use it in GitHub Desktop.

Description of how LGPE spawns Pokémon into the overworld.

Isn't this cryptosecure?

PID generation in LGPE is cryptosecure. This means that it is not possible to predict whether a Pokémon will be shiny or not. However, spawns can be predicted to some extent.

Spawn Type Definitions

In this writeup, I will refer to 3 categories of spawns:

  • Standard: The regular encounterslot table for the area. Sky and water spawns have their own tables.
  • Rare: These are hardcoded special spawns for each area. Examples include Chansey, Lapras, Snorlax, the starters, Charizard, and Dragonite.
  • Legendary: The 3 legendary birds (Articuno, Zapdos, Moltres) for sky spawns only.

As an example, the sky spawns on Route 4 have Spearow and Fearow as standard spawns, Dragonite and Charizard as rare spawns, and the 3 birds as legendary spawns.

Important Mechanics

  • If a Lure is active, rare and legendary spawn checks occur twice instead of once. This also causes the RNG states to advance faster since more rolls are consumed.

  • Rare and legendary spawns are limited to one per species. The game will not attempt to spawn a new Bulbasaur unless the previous Bulbasaur is despawned. It is possible to have one of each legendary bird, but not two of a single legendary bird.

  • The Catch Combo increases the number of guaranteed 31 IVs. These are the actual IV bonuses:

    Catch Combo Guaranteed IVs
    0-5 0
    6-10 1
    11-20 2
    21-30 3
    31+ 4
  • The game skews the height and weight of Pokémon to more extreme values as the Catch Combo increases. Height and weight range from 0-255. For each roll, the game generates new height/weight values and keeps them if they are farther away from 128 than the previous values. This is how many height/weight rolls you get for each range of Catch Combo lengths for overworld Pokémon:

    Catch Combo Height/Weight Rolls
    0 1
    1-5 2
    6-10 3
    11-20 4
    21-30 5
    31+ 6

    Having a lure active grants an additional +1 roll to try for more extreme values, up to a maximum of 7. Static and gift encounters are limited to 1 roll regardless of Catch Combo length.

    • Before size differences are activated in Viridian Forest, the game uses a different formula to generate height/weight. This is a rand.NextInt(160) + 48 which forces all wild encounters to be average size since the range for average is 48-207.
    • The starter Eevee or Pikachu does not use the forced average calculation and instead uses the regular formula.
    • After the event flag has been activated in Viridan Forest, the game allows the entire range of values, which are generated as rand.NextInt(129) + rand.NextInt(128).

RNG States

LGPE's spawning algorithm is handled by two 128-bit RNG states and uses Xoroshiro128+. The logic is in sub_71002FDFC0.

  • RNG state #1 handles, among other things, standard and rare spawn species and spawn level.
  • RNG state #2 handles legendary spawn species.

RNG State Reset Behavior

Both RNG states are reinitialized upon leaving a battle and entering a new zone. In addition:

  • RNG state #1 is generated with the Xoroshiro constant and a time-based 64-bit half.
  • RNG state #2 is generated with the Xoroshiro constant and a cryptosecure 64-bit half.

RAM Locations

This is for version 1.0.2 in both Pikachu and Eevee.

  • RNG state #1 is located at HEAP + 0x9A2CDFB0.
  • RNG state #2 is located at HEAP + 0x9A2CDFC0.

Algorithm

  1. First, the standard encounterslot is determined with a rand.Next() % 100.

  2. Next, the game checks whether the Catch Combo triggers a bonus to the combo species with a rand.Next() % 100. Only standard spawns are affected by this bonus.

    If this check passes, the combo species replaces the standard encounterslot. The chance of this happening is as follows:

    Catch Combo Chance of Combo Species Replacing Standard Spawn (%)
    0 0
    1-5 5
    6-10 15
    11-20 30
    21-30 40
    31+ 50
  3. The next check is for whether a rare species overrides the standard or Catch Combo species, using a rand.Next() % 10000. Note that the Catch Combo species does not matter for this check! All that matters is the length of the Catch Combo.

    Catch Combo Chance of Rare Spawn (%)
    0-1 0.5
    2-5 0.75
    6-10 1
    11+ 50
  4. Finally, for sky spawns, the game checks whether to spawn a legendary bird using a rand.Next() % 10000 <= 4. This makes the legendary bird rate a base of 1/2000.

    If a legendary bird is spawned, the species is then determined with another rand.Next() % 3.

    • 0 spawns Moltres.
    • 1 spawns Zapdos.
    • 2 spawns Articuno.

    The legendary species overrides all the previous determinations.

Due to the use of modulo, all true rates are very slightly different, but too small to be of significance.

You can see an example of how a legendary bird spawn can be predicted in this script here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment