Vanilla biome setting (sort of) works on Fabric now

This commit is contained in:
dfsek
2020-12-13 17:18:19 -07:00
parent a058f1c58b
commit f609727afb
12 changed files with 125 additions and 101 deletions

View File

@@ -1,57 +1,26 @@
package com.dfsek.terra;
import com.dfsek.terra.api.gaea.biome.BiomeGrid;
import com.dfsek.terra.api.generic.TerraPlugin;
import com.dfsek.terra.api.generic.world.World;
import com.dfsek.terra.biome.BiomeZone;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.biome.grid.master.TerraRadialBiomeGrid;
import com.dfsek.terra.biome.grid.master.TerraStandardBiomeGrid;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.base.ConfigPackTemplate;
import com.dfsek.terra.config.builder.biomegrid.BiomeGridBuilder;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.generation.TerraChunkGenerator;
public class TerraWorld {
private final TerraBiomeGrid grid;
private final BiomeZone zone;
private final ConfigPack config;
private boolean safe;
private final boolean safe;
private final TerraProfiler profiler;
public TerraWorld(World w, ConfigPack c, TerraPlugin main) {
safe = true;
config = c;
profiler = new TerraProfiler(w);
ConfigPackTemplate template = config.getTemplate();
int zoneSize = template.getGrids().size();
BiomeGrid[] definedGrids = new BiomeGrid[zoneSize];
for(int i = 0; i < zoneSize; i++) {
String partName = template.getGrids().get(i);
try {
BiomeGridBuilder g = config.getBiomeGrid(partName);
BiomeGrid b = g.build(w.getSeed(), c);
definedGrids[i] = b;
} catch(NullPointerException e) {
safe = false;
Debug.stack(e);
main.getLogger().severe("No such BiomeGrid " + partName);
main.getLogger().severe("Please check configuration files for errors. Configuration errors will have been reported during initialization.");
main.getLogger().severe("ONLY report this to Terra if you are SURE your config is error-free.");
main.getLogger().severe("Terrain will NOT generate properly at this point. Correct your config before using your server!");
}
}
zone = new BiomeZone(w.getSeed(), c, definedGrids);
if(template.getGridType().equals(TerraBiomeGrid.Type.RADIAL)) {
BiomeGrid internal = config.getBiomeGrid(template.getRadialInternalGrid()).build(w.getSeed(), c);
grid = new TerraRadialBiomeGrid(w.getSeed(), template.getGridFreqX(), template.getGridFreqZ(), zone, config, template.getRadialGridRadius(), internal);
} else grid = new TerraStandardBiomeGrid(w.getSeed(), template.getGridFreqX(), template.getGridFreqZ(), zone, config);
this.grid = new TerraBiomeGrid.TerraBiomeGridBuilder(w.getSeed(), c, main).build();
this.zone = grid.getZone();
safe = true;
}
public static boolean isTerraWorld(World w) {

View File

@@ -11,7 +11,9 @@ import java.util.List;
import java.util.Random;
public interface TerraChunkGenerator {
ChunkGenerator.ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome, ChunkGenerator.ChunkData original);
ChunkGenerator.ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int x, int z, ChunkGenerator.ChunkData original);
void generateBiomes(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome);
void attachProfiler(WorldProfiler profiler);

View File

@@ -1,11 +1,24 @@
package com.dfsek.terra.biome.grid.master;
import com.dfsek.terra.api.gaea.biome.BiomeGrid;
import com.dfsek.terra.api.generic.TerraPlugin;
import com.dfsek.terra.biome.BiomeZone;
import com.dfsek.terra.biome.grid.UserDefinedGrid;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.base.ConfigPackTemplate;
import com.dfsek.terra.config.builder.biomegrid.BiomeGridBuilder;
import com.dfsek.terra.debug.Debug;
public abstract class TerraBiomeGrid extends BiomeGrid {
public TerraBiomeGrid(long seed, double freq1, double freq2, int sizeX, int sizeZ) {
protected final BiomeZone zone;
public TerraBiomeGrid(long seed, double freq1, double freq2, int sizeX, int sizeZ, BiomeZone zone) {
super(seed, freq1, freq2, sizeX, sizeZ);
this.zone = zone;
}
public BiomeZone getZone() {
return zone;
}
public abstract UserDefinedGrid getGrid(int x, int z);
@@ -13,4 +26,44 @@ public abstract class TerraBiomeGrid extends BiomeGrid {
public enum Type {
RADIAL, STANDARD
}
public static final class TerraBiomeGridBuilder {
private final long seed;
private final ConfigPack config;
private final TerraPlugin main;
public TerraBiomeGridBuilder(long seed, ConfigPack config, TerraPlugin main) {
this.seed = seed;
this.config = config;
this.main = main;
}
public TerraBiomeGrid build() {
ConfigPackTemplate template = config.getTemplate();
int zoneSize = template.getGrids().size();
BiomeGrid[] definedGrids = new BiomeGrid[zoneSize];
for(int i = 0; i < zoneSize; i++) {
String partName = template.getGrids().get(i);
try {
BiomeGridBuilder g = config.getBiomeGrid(partName);
BiomeGrid b = g.build(seed, config);
definedGrids[i] = b;
} catch(NullPointerException e) {
Debug.stack(e);
main.getLogger().severe("No such BiomeGrid " + partName);
main.getLogger().severe("Please check configuration files for errors. Configuration errors will have been reported during initialization.");
main.getLogger().severe("ONLY report this to Terra if you are SURE your config is error-free.");
main.getLogger().severe("Terrain will NOT generate properly at this point. Correct your config before using your server!");
}
}
BiomeZone zone = new BiomeZone(seed, config, definedGrids);
if(template.getGridType().equals(TerraBiomeGrid.Type.RADIAL)) {
BiomeGrid internal = config.getBiomeGrid(template.getRadialInternalGrid()).build(seed, config);
return new TerraRadialBiomeGrid(seed, template.getGridFreqX(), template.getGridFreqZ(), zone, config, template.getRadialGridRadius(), internal);
} else return new TerraStandardBiomeGrid(seed, template.getGridFreqX(), template.getGridFreqZ(), zone, config);
}
}
}

View File

@@ -16,19 +16,17 @@ import net.jafama.FastMath;
public class TerraRadialBiomeGrid extends TerraBiomeGrid {
private static final int failNum = 0;
private final BiomeZone zone;
private final double radiusSq;
private final BiomeGrid internal;
private CoordinatePerturb perturb;
private ErosionNoise erode;
public TerraRadialBiomeGrid(long seed, double freq1, double freq2, BiomeZone zone, ConfigPack c, double radius, BiomeGrid internal) {
super(seed, freq1, freq2, 0, 0);
super(seed, freq1, freq2, 0, 0, zone);
ConfigPackTemplate t = c.getTemplate();
if(c.getTemplate().isBlend()) {
perturb = new CoordinatePerturb(t.getBlendFreq(), t.getBlendAmp(), seed);
}
this.zone = zone;
if(c.getTemplate().isErode()) {
erode = new ErosionNoise(t.getErodeFreq(), t.getErodeThresh(), t.getErodeOctaves(), seed);
}

View File

@@ -14,17 +14,15 @@ import com.dfsek.terra.config.base.ConfigPackTemplate;
public class TerraStandardBiomeGrid extends TerraBiomeGrid {
private static final int failNum = 0;
private final BiomeZone zone;
private CoordinatePerturb perturb;
private ErosionNoise erode;
public TerraStandardBiomeGrid(long seed, double freq1, double freq2, BiomeZone zone, ConfigPack c) {
super(seed, freq1, freq2, 0, 0);
super(seed, freq1, freq2, 0, 0, zone);
ConfigPackTemplate t = c.getTemplate();
if(c.getTemplate().isBlend()) {
perturb = new CoordinatePerturb(t.getBlendFreq(), t.getBlendAmp(), seed);
}
this.zone = zone;
if(c.getTemplate().isErode()) {
erode = new ErosionNoise(t.getErodeFreq(), t.getErodeThresh(), t.getErodeOctaves(), seed);
}

View File

@@ -114,7 +114,7 @@ public class TerraChunkGenerator implements com.dfsek.terra.api.generic.generato
@Override
@SuppressWarnings({"try"})
public ChunkGenerator.ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int chunkX, int chunkZ, @NotNull BiomeGrid biome, ChunkGenerator.ChunkData chunk) {
public ChunkGenerator.ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int chunkX, int chunkZ, ChunkGenerator.ChunkData chunk) {
TerraWorld tw = main.getWorld(world);
com.dfsek.terra.api.gaea.biome.BiomeGrid grid = tw.getGrid();
try(ProfileFuture ignore = tw.getProfiler().measure("TotalChunkGenTime")) {
@@ -177,21 +177,27 @@ public class TerraChunkGenerator implements com.dfsek.terra.api.generic.generato
}
}
}
int xOrig = (chunkX << 4);
int zOrig = (chunkZ << 4);
for(int x = 0; x < 4; x++) {
for(byte z = 0; z < 4; z++) {
int cx = xOrig + (x << 2);
int cz = zOrig + (z << 2);
Biome b = grid.getBiome(cx, cz, GenerationPhase.PALETTE_APPLY);
biome.setBiome(x << 2, z << 2, b.getVanillaBiome());
}
}
return chunk;
}
}
@Override
public void generateBiomes(@NotNull World world, @NotNull Random random, int chunkX, int chunkZ, @NotNull BiomeGrid biome) {
int xOrig = (chunkX << 4);
int zOrig = (chunkZ << 4);
com.dfsek.terra.api.gaea.biome.BiomeGrid grid = main.getWorld(world).getGrid();
for(int x = 0; x < 4; x++) {
for(byte z = 0; z < 4; z++) {
int cx = xOrig + (x << 2);
int cz = zOrig + (z << 2);
Biome b = grid.getBiome(cx, cz, GenerationPhase.PALETTE_APPLY);
biome.setBiome(x << 2, z << 2, b.getVanillaBiome());
}
}
}
public void attachProfiler(WorldProfiler p) {
popMan.attachProfiler(p);
}

View File

@@ -23,8 +23,8 @@ public class BukkitChunkGeneratorWrapper extends ChunkGenerator implements Handl
@Override
public @NotNull ChunkData generateChunkData(@NotNull World world, @NotNull Random random, int x, int z, @NotNull BiomeGrid biome) {
BukkitWorld bukkitWorld = new BukkitWorld(world);
return (ChunkData) delegate.generateChunkData(bukkitWorld, random, x, z, new BukkitBiomeGrid(biome), new BukkitChunkGenerator.BukkitChunkData(createChunkData(world))).getHandle();
delegate.generateBiomes(bukkitWorld, random, x, z, new BukkitBiomeGrid(biome));
return (ChunkData) delegate.generateChunkData(bukkitWorld, random, x, z, new BukkitChunkGenerator.BukkitChunkData(createChunkData(world))).getHandle();
}
@Override

View File

@@ -23,6 +23,8 @@ import com.dfsek.terra.registry.ConfigRegistry;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.world.GeneratorType;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.chunk.ChunkGenerator;
@@ -127,7 +129,15 @@ public class TerraFabricPlugin implements TerraPlugin, ModInitializer {
registry
.registerLoader(BlockData.class, (t, o, l) -> worldHandle.createBlockData((String) o))
.registerLoader(MaterialData.class, (t, o, l) -> worldHandle.createMaterialData((String) o))
.registerLoader(com.dfsek.terra.api.generic.world.Biome.class, (t, o, l) -> new FabricBiome());
.registerLoader(com.dfsek.terra.api.generic.world.Biome.class, (t, o, l) -> {
String id = (String) o;
if(!id.contains(":")) id = "minecraft:" + id.toLowerCase();
Identifier identifier = new Identifier(id);
logger.info("Registering Vanilla biome: " + o.toString() + " with ID " + identifier + "/" + id);
Biome biome = BuiltinRegistries.BIOME.get(identifier);
logger.info("Found " + biome + " in registry.");
return new FabricBiome(biome);
});
}
@Override

View File

@@ -3,8 +3,15 @@ package com.dfsek.terra.fabric.world;
import com.dfsek.terra.api.generic.world.Biome;
public class FabricBiome implements Biome {
private final net.minecraft.world.biome.Biome delegate;
public FabricBiome(net.minecraft.world.biome.Biome delegate) {
this.delegate = delegate;
}
@Override
public Object getHandle() {
return null;
public net.minecraft.world.biome.Biome getHandle() {
return delegate;
}
}

View File

@@ -1,32 +0,0 @@
package com.dfsek.terra.fabric.world;
import com.dfsek.terra.api.generic.world.Biome;
import com.dfsek.terra.api.generic.world.BiomeGrid;
import org.jetbrains.annotations.NotNull;
public class FabricBiomeGrid implements BiomeGrid {
@Override
public @NotNull Biome getBiome(int x, int z) {
return new FabricBiome();
}
@Override
public @NotNull Biome getBiome(int x, int y, int z) {
return new FabricBiome();
}
@Override
public void setBiome(int x, int z, @NotNull Biome bio) {
}
@Override
public void setBiome(int x, int y, int z, @NotNull Biome bio) {
}
@Override
public Object getHandle() {
return null;
}
}

View File

@@ -1,6 +1,7 @@
package com.dfsek.terra.fabric.world;
import com.dfsek.terra.api.generic.TerraPlugin;
import com.dfsek.terra.api.gaea.generation.GenerationPhase;
import com.dfsek.terra.biome.grid.master.TerraBiomeGrid;
import com.dfsek.terra.fabric.TerraFabricPlugin;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
@@ -8,6 +9,7 @@ import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryLookupCodec;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.biome.source.BiomeSource;
import net.minecraft.world.gen.feature.StructureFeature;
import java.util.stream.Collectors;
@@ -19,13 +21,13 @@ public class TerraBiomeSource extends BiomeSource {
private final Registry<Biome> biomeRegistry;
private final long seed;
private final TerraPlugin main;
private final TerraBiomeGrid grid;
public TerraBiomeSource(Registry<Biome> biomes, long seed) {
super(biomes.stream().collect(Collectors.toList()));
this.biomeRegistry = biomes;
this.seed = seed;
this.main = TerraFabricPlugin.getInstance();
this.grid = new TerraBiomeGrid.TerraBiomeGridBuilder(seed, TerraFabricPlugin.getInstance().getRegistry().get("DEFAULT"), TerraFabricPlugin.getInstance()).build();
}
@Override
@@ -40,6 +42,14 @@ public class TerraBiomeSource extends BiomeSource {
@Override
public Biome getBiomeForNoiseGen(int biomeX, int biomeY, int biomeZ) {
return null;
FabricBiome biome = ((FabricBiome) grid.getBiome(biomeX * 4, biomeZ * 4, GenerationPhase.BASE).getVanillaBiome());
return biome.getHandle();
}
@Override
public boolean hasStructureFeature(StructureFeature<?> feature) {
return false;
}
}

View File

@@ -3,7 +3,7 @@ package com.dfsek.terra.fabric.world.generator;
import com.dfsek.terra.api.gaea.util.FastRandom;
import com.dfsek.terra.api.generic.Handle;
import com.dfsek.terra.fabric.TerraFabricPlugin;
import com.dfsek.terra.fabric.world.FabricBiomeGrid;
import com.dfsek.terra.fabric.world.TerraBiomeSource;
import com.dfsek.terra.fabric.world.handles.FabricSeededWorldAccess;
import com.dfsek.terra.generation.TerraChunkGenerator;
import com.mojang.serialization.Codec;
@@ -14,7 +14,6 @@ import net.minecraft.world.BlockView;
import net.minecraft.world.ChunkRegion;
import net.minecraft.world.Heightmap;
import net.minecraft.world.WorldAccess;
import net.minecraft.world.biome.source.BiomeSource;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.gen.StructureAccessor;
import net.minecraft.world.gen.chunk.ChunkGenerator;
@@ -24,15 +23,18 @@ import net.minecraft.world.gen.chunk.VerticalBlockSample;
public class FabricChunkGeneratorWrapper extends ChunkGenerator implements Handle {
private final long seed;
private final TerraChunkGenerator delegate;
private final TerraBiomeSource biomeSource;
private final Codec<FabricChunkGeneratorWrapper> codec = RecordCodecBuilder.create(instance -> instance.group(
BiomeSource.CODEC.fieldOf("biome_source").forGetter(generator -> generator.biomeSource),
TerraBiomeSource.CODEC.fieldOf("biome_source").forGetter(generator -> generator.biomeSource),
Codec.LONG.fieldOf("seed").stable().forGetter(generator -> generator.seed))
.apply(instance, instance.stable(FabricChunkGeneratorWrapper::new)));
public FabricChunkGeneratorWrapper(BiomeSource biomeSource, long seed) {
public FabricChunkGeneratorWrapper(TerraBiomeSource biomeSource, long seed) {
super(biomeSource, new StructuresConfig(false));
this.delegate = new TerraChunkGenerator(TerraFabricPlugin.getInstance().getRegistry().get("DEFAULT"), TerraFabricPlugin.getInstance());
delegate.getMain().getLogger().info("Loading world...");
this.biomeSource = biomeSource;
this.seed = seed;
}
@@ -49,7 +51,7 @@ public class FabricChunkGeneratorWrapper extends ChunkGenerator implements Handl
@Override
public ChunkGenerator withSeed(long seed) {
return new FabricChunkGeneratorWrapper(this.biomeSource.withSeed(seed), seed);
return new FabricChunkGeneratorWrapper((TerraBiomeSource) this.biomeSource.withSeed(seed), seed);
}
@Override
@@ -59,7 +61,8 @@ public class FabricChunkGeneratorWrapper extends ChunkGenerator implements Handl
@Override
public void populateNoise(WorldAccess world, StructureAccessor accessor, Chunk chunk) {
delegate.generateChunkData(new FabricSeededWorldAccess(world, seed, this), new FastRandom(), chunk.getPos().x, chunk.getPos().z, new FabricBiomeGrid(), new FabricChunkData(chunk));
FabricSeededWorldAccess worldAccess = new FabricSeededWorldAccess(world, seed, this);
delegate.generateChunkData(worldAccess, new FastRandom(), chunk.getPos().x, chunk.getPos().z, new FabricChunkData(chunk));
}
@Override