Biome API (WIP), javadocs, biome map

This commit is contained in:
paulevsGitch 2021-05-23 09:58:27 +03:00
parent c89235ec92
commit 67c9c2302d
9 changed files with 424 additions and 38 deletions

View file

@ -2,8 +2,10 @@ package ru.bclib.util;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
@ -11,6 +13,15 @@ import net.minecraft.world.level.block.Block;
public class BonemealUtil {
private static final Map<ResourceLocation, Map<Block, WeightedList<Block>>> GRASS_BIOMES = Maps.newHashMap();
private static final Map<Block, WeightedList<Block>> GRASS_TYPES = Maps.newHashMap();
private static final Set<Block> SPREADABLE_BLOCKS = Sets.newHashSet();
public static void addSpreadableBlock(Block block) {
SPREADABLE_BLOCKS.add(block);
}
public static boolean isSpreadable(Block block) {
return SPREADABLE_BLOCKS.contains(block);
}
public static void addBonemealGrass(Block terrain, Block plant) {
addBonemealGrass(terrain, plant, 1F);

View file

@ -0,0 +1,74 @@
package ru.bclib.util;
import java.util.Random;
public class WeighTree<T> {
private final float maxWeight;
private final Node root;
public WeighTree(WeightedList<T> list) {
maxWeight = list.getMaxWeight();
root = getNode(list);
}
/**
* Get eandom value from tree.
* @param random - {@link Random}.
* @return {@link T} value.
*/
public T get(Random random) {
return root.get(random.nextFloat() * maxWeight);
}
private Node getNode(WeightedList<T> biomes) {
int size = biomes.size();
if (size == 1) {
return new Leaf(biomes.get(0));
}
else if (size == 2) {
T first = biomes.get(0);
return new Branch(biomes.getWeight(0), new Leaf(first), new Leaf(biomes.get(1)));
}
else {
int index = size >> 1;
float separator = biomes.getWeight(index);
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 T get(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
T get(float value) {
return value < separator ? min.get(value) : max.get(value);
}
}
private class Leaf extends Node {
final T biome;
Leaf(T biome) {
this.biome = biome;
}
@Override
T get(float value) {
return biome;
}
}
}

View file

@ -3,18 +3,29 @@ package ru.bclib.util;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;
public class WeightedList<T> {
private final List<Float> weights = new ArrayList<Float>();
private final List<T> values = new ArrayList<T>();
private float maxWeight;
/**
* Adds value with specified weight to the list
* @param value
* @param weight
*/
public void add(T value, float weight) {
maxWeight += weight;
weights.add(maxWeight);
values.add(value);
}
/**
* Get random value.
* @param random - {@link Random}.
* @return {@link T} value.
*/
public T get(Random random) {
if (maxWeight < 1) {
return null;
@ -27,8 +38,79 @@ public class WeightedList<T> {
}
return null;
}
/**
* Get value by index.
* @param index - {@code int} index.
* @return {@link T} value.
*/
public T get(int index) {
return values.get(index);
}
/**
* Get value weight. Weight is summed with all previous values weights.
* @param index - {@code int} index.
* @return {@code float} weight.
*/
public float getWeight(int index) {
return weights.get(index);
}
/**
* Chech if the list is empty.
* @return {@code true} if list is empty and {@code false} if not.
*/
public boolean isEmpty() {
return maxWeight == 0;
}
/**
* Get the list size.
* @return {@code int} list size.
*/
public int size() {
return values.size();
}
/**
* Makes a sublist of this list with same weights. Used only in {@link WeighTree}
* @param start - {@code int} start index (inclusive).
* @param end - {@code int} end index (exclusive).
* @return {@link WeightedList<T>}.
*/
protected WeightedList<T> subList(int start, int end) {
WeightedList<T> list = new WeightedList<T>();
for (int i = start; i < end; i++) {
list.weights.add(weights.get(i));
list.values.add(values.get(i));
}
list.maxWeight = list.weights.get(end - 1);
return list;
}
/**
* Check if list contains certain value.
* @param value - {@link T} value.
* @return {@code true} if value is in list and {@code false} if not.
*/
public boolean contains(T value) {
return values.contains(value);
}
/**
* Applies {@link Consumer} to all values in list.
* @param function - {@link Consumer}.
*/
public void forEach(Consumer<T> function) {
values.forEach(function);
}
/**
* Get the maximum weight of the tree.
* @return {@code float} maximum weight.
*/
public float getMaxWeight() {
return maxWeight;
}
}