Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.util.noise;
2
3/**
4 * Base class for all noise generators
5 */
6public abstract class NoiseGenerator {
7 protected final int perm[] = new int[512];
8 protected double offsetX;
9 protected double offsetY;
10 protected double offsetZ;
11
12 /**
13 * Speedy floor, faster than (int)Math.floor(x)
14 *
15 * @param x Value to floor
16 * @return Floored value
17 */
18 public static int floor(double x) {
19 return x >= 0 ? (int) x : (int) x - 1;
20 }
21
22 protected static double fade(double x) {
23 return x * x * x * (x * (x * 6 - 15) + 10);
24 }
25
26 protected static double lerp(double x, double y, double z) {
27 return y + x * (z - y);
28 }
29
30 protected static double grad(int hash, double x, double y, double z) {
31 hash &= 15;
32 double u = hash < 8 ? x : y;
33 double v = hash < 4 ? y : hash == 12 || hash == 14 ? x : z;
34 return ((hash & 1) == 0 ? u : -u) + ((hash & 2) == 0 ? v : -v);
35 }
36
37 /**
38 * Computes and returns the 1D noise for the given coordinate in 1D space
39 *
40 * @param x X coordinate
41 * @return Noise at given location, from range -1 to 1
42 */
43 public double noise(double x) {
44 return noise(x, 0, 0);
45 }
46
47 /**
48 * Computes and returns the 2D noise for the given coordinates in 2D space
49 *
50 * @param x X coordinate
51 * @param y Y coordinate
52 * @return Noise at given location, from range -1 to 1
53 */
54 public double noise(double x, double y) {
55 return noise(x, y, 0);
56 }
57
58 /**
59 * Computes and returns the 3D noise for the given coordinates in 3D space
60 *
61 * @param x X coordinate
62 * @param y Y coordinate
63 * @param z Z coordinate
64 * @return Noise at given location, from range -1 to 1
65 */
66 public abstract double noise(double x, double y, double z);
67
68 /**
69 * Generates noise for the 1D coordinates using the specified number of octaves and parameters
70 *
71 * @param x X-coordinate
72 * @param octaves Number of octaves to use
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 */
77 public double noise(double x, int octaves, double frequency, double amplitude) {
78 return noise(x, 0, 0, octaves, frequency, amplitude);
79 }
80
81 /**
82 * Generates noise for the 1D coordinates using the specified number of octaves and parameters
83 *
84 * @param x X-coordinate
85 * @param octaves Number of octaves to use
86 * @param frequency How much to alter the frequency by each octave
87 * @param amplitude How much to alter the amplitude by each octave
88 * @param normalized If true, normalize the value to [-1, 1]
89 * @return Resulting noise
90 */
91 public double noise(double x, int octaves, double frequency, double amplitude, boolean normalized) {
92 return noise(x, 0, 0, octaves, frequency, amplitude, normalized);
93 }
94
95 /**
96 * Generates noise for the 2D coordinates using the specified number of octaves and parameters
97 *
98 * @param x X-coordinate
99 * @param y Y-coordinate
100 * @param octaves Number of octaves to use
101 * @param frequency How much to alter the frequency by each octave
102 * @param amplitude How much to alter the amplitude by each octave
103 * @return Resulting noise
104 */
105 public double noise(double x, double y, int octaves, double frequency, double amplitude) {
106 return noise(x, y, 0, octaves, frequency, amplitude);
107 }
108
109 /**
110 * Generates noise for the 2D coordinates using the specified number of octaves and parameters
111 *
112 * @param x X-coordinate
113 * @param y Y-coordinate
114 * @param octaves Number of octaves to use
115 * @param frequency How much to alter the frequency by each octave
116 * @param amplitude How much to alter the amplitude by each octave
117 * @param normalized If true, normalize the value to [-1, 1]
118 * @return Resulting noise
119 */
120 public double noise(double x, double y, int octaves, double frequency, double amplitude, boolean normalized) {
121 return noise(x, y, 0, octaves, frequency, amplitude, normalized);
122 }
123
124 /**
125 * Generates noise for the 3D coordinates using the specified number of octaves and parameters
126 *
127 * @param x X-coordinate
128 * @param y Y-coordinate
129 * @param z Z-coordinate
130 * @param octaves Number of octaves to use
131 * @param frequency How much to alter the frequency by each octave
132 * @param amplitude How much to alter the amplitude by each octave
133 * @return Resulting noise
134 */
135 public double noise(double x, double y, double z, int octaves, double frequency, double amplitude) {
136 return noise(x, y, z, octaves, frequency, amplitude, false);
137 }
138
139 /**
140 * Generates noise for the 3D coordinates using the specified number of octaves and parameters
141 *
142 * @param x X-coordinate
143 * @param y Y-coordinate
144 * @param z Z-coordinate
145 * @param octaves Number of octaves to use
146 * @param frequency How much to alter the frequency by each octave
147 * @param amplitude How much to alter the amplitude by each octave
148 * @param normalized If true, normalize the value to [-1, 1]
149 * @return Resulting noise
150 */
151 public double noise(double x, double y, double z, int octaves, double frequency, double amplitude, boolean normalized) {
152 double result = 0;
153 double amp = 1;
154 double freq = 1;
155 double max = 0;
156
157 for (int i = 0; i < octaves; i++) {
158 result += noise(x * freq, y * freq, z * freq) * amp;
159 max += amp;
160 freq *= frequency;
161 amp *= amplitude;
162 }
163
164 if (normalized) {
165 result /= max;
166 }
167
168 return result;
169 }
170}