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"
29 #include "content_mapnode.h" // For content_mapnode_get_new_name
30 #include "voxelalgorithms.h"
32 #include "settings.h" // For g_settings
33 #include "main.h" // For g_profiler
35 #include "serialization.h"
36 #include "util/serialize.h"
40 const char *GenElementManager::ELEMENT_TITLE = "element";
42 FlagDesc flagdesc_mapgen[] = {
45 {"dungeons", MG_DUNGEONS},
51 FlagDesc flagdesc_gennotify[] = {
52 {"dungeon", 1 << GENNOTIFY_DUNGEON},
53 {"temple", 1 << GENNOTIFY_TEMPLE},
54 {"cave_begin", 1 << GENNOTIFY_CAVE_BEGIN},
55 {"cave_end", 1 << GENNOTIFY_CAVE_END},
56 {"large_cave_begin", 1 << GENNOTIFY_LARGECAVE_BEGIN},
57 {"large_cave_end", 1 << GENNOTIFY_LARGECAVE_END},
62 ///////////////////////////////////////////////////////////////////////////////
75 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
76 gen_notifications[i] = new std::vector<v3s16>;
81 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
82 delete gen_notifications[i];
86 // Returns Y one under area minimum if not found
87 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
88 v3s16 em = vm->m_area.getExtent();
89 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
90 s16 y_nodes_min = vm->m_area.MinEdge.Y;
91 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
94 for (y = y_nodes_max; y >= y_nodes_min; y--) {
95 MapNode &n = vm->m_data[i];
96 if (ndef->get(n).walkable)
99 vm->m_area.add_y(em, i, -1);
101 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
105 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
106 v3s16 em = vm->m_area.getExtent();
107 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
110 for (y = ymax; y >= ymin; y--) {
111 MapNode &n = vm->m_data[i];
112 if (ndef->get(n).walkable)
115 vm->m_area.add_y(em, i, -1);
121 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
125 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
127 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
128 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
129 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
131 // if the values found are out of range, trust the old heightmap
132 if (y == nmax.Y && heightmap[index] > nmax.Y)
134 if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
137 heightmap[index] = y;
140 //printf("updateHeightmap: %dus\n", t.stop());
144 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
145 bool isliquid, wasliquid;
146 v3s16 em = vm->m_area.getExtent();
148 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
149 for (s16 x = nmin.X; x <= nmax.X; x++) {
152 u32 i = vm->m_area.index(x, nmax.Y, z);
153 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
154 isliquid = ndef->get(vm->m_data[i]).isLiquid();
156 // there was a change between liquid and nonliquid, add to queue.
157 if (isliquid != wasliquid)
158 trans_liquid->push_back(v3s16(x, y, z));
160 wasliquid = isliquid;
161 vm->m_area.add_y(em, i, -1);
168 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
169 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
170 VoxelArea a(nmin, nmax);
172 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
173 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
174 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
175 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
176 vm->m_data[i].param1 = light;
182 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
183 if (light <= 1 || !a.contains(p))
186 u32 vi = vm->m_area.index(p);
187 MapNode &nn = vm->m_data[vi];
190 // should probably compare masked, but doesn't seem to make a difference
191 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
196 lightSpread(a, p + v3s16(0, 0, 1), light);
197 lightSpread(a, p + v3s16(0, 1, 0), light);
198 lightSpread(a, p + v3s16(1, 0, 0), light);
199 lightSpread(a, p - v3s16(0, 0, 1), light);
200 lightSpread(a, p - v3s16(0, 1, 0), light);
201 lightSpread(a, p - v3s16(1, 0, 0), light);
205 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
206 VoxelArea a(nmin, nmax);
207 bool block_is_underground = (water_level >= nmax.Y);
209 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
210 //TimeTaker t("updateLighting");
212 // first, send vertical rays of sunshine downward
213 v3s16 em = vm->m_area.getExtent();
214 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
215 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
216 // see if we can get a light value from the overtop
217 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
218 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
219 if (block_is_underground)
221 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
224 vm->m_area.add_y(em, i, -1);
226 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
227 MapNode &n = vm->m_data[i];
228 if (!ndef->get(n).sunlight_propagates)
230 n.param1 = LIGHT_SUN;
231 vm->m_area.add_y(em, i, -1);
236 // now spread the sunlight and light up any sources
237 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
238 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
239 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
240 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
241 MapNode &n = vm->m_data[i];
242 if (n.getContent() == CONTENT_IGNORE ||
243 !ndef->get(n).light_propagates)
246 u8 light_produced = ndef->get(n).light_source & 0x0F;
248 n.param1 = light_produced;
250 u8 light = n.param1 & 0x0F;
252 lightSpread(a, v3s16(x, y, z + 1), light - 1);
253 lightSpread(a, v3s16(x, y + 1, z ), light - 1);
254 lightSpread(a, v3s16(x + 1, y, z ), light - 1);
255 lightSpread(a, v3s16(x, y, z - 1), light - 1);
256 lightSpread(a, v3s16(x, y - 1, z ), light - 1);
257 lightSpread(a, v3s16(x - 1, y, z ), light - 1);
263 //printf("updateLighting: %dms\n", t.stop());
267 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
268 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
269 VoxelArea a(nmin, nmax);
270 bool block_is_underground = (water_level > nmax.Y);
271 bool sunlight = !block_is_underground;
273 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
275 for (int i = 0; i < 2; i++) {
276 enum LightBank bank = banks[i];
277 std::set<v3s16> light_sources;
278 std::map<v3s16, u8> unlight_from;
280 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
281 light_sources, unlight_from);
282 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
284 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
285 vm->spreadLight(bank, light_sources, ndef);
290 ///////////////////////////////////////////////////////////////////////////////
293 GenElementManager::~GenElementManager()
295 for (size_t i = 0; i != m_elements.size(); i++)
296 delete m_elements[i];
300 u32 GenElementManager::add(GenElement *elem)
302 size_t nelem = m_elements.size();
304 for (size_t i = 0; i != nelem; i++) {
305 if (m_elements[i] == NULL) {
307 m_elements[i] = elem;
312 if (nelem >= this->ELEMENT_LIMIT)
316 m_elements.push_back(elem);
318 verbosestream << "GenElementManager: added " << this->ELEMENT_TITLE
319 << " element '" << elem->name << "'" << std::endl;
325 GenElement *GenElementManager::get(u32 id)
327 return (id < m_elements.size()) ? m_elements[id] : NULL;
331 GenElement *GenElementManager::getByName(const char *name)
333 for (size_t i = 0; i != m_elements.size(); i++) {
334 GenElement *elem = m_elements[i];
335 if (elem && !strcmp(elem->name.c_str(), name))
342 GenElement *GenElementManager::getByName(std::string &name)
344 return getByName(name.c_str());
348 GenElement *GenElementManager::update(u32 id, GenElement *elem)
350 if (id >= m_elements.size())
353 GenElement *old_elem = m_elements[id];
354 m_elements[id] = elem;
359 GenElement *GenElementManager::remove(u32 id)
361 return update(id, NULL);