Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.70 KB | None | 0 0
  1.     // Number of octaves for the noise function
  2.     private static final int OCTAVES = 16;
  3.     private final IBuilder terrainBuilder;
  4.     private final BiomeHeightVolatilitySource biomeSource;
  5.  
  6.     public CustomTerrainGenerator(ICubicWorld world, final long seed) {
  7.         ChunkProviderSettings.Factory factory = new ChunkProviderSettings.Factory();
  8.         factory.setDefaults();
  9.         ChunkProviderSettings conf = factory.build();
  10.         final int selectorOctaves = 8;
  11.         final double selectorFreq = 8.55515/Math.pow(2, selectorOctaves);
  12.         Random rnd = new Random(seed);
  13.         IBuilder selector = NoiseSource.perlin().
  14.             seed(rnd.nextLong()).
  15.             frequency(selectorFreq, selectorFreq*2, selectorFreq).
  16.             octaves(selectorOctaves).
  17.             normalizeTo(-1, 1).
  18.             create().
  19.             mul(25.6).add(0.5).clamp(0, 1);
  20.  
  21.         IBuilder low = NoiseSource.perlin().
  22.             seed(rnd.nextLong()).
  23.             frequency(684.412D/Math.pow(2, OCTAVES)).
  24.             octaves(OCTAVES).
  25.             normalizeTo(-1, 1).
  26.             create().
  27.             mul(2).clamp(-1, 1);
  28.  
  29.         IBuilder high = NoiseSource.perlin().
  30.             seed(rnd.nextLong()).
  31.             frequency(684.412D/Math.pow(2, OCTAVES)).
  32.             octaves(OCTAVES).
  33.             normalizeTo(-1, 1).
  34.             create().
  35.             mul(2).clamp(-1, 1);
  36.  
  37.         int heightmapOctaves = 10;
  38.         double heightmapFreq = 200.0/Math.pow(2, heightmapOctaves);
  39.         IBuilder randomHeight2d = NoiseSource.perlin().
  40.             seed(rnd.nextLong()).
  41.             frequency(heightmapFreq, 0, heightmapFreq).
  42.             octaves(heightmapOctaves).
  43.             normalizeTo(-1, 1).
  44.             create().
  45.             mulIf(NEGATIVE, -0.3).
  46.             mul(3).sub(2).
  47.             clamp(-2, 1).
  48.             divIf(NEGATIVE, 2*2*1.4).
  49.             divIf(NOT_NEGATIVE, 8).
  50.             mul(0.2*17/64.0);
  51.  
  52.         this.biomeSource = new BiomeHeightVolatilitySource(
  53.             world.getBiomeProvider(), 2, X_SECTION_SIZE, Z_SECTION_SIZE);
  54.  
  55.         IBuilder height = biomeSource::getHeight;
  56.         IBuilder volatility = biomeSource::getVolatility;
  57.  
  58.         this.terrainBuilder = selector.
  59.             lerp(low, high).
  60.             mul(volatility.div((x, y, z) -> (y*8/MAX_ELEV < height.get(x, y, z)) ? 4 : 1)).
  61.             add(height).add(randomHeight2d).
  62.             mul(MAX_ELEV).add(64).sub((x, y, z) -> y*8);
  63.     }
  64.  
  65.     /**
  66.      * Generate the cube as the specified location
  67.      *
  68.      * @param cubePrimer cube primer to use
  69.      * @param cubeX cube x location
  70.      * @param cubeY cube y location
  71.      * @param cubeZ cube z location
  72.      */
  73.     public void generate(final ICubePrimer cubePrimer, int cubeX, int cubeY, int cubeZ) {
  74.         CubePos cubePos = new CubePos(cubeX, cubeY, cubeZ);
  75.         BlockPos start = cubePos.getMinBlockPos();
  76.         BlockPos end = cubePos.getMaxBlockPos();
  77.         biomeSource.setChunk(cubeX, cubeZ);
  78.         terrainBuilder.scaledStream(start, end, new Vec3i(4, 8, 4)).map(this::getBlock).forEach(b -> b.setBlock(cubePrimer));
  79.     }
  80.  
  81.     /**
  82.      * Retrieve the blockstate appropriate for the specified builder entry
  83.      *
  84.      * @return The block state
  85.      */
  86.     private BlockStateInstance getBlock(IBuilder.IExtendedEntry entry) {
  87.         int x = entry.getX();
  88.         int y = entry.getY();
  89.         int z = entry.getZ();
  90.         double density = entry.getValue();
  91.         double yGrad = entry.getYGradient();
  92.         Biome biome = biomeSource.getBiome(x, y, z);
  93.  
  94.         final int seaLevel = 64;
  95.         final double dirtDepth = 4;
  96.         IBlockState state = Blocks.AIR.getDefaultState();
  97.         if (density > 0) {
  98.             state = Blocks.STONE.getDefaultState();
  99.             //if the block above would be empty:
  100.             if (density + yGrad <= 0) {
  101.                 if (y < seaLevel - 1) {
  102.                     state = biome.fillerBlock;
  103.                 } else {
  104.                     state = biome.topBlock;
  105.                 }
  106.                 //if density decreases as we go up && density < dirtDepth
  107.             } else if (yGrad < 0 && density < dirtDepth) {
  108.                 state = biome.fillerBlock;
  109.             }
  110.         } else if (y < seaLevel) {
  111.             // TODO replace check with GlobalGeneratorConfig.SEA_LEVEL
  112.             state = Blocks.WATER.getDefaultState();
  113.         }
  114.         return new BlockStateInstance(state, x, y, z);
  115.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement