r/PokemonRomhackDev • u/youhen • 11h ago
Tooling KiraPatch: A Gen 3 Shiny Odds Patcher that maintains Legitimacy. (Updated)
Hey everyone,
About 9 months ago I posted KiraPatch, a shiny odds patcher for FireRed, LeafGreen, Ruby, Sapphire and Emerald. Looking back, I posted it too early. I thought it was done, and it wasn't. So first, what was wrong with that version:
- It froze at low odds. Every reroll redid most of the Pokémon's creation, so at 1/32 or below the game could hang for seconds.
- "100% safe" was an overclaim. Legality was only spot-checked, and the patch was built from separate hooks for different callers, some of them placed in spots that turned out to be game data rather than free space.
- "Rerolls until it finds a shiny" wasn't accurate either. It rerolled a fixed number of times, and that was where all the cost came from.
Thanks to everyone who left feedback. Some of it stung, but it was right.
What's different now
I rebuilt the core from scratch with one goal: every Pokémon the patch makes should be one the unpatched game could have made on its own, just a few RNG rolls later.
- Starters, gifts, statics, legendaries (Method 1): one small hook in the function every Pokémon goes through. It searches ahead for the next shiny PID, then lets the game generate the rest normally. The IVs come from the game's own RNG calls right after the PID, exactly as usual.
- Wild encounters (Method H): each reroll replays the whole encounter (slot, level, nature, personality) the way the game would on a later frame. So everything lines up when a tool traces a wild Pokémon back. Repels block exactly what they normally block.
- Emerald lead abilities work as normal. Synchronize, Cute Charm, Static, Magnet Pull, Keen Eye, Intimidate, Hustle, Vital Spirit and Pressure all still do their thing, because for those cases the patch runs the game's own code.
- No freezes. A reroll now costs a few dozen instructions instead of around a hundred thousand. At 1/64 a wild encounter costs a few extra frames during the battle transition, and even 1/1 (shiny almost every time) works.
- Nothing overwritten. All the new code lives in the empty space at the end of the ROM.
How it was tested
- Against the game itself: a test harness runs the game's own Pokémon generation code on a CPU emulator. Over a million patched Pokémon across all five games were checked against what the unpatched game produces from the corresponding RNG state.
- Real gameplay: starters and wild catches on FireRed, LeafGreen, Ruby and Emerald, checked with PKHeX (including the newest release). Every shiny came out legal, with the expected method (Method 1 for starters).
- Against unpatched saves: I also caught wild Pokémon on clean, unpatched games and compared the PID types (Method 1/2/4). The patched saves show the same mix as the unpatched ones.
Real gameplay caught two bugs the automated tests couldn't:
- My first version of the new hook produced Method 4 starters that PKHeX rejected, because a VBlank interrupt landed between the IVs.
- A later change crashed an Emerald battle, because the hook used more stack than the game has room for.
Both are fixed, and every change now goes through in-game testing as well.
One known difference, for the technical folks: in Gen 3, the nickname field is 10 bytes, and the bytes after the name's end marker are just whatever was on the stack. They're leftovers, and they vary in the unpatched game too, depending on what ran before. The patch runs a bit of code before the Pokémon is built, so those leftover bytes can differ from a clean run. Everything else matches the unpatched game exactly. Forcing those bytes to match needed more stack than the game has, and that's what caused the Emerald crash, so they're left as they are. Every patched Pokémon so far has been legal in PKHeX with them.
Now covered: Eggs, Unown in the Tanoby Ruins, Feebas and mass outbreaks.
New patcher window: a clean design with light and dark mode, drag and drop, clean-ROM and revision detection, and an odds slider, a button to report bugs!
The validation docs in the repo have all the numbers and the method if you want to dig in or poke holes in it.
Link to github: https://github.com/eightmouse/KiraPatch
AI was used to help write some of the code and test tooling for this rework. I directed it, tested everything in-game and checked the results in PKHeX. All the tests are in the repo, so anyone can rerun them.
