Skip to content

Commit

Permalink
add block layers
Browse files Browse the repository at this point in the history
  • Loading branch information
ToCraft committed Oct 25, 2024
1 parent 8845eff commit 9579654
Show file tree
Hide file tree
Showing 30 changed files with 408 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.tocraft.crafted.ctgen;

import dev.tocraft.crafted.ctgen.blockplacer.BlockPlacer;
import dev.tocraft.crafted.ctgen.zone.Zone;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,31 @@
import dev.tocraft.crafted.ctgen.zone.Codecs;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.NotNull;

public class BasicPlacer extends BlockPlacer {
public static final BlockPlacer DEEPSLATE_BLOCK = new BasicPlacer(Blocks.DEEPSLATE);
public static final BlockPlacer STONE_BLOCK = new BasicPlacer(Blocks.STONE);
public static final BlockPlacer DIRT_BLOCK = new BasicPlacer(Blocks.DIRT);
public static final BlockPlacer GRASS_BLOCK = new BasicPlacer(Blocks.GRASS_BLOCK);
public static final BlockPlacer WATER_BLOCK = new BasicPlacer(Blocks.WATER);
public static final BlockPlacer AIR = new BasicPlacer(Blocks.AIR);

@NotNull
private final Block value;

public BasicPlacer(@NotNull Block value) {
this.value = value;
}

public Block get(SimplexNoise noise, double x, double y, double z, String layer) {
public @NotNull Block get(SimplexNoise noise, double x, double y, double z, String layer) {
return value;
}

@Override
public Block get(SimplexNoise noise, double x, double z, String layer) {
public @NotNull Block get(SimplexNoise noise, double x, double z, String layer) {
return value;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.core.Registry;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;
import java.util.function.Function;
Expand All @@ -33,8 +34,10 @@ public static void register() {
Registry.register(CTRegistries.BLOCK_PLACER, NoisePlacer.ID, NoisePlacer.CODEC);
}

@NotNull
public abstract Block get(SimplexNoise noise, double x, double y, double z, String layer);

@NotNull
public abstract Block get(SimplexNoise noise, double x, double z, String layer);

protected abstract Codec<? extends BlockPlacer> codec();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public boolean hasNoise() {
return noise != null && !thresholdMap.isEmpty();
}

public Block get(SimplexNoise noise, double x, double y, double z, String layer) {
public @NotNull Block get(SimplexNoise noise, double x, double y, double z, String layer) {
double perlin;

if (this.noise != null && hasNoise()) {
Expand All @@ -60,7 +60,7 @@ public Block get(SimplexNoise noise, double x, double y, double z, String layer)
}

@Override
public Block get(SimplexNoise noise, double x, double z, String layer) {
public @NotNull Block get(SimplexNoise noise, double x, double z, String layer) {
double perlin;

if (this.noise != null && hasNoise()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public MapWidget(Minecraft minecraft, int x, int y, int width, int height, Resou

updateZoomedWidth();
updateZoomedHeight();
resetTextureOffsets();
}

public float defaultZoom() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,72 @@
package dev.tocraft.crafted.ctgen.layer;

import com.mojang.serialization.Codec;
import dev.tocraft.crafted.ctgen.blockplacer.BasicPlacer;
import dev.tocraft.crafted.ctgen.blockplacer.BlockPlacer;
import dev.tocraft.crafted.ctgen.util.CTRegistries;
import dev.tocraft.crafted.ctgen.zone.Zone;
import net.minecraft.core.Registry;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;

import java.util.List;
import java.util.function.Function;

public abstract class BlockLayer {
public static final Codec<BlockLayer> CODEC = CTRegistries.BLOCK_LAYER.byNameCodec().dispatchStable(BlockLayer::codec, Function.identity());
private final String name;
private final boolean hasCaves;
private final BlockPlacer fallback;

protected final String name;

public BlockLayer(String name) {
public BlockLayer(String name, boolean hasCaves, BlockPlacer fallback) {
this.name = name;
this.hasCaves = hasCaves;
this.fallback = fallback;
}

public static void register() {
Registry.register(CTRegistries.BLOCK_LAYER, SeaLayer.ID, SeaLayer.CODEC);
Registry.register(CTRegistries.BLOCK_LAYER, HeightLayer.ID, HeightLayer.CODEC);
Registry.register(CTRegistries.BLOCK_LAYER, WeightLayer.ID, WeightLayer.CODEC);
}

public static BlockLayer deepslate(int minY) {
return new HeightLayer(minY, 0, true, "deepslate", BasicPlacer.DEEPSLATE_BLOCK);
}

public static BlockLayer stone() {
return new WeightLayer(0, 0.96, "stone", BasicPlacer.STONE_BLOCK);
}

public static BlockLayer dirt() {
// max is 200% to ensure there's no gap caused by "shifted" blocks
return new WeightLayer(0.96, 2, "dirt", BasicPlacer.DIRT_BLOCK);
}

public abstract boolean is(SimplexNoise noise, double x, double y, double z);
public static BlockLayer surface() {
return new SurfaceLayer("surface", BasicPlacer.GRASS_BLOCK);
}

public static BlockLayer sea() {
return new SeaLayer("sea", BasicPlacer.WATER_BLOCK);
}

public static List<BlockLayer> defaultLayers(int minY) {
return List.of(surface(), deepslate(minY), sea(), stone(), dirt());
}

public abstract boolean is(SimplexNoise noise, int x, int y, int z, Zone zone, int minY, int seaLevel, double surfaceHeight, int genHeight, int shift);

public String getName() {
return this.name;
}

;
public boolean hasCaves() {
return hasCaves;
}

public BlockPlacer getFallback() {
return fallback;
}

protected abstract Codec<? extends BlockLayer> codec();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package dev.tocraft.crafted.ctgen.layer;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.crafted.ctgen.CTerrainGeneration;
import dev.tocraft.crafted.ctgen.blockplacer.BlockPlacer;
import dev.tocraft.crafted.ctgen.zone.Zone;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;

@SuppressWarnings("unused")
public class HeightLayer extends BlockLayer {
private final int min;
private final int max;
private final boolean limitedToSurface;
private final boolean hasShift;

public HeightLayer(int minY, int maxY, String name, BlockPlacer fallback) {
this(minY, maxY, true, name, fallback);
}

public HeightLayer(int minY, int maxY, boolean hasShift, String name, BlockPlacer fallback) {
this(minY, maxY, hasShift, name, true, fallback);
}

public HeightLayer(int minY, int maxY, boolean hasShift, String name, boolean hasCaves, BlockPlacer fallback) {
this(minY, maxY, true, hasShift, name, hasCaves, fallback);
}

public HeightLayer(int minY, int maxY, boolean limitedToSurface, boolean hasShift, String name, boolean hasCaves, BlockPlacer fallback) {
super(name, hasCaves, fallback);
this.min = minY;
this.max = maxY;
this.hasShift = hasShift;
this.limitedToSurface = limitedToSurface;
}

public int getMaxY() {
return max;
}

public int getMinY() {
return min;
}

public boolean hasShift() {
return hasShift;
}

public boolean isLimitedToSurface() {
return limitedToSurface;
}

@Override
public boolean is(SimplexNoise noise, int x, int y, int z, Zone zone, int minY, int seaLevel, double surfaceHeight, int genHeight, int shift) {
int y2 = hasShift ? y + shift : y;
return y2 >= min - 1 && y2 <= max && (!limitedToSurface || y < surfaceHeight);
}

public static final Codec<HeightLayer> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.fieldOf("min").forGetter(HeightLayer::getMinY),
Codec.INT.fieldOf("max").forGetter(HeightLayer::getMaxY),
Codec.BOOL.optionalFieldOf("limit_to_surface", true).forGetter(HeightLayer::isLimitedToSurface),
Codec.BOOL.optionalFieldOf("shift", true).forGetter(HeightLayer::hasShift),
Codec.STRING.fieldOf("name").forGetter(BlockLayer::getName),
Codec.BOOL.optionalFieldOf("has_caves", true).forGetter(BlockLayer::hasCaves),
BlockPlacer.CODEC.fieldOf("fallback").forGetter(BlockLayer::getFallback)
).apply(instance, instance.stable(HeightLayer::new)));

public static final ResourceLocation ID = CTerrainGeneration.id("height");

@Override
protected Codec<HeightLayer> codec() {
return CODEC;
}
}
32 changes: 32 additions & 0 deletions common/src/main/java/dev/tocraft/crafted/ctgen/layer/SeaLayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.tocraft.crafted.ctgen.layer;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.crafted.ctgen.CTerrainGeneration;
import dev.tocraft.crafted.ctgen.blockplacer.BlockPlacer;
import dev.tocraft.crafted.ctgen.zone.Zone;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;

public class SeaLayer extends BlockLayer {
public SeaLayer(String name, BlockPlacer fallback) {
super(name, false, fallback);
}

@Override
public boolean is(SimplexNoise noise, int x, int y, int z, Zone zone, int minY, int seaLevel, double surfaceHeight, int genHeight, int shift) {
return y > surfaceHeight && surfaceHeight < seaLevel;
}

public static final Codec<SeaLayer> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.fieldOf("name").forGetter(BlockLayer::getName),
BlockPlacer.CODEC.fieldOf("fallback").forGetter(BlockLayer::getFallback)
).apply(instance, instance.stable(SeaLayer::new)));

public static final ResourceLocation ID = CTerrainGeneration.id("sea");

@Override
protected Codec<SeaLayer> codec() {
return CODEC;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.tocraft.crafted.ctgen.layer;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.crafted.ctgen.CTerrainGeneration;
import dev.tocraft.crafted.ctgen.blockplacer.BlockPlacer;
import dev.tocraft.crafted.ctgen.zone.Zone;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;

public class SurfaceLayer extends BlockLayer {
public SurfaceLayer(String name, BlockPlacer fallback) {
this(name, true, fallback);
}

public SurfaceLayer(String name, boolean hasCaves, BlockPlacer fallback) {
super(name, hasCaves, fallback);
}

@Override
public boolean is(SimplexNoise noise, int x, int y, int z, Zone zone, int minY, int seaLevel, double surfaceHeight, int genHeight, int shift) {
return y == (int) surfaceHeight;
}

public static final Codec<SurfaceLayer> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.STRING.optionalFieldOf("name", "surface").forGetter(BlockLayer::getName),
Codec.BOOL.optionalFieldOf("has_caves", true).forGetter(BlockLayer::hasCaves),
BlockPlacer.CODEC.fieldOf("fallback").forGetter(BlockLayer::getFallback)
).apply(instance, instance.stable(SurfaceLayer::new)));

public static final ResourceLocation ID = CTerrainGeneration.id("sea");

@Override
protected Codec<SurfaceLayer> codec() {
return CODEC;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package dev.tocraft.crafted.ctgen.layer;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.tocraft.crafted.ctgen.CTerrainGeneration;
import dev.tocraft.crafted.ctgen.blockplacer.BlockPlacer;
import dev.tocraft.crafted.ctgen.zone.Zone;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;

public class WeightLayer extends BlockLayer {
private final double min;
private final double max;
private final boolean hasShift;

public WeightLayer(double minPercentage, double maxPercentage, String name, BlockPlacer fallback) {
this(minPercentage, maxPercentage, true, name, fallback);
}

public WeightLayer(double minPercentage, double maxPercentage, boolean hasShift, String name, BlockPlacer fallback) {
this(minPercentage, maxPercentage, hasShift, name, true, fallback);
}

public WeightLayer(double minPercentage, double maxPercentage, boolean hasShift, String name, boolean hasCaves, BlockPlacer fallback) {
super(name, hasCaves, fallback);
this.min = minPercentage;
this.max = maxPercentage;
this.hasShift = hasShift;
}

public double getMaxPercentage() {
return max;
}

public double getMinPercentage() {
return min;
}

public boolean hasShift() {
return hasShift;
}

@Override
public boolean is(SimplexNoise noise, int x, int y, int z, Zone zone, int minY, int seaLevel, double surfaceHeight, int genHeight, int shift) {
int y2 = hasShift ? y + shift : y;
double percentage = (y2 - minY) / (surfaceHeight - minY);
return this.min <= percentage && percentage <= this.max;
}

public static final Codec<WeightLayer> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.DOUBLE.fieldOf("min_percentage").forGetter(WeightLayer::getMinPercentage),
Codec.DOUBLE.fieldOf("max_percentage").forGetter(WeightLayer::getMaxPercentage),
Codec.BOOL.optionalFieldOf("shift", true).forGetter(WeightLayer::hasShift),
Codec.STRING.fieldOf("name").forGetter(BlockLayer::getName),
Codec.BOOL.optionalFieldOf("has_caves", true).forGetter(BlockLayer::hasCaves),
BlockPlacer.CODEC.fieldOf("fallback").forGetter(BlockLayer::getFallback)
).apply(instance, instance.stable(WeightLayer::new)));

public static final ResourceLocation ID = CTerrainGeneration.id("weight");

@Override
protected Codec<WeightLayer> codec() {
return CODEC;
}
}
Loading

0 comments on commit 9579654

Please sign in to comment.