3 Copyright (C) 2010-2015 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2010-2015 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
29 #include "content_sao.h"
32 #include "voxelalgorithms.h"
37 #include "serialization.h"
38 #include "util/serialize.h"
39 #include "util/numeric.h"
43 FlagDesc flagdesc_mapgen[] = {
46 {"dungeons", MG_DUNGEONS},
49 {"decorations", MG_DECORATIONS},
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},
86 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
87 gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
91 seed = (int)params->seed;
92 water_level = params->water_level;
93 flags = params->flags;
94 csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
110 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
119 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
121 u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
123 return (n * (n * n * 60493 + 19990303) + 1376312589);
127 // Returns Y one under area minimum if not found
128 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
130 v3s16 em = vm->m_area.getExtent();
131 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
132 s16 y_nodes_min = vm->m_area.MinEdge.Y;
133 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
136 for (y = y_nodes_max; y >= y_nodes_min; y--) {
137 MapNode &n = vm->m_data[i];
138 if (ndef->get(n).walkable)
141 vm->m_area.add_y(em, i, -1);
143 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
147 // Returns -MAX_MAP_GENERATION_LIMIT if not found
148 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
150 v3s16 em = vm->m_area.getExtent();
151 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
154 for (y = ymax; y >= ymin; y--) {
155 MapNode &n = vm->m_data[i];
156 if (ndef->get(n).walkable)
159 vm->m_area.add_y(em, i, -1);
161 return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
165 // Returns -MAX_MAP_GENERATION_LIMIT if not found or if ground is found first
166 s16 Mapgen::findLiquidSurface(v2s16 p2d, s16 ymin, s16 ymax)
168 v3s16 em = vm->m_area.getExtent();
169 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
172 for (y = ymax; y >= ymin; y--) {
173 MapNode &n = vm->m_data[i];
174 if (ndef->get(n).walkable)
175 return -MAX_MAP_GENERATION_LIMIT;
176 else if (ndef->get(n).isLiquid())
179 vm->m_area.add_y(em, i, -1);
181 return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
185 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
190 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
192 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
193 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
194 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
196 heightmap[index] = y;
199 //printf("updateHeightmap: %dus\n", t.stop());
203 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
205 bool isliquid, wasliquid;
206 v3s16 em = vm->m_area.getExtent();
208 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
209 for (s16 x = nmin.X; x <= nmax.X; x++) {
212 u32 i = vm->m_area.index(x, nmax.Y, z);
213 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
214 isliquid = ndef->get(vm->m_data[i]).isLiquid();
216 // there was a change between liquid and nonliquid, add to queue.
217 if (isliquid != wasliquid)
218 trans_liquid->push_back(v3s16(x, y, z));
220 wasliquid = isliquid;
221 vm->m_area.add_y(em, i, -1);
228 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
230 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
231 VoxelArea a(nmin, nmax);
233 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
234 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
235 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
236 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
237 vm->m_data[i].param1 = light;
243 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
245 if (light <= 1 || !a.contains(p))
248 u32 vi = vm->m_area.index(p);
249 MapNode &n = vm->m_data[vi];
251 // Decay light in each of the banks separately
252 u8 light_day = light & 0x0F;
256 u8 light_night = light & 0xF0;
260 // Bail out only if we have no more light from either bank to propogate, or
261 // we hit a solid block that light cannot pass through.
262 if ((light_day <= (n.param1 & 0x0F) &&
263 light_night <= (n.param1 & 0xF0)) ||
264 !ndef->get(n).light_propagates)
267 // Since this recursive function only terminates when there is no light from
268 // either bank left, we need to take the max of both banks into account for
269 // the case where spreading has stopped for one light bank but not the other.
270 light = MYMAX(light_day, n.param1 & 0x0F) |
271 MYMAX(light_night, n.param1 & 0xF0);
275 lightSpread(a, p + v3s16(0, 0, 1), light);
276 lightSpread(a, p + v3s16(0, 1, 0), light);
277 lightSpread(a, p + v3s16(1, 0, 0), light);
278 lightSpread(a, p - v3s16(0, 0, 1), light);
279 lightSpread(a, p - v3s16(0, 1, 0), light);
280 lightSpread(a, p - v3s16(1, 0, 0), light);
284 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax,
285 bool propagate_shadow)
287 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
288 //TimeTaker t("updateLighting");
290 propagateSunlight(nmin, nmax, propagate_shadow);
291 spreadLight(full_nmin, full_nmax);
293 //printf("updateLighting: %dms\n", t.stop());
297 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax, bool propagate_shadow)
299 //TimeTaker t("propagateSunlight");
300 VoxelArea a(nmin, nmax);
301 bool block_is_underground = (water_level >= nmax.Y);
302 v3s16 em = vm->m_area.getExtent();
304 // NOTE: Direct access to the low 4 bits of param1 is okay here because,
305 // by definition, sunlight will never be in the night lightbank.
307 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
308 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
309 // see if we can get a light value from the overtop
310 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
311 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
312 if (block_is_underground)
314 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN &&
318 vm->m_area.add_y(em, i, -1);
320 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
321 MapNode &n = vm->m_data[i];
322 if (!ndef->get(n).sunlight_propagates)
324 n.param1 = LIGHT_SUN;
325 vm->m_area.add_y(em, i, -1);
329 //printf("propagateSunlight: %dms\n", t.stop());
333 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
335 //TimeTaker t("spreadLight");
336 VoxelArea a(nmin, nmax);
338 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
339 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
340 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
341 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
342 MapNode &n = vm->m_data[i];
343 if (n.getContent() == CONTENT_IGNORE)
346 const ContentFeatures &cf = ndef->get(n);
347 if (!cf.light_propagates)
350 // TODO(hmmmmm): Abstract away direct param1 accesses with a
351 // wrapper, but something lighter than MapNode::get/setLight
353 u8 light_produced = cf.light_source;
355 n.param1 = light_produced | (light_produced << 4);
359 lightSpread(a, v3s16(x, y, z + 1), light);
360 lightSpread(a, v3s16(x, y + 1, z ), light);
361 lightSpread(a, v3s16(x + 1, y, z ), light);
362 lightSpread(a, v3s16(x, y, z - 1), light);
363 lightSpread(a, v3s16(x, y - 1, z ), light);
364 lightSpread(a, v3s16(x - 1, y, z ), light);
370 //printf("spreadLight: %dms\n", t.stop());
375 //// GenerateNotifier
378 GenerateNotifier::GenerateNotifier()
384 GenerateNotifier::GenerateNotifier(u32 notify_on,
385 std::set<u32> *notify_on_deco_ids)
387 m_notify_on = notify_on;
388 m_notify_on_deco_ids = notify_on_deco_ids;
392 void GenerateNotifier::setNotifyOn(u32 notify_on)
394 m_notify_on = notify_on;
398 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
400 m_notify_on_deco_ids = notify_on_deco_ids;
404 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
406 if (!(m_notify_on & (1 << type)))
409 if (type == GENNOTIFY_DECORATION &&
410 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
417 m_notify_events.push_back(gne);
423 void GenerateNotifier::getEvents(
424 std::map<std::string, std::vector<v3s16> > &event_map,
427 std::list<GenNotifyEvent>::iterator it;
429 for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
430 GenNotifyEvent &gn = *it;
431 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
432 "decoration#"+ itos(gn.id) :
433 flagdesc_gennotify[gn.type].name;
435 event_map[name].push_back(gn.pos);
439 m_notify_events.clear();
447 void MapgenParams::load(const Settings &settings)
449 std::string seed_str;
450 const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
452 if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty())
453 seed = read_seed(seed_str.c_str());
455 myrand_bytes(&seed, sizeof(seed));
457 settings.getNoEx("mg_name", mg_name);
458 settings.getS16NoEx("water_level", water_level);
459 settings.getS16NoEx("chunksize", chunksize);
460 settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen);
461 settings.getNoiseParams("mg_biome_np_heat", np_biome_heat);
462 settings.getNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend);
463 settings.getNoiseParams("mg_biome_np_humidity", np_biome_humidity);
464 settings.getNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend);
467 MapgenFactory *mgfactory = EmergeManager::getMapgenFactory(mg_name);
469 sparams = mgfactory->createMapgenParams();
470 sparams->readParams(&settings);
475 void MapgenParams::save(Settings &settings) const
477 settings.set("mg_name", mg_name);
478 settings.setU64("seed", seed);
479 settings.setS16("water_level", water_level);
480 settings.setS16("chunksize", chunksize);
481 settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, U32_MAX);
482 settings.setNoiseParams("mg_biome_np_heat", np_biome_heat);
483 settings.setNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend);
484 settings.setNoiseParams("mg_biome_np_humidity", np_biome_humidity);
485 settings.setNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend);
488 sparams->writeParams(&settings);