Advertisement
jtjj222

Using 2d and 3d noise together

Aug 18th, 2012
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.04 KB | None | 0 0
  1.  
  2. //for part 5: using 3d and 2d noise together
  3.  
  4. public class BasicChunkGenerator extends ChunkGenerator {
  5.  
  6.     /**
  7.      *
  8.      * @param x
  9.      * X co-ordinate of the block to be set in the array
  10.      * @param y
  11.      * Y co-ordinate of the block to be set in the array
  12.      * @param z
  13.      * Z co-ordinate of the block to be set in the array
  14.      * @param chunk
  15.      * An array containing the Block id's of all the blocks in the chunk. The first offset
  16.      * is the block section number. There are 16 block sections, stacked vertically, each of which
  17.      * 16 by 16 by 16 blocks.
  18.      * @param material
  19.      * The material to set the block to.
  20.      */
  21.     void setBlock(int x, int y, int z, byte[][] chunk, Material material) {
  22.         //if the Block section the block is in hasn't been used yet, allocate it
  23.         if (chunk[y >> 4] == null)
  24.             chunk[y >> 4] = new byte[16 * 16 * 16];
  25.         if (!(y <= 256 && y >= 0 && x <= 16 && x >= 0 && z <= 16 && z >= 0))
  26.             return;
  27.         try {
  28.             chunk[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = (byte) material
  29.                     .getId();
  30.         } catch (Exception e) {
  31.             // do nothing
  32.         }
  33.     }
  34.  
  35.     @Override
  36.     /**
  37.      * @param world
  38.      * The world the chunk belongs to
  39.      * @param rand
  40.      * Don't use this, make a new random object using the world seed (world.getSeed())
  41.      * @param biome
  42.      * Use this to set/get the current biome
  43.      * @param ChunkX and ChunkZ
  44.      * The x and z co-ordinates of the current chunk.
  45.      */
  46.     public byte[][] generateBlockSections(World world, Random rand, int ChunkX,
  47.             int ChunkZ, BiomeGrid biome) {
  48.         //where we will store our blocks
  49.         byte[][] chunk = new byte[world.getMaxHeight() / 16][];
  50.        
  51.         SimplexOctaveGenerator overhangs = new SimplexOctaveGenerator(world,8);
  52.         SimplexOctaveGenerator bottoms = new SimplexOctaveGenerator(world,8);
  53.        
  54.         overhangs.setScale(1/64.0); //little note: the .0 is VERY important
  55.         bottoms.setScale(1/128.0);
  56.        
  57.         int overhangsMagnitude = 16; //used when we generate the noise for the tops of the overhangs
  58.         int bottomsMagnitude = 32;
  59.        
  60.         for (int x=0; x<16; x++) {
  61.             for (int z=0; z<16; z++) {
  62.                 int realX = x + ChunkX * 16;
  63.                 int realZ = z + ChunkZ * 16;
  64.                
  65.                 int bottomHeight = (int) (bottoms.noise(realX, realZ, 0.5, 0.5) * bottomsMagnitude + 64);
  66.                 int maxHeight = (int) overhangs.noise(realX, realZ, 0.5, 0.5) * overhangsMagnitude + bottomHeight + 32;
  67.                 double threshold = 0.5;
  68.                
  69.                 for (int y=0; y<maxHeight; y++) {
  70.                     if (y > bottomHeight) { //part where we do the overhangs
  71.                         double density = overhangs.noise(realX, y, realZ, 0.5, 0.5);
  72.                        
  73.                         if (density > threshold) setBlock(x,y,z,chunk,Material.STONE);
  74.                        
  75.                     } else {
  76.                         setBlock(x,y,z,chunk,Material.STONE);
  77.                     }
  78.                 }
  79.            
  80.             }
  81.         }
  82.         return chunk;
  83.     }
  84.     /**
  85.      * Returns a list of all of the block populators (that do "little" features)
  86.      * to be called after the chunk generator
  87.      */
  88.     @Override
  89.     public List<BlockPopulator> getDefaultPopulators(World world) {
  90.         ArrayList<BlockPopulator> pops = new ArrayList<BlockPopulator>();
  91.         //Add Block populators here
  92.         return pops;
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement