diff options
-rw-r--r-- | source/Piston.cpp | 180 |
1 files changed, 108 insertions, 72 deletions
diff --git a/source/Piston.cpp b/source/Piston.cpp index ee4f030c9..49093df95 100644 --- a/source/Piston.cpp +++ b/source/Piston.cpp @@ -12,35 +12,60 @@ #include "Server.h" #include "Blocks/BlockHandler.h" + + + + 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; } +#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; \ + } -cPiston::cPiston( cWorld* a_World ) - :m_World ( a_World ) +cPiston::cPiston(cWorld * a_World) + : m_World(a_World) { } -unsigned short cPiston::FirstPassthroughBlock( int pistonX, int pistonY, int pistonZ, char pistonmeta ) { + + + + +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"); + 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"); return 9001; } - char currBlock; - for( ret = 0; ret < 24; ret++ ) { // push up to 24 blocks - AddDir( pistonX, pistonY, pistonZ, pistonmeta, 1 ) + BLOCKTYPE 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;} + if ((currBlock == E_BLOCK_BEDROCK) || (currBlock == E_BLOCK_OBSIDIAN) || (currBlock == E_BLOCK_PISTON_EXTENSION)) + { + return 9001; + } + if (g_BlockPistonBreakable[currBlock]) + { + return ret; + } } return 9001; } @@ -51,49 +76,53 @@ unsigned short cPiston::FirstPassthroughBlock( int pistonX, int pistonY, int pis void cPiston::ExtendPiston( int pistx, int pisty, int pistz ) { - BLOCKTYPE pistonBlock = m_World->GetBlock( pistx, pisty, pistz ); - NIBBLETYPE pistonMeta = m_World->GetBlockMeta( pistx, pisty, pistz ); + BLOCKTYPE pistonBlock; + NIBBLETYPE pistonMeta; + m_World->GetBlockTypeMeta(pistx, pisty, pistz, pistonBlock, pistonMeta); char isSticky = (char)(pistonBlock == E_BLOCK_STICKY_PISTON) * 8; - if ( (pistonMeta & 0x8) == 0x0 ) // only extend if piston is not already extended + if ( (pistonMeta & 0x8) != 0x0 ) { - unsigned short dist = FirstPassthroughBlock(pistx, pisty, pistz, pistonMeta); - if (dist > 9000) return; // too many blocks + // Piston already extended, bail out + return; + } - AddDir( pistx, pisty, pistz, pistonMeta & 7, dist+1 ) - BLOCKTYPE currBlock = m_World->GetBlock (pistx, pisty, pistz); - NIBBLETYPE currMeta = m_World->GetBlockMeta(pistx, pisty, pistz); - if (currBlock != E_BLOCK_AIR) - { - cBlockHandler * Handler = BlockHandler(currBlock); - if (Handler->DoesDropOnUnsuitable()) - { - Handler->DropBlock(m_World, pistx, pisty, pistz); - } - } - int oldx = pistx, oldy = pisty, oldz = pistz; - char currBlockMeta; - for (int i = dist + 1; i>0; i--) + 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()) { - 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; + Handler->DropBlock(m_World, pistx, pisty, 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 ); + } + 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 ); - int extx = pistx; - int exty = pisty; - int extz = pistz; - - AddDir(extx, exty, extz, pistonMeta & 7, 1) + int extx = pistx; + int exty = pisty; + int extz = pistz; - m_World->SetBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, isSticky + pistonMeta & 7); - } + AddDir(extx, exty, extz, pistonMeta & 7, 1) + + m_World->SetBlock(extx, exty, extz, E_BLOCK_PISTON_EXTENSION, isSticky + pistonMeta & 7); } @@ -102,10 +131,12 @@ void cPiston::ExtendPiston( int pistx, int pisty, int pistz ) 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 ); - if (pistonMeta <= 6) // only retract if piston is not already retracted + BLOCKTYPE pistonBlock; + NIBBLETYPE pistonMeta; + m_World->GetBlockTypeMeta(pistx, pisty, pistz, pistonBlock, pistonMeta); + if (pistonMeta <= 6) { + // Already retracted, bail out return; } m_World->BroadcastBlockAction(pistx, pisty, pistz, 1, pistonMeta & ~(8), E_BLOCK_PISTON); @@ -113,29 +144,34 @@ void cPiston::RetractPiston( int pistx, int pisty, int pistz ) 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 (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) - ) - { - // These cannot be moved by the sticky piston, bail out - 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 + 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) + ) { - m_World->SetBlock( pistx, pisty, pistz, E_BLOCK_AIR, 0 ); + // These cannot be moved by the sticky piston, bail out + return; } + m_World->SetBlock(pistx, pisty, pistz, tempblock, tempmeta); + m_World->SetBlock(tempx, tempy, tempz, E_BLOCK_AIR, 0); + } + else + { + m_World->SetBlock(pistx, pisty, pistz, E_BLOCK_AIR, 0); } } |