+
+///////////////////////////////////////////////////////////////////////////////
+
+OrePuff::OrePuff() :
+ Ore()
+{
+ noise_puff_top = NULL;
+ noise_puff_bottom = NULL;
+}
+
+
+OrePuff::~OrePuff()
+{
+ delete noise_puff_top;
+ delete noise_puff_bottom;
+}
+
+
+void OrePuff::generate(MMVManip *vm, int mapseed, u32 blockseed,
+ v3s16 nmin, v3s16 nmax, u8 *biomemap)
+{
+ PcgRandom pr(blockseed + 4234);
+ MapNode n_ore(c_ore, 0, ore_param2);
+
+ int y_start = pr.range(nmin.Y, nmax.Y);
+
+ if (!noise) {
+ int sx = nmax.X - nmin.X + 1;
+ int sz = nmax.Z - nmin.Z + 1;
+ noise = new Noise(&np, 0, sx, sz);
+ noise_puff_top = new Noise(&np_puff_top, 0, sx, sz);
+ noise_puff_bottom = new Noise(&np_puff_bottom, 0, sx, sz);
+ }
+
+ noise->seed = mapseed + y_start;
+ noise->perlinMap2D(nmin.X, nmin.Z);
+ bool noise_generated = false;
+
+ size_t index = 0;
+ for (int z = nmin.Z; z <= nmax.Z; z++)
+ for (int x = nmin.X; x <= nmax.X; x++, index++) {
+ float noiseval = noise->result[index];
+ if (noiseval < nthresh)
+ continue;
+
+ if (biomemap && !biomes.empty()) {
+ UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[index]);
+ if (it == biomes.end())
+ continue;
+ }
+
+ if (!noise_generated) {
+ noise_generated = true;
+ noise_puff_top->perlinMap2D(nmin.X, nmin.Z);
+ noise_puff_bottom->perlinMap2D(nmin.X, nmin.Z);
+ }
+
+ float ntop = noise_puff_top->result[index];
+ float nbottom = noise_puff_bottom->result[index];
+
+ if (!(flags & OREFLAG_PUFF_CLIFFS)) {
+ float ndiff = noiseval - nthresh;
+ if (ndiff < 1.0f) {
+ ntop *= ndiff;
+ nbottom *= ndiff;
+ }
+ }
+
+ int ymid = y_start;
+ int y0 = ymid - nbottom;
+ int y1 = ymid + ntop;
+
+ if ((flags & OREFLAG_PUFF_ADDITIVE) && (y0 > y1))
+ SWAP(int, y0, y1);
+
+ for (int y = y0; y <= y1; y++) {
+ u32 i = vm->m_area.index(x, y, z);
+ if (!vm->m_area.contains(i))
+ continue;
+ if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
+ continue;
+
+ vm->m_data[i] = n_ore;
+ }
+ }
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+
+void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
+ v3s16 nmin, v3s16 nmax, u8 *biomemap)
+{
+ PcgRandom pr(blockseed + 2404);
+ MapNode n_ore(c_ore, 0, ore_param2);
+
+ u32 sizex = (nmax.X - nmin.X + 1);
+ u32 volume = (nmax.X - nmin.X + 1) *
+ (nmax.Y - nmin.Y + 1) *
+ (nmax.Z - nmin.Z + 1);
+ u32 csize = clust_size;
+ u32 nblobs = volume / clust_scarcity;
+
+ if (!noise)
+ noise = new Noise(&np, mapseed, csize, csize, csize);
+
+ for (u32 i = 0; i != nblobs; i++) {
+ int x0 = pr.range(nmin.X, nmax.X - csize + 1);
+ int y0 = pr.range(nmin.Y, nmax.Y - csize + 1);
+ int z0 = pr.range(nmin.Z, nmax.Z - csize + 1);
+
+ if (biomemap && !biomes.empty()) {
+ u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
+ UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[bmapidx]);
+ if (it == biomes.end())
+ continue;
+ }
+
+ bool noise_generated = false;
+ noise->seed = blockseed + i;
+
+ size_t index = 0;
+ for (u32 z1 = 0; z1 != csize; z1++)
+ for (u32 y1 = 0; y1 != csize; y1++)
+ for (u32 x1 = 0; x1 != csize; x1++, index++) {
+ u32 i = vm->m_area.index(x0 + x1, y0 + y1, z0 + z1);
+ if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
+ continue;
+
+ // Lazily generate noise only if there's a chance of ore being placed
+ // This simple optimization makes calls 6x faster on average
+ if (!noise_generated) {
+ noise_generated = true;
+ noise->perlinMap3D(x0, y0, z0);
+ }
+
+ float noiseval = noise->result[index];
+
+ float xdist = (s32)x1 - (s32)csize / 2;
+ float ydist = (s32)y1 - (s32)csize / 2;
+ float zdist = (s32)z1 - (s32)csize / 2;
+
+ noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
+
+ if (noiseval < nthresh)
+ continue;
+
+ vm->m_data[i] = n_ore;
+ }
+ }
+}
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+OreVein::OreVein() :
+ Ore()
+{
+ noise2 = NULL;
+}
+
+
+OreVein::~OreVein()
+{
+ delete noise2;
+}
+
+
+void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
+ v3s16 nmin, v3s16 nmax, u8 *biomemap)
+{
+ PcgRandom pr(blockseed + 520);
+ MapNode n_ore(c_ore, 0, ore_param2);
+
+ u32 sizex = (nmax.X - nmin.X + 1);
+
+ if (!noise) {
+ int sx = nmax.X - nmin.X + 1;
+ int sy = nmax.Y - nmin.Y + 1;
+ int sz = nmax.Z - nmin.Z + 1;
+ noise = new Noise(&np, mapseed, sx, sy, sz);
+ noise2 = new Noise(&np, mapseed + 436, sx, sy, sz);
+ }
+ bool noise_generated = false;
+
+ size_t index = 0;
+ for (int z = nmin.Z; z <= nmax.Z; z++)
+ for (int y = nmin.Y; y <= nmax.Y; y++)
+ for (int x = nmin.X; x <= nmax.X; x++, index++) {
+ u32 i = vm->m_area.index(x, y, z);
+ if (!vm->m_area.contains(i))
+ continue;
+ if (!CONTAINS(c_wherein, vm->m_data[i].getContent()))
+ continue;
+
+ if (biomemap && !biomes.empty()) {
+ u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
+ UNORDERED_SET<u8>::iterator it = biomes.find(biomemap[bmapidx]);
+ if (it == biomes.end())
+ continue;
+ }
+
+ // Same lazy generation optimization as in OreBlob
+ if (!noise_generated) {
+ noise_generated = true;
+ noise->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
+ noise2->perlinMap3D(nmin.X, nmin.Y, nmin.Z);
+ }
+
+ // randval ranges from -1..1
+ float randval = (float)pr.next() / (pr.RANDOM_RANGE / 2) - 1.f;
+ float noiseval = contour(noise->result[index]);
+ float noiseval2 = contour(noise2->result[index]);
+ if (noiseval * noiseval2 + randval * random_factor < nthresh)
+ continue;
+
+ vm->m_data[i] = n_ore;
+ }
+}