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