Skip to content

Commit

Permalink
Reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
duplexsystem committed Sep 24, 2024
1 parent f469193 commit d6772f5
Show file tree
Hide file tree
Showing 29 changed files with 411 additions and 383 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.dfsek.terra.addons.biome.extrusion.api;

import java.util.Collection;

import com.dfsek.terra.api.world.biome.Biome;

import java.util.Collection;


public interface Extrusion {
Biome extrude(Biome original, int x, int y, int z, long seed);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.dfsek.terra.addons.biome.pipeline.api.delegate;

import com.dfsek.terra.api.world.biome.Biome;

import java.util.Collections;
import java.util.Set;

import com.dfsek.terra.api.world.biome.Biome;


final class SelfDelegate implements BiomeDelegate {
public static final SelfDelegate INSTANCE = new SelfDelegate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

package com.dfsek.terra.addons.biome.holder;

import com.dfsek.terra.api.world.chunk.generation.util.Palette;

import java.util.Map;
import java.util.TreeMap;

import com.dfsek.terra.api.world.chunk.generation.util.Palette;


public class PaletteHolderBuilder {
private final TreeMap<Integer, Palette> paletteMap = new TreeMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

package com.dfsek.terra.addons.feature.distributor.distributors;

import java.util.Set;

import com.dfsek.terra.addons.feature.distributor.util.Point;
import com.dfsek.terra.api.structure.feature.Distributor;

import java.util.Set;


public class PointSetDistributor implements Distributor {
private final Set<Point> points;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,46 @@

import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Supplier;

import com.dfsek.terra.addons.manifest.api.AddonInitializer;
import com.dfsek.terra.addons.noise.config.CubicSplinePointTemplate;
import com.dfsek.terra.addons.noise.config.DimensionApplicableNoiseSampler;
import com.dfsek.terra.addons.noise.config.templates.*;
import com.dfsek.terra.addons.noise.config.templates.noise.*;
import com.dfsek.terra.addons.noise.config.templates.BinaryArithmeticTemplate;
import com.dfsek.terra.addons.noise.config.templates.DerivativeNoiseSamplerTemplate;
import com.dfsek.terra.addons.noise.config.templates.DomainWarpTemplate;
import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate;
import com.dfsek.terra.addons.noise.config.templates.ImageSamplerTemplate;
import com.dfsek.terra.addons.noise.config.templates.KernelTemplate;
import com.dfsek.terra.addons.noise.config.templates.LinearHeightmapSamplerTemplate;
import com.dfsek.terra.addons.noise.config.templates.TranslateSamplerTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.CellularNoiseTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.ConstantNoiseTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.DistanceSamplerTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.ExpressionFunctionTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.GaborNoiseTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.PseudoErosionTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.SimpleNoiseTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.fractal.BrownianMotionTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.fractal.PingPongTemplate;
import com.dfsek.terra.addons.noise.config.templates.noise.fractal.RidgedFractalTemplate;
import com.dfsek.terra.addons.noise.config.templates.normalizer.*;
import com.dfsek.terra.addons.noise.config.templates.normalizer.ClampNormalizerTemplate;
import com.dfsek.terra.addons.noise.config.templates.normalizer.CubicSplineNormalizerTemplate;
import com.dfsek.terra.addons.noise.config.templates.normalizer.ExpressionNormalizerTemplate;
import com.dfsek.terra.addons.noise.config.templates.normalizer.LinearNormalizerTemplate;
import com.dfsek.terra.addons.noise.config.templates.normalizer.NormalNormalizerTemplate;
import com.dfsek.terra.addons.noise.config.templates.normalizer.PosterizationNormalizerTemplate;
import com.dfsek.terra.addons.noise.config.templates.normalizer.ProbabilityNormalizerTemplate;
import com.dfsek.terra.addons.noise.config.templates.normalizer.ScaleNormalizerTemplate;
import com.dfsek.terra.addons.noise.math.CubicSpline;
import com.dfsek.terra.addons.noise.samplers.arithmetic.*;
import com.dfsek.terra.addons.noise.samplers.arithmetic.AdditionSampler;
import com.dfsek.terra.addons.noise.samplers.arithmetic.DivisionSampler;
import com.dfsek.terra.addons.noise.samplers.arithmetic.MaxSampler;
import com.dfsek.terra.addons.noise.samplers.arithmetic.MinSampler;
import com.dfsek.terra.addons.noise.samplers.arithmetic.MultiplicationSampler;
import com.dfsek.terra.addons.noise.samplers.arithmetic.SubtractionSampler;
import com.dfsek.terra.addons.noise.samplers.noise.CellularSampler;
import com.dfsek.terra.addons.noise.samplers.noise.DistanceSampler;
import com.dfsek.terra.addons.noise.samplers.noise.random.GaussianNoiseSampler;
Expand All @@ -41,10 +70,6 @@
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.util.reflection.TypeKey;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Supplier;


public class NoiseAddon implements AddonInitializer {
public static final TypeKey<Supplier<ObjectTemplate<NoiseSampler>>> NOISE_SAMPLER_TOKEN = new TypeKey<>() {
Expand Down Expand Up @@ -97,7 +122,7 @@ public void initialize() {
noiseRegistry.register(addon.key("SIMPLEX"), () -> new SimpleNoiseTemplate(SimplexSampler::new));
noiseRegistry.register(addon.key("GABOR"), GaborNoiseTemplate::new);
noiseRegistry.register(addon.key("PSEUDOEROSION"), PseudoErosionTemplate::new);

noiseRegistry.register(addon.key("VALUE"), () -> new SimpleNoiseTemplate(ValueSampler::new));
noiseRegistry.register(addon.key("VALUE_CUBIC"), () -> new SimpleNoiseTemplate(ValueCubicSampler::new));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
import com.dfsek.tectonic.api.config.template.annotations.Default;
import com.dfsek.tectonic.api.config.template.annotations.Value;

import java.util.LinkedHashMap;
import java.util.Map;

import com.dfsek.terra.addons.noise.config.DimensionApplicableNoiseSampler;
import com.dfsek.terra.addons.noise.config.templates.FunctionTemplate;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.properties.Properties;

import java.util.LinkedHashMap;
import java.util.Map;


@SuppressWarnings("FieldMayBeFinal")
public class NoiseConfigPackTemplate implements ConfigTemplate, Properties {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class DerivativeNoiseSamplerTemplate extends SamplerTemplate<DerivativeNo

@Override
public boolean validate() throws ValidationException {
if (!DerivativeNoiseSampler.isDifferentiable(sampler)) throw new ValidationException("Provided sampler does not support calculating a derivative");
if(!DerivativeNoiseSampler.isDifferentiable(sampler)) throw new ValidationException(
"Provided sampler does not support calculating a derivative");
return super.validate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
package com.dfsek.terra.addons.noise.config.templates;

import com.dfsek.tectonic.api.config.template.annotations.Value;

import com.dfsek.terra.addons.noise.samplers.ImageSampler;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.noise.NoiseSampler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.image.BufferedImage;

import com.dfsek.terra.addons.noise.samplers.ImageSampler;
import com.dfsek.terra.api.config.meta.Meta;
import com.dfsek.terra.api.noise.NoiseSampler;


@SuppressWarnings({ "unused", "FieldMayBeFinal" })
public class ImageSamplerTemplate extends SamplerTemplate<ImageSampler> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,61 +10,48 @@

public class PseudoErosionTemplate extends NoiseTemplate<PseudoErosionSampler> {

@Value("frequency")
@Default
protected @Meta double frequency = 1d;
@Value("octaves")
@Default
private int octaves = 4;

@Value("lacunarity")
@Default
private double lacunarity = 2.0;

@Value("gain")
@Default
private double gain = 0.5;

@Value("slope-strength")
@Default
private double slopeStrength = 1.0;

@Value("branch-strength")
@Default
private double branchStrength = 1.0;

@Value("strength")
@Default
private double strength = 0.04;

@Value("erosion-frequency")
@Default
private double erosionFrequency = 0.02;

@Value("sampler")
private DerivativeNoiseSampler heightSampler;

@Value("slope-mask.enable")
@Default
private boolean slopeMask = true;

@Value("slope-mask.none")
@Default
private double slopeMaskNone = -0.5;

@Value("slope-mask.full")
@Default
private double slopeMaskFull = 1;

@Value("jitter")
@Default
private double jitterModifier = 1;

@Value("average-impulses")
@Default
private boolean averageErosionImpulses = true;

@Value("frequency")
@Default
protected @Meta double frequency = 1d;

@Override
public PseudoErosionSampler get() {
PseudoErosionSampler pseudoErosion = new PseudoErosionSampler(octaves, gain, lacunarity,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dfsek.terra.api.noise.DerivativeNoiseSampler;


public abstract class DerivativeNoiseFunction extends NoiseFunction implements DerivativeNoiseSampler {
@Override
public boolean isDifferentiable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public class PseudoErosionSampler extends NoiseFunction {
public static final float TAU = (float) (2.0 * Math.PI);
private static final float HASH_X = 0.3183099f;
private static final float HASH_Y = 0.3678794f;
private final int octaves;
public final double gain;
public final double lacunarity;
public final double slopeStrength;
public final double branchStrength;
public final double erosionStrength;
private final int octaves;
private final double erosionFrequency;
private final DerivativeNoiseSampler sampler;
private final boolean slopeMask;
Expand All @@ -25,7 +25,8 @@ public class PseudoErosionSampler extends NoiseFunction {
private final double maxCellDistSqRecip;
private final boolean averageErosionImpulses;

public PseudoErosionSampler(int octaves, double gain, double lacunarity, double slopeStrength, double branchStrength, double erosionStrength, double erosionFrequency, DerivativeNoiseSampler sampler,
public PseudoErosionSampler(int octaves, double gain, double lacunarity, double slopeStrength, double branchStrength,
double erosionStrength, double erosionFrequency, DerivativeNoiseSampler sampler,
boolean slopeMask, double slopeMaskFull, double slopeMaskNone, double jitterModifier,
boolean averageErosionImpulses) {
this.octaves = octaves;
Expand Down Expand Up @@ -59,7 +60,22 @@ public static float hashY(float seed, float n) {
}

public static float fract(float x) {
return (x - (float)Math.floor(x));
return (x - (float) Math.floor(x));
}

public static float smoothstep(float edge0, float edge1, float x) {
// Scale, bias and saturate x to 0..1 range
x = clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
// Evaluate polynomial
return x * x * (3 - 2 * x);
}

public static float clamp(float x, float minVal, float maxVal) {
return Math.max(minVal, Math.min(maxVal, x));
}

public static float dot(float x1, float y1, float x2, float y2) {
return x1 * x2 + y1 * y2;
}

public float[] erosion(int seed, float x, float y, float dirX, float dirY) {
Expand All @@ -70,16 +86,17 @@ public float[] erosion(int seed, float x, float y, float dirX, float dirY) {
float dirOutY = 0.0f;
float cumAmp = 0.0f;

for (int cellX = gridX - 1; cellX <= gridX + 1; cellX++) {
for (int cellY = gridY - 1; cellY <= gridY + 1; cellY++) {
for(int cellX = gridX - 1; cellX <= gridX + 1; cellX++) {
for(int cellY = gridY - 1; cellY <= gridY + 1; cellY++) {
float cellHash = hash(seed, cellX, cellY);
float cellOffsetX = (float) (hashX(seed, cellHash) * jitter);
float cellOffsetY = (float) (hashY(seed, cellHash) * jitter);
float cellOriginDeltaX = (x - cellX) + cellOffsetX;
float cellOriginDeltaY = (y - cellY) + cellOffsetY;
float cellOriginDistSq = cellOriginDeltaX * cellOriginDeltaX + cellOriginDeltaY * cellOriginDeltaY;
if (cellOriginDistSq > maxCellDistSq) continue; // Skip calculating cells too far away
float ampTmp = (float) ((cellOriginDistSq * maxCellDistSqRecip) - 1); float amp = ampTmp * ampTmp; // Decrease cell amplitude further away
if(cellOriginDistSq > maxCellDistSq) continue; // Skip calculating cells too far away
float ampTmp = (float) ((cellOriginDistSq * maxCellDistSqRecip) - 1);
float amp = ampTmp * ampTmp; // Decrease cell amplitude further away
cumAmp += amp;
float directionalStrength = dot(cellOriginDeltaX, cellOriginDeltaY, dirX, dirY) * TAU;
noise += (float) (MathUtil.cos(directionalStrength) * amp);
Expand All @@ -88,23 +105,12 @@ public float[] erosion(int seed, float x, float y, float dirX, float dirY) {
dirOutY -= sinAngle * (cellOriginDeltaY + dirY);
}
}
if (averageErosionImpulses && cumAmp != 0) {
if(averageErosionImpulses && cumAmp != 0) {
noise /= cumAmp;
dirOutX /= cumAmp;
dirOutY /= cumAmp;
}
return new float[] {noise, dirOutX, dirOutY};
}

public static float smoothstep(float edge0, float edge1, float x) {
// Scale, bias and saturate x to 0..1 range
x = clamp((x - edge0) / (edge1 - edge0), 0.0f, 1.0f);
// Evaluate polynomial
return x * x * (3 - 2 * x);
}

public static float clamp(float x, float minVal, float maxVal) {
return Math.max(minVal, Math.min(maxVal, x));
return new float[]{ noise, dirOutX, dirOutY };
}

public float heightMap(long seed, float x, float y) {
Expand All @@ -125,7 +131,7 @@ public float heightMap(long seed, float x, float y) {
float freq = 1.0f;

// Stack erosion octaves
for (int i = 0; i < octaves; i++) {
for(int i = 0; i < octaves; i++) {
float[] erosionResult = erosion((int) seed,
x * freq * (float) erosionFrequency,
y * freq * (float) erosionFrequency,
Expand All @@ -139,15 +145,14 @@ public float heightMap(long seed, float x, float y) {
freq *= lacunarity;
}

// TODO - Test different output ranges, see how they affect visuals
// Normalize erosion noise
erosion /= cumAmp;
// [-1, 1] -> [0, 1]
erosion = erosion * 0.5F + 0.5F;

// Without masking, erosion noise in areas with small gradients tend to produce mounds,
// this reduces erosion amplitude towards smaller gradients to avoid this
if (slopeMask) {
if(slopeMask) {
float dirMagSq = dot(baseDirX, baseDirY, baseDirX, baseDirY);
float flatness = smoothstep((float) slopeMaskNoneSq, (float) slopeMaskFullSq, dirMagSq);
erosion *= flatness;
Expand All @@ -156,10 +161,6 @@ public float heightMap(long seed, float x, float y) {
return (float) (height + erosion * erosionStrength);
}

public static float dot(float x1, float y1, float x2, float y2) {
return x1 * x2 + y1 * y2;
}

@Override
public double getNoiseRaw(long seed, double x, double y) {
return heightMap(seed, (float) x, (float) y);
Expand Down
Loading

0 comments on commit d6772f5

Please sign in to comment.