]> git.lizzy.rs Git - minetest.git/blob - src/mapgen.cpp
WoW-style Autorun
[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 //// Mapgen
66 ////
67
68 Mapgen::Mapgen()
69 {
70         generating  = false;
71         id          = -1;
72         seed        = 0;
73         water_level = 0;
74         flags       = 0;
75
76         vm        = NULL;
77         ndef      = NULL;
78         heightmap = NULL;
79         biomemap  = NULL;
80         heatmap   = NULL;
81         humidmap  = NULL;
82 }
83
84
85 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
86         gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
87 {
88         generating  = false;
89         id          = mapgenid;
90         seed        = (int)params->seed;
91         water_level = params->water_level;
92         flags       = params->flags;
93         csize       = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
94
95         vm        = NULL;
96         ndef      = NULL;
97         heightmap = NULL;
98         biomemap  = NULL;
99         heatmap   = NULL;
100         humidmap  = NULL;
101 }
102
103
104 Mapgen::~Mapgen()
105 {
106 }
107
108
109 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
110 {
111         return (u32)seed   +
112                 p.Z * 38134234 +
113                 p.Y * 42123    +
114                 p.X * 23;
115 }
116
117
118 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
119 {
120         u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
121         n = (n >> 13) ^ n;
122         return (n * (n * n * 60493 + 19990303) + 1376312589);
123 }
124
125
126 // Returns Y one under area minimum if not found
127 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
128 {
129         v3s16 em = vm->m_area.getExtent();
130         s16 y_nodes_max = vm->m_area.MaxEdge.Y;
131         s16 y_nodes_min = vm->m_area.MinEdge.Y;
132         u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
133         s16 y;
134
135         for (y = y_nodes_max; y >= y_nodes_min; y--) {
136                 MapNode &n = vm->m_data[i];
137                 if (ndef->get(n).walkable)
138                         break;
139
140                 vm->m_area.add_y(em, i, -1);
141         }
142         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
143 }
144
145
146 // Returns -MAX_MAP_GENERATION_LIMIT if not found
147 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
148 {
149         v3s16 em = vm->m_area.getExtent();
150         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
151         s16 y;
152
153         for (y = ymax; y >= ymin; y--) {
154                 MapNode &n = vm->m_data[i];
155                 if (ndef->get(n).walkable)
156                         break;
157
158                 vm->m_area.add_y(em, i, -1);
159         }
160         return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
161 }
162
163
164 // Returns -MAX_MAP_GENERATION_LIMIT if not found or if ground is found first
165 s16 Mapgen::findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax)
166 {
167         v3s16 em = vm->m_area.getExtent();
168         u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
169         s16 y;
170
171         for (y = ymax; y >= ymin; y--) {
172                 MapNode &n = vm->m_data[i];
173                 if (ndef->get(n).walkable)
174                         return -MAX_MAP_GENERATION_LIMIT;
175                 else if (ndef->get(n).isLiquid())
176                         break;
177
178                 vm->m_area.add_y(em, i, -1);
179         }
180         return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
181 }
182
183
184 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
185 {
186         if (!heightmap)
187                 return;
188
189         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
190         int index = 0;
191         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
192                 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
193                         s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
194
195                         heightmap[index] = y;
196                 }
197         }
198         //printf("updateHeightmap: %dus\n", t.stop());
199 }
200
201
202 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
203 {
204         bool isliquid, wasliquid;
205         v3s16 em  = vm->m_area.getExtent();
206
207         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
208                 for (s16 x = nmin.X; x <= nmax.X; x++) {
209                         wasliquid = true;
210
211                         u32 i = vm->m_area.index(x, nmax.Y, z);
212                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
213                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
214
215                                 // there was a change between liquid and nonliquid, add to queue.
216                                 if (isliquid != wasliquid)
217                                         trans_liquid->push_back(v3s16(x, y, z));
218
219                                 wasliquid = isliquid;
220                                 vm->m_area.add_y(em, i, -1);
221                         }
222                 }
223         }
224 }
225
226
227 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
228 {
229         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
230         VoxelArea a(nmin, nmax);
231
232         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
233                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
234                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
235                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
236                                 vm->m_data[i].param1 = light;
237                 }
238         }
239 }
240
241
242 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
243 {
244         if (light <= 1 || !a.contains(p))
245                 return;
246
247         u32 vi = vm->m_area.index(p);
248         MapNode &nn = vm->m_data[vi];
249
250         light--;
251         // should probably compare masked, but doesn't seem to make a difference
252         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
253                 return;
254
255         nn.param1 = light;
256
257         lightSpread(a, p + v3s16(0, 0, 1), light);
258         lightSpread(a, p + v3s16(0, 1, 0), light);
259         lightSpread(a, p + v3s16(1, 0, 0), light);
260         lightSpread(a, p - v3s16(0, 0, 1), light);
261         lightSpread(a, p - v3s16(0, 1, 0), light);
262         lightSpread(a, p - v3s16(1, 0, 0), light);
263 }
264
265
266 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax)
267 {
268         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
269         //TimeTaker t("updateLighting");
270
271         propagateSunlight(nmin, nmax);
272         spreadLight(full_nmin, full_nmax);
273
274         //printf("updateLighting: %dms\n", t.stop());
275 }
276
277
278
279 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
280 {
281         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
282         //TimeTaker t("updateLighting");
283
284         propagateSunlight(
285                 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
286                 nmax + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
287
288         spreadLight(
289                 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
290                 nmax + v3s16(1, 1, 1) * MAP_BLOCKSIZE);
291
292         //printf("updateLighting: %dms\n", t.stop());
293 }
294
295
296 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax)
297 {
298         //TimeTaker t("propagateSunlight");
299         VoxelArea a(nmin, nmax);
300         bool block_is_underground = (water_level >= nmax.Y);
301         v3s16 em = vm->m_area.getExtent();
302
303         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
304                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
305                         // see if we can get a light value from the overtop
306                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
307                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
308                                 if (block_is_underground)
309                                         continue;
310                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
311                                 continue;
312                         }
313                         vm->m_area.add_y(em, i, -1);
314
315                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
316                                 MapNode &n = vm->m_data[i];
317                                 if (!ndef->get(n).sunlight_propagates)
318                                         break;
319                                 n.param1 = LIGHT_SUN;
320                                 vm->m_area.add_y(em, i, -1);
321                         }
322                 }
323         }
324         //printf("propagateSunlight: %dms\n", t.stop());
325 }
326
327
328
329 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
330 {
331         //TimeTaker t("spreadLight");
332         VoxelArea a(nmin, nmax);
333
334         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
335                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
336                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
337                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
338                                 MapNode &n = vm->m_data[i];
339                                 if (n.getContent() == CONTENT_IGNORE ||
340                                         !ndef->get(n).light_propagates)
341                                         continue;
342
343                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
344                                 if (light_produced)
345                                         n.param1 = light_produced;
346
347                                 u8 light = n.param1 & 0x0F;
348                                 if (light) {
349                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
350                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
351                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
352                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
353                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
354                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
355                                 }
356                         }
357                 }
358         }
359
360         //printf("spreadLight: %dms\n", t.stop());
361 }
362
363
364 ////
365 //// GenerateNotifier
366 ////
367
368 GenerateNotifier::GenerateNotifier()
369 {
370         m_notify_on = 0;
371 }
372
373
374 GenerateNotifier::GenerateNotifier(u32 notify_on,
375         std::set<u32> *notify_on_deco_ids)
376 {
377         m_notify_on = notify_on;
378         m_notify_on_deco_ids = notify_on_deco_ids;
379 }
380
381
382 void GenerateNotifier::setNotifyOn(u32 notify_on)
383 {
384         m_notify_on = notify_on;
385 }
386
387
388 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
389 {
390         m_notify_on_deco_ids = notify_on_deco_ids;
391 }
392
393
394 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
395 {
396         if (!(m_notify_on & (1 << type)))
397                 return false;
398
399         if (type == GENNOTIFY_DECORATION &&
400                 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
401                 return false;
402
403         GenNotifyEvent gne;
404         gne.type = type;
405         gne.pos  = pos;
406         gne.id   = id;
407         m_notify_events.push_back(gne);
408
409         return true;
410 }
411
412
413 void GenerateNotifier::getEvents(
414         std::map<std::string, std::vector<v3s16> > &event_map,
415         bool peek_events)
416 {
417         std::list<GenNotifyEvent>::iterator it;
418
419         for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
420                 GenNotifyEvent &gn = *it;
421                 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
422                         "decoration#"+ itos(gn.id) :
423                         flagdesc_gennotify[gn.type].name;
424
425                 event_map[name].push_back(gn.pos);
426         }
427
428         if (!peek_events)
429                 m_notify_events.clear();
430 }
431
432
433 ////
434 //// MapgenParams
435 ////
436
437 void MapgenParams::load(const Settings &settings)
438 {
439         std::string seed_str;
440         const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
441
442         if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty())
443                 seed = read_seed(seed_str.c_str());
444         else
445                 myrand_bytes(&seed, sizeof(seed));
446
447         settings.getNoEx("mg_name", mg_name);
448         settings.getS16NoEx("water_level", water_level);
449         settings.getS16NoEx("chunksize", chunksize);
450         settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen);
451         settings.getNoiseParams("mg_biome_np_heat", np_biome_heat);
452         settings.getNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend);
453         settings.getNoiseParams("mg_biome_np_humidity", np_biome_humidity);
454         settings.getNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend);
455
456         delete sparams;
457         MapgenFactory *mgfactory = EmergeManager::getMapgenFactory(mg_name);
458         if (mgfactory) {
459                 sparams = mgfactory->createMapgenParams();
460                 sparams->readParams(&settings);
461         }
462 }
463
464
465 void MapgenParams::save(Settings &settings) const
466 {
467         settings.set("mg_name", mg_name);
468         settings.setU64("seed", seed);
469         settings.setS16("water_level", water_level);
470         settings.setS16("chunksize", chunksize);
471         settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX);
472         settings.setNoiseParams("mg_biome_np_heat", np_biome_heat);
473         settings.setNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend);
474         settings.setNoiseParams("mg_biome_np_humidity", np_biome_humidity);
475         settings.setNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend);
476
477         if (sparams)
478                 sparams->writeParams(&settings);
479 }