From baa863d6e1ffd92443aa89c6768a60341dbe4d6e Mon Sep 17 00:00:00 2001 From: valoeghese Date: Fri, 28 Feb 2020 20:23:27 +1300 Subject: [PATCH] e --- .../github/halotroop/litecraft/Litecraft.java | 2 +- .../litecraft/save/LitecraftSave.java | 2 +- .../litecraft/types/block/Block.java | 3 ++ .../util/noise/OctaveSimplexNoise.java | 25 +++++++++++++-- .../halotroop/litecraft/world/Chunk.java | 2 +- .../halotroop/litecraft/world/World.java | 1 + .../world/{gen => dimension}/Dimension.java | 4 ++- .../world/{gen => dimension}/Dimensions.java | 4 ++- .../world/gen/OverworldChunkGenerator.java | 32 ++++++++++++++++--- .../world/gen/OverworldDimension.java | 2 ++ 10 files changed, 65 insertions(+), 12 deletions(-) rename src/main/java/com/github/halotroop/litecraft/world/{gen => dimension}/Dimension.java (86%) rename src/main/java/com/github/halotroop/litecraft/world/{gen => dimension}/Dimensions.java (54%) diff --git a/src/main/java/com/github/halotroop/litecraft/Litecraft.java b/src/main/java/com/github/halotroop/litecraft/Litecraft.java index f383aa2..5e52f79 100644 --- a/src/main/java/com/github/halotroop/litecraft/Litecraft.java +++ b/src/main/java/com/github/halotroop/litecraft/Litecraft.java @@ -9,7 +9,7 @@ import com.github.halotroop.litecraft.screens.TitleScreen; import com.github.halotroop.litecraft.types.block.Blocks; import com.github.halotroop.litecraft.util.RelativeDirection; import com.github.halotroop.litecraft.world.World; -import com.github.halotroop.litecraft.world.gen.Dimensions; +import com.github.halotroop.litecraft.world.dimension.Dimensions; import com.github.hydos.ginger.engine.api.*; import com.github.hydos.ginger.engine.api.game.*; import com.github.hydos.ginger.engine.cameras.*; diff --git a/src/main/java/com/github/halotroop/litecraft/save/LitecraftSave.java b/src/main/java/com/github/halotroop/litecraft/save/LitecraftSave.java index eb63571..58778a9 100644 --- a/src/main/java/com/github/halotroop/litecraft/save/LitecraftSave.java +++ b/src/main/java/com/github/halotroop/litecraft/save/LitecraftSave.java @@ -7,7 +7,7 @@ import org.joml.Vector3f; import com.github.halotroop.litecraft.Litecraft; import com.github.halotroop.litecraft.world.*; -import com.github.halotroop.litecraft.world.gen.Dimension; +import com.github.halotroop.litecraft.world.dimension.Dimension; import com.github.hydos.ginger.engine.cameras.Camera; import com.github.hydos.ginger.engine.elements.objects.Player; diff --git a/src/main/java/com/github/halotroop/litecraft/types/block/Block.java b/src/main/java/com/github/halotroop/litecraft/types/block/Block.java index 62f9376..8d9d073 100644 --- a/src/main/java/com/github/halotroop/litecraft/types/block/Block.java +++ b/src/main/java/com/github/halotroop/litecraft/types/block/Block.java @@ -57,5 +57,8 @@ public class Block public static final Block getBlock(String identifier) { return IDENTIFIER_TO_BLOCK.get(identifier); } + public static final Block getBlockOrAir(String identifier) + { return IDENTIFIER_TO_BLOCK.getOrDefault(identifier, Blocks.AIR); } + private static final Map IDENTIFIER_TO_BLOCK = new HashMap<>(); } diff --git a/src/main/java/com/github/halotroop/litecraft/util/noise/OctaveSimplexNoise.java b/src/main/java/com/github/halotroop/litecraft/util/noise/OctaveSimplexNoise.java index 8ed977a..d58217c 100644 --- a/src/main/java/com/github/halotroop/litecraft/util/noise/OctaveSimplexNoise.java +++ b/src/main/java/com/github/halotroop/litecraft/util/noise/OctaveSimplexNoise.java @@ -8,6 +8,11 @@ public final class OctaveSimplexNoise private double clamp; private double spread, amplitudeLow, amplitudeHigh; + public OctaveSimplexNoise(Random rand, int octaves) + { + this(rand, octaves, 1D, 1D, 1D); + } + public OctaveSimplexNoise(Random rand, int octaves, double spread, double amplitudeHigh, double amplitudeLow) { this.samplers = new SimplexNoise[octaves]; @@ -21,12 +26,26 @@ public final class OctaveSimplexNoise public double sample(double x, double y) { - double amplFreq = 0.5D; + double amplSpread = 0.5D; double result = 0; for (SimplexNoise sampler : this.samplers) { - result += (amplFreq * sampler.sample(x / (amplFreq * this.spread), y / (amplFreq * this.spread))); - amplFreq *= 0.5D; + result += (amplSpread * sampler.sample(x / (amplSpread * this.spread), y / (amplSpread * this.spread))); + amplSpread *= 0.5D; + } + result = result * this.clamp; + return result > 0 ? result * this.amplitudeHigh : result * this.amplitudeLow; + } + + public double sample(double x, double y, double z) + { + double amplSpread = 0.5D; + double result = 0; + for (SimplexNoise sampler : this.samplers) + { + double divisor = amplSpread * this.spread; + result += (amplSpread * sampler.sample(x / divisor, y / divisor, z / divisor)); + amplSpread *= 0.5D; } result = result * this.clamp; return result > 0 ? result * this.amplitudeHigh : result * this.amplitudeLow; diff --git a/src/main/java/com/github/halotroop/litecraft/world/Chunk.java b/src/main/java/com/github/halotroop/litecraft/world/Chunk.java index be7eb4e..113b8a2 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/Chunk.java +++ b/src/main/java/com/github/halotroop/litecraft/world/Chunk.java @@ -165,7 +165,7 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage } else { - palette.put(intIdCache, Block.getBlock((String) o)); + palette.put(intIdCache, Block.getBlockOrAir((String) o)); readInt = true; } } diff --git a/src/main/java/com/github/halotroop/litecraft/world/World.java b/src/main/java/com/github/halotroop/litecraft/world/World.java index 975da56..632f473 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/World.java +++ b/src/main/java/com/github/halotroop/litecraft/world/World.java @@ -8,6 +8,7 @@ import org.joml.Vector3f; import com.github.halotroop.litecraft.save.LitecraftSave; import com.github.halotroop.litecraft.types.block.Block; import com.github.halotroop.litecraft.world.block.BlockRenderer; +import com.github.halotroop.litecraft.world.dimension.Dimension; import com.github.halotroop.litecraft.world.gen.*; import com.github.hydos.ginger.engine.elements.objects.Player; import com.github.hydos.ginger.engine.obj.ModelLoader; diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/Dimension.java b/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimension.java similarity index 86% rename from src/main/java/com/github/halotroop/litecraft/world/gen/Dimension.java rename to src/main/java/com/github/halotroop/litecraft/world/dimension/Dimension.java index 1f76fb7..8c1f61e 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/Dimension.java +++ b/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimension.java @@ -1,7 +1,9 @@ -package com.github.halotroop.litecraft.world.gen; +package com.github.halotroop.litecraft.world.dimension; import java.util.*; +import com.github.halotroop.litecraft.world.gen.*; + import it.unimi.dsi.fastutil.ints.*; public abstract class Dimension diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/Dimensions.java b/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimensions.java similarity index 54% rename from src/main/java/com/github/halotroop/litecraft/world/gen/Dimensions.java rename to src/main/java/com/github/halotroop/litecraft/world/dimension/Dimensions.java index 1490943..e915800 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/Dimensions.java +++ b/src/main/java/com/github/halotroop/litecraft/world/dimension/Dimensions.java @@ -1,4 +1,6 @@ -package com.github.halotroop.litecraft.world.gen; +package com.github.halotroop.litecraft.world.dimension; + +import com.github.halotroop.litecraft.world.gen.*; public final class Dimensions { diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/OverworldChunkGenerator.java b/src/main/java/com/github/halotroop/litecraft/world/gen/OverworldChunkGenerator.java index 96a7455..2b3e6bc 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/OverworldChunkGenerator.java +++ b/src/main/java/com/github/halotroop/litecraft/world/gen/OverworldChunkGenerator.java @@ -10,11 +10,14 @@ public class OverworldChunkGenerator implements ChunkGenerator, WorldGenConstant { public OverworldChunkGenerator(long seed, int dimension) { - this.noise = new OctaveSimplexNoise(new Random(seed), 3, 250.0, 35.0, 10.0); + Random rand = new Random(seed); + this.noise = new OctaveSimplexNoise(rand, 3, 250.0, 35.0, 10.0); + this.stoneNoise = new OctaveSimplexNoise(rand, 1); this.dimension = dimension; } private final OctaveSimplexNoise noise; + private final OctaveSimplexNoise stoneNoise; private final int dimension; @Override @@ -26,19 +29,40 @@ public class OverworldChunkGenerator implements ChunkGenerator, WorldGenConstant double totalX = x + chunk.chunkStartX; for (int z = 0; z < CHUNK_SIZE; z++) { - int height = (int) this.noise.sample(totalX, chunk.chunkStartZ + z); + double totalZ = chunk.chunkStartZ + z; + int height = (int) this.noise.sample(totalX, totalZ); for (int y = 0; y < CHUNK_SIZE; y++) { + double rockNoise = this.stoneNoise.sample(totalX / 160.0, (chunk.chunkStartY + y) / 50.0, totalZ / 160.0); int totalY = chunk.chunkStartY + y; Block block = Blocks.AIR; if (totalY < height - 3) - block = Blocks.DIRT; + block = pickStone(rockNoise); else if (totalY < height) - block = Blocks.GNEISS; + block = Blocks.DIRT; chunk.setBlock(x, y, z, block); } } } return chunk; } + + private static Block pickStone(double rockNoise) { + if (rockNoise < -0.25) + { + return Blocks.ANDESITE; + } + else if (rockNoise < 0) + { + return Blocks.DIORITE; + } + else if (rockNoise < 0.25) + { + return Blocks.GNEISS; + } + else + { + return Blocks.GRANITE; + } + } } diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/OverworldDimension.java b/src/main/java/com/github/halotroop/litecraft/world/gen/OverworldDimension.java index b47663d..4f3f08b 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/OverworldDimension.java +++ b/src/main/java/com/github/halotroop/litecraft/world/gen/OverworldDimension.java @@ -1,5 +1,7 @@ package com.github.halotroop.litecraft.world.gen; +import com.github.halotroop.litecraft.world.dimension.Dimension; + public class OverworldDimension extends Dimension { public OverworldDimension(int id)