[INTERMEDIATE] Infinite terrain generation using SimplexOctaves

Discussion in 'Resources' started by codename_B, Jul 30, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    codename_B

    Many of you are making flatmaps - and frankly while this is a good way to learn the basics I'm finding it annoying how many flatmaps there are now - get some hilly terrain down ye!

    Code:
    public byte[] generate(World world, Random random, int chunkX, int chunkZ) {
    // This is just the blank byte array for the chunk
    byte[] b = new byte[16*16*128];
    // This ensures a set random. (Let's not go into it, it just works.)
    Random seed = new Random(world.getSeed());
    // This generates an "octave generator" from the random - the same one each time
    SimplexOctaveGenerator gen =  new SimplexOctaveGenerator(seed, 8);
    // This set's the "scale" ie how spread out it is - 64 is a good number for hilly terrain
    gen.setScale(1/64.0);
    // This loops through the chunk
    for (int x=0; x<16; x++){
                for (int z=0; z<16; z++){
                    // This generates noise based on the absolute x and z (between -1 and 1) - it will be smooth noise if all goes well - multiply it by the desired value - 16 is good for hilly terrain.
                    double noise = gen.noise(x+chunkX*16, z+chunkZ*16, 0.5, 0.5)*16;
                    for(int y=0; y<32+noise; y++){
                        // Just some checks I use - you don't need this.
                        if(b[xyzToByte(x,y,z)] ==0)
                        // Obviously sets that byte[] corresponding to the chunk x,y,z to stone - you can use your preferred way of doing this.
                        b[xyzToByte(x,y,z)] = (byte)  (Material.STONE).getId();
                    }
                }
            }
    
    return b;
    }
    
    There you have it - basic hilly terrain - don't jump straight into this, read my first few tutorials on the basic side of WGEN first - firstly because xyzToByte(x,y,z) is included in those - and also because this is a little more complicated.
     
    PDKnight, ferrybig, MasterAsp and 5 others like this.
  2. Offline

    ZNickq

    Nice! :)
     
Thread Status:
Not open for further replies.

Share This Page