Pokewalker PID Calculation:
Adapting Wichu's pseudocode to explain my findings:
Code:
rnd = CallIrng()
nature = rnd % 24 # There are actually 25 natures, so this prevents the Quirky nature being chosen, for some reason.
pid = (((trainerId ^ secretId) >> 8) ^ 0xFF) << 24 # the most significant byte of the PID is chosen so the Pokémon can never be shiny.
nature = nature - (pid % 25) # Adjusts the nature value so that when it is added to the PID, the resulting nature is the same as the original chosen nature. This can underflow to give a negative number.
pid = pid + nature # Creates the PID with the correct nature. If nature is negative, underflows to give a PID in the form 0xXXFFFFYY; otherwise, the PID is in the for 0xXX0000YY.
gr = GenderRate(species) # Gets the gender rate of the Pokémon (0-255; 0 is always male, 254 is always female, and 255 is genderless).
if (gender == male) #the gender forcing only seems to happen when necessary
pid = pid + ((((gr - (pid & 0xFF)) / 25) + 1) * 25) # Ensures gender is set to male without affecting nature.
if ((rnd & 1) != (pid & 1)) # If ability does not match the chosen ability
pid = pid + 25 # Switches ability without affecting nature
end if
else if (gender == female)
pid = pid - (((((pid & 0xFF) - gr) / 25) + 1) * 25) # Ensures gender is set to female without affecting nature.
if ((rnd & 1) != (pid & 1)) # If ability does not match the chosen ability
pid = pid - 25 # Switches ability without affecting nature
end if
end if
return pid
Key differences between Wichu's original posting and the above code:
Gender forcing algorithms only happen when the initial PID's gender does not match the forced gender. Single-gender and genderless Pokemon do not appear to have their PID adjusted by these steps.
The force female algorithm is pid - (), not pid + ()
The ability swap only happens when the gender is swapped.
Practical applications:
Gender rates:
31: Munchlax, Combee, Eevee, Torchic
63: Magby, Machop, Elekid, Abra
127: Everything with a 50/50 gender split
191: Azurill, Clefairy, Jigglypuff, Corsola, Cleffa, Igglybuff
Whenever the gender is forced, the natures with even numbers will be ability 0, and the natures with odd numbers will be ability 1. The only way to avoid this is to have an ID/SID combination that has the correct nature/ability combo and does not have a gender swap. As far as Munchlax is concerned, this makes Selfdestruct + Sassy + Thick Fat very difficult to get*, and Selfdestruct + Careful + Pickup impossible.
(EDIT: It might be worth noting that Hardy + Ability 1 is similarly unobtainable for Pokemon that are forced to be female.)
*Need [(((trainerId ^ secretId) >> 8) ^ 0xFF) << 24] mod 25 to be 23. Every other number either makes Sassy Munchlax go through the gender correction (and even nature -> Ability 0) or are innately even.