Merge tree and flora population, increase performance of both.

This commit is contained in:
dfsek
2020-10-13 18:25:22 -07:00
parent 49045eca06
commit 064a30190c
8 changed files with 42 additions and 88 deletions

View File

@@ -1,5 +1,6 @@
package com.dfsek.terra.population;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.TerraBiomeGrid;
@@ -7,17 +8,26 @@ import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.genconfig.biome.BiomeConfig;
import com.dfsek.terra.config.genconfig.biome.BiomeFloraConfig;
import com.dfsek.terra.generation.UserDefinedDecorator;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.biome.Biome;
import org.polydev.gaea.generation.GenerationPhase;
import org.polydev.gaea.math.Range;
import org.polydev.gaea.population.GaeaBlockPopulator;
import org.polydev.gaea.profiler.ProfileFuture;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.world.Flora;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Populates Flora and Trees
*/
public class FloraPopulator extends GaeaBlockPopulator {
@Override
public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) {
@@ -29,6 +39,12 @@ public class FloraPopulator extends GaeaBlockPopulator {
for(int x = 0; x < 16; x++) {
for(int z = 0; z < 16; z++) {
UserDefinedBiome biome = (UserDefinedBiome) grid.getBiome((chunk.getX() << 4) + x, (chunk.getZ() << 4) + z, GenerationPhase.POPULATE);
if((x & 1) == 0 && (z & 1) == 0) {
int treeChance = biome.getDecorator().getTreeDensity();
if(random.nextInt(1000) < treeChance) {
if(doTrees(biome, tw, random, chunk, offset(random, x), offset(random, z))) continue;
}
}
if(biome.getDecorator().getFloraChance() <= 0) continue;
try {
BiomeConfig c = config.getBiome(biome);
@@ -47,4 +63,27 @@ public class FloraPopulator extends GaeaBlockPopulator {
}
}
}
private static boolean doTrees(@NotNull UserDefinedBiome biome, TerraWorld world, @NotNull Random random, @NotNull Chunk chunk, int x, int z) {
for(Block block : getValidTreeSpawnsAt(chunk, x, z, new Range(0, 254))) {
Tree tree = biome.getDecorator().getTrees().get(random);
Range range = world.getConfig().getBiome(biome).getTreeRange(tree);
if(!range.isInRange(block.getY())) continue;
try {
return tree.plant(block.getLocation(), random, false, Terra.getInstance());
} catch(NullPointerException ignore) {}
}
return false;
}
private static int offset(Random r, int i) {
return Math.min(Math.max(i + r.nextInt(3)-1, 0), 15);
}
public static List<Block> getValidTreeSpawnsAt(Chunk chunk, int x, int z, Range check) {
List<Block> blocks = new ArrayList<>();
for(int y : check) {
if(chunk.getBlock(x, y, z).getType().isSolid() && chunk.getBlock(x, y + 1, z).getType().isAir()) {
blocks.add(chunk.getBlock(x, y, z));
}
}
return blocks;
}
}

View File

@@ -1,69 +0,0 @@
package com.dfsek.terra.population;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.TerraBiomeGrid;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.base.WorldConfig;
import com.dfsek.terra.generation.UserDefinedDecorator;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.jetbrains.annotations.NotNull;
import org.polydev.gaea.biome.Biome;
import org.polydev.gaea.generation.GenerationPhase;
import org.polydev.gaea.math.Range;
import org.polydev.gaea.population.GaeaBlockPopulator;
import org.polydev.gaea.profiler.ProfileFuture;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.util.WorldUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class TreePopulator extends GaeaBlockPopulator {
@Override
public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) {
try(ProfileFuture ignored = TerraProfiler.fromWorld(world).measure("TreeGenTime")) {
TerraWorld tw = TerraWorld.getWorld(world);
if(!tw.isSafe()) return;
TerraBiomeGrid grid = tw.getGrid();;
int x = random.nextInt(16);
int z = random.nextInt(16);
int origX = chunk.getX() << 4;
int origZ = chunk.getZ() << 4;
Biome b = grid.getBiome(x+origX, z+origZ, GenerationPhase.POPULATE);
if(((UserDefinedDecorator) b.getDecorator()).getTreeChance() < random.nextInt(100)) return;
int max = 32;
int att = 0;
for(int i = 0; i < b.getDecorator().getTreeDensity() && att < max; ) {
att++;
for(Block block : getValidSpawnsAt(chunk, x, z, new Range(0, 254))) {
b = grid.getBiome(x+origX, z+origZ, GenerationPhase.POPULATE);
Tree tree = b.getDecorator().getTrees().get(random);
Range range = tw.getConfig().getBiome((UserDefinedBiome) b).getTreeRange(tree);
if(!range.isInRange(block.getY())) continue;
try {
if(tree.plant(block.getLocation(), random, false, Terra.getInstance())) i++;
} catch(NullPointerException ignore) {}
}
x = random.nextInt(16);
z = random.nextInt(16);
}
}
}
public List<Block> getValidSpawnsAt(Chunk chunk, int x, int z, Range check) {
List<Block> blocks = new ArrayList<>();
for(int y : check) {
if(chunk.getBlock(x, y, z).getType().isSolid() && chunk.getBlock(x, y + 1, z).getType().isAir()) {
blocks.add(chunk.getBlock(x, y, z));
}
}
return blocks;
}
}