]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen.cpp
Clean up Mapgen
[dragonfireclient.git] / src / mapgen.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "mapgen.h"
21 #include "voxel.h"
22 #include "noise.h"
23 #include "biome.h"
24 #include "mapblock.h"
25 #include "mapnode.h"
26 #include "map.h"
27 //#include "serverobject.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
32 #include "profiler.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
35 #include "treegen.h"
36 #include "mapgen_v6.h"
37
38 FlagDesc flagdesc_mapgen[] = {
39         {"trees",          MG_TREES},
40         {"caves",          MG_CAVES},
41         {"dungeons",       MG_DUNGEONS},
42         {"v6_forests",     MGV6_FORESTS},
43         {"v6_biome_blend", MGV6_BIOME_BLEND},
44         {"flat",           MG_FLAT},
45         {NULL,                     0}
46 };
47
48
49 ///////////////////////////////////////////////////////////////////////////////
50
51
52 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
53         bool isliquid, wasliquid;
54         u32 i;
55
56         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
57                 for (s16 x = nmin.X; x <= nmax.X; x++) {
58                         v2s16 p2d(x, z);
59                         wasliquid = true;
60                         v3s16 em  = vm->m_area.getExtent();
61                         i = vm->m_area.index(v3s16(p2d.X, nmax.Y, p2d.Y));
62
63                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
64                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
65                                 
66                                 // there was a change between liquid and nonliquid, add to queue
67                                 if (isliquid != wasliquid)
68                                         trans_liquid->push_back(v3s16(p2d.X, y, p2d.Y));
69
70                                 wasliquid = isliquid;
71                                 vm->m_area.add_y(em, i, -1);
72                         }
73                 }
74         }
75 }
76
77
78 void Mapgen::updateLighting(v3s16 nmin, v3s16 nmax) {
79         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
80
81         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
82                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
83         bool block_is_underground = (water_level > nmax.Y);
84         bool sunlight = !block_is_underground;
85
86         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
87         for (int i = 0; i < 2; i++) {
88                 enum LightBank bank = banks[i];
89         std::set<v3s16> light_sources;
90         std::map<v3s16, u8> unlight_from;
91
92                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
93                                         light_sources, unlight_from);
94                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
95                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
96                 vm->spreadLight(bank, light_sources, ndef);
97         }
98 }
99
100
101 //////////////////////// Mapgen V6 parameter read/write
102
103 bool MapgenV6Params::readParams(Settings *settings) {
104         freq_desert = settings->getFloat("mgv6_freq_desert");
105         freq_beach  = settings->getFloat("mgv6_freq_beach");
106
107         np_terrain_base   = settings->getNoiseParams("mgv6_np_terrain_base");
108         np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
109         np_steepness      = settings->getNoiseParams("mgv6_np_steepness");
110         np_height_select  = settings->getNoiseParams("mgv6_np_height_select");
111         np_trees          = settings->getNoiseParams("mgv6_np_trees");
112         np_mud            = settings->getNoiseParams("mgv6_np_mud");
113         np_beach          = settings->getNoiseParams("mgv6_np_beach");
114         np_biome          = settings->getNoiseParams("mgv6_np_biome");
115         np_cave           = settings->getNoiseParams("mgv6_np_cave");
116
117         bool success =
118                 np_terrain_base  && np_terrain_higher && np_steepness &&
119                 np_height_select && np_trees          && np_mud       &&
120                 np_beach         && np_biome          && np_cave;
121         return success;
122 }
123
124
125 void MapgenV6Params::writeParams(Settings *settings) {
126         settings->setFloat("mgv6_freq_desert", freq_desert);
127         settings->setFloat("mgv6_freq_beach",  freq_beach);
128         
129         settings->setNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
130         settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
131         settings->setNoiseParams("mgv6_np_steepness",      np_steepness);
132         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
133         settings->setNoiseParams("mgv6_np_trees",          np_trees);
134         settings->setNoiseParams("mgv6_np_mud",            np_mud);
135         settings->setNoiseParams("mgv6_np_beach",          np_beach);
136         settings->setNoiseParams("mgv6_np_biome",          np_biome);
137         settings->setNoiseParams("mgv6_np_cave",           np_cave);
138 }
139
140
141 /////////////////////////////////// legacy static functions for farmesh
142
143
144 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
145         //just need to return something
146         s16 level = 5;
147         return level;
148 }
149
150
151 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
152         double sandnoise = noise2d_perlin(
153                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
154                         seed+59420, 3, 0.50);
155
156         return (sandnoise > 0.15);
157 }
158
159
160 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
161         double noise = noise2d_perlin(
162                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
163                         seed+2, 4, 0.66);
164         double zeroval = -0.39;
165         if(noise < zeroval)
166                 return 0;
167         else
168                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
169 }