summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/VoronoiMap.cpp95
-rw-r--r--src/VoronoiMap.h45
2 files changed, 140 insertions, 0 deletions
diff --git a/src/VoronoiMap.cpp b/src/VoronoiMap.cpp
new file mode 100644
index 000000000..e95cb0bfa
--- /dev/null
+++ b/src/VoronoiMap.cpp
@@ -0,0 +1,95 @@
+
+// VoronoiMap.cpp
+
+// Implements the cVoronoiMap class that implements a Voronoi algorithm over a noise to produce a map
+
+#include "Globals.h"
+#include "VoronoiMap.h"
+
+
+
+
+
+cVoronoiMap::cVoronoiMap(int a_Seed, int a_CellSize) :
+ m_Noise(a_Seed * 13 + 7),
+ m_CellSize(a_CellSize)
+{
+}
+
+
+
+
+
+void cVoronoiMap::SetCellSize(int a_CellSize)
+{
+ m_CellSize = a_CellSize;
+}
+
+
+
+
+
+int cVoronoiMap::GetValueAt(int a_X, int a_Y)
+{
+ int MinDist1, MinDist2;
+ return GetValueAt(a_X, a_Y, MinDist1, MinDist2);
+}
+
+
+
+
+
+int cVoronoiMap::GetValueAt(int a_X, int a_Y, int & a_MinDist)
+{
+ int MinDist2;
+ return GetValueAt(a_X, a_Y, a_MinDist, MinDist2);
+}
+
+
+
+
+
+int cVoronoiMap::GetValueAt(int a_X, int a_Y, int & a_MinDist1, int & a_MinDist2)
+{
+ // Note that due to historical reasons, the algorithm uses XZ coords, while the input uses XY coords.
+ // This is because the algorithm was first implemented directly in the biome generators which use MC coords.
+
+ int CellX = a_X / m_CellSize;
+ int CellZ = a_Y / m_CellSize;
+
+ // Get 5x5 neighboring cell seeds, compare distance to each. Return the value in the minumim-distance cell
+ int MinDist = m_CellSize * m_CellSize * 16; // There has to be a cell closer than this
+ int MinDist2 = MinDist;
+ int res = 0; // Will be overriden
+ for (int x = CellX - 2; x <= CellX + 2; x++)
+ {
+ int BaseX = x * m_CellSize;
+ for (int z = CellZ - 2; z < CellZ + 2; z++)
+ {
+ int OffsetX = (m_Noise.IntNoise3DInt(x, 16 * x + 32 * z, z) / 8) % m_CellSize;
+ int OffsetZ = (m_Noise.IntNoise3DInt(x, 32 * x - 16 * z, z) / 8) % m_CellSize;
+ int SeedX = BaseX + OffsetX;
+ int SeedZ = z * m_CellSize + OffsetZ;
+
+ int Dist = (SeedX - a_X) * (SeedX - a_X) + (SeedZ - a_Y) * (SeedZ - a_Y);
+ if (Dist < MinDist)
+ {
+ MinDist2 = MinDist;
+ MinDist = Dist;
+ res = m_Noise.IntNoise3DInt(x, x - z + 1000, z);
+ }
+ else if (Dist < MinDist2)
+ {
+ MinDist2 = Dist;
+ }
+ } // for z
+ } // for x
+
+ a_MinDist1 = MinDist;
+ a_MinDist2 = MinDist2;
+ return res;
+}
+
+
+
+
diff --git a/src/VoronoiMap.h b/src/VoronoiMap.h
new file mode 100644
index 000000000..bcd37f9cf
--- /dev/null
+++ b/src/VoronoiMap.h
@@ -0,0 +1,45 @@
+
+// VoronoiMap.h
+
+// Declares the cVoronoiMap class that implements a Voronoi algorithm over a noise to produce a map
+
+
+
+
+
+#pragma once
+
+#include "Noise.h"
+
+
+
+
+
+class cVoronoiMap
+{
+public:
+ cVoronoiMap(int a_Seed, int a_CellSize = 128);
+
+ /// Sets the cell size used for generating the Voronoi seeds
+ void SetCellSize(int a_CellSize);
+
+ /// Returns the value in the cell into which the specified point lies
+ int GetValueAt(int a_X, int a_Y);
+
+ /// Returns the value in the cell into which the specified point lies, and the distance to the nearest Voronoi seed
+ int GetValueAt(int a_X, int a_Y, int & a_MinDistance);
+
+ /// Returns the value in the cell into which the specified point lies, and the distances to the 2 nearest Voronoi seeds
+ int GetValueAt(int a_X, int a_Y, int & a_MinDistance1, int & a_MinDistance2);
+
+protected:
+ /// The noise used for generating Voronoi seeds
+ cNoise m_Noise;
+
+ /// Size of the Voronoi cells (avg X/Y distance between the seeds)
+ int m_CellSize;
+} ;
+
+
+
+