Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 192 lines 5.8 kB view raw
1package org.bukkit.util.noise; 2 3/** 4 * Creates noise using unbiased octaves 5 */ 6public abstract class OctaveGenerator { 7 protected final NoiseGenerator[] octaves; 8 protected double xScale = 1; 9 protected double yScale = 1; 10 protected double zScale = 1; 11 12 protected OctaveGenerator(NoiseGenerator[] octaves) { 13 this.octaves = octaves; 14 } 15 16 /** 17 * Sets the scale used for all coordinates passed to this generator. 18 * <p> 19 * This is the equivalent to setting each coordinate to the specified value. 20 * 21 * @param scale New value to scale each coordinate by 22 */ 23 public void setScale(double scale) { 24 setXScale(scale); 25 setYScale(scale); 26 setZScale(scale); 27 } 28 29 /** 30 * Gets the scale used for each X-coordinates passed 31 * 32 * @return X scale 33 */ 34 public double getXScale() { 35 return xScale; 36 } 37 38 /** 39 * Sets the scale used for each X-coordinates passed 40 * 41 * @param scale New X scale 42 */ 43 public void setXScale(double scale) { 44 xScale = scale; 45 } 46 47 /** 48 * Gets the scale used for each Y-coordinates passed 49 * 50 * @return Y scale 51 */ 52 public double getYScale() { 53 return yScale; 54 } 55 56 /** 57 * Sets the scale used for each Y-coordinates passed 58 * 59 * @param scale New Y scale 60 */ 61 public void setYScale(double scale) { 62 yScale = scale; 63 } 64 65 /** 66 * Gets the scale used for each Z-coordinates passed 67 * 68 * @return Z scale 69 */ 70 public double getZScale() { 71 return zScale; 72 } 73 74 /** 75 * Sets the scale used for each Z-coordinates passed 76 * 77 * @param scale New Z scale 78 */ 79 public void setZScale(double scale) { 80 zScale = scale; 81 } 82 83 /** 84 * Gets a clone of the individual octaves used within this generator 85 * 86 * @return Clone of the individual octaves 87 */ 88 public NoiseGenerator[] getOctaves() { 89 return octaves.clone(); 90 } 91 92 /** 93 * Generates noise for the 1D coordinates using the specified number of octaves and parameters 94 * 95 * @param x X-coordinate 96 * @param frequency How much to alter the frequency by each octave 97 * @param amplitude How much to alter the amplitude by each octave 98 * @return Resulting noise 99 */ 100 public double noise(double x, double frequency, double amplitude) { 101 return noise(x, 0, 0, frequency, amplitude); 102 } 103 104 /** 105 * Generates noise for the 1D coordinates using the specified number of octaves and parameters 106 * 107 * @param x X-coordinate 108 * @param frequency How much to alter the frequency by each octave 109 * @param amplitude How much to alter the amplitude by each octave 110 * @param normalized If true, normalize the value to [-1, 1] 111 * @return Resulting noise 112 */ 113 public double noise(double x, double frequency, double amplitude, boolean normalized) { 114 return noise(x, 0, 0, frequency, amplitude, normalized); 115 } 116 117 /** 118 * Generates noise for the 2D coordinates using the specified number of octaves and parameters 119 * 120 * @param x X-coordinate 121 * @param y Y-coordinate 122 * @param frequency How much to alter the frequency by each octave 123 * @param amplitude How much to alter the amplitude by each octave 124 * @return Resulting noise 125 */ 126 public double noise(double x, double y, double frequency, double amplitude) { 127 return noise(x, y, 0, frequency, amplitude); 128 } 129 130 /** 131 * Generates noise for the 2D coordinates using the specified number of octaves and parameters 132 * 133 * @param x X-coordinate 134 * @param y Y-coordinate 135 * @param frequency How much to alter the frequency by each octave 136 * @param amplitude How much to alter the amplitude by each octave 137 * @param normalized If true, normalize the value to [-1, 1] 138 * @return Resulting noise 139 */ 140 public double noise(double x, double y, double frequency, double amplitude, boolean normalized) { 141 return noise(x, y, 0, frequency, amplitude, normalized); 142 } 143 144 /** 145 * Generates noise for the 3D coordinates using the specified number of octaves and parameters 146 * 147 * @param x X-coordinate 148 * @param y Y-coordinate 149 * @param z Z-coordinate 150 * @param frequency How much to alter the frequency by each octave 151 * @param amplitude How much to alter the amplitude by each octave 152 * @return Resulting noise 153 */ 154 public double noise(double x, double y, double z, double frequency, double amplitude) { 155 return noise(x, y, z, frequency, amplitude, false); 156 } 157 158 /** 159 * Generates noise for the 3D coordinates using the specified number of octaves and parameters 160 * 161 * @param x X-coordinate 162 * @param y Y-coordinate 163 * @param z Z-coordinate 164 * @param frequency How much to alter the frequency by each octave 165 * @param amplitude How much to alter the amplitude by each octave 166 * @param normalized If true, normalize the value to [-1, 1] 167 * @return Resulting noise 168 */ 169 public double noise(double x, double y, double z, double frequency, double amplitude, boolean normalized) { 170 double result = 0; 171 double amp = 1; 172 double freq = 1; 173 double max = 0; 174 175 x *= xScale; 176 y *= yScale; 177 z *= zScale; 178 179 for (int i = 0; i < octaves.length; i++) { 180 result += octaves[i].noise(x * freq, y * freq, z * freq) * amp; 181 max += amp; 182 freq *= frequency; 183 amp *= amplitude; 184 } 185 186 if (normalized) { 187 result /= max; 188 } 189 190 return result; 191 } 192}