]> git.lizzy.rs Git - minetest.git/blob - src/mg_ore.cpp
Ore: Add ore sheet column height range selection
[minetest.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         {NULL,        0}
30 };
31
32
33 ///////////////////////////////////////////////////////////////////////////////
34
35
36 OreManager::OreManager(IGameDef *gamedef) :
37         ObjDefManager(gamedef, OBJDEF_ORE)
38 {
39 }
40
41
42 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
43 {
44         size_t nplaced = 0;
45
46         for (size_t i = 0; i != m_objects.size(); i++) {
47                 Ore *ore = (Ore *)m_objects[i];
48                 if (!ore)
49                         continue;
50
51                 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
52                 blockseed++;
53         }
54
55         return nplaced;
56 }
57
58
59 void OreManager::clear()
60 {
61         for (size_t i = 0; i < m_objects.size(); i++) {
62                 Ore *ore = (Ore *)m_objects[i];
63                 delete ore;
64         }
65         m_objects.clear();
66 }
67
68
69 ///////////////////////////////////////////////////////////////////////////////
70
71
72 Ore::Ore()
73 {
74         flags = 0;
75         noise = NULL;
76 }
77
78
79 Ore::~Ore()
80 {
81         delete noise;
82 }
83
84
85 void Ore::resolveNodeNames()
86 {
87         getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
88         getIdsFromNrBacklog(&c_wherein);
89 }
90
91
92 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
93 {
94         int in_range = 0;
95
96         in_range |= (nmin.Y <= y_max && nmax.Y >= y_min);
97         if (flags & OREFLAG_ABSHEIGHT)
98                 in_range |= (nmin.Y >= -y_max && nmax.Y <= -y_min) << 1;
99         if (!in_range)
100                 return 0;
101
102         int actual_ymin, actual_ymax;
103         if (in_range & ORE_RANGE_MIRROR) {
104                 actual_ymin = MYMAX(nmin.Y, -y_max);
105                 actual_ymax = MYMIN(nmax.Y, -y_min);
106         } else {
107                 actual_ymin = MYMAX(nmin.Y, y_min);
108                 actual_ymax = MYMIN(nmax.Y, y_max);
109         }
110         if (clust_size >= actual_ymax - actual_ymin + 1)
111                 return 0;
112
113         nmin.Y = actual_ymin;
114         nmax.Y = actual_ymax;
115         generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
116
117         return 1;
118 }
119
120
121 ///////////////////////////////////////////////////////////////////////////////
122
123
124 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
125         v3s16 nmin, v3s16 nmax, u8 *biomemap)
126 {
127         PseudoRandom pr(blockseed);
128         MapNode n_ore(c_ore, 0, ore_param2);
129
130         u32 sizex  = (nmax.X - nmin.X + 1);
131         u32 volume = (nmax.X - nmin.X + 1) *
132                                  (nmax.Y - nmin.Y + 1) *
133                                  (nmax.Z - nmin.Z + 1);
134         u32 csize     = clust_size;
135         u32 cvolume    = csize * csize * csize;
136         u32 nclusters = volume / clust_scarcity;
137
138         for (u32 i = 0; i != nclusters; i++) {
139                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
140                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
141                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
142
143                 if ((flags & OREFLAG_USE_NOISE) &&
144                         (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
145                         continue;
146
147                 if (biomemap && !biomes.empty()) {
148                         u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
149                         std::set<u8>::iterator it = biomes.find(biomemap[index]);
150                         if (it == biomes.end())
151                                 continue;
152                 }
153
154                 for (u32 z1 = 0; z1 != csize; z1++)
155                 for (u32 y1 = 0; y1 != csize; y1++)
156                 for (u32 x1 = 0; x1 != csize; x1++) {
157                         if (pr.range(1, cvolume) > clust_num_ores)
158                                 continue;
159
160                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
161                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
162                                 continue;
163
164                         vm->m_data[i] = n_ore;
165                 }
166         }
167 }
168
169
170 ///////////////////////////////////////////////////////////////////////////////
171
172
173 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
174         v3s16 nmin, v3s16 nmax, u8 *biomemap)
175 {
176         PseudoRandom pr(blockseed + 4234);
177         MapNode n_ore(c_ore, 0, ore_param2);
178
179         u16 max_height = column_height_max;
180         int y_start = pr.range(nmin.Y + max_height, nmax.Y - max_height);
181
182         if (!noise) {
183                 int sx = nmax.X - nmin.X + 1;
184                 int sz = nmax.Z - nmin.Z + 1;
185                 noise = new Noise(&np, 0, sx, sz);
186         }
187         noise->seed = mapseed + y_start;
188         noise->perlinMap2D(nmin.X, nmin.Z);
189
190         size_t index = 0;
191         for (int z = nmin.Z; z <= nmax.Z; z++)
192         for (int x = nmin.X; x <= nmax.X; x++, index++) {
193                 float noiseval = noise->result[index];
194                 if (noiseval < nthresh)
195                         continue;
196
197                 if (biomemap && !biomes.empty()) {
198                         std::set<u8>::iterator it = biomes.find(biomemap[index]);
199                         if (it == biomes.end())
200                                 continue;
201                 }
202
203                 u16 height = pr.range(column_height_min, column_height_max);
204                 int ymidpoint = y_start + noiseval;
205                 int y0 = ymidpoint - height * (1 - column_midpoint_factor);
206                 int y1 = y0 + height;
207
208                 for (int y = y0; y < y1; y++) {
209                         u32 i = vm->m_area.index(x, y, z);
210                         if (!vm->m_area.contains(i))
211                                 continue;
212                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
213                                 continue;
214
215                         vm->m_data[i] = n_ore;
216                 }
217         }
218 }
219
220
221 ///////////////////////////////////////////////////////////////////////////////
222
223 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
224         v3s16 nmin, v3s16 nmax, u8 *biomemap)
225 {
226         PseudoRandom pr(blockseed + 2404);
227         MapNode n_ore(c_ore, 0, ore_param2);
228
229         u32 sizex  = (nmax.X - nmin.X + 1);
230         u32 volume = (nmax.X - nmin.X + 1) *
231                                  (nmax.Y - nmin.Y + 1) *
232                                  (nmax.Z - nmin.Z + 1);
233         u32 csize  = clust_size;
234         u32 nblobs = volume / clust_scarcity;
235
236         if (!noise)
237                 noise = new Noise(&np, mapseed, csize, csize, csize);
238
239         for (u32 i = 0; i != nblobs; i++) {
240                 int x0 = pr.range(nmin.X, nmax.X - csize + 1);
241                 int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
242                 int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
243
244                 if (biomemap && !biomes.empty()) {
245                         u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
246                         std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
247                         if (it == biomes.end())
248                                 continue;
249                 }
250
251                 bool noise_generated = false;
252                 noise->seed = blockseed + i;
253
254                 size_t index = 0;
255                 for (u32 z1 = 0; z1 != csize; z1++)
256                 for (u32 y1 = 0; y1 != csize; y1++)
257                 for (u32 x1 = 0; x1 != csize; x1++, index++) {
258                         u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
259                         if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
260                                 continue;
261
262                         // Lazily generate noise only if there's a chance of ore being placed
263                         // This simple optimization makes calls 6x faster on average
264                         if (!noise_generated) {
265                                 noise_generated = true;
266                                 noise->perlinMap3D(x0, y0, z0);
267                         }
268
269                         float noiseval = noise->result[index];
270
271                         float xdist = x1 - csize / 2;
272                         float ydist = y1 - csize / 2;
273                         float zdist = z1 - csize / 2;
274
275                         noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
276
277                         if (noiseval < nthresh)
278                                 continue;
279
280                         vm->m_data[i] = n_ore;
281                 }
282         }
283 }
284
285
286 ///////////////////////////////////////////////////////////////////////////////
287
288 OreVein::OreVein()
289 {
290         noise2 = NULL;
291 }
292
293
294 OreVein::~OreVein()
295 {
296         delete noise2;
297 }
298
299
300 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
301         v3s16 nmin, v3s16 nmax, u8 *biomemap)
302 {
303         PseudoRandom pr(blockseed + 520);
304         MapNode n_ore(c_ore, 0, ore_param2);
305
306         u32 sizex = (nmax.X - nmin.X + 1);
307
308         if (!noise) {
309                 int sx = nmax.X - nmin.X + 1;
310                 int sy = nmax.Y - nmin.Y + 1;
311                 int sz = nmax.Z - nmin.Z + 1;
312                 noise  = new Noise(&np, mapseed, sx, sy, sz);
313                 noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
314         }
315         bool noise_generated = false;
316
317         size_t index = 0;
318         for (int z = nmin.Z; z <= nmax.Z; z++)
319         for (int y = nmin.Y; y <= nmax.Y; y++)
320         for (int x = nmin.X; x <= nmax.X; x++, index++) {
321                 u32 i = vm->m_area.index(x, y, z);
322                 if (!vm->m_area.contains(i))
323                         continue;
324                 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
325                         continue;
326
327                 if (biomemap && !biomes.empty()) {
328                         u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
329                         std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
330                         if (it == biomes.end())
331                                 continue;
332                 }
333
334                 // Same lazy generation optimization as in OreBlob
335                 if (!noise_generated) {
336                         noise_generated = true;
337                         noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
338                         noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
339                 }
340
341                 // randval ranges from -1..1
342                 float randval   = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
343                 float noiseval  = contour(noise->result[index]);
344                 float noiseval2 = contour(noise2->result[index]);
345                 if (noiseval * noiseval2 + randval * random_factor < nthresh)
346                         continue;
347
348                 vm->m_data[i] = n_ore;
349         }
350 }