From 900fa26965a2fbdf2b9de74e2d680c083de91482 Mon Sep 17 00:00:00 2001
From: kwolekr <kwolekr@minetest.net>
Date: Sun, 28 Dec 2014 03:11:00 -0500
Subject: [PATCH] Ore: Add Blob ore type

---
 doc/lua_api.txt                 |  6 ++--
 src/mg_ore.cpp                  | 61 +++++++++++++++++++++++++++++++--
 src/mg_ore.h                    | 28 +++++++++------
 src/script/lua_api/l_mapgen.cpp |  8 ++---
 4 files changed, 84 insertions(+), 19 deletions(-)

diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 0ceac1fca..8f491f5a8 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -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
diff --git a/src/mg_ore.cpp b/src/mg_ore.cpp
index 8681b5782..2c5712da7 100644
--- a/src/mg_ore.cpp
+++ b/src/mg_ore.cpp
@@ -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;
+		}
+	}
+}
diff --git a/src/mg_ore.h b/src/mg_ore.h
index 3b413674e..a20aeefe0 100644
--- a/src/mg_ore.h
+++ b/src/mg_ore.h
@@ -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 @@ class Ore : public GenElement, public NodeResolver {
 
 	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 @@ class OreScatter : public Ore {
 	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 @@ class OreSheet : public Ore {
 	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 @@ class OreManager : public GenElementManager {
 	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;
 		}
diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp
index 383fbeaef..8c81412b1 100644
--- a/src/script/lua_api/l_mapgen.cpp
+++ b/src/script/lua_api/l_mapgen.cpp
@@ -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";
-- 
GitLab