mirror of
https://github.com/plexusorg/Plex-FAWE.git
synced 2025-07-10 04:38:35 +00:00
Merge pull request #569 from IntellectualSites/feature/better-noise
Feature/better noise
This commit is contained in:
@ -5,12 +5,29 @@ import com.boydti.fawe.util.MathMan;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
public class FastRandomCollection<T> extends RandomCollection<T> {
|
||||
private T[] values;
|
||||
private final T[] values;
|
||||
|
||||
public FastRandomCollection(Map<T, Double> weights, SimpleRandom random) {
|
||||
super(weights, random);
|
||||
private FastRandomCollection(T[] values, SimpleRandom random) {
|
||||
super(random);
|
||||
this.values = values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new FastRandomCollection if the given values and weights match the criteria.
|
||||
* The criteria may change at any point, so this method isn't guaranteed to return a
|
||||
* non-empty Optional in any case.
|
||||
*
|
||||
* @param weights the weight of the values.
|
||||
* @param random the random generator to use for this collection.
|
||||
* @param <T> the value type.
|
||||
* @return an {@link Optional} containing the new collection if it could
|
||||
* be created, {@link Optional#empty()} otherwise.
|
||||
* @see RandomCollection for API usage.
|
||||
*/
|
||||
public static <T> Optional<RandomCollection<T>> create(Map<T, Double> weights, SimpleRandom random) {
|
||||
int max = 0;
|
||||
int[] counts = new int[weights.size()];
|
||||
Double[] weightDoubles = weights.values().toArray(new Double[0]);
|
||||
@ -18,7 +35,7 @@ public class FastRandomCollection<T> extends RandomCollection<T> {
|
||||
int weight = (int) (weightDoubles[i] * 100);
|
||||
counts[i] = weight;
|
||||
if (weight != (weightDoubles[i] * 100)) {
|
||||
throw new IllegalArgumentException("Too small");
|
||||
return Optional.empty();
|
||||
}
|
||||
if (weight > max) {
|
||||
max = weight;
|
||||
@ -26,7 +43,7 @@ public class FastRandomCollection<T> extends RandomCollection<T> {
|
||||
}
|
||||
int gcd = MathMan.gcd(counts);
|
||||
if (max / gcd > 100000) {
|
||||
throw new IllegalArgumentException("Too large");
|
||||
return Optional.empty();
|
||||
}
|
||||
ArrayList<T> parsed = new ArrayList<>();
|
||||
for (Map.Entry<T, Double> entry : weights.entrySet()) {
|
||||
@ -35,11 +52,14 @@ public class FastRandomCollection<T> extends RandomCollection<T> {
|
||||
parsed.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
this.values = (T[]) parsed.toArray();
|
||||
@SuppressWarnings("unchecked")
|
||||
T[] values = (T[]) parsed.toArray();
|
||||
FastRandomCollection<T> fastRandomCollection = new FastRandomCollection<>(values, random);
|
||||
return Optional.of(fastRandomCollection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next(int x, int y, int z) {
|
||||
return values[random.nextInt(x, y, z, values.length)];
|
||||
return values[getRandom().nextInt(x, y, z, values.length)];
|
||||
}
|
||||
}
|
||||
|
@ -6,19 +6,33 @@ import java.util.Map;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* A RandomCollection holds multiple values that can be accessed by using
|
||||
* {@link RandomCollection#next(int, int, int)}. The returned value is
|
||||
* determined by a given {@link SimpleRandom} implementation.
|
||||
*
|
||||
* @param <T> the type of values the collection holds.
|
||||
*/
|
||||
public abstract class RandomCollection<T> {
|
||||
protected SimpleRandom random;
|
||||
private SimpleRandom random;
|
||||
|
||||
public RandomCollection(Map<T, Double> weights, SimpleRandom random) {
|
||||
protected RandomCollection(SimpleRandom random) {
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new RandomCollection. The implementation may differ depending on the
|
||||
* given arguments but there is no need to differ.
|
||||
*
|
||||
* @param weights the weighted map.
|
||||
* @param random the random number generator.
|
||||
* @param <T> the type the collection holds.
|
||||
* @return a RandomCollection using the given weights and the RNG.
|
||||
*/
|
||||
public static <T> RandomCollection<T> of(Map<T, Double> weights, SimpleRandom random) {
|
||||
try {
|
||||
return new FastRandomCollection<>(weights, random);
|
||||
} catch (IllegalArgumentException ignore) {
|
||||
return new SimpleRandomCollection<>(weights, random);
|
||||
}
|
||||
checkNotNull(random);
|
||||
return FastRandomCollection.create(weights, random)
|
||||
.orElse(new SimpleRandomCollection<>(weights, random));
|
||||
}
|
||||
|
||||
public void setRandom(SimpleRandom random) {
|
||||
|
@ -11,8 +11,16 @@ public class SimpleRandomCollection<E> extends RandomCollection<E> {
|
||||
private final NavigableMap<Double, E> map = new TreeMap<>();
|
||||
private double total = 0;
|
||||
|
||||
/**
|
||||
* Create a {@link RandomCollection} from a weighted map and a RNG.
|
||||
* It is recommended to use {@link RandomCollection#of(Map, SimpleRandom)}
|
||||
* instead of this constructor.
|
||||
*
|
||||
* @param weights the weighted map.
|
||||
* @param random the random number generator.
|
||||
*/
|
||||
public SimpleRandomCollection(Map<E, Double> weights, SimpleRandom random) {
|
||||
super(weights, random);
|
||||
super(random);
|
||||
for (Map.Entry<E, Double> entry : weights.entrySet()) {
|
||||
add(entry.getValue(), entry.getKey());
|
||||
}
|
||||
@ -24,7 +32,8 @@ public class SimpleRandomCollection<E> extends RandomCollection<E> {
|
||||
map.put(total, result);
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next(int x, int y, int z) {
|
||||
return map.ceilingEntry(random.nextDouble(x, y, z)).getValue();
|
||||
return map.ceilingEntry(getRandom().nextDouble(x, y, z)).getValue();
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,34 @@
|
||||
package com.boydti.fawe.object.random;
|
||||
|
||||
import com.sk89q.worldedit.math.Vector3;
|
||||
import com.sk89q.worldedit.math.noise.NoiseGenerator;
|
||||
|
||||
public class NoiseRandom implements SimpleRandom {
|
||||
|
||||
private final NoiseGenerator generator;
|
||||
private final double scale;
|
||||
|
||||
/**
|
||||
* Create a new NoiseRandom instance using a specific {@link NoiseGenerator} and a scale.
|
||||
*
|
||||
* @param generator The generator to use for the noise
|
||||
* @param scale The scale of the noise
|
||||
*/
|
||||
public NoiseRandom(NoiseGenerator generator, double scale) {
|
||||
this.generator = generator;
|
||||
this.scale = scale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double nextDouble(int x, int y, int z) {
|
||||
return cap(this.generator.noise(Vector3.at(x, y, z).multiply(this.scale)));
|
||||
}
|
||||
|
||||
// workaround for noise generators returning [0, 1]
|
||||
private double cap(double d) {
|
||||
if (d >= 1.0) {
|
||||
return 0x1.fffffffffffffp-1; // very close to 1 but less
|
||||
}
|
||||
return d;
|
||||
}
|
||||
}
|
@ -2,10 +2,29 @@ package com.boydti.fawe.object.random;
|
||||
|
||||
public interface SimpleRandom {
|
||||
|
||||
/**
|
||||
* Generate a random double from three integer components.
|
||||
* The generated value is between 0 (inclusive) and 1 (exclusive).
|
||||
*
|
||||
* @param x the first component
|
||||
* @param y the second component
|
||||
* @param z the third component
|
||||
* @return a double between 0 (inclusive) and 1 (exclusive)
|
||||
*/
|
||||
double nextDouble(int x, int y, int z);
|
||||
|
||||
default int nextInt(int x, int y, int z, int len) {
|
||||
/**
|
||||
* Generate a random integer from three integer components.
|
||||
* The generated value is between 0 (inclusive) and 1 (exclusive)
|
||||
*
|
||||
* @param x the first component
|
||||
* @param y the second component
|
||||
* @param z the third component
|
||||
* @param bound the upper bound (exclusive)
|
||||
* @return a random integer between 0 (inclusive) and {@code bound} (exclusive)
|
||||
*/
|
||||
default int nextInt(int x, int y, int z, int bound) {
|
||||
double val = nextDouble(x, y, z);
|
||||
return (int) (val * len);
|
||||
return (int) (val * bound);
|
||||
}
|
||||
}
|
||||
|
@ -1,14 +0,0 @@
|
||||
package com.boydti.fawe.object.random;
|
||||
|
||||
public class SimplexRandom implements SimpleRandom {
|
||||
private final double scale;
|
||||
|
||||
public SimplexRandom(double scale) {
|
||||
this.scale = scale;
|
||||
}
|
||||
|
||||
@Override
|
||||
public double nextDouble(int x, int y, int z) {
|
||||
return (SimplexNoise.noise(x * scale, y * scale, z * scale) + 1) * 0.5;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user