Ore: Add Blob ore type
authorkwolekr <kwolekr@minetest.net>
Sun, 28 Dec 2014 08:11:00 +0000 (03:11 -0500)
committerkwolekr <kwolekr@minetest.net>
Sun, 28 Dec 2014 08:11:00 +0000 (03:11 -0500)
doc/lua_api.txt
src/mg_ore.cpp
src/mg_ore.h
src/script/lua_api/l_mapgen.cpp

index 0ceac1fca352451fea89984f49fc5618c5dd5b00..8f491f5a89292b839b59f3729221ccf8eab0843e 100644 (file)
@@ -562,9 +562,9 @@ All default ores are of the uniformly-distributed scatter type.
     The height of the blob is randomly scattered, with a maximum height of clust_size.
     clust_scarcity and clust_num_ores are ignored.
     This is essentially an improved version of the so-called "stratus" ore seen in some unofficial mods.
- - claylike - NOT YET IMPLEMENTED
-    Places ore if there are no more than clust_scarcity number of specified nodes within a Von Neumann
-    neighborhood of clust_size radius.
+ - blob
+    Creates a roundish blob of ore according to 3d perlin noise described by noise_params. The maximum
+    size of the blob is clust_size, and clust_scarcity has the same meaning as with scatter type.
 
 
 Ore attributes
index 8681b5782cca4359cc05a5d93661aa1608604cb6..2c5712da7f2c1c578484fd748845293c002bd195 100644 (file)
@@ -178,12 +178,12 @@ void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
        if (!noise) {
                int sx = nmax.X - nmin.X + 1;
                int sz = nmax.Z - nmin.Z + 1;
-               noise = new Noise(&np, seed, sx, sz);
+               noise = new Noise(&np, 0, sx, sz);
        }
        noise->seed = seed + y_start;
        noise->perlinMap2D(nmin.X, nmin.Z);
 
-       int index = 0;
+       size_t index = 0;
        for (int z = nmin.Z; z <= nmax.Z; z++)
        for (int x = nmin.X; x <= nmax.X; x++) {
                float noiseval = noise->result[index++];
@@ -204,3 +204,60 @@ void OreSheet::generate(ManualMapVoxelManipulator *vm, int seed,
                }
        }
 }
+
+
+///////////////////////////////////////////////////////////////////////////////
+
+void OreBlob::generate(ManualMapVoxelManipulator *vm, int seed, u32 blockseed,
+       v3s16 nmin, v3s16 nmax)
+{
+       PseudoRandom pr(blockseed + 2404);
+       MapNode n_ore(c_ore, 0, ore_param2);
+
+       int volume = (nmax.X - nmin.X + 1) *
+                                (nmax.Y - nmin.Y + 1) *
+                                (nmax.Z - nmin.Z + 1);
+       int csize     = clust_size;
+       int nblobs = volume / clust_scarcity;
+
+       if (!noise)
+               noise = new Noise(&np, seed, csize, csize, csize);
+
+       for (int 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);
+
+               bool noise_generated = false;
+               noise->seed = blockseed + i;
+
+               size_t index = 0;
+               for (int z1 = 0; z1 != csize; z1++)
+               for (int y1 = 0; y1 != csize; y1++)
+               for (int 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 = x1 - csize / 2;
+                       float ydist = y1 - csize / 2;
+                       float zdist = z1 - csize / 2;
+
+                       noiseval -= (sqrt(xdist * xdist + ydist * ydist + zdist * zdist) / csize);
+
+                       if (noiseval < nthresh)
+                               continue;
+
+                       vm->m_data[i] = n_ore;
+               }
+       }
+}
index 3b413674e926495d4e46fa96ed4e29df2a5797bf..a20aeefe0fe3a2f7e4f7f20e3f98e628079c512c 100644 (file)
@@ -47,9 +47,9 @@ class ManualMapVoxelManipulator;
 
 
 enum OreType {
-       ORE_SCATTER,
-       ORE_SHEET,
-       ORE_CLAYLIKE
+       ORE_TYPE_SCATTER,
+       ORE_TYPE_SHEET,
+       ORE_TYPE_BLOB,
 };
 
 extern FlagDesc flagdesc_ore[];
@@ -78,7 +78,7 @@ public:
 
        size_t placeOre(Mapgen *mg, u32 blockseed, v3s16 nmin, v3s16 nmax);
        virtual void generate(ManualMapVoxelManipulator *vm, int seed,
-                                               u32 blockseed, v3s16 nmin, v3s16 nmax) = 0;
+               u32 blockseed, v3s16 nmin, v3s16 nmax) = 0;
 };
 
 class OreScatter : public Ore {
@@ -86,7 +86,7 @@ public:
        static const bool NEEDS_NOISE = false;
 
        virtual void generate(ManualMapVoxelManipulator *vm, int seed,
-                                               u32 blockseed, v3s16 nmin, v3s16 nmax);
+               u32 blockseed, v3s16 nmin, v3s16 nmax);
 };
 
 class OreSheet : public Ore {
@@ -94,7 +94,15 @@ public:
        static const bool NEEDS_NOISE = true;
 
        virtual void generate(ManualMapVoxelManipulator *vm, int seed,
-                                               u32 blockseed, v3s16 nmin, v3s16 nmax);
+               u32 blockseed, v3s16 nmin, v3s16 nmax);
+};
+
+class OreBlob : public Ore {
+public:
+       static const bool NEEDS_NOISE = true;
+
+       virtual void generate(ManualMapVoxelManipulator *vm, int seed,
+               u32 blockseed, v3s16 nmin, v3s16 nmax);
 };
 
 class OreManager : public GenElementManager {
@@ -108,12 +116,12 @@ public:
        Ore *create(int type)
        {
                switch (type) {
-               case ORE_SCATTER:
+               case ORE_TYPE_SCATTER:
                        return new OreScatter;
-               case ORE_SHEET:
+               case ORE_TYPE_SHEET:
                        return new OreSheet;
-               //case ORE_CLAYLIKE: //TODO: implement this!
-               //      return new OreClaylike;
+               case ORE_TYPE_BLOB:
+                       return new OreBlob;
                default:
                        return NULL;
                }
index 383fbeaef92210e11944ab5246f5e0eb39ba9b31..8c81412b179582be8fadf478d0994904148cfac4 100644 (file)
@@ -68,9 +68,9 @@ struct EnumString ModApiMapgen::es_MapgenObject[] =
 
 struct EnumString ModApiMapgen::es_OreType[] =
 {
-       {ORE_SCATTER,  "scatter"},
-       {ORE_SHEET,    "sheet"},
-       {ORE_CLAYLIKE, "claylike"},
+       {ORE_TYPE_SCATTER,  "scatter"},
+       {ORE_TYPE_SHEET,    "sheet"},
+       {ORE_TYPE_BLOB,     "blob"},
        {0, NULL},
 };
 
@@ -642,7 +642,7 @@ int ModApiMapgen::l_register_ore(lua_State *L)
        OreManager *oremgr    = getServer(L)->getEmergeManager()->oremgr;
 
        enum OreType oretype = (OreType)getenumfield(L, index,
-                               "ore_type", es_OreType, ORE_SCATTER);
+                               "ore_type", es_OreType, ORE_TYPE_SCATTER);
        Ore *ore = oremgr->create(oretype);
        if (!ore) {
                errorstream << "register_ore: ore_type " << oretype << " not implemented";