-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
RandomTransform.java
86 lines (71 loc) · 2.61 KB
/
RandomTransform.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.fastasyncworldedit.core.extent.transform;
import com.fastasyncworldedit.core.extent.ResettableExtent;
import com.fastasyncworldedit.core.math.random.SimpleRandom;
import com.fastasyncworldedit.core.math.random.TrueRandom;
import com.fastasyncworldedit.core.util.collection.RandomCollection;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Uses a random pattern of a weighted list of patterns.
*/
public class RandomTransform extends SelectTransform {
private final SimpleRandom random;
private final List<RandomCollection.Weighted<ResettableExtent>> weights;
private transient RandomCollection<ResettableExtent> collection;
public RandomTransform() {
this(new TrueRandom());
}
/**
* New instance
*
* @param random {@link SimpleRandom} used to choose between transforms, given weights
*/
public RandomTransform(SimpleRandom random) {
this.random = random;
this.weights = new ArrayList<>();
}
@Override
public AbstractDelegateExtent getExtent(int x, int y, int z) {
return collection.next(this.random, x, y, z);
}
@Override
public AbstractDelegateExtent getExtent(int x, int z) {
return collection.next(this.random, x, 0, z);
}
@Override
public ResettableExtent setExtent(Extent extent) {
if (collection == null) {
collection = RandomCollection.of(weights);
}
super.setExtent(extent);
for (RandomCollection.Weighted<ResettableExtent> current : this.weights) {
current.value().setExtent(extent);
}
return this;
}
/**
* Add a pattern to the weight list of patterns.
* <p>
* <p>The probability for the pattern added is chance / max where max is
* the sum of the probabilities of all added patterns.</p>
*
* @param extent the extent
* @param chance the chance, which can be any positive number
*/
public void add(ResettableExtent extent, double chance) {
checkNotNull(extent);
weights.add(new RandomCollection.Weighted<>(extent, chance));
collection = RandomCollection.of(weights);
}
public Set<ResettableExtent> getExtents() {
return this.weights.stream().map(RandomCollection.Weighted::value).collect(Collectors.toSet());
}
public RandomCollection<ResettableExtent> getCollection() {
return collection;
}
}