I'm trying to learn about procedural 2D world generation but I'm a little confused about something. I already have a good algorithm (http://www.roguebasin.com/index.php?title=Cellular_Automata_Method_for_Generating_Random_Cave-Like_Levels) that randomly generates the kind of terrain i want, but the problem is I don't know how to scale this effectively for 'infinitely' large worlds.
One of the simple obvious solutions i thought of was to just use a 2D array like so:
int[][] field = new int[Short.MAX_VALUE][Short.MAX_VALUE];
Where each int in the array represented information on a tile and it's index values represented the coordinates of each tile. I would then just simply run the algorithm on the field to generate the level's terrain.
Problem is, this line of code doesn't work. I get a java.lang.OutOfMemoryError. Even if it did work i'd imagine it would take forever just to populate the field.
It's obvious i need to split the field so that i can just run the population algorithm onto the local area near the player, and dynamically generate the rest of the terrain as the player walks around.
I'm thinking of splitting the world into chunks. I'm not really sure how to do this though. If all I did was split the field into chunks and populate them individually, then i'd have abrupt terrain changes at the border between each chunk. I need the terrain to be consistent accross chunks. It seems if I went for this approach I would need each chunk to "know about" its neighbouring chunks as its being generated to ensure consistent, seamless terrain between them.
I'd imagine i'd have to do something like check the 8 neighbouring 2D arrays together, patch together the ones that are loaded, running the population algorithm, then splitting the array again to form the target chunk. It seems like a really complicated solution i don't think its a good one. Can anyone give me tips?