Handmade Network»Forums
27 posts
Handling 2D tile chunk management and generation.
Edited by mojobojo on
I am working on a little 2D RTS engine and I am working my way up to dynamic world generation and "infinite" terrain. Currently I generate the world and split it into chunks I query via camera position. You can see how its split in this image.

Here you can see the aprin where chunks start to load in.

I was thinking I would store chunks in hash tables and dynamically size the hash table as I need more. The down side is I think that means I will have to recompute entry locations every time I resize the table. I am a little stuck how I will store and query the information in a sane way.

Another think is when I generate a tile and say I have not queried the tile next to it however the one next to it has something that extends into it, how do I handle that case? I was thinking I could tell tiles to generate much farther away than what the camera sees but that will have to extend as far as the max radius of whatever I am generating like a lake or a large biome.
9 posts
Handling 2D tile chunk management and generation.
Edited by thebeast33 on
You could try generating the world in a deterministic way. A random number generator requires a seed to start, you could trying having a global seed for that instance of the world and then using the grid location of each tile along with that global seed to set some tile properties.

One method I thought about was creating a bit stream with the seed and grid coordinates of each tile and hashing them using CRC32 to get some deterministic number that I could use to generate properties for that tile. So something like:

int dataToBeHashed[3] = { GlobalSeed, GridX, GridY };
u32 hash = CRC32( (u8*)dataToBeHashed, sizeof( dataToBeHashed ) );

You could then use 'hash' any way you like to generate a specific tile. Two methods I have thought of are using specific bits to control specific properties (i.e. the first 5 bits determine terrain type, etc). The other method is using that hash as a seed local to an instance of a rng for that tile. Both are deterministic and fairly easy to compute.

For how tiles can be visually blended, you could generate the hash of 2 tiles next to each other and combine them somehow (maybe XOR or CRC again) and use the result of that to determine how they should blend. It being deterministic can be very important for reproduction of certain level configurations you are trying to debug/test too.

As a side note, you don't have to CRC32. I only choose it because it is one that I know and have using in the past for various things. There are other hashing algorithms that are faster and more random.