Various style cleanups + unused code removal
[oweals/minetest.git] / src / mapgen.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "mapgen.h"
22 #include "voxel.h"
23 #include "noise.h"
24 #include "gamedef.h"
25 #include "mg_biome.h"
26 #include "mapblock.h"
27 #include "mapnode.h"
28 #include "map.h"
29 #include "content_sao.h"
30 #include "nodedef.h"
31 #include "emerge.h"
32 #include "voxelalgorithms.h"
33 #include "porting.h"
34 #include "profiler.h"
35 #include "settings.h"
36 #include "treegen.h"
37 #include "serialization.h"
38 #include "util/serialize.h"
39 #include "util/numeric.h"
40 #include "filesys.h"
41 #include "log.h"
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
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         heatmap   = NULL;
80         humidmap  = NULL;
81 }
82
83
84 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
85         gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
86 {
87         generating  = false;
88         id          = mapgenid;
89         seed        = (int)params->seed;
90         water_level = params->water_level;
91         flags       = params->flags;
92         csize       = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
93
94         vm        = NULL;
95         ndef      = NULL;
96         heightmap = NULL;
97         biomemap  = NULL;
98         heatmap   = NULL;
99         humidmap  = NULL;
100 }
101
102
103 Mapgen::~Mapgen()
104 {
105 }
106
107
108 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
109 {
110         return (u32)seed   +
111                 p.Z * 38134234 +
112                 p.Y * 42123    +
113                 p.X * 23;
114 }
115
116
117 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
118 {
119         u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
120         n = (n >> 13) ^ n;
121         return (n * (n * n * 60493 + 19990303) + 1376312589);
122 }
123
124
125 // Returns Y one under area minimum if not found
126 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
127 {
128         v3s16 em = vm->m_area.getExtent();
129         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
130         s16 y_nodes_min = vm->m_area.MinEdge.Y;
131         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
132         s16 y;
133
134         for (y = y_nodes_max; y >= y_nodes_min; y--) {
135                 MapNode &n = vm->m_data[i];
136                 if (ndef->get(n).walkable)
137                         break;
138
139                 vm->m_area.add_y(em, i, -1);
140         }
141         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
142 }
143
144
145 // Returns -MAX_MAP_GENERATION_LIMIT if not found
146 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
147 {
148         v3s16 em = vm->m_area.getExtent();
149         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
150         s16 y;
151
152         for (y = ymax; y >= ymin; y--) {
153                 MapNode &n = vm->m_data[i];
154                 if (ndef->get(n).walkable)
155                         break;
156
157                 vm->m_area.add_y(em, i, -1);
158         }
159         return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
160 }
161
162
163 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
164 {
165         if (!heightmap)
166                 return;
167
168         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
169         int index = 0;
170         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
171                 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
172                         s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
173
174                         heightmap[index] = y;
175                 }
176         }
177         //printf("updateHeightmap: %dus\n", t.stop());
178 }
179
180
181 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
182 {
183         bool isliquid, wasliquid;
184         v3s16 em  = vm->m_area.getExtent();
185
186         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
187                 for (s16 x = nmin.X; x <= nmax.X; x++) {
188                         wasliquid = true;
189
190                         u32 i = vm->m_area.index(x, nmax.Y, z);
191                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
192                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
193
194                                 // there was a change between liquid and nonliquid, add to queue.
195                                 if (isliquid != wasliquid)
196                                         trans_liquid->push_back(v3s16(x, y, z));
197
198                                 wasliquid = isliquid;
199                                 vm->m_area.add_y(em, i, -1);
200                         }
201                 }
202         }
203 }
204
205
206 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
207 {
208         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
209         VoxelArea a(nmin, nmax);
210
211         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
212                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
213                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
214                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
215                                 vm->m_data[i].param1 = light;
216                 }
217         }
218 }
219
220
221 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
222 {
223         if (light <= 1 || !a.contains(p))
224                 return;
225
226         u32 vi = vm->m_area.index(p);
227         MapNode &nn = vm->m_data[vi];
228
229         light--;
230         // should probably compare masked, but doesn't seem to make a difference
231         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
232                 return;
233
234         nn.param1 = light;
235
236         lightSpread(a, p + v3s16(0, 0, 1), light);
237         lightSpread(a, p + v3s16(0, 1, 0), light);
238         lightSpread(a, p + v3s16(1, 0, 0), light);
239         lightSpread(a, p - v3s16(0, 0, 1), light);
240         lightSpread(a, p - v3s16(0, 1, 0), light);
241         lightSpread(a, p - v3s16(1, 0, 0), light);
242 }
243
244
245 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax)
246 {
247         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
248         //TimeTaker t("updateLighting");
249
250         propagateSunlight(nmin, nmax);
251         spreadLight(full_nmin, full_nmax);
252
253         //printf("updateLighting: %dms\n", t.stop());
254 }
255
256
257
258 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
259 {
260         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
261         //TimeTaker t("updateLighting");
262
263         propagateSunlight(
264                 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
265                 nmax + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
266
267         spreadLight(
268                 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
269                 nmax + v3s16(1, 1, 1) * MAP_BLOCKSIZE);
270
271         //printf("updateLighting: %dms\n", t.stop());
272 }
273
274
275 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax)
276 {
277         //TimeTaker t("propagateSunlight");
278         VoxelArea a(nmin, nmax);
279         bool block_is_underground = (water_level >= nmax.Y);
280         v3s16 em = vm->m_area.getExtent();
281
282         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
283                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
284                         // see if we can get a light value from the overtop
285                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
286                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
287                                 if (block_is_underground)
288                                         continue;
289                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
290                                 continue;
291                         }
292                         vm->m_area.add_y(em, i, -1);
293
294                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
295                                 MapNode &n = vm->m_data[i];
296                                 if (!ndef->get(n).sunlight_propagates)
297                                         break;
298                                 n.param1 = LIGHT_SUN;
299                                 vm->m_area.add_y(em, i, -1);
300                         }
301                 }
302         }
303         //printf("propagateSunlight: %dms\n", t.stop());
304 }
305
306
307
308 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
309 {
310         //TimeTaker t("spreadLight");
311         VoxelArea a(nmin, nmax);
312
313         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
314                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
315                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
316                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
317                                 MapNode &n = vm->m_data[i];
318                                 if (n.getContent() == CONTENT_IGNORE ||
319                                         !ndef->get(n).light_propagates)
320                                         continue;
321
322                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
323                                 if (light_produced)
324                                         n.param1 = light_produced;
325
326                                 u8 light = n.param1 & 0x0F;
327                                 if (light) {
328                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
329                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
330                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
331                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
332                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
333                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
334                                 }
335                         }
336                 }
337         }
338
339         //printf("spreadLight: %dms\n", t.stop());
340 }
341
342
343
344 ///////////////////////////////////////////////////////////////////////////////
345
346 GenerateNotifier::GenerateNotifier()
347 {
348         m_notify_on = 0;
349 }
350
351
352 GenerateNotifier::GenerateNotifier(u32 notify_on,
353         std::set<u32> *notify_on_deco_ids)
354 {
355         m_notify_on = notify_on;
356         m_notify_on_deco_ids = notify_on_deco_ids;
357 }
358
359
360 void GenerateNotifier::setNotifyOn(u32 notify_on)
361 {
362         m_notify_on = notify_on;
363 }
364
365
366 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
367 {
368         m_notify_on_deco_ids = notify_on_deco_ids;
369 }
370
371
372 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
373 {
374         if (!(m_notify_on & (1 << type)))
375                 return false;
376
377         if (type == GENNOTIFY_DECORATION &&
378                 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
379                 return false;
380
381         GenNotifyEvent gne;
382         gne.type = type;
383         gne.pos  = pos;
384         gne.id   = id;
385         m_notify_events.push_back(gne);
386
387         return true;
388 }
389
390
391 void GenerateNotifier::getEvents(
392         std::map<std::string, std::vector<v3s16> > &event_map,
393         bool peek_events)
394 {
395         std::list<GenNotifyEvent>::iterator it;
396
397         for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
398                 GenNotifyEvent &gn = *it;
399                 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
400                         "decoration#"+ itos(gn.id) :
401                         flagdesc_gennotify[gn.type].name;
402
403                 event_map[name].push_back(gn.pos);
404         }
405
406         if (!peek_events)
407                 m_notify_events.clear();
408 }
409
410 ///////////////////////////////////////////////////////////////////////////////
411
412 void MapgenParams::load(const Settings &settings)
413 {
414         std::string seed_str;
415         const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
416
417         if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty())
418                 seed = read_seed(seed_str.c_str());
419         else
420                 myrand_bytes(&seed, sizeof(seed));
421
422         settings.getNoEx("mg_name", mg_name);
423         settings.getS16NoEx("water_level", water_level);
424         settings.getS16NoEx("chunksize", chunksize);
425         settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen);
426         settings.getNoiseParams("mg_biome_np_heat", np_biome_heat);
427         settings.getNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend);
428         settings.getNoiseParams("mg_biome_np_humidity", np_biome_humidity);
429         settings.getNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend);
430
431         delete sparams;
432         sparams = EmergeManager::createMapgenParams(mg_name);
433         if (sparams)
434                 sparams->readParams(&settings);
435 }
436
437
438 void MapgenParams::save(Settings &settings) const
439 {
440         settings.set("mg_name", mg_name);
441         settings.setU64("seed", seed);
442         settings.setS16("water_level", water_level);
443         settings.setS16("chunksize", chunksize);
444         settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, (u32)-1);
445         settings.setNoiseParams("mg_biome_np_heat", np_biome_heat);
446         settings.setNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend);
447         settings.setNoiseParams("mg_biome_np_humidity", np_biome_humidity);
448         settings.setNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend);
449
450         if (sparams)
451                 sparams->writeParams(&settings);
452 }