Split up mapgen.cpp
[oweals/minetest.git] / src / mapgen.cpp
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "mapgen.h"
21 #include "voxel.h"
22 #include "noise.h"
23 #include "mg_biome.h"
24 #include "mapblock.h"
25 #include "mapnode.h"
26 #include "map.h"
27 #include "content_sao.h"
28 #include "nodedef.h"
29 #include "content_mapnode.h" // For content_mapnode_get_new_name
30 #include "voxelalgorithms.h"
31 #include "profiler.h"
32 #include "settings.h" // For g_settings
33 #include "main.h" // For g_profiler
34 #include "treegen.h"
35 #include "serialization.h"
36 #include "util/serialize.h"
37 #include "filesys.h"
38 #include "log.h"
39
40
41 FlagDesc flagdesc_mapgen[] = {
42         {"trees",    MG_TREES},
43         {"caves",    MG_CAVES},
44         {"dungeons", MG_DUNGEONS},
45         {"flat",     MG_FLAT},
46         {"light",    MG_LIGHT},
47         {NULL,       0}
48 };
49
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},
57         {NULL,               0}
58 };
59
60 ///////////////////////////////////////////////////////////////////////////////
61
62
63
64
65
66 ///////////////////////////////////////////////////////////////////////////////
67
68
69
70 Mapgen::Mapgen() {
71         seed        = 0;
72         water_level = 0;
73         generating  = false;
74         id          = -1;
75         vm          = NULL;
76         ndef        = NULL;
77         heightmap   = NULL;
78         biomemap    = NULL;
79
80         for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
81                 gen_notifications[i] = new std::vector<v3s16>;
82 }
83
84
85 Mapgen::~Mapgen() {
86         for (unsigned int i = 0; i != NUM_GEN_NOTIFY; i++)
87                 delete gen_notifications[i];
88 }
89
90
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);
97         s16 y;
98
99         for (y = y_nodes_max; y >= y_nodes_min; y--) {
100                 MapNode &n = vm->m_data[i];
101                 if (ndef->get(n).walkable)
102                         break;
103
104                 vm->m_area.add_y(em, i, -1);
105         }
106         return (y >= y_nodes_min) ? y : y_nodes_min - 1;
107 }
108
109
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);
113         s16 y;
114
115         for (y = ymax; y >= ymin; y--) {
116                 MapNode &n = vm->m_data[i];
117                 if (ndef->get(n).walkable)
118                         break;
119
120                 vm->m_area.add_y(em, i, -1);
121         }
122         return y;
123 }
124
125
126 void Mapgen::updateHeightmap(v3s16 nmin, v3s16 nmax) {
127         if (!heightmap)
128                 return;
129
130         //TimeTaker t("Mapgen::updateHeightmap", NULL, PRECISION_MICRO);
131         int index = 0;
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);
135
136                         // if the values found are out of range, trust the old heightmap
137                         if (y == nmax.Y && heightmap[index] > nmax.Y)
138                                 continue;
139                         if (y == nmin.Y - 1 && heightmap[index] < nmin.Y)
140                                 continue;
141
142                         heightmap[index] = y;
143                 }
144         }
145         //printf("updateHeightmap: %dus\n", t.stop());
146 }
147
148
149 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
150         bool isliquid, wasliquid;
151         v3s16 em  = vm->m_area.getExtent();
152
153         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
154                 for (s16 x = nmin.X; x <= nmax.X; x++) {
155                         wasliquid = true;
156
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();
160
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));
164
165                                 wasliquid = isliquid;
166                                 vm->m_area.add_y(em, i, -1);
167                         }
168                 }
169         }
170 }
171
172
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);
176
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;
182                 }
183         }
184 }
185
186
187 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
188         if (light <= 1 || !a.contains(p))
189                 return;
190
191         u32 vi = vm->m_area.index(p);
192         MapNode &nn = vm->m_data[vi];
193
194         light--;
195         // should probably compare masked, but doesn't seem to make a difference
196         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
197                 return;
198
199         nn.param1 = light;
200
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);
207 }
208
209
210 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
211         VoxelArea a(nmin, nmax);
212         bool block_is_underground = (water_level >= nmax.Y);
213
214         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
215         //TimeTaker t("updateLighting");
216
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)
225                                         continue;
226                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
227                                 continue;
228                         }
229                         vm->m_area.add_y(em, i, -1);
230
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)
234                                         break;
235                                 n.param1 = LIGHT_SUN;
236                                 vm->m_area.add_y(em, i, -1);
237                         }
238                 }
239         }
240
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)
249                                         continue;
250
251                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
252                                 if (light_produced)
253                                         n.param1 = light_produced;
254
255                                 u8 light = n.param1 & 0x0F;
256                                 if (light) {
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);
263                                 }
264                         }
265                 }
266         }
267
268         //printf("updateLighting: %dms\n", t.stop());
269 }
270
271
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;
277
278         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
279
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;
284
285                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
286                                                                                          light_sources, unlight_from);
287                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
288
289                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
290                 vm->spreadLight(bank, light_sources, ndef);
291         }
292 }