2011-11-05 14:28:19 -04:00
|
|
|
#include "cPiston.h"
|
2011-11-07 17:59:29 -05:00
|
|
|
#include "cRedstone.h"
|
2011-11-07 13:19:38 -05:00
|
|
|
#include "cChunk.h"
|
|
|
|
#include "cPickup.h"
|
|
|
|
#include "cBlockToPickup.h"
|
|
|
|
#include "cItem.h"
|
2011-11-05 14:28:19 -04:00
|
|
|
#include "cRoot.h"
|
2011-11-07 14:15:27 -05:00
|
|
|
#include "cClientHandle.h"
|
2011-11-05 14:28:19 -04:00
|
|
|
#include "cWorld.h"
|
|
|
|
#include "BlockID.h"
|
2011-11-07 00:49:18 -05:00
|
|
|
#include "packets/cPacket_BlockAction.h"
|
|
|
|
#include "cServer.h"
|
2011-11-05 14:28:19 -04:00
|
|
|
|
2011-11-07 13:19:38 -05:00
|
|
|
extern bool g_BlockPistonBreakable[];
|
|
|
|
|
|
|
|
#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; }
|
2011-11-08 20:31:19 -05:00
|
|
|
|
2011-11-07 14:15:27 -05:00
|
|
|
#define FAST_FLOOR( x ) ( (x) < 0 ? ((int)x)-1 : ((int)x) )
|
2011-11-07 13:19:38 -05:00
|
|
|
|
2011-11-05 14:28:19 -04:00
|
|
|
cPiston::cPiston( cWorld* a_World )
|
|
|
|
:m_World ( a_World )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2011-11-05 22:36:05 -04:00
|
|
|
|
2011-11-07 13:19:38 -05:00
|
|
|
unsigned short cPiston::FirstPassthroughBlock( int pistonX, int pistonY, int pistonZ, char pistonmeta ) {
|
|
|
|
unsigned short ret;
|
|
|
|
pistonmeta &= 7;
|
|
|
|
if(pistonmeta >= 6) { // just in case, it shouldn't happen but if it would, it'd case inf loop
|
|
|
|
printf("cPiston::FirstPassthroughBlock - piston has invalid meta data!\n");
|
|
|
|
return 9001;
|
2011-11-05 22:36:05 -04:00
|
|
|
}
|
2011-11-07 13:19:38 -05:00
|
|
|
char currBlock;
|
|
|
|
for( ret = 0; ret < 24; ret++ ) { // push up to 24 blocks
|
|
|
|
AddDir( pistonX, pistonY, pistonZ, pistonmeta, 1 )
|
|
|
|
currBlock = m_World->GetBlock( pistonX, pistonY, pistonZ );
|
|
|
|
if(currBlock == E_BLOCK_BEDROCK || currBlock == E_BLOCK_OBSIDIAN || currBlock == E_BLOCK_PISTON_EXTENSION ) {return 9001;}
|
|
|
|
if(g_BlockPistonBreakable[currBlock]) {return ret;}
|
2011-11-06 04:23:20 -05:00
|
|
|
}
|
2011-11-07 13:19:38 -05:00
|
|
|
return 9001;
|
2011-11-05 22:36:05 -04:00
|
|
|
}
|
|
|
|
|
2011-11-07 13:19:38 -05:00
|
|
|
void cPiston::ExtendPiston( int pistx, int pisty, int pistz ) {
|
|
|
|
char pistonBlock = m_World->GetBlock( pistx, pisty, pistz );
|
|
|
|
char pistonMeta = m_World->GetBlockMeta( pistx, pisty, pistz );
|
|
|
|
char isSticky = (char)(pistonBlock == E_BLOCK_STICKY_PISTON) * 8;
|
2011-11-07 17:59:29 -05:00
|
|
|
bool recalc = false;
|
|
|
|
if (pistonMeta < 6) {// only extend if piston is not already extended
|
|
|
|
unsigned short dist = FirstPassthroughBlock(pistx, pisty, pistz, pistonMeta);
|
|
|
|
if(dist>9000) return; // too many blocks
|
|
|
|
|
|
|
|
AddDir( pistx, pisty, pistz, pistonMeta & 7, dist+1 )
|
|
|
|
char currBlock = m_World->GetBlock( pistx, pisty, pistz );
|
|
|
|
if( currBlock != E_BLOCK_AIR ) {
|
|
|
|
cItem PickupItem;
|
|
|
|
PickupItem.m_ItemID = cBlockToPickup::ToPickup( (ENUM_BLOCK_ID) currBlock, E_ITEM_EMPTY );
|
|
|
|
PickupItem.m_ItemCount = 1;
|
|
|
|
cPickup* Pickup = new cPickup( pistx*32 + 16, pisty*32 + 16, pistz*32 + 16, PickupItem );
|
|
|
|
Pickup->Initialize( m_World );
|
|
|
|
recalc = true;
|
|
|
|
}
|
|
|
|
int oldx = pistx, oldy = pisty, oldz = pistz;
|
|
|
|
char currBlockMeta;
|
|
|
|
for( int i = dist+1; i>0; i-- ) {
|
|
|
|
AddDir( pistx, pisty, pistz, pistonMeta & 7, -1 )
|
|
|
|
currBlock = m_World->GetBlock( pistx, pisty, pistz );
|
|
|
|
currBlockMeta = m_World->GetBlockMeta( pistx, pisty, pistz );
|
|
|
|
m_World->SetBlock( oldx, oldy, oldz, currBlock, currBlockMeta );
|
|
|
|
oldx = pistx;
|
|
|
|
oldy = pisty;
|
|
|
|
oldz = pistz;
|
|
|
|
}
|
|
|
|
cPacket_BlockAction Action;
|
|
|
|
Action.m_PosX = (int)pistx;
|
|
|
|
Action.m_PosY = (short)pisty;
|
|
|
|
Action.m_PosZ = (int)pistz;
|
|
|
|
Action.m_Byte1 = 0;
|
|
|
|
Action.m_Byte2 = pistonMeta;
|
2011-11-08 20:31:19 -05:00
|
|
|
|
|
|
|
|
2011-11-07 17:59:29 -05:00
|
|
|
cChunk* Chunk = m_World->GetChunk( FAST_FLOOR(pistx/16), FAST_FLOOR(pisty/16), FAST_FLOOR(pistz/16) );
|
|
|
|
Chunk->Broadcast( Action );
|
|
|
|
m_World->FastSetBlock( pistx, pisty, pistz, pistonBlock, pistonMeta | 8 );
|
2011-11-07 13:19:38 -05:00
|
|
|
|
2011-11-07 17:59:29 -05:00
|
|
|
int extx = pistx;
|
|
|
|
int exty = pisty;
|
|
|
|
int extz = pistz;
|
2011-11-07 13:19:38 -05:00
|
|
|
|
2011-11-07 17:59:29 -05:00
|
|
|
AddDir( extx, exty, extz, pistonMeta&7, 1 )
|
2011-11-07 13:19:38 -05:00
|
|
|
|
2011-11-07 17:59:29 -05:00
|
|
|
m_World->SetBlock( extx, exty, extz, E_BLOCK_PISTON_EXTENSION, isSticky+pistonMeta&7 );
|
|
|
|
|
|
|
|
if (recalc) {
|
|
|
|
cRedstone Redstone(m_World);
|
|
|
|
Redstone.ChangeRedstone( extx, exty, extz, false ); //recalculate redstone around current device.
|
|
|
|
Redstone.ChangeRedstone( pistx, pisty, pistz, false ); //recalculate redstone around current device.
|
|
|
|
}
|
2011-11-07 13:19:38 -05:00
|
|
|
|
2011-11-07 17:59:29 -05:00
|
|
|
}
|
2011-11-05 22:36:05 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-07 13:19:38 -05:00
|
|
|
void cPiston::RetractPiston( int pistx, int pisty, int pistz ) {
|
|
|
|
char pistonBlock = m_World->GetBlock( pistx, pisty, pistz );
|
|
|
|
char pistonMeta = m_World->GetBlockMeta( pistx, pisty, pistz );
|
2011-11-07 17:59:29 -05:00
|
|
|
if (pistonMeta > 6) {// only retract if piston is not already retracted
|
|
|
|
//send blockaction packet
|
|
|
|
cPacket_BlockAction Action;
|
|
|
|
Action.m_PosX = (int)pistx;
|
|
|
|
Action.m_PosY = (short)pisty;
|
|
|
|
Action.m_PosZ = (int)pistz;
|
|
|
|
Action.m_Byte1 = 1;
|
2011-11-08 20:31:19 -05:00
|
|
|
Action.m_Byte2 = pistonMeta & ~(8);
|
2011-11-07 17:59:29 -05:00
|
|
|
cChunk* Chunk = m_World->GetChunk( FAST_FLOOR(pistx/16), FAST_FLOOR(pisty/16), FAST_FLOOR(pistz/16) );
|
|
|
|
Chunk->Broadcast( Action );
|
|
|
|
m_World->FastSetBlock( pistx, pisty, pistz, pistonBlock, pistonMeta & ~(8) );
|
|
|
|
|
|
|
|
AddDir( pistx, pisty, pistz, pistonMeta & 7, 1 )
|
|
|
|
if( m_World->GetBlock( pistx, pisty, pistz ) == E_BLOCK_PISTON_EXTENSION ) {
|
|
|
|
if( pistonBlock == E_BLOCK_STICKY_PISTON ) {
|
|
|
|
int tempx = pistx, tempy = pisty, tempz = pistz;
|
|
|
|
AddDir( tempx, tempy, tempz, pistonMeta&7, 1 )
|
|
|
|
char tempblock = m_World->GetBlock( tempx, tempy, tempz );
|
|
|
|
if(tempblock == E_BLOCK_OBSIDIAN || tempblock == E_BLOCK_BEDROCK || tempblock == E_BLOCK_PISTON_EXTENSION) {return;}
|
|
|
|
m_World->SetBlock( pistx, pisty, pistz, tempblock, m_World->GetBlockMeta( tempx, tempy, tempz ) );
|
|
|
|
m_World->SetBlock( tempx, tempy, tempz, E_BLOCK_AIR, 0 );
|
|
|
|
}else{
|
|
|
|
m_World->SetBlock( pistx, pisty, pistz, E_BLOCK_AIR, 0 );
|
|
|
|
}
|
2011-11-06 04:23:20 -05:00
|
|
|
}
|
|
|
|
}
|
2011-11-07 13:19:38 -05:00
|
|
|
}
|