3
0
Mirror von https://github.com/IntellectualSites/FastAsyncWorldEdit.git synchronisiert 2024-11-17 00:20:09 +01:00

Allow repeated keys in RandomCollection (#2624)

Dieser Commit ist enthalten in:
Hannes Greule 2024-03-16 20:12:21 +01:00 committet von dordsor21
Ursprung 0e88bb438b
Commit 986d351519
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 1E53E88969FFCF0B
5 geänderte Dateien mit 71 neuen und 102 gelöschten Zeilen

Datei anzeigen

@ -7,10 +7,10 @@ import com.fastasyncworldedit.core.util.collection.RandomCollection;
import com.sk89q.worldedit.extent.AbstractDelegateExtent; import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent; import com.sk89q.worldedit.extent.Extent;
import java.util.HashMap; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -20,10 +20,9 @@ import static com.google.common.base.Preconditions.checkNotNull;
public class RandomTransform extends SelectTransform { public class RandomTransform extends SelectTransform {
private final SimpleRandom random; private final SimpleRandom random;
private final Map<ResettableExtent, Double> weights = new HashMap<>(); private final List<RandomCollection.Weighted<ResettableExtent>> weights;
private transient RandomCollection<ResettableExtent> collection; private transient RandomCollection<ResettableExtent> collection;
private transient LinkedHashSet<ResettableExtent> extents = new LinkedHashSet<>();
public RandomTransform() { public RandomTransform() {
this(new TrueRandom()); this(new TrueRandom());
@ -36,27 +35,27 @@ public class RandomTransform extends SelectTransform {
*/ */
public RandomTransform(SimpleRandom random) { public RandomTransform(SimpleRandom random) {
this.random = random; this.random = random;
this.weights = new ArrayList<>();
} }
@Override @Override
public AbstractDelegateExtent getExtent(int x, int y, int z) { public AbstractDelegateExtent getExtent(int x, int y, int z) {
return collection.next(x, y, z); return collection.next(this.random, x, y, z);
} }
@Override @Override
public AbstractDelegateExtent getExtent(int x, int z) { public AbstractDelegateExtent getExtent(int x, int z) {
return collection.next(x, 0, z); return collection.next(this.random, x, 0, z);
} }
@Override @Override
public ResettableExtent setExtent(Extent extent) { public ResettableExtent setExtent(Extent extent) {
if (collection == null) { if (collection == null) {
collection = RandomCollection.of(weights, random); collection = RandomCollection.of(weights);
extents = new LinkedHashSet<>(weights.keySet());
} }
super.setExtent(extent); super.setExtent(extent);
for (ResettableExtent current : extents) { for (RandomCollection.Weighted<ResettableExtent> current : this.weights) {
current.setExtent(extent); current.value().setExtent(extent);
} }
return this; return this;
} }
@ -72,13 +71,12 @@ public class RandomTransform extends SelectTransform {
*/ */
public void add(ResettableExtent extent, double chance) { public void add(ResettableExtent extent, double chance) {
checkNotNull(extent); checkNotNull(extent);
weights.put(extent, chance); weights.add(new RandomCollection.Weighted<>(extent, chance));
collection = RandomCollection.of(weights, random); collection = RandomCollection.of(weights);
this.extents.add(extent);
} }
public Set<ResettableExtent> getExtents() { public Set<ResettableExtent> getExtents() {
return extents; return this.weights.stream().map(RandomCollection.Weighted::value).collect(Collectors.toSet());
} }
public RandomCollection<ResettableExtent> getCollection() { public RandomCollection<ResettableExtent> getCollection() {

Datei anzeigen

@ -4,15 +4,14 @@ import com.fastasyncworldedit.core.math.random.SimpleRandom;
import com.fastasyncworldedit.core.util.MathMan; import com.fastasyncworldedit.core.util.MathMan;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Map; import java.util.List;
import java.util.Optional; import java.util.Optional;
public class FastRandomCollection<T> extends RandomCollection<T> { public final class FastRandomCollection<T> implements RandomCollection<T> {
private final T[] values; private final T[] values;
private FastRandomCollection(T[] values, SimpleRandom random) { private FastRandomCollection(T[] values) {
super(random);
this.values = values; this.values = values;
} }
@ -22,16 +21,15 @@ public class FastRandomCollection<T> extends RandomCollection<T> {
* {@code Optional} in any case. * {@code Optional} in any case.
* *
* @param weights the weight of the values. * @param weights the weight of the values.
* @param random the random generator to use for this collection.
* @param <T> the value type. * @param <T> the value type.
* @return an {@link Optional} containing the new collection if it could be created, {@link * @return an {@link Optional} containing the new collection if it could be created, {@link
* Optional#empty()} otherwise. * Optional#empty()} otherwise.
* @see RandomCollection for API usage. * @see RandomCollection for API usage.
*/ */
public static <T> Optional<RandomCollection<T>> create(Map<T, Double> weights, SimpleRandom random) { public static <T> Optional<RandomCollection<T>> create(List<Weighted<T>> weights) {
int max = 0; int max = 0;
int[] counts = new int[weights.size()]; int[] counts = new int[weights.size()];
Double[] weightDoubles = weights.values().toArray(new Double[0]); double[] weightDoubles = weights.stream().mapToDouble(Weighted::weight).toArray();
for (int i = 0; i < weightDoubles.length; i++) { for (int i = 0; i < weightDoubles.length; i++) {
int weight = (int) (weightDoubles[i] * 100); int weight = (int) (weightDoubles[i] * 100);
counts[i] = weight; counts[i] = weight;
@ -47,21 +45,21 @@ public class FastRandomCollection<T> extends RandomCollection<T> {
return Optional.empty(); return Optional.empty();
} }
ArrayList<T> parsed = new ArrayList<>(); ArrayList<T> parsed = new ArrayList<>();
for (Map.Entry<T, Double> entry : weights.entrySet()) { for (Weighted<T> entry : weights) {
int num = (int) (100 * entry.getValue()); int num = (int) (100 * entry.weight());
for (int j = 0; j < num / gcd; j++) { for (int j = 0; j < num / gcd; j++) {
parsed.add(entry.getKey()); parsed.add(entry.value());
} }
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T[] values = (T[]) parsed.toArray(); T[] values = (T[]) parsed.toArray();
FastRandomCollection<T> fastRandomCollection = new FastRandomCollection<>(values, random); FastRandomCollection<T> fastRandomCollection = new FastRandomCollection<>(values);
return Optional.of(fastRandomCollection); return Optional.of(fastRandomCollection);
} }
@Override @Override
public T next(int x, int y, int z) { public T next(final SimpleRandom random, int x, int y, int z) {
return values[getRandom().nextInt(x, y, z, values.length)]; return values[random.nextInt(x, y, z, values.length)];
} }
} }

Datei anzeigen

@ -2,49 +2,35 @@ package com.fastasyncworldedit.core.util.collection;
import com.fastasyncworldedit.core.math.random.SimpleRandom; import com.fastasyncworldedit.core.math.random.SimpleRandom;
import java.util.Map; import java.util.List;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
/** /**
* A RandomCollection holds multiple values that can be accessed by using * A RandomCollection holds multiple values that can be accessed by using
* {@link RandomCollection#next(int, int, int)}. The returned value is * {@link RandomCollection#next(SimpleRandom, int, int, int)}. The returned value is
* determined by a given {@link SimpleRandom} implementation. * determined by a given {@link SimpleRandom} implementation.
* *
* @param <T> the type of values the collection holds. * @param <T> the type of values the collection holds.
*/ */
public abstract class RandomCollection<T> { public sealed interface RandomCollection<T> permits FastRandomCollection, SimpleRandomCollection {
private SimpleRandom random;
protected RandomCollection(SimpleRandom random) {
this.random = random;
}
/** /**
* Return a new RandomCollection. The implementation may differ depending on the * Return a new RandomCollection. The implementation may differ depending on the
* given arguments but there is no need to differ. * given arguments but there is no need to differ.
* *
* @param weights the weighted map. * @param weights the weighted map.
* @param random the random number generator.
* @param <T> the type the collection holds. * @param <T> the type the collection holds.
* @return a RandomCollection using the given weights and the RNG. * @return a RandomCollection using the given weights and the RNG.
*/ */
public static <T> RandomCollection<T> of(Map<T, Double> weights, SimpleRandom random) { static <T> RandomCollection<T> of(List<Weighted<T>> weights) {
checkNotNull(random); return FastRandomCollection.create(weights)
return FastRandomCollection.create(weights, random) .orElseGet(() -> new SimpleRandomCollection<>(weights));
.orElseGet(() -> new SimpleRandomCollection<>(weights, random));
} }
public void setRandom(SimpleRandom random) { T next(SimpleRandom random, int x, int y, int z);
checkNotNull(random);
this.random = random; record Weighted<T>(T value, double weight) {
} }
public SimpleRandom getRandom() {
return random;
}
public abstract T next(int x, int y, int z);
} }

Datei anzeigen

@ -2,41 +2,39 @@ package com.fastasyncworldedit.core.util.collection;
import com.fastasyncworldedit.core.math.random.SimpleRandom; import com.fastasyncworldedit.core.math.random.SimpleRandom;
import java.util.Map; import java.util.List;
import java.util.NavigableMap; import java.util.NavigableMap;
import java.util.TreeMap; import java.util.TreeMap;
public class SimpleRandomCollection<E> extends RandomCollection<E> { public final class SimpleRandomCollection<T> implements RandomCollection<T> {
private final NavigableMap<Double, E> map = new TreeMap<>(); private final NavigableMap<Double, T> map;
private double total = 0; private final double total;
/** /**
* Create a {@link RandomCollection} from a weighted map and a RNG. * Create a {@link RandomCollection} from a weighted map and a RNG.
* It is recommended to use {@link RandomCollection#of(Map, SimpleRandom)} * It is recommended to use {@link RandomCollection#of(List)}
* instead of this constructor. * instead of this constructor.
* *
* @param weights the weighted map. * @param weights the weighted map.
* @param random the random number generator.
*/ */
public SimpleRandomCollection(Map<E, Double> weights, SimpleRandom random) { public SimpleRandomCollection(List<Weighted<T>> weights) {
super(random); this.map = new TreeMap<>();
for (Map.Entry<E, Double> entry : weights.entrySet()) { double total = 0;
add(entry.getValue(), entry.getKey()); for (Weighted<T> entry : weights) {
final double weight = entry.weight();
if (weight <= 0) {
throw new IllegalArgumentException("Weights must be positive");
}
total += weight;
this.map.put(total, entry.value());
} }
} this.total = total;
public void add(double weight, E result) {
if (weight <= 0) {
return;
}
total += weight;
map.put(total, result);
} }
@Override @Override
public E next(int x, int y, int z) { public T next(final SimpleRandom random, int x, int y, int z) {
return map.ceilingEntry(getRandom().nextDouble(x, y, z, this.total)).getValue(); return map.ceilingEntry(random.nextDouble(x, y, z, this.total)).getValue();
} }
} }

Datei anzeigen

@ -27,10 +27,10 @@ import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.math.BlockVector3; import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.world.block.BaseBlock; import com.sk89q.worldedit.world.block.BaseBlock;
import java.util.LinkedHashMap; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
@ -39,11 +39,10 @@ import static com.google.common.base.Preconditions.checkNotNull;
*/ */
public class RandomPattern extends AbstractPattern { public class RandomPattern extends AbstractPattern {
//FAWE start - SimpleRandom > Random, LHS<P> > List //FAWE start - SimpleRandom > Random, RandomCollection
private final SimpleRandom random; private final SimpleRandom random;
private Map<Pattern, Double> weights = new LinkedHashMap<>(); private final List<RandomCollection.Weighted<Pattern>> weights;
private RandomCollection<Pattern> collection; private RandomCollection<Pattern> collection;
private LinkedHashSet<Pattern> patterns = new LinkedHashSet<>();
//FAWE end //FAWE end
//FAWE start //FAWE start
@ -53,6 +52,7 @@ public class RandomPattern extends AbstractPattern {
public RandomPattern(SimpleRandom random) { public RandomPattern(SimpleRandom random) {
this.random = random; this.random = random;
this.weights = new ArrayList<>();
} }
/** /**
@ -63,16 +63,10 @@ public class RandomPattern extends AbstractPattern {
*/ */
public RandomPattern(SimpleRandom random, RandomPattern parent) { public RandomPattern(SimpleRandom random, RandomPattern parent) {
this.random = random; this.random = random;
this.weights = parent.weights; this.weights = parent.weights.stream()
this.collection = RandomCollection.of(weights, random); .map(weighted -> new RandomCollection.Weighted<>(weighted.value().fork(), weighted.weight()))
this.patterns = parent.patterns; .collect(Collectors.toCollection(ArrayList::new));
} this.collection = RandomCollection.of(weights);
private RandomPattern(SimpleRandom random, Map<Pattern, Double> weights) {
this.random = random;
this.weights = weights;
this.collection = RandomCollection.of(weights, random);
this.patterns = new LinkedHashSet<>(weights.keySet());
} }
//FAWE end //FAWE end
@ -87,18 +81,15 @@ public class RandomPattern extends AbstractPattern {
*/ */
public void add(Pattern pattern, double chance) { public void add(Pattern pattern, double chance) {
checkNotNull(pattern); checkNotNull(pattern);
//FAWE start - Double, weights, patterns and collection //FAWE start - Double, weights, repeating patterns, and collection
Double existingWeight = weights.get(pattern); this.weights.add(new RandomCollection.Weighted<>(pattern, chance));
if (existingWeight != null) { this.collection = RandomCollection.of(weights);
chance += existingWeight;
}
weights.put(pattern, chance);
collection = RandomCollection.of(weights, random);
this.patterns.add(pattern);
} }
public Set<Pattern> getPatterns() { public Set<Pattern> getPatterns() {
return patterns; return this.weights.stream()
.map(RandomCollection.Weighted::value)
.collect(Collectors.toSet());
} }
public RandomCollection<Pattern> getCollection() { public RandomCollection<Pattern> getCollection() {
@ -107,19 +98,17 @@ public class RandomPattern extends AbstractPattern {
@Override @Override
public BaseBlock applyBlock(BlockVector3 position) { public BaseBlock applyBlock(BlockVector3 position) {
return collection.next(position.x(), position.y(), position.z()).applyBlock(position); return collection.next(this.random, position.x(), position.y(), position.z()).applyBlock(position);
} }
@Override @Override
public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException { public boolean apply(Extent extent, BlockVector3 get, BlockVector3 set) throws WorldEditException {
return collection.next(get.x(), get.y(), get.z()).apply(extent, get, set); return collection.next(this.random, get.x(), get.y(), get.z()).apply(extent, get, set);
} }
@Override @Override
public Pattern fork() { public Pattern fork() {
final LinkedHashMap<Pattern, Double> newWeights = new LinkedHashMap<>(); return new RandomPattern(this.random, this);
this.weights.forEach((p, w) -> newWeights.put(p.fork(), w));
return new RandomPattern(this.random, newWeights);
} }
//FAWE end //FAWE end