Mart Guy Buffer Overflow (Mart Pwner)
Posted by: luckytyphlosion
Date: 2016-01-17 18:27:06
To set up a custom text pointer, steps would go somewhere similar to this:
[li]Set the text pointer to somewhere in WRAM (preferably accessible by the player, such as bag items): e.g. $d31e will point to the beginning of the inventory.[/li]
[li]For each NPC on the map, create a pointer starting from the text pointer which will point to the NPC's text. The pointers must be in order of the NPC's index, starting from 0: e.g. NPC with index 1 should have a pointer 2 bytes from the text pointer. [/li]
Now, in order for an NPC to be a Poke Mart vendor, the first byte of its text must be 0xff. The next byte represents quantity (which is semi-irrelevant unless you want to buy items) and the bytes following are a list of items in the mart, terminated by $ff. This will allow you to create your own Poke Mart Vendor!
If you make the list too long, the mart inventory will eventually overflow into other portions of memory. We can use this to our advantage to write values starting from $cf7b, similar to string buffer overflows such as TMTRAINER items and the CoolTrainer move. Unlike those two, using a Mart Guy allows much easier manipulation as the source is hypothetically user controlled. In addition, it's possible to write to much farther values because the source can be set at any address, allowing a longer distance between the destination (cf7b) and the source.
One application is for catching all Pokémon. I made a speedrunning route using this glitch, as shown here by Chivu93 (setup starts at 18:45, glitch starts at 25:34)
Another application could be to create a Pokémon using items, although it would take a considerable amount of space before the destination would reach $d16b (wPartyMon1).
(Speedrunning route if anyone is interested: Link)