2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "BlockPiston.h"
|
2020-04-03 02:57:01 -04:00
|
|
|
#include "../BlockInfo.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
#include "../Item.h"
|
|
|
|
#include "../World.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Player.h"
|
2018-08-28 20:51:25 -04:00
|
|
|
#include "../BlockInServerPluginInterface.h"
|
2014-09-26 13:13:19 -04:00
|
|
|
#include "ChunkInterface.h"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-05-29 06:57:06 -04:00
|
|
|
#define PISTON_MAX_PUSH_DISTANCE 12
|
2014-05-25 08:46:34 -04:00
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
cBlockPistonHandler::cBlockPistonHandler(BLOCKTYPE a_BlockType):
|
2020-04-13 12:38:06 -04:00
|
|
|
Super(a_BlockType)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
void cBlockPistonHandler::OnBroken(
|
|
|
|
cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
|
|
|
|
Vector3i a_BlockPos,
|
|
|
|
BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta
|
|
|
|
)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2020-08-01 06:25:06 -04:00
|
|
|
if (!IsExtended(a_OldBlockMeta))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2020-08-01 06:25:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto Extension = a_BlockPos + MetadataToOffset(a_OldBlockMeta);
|
|
|
|
if (
|
|
|
|
cChunkDef::IsValidHeight(Extension.y) &&
|
|
|
|
(a_ChunkInterface.GetBlock(Extension) == E_BLOCK_PISTON_EXTENSION)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
// If the piston is extended, destroy the extension as well:
|
|
|
|
a_ChunkInterface.SetBlock(Extension, E_BLOCK_AIR, 0);
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-06 11:11:54 -05:00
|
|
|
Vector3i cBlockPistonHandler::MetadataToOffset(NIBBLETYPE a_PistonMeta)
|
2015-11-05 08:50:43 -05:00
|
|
|
{
|
|
|
|
switch (a_PistonMeta & 0x07)
|
|
|
|
{
|
|
|
|
case 0: return Vector3i( 0, -1, 0);
|
|
|
|
case 1: return Vector3i( 0, 1, 0);
|
|
|
|
case 2: return Vector3i( 0, 0, -1);
|
|
|
|
case 3: return Vector3i( 0, 0, 1);
|
|
|
|
case 4: return Vector3i(-1, 0, 0);
|
|
|
|
case 5: return Vector3i( 1, 0, 0);
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
LOGWARNING("%s: invalid direction %d, ignoring", __FUNCTION__, a_PistonMeta & 0x07);
|
2015-11-06 11:11:54 -05:00
|
|
|
ASSERT(!"Invalid direction");
|
2015-11-05 08:50:43 -05:00
|
|
|
return Vector3i();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-06 11:11:54 -05:00
|
|
|
void cBlockPistonHandler::PushBlocks(
|
|
|
|
const Vector3iSet & a_BlocksToPush,
|
2017-07-14 22:09:55 -04:00
|
|
|
cWorld & a_World, const Vector3i & a_PushDir
|
2015-11-05 12:49:15 -05:00
|
|
|
)
|
|
|
|
{
|
2020-08-01 06:25:06 -04:00
|
|
|
// Sort blocks to move the blocks first, which are farthest away from the piston
|
2015-11-05 14:13:58 -05:00
|
|
|
// This prevents the overwriting of existing blocks
|
2015-11-05 12:49:15 -05:00
|
|
|
std::vector<Vector3i> sortedBlocks(a_BlocksToPush.begin(), a_BlocksToPush.end());
|
|
|
|
std::sort(sortedBlocks.begin(), sortedBlocks.end(), [a_PushDir](const Vector3i & a, const Vector3i & b)
|
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
return (a.Dot(a_PushDir) > b.Dot(a_PushDir));
|
2015-11-05 12:49:15 -05:00
|
|
|
});
|
|
|
|
|
2015-11-05 14:13:58 -05:00
|
|
|
// Move every block
|
2015-11-05 12:49:15 -05:00
|
|
|
BLOCKTYPE moveBlock;
|
|
|
|
NIBBLETYPE moveMeta;
|
2015-11-06 11:11:54 -05:00
|
|
|
for (auto & moveBlockPos : sortedBlocks)
|
2015-11-05 14:13:58 -05:00
|
|
|
{
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.GetBlockTypeMeta(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, moveBlock, moveMeta);
|
2015-11-05 12:49:15 -05:00
|
|
|
|
|
|
|
if (cBlockInfo::IsPistonBreakable(moveBlock))
|
|
|
|
{
|
2019-10-16 04:06:34 -04:00
|
|
|
// Block is breakable, drop it:
|
|
|
|
a_World.DropBlockAsPickups(moveBlockPos, nullptr, nullptr);
|
2016-10-20 13:09:18 -04:00
|
|
|
}
|
|
|
|
else
|
2015-11-05 12:49:15 -05:00
|
|
|
{
|
2015-11-05 14:13:58 -05:00
|
|
|
// Not breakable, just move it
|
2017-08-25 05:56:01 -04:00
|
|
|
a_World.SetBlock(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, E_BLOCK_AIR, 0);
|
|
|
|
moveBlockPos += a_PushDir;
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.SetBlock(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, moveBlock, moveMeta);
|
2015-11-05 12:49:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-02 16:12:58 -05:00
|
|
|
bool cBlockPistonHandler::CanPushBlock(
|
2017-07-14 22:09:55 -04:00
|
|
|
const Vector3i & a_BlockPos, cWorld & a_World, bool a_RequirePushable,
|
2015-11-06 11:11:54 -05:00
|
|
|
Vector3iSet & a_BlocksPushed, const Vector3i & a_PushDir
|
2015-11-02 16:12:58 -05:00
|
|
|
)
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
2015-11-06 11:11:54 -05:00
|
|
|
const static std::array<Vector3i, 6> pushingDirs =
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Vector3i(-1, 0, 0), Vector3i(1, 0, 0),
|
|
|
|
Vector3i( 0, -1, 0), Vector3i(0, 1, 0),
|
|
|
|
Vector3i( 0, 0, -1), Vector3i(0, 0, 1)
|
|
|
|
}
|
|
|
|
};
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2015-11-02 16:12:58 -05:00
|
|
|
BLOCKTYPE currBlock;
|
|
|
|
NIBBLETYPE currMeta;
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, currBlock, currMeta);
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2020-03-22 16:51:11 -04:00
|
|
|
if (!cChunkDef::IsValidHeight(a_BlockPos.y))
|
|
|
|
{
|
|
|
|
return !a_RequirePushable;
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:12:58 -05:00
|
|
|
if (currBlock == E_BLOCK_AIR)
|
|
|
|
{
|
|
|
|
// Air can be pushed
|
|
|
|
return true;
|
|
|
|
}
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2015-11-05 14:13:58 -05:00
|
|
|
if (!a_RequirePushable && cBlockInfo::IsPistonBreakable(currBlock))
|
2015-11-05 06:36:51 -05:00
|
|
|
{
|
|
|
|
// Block should not be broken, when it's not in the pushing direction
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-02 16:12:58 -05:00
|
|
|
if (!CanPush(currBlock, currMeta))
|
|
|
|
{
|
2015-11-05 14:13:58 -05:00
|
|
|
// When it's not required to push this block, don't fail
|
2015-11-02 16:12:58 -05:00
|
|
|
return !a_RequirePushable;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a_BlocksPushed.size() >= PISTON_MAX_PUSH_DISTANCE)
|
|
|
|
{
|
2015-11-05 14:13:58 -05:00
|
|
|
// Do not allow to push too much blocks
|
2015-11-02 16:12:58 -05:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2015-11-07 11:22:53 -05:00
|
|
|
if (!a_BlocksPushed.insert(a_BlockPos).second || cBlockInfo::IsPistonBreakable(currBlock))
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
2015-11-02 16:12:58 -05:00
|
|
|
return true; // Element exist already
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2015-11-05 14:13:58 -05:00
|
|
|
if (currBlock == E_BLOCK_SLIME_BLOCK)
|
2015-11-05 04:25:59 -05:00
|
|
|
{
|
|
|
|
// Try to push the other directions
|
2015-11-06 11:11:54 -05:00
|
|
|
for (const auto & testDir : pushingDirs)
|
2015-11-05 04:25:59 -05:00
|
|
|
{
|
2015-11-07 11:22:53 -05:00
|
|
|
if (!CanPushBlock(a_BlockPos + testDir, a_World, false, a_BlocksPushed, a_PushDir))
|
2015-11-05 04:25:59 -05:00
|
|
|
{
|
2015-11-05 14:13:58 -05:00
|
|
|
// When it's not possible for a direction, then fail
|
2015-11-05 04:25:59 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 14:13:58 -05:00
|
|
|
// Try to push the block in front of this block
|
2015-11-07 11:22:53 -05:00
|
|
|
return CanPushBlock(a_BlockPos + a_PushDir, a_World, true, a_BlocksPushed, a_PushDir);
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
void cBlockPistonHandler::ExtendPiston(Vector3i a_BlockPos, cWorld & a_World)
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
|
|
|
{
|
2018-07-23 19:35:20 -04:00
|
|
|
// Broadcast block action first. Will do nothing if piston cannot in fact push
|
|
|
|
|
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
|
|
|
|
a_World.BroadcastBlockAction(a_BlockPos, PistonExtendAction, pistonMeta, pistonBlock);
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
// Client expects the server to "play" the animation before setting the final blocks
|
|
|
|
// However, we don't confuse animation with the underlying state of the world, so emulate by delaying 1 tick
|
|
|
|
// (Probably why vanilla has so many dupe glitches with sand and pistons lolol)
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
a_World.ScheduleTask(1, [a_BlockPos](cWorld & World)
|
|
|
|
{
|
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
if ((pistonBlock != E_BLOCK_PISTON) && !IsSticky(pistonBlock))
|
|
|
|
{
|
|
|
|
// Ensure we operate on a piston to avoid spurious behaviour
|
|
|
|
// Note that the scheduled task may result in the block type of a_BlockPos changing
|
|
|
|
return;
|
|
|
|
}
|
2014-05-25 08:46:34 -04:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
if (IsExtended(pistonMeta))
|
|
|
|
{
|
|
|
|
// Already extended, bail out
|
|
|
|
return;
|
|
|
|
}
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
Vector3i pushDir = MetadataToOffset(pistonMeta);
|
|
|
|
Vector3iSet blocksPushed;
|
|
|
|
if (!CanPushBlock(a_BlockPos + pushDir, World, true, blocksPushed, pushDir))
|
|
|
|
{
|
|
|
|
// Can't push anything, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
PushBlocks(blocksPushed, World, pushDir);
|
|
|
|
|
|
|
|
// Set the extension and the piston base correctly
|
|
|
|
Vector3i extensionPos = a_BlockPos + pushDir;
|
|
|
|
World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta | 0x8);
|
|
|
|
World.SetBlock(
|
|
|
|
extensionPos.x, extensionPos.y, extensionPos.z,
|
|
|
|
E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Play sound effect only if extended successfully
|
|
|
|
World.BroadcastSoundEffect("block.piston.extend", a_BlockPos, 0.5f, 0.7f);
|
|
|
|
}
|
2015-11-05 12:49:15 -05:00
|
|
|
);
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
void cBlockPistonHandler::RetractPiston(Vector3i a_BlockPos, cWorld & a_World)
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
|
|
|
{
|
2018-07-23 19:35:20 -04:00
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
|
|
|
|
a_World.BroadcastBlockAction(a_BlockPos, PistonRetractAction, pistonMeta, pistonBlock);
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
a_World.ScheduleTask(1, [a_BlockPos](cWorld & World)
|
|
|
|
{
|
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
|
2015-11-05 08:50:43 -05:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
if ((pistonBlock != E_BLOCK_PISTON) && !IsSticky(pistonBlock))
|
|
|
|
{
|
|
|
|
// Ensure we operate on a piston to avoid spurious behaviour
|
|
|
|
// Note that the scheduled task may result in the block type of a_BlockPos changing
|
|
|
|
return;
|
|
|
|
}
|
2014-05-25 08:46:34 -04:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
if (!IsExtended(pistonMeta))
|
|
|
|
{
|
|
|
|
// Already retracted, bail out
|
|
|
|
return;
|
|
|
|
}
|
2015-11-05 05:39:07 -05:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
Vector3i pushDir = MetadataToOffset(pistonMeta);
|
2014-05-25 08:46:34 -04:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
// Check the extension:
|
|
|
|
Vector3i extensionPos = a_BlockPos + pushDir;
|
|
|
|
if (World.GetBlock(extensionPos) != E_BLOCK_PISTON_EXTENSION)
|
|
|
|
{
|
|
|
|
LOGD("%s: Piston without an extension - still extending, or just in an invalid state?", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2020-08-01 06:25:06 -04:00
|
|
|
// Remove extension, update base state:
|
2020-04-03 16:24:49 -04:00
|
|
|
World.SetBlock(extensionPos, E_BLOCK_AIR, 0);
|
|
|
|
World.SetBlock(a_BlockPos, pistonBlock, pistonMeta & ~(8));
|
2015-11-05 05:39:07 -05:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
// (Retraction is always successful, but play in the task for consistency)
|
|
|
|
World.BroadcastSoundEffect("block.piston.contract", a_BlockPos, 0.5f, 0.7f);
|
2015-11-05 05:39:07 -05:00
|
|
|
|
2018-07-23 19:35:20 -04:00
|
|
|
if (!IsSticky(pistonBlock))
|
|
|
|
{
|
|
|
|
// No need for block pulling, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the block to pull
|
|
|
|
Vector3i AdjustedPosition = a_BlockPos + pushDir * 2;
|
|
|
|
// Try to "push" the pulling block in the opposite direction
|
|
|
|
pushDir *= -1;
|
|
|
|
|
|
|
|
Vector3iSet pushedBlocks;
|
|
|
|
if (!CanPushBlock(AdjustedPosition, World, false, pushedBlocks, pushDir))
|
|
|
|
{
|
|
|
|
// Not pushable, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PushBlocks(pushedBlocks, World, pushDir);
|
|
|
|
}
|
|
|
|
);
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-07-17 16:15:34 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2013-08-09 10:20:12 -04:00
|
|
|
// cBlockPistonHeadHandler:
|
|
|
|
|
|
|
|
cBlockPistonHeadHandler::cBlockPistonHeadHandler(void) :
|
2020-04-13 12:38:06 -04:00
|
|
|
Super(E_BLOCK_PISTON_EXTENSION)
|
2013-08-09 10:20:12 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-10-16 04:06:34 -04:00
|
|
|
void cBlockPistonHeadHandler::OnBroken(
|
|
|
|
cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface,
|
|
|
|
Vector3i a_BlockPos,
|
|
|
|
BLOCKTYPE a_OldBlockType, NIBBLETYPE a_OldBlockMeta
|
|
|
|
)
|
2013-08-09 10:20:12 -04:00
|
|
|
{
|
2020-08-01 06:25:06 -04:00
|
|
|
const auto Base = a_BlockPos - cBlockPistonHandler::MetadataToOffset(a_OldBlockMeta);
|
|
|
|
if (!cChunkDef::IsValidHeight(Base.y))
|
2013-08-09 10:20:12 -04:00
|
|
|
{
|
2020-08-01 06:25:06 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto Block = a_ChunkInterface.GetBlock(Base);
|
|
|
|
if ((Block == E_BLOCK_PISTON) || (Block == E_BLOCK_STICKY_PISTON))
|
|
|
|
{
|
|
|
|
// Remove the base of the piston:
|
|
|
|
a_ChunkInterface.SetBlock(Base, E_BLOCK_AIR, 0);
|
2013-08-09 10:20:12 -04:00
|
|
|
}
|
|
|
|
}
|
2015-11-06 11:11:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-08-01 06:25:06 -04:00
|
|
|
cItems cBlockPistonHeadHandler::ConvertToPickups(NIBBLETYPE a_BlockMeta, cBlockEntity * a_BlockEntity, const cEntity * a_Digger, const cItem * a_Tool)
|
|
|
|
{
|
|
|
|
// Give a normal\sticky piston base, not piston extension
|
|
|
|
// With 1.7, the item forms of these technical blocks have been removed, so giving someone this will crash their client...
|
|
|
|
return { cItem(((a_BlockMeta & 0x8) == 0x8) ? E_BLOCK_STICKY_PISTON : E_BLOCK_PISTON) };
|
|
|
|
}
|