End biome source (WIP)

This commit is contained in:
paulevsGitch 2021-08-13 21:23:12 +03:00
parent 0e0944ba51
commit 34ecbb3f14
8 changed files with 300 additions and 7 deletions

View file

@ -16,6 +16,7 @@ import ru.bclib.recipes.CraftingRecipes;
import ru.bclib.registry.BaseBlockEntities;
import ru.bclib.registry.BaseRegistry;
import ru.bclib.util.Logger;
import ru.bclib.world.generator.BCLibEndBiomeSource;
import ru.bclib.world.surface.BCLSurfaceBuilders;
import java.util.List;
@ -29,6 +30,7 @@ public class BCLib implements ModInitializer {
BaseRegistry.register();
BaseBlockEntities.register();
BCLSurfaceBuilders.register();
BCLibEndBiomeSource.register();
TagAPI.init();
CraftingRecipes.init();
WorldDataAPI.registerModCache(MOD_ID);

View file

@ -1,10 +1,11 @@
package ru.bclib.api;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.impl.biome.InternalBiomeData;
import net.fabricmc.fabric.impl.biome.WeightedBiomePicker;
import net.minecraft.client.Minecraft;
import net.minecraft.core.Registry;
import net.minecraft.data.BuiltinRegistries;
@ -14,13 +15,15 @@ import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biome.ClimateParameters;
import net.minecraft.world.level.biome.Biomes;
import ru.bclib.interfaces.BiomeListProvider;
import ru.bclib.util.MHelper;
import ru.bclib.world.biomes.BCLBiome;
import ru.bclib.world.generator.BiomePicker;
import java.util.HashMap;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
public class BiomeAPI {
/**
@ -29,9 +32,13 @@ public class BiomeAPI {
*/
public static final BCLBiome EMPTY_BIOME = new BCLBiome(Biomes.THE_VOID.location(), BuiltinRegistries.BIOME.get(Biomes.THE_VOID), 1, 0);
private static final Set<BCLBiome> NETHER_BIOMES = Sets.newHashSet();
private static final Set<BCLBiome> END_LAND_BIOMES = Sets.newHashSet();
private static final Set<BCLBiome> END_VOID_BIOMES = Sets.newHashSet();
private static final Map<ResourceLocation, BCLBiome> NETHER_BIOMES = Maps.newHashMap();
private static final Map<ResourceLocation, BCLBiome> END_LAND_BIOMES = Maps.newHashMap();
private static final Map<ResourceLocation, BCLBiome> END_VOID_BIOMES = Maps.newHashMap();
public static final BiomePicker NETHER_BIOME_PICKER = new BiomePicker();
public static final BiomePicker END_LAND_BIOME_PICKER = new BiomePicker();
public static final BiomePicker END_VOID_BIOME_PICKER = new BiomePicker();
private static final Map<ResourceLocation, BCLBiome> ID_MAP = Maps.newHashMap();
private static final Map<Biome, BCLBiome> CLIENT = Maps.newHashMap();
@ -65,6 +72,7 @@ public class BiomeAPI {
*/
public static void registerNetherBiome(BCLBiome biome) {
registerBiome(biome);
NETHER_BIOME_PICKER.addBiome(biome);
Random random = new Random(biome.getID().hashCode());
ClimateParameters parameters = new ClimateParameters(
MHelper.randRange(-1.5F, 1.5F, random),
@ -77,6 +85,18 @@ public class BiomeAPI {
InternalBiomeData.addNetherBiome(key, parameters);
}
/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib Nether Biome Generator and into Fabric Biome API.
* @param biome {@link BCLBiome}
*/
public static void registerNetherBiome(Biome biome) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, 1);
NETHER_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
}
/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a land biome (will generate only on islands).
@ -84,12 +104,25 @@ public class BiomeAPI {
*/
public static void registerEndLandBiome(BCLBiome biome) {
registerBiome(biome);
END_LAND_BIOME_PICKER.addBiome(biome);
float weight = biome.getGenChance();
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome.getBiome()).get();
InternalBiomeData.addEndBiomeReplacement(Biomes.END_HIGHLANDS, key, weight);
InternalBiomeData.addEndBiomeReplacement(Biomes.END_MIDLANDS, key, weight);
}
/**
* Register {@link BCLBiome} wrapper for {@link Biome}.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a land biome (will generate only on islands).
* @param biome {@link BCLBiome}
*/
public static void registerEndLandBiome(Biome biome) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, 1);
END_LAND_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
}
/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a void biome (will generate only in the End void - between islands).
@ -97,11 +130,24 @@ public class BiomeAPI {
*/
public static void registerEndVoidBiome(BCLBiome biome) {
registerBiome(biome);
END_VOID_BIOME_PICKER.addBiome(biome);
float weight = biome.getGenChance();
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome.getBiome()).get();
InternalBiomeData.addEndBiomeReplacement(Biomes.SMALL_END_ISLANDS, key, weight);
}
/**
* Register {@link BCLBiome} instance and its {@link Biome} if necessary.
* After that biome will be added to BCLib End Biome Generator and into Fabric Biome API as a void biome (will generate only in the End void - between islands).
* @param biome {@link BCLBiome}
*/
public static void registerEndVoidBiome(Biome biome) {
ResourceKey<Biome> key = BuiltinRegistries.BIOME.getResourceKey(biome).get();
BCLBiome bclBiome = new BCLBiome(key.location(), biome, 1, 1);
END_VOID_BIOME_PICKER.addBiome(bclBiome);
registerBiome(bclBiome);
}
/**
* Adds {@link BCLBiome} to FabricAPI biomes as the Nether biome (with random {@link ClimateParameters}).
*
@ -116,7 +162,7 @@ public class BiomeAPI {
MHelper.randRange(-2F, 2F, random),
MHelper.randRange(-2F, 2F, random),
MHelper.randRange(-2F, 2F, random),
MHelper.randRange(-2F, 2F, random)
random.nextFloat()
);
InternalBiomeData.addNetherBiome(key, parameters);
}
@ -226,4 +272,32 @@ public class BiomeAPI {
public static boolean hasBiome(ResourceLocation biomeID) {
return ID_MAP.containsKey(biomeID);
}
/**
* Load biomes from Fabric API. For internal usage only.
*/
public static void loadFabricAPIBiomes() {
List<ResourceKey<Biome>> biomes = Lists.newArrayList();
biomes.addAll(getBiomes(InternalBiomeData.getEndBiomesMap().get(Biomes.SMALL_END_ISLANDS)));
biomes.addAll(getBiomes(InternalBiomeData.getEndBarrensMap().get(Biomes.END_BARRENS)));
biomes.forEach((key) -> registerEndVoidBiome(BuiltinRegistries.BIOME.get(key.location())));
biomes.clear();
biomes.addAll(getBiomes(InternalBiomeData.getEndBiomesMap().get(Biomes.END_MIDLANDS)));
biomes.addAll(getBiomes(InternalBiomeData.getEndBiomesMap().get(Biomes.END_HIGHLANDS)));
biomes.forEach((key) -> registerEndLandBiome(BuiltinRegistries.BIOME.get(key.location())));
}
private static List<ResourceKey<Biome>> getBiomes(WeightedBiomePicker picker) {
BiomeListProvider biomeList = (BiomeListProvider) (Object) picker;
return biomeList == null ? Collections.emptyList() : biomeList.getBiomes();
}
public static boolean isNetherBiome(ResourceLocation biomeID) {
return NETHER_BIOMES.containsKey(biomeID);
}
public static boolean isEndBiome(ResourceLocation biomeID) {
return END_LAND_BIOMES.containsKey(biomeID) || END_VOID_BIOMES.containsKey(biomeID);
}
}

