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 "content_mapnode.h" // For content_mapnode_get_new_name
33 #include "voxelalgorithms.h"
38 #include "serialization.h"
39 #include "util/serialize.h"
40 #include "util/numeric.h"
44 FlagDesc flagdesc_mapgen[] = {
47 {"dungeons", MG_DUNGEONS},
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},
65 ///////////////////////////////////////////////////////////////////////////////
85 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
86 gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
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);
109 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
118 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
120 u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
122 return (n * (n * n * 60493 + 19990303) + 1376312589);
126 // Returns Y one under area minimum if not found
127 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
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);
135 for (y = y_nodes_max; y >= y_nodes_min; y--) {
136 MapNode &n = vm->m_data[i];
137 if (ndef->get(n).walkable)
140 vm->m_area.add_y(em, i, -1);
142 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
146 // Returns -MAX_MAP_GENERATION_LIMIT if not found
147 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
149 v3s16 em = vm->m_area.getExtent();
150 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
153 for (y = ymax; y >= ymin; y--) {
154 MapNode &n = vm->m_data[i];
155 if (ndef->get(n).walkable)
158 vm->m_area.add_y(em, i, -1);
160 return (y >= ymin) ? y : -MAX_MAP_GENERATION_LIMIT;
164 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
169 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
171 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
172 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
173 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
175 heightmap[index] = y;
178 //printf("updateHeightmap: %dus\n", t.stop());
182 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
184 bool isliquid, wasliquid;
185 v3s16 em = vm->m_area.getExtent();
187 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
188 for (s16 x = nmin.X; x <= nmax.X; x++) {
191 u32 i = vm->m_area.index(x, nmax.Y, z);
192 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
193 isliquid = ndef->get(vm->m_data[i]).isLiquid();
195 // there was a change between liquid and nonliquid, add to queue.
196 if (isliquid != wasliquid)
197 trans_liquid->push_back(v3s16(x, y, z));
199 wasliquid = isliquid;
200 vm->m_area.add_y(em, i, -1);
207 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
209 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
210 VoxelArea a(nmin, nmax);
212 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
213 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
214 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
215 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
216 vm->m_data[i].param1 = light;
222 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
224 if (light <= 1 || !a.contains(p))
227 u32 vi = vm->m_area.index(p);
228 MapNode &nn = vm->m_data[vi];
231 // should probably compare masked, but doesn't seem to make a difference
232 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
237 lightSpread(a, p + v3s16(0, 0, 1), light);
238 lightSpread(a, p + v3s16(0, 1, 0), light);
239 lightSpread(a, p + v3s16(1, 0, 0), light);
240 lightSpread(a, p - v3s16(0, 0, 1), light);
241 lightSpread(a, p - v3s16(0, 1, 0), light);
242 lightSpread(a, p - v3s16(1, 0, 0), light);
246 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax)
248 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
249 //TimeTaker t("updateLighting");
251 propagateSunlight(nmin, nmax);
252 spreadLight(full_nmin, full_nmax);
254 //printf("updateLighting: %dms\n", t.stop());
259 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
261 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
262 //TimeTaker t("updateLighting");
265 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
266 nmax + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
269 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
270 nmax + v3s16(1, 1, 1) * MAP_BLOCKSIZE);
272 //printf("updateLighting: %dms\n", t.stop());
276 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax)
278 //TimeTaker t("propagateSunlight");
279 VoxelArea a(nmin, nmax);
280 bool block_is_underground = (water_level >= nmax.Y);
281 v3s16 em = vm->m_area.getExtent();
283 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
284 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
285 // see if we can get a light value from the overtop
286 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
287 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
288 if (block_is_underground)
290 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
293 vm->m_area.add_y(em, i, -1);
295 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
296 MapNode &n = vm->m_data[i];
297 if (!ndef->get(n).sunlight_propagates)
299 n.param1 = LIGHT_SUN;
300 vm->m_area.add_y(em, i, -1);
304 //printf("propagateSunlight: %dms\n", t.stop());
309 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
311 //TimeTaker t("spreadLight");
312 VoxelArea a(nmin, nmax);
314 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
315 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
316 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
317 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
318 MapNode &n = vm->m_data[i];
319 if (n.getContent() == CONTENT_IGNORE ||
320 !ndef->get(n).light_propagates)
323 u8 light_produced = ndef->get(n).light_source & 0x0F;
325 n.param1 = light_produced;
327 u8 light = n.param1 & 0x0F;
329 lightSpread(a, v3s16(x, y, z + 1), light);
330 lightSpread(a, v3s16(x, y + 1, z ), light);
331 lightSpread(a, v3s16(x + 1, y, z ), light);
332 lightSpread(a, v3s16(x, y, z - 1), light);
333 lightSpread(a, v3s16(x, y - 1, z ), light);
334 lightSpread(a, v3s16(x - 1, y, z ), light);
340 //printf("spreadLight: %dms\n", t.stop());
345 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax)
347 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
348 VoxelArea a(nmin, nmax);
349 bool block_is_underground = (water_level > nmax.Y);
350 bool sunlight = !block_is_underground;
352 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
354 for (int i = 0; i < 2; i++) {
355 enum LightBank bank = banks[i];
356 std::set<v3s16> light_sources;
357 std::map<v3s16, u8> unlight_from;
359 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
360 light_sources, unlight_from);
361 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
363 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
364 vm->spreadLight(bank, light_sources, ndef);
369 ///////////////////////////////////////////////////////////////////////////////
371 GenerateNotifier::GenerateNotifier()
377 GenerateNotifier::GenerateNotifier(u32 notify_on,
378 std::set<u32> *notify_on_deco_ids)
380 m_notify_on = notify_on;
381 m_notify_on_deco_ids = notify_on_deco_ids;
385 void GenerateNotifier::setNotifyOn(u32 notify_on)
387 m_notify_on = notify_on;
391 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
393 m_notify_on_deco_ids = notify_on_deco_ids;
397 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
399 if (!(m_notify_on & (1 << type)))
402 if (type == GENNOTIFY_DECORATION &&
403 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
410 m_notify_events.push_back(gne);
416 void GenerateNotifier::getEvents(
417 std::map<std::string, std::vector<v3s16> > &event_map,
420 std::list<GenNotifyEvent>::iterator it;
422 for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
423 GenNotifyEvent &gn = *it;
424 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
425 "decoration#"+ itos(gn.id) :
426 flagdesc_gennotify[gn.type].name;
428 event_map[name].push_back(gn.pos);
432 m_notify_events.clear();
435 ///////////////////////////////////////////////////////////////////////////////
437 void MapgenParams::load(const Settings &settings)
439 std::string seed_str;
440 const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
442 if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty())
443 seed = read_seed(seed_str.c_str());
445 myrand_bytes(&seed, sizeof(seed));
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);
457 sparams = EmergeManager::createMapgenParams(mg_name);
459 sparams->readParams(&settings);
463 void MapgenParams::save(Settings &settings) const
465 settings.set("mg_name", mg_name);
466 settings.setU64("seed", seed);
467 settings.setS16("water_level", water_level);
468 settings.setS16("chunksize", chunksize);
469 settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, (u32)-1);
470 settings.setNoiseParams("mg_biome_np_heat", np_biome_heat);
471 settings.setNoiseParams("mg_biome_np_heat_blend", np_biome_heat_blend);
472 settings.setNoiseParams("mg_biome_np_humidity", np_biome_humidity);
473 settings.setNoiseParams("mg_biome_np_humidity_blend", np_biome_humidity_blend);
476 sparams->writeParams(&settings);