2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
#include "BlockPiston.h"
|
|
|
|
#include "../Item.h"
|
|
|
|
#include "../World.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "../Entities/Player.h"
|
2014-05-25 08:46:34 -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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cBlockPistonHandler::cBlockPistonHandler(BLOCKTYPE a_BlockType)
|
|
|
|
: cBlockHandler(a_BlockType)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-01 08:06:32 -05:00
|
|
|
void cBlockPistonHandler::OnDestroyed(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, int a_BlockX, int a_BlockY, int a_BlockZ)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2015-11-07 11:22:53 -05:00
|
|
|
Vector3i blockPos(a_BlockX, a_BlockY, a_BlockZ);
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2017-11-20 06:13:11 -05:00
|
|
|
NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(blockPos);
|
2016-07-05 19:21:53 -04:00
|
|
|
// If the piston is extended, destroy the extension as well
|
|
|
|
if (IsExtended(OldMeta))
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2016-07-05 19:21:53 -04:00
|
|
|
// Get the position of the extension
|
|
|
|
blockPos += MetadataToOffset(OldMeta);
|
|
|
|
|
|
|
|
if (a_ChunkInterface.GetBlock(blockPos) == E_BLOCK_PISTON_EXTENSION)
|
|
|
|
{
|
|
|
|
a_ChunkInterface.SetBlock(blockPos.x, blockPos.y, blockPos.z, E_BLOCK_AIR, 0);
|
|
|
|
}
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-03-20 06:14:42 -04:00
|
|
|
void cBlockPistonHandler::ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta)
|
|
|
|
{
|
|
|
|
// Returning Piston Item without Direction-Metavalue
|
|
|
|
a_Pickups.push_back(cItem(m_BlockType, 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-29 07:13:03 -04:00
|
|
|
bool cBlockPistonHandler::GetPlacementBlockTypeMeta(
|
2017-07-31 16:17:52 -04:00
|
|
|
cChunkInterface & a_ChunkInterface, cPlayer & a_Player,
|
2014-07-17 16:50:58 -04:00
|
|
|
int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace,
|
2013-07-29 07:13:03 -04:00
|
|
|
int a_CursorX, int a_CursorY, int a_CursorZ,
|
|
|
|
BLOCKTYPE & a_BlockType, NIBBLETYPE & a_BlockMeta
|
|
|
|
)
|
|
|
|
{
|
|
|
|
a_BlockType = m_BlockType;
|
2017-07-31 16:17:52 -04:00
|
|
|
a_BlockMeta = RotationPitchToMetaData(a_Player.GetYaw(), a_Player.GetPitch());
|
2013-07-29 07:13:03 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-09 10:20:12 -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
|
|
|
)
|
|
|
|
{
|
2015-11-05 14:13:58 -05:00
|
|
|
// Sort blocks to move the blocks first, which are farest away from the piston
|
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
return a.Dot(a_PushDir) > b.Dot(a_PushDir);
|
|
|
|
});
|
|
|
|
|
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))
|
|
|
|
{
|
2015-11-05 14:13:58 -05:00
|
|
|
// Block is breakable, drop it
|
2015-11-05 12:49:15 -05:00
|
|
|
cBlockHandler * Handler = BlockHandler(moveBlock);
|
|
|
|
if (Handler->DoesDropOnUnsuitable())
|
|
|
|
{
|
2017-07-14 22:09:55 -04:00
|
|
|
cChunkInterface ChunkInterface(a_World.GetChunkMap());
|
|
|
|
cBlockInServerPluginInterface PluginInterface(a_World);
|
|
|
|
Handler->DropBlock(ChunkInterface, a_World, PluginInterface, nullptr,
|
2015-11-05 12:49:15 -05:00
|
|
|
moveBlockPos.x, moveBlockPos.y, moveBlockPos.z
|
|
|
|
);
|
|
|
|
}
|
2017-08-25 05:56:01 -04:00
|
|
|
a_World.SetBlock(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, E_BLOCK_AIR, 0);
|
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
|
|
|
|
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
|
|
|
{
|
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
|
2014-05-25 08:46:34 -04:00
|
|
|
|
|
|
|
if (IsExtended(pistonMeta))
|
|
|
|
{
|
|
|
|
// Already extended, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:11:54 -05:00
|
|
|
Vector3i pushDir = MetadataToOffset(pistonMeta);
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2015-11-06 11:11:54 -05:00
|
|
|
Vector3iSet blocksPushed;
|
2015-11-07 11:22:53 -05:00
|
|
|
if (!CanPushBlock(a_BlockPos + pushDir, a_World, true, blocksPushed, pushDir))
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
2015-11-02 16:12:58 -05:00
|
|
|
// Can't push anything, bail out
|
2014-05-25 08:46:34 -04:00
|
|
|
return;
|
|
|
|
}
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2017-09-19 10:12:54 -04:00
|
|
|
a_World.BroadcastBlockAction(a_BlockPos, 0, pistonMeta, pistonBlock);
|
|
|
|
a_World.BroadcastSoundEffect("block.piston.extend", a_BlockPos, 0.5f, 0.7f);
|
2014-05-25 08:46:34 -04:00
|
|
|
|
2015-11-05 12:49:15 -05:00
|
|
|
PushBlocks(blocksPushed, a_World, pushDir);
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2015-11-05 14:13:58 -05:00
|
|
|
// Set the extension and the piston base correctly
|
2015-11-07 11:22:53 -05:00
|
|
|
Vector3i extensionPos = a_BlockPos + pushDir;
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta | 0x8);
|
|
|
|
a_World.SetBlock(
|
2015-11-07 11:22:53 -05:00
|
|
|
extensionPos.x, extensionPos.y, extensionPos.z,
|
2015-11-05 12:49:15 -05:00
|
|
|
E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0)
|
|
|
|
);
|
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
|
|
|
{
|
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta);
|
2014-05-25 08:46:34 -04:00
|
|
|
|
|
|
|
if (!IsExtended(pistonMeta))
|
|
|
|
{
|
|
|
|
// Already retracted, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:11:54 -05:00
|
|
|
Vector3i pushDir = MetadataToOffset(pistonMeta);
|
2015-11-05 08:50:43 -05:00
|
|
|
|
2014-05-25 08:46:34 -04:00
|
|
|
// Check the extension:
|
2015-11-07 11:22:53 -05:00
|
|
|
Vector3i extensionPos = a_BlockPos + pushDir;
|
2017-07-14 22:09:55 -04:00
|
|
|
if (a_World.GetBlock(extensionPos) != E_BLOCK_PISTON_EXTENSION)
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
|
|
|
LOGD("%s: Piston without an extension - still extending, or just in an invalid state?", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-05 05:39:07 -05:00
|
|
|
// Remove Extension
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.SetBlock(extensionPos.x, extensionPos.y, extensionPos.z, E_BLOCK_AIR, 0);
|
2015-11-05 05:39:07 -05:00
|
|
|
|
2017-07-14 22:09:55 -04:00
|
|
|
a_World.SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta & ~(8));
|
2017-09-19 10:12:54 -04:00
|
|
|
a_World.BroadcastBlockAction(a_BlockPos, 1, pistonMeta & ~(8), pistonBlock);
|
|
|
|
a_World.BroadcastSoundEffect("block.piston.contract", a_BlockPos, 0.5f, 0.7f);
|
2014-05-25 08:46:34 -04:00
|
|
|
|
2015-11-05 14:13:58 -05:00
|
|
|
if (!IsSticky(pistonBlock))
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
2015-11-05 05:39:07 -05:00
|
|
|
// No need for block pulling, bail out
|
|
|
|
return;
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2015-11-05 14:13:58 -05:00
|
|
|
// Get the block to pull
|
2015-11-07 11:22:53 -05:00
|
|
|
a_BlockPos += pushDir * 2;
|
2015-11-05 05:39:07 -05:00
|
|
|
// Try to "push" the pulling block in the opposite direction
|
2015-11-05 08:50:43 -05:00
|
|
|
pushDir *= -1;
|
2015-11-05 05:39:07 -05:00
|
|
|
|
2015-11-06 11:11:54 -05:00
|
|
|
Vector3iSet pushedBlocks;
|
2015-11-07 11:22:53 -05:00
|
|
|
if (!CanPushBlock(a_BlockPos, a_World, false, pushedBlocks, pushDir))
|
2015-11-05 05:39:07 -05:00
|
|
|
{
|
|
|
|
// Not pushable, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-05 12:49:15 -05:00
|
|
|
PushBlocks(pushedBlocks, a_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) :
|
|
|
|
super(E_BLOCK_PISTON_EXTENSION)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-07-31 16:17:52 -04:00
|
|
|
void cBlockPistonHeadHandler::OnDestroyedByPlayer(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer & a_Player, int a_BlockX, int a_BlockY, int a_BlockZ)
|
2013-08-09 10:20:12 -04:00
|
|
|
{
|
2015-11-07 11:22:53 -05:00
|
|
|
Vector3i blockPos(a_BlockX, a_BlockY, a_BlockZ);
|
2013-08-09 10:20:12 -04:00
|
|
|
|
2015-11-07 11:22:53 -05:00
|
|
|
// Get the base of the piston
|
2017-11-20 06:13:11 -05:00
|
|
|
NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(blockPos);
|
2015-11-07 11:22:53 -05:00
|
|
|
blockPos -= cBlockPistonHandler::MetadataToOffset(OldMeta);
|
2013-08-09 10:20:12 -04:00
|
|
|
|
2015-11-07 11:22:53 -05:00
|
|
|
BLOCKTYPE Block = a_ChunkInterface.GetBlock(blockPos);
|
2013-08-09 10:20:12 -04:00
|
|
|
if ((Block == E_BLOCK_STICKY_PISTON) || (Block == E_BLOCK_PISTON))
|
|
|
|
{
|
2015-11-07 11:22:53 -05:00
|
|
|
a_ChunkInterface.DigBlock(a_WorldInterface, blockPos.x, blockPos.y, blockPos.z);
|
2017-07-31 16:17:52 -04:00
|
|
|
if (a_Player.IsGameModeCreative())
|
2013-11-23 07:10:33 -05:00
|
|
|
{
|
2014-07-17 16:15:34 -04:00
|
|
|
return; // No pickups if creative
|
2013-11-23 07:10:33 -05:00
|
|
|
}
|
2013-11-22 16:57:06 -05:00
|
|
|
|
|
|
|
cItems Pickups;
|
|
|
|
Pickups.push_back(cItem(Block, 1));
|
2015-11-07 11:22:53 -05:00
|
|
|
a_WorldInterface.SpawnItemPickups(Pickups, blockPos.x + 0.5, blockPos.y + 0.5, blockPos.z + 0.5);
|
2013-08-09 10:20:12 -04:00
|
|
|
}
|
|
|
|
}
|
2015-11-06 11:11:54 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|