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 | |
… | |
… | |
245 | |
246 | |
246 | template<class vec_t> |
247 | template<class vec_t> |
247 | void |
248 | void |
248 | noise_gen_base<vec_t>::seed (seedable_rand_gen &rng) |
249 | noise_gen_base<vec_t>::seed (seedable_rand_gen &rng) |
249 | { |
250 | { |
250 | for (int i = 0; i < array_length (rvmap); ++i) |
251 | for (int i = 0; i < ecb_array_length (rvmap); ++i) |
251 | rvmap[i].seed (rng); |
252 | rvmap[i].seed (rng); |
252 | } |
253 | } |
253 | |
254 | |
254 | template<class vec_t> |
255 | template<class vec_t> |
255 | void |
256 | void |
… | |
… | |
299 | } |
300 | } |
300 | } |
301 | } |
301 | } |
302 | } |
302 | |
303 | |
303 | return v; |
304 | return v; |
304 | } |
305 | } |
305 | |
306 | |
306 | ///////////////////////////////////////////////////////////////////////////// |
307 | ///////////////////////////////////////////////////////////////////////////// |
307 | |
308 | |
308 | template<> |
309 | template<> |
309 | vec3d::T_numtype |
310 | vec3d::T_numtype |
… | |
… | |
412 | } |
413 | } |
413 | |
414 | |
414 | return v; |
415 | return v; |
415 | } |
416 | } |
416 | |
417 | |
417 | template class noise_gen<vec2d>; |
|
|
418 | template class noise_gen<vec3d>; |
|
|
419 | |
|
|
420 | ///////////////////////////////////////////////////////////////////////////// |
418 | ///////////////////////////////////////////////////////////////////////////// |
421 | |
419 | |
422 | template<class vec_t> |
420 | template<class vec_t> |
423 | frac_gen<vec_t>::frac_gen (int octaves, value_t lacunarity, value_t hurst_expo, seed_t seed) |
421 | frac_gen<vec_t>::frac_gen (int octaves, value_t lacunarity, value_t hurst_expo, seed_t seed) |
424 | : octaves (octaves), lac (lacunarity), h (hurst_expo) |
422 | : octaves (octaves), lac (lacunarity), h (hurst_expo) |
… | |
… | |
610 | } |
608 | } |
611 | |
609 | |
612 | template class frac_gen<vec2d>; |
610 | template class frac_gen<vec2d>; |
613 | template class frac_gen<vec3d>; |
611 | template class frac_gen<vec3d>; |
614 | |
612 | |
615 | ///////////////////////////////////////////////////////////////////////////// |
|
|
616 | |
|
|
617 | template<typename T, typename U> |
|
|
618 | T |
|
|
619 | inline border_blend (T a, T b, vec2d P, U N, U W) |
|
|
620 | { |
|
|
621 | U border = W; // within n places of the border |
|
|
622 | |
|
|
623 | min_it (border, P [0]); |
|
|
624 | min_it (border, N - P [0]); |
|
|
625 | min_it (border, P [1]); |
|
|
626 | min_it (border, N - P [1]); |
|
|
627 | |
|
|
628 | return blend (a, b,border, U(0), W); |
|
|
629 | } |
|
|
630 | |
|
|
631 | // highest mountains, deepest sea == 200 .. -200 |
|
|
632 | |
|
|
633 | static void |
|
|
634 | gen_height (int x, int y) |
|
|
635 | { |
|
|
636 | vec2d P = vec2d (x, y); |
|
|
637 | |
|
|
638 | const int deep_sea_z = -200; |
|
|
639 | const int mountain_z = 200; |
|
|
640 | |
|
|
641 | static frac2d gen(13); |
|
|
642 | |
|
|
643 | static frac2d vec_gen1 (6, 2, 0.5, 1); |
|
|
644 | static frac2d vec_gen2 (6, 2, 0.5, 2); |
|
|
645 | |
|
|
646 | const float continent_scale = 0.00008; |
|
|
647 | |
|
|
648 | vec2d perturb_pos = pow (P, 1.4) * 1e-5; |
|
|
649 | |
|
|
650 | vec2d perturb ( |
|
|
651 | vec_gen1.fBm (perturb_pos), |
|
|
652 | vec_gen2.fBm (perturb_pos) |
|
|
653 | ); |
|
|
654 | |
|
|
655 | float perturb_perturb = 1 - (P[1] - P[0]) * (1. / 25000 / 2); |
|
|
656 | perturb_perturb = perturb_perturb * perturb_perturb * 0.4; |
|
|
657 | perturb *= perturb_perturb; |
|
|
658 | |
|
|
659 | vec2d P_continent = P * continent_scale + perturb; |
|
|
660 | |
|
|
661 | static frac2d continent_gen (13, 2.13, 0.5); |
|
|
662 | float continent = continent_gen.fBm (P_continent) + 0.05f; |
|
|
663 | |
|
|
664 | float x_gradient = P[0] * (1. / 25000); |
|
|
665 | float y_gradient = P[1] * (1. / 25000); |
|
|
666 | float xy_gradient = (P[0] + P[1]) * (0.5 / 25000); |
|
|
667 | |
|
|
668 | const float N = (25000 - 1) * continent_scale; |
|
|
669 | |
|
|
670 | // we clip a large border on the perturbed shape, to get irregular coastline |
|
|
671 | // and then clip a smaller border around the real shape |
|
|
672 | continent = border_blend (-1.f, continent, P_continent , N, 400 * continent_scale); |
|
|
673 | continent = border_blend (-1.f, continent, P * continent_scale + perturb * 0.1, N, 100 * continent_scale); |
|
|
674 | |
|
|
675 | enum { |
|
|
676 | T_NONE, |
|
|
677 | T_OCEAN, |
|
|
678 | T_RIVER, |
|
|
679 | T_VALLEY, |
|
|
680 | T_MOUNTAIN, |
|
|
681 | } t = T_NONE; |
|
|
682 | |
|
|
683 | vec3d c; |
|
|
684 | int z = 1000000; |
|
|
685 | |
|
|
686 | // the continent increases in height from 0 to ~700 levels in the absence of anything else |
|
|
687 | // thats about one step every 7 maps. |
|
|
688 | int base_height = blend (0, 300, xy_gradient, 0.2f, 0.9f); |
|
|
689 | int river_height = base_height * 9 / 10; |
|
|
690 | |
|
|
691 | // add this to rivers to "dry them out" |
|
|
692 | float dry_out = max (0.f, lerp (xy_gradient, 0.7f, 1.f, 0.f, 0.3f)); |
|
|
693 | |
|
|
694 | static frac2d river_gen (2); |
|
|
695 | float river1 = abs (river_gen.fBm (P * 0.001 + perturb * 4)) + dry_out; |
|
|
696 | float river2 = river_gen.ridgedmultifractal (P * 0.04, 0.8, 10) - y_gradient * 0.2 - 0.16 - dry_out; |
|
|
697 | |
|
|
698 | float valley = river1 - 0.2f; |
|
|
699 | |
|
|
700 | static frac2d mountain_gen (8, 2.14, 0.5); |
|
|
701 | float mountain = mountain_gen.ridgedmultifractal (P * 0.004); |
|
|
702 | |
|
|
703 | t = valley < 0 ? T_VALLEY : T_MOUNTAIN; |
|
|
704 | c = blend0 (vec3d (0.8, 0.8, 0), vec3d (0.8, 0, 0), valley, 0.1f); |
|
|
705 | z = blend0 (base_height + continent * 300, base_height + mountain * xy_gradient * 400, valley, 0.1f); |
|
|
706 | |
|
|
707 | if (river1 < 0.01f) |
|
|
708 | { |
|
|
709 | // main rivers - they cut deeply into the mountains (base_height * 0.9f) |
|
|
710 | t = T_RIVER; |
|
|
711 | c = vec3d (0.2, 0.2, 1); |
|
|
712 | min_it (z, river_height + lerp<float> (river1, 0.f, 0.01f, -20, -1)); |
|
|
713 | } |
|
|
714 | |
|
|
715 | if (river2 > 0) |
|
|
716 | { |
|
|
717 | t = T_RIVER; |
|
|
718 | c = vec3d (0.2, 0.2, 1); |
|
|
719 | min_it (z, river_height + lerp<float> (river1, 0.f, 0.01f, -5, -1)); |
|
|
720 | } |
|
|
721 | |
|
|
722 | if (continent < 0) |
|
|
723 | { |
|
|
724 | t = T_OCEAN; |
|
|
725 | min_it (z, min (continent * 200, -1)); |
|
|
726 | c = vec3d (0, 0, 1); |
|
|
727 | } |
|
|
728 | |
|
|
729 | float v = clamp (lerp<float> (z, deep_sea_z, mountain_z*0+800, 0.f, 1.f), 0.f, 1.f); |
|
|
730 | c *= v; |
|
|
731 | |
|
|
732 | putc (clamp<int> (255 * c[0], 0, 255), stdout); |
|
|
733 | putc (clamp<int> (255 * c[1], 0, 255), stdout); |
|
|
734 | putc (clamp<int> (255 * c[2], 0, 255), stdout); |
|
|
735 | } |
|
|
736 | |
|
|
737 | void noise_test (); |
|
|
738 | void noise_test () |
|
|
739 | { |
|
|
740 | #if 1 |
|
|
741 | int Nw = 700; |
|
|
742 | |
|
|
743 | printf ("P6 %d %d 255\n", Nw * 3, Nw * 2); |
|
|
744 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
|
|
745 | for (int y = 0; y < Nw; ++y) |
|
|
746 | { |
|
|
747 | if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw);//D |
|
|
748 | |
|
|
749 | for (int x = 0; x < Nw; ++x) gen_height (x * 25000 / Nw, y * 25000 / Nw); |
|
|
750 | |
|
|
751 | for (int x = 0; x < Nw; ++x) gen_height (x + 400, y); |
|
|
752 | for (int x = 0; x < Nw; ++x) gen_height (x + 22000, y + 2000); |
|
|
753 | } |
|
|
754 | for (int y = 0; y < Nw; ++y) |
|
|
755 | { |
|
|
756 | if (!(y&63))fprintf (stderr, "y %d\n", y * 50 / Nw+50);//D |
|
|
757 | |
|
|
758 | for (int x = 0; x < Nw; ++x) gen_height (x + 1000, y + 22000); |
|
|
759 | for (int x = 0; x < Nw; ++x) gen_height (x + 12500, y + 12500); |
|
|
760 | for (int x = 0; x < Nw; ++x) gen_height (x + 22000, y + 22000); |
|
|
761 | } |
|
|
762 | |
|
|
763 | //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout); |
|
|
764 | //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout); |
|
|
765 | //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout); |
|
|
766 | //putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout); |
|
|
767 | //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout); |
|
|
768 | //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout); |
|
|
769 | //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout); |
|
|
770 | //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout); |
|
|
771 | #else |
|
|
772 | int N = 128; |
|
|
773 | |
|
|
774 | //printf ("P6 %d %d 255\n", N, N); |
|
|
775 | // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm |
|
|
776 | frac3d gen3 (3);; |
|
|
777 | for (int z = 0; z < N; ++z) |
|
|
778 | { |
|
|
779 | if (!(z&7))fprintf (stderr, "z %d\n", z);//D |
|
|
780 | for (int y = 0; y < N; ++y) |
|
|
781 | for (int x = 0; x < N; ++x) |
|
|
782 | { |
|
|
783 | float v = gen3.ridgedmultifractal (vec3d (x * 0.001 + 0.2, y * 0.001 + 0.2, z * 0.01 + 0.2), 1.03, 2) * 2; |
|
|
784 | |
|
|
785 | #if 0 |
|
|
786 | if (z < 64) |
|
|
787 | v = v * (z * z) / (64 * 64); |
|
|
788 | #endif |
|
|
789 | |
|
|
790 | if (v <= 0.9) |
|
|
791 | continue; |
|
|
792 | |
|
|
793 | float r[4]; |
|
|
794 | int i[4]; |
|
|
795 | |
|
|
796 | r[0] = x; |
|
|
797 | r[1] = y; |
|
|
798 | r[2] = z; |
|
|
799 | r[3] = v; |
|
|
800 | |
|
|
801 | memcpy (i, r, 16); |
|
|
802 | |
|
|
803 | i[0] = htonl (i[0]); |
|
|
804 | i[1] = htonl (i[1]); |
|
|
805 | i[2] = htonl (i[2]); |
|
|
806 | i[3] = htonl (i[3]); |
|
|
807 | |
|
|
808 | fwrite (i, 4*4, 1, stdout); |
|
|
809 | } |
|
|
810 | } |
|
|
811 | #endif |
|
|
812 | |
|
|
813 | exit (0); |
|
|
814 | } |
|
|