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"
34 #include "settings.h" // For g_settings
35 #include "main.h" // For g_profiler
37 #include "serialization.h"
38 #include "util/serialize.h"
42 const char *GenElementManager::ELEMENT_TITLE = "element";
44 static const s16 INVALID_HEIGHT = MAP_GENERATION_LIMIT + 1;
46 FlagDesc flagdesc_mapgen[] = {
49 {"dungeons", MG_DUNGEONS},
55 FlagDesc flagdesc_gennotify[] = {
56 {"dungeon", 1 << GENNOTIFY_DUNGEON},
57 {"temple", 1 << GENNOTIFY_TEMPLE},
58 {"cave_begin", 1 << GENNOTIFY_CAVE_BEGIN},
59 {"cave_end", 1 << GENNOTIFY_CAVE_END},
60 {"large_cave_begin", 1 << GENNOTIFY_LARGECAVE_BEGIN},
61 {"large_cave_end", 1 << GENNOTIFY_LARGECAVE_END},
62 {"decoration", 1 << GENNOTIFY_DECORATION},
67 ///////////////////////////////////////////////////////////////////////////////
84 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
85 gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
89 seed = (int)params->seed;
90 water_level = params->water_level;
91 flags = params->flags;
92 csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
106 u32 Mapgen::getBlockSeed(v3s16 p, int seed)
115 u32 Mapgen::getBlockSeed2(v3s16 p, int seed)
117 u32 n = 1619 * p.X + 31337 * p.Y + 52591 * p.Z + 1013 * seed;
119 return (n * (n * n * 60493 + 19990303) + 1376312589);
123 // Returns Y one under area minimum if not found
124 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
126 v3s16 em = vm->m_area.getExtent();
127 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
128 s16 y_nodes_min = vm->m_area.MinEdge.Y;
129 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
132 for (y = y_nodes_max; y >= y_nodes_min; y--) {
133 MapNode &n = vm->m_data[i];
134 if (ndef->get(n).walkable)
137 vm->m_area.add_y(em, i, -1);
139 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
143 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
145 v3s16 em = vm->m_area.getExtent();
146 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
149 for (y = ymax; y >= ymin; y--) {
150 MapNode &n = vm->m_data[i];
151 if (ndef->get(n).walkable)
154 vm->m_area.add_y(em, i, -1);
160 void Mapgen::initHeightMap(s16 *dest, size_t len)
162 for (size_t i = 0; i < len; i++)
163 dest[i] = INVALID_HEIGHT;
166 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
171 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
173 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
174 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
175 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
177 if (heightmap[index] != INVALID_HEIGHT) {
178 // if the values found are out of range, trust the old heightmap
179 if (y == nmax.Y && heightmap[index] > nmax.Y)
181 if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
185 heightmap[index] = y;
188 //printf("updateHeightmap: %dus\n", t.stop());
192 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
194 bool isliquid, wasliquid;
195 v3s16 em = vm->m_area.getExtent();
197 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
198 for (s16 x = nmin.X; x <= nmax.X; x++) {
201 u32 i = vm->m_area.index(x, nmax.Y, z);
202 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
203 isliquid = ndef->get(vm->m_data[i]).isLiquid();
205 // there was a change between liquid and nonliquid, add to queue.
206 if (isliquid != wasliquid)
207 trans_liquid->push_back(v3s16(x, y, z));
209 wasliquid = isliquid;
210 vm->m_area.add_y(em, i, -1);
217 void Mapgen::setLighting(u8 light, v3s16 nmin, v3s16 nmax)
219 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
220 VoxelArea a(nmin, nmax);
222 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
223 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
224 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
225 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
226 vm->m_data[i].param1 = light;
232 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
234 if (light <= 1 || !a.contains(p))
237 u32 vi = vm->m_area.index(p);
238 MapNode &nn = vm->m_data[vi];
241 // should probably compare masked, but doesn't seem to make a difference
242 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
247 lightSpread(a, p + v3s16(0, 0, 1), light);
248 lightSpread(a, p + v3s16(0, 1, 0), light);
249 lightSpread(a, p + v3s16(1, 0, 0), light);
250 lightSpread(a, p - v3s16(0, 0, 1), light);
251 lightSpread(a, p - v3s16(0, 1, 0), light);
252 lightSpread(a, p - v3s16(1, 0, 0), light);
256 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax, v3s16 full_nmin, v3s16 full_nmax)
258 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
259 //TimeTaker t("updateLighting");
261 propagateSunlight(nmin, nmax);
262 spreadLight(full_nmin, full_nmax);
264 //printf("updateLighting: %dms\n", t.stop());
269 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
271 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
272 //TimeTaker t("updateLighting");
275 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
276 nmax + v3s16(1, 0, 1) * MAP_BLOCKSIZE);
279 nmin - v3s16(1, 1, 1) * MAP_BLOCKSIZE,
280 nmax + v3s16(1, 1, 1) * MAP_BLOCKSIZE);
282 //printf("updateLighting: %dms\n", t.stop());
286 void Mapgen::propagateSunlight(v3s16 nmin, v3s16 nmax)
288 //TimeTaker t("propagateSunlight");
289 VoxelArea a(nmin, nmax);
290 bool block_is_underground = (water_level >= nmax.Y);
291 v3s16 em = vm->m_area.getExtent();
293 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
294 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
295 // see if we can get a light value from the overtop
296 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
297 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
298 if (block_is_underground)
300 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
303 vm->m_area.add_y(em, i, -1);
305 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
306 MapNode &n = vm->m_data[i];
307 if (!ndef->get(n).sunlight_propagates)
309 n.param1 = LIGHT_SUN;
310 vm->m_area.add_y(em, i, -1);
314 //printf("propagateSunlight: %dms\n", t.stop());
319 void Mapgen::spreadLight(v3s16 nmin, v3s16 nmax)
321 //TimeTaker t("spreadLight");
322 VoxelArea a(nmin, nmax);
324 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
325 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
326 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
327 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
328 MapNode &n = vm->m_data[i];
329 if (n.getContent() == CONTENT_IGNORE ||
330 !ndef->get(n).light_propagates)
333 u8 light_produced = ndef->get(n).light_source & 0x0F;
335 n.param1 = light_produced;
337 u8 light = n.param1 & 0x0F;
339 lightSpread(a, v3s16(x, y, z + 1), light);
340 lightSpread(a, v3s16(x, y + 1, z ), light);
341 lightSpread(a, v3s16(x + 1, y, z ), light);
342 lightSpread(a, v3s16(x, y, z - 1), light);
343 lightSpread(a, v3s16(x, y - 1, z ), light);
344 lightSpread(a, v3s16(x - 1, y, z ), light);
350 //printf("spreadLight: %dms\n", t.stop());
355 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax)
357 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
358 VoxelArea a(nmin, nmax);
359 bool block_is_underground = (water_level > nmax.Y);
360 bool sunlight = !block_is_underground;
362 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
364 for (int i = 0; i < 2; i++) {
365 enum LightBank bank = banks[i];
366 std::set<v3s16> light_sources;
367 std::map<v3s16, u8> unlight_from;
369 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
370 light_sources, unlight_from);
371 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
373 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
374 vm->spreadLight(bank, light_sources, ndef);
379 ///////////////////////////////////////////////////////////////////////////////
381 GenerateNotifier::GenerateNotifier()
387 GenerateNotifier::GenerateNotifier(u32 notify_on,
388 std::set<u32> *notify_on_deco_ids)
390 m_notify_on = notify_on;
391 m_notify_on_deco_ids = notify_on_deco_ids;
395 void GenerateNotifier::setNotifyOn(u32 notify_on)
397 m_notify_on = notify_on;
401 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
403 m_notify_on_deco_ids = notify_on_deco_ids;
407 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
409 if (!(m_notify_on & (1 << type)))
412 if (type == GENNOTIFY_DECORATION &&
413 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
420 m_notify_events.push_back(gne);
426 void GenerateNotifier::getEvents(
427 std::map<std::string, std::vector<v3s16> > &event_map,
430 for (std::vector<GenNotifyEvent>::iterator it = m_notify_events.begin();
431 it != m_notify_events.end(); ++it) {
432 GenNotifyEvent &gn = *it;
433 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
434 "decoration#"+ itos(gn.id) :
435 flagdesc_gennotify[gn.type].name;
437 event_map[name].push_back(gn.pos);
441 m_notify_events.clear();
445 ///////////////////////////////////////////////////////////////////////////////
448 GenElementManager::GenElementManager(IGameDef *gamedef)
450 m_ndef = gamedef->getNodeDefManager();
454 GenElementManager::~GenElementManager()
456 for (size_t i = 0; i != m_elements.size(); i++)
457 delete m_elements[i];
461 u32 GenElementManager::add(GenElement *elem)
463 size_t nelem = m_elements.size();
465 for (size_t i = 0; i != nelem; i++) {
466 if (m_elements[i] == NULL) {
468 m_elements[i] = elem;
473 if (nelem >= this->ELEMENT_LIMIT)
477 m_elements.push_back(elem);
479 verbosestream << "GenElementManager: added " << this->ELEMENT_TITLE
480 << " element '" << elem->name << "'" << std::endl;
486 GenElement *GenElementManager::get(u32 id)
488 return (id < m_elements.size()) ? m_elements[id] : NULL;
492 GenElement *GenElementManager::getByName(const std::string &name)
494 for (size_t i = 0; i != m_elements.size(); i++) {
495 GenElement *elem = m_elements[i];
496 if (elem && name == elem->name)
504 GenElement *GenElementManager::update(u32 id, GenElement *elem)
506 if (id >= m_elements.size())
509 GenElement *old_elem = m_elements[id];
510 m_elements[id] = elem;
515 GenElement *GenElementManager::remove(u32 id)
517 return update(id, NULL);
521 void GenElementManager::clear()