Advertisement
jtjj222

using 3d noise with 2d noise (with grass)

Aug 18th, 2012
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. /**
  2. * @author jtjj222
  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.     byte getBlock(int x, int y, int z, byte[][] chunk) {
  36.         //if the Block section the block is in hasn't been used yet, allocate it
  37.         if (chunk[y >> 4] == null)
  38.             return 0; //block is air as it hasnt been allocated
  39.         if (!(y <= 256 && y >= 0 && x <= 16 && x >= 0 && z <= 16 && z >= 0))
  40.             return 0;
  41.         try {
  42.             return chunk[y >> 4][((y & 0xF) << 8) | (z << 4) | x];
  43.         } catch (Exception e) {
  44.             e.printStackTrace();
  45.             return 0;
  46.         }
  47.     }
  48.  
  49.     @Override
  50.     /**
  51.      * @param world
  52.      * The world the chunk belongs to
  53.      * @param rand
  54.      * Don't use this, make a new random object using the world seed (world.getSeed())
  55.      * @param biome
  56.      * Use this to set/get the current biome
  57.      * @param ChunkX and ChunkZ
  58.      * The x and z co-ordinates of the current chunk.
  59.      */
  60.     public byte[][] generateBlockSections(World world, Random rand, int ChunkX,
  61.             int ChunkZ, BiomeGrid biome) {
  62.         //where we will store our blocks
  63.         byte[][] chunk = new byte[world.getMaxHeight() / 16][];
  64.        
  65.         SimplexOctaveGenerator overhangs = new SimplexOctaveGenerator(world,8);
  66.         SimplexOctaveGenerator bottoms = new SimplexOctaveGenerator(world,8);
  67.        
  68.         overhangs.setScale(1/64.0); //little note: the .0 is VERY important
  69.         bottoms.setScale(1/128.0);
  70.        
  71.         int overhangsMagnitude = 16; //used when we generate the noise for the tops of the overhangs
  72.         int bottomsMagnitude = 32;
  73.        
  74.         for (int x=0; x<16; x++) {
  75.             for (int z=0; z<16; z++) {
  76.                 int realX = x + ChunkX * 16;
  77.                 int realZ = z + ChunkZ * 16;
  78.                
  79.                 int bottomHeight = (int) (bottoms.noise(realX, realZ, 0.5, 0.5) * bottomsMagnitude + 64);
  80.                 int maxHeight = (int) overhangs.noise(realX, realZ, 0.5, 0.5) * overhangsMagnitude + bottomHeight + 32;
  81.                 double threshold = 0.3;
  82.                
  83.                 //make the terrain
  84.                 for (int y=0; y<maxHeight; y++) {
  85.                     if (y > bottomHeight) { //part where we do the overhangs
  86.                         double density = overhangs.noise(realX, y, realZ, 0.5, 0.5);
  87.                        
  88.                         if (density > threshold) setBlock(x,y,z,chunk,Material.STONE);
  89.                        
  90.                     } else {
  91.                         setBlock(x,y,z,chunk,Material.STONE);
  92.                     }
  93.                 }
  94.                
  95.                 //turn the tops into grass
  96.                 setBlock(x,bottomHeight,z,chunk,Material.GRASS); //the top of the base hills
  97.                 setBlock(x,bottomHeight - 1,z,chunk,Material.DIRT);
  98.                 setBlock(x,bottomHeight - 2,z,chunk,Material.DIRT);
  99.                
  100.                 for (int y=bottomHeight + 1; y>bottomHeight && y < maxHeight; y++ ) { //the overhang
  101.                     int thisblock = getBlock(x, y, z, chunk);
  102.                     int blockabove = getBlock(x, y+1, z, chunk);
  103.                    
  104.                     if(thisblock != Material.AIR.getId() && blockabove == Material.AIR.getId()) {
  105.                         setBlock(x, y, z, chunk, Material.GRASS);
  106.                         if(getBlock(x, y-1, z, chunk) != Material.AIR.getId())
  107.                             setBlock(x, y-1, z, chunk, Material.DIRT);
  108.                         if(getBlock(x, y-2, z, chunk) != Material.AIR.getId())
  109.                             setBlock(x, y-2, z, chunk, Material.DIRT);
  110.                     }
  111.                 }
  112.            
  113.             }
  114.         }
  115.         return chunk;
  116.     }
  117.     /**
  118.      * Returns a list of all of the block populators (that do "little" features)
  119.      * to be called after the chunk generator
  120.      */
  121.     @Override
  122.     public List<BlockPopulator> getDefaultPopulators(World world) {
  123.         ArrayList<BlockPopulator> pops = new ArrayList<BlockPopulator>();
  124.         //Add Block populators here
  125.         return pops;
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement