2012-06-14 09:06:06 -04:00
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Piston.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "ChunkDef.h"
|
2013-08-19 05:39:13 -04:00
|
|
|
#include "Entities/Pickup.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Item.h"
|
|
|
|
#include "Root.h"
|
|
|
|
#include "ClientHandle.h"
|
|
|
|
#include "World.h"
|
|
|
|
#include "Server.h"
|
2012-09-29 09:59:32 -04:00
|
|
|
#include "Blocks/BlockHandler.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
|
|
|
|
|
2013-08-13 12:44:01 -04:00
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
|
2013-08-19 17:43:08 -04:00
|
|
|
/// Number of ticks that the piston extending / retracting waits before setting the block
|
2013-11-18 17:30:34 -05:00
|
|
|
const int PISTON_TICK_DELAY = 6;
|
2013-08-19 17:43:08 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
cPiston::cPiston(cWorld * a_World)
|
|
|
|
: m_World(a_World)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-18 08:28:52 -04:00
|
|
|
int cPiston::FirstPassthroughBlock(int pistonX, int pistonY, int pistonZ, NIBBLETYPE pistonmeta)
|
2012-12-16 07:16:04 -05:00
|
|
|
{
|
2013-08-18 08:28:52 -04:00
|
|
|
// Examine each of the 12 blocks ahead of the piston:
|
|
|
|
for (int ret = 0; ret < 12; ret++)
|
2012-12-16 07:16:04 -05:00
|
|
|
{
|
2013-08-18 08:28:52 -04:00
|
|
|
BLOCKTYPE currBlock;
|
|
|
|
NIBBLETYPE currMeta;
|
|
|
|
AddDir(pistonX, pistonY, pistonZ, pistonmeta, 1);
|
2013-08-16 06:23:24 -04:00
|
|
|
m_World->GetBlockTypeMeta(pistonX, pistonY, pistonZ, currBlock, currMeta);
|
2013-08-18 08:28:52 -04:00
|
|
|
if (CanBreakPush(currBlock, currMeta))
|
2012-12-16 07:16:04 -05:00
|
|
|
{
|
2013-08-18 08:28:52 -04:00
|
|
|
// This block breaks when pushed, extend up to here
|
|
|
|
return ret;
|
2012-12-16 07:16:04 -05:00
|
|
|
}
|
2013-08-18 08:28:52 -04:00
|
|
|
if (!CanPush(currBlock, currMeta))
|
2012-12-16 07:16:04 -05:00
|
|
|
{
|
2013-08-18 08:28:52 -04:00
|
|
|
// This block cannot be pushed at all, the piston can't extend
|
|
|
|
return -1;
|
2012-12-16 07:16:04 -05:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-08-18 08:28:52 -04:00
|
|
|
// There is no space for the blocks to move, piston can't extend
|
|
|
|
return -1;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-18 08:28:52 -04:00
|
|
|
void cPiston::ExtendPiston(int pistx, int pisty, int pistz)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-16 07:16:04 -05:00
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
m_World->GetBlockTypeMeta(pistx, pisty, pistz, pistonBlock, pistonMeta);
|
2013-08-16 06:23:24 -04:00
|
|
|
|
2013-08-18 08:28:52 -04:00
|
|
|
if (IsExtended(pistonMeta))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-18 08:28:52 -04:00
|
|
|
// Already extended, bail out
|
2012-12-16 07:16:04 -05:00
|
|
|
return;
|
|
|
|
}
|
2013-08-20 15:17:35 -04:00
|
|
|
|
2013-08-18 08:28:52 -04:00
|
|
|
int dist = FirstPassthroughBlock(pistx, pisty, pistz, pistonMeta);
|
|
|
|
if (dist < 0)
|
|
|
|
{
|
|
|
|
// FirstPassthroughBlock says piston can't push anything, bail out
|
|
|
|
return;
|
|
|
|
}
|
2012-12-16 07:16:04 -05:00
|
|
|
|
2013-11-19 15:40:07 -05:00
|
|
|
m_World->BroadcastBlockAction(pistx, pisty, pistz, 0, pistonMeta, pistonBlock);
|
|
|
|
m_World->BroadcastSoundEffect("tile.piston.out", pistx * 8, pisty * 8, pistz * 8, 0.5f, 0.7f);
|
|
|
|
|
2013-08-18 08:28:52 -04:00
|
|
|
// Drop the breakable block in the line, if appropriate:
|
|
|
|
AddDir(pistx, pisty, pistz, pistonMeta, dist + 1); // "pist" now at the breakable / empty block
|
2012-12-16 07:16:04 -05:00
|
|
|
BLOCKTYPE currBlock;
|
|
|
|
NIBBLETYPE currMeta;
|
|
|
|
m_World->GetBlockTypeMeta(pistx, pisty, pistz, currBlock, currMeta);
|
|
|
|
if (currBlock != E_BLOCK_AIR)
|
|
|
|
{
|
|
|
|
cBlockHandler * Handler = BlockHandler(currBlock);
|
|
|
|
if (Handler->DoesDropOnUnsuitable())
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-18 13:56:04 -04:00
|
|
|
Handler->DropBlock(m_World, NULL, pistx, pisty, pistz);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-12-16 07:16:04 -05:00
|
|
|
}
|
2013-08-18 08:28:52 -04:00
|
|
|
|
|
|
|
// Push blocks, from the furthest to the nearest:
|
|
|
|
int oldx = pistx, oldy = pisty, oldz = pistz;
|
2012-12-16 07:16:04 -05:00
|
|
|
NIBBLETYPE currBlockMeta;
|
2013-08-18 08:28:52 -04:00
|
|
|
for (int i = dist + 1; i > 1; i--)
|
2012-12-16 07:16:04 -05:00
|
|
|
{
|
2013-08-18 08:28:52 -04:00
|
|
|
AddDir(pistx, pisty, pistz, pistonMeta, -1);
|
|
|
|
m_World->GetBlockTypeMeta(pistx, pisty, pistz, currBlock, currBlockMeta);
|
2013-08-19 17:43:08 -04:00
|
|
|
m_World->QueueSetBlock( oldx, oldy, oldz, currBlock, currBlockMeta, PISTON_TICK_DELAY);
|
2013-08-18 08:28:52 -04:00
|
|
|
oldx = pistx;
|
|
|
|
oldy = pisty;
|
|
|
|
oldz = pistz;
|
2012-12-16 07:16:04 -05:00
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
int extx = pistx;
|
|
|
|
int exty = pisty;
|
|
|
|
int extz = pistz;
|
2013-08-18 08:28:52 -04:00
|
|
|
AddDir(pistx, pisty, pistz, pistonMeta, -1);
|
|
|
|
// "pist" now at piston body, "ext" at future extension
|
|
|
|
|
2013-11-18 17:30:34 -05:00
|
|
|
m_World->SetBlock( pistx, pisty, pistz, pistonBlock, pistonMeta | 0x8);
|
2013-08-19 17:43:08 -04:00
|
|
|
m_World->QueueSetBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0), PISTON_TICK_DELAY);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-19 17:43:08 -04:00
|
|
|
void cPiston::RetractPiston(int pistx, int pisty, int pistz)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-16 07:16:04 -05:00
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
m_World->GetBlockTypeMeta(pistx, pisty, pistz, pistonBlock, pistonMeta);
|
2013-08-18 08:28:52 -04:00
|
|
|
if (!IsExtended(pistonMeta))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-16 07:16:04 -05:00
|
|
|
// Already retracted, bail out
|
2012-06-14 09:06:06 -04:00
|
|
|
return;
|
|
|
|
}
|
2013-08-18 08:28:52 -04:00
|
|
|
|
2013-08-18 13:56:04 -04:00
|
|
|
m_World->BroadcastBlockAction(pistx, pisty, pistz, 1, pistonMeta & ~(8), pistonBlock);
|
2013-08-20 15:17:35 -04:00
|
|
|
m_World->BroadcastSoundEffect("tile.piston.in", pistx * 8, pisty * 8, pistz * 8, 0.5f, 0.7f);
|
2013-11-18 17:30:34 -05:00
|
|
|
m_World->SetBlock(pistx, pisty, pistz, pistonBlock, pistonMeta & ~(8));
|
2013-08-13 11:34:26 -04:00
|
|
|
|
2013-08-18 08:28:52 -04:00
|
|
|
// Check the extension:
|
|
|
|
AddDir(pistx, pisty, pistz, pistonMeta, 1);
|
2012-12-16 07:16:04 -05:00
|
|
|
if (m_World->GetBlock(pistx, pisty, pistz) != E_BLOCK_PISTON_EXTENSION)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-16 07:16:04 -05:00
|
|
|
LOGD("%s: Piston without an extension?", __FUNCTION__);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-18 08:28:52 -04:00
|
|
|
// Retract the extension, pull block if appropriate
|
|
|
|
if (IsSticky(pistonBlock))
|
2012-12-16 07:16:04 -05:00
|
|
|
{
|
|
|
|
int tempx = pistx, tempy = pisty, tempz = pistz;
|
2013-08-18 08:28:52 -04:00
|
|
|
AddDir( tempx, tempy, tempz, pistonMeta, 1);
|
|
|
|
BLOCKTYPE tempBlock;
|
|
|
|
NIBBLETYPE tempMeta;
|
|
|
|
m_World->GetBlockTypeMeta(tempx, tempy, tempz, tempBlock, tempMeta);
|
|
|
|
if (CanPull(tempBlock, tempMeta))
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2013-08-18 13:56:04 -04:00
|
|
|
// Pull the block
|
2013-08-19 17:43:08 -04:00
|
|
|
m_World->QueueSetBlock(pistx, pisty, pistz, tempBlock, tempMeta, PISTON_TICK_DELAY);
|
|
|
|
m_World->QueueSetBlock(tempx, tempy, tempz, E_BLOCK_AIR, 0, PISTON_TICK_DELAY);
|
2013-08-18 08:28:52 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-18 13:56:04 -04:00
|
|
|
// Retract without pulling
|
2013-08-19 17:43:08 -04:00
|
|
|
m_World->QueueSetBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0, PISTON_TICK_DELAY);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2012-12-16 07:16:04 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-19 17:43:08 -04:00
|
|
|
m_World->QueueSetBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0, PISTON_TICK_DELAY);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-08-18 08:28:52 -04:00
|
|
|
|
|
|
|
bool cPiston::IsExtended(NIBBLETYPE a_PistonMeta)
|
|
|
|
{
|
|
|
|
return ((a_PistonMeta & 0x8) != 0x0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cPiston::IsSticky(BLOCKTYPE a_BlockType)
|
|
|
|
{
|
|
|
|
return (a_BlockType == E_BLOCK_STICKY_PISTON);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cPiston::CanPush(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
|
|
|
{
|
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_ANVIL:
|
|
|
|
case E_BLOCK_BED:
|
|
|
|
case E_BLOCK_BEDROCK:
|
|
|
|
case E_BLOCK_BREWING_STAND:
|
|
|
|
case E_BLOCK_CHEST:
|
|
|
|
case E_BLOCK_COMMAND_BLOCK:
|
|
|
|
case E_BLOCK_DISPENSER:
|
|
|
|
case E_BLOCK_DROPPER:
|
|
|
|
case E_BLOCK_ENCHANTMENT_TABLE:
|
|
|
|
case E_BLOCK_END_PORTAL:
|
|
|
|
case E_BLOCK_END_PORTAL_FRAME:
|
|
|
|
case E_BLOCK_FURNACE:
|
2013-08-18 10:58:27 -04:00
|
|
|
case E_BLOCK_LIT_FURNACE:
|
2013-08-18 08:28:52 -04:00
|
|
|
case E_BLOCK_HOPPER:
|
|
|
|
case E_BLOCK_JUKEBOX:
|
|
|
|
case E_BLOCK_MOB_SPAWNER:
|
|
|
|
case E_BLOCK_NETHER_PORTAL:
|
|
|
|
case E_BLOCK_NOTE_BLOCK:
|
|
|
|
case E_BLOCK_OBSIDIAN:
|
|
|
|
case E_BLOCK_PISTON_EXTENSION:
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-19 17:43:08 -04:00
|
|
|
case E_BLOCK_STICKY_PISTON:
|
2013-08-18 08:28:52 -04:00
|
|
|
case E_BLOCK_PISTON:
|
|
|
|
{
|
|
|
|
// A piston can only be pushed if retracted:
|
|
|
|
return !IsExtended(a_BlockMeta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cPiston::CanBreakPush(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
|
|
|
{
|
|
|
|
return g_BlockPistonBreakable[a_BlockType];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool cPiston::CanPull(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta)
|
|
|
|
{
|
|
|
|
switch (a_BlockType)
|
|
|
|
{
|
|
|
|
case E_BLOCK_LAVA:
|
|
|
|
case E_BLOCK_STATIONARY_LAVA:
|
|
|
|
case E_BLOCK_STATIONARY_WATER:
|
|
|
|
case E_BLOCK_WATER:
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-09-15 11:40:09 -04:00
|
|
|
|
|
|
|
if (CanBreakPush(a_BlockType, a_BlockMeta))
|
|
|
|
{
|
|
|
|
return false; // CanBreakPush returns true, but we need false to prevent pulling
|
|
|
|
}
|
|
|
|
|
|
|
|
return CanPush(a_BlockType, a_BlockMeta);
|
2013-08-18 08:28:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPiston::AddDir(int & a_BlockX, int & a_BlockY, int & a_BlockZ, NIBBLETYPE a_PistonMeta, int a_Amount)
|
|
|
|
{
|
|
|
|
switch (a_PistonMeta & 0x07)
|
|
|
|
{
|
|
|
|
case 0: a_BlockY -= a_Amount; break;
|
|
|
|
case 1: a_BlockY += a_Amount; break;
|
|
|
|
case 2: a_BlockZ -= a_Amount; break;
|
|
|
|
case 3: a_BlockZ += a_Amount; break;
|
|
|
|
case 4: a_BlockX -= a_Amount; break;
|
|
|
|
case 5: a_BlockX += a_Amount; break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
LOGWARNING("%s: invalid direction %d, ignoring", __FUNCTION__, a_PistonMeta & 0x07);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|