summaryrefslogtreecommitdiffstats
path: root/src/VoronoiMap.cpp
diff options
context:
space:
mode:
authorTiger Wang <ziwei.tiger@hotmail.co.uk>2014-06-21 23:14:23 +0200
committerTiger Wang <ziwei.tiger@hotmail.co.uk>2014-06-21 23:14:23 +0200
commit7615ed90c020b88db52b0b094f00cd028e326b5a (patch)
treeca7f647e77195a6346a2333f3d9fe5292cd2ed40 /src/VoronoiMap.cpp
parentFixed invalid iterators (diff)
parentMerge branch 'master' of https://github.com/mc-server/MCServer (diff)
downloadcuberite-7615ed90c020b88db52b0b094f00cd028e326b5a.tar
cuberite-7615ed90c020b88db52b0b094f00cd028e326b5a.tar.gz
cuberite-7615ed90c020b88db52b0b094f00cd028e326b5a.tar.bz2
cuberite-7615ed90c020b88db52b0b094f00cd028e326b5a.tar.lz
cuberite-7615ed90c020b88db52b0b094f00cd028e326b5a.tar.xz
cuberite-7615ed90c020b88db52b0b094f00cd028e326b5a.tar.zst
cuberite-7615ed90c020b88db52b0b094f00cd028e326b5a.zip
Diffstat (limited to 'src/VoronoiMap.cpp')
-rw-r--r--src/VoronoiMap.cpp49
1 files changed, 40 insertions, 9 deletions
diff --git a/src/VoronoiMap.cpp b/src/VoronoiMap.cpp
index 7a36edebc..9c3ca7e65 100644
--- a/src/VoronoiMap.cpp
+++ b/src/VoronoiMap.cpp
@@ -11,7 +11,9 @@
cVoronoiMap::cVoronoiMap(int a_Seed, int a_CellSize) :
- m_Noise(a_Seed),
+ m_Noise1(a_Seed + 1),
+ m_Noise2(a_Seed + 2),
+ m_Noise3(a_Seed + 3),
m_CellSize(a_CellSize)
{
}
@@ -57,26 +59,25 @@ int cVoronoiMap::GetValueAt(int a_X, int a_Y, int & a_MinDist1, int & a_MinDist2
int CellX = a_X / m_CellSize;
int CellZ = a_Y / m_CellSize;
+ UpdateCell(CellX, CellZ);
+
// 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++)
+ for (int x = 0; x < 5; x++)
{
- int BaseX = x * m_CellSize;
- for (int z = CellZ - 2; z < CellZ + 2; z++)
+ for (int z = 0; z < 5; 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 SeedX = m_SeedX[x][z];
+ int SeedZ = m_SeedZ[x][z];
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);
+ res = m_Noise3.IntNoise2DInt(x + CellX - 2, z + CellZ - 2);
}
else if (Dist < MinDist2)
{
@@ -93,3 +94,33 @@ int cVoronoiMap::GetValueAt(int a_X, int a_Y, int & a_MinDist1, int & a_MinDist2
+
+void cVoronoiMap::UpdateCell(int a_CellX, int a_CellZ)
+{
+ // If the specified cell is currently cached, bail out:
+ if ((a_CellX == m_CurrentCellX) && (a_CellZ == m_CurrentCellZ))
+ {
+ return;
+ }
+
+ // Update the cell cache for the new cell position:
+ int NoiseBaseX = a_CellX - 2;
+ int NoiseBaseZ = a_CellZ - 2;
+ for (int x = 0; x < 5; x++)
+ {
+ int BaseX = (NoiseBaseX + x) * m_CellSize;
+ for (int z = 0; z < 5; z++)
+ {
+ int OffsetX = (m_Noise1.IntNoise2DInt(NoiseBaseX + x, NoiseBaseZ + z) / 8) % m_CellSize;
+ int OffsetZ = (m_Noise2.IntNoise2DInt(NoiseBaseX + x, NoiseBaseZ + z) / 8) % m_CellSize;
+ m_SeedX[x][z] = BaseX + OffsetX;
+ m_SeedZ[x][z] = (NoiseBaseZ + z) * m_CellSize + OffsetZ;
+ } // for z
+ } // for x
+ m_CurrentCellX = a_CellX;
+ m_CurrentCellZ = a_CellZ;
+}
+
+
+
+