1
0

Leaves decay properly - if they are not connected to a log

git-svn-id: http://mc-server.googlecode.com/svn/trunk@644 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
madmaxoft@gmail.com 2012-07-02 19:22:05 +00:00
parent eab3f29bc0
commit 919f896894
3 changed files with 123 additions and 32 deletions

View File

@ -76,6 +76,15 @@ public:
/// Returns the datatypes that are stored in the object (bitmask of baXXX values)
int GetDataTypes(void) const;
// tolua_end
// Clients can use these for faster access to all blocktypes. Be careful though!
/// Returns the internal pointer to the block types
BLOCKTYPE * GetBlockTypes(void) { return m_BlockTypes; }
int GetBlockCount(void) const { return m_SizeX * m_SizeY * m_SizeZ; }
// tolua_begin
protected:

View File

@ -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;
}

View File

@ -40,6 +40,7 @@ class cPlayer;
class cChunkMap;
class cChestEntity;
class cFurnaceEntity;
class cBlockArea;
typedef std::list<cClientHandle *> cClientHandleList;
typedef cItemCallback<cEntity> cEntityCallback;
@ -256,7 +257,7 @@ private:
void TickGrass (int a_RelX, int a_RelY, int a_RelZ, MTRand & a_TickRandom);
void TickMelonPumpkin(int a_RelX, int a_RelY, int a_RelZ, int a_BlockIdx, BLOCKTYPE a_BlockType, MTRand & a_TickRandom);
void TickFarmland (int a_RelX, int a_RelY, int a_RelZ);
void TickLeaves (int a_RelX, int a_RelY, int a_RelZ);
void TickLeaves (int a_RelX, int a_RelY, int a_RelZ, MTRand & a_TickRandom);
/// Grows sugarcane by the specified number of blocks, but no more than 3 blocks high (used by both bonemeal and ticking)
void GrowSugarcane (int a_RelX, int a_RelY, int a_RelZ, int a_NumBlocks);
@ -267,6 +268,9 @@ private:
/// Grows a melon or a pumpkin next to the block specified (assumed to be the stem)
void GrowMelonPumpkin(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_BlockType, MTRand & a_Random);
/// Checks if a leaves block at the specified coords has a log up to 4 blocks away connected by other leaves blocks (false if no log)
bool HasNearLog(cBlockArea & a_Area, int a_BlockX, int a_BlockY, int a_BlockZ);
/// Same as GetBlock(), but relative coords needn't be in this chunk (uses m_ChunkMap in such a case); returns true on success; only usable in Tick()
bool UnboundedRelGetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta);