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 "../BlockArea.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Leaves can be this many blocks that away (inclusive) from the log not to decay
|
|
|
|
#define LEAVES_CHECK_DISTANCE 6
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-17 05:36:37 -04:00
|
|
|
class cBlockLeavesHandler:
|
2013-07-29 07:13:03 -04:00
|
|
|
public cBlockHandler
|
|
|
|
{
|
2020-04-17 05:36:37 -04:00
|
|
|
using Super = cBlockHandler;
|
|
|
|
|
|
|
|
/** Returns true if the area contains a continous path from the specified block to a log block entirely made out of leaves blocks. */
|
|
|
|
static bool HasNearLog(cBlockArea & a_Area, const Vector3i a_BlockPos)
|
|
|
|
{
|
|
|
|
// Filter the blocks into a {leaves, log, other (air)} set:
|
|
|
|
auto * Types = a_Area.GetBlockTypes();
|
|
|
|
for (size_t i = a_Area.GetBlockCount() - 1; i > 0; i--)
|
|
|
|
{
|
|
|
|
switch (Types[i])
|
|
|
|
{
|
|
|
|
case E_BLOCK_LEAVES:
|
|
|
|
case E_BLOCK_LOG:
|
|
|
|
case E_BLOCK_NEW_LEAVES:
|
|
|
|
case E_BLOCK_NEW_LOG:
|
|
|
|
{
|
|
|
|
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
|
|
|
|
a_Area.SetBlockType(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, E_BLOCK_SPONGE);
|
|
|
|
for (int i = 0; i < LEAVES_CHECK_DISTANCE; i++)
|
|
|
|
{
|
|
|
|
auto ProcessNeighbor = [&a_Area, i](int cbx, int cby, int cbz) -> bool
|
|
|
|
{
|
|
|
|
switch (a_Area.GetBlockType(cbx, cby, cbz))
|
|
|
|
{
|
|
|
|
case E_BLOCK_LEAVES: a_Area.SetBlockType(cbx, cby, cbz, static_cast<BLOCKTYPE>(E_BLOCK_SPONGE + i + 1)); break;
|
|
|
|
case E_BLOCK_LOG: return true;
|
|
|
|
case E_BLOCK_NEW_LEAVES: a_Area.SetBlockType(cbx, cby, cbz, static_cast<BLOCKTYPE>(E_BLOCK_SPONGE + i + 1)); break;
|
|
|
|
case E_BLOCK_NEW_LOG: return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
for (int y = std::max(a_BlockPos.y - i, 0); y <= std::min(a_BlockPos.y + i, cChunkDef::Height - 1); y++)
|
|
|
|
{
|
|
|
|
for (int z = a_BlockPos.z - i; z <= a_BlockPos.z + i; z++)
|
|
|
|
{
|
|
|
|
for (int x = a_BlockPos.x - i; x <= a_BlockPos.x + i; x++)
|
|
|
|
{
|
|
|
|
if (a_Area.GetBlockType(x, y, z) != E_BLOCK_SPONGE + i)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
ProcessNeighbor(x - 1, y, z) ||
|
|
|
|
ProcessNeighbor(x + 1, y, z) ||
|
|
|
|
ProcessNeighbor(x, y, z - 1) ||
|
|
|
|
ProcessNeighbor(x, y, z + 1) ||
|
|
|
|
ProcessNeighbor(x, y + 1, z) ||
|
|
|
|
ProcessNeighbor(x, y - 1, z)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // for x
|
|
|
|
} // for z
|
|
|
|
} // for y
|
|
|
|
} // for i - BFS iterations
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
public:
|
2020-04-17 05:36:37 -04:00
|
|
|
|
|
|
|
cBlockLeavesHandler(BLOCKTYPE a_BlockType):
|
|
|
|
Super(a_BlockType)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual cItems ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool) override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
// If breaking with shears, drop self:
|
|
|
|
if ((a_Tool != nullptr) && (a_Tool->m_ItemType == E_ITEM_SHEARS))
|
|
|
|
{
|
|
|
|
return cItem(m_BlockType, a_BlockMeta & 0x03);
|
|
|
|
}
|
|
|
|
|
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.
|
2019-10-16 04:06:34 -04:00
|
|
|
// Note: It is possible (though very rare) for a single leaves block to drop both a sapling and an apple
|
2015-05-18 08:32:47 -04:00
|
|
|
// TODO: Take into account fortune for sapling drops.
|
2017-06-13 15:35:30 -04:00
|
|
|
double chance = 0.0;
|
2019-10-16 04:06:34 -04:00
|
|
|
auto & rand = GetRandomProvider();
|
|
|
|
cItems res;
|
2015-05-18 08:32:47 -04:00
|
|
|
if ((m_BlockType == E_BLOCK_LEAVES) && ((a_BlockMeta & 0x03) == E_META_LEAVES_JUNGLE))
|
|
|
|
{
|
|
|
|
// Jungle leaves have a 2.5% chance of dropping a sapling.
|
2017-06-13 15:35:30 -04:00
|
|
|
chance = 0.025;
|
2015-05-18 08:32:47 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Other leaves have a 5% chance of dropping a sapling.
|
2017-06-13 15:35:30 -04:00
|
|
|
chance = 0.05;
|
2015-05-18 08:32:47 -04:00
|
|
|
}
|
2017-06-13 15:35:30 -04:00
|
|
|
if (rand.RandBool(chance))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
res.Add(
|
|
|
|
E_BLOCK_SAPLING,
|
|
|
|
1,
|
|
|
|
(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
|
|
|
{
|
2017-06-13 15:35:30 -04:00
|
|
|
if (rand.RandBool(0.005))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
res.Add(E_ITEM_RED_APPLE, 1, 0);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
2019-10-16 04:06:34 -04:00
|
|
|
return res;
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2015-05-18 08:32:47 -04:00
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
virtual void OnNeighborChanged(cChunkInterface & a_ChunkInterface, Vector3i a_BlockPos, eBlockFace a_WhichNeighbor) override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
auto meta = a_ChunkInterface.GetBlockMeta(a_BlockPos);
|
2016-04-18 06:30:23 -04:00
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
// Set bit 0x08, so this block gets checked for decay:
|
|
|
|
if ((meta & 0x08) == 0)
|
2015-07-01 04:40:16 -04:00
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
a_ChunkInterface.SetBlockMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, meta | 0x8, true, false);
|
2015-07-01 04:40:16 -04:00
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2016-02-05 16:45:45 -05:00
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-04-17 05:36:37 -04:00
|
|
|
virtual void OnUpdate(
|
|
|
|
cChunkInterface & a_ChunkInterface,
|
|
|
|
cWorldInterface & a_WorldInterface,
|
|
|
|
cBlockPluginInterface & a_PluginInterface,
|
|
|
|
cChunk & a_Chunk,
|
|
|
|
const Vector3i a_RelPos
|
|
|
|
) override
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2020-04-17 05:36:37 -04:00
|
|
|
auto Meta = a_Chunk.GetMeta(a_RelPos);
|
2013-07-29 07:13:03 -04:00
|
|
|
if ((Meta & 0x04) != 0)
|
|
|
|
{
|
|
|
|
// Player-placed leaves, don't decay
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-17 05:36:37 -04:00
|
|
|
if ((Meta & 0x08) == 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:
|
2020-04-17 05:36:37 -04:00
|
|
|
auto worldPos = a_Chunk.RelativeToAbsolute(a_RelPos);
|
2013-07-29 07:13:03 -04:00
|
|
|
cBlockArea Area;
|
|
|
|
if (!Area.Read(
|
2017-02-05 10:00:38 -05:00
|
|
|
*a_Chunk.GetWorld(),
|
2020-04-17 05:36:37 -04:00
|
|
|
worldPos - Vector3i(LEAVES_CHECK_DISTANCE, LEAVES_CHECK_DISTANCE, LEAVES_CHECK_DISTANCE),
|
|
|
|
worldPos + Vector3i(LEAVES_CHECK_DISTANCE, LEAVES_CHECK_DISTANCE, LEAVES_CHECK_DISTANCE),
|
2013-07-29 07:13:03 -04:00
|
|
|
cBlockArea::baTypes)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// Cannot check leaves, a chunk is missing too close
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-17 05:36:37 -04:00
|
|
|
if (HasNearLog(Area, worldPos))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2016-04-18 06:30:23 -04:00
|
|
|
// Wood found, the leaves stay; unset the check bit
|
2020-04-17 05:36:37 -04:00
|
|
|
a_Chunk.SetMeta(a_RelPos, Meta ^ 0x08, true, false);
|
2013-07-29 07:13:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-30 09:58:27 -05:00
|
|
|
// Decay the leaves:
|
2020-04-17 05:36:37 -04:00
|
|
|
a_ChunkInterface.DropBlockAsPickups(worldPos);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
2015-06-30 10:50:15 -04:00
|
|
|
|
2019-10-16 04:06:34 -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
|
|
|
} ;
|