Add definable node_stone to biome API, mgv5, mgv7. Reduce and correct depth of mgv7...
[oweals/minetest.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 "mg_biome.h"
24 #include "mapblock.h"
25 #include "mapnode.h"
26 #include "map.h"
27 #include "content_sao.h"
28 #include "nodedef.h"
29 #include "content_mapnode.h" // For content_mapnode_get_new_name
30 #include "voxelalgorithms.h"
31 #include "profiler.h"
32 #include "settings.h" // For g_settings
33 #include "main.h" // For g_profiler
34 #include "treegen.h"
35 #include "serialization.h"
36 #include "util/serialize.h"
37 #include "filesys.h"
38 #include "log.h"
39
40 const char *GenElementManager::ELEMENT_TITLE = "element";
41
42 FlagDesc flagdesc_mapgen[] = {
43         {"trees",    MG_TREES},
44         {"caves",    MG_CAVES},
45         {"dungeons", MG_DUNGEONS},
46         {"flat",     MG_FLAT},
47         {"light",    MG_LIGHT},
48         {NULL,       0}
49 };
50
51 FlagDesc flagdesc_gennotify[] = {
52         {"dungeon",          1 << GENNOTIFY_DUNGEON},
53         {"temple",           1 << GENNOTIFY_TEMPLE},
54         {"cave_begin",       1 << GENNOTIFY_CAVE_BEGIN},
55         {"cave_end",         1 << GENNOTIFY_CAVE_END},
56         {"large_cave_begin", 1 << GENNOTIFY_LARGECAVE_BEGIN},
57         {"large_cave_end",   1 << GENNOTIFY_LARGECAVE_END},
58         {NULL,               0}
59 };
60
61
62 ///////////////////////////////////////////////////////////////////////////////
63
64
65 Mapgen::Mapgen() {
66         seed        = 0;
67         water_level = 0;
68         generating  = false;
69         id          = -1;
70         vm          = NULL;
71         ndef        = NULL;
72         heightmap   = NULL;
73         biomemap    = NULL;
74
75         for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
76                 gen_notifications[i] = new std::vector<v3s16>;
77 }
78
79
80 Mapgen::~Mapgen() {
81         for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
82                 delete gen_notifications[i];
83 }
84
85
86 // Returns Y one under area minimum if not found
87 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
88         v3s16 em = vm->m_area.getExtent();
89         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
90         s16 y_nodes_min = vm->m_area.MinEdge.Y;
91         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
92         s16 y;
93
94         for (y = y_nodes_max; y >= y_nodes_min; y--) {
95                 MapNode &n = vm->m_data[i];
96                 if (ndef->get(n).walkable)
97                         break;
98
99                 vm->m_area.add_y(em, i, -1);
100         }
101         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
102 }
103
104
105 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
106         v3s16 em = vm->m_area.getExtent();
107         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
108         s16 y;
109
110         for (y = ymax; y >= ymin; y--) {
111                 MapNode &n = vm->m_data[i];
112                 if (ndef->get(n).walkable)
113                         break;
114
115                 vm->m_area.add_y(em, i, -1);
116         }
117         return y;
118 }
119
120
121 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
122         if (!heightmap)
123                 return;
124
125         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
126         int index = 0;
127         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
128                 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
129                         s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
130
131                         // if the values found are out of range, trust the old heightmap
132                         if (y == nmax.Y && heightmap[index] > nmax.Y)
133                                 continue;
134                         if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
135                                 continue;
136
137                         heightmap[index] = y;
138                 }
139         }
140         //printf("updateHeightmap: %dus\n", t.stop());
141 }
142
143
144 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
145         bool isliquid, wasliquid;
146         v3s16 em  = vm->m_area.getExtent();
147
148         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
149                 for (s16 x = nmin.X; x <= nmax.X; x++) {
150                         wasliquid = true;
151
152                         u32 i = vm->m_area.index(x, nmax.Y, z);
153                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
154                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
155
156                                 // there was a change between liquid and nonliquid, add to queue.
157                                 if (isliquid != wasliquid)
158                                         trans_liquid->push_back(v3s16(x, y, z));
159
160                                 wasliquid = isliquid;
161                                 vm->m_area.add_y(em, i, -1);
162                         }
163                 }
164         }
165 }
166
167
168 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
169         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
170         VoxelArea a(nmin, nmax);
171
172         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
173                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
174                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
175                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
176                                 vm->m_data[i].param1 = light;
177                 }
178         }
179 }
180
181
182 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
183         if (light <= 1 || !a.contains(p))
184                 return;
185
186         u32 vi = vm->m_area.index(p);
187         MapNode &nn = vm->m_data[vi];
188
189         light--;
190         // should probably compare masked, but doesn't seem to make a difference
191         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
192                 return;
193
194         nn.param1 = light;
195
196         lightSpread(a, p + v3s16(0, 0, 1), light);
197         lightSpread(a, p + v3s16(0, 1, 0), light);
198         lightSpread(a, p + v3s16(1, 0, 0), light);
199         lightSpread(a, p - v3s16(0, 0, 1), light);
200         lightSpread(a, p - v3s16(0, 1, 0), light);
201         lightSpread(a, p - v3s16(1, 0, 0), light);
202 }
203
204
205 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
206         VoxelArea a(nmin, nmax);
207         bool block_is_underground = (water_level >= nmax.Y);
208
209         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
210         //TimeTaker t("updateLighting");
211
212         // first, send vertical rays of sunshine downward
213         v3s16 em = vm->m_area.getExtent();
214         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
215                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
216                         // see if we can get a light value from the overtop
217                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
218                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
219                                 if (block_is_underground)
220                                         continue;
221                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
222                                 continue;
223                         }
224                         vm->m_area.add_y(em, i, -1);
225
226                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
227                                 MapNode &n = vm->m_data[i];
228                                 if (!ndef->get(n).sunlight_propagates)
229                                         break;
230                                 n.param1 = LIGHT_SUN;
231                                 vm->m_area.add_y(em, i, -1);
232                         }
233                 }
234         }
235
236         // now spread the sunlight and light up any sources
237         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
238                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
239                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
240                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
241                                 MapNode &n = vm->m_data[i];
242                                 if (n.getContent() == CONTENT_IGNORE ||
243                                         !ndef->get(n).light_propagates)
244                                         continue;
245
246                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
247                                 if (light_produced)
248                                         n.param1 = light_produced;
249
250                                 u8 light = n.param1 & 0x0F;
251                                 if (light) {
252                                         lightSpread(a, v3s16(x,     y,     z + 1), light - 1);
253                                         lightSpread(a, v3s16(x,     y + 1, z    ), light - 1);
254                                         lightSpread(a, v3s16(x + 1, y,     z    ), light - 1);
255                                         lightSpread(a, v3s16(x,     y,     z - 1), light - 1);
256                                         lightSpread(a, v3s16(x,     y - 1, z    ), light - 1);
257                                         lightSpread(a, v3s16(x - 1, y,     z    ), light - 1);
258                                 }
259                         }
260                 }
261         }
262
263         //printf("updateLighting: %dms\n", t.stop());
264 }
265
266
267 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
268         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
269         VoxelArea a(nmin, nmax);
270         bool block_is_underground = (water_level > nmax.Y);
271         bool sunlight = !block_is_underground;
272
273         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
274
275         for (int i = 0; i < 2; i++) {
276                 enum LightBank bank = banks[i];
277                 std::set<v3s16> light_sources;
278                 std::map<v3s16, u8> unlight_from;
279
280                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
281                         light_sources, unlight_from);
282                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
283
284                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
285                 vm->spreadLight(bank, light_sources, ndef);
286         }
287 }
288
289
290 ///////////////////////////////////////////////////////////////////////////////
291
292
293 GenElementManager::~GenElementManager()
294 {
295         for (size_t i = 0; i != m_elements.size(); i++)
296                 delete m_elements[i];
297 }
298
299
300 u32 GenElementManager::add(GenElement *elem)
301 {
302         size_t nelem = m_elements.size();
303
304         for (size_t i = 0; i != nelem; i++) {
305                 if (m_elements[i] == NULL) {
306                         elem->id = i;
307                         m_elements[i] = elem;
308                         return i;
309                 }
310         }
311
312         if (nelem >= this->ELEMENT_LIMIT)
313                 return -1;
314
315         elem->id = nelem;
316         m_elements.push_back(elem);
317
318         verbosestream << "GenElementManager: added " << this->ELEMENT_TITLE
319                 << " element '" << elem->name << "'" << std::endl;
320
321         return nelem;
322 }
323
324
325 GenElement *GenElementManager::get(u32 id)
326 {
327         return (id < m_elements.size()) ? m_elements[id] : NULL;
328 }
329
330
331 GenElement *GenElementManager::getByName(const char *name)
332 {
333         for (size_t i = 0; i != m_elements.size(); i++) {
334                 GenElement *elem = m_elements[i];
335                 if (elem && !strcmp(elem->name.c_str(), name))
336                         return elem;
337         }
338
339         return NULL;
340 }
341
342 GenElement *GenElementManager::getByName(std::string &name)
343 {
344         return getByName(name.c_str());
345 }
346
347
348 GenElement *GenElementManager::update(u32 id, GenElement *elem)
349 {
350         if (id >= m_elements.size())
351                 return NULL;
352
353         GenElement *old_elem = m_elements[id];
354         m_elements[id] = elem;
355         return old_elem;
356 }
357
358
359 GenElement *GenElementManager::remove(u32 id)
360 {
361         return update(id, NULL);
362 }