Weight tree & cleanup

This commit is contained in:
paulevsGitch 2020-11-19 17:37:41 +03:00
parent aec1216769
commit 2093c747c6
8 changed files with 83 additions and 16 deletions

View file

@ -8,7 +8,6 @@ import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import net.minecraft.util.Identifier;
import ru.betterend.registry.EndBiomes;
import ru.betterend.world.biome.EndBiome;
public class BiomePicker {
@ -17,6 +16,7 @@ public class BiomePicker {
private float maxChanceUnmutable = 0;
private float maxChance = 0;
private int biomeCount = 0;
private WeighTree tree;
public void addBiome(EndBiome biome) {
maxChance = biome.mutateGenChance(maxChance);
@ -38,11 +38,7 @@ public class BiomePicker {
}
public EndBiome getBiome(Random random) {
float chance = random.nextFloat() * maxChance;
for (EndBiome biome: biomes)
if (biome.canGenerate(chance))
return biome;
return EndBiomes.END;
return tree.getBiome(random.nextFloat() * maxChance);
}
public List<EndBiome> getBiomes() {
@ -52,4 +48,8 @@ public class BiomePicker {
public boolean containsImmutable(Identifier id) {
return immutableIDs.contains(id);
}
public void rebuild() {
tree = new WeighTree(biomes);
}
}

View file

@ -0,0 +1,69 @@
package ru.betterend.world.generator;
import java.util.List;
import ru.betterend.world.biome.EndBiome;
public class WeighTree {
private final Node root;
public WeighTree(List<EndBiome> biomes) {
root = getNode(biomes);
}
public EndBiome getBiome(float value) {
return root.getBiome(value);
}
private Node getNode(List<EndBiome> biomes) {
int size = biomes.size();
if (size == 1) {
return new Leaf(biomes.get(0));
}
else if (size == 2) {
EndBiome first = biomes.get(0);
return new Branch(first.getGenChance(), new Leaf(first), new Leaf(biomes.get(1)));
}
else {
int index = size >> 1;
float separator = biomes.get(index).getGenChance();
Node a = getNode(biomes.subList(0, index + 1));
Node b = getNode(biomes.subList(index, size));
return new Branch(separator, a, b);
}
}
private abstract class Node {
abstract EndBiome getBiome(float value);
}
private class Branch extends Node {
final float separator;
final Node min;
final Node max;
public Branch(float separator, Node min, Node max) {
this.separator = separator;
this.min = min;
this.max = max;
}
@Override
EndBiome getBiome(float value) {
return value < separator ? min.getBiome(value) : max.getBiome(value);
}
}
private class Leaf extends Node {
final EndBiome biome;
Leaf(EndBiome biome) {
this.biome = biome;
}
@Override
EndBiome getBiome(float value) {
return biome;
}
}
}