summaryrefslogtreecommitdiffstats
path: root/source/cChunk.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--source/cChunk.cpp140
1 files changed, 109 insertions, 31 deletions
diff --git a/source/cChunk.cpp b/source/cChunk.cpp
index ff1f7472b..6c9165b30 100644
--- a/source/cChunk.cpp
+++ b/source/cChunk.cpp
@@ -27,6 +27,8 @@
#include "cBlockToPickup.h"
#include "MersenneTwister.h"
#include "cPlayer.h"
+#include "BlockArea.h"
+#include "cPluginManager.h"
#include "packets/cPacket_DestroyEntity.h"
#include "packets/cPacket_PreChunk.h"
@@ -39,6 +41,13 @@
+// Leaves can be this many blocks that away from the log not to decay
+#define LEAVES_CHECK_DISTANCE 4
+
+
+
+
+
extern bool g_bWaterPhysics;
@@ -515,6 +524,13 @@ void cChunk::CheckBlocks(void)
}
break;
}
+
+ // If anything next to a leaves block changes, set the leaves' "check for decay" bit (clear bit 0x08):
+ case E_BLOCK_LEAVES:
+ {
+ cChunkDef::SetNibble(m_BlockMeta, index, BlockMeta & 0x07);
+ break;
+ }
} // switch (BlockType)
} // for itr - ToTickBlocks[]
}
@@ -591,7 +607,7 @@ void cChunk::TickBlocks(MTRand & a_TickRandom)
break;
}
- case E_BLOCK_LEAVES: TickLeaves(m_BlockTickX, m_BlockTickY, m_BlockTickZ); break;
+ case E_BLOCK_LEAVES: TickLeaves(m_BlockTickX, m_BlockTickY, m_BlockTickZ, a_TickRandom); break;
default:
{
@@ -746,53 +762,115 @@ void cChunk::TickFarmland(int a_RelX, int a_RelY, int a_RelZ)
-void cChunk::TickLeaves(int a_RelX, int a_RelY, int a_RelZ)
+void cChunk::TickLeaves(int a_RelX, int a_RelY, int a_RelZ, MTRand & a_TickRandom)
{
// Since leaves-checking is a costly operation, it is done only if leaves are marked for checking (Meta has its 0x08 bit cleared)
- // TODO: The meta is cleared (check flag set) each time a block next to the leaves changes
+ // The meta bit 0x08 bit is cleared (check flag set) each time a block next to the leaves changes
- NIBBLETYPE Meta = GetMeta(m_BlockTickX, m_BlockTickY, m_BlockTickZ);
+ NIBBLETYPE Meta = GetMeta(a_RelX, a_RelY, a_RelZ);
if ((Meta & 0x04) != 0)
{
// Player-placed leaves, don't decay
return;
}
- if ((Meta & 0x08) == 0)
+ if ((Meta & 0x08) != 0)
{
// These leaves have been checked for decay lately and nothing around them changed
return;
}
-
- // TODO: We need a proper BFS check - is the leaves block connected to a log via other leaves blocks?
- for (int y = 4; y > - 5; y--)
+
+ // Get the data around the leaves:
+ cBlockArea Area;
+ int BaseX = cChunkDef::Width * m_PosX + a_RelX;
+ int BaseZ = cChunkDef::Width * m_PosZ + a_RelZ;
+ if (!Area.Read(
+ m_World,
+ BaseX - LEAVES_CHECK_DISTANCE, BaseX + LEAVES_CHECK_DISTANCE,
+ a_RelY - LEAVES_CHECK_DISTANCE, a_RelY + LEAVES_CHECK_DISTANCE,
+ BaseZ - LEAVES_CHECK_DISTANCE, BaseZ + LEAVES_CHECK_DISTANCE,
+ cBlockArea::baTypes)
+ )
{
- for (int z = - 4; z < 5; z++ )
+ // Cannot check leaves, a chunk is missing too close
+ return;
+ }
+
+ if (HasNearLog(Area, BaseX, a_RelY, BaseZ))
+ {
+ // Wood found, the leaves stay; mark them as checked:
+ SetNibble(m_BlockMeta, a_RelX, a_RelY, a_RelZ, Meta & 0x07);
+ return;
+ }
+ // Decay the leaves:
+ m_World->DigBlock(m_PosX * cChunkDef::Width + a_RelX, a_RelY, m_PosZ * cChunkDef::Width + a_RelZ);
+
+ // Let them drop something if the random is right:
+ cItems PickupItems;
+ cBlockToPickup::ToPickup(E_BLOCK_LEAVES, Meta, cItem(), PickupItems);
+
+ // Allow plugins to change the dropped objects:
+ cRoot::Get()->GetPluginManager()->CallHookBlockToPickup(E_BLOCK_LEAVES, Meta, NULL, cItem(), PickupItems);
+ m_World->SpawnItemPickups(PickupItems, BaseX, a_RelY, BaseZ);
+}
+
+
+
+
+
+#define PROCESS_NEIGHBOR(x,y,z) \
+ switch (a_Area.GetBlockType(x, y, z)) \
+ { \
+ case E_BLOCK_LEAVES: a_Area.SetBlockType(x, y, z, E_BLOCK_SPONGE + i + 1); break; \
+ case E_BLOCK_LOG: return true; \
+ }
+
+bool cChunk::HasNearLog(cBlockArea & a_Area, int a_BlockX, int a_BlockY, int a_BlockZ)
+{
+ // Filter the blocks into a {leaves, log, other (air)} set:
+ BLOCKTYPE * Types = a_Area.GetBlockTypes();
+ for (int i = a_Area.GetBlockCount() - 1; i > 0; i--)
+ {
+ switch (Types[i])
{
- for (int x = - 4; x < 5; x++ )
+ case E_BLOCK_LEAVES:
+ case E_BLOCK_LOG:
{
- if (abs(x) + abs(y) + abs(z) > 4)
- {
- // Too far away
- continue;
- }
- BLOCKTYPE BlockType;
- NIBBLETYPE BlockMeta;
- if (!UnboundedRelGetBlock(a_RelX + x, a_RelY + y, a_RelZ + z, BlockType, BlockMeta))
- {
- // Too close to unloaded chunks, don't check at all
- return;
- }
- if (BlockType == E_BLOCK_LOG)
- {
- // Wood found, the leaves stay; mark them as checked:
- SetNibble(m_BlockMeta, a_RelX, a_RelY, a_RelZ, Meta & 0x07);
- return;
- }
+ break;
+ }
+ default:
+ {
+ Types[i] = E_BLOCK_AIR;
+ break;
}
}
- }
- // Decay the leaves. Let them drop something if the random is right
- m_World->DigBlock(m_PosX * cChunkDef::Width + a_RelX, a_RelY, m_PosZ * cChunkDef::Width + a_RelZ);
+ } // for i - Types[]
+
+ // Perform a breadth-first search to see if there's a log connected within 4 blocks of the leaves block:
+ // Simply replace all reachable leaves blocks with a sponge block plus iteration (in the Area) and see if we can reach a log in 4 iterations
+ a_Area.SetBlockType(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_SPONGE);
+ for (int i = 0; i < LEAVES_CHECK_DISTANCE; i++)
+ {
+ for (int y = a_BlockY - i; y <= a_BlockY + i; y++)
+ {
+ for (int z = a_BlockZ - i; z <= a_BlockZ + i; z++)
+ {
+ for (int x = a_BlockX - i; x <= a_BlockX + i; x++)
+ {
+ if (a_Area.GetBlockType(x, y, z) != E_BLOCK_SPONGE + i)
+ {
+ continue;
+ }
+ PROCESS_NEIGHBOR(x - 1, y, z);
+ PROCESS_NEIGHBOR(x + 1, y, z);
+ PROCESS_NEIGHBOR(x, y, z - 1);
+ PROCESS_NEIGHBOR(x, y, z + 1);
+ PROCESS_NEIGHBOR(x, y + 1, z);
+ PROCESS_NEIGHBOR(x, y - 1, z);
+ } // for x
+ } // for z
+ } // for y
+ } // for i - BFS iterations
+ return false;
}