Nearly Infinite Procedural Terrain Generation
I know it has been a little over 4 weeks since my last update. But I have been very busy with my class and full-time job. I feel like I have not made much progress with the game. About 3 weeks ago I modified the terrain generation to work using chunks and changed the generation to be nearly infinite.
Chunk System:
The game world now is limited by the max size of a 32-bit integer. So, the terrain generation can only modify x and y values between -2147483648 and positive 2147483648. Which if my math is correct, makes my game world about 2 billion pixels in each direction from the center. I am not the best at math, but the map would have a lot of tiles before causing issues. So that is why I am calling it nearly infinite. It would take a long time to see all the tiles of the game world. Also, each time you start a new game the seed can change and generate another entirely different terrain. Or if you keep the same seed the game world would be the same at the start. This is like how Minecraft world generation works, except mine is 2D.
Major Changes:
Due to this massive change I modified my terrain generation edge checking code to check the edges on each chunk. However, currently along the edges of chunks because it does not know what type of tile is next to the edge, it draws incorrectly. I believe I need to rewrite the code now to have all the current chunks load on the screen and then calculate the edges of the tiles that are visible. That way there are not just some tiles that do not change to edge tiles. Because I am loading in 16×16 chunks of tiles, I am loading about 25 chunks total to fill my screen. I think the best way to solve my issue is to load the chunks and then calculate the edge changes. Hopefully, this solution will work after I get a chance to implement it.
When I am testing currently I have only one chunk load in which is what you see in the video. But I can have more load in, and then you will not see the transitions because they would be off the screen.
Other Issues:
Another major issue currently is that the map is generated using Perlin noise and a seed. Which works great for the terrain generation because it can use the seed to get the next chunk of terrain data. However, all the other items like trees or entities I need to come up with a system to not just randomly place them. Currently the game draws these objects at random when the chunk loads. But when the chunk unloads, I have not yet programmed in to remove the entities in the chunk. I need to come up with a system to save this information. I am thinking I will need to create id’s for the chunks and check if they have already been seen by the player. If they have then I will need to load them from a file of chunks. This will also require me to be able to save this same data to a file each time a chunk is unloaded.
An additional part that needs to be implemented is saving the entire game and loading from a save file. I have not yet implemented this. I may need to get this working before starting to save chunks. The game should be able to save a game and then reload that point so you can come back and play at another time. Because the game world is so large, and I want it to be an adventure I need to be able to save the current game. Also, you should be able to start a new game if you so desire.
Game Outlook:
Because of the amount of work required I think I put myself in a tough spot and I lost some of my drive to work on the project. I am hopeful now that I can start working towards solving these problems and getting the project back on track. I started working on serialization code, but I have not really tested it yet. My hope is that saving this data will not cause issues with delays. I may need to separate this data into smaller files, but I think many chunks should be saved in each file. My only concern is that if I have multiple files, I will have to check each file to see if the player has seen the chunk before loading the chunk. Or I would need an index to reference which file that information was stored in. I’ve never coded anything like this before so it will be difficult.
I think if I start tackling one piece at a time, I will be able to overcome these issues. I just think that it is going to take me a long time. Winter Break at the college is fast approaching so that may help me be able to work more on the project. I will do my best to post more soon. Until next time.
Full Change List:
Coded: a chunk system to load tiles in a 16×16 grid to allow less to render at a time. Currently the game loads 25 chunks in total, a 5×5 grid of chunks to cover the entire screen.
Updated: The game map system to handle chunks and level data.
Updated: Replaced the original level generation with chunks to have a nearly infinite world.
Bug: Fixed attack animations and arrow shooting after the map update bugged the code.
Bug: Fixed the spawning system after the map update bugged the code.
Nearly Infinite Procedural Terrain Generation Read More »