mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-05 07:16:10 +00:00
custom noise functions per palette layer
This commit is contained in:
@@ -5,19 +5,24 @@ import com.dfsek.terra.api.math.noise.samplers.NoiseSampler;
|
||||
import java.util.List;
|
||||
|
||||
public class NoisePalette<E> extends Palette<E> {
|
||||
private final NoiseSampler r;
|
||||
private final NoiseSampler sampler;
|
||||
private final boolean is2D;
|
||||
|
||||
public NoisePalette(NoiseSampler r, boolean is2D) {
|
||||
this.r = r;
|
||||
public NoisePalette(NoiseSampler sampler, boolean is2D) {
|
||||
this.sampler = sampler;
|
||||
this.is2D = is2D;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int layer, double x, double y, double z) {
|
||||
if(layer > this.getSize()) return this.getLayers().get(this.getLayers().size() - 1).get(r, x, y, z, is2D);
|
||||
List<PaletteLayer<E>> pl = getLayers();
|
||||
if(layer >= pl.size()) return pl.get(pl.size() - 1).get(r, x, y, z, is2D);
|
||||
return pl.get(layer).get(r, x, y, z, is2D);
|
||||
PaletteLayer<E> paletteLayer;
|
||||
if(layer > this.getSize()) paletteLayer = this.getLayers().get(this.getLayers().size() - 1);
|
||||
else {
|
||||
List<PaletteLayer<E>> pl = getLayers();
|
||||
if(layer >= pl.size()) paletteLayer = pl.get(pl.size() - 1);
|
||||
else paletteLayer = pl.get(layer);
|
||||
}
|
||||
NoiseSampler paletteSampler = paletteLayer.getSampler();
|
||||
return paletteLayer.get(paletteSampler == null ? sampler : paletteSampler, x, y, z, is2D);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,30 +21,16 @@ public abstract class Palette<E> {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a material to the palette, for a number of layers.
|
||||
*
|
||||
* @param m - The material to add to the palette.
|
||||
* @param layers - The number of layers the material occupies.
|
||||
* @return - BlockPalette instance for chaining.
|
||||
*/
|
||||
public com.dfsek.terra.api.world.palette.Palette<E> add(E m, int layers) {
|
||||
public com.dfsek.terra.api.world.palette.Palette<E> add(E m, int layers, NoiseSampler sampler) {
|
||||
for(int i = 0; i < layers; i++) {
|
||||
pallet.add(new PaletteLayer<>(m));
|
||||
pallet.add(new PaletteLayer<>(m, sampler));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a ProbabilityCollection to the palette, for a number of layers.
|
||||
*
|
||||
* @param m - The ProbabilityCollection to add to the palette.
|
||||
* @param layers - The number of layers the material occupies.
|
||||
* @return - BlockPalette instance for chaining.
|
||||
*/
|
||||
public com.dfsek.terra.api.world.palette.Palette<E> add(ProbabilityCollection<E> m, int layers) {
|
||||
public com.dfsek.terra.api.world.palette.Palette<E> add(ProbabilityCollection<E> m, int layers, NoiseSampler sampler) {
|
||||
for(int i = 0; i < layers; i++) {
|
||||
pallet.add(new PaletteLayer<>(m));
|
||||
pallet.add(new PaletteLayer<>(m, sampler));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
@@ -72,14 +58,17 @@ public abstract class Palette<E> {
|
||||
public static class PaletteLayer<E> {
|
||||
private final boolean col; // Is layer using a collection?
|
||||
private ProbabilityCollection<E> collection;
|
||||
private final NoiseSampler sampler;
|
||||
private E m;
|
||||
|
||||
/**
|
||||
* Constructs a PaletteLayer with a ProbabilityCollection of materials and a number of layers.
|
||||
*
|
||||
* @param type - The collection of materials to choose from.
|
||||
* @param type The collection of materials to choose from.
|
||||
* @param sampler Noise sampler to use
|
||||
*/
|
||||
public PaletteLayer(ProbabilityCollection<E> type) {
|
||||
public PaletteLayer(ProbabilityCollection<E> type, NoiseSampler sampler) {
|
||||
this.sampler = sampler;
|
||||
this.col = true;
|
||||
this.collection = type;
|
||||
}
|
||||
@@ -87,13 +76,19 @@ public abstract class Palette<E> {
|
||||
/**
|
||||
* Constructs a PaletteLayer with a single Material and a number of layers.
|
||||
*
|
||||
* @param type - The material to use.
|
||||
* @param type The material to use.
|
||||
* @param sampler Noise sampler to use
|
||||
*/
|
||||
public PaletteLayer(E type) {
|
||||
public PaletteLayer(E type, NoiseSampler sampler) {
|
||||
this.sampler = sampler;
|
||||
this.col = false;
|
||||
this.m = type;
|
||||
}
|
||||
|
||||
public NoiseSampler getSampler() {
|
||||
return sampler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a material from the layer.
|
||||
*
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.dfsek.terra.api.world.palette;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class RandomPalette<E> extends Palette<E> {
|
||||
private final Random r;
|
||||
|
||||
public RandomPalette(Random r) {
|
||||
this.r = r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int layer, double x, double y, double z) {
|
||||
if(layer > this.getSize()) return this.getLayers().get(this.getLayers().size() - 1).get(r);
|
||||
List<PaletteLayer<E>> pl = getLayers();
|
||||
if(layer >= pl.size()) return pl.get(pl.size() - 1).get(r);
|
||||
return pl.get(layer).get(r);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,18 @@
|
||||
package com.dfsek.terra.biome.palette;
|
||||
|
||||
import com.dfsek.terra.api.math.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.math.noise.samplers.NoiseSampler;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class PaletteLayer {
|
||||
private final ProbabilityCollection<BlockData> layer;
|
||||
private final NoiseSampler sampler;
|
||||
private final int size;
|
||||
|
||||
public PaletteLayer(@NotNull ProbabilityCollection<BlockData> layer, int size) {
|
||||
public PaletteLayer(@NotNull ProbabilityCollection<BlockData> layer, NoiseSampler sampler, int size) {
|
||||
this.layer = layer;
|
||||
this.sampler = sampler;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
@@ -21,4 +24,8 @@ public class PaletteLayer {
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public NoiseSampler getSampler() {
|
||||
return sampler;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.dfsek.terra.config.factories;
|
||||
|
||||
import com.dfsek.terra.api.math.noise.samplers.FastNoiseLite;
|
||||
import com.dfsek.terra.api.platform.TerraPlugin;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.util.FastRandom;
|
||||
import com.dfsek.terra.api.world.flora.Flora;
|
||||
import com.dfsek.terra.api.world.palette.NoisePalette;
|
||||
import com.dfsek.terra.api.world.palette.Palette;
|
||||
import com.dfsek.terra.api.world.palette.RandomPalette;
|
||||
import com.dfsek.terra.biome.palette.PaletteLayer;
|
||||
import com.dfsek.terra.config.templates.FloraTemplate;
|
||||
import com.dfsek.terra.population.items.flora.TerraFlora;
|
||||
@@ -13,9 +13,11 @@ import com.dfsek.terra.population.items.flora.TerraFlora;
|
||||
public class FloraFactory implements TerraFactory<FloraTemplate, Flora> {
|
||||
@Override
|
||||
public TerraFlora build(FloraTemplate config, TerraPlugin main) {
|
||||
Palette<BlockData> palette = new RandomPalette<>(new FastRandom(2403));
|
||||
FastNoiseLite whiteNoise = new FastNoiseLite();
|
||||
whiteNoise.setNoiseType(FastNoiseLite.NoiseType.WhiteNoise);
|
||||
Palette<BlockData> palette = new NoisePalette<>(whiteNoise, false);
|
||||
for(PaletteLayer layer : config.getFloraPalette()) {
|
||||
palette.add(layer.getLayer(), layer.getSize());
|
||||
palette.add(layer.getLayer(), layer.getSize(), layer.getSampler());
|
||||
}
|
||||
return new TerraFlora(palette, config.doPhysics(), config.isCeiling(), config.getIrrigable(), config.getSpawnable(), config.getReplaceable(), config.getRotatable(), config.getMaxPlacements(), config.getSearch(), config.isSpawnBlacklist(), config.getIrrigableOffset(), main);
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ import com.dfsek.terra.config.templates.PaletteTemplate;
|
||||
public class PaletteFactory implements TerraFactory<PaletteTemplate, Palette<BlockData>> {
|
||||
@Override
|
||||
public Palette<BlockData> build(PaletteTemplate config, TerraPlugin main) {
|
||||
Palette<BlockData> palette = new NoisePalette<>(config.getNoise().build(2403), config.getNoise().getDimensions() == 2);
|
||||
NoisePalette<BlockData> palette = new NoisePalette<>(config.getNoise().build(2403), config.getNoise().getDimensions() == 2);
|
||||
for(PaletteLayer layer : config.getPalette()) {
|
||||
palette.add(layer.getLayer(), layer.getSize());
|
||||
palette.add(layer.getLayer(), layer.getSize(), layer.getSampler());
|
||||
}
|
||||
return palette;
|
||||
}
|
||||
|
||||
@@ -4,9 +4,12 @@ import com.dfsek.tectonic.exception.LoadException;
|
||||
import com.dfsek.tectonic.loading.ConfigLoader;
|
||||
import com.dfsek.tectonic.loading.TypeLoader;
|
||||
import com.dfsek.terra.api.math.ProbabilityCollection;
|
||||
import com.dfsek.terra.api.math.noise.samplers.NoiseSampler;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.biome.palette.PaletteLayer;
|
||||
import com.dfsek.terra.config.loaders.Types;
|
||||
import com.dfsek.terra.config.loaders.config.NoiseBuilderLoader;
|
||||
import com.dfsek.terra.generation.config.NoiseBuilder;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
@@ -17,7 +20,13 @@ public class PaletteLayerLoader implements TypeLoader<PaletteLayer> {
|
||||
public PaletteLayer load(Type type, Object o, ConfigLoader configLoader) throws LoadException {
|
||||
Map<String, Object> map = (Map<String, Object>) o;
|
||||
ProbabilityCollection<BlockData> collection = (ProbabilityCollection<BlockData>) configLoader.loadType(Types.BLOCK_DATA_PROBABILITY_COLLECTION_TYPE, map.get("materials"));
|
||||
|
||||
NoiseSampler sampler = null;
|
||||
if(map.containsKey("noise")) {
|
||||
sampler = new NoiseBuilderLoader().load(NoiseBuilder.class, map.get("noise"), configLoader).build(2403);
|
||||
}
|
||||
|
||||
if(collection == null) throw new LoadException("Collection is null: " + map.get("materials"));
|
||||
return new PaletteLayer(collection, (Integer) map.get("layers"));
|
||||
return new PaletteLayer(collection, sampler, (Integer) map.get("layers"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,8 +73,8 @@ public class BiomeTest {
|
||||
.addStage(new MutatorStage(new SmoothMutator(whiteNoise(6))))
|
||||
.addStage(new ExpanderStage(new FractalExpander(whiteNoise(4))))
|
||||
.addStage(new ExpanderStage(new FractalExpander(whiteNoise(4))))
|
||||
.addStage(new ExpanderStage(new FractalExpander(whiteNoise(4))))
|
||||
.addStage(new MutatorStage(new BorderMutator("OCEAN", "LAND", whiteNoise(1234), beachBiomes)))
|
||||
.addStage(new ExpanderStage(new FractalExpander(whiteNoise(4))))
|
||||
.addStage(new MutatorStage(new SmoothMutator(whiteNoise(6))))
|
||||
.addStage(new MutatorStage(new SmoothMutator(whiteNoise(6))))
|
||||
.build(source), null).build(0);
|
||||
|
||||
Reference in New Issue
Block a user