the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
1#include "stdafx.h"
2#include "SimplexNoise.h"
3
4int SimplexNoise::grad3[12][3] = { { 1, 1, 0 }, { -1, 1, 0 }, { 1, -1, 0 }, { -1, -1, 0 }, { 1, 0, 1 }, { -1, 0, 1 }, { 1, 0, -1 }, { -1, 0, -1 }, { 0, 1, 1 }, { 0, -1, 1 }, { 0, 1, -1 }, { 0, -1, -1 } };
5
6double SimplexNoise::F2 = 0.5 * (sqrt(3.0) - 1.0);
7double SimplexNoise::G2 = (3.0 - sqrt(3.0)) / 6.0;
8double SimplexNoise::F3 = 1.0 / 3.0;
9double SimplexNoise::G3 = 1.0 / 6.0;
10
11SimplexNoise::SimplexNoise()
12{
13 Random random;
14 init(&random);
15}
16
17SimplexNoise::SimplexNoise(Random *random)
18{
19 init(random);
20}
21
22void SimplexNoise::init(Random *random)
23{
24 p = new int[512];
25
26 xo = random->nextDouble() * 256;
27 yo = random->nextDouble() * 256;
28 zo = random->nextDouble() * 256;
29 for (int i = 0; i < 256; i++)
30 {
31 p[i] = i;
32 }
33
34 for (int i = 0; i < 256; i++)
35 {
36 int j = random->nextInt(256 - i) + i;
37 int tmp = p[i];
38 p[i] = p[j];
39 p[j] = tmp;
40
41 p[i + 256] = p[i];
42 }
43}
44
45SimplexNoise::~SimplexNoise()
46{
47 delete [] p;
48}
49
50int SimplexNoise::fastfloor(double x)
51{
52 return x > 0 ? (int) x : (int) x - 1;
53}
54
55double SimplexNoise::dot(int *g, double x, double y)
56{
57 return g[0] * x + g[1] * y;
58}
59
60double SimplexNoise::dot(int *g, double x, double y, double z)
61{
62 return g[0] * x + g[1] * y + g[2] * z;
63}
64
65double SimplexNoise::getValue(double xin, double yin)
66{
67 double n0, n1, n2; // Noise contributions from the three corners
68 // Skew the input space to determine which simplex cell we're in
69 double s = (xin + yin) * F2; // Hairy factor for 2D
70 int i = fastfloor(xin + s);
71 int j = fastfloor(yin + s);
72 double t = (i + j) * G2;
73 double X0 = i - t; // Unskew the cell origin back to (x,y) space
74 double Y0 = j - t;
75 double x0 = xin - X0; // The x,y distances from the cell origin
76 double y0 = yin - Y0;
77 // For the 2D case, the simplex shape is an equilateral triangle.
78 // Determine which simplex we are in.
79 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
80 if (x0 > y0) {
81 i1 = 1;
82 j1 = 0;
83 } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
84 else {
85 i1 = 0;
86 j1 = 1;
87 } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
88 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
89 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
90 // c = (3-sqrt(3))/6
91 double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
92 double y1 = y0 - j1 + G2;
93 double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
94 double y2 = y0 - 1.0 + 2.0 * G2;
95 // Work out the hashed gradient indices of the three simplex corners
96 int ii = i & 255;
97 int jj = j & 255;
98 int gi0 = p[ii + p[jj]] % 12;
99 int gi1 = p[ii + i1 + p[jj + j1]] % 12;
100 int gi2 = p[ii + 1 + p[jj + 1]] % 12;
101 // Calculate the contribution from the three corners
102 double t0 = 0.5 - x0 * x0 - y0 * y0;
103 if (t0 < 0) n0 = 0.0;
104 else {
105 t0 *= t0;
106 n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
107 }
108 double t1 = 0.5 - x1 * x1 - y1 * y1;
109 if (t1 < 0) n1 = 0.0;
110 else {
111 t1 *= t1;
112 n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
113 }
114 double t2 = 0.5 - x2 * x2 - y2 * y2;
115 if (t2 < 0) n2 = 0.0;
116 else {
117 t2 *= t2;
118 n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
119 }
120 // Add contributions from each corner to get the final noise value.
121 // The result is scaled to return values in the interval [-1,1].
122 return 70.0 * (n0 + n1 + n2);
123}
124
125double SimplexNoise::getValue(double xin, double yin, double zin)
126{
127 double n0, n1, n2, n3;
128 double s = (xin + yin + zin) * F3;
129 int i = fastfloor(xin + s);
130 int j = fastfloor(yin + s);
131 int k = fastfloor(zin + s);
132
133 double t = (i + j + k) * G3;
134 double X0 = i - t;
135 double Y0 = j - t;
136 double Z0 = k - t;
137 double x0 = xin - X0;
138 double y0 = yin - Y0;
139 double z0 = zin - Z0;
140 int i1, j1, k1;
141 int i2, j2, k2;
142 if (x0 >= y0)
143 {
144 if (y0 >= z0)
145 {
146 i1 = 1;
147 j1 = 0;
148 k1 = 0;
149 i2 = 1;
150 j2 = 1;
151 k2 = 0;
152 } // X Y Z order
153 else if (x0 >= z0)
154 {
155 i1 = 1;
156 j1 = 0;
157 k1 = 0;
158 i2 = 1;
159 j2 = 0;
160 k2 = 1;
161 } // X Z Y order
162 else
163 {
164 i1 = 0;
165 j1 = 0;
166 k1 = 1;
167 i2 = 1;
168 j2 = 0;
169 k2 = 1;
170 } // Z X Y order
171 }
172 else
173 { // x0<y0
174 if (y0 < z0)
175 {
176 i1 = 0;
177 j1 = 0;
178 k1 = 1;
179 i2 = 0;
180 j2 = 1;
181 k2 = 1;
182 } // Z Y X order
183 else if (x0 < z0)
184 {
185 i1 = 0;
186 j1 = 1;
187 k1 = 0;
188 i2 = 0;
189 j2 = 1;
190 k2 = 1;
191 } // Y Z X order
192 else
193 {
194 i1 = 0;
195 j1 = 1;
196 k1 = 0;
197 i2 = 1;
198 j2 = 1;
199 k2 = 0;
200 } // Y X Z order
201 }
202 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
203 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
204 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
205 // c = 1/6.
206
207 double x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
208 double y1 = y0 - j1 + G3;
209 double z1 = z0 - k1 + G3;
210 double x2 = x0 - i2 + 2.0 * G3; // Offsets for third corner in (x,y,z) coords
211 double y2 = y0 - j2 + 2.0 * G3;
212 double z2 = z0 - k2 + 2.0 * G3;
213 double x3 = x0 - 1.0 + 3.0 * G3; // Offsets for last corner in (x,y,z) coords
214 double y3 = y0 - 1.0 + 3.0 * G3;
215 double z3 = z0 - 1.0 + 3.0 * G3;
216 // Work out the hashed gradient indices of the four simplex corners
217 int ii = i & 255;
218 int jj = j & 255;
219 int kk = k & 255;
220 int gi0 = p[ii + p[jj + p[kk]]] % 12;
221 int gi1 = p[ii + i1 + p[jj + j1 + p[kk + k1]]] % 12;
222 int gi2 = p[ii + i2 + p[jj + j2 + p[kk + k2]]] % 12;
223 int gi3 = p[ii + 1 + p[jj + 1 + p[kk + 1]]] % 12;
224 // Calculate the contribution from the four corners
225 double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
226 if (t0 < 0) n0 = 0.0;
227 else
228 {
229 t0 *= t0;
230 n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
231 }
232 double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
233 if (t1 < 0) n1 = 0.0;
234 else
235 {
236 t1 *= t1;
237 n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
238 }
239 double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
240 if (t2 < 0) n2 = 0.0;
241 else
242 {
243 t2 *= t2;
244 n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
245 }
246 double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
247 if (t3 < 0) n3 = 0.0;
248 else
249 {
250 t3 *= t3;
251 n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
252 }
253 // Add contributions from each corner to get the final noise value.
254 // The result is scaled to stay just inside [-1,1]
255 return 32.0 * (n0 + n1 + n2 + n3);
256}
257
258void SimplexNoise::add(doubleArray buffer, double _x, double _y, int xSize, int ySize, double xs, double ys, double pow)
259{
260 int pp = 0;
261 for (int xx = 0; xx < xSize; xx++)
262 {
263 double xin = (_x + xx) * xs + xo;
264 for (int yy = 0; yy < ySize; yy++)
265 {
266 double yin = (_y + yy) * ys + yo;
267
268 double n0, n1, n2;
269 double s = (xin + yin) * F2; // Hairy factor for 2D
270 int i = fastfloor(xin + s);
271 int j = fastfloor(yin + s);
272 double t = (i + j) * G2;
273 double X0 = i - t; // Unskew the cell origin back to (x,y) space
274 double Y0 = j - t;
275 double x0 = xin - X0; // The x,y distances from the cell origin
276 double y0 = yin - Y0;
277 // For the 2D case, the simplex shape is an equilateral triangle.
278 // Determine which simplex we are in.
279 int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
280 if (x0 > y0)
281 {
282 i1 = 1;
283 j1 = 0;
284 } // lower triangle, XY order: (0,0)->(1,0)->(1,1)
285 else
286 {
287 i1 = 0;
288 j1 = 1;
289 } // upper triangle, YX order: (0,0)->(0,1)->(1,1)
290 // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
291 // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
292 // c = (3-sqrt(3))/6
293 double x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
294 double y1 = y0 - j1 + G2;
295 double x2 = x0 - 1.0 + 2.0 * G2; // Offsets for last corner in (x,y) unskewed coords
296 double y2 = y0 - 1.0 + 2.0 * G2;
297 // Work out the hashed gradient indices of the three simplex corners
298 int ii = i & 255;
299 int jj = j & 255;
300 int gi0 = p[ii + p[jj]] % 12;
301 int gi1 = p[ii + i1 + p[jj + j1]] % 12;
302 int gi2 = p[ii + 1 + p[jj + 1]] % 12;
303 // Calculate the contribution from the three corners
304 double t0 = 0.5 - x0 * x0 - y0 * y0;
305 if (t0 < 0) n0 = 0.0;
306 else
307 {
308 t0 *= t0;
309 n0 = t0 * t0 * dot(grad3[gi0], x0, y0); // (x,y) of grad3 used for 2D gradient
310 }
311 double t1 = 0.5 - x1 * x1 - y1 * y1;
312 if (t1 < 0) n1 = 0.0;
313 else
314 {
315 t1 *= t1;
316 n1 = t1 * t1 * dot(grad3[gi1], x1, y1);
317 }
318 double t2 = 0.5 - x2 * x2 - y2 * y2;
319 if (t2 < 0) n2 = 0.0;
320 else
321 {
322 t2 *= t2;
323 n2 = t2 * t2 * dot(grad3[gi2], x2, y2);
324 }
325 // Add contributions from each corner to get the final noise value.
326 // The result is scaled to return values in the interval [-1,1].
327 buffer[pp++] += (70.0 * (n0 + n1 + n2))*pow;
328 }
329 }
330
331}
332void SimplexNoise::add(doubleArray buffer, double _x, double _y, double _z, int xSize, int ySize, int zSize, double xs, double ys, double zs, double pow)
333{
334 int pp = 0;
335 for (int xx = 0; xx < xSize; xx++)
336 {
337 double xin = (_x + xx) * xs + xo;
338 for (int zz = 0; zz < zSize; zz++)
339 {
340 double zin = (_z + zz) * zs + zo;
341 for (int yy = 0; yy < ySize; yy++)
342 {
343 double yin = (_y + yy) * ys + yo;
344
345 double n0, n1, n2, n3;
346 double s = (xin + yin + zin) * F3;
347 int i = fastfloor(xin + s);
348 int j = fastfloor(yin + s);
349 int k = fastfloor(zin + s);
350 double t = (i + j + k) * G3;
351 double X0 = i - t;
352 double Y0 = j - t;
353 double Z0 = k - t;
354 double x0 = xin - X0;
355 double y0 = yin - Y0;
356 double z0 = zin - Z0;
357 int i1, j1, k1;
358 int i2, j2, k2;
359 if (x0 >= y0)
360 {
361 if (y0 >= z0)
362 {
363 i1 = 1;
364 j1 = 0;
365 k1 = 0;
366 i2 = 1;
367 j2 = 1;
368 k2 = 0;
369 } // X Y Z order
370 else if (x0 >= z0)
371 {
372 i1 = 1;
373 j1 = 0;
374 k1 = 0;
375 i2 = 1;
376 j2 = 0;
377 k2 = 1;
378 } // X Z Y order
379 else
380 {
381 i1 = 0;
382 j1 = 0;
383 k1 = 1;
384 i2 = 1;
385 j2 = 0;
386 k2 = 1;
387 } // Z X Y order
388 }
389 else
390 { // x0<y0
391 if (y0 < z0)
392 {
393 i1 = 0;
394 j1 = 0;
395 k1 = 1;
396 i2 = 0;
397 j2 = 1;
398 k2 = 1;
399 } // Z Y X order
400 else if (x0 < z0)
401 {
402 i1 = 0;
403 j1 = 1;
404 k1 = 0;
405 i2 = 0;
406 j2 = 1;
407 k2 = 1;
408 } // Y Z X order
409 else
410 {
411 i1 = 0;
412 j1 = 1;
413 k1 = 0;
414 i2 = 1;
415 j2 = 1;
416 k2 = 0;
417 } // Y X Z order
418 }
419 // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
420 // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
421 // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
422 // c = 1/6.
423
424 double x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
425 double y1 = y0 - j1 + G3;
426 double z1 = z0 - k1 + G3;
427 double x2 = x0 - i2 + 2.0 * G3; // Offsets for third corner in (x,y,z) coords
428 double y2 = y0 - j2 + 2.0 * G3;
429 double z2 = z0 - k2 + 2.0 * G3;
430 double x3 = x0 - 1.0 + 3.0 * G3; // Offsets for last corner in (x,y,z) coords
431 double y3 = y0 - 1.0 + 3.0 * G3;
432 double z3 = z0 - 1.0 + 3.0 * G3;
433 // Work out the hashed gradient indices of the four simplex corners
434 int ii = i & 255;
435 int jj = j & 255;
436 int kk = k & 255;
437 int gi0 = p[ii + p[jj + p[kk]]] % 12;
438 int gi1 = p[ii + i1 + p[jj + j1 + p[kk + k1]]] % 12;
439 int gi2 = p[ii + i2 + p[jj + j2 + p[kk + k2]]] % 12;
440 int gi3 = p[ii + 1 + p[jj + 1 + p[kk + 1]]] % 12;
441 // Calculate the contribution from the four corners
442 double t0 = 0.6 - x0 * x0 - y0 * y0 - z0 * z0;
443 if (t0 < 0) n0 = 0.0;
444 else
445 {
446 t0 *= t0;
447 n0 = t0 * t0 * dot(grad3[gi0], x0, y0, z0);
448 }
449 double t1 = 0.6 - x1 * x1 - y1 * y1 - z1 * z1;
450 if (t1 < 0) n1 = 0.0;
451 else
452 {
453 t1 *= t1;
454 n1 = t1 * t1 * dot(grad3[gi1], x1, y1, z1);
455 }
456 double t2 = 0.6 - x2 * x2 - y2 * y2 - z2 * z2;
457 if (t2 < 0) n2 = 0.0;
458 else
459 {
460 t2 *= t2;
461 n2 = t2 * t2 * dot(grad3[gi2], x2, y2, z2);
462 }
463 double t3 = 0.6 - x3 * x3 - y3 * y3 - z3 * z3;
464 if (t3 < 0) n3 = 0.0;
465 else
466 {
467 t3 *= t3;
468 n3 = t3 * t3 * dot(grad3[gi3], x3, y3, z3);
469 }
470 // Add contributions from each corner to get the final noise value.
471 // The result is scaled to stay just inside [-1,1]
472 buffer[pp++] += (32.0 * (n0 + n1 + n2 + n3))*pow;
473 }
474 }
475 }
476}