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"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Pickup.h"
|
|
|
|
#include "Item.h"
|
|
|
|
#include "Root.h"
|
|
|
|
#include "ClientHandle.h"
|
|
|
|
#include "World.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
#include "BlockID.h"
|
2012-09-23 18:09:57 -04:00
|
|
|
#include "Server.h"
|
2012-09-29 09:59:32 -04:00
|
|
|
#include "Blocks/BlockHandler.h"
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2013-08-13 12:41:50 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2012-12-16 07:16:04 -05:00
|
|
|
|
|
|
|
|
2013-08-13 12:44:01 -04:00
|
|
|
//Athar from http://www.cplusplus.com/forum/unices/60161/ helped with the sleep code.
|
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
|
2012-06-14 09:06:06 -04:00
|
|
|
extern bool g_BlockPistonBreakable[];
|
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
#define AddDir( x, y, z, dir, amount ) \
|
|
|
|
switch (dir) \
|
|
|
|
{ \
|
|
|
|
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; \
|
|
|
|
}
|
2012-06-14 09:06:06 -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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned short cPiston::FirstPassthroughBlock(int pistonX, int pistonY, int pistonZ, char pistonmeta)
|
|
|
|
{
|
2012-06-14 09:06:06 -04:00
|
|
|
unsigned short ret;
|
|
|
|
pistonmeta &= 7;
|
2012-12-16 07:16:04 -05:00
|
|
|
if (pistonmeta >= 6)
|
|
|
|
{
|
|
|
|
// Just in case, it shouldn't happen but if it would, it'd case inf loop
|
|
|
|
LOGD("cPiston::FirstPassthroughBlock - piston has invalid meta data!\n");
|
2012-06-14 09:06:06 -04:00
|
|
|
return 9001;
|
|
|
|
}
|
2012-12-16 07:16:04 -05:00
|
|
|
BLOCKTYPE currBlock;
|
|
|
|
for (ret = 0; ret < 24; ret++) // push up to 24 blocks
|
|
|
|
{
|
|
|
|
AddDir( pistonX, pistonY, pistonZ, pistonmeta, 1)
|
2012-06-14 09:06:06 -04:00
|
|
|
currBlock = m_World->GetBlock( pistonX, pistonY, pistonZ );
|
2012-12-16 07:16:04 -05:00
|
|
|
if ((currBlock == E_BLOCK_BEDROCK) || (currBlock == E_BLOCK_OBSIDIAN) || (currBlock == E_BLOCK_PISTON_EXTENSION))
|
|
|
|
{
|
|
|
|
return 9001;
|
|
|
|
}
|
|
|
|
if (g_BlockPistonBreakable[currBlock])
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
return 9001;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPiston::ExtendPiston( int pistx, int pisty, int pistz )
|
|
|
|
{
|
2012-12-16 07:16:04 -05:00
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
m_World->GetBlockTypeMeta(pistx, pisty, pistz, pistonBlock, pistonMeta);
|
2012-06-14 09:06:06 -04:00
|
|
|
char isSticky = (char)(pistonBlock == E_BLOCK_STICKY_PISTON) * 8;
|
2012-12-16 07:16:04 -05:00
|
|
|
if ( (pistonMeta & 0x8) != 0x0 )
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-16 07:16:04 -05:00
|
|
|
// Piston already extended, bail out
|
|
|
|
return;
|
|
|
|
}
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
unsigned short dist = FirstPassthroughBlock(pistx, pisty, pistz, pistonMeta);
|
|
|
|
if (dist > 9000) return; // too many blocks
|
|
|
|
|
|
|
|
AddDir(pistx, pisty, pistz, pistonMeta & 7, dist + 1)
|
|
|
|
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-01-26 21:00:33 -05: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
|
|
|
}
|
|
|
|
int oldx = pistx, oldy = pisty, oldz = pistz;
|
|
|
|
NIBBLETYPE currBlockMeta;
|
|
|
|
for (int i = dist + 1; i > 0; i--)
|
|
|
|
{
|
|
|
|
AddDir(pistx, pisty, pistz, pistonMeta & 7, -1)
|
|
|
|
m_World->GetBlockTypeMeta(pistx, pisty, pistz, currBlock, currBlockMeta);
|
|
|
|
m_World->SetBlock( oldx, oldy, oldz, currBlock, currBlockMeta);
|
|
|
|
oldx = pistx;
|
|
|
|
oldy = pisty;
|
|
|
|
oldz = pistz;
|
|
|
|
}
|
|
|
|
m_World->BroadcastBlockAction(pistx, pisty, pistz, 0, pistonMeta, E_BLOCK_PISTON);
|
|
|
|
m_World->BroadcastSoundEffect("tile.piston.out", pistx * 8, pisty * 8, pistz * 8, 0.5f, 0.7f);
|
|
|
|
m_World->FastSetBlock( pistx, pisty, pistz, pistonBlock, pistonMeta | 0x8 );
|
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;
|
2012-06-14 09:06:06 -04:00
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
AddDir(extx, exty, extz, pistonMeta & 7, 1)
|
|
|
|
|
2013-08-13 12:41:50 -04:00
|
|
|
#ifdef __WIN32__
|
|
|
|
Sleep(100);
|
2013-08-13 11:34:26 -04:00
|
|
|
#else
|
2013-08-13 12:44:01 -04:00
|
|
|
usleep(static_cast<useconds_t>(100)*1000);
|
2013-08-13 11:34:26 -04:00
|
|
|
#endif
|
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
m_World->SetBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, isSticky + pistonMeta & 7);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cPiston::RetractPiston( int pistx, int pisty, int pistz )
|
|
|
|
{
|
2012-12-16 07:16:04 -05:00
|
|
|
BLOCKTYPE pistonBlock;
|
|
|
|
NIBBLETYPE pistonMeta;
|
|
|
|
m_World->GetBlockTypeMeta(pistx, pisty, pistz, pistonBlock, pistonMeta);
|
|
|
|
if (pistonMeta <= 6)
|
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;
|
|
|
|
}
|
2012-08-31 17:59:57 -04:00
|
|
|
m_World->BroadcastBlockAction(pistx, pisty, pistz, 1, pistonMeta & ~(8), E_BLOCK_PISTON);
|
2012-12-15 08:27:01 -05:00
|
|
|
m_World->BroadcastSoundEffect("tile.piston.in", pistx * 8, pisty * 8, pistz * 8, 0.5f, 0.7f);
|
2012-08-19 15:42:32 -04:00
|
|
|
m_World->FastSetBlock(pistx, pisty, pistz, pistonBlock, pistonMeta & ~(8));
|
2013-08-13 11:34:26 -04:00
|
|
|
|
2012-08-19 15:42:32 -04:00
|
|
|
AddDir(pistx, pisty, pistz, pistonMeta & 7, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pistonBlock == E_BLOCK_STICKY_PISTON)
|
|
|
|
{
|
|
|
|
int tempx = pistx, tempy = pisty, tempz = pistz;
|
|
|
|
AddDir( tempx, tempy, tempz, pistonMeta & 7, 1 )
|
|
|
|
BLOCKTYPE tempblock;
|
|
|
|
NIBBLETYPE tempmeta;
|
|
|
|
m_World->GetBlockTypeMeta(tempx, tempy, tempz, tempblock, tempmeta);
|
|
|
|
if (
|
|
|
|
(tempblock == E_BLOCK_OBSIDIAN) ||
|
|
|
|
(tempblock == E_BLOCK_BEDROCK) ||
|
|
|
|
(tempblock == E_BLOCK_PISTON_EXTENSION)
|
|
|
|
)
|
2012-06-14 09:06:06 -04:00
|
|
|
{
|
2012-12-16 07:16:04 -05:00
|
|
|
// These cannot be moved by the sticky piston, bail out
|
|
|
|
return;
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
2013-08-13 12:41:50 -04:00
|
|
|
#ifdef __WIN32__
|
|
|
|
Sleep(100);
|
2013-08-13 11:34:26 -04:00
|
|
|
#else
|
2013-08-13 12:44:01 -04:00
|
|
|
usleep(static_cast<useconds_t>(100)*1000);
|
2013-08-13 11:34:26 -04:00
|
|
|
#endif
|
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
m_World->SetBlock(pistx, pisty, pistz, tempblock, tempmeta);
|
|
|
|
m_World->SetBlock(tempx, tempy, tempz, E_BLOCK_AIR, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-08-13 12:41:50 -04:00
|
|
|
#ifdef __WIN32__
|
|
|
|
Sleep(100);
|
2013-08-13 11:34:26 -04:00
|
|
|
#else
|
2013-08-13 12:44:01 -04:00
|
|
|
usleep(static_cast<useconds_t>(100)*1000);
|
2013-08-13 11:34:26 -04:00
|
|
|
#endif
|
|
|
|
|
2012-12-16 07:16:04 -05:00
|
|
|
m_World->SetBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0);
|
2012-06-14 09:06:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|