Ores: Make 'absheight' flag non-functional
[oweals/minetest.git] / src / mg_ore.cpp
1 /*
2 Minetest
3 Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
4 Copyright (C) 2015-2017 paramat
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "mg_ore.h"
22 #include "mapgen.h"
23 #include "noise.h"
24 #include "map.h"
25 #include "log.h"
26 #include <algorithm>
27
28
29 FlagDesc flagdesc_ore[] = {
30         {"absheight",                 OREFLAG_ABSHEIGHT}, // Non-functional
31         {"puff_cliffs",               OREFLAG_PUFF_CLIFFS},
32         {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
33         {NULL,                        0}
34 };
35
36
37 ///////////////////////////////////////////////////////////////////////////////
38
39
40 OreManager::OreManager(IGameDef *gamedef) :
41         ObjDefManager(gamedef, OBJDEF_ORE)
42 {
43 }
44
45
46 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
47 {
48         size_t nplaced = 0;
49
50         for (size_t i = 0; i != m_objects.size(); i++) {
51                 Ore *ore = (Ore *)m_objects[i];
52                 if (!ore)
53                         continue;
54
55                 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
56                 blockseed++;
57         }
58
59         return nplaced;
60 }
61
62
63 void OreManager::clear()
64 {
65         for (size_t i = 0; i < m_objects.size(); i++) {
66                 Ore *ore = (Ore *)m_objects[i];
67                 delete ore;
68         }
69         m_objects.clear();
70 }
71
72
73 ///////////////////////////////////////////////////////////////////////////////
74
75 Ore::~Ore()
76 {
77         delete noise;
78 }
79
80
81 void Ore::resolveNodeNames()
82 {
83         getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
84         getIdsFromNrBacklog(&c_wherein);
85 }
86
87
88 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
89 {
90         if (!(nmin.Y <= y_max && nmax.Y >= y_min))
91                 return 0;
92
93         int actual_ymin = MYMAX(nmin.Y, y_min);
94         int actual_ymax = MYMIN(nmax.Y, y_max);
95         if (clust_size >= actual_ymax - actual_ymin + 1)
96                 return 0;
97
98         nmin.Y = actual_ymin;
99         nmax.Y = actual_ymax;
100         generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
101
102         return 1;
103 }
104
105
106 ///////////////////////////////////////////////////////////////////////////////
107
108
109 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
110         v3s16 nmin, v3s16 nmax, u8 *biomemap)
111 {
112         PcgRandom pr(blockseed);
113         MapNode n_ore(c_ore, 0, ore_param2);
114
115         u32 sizex  = (nmax.X - nmin.X + 1);
116         u32 volume = (nmax.X - nmin.X + 1) *
117                                  (nmax.Y - nmin.Y + 1) *
118                                  (nmax.Z - nmin.Z + 1);
119         u32 csize     = clust_size;
120         u32 cvolume    = csize * csize * csize;
121         u32 nclusters = volume / clust_scarcity;
122
123         for (u32 i = 0; i != nclusters; i++) {
124                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
125                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
126                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
127
128                 if ((flags & OREFLAG_USE_NOISE) &&
129                         (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
130                         continue;
131
132                 if (biomemap && !biomes.empty()) {
133                         u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
134                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
135                         if (it == biomes.end())
136                                 continue;
137                 }
138
139                 for (u32 z1 = 0; z1 != csize; z1++)
140                 for (u32 y1 = 0; y1 != csize; y1++)
141                 for (u32 x1 = 0; x1 != csize; x1++) {
142                         if (pr.range(1, cvolume) > clust_num_ores)
143                                 continue;
144
145                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
146                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
147                                 continue;
148
149                         vm->m_data[i] = n_ore;
150                 }
151         }
152 }
153
154
155 ///////////////////////////////////////////////////////////////////////////////
156
157
158 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
159         v3s16 nmin, v3s16 nmax, u8 *biomemap)
160 {
161         PcgRandom pr(blockseed + 4234);
162         MapNode n_ore(c_ore, 0, ore_param2);
163
164         u16 max_height = column_height_max;
165         int y_start_min = nmin.Y + max_height;
166         int y_start_max = nmax.Y - max_height;
167
168         int y_start = y_start_min < y_start_max ?
169                 pr.range(y_start_min, y_start_max) :
170                 (y_start_min + y_start_max) / 2;
171
172         if (!noise) {
173                 int sx = nmax.X - nmin.X + 1;
174                 int sz = nmax.Z - nmin.Z + 1;
175                 noise = new Noise(&np, 0, sx, sz);
176         }
177         noise->seed = mapseed + y_start;
178         noise->perlinMap2D(nmin.X, nmin.Z);
179
180         size_t index = 0;
181         for (int z = nmin.Z; z <= nmax.Z; z++)
182         for (int x = nmin.X; x <= nmax.X; x++, index++) {
183                 float noiseval = noise->result[index];
184                 if (noiseval < nthresh)
185                         continue;
186
187                 if (biomemap && !biomes.empty()) {
188                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
189                         if (it == biomes.end())
190                                 continue;
191                 }
192
193                 u16 height = pr.range(column_height_min, column_height_max);
194                 int ymidpoint = y_start + noiseval;
195                 int y0 = MYMAX(nmin.Y, ymidpoint - height * (1 - column_midpoint_factor));
196                 int y1 = MYMIN(nmax.Y, y0 + height - 1);
197
198                 for (int y = y0; y <= y1; y++) {
199                         u32 i = vm->m_area.index(x, y, z);
200                         if (!vm->m_area.contains(i))
201                                 continue;
202                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
203                                 continue;
204
205                         vm->m_data[i] = n_ore;
206                 }
207         }
208 }
209
210
211 ///////////////////////////////////////////////////////////////////////////////
212
213 OrePuff::OrePuff() :
214         Ore()
215 {
216 }
217
218
219 OrePuff::~OrePuff()
220 {
221         delete noise_puff_top;
222         delete noise_puff_bottom;
223 }
224
225
226 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
227         v3s16 nmin, v3s16 nmax, u8 *biomemap)
228 {
229         PcgRandom pr(blockseed + 4234);
230         MapNode n_ore(c_ore, 0, ore_param2);
231
232         int y_start = pr.range(nmin.Y, nmax.Y);
233
234         if (!noise) {
235                 int sx = nmax.X - nmin.X + 1;
236                 int sz = nmax.Z - nmin.Z + 1;
237                 noise = new Noise(&np, 0, sx, sz);
238                 noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
239                 noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
240         }
241
242         noise->seed = mapseed + y_start;
243         noise->perlinMap2D(nmin.X, nmin.Z);
244         bool noise_generated = false;
245
246         size_t index = 0;
247         for (int z = nmin.Z; z <= nmax.Z; z++)
248         for (int x = nmin.X; x <= nmax.X; x++, index++) {
249                 float noiseval = noise->result[index];
250                 if (noiseval < nthresh)
251                         continue;
252
253                 if (biomemap && !biomes.empty()) {
254                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
255                         if (it == biomes.end())
256                                 continue;
257                 }
258
259                 if (!noise_generated) {
260                         noise_generated = true;
261                         noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
262                         noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
263                 }
264
265                 float ntop    = noise_puff_top->result[index];
266                 float nbottom = noise_puff_bottom->result[index];
267
268                 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
269                         float ndiff = noiseval - nthresh;
270                         if (ndiff < 1.0f) {
271                                 ntop *= ndiff;
272                                 nbottom *= ndiff;
273                         }
274                 }
275
276                 int ymid = y_start;
277                 int y0 = ymid - nbottom;
278                 int y1 = ymid + ntop;
279
280                 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
281                         SWAP(int, y0, y1);
282
283                 for (int y = y0; y <= y1; y++) {
284                         u32 i = vm->m_area.index(x, y, z);
285                         if (!vm->m_area.contains(i))
286                                 continue;
287                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
288                                 continue;
289
290                         vm->m_data[i] = n_ore;
291                 }
292         }
293 }
294
295
296 ///////////////////////////////////////////////////////////////////////////////
297
298
299 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
300         v3s16 nmin, v3s16 nmax, u8 *biomemap)
301 {
302         PcgRandom pr(blockseed + 2404);
303         MapNode n_ore(c_ore, 0, ore_param2);
304
305         u32 sizex  = (nmax.X - nmin.X + 1);
306         u32 volume = (nmax.X - nmin.X + 1) *
307                                  (nmax.Y - nmin.Y + 1) *
308                                  (nmax.Z - nmin.Z + 1);
309         u32 csize  = clust_size;
310         u32 nblobs = volume / clust_scarcity;
311
312         if (!noise)
313                 noise = new Noise(&np, mapseed, csize, csize, csize);
314
315         for (u32 i = 0; i != nblobs; i++) {
316                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
317                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
318                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
319
320                 if (biomemap && !biomes.empty()) {
321                         u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
322                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
323                         if (it == biomes.end())
324                                 continue;
325                 }
326
327                 bool noise_generated = false;
328                 noise->seed = blockseed + i;
329
330                 size_t index = 0;
331                 for (u32 z1 = 0; z1 != csize; z1++)
332                 for (u32 y1 = 0; y1 != csize; y1++)
333                 for (u32 x1 = 0; x1 != csize; x1++, index++) {
334                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
335                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
336                                 continue;
337
338                         // Lazily generate noise only if there's a chance of ore being placed
339                         // This simple optimization makes calls 6x faster on average
340                         if (!noise_generated) {
341                                 noise_generated = true;
342                                 noise->perlinMap3D(x0, y0, z0);
343                         }
344
345                         float noiseval = noise->result[index];
346
347                         float xdist = (s32)x1 - (s32)csize / 2;
348                         float ydist = (s32)y1 - (s32)csize / 2;
349                         float zdist = (s32)z1 - (s32)csize / 2;
350
351                         noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
352
353                         if (noiseval < nthresh)
354                                 continue;
355
356                         vm->m_data[i] = n_ore;
357                 }
358         }
359 }
360
361
362 ///////////////////////////////////////////////////////////////////////////////
363
364 OreVein::OreVein() :
365         Ore()
366 {
367 }
368
369
370 OreVein::~OreVein()
371 {
372         delete noise2;
373 }
374
375
376 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
377         v3s16 nmin, v3s16 nmax, u8 *biomemap)
378 {
379         PcgRandom pr(blockseed + 520);
380         MapNode n_ore(c_ore, 0, ore_param2);
381
382         u32 sizex = (nmax.X - nmin.X + 1);
383
384         if (!noise) {
385                 int sx = nmax.X - nmin.X + 1;
386                 int sy = nmax.Y - nmin.Y + 1;
387                 int sz = nmax.Z - nmin.Z + 1;
388                 noise  = new Noise(&np, mapseed, sx, sy, sz);
389                 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
390         }
391         bool noise_generated = false;
392
393         size_t index = 0;
394         for (int z = nmin.Z; z <= nmax.Z; z++)
395         for (int y = nmin.Y; y <= nmax.Y; y++)
396         for (int x = nmin.X; x <= nmax.X; x++, index++) {
397                 u32 i = vm->m_area.index(x, y, z);
398                 if (!vm->m_area.contains(i))
399                         continue;
400                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
401                         continue;
402
403                 if (biomemap && !biomes.empty()) {
404                         u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
405                         std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
406                         if (it == biomes.end())
407                                 continue;
408                 }
409
410                 // Same lazy generation optimization as in OreBlob
411                 if (!noise_generated) {
412                         noise_generated = true;
413                         noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
414                         noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
415                 }
416
417                 // randval ranges from -1..1
418                 float randval   = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
419                 float noiseval  = contour(noise->result[index]);
420                 float noiseval2 = contour(noise2->result[index]);
421                 if (noiseval * noiseval2 + randval * random_factor < nthresh)
422                         continue;
423
424                 vm->m_data[i] = n_ore;
425         }
426 }