Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.util.noise;
2
3import org.bukkit.World;
4
5import java.util.Random;
6
7/**
8 * Creates simplex noise through unbiased octaves
9 */
10public class SimplexOctaveGenerator extends OctaveGenerator {
11 private double wScale = 1;
12
13 /**
14 * Creates a simplex octave generator for the given world
15 *
16 * @param world World to construct this generator for
17 * @param octaves Amount of octaves to create
18 */
19 public SimplexOctaveGenerator(World world, int octaves) {
20 this(new Random(world.getSeed()), octaves);
21 }
22
23 /**
24 * Creates a simplex octave generator for the given world
25 *
26 * @param seed Seed to construct this generator for
27 * @param octaves Amount of octaves to create
28 */
29 public SimplexOctaveGenerator(long seed, int octaves) {
30 this(new Random(seed), octaves);
31 }
32
33 /**
34 * Creates a simplex octave generator for the given {@link Random}
35 *
36 * @param rand Random object to construct this generator for
37 * @param octaves Amount of octaves to create
38 */
39 public SimplexOctaveGenerator(Random rand, int octaves) {
40 super(createOctaves(rand, octaves));
41 }
42
43 @Override
44 public void setScale(double scale) {
45 super.setScale(scale);
46 setWScale(scale);
47 }
48
49 /**
50 * Gets the scale used for each W-coordinates passed
51 *
52 * @return W scale
53 */
54 public double getWScale() {
55 return wScale;
56 }
57
58 /**
59 * Sets the scale used for each W-coordinates passed
60 *
61 * @param scale New W scale
62 */
63 public void setWScale(double scale) {
64 wScale = scale;
65 }
66
67 /**
68 * Generates noise for the 3D coordinates using the specified number of octaves and parameters
69 *
70 * @param x X-coordinate
71 * @param y Y-coordinate
72 * @param z Z-coordinate
73 * @param frequency How much to alter the frequency by each octave
74 * @param amplitude How much to alter the amplitude by each octave
75 * @return Resulting noise
76 * @para, w W-coordinate
77 */
78 public double noise(double x, double y, double z, double w, double frequency, double amplitude) {
79 return noise(x, y, z, w, frequency, amplitude, false);
80 }
81
82 /**
83 * Generates noise for the 3D coordinates using the specified number of octaves and parameters
84 *
85 * @param x X-coordinate
86 * @param y Y-coordinate
87 * @param z Z-coordinate
88 * @param frequency How much to alter the frequency by each octave
89 * @param amplitude How much to alter the amplitude by each octave
90 * @param normalized If true, normalize the value to [-1, 1]
91 * @return Resulting noise
92 * @para, w W-coordinate
93 */
94 public double noise(double x, double y, double z, double w, double frequency, double amplitude, boolean normalized) {
95 double result = 0;
96 double amp = 1;
97 double freq = 1;
98 double max = 0;
99
100 x *= xScale;
101 y *= yScale;
102 z *= zScale;
103 w *= wScale;
104
105 for (int i = 0; i < octaves.length; i++) {
106 result += ((SimplexNoiseGenerator) octaves[i]).noise(x * freq, y * freq, z * freq, w * freq) * amp;
107 max += amp;
108 freq *= frequency;
109 amp *= amplitude;
110 }
111
112 if (normalized) {
113 result /= max;
114 }
115
116 return result;
117 }
118
119 private static NoiseGenerator[] createOctaves(Random rand, int octaves) {
120 NoiseGenerator[] result = new NoiseGenerator[octaves];
121
122 for (int i = 0; i < octaves; i++) {
123 result[i] = new SimplexNoiseGenerator(rand);
124 }
125
126 return result;
127 }
128}