]> git.lizzy.rs Git - minetest.git/blob - src/mapgen.cpp
Fixes #1687 by extra semaphore retval handle code for OSX
[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 // Returns Y one under area minimum if not found
105 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
106 {
107         v3s16 em = vm->m_area.getExtent();
108         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
109         s16 y_nodes_min = vm->m_area.MinEdge.Y;
110         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
111         s16 y;
112
113         for (y = y_nodes_max; y >= y_nodes_min; y--) {
114                 MapNode &n = vm->m_data[i];
115                 if (ndef->get(n).walkable)
116                         break;
117
118                 vm->m_area.add_y(em, i, -1);
119         }
120         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
121 }
122
123
124 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
125 {
126         v3s16 em = vm->m_area.getExtent();
127         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
128         s16 y;
129
130         for (y = ymax; y >= ymin; y--) {
131                 MapNode &n = vm->m_data[i];
132                 if (ndef->get(n).walkable)
133                         break;
134
135                 vm->m_area.add_y(em, i, -1);
136         }
137         return y;
138 }
139
140
141 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
142 {
143         if (!heightmap)
144                 return;
145
146         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
147         int index = 0;
148         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
149                 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
150                         s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
151
152                         // if the values found are out of range, trust the old heightmap
153                         if (y == nmax.Y && heightmap[index] > nmax.Y)
154                                 continue;
155                         if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
156                                 continue;
157
158                         heightmap[index] = y;
159                 }
160         }
161         //printf("updateHeightmap: %dus\n", t.stop());
162 }
163
164
165 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
166 {
167         bool isliquid, wasliquid;
168         v3s16 em  = vm->m_area.getExtent();
169
170         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
171                 for (s16 x = nmin.X; x <= nmax.X; x++) {
172                         wasliquid = true;
173
174                         u32 i = vm->m_area.index(x, nmax.Y, z);
175                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
176                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
177
178                                 // there was a change between liquid and nonliquid, add to queue.
179                                 if (isliquid != wasliquid)
180                                         trans_liquid->push_back(v3s16(x, y, z));
181
182                                 wasliquid = isliquid;
183                                 vm->m_area.add_y(em, i, -1);
184                         }
185                 }
186         }
187 }
188
189
190 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light)
191 {
192         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
193         VoxelArea a(nmin, nmax);
194
195         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
196                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
197                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
198                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
199                                 vm->m_data[i].param1 = light;
200                 }
201         }
202 }
203
204
205 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
206 {
207         if (light <= 1 || !a.contains(p))
208                 return;
209
210         u32 vi = vm->m_area.index(p);
211         MapNode &nn = vm->m_data[vi];
212
213         light--;
214         // should probably compare masked, but doesn't seem to make a difference
215         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
216                 return;
217
218         nn.param1 = light;
219
220         lightSpread(a, p + v3s16(0, 0, 1), light);
221         lightSpread(a, p + v3s16(0, 1, 0), light);
222         lightSpread(a, p + v3s16(1, 0, 0), light);
223         lightSpread(a, p - v3s16(0, 0, 1), light);
224         lightSpread(a, p - v3s16(0, 1, 0), light);
225         lightSpread(a, p - v3s16(1, 0, 0), light);
226 }
227
228
229 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
230 {
231         VoxelArea a(nmin, nmax);
232         bool block_is_underground = (water_level >= nmax.Y);
233
234         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
235         //TimeTaker t("updateLighting");
236
237         // first, send vertical rays of sunshine downward
238         v3s16 em = vm->m_area.getExtent();
239         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
240                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
241                         // see if we can get a light value from the overtop
242                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
243                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
244                                 if (block_is_underground)
245                                         continue;
246                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
247                                 continue;
248                         }
249                         vm->m_area.add_y(em, i, -1);
250
251                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
252                                 MapNode &n = vm->m_data[i];
253                                 if (!ndef->get(n).sunlight_propagates)
254                                         break;
255                                 n.param1 = LIGHT_SUN;
256                                 vm->m_area.add_y(em, i, -1);
257                         }
258                 }
259         }
260
261         // now spread the sunlight and light up any sources
262         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
263                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
264                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
265                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
266                                 MapNode &n = vm->m_data[i];
267                                 if (n.getContent() == CONTENT_IGNORE ||
268                                         !ndef->get(n).light_propagates)
269                                         continue;
270
271                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
272                                 if (light_produced)
273                                         n.param1 = light_produced;
274
275                                 u8 light = n.param1 & 0x0F;
276                                 if (light) {
277                                         lightSpread(a, v3s16(x,     y,     z + 1), light - 1);
278                                         lightSpread(a, v3s16(x,     y + 1, z    ), light - 1);
279                                         lightSpread(a, v3s16(x + 1, y,     z    ), light - 1);
280                                         lightSpread(a, v3s16(x,     y,     z - 1), light - 1);
281                                         lightSpread(a, v3s16(x,     y - 1, z    ), light - 1);
282                                         lightSpread(a, v3s16(x - 1, y,     z    ), light - 1);
283                                 }
284                         }
285                 }
286         }
287
288         //printf("updateLighting: %dms\n", t.stop());
289 }
290
291
292 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax)
293 {
294         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
295         VoxelArea a(nmin, nmax);
296         bool block_is_underground = (water_level > nmax.Y);
297         bool sunlight = !block_is_underground;
298
299         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
300
301         for (int i = 0; i < 2; i++) {
302                 enum LightBank bank = banks[i];
303                 std::set<v3s16> light_sources;
304                 std::map<v3s16, u8> unlight_from;
305
306                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
307                         light_sources, unlight_from);
308                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
309
310                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
311                 vm->spreadLight(bank, light_sources, ndef);
312         }
313 }
314
315
316 ///////////////////////////////////////////////////////////////////////////////
317
318 GenerateNotifier::GenerateNotifier()
319 {
320 }
321
322
323 GenerateNotifier::GenerateNotifier(u32 notify_on,
324         std::set<u32> *notify_on_deco_ids)
325 {
326         m_notify_on = notify_on;
327         m_notify_on_deco_ids = notify_on_deco_ids;
328 }
329
330
331 void GenerateNotifier::setNotifyOn(u32 notify_on)
332 {
333         m_notify_on = notify_on;
334 }
335
336
337 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
338 {
339         m_notify_on_deco_ids = notify_on_deco_ids;
340 }
341
342
343 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
344 {
345         if (!(m_notify_on & (1 << type)))
346                 return false;
347
348         if (type == GENNOTIFY_DECORATION &&
349                 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
350                 return false;
351
352         GenNotifyEvent gne;
353         gne.type = type;
354         gne.pos  = pos;
355         gne.id   = id;
356         m_notify_events.push_back(gne);
357
358         return true;
359 }
360
361
362 void GenerateNotifier::getEvents(
363         std::map<std::string, std::vector<v3s16> > &event_map,
364         bool peek_events)
365 {
366         std::list<GenNotifyEvent>::iterator it;
367
368         for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
369                 GenNotifyEvent &gn = *it;
370                 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
371                         "decoration#"+ itos(gn.id) :
372                         flagdesc_gennotify[gn.type].name;
373
374                 event_map[name].push_back(gn.pos);
375         }
376
377         if (!peek_events)
378                 m_notify_events.clear();
379 }
380
381
382 ///////////////////////////////////////////////////////////////////////////////
383
384
385 GenElementManager::GenElementManager(IGameDef *gamedef)
386 {
387         m_resolver = gamedef->getNodeDefManager()->getResolver();
388 }
389
390
391 GenElementManager::~GenElementManager()
392 {
393         for (size_t i = 0; i != m_elements.size(); i++)
394                 delete m_elements[i];
395 }
396
397
398 u32 GenElementManager::add(GenElement *elem)
399 {
400         size_t nelem = m_elements.size();
401
402         for (size_t i = 0; i != nelem; i++) {
403                 if (m_elements[i] == NULL) {
404                         elem->id = i;
405                         m_elements[i] = elem;
406                         return i;
407                 }
408         }
409
410         if (nelem >= this->ELEMENT_LIMIT)
411                 return -1;
412
413         elem->id = nelem;
414         m_elements.push_back(elem);
415
416         verbosestream << "GenElementManager: added " << this->ELEMENT_TITLE
417                 << " element '" << elem->name << "'" << std::endl;
418
419         return nelem;
420 }
421
422
423 GenElement *GenElementManager::get(u32 id)
424 {
425         return (id < m_elements.size()) ? m_elements[id] : NULL;
426 }
427
428
429 GenElement *GenElementManager::getByName(const std::string &name)
430 {
431         for (size_t i = 0; i != m_elements.size(); i++) {
432                 GenElement *elem = m_elements[i];
433                 if (elem && name == elem->name)
434                         return elem;
435         }
436
437         return NULL;
438 }
439
440
441 GenElement *GenElementManager::update(u32 id, GenElement *elem)
442 {
443         if (id >= m_elements.size())
444                 return NULL;
445
446         GenElement *old_elem = m_elements[id];
447         m_elements[id] = elem;
448         return old_elem;
449 }
450
451
452 GenElement *GenElementManager::remove(u32 id)
453 {
454         return update(id, NULL);
455 }
456
457
458 void GenElementManager::clear()
459 {
460         m_elements.clear();
461 }