Add Ore infrastructure and l_register_ore()
[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 "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         
146         int x0 = nmin.X;
147         int z0 = nmin.Z;
148         
149         int x1 = nmax.X;
150         int z1 = nmax.Z;
151         
152         int max_height = clust_size;
153         
154         int y_start = pr.range(ymin, ymax - max_height);
155         
156         if (!noise) {
157                 int sx = nmax.X - nmin.X + 1;
158                 int sz = nmax.Z - nmin.Z + 1;
159                 noise = new Noise(np, 0, sx, sz);
160         }
161         noise->seed = mg->seed + y_start;
162         noise->perlinMap2D(x0, z0);
163         
164         int index = 0;
165         for (int z = z0; z != z1; z++)
166         for (int x = x0; x != x1; x++) {
167                 
168                 if (noise->result[index++] < nthresh)
169                         continue;
170                         
171                 int height = max_height * (1. / pr.range(1, 3));
172                 int y0 = y_start + pr.range(1, 3) - 1;
173                 int y1 = y0 + height;
174                 for (int y = y0; y != y1; y++) {
175                         u32 i = vm->m_area.index(x, y, z);
176                         if (!vm->m_area.contains(i))
177                                 continue;
178                                 
179                         if (vm->m_data[i].getContent() == wherein)
180                                 vm->m_data[i] = n_ore;
181                 }
182         }
183 }
184
185
186 void Mapgen::updateLiquid(UniqueQueue<v3s16> *trans_liquid, v3s16 nmin, v3s16 nmax) {
187         bool isliquid, wasliquid;
188         v3s16 em  = vm->m_area.getExtent();
189
190         for (s16 z = nmin.Z; z <= nmax.Z; z++) {
191                 for (s16 x = nmin.X; x <= nmax.X; x++) {
192                         wasliquid = true;
193                         
194                         u32 i = vm->m_area.index(x, nmax.Y, z);
195                         for (s16 y = nmax.Y; y >= nmin.Y; y--) {
196                                 isliquid = ndef->get(vm->m_data[i]).isLiquid();
197                                 
198                                 // there was a change between liquid and nonliquid, add to queue
199                                 if (isliquid != wasliquid)
200                                         trans_liquid->push_back(v3s16(x, y, z));
201
202                                 wasliquid = isliquid;
203                                 vm->m_area.add_y(em, i, -1);
204                         }
205                 }
206         }
207 }
208
209
210 void Mapgen::setLighting(v3s16 nmin, v3s16 nmax, u8 light) {
211         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
212         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
213                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
214
215         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
216                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
217                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
218                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++)
219                                 vm->m_data[i].param1 = light;
220                 }
221         }
222 }
223
224
225 void Mapgen::lightSpread(VoxelArea &a, v3s16 p, u8 light) {
226         if (light <= 1 || !a.contains(p))
227                 return;
228                 
229         u32 vi = vm->m_area.index(p);
230         MapNode &nn = vm->m_data[vi];
231
232         light--;
233         // should probably compare masked, but doesn't seem to make a difference
234         if (light <= nn.param1 || !ndef->get(nn).light_propagates)
235                 return;
236         
237         nn.param1 = light;
238         
239         lightSpread(a, p + v3s16(0, 0, 1), light);
240         lightSpread(a, p + v3s16(0, 1, 0), light);
241         lightSpread(a, p + v3s16(1, 0, 0), light);
242         lightSpread(a, p - v3s16(0, 0, 1), light);
243         lightSpread(a, p - v3s16(0, 1, 0), light);
244         lightSpread(a, p - v3s16(1, 0, 0), light);
245 }
246
247
248 void Mapgen::calcLighting(v3s16 nmin, v3s16 nmax) {
249         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
250                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
251         bool block_is_underground = (water_level >= nmax.Y);
252
253         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
254         //TimeTaker t("updateLighting");
255
256         // first, send vertical rays of sunshine downward
257         v3s16 em = vm->m_area.getExtent();
258         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
259                 for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++) {
260                         // see if we can get a light value from the overtop
261                         u32 i = vm->m_area.index(x, a.MaxEdge.Y + 1, z);
262                         if (vm->m_data[i].getContent() == CONTENT_IGNORE) {
263                                 if (block_is_underground)
264                                         continue;
265                         } else if ((vm->m_data[i].param1 & 0x0F) != LIGHT_SUN) {
266                                 continue;
267                         }
268                         vm->m_area.add_y(em, i, -1);
269
270                         for (int y = a.MaxEdge.Y; y >= a.MinEdge.Y; y--) {
271                                 MapNode &n = vm->m_data[i];
272                                 if (!ndef->get(n).sunlight_propagates)
273                                         break;
274                                 n.param1 = LIGHT_SUN;
275                                 vm->m_area.add_y(em, i, -1);
276                         }
277                 }
278         }
279         
280         // now spread the sunlight and light up any sources
281         for (int z = a.MinEdge.Z; z <= a.MaxEdge.Z; z++) {
282                 for (int y = a.MinEdge.Y; y <= a.MaxEdge.Y; y++) {
283                         u32 i = vm->m_area.index(a.MinEdge.X, y, z);
284                         for (int x = a.MinEdge.X; x <= a.MaxEdge.X; x++, i++) {
285                                 MapNode &n = vm->m_data[i];
286                                 if (n.getContent() == CONTENT_IGNORE ||
287                                         !ndef->get(n).light_propagates)
288                                         continue;
289                                 
290                                 u8 light_produced = ndef->get(n).light_source & 0x0F;
291                                 if (light_produced)
292                                         n.param1 = light_produced;
293                                 
294                                 u8 light = n.param1 & 0x0F;
295                                 if (light) {
296                                         lightSpread(a, v3s16(x,     y,     z + 1), light);
297                                         lightSpread(a, v3s16(x,     y + 1, z    ), light);
298                                         lightSpread(a, v3s16(x + 1, y,     z    ), light);
299                                         lightSpread(a, v3s16(x,     y,     z - 1), light);
300                                         lightSpread(a, v3s16(x,     y - 1, z    ), light);
301                                         lightSpread(a, v3s16(x - 1, y,     z    ), light);
302                                 }
303                         }
304                 }
305         }
306         
307         //printf("updateLighting: %dms\n", t.stop());
308 }
309
310
311 void Mapgen::calcLightingOld(v3s16 nmin, v3s16 nmax) {
312         enum LightBank banks[2] = {LIGHTBANK_DAY, LIGHTBANK_NIGHT};
313
314         VoxelArea a(nmin - v3s16(1,0,1) * MAP_BLOCKSIZE,
315                                 nmax + v3s16(1,0,1) * MAP_BLOCKSIZE);
316         bool block_is_underground = (water_level > nmax.Y);
317         bool sunlight = !block_is_underground;
318
319         ScopeProfiler sp(g_profiler, "EmergeThread: mapgen lighting update", SPT_AVG);
320         
321         for (int i = 0; i < 2; i++) {
322                 enum LightBank bank = banks[i];
323                 std::set<v3s16> light_sources;
324                 std::map<v3s16, u8> unlight_from;
325
326                 voxalgo::clearLightAndCollectSources(*vm, a, bank, ndef,
327                                         light_sources, unlight_from);
328                 voxalgo::propagateSunlight(*vm, a, sunlight, light_sources, ndef);
329
330                 vm->unspreadLight(bank, unlight_from, light_sources, ndef);
331                 vm->spreadLight(bank, light_sources, ndef);
332         }
333 }
334
335
336 //////////////////////// Mapgen V6 parameter read/write
337
338 bool MapgenV6Params::readParams(Settings *settings) {
339         freq_desert = settings->getFloat("mgv6_freq_desert");
340         freq_beach  = settings->getFloat("mgv6_freq_beach");
341
342         np_terrain_base   = settings->getNoiseParams("mgv6_np_terrain_base");
343         np_terrain_higher = settings->getNoiseParams("mgv6_np_terrain_higher");
344         np_steepness      = settings->getNoiseParams("mgv6_np_steepness");
345         np_height_select  = settings->getNoiseParams("mgv6_np_height_select");
346         np_mud            = settings->getNoiseParams("mgv6_np_mud");
347         np_beach          = settings->getNoiseParams("mgv6_np_beach");
348         np_biome          = settings->getNoiseParams("mgv6_np_biome");
349         np_cave           = settings->getNoiseParams("mgv6_np_cave");
350         np_humidity       = settings->getNoiseParams("mgv6_np_humidity");
351         np_trees          = settings->getNoiseParams("mgv6_np_trees");
352         np_apple_trees    = settings->getNoiseParams("mgv6_np_apple_trees");
353
354         bool success =
355                 np_terrain_base  && np_terrain_higher && np_steepness &&
356                 np_height_select && np_trees          && np_mud       &&
357                 np_beach         && np_biome          && np_cave      &&
358                 np_humidity      && np_apple_trees;
359         return success;
360 }
361
362
363 void MapgenV6Params::writeParams(Settings *settings) {
364         settings->setFloat("mgv6_freq_desert", freq_desert);
365         settings->setFloat("mgv6_freq_beach",  freq_beach);
366         
367         settings->setNoiseParams("mgv6_np_terrain_base",   np_terrain_base);
368         settings->setNoiseParams("mgv6_np_terrain_higher", np_terrain_higher);
369         settings->setNoiseParams("mgv6_np_steepness",      np_steepness);
370         settings->setNoiseParams("mgv6_np_height_select",  np_height_select);
371         settings->setNoiseParams("mgv6_np_mud",            np_mud);
372         settings->setNoiseParams("mgv6_np_beach",          np_beach);
373         settings->setNoiseParams("mgv6_np_biome",          np_biome);
374         settings->setNoiseParams("mgv6_np_cave",           np_cave);
375         settings->setNoiseParams("mgv6_np_humidity",       np_humidity);
376         settings->setNoiseParams("mgv6_np_trees",          np_trees);
377         settings->setNoiseParams("mgv6_np_apple_trees",    np_apple_trees);
378 }
379
380
381 /////////////////////////////////// legacy static functions for farmesh
382
383
384 s16 Mapgen::find_ground_level_from_noise(u64 seed, v2s16 p2d, s16 precision) {
385         //just need to return something
386         s16 level = 5;
387         return level;
388 }
389
390
391 bool Mapgen::get_have_beach(u64 seed, v2s16 p2d) {
392         double sandnoise = noise2d_perlin(
393                         0.2+(float)p2d.X/250, 0.7+(float)p2d.Y/250,
394                         seed+59420, 3, 0.50);
395
396         return (sandnoise > 0.15);
397 }
398
399
400 double Mapgen::tree_amount_2d(u64 seed, v2s16 p) {
401         double noise = noise2d_perlin(
402                         0.5+(float)p.X/125, 0.5+(float)p.Y/125,
403                         seed+2, 4, 0.66);
404         double zeroval = -0.39;
405         if(noise < zeroval)
406                 return 0;
407         else
408                 return 0.04 * (noise-zeroval) / (1.0-zeroval);
409 }