Skip to content

Instantly share code, notes, and snippets.

@i509VCB
Created July 18, 2019 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i509VCB/13290d8b43fff4308b63c46861865cc9 to your computer and use it in GitHub Desktop.
Save i509VCB/13290d8b43fff4308b63c46861865cc9 to your computer and use it in GitHub Desktop.
World Generator sample
package me.i509.tests.worldgen;
import java.util.Random;
import java.util.SplittableRandom;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Biome;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Snow;
import org.bukkit.generator.ChunkGenerator;
import org.bukkit.util.noise.SimplexOctaveGenerator;
import me.i509.generator.noise.SplittableSimplexOctaveGenerator;
public class BukkitGenerator extends ChunkGenerator {
private double localScale = 102.75;
int currentHeight = 50;
private double powOffset = 1.15;
public BukkitGenerator() {
}
@Override
public ChunkData generateChunkData(World world, Random random, int chunkX, int chunkZ, BiomeGrid biome) {
SimplexOctaveGenerator generator = new SimplexOctaveGenerator(new Random(world.getSeed()), 8);
SimplexOctaveGenerator generator2 = new SimplexOctaveGenerator(new Random(world.getSeed() + 63L), 4);
SimplexOctaveGenerator generator3 = new SimplexOctaveGenerator(new Random(world.getSeed() + 509L), 8);
SimplexOctaveGenerator generator4 = new SimplexOctaveGenerator(new Random(world.getSeed() + 241234509L), 8);
ChunkData chunk = createChunkData(world);
generator.setScale(0.005D);
generator2.setScale(0.005D);
generator3.setScale(0.005D);
generator4.setScale(0.007D);
doBiomes(world, random, chunkX, chunkZ, biome);
for (int X = 0; X < 16; X++)
for (int Z = 0; Z < 16; Z++) {
if(biome.getBiome(X, Z)==Biome.BADLANDS) {
chunk.setBlock(X, /*currentHeight*/1, Z, Material.RED_TERRACOTTA);
} else if (biome.getBiome(X, Z)==Biome.ERODED_BADLANDS) {
chunk.setBlock(X, /*currentHeight*/1, Z, Material.RED_SANDSTONE);
} else if (biome.getBiome(X, Z)==Biome.BADLANDS_PLATEAU) {
chunk.setBlock(X, /*currentHeight*/1, Z, Material.TERRACOTTA);
} else if (biome.getBiome(X, Z)==Biome.SAVANNA) {
chunk.setBlock(X, /*currentHeight*/1, Z, Material.GRASS_PATH);
} else if (biome.getBiome(X, Z)==Biome.DESERT) {
chunk.setBlock(X, /*currentHeight*/1, Z, Material.SANDSTONE);
} else if (biome.getBiome(X, Z)==Biome.SNOWY_MOUNTAINS) {
chunk.setBlock(X, /*currentHeight*/2, Z, Material.SNOW.createBlockData());
chunk.setBlock(X, /*currentHeight*/1, Z, Material.GRAVEL);
} else if (biome.getBiome(X, Z)==Biome.BEACH) { // Nics
chunk.setBlock(X, /*currentHeight*/1, Z, Material.SAND);
chunk.setBlock(X, /*currentHeight*/1, Z, Material.STONE);
} else if (biome.getBiome(X, Z)==Biome.PLAINS) {
chunk.setBlock(X, /*currentHeight*/1, Z, Material.GRASS_BLOCK);
} else if (biome.getBiome(X, Z)==Biome.TAIGA) {
chunk.setBlock(X, /*currentHeight*/1, Z, Material.PODZOL); // Nics
} else {
chunk.setBlock(X, /*currentHeight*/1, Z, Material.WATER);
}
//currentHeight = heightAt(chunkX, chunkZ, X, Z, generator, generator2, generator3, generator4);
// chunk.setBlock(X, currentHeight, Z, Material.GRASS_BLOCK);
// chunk.setBlock(X, currentHeight - 1, Z, Material.DIRT);
//for (int i = currentHeight - 2; i > 0; i--)
// chunk.setBlock(X, i, Z, Material.STONE);
//chunk.setBlock(X, 0, Z, Material.BEDROCK);
}
return chunk;
}
private void doBiomes(World world, Random random, int chunkX, int chunkZ, BiomeGrid biome) {
SimplexOctaveGenerator height = new SimplexOctaveGenerator(new Random(world.getSeed()), 24);
SimplexOctaveGenerator height2 = new SimplexOctaveGenerator(new Random(world.getSeed() + 63L), 16);
SimplexOctaveGenerator humidity = new SimplexOctaveGenerator(new Random(world.getSeed() + 509L), 12);
SimplexOctaveGenerator humidity2 = new SimplexOctaveGenerator(new Random(world.getSeed() + 241234509L), 128);
SimplexOctaveGenerator humidity3 = new SimplexOctaveGenerator(new Random(world.getSeed() + 1234509L), 4);
height.setScale(0.005D);
height2.setScale(0.002D);
humidity.setScale(0.0095D);
humidity2.setScale(0.007D);
humidity3.setScale(0.0024D);
for (int X = 0; X < 16; X++)
for (int Z = 0; Z < 16; Z++) {
double globalHeight = 0;
double humidityGlobal = 0;
globalHeight += height.noise(LocationUtil.toGlobal1D(chunkX, X), LocationUtil.toGlobal1D(chunkZ, Z), 0.05D, 0.05D);
globalHeight += Math.pow(height2.noise(LocationUtil.toGlobal1D(chunkX, X), LocationUtil.toGlobal1D(chunkZ, Z), 0.032D, 0.05D), powOffset); // 0.05, 0.05
globalHeight/=1.72; //2
humidityGlobal += LazyMath.average(
Math.pow(humidity.noise(LocationUtil.toGlobal1D(chunkX, X), LocationUtil.toGlobal1D(chunkZ, Z), 0.0075D, 0.0899D), 1.023),
humidity2.noise(LocationUtil.toGlobal1D(chunkX, X), LocationUtil.toGlobal1D(chunkZ, Z), 0.0075D, 0.0899D));
humidityGlobal += Math.pow(humidity3.noise(LocationUtil.toGlobal1D(chunkX, X), LocationUtil.toGlobal1D(chunkZ, Z), 0.0075D, 0.0899D), 1.31);
humidityGlobal/=1.23;
System.out.println("Lerp:" + LazyMath.lerp(globalHeight, humidityGlobal, 0.00234));
if(humidityGlobal<0.05) {
if(globalHeight<0.33) {
biome.setBiome(X, Z, Biome.BADLANDS); // IS
} else if (globalHeight<=0.33 || globalHeight>=0.66) {// flip signs
biome.setBiome(X, Z, Biome.ERODED_BADLANDS); // IS
} else if (globalHeight>0.66) {
biome.setBiome(X, Z, Biome.BADLANDS_PLATEAU); // IS
}
} else if (humidityGlobal>=0.05 && humidityGlobal<=0.59) {
if(globalHeight<0.33) {
biome.setBiome(X, Z, Biome.SAVANNA); // IS
} else if (globalHeight<=0.33 || globalHeight>=0.66) {// flip signs
biome.setBiome(X, Z, Biome.DESERT); // IS
} else if (globalHeight>0.66) {
biome.setBiome(X, Z, Biome.SNOWY_MOUNTAINS);
}
} else if (humidityGlobal>0.59) {
if(globalHeight<0.33) {
biome.setBiome(X, Z, Biome.BEACH); // Lowest works
} else if (globalHeight<=0.33 || globalHeight>=0.66) {// flip signs
biome.setBiome(X, Z, Biome.PLAINS);
} else if (globalHeight>0.66) {
biome.setBiome(X, Z, Biome.TAIGA); // Highest works
}
}
}
}
/**
* Gets height at position including lerping
*
* @param chunkX
* @param chunkZ
* @param x
* @param z
* @return
*/
private int heightAt(int chunkX, int chunkZ, int X, int Z, SimplexOctaveGenerator generator,
SimplexOctaveGenerator generator2, SimplexOctaveGenerator generator3, SimplexOctaveGenerator generator4) {
int total = 0;
if (LazyMath.average(generator3.noise(chunkX * 16 + X, chunkZ * 16 + Z, 0.0025D, 0.001D),
generator4.noise(chunkX * 16 + X, chunkZ * 16 + Z, 0.0025D, 0.001D)) > 0.65) {
for (int cx = -2; cx < 3; ++cx)
for (int cz = -2; cz < 3; ++cz) {
total = (int) (total + ((generator.noise(chunkX * 16 + X, chunkZ * 16 + Z, 0.0025D, 0.001D)
+ generator2.noise(chunkX * 16 + X + cx, chunkZ * 16 + Z + cz, 0.0025D, 0.001D)) * 2.5D
+ 64D));
}
}
return total / 25;
}
/**
* Average out the surronding noise, this is mainly for biome combinations
*/
private double interpolateNoise(int chunkX, int chunkZ, int x, int z, SimplexOctaveGenerator ds) {
double total = 0;
for (int cx = -2; cx < 3; ++cx)
for (int cz = -2; cz < 3; ++cz) {
total = total + (ds.noise(chunkX * 16 + x + cx, chunkZ * 16 + z + cz, 0.5D, 0.5D) * 2);
}
return total /= 25;
}
/*
* @Override public List<BlockPopulator> getDefaultPopulators(World world) {
* return null; }
*/
@Override
public boolean isParallelCapable() {
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment