Minecraft-esque chunks!
I want (no... need!) to gain a better understanding on manipulation of chunks, so why not attempt to learn in a environment that I'm an expert at; Garry's Mod! Main reason for this, is that I'm currently making an indie-game using Unity3D (yes... its language is in C# and JavaScript, I have my ways.), but for the life of me, I cannot start, since terrain generation is the ONLY way for me to properly start (I'm weird). Using my previous knowledge of how to render pixels, generating chunks has become easy. Here's a little snippet on generation:
Code:
for x = 0, math.floor(RENDER_DISTANCE / 2) do
for y = 0, math.floor(RENDER_DISTANCE / 2) do
CreateChunk( Vector( self.Chunk.x + x, self.Chunk.y + y, 1) )
CreateChunk( Vector( self.Chunk.x - x, self.Chunk.y - y, 1) )
CreateChunk( Vector( self.Chunk.x - x, self.Chunk.y + y, 1) )
CreateChunk( Vector( self.Chunk.x + x, self.Chunk.y - y, 1) )
end
end
First, two FOR loops are created, one for the X COORD and one for the Y COORD (Z COORD can be done as well for infinite terrains in ALL axes). In the last FOR loop, generate your chunks in there. The function 'CreateChunk( Vector )' is a function defined outside of the loop. All it does, is create the chunk entity, and set it's position according to what's in the Vector given. The reason for 'CreateChunk( Vector )' four times, is for radial generation (Where it generates around the current chunk.) This achieves Minecraft-esque chunks, where the first chunk is created for loading the game, and after the game loads, chunks around the player loads.
After ALL players are an X amount (where X is the size of the RENDER_DISTANCE halved * the chunk size) away from any chunk, it saves into a .txt file (currently saves the color and position ), and radially generates EVERY Player's current chunk (Saved to a NWString). From there, infinite-cy is attained.
There are... complications with this system.
First off, there's chunk errors, not much of a rarity (I hate fucking ponies...) in Minecraft. This can be created by using the PhysGun to move the chunk. I'm trying to patch this error, but it can happen.
Chunk error!
Second, there's a VERY RARE chance of 'ED_Alloc: No Free Edicts' (Layman's Terms: Out of Memory). I'm implementing a limiter, where a maximum of 512 chunks (Entities cap is 2048) can exist in The Game (I lost...)
Third, performance, as it is an issue with Minecraft with too many chunks loaded. I would love to find alternative ways for a lot of performance-whoring actions, but there's not much. Only Think() and StartTouch() are the only performance whore processes, that I know of/are, that are being used.
Next on the agenda:
I plan on using structure generation (spawning homes here and there) based on a world seed, as well as saving EVERYTHING inside of the chunk. I have an idea on saving the contents of a chunk, it's not that hard.
Edited:
Note: I rewritten this post, as most of the things I said have changed.