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