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