1 | /* |
1 | /* |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
2 | * This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 | * |
3 | * |
|
|
4 | * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team |
4 | * Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * Copyright (©) 2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 | * |
6 | * |
6 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * Deliantra is free software: you can redistribute it and/or modify it under |
7 | * the terms of the Affero GNU General Public License as published by the |
8 | * the terms of the Affero GNU General Public License as published by the |
8 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * Free Software Foundation, either version 3 of the License, or (at your |
9 | * option) any later version. |
10 | * option) any later version. |
10 | * |
11 | * |
11 | * This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
15 | * |
16 | * |
16 | * You should have received a copy of the Affero GNU General Public License |
17 | * You should have received a copy of the Affero GNU General Public License |
17 | * and the GNU General Public License along with this program. If not, see |
18 | * and the GNU General Public License along with this program. If not, see |
18 | * <http://www.gnu.org/licenses/>. |
19 | * <http://www.gnu.org/licenses/>. |
19 | * |
20 | * |
20 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | * The authors can be reached via e-mail to <support@deliantra.net> |
21 | */ |
22 | */ |
22 | |
23 | |
23 | #include "noise.h" |
24 | #include "noise.h" |
24 | |
25 | |
… | |
… | |
267 | vec2d I = floor (P); |
268 | vec2d I = floor (P); |
268 | vec2d F = P - I; |
269 | vec2d F = P - I; |
269 | |
270 | |
270 | value_t v = 0; |
271 | value_t v = 0; |
271 | |
272 | |
|
|
273 | seed *= 3039177861; |
272 | uint8_t i1 = uint8_t (I[1]) ^ (seed >> 16); |
274 | uint8_t i1 = uint8_t (I[1]) + (seed >> 16); |
273 | uint8_t i0 = uint8_t (I[0]) ^ (seed >> 8); |
275 | uint8_t i0 = uint8_t (I[0]) + (seed >> 8); |
|
|
276 | |
|
|
277 | uint8_t h2 = rvmap[2](seed); |
274 | |
278 | |
275 | for (int j = -1; j <= 2; ++j) |
279 | for (int j = -1; j <= 2; ++j) |
276 | { |
280 | { |
277 | uint8_t h1 = rvmap[1](i1 + j) ^ seed; |
281 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
278 | |
282 | |
279 | for (int i = -1; i <= 2; ++i) |
283 | for (int i = -1; i <= 2; ++i) |
280 | { |
284 | { |
281 | vec2d A = F - vec2d (i, j); |
285 | vec2d A = F - vec2d (i, j); |
282 | value_t d = dot (A, A); |
286 | value_t d = dot (A, A); |
… | |
… | |
296 | } |
300 | } |
297 | } |
301 | } |
298 | } |
302 | } |
299 | |
303 | |
300 | return v; |
304 | return v; |
301 | } |
305 | } |
302 | |
306 | |
303 | ///////////////////////////////////////////////////////////////////////////// |
307 | ///////////////////////////////////////////////////////////////////////////// |
304 | |
308 | |
305 | template<> |
309 | template<> |
306 | vec3d::T_numtype |
310 | vec3d::T_numtype |
307 | noise_gen_base<vec3d>::operator() (vec3d P, uint32_t seed) |
311 | noise_gen_base<vec3d>::operator() (vec3d P, uint32_t seed) |
308 | { |
312 | { |
309 | vec3d I = floor (P); |
313 | vec3d I = floor (P); |
310 | vec3d F = P - I; |
314 | vec3d F = P - I; |
311 | |
315 | |
|
|
316 | seed *= 3039177861; |
312 | uint8_t i2 = uint8_t (I[2]) ^ (seed >> 24); |
317 | uint8_t i2 = uint8_t (I[2]) + (seed >> 24); |
313 | uint8_t i1 = uint8_t (I[1]) ^ (seed >> 16); |
318 | uint8_t i1 = uint8_t (I[1]) + (seed >> 16); |
314 | uint8_t i0 = uint8_t (I[0]) ^ (seed >> 8); |
319 | uint8_t i0 = uint8_t (I[0]) + (seed >> 8); |
|
|
320 | |
|
|
321 | uint8_t h3 = rvmap[3](seed); |
315 | |
322 | |
316 | value_t v = 0; |
323 | value_t v = 0; |
317 | |
324 | |
318 | for (int k = -1; k <= 2; ++k) |
325 | for (int k = -1; k <= 2; ++k) |
319 | { |
326 | { |
320 | uint8_t h2 = rvmap[2](i2 + k) ^ seed; |
327 | uint8_t h2 = rvmap[2](i2 + k) ^ h3; |
321 | |
328 | |
322 | for (int j = -1; j <= 2; ++j) |
329 | for (int j = -1; j <= 2; ++j) |
323 | { |
330 | { |
324 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
331 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
325 | |
332 | |
… | |
… | |
353 | noise_gen<vec3d>::operator() (vec3d P, vec3d N, uint32_t seed) |
360 | noise_gen<vec3d>::operator() (vec3d P, vec3d N, uint32_t seed) |
354 | { |
361 | { |
355 | vec3d I = floor (P); |
362 | vec3d I = floor (P); |
356 | vec3d F = P - I; |
363 | vec3d F = P - I; |
357 | |
364 | |
|
|
365 | seed *= 3039177861; |
358 | uint8_t i2 = uint8_t (I[2]) ^ (seed >> 24); |
366 | uint8_t i2 = uint8_t (I[2]) + (seed >> 24); |
359 | uint8_t i1 = uint8_t (I[1]) ^ (seed >> 16); |
367 | uint8_t i1 = uint8_t (I[1]) + (seed >> 16); |
360 | uint8_t i0 = uint8_t (I[0]) ^ (seed >> 8); |
368 | uint8_t i0 = uint8_t (I[0]) + (seed >> 8); |
|
|
369 | |
|
|
370 | uint8_t h3 = rvmap[3](seed); |
361 | |
371 | |
362 | value_t v = 0; |
372 | value_t v = 0; |
363 | |
373 | |
364 | for (int k = -1; k <= 2; ++k) |
374 | for (int k = -1; k <= 2; ++k) |
365 | { |
375 | { |
366 | uint8_t h2 = rvmap[2](i2 + k) ^ seed; |
376 | uint8_t h2 = rvmap[2](i2 + k) ^ h3; |
367 | |
377 | |
368 | for (int j = -1; j <= 2; ++j) |
378 | for (int j = -1; j <= 2; ++j) |
369 | { |
379 | { |
370 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
380 | uint8_t h1 = rvmap[1](i1 + j) ^ h2; |
371 | |
381 | |
… | |
… | |
601 | } |
611 | } |
602 | |
612 | |
603 | template class frac_gen<vec2d>; |
613 | template class frac_gen<vec2d>; |
604 | template class frac_gen<vec3d>; |
614 | template class frac_gen<vec3d>; |
605 | |
615 | |
606 | ///////////////////////////////////////////////////////////////////////////// |
|
|
607 | |
|
|
608 | void noise_test (); |
|
|
609 | void noise_test () |
|
|
610 | { |
|
|
611 | frac2d gen; |
|
|
612 | |
|
|
613 | frac2d vec_gen1 (4, 2, 0.5, 1); |
|
|
614 | frac2d vec_gen2 (4, 2, 0.5, 2); |
|
|
615 | |
|
|
616 | #if 1 |
|
|
617 | int N = 1024; |
|
|
618 | |
|
|
619 | printf ("P6 %d %d 255\n", N, N); |
|
|
620 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
|
|
621 | for (int y = 0; y < N; ++y) |
|
|
622 | { |
|
|
623 | if (!(y&63))fprintf (stderr, "y %d\n", y);//D |
|
|
624 | for (int x = 0; x < N; ++x) |
|
|
625 | { |
|
|
626 | vec2d P = vec2d (x, y) * (25000.f / N); |
|
|
627 | |
|
|
628 | //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout); |
|
|
629 | //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout); |
|
|
630 | //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout); |
|
|
631 | //putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout); |
|
|
632 | //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout); |
|
|
633 | |
|
|
634 | // mountais or somesuch(?) |
|
|
635 | //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout); |
|
|
636 | |
|
|
637 | // temperature |
|
|
638 | //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout); |
|
|
639 | // rain |
|
|
640 | |
|
|
641 | float continent; |
|
|
642 | |
|
|
643 | { |
|
|
644 | const float continent_scale = 0.00008; |
|
|
645 | |
|
|
646 | vec2d perturb ( |
|
|
647 | vec_gen1.fBm (P * 0.0004), |
|
|
648 | vec_gen2.fBm (P * 0.0004) |
|
|
649 | ); |
|
|
650 | |
|
|
651 | const float W = 1000 * continent_scale; |
|
|
652 | const float N = (25000 - 1) * continent_scale; |
|
|
653 | |
|
|
654 | static frac2d perturb_gen (4, 2, 0.5, 3); |
|
|
655 | float perturb_perturb = gen.fBm (P * 0.0004); |
|
|
656 | perturb_perturb = perturb_perturb * perturb_perturb * 0.004; |
|
|
657 | |
|
|
658 | vec2d P_perturb = P * continent_scale + perturb * min (W, gen.noise (P * perturb_perturb) * W * 8); |
|
|
659 | |
|
|
660 | static frac2d continent_gen (13, 2.13, 0.5); |
|
|
661 | continent = continent_gen.fBm (P_perturb) + 0.05f; |
|
|
662 | |
|
|
663 | float border = W; // within n places of the border |
|
|
664 | |
|
|
665 | min_it (border, P_perturb [0]); |
|
|
666 | min_it (border, N - P_perturb [0]); |
|
|
667 | min_it (border, P_perturb [1]); |
|
|
668 | min_it (border, N - P_perturb [1]); |
|
|
669 | |
|
|
670 | continent = blend (-1.f, continent, border, 0.f, W); |
|
|
671 | } |
|
|
672 | |
|
|
673 | vec3d c (1, 1, 1); |
|
|
674 | float v; |
|
|
675 | |
|
|
676 | if (continent < 0) |
|
|
677 | { |
|
|
678 | // ocean |
|
|
679 | |
|
|
680 | v = min (continent * 10, -0.2f); |
|
|
681 | c = vec3d (0, 0, 1); |
|
|
682 | } |
|
|
683 | else |
|
|
684 | { |
|
|
685 | // continent |
|
|
686 | |
|
|
687 | // big rivers |
|
|
688 | static frac2d river_gen (2); |
|
|
689 | float river1 = river_gen.fBm (P * 0.0004);// - (P[0] / 25000) * 0.18 + 0.06; |
|
|
690 | float river2 = river_gen.ridgedmultifractal (P * 0.04 + vec2d (3, 5), 0.8, 10) - (P[1] / 25000) * 0.1; |
|
|
691 | |
|
|
692 | if (river1 > 0.05f) |
|
|
693 | { |
|
|
694 | v = -0.1f; |
|
|
695 | c = vec3d (0.4, 0.4, 1); |
|
|
696 | } |
|
|
697 | else if (river1 > 0.08f && river2 > 0.1f) |
|
|
698 | { |
|
|
699 | v = -0.05f; |
|
|
700 | c = vec3d (0.4, 0.4, 1); |
|
|
701 | } |
|
|
702 | else |
|
|
703 | { |
|
|
704 | river1 -= 0.07f; |
|
|
705 | |
|
|
706 | //c = river1 > 0 ? vec3d (0.8, 0.8, 0) : vec3d (0.8, 0, 0); |
|
|
707 | c = blend0 (vec3d (0.8, 0, 0), vec3d (0.8, 0.8, 0), river1, 0.01f);; |
|
|
708 | |
|
|
709 | static frac2d mountain_gen (8, 2.14, 0.5); |
|
|
710 | float mountain = mountain_gen.ridgedmultifractal (P * 0.004); |
|
|
711 | v = blend0 (mountain * 3 - 1, continent, river1, 0.05f); |
|
|
712 | } |
|
|
713 | |
|
|
714 | v=abs(river1) < 0.1; |
|
|
715 | c=vec3d(1,1,1); |
|
|
716 | } |
|
|
717 | |
|
|
718 | c *= v * 0.5 + 0.5; |
|
|
719 | |
|
|
720 | putc (clamp<int> (255 * c[0], 0, 255), stdout); |
|
|
721 | putc (clamp<int> (255 * c[1], 0, 255), stdout); |
|
|
722 | putc (clamp<int> (255 * c[2], 0, 255), stdout); |
|
|
723 | |
|
|
724 | //cells |
|
|
725 | //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout); |
|
|
726 | } |
|
|
727 | } |
|
|
728 | #else |
|
|
729 | int N = 128; |
|
|
730 | |
|
|
731 | //printf ("P6 %d %d 255\n", N, N); |
|
|
732 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
|
|
733 | frac3d gen3 (3);; |
|
|
734 | for (int z = 0; z < N; ++z) |
|
|
735 | { |
|
|
736 | if (!(z&7))fprintf (stderr, "z %d\n", z);//D |
|
|
737 | for (int y = 0; y < N; ++y) |
|
|
738 | for (int x = 0; x < N; ++x) |
|
|
739 | { |
|
|
740 | float v = gen3.ridgedmultifractal (vec3d (x * 0.001 + 0.2, y * 0.001 + 0.2, z * 0.01 + 0.2), 1.03, 2) * 2; |
|
|
741 | |
|
|
742 | #if 0 |
|
|
743 | if (z < 64) |
|
|
744 | v = v * (z * z) / (64 * 64); |
|
|
745 | #endif |
|
|
746 | |
|
|
747 | if (v <= 0.9) |
|
|
748 | continue; |
|
|
749 | |
|
|
750 | float r[4]; |
|
|
751 | int i[4]; |
|
|
752 | |
|
|
753 | r[0] = x; |
|
|
754 | r[1] = y; |
|
|
755 | r[2] = z; |
|
|
756 | r[3] = v; |
|
|
757 | |
|
|
758 | memcpy (i, r, 16); |
|
|
759 | |
|
|
760 | i[0] = htonl (i[0]); |
|
|
761 | i[1] = htonl (i[1]); |
|
|
762 | i[2] = htonl (i[2]); |
|
|
763 | i[3] = htonl (i[3]); |
|
|
764 | |
|
|
765 | fwrite (i, 4*4, 1, stdout); |
|
|
766 | } |
|
|
767 | } |
|
|
768 | #endif |
|
|
769 | |
|
|
770 | exit (0); |
|
|
771 | } |
|
|