mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-20 20:12:19 +00:00
Fix #1616 Smarter tree generation
This commit is contained in:
1 parent
c2313c7569
commit
d29aa13377
3 files changed
+57
-22
No files matched your search
@@ -7,9 +7,12 @@ package forestry.api.arboriculture;
|
||||
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
|
||||
public interface IGrowthProvider {
|
||||
|
||||
boolean canSpawn(ITreeGenome genome, World world, BlockPos pos);
|
||||
boolean canSpawn(ITree tree, World world, BlockPos pos);
|
||||
|
||||
boolean isBiomeValid(ITree tree, Biome biome);
|
||||
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
package forestry.arboriculture.genetics;
|
||||
|
||||
import forestry.api.arboriculture.IGrowthProvider;
|
||||
import forestry.api.arboriculture.ITree;
|
||||
import forestry.api.arboriculture.ITreeGenome;
|
||||
import forestry.api.core.EnumHumidity;
|
||||
import forestry.api.core.EnumTemperature;
|
||||
import forestry.api.genetics.AlleleManager;
|
||||
import forestry.api.genetics.EnumTolerance;
|
||||
import forestry.core.utils.ClimateUtil;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
|
||||
public class ClimateGrowthProvider implements IGrowthProvider {
|
||||
|
||||
@@ -32,9 +33,15 @@ public class ClimateGrowthProvider implements IGrowthProvider {
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSpawn(ITreeGenome genome, World world, BlockPos pos) {
|
||||
EnumTemperature biomeTemperature = EnumTemperature.getFromBiome(world.getBiome(pos), world, pos);
|
||||
EnumHumidity biomeHumidity = EnumHumidity.getFromValue(ClimateUtil.getHumidity(world, pos));
|
||||
public boolean canSpawn(ITree tree, World world, BlockPos pos) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isBiomeValid(ITree tree, Biome biome) {
|
||||
EnumTemperature biomeTemperature = EnumTemperature.getFromBiome(biome);
|
||||
EnumHumidity biomeHumidity = EnumHumidity.getFromValue(biome.getRainfall());
|
||||
ITreeGenome genome = tree.getGenome();
|
||||
if(temperature == null){
|
||||
temperature = genome.getPrimary().getTemperature();
|
||||
}
|
||||
|
||||
@@ -11,12 +11,16 @@
|
||||
package forestry.arboriculture.worldgen;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Random;
|
||||
import java.util.Set;
|
||||
|
||||
import forestry.api.arboriculture.EnumTreeChromosome;
|
||||
import forestry.api.arboriculture.IAlleleTreeSpecies;
|
||||
import forestry.api.arboriculture.IGrowthProvider;
|
||||
import forestry.api.arboriculture.ITree;
|
||||
import forestry.api.arboriculture.ITreeGenome;
|
||||
import forestry.api.arboriculture.TreeManager;
|
||||
@@ -24,11 +28,11 @@ import forestry.api.genetics.AlleleManager;
|
||||
import forestry.api.genetics.IAllele;
|
||||
import forestry.arboriculture.commands.TreeGenHelper;
|
||||
import forestry.core.utils.BlockUtil;
|
||||
import forestry.core.utils.Log;
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
import net.minecraftforge.common.IPlantable;
|
||||
import net.minecraftforge.common.util.EnumHelper;
|
||||
import net.minecraftforge.event.terraingen.PopulateChunkEvent.Populate.EventType;
|
||||
@@ -39,6 +43,7 @@ public class TreeDecorator {
|
||||
|
||||
private static final EventType EVENT_TYPE = EnumHelper.addEnum(EventType.class, "FORESTRY_TREES", new Class[0]);
|
||||
private static final List<IAlleleTreeSpecies> SPECIES = new ArrayList<>();
|
||||
private static final Map<Biome, Set<ITree>> biomeCache = new HashMap<>();
|
||||
|
||||
@SubscribeEvent
|
||||
public void decorateTrees(Decorate event) {
|
||||
@@ -48,32 +53,27 @@ public class TreeDecorator {
|
||||
}
|
||||
|
||||
public static void decorateTrees(World world, Random rand, int worldX, int worldZ) {
|
||||
List<IAlleleTreeSpecies> trees = getSpecies();
|
||||
|
||||
Collections.shuffle(trees, rand);
|
||||
|
||||
for (int tries = 0; tries < 4; tries++) {
|
||||
if(biomeCache.isEmpty()){
|
||||
generateBiomeCache(world, rand);
|
||||
}
|
||||
for (int tries = 0; tries < 4 + rand.nextInt(2); tries++) {
|
||||
int x = worldX + rand.nextInt(16);
|
||||
int z = worldZ + rand.nextInt(16);
|
||||
|
||||
BlockPos pos = new BlockPos(x, 0, z);
|
||||
if (!world.isBlockLoaded(pos)) {
|
||||
Log.error("tried to generate a hive in an unloaded area.");
|
||||
return;
|
||||
}
|
||||
Biome biome = world.getBiome(pos);
|
||||
|
||||
for (IAlleleTreeSpecies species : trees) {
|
||||
Set<ITree> trees = biomeCache.get(biome);
|
||||
for(ITree tree : trees){
|
||||
IAlleleTreeSpecies species = tree.getGenome().getPrimary();
|
||||
if (species.getRarity() >= rand.nextFloat()) {
|
||||
IAllele[] template = TreeManager.treeRoot.getTemplate(species);
|
||||
ITreeGenome genome = TreeManager.treeRoot.templateAsGenome(template);
|
||||
ITree tree = TreeManager.treeRoot.getTree(world, genome);
|
||||
pos = getValidPos(world, x, z, tree);
|
||||
|
||||
|
||||
if(pos == null){
|
||||
continue;
|
||||
}
|
||||
|
||||
if (species.getGrowthProvider().canSpawn(genome, world, pos)) {
|
||||
if (species.getGrowthProvider().canSpawn(tree, world, pos)) {
|
||||
if (TreeGenHelper.generateTree(tree, world, pos)) {
|
||||
return;
|
||||
}
|
||||
@@ -122,4 +122,29 @@ public class TreeDecorator {
|
||||
}
|
||||
return SPECIES;
|
||||
}
|
||||
|
||||
private static void generateBiomeCache(World world, Random rand){
|
||||
for(IAlleleTreeSpecies species : getSpecies()){
|
||||
IAllele[] template = TreeManager.treeRoot.getTemplate(species);
|
||||
ITreeGenome genome = TreeManager.treeRoot.templateAsGenome(template);
|
||||
ITree tree = TreeManager.treeRoot.getTree(world, genome);
|
||||
IGrowthProvider growthProvider = species.getGrowthProvider();
|
||||
for(Biome biome : Biome.REGISTRY){
|
||||
Set<ITree> trees = biomeCache.computeIfAbsent(biome, k -> new HashSet<>());
|
||||
if(growthProvider.isBiomeValid(tree, biome)){
|
||||
trees.add(tree);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final class BiomeCache {
|
||||
protected final Biome biome;
|
||||
protected final List<ITree> validTrees;
|
||||
|
||||
public BiomeCache(Biome biome, List<ITree> validTrees) {
|
||||
this.biome = biome;
|
||||
this.validTrees = validTrees;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user