3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "content_sao.h"
31 #include "content_mapnode.h" // For content_mapnode_get_new_name
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},
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},
64 ///////////////////////////////////////////////////////////////////////////////
82 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
83 gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
87 seed = (int)params->seed;
88 water_level = params->water_level;
89 flags = params->flags;
90 csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
104 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
113 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
115 u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
117 return (n * (n * n * 60493 + 19990303) + 1376312589);
121 // Returns Y one under area minimum if not found
122 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
124 v3s16 em = vm->m_area.getExtent();
125 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
126 s16 y_nodes_min = vm->m_area.MinEdge.Y;
127 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
130 for (y = y_nodes_max; y >= y_nodes_min; y--) {
131 MapNode &n = vm->m_data[i];
132 if (ndef->get(n).walkable)
135 vm->m_area.add_y(em, i, -1);
137 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
141 // Returns -MAP_GENERATION_LIMIT if not found
142 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
144 v3s16 em = vm->m_area.getExtent();
145 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
148 for (y = ymax; y >= ymin; y--) {
149 MapNode &n = vm->m_data[i];
150 if (ndef->get(n).walkable)
153 vm->m_area.add_y(em, i, -1);
155 return (y >= ymin) ? y : -MAP_GENERATION_LIMIT;
159 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
164 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
166 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
167 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
168 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
170 heightmap[index] = y;
173 //printf("updateHeightmap: %dus\n", t.stop());
177 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
179 bool isliquid, wasliquid;
180 v3s16 em = vm->m_area.getExtent();
182 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
183 for (s16 x = nmin.X; x <= nmax.X; x++) {
186 u32 i = vm->m_area.index(x, nmax.Y, z);
187 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
188 isliquid = ndef->get(vm->m_data[i]).isLiquid();
190 // there was a change between liquid and nonliquid, add to queue.
191 if (isliquid != wasliquid)
192 trans_liquid->push_back(v3s16(x, y, z));
194 wasliquid = isliquid;
195 vm->m_area.add_y(em, i, -1);
202 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
204 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
205 VoxelArea a(nmin, nmax);
207 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
208 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
209 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
210 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
211 vm->m_data[i].param1 = light;
217 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
219 if (light <= 1 || !a.contains(p))
222 u32 vi = vm->m_area.index(p);
223 MapNode &nn = vm->m_data[vi];
226 // should probably compare masked, but doesn't seem to make a difference
227 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
232 lightSpread(a, p + v3s16(0, 0, 1), light);
233 lightSpread(a, p + v3s16(0, 1, 0), light);
234 lightSpread(a, p + v3s16(1, 0, 0), light);
235 lightSpread(a, p - v3s16(0, 0, 1), light);
236 lightSpread(a, p - v3s16(0, 1, 0), light);
237 lightSpread(a, p - v3s16(1, 0, 0), light);
241 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax)
243 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
244 //TimeTaker t("updateLighting");
246 propagateSunlight(nmin, nmax);
247 spreadLight(full_nmin, full_nmax);
249 //printf("updateLighting: %dms\n", t.stop());
254 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
256 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
257 //TimeTaker t("updateLighting");
260 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
261 nmax + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
264 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
265 nmax + v3s16(1, 1, 1) * MAP_BLOCKSIZE);
267 //printf("updateLighting: %dms\n", t.stop());
271 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax)
273 //TimeTaker t("propagateSunlight");
274 VoxelArea a(nmin, nmax);
275 bool block_is_underground = (water_level >= nmax.Y);
276 v3s16 em = vm->m_area.getExtent();
278 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
279 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
280 // see if we can get a light value from the overtop
281 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
282 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
283 if (block_is_underground)
285 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
288 vm->m_area.add_y(em, i, -1);
290 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
291 MapNode &n = vm->m_data[i];
292 if (!ndef->get(n).sunlight_propagates)
294 n.param1 = LIGHT_SUN;
295 vm->m_area.add_y(em, i, -1);
299 //printf("propagateSunlight: %dms\n", t.stop());
304 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
306 //TimeTaker t("spreadLight");
307 VoxelArea a(nmin, nmax);
309 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
310 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
311 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
312 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
313 MapNode &n = vm->m_data[i];
314 if (n.getContent() == CONTENT_IGNORE ||
315 !ndef->get(n).light_propagates)
318 u8 light_produced = ndef->get(n).light_source & 0x0F;
320 n.param1 = light_produced;
322 u8 light = n.param1 & 0x0F;
324 lightSpread(a, v3s16(x, y, z + 1), light);
325 lightSpread(a, v3s16(x, y + 1, z ), light);
326 lightSpread(a, v3s16(x + 1, y, z ), light);
327 lightSpread(a, v3s16(x, y, z - 1), light);
328 lightSpread(a, v3s16(x, y - 1, z ), light);
329 lightSpread(a, v3s16(x - 1, y, z ), light);
335 //printf("spreadLight: %dms\n", t.stop());
340 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax)
342 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
343 VoxelArea a(nmin, nmax);
344 bool block_is_underground = (water_level > nmax.Y);
345 bool sunlight = !block_is_underground;
347 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
349 for (int i = 0; i < 2; i++) {
350 enum LightBank bank = banks[i];
351 std::set<v3s16> light_sources;
352 std::map<v3s16, u8> unlight_from;
354 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
355 light_sources, unlight_from);
356 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
358 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
359 vm->spreadLight(bank, light_sources, ndef);
364 ///////////////////////////////////////////////////////////////////////////////
366 GenerateNotifier::GenerateNotifier()
372 GenerateNotifier::GenerateNotifier(u32 notify_on,
373 std::set<u32> *notify_on_deco_ids)
375 m_notify_on = notify_on;
376 m_notify_on_deco_ids = notify_on_deco_ids;
380 void GenerateNotifier::setNotifyOn(u32 notify_on)
382 m_notify_on = notify_on;
386 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
388 m_notify_on_deco_ids = notify_on_deco_ids;
392 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
394 if (!(m_notify_on & (1 << type)))
397 if (type == GENNOTIFY_DECORATION &&
398 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
405 m_notify_events.push_back(gne);
411 void GenerateNotifier::getEvents(
412 std::map<std::string, std::vector<v3s16> > &event_map,
415 std::list<GenNotifyEvent>::iterator it;
417 for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
418 GenNotifyEvent &gn = *it;
419 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
420 "decoration#"+ itos(gn.id) :
421 flagdesc_gennotify[gn.type].name;
423 event_map[name].push_back(gn.pos);
427 m_notify_events.clear();
431 ///////////////////////////////////////////////////////////////////////////////
434 ObjDefManager::ObjDefManager(IGameDef *gamedef, ObjDefType type)
437 m_ndef = gamedef->getNodeDefManager();
441 ObjDefManager::~ObjDefManager()
443 for (size_t i = 0; i != m_objects.size(); i++)
448 ObjDefHandle ObjDefManager::add(ObjDef *obj)
452 if (obj->name.length() && getByName(obj->name))
453 return OBJDEF_INVALID_HANDLE;
455 u32 index = addRaw(obj);
456 if (index == OBJDEF_INVALID_INDEX)
457 return OBJDEF_INVALID_HANDLE;
459 obj->handle = createHandle(index, m_objtype, obj->uid);
464 ObjDef *ObjDefManager::get(ObjDefHandle handle) const
466 u32 index = validateHandle(handle);
467 return (index != OBJDEF_INVALID_INDEX) ? getRaw(index) : NULL;
471 ObjDef *ObjDefManager::set(ObjDefHandle handle, ObjDef *obj)
473 u32 index = validateHandle(handle);
474 return (index != OBJDEF_INVALID_INDEX) ? setRaw(index, obj) : NULL;
478 u32 ObjDefManager::addRaw(ObjDef *obj)
480 size_t nobjects = m_objects.size();
481 if (nobjects >= OBJDEF_MAX_ITEMS)
484 obj->index = nobjects;
486 // Ensure UID is nonzero so that a valid handle == OBJDEF_INVALID_HANDLE
487 // is not possible. The slight randomness bias isn't very significant.
488 obj->uid = myrand() & OBJDEF_UID_MASK;
492 m_objects.push_back(obj);
494 infostream << "ObjDefManager: added " << getObjectTitle()
495 << ": name=\"" << obj->name
496 << "\" index=" << obj->index
497 << " uid=" << obj->uid
504 ObjDef *ObjDefManager::getRaw(u32 index) const
506 return m_objects[index];
510 ObjDef *ObjDefManager::setRaw(u32 index, ObjDef *obj)
512 ObjDef *old_obj = m_objects[index];
513 m_objects[index] = obj;
518 ObjDef *ObjDefManager::getByName(const std::string &name) const
520 for (size_t i = 0; i != m_objects.size(); i++) {
521 ObjDef *obj = m_objects[i];
522 if (obj && !strcasecmp(name.c_str(), obj->name.c_str()))
530 void ObjDefManager::clear()
532 for (size_t i = 0; i != m_objects.size(); i++)
539 u32 ObjDefManager::validateHandle(ObjDefHandle handle) const
546 (handle != OBJDEF_INVALID_HANDLE) &&
547 decodeHandle(handle, &index, &type, &uid) &&
548 (type == m_objtype) &&
549 (index < m_objects.size()) &&
550 (m_objects[index]->uid == uid);
552 return is_valid ? index : -1;
556 ObjDefHandle ObjDefManager::createHandle(u32 index, ObjDefType type, u32 uid)
558 ObjDefHandle handle = 0;
559 set_bits(&handle, 0, 18, index);
560 set_bits(&handle, 18, 6, type);
561 set_bits(&handle, 24, 7, uid);
563 u32 parity = calc_parity(handle);
564 set_bits(&handle, 31, 1, parity);
566 return handle ^ OBJDEF_HANDLE_SALT;
570 bool ObjDefManager::decodeHandle(ObjDefHandle handle, u32 *index,
571 ObjDefType *type, u32 *uid)
573 handle ^= OBJDEF_HANDLE_SALT;
575 u32 parity = get_bits(handle, 31, 1);
576 set_bits(&handle, 31, 1, 0);
577 if (parity != calc_parity(handle))
580 *index = get_bits(handle, 0, 18);
581 *type = (ObjDefType)get_bits(handle, 18, 6);
582 *uid = get_bits(handle, 24, 7);
587 ///////////////////////////////////////////////////////////////////////////////
590 void MapgenParams::load(const Settings &settings)
592 std::string seed_str;
593 const char *seed_name = (&settings == g_settings) ? "fixed_map_seed" : "seed";
595 if (settings.getNoEx(seed_name, seed_str) && !seed_str.empty())
596 seed = read_seed(seed_str.c_str());
598 myrand_bytes(&seed, sizeof(seed));
600 settings.getNoEx("mg_name", mg_name);
601 settings.getS16NoEx("water_level", water_level);
602 settings.getS16NoEx("chunksize", chunksize);
603 settings.getFlagStrNoEx("mg_flags", flags, flagdesc_mapgen);
604 settings.getNoiseParams("mg_biome_np_heat", np_biome_heat);
605 settings.getNoiseParams("mg_biome_np_humidity", np_biome_humidity);
608 sparams = EmergeManager::createMapgenParams(mg_name);
610 sparams->readParams(&settings);
614 void MapgenParams::save(Settings &settings) const
616 settings.set("mg_name", mg_name);
617 settings.setU64("seed", seed);
618 settings.setS16("water_level", water_level);
619 settings.setS16("chunksize", chunksize);
620 settings.setFlagStr("mg_flags", flags, flagdesc_mapgen, (u32)-1);
621 settings.setNoiseParams("mg_biome_np_heat", np_biome_heat);
622 settings.setNoiseParams("mg_biome_np_humidity", np_biome_humidity);
625 sparams->writeParams(&settings);