]> git.lizzy.rs Git - minetest.git/blobdiff - src/mg_ore.cpp
client.cpp: modernize code
[minetest.git] / src / mg_ore.cpp
index f5d312ba273353474ea41c0acf754ccc3298e7ca..36100f762ac80183542bb0544d28205398566139 100644 (file)
@@ -1,6 +1,7 @@
 /*
 Minetest
-Copyright (C) 2010-2014 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
+Copyright (C) 2014-2016 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
+Copyright (C) 2015-2017 paramat
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as published by
@@ -20,13 +21,16 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "mg_ore.h"
 #include "mapgen.h"
 #include "noise.h"
-#include "util/numeric.h"
 #include "map.h"
 #include "log.h"
+#include <algorithm>
+
 
 FlagDesc flagdesc_ore[] = {
-       {"absheight", OREFLAG_ABSHEIGHT},
-       {NULL,        0}
+       {"absheight",                 OREFLAG_ABSHEIGHT}, // Non-functional
+       {"puff_cliffs",               OREFLAG_PUFF_CLIFFS},
+       {"puff_additive_composition", OREFLAG_PUFF_ADDITIVE},
+       {NULL,                        0}
 };
 
 
@@ -39,7 +43,8 @@ OreManager::OreManager(IGameDef *gamedef) :
 }
 
 
-size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
+size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed,
+       v3s16 nmin, v3s16 nmax, s16 ore_zero_level)
 {
        size_t nplaced = 0;
 
@@ -48,7 +53,7 @@ size_t OreManager::placeAllOres(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nma
                if (!ore)
                        continue;
 
-               nplaced += ore->placeOre(mg, blockseed, nmin, nmax);
+               nplaced += ore->placeOre(mg, blockseed, nmin, nmax, ore_zero_level);
                blockseed++;
        }
 
@@ -68,14 +73,6 @@ void OreManager::clear()
 
 ///////////////////////////////////////////////////////////////////////////////
 
-
-Ore::Ore()
-{
-       flags = 0;
-       noise = NULL;
-}
-
-
 Ore::~Ore()
 {
        delete noise;
@@ -89,24 +86,23 @@ void Ore::resolveNodeNames()
 }
 
 
-size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
+size_t Ore::placeOre(Mapgen *mg, u32 blockseed,
+       v3s16 nmin, v3s16 nmax, s16 ore_zero_level)
 {
-       int in_range = 0;
+       // Ore y_min / y_max is displaced by ore_zero_level or remains unchanged.
+       // Any ore with a limit at +-MAX_MAP_GENERATION_LIMIT is considered to have
+       // that limit at +-infinity, so we do not alter that limit.
+       s32 y_min_disp = (y_min <= -MAX_MAP_GENERATION_LIMIT) ?
+               -MAX_MAP_GENERATION_LIMIT : y_min + ore_zero_level;
+
+       s32 y_max_disp = (y_max >= MAX_MAP_GENERATION_LIMIT) ?
+               MAX_MAP_GENERATION_LIMIT : y_max + ore_zero_level;
 
-       in_range |= (nmin.Y <= y_max && nmax.Y >= y_min);
-       if (flags & OREFLAG_ABSHEIGHT)
-               in_range |= (nmin.Y >= -y_max && nmax.Y <= -y_min) << 1;
-       if (!in_range)
+       if (nmin.Y > y_max_disp || nmax.Y < y_min_disp)
                return 0;
 
-       int actual_ymin, actual_ymax;
-       if (in_range & ORE_RANGE_MIRROR) {
-               actual_ymin = MYMAX(nmin.Y, -y_max);
-               actual_ymax = MYMIN(nmax.Y, -y_min);
-       } else {
-               actual_ymin = MYMAX(nmin.Y, y_min);
-               actual_ymax = MYMIN(nmax.Y, y_max);
-       }
+       int actual_ymin = MYMAX(nmin.Y, y_min_disp);
+       int actual_ymax = MYMIN(nmax.Y, y_max_disp);
        if (clust_size >= actual_ymax - actual_ymin + 1)
                return 0;
 
@@ -124,7 +120,7 @@ size_t Ore::placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax)
 void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
        v3s16 nmin, v3s16 nmax, u8 *biomemap)
 {
-       PseudoRandom pr(blockseed);
+       PcgRandom pr(blockseed);
        MapNode n_ore(c_ore, 0, ore_param2);
 
        u32 sizex  = (nmax.X - nmin.X + 1);
@@ -146,7 +142,7 @@ void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
 
                if (biomemap && !biomes.empty()) {
                        u32 index = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
-                       std::set<u8>::iterator it = biomes.find(biomemap[index]);
+                       std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
                        if (it == biomes.end())
                                continue;
                }
@@ -173,11 +169,16 @@ void OreScatter::generate(MMVManip *vm, int mapseed, u32 blockseed,
 void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
        v3s16 nmin, v3s16 nmax, u8 *biomemap)
 {
-       PseudoRandom pr(blockseed + 4234);
+       PcgRandom pr(blockseed + 4234);
        MapNode n_ore(c_ore, 0, ore_param2);
 
        u16 max_height = column_height_max;
-       int y_start = pr.range(nmin.Y + max_height, nmax.Y - max_height);
+       int y_start_min = nmin.Y + max_height;
+       int y_start_max = nmax.Y - max_height;
+
+       int y_start = y_start_min < y_start_max ?
+               pr.range(y_start_min, y_start_max) :
+               (y_start_min + y_start_max) / 2;
 
        if (!noise) {
                int sx = nmax.X - nmin.X + 1;
@@ -195,17 +196,17 @@ void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
                        continue;
 
                if (biomemap && !biomes.empty()) {
-                       std::set<u8>::iterator it = biomes.find(biomemap[index]);
+                       std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[index]);
                        if (it == biomes.end())
                                continue;
                }
 
                u16 height = pr.range(column_height_min, column_height_max);
                int ymidpoint = y_start + noiseval;
-               int y0 = ymidpoint - height * (1 - column_midpoint_factor);
-               int y1 = y0 + height;
+               int y0 = MYMAX(nmin.Y, ymidpoint - height * (1 - column_midpoint_factor));
+               int y1 = MYMIN(nmax.Y, y0 + height - 1);
 
-               for (int y = y0; y < y1; y++) {
+               for (int y = y0; y <= y1; y++) {
                        u32 i = vm->m_area.index(x, y, z);
                        if (!vm->m_area.contains(i))
                                continue;
@@ -220,10 +221,96 @@ void OreSheet::generate(MMVManip *vm, int mapseed, u32 blockseed,
 
 ///////////////////////////////////////////////////////////////////////////////
 
+OrePuff::OrePuff() :
+       Ore()
+{
+}
+
+
+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()) {
+                       std::unordered_set<u8>::const_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)
 {
-       PseudoRandom pr(blockseed + 2404);
+       PcgRandom pr(blockseed + 2404);
        MapNode n_ore(c_ore, 0, ore_param2);
 
        u32 sizex  = (nmax.X - nmin.X + 1);
@@ -243,7 +330,7 @@ void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
 
                if (biomemap && !biomes.empty()) {
                        u32 bmapidx = sizex * (z0 - nmin.Z) + (x0 - nmin.X);
-                       std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
+                       std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
                        if (it == biomes.end())
                                continue;
                }
@@ -268,9 +355,9 @@ void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
 
                        float noiseval = noise->result[index];
 
-                       float xdist = x1 - csize / 2;
-                       float ydist = y1 - csize / 2;
-                       float zdist = z1 - csize / 2;
+                       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);
 
@@ -285,9 +372,9 @@ void OreBlob::generate(MMVManip *vm, int mapseed, u32 blockseed,
 
 ///////////////////////////////////////////////////////////////////////////////
 
-OreVein::OreVein()
+OreVein::OreVein() :
+       Ore()
 {
-       noise2 = NULL;
 }
 
 
@@ -300,7 +387,7 @@ OreVein::~OreVein()
 void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
        v3s16 nmin, v3s16 nmax, u8 *biomemap)
 {
-       PseudoRandom pr(blockseed + 520);
+       PcgRandom pr(blockseed + 520);
        MapNode n_ore(c_ore, 0, ore_param2);
 
        u32 sizex = (nmax.X - nmin.X + 1);
@@ -326,7 +413,7 @@ void OreVein::generate(MMVManip *vm, int mapseed, u32 blockseed,
 
                if (biomemap && !biomes.empty()) {
                        u32 bmapidx = sizex * (z - nmin.Z) + (x - nmin.X);
-                       std::set<u8>::iterator it = biomes.find(biomemap[bmapidx]);
+                       std::unordered_set<u8>::const_iterator it = biomes.find(biomemap[bmapidx]);
                        if (it == biomes.end())
                                continue;
                }