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"
41 FlagDesc flagdesc_mapgen[] = {
44 {"dungeons", MG_DUNGEONS},
50 FlagDesc flagdesc_gennotify[] = {
51 {"dungeon", 1 << GENNOTIFY_DUNGEON},
52 {"temple", 1 << GENNOTIFY_TEMPLE},
53 {"cave_begin", 1 << GENNOTIFY_CAVE_BEGIN},
54 {"cave_end", 1 << GENNOTIFY_CAVE_END},
55 {"large_cave_begin", 1 << GENNOTIFY_LARGECAVE_BEGIN},
56 {"large_cave_end", 1 << GENNOTIFY_LARGECAVE_END},
60 ///////////////////////////////////////////////////////////////////////////////
66 ///////////////////////////////////////////////////////////////////////////////
80 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
81 gen_notifications[i] = new std::vector<v3s16>;
86 for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
87 delete gen_notifications[i];
91 // Returns Y one under area minimum if not found
92 s16 Mapgen::findGroundLevelFull(v2s16 p2d) {
93 v3s16 em = vm->m_area.getExtent();
94 s16 y_nodes_max = vm->m_area.MaxEdge.Y;
95 s16 y_nodes_min = vm->m_area.MinEdge.Y;
96 u32 i = vm->m_area.index(p2d.X, y_nodes_max, p2d.Y);
99 for (y = y_nodes_max; y >= y_nodes_min; y--) {
100 MapNode &n = vm->m_data[i];
101 if (ndef->get(n).walkable)
104 vm->m_area.add_y(em, i, -1);
106 return (y >= y_nodes_min) ? y : y_nodes_min - 1;
110 s16 Mapgen::findGroundLevel(v2s16 p2d, s16 ymin, s16 ymax) {
111 v3s16 em = vm->m_area.getExtent();
112 u32 i = vm->m_area.index(p2d.X, ymax, p2d.Y);
115 for (y = ymax; y >= ymin; y--) {
116 MapNode &n = vm->m_data[i];
117 if (ndef->get(n).walkable)
120 vm->m_area.add_y(em, i, -1);
126 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
130 //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
132 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
133 for (s16 x = nmin.X; x <= nmax.X; x++, index++) {
134 s16 y = findGroundLevel(v2s16(x, z), nmin.Y, nmax.Y);
136 // if the values found are out of range, trust the old heightmap
137 if (y == nmax.Y && heightmap[index] > nmax.Y)
139 if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
142 heightmap[index] = y;
145 //printf("updateHeightmap: %dus\n", t.stop());
149 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
150 bool isliquid, wasliquid;
151 v3s16 em = vm->m_area.getExtent();
153 for (s16 z = nmin.Z; z <= nmax.Z; z++) {
154 for (s16 x = nmin.X; x <= nmax.X; x++) {
157 u32 i = vm->m_area.index(x, nmax.Y, z);
158 for (s16 y = nmax.Y; y >= nmin.Y; y--) {
159 isliquid = ndef->get(vm->m_data[i]).isLiquid();
161 // there was a change between liquid and nonliquid, add to queue.
162 if (isliquid != wasliquid)
163 trans_liquid->push_back(v3s16(x, y, z));
165 wasliquid = isliquid;
166 vm->m_area.add_y(em, i, -1);
173 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
174 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
175 VoxelArea a(nmin, nmax);
177 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
178 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
179 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
180 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
181 vm->m_data[i].param1 = light;
187 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
188 if (light <= 1 || !a.contains(p))
191 u32 vi = vm->m_area.index(p);
192 MapNode &nn = vm->m_data[vi];
195 // should probably compare masked, but doesn't seem to make a difference
196 if (light <= nn.param1 || !ndef->get(nn).light_propagates)
201 lightSpread(a, p + v3s16(0, 0, 1), light);
202 lightSpread(a, p + v3s16(0, 1, 0), light);
203 lightSpread(a, p + v3s16(1, 0, 0), light);
204 lightSpread(a, p - v3s16(0, 0, 1), light);
205 lightSpread(a, p - v3s16(0, 1, 0), light);
206 lightSpread(a, p - v3s16(1, 0, 0), light);
210 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
211 VoxelArea a(nmin, nmax);
212 bool block_is_underground = (water_level >= nmax.Y);
214 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
215 //TimeTaker t("updateLighting");
217 // first, send vertical rays of sunshine downward
218 v3s16 em = vm->m_area.getExtent();
219 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
220 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
221 // see if we can get a light value from the overtop
222 u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
223 if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
224 if (block_is_underground)
226 } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
229 vm->m_area.add_y(em, i, -1);
231 for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
232 MapNode &n = vm->m_data[i];
233 if (!ndef->get(n).sunlight_propagates)
235 n.param1 = LIGHT_SUN;
236 vm->m_area.add_y(em, i, -1);
241 // now spread the sunlight and light up any sources
242 for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
243 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
244 u32 i = vm->m_area.index(a.MinEdge.X, y, z);
245 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
246 MapNode &n = vm->m_data[i];
247 if (n.getContent() == CONTENT_IGNORE ||
248 !ndef->get(n).light_propagates)
251 u8 light_produced = ndef->get(n).light_source & 0x0F;
253 n.param1 = light_produced;
255 u8 light = n.param1 & 0x0F;
257 lightSpread(a, v3s16(x, y, z + 1), light - 1);
258 lightSpread(a, v3s16(x, y + 1, z ), light - 1);
259 lightSpread(a, v3s16(x + 1, y, z ), light - 1);
260 lightSpread(a, v3s16(x, y, z - 1), light - 1);
261 lightSpread(a, v3s16(x, y - 1, z ), light - 1);
262 lightSpread(a, v3s16(x - 1, y, z ), light - 1);
268 //printf("updateLighting: %dms\n", t.stop());
272 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
273 enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
274 VoxelArea a(nmin, nmax);
275 bool block_is_underground = (water_level > nmax.Y);
276 bool sunlight = !block_is_underground;
278 ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
280 for (int i = 0; i < 2; i++) {
281 enum LightBank bank = banks[i];
282 std::set<v3s16> light_sources;
283 std::map<v3s16, u8> unlight_from;
285 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
286 light_sources, unlight_from);
287 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
289 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
290 vm->spreadLight(bank, light_sources, ndef);