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
|
|
|
|
2015-11-04 15:45:51 -05:00
|
|
|
#include <vector>
|
2015-11-05 04:25:59 -05:00
|
|
|
#include <array>
|
2015-11-04 15:45:51 -05:00
|
|
|
|
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
|
|
|
{
|
2014-02-01 08:06:32 -05:00
|
|
|
NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2015-11-05 08:50:43 -05:00
|
|
|
const Vector3i pushDir = GetDirectionVec(OldMeta);
|
|
|
|
int newX = a_BlockX + pushDir.x;
|
|
|
|
int newY = a_BlockY + pushDir.y;
|
|
|
|
int newZ = a_BlockZ + pushDir.z;
|
2013-07-29 07:13:03 -04:00
|
|
|
|
2014-02-01 08:06:32 -05:00
|
|
|
if (a_ChunkInterface.GetBlock(newX, newY, newZ) == E_BLOCK_PISTON_EXTENSION)
|
2013-07-29 07:13:03 -04:00
|
|
|
{
|
2014-09-12 13:07:20 -04:00
|
|
|
a_ChunkInterface.SetBlock(newX, newY, newZ, 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(
|
2014-02-01 08:06:32 -05: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;
|
2014-05-25 08:46:34 -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-05 08:50:43 -05:00
|
|
|
Vector3i cBlockPistonHandler::GetDirectionVec(int a_PistonMeta)
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
return Vector3i();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-05 12:49:15 -05:00
|
|
|
void cBlockPistonHandler::PushBlocks(const std::unordered_set<Vector3i, VectorHasher<int>> & a_BlocksToPush,
|
|
|
|
cWorld * a_World, const Vector3i & a_PushDir
|
|
|
|
)
|
|
|
|
{
|
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;
|
|
|
|
for (Vector3i & moveBlockPos : sortedBlocks)
|
2015-11-05 14:13:58 -05:00
|
|
|
{
|
2015-11-05 12:49:15 -05:00
|
|
|
a_World->GetBlockTypeMeta(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, moveBlock, moveMeta);
|
|
|
|
a_World->SetBlock(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, E_BLOCK_AIR, 0);
|
|
|
|
|
|
|
|
moveBlockPos += a_PushDir;
|
|
|
|
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())
|
|
|
|
{
|
|
|
|
cChunkInterface ChunkInterface(a_World->GetChunkMap());
|
|
|
|
cBlockInServerPluginInterface PluginInterface(*a_World);
|
|
|
|
Handler->DropBlock(ChunkInterface, *a_World, PluginInterface, nullptr,
|
|
|
|
moveBlockPos.x, moveBlockPos.y, moveBlockPos.z
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
2015-11-05 14:13:58 -05:00
|
|
|
// Not breakable, just move it
|
2015-11-05 12:49:15 -05:00
|
|
|
a_World->SetBlock(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, moveBlock, moveMeta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-11-02 16:12:58 -05:00
|
|
|
bool cBlockPistonHandler::CanPushBlock(
|
|
|
|
int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World, bool a_RequirePushable,
|
2015-11-05 08:50:43 -05:00
|
|
|
std::unordered_set<Vector3i, VectorHasher<int>> & a_BlocksPushed, const Vector3i & a_PushDir
|
2015-11-02 16:12:58 -05:00
|
|
|
)
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
2015-11-05 04:25:59 -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-02 16:12:58 -05:00
|
|
|
BLOCKTYPE currBlock;
|
|
|
|
NIBBLETYPE currMeta;
|
|
|
|
a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, 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-02 16:12:58 -05:00
|
|
|
if (!a_BlocksPushed.emplace(a_BlockX, a_BlockY, a_BlockZ).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-05 14:13:58 -05:00
|
|
|
for (const Vector3i & testDir : pushingDirs)
|
2015-11-05 04:25:59 -05:00
|
|
|
{
|
2015-11-05 14:13:58 -05:00
|
|
|
if (!CanPushBlock(a_BlockX + testDir.x, a_BlockY + testDir.y, a_BlockZ + testDir.z, 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-05 08:50:43 -05:00
|
|
|
return CanPushBlock(a_BlockX + a_PushDir.x, a_BlockY + a_PushDir.y, a_BlockZ + a_PushDir.z, a_World, true, a_BlocksPushed, a_PushDir);
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cBlockPistonHandler::ExtendPiston(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World)
|
|
|
|
{
|
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, pistonBlock, pistonMeta);
|
|
|
|
|
|
|
|
if (IsExtended(pistonMeta))
|
|
|
|
{
|
|
|
|
// Already extended, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-05 08:50:43 -05:00
|
|
|
Vector3i pushDir = GetDirectionVec(pistonMeta);
|
2015-11-05 04:25:59 -05:00
|
|
|
|
2015-11-02 16:12:58 -05:00
|
|
|
std::unordered_set<Vector3i, VectorHasher<int>> blocksPushed;
|
2015-11-05 12:49:15 -05:00
|
|
|
if (!CanPushBlock(a_BlockX + pushDir.x, a_BlockY + pushDir.y, a_BlockZ + pushDir.z,
|
|
|
|
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
|
|
|
|
2014-05-25 08:46:34 -04:00
|
|
|
a_World->BroadcastBlockAction(a_BlockX, a_BlockY, a_BlockZ, 0, pistonMeta, pistonBlock);
|
2015-07-29 11:04:03 -04:00
|
|
|
a_World->BroadcastSoundEffect("tile.piston.out", static_cast<double>(a_BlockX), static_cast<double>(a_BlockY), static_cast<double>(a_BlockZ), 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
|
2014-05-25 08:46:34 -04:00
|
|
|
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, pistonBlock, pistonMeta | 0x8);
|
2015-11-05 12:49:15 -05:00
|
|
|
a_World->SetBlock(a_BlockX + pushDir.x, a_BlockY + pushDir.y, a_BlockZ + pushDir.z,
|
|
|
|
E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0)
|
|
|
|
);
|
2014-05-25 08:46:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cBlockPistonHandler::RetractPiston(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World)
|
|
|
|
{
|
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, pistonBlock, pistonMeta);
|
|
|
|
|
|
|
|
if (!IsExtended(pistonMeta))
|
|
|
|
{
|
|
|
|
// Already retracted, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-05 08:50:43 -05:00
|
|
|
Vector3i pushDir = GetDirectionVec(pistonMeta);
|
|
|
|
|
2014-05-25 08:46:34 -04:00
|
|
|
// Check the extension:
|
2015-11-05 08:50:43 -05:00
|
|
|
if (a_World->GetBlock(a_BlockX + pushDir.x, a_BlockY + pushDir.y, a_BlockZ + pushDir.z) != 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
|
2015-11-05 08:50:43 -05:00
|
|
|
a_World->SetBlock(a_BlockX + pushDir.x, a_BlockY + pushDir.y, a_BlockZ + pushDir.z, E_BLOCK_AIR, 0);
|
2015-11-05 05:39:07 -05:00
|
|
|
|
2014-05-25 08:46:34 -04:00
|
|
|
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, pistonBlock, pistonMeta & ~(8));
|
|
|
|
a_World->BroadcastBlockAction(a_BlockX, a_BlockY, a_BlockZ, 1, pistonMeta & ~(8), pistonBlock);
|
2015-07-29 11:04:03 -04:00
|
|
|
a_World->BroadcastSoundEffect("tile.piston.in", static_cast<double>(a_BlockX), static_cast<double>(a_BlockY), static_cast<double>(a_BlockZ), 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-05 08:50:43 -05:00
|
|
|
a_BlockX += 2 * pushDir.x;
|
|
|
|
a_BlockY += 2 * pushDir.y;
|
|
|
|
a_BlockZ += 2 * pushDir.z;
|
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
|
|
|
|
|
|
|
std::unordered_set<Vector3i, VectorHasher<int>> pushedBlocks;
|
2015-11-05 08:50:43 -05:00
|
|
|
if (!CanPushBlock(a_BlockX, a_BlockY, a_BlockZ, 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)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-02-01 09:01:13 -05: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
|
|
|
{
|
2014-02-01 09:01:13 -05:00
|
|
|
NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ);
|
2013-08-09 10:20:12 -04:00
|
|
|
|
2015-11-05 08:50:43 -05:00
|
|
|
Vector3i pushDir = cBlockPistonHandler::GetDirectionVec(OldMeta);
|
|
|
|
int newX = a_BlockX - pushDir.x;
|
|
|
|
int newY = a_BlockY - pushDir.y;
|
|
|
|
int newZ = a_BlockZ - pushDir.z;
|
2013-08-09 10:20:12 -04:00
|
|
|
|
2014-02-01 09:01:13 -05:00
|
|
|
BLOCKTYPE Block = a_ChunkInterface.GetBlock(newX, newY, newZ);
|
2013-08-09 10:20:12 -04:00
|
|
|
if ((Block == E_BLOCK_STICKY_PISTON) || (Block == E_BLOCK_PISTON))
|
|
|
|
{
|
2014-02-01 09:01:13 -05:00
|
|
|
a_ChunkInterface.DigBlock(a_WorldInterface, newX, newY, newZ);
|
2013-11-23 07:10:33 -05:00
|
|
|
if (a_Player->IsGameModeCreative())
|
|
|
|
{
|
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));
|
2014-02-01 09:01:13 -05:00
|
|
|
a_WorldInterface.SpawnItemPickups(Pickups, a_BlockX + 0.5, a_BlockY + 0.5, a_BlockZ + 0.5);
|
2013-08-09 10:20:12 -04:00
|
|
|
}
|
|
|
|
}
|