Skip to content

Commit

Permalink
add Carver
Browse files Browse the repository at this point in the history
  • Loading branch information
ToCraft committed Oct 31, 2024
1 parent df7c0ca commit 6b491f7
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// 1.20.1 2024-10-31T12:11:48.172926144 CTGen/Registries
// 1.20.1 2024-10-31T12:27:26.172865087 CTGen/Registries
09917112f8476aaef3e39f5780a478b955edc3e7 data/ctgen/worldgen/map_based/zones/plains.json
3ca73f0225adf1fd168bfc73558fbf63bcf54aec data/ctgen/worldgen/map_based/zones/badlands_mountains.json
ea8abee99ec67bf274bec024eff481325863952a data/ctgen/worldgen/map_based/zones/stony_flats.json
4e10b1b02ad79d86538524c470d56dedf8c779f1 data/ctgen/worldgen/map_based/zones/desert.json
b24dcc94b75a2092b2386357c78a1f93808fd7f8 data/ctgen/worldgen/map_based/zones/river.json
ea8abee99ec67bf274bec024eff481325863952a data/ctgen/worldgen/map_based/zones/stony_flats.json
0bd4367d5837e151081d147227d11e56c29ecffb data/ctgen/worldgen/map_based/zones/river.json
166eb560b75b90c63217a66b4eac9cf59dcb8f9e data/ctgen/worldgen/map_based/zones/lake.json
786353e42c828bf1348fb727db3e60adec29c18d data/ctgen/worldgen/map_based/zones/forest.json
a78f910d231a5d843042462e8517155aab8b5ca2 data/ctgen/worldgen/map_based/zones/snowy_mountains.json
3fcf216d906f96f57ba447df2dab0a13157af1ce data/ctgen/worldgen/map_based/zones/snowy_slopes.json
10fbdfeee308b501bed36a2d800c40b11810ce63 data/ctgen/worldgen/map_based/zones/ocean.json
a034954687c0b0280accf18519f3022d029489b8 data/ctgen/worldgen/map_based/zones/ocean.json
178d1061c64452922300ec3d5db0f066c59ca438 data/ctgen/worldgen/map_based/zones/badlands.json
b5339019d9316486c132e0832d7f86a08a9ec1e4 data/ctgen/worldgen/map_based/zones/mountains.json
d281f3a59adaf68e61a56c40493788015fd6ef95 data/ctgen/worldgen/map_based/zones/hills.json
12195224411b05e2dc54730b03a6ed894279f49c data/ctgen/worldgen/map_based/zones/frozen_river.json
2a289331bed4faa0aabefde7efaaff6ade8fd966 data/ctgen/worldgen/map_based/zones/frozen_river.json
bdedb4d289b62ce9049dad7260e5e2a58987dfb3 data/ctgen/worldgen/map_based/zones/snowy_flats.json
e09748905aab9f3b627aabb7cf3e1ae3eee3b2b1 data/ctgen/worldgen/map_based/zones/deep_ocean.json
c6a7a763fb76dc1de84eb2511629ae6faeb17945 data/ctgen/worldgen/map_based/zones/frozen_lake.json
e20907968549754b4f2bdd776a641ca5c6f44fcd data/ctgen/worldgen/map_based/zones/deep_ocean.json
ae7297b8304833244f8be82c73215aefae5bb273 data/ctgen/worldgen/map_based/zones/frozen_lake.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"biome": "minecraft:deep_ocean",
"carver_modifier": 26.0,
"color": {
"b": 85,
"g": 35,
"r": 0
},
"height": -60,
"terrain_modifier": 33.0,
"threshold_modifier": 26
"terrain_modifier": 33.0
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"biome": "minecraft:frozen_ocean",
"carver_modifier": 26.0,
"color": {
"b": 204,
"g": 126,
Expand All @@ -9,6 +10,5 @@
"layers": {
"surface": "minecraft:sand"
},
"pixel_weight": 3.0,
"threshold_modifier": 26
"pixel_weight": 3.0
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"biome": "minecraft:frozen_river",
"carver_modifier": 26.0,
"color": {
"b": 240,
"g": 145,
Expand All @@ -10,6 +11,5 @@
"dirt": "minecraft:sand",
"surface": "minecraft:sand"
},
"pixel_weight": 2.0,
"threshold_modifier": 26
"pixel_weight": 2.0
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"biome": "minecraft:ocean",
"carver_modifier": 26.0,
"color": {
"b": 103,
"g": 42,
Expand All @@ -9,6 +10,5 @@
"layers": {
"surface": "minecraft:sand"
},
"terrain_modifier": 16.0,
"threshold_modifier": 26
"terrain_modifier": 16.0
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"biome": "minecraft:river",
"carver_modifier": 26.0,
"color": {
"b": 255,
"g": 98,
Expand All @@ -10,6 +11,5 @@
"dirt": "minecraft:sand",
"surface": "minecraft:sand"
},
"pixel_weight": 2.0,
"threshold_modifier": 26
"pixel_weight": 2.0
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void buildSurface(@NotNull WorldGenRegion pLevel, @NotNull StructureManag

Zone zone = getSettings().getZone(xOff >> 2, zOff >> 2).value();
double surfaceHeight = getSettings().getHeight(noise, xOff, zOff) + getSettings().surfaceLevel;
int thresholdModifier = (int) getSettings().getValueWithTransition(xOff, zOff, zo -> (double) zo.thresholdModifier().orElse(getSettings().thresholdModifier));
double carverModifier = getSettings().getValueWithTransition(xOff, zOff, zo -> zo.carverModifier().orElse(getSettings().carverModifier));

int shift = (int) (noise.getValue(xOff, zOff) * 3);
int bedrockLevel = minHeight + shift;
Expand All @@ -96,7 +96,7 @@ public void buildSurface(@NotNull WorldGenRegion pLevel, @NotNull StructureManag
BlockPlacer placer = layer != null ? zone.layers().getOrDefault(layer.getName(), layer.getFallback()) : BasicPlacer.AIR;
Block block = placer.get(this.noise, x, y, z, layer != null ? layer.getName() : "fill");

if (layer != null && !layer.hasCaves() || canSetBlock(pos, surfaceHeight, minHeight, thresholdModifier)) {
if (layer != null && !layer.hasCaves() || canSetBlock(pos, surfaceHeight, minHeight, carverModifier)) {
// no grass underwater
if (surfaceHeight < getSeaLevel() && block == Blocks.GRASS_BLOCK) {
block = Blocks.DIRT;
Expand All @@ -115,20 +115,8 @@ public void buildSurface(@NotNull WorldGenRegion pLevel, @NotNull StructureManag
}
}

private boolean canSetBlock(@NotNull BlockPos pos, double surfaceHeight, int minHeight, int thresholdModifier) {
double height = (double) (pos.getY() - minHeight) / (surfaceHeight - minHeight) - 0.5;
double addThreshold = height * height * height * height * thresholdModifier;

for (CarverSetting carver : getSettings().carverSettings) {
double perlin = carver.noise().getPerlin(this.noise, pos.getX(), pos.getY(), pos.getZ());

double threshold = carver.threshold() + addThreshold;
if (perlin > threshold) {
return false;
}
}

return true;
private boolean canSetBlock(@NotNull BlockPos pos, double surfaceHeight, int minHeight, double carverModifier) {
return getSettings().carver.canSetBlock(this.noise, pos, surfaceHeight, minHeight, carverModifier);
}

@Override
Expand Down
18 changes: 10 additions & 8 deletions common/src/main/java/dev/tocraft/ctgen/worldgen/MapSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.ctgen.data.MapImageRegistry;
import dev.tocraft.ctgen.xtend.carver.Carver;
import dev.tocraft.ctgen.xtend.carver.NoiseCarver;
import dev.tocraft.ctgen.xtend.height.NoiseHeight;
import dev.tocraft.ctgen.xtend.height.TerrainHeight;
import dev.tocraft.ctgen.xtend.layer.BlockLayer;
Expand All @@ -23,12 +25,11 @@

@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public final class MapSettings {
static final MapSettings DEFAULT = new MapSettings(null, false, 1, new ArrayList<>(), null, BlockLayer.defaultLayers(-64), 66, -64, 279, 64, NoiseHeight.DEFAULT, 31, Optional.empty(), Optional.empty(), List.of(CarverSetting.DEFAULT));
static final MapSettings DEFAULT = new MapSettings(null, false, new ArrayList<>(), null, BlockLayer.defaultLayers(-64), 66, -64, 279, 64, NoiseHeight.DEFAULT, 31, Optional.empty(), Optional.empty(), NoiseCarver.DEFAULT, 1);

public static final Codec<MapSettings> CODEC = RecordCodecBuilder.create((instance) -> instance.group(
ResourceLocation.CODEC.fieldOf("biome_map").forGetter(o -> o.mapId),
Codec.BOOL.optionalFieldOf("pixels_are_chunks", DEFAULT.pixelsAreChunks).forGetter(o -> o.pixelsAreChunks),
Codec.INT.optionalFieldOf("default_threshold_modifier", DEFAULT.thresholdModifier).forGetter(o -> o.thresholdModifier),
Codec.list(Zone.CODEC).optionalFieldOf("zones", DEFAULT.zones).forGetter(o -> o.zones),
Zone.CODEC.fieldOf("default_map_biome").forGetter(o -> o.defaultBiome),
Codec.list(BlockLayer.CODEC).optionalFieldOf("layers", DEFAULT.layers).forGetter(o -> o.layers),
Expand All @@ -40,12 +41,12 @@ public final class MapSettings {
Codec.INT.optionalFieldOf("transition", DEFAULT.transition).forGetter(o -> o.transition),
Codec.INT.optionalFieldOf("spawn_pixel_x").forGetter(o -> o.spawnX),
Codec.INT.optionalFieldOf("spawn_pixel_y").forGetter(o -> o.spawnY),
Codec.list(CarverSetting.CODEC).optionalFieldOf("cave_carver", DEFAULT.carverSettings).forGetter(o -> o.carverSettings)
Carver.CODEC.optionalFieldOf("carver", DEFAULT.carver).forGetter(o -> o.carver),
Codec.DOUBLE.optionalFieldOf("default_carver_modifier", DEFAULT.carverModifier).forGetter(o -> o.carverModifier)
).apply(instance, instance.stable(MapSettings::new)));

private final ResourceLocation mapId;
final boolean pixelsAreChunks;
final int thresholdModifier;
final List<Holder<Zone>> zones;
private final Holder<Zone> defaultBiome;
final int surfaceLevel;
Expand All @@ -57,14 +58,15 @@ public final class MapSettings {
private final Supplier<BufferedImage> mapImage;
final Optional<Integer> spawnX;
final Optional<Integer> spawnY;
final List<CarverSetting> carverSettings;
final Carver carver;
final double carverModifier;
private final List<BlockLayer> layers;

@ApiStatus.Internal
public MapSettings(ResourceLocation mapId, boolean pixelsAreChunks, int thresholdModifier, List<Holder<Zone>> zones, Holder<Zone> defaultBiome, List<BlockLayer> layers, int surfaceLevel, int minY, int genHeight, int seaLevel, TerrainHeight terrain, int transition, @NotNull Optional<Integer> spawnX, @NotNull Optional<Integer> spawnY, List<CarverSetting> carverSettings) {
public MapSettings(ResourceLocation mapId, boolean pixelsAreChunks, List<Holder<Zone>> zones, Holder<Zone> defaultBiome, List<BlockLayer> layers, int surfaceLevel, int minY, int genHeight, int seaLevel, TerrainHeight terrain, int transition, @NotNull Optional<Integer> spawnX, @NotNull Optional<Integer> spawnY, Carver carver, double carverModifier) {
this.mapId = mapId;
this.pixelsAreChunks = pixelsAreChunks;
this.thresholdModifier = thresholdModifier;
this.carverModifier = carverModifier;
this.zones = zones;
this.defaultBiome = defaultBiome;
this.layers = layers;
Expand All @@ -89,7 +91,7 @@ public MapSettings(ResourceLocation mapId, boolean pixelsAreChunks, int threshol
return sX;
}
});
this.carverSettings = carverSettings;
this.carver = carver;
}

public List<BlockLayer> getLayers() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.tocraft.ctgen.worldgen;

import dev.tocraft.ctgen.xtend.carver.Carver;
import dev.tocraft.ctgen.xtend.height.TerrainHeight;
import dev.tocraft.ctgen.xtend.layer.BlockLayer;
import dev.tocraft.ctgen.zone.Zone;
Expand All @@ -13,7 +14,6 @@
public class MapSettingsBuilder {
private ResourceLocation biomeMapId;
private boolean pixelsAreChunks = MapSettings.DEFAULT.pixelsAreChunks;
private int thresholdModifier = MapSettings.DEFAULT.thresholdModifier;
private List<Holder<Zone>> zones = MapSettings.DEFAULT.zones;
private Holder<Zone> defaultBiome;
private List<BlockLayer> layers = MapSettings.DEFAULT.getLayers();
Expand All @@ -25,7 +25,8 @@ public class MapSettingsBuilder {
private int transition = MapSettings.DEFAULT.transition;
private Optional<Integer> spawnX = MapSettings.DEFAULT.spawnX;
private Optional<Integer> spawnY = MapSettings.DEFAULT.spawnY;
private List<CarverSetting> carverSettings = MapSettings.DEFAULT.carverSettings;
private Carver carver = MapSettings.DEFAULT.carver;
private double carverModifier = MapSettings.DEFAULT.carverModifier;

public MapSettingsBuilder setBiomeMapId(ResourceLocation biomeMapId) {
this.biomeMapId = biomeMapId;
Expand All @@ -37,10 +38,6 @@ public MapSettingsBuilder setPixelsAreChunks(boolean pixelsAreChunks) {
return this;
}

public MapSettingsBuilder setThresholdModifier(int thresholdModifier) {
this.thresholdModifier = thresholdModifier;
return this;
}

public MapSettingsBuilder setZones(List<Holder<Zone>> zones) {
this.zones = zones;
Expand Down Expand Up @@ -102,12 +99,17 @@ public MapSettingsBuilder setSpawnY(int spawnY) {
return this;
}

public MapSettingsBuilder setCarverSettings(List<CarverSetting> carverSetting) {
this.carverSettings = carverSetting;
public MapSettingsBuilder setCarver(Carver carver) {
this.carver = carver;
return this;
}

public MapSettingsBuilder setCarverModifier(double carverModifier) {
this.carverModifier = carverModifier;
return this;
}

public MapSettings build() {
return new MapSettings(biomeMapId, pixelsAreChunks, thresholdModifier, zones, defaultBiome, layers, surfaceLevel, minY, genHeight, seaLevel, terrain, transition, spawnX, spawnY, carverSettings);
return new MapSettings(biomeMapId, pixelsAreChunks, zones, defaultBiome, layers, surfaceLevel, minY, genHeight, seaLevel, terrain, transition, spawnX, spawnY, carver, carverModifier);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.serialization.Codec;
import dev.tocraft.ctgen.util.Registrar;
import dev.tocraft.ctgen.xtend.carver.Carver;
import dev.tocraft.ctgen.xtend.height.TerrainHeight;
import dev.tocraft.ctgen.xtend.layer.BlockLayer;
import dev.tocraft.ctgen.xtend.placer.BlockPlacer;
Expand All @@ -16,4 +17,5 @@ public final class CTRegistries {
public static final Registrar<Codec<? extends BlockPlacer>> BLOCK_PLACER = new Registrar<>();
public static final Registrar<Codec<? extends BlockLayer>> BLOCK_LAYER = new Registrar<>();
public static final Registrar<Codec<? extends TerrainHeight>> TERRAIN = new Registrar<>();
public static final Registrar<Codec<? extends Carver>> CARVER = new Registrar<>();
}
22 changes: 22 additions & 0 deletions common/src/main/java/dev/tocraft/ctgen/xtend/carver/Carver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.tocraft.ctgen.xtend.carver;

import com.mojang.serialization.Codec;
import dev.tocraft.ctgen.xtend.CTRegistries;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;

public abstract class Carver {
public static final Codec<Carver> CODEC = CTRegistries.CARVER.byNameCodec().dispatchStable(Carver::codec, Function.identity());

public static void register() {
CTRegistries.CARVER.register(NoiseCarver.ID, NoiseCarver.CODEC);
}


public abstract boolean canSetBlock(SimplexNoise noise, @NotNull BlockPos pos, double surfaceHeight, int minHeight, double carverModifier);

protected abstract Codec<? extends Carver> codec();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.tocraft.ctgen.worldgen;
package dev.tocraft.ctgen.xtend.carver;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dev.tocraft.ctgen.xtend.carver;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.ctgen.CTerrainGeneration;
import dev.tocraft.ctgen.util.Noise;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.NotNull;

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 Codec<NoiseCarver> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Noise.CODEC.optionalFieldOf("noise", DEFAULT.noise).forGetter(o -> o.noise),
Codec.DOUBLE.optionalFieldOf("threshold", DEFAULT.threshold).forGetter(o -> o.threshold)
).apply(instance, NoiseCarver::new));
public static final ResourceLocation ID = CTerrainGeneration.id("noise_carver");

private final Noise noise;
private final double threshold;

public NoiseCarver(Noise noise, double threshold) {
this.noise = noise;
this.threshold = threshold;
}


@Override
public boolean canSetBlock(SimplexNoise noise, @NotNull BlockPos pos, double surfaceHeight, int minHeight, double carverModifier) {
double height = (double) (pos.getY() - minHeight) / (surfaceHeight - minHeight) - 0.5;
double addThreshold = height * height * height * height * carverModifier;

double perlin = this.noise.getPerlin(noise, pos.getX(), pos.getY(), pos.getZ());

double threshold = this.threshold + addThreshold;
return !(perlin > threshold);
}

@Override
protected Codec<NoiseCarver> codec() {
return CODEC;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class NoiseHeight extends TerrainHeight {
public static final NoiseHeight DEFAULT = new NoiseHeight(Noise.DEFAULT);

public static final Codec<NoiseHeight> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Noise.CODEC.optionalFieldOf("noise", Noise.DEFAULT).forGetter(o -> o.noise)
Noise.CODEC.optionalFieldOf("noise", DEFAULT.noise).forGetter(o -> o.noise)
).apply(instance, NoiseHeight::new));
public static final ResourceLocation ID = CTerrainGeneration.id("noise_height");

Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/dev/tocraft/ctgen/zone/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public record Zone(Holder<Biome> biome, int color, Map<String, BlockPlacer> layers, int height, double terrainModifier,
double pixelWeight,
Optional<Integer> thresholdModifier) {
Optional<Double> carverModifier) {

public static final int DEFAULT_HEIGHT = 0;
public static final double DEFAULT_TERRAIN_MODIFIER = 8;
Expand All @@ -28,7 +28,7 @@ public record Zone(Holder<Biome> biome, int color, Map<String, BlockPlacer> laye
Codec.INT.optionalFieldOf("height", DEFAULT_HEIGHT).forGetter(Zone::height),
Codec.DOUBLE.optionalFieldOf("terrain_modifier", DEFAULT_TERRAIN_MODIFIER).forGetter(Zone::terrainModifier),
Codec.DOUBLE.optionalFieldOf("pixel_weight", DEFAULT_PIXEL_WEIGHT).forGetter(Zone::pixelWeight),
Codec.INT.optionalFieldOf("threshold_modifier").forGetter(Zone::thresholdModifier)
Codec.DOUBLE.optionalFieldOf("carver_modifier").forGetter(Zone::carverModifier)
).apply(instance, instance.stable(Zone::new)));

public static RegistryFileCodec<Zone> CODEC = RegistryFileCodec.create(CTRegistries.ZONES_KEY, DIRECT_CODEC);
Expand Down
Loading

0 comments on commit 6b491f7

Please sign in to comment.