Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Number of octaves for the noise function
- private static final int OCTAVES = 16;
- private final IBuilder terrainBuilder;
- private final BiomeHeightVolatilitySource biomeSource;
- public CustomTerrainGenerator(ICubicWorld world, final long seed) {
- ChunkProviderSettings.Factory factory = new ChunkProviderSettings.Factory();
- factory.setDefaults();
- ChunkProviderSettings conf = factory.build();
- final int selectorOctaves = 8;
- final double selectorFreq = 8.55515/Math.pow(2, selectorOctaves);
- Random rnd = new Random(seed);
- IBuilder selector = NoiseSource.perlin().
- seed(rnd.nextLong()).
- frequency(selectorFreq, selectorFreq*2, selectorFreq).
- octaves(selectorOctaves).
- normalizeTo(-1, 1).
- create().
- mul(25.6).add(0.5).clamp(0, 1);
- IBuilder low = NoiseSource.perlin().
- seed(rnd.nextLong()).
- frequency(684.412D/Math.pow(2, OCTAVES)).
- octaves(OCTAVES).
- normalizeTo(-1, 1).
- create().
- mul(2).clamp(-1, 1);
- IBuilder high = NoiseSource.perlin().
- seed(rnd.nextLong()).
- frequency(684.412D/Math.pow(2, OCTAVES)).
- octaves(OCTAVES).
- normalizeTo(-1, 1).
- create().
- mul(2).clamp(-1, 1);
- int heightmapOctaves = 10;
- double heightmapFreq = 200.0/Math.pow(2, heightmapOctaves);
- IBuilder randomHeight2d = NoiseSource.perlin().
- seed(rnd.nextLong()).
- frequency(heightmapFreq, 0, heightmapFreq).
- octaves(heightmapOctaves).
- normalizeTo(-1, 1).
- create().
- mulIf(NEGATIVE, -0.3).
- mul(3).sub(2).
- clamp(-2, 1).
- divIf(NEGATIVE, 2*2*1.4).
- divIf(NOT_NEGATIVE, 8).
- mul(0.2*17/64.0);
- this.biomeSource = new BiomeHeightVolatilitySource(
- world.getBiomeProvider(), 2, X_SECTION_SIZE, Z_SECTION_SIZE);
- IBuilder height = biomeSource::getHeight;
- IBuilder volatility = biomeSource::getVolatility;
- this.terrainBuilder = selector.
- lerp(low, high).
- mul(volatility.div((x, y, z) -> (y*8/MAX_ELEV < height.get(x, y, z)) ? 4 : 1)).
- add(height).add(randomHeight2d).
- mul(MAX_ELEV).add(64).sub((x, y, z) -> y*8);
- }
- /**
- * Generate the cube as the specified location
- *
- * @param cubePrimer cube primer to use
- * @param cubeX cube x location
- * @param cubeY cube y location
- * @param cubeZ cube z location
- */
- public void generate(final ICubePrimer cubePrimer, int cubeX, int cubeY, int cubeZ) {
- CubePos cubePos = new CubePos(cubeX, cubeY, cubeZ);
- BlockPos start = cubePos.getMinBlockPos();
- BlockPos end = cubePos.getMaxBlockPos();
- biomeSource.setChunk(cubeX, cubeZ);
- terrainBuilder.scaledStream(start, end, new Vec3i(4, 8, 4)).map(this::getBlock).forEach(b -> b.setBlock(cubePrimer));
- }
- /**
- * Retrieve the blockstate appropriate for the specified builder entry
- *
- * @return The block state
- */
- private BlockStateInstance getBlock(IBuilder.IExtendedEntry entry) {
- int x = entry.getX();
- int y = entry.getY();
- int z = entry.getZ();
- double density = entry.getValue();
- double yGrad = entry.getYGradient();
- Biome biome = biomeSource.getBiome(x, y, z);
- final int seaLevel = 64;
- final double dirtDepth = 4;
- IBlockState state = Blocks.AIR.getDefaultState();
- if (density > 0) {
- state = Blocks.STONE.getDefaultState();
- //if the block above would be empty:
- if (density + yGrad <= 0) {
- if (y < seaLevel - 1) {
- state = biome.fillerBlock;
- } else {
- state = biome.topBlock;
- }
- //if density decreases as we go up && density < dirtDepth
- } else if (yGrad < 0 && density < dirtDepth) {
- state = biome.fillerBlock;
- }
- } else if (y < seaLevel) {
- // TODO replace check with GlobalGeneratorConfig.SEA_LEVEL
- state = Blocks.WATER.getDefaultState();
- }
- return new BlockStateInstance(state, x, y, z);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement