Skip to content

Commit

Permalink
update noise codec
Browse files Browse the repository at this point in the history
  • Loading branch information
ToCraft committed Nov 13, 2024
1 parent 4e18ee5 commit f6acbac
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 1.21.3 2024-11-11T14:50:47.678304767 CTGen/Registries
// 1.21.3 2024-11-13T16:30:12.837407206 CTGen/Registries
1c5db5f4593f5fe97dbaf73bdbd63fe354a1185d data/ctgen/worldgen/map_based/zones/plains.json
6558d921786e5388da9f7f0366456041995fe2ff data/ctgen/worldgen/map_based/zones/badlands_mountains.json
3b20f0ae96f94e9a6aed34a730107acbf62fb269 data/ctgen/worldgen/map_based/zones/desert.json
cabb67dcfcfd601de0edcbb9a954770e0d9e5831 data/ctgen/worldgen/map_based/zones/badlands_mountains.json
97ccec666213300c0d55e330c49fad22e57e67f3 data/ctgen/worldgen/map_based/zones/stony_flats.json
3b20f0ae96f94e9a6aed34a730107acbf62fb269 data/ctgen/worldgen/map_based/zones/desert.json
a0432f3114eaaeca727c52f71e231dc7abbc6852 data/ctgen/worldgen/map_based/zones/river.json
3f241a73909060b3e47b9c13ae766d4c59568f77 data/ctgen/worldgen/map_based/zones/lake.json
754af079c6a28d87c26f7c9eae41f7a8f35f8261 data/ctgen/worldgen/map_based/zones/forest.json
Expand All @@ -11,8 +11,8 @@ a0432f3114eaaeca727c52f71e231dc7abbc6852 data/ctgen/worldgen/map_based/zones/riv
201b650496b9dddf4a2282c26e363e0b53718cc2 data/ctgen/worldgen/map_based/zones/ocean.json
a7977c1dada7eba7b8061d96da7d062e5807b621 data/ctgen/worldgen/map_based/zones/badlands.json
d27c95e66c317aba9c83e1f928dc572cc66e4aa8 data/ctgen/worldgen/map_based/zones/mountains.json
e6faad7f254549d65c1b04bcd3f2bdf8f4690e22 data/ctgen/worldgen/map_based/zones/hills.json
c8b6815ea720a5bb4312e5805143598c40b31648 data/ctgen/worldgen/map_based/zones/frozen_river.json
e6faad7f254549d65c1b04bcd3f2bdf8f4690e22 data/ctgen/worldgen/map_based/zones/hills.json
c43ae3888694ab66b65eef8cf94a5a041c974a98 data/ctgen/worldgen/map_based/zones/snowy_flats.json
bf803d6e35a3e69a5e34361863c82113446e4690 data/ctgen/worldgen/map_based/zones/deep_ocean.json
7e4477583efb49a9ee6fcb344282ed1cefbc6c43 data/ctgen/worldgen/map_based/zones/frozen_lake.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
"default": "minecraft:brown_concrete",
"noise": {
"octaves": [
1.0
{
"amplitude": 1.0,
"frequency": 1.0
}
],
"persistence": 1.0,
"stretch": 50
},
"values": {
Expand Down
35 changes: 18 additions & 17 deletions common/src/main/java/dev/tocraft/ctgen/util/Noise.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@

import java.util.List;

public record Noise(List<Float> octaves, double persistence, int stretchXZ, int stretchY) {
public static final Noise DEFAULT = new Noise(List.of(1f, 2f, 4f), 0.5f, 250);
public record Noise(List<Octave> octaves, int stretchXZ, int stretchY) {
public record Octave(float frequency, float amplitude) {
public static final Codec<Octave> CODEC = RecordCodecBuilder.create((instance) -> instance.group(
Codec.FLOAT.fieldOf("frequency").forGetter(Octave::frequency),
Codec.FLOAT.fieldOf("amplitude").forGetter(Octave::amplitude)
).apply(instance, instance.stable(Octave::new)));
}

public static final Noise DEFAULT = new Noise(List.of(new Octave(1, 1), new Octave(2, 0.5f), new Octave(4, 0.25f)), 250);

public Noise(List<Float> octaves, double persistence, int stretch) {
this(octaves, persistence, stretch, -1);
public Noise(List<Octave> octaves, int stretch) {
this(octaves, stretch, -1);
}

public static final Codec<Noise> CODEC = RecordCodecBuilder.create((instance) -> instance.group(
Codec.list(Codec.FLOAT).optionalFieldOf("octaves", DEFAULT.octaves).forGetter(Noise::octaves),
Codec.DOUBLE.optionalFieldOf("persistence", DEFAULT.persistence).forGetter(Noise::persistence),
Codec.list(Octave.CODEC).optionalFieldOf("octaves", DEFAULT.octaves).forGetter(Noise::octaves),
Codec.INT.optionalFieldOf("stretch", DEFAULT.stretchXZ).forGetter(Noise::stretchXZ),
Codec.INT.optionalFieldOf("stretch_y", DEFAULT.stretchY).forGetter(Noise::stretchY)
).apply(instance, instance.stable(Noise::new)));
Expand All @@ -26,12 +32,10 @@ public double getPerlin(SimplexNoise noise, double x, double y, double z) {
double z2 = z / stretchXZ;

double total = 0;
double amplitude = 1;
double totalAmplitude = 0;
for (float frequency : octaves) {
total += noise.getValue(x2 * frequency, y2 * frequency, z2 * frequency) * amplitude;
totalAmplitude += amplitude;
amplitude *= persistence;
for (Octave octave : octaves) {
total += noise.getValue(x2 * octave.frequency, y2 * octave.frequency, z2 * octave.frequency) * octave.amplitude;
totalAmplitude += octave.amplitude;
}

return total / totalAmplitude;
Expand All @@ -42,13 +46,10 @@ public double getPerlin(SimplexNoise noise, double x, double z) {
double z2 = z / stretchXZ;

double total = 0;
double amplitude = 1;
double totalAmplitude = 0;

for (float frequency : octaves) {
total += noise.getValue(x2 * frequency, z2 * frequency) * amplitude;
totalAmplitude += amplitude;
amplitude *= persistence;
for (Octave octave : octaves) {
total += noise.getValue(x2 * octave.frequency, z2 * octave.frequency) * octave.amplitude;
totalAmplitude += octave.amplitude;
}

total /= totalAmplitude;
Expand Down
10 changes: 10 additions & 0 deletions common/src/main/java/dev/tocraft/ctgen/worldgen/MapSettings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.tocraft.ctgen.worldgen;

import com.mojang.logging.LogUtils;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.ctgen.data.MapImageRegistry;
Expand All @@ -9,13 +10,15 @@
import dev.tocraft.ctgen.xtend.height.TerrainHeight;
import dev.tocraft.ctgen.xtend.layer.BlockLayer;
import dev.tocraft.ctgen.zone.Zone;
import dev.tocraft.ctgen.zone.Zones;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -92,6 +95,13 @@ public MapSettings(ResourceLocation mapId, boolean pixelsAreChunks, List<Holder<
}
});
this.carver = carver;

for (Holder<Zone> zone : this.zones) {
if (zone.is(Zones.RIVER)) {
Color c = new Color(1, 98, 255);
LogUtils.getLogger().warn("Color: " + zone.value().color() + " should be: " + c.getRGB());
}
}
}

public List<BlockLayer> getLayers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.List;

public class NoiseCarver extends Carver {
public static final NoiseCarver DEFAULT = new NoiseCarver(new Noise(List.of(1F, 0.5F), 2, 63, 47), 0.55F);
public static final NoiseCarver DEFAULT = new NoiseCarver(new Noise(List.of(new Noise.Octave(1, 1), new Noise.Octave(0.5f, 2)), 63, 47), 0.55F);

public static final MapCodec<NoiseCarver> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Noise.CODEC.optionalFieldOf("noise", DEFAULT.noise).forGetter(o -> o.noise),
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/dev/tocraft/ctgen/zone/Zones.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static void bootstrap(@NotNull BootstrapContext<Zone> context) {
// Western Continent
context.register(DESERT, new ZoneBuilder().setBiome(getBiome(context, Biomes.DESERT)).setColor(new Color(165, 171, 54)).setDirtBlock(Blocks.SANDSTONE).setSurfaceBlock(Blocks.SAND).setHeight(5).setTerrainModifier(4).build());
context.register(BADLANDS, new ZoneBuilder().setBiome(getBiome(context, Biomes.BADLANDS)).setColor(new Color(84, 84, 56)).setDirtBlock(Blocks.RED_CONCRETE).setSurfaceBlock(Blocks.RED_SAND).setHeight(18).setTerrainModifier(12).build());
context.register(BADLANDS_MOUNTAINS, new ZoneBuilder().setBiome(getBiome(context, Biomes.BADLANDS)).setColor(new Color(70, 71, 53)).setDirtBlock(Blocks.RED_CONCRETE).putLayer("surface", NoisePlacer.of(new Noise(List.of(1f), 1, 50), new HashMap<>() {
context.register(BADLANDS_MOUNTAINS, new ZoneBuilder().setBiome(getBiome(context, Biomes.BADLANDS)).setColor(new Color(70, 71, 53)).setDirtBlock(Blocks.RED_CONCRETE).putLayer("surface", NoisePlacer.of(new Noise(List.of(new Noise.Octave(1, 1)), 50), new HashMap<>() {
{
put(-0.5d, Blocks.ORANGE_CONCRETE);
put(0d, Blocks.RED_CONCRETE);
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ org.gradle.jvmargs=-Xmx4G
# Base Versions
archives_base_name=ctgen
artifact_type=release
mod_version=0.25
mod_version=0.26
maven_group=dev.tocraft
main_class=dev.tocraft.ctgen.runtime.Main
# Loader Versions
Expand Down

0 comments on commit f6acbac

Please sign in to comment.