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"
|
2013-07-29 07:13:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define AddPistonDir(x, y, z, dir, amount) \
|
2014-05-25 08:46:34 -04:00
|
|
|
switch (dir & 0x07) \
|
2013-07-29 07:13:03 -04:00
|
|
|
{ \
|
|
|
|
case 0: (y) -= (amount); break; \
|
|
|
|
case 1: (y) += (amount); break; \
|
|
|
|
case 2: (z) -= (amount); break; \
|
|
|
|
case 3: (z) += (amount); break; \
|
|
|
|
case 4: (x) -= (amount); break; \
|
|
|
|
case 5: (x) += (amount); break; \
|
2014-05-25 08:46:34 -04:00
|
|
|
default: \
|
|
|
|
{ \
|
|
|
|
LOGWARNING("%s: invalid direction %d, ignoring", __FUNCTION__, dir & 0x07); \
|
|
|
|
break; \
|
|
|
|
} \
|
2013-07-29 07:13:03 -04:00
|
|
|
}
|
|
|
|
|
2014-05-25 08:46:34 -04:00
|
|
|
#define PISTON_TICK_DELAY 1
|
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
|
|
|
|
|
|
|
int newX = a_BlockX;
|
|
|
|
int newY = a_BlockY;
|
|
|
|
int newZ = a_BlockZ;
|
2014-05-25 08:46:34 -04:00
|
|
|
AddPistonDir(newX, newY, newZ, OldMeta, 1);
|
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-02-01 08:06:32 -05:00
|
|
|
a_ChunkInterface.SetBlock(a_WorldInterface, newX, newY, newZ, E_BLOCK_AIR, 0);
|
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-02-04 13:59:05 -05: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
|
|
|
|
2014-06-01 15:00:11 -04:00
|
|
|
int cBlockPistonHandler::FirstPassthroughBlock(int a_PistonX, int a_PistonY, int a_PistonZ, NIBBLETYPE pistonmeta, cWorld * a_World)
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
|
|
|
// Examine each of the 12 blocks ahead of the piston:
|
2014-05-29 06:57:06 -04:00
|
|
|
for (int ret = 0; ret < PISTON_MAX_PUSH_DISTANCE; ret++)
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
|
|
|
BLOCKTYPE currBlock;
|
|
|
|
NIBBLETYPE currMeta;
|
2014-06-01 15:00:11 -04:00
|
|
|
AddPistonDir(a_PistonX, a_PistonY, a_PistonZ, pistonmeta, 1);
|
|
|
|
a_World->GetBlockTypeMeta(a_PistonX, a_PistonY, a_PistonZ, currBlock, currMeta);
|
2014-07-06 18:50:22 -04:00
|
|
|
if (cBlockInfo::IsPistonBreakable(currBlock))
|
2014-05-25 08:46:34 -04:00
|
|
|
{
|
|
|
|
// This block breaks when pushed, extend up to here
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
if (!CanPush(currBlock, currMeta))
|
|
|
|
{
|
|
|
|
// This block cannot be pushed at all, the piston can't extend
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// There is no space for the blocks to move, piston can't extend
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int dist = FirstPassthroughBlock(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, a_World);
|
|
|
|
if (dist < 0)
|
|
|
|
{
|
|
|
|
// FirstPassthroughBlock says piston can't push anything, bail out
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
a_World->BroadcastBlockAction(a_BlockX, a_BlockY, a_BlockZ, 0, pistonMeta, pistonBlock);
|
2014-07-12 20:08:02 -04:00
|
|
|
a_World->BroadcastSoundEffect("tile.piston.out", (double)a_BlockX, (double)a_BlockY, (double)a_BlockZ, 0.5f, 0.7f);
|
2014-05-25 08:46:34 -04:00
|
|
|
|
|
|
|
// Drop the breakable block in the line, if appropriate:
|
|
|
|
AddPistonDir(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, dist + 1); // "a_Block" now at the breakable / empty block
|
|
|
|
BLOCKTYPE currBlock;
|
|
|
|
NIBBLETYPE currMeta;
|
|
|
|
a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, currBlock, currMeta);
|
|
|
|
if (currBlock != E_BLOCK_AIR)
|
|
|
|
{
|
|
|
|
cBlockHandler * Handler = BlockHandler(currBlock);
|
|
|
|
if (Handler->DoesDropOnUnsuitable())
|
|
|
|
{
|
|
|
|
cChunkInterface ChunkInterface(a_World->GetChunkMap());
|
|
|
|
cBlockInServerPluginInterface PluginInterface(*a_World);
|
|
|
|
Handler->DropBlock(ChunkInterface, *a_World, PluginInterface, NULL, a_BlockX, a_BlockY, a_BlockZ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push blocks, from the furthest to the nearest:
|
|
|
|
int oldx = a_BlockX, oldy = a_BlockY, oldz = a_BlockZ;
|
|
|
|
NIBBLETYPE currBlockMeta;
|
2014-05-29 06:57:06 -04:00
|
|
|
std::vector<Vector3i> ScheduledBlocks;
|
|
|
|
ScheduledBlocks.reserve(PISTON_MAX_PUSH_DISTANCE);
|
|
|
|
|
2014-05-25 08:46:34 -04:00
|
|
|
for (int i = dist + 1; i > 1; i--)
|
|
|
|
{
|
|
|
|
AddPistonDir(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, -1);
|
|
|
|
a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, currBlock, currBlockMeta);
|
|
|
|
a_World->SetBlock(oldx, oldy, oldz, currBlock, currBlockMeta, false);
|
2014-05-29 06:57:06 -04:00
|
|
|
ScheduledBlocks.push_back(Vector3i(oldx, oldy, oldz));
|
2014-05-25 08:46:34 -04:00
|
|
|
oldx = a_BlockX;
|
|
|
|
oldy = a_BlockY;
|
|
|
|
oldz = a_BlockZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
int extx = a_BlockX;
|
|
|
|
int exty = a_BlockY;
|
|
|
|
int extz = a_BlockZ;
|
2014-05-29 06:57:06 -04:00
|
|
|
ScheduledBlocks.push_back(Vector3i(extx, exty, extz));
|
2014-05-25 08:46:34 -04:00
|
|
|
AddPistonDir(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, -1);
|
|
|
|
// "a_Block" now at piston body, "ext" at future extension
|
|
|
|
|
|
|
|
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, pistonBlock, pistonMeta | 0x8);
|
|
|
|
a_World->SetBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0), false);
|
2014-05-29 06:57:06 -04:00
|
|
|
a_World->ScheduleTask(PISTON_TICK_DELAY, new cWorld::cTaskSendBlockToAllPlayers(ScheduledBlocks));
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the extension:
|
|
|
|
AddPistonDir(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, 1);
|
|
|
|
if (a_World->GetBlock(a_BlockX, a_BlockY, a_BlockZ) != E_BLOCK_PISTON_EXTENSION)
|
|
|
|
{
|
|
|
|
LOGD("%s: Piston without an extension - still extending, or just in an invalid state?", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AddPistonDir(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, -1);
|
|
|
|
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);
|
2014-07-12 20:08:02 -04:00
|
|
|
a_World->BroadcastSoundEffect("tile.piston.in", (double)a_BlockX, (double)a_BlockY, (double)a_BlockZ, 0.5f, 0.7f);
|
2014-05-25 08:46:34 -04:00
|
|
|
AddPistonDir(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, 1);
|
|
|
|
|
|
|
|
// Retract the extension, pull block if appropriate
|
|
|
|
if (IsSticky(pistonBlock))
|
|
|
|
{
|
|
|
|
int tempx = a_BlockX, tempy = a_BlockY, tempz = a_BlockZ;
|
|
|
|
AddPistonDir(tempx, tempy, tempz, pistonMeta, 1);
|
|
|
|
BLOCKTYPE tempBlock;
|
|
|
|
NIBBLETYPE tempMeta;
|
|
|
|
a_World->GetBlockTypeMeta(tempx, tempy, tempz, tempBlock, tempMeta);
|
|
|
|
if (CanPull(tempBlock, tempMeta))
|
|
|
|
{
|
|
|
|
// Pull the block
|
|
|
|
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, tempBlock, tempMeta, false);
|
|
|
|
a_World->SetBlock(tempx, tempy, tempz, E_BLOCK_AIR, 0, false);
|
2014-05-29 06:57:06 -04:00
|
|
|
|
|
|
|
std::vector<Vector3i> ScheduledBlocks;
|
|
|
|
ScheduledBlocks.push_back(Vector3i(a_BlockX, a_BlockY, a_BlockZ));
|
|
|
|
ScheduledBlocks.push_back(Vector3i(tempx, tempy, tempz));
|
|
|
|
a_World->ScheduleTask(PISTON_TICK_DELAY + 1, new cWorld::cTaskSendBlockToAllPlayers(ScheduledBlocks));
|
2014-05-25 08:46:34 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retract without pulling
|
|
|
|
a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0, false);
|
2014-05-29 06:57:06 -04:00
|
|
|
|
|
|
|
std::vector<Vector3i> ScheduledBlocks;
|
|
|
|
ScheduledBlocks.push_back(Vector3i(a_BlockX, a_BlockY, a_BlockZ));
|
|
|
|
a_World->ScheduleTask(PISTON_TICK_DELAY + 1, new cWorld::cTaskSendBlockToAllPlayers(ScheduledBlocks));
|
2014-05-25 08:46: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
|
|
|
|
|
|
|
int newX = a_BlockX;
|
|
|
|
int newY = a_BlockY;
|
|
|
|
int newZ = a_BlockZ;
|
2014-05-25 08:46:34 -04:00
|
|
|
AddPistonDir(newX, newY, newZ, OldMeta, -1);
|
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())
|
|
|
|
{
|
|
|
|
return; // No pickups if creative
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|