2013-07-29 07:13:03 -04:00
|
|
|
#pragma once
|
|
|
|
#include "BlockHandler.h"
|
2014-03-30 17:13:13 -04:00
|
|
|
#include "../FastRandom.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "../World.h"
|
|
|
|
#include "../BlockArea.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Leaves can be this many blocks that away (inclusive) from the log not to decay
|
|
|
|
#define LEAVES_CHECK_DISTANCE 6
|
|
|
|
|
2014-07-19 08:53:41 -04:00
|
|
|
#define PROCESS_NEIGHBOR(x, y, z) \
|
2013-07-29 07:13:03 -04:00
|
|
|
switch (a_Area.GetBlockType(x, y, z)) \
|
|
|
|
{ \
|
2015-07-29 11:04:03 -04:00
|
|
|
case E_BLOCK_LEAVES: a_Area.SetBlockType(x, y, z, static_cast<BLOCKTYPE>(E_BLOCK_SPONGE + i + 1)); break; \
|
2013-07-29 07:13:03 -04:00
|
|
|
case E_BLOCK_LOG: return true; \
|
2015-07-29 11:04:03 -04:00
|
|
|
case E_BLOCK_NEW_LEAVES: a_Area.SetBlockType(x, y, z, static_cast<BLOCKTYPE>(E_BLOCK_SPONGE + i + 1)); break; \
|
2014-03-16 09:01:22 -04:00
|
|
|
case E_BLOCK_NEW_LOG: return true; \
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasNearLog(cBlockArea &a_Area, int a_BlockX, int a_BlockY, int a_BlockZ);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cBlockLeavesHandler :
|
|
|
|
public cBlockHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
cBlockLeavesHandler(BLOCKTYPE a_BlockType)
|
|
|
|
: cBlockHandler(a_BlockType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override
|
|
|
|
{
|
2014-03-30 17:13:13 -04:00
|
|
|
cFastRandom rand;
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2015-05-18 08:32:47 -04:00
|
|
|
// There is a chance to drop a sapling that varies depending on the type of leaf broken.
|
|
|
|
// TODO: Take into account fortune for sapling drops.
|
|
|
|
int chance;
|
|
|
|
if ((m_BlockType == E_BLOCK_LEAVES) && ((a_BlockMeta & 0x03) == E_META_LEAVES_JUNGLE))
|
|
|
|
{
|
|
|
|
// Jungle leaves have a 2.5% chance of dropping a sapling.
|
|
|
|
chance = rand.NextInt(40);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Other leaves have a 5% chance of dropping a sapling.
|
|
|
|
chance = rand.NextInt(20);
|
|
|
|
}
|
|
|
|
if (chance == 0)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-07-17 17:26:53 -04:00
|
|
|
a_Pickups.push_back(
|
|
|
|
cItem(
|
|
|
|
E_BLOCK_SAPLING,
|
|
|
|
1,
|
2015-05-19 06:55:23 -04:00
|
|
|
(m_BlockType == E_BLOCK_LEAVES) ? (a_BlockMeta & 0x03) : static_cast<short>(4 + (a_BlockMeta & 0x01))
|
2014-07-17 17:26:53 -04:00
|
|
|
)
|
|
|
|
);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2015-05-18 08:32:47 -04:00
|
|
|
|
|
|
|
// 0.5 % chance of dropping an apple, if the leaves' type is Apple Leaves
|
2014-07-17 17:26:53 -04:00
|
|
|
if ((m_BlockType == E_BLOCK_LEAVES) && ((a_BlockMeta & 0x03) == E_META_LEAVES_APPLE))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-05-18 08:32:47 -04:00
|
|
|
if (rand.NextInt(200) == 0)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
a_Pickups.push_back(cItem(E_ITEM_RED_APPLE, 1, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-18 08:32:47 -04:00
|
|
|
|
2015-07-01 04:40:16 -04:00
|
|
|
virtual void OnNeighborChanged(cChunkInterface & a_ChunkInterface, int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_WhichNeighbor) override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-07-01 04:40:16 -04:00
|
|
|
// Unset 0x8 bit so this block gets checked for decay:
|
2014-02-01 08:06:32 -05:00
|
|
|
NIBBLETYPE Meta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
|
2015-07-01 04:40:16 -04:00
|
|
|
if ((Meta & 0x08) != 0)
|
|
|
|
{
|
|
|
|
a_ChunkInterface.SetBlockMeta(a_BlockX, a_BlockY, a_BlockZ, Meta & 0x7);
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
2014-02-02 09:49:37 -05:00
|
|
|
virtual void OnUpdate(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cBlockPluginInterface & a_PluginInterface, cChunk & a_Chunk, int a_RelX, int a_RelY, int a_RelZ) override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2013-11-30 09:58:27 -05:00
|
|
|
NIBBLETYPE Meta = a_Chunk.GetMeta(a_RelX, a_RelY, a_RelZ);
|
2013-07-29 07:13:03 -04:00
|
|
|
if ((Meta & 0x04) != 0)
|
|
|
|
{
|
|
|
|
// Player-placed leaves, don't decay
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-16 11:15:22 -04:00
|
|
|
if ((Meta & 0x8) != 0)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// These leaves have been checked for decay lately and nothing around them changed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the data around the leaves:
|
2013-11-30 09:58:27 -05:00
|
|
|
int BlockX = a_RelX + a_Chunk.GetPosX() * cChunkDef::Width;
|
|
|
|
int BlockZ = a_RelZ + a_Chunk.GetPosZ() * cChunkDef::Width;
|
2013-07-29 07:13:03 -04:00
|
|
|
cBlockArea Area;
|
|
|
|
if (!Area.Read(
|
2014-07-17 16:50:58 -04:00
|
|
|
a_Chunk.GetWorld(),
|
|
|
|
BlockX - LEAVES_CHECK_DISTANCE, BlockX + LEAVES_CHECK_DISTANCE,
|
|
|
|
a_RelY - LEAVES_CHECK_DISTANCE, a_RelY + LEAVES_CHECK_DISTANCE,
|
|
|
|
BlockZ - LEAVES_CHECK_DISTANCE, BlockZ + LEAVES_CHECK_DISTANCE,
|
2013-07-29 07:13:03 -04:00
|
|
|
cBlockArea::baTypes)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Cannot check leaves, a chunk is missing too close
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-30 09:58:27 -05:00
|
|
|
if (HasNearLog(Area, BlockX, a_RelY, BlockZ))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
// Wood found, the leaves stay; mark them as checked:
|
2013-11-30 09:58:27 -05:00
|
|
|
a_Chunk.SetMeta(a_RelX, a_RelY, a_RelZ, Meta | 0x8);
|
2013-07-29 07:13:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-30 09:58:27 -05:00
|
|
|
// Decay the leaves:
|
2014-10-20 16:55:07 -04:00
|
|
|
DropBlock(a_ChunkInterface, a_WorldInterface, a_PluginInterface, nullptr, BlockX, a_RelY, BlockZ);
|
2014-02-02 09:49:37 -05:00
|
|
|
a_ChunkInterface.DigBlock(a_WorldInterface, BlockX, a_RelY, BlockZ);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2015-06-30 10:50:15 -04:00
|
|
|
|
|
|
|
virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override
|
|
|
|
{
|
|
|
|
UNUSED(a_Meta);
|
|
|
|
return 7;
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
} ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool 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();
|
2014-05-08 14:16:35 -04:00
|
|
|
for (size_t i = a_Area.GetBlockCount() - 1; i > 0; i--)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
switch (Types[i])
|
|
|
|
{
|
|
|
|
case E_BLOCK_LEAVES:
|
|
|
|
case E_BLOCK_LOG:
|
2014-05-08 14:16:35 -04:00
|
|
|
case E_BLOCK_NEW_LEAVES:
|
|
|
|
case E_BLOCK_NEW_LOG:
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
Types[i] = E_BLOCK_AIR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // 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++)
|
|
|
|
{
|
2014-08-30 19:15:48 -04:00
|
|
|
for (int y = std::max(a_BlockY - i, 0); y <= std::min(a_BlockY + i, 255); y++)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|