Advertisement
jtjj222

Part 5: code section 1

Aug 17th, 2012
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //for part 5 in the series: http://forums.bukkit.org/threads/the-always-up-to-date-definitive-guide-to-terrain-generation-part-one-prerequisites-and-setup.93982/
  2. public class BasicChunkGenerator extends ChunkGenerator {
  3.  
  4.     /**
  5.      *
  6.      * @param x
  7.      * X co-ordinate of the block to be set in the array
  8.      * @param y
  9.      * Y co-ordinate of the block to be set in the array
  10.      * @param z
  11.      * Z co-ordinate of the block to be set in the array
  12.      * @param chunk
  13.      * An array containing the Block id's of all the blocks in the chunk. The first offset
  14.      * is the block section number. There are 16 block sections, stacked vertically, each of which
  15.      * 16 by 16 by 16 blocks.
  16.      * @param material
  17.      * The material to set the block to.
  18.      */
  19.     void setBlock(int x, int y, int z, byte[][] chunk, Material material) {
  20.         //if the Block section the block is in hasn't been used yet, allocate it
  21.         if (chunk[y >> 4] == null)
  22.             chunk[y >> 4] = new byte[16 * 16 * 16];
  23.         if (!(y <= 256 && y >= 0 && x <= 16 && x >= 0 && z <= 16 && z >= 0))
  24.             return;
  25.         try {
  26.             chunk[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = (byte) material
  27.                     .getId();
  28.         } catch (Exception e) {
  29.             // do nothing
  30.         }
  31.     }
  32.  
  33.     @Override
  34.     /**
  35.      * @param world
  36.      * The world the chunk belongs to
  37.      * @param rand
  38.      * Don't use this, make a new random object using the world seed (world.getSeed())
  39.      * @param biome
  40.      * Use this to set/get the current biome
  41.      * @param ChunkX and ChunkZ
  42.      * The x and z co-ordinates of the current chunk.
  43.      */
  44.     public byte[][] generateBlockSections(World world, Random rand, int ChunkX,
  45.             int ChunkZ, BiomeGrid biome) {
  46.        
  47.         SimplexOctaveGenerator gen1 = new SimplexOctaveGenerator(world,8);
  48.         gen1.setScale(1/32.0); //how "scaled" the noise generator should be.
  49.        
  50.         byte[][] chunk = new byte[world.getMaxHeight() / 16][];
  51.        
  52.         for (int x=0; x<16; x++) {
  53.             for (int z=0; z<16; z++) {
  54.                
  55.                 int realX = x + ChunkX * 16; //used so that the noise function gives us
  56.                 int realZ = z + ChunkZ * 16; //different values each chunk
  57.  
  58.                 for (int y=64;y<128;y++) {
  59.                     double density = gen1.noise(realX,y, realZ, 0.5, 0.5); //note 3d noise is VERY slow, I recommend using 2d noise to limit the number of 3d noise values that must be calculated.
  60.                     double threshold = 0.0; //the noise function returns values between -1 and 1.
  61.                     if (density > threshold) {
  62.                         setBlock(x,y,z,chunk,Material.STONE);
  63.                     }                    
  64.                 }
  65.             }
  66.         }
  67.         return chunk;
  68.     }
  69.     /**
  70.      * Returns a list of all of the block populators (that do "little" features)
  71.      * to be called after the chunk generator
  72.      */
  73.     @Override
  74.     public List<BlockPopulator> getDefaultPopulators(World world) {
  75.         ArrayList<BlockPopulator> pops = new ArrayList<BlockPopulator>();
  76.         //Add Block populators here
  77.         return pops;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement