]> git.lizzy.rs Git - dragonfireclient.git/blob - src/mapgen.cpp
Vary ore sheet y position by noise
[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_jungles",     MGV6_JUNGLES},
43         {"v6_biome_blend", MGV6_BIOME_BLEND},
44         {"flat",           MG_FLAT},
45         {NULL,                     0}
46 };
47
48
49 ///////////////////////////////////////////////////////////////////////////////
50
51
52 Ore *createOre(OreType type) {
53         switch (type) {
54                 case ORE_SCATTER:
55                         return new OreScatter;
56                 case ORE_SHEET:
57                         return new OreSheet;
58                 //case ORE_CLAYLIKE: //TODO: implement this!
59                 //      return new OreClaylike;
60                 default:
61                         return NULL;
62         }
63 }
64
65
66 void Ore::resolveNodeNames(INodeDefManager *ndef) {
67         if (ore == CONTENT_IGNORE) {
68                 ore = ndef->getId(ore_name);
69                 if (ore == CONTENT_IGNORE) {
70                         errorstream << "Ore::resolveNodeNames: ore node '"
71                                 << ore_name << "' not defined";
72                         ore     = CONTENT_AIR;
73                         wherein = CONTENT_AIR;
74                 }
75         }
76         
77         if (wherein == CONTENT_IGNORE) {
78                 wherein = ndef->getId(wherein_name);
79                 if (wherein == CONTENT_IGNORE) {
80                         errorstream << "Ore::resolveNodeNames: wherein node '"
81                                 << wherein_name << "' not defined";
82                         ore     = CONTENT_AIR;
83                         wherein = CONTENT_AIR;
84                 }
85         }
86 }
87
88
89 void OreScatter::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
90         if (nmin.Y > height_max || nmax.Y < height_min)
91                 return;
92         
93         resolveNodeNames(mg->ndef);
94         
95         MapNode n_ore(ore);
96         ManualMapVoxelManipulator *vm = mg->vm;
97         PseudoRandom pr(blockseed);
98         
99         int ymin   = MYMAX(nmin.Y, height_min);
100         int ymax   = MYMIN(nmax.Y, height_max);
101         if (clust_size >= ymax - ymin + 1)
102                 return;
103         
104         int volume = (nmax.X - nmin.X + 1) *
105                                  (nmax.Y - nmin.Y + 1) *
106                                  (nmax.Z - nmin.Z + 1);
107         int csize     = clust_size;
108         int orechance = (csize * csize * csize) / clust_num_ores;
109         int nclusters = volume / clust_scarcity;
110
111         for (int i = 0; i != nclusters; i++) {
112                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
113                 int y0 = pr.range(ymin,   ymax   - csize + 1);
114                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
115                 
116                 if (np && (NoisePerlin3D(np, x0, y0, z0, mg->seed) < nthresh))
117                         continue;
118                 
119                 for (int z1 = 0; z1 != csize; z1++)
120                 for (int y1 = 0; y1 != csize; y1++)
121                 for (int x1 = 0; x1 != csize; x1++) {
122                         if (pr.range(1, orechance) != 1)
123                                 continue;
124                         
125                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
126                         if (vm->m_data[i].getContent() == wherein)
127                                 vm->m_data[i] = n_ore;
128                 }
129         }
130 }
131
132
133 void OreSheet::generate(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax) {
134         if (nmin.Y > height_max || nmax.Y < height_min)
135                 return;
136
137         resolveNodeNames(mg->ndef);
138
139         MapNode n_ore(ore);
140         ManualMapVoxelManipulator *vm = mg->vm;
141         PseudoRandom pr(blockseed + 4234);
142         
143         int ymin = MYMAX(nmin.Y, height_min);
144         int ymax = MYMIN(nmax.Y, height_max);
145         if (clust_size >= ymax - ymin + 1)
146                 return;
147                 
148         int x0 = nmin.X;
149         int z0 = nmin.Z;
150         
151         int x1 = nmax.X;
152         int z1 = nmax.Z;
153         
154         int max_height = clust_size;
155         int y_start = pr.range(ymin, ymax - max_height);
156         
157         if (!noise) {
158                 int sx = nmax.X - nmin.X + 1;
159                 int sz = nmax.Z - nmin.Z + 1;
160                 noise = new Noise(np, 0, sx, sz);
161         }
162         noise->seed = mg->seed + y_start;
163         noise->perlinMap2D(x0, z0);
164         
165         int index = 0;
166         for (int z = z0; z != z1; z++)
167         for (int x = x0; x != x1; x++) {
168                 float noiseval = noise->result[index++];
169                 if (noiseval < nthresh)
170                         continue;
171                         
172                 int height = max_height * (1. / pr.range(1, 3));
173                 int y0 = y_start + np->scale * noiseval; //pr.range(1, 3) - 1;
174                 int y1 = y0 + height;
175                 for (int y = y0; y != y1; y++) {
176                         u32 i = vm->m_area.index(x, y, z);
177                         if (!vm->m_area.contains(i))
178                                 continue;
179                                 
180                         if (vm->m_data[i].getContent() == wherein)
181                                 vm->m_data[i] = n_ore;
182                 }
183         }
184 }
185
186
187 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
188         bool isliquid, wasliquid;
189         v3s16 em  = vm->m_area.getExtent();
190
191         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
192                 for (s16 x = nmin.X; x <= nmax.X; x++) {
193                         wasliquid = true;
194                         
195                         u32 i = vm->m_area.index(x, nmax.Y, z);
196                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
197                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
198                                 
199                                 // there was a change between liquid and nonliquid, add to queue
200                                 if (isliquid != wasliquid)
201                                         trans_liquid->push_back(v3s16(x, y, z));
202
203                                 wasliquid = isliquid;
204                                 vm->m_area.add_y(em, i, -1);
205                         }
206                 }
207         }
208 }
209
210
211 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
212         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
213         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
214                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
215
216         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
217                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
218                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
219                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
220                                 vm->m_data[i].param1 = light;
221                 }
222         }
223 }
224
225
226 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
227         if (light <= 1 || !a.contains(p))
228                 return;
229                 
230         u32 vi = vm->m_area.index(p);
231         MapNode &nn = vm->m_data[vi];
232
233         light--;
234         // should probably compare masked, but doesn't seem to make a difference
235         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
236                 return;
237         
238         nn.param1 = light;
239         
240         lightSpread(a, p + v3s16(0, 0, 1), light);
241         lightSpread(a, p + v3s16(0, 1, 0), light);
242         lightSpread(a, p + v3s16(1, 0, 0), light);
243         lightSpread(a, p - v3s16(0, 0, 1), light);
244         lightSpread(a, p - v3s16(0, 1, 0), light);
245         lightSpread(a, p - v3s16(1, 0, 0), light);
246 }
247
248
249 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
250         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
251                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
252         bool block_is_underground = (water_level >= nmax.Y);
253
254         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
255         //TimeTaker t("updateLighting");
256
257         // first, send vertical rays of sunshine downward
258         v3s16 em = vm->m_area.getExtent();
259         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
260                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
261                         // see if we can get a light value from the overtop
262                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
263                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
264                                 if (block_is_underground)
265                                         continue;
266                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
267                                 continue;
268                         }
269                         vm->m_area.add_y(em, i, -1);
270
271                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
272                                 MapNode &n = vm->m_data[i];
273                                 if (!ndef->get(n).sunlight_propagates)
274                                         break;
275                                 n.param1 = LIGHT_SUN;
276                                 vm->m_area.add_y(em, i, -1);
277                         }
278                 }
279         }
280         
281         // now spread the sunlight and light up any sources
282         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
283                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
284                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
285                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
286                                 MapNode &n = vm->m_data[i];
287                                 if (n.getContent() == CONTENT_IGNORE ||
288                                         !ndef->get(n).light_propagates)
289                                         continue;
290                                 
291                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
292                                 if (light_produced)
293                                         n.param1 = light_produced;
294                                 
295                                 u8 light = n.param1 & 0x0F;
296                                 if (light) {
297                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
298                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
299                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
300                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
301                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
302                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
303                                 }
304                         }
305                 }
306         }
307         
308         //printf("updateLighting: %dms\n", t.stop());
309 }
310
311
312 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
313         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
314
315         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
316                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
317         bool block_is_underground = (water_level > nmax.Y);
318         bool sunlight = !block_is_underground;
319
320         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
321         
322         for (int i = 0; i < 2; i++) {
323                 enum LightBank bank = banks[i];
324                 std::set<v3s16> light_sources;
325                 std::map<v3s16, u8> unlight_from;
326
327                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
328                                         light_sources, unlight_from);
329                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
330
331                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
332                 vm->spreadLight(bank, light_sources, ndef);
333         }
334 }
335
336
337 //////////////////////// Mapgen V6 parameter read/write
338
339 bool MapgenV6Params::readParams(Settings *settings) {
340         freq_desert = settings->getFloat("mgv6_freq_desert");
341         freq_beach  = settings->getFloat("mgv6_freq_beach");
342
343         np_terrain_base   = settings->getNoiseParams("mgv6_np_terrain_base");
344         np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
345         np_steepness      = settings->getNoiseParams("mgv6_np_steepness");
346         np_height_select  = settings->getNoiseParams("mgv6_np_height_select");
347         np_mud            = settings->getNoiseParams("mgv6_np_mud");
348         np_beach          = settings->getNoiseParams("mgv6_np_beach");
349         np_biome          = settings->getNoiseParams("mgv6_np_biome");
350         np_cave           = settings->getNoiseParams("mgv6_np_cave");
351         np_humidity       = settings->getNoiseParams("mgv6_np_humidity");
352         np_trees          = settings->getNoiseParams("mgv6_np_trees");
353         np_apple_trees    = settings->getNoiseParams("mgv6_np_apple_trees");
354
355         bool success =
356                 np_terrain_base  && np_terrain_higher && np_steepness &&
357                 np_height_select && np_trees          && np_mud       &&
358                 np_beach         && np_biome          && np_cave      &&
359                 np_humidity      && np_apple_trees;
360         return success;
361 }
362
363
364 void MapgenV6Params::writeParams(Settings *settings) {
365         settings->setFloat("mgv6_freq_desert", freq_desert);
366         settings->setFloat("mgv6_freq_beach",  freq_beach);
367         
368         settings->setNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
369         settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
370         settings->setNoiseParams("mgv6_np_steepness",      np_steepness);
371         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
372         settings->setNoiseParams("mgv6_np_mud",            np_mud);
373         settings->setNoiseParams("mgv6_np_beach",          np_beach);
374         settings->setNoiseParams("mgv6_np_biome",          np_biome);
375         settings->setNoiseParams("mgv6_np_cave",           np_cave);
376         settings->setNoiseParams("mgv6_np_humidity",       np_humidity);
377         settings->setNoiseParams("mgv6_np_trees",          np_trees);
378         settings->setNoiseParams("mgv6_np_apple_trees",    np_apple_trees);
379 }
380
381
382 /////////////////////////////////// legacy static functions for farmesh
383
384
385 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
386         //just need to return something
387         s16 level = 5;
388         return level;
389 }
390
391
392 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
393         double sandnoise = noise2d_perlin(
394                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
395                         seed+59420, 3, 0.50);
396
397         return (sandnoise > 0.15);
398 }
399
400
401 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
402         double noise = noise2d_perlin(
403                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
404                         seed+2, 4, 0.66);
405         double zeroval = -0.39;
406         if(noise < zeroval)
407                 return 0;
408         else
409                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
410 }