]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen.cpp
Optimize Mapgen::updateLighting(), add setLighting()
[dragonfireclient.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 "biome.h"
24 #include "mapblock.h"
25 #include "mapnode.h"
26 #include "map.h"
27 //#include "serverobject.h"
28 #include "content_sao.h"
29 #include "nodedef.h"
30 #include "content_mapnode.h" // For content_mapnode_get_new_name
31 #include "voxelalgorithms.h"
32 #include "profiler.h"
33 #include "settings.h" // For g_settings
34 #include "main.h" // For g_profiler
35 #include "treegen.h"
36 #include "mapgen_v6.h"
37
38 FlagDesc flagdesc_mapgen[] = {
39         {"trees",          MG_TREES},
40         {"caves",          MG_CAVES},
41         {"dungeons",       MG_DUNGEONS},
42         {"v6_forests",     MGV6_FORESTS},
43         {"v6_biome_blend", MGV6_BIOME_BLEND},
44         {"flat",           MG_FLAT},
45         {NULL,                     0}
46 };
47
48
49 ///////////////////////////////////////////////////////////////////////////////
50
51
52 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
53         bool isliquid, wasliquid;
54         v3s16 em  = vm->m_area.getExtent();
55
56         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
57                 for (s16 x = nmin.X; x <= nmax.X; x++) {
58                         wasliquid = true;
59                         
60                         u32 i = vm->m_area.index(x, nmax.Y, z);
61                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
62                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
63                                 
64                                 // there was a change between liquid and nonliquid, add to queue
65                                 if (isliquid != wasliquid)
66                                         trans_liquid->push_back(v3s16(x, y, z));
67
68                                 wasliquid = isliquid;
69                                 vm->m_area.add_y(em, i, -1);
70                         }
71                 }
72         }
73 }
74
75
76 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
77         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
78         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
79                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
80
81         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
82                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
83                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
84                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
85                                 vm->m_data[i].param1 = light;
86                 }
87         }
88 }
89
90
91 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
92         if (light <= 1 || !a.contains(p))
93                 return;
94                 
95         u32 vi = vm->m_area.index(p);
96         MapNode &nn = vm->m_data[vi];
97
98         light--;
99         // should probably compare masked, but doesn't seem to make a difference
100         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
101                 return;
102         
103         nn.param1 = light;
104         
105         lightSpread(a, p + v3s16(0, 0, 1), light);
106         lightSpread(a, p + v3s16(0, 1, 0), light);
107         lightSpread(a, p + v3s16(1, 0, 0), light);
108         lightSpread(a, p - v3s16(0, 0, 1), light);
109         lightSpread(a, p - v3s16(0, 1, 0), light);
110         lightSpread(a, p - v3s16(1, 0, 0), light);
111 }
112
113
114 void Mapgen::updateLighting(v3s16 nmin, v3s16 nmax) {
115         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
116                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
117         bool block_is_underground = (water_level >= nmax.Y);
118
119         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
120         //TimeTaker t("updateLighting");
121
122         // first, send vertical rays of sunshine downward
123         v3s16 em = vm->m_area.getExtent();
124         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
125                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
126                         // see if we can get a light value from the overtop
127                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
128                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
129                                 if (block_is_underground)
130                                         continue;
131                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
132                                 continue;
133                         }
134                         vm->m_area.add_y(em, i, -1);
135
136                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
137                                 MapNode &n = vm->m_data[i];
138                                 if (!ndef->get(n).sunlight_propagates)
139                                         break;
140                                 n.param1 = LIGHT_SUN;
141                                 vm->m_area.add_y(em, i, -1);
142                         }
143                 }
144         }
145         
146         // now spread the sunlight and light up any sources
147         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
148                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
149                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
150                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
151                                 MapNode &n = vm->m_data[i];
152                                 if (n.getContent() == CONTENT_IGNORE ||
153                                         !ndef->get(n).light_propagates)
154                                         continue;
155                                 
156                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
157                                 if (light_produced)
158                                         n.param1 = light_produced;
159                                 
160                                 u8 light = n.param1 & 0x0F;
161                                 if (light) {
162                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
163                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
164                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
165                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
166                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
167                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
168                                 }
169                         }
170                 }
171         }
172         
173         //printf("updateLighting: %dms\n", t.stop());
174 }
175
176
177 void Mapgen::updateLightingOld(v3s16 nmin, v3s16 nmax) {
178         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
179
180         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
181                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
182         bool block_is_underground = (water_level > nmax.Y);
183         bool sunlight = !block_is_underground;
184
185         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
186         
187         for (int i = 0; i < 2; i++) {
188                 enum LightBank bank = banks[i];
189                 std::set<v3s16> light_sources;
190                 std::map<v3s16, u8> unlight_from;
191
192                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
193                                         light_sources, unlight_from);
194                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
195
196                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
197                 vm->spreadLight(bank, light_sources, ndef);
198         }
199 }
200
201
202 //////////////////////// Mapgen V6 parameter read/write
203
204 bool MapgenV6Params::readParams(Settings *settings) {
205         freq_desert = settings->getFloat("mgv6_freq_desert");
206         freq_beach  = settings->getFloat("mgv6_freq_beach");
207
208         np_terrain_base   = settings->getNoiseParams("mgv6_np_terrain_base");
209         np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
210         np_steepness      = settings->getNoiseParams("mgv6_np_steepness");
211         np_height_select  = settings->getNoiseParams("mgv6_np_height_select");
212         np_trees          = settings->getNoiseParams("mgv6_np_trees");
213         np_mud            = settings->getNoiseParams("mgv6_np_mud");
214         np_beach          = settings->getNoiseParams("mgv6_np_beach");
215         np_biome          = settings->getNoiseParams("mgv6_np_biome");
216         np_cave           = settings->getNoiseParams("mgv6_np_cave");
217
218         bool success =
219                 np_terrain_base  && np_terrain_higher && np_steepness &&
220                 np_height_select && np_trees          && np_mud       &&
221                 np_beach         && np_biome          && np_cave;
222         return success;
223 }
224
225
226 void MapgenV6Params::writeParams(Settings *settings) {
227         settings->setFloat("mgv6_freq_desert", freq_desert);
228         settings->setFloat("mgv6_freq_beach",  freq_beach);
229         
230         settings->setNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
231         settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
232         settings->setNoiseParams("mgv6_np_steepness",      np_steepness);
233         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
234         settings->setNoiseParams("mgv6_np_trees",          np_trees);
235         settings->setNoiseParams("mgv6_np_mud",            np_mud);
236         settings->setNoiseParams("mgv6_np_beach",          np_beach);
237         settings->setNoiseParams("mgv6_np_biome",          np_biome);
238         settings->setNoiseParams("mgv6_np_cave",           np_cave);
239 }
240
241
242 /////////////////////////////////// legacy static functions for farmesh
243
244
245 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
246         //just need to return something
247         s16 level = 5;
248         return level;
249 }
250
251
252 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
253         double sandnoise = noise2d_perlin(
254                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
255                         seed+59420, 3, 0.50);
256
257         return (sandnoise > 0.15);
258 }
259
260
261 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
262         double noise = noise2d_perlin(
263                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
264                         seed+2, 4, 0.66);
265         double zeroval = -0.39;
266         if(noise < zeroval)
267                 return 0;
268         else
269                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
270 }