3 Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
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.
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.
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.
23 #include "util/numeric.h"
27 FlagDesc flagdesc_ore[] = {
28 {"absheight", OREFLAG_ABSHEIGHT},
29 {"puff_cliffs", OREFLAG_PUFF_CLIFFS},
30 {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
35 ///////////////////////////////////////////////////////////////////////////////
38 OreManager::OreManager(IGameDef *gamedef) :
39 ObjDefManager(gamedef, OBJDEF_ORE)
44 size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
48 for (size_t i = 0; i != m_objects.size(); i++) {
49 Ore *ore = (Ore *)m_objects[i];
53 nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
61 void OreManager::clear()
63 for (size_t i = 0; i < m_objects.size(); i++) {
64 Ore *ore = (Ore *)m_objects[i];
71 ///////////////////////////////////////////////////////////////////////////////
87 void Ore::resolveNodeNames()
89 getIdFromNrBacklog(&c_ore, "", CONTENT_AIR);
90 getIdsFromNrBacklog(&c_wherein);
94 size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
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;
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);
109 actual_ymin = MYMAX(nmin.Y, y_min);
110 actual_ymax = MYMIN(nmax.Y, y_max);
112 if (clust_size >= actual_ymax - actual_ymin + 1)
115 nmin.Y = actual_ymin;
116 nmax.Y = actual_ymax;
117 generate(mg->vm, mg->seed, blockseed, nmin, nmax, mg->biomemap);
123 ///////////////////////////////////////////////////////////////////////////////
126 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
127 v3s16 nmin, v3s16 nmax, u8 *biomemap)
129 PseudoRandom pr(blockseed);
130 MapNode n_ore(c_ore, 0, ore_param2);
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;
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);
145 if ((flags & OREFLAG_USE_NOISE) &&
146 (NoisePerlin3D(&np, x0, y0, z0, mapseed) < nthresh))
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())
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)
162 u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
163 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
166 vm->m_data[i] = n_ore;
172 ///////////////////////////////////////////////////////////////////////////////
175 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
176 v3s16 nmin, v3s16 nmax, u8 *biomemap)
178 PseudoRandom pr(blockseed + 4234);
179 MapNode n_ore(c_ore, 0, ore_param2);
181 u16 max_height = column_height_max;
182 int y_start = pr.range(nmin.Y + max_height, nmax.Y - max_height);
185 int sx = nmax.X - nmin.X + 1;
186 int sz = nmax.Z - nmin.Z + 1;
187 noise = new Noise(&np, 0, sx, sz);
189 noise->seed = mapseed + y_start;
190 noise->perlinMap2D(nmin.X, nmin.Z);
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)
199 if (biomemap && !biomes.empty()) {
200 std::set<u8>::iterator it = biomes.find(biomemap[index]);
201 if (it == biomes.end())
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;
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))
214 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
217 vm->m_data[i] = n_ore;
223 ///////////////////////////////////////////////////////////////////////////////
228 noise_puff_top = NULL;
229 noise_puff_bottom = NULL;
235 delete noise_puff_top;
236 delete noise_puff_bottom;
240 void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
241 v3s16 nmin, v3s16 nmax, u8 *biomemap)
243 PseudoRandom pr(blockseed + 4234);
244 MapNode n_ore(c_ore, 0, ore_param2);
246 int y_start = pr.range(nmin.Y, nmax.Y);
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);
256 noise->seed = mapseed + y_start;
257 noise->perlinMap2D(nmin.X, nmin.Z);
258 bool noise_generated = false;
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)
267 if (biomemap && !biomes.empty()) {
268 std::set<u8>::iterator it = biomes.find(biomemap[index]);
269 if (it == biomes.end())
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);
279 float ntop = noise_puff_top->result[index];
280 float nbottom = noise_puff_bottom->result[index];
282 if (!(flags & OREFLAG_PUFF_CLIFFS)) {
283 float ndiff = noiseval - nthresh;
291 int y0 = ymid - nbottom;
292 int y1 = ymid + ntop;
294 if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
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))
301 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
304 vm->m_data[i] = n_ore;
310 ///////////////////////////////////////////////////////////////////////////////
313 void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
314 v3s16 nmin, v3s16 nmax, u8 *biomemap)
316 PseudoRandom pr(blockseed + 2404);
317 MapNode n_ore(c_ore, 0, ore_param2);
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;
327 noise = new Noise(&np, mapseed, csize, csize, csize);
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);
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())
341 bool noise_generated = false;
342 noise->seed = blockseed + i;
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()))
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);
359 float noiseval = noise->result[index];
361 float xdist = (s32)x1 - (s32)csize / 2;
362 float ydist = (s32)y1 - (s32)csize / 2;
363 float zdist = (s32)z1 - (s32)csize / 2;
365 noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
367 if (noiseval < nthresh)
370 vm->m_data[i] = n_ore;
376 ///////////////////////////////////////////////////////////////////////////////
391 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
392 v3s16 nmin, v3s16 nmax, u8 *biomemap)
394 PseudoRandom pr(blockseed + 520);
395 MapNode n_ore(c_ore, 0, ore_param2);
397 u32 sizex = (nmax.X - nmin.X + 1);
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);
406 bool noise_generated = false;
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))
415 if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
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())
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);
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)
439 vm->m_data[i] = n_ore;