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