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 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 ///////////////////////////////////////////////////////////////////////////////
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 // Returns Y one under area minimum if not found
105 s16 Mapgen::findGroundLevelFull(v2s16 p2d)
107 v3s16 em = vm->m_area.getExtent();
108 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
109 s16 y_nodes_min = vm->m_area.MinEdge.Y;
110 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
113 for (y = y_nodes_max; y >= y_nodes_min; y--) {
114 MapNode &n = vm->m_data[i];
115 if (ndef->get(n).walkable)
118 vm->m_area.add_y(em, i, -1);
120 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
124 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax)
126 v3s16 em = vm->m_area.getExtent();
127 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
130 for (y = ymax; y >= ymin; y--) {
131 MapNode &n = vm->m_data[i];
132 if (ndef->get(n).walkable)
135 vm->m_area.add_y(em, i, -1);
141 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax)
146 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
148 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
149 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
150 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
152 // if the values found are out of range, trust the old heightmap
153 if (y == nmax.Y && heightmap[index] > nmax.Y)
155 if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
158 heightmap[index] = y;
161 //printf("updateHeightmap: %dus\n", t.stop());
165 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax)
167 bool isliquid, wasliquid;
168 v3s16 em = vm->m_area.getExtent();
170 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
171 for (s16 x = nmin.X; x <= nmax.X; x++) {
174 u32 i = vm->m_area.index(x, nmax.Y, z);
175 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
176 isliquid = ndef->get(vm->m_data[i]).isLiquid();
178 // there was a change between liquid and nonliquid, add to queue.
179 if (isliquid != wasliquid)
180 trans_liquid->push_back(v3s16(x, y, z));
182 wasliquid = isliquid;
183 vm->m_area.add_y(em, i, -1);
190 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light)
192 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
193 VoxelArea a(nmin, nmax);
195 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
196 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
197 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
198 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
199 vm->m_data[i].param1 = light;
205 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light)
207 if (light <= 1 || !a.contains(p))
210 u32 vi = vm->m_area.index(p);
211 MapNode &nn = vm->m_data[vi];
214 // should probably compare masked, but doesn't seem to make a difference
215 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
220 lightSpread(a, p + v3s16(0, 0, 1), light);
221 lightSpread(a, p + v3s16(0, 1, 0), light);
222 lightSpread(a, p + v3s16(1, 0, 0), light);
223 lightSpread(a, p - v3s16(0, 0, 1), light);
224 lightSpread(a, p - v3s16(0, 1, 0), light);
225 lightSpread(a, p - v3s16(1, 0, 0), light);
229 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax)
231 VoxelArea a(nmin, nmax);
232 bool block_is_underground = (water_level >= nmax.Y);
234 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
235 //TimeTaker t("updateLighting");
237 // first, send vertical rays of sunshine downward
238 v3s16 em = vm->m_area.getExtent();
239 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
240 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
241 // see if we can get a light value from the overtop
242 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
243 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
244 if (block_is_underground)
246 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
249 vm->m_area.add_y(em, i, -1);
251 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
252 MapNode &n = vm->m_data[i];
253 if (!ndef->get(n).sunlight_propagates)
255 n.param1 = LIGHT_SUN;
256 vm->m_area.add_y(em, i, -1);
261 // now spread the sunlight and light up any sources
262 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
263 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
264 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
265 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
266 MapNode &n = vm->m_data[i];
267 if (n.getContent() == CONTENT_IGNORE ||
268 !ndef->get(n).light_propagates)
271 u8 light_produced = ndef->get(n).light_source & 0x0F;
273 n.param1 = light_produced;
275 u8 light = n.param1 & 0x0F;
277 lightSpread(a, v3s16(x, y, z + 1), light - 1);
278 lightSpread(a, v3s16(x, y + 1, z ), light - 1);
279 lightSpread(a, v3s16(x + 1, y, z ), light - 1);
280 lightSpread(a, v3s16(x, y, z - 1), light - 1);
281 lightSpread(a, v3s16(x, y - 1, z ), light - 1);
282 lightSpread(a, v3s16(x - 1, y, z ), light - 1);
288 //printf("updateLighting: %dms\n", t.stop());
292 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax)
294 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
295 VoxelArea a(nmin, nmax);
296 bool block_is_underground = (water_level > nmax.Y);
297 bool sunlight = !block_is_underground;
299 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
301 for (int i = 0; i < 2; i++) {
302 enum LightBank bank = banks[i];
303 std::set<v3s16> light_sources;
304 std::map<v3s16, u8> unlight_from;
306 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
307 light_sources, unlight_from);
308 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
310 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
311 vm->spreadLight(bank, light_sources, ndef);
316 ///////////////////////////////////////////////////////////////////////////////
318 GenerateNotifier::GenerateNotifier()
323 GenerateNotifier::GenerateNotifier(u32 notify_on,
324 std::set<u32> *notify_on_deco_ids)
326 m_notify_on = notify_on;
327 m_notify_on_deco_ids = notify_on_deco_ids;
331 void GenerateNotifier::setNotifyOn(u32 notify_on)
333 m_notify_on = notify_on;
337 void GenerateNotifier::setNotifyOnDecoIds(std::set<u32> *notify_on_deco_ids)
339 m_notify_on_deco_ids = notify_on_deco_ids;
343 bool GenerateNotifier::addEvent(GenNotifyType type, v3s16 pos, u32 id)
345 if (!(m_notify_on & (1 << type)))
348 if (type == GENNOTIFY_DECORATION &&
349 m_notify_on_deco_ids->find(id) == m_notify_on_deco_ids->end())
356 m_notify_events.push_back(gne);
362 void GenerateNotifier::getEvents(
363 std::map<std::string, std::vector<v3s16> > &event_map,
366 std::list<GenNotifyEvent>::iterator it;
368 for (it = m_notify_events.begin(); it != m_notify_events.end(); ++it) {
369 GenNotifyEvent &gn = *it;
370 std::string name = (gn.type == GENNOTIFY_DECORATION) ?
371 "decoration#"+ itos(gn.id) :
372 flagdesc_gennotify[gn.type].name;
374 event_map[name].push_back(gn.pos);
378 m_notify_events.clear();
382 ///////////////////////////////////////////////////////////////////////////////
385 GenElementManager::GenElementManager(IGameDef *gamedef)
387 m_ndef = gamedef->getNodeDefManager();
391 GenElementManager::~GenElementManager()
393 for (size_t i = 0; i != m_elements.size(); i++)
394 delete m_elements[i];
398 u32 GenElementManager::add(GenElement *elem)
400 size_t nelem = m_elements.size();
402 for (size_t i = 0; i != nelem; i++) {
403 if (m_elements[i] == NULL) {
405 m_elements[i] = elem;
410 if (nelem >= this->ELEMENT_LIMIT)
414 m_elements.push_back(elem);
416 verbosestream << "GenElementManager: added " << this->ELEMENT_TITLE
417 << " element '" << elem->name << "'" << std::endl;
423 GenElement *GenElementManager::get(u32 id)
425 return (id < m_elements.size()) ? m_elements[id] : NULL;
429 GenElement *GenElementManager::getByName(const std::string &name)
431 for (size_t i = 0; i != m_elements.size(); i++) {
432 GenElement *elem = m_elements[i];
433 if (elem && name == elem->name)
441 GenElement *GenElementManager::update(u32 id, GenElement *elem)
443 if (id >= m_elements.size())
446 GenElement *old_elem = m_elements[id];
447 m_elements[id] = elem;
452 GenElement *GenElementManager::remove(u32 id)
454 return update(id, NULL);
458 void GenElementManager::clear()