The Data Behind Battle Backgrounds
Originally Posted by DrFuji
Hey everybody, this thread is collaboration between myself and karatekid552 that seeks to help people with the many facets of battle backgrounds in Fire Red. In this section I will walkthrough how to insert new backgrounds over already existing ones. In the next half karatekid552 will go into further detail about how the background table is structured and present a way to allow you to expand the table and change the active background through a single variable. We hope that you can learn a lot from this and will be able to insert and do all sorts of wondrous things with battle backgrounds afterwards.
- Nameless TileMap Editor (NTME)
- Old version of Paint/ Photoshop/ GIMP/ An image-editing program of some kind that can work with indexed images
- A Hex Editor
- Free Space Finder
- An Emulator with a Fire Red ROM
Starting off, make a back up of you ROM, it is very possible that things may not come out as originally intended.
Firstly, we need to create a mock-up of the background we want to insert in Paint. In this instance I’ve decided to make a background for a generic grass battle. Here’s what I want to insert in the ROM:
The dimensions of this background are 240x112 pixels if you’re interested. Some of you keen-eyed people may have noticed that I have more than the standard 15 colours plus a transparent colour as per usual in an indexed image. This is the case because I’ll be taking advantage of a second palette that can be used when we are in battle – I will go further into this later.
If you don’t know whow to index an image then there are many other tutorials that cover this. For the sake of
length I would advise you to find another graphics tutorial that includes indexing.
Anyway, now that we know what we want to insert, we need to make it more compact for the next step. We will need to remove as many 8x8 pixel tiles that have been repeated as possible. If we were to insert something the size of our mock-up above it would take up quite a lot of unnecessary space and possibly overwrite/ be overwritten by other graphics loaded in the RAM. Here’s the tileset that I’ve compacted our background down to:
While this tileset isn’t optimal
as there are still a number of repeated tiles it is close enough and has already significantly shrunken the filesize that will be inserted. There also appears to be an anomaly on the right hand side of the tileset, this is to do with the second palette that I mentioned earlier. I’ve lined up the colours in the tiles so they will directly match their places in the second palette. As this particular picture can only be made with fifteen colours (plus transparency) it has to look out of place for the time being. Nevertheless, this is what we’re going to be working with.
Now we’re going to need to extract some things via unLZ.GBA (It is possible to insert a background without doing this step, but it makes everything easier). Open your ROM in unLZ and head to picture number 3. There you should see the tileset for the current grass background. Click ‘Save As’ and put it somewhere on your computer. Click ‘Next’ to arrive at picture number 4 which just appears to be a big jumble of random lines resembling television static. This is the tilemap for the grass background, it essentially tell our tileset how to arrange itself in-game to give us a full blown battlefield. To save it click ‘RAW Dump’ and then proceed to open up NTME.
NTME is a program that allows you to create your own tilemaps to be inserted along with tilesets. For now just open up the two files you saved from unLZ and you should be able to be able to recreate the default grass background. To make it look like it should in-game select the dropdown menu underneath ‘Size (Keep Ratio)’ and select 32x64.
But this isn’t what we want – We want to aim higher and greater. Now we should open up the combination of our own tileset as well as the default grass tilemap. 99% of the time a message will pop up asking about whether some tiles should be replaced with the first tile in your tileset. Just say yes to this and move on. The result may look random, but that’s only because we’re working with old data (the default tilemap) so now we need to remake our mock-up from the beginning of the tutorial. Load your tileset and start painting! It is easiest to just follow what has already been done by mirroring the original tilemap which is why it is open now. After some work these are the before and after screenshots:
But we’ve only got half the story in this screenshot, there’s a lot more that has gone unseen here - NTME’s viewing window can only show a fraction of what needs to be done. This actually what we have to make by manipulating the up/ down and right/ left scroll bars:
Yup, it’s just like an iceberg: most of the image’s size lies beneath the water. A handy image that briefly covers what each part of the tilemap represents has also been included for your convenience.
Once you’ve recreated your battle background we need to make sure it is using the correct palette. Select the ‘PaletteMap’ tab at the bottom of NTME and the viewing window should change to a series of numbers overlaying your background. These numbers indicate which palette the tiles will be loaded from when being played in-game. The only two numbers which we are interested in are two and three as they are what the game uses in its original backgrounds.
This is the point where if you prepared ahead of time you will be able to add more than fifteen colours (plus transparency) to your background. In this background I have a large strip of background that isn’t supposed to use the same palette as the majority of the background. There is a catch when it comes to adding a second palette though – It can only have a maximum of seven new colours. You will be able to see why this is the case later on, but for now know that you are restricting in how much you can add. Anyway, this is how my palette map will look, where palette two applies the full fifteen colour palette while palette three will only apply the restricted seven colour palette.
And with that, we’re finished making the tilemap for our background – Now it is time to insert both it and the tileset into the ROM. Reopen unLZ.GBA and head to the background of your choice. Once you’re at your background’s position select ‘Import’ and open your tileset. Select ‘Write to ROM’ and a second smaller window should pop-up; this is where we shall decide where the tileset will be inserted in the ROM. I’m going to be inserting the tileset at 0x720000 in the FR ROM as there is quite a lot of free space from there so I know nothing will be accidentally overwritten.
Don’t try to insert the palette accompanying the tileset at this point. Unlike other compressed images like Pokemon sprites, the proper palette isn’t attached to the tileset in unLZ – We need to change it manually later. Now let’s enter a battle and see how it’s looking.
Eww. To avoid seeing something Picasso vomited out during a hazy Friday night we have to insert the tilemap. Click ‘Next’ in unLZ and you should have returned to the tilemap from earlier. Rather than simply importing the tilemap like the tileset there is a slightly different method as it is not an image file (though unLZ is able to translate it into one). Select ‘File > Load RAW’ and select your custom tilemap. Now we just have to write it to the ROM in the exact same manner we did with the tileset. I’ve chosen to insert it directly after my tileset which means it will be located at 0x720400 according to HackMew’s Free Space Finder.
With both our tileset and tilemap inserted the background should be shaping up far better than before. This is what we get when we enter a battle now:
Alright, that’s looking much better – Our background actually has a recognisable structure now. We are now up to the final part of our insertion method, altering the palette. As you must have noted several times throughout this tutorial I have deferred talking about the background palettes for one reason or another. This was because it would have been quite jarring to interrupt with notes on how the palettes are structured as they differ from ordinary compressed and uncompressed palettes.
The palettes which are assigned to battle backgrounds are made from two palettes, palettes 0x2 and 0x3 in your emulator’s background palette viewer. Both of these palettes are highly interlinked as 0x3 borrows most of its data from 0x2 to save some space. This has caused them to appear fractured with a byte jutting out after every fourth word. For example, here’s a screenshot of the original grass background palette in a HEX editor with each stray byte indicated:
Thanks to this interlinking we cannot simply open up palettes in a palette editing program such as APE, even if it is instructed to uncompress the palette.
There is one main way to find your palette. Search for the first string of four words of your background’s 0x2 palette (I’ll discuss palette 0x3 shortly). You can do this by entering a battle with your chosen background, opening the palette viewer and reversing the colour values (e.g 0x1234 becomes 34 12). In the last screenshot you can see this as the string ’00 00 FC 73 D4 3F 90 3F‘ in front of the first marked byte. At this point you can just calculate the HEX values of your new palette and overwrite the current words but keep in mind the stray bytes
- Leave them as they are.
Now when we check our background in-game:
Awesome, we’re almost done. This part of the tutorial is only needed if you have chosen to utilise palette 0x3 in conjunction with 0x2. Since palette 0x3 is interwoven with palette 0x2 it is generally more garbled and difficult to find if you don't know what to do. Once you have found palette 0x2’s location, palette 0x3 should be somewhere nearby, but you should only search for the NEW values that are present in 0x3. In my case, when I searched for the grass background’s first two new values in palette 0x3 (92 37 4E 37) I found them at 0x24842B. Changes these values (once again looking out for any stray bytes) and save your ROM.
Let’s check out our final result:
Congratulations, you have just inserted your first battle background!
Now that DrFuji has taught you how to edit Battle Backgrounds, I am going to take you into the data aspect. For right now, this lesson will just be for FireRed. If you do want it for another game and you are actually going to use it, I will attempt to do my best to help you.
To start off with, apply the patch at the bottom of this post to your rom. What it does is move the battle BG table to 0xF10000 where we can work with and expand it, and it also hacks the BG loading routine with branches placed 0xF00000 (I will completely explain this later).
To be honest, the table is quite simple. To start off, it has 20 backgrounds already in it designated by Game Freak. They are as follows:
9. Multi/Indoor- White & Grey & some Yellow in the Palette
10. Multi/Indoor- Green & a little Yellow
11. Multi/Indoor- White & Grey
12. Multi/Indoor- White & Grey & some Tan in the Palette
13. Multi/Indoor- White & Grey & some Tan in the Palette (2)
14. Multi2/Indoor2- White & Grey
15. Multi2/Indoor2- White & Grey & some Purple in the Palette
16. Multi2/Indoor2- Blue & some Tan in the Palette
17. Multi2/Indoor2- Yellow & Tan
18. Multi2/Indoor2- Purple & some Tan in the Palette
19. Multi2/Indoor2- Turquoise & some Tan in the Palette
20. Multi2/Indoor2- White & Grey & some Purple in the Palette
For each background, there are 5 pointers in order with this format:
VV VV VV 08 WW WW WW 08 XX XX XX 08 YY YY YY 08 ZZ ZZ ZZ 08
VV = Image Offset
WW = TileMap Offset
XX = Entry Image
(Ex. The Grass that moves in front of the screen at the beginning of a grass battle.)
YY = Entry Image Tilemap
ZZ = Palette
Here is the documentation of each background and their pointers in the original table:
So, expanding this is really quite simple. Just follow the setup of the table and add on to the end. Don't delete any of them if you are not going to replace them.
This may sound obvious, but you never know what people will do:p.
So, I expanded it... Now what?
This is probably what you are thinking after completing the above. This is where the hack of the loading routine that comes in. What it does is allows you to very easily change the battle background to whatever you wish.
If you remember, all of the backgrounds are in order. If the first grass is 0, then numbering from there will give you the number of background, which means that background slots from 0 to 19 are already filled. So, let's say we add 4 more backgrounds to the end of the table: Fire, Snow, Storm, and Metal, respectively.
The games coding does not directly allow access to these backgrounds, which is what the hacked routine does. Basically, it branches to a section of ASM that checks var 0x40F7. If this var is set to 0, it will automatically return to the regular routine and continue as normal. However, if it is set to any other number, the routine will operate from there and load the battle background of that number instead.
So, if you set var 0x40F7 to 20, then the Fire background will be loaded. If you set it to 22, then the Storm bg will be loaded, and so on. This will stay until you change it to something else. In order to return to the normal background for that map, then you simply set the var to 0.
Please note that this routine overrides every type of battle. This means you can change trainer battles, sea battles, grass battles, cave battles, etc, all by setting that one var. Also, the routine loads a half-word, not a byte, which means that you can have up to 0xFFFF backgrounds in the table, which is a little over 65,000. Granted, there isn't enough space in the rom to have unique backgrounds in each slot, but don't worry about over expanding the table. You won't have a problem there.:)
Well, that about wraps this up. Originally I had planned to give you the ASM routines and explain how to use them, but that got overly complicated with the branches, so I made it really simple with the patch.
Also, I did my best to make this compatible with both JPAN's engine and Jambo51's 649 patch. I haven't tested it on either, but I asked Jambo if there would be any complications or things I should avoid and the only thing he gave me was which var to use since he had used a few. For JPAN's, I avoided using any RAM that he used (which didn't matter in the end because Jambo had me switch to a var) and I don't think he had any routines and such that would affect this.
In order to make this even better for people, on case you have already used the free space at 0xF00000, I have included the source code here:
You can do as you wish with this code.
If there are complications, please let me know!
Driver - Driver was an old-time German hacker whom originally accomplished this for his own hack. Instead of using a var, he freed up some RAM and used that. About two months ago, I received a rom and some notes of his from Narutoactor via Tajaros. It took me a while to make heads or tails of them, but once I did, I set out to port it over to English FireRed and this was the result! So, most of the credit goes to him!
Jambo51 - A HUGE help in making this. He taught me how to access vars and answered a lot of questions. I know he hates people bugging him with questions, so I really appreciate him not ignoring me!
Darthatron - For being a really good friend and giving me a lot of help. When I was stuck and didn't have access to the var_access routine due to being on the go, but I still wanted to write up the ASM for it, he Pastied it me and I got a lot done. As usual, he also answered a lot of questions and was really helpful.
Mat - While I didn't end up using it because I switched to a var, Mat spent over an hour helping me try to find some free save-able RAM for this project. I really appreciated the time he gave me as we explored the save blocks and got almost nowhere, but he stuck with me until he had to go, so I have to give him a huge thanks!
Tajaros (Not Shadowraze, this was before the switch:p) - For starting this whole project by sending me all of Drivers stuff and asking for my help in deciphering it. He also decoded a lot of the table and documented the first 9 backgrounds before I even started working, which was a huge help.
Narutoactor - For sharing all of Driver's research, and attempting to help me contact Driver which, unfortunately, never happened:(.
Lastly, Drfuji - For digging into the archives and redoing his old private bg editing tutorial and completely rewriting it to go along with this research to make this ultimate guide to hacking battle backgrounds.
Well, that is all for now! On to the next project.... !