Mapgen: Use getBlockSeed2() for blockseeds (much better uniformity)
[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 "gamedef.h"
24 #include "mg_biome.h"
25 #include "mapblock.h"
26 #include "mapnode.h"
27 #include "map.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "emerge.h"
31 #include "content_mapnode.h" // For content_mapnode_get_new_name
32 #include "voxelalgorithms.h"
33 #include "profiler.h"
34 #include "settings.h" // For g_settings
35 #include "main.h" // For g_profiler
36 #include "treegen.h"
37 #include "serialization.h"
38 #include "util/serialize.h"
39 #include "filesys.h"
40 #include "log.h"
41
42 const char *GenElementManager::ELEMENT_TITLE = "element";
43
44 FlagDesc flagdesc_mapgen[] = {
45         {"trees",    MG_TREES},
46         {"caves",    MG_CAVES},
47         {"dungeons", MG_DUNGEONS},
48         {"flat",     MG_FLAT},
49         {"light",    MG_LIGHT},
50         {NULL,       0}
51 };
52
53 FlagDesc flagdesc_gennotify[] = {
54         {"dungeon",          1 << GENNOTIFY_DUNGEON},
55         {"temple",           1 << GENNOTIFY_TEMPLE},
56         {"cave_begin",       1 << GENNOTIFY_CAVE_BEGIN},
57         {"cave_end",         1 << GENNOTIFY_CAVE_END},
58         {"large_cave_begin", 1 << GENNOTIFY_LARGECAVE_BEGIN},
59         {"large_cave_end",   1 << GENNOTIFY_LARGECAVE_END},
60         {"decoration",       1 << GENNOTIFY_DECORATION},
61         {NULL,               0}
62 };
63
64
65 ///////////////////////////////////////////////////////////////////////////////
66
67 Mapgen::Mapgen()
68 {
69         generating    = false;
70         id            = -1;
71         seed          = 0;
72         water_level   = 0;
73         flags         = 0;
74
75         vm          = NULL;
76         ndef        = NULL;
77         heightmap   = NULL;
78         biomemap    = NULL;
79 }
80
81
82 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
83         gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
84 {
85         generating    = false;
86         id            = mapgenid;
87         seed          = (int)params->seed;
88         water_level   = params->water_level;
89         flags         = params->flags;
90         csize         = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
91
92         vm        = NULL;
93         ndef      = NULL;
94         heightmap = NULL;
95         biomemap  = NULL;
96 }
97
98
99 Mapgen::~Mapgen()
100 {
101 }
102
103
104 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
105 {
106         return (u32)seed   +
107                 p.Z * 38134234 +
108                 p.Y * 42123    +
109                 p.X * 23;
110 }
111
112
113 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
114 {
115         return noise3d(p.X, p.Y, p.Z, seed);
116 }
117
118
119 // Returns Y one under area minimum if not found
120 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
121 {
122         v3s16 em = vm->m_area.getExtent();
123         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
124         s16 y_nodes_min = vm->m_area.MinEdge.Y;
125         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
126         s16 y;
127
128         for (y = y_nodes_max; y >= y_nodes_min; y--) {
129                 MapNode &n = vm->m_data[i];
130                 if (ndef->get(n).walkable)
131                         break;
132
133                 vm->m_area.add_y(em, i, -1);
134         }
135         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
136 }
137
138
139 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
140 {
141         v3s16 em = vm->m_area.getExtent();
142         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
143         s16 y;
144
145         for (y = ymax; y >= ymin; y--) {
146                 MapNode &n = vm->m_data[i];
147                 if (ndef->get(n).walkable)
148                         break;
149
150                 vm->m_area.add_y(em, i, -1);
151         }
152         return y;
153 }
154
155
156 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
157 {
158         if (!heightmap)
159                 return;
160
161         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
162         int index = 0;
163         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
164                 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
165                         s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
166
167                         // if the values found are out of range, trust the old heightmap
168                         if (y == nmax.Y && heightmap[index] > nmax.Y)
169                                 continue;
170                         if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
171                                 continue;
172
173                         heightmap[index] = y;
174                 }
175         }
176         //printf("updateHeightmap: %dus\n", t.stop());
177 }
178
179
180 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
181 {
182         bool isliquid, wasliquid;
183         v3s16 em  = vm->m_area.getExtent();
184
185         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
186                 for (s16 x = nmin.X; x <= nmax.X; x++) {
187                         wasliquid = true;
188
189                         u32 i = vm->m_area.index(x, nmax.Y, z);
190                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
191                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
192
193                                 // there was a change between liquid and nonliquid, add to queue.
194                                 if (isliquid != wasliquid)
195                                         trans_liquid->push_back(v3s16(x, y, z));
196
197                                 wasliquid = isliquid;
198                                 vm->m_area.add_y(em, i, -1);
199                         }
200                 }
201         }
202 }
203
204
205 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light)
206 {
207         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
208         VoxelArea a(nmin, nmax);
209
210         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
211                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
212                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
213                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
214                                 vm->m_data[i].param1 = light;
215                 }
216         }
217 }
218
219
220 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
221 {
222         if (light <= 1 || !a.contains(p))
223                 return;
224
225         u32 vi = vm->m_area.index(p);
226         MapNode &nn = vm->m_data[vi];
227
228         light--;
229         // should probably compare masked, but doesn't seem to make a difference
230         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
231                 return;
232
233         nn.param1 = light;
234
235         lightSpread(a, p + v3s16(0, 0, 1), light);
236         lightSpread(a, p + v3s16(0, 1, 0), light);
237         lightSpread(a, p + v3s16(1, 0, 0), light);
238         lightSpread(a, p - v3s16(0, 0, 1), light);
239         lightSpread(a, p - v3s16(0, 1, 0), light);
240         lightSpread(a, p - v3s16(1, 0, 0), light);
241 }
242
243
244 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
245 {
246         VoxelArea a(nmin, nmax);
247         bool block_is_underground = (water_level >= nmax.Y);
248
249         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
250         //TimeTaker t("updateLighting");
251
252         // first, send vertical rays of sunshine downward
253         v3s16 em = vm->m_area.getExtent();
254         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
255                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
256                         // see if we can get a light value from the overtop
257                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
258                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
259                                 if (block_is_underground)
260                                         continue;
261                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
262                                 continue;
263                         }
264                         vm->m_area.add_y(em, i, -1);
265
266                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
267                                 MapNode &n = vm->m_data[i];
268                                 if (!ndef->get(n).sunlight_propagates)
269                                         break;
270                                 n.param1 = LIGHT_SUN;
271                                 vm->m_area.add_y(em, i, -1);
272                         }
273                 }
274         }
275
276         // now spread the sunlight and light up any sources
277         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
278                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
279                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
280                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
281                                 MapNode &n = vm->m_data[i];
282                                 if (n.getContent() == CONTENT_IGNORE ||
283                                         !ndef->get(n).light_propagates)
284                                         continue;
285
286                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
287                                 if (light_produced)
288                                         n.param1 = light_produced;
289
290                                 u8 light = n.param1 & 0x0F;
291                                 if (light) {
292                                         lightSpread(a, v3s16(x,     y,     z + 1), light - 1);
293                                         lightSpread(a, v3s16(x,     y + 1, z    ), light - 1);
294                                         lightSpread(a, v3s16(x + 1, y,     z    ), light - 1);
295                                         lightSpread(a, v3s16(x,     y,     z - 1), light - 1);
296                                         lightSpread(a, v3s16(x,     y - 1, z    ), light - 1);
297                                         lightSpread(a, v3s16(x - 1, y,     z    ), light - 1);
298                                 }
299                         }
300                 }
301         }
302
303         //printf("updateLighting: %dms\n", t.stop());
304 }
305
306
307 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax)
308 {
309         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
310         VoxelArea a(nmin, nmax);
311         bool block_is_underground = (water_level > nmax.Y);
312         bool sunlight = !block_is_underground;
313
314         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
315
316         for (int i = 0; i < 2; i++) {
317                 enum LightBank bank = banks[i];
318                 std::set<v3s16> light_sources;
319                 std::map<v3s16, u8> unlight_from;
320
321                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
322                         light_sources, unlight_from);
323                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
324
325                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
326                 vm->spreadLight(bank, light_sources, ndef);
327         }
328 }
329
330
331 ///////////////////////////////////////////////////////////////////////////////
332
333 GenerateNotifier::GenerateNotifier()
334 {
335 }
336
337
338 GenerateNotifier::GenerateNotifier(u32 notify_on,
339         std::set<u32> *notify_on_deco_ids)
340 {
341         m_notify_on = notify_on;
342         m_notify_on_deco_ids = notify_on_deco_ids;
343 }
344
345
346 void GenerateNotifier::setNotifyOn(u32 notify_on)
347 {
348         m_notify_on = notify_on;
349 }
350
351
352 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
353 {
354         m_notify_on_deco_ids = notify_on_deco_ids;
355 }
356
357
358 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
359 {
360         if (!(m_notify_on & (1 << type)))
361                 return false;
362
363         if (type == GENNOTIFY_DECORATION &&
364                 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
365                 return false;
366
367         GenNotifyEvent gne;
368         gne.type = type;
369         gne.pos  = pos;
370         gne.id   = id;
371         m_notify_events.push_back(gne);
372
373         return true;
374 }
375
376
377 void GenerateNotifier::getEvents(
378         std::map<std::string, std::vector<v3s16> > &event_map,
379         bool peek_events)
380 {
381         std::list<GenNotifyEvent>::iterator it;
382
383         for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
384                 GenNotifyEvent &gn = *it;
385                 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
386                         "decoration#"+ itos(gn.id) :
387                         flagdesc_gennotify[gn.type].name;
388
389                 event_map[name].push_back(gn.pos);
390         }
391
392         if (!peek_events)
393                 m_notify_events.clear();
394 }
395
396
397 ///////////////////////////////////////////////////////////////////////////////
398
399
400 GenElementManager::GenElementManager(IGameDef *gamedef)
401 {
402         m_ndef = gamedef->getNodeDefManager();
403 }
404
405
406 GenElementManager::~GenElementManager()
407 {
408         for (size_t i = 0; i != m_elements.size(); i++)
409                 delete m_elements[i];
410 }
411
412
413 u32 GenElementManager::add(GenElement *elem)
414 {
415         size_t nelem = m_elements.size();
416
417         for (size_t i = 0; i != nelem; i++) {
418                 if (m_elements[i] == NULL) {
419                         elem->id = i;
420                         m_elements[i] = elem;
421                         return i;
422                 }
423         }
424
425         if (nelem >= this->ELEMENT_LIMIT)
426                 return -1;
427
428         elem->id = nelem;
429         m_elements.push_back(elem);
430
431         verbosestream << "GenElementManager: added " << this->ELEMENT_TITLE
432                 << " element '" << elem->name << "'" << std::endl;
433
434         return nelem;
435 }
436
437
438 GenElement *GenElementManager::get(u32 id)
439 {
440         return (id < m_elements.size()) ? m_elements[id] : NULL;
441 }
442
443
444 GenElement *GenElementManager::getByName(const std::string &name)
445 {
446         for (size_t i = 0; i != m_elements.size(); i++) {
447                 GenElement *elem = m_elements[i];
448                 if (elem && name == elem->name)
449                         return elem;
450         }
451
452         return NULL;
453 }
454
455
456 GenElement *GenElementManager::update(u32 id, GenElement *elem)
457 {
458         if (id >= m_elements.size())
459                 return NULL;
460
461         GenElement *old_elem = m_elements[id];
462         m_elements[id] = elem;
463         return old_elem;
464 }
465
466
467 GenElement *GenElementManager::remove(u32 id)
468 {
469         return update(id, NULL);
470 }
471
472
473 void GenElementManager::clear()
474 {
475         m_elements.clear();
476 }