kwsch / PKHeX

Pokémon Save File Editor
https://projectpokemon.org/pkhex/
Other
3.63k stars 682 forks source link

[BW] C-Gear skin file importer destroys tilemap details #4337

Closed kuroppoi closed 2 weeks ago

kuroppoi commented 1 month ago

When importing a .cgb or .psk file, PKHeX will decompose all of the tilemap indices under the assumption that they are obfuscated. In reality, the C-Gear skin tiles are just stored in an odd manner in tile memory.

(Tileset image, the top left tile is tile 0x0 and the bottom right is 0x3FF. The blank areas are typically filled with tiles from recently accessed menus and are unreliable to use.) image

All of these tiles can be used in C-Gear skin tilemaps. Skin tiles start at index 0xA0 and seems to skip 15 tiles after every strip of 17 tiles. The problem with decomposing the tilemap indices is that it gets rid of indices belonging to tiles not part of the skin itself, which is actually something that most official C-Gear skins make use of.

This bar that stretches across the top of the screen is stored in the tilemap data and is constructed using tiles 0xA and 0xB with color palette 8 (which is dynamic: either blue or reddish depending on the trainer's gender.) Currently, PKHeX will filter these indices out which is why this detail is not present in all skins that have been imported by it. image

Speaking of palettes -- those are stored in the tilemap data as well. In Black & White, skin colors are stored in palette 10 (0xA). Taking example index 0xA0A0 (or 0b1010000010100000 in binary) the complete data is as follows:

Palette Index Flip XY Tile Index
Binary 1010 00 0010100000
Hex value 0xA 0x0 0xA0
Bitmask 0xF000 0xC00 0x3FF

I should add that all of this data pertains to Black & White only. In Black 2 & White 2, skin tiles are stored linearly and the game forces all tiles to use the skin palette. There are also no interesting non-skin tiles that can be used. This is probably because the game seems to add 0x200 to all skin tile indices which puts all of the earlier tiles out of reach.

kwsch commented 2 weeks ago

Initial rewrite is complete; I've tested importing/exporting for both games (img and file).

I'll let it marinate a bit before merging, unless someone finds any issues :)

Rewrite makes it much easier to add support for PokéDex Skins, but that requires figuring out the various flags to enable display in-game (see SAV5 for enabling logic).