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.
27 #include "content_sao.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
36 #include "serialization.h"
37 #include "util/serialize.h"
41 const char *GenElementManager::ELEMENT_TITLE = "element";
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 ///////////////////////////////////////////////////////////////////////////////
81 Mapgen::Mapgen(int mapgenid, MapgenParams *params, EmergeManager *emerge) :
82 gennotify(emerge->gen_notify_on, &emerge->gen_notify_on_deco_ids)
86 seed = (int)params->seed;
87 water_level = params->water_level;
88 flags = params->flags;
89 csize = v3s16(1, 1, 1) * (params->chunksize * MAP_BLOCKSIZE);
103 // Returns Y one under area minimum if not found
104 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
106 v3s16 em = vm->m_area.getExtent();
107 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
108 s16 y_nodes_min = vm->m_area.MinEdge.Y;
109 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
112 for (y = y_nodes_max; y >= y_nodes_min; y--) {
113 MapNode &n = vm->m_data[i];
114 if (ndef->get(n).walkable)
117 vm->m_area.add_y(em, i, -1);
119 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
123 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
125 v3s16 em = vm->m_area.getExtent();
126 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
129 for (y = ymax; y >= ymin; y--) {
130 MapNode &n = vm->m_data[i];
131 if (ndef->get(n).walkable)
134 vm->m_area.add_y(em, i, -1);
140 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
145 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
147 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
148 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
149 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
151 // if the values found are out of range, trust the old heightmap
152 if (y == nmax.Y && heightmap[index] > nmax.Y)
154 if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
157 heightmap[index] = y;
160 //printf("updateHeightmap: %dus\n", t.stop());
164 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
166 bool isliquid, wasliquid;
167 v3s16 em = vm->m_area.getExtent();
169 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
170 for (s16 x = nmin.X; x <= nmax.X; x++) {
173 u32 i = vm->m_area.index(x, nmax.Y, z);
174 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
175 isliquid = ndef->get(vm->m_data[i]).isLiquid();
177 // there was a change between liquid and nonliquid, add to queue.
178 if (isliquid != wasliquid)
179 trans_liquid->push_back(v3s16(x, y, z));
181 wasliquid = isliquid;
182 vm->m_area.add_y(em, i, -1);
189 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light)
191 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
192 VoxelArea a(nmin, nmax);
194 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
195 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
196 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
197 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
198 vm->m_data[i].param1 = light;
204 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
206 if (light <= 1 || !a.contains(p))
209 u32 vi = vm->m_area.index(p);
210 MapNode &nn = vm->m_data[vi];
213 // should probably compare masked, but doesn't seem to make a difference
214 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
219 lightSpread(a, p + v3s16(0, 0, 1), light);
220 lightSpread(a, p + v3s16(0, 1, 0), light);
221 lightSpread(a, p + v3s16(1, 0, 0), light);
222 lightSpread(a, p - v3s16(0, 0, 1), light);
223 lightSpread(a, p - v3s16(0, 1, 0), light);
224 lightSpread(a, p - v3s16(1, 0, 0), light);
228 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
230 VoxelArea a(nmin, nmax);
231 bool block_is_underground = (water_level >= nmax.Y);
233 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
234 //TimeTaker t("updateLighting");
236 // first, send vertical rays of sunshine downward
237 v3s16 em = vm->m_area.getExtent();
238 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
239 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
240 // see if we can get a light value from the overtop
241 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
242 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
243 if (block_is_underground)
245 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
248 vm->m_area.add_y(em, i, -1);
250 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
251 MapNode &n = vm->m_data[i];
252 if (!ndef->get(n).sunlight_propagates)
254 n.param1 = LIGHT_SUN;
255 vm->m_area.add_y(em, i, -1);
260 // now spread the sunlight and light up any sources
261 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
262 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
263 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
264 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
265 MapNode &n = vm->m_data[i];
266 if (n.getContent() == CONTENT_IGNORE ||
267 !ndef->get(n).light_propagates)
270 u8 light_produced = ndef->get(n).light_source & 0x0F;
272 n.param1 = light_produced;
274 u8 light = n.param1 & 0x0F;
276 lightSpread(a, v3s16(x, y, z + 1), light - 1);
277 lightSpread(a, v3s16(x, y + 1, z ), light - 1);
278 lightSpread(a, v3s16(x + 1, y, z ), light - 1);
279 lightSpread(a, v3s16(x, y, z - 1), light - 1);
280 lightSpread(a, v3s16(x, y - 1, z ), light - 1);
281 lightSpread(a, v3s16(x - 1, y, z ), light - 1);
287 //printf("updateLighting: %dms\n", t.stop());
291 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax)
293 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
294 VoxelArea a(nmin, nmax);
295 bool block_is_underground = (water_level > nmax.Y);
296 bool sunlight = !block_is_underground;
298 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
300 for (int i = 0; i < 2; i++) {
301 enum LightBank bank = banks[i];
302 std::set<v3s16> light_sources;
303 std::map<v3s16, u8> unlight_from;
305 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
306 light_sources, unlight_from);
307 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
309 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
310 vm->spreadLight(bank, light_sources, ndef);
315 ///////////////////////////////////////////////////////////////////////////////
317 GenerateNotifier::GenerateNotifier()
322 GenerateNotifier::GenerateNotifier(u32 notify_on,
323 std::set<u32> *notify_on_deco_ids)
325 m_notify_on = notify_on;
326 m_notify_on_deco_ids = notify_on_deco_ids;
330 void GenerateNotifier::setNotifyOn(u32 notify_on)
332 m_notify_on = notify_on;
336 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
338 m_notify_on_deco_ids = notify_on_deco_ids;
342 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
344 if (!(m_notify_on & (1 << type)))
347 if (type == GENNOTIFY_DECORATION &&
348 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
355 m_notify_events.push_back(gne);
361 void GenerateNotifier::getEvents(
362 std::map<std::string, std::vector<v3s16> > &event_map,
365 std::list<GenNotifyEvent>::iterator it;
367 for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
368 GenNotifyEvent &gn = *it;
369 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
370 "decoration#"+ itos(gn.id) :
371 flagdesc_gennotify[gn.type].name;
373 event_map[name].push_back(gn.pos);
377 m_notify_events.clear();
381 ///////////////////////////////////////////////////////////////////////////////
384 GenElementManager::~GenElementManager()
386 for (size_t i = 0; i != m_elements.size(); i++)
387 delete m_elements[i];
391 u32 GenElementManager::add(GenElement *elem)
393 size_t nelem = m_elements.size();
395 for (size_t i = 0; i != nelem; i++) {
396 if (m_elements[i] == NULL) {
398 m_elements[i] = elem;
403 if (nelem >= this->ELEMENT_LIMIT)
407 m_elements.push_back(elem);
409 verbosestream << "GenElementManager: added " << this->ELEMENT_TITLE
410 << " element '" << elem->name << "'" << std::endl;
416 GenElement *GenElementManager::get(u32 id)
418 return (id < m_elements.size()) ? m_elements[id] : NULL;
422 GenElement *GenElementManager::getByName(const std::string &name)
424 for (size_t i = 0; i != m_elements.size(); i++) {
425 GenElement *elem = m_elements[i];
426 if (elem && name == elem->name)
434 GenElement *GenElementManager::update(u32 id, GenElement *elem)
436 if (id >= m_elements.size())
439 GenElement *old_elem = m_elements[id];
440 m_elements[id] = elem;
445 GenElement *GenElementManager::remove(u32 id)
447 return update(id, NULL);
451 void GenElementManager::clear()