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