View file

@ -48,6 +48,7 @@ public class PostInitAPI {
}
});
postInitFunctions = null;
BiomeAPI.loadFabricAPIBiomes();
}
@Environment(EnvType.CLIENT)

View file

@ -5,6 +5,7 @@ import net.fabricmc.api.Environment;
import ru.bclib.BCLib;
public class Configs {
public static final PathConfig GENERATOR_CONFIG = new PathConfig(BCLib.MOD_ID, "generator");
public static final PathConfig MAIN_CONFIG = new PathConfig(BCLib.MOD_ID, "main");
public static final String MAIN_PATCH_CATEGORY = "patches";
public static final String MAIN_SYNC_CATEGORY = "client_sync";

View file

@ -0,0 +1,10 @@
package ru.bclib.interfaces;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;
import java.util.List;
public interface BiomeListProvider {
List<ResourceKey<Biome>> getBiomes();
}

View file

@ -0,0 +1,37 @@
package ru.bclib.mixin.common;
import com.google.common.collect.Lists;
import net.fabricmc.fabric.impl.biome.InternalBiomeData;
import net.fabricmc.fabric.impl.biome.WeightedBiomePicker;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.Biomes;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import ru.bclib.interfaces.BiomeListProvider;
import java.util.List;
@Mixin(value = WeightedBiomePicker.class, remap = false)
public class WeightedBiomePickerAccessor implements BiomeListProvider {
private final List<ResourceKey<Biome>> biomes = Lists.newArrayList();
@Inject(method = "addBiome", at = @At("TAIL"))
private void bclib_addBiome(final ResourceKey<Biome> biome, final double weight, CallbackInfo info) {
if (be_isCorrectPicker(WeightedBiomePicker.class.cast(this))) {
biomes.add(biome);
}
}
private boolean be_isCorrectPicker(WeightedBiomePicker picker) {
return picker == InternalBiomeData.getEndBiomesMap().get(Biomes.SMALL_END_ISLANDS) ||
picker == InternalBiomeData.getEndBarrensMap().get(Biomes.END_BARRENS);
}
@Override
public List<ResourceKey<Biome>> getBiomes() {
return biomes;
}
}

View file

@ -0,0 +1,128 @@
package ru.bclib.world.generator;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.core.Registry;
import net.minecraft.resources.RegistryLookupCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.biome.Biomes;
import net.minecraft.world.level.biome.TheEndBiomeSource;
import net.minecraft.world.level.levelgen.WorldgenRandom;
import net.minecraft.world.level.levelgen.synth.SimplexNoise;
import ru.bclib.BCLib;
import ru.bclib.api.BiomeAPI;
import ru.bclib.noise.OpenSimplexNoise;
import ru.bclib.world.biomes.BCLBiome;
import java.awt.Point;
import java.util.List;
import java.util.function.Function;
public class BCLibEndBiomeSource extends BiomeSource {
public static final Codec<BCLibEndBiomeSource> CODEC = RecordCodecBuilder.create((instance) -> {
return instance.group(RegistryLookupCodec.create(Registry.BIOME_REGISTRY).forGetter((theEndBiomeSource) -> {
return theEndBiomeSource.biomeRegistry;
}), Codec.LONG.fieldOf("seed").stable().forGetter((theEndBiomeSource) -> {
return theEndBiomeSource.seed;
})).apply(instance, instance.stable(BCLibEndBiomeSource::new));
});
private static final OpenSimplexNoise SMALL_NOISE = new OpenSimplexNoise(8324);
private Function<Point, Boolean> endLandFunction;
private final Registry<Biome> biomeRegistry;
private final SimplexNoise noise;
private final Biome centerBiome;
private final Biome barrens;
private BiomeMap mapLand;
private BiomeMap mapVoid;
private final long seed;
private final Point pos;
public BCLibEndBiomeSource(Registry<Biome> biomeRegistry, long seed) {
super(getBiomes(biomeRegistry));
biomeRegistry.forEach(biome -> {
ResourceLocation key = biomeRegistry.getKey(biome);
BCLBiome bclBiome = BiomeAPI.getBiome(key);
bclBiome.updateActualBiomes(biomeRegistry);
if (!BiomeAPI.END_LAND_BIOME_PICKER.containsImmutable(key)) {
BiomeAPI.END_LAND_BIOME_PICKER.addBiomeMutable(bclBiome);
}
});
this.mapLand = new BiomeMap(seed, GeneratorOptions.getBiomeSizeEndLand(), BiomeAPI.END_LAND_BIOME_PICKER);
this.mapVoid = new BiomeMap(seed, GeneratorOptions.getBiomeSizeEndVoid(), BiomeAPI.END_VOID_BIOME_PICKER);
this.centerBiome = biomeRegistry.getOrThrow(Biomes.THE_END);
this.barrens = biomeRegistry.getOrThrow(Biomes.END_BARRENS);
this.biomeRegistry = biomeRegistry;
this.seed = seed;
WorldgenRandom chunkRandom = new WorldgenRandom(seed);
chunkRandom.consumeCount(17292);
this.noise = new SimplexNoise(chunkRandom);
this.endLandFunction = GeneratorOptions.getEndLandFunction();
this.pos = new Point();
}
private static List<Biome> getBiomes(Registry<Biome> biomeRegistry) {
return biomeRegistry.stream().filter(biome -> BiomeAPI.isEndBiome(biomeRegistry.getKey(biome))).toList();
}
@Override
public Biome getNoiseBiome(int biomeX, int biomeY, int biomeZ) {
long i = (long) biomeX * (long) biomeX;
long j = (long) biomeZ * (long) biomeZ;
long dist = i + j;
if ((biomeX & 31) == 0 && (biomeZ & 31) == 0) {
mapLand.clearCache();
mapVoid.clearCache();
}
BCLBiome endBiome = null;
if (endLandFunction == null) {
if (dist <= 65536L) return centerBiome;
float height = TheEndBiomeSource.getHeightValue(
noise,
(biomeX >> 1) + 1,
(biomeZ >> 1) + 1
) + (float) SMALL_NOISE.eval(biomeX, biomeZ) * 5;
if (height > -20F && height < -5F) {
return barrens;
}
if (height < -10F) {
return mapVoid.getBiome(biomeX << 2, biomeZ << 2).getActualBiome();
}
else {
return mapLand.getBiome(biomeX << 2, biomeZ << 2).getActualBiome();
}
}
else {
pos.setLocation(biomeX, biomeZ);
if (endLandFunction.apply(pos)) {
return dist <= 65536L ? centerBiome : mapLand.getBiome(biomeX << 2, biomeZ << 2).getActualBiome();
}
else {
return dist <= 65536L ? barrens : mapVoid.getBiome(biomeX << 2, biomeZ << 2).getActualBiome();
}
}
}
@Override
public BiomeSource withSeed(long seed) {
return new BCLibEndBiomeSource(biomeRegistry, seed);
}
@Override
protected Codec<? extends BiomeSource> codec() {
return CODEC;
}
public static void register() {
Registry.register(Registry.BIOME_SOURCE, BCLib.makeID("better_end_biome_source"), CODEC);
}
}

View file

@ -0,0 +1,40 @@
package ru.bclib.world.generator;
import net.minecraft.util.Mth;
import ru.bclib.config.Configs;
import java.awt.Point;
import java.util.function.Function;
public class GeneratorOptions {
private static int biomeSizeNether;
private static int biomeSizeEndLand;
private static int biomeSizeEndVoid;
private static Function<Point, Boolean> endLandFunction;
public static void init() {
biomeSizeNether = Configs.GENERATOR_CONFIG.getInt("nether.biomeMap", "biomeSize", 256);
biomeSizeEndLand = Configs.GENERATOR_CONFIG.getInt("end.biomeMap", "biomeSizeLand", 256);
biomeSizeEndVoid = Configs.GENERATOR_CONFIG.getInt("end.biomeMap", "biomeSizeVoid", 256);
}
public static int getBiomeSizeNether() {
return Mth.clamp(biomeSizeNether, 1, 8192);
}
public static int getBiomeSizeEndLand() {
return Mth.clamp(biomeSizeEndLand, 1, 8192);
}
public static int getBiomeSizeEndVoid() {
return Mth.clamp(biomeSizeEndVoid, 1, 8192);
}
public static void setEndLandFunction(Function<Point, Boolean> endLandFunction) {
GeneratorOptions.endLandFunction = endLandFunction;
}
public static Function<Point, Boolean> getEndLandFunction() {
return endLandFunction;
}
}