Terrain chunk loading and unloading

Hi! I am working on a procedural landscape thing. It loads the world with the marching cubes algorithm in chunks (16x16 grids). I want to have the world load chunks strategically like shown in the gif (top-down view). In the gif the player is looking towards the top of the screen. Therefore the algorithm would load the chunk on the top first (to generalize it, the algorithm should load the chunks in the direction the player is facing first. I think limiting it to the 4 directions north, south, east and west would be best (and easiest)). I just need the chunks to be sorted in a list based on this concept I think. Then I can load them in that order. Can anyone help me achieve this or lead me on the right path? I don’t have a lot of experience with algorithms. Also, I am not sure what the best way to load and unload chunks are. Do I just disable them or what? I mean, the player can build and stuff so if I were to destroy them I would have to save the data somewhere. Any help would be greatly appreciated.
178027-chunk-loading.gif

You have packed a lot of different questions into a single post here. Saving and loading a chunk is completely unrelated to the order in which you load them. We can’t really help you with your save format since it completely depends on what data you need to store per chunk. The region file format that minecraft uses (which Notch invented) contains a variety of data. Though it’s generally divided into entities and terrain data. The terrain data just contains the actual voxel data that was generated by the chunk generator in a compressed format. The entities contain highly variable data depending on the entity type. The region format is very well documented. So if you have no idea where to start, you may want to have a look at the documentation. Please keep in mind that this should only give you an idea how it could be done. I do not recommend to directly copy the format. Understanding the concepts and adapting it to your needs is what you need to do.

As for the loading pattern, you think too much in static problems. While the world loads around the player, the player usually can already move around. So which chunks need to be loaded next can change at any time. The pattern you described would be a direct consequence of simply prioritizing chunks with a manhatten distance closest to the player and additionally increate the priority of those chunks that are closer to the players “front side”.

So what you should do is create a list of “active chunks”. That is a list of chunk objects that need to be loaded. This list need to be filled with all chunks that should be loaded, the order doesn’t matter. Lets say you want a chunk radius of 8 chunks. So you have a 16x16 area of chunks that should be loaded. What you do is give all 256 active chunks a priotity value and sort the list of chunks based on that priority. The priority value should be determined by the inverse of the manhatten distance from the player. In addition you can simply add a value depending on the direction of the chunk relative to the player’s view direction. This can be done either by calculating the angle in the x-z plane between the player forward vector and the relative direction vector from the player to the chunk. An alternative would be to just use the dot product between those normalized vectors. This will yield a value of 1 for the chunk directly in the line of sight, a 0 for chunks to the left and right of the player and a -1 for those behind the player. Just add this value to the priority value determined by the distance. The highest manhatten distance for a 16x16 area seen from the center is 16. So you can simply calculat the distance by doing

dist = Abs(dx) + Abs(dz)

where dx and dz are the relative distances of the chunk from the player in chunk coordinates(so essentially +/-8). To get the inverse relationship you can either divide 16 by dist or subtract dist from 16. You get a different distribution in each case,

Note that this is a highly dynamic process. As the player rotates or move, the priorities of all not-yet-loaded chunks change. You may recalculate those priorities 5 to 10 times per second to get a good reaction to the player movement. Once that’s done all you have to do is fill a priority queue with your chunks and load them in order of highest priority. Of course chunks that are already loaded can be taken out of the queue. You generally would have several lists and queues to manage all your active chunks. You always want some hysteresis when it comes to loading / unloading chunks to avoid too frequent loading / unloading actions. That simply means the unloading radius should be slightly larger than the loading radius. So when the player moves back and forth only one or two chunk, no chunks would be loaded unloaded.

Of course usually you would use a pool of chunk objects and reuse them since whenever you unload a chunk behind you, you generally need some new ones before you. Note that this techique can be extended into 3 dimensions, at least for the visual subsections since you generally have to think about all 3 axes. Minecraft uses 16x16x16 sub sections and a single chunk has 16 sub sections (therefore a chunk height of 256)