I recently got the platformer terrain generation working, and it looks amazing. I'm extremely happy with the result but I just have a few questions. I'm trying to generate worlds as big as Terraria which is a few million blocks. My blocks are 32x32 and when I start to scale the numbers up to make my world bigger it starts taking WAY longer to generate. It takes me about 14 seconds to generate 1.3 million blocks and my PC has a fast processor as well. It's not the rendering, it's the actual generation. I'll post a part of my generation code and I'm wondering how to speed up the performance? I haven't even gotten to the part where I generate ores, dirt, grass, trees, etc. so I'm hoping that I'm just using something wrong. Here's the code:
/*
* This will be later to split up the rendering. I've tried generating individual chunks but the noise doesn't work well, it needs to be generated all at once
*/
List<Chunk> world = new ArrayList<>();
List<Block> blocks = new ArrayList<>();
Module gen = Noise.getNoiseMod();
float px, py, raw;
// Carve basic world shape with air and stone
for (int x = 0; x < WIDTH; x += Block.WIDTH)
{
for (int y = 0; y < HEIGHT; y += Block.HEIGHT)
{
px = x / (float) WIDTH;
py = y / (float) HEIGHT;
raw = (float) gen.get(px, py);
raw = Math.max(0, Math.min(1, raw));
if (raw == 1)
{
blocks.add(new Block(BlockType.STONE, x, y));
}
else
{
blocks.add(new Block(BlockType.AIR, x, y));
}
}
}
Chunk c = new Chunk(blocks); // For use with generating ores, etc.
// Add ores - Here's where I need the most performance because I'll be looping through the world and checking for certain conditions
/*for (int x = 0; x < WIDTH; x += Block.WIDTH)
{
for (int y = 0; y < HEIGHT; y += Block.HEIGHT)
{
}
}*/
float end = (System.currentTimeMillis() - start) / 1000;
System.out.println("Finished generating world (" + blocks.size() + " blocks) in " + end + " seconds");
world.add(c);
return world;