Skip to content

Commit

Permalink
chore: distribution utilities were moved
Browse files Browse the repository at this point in the history
  • Loading branch information
naalit committed Aug 16, 2021
1 parent cd46d7e commit efbf6d8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import org.terasology.gestalt.naming.Name;
import org.terasology.gf.PlantRegistry;
import org.terasology.gf.PlantType;
import org.terasology.utilities.procedural.ChanceRandomizer;
import org.terasology.engine.utilities.random.DiscreteDistribution;

import java.util.Comparator;
import java.util.HashMap;
Expand All @@ -31,15 +31,15 @@ public class FloraFeatureGenerator implements WorldRasterizer {

private Multimap<Name, PlantSpawnDefinition> treeDefinitions = TreeMultimap.create(Ordering.natural(),
Comparator.comparing(PlantSpawnDefinition::getPlantId));
private Map<Name, ChanceRandomizer<PlantSpawnDefinition>> treeDefinitionsCache = new HashMap<>();
private Map<Name, DiscreteDistribution<PlantSpawnDefinition>> treeDefinitionsCache = new HashMap<>();

private Multimap<Name, PlantSpawnDefinition> bushDefinitions = TreeMultimap.create(Ordering.natural(),
Comparator.comparing(PlantSpawnDefinition::getPlantId));
private Map<Name, ChanceRandomizer<PlantSpawnDefinition>> bushDefinitionsCache = new HashMap<>();
private Map<Name, DiscreteDistribution<PlantSpawnDefinition>> bushDefinitionsCache = new HashMap<>();

private Multimap<Name, PlantSpawnDefinition> foliageDefinitions = TreeMultimap.create(Ordering.natural(),
Comparator.comparing(PlantSpawnDefinition::getPlantId));
private Map<Name, ChanceRandomizer<PlantSpawnDefinition>> foliageDefinitionsCache = new HashMap<>();
private Map<Name, DiscreteDistribution<PlantSpawnDefinition>> foliageDefinitionsCache = new HashMap<>();

public FloraFeatureGenerator() {
loadPlantGrowthDefinitions();
Expand Down Expand Up @@ -95,8 +95,8 @@ public void generateChunk(Chunk chunk, Region chunkRegion) {

long seed = Float.floatToRawIntBits(value);
Random random = new FastRandom(seed);
ChanceRandomizer<PlantSpawnDefinition> definitionsForBiome = getDefinitionsForBiome(biome, treeDefinitionsCache, treeDefinitions);
PlantSpawnDefinition treeDefinition = definitionsForBiome.randomizeObject(random);
DiscreteDistribution<PlantSpawnDefinition> definitionsForBiome = getDefinitionsForBiome(biome, treeDefinitionsCache, treeDefinitions);
PlantSpawnDefinition treeDefinition = definitionsForBiome.sample(random);
if (treeDefinition != null && random.nextFloat() < treeDefinition.getProbability()) {
treeDefinition.generatePlant(seed, chunk, position.x(), position.y(), position.z(), chunkRegion);
}
Expand All @@ -114,8 +114,8 @@ public void generateChunk(Chunk chunk, Region chunkRegion) {

long seed = Float.floatToRawIntBits(value);
Random random = new FastRandom(seed);
ChanceRandomizer<PlantSpawnDefinition> definitionsForBiome = getDefinitionsForBiome(biome, bushDefinitionsCache, bushDefinitions);
PlantSpawnDefinition bushDefinition = definitionsForBiome.randomizeObject(random);
DiscreteDistribution<PlantSpawnDefinition> definitionsForBiome = getDefinitionsForBiome(biome, bushDefinitionsCache, bushDefinitions);
PlantSpawnDefinition bushDefinition = definitionsForBiome.sample(random);
if (bushDefinition != null && random.nextFloat() < bushDefinition.getProbability()) {
bushDefinition.generatePlant(seed, chunk, position.x(), position.y(), position.z(), chunkRegion);
}
Expand All @@ -134,8 +134,8 @@ public void generateChunk(Chunk chunk, Region chunkRegion) {

long seed = Float.floatToRawIntBits(value);
Random random = new FastRandom(seed);
ChanceRandomizer<PlantSpawnDefinition> definitionsForBiome = getDefinitionsForBiome(biome, foliageDefinitionsCache, foliageDefinitions);
PlantSpawnDefinition foliageDefinition = definitionsForBiome.randomizeObject(random);
DiscreteDistribution<PlantSpawnDefinition> definitionsForBiome = getDefinitionsForBiome(biome, foliageDefinitionsCache, foliageDefinitions);
PlantSpawnDefinition foliageDefinition = definitionsForBiome.sample(random);
if (foliageDefinition != null && random.nextFloat() < foliageDefinition.getProbability()) {
foliageDefinition.generatePlant(seed, chunk, position.x(), position.y(), position.z(), chunkRegion);
}
Expand All @@ -145,18 +145,17 @@ public void generateChunk(Chunk chunk, Region chunkRegion) {
}
}

private ChanceRandomizer<PlantSpawnDefinition> getDefinitionsForBiome(Biome biome, Map<Name,
ChanceRandomizer<PlantSpawnDefinition>> cache, Multimap<Name, PlantSpawnDefinition> definitions) {
ChanceRandomizer<PlantSpawnDefinition> result = cache.get(biome.getId());
private DiscreteDistribution<PlantSpawnDefinition> getDefinitionsForBiome(Biome biome, Map<Name,
DiscreteDistribution<PlantSpawnDefinition>> cache, Multimap<Name, PlantSpawnDefinition> definitions) {
DiscreteDistribution<PlantSpawnDefinition> result = cache.get(biome.getId());
if (result != null) {
return result;
}

result = new ChanceRandomizer<>(100);
result = new DiscreteDistribution<>();
for (PlantSpawnDefinition floraDefinition : definitions.get(biome.getId())) {
result.addChance(floraDefinition.getRarity(), floraDefinition);
result.add(floraDefinition, floraDefinition.getRarity());
}
result.initialize();
cache.put(biome.getId(), result);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.terasology.engine.world.chunks.Chunks;
import org.terasology.gf.LivingPlantComponent;
import org.terasology.gestalt.naming.Name;
import org.terasology.utilities.procedural.PDist;
import org.terasology.engine.utilities.random.PDist;

import java.util.Collection;
import java.util.Collections;
Expand Down

0 comments on commit efbf6d8

Please sign in to comment.