diff options
author | worktycho <work.tycho@gmail.com> | 2015-11-07 19:45:59 +0100 |
---|---|---|
committer | worktycho <work.tycho@gmail.com> | 2015-11-07 19:45:59 +0100 |
commit | e85082d2f1161fa29d0a04c87b35cab1252ff143 (patch) | |
tree | f3dfcaa95ccf110bada016d20aac3c3446d3d501 /src/Blocks | |
parent | Merge pull request #2621 from Gargaj/pressureplatepower (diff) | |
parent | Refactored code to use vectors in the cPistonHandler class (diff) | |
download | cuberite-e85082d2f1161fa29d0a04c87b35cab1252ff143.tar cuberite-e85082d2f1161fa29d0a04c87b35cab1252ff143.tar.gz cuberite-e85082d2f1161fa29d0a04c87b35cab1252ff143.tar.bz2 cuberite-e85082d2f1161fa29d0a04c87b35cab1252ff143.tar.lz cuberite-e85082d2f1161fa29d0a04c87b35cab1252ff143.tar.xz cuberite-e85082d2f1161fa29d0a04c87b35cab1252ff143.tar.zst cuberite-e85082d2f1161fa29d0a04c87b35cab1252ff143.zip |
Diffstat (limited to '')
-rw-r--r-- | src/Blocks/BlockHandler.cpp | 2 | ||||
-rw-r--r-- | src/Blocks/BlockPiston.cpp | 294 | ||||
-rw-r--r-- | src/Blocks/BlockPiston.h | 34 | ||||
-rw-r--r-- | src/Blocks/BlockSlime.h | 32 |
4 files changed, 235 insertions, 127 deletions
diff --git a/src/Blocks/BlockHandler.cpp b/src/Blocks/BlockHandler.cpp index dab99e53d..bb479db53 100644 --- a/src/Blocks/BlockHandler.cpp +++ b/src/Blocks/BlockHandler.cpp @@ -72,6 +72,7 @@ #include "BlockSideways.h" #include "BlockSignPost.h" #include "BlockSlab.h" +#include "BlockSlime.h" #include "BlockSnow.h" #include "BlockStairs.h" #include "BlockStems.h" @@ -297,6 +298,7 @@ cBlockHandler * cBlockHandler::CreateBlockHandler(BLOCKTYPE a_BlockType) case E_BLOCK_SEA_LANTERN: return new cBlockSeaLanternHandler (a_BlockType); case E_BLOCK_SIGN_POST: return new cBlockSignPostHandler (a_BlockType); case E_BLOCK_SNOW: return new cBlockSnowHandler (a_BlockType); + case E_BLOCK_SLIME_BLOCK: return new cBlockSlimeHandler (a_BlockType); case E_BLOCK_SPRUCE_DOOR: return new cBlockDoorHandler (a_BlockType); case E_BLOCK_SPRUCE_FENCE_GATE: return new cBlockFenceGateHandler (a_BlockType); case E_BLOCK_SPRUCE_WOOD_STAIRS: return new cBlockStairsHandler (a_BlockType); diff --git a/src/Blocks/BlockPiston.cpp b/src/Blocks/BlockPiston.cpp index 94782a7ed..1da2a61ec 100644 --- a/src/Blocks/BlockPiston.cpp +++ b/src/Blocks/BlockPiston.cpp @@ -7,25 +7,12 @@ #include "BlockInServerPluginInterface.h" #include "ChunkInterface.h" +#include <vector> +#include <array> -#define AddPistonDir(x, y, z, dir, amount) \ - switch (dir & 0x07) \ - { \ - 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; \ - default: \ - { \ - LOGWARNING("%s: invalid direction %d, ignoring", __FUNCTION__, dir & 0x07); \ - break; \ - } \ - } #define PISTON_MAX_PUSH_DISTANCE 12 @@ -43,16 +30,15 @@ cBlockPistonHandler::cBlockPistonHandler(BLOCKTYPE a_BlockType) void cBlockPistonHandler::OnDestroyed(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, int a_BlockX, int a_BlockY, int a_BlockZ) { - NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + Vector3i blockPos(a_BlockX, a_BlockY, a_BlockZ); - int newX = a_BlockX; - int newY = a_BlockY; - int newZ = a_BlockZ; - AddPistonDir(newX, newY, newZ, OldMeta, 1); + // Get the extension of the piston + NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(blockPos.x, blockPos.y, blockPos.z); + blockPos += MetadataToOffset(OldMeta); - if (a_ChunkInterface.GetBlock(newX, newY, newZ) == E_BLOCK_PISTON_EXTENSION) + if (a_ChunkInterface.GetBlock(blockPos) == E_BLOCK_PISTON_EXTENSION) { - a_ChunkInterface.SetBlock(newX, newY, newZ, E_BLOCK_AIR, 0); + a_ChunkInterface.SetBlock(blockPos.x, blockPos.y, blockPos.z, E_BLOCK_AIR, 0); } } @@ -86,105 +72,187 @@ bool cBlockPistonHandler::GetPlacementBlockTypeMeta( -int cBlockPistonHandler::FirstPassthroughBlock(int a_PistonX, int a_PistonY, int a_PistonZ, NIBBLETYPE pistonmeta, cWorld * a_World) +Vector3i cBlockPistonHandler::MetadataToOffset(NIBBLETYPE a_PistonMeta) { - // Examine each of the 12 blocks ahead of the piston: - for (int ret = 0; ret < PISTON_MAX_PUSH_DISTANCE; ret++) + switch (a_PistonMeta & 0x07) { - BLOCKTYPE currBlock; - NIBBLETYPE currMeta; - AddPistonDir(a_PistonX, a_PistonY, a_PistonZ, pistonmeta, 1); - a_World->GetBlockTypeMeta(a_PistonX, a_PistonY, a_PistonZ, currBlock, currMeta); - if (cBlockInfo::IsPistonBreakable(currBlock)) + case 0: return Vector3i( 0, -1, 0); + case 1: return Vector3i( 0, 1, 0); + case 2: return Vector3i( 0, 0, -1); + case 3: return Vector3i( 0, 0, 1); + case 4: return Vector3i(-1, 0, 0); + case 5: return Vector3i( 1, 0, 0); + default: { - // This block breaks when pushed, extend up to here - return ret; + LOGWARNING("%s: invalid direction %d, ignoring", __FUNCTION__, a_PistonMeta & 0x07); + ASSERT(!"Invalid direction"); + return Vector3i(); } - if (!CanPush(currBlock, currMeta)) + } +} + + + + + +void cBlockPistonHandler::PushBlocks( + const Vector3iSet & a_BlocksToPush, + cWorld * a_World, const Vector3i & a_PushDir +) +{ + // Sort blocks to move the blocks first, which are farest away from the piston + // This prevents the overwriting of existing blocks + std::vector<Vector3i> sortedBlocks(a_BlocksToPush.begin(), a_BlocksToPush.end()); + std::sort(sortedBlocks.begin(), sortedBlocks.end(), [a_PushDir](const Vector3i & a, const Vector3i & b) + { + return a.Dot(a_PushDir) > b.Dot(a_PushDir); + }); + + // Move every block + BLOCKTYPE moveBlock; + NIBBLETYPE moveMeta; + for (auto & moveBlockPos : sortedBlocks) + { + a_World->GetBlockTypeMeta(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, moveBlock, moveMeta); + a_World->SetBlock(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, E_BLOCK_AIR, 0); + + moveBlockPos += a_PushDir; + if (cBlockInfo::IsPistonBreakable(moveBlock)) + { + // Block is breakable, drop it + cBlockHandler * Handler = BlockHandler(moveBlock); + if (Handler->DoesDropOnUnsuitable()) + { + cChunkInterface ChunkInterface(a_World->GetChunkMap()); + cBlockInServerPluginInterface PluginInterface(*a_World); + Handler->DropBlock(ChunkInterface, *a_World, PluginInterface, nullptr, + moveBlockPos.x, moveBlockPos.y, moveBlockPos.z + ); + } + } else { - // This block cannot be pushed at all, the piston can't extend - return -1; + // Not breakable, just move it + a_World->SetBlock(moveBlockPos.x, moveBlockPos.y, moveBlockPos.z, moveBlock, moveMeta); } } - // 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) +bool cBlockPistonHandler::CanPushBlock( + const Vector3i & a_BlockPos, cWorld * a_World, bool a_RequirePushable, + Vector3iSet & a_BlocksPushed, const Vector3i & a_PushDir +) { - BLOCKTYPE pistonBlock; - NIBBLETYPE pistonMeta; - a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, pistonBlock, pistonMeta); + const static std::array<Vector3i, 6> pushingDirs = + { + { + Vector3i(-1, 0, 0), Vector3i(1, 0, 0), + Vector3i( 0, -1, 0), Vector3i(0, 1, 0), + Vector3i( 0, 0, -1), Vector3i(0, 0, 1) + } + }; - if (IsExtended(pistonMeta)) + BLOCKTYPE currBlock; + NIBBLETYPE currMeta; + a_World->GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, currBlock, currMeta); + + if (currBlock == E_BLOCK_AIR) { - // Already extended, bail out - return; + // Air can be pushed + return true; } - int dist = FirstPassthroughBlock(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, a_World); - if (dist < 0) + if (!a_RequirePushable && cBlockInfo::IsPistonBreakable(currBlock)) { - // FirstPassthroughBlock says piston can't push anything, bail out - return; + // Block should not be broken, when it's not in the pushing direction + return true; } - a_World->BroadcastBlockAction(a_BlockX, a_BlockY, a_BlockZ, 0, pistonMeta, pistonBlock); - a_World->BroadcastSoundEffect("tile.piston.out", static_cast<double>(a_BlockX), static_cast<double>(a_BlockY), static_cast<double>(a_BlockZ), 0.5f, 0.7f); + if (!CanPush(currBlock, currMeta)) + { + // When it's not required to push this block, don't fail + return !a_RequirePushable; + } - // 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) + if (a_BlocksPushed.size() >= PISTON_MAX_PUSH_DISTANCE) { - cBlockHandler * Handler = BlockHandler(currBlock); - if (Handler->DoesDropOnUnsuitable()) + // Do not allow to push too much blocks + return false; + } + + if (!a_BlocksPushed.insert(a_BlockPos).second || cBlockInfo::IsPistonBreakable(currBlock)) + { + return true; // Element exist already + } + + if (currBlock == E_BLOCK_SLIME_BLOCK) + { + // Try to push the other directions + for (const auto & testDir : pushingDirs) { - cChunkInterface ChunkInterface(a_World->GetChunkMap()); - cBlockInServerPluginInterface PluginInterface(*a_World); - Handler->DropBlock(ChunkInterface, *a_World, PluginInterface, nullptr, a_BlockX, a_BlockY, a_BlockZ); + if (!CanPushBlock(a_BlockPos + testDir, a_World, false, a_BlocksPushed, a_PushDir)) + { + // When it's not possible for a direction, then fail + return false; + } } } - // Push blocks, from the furthest to the nearest: - int oldx = a_BlockX, oldy = a_BlockY, oldz = a_BlockZ; - NIBBLETYPE currBlockMeta; + // Try to push the block in front of this block + return CanPushBlock(a_BlockPos + a_PushDir, a_World, true, a_BlocksPushed, a_PushDir); +} + + + + - for (int i = dist + 1; i > 1; i--) +void cBlockPistonHandler::ExtendPiston(Vector3i a_BlockPos, cWorld * a_World) +{ + BLOCKTYPE pistonBlock; + NIBBLETYPE pistonMeta; + a_World->GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta); + + if (IsExtended(pistonMeta)) { - 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); - oldx = a_BlockX; - oldy = a_BlockY; - oldz = a_BlockZ; + // Already extended, bail out + return; } - int extx = a_BlockX; - int exty = a_BlockY; - int extz = a_BlockZ; - AddPistonDir(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, -1); - // "a_Block" now at piston body, "ext" at future extension + Vector3i pushDir = MetadataToOffset(pistonMeta); - 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)); + Vector3iSet blocksPushed; + if (!CanPushBlock(a_BlockPos + pushDir, a_World, true, blocksPushed, pushDir)) + { + // Can't push anything, bail out + return; + } + + a_World->BroadcastBlockAction(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, 0, pistonMeta, pistonBlock); + a_World->BroadcastSoundEffect("tile.piston.out", a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, 0.5f, 0.7f); + + PushBlocks(blocksPushed, a_World, pushDir); + + // Set the extension and the piston base correctly + Vector3i extensionPos = a_BlockPos + pushDir; + a_World->SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta | 0x8); + a_World->SetBlock( + extensionPos.x, extensionPos.y, extensionPos.z, + E_BLOCK_PISTON_EXTENSION, pistonMeta | (IsSticky(pistonBlock) ? 8 : 0) + ); } -void cBlockPistonHandler::RetractPiston(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World) +void cBlockPistonHandler::RetractPiston(Vector3i a_BlockPos, cWorld * a_World) { BLOCKTYPE pistonBlock; NIBBLETYPE pistonMeta; - a_World->GetBlockTypeMeta(a_BlockX, a_BlockY, a_BlockZ, pistonBlock, pistonMeta); + a_World->GetBlockTypeMeta(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta); if (!IsExtended(pistonMeta)) { @@ -192,39 +260,42 @@ void cBlockPistonHandler::RetractPiston(int a_BlockX, int a_BlockY, int a_BlockZ return; } + Vector3i pushDir = MetadataToOffset(pistonMeta); + // 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) + Vector3i extensionPos = a_BlockPos + pushDir; + if (a_World->GetBlock(extensionPos) != 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); - a_World->BroadcastSoundEffect("tile.piston.in", static_cast<double>(a_BlockX), static_cast<double>(a_BlockY), static_cast<double>(a_BlockZ), 0.5f, 0.7f); - AddPistonDir(a_BlockX, a_BlockY, a_BlockZ, pistonMeta, 1); + // Remove Extension + a_World->SetBlock(extensionPos.x, extensionPos.y, extensionPos.z, E_BLOCK_AIR, 0); - // Retract the extension, pull block if appropriate - if (IsSticky(pistonBlock)) + a_World->SetBlock(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, pistonBlock, pistonMeta & ~(8)); + a_World->BroadcastBlockAction(a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, 1, pistonMeta & ~(8), pistonBlock); + a_World->BroadcastSoundEffect("tile.piston.in", a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, 0.5f, 0.7f); + + 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); - a_World->SetBlock(tempx, tempy, tempz, E_BLOCK_AIR, 0); - return; - } + // No need for block pulling, bail out + return; + } + + // Get the block to pull + a_BlockPos += pushDir * 2; + // Try to "push" the pulling block in the opposite direction + pushDir *= -1; + + Vector3iSet pushedBlocks; + if (!CanPushBlock(a_BlockPos, a_World, false, pushedBlocks, pushDir)) + { + // Not pushable, bail out + return; } - - // Retract without pulling - a_World->SetBlock(a_BlockX, a_BlockY, a_BlockZ, E_BLOCK_AIR, 0); + + PushBlocks(pushedBlocks, a_World, pushDir); } @@ -245,17 +316,16 @@ cBlockPistonHeadHandler::cBlockPistonHeadHandler(void) : void cBlockPistonHeadHandler::OnDestroyedByPlayer(cChunkInterface & a_ChunkInterface, cWorldInterface & a_WorldInterface, cPlayer * a_Player, int a_BlockX, int a_BlockY, int a_BlockZ) { - NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(a_BlockX, a_BlockY, a_BlockZ); + Vector3i blockPos(a_BlockX, a_BlockY, a_BlockZ); - int newX = a_BlockX; - int newY = a_BlockY; - int newZ = a_BlockZ; - AddPistonDir(newX, newY, newZ, OldMeta, -1); + // Get the base of the piston + NIBBLETYPE OldMeta = a_ChunkInterface.GetBlockMeta(blockPos.x, blockPos.y, blockPos.z); + blockPos -= cBlockPistonHandler::MetadataToOffset(OldMeta); - BLOCKTYPE Block = a_ChunkInterface.GetBlock(newX, newY, newZ); + BLOCKTYPE Block = a_ChunkInterface.GetBlock(blockPos); if ((Block == E_BLOCK_STICKY_PISTON) || (Block == E_BLOCK_PISTON)) { - a_ChunkInterface.DigBlock(a_WorldInterface, newX, newY, newZ); + a_ChunkInterface.DigBlock(a_WorldInterface, blockPos.x, blockPos.y, blockPos.z); if (a_Player->IsGameModeCreative()) { return; // No pickups if creative @@ -263,7 +333,7 @@ void cBlockPistonHeadHandler::OnDestroyedByPlayer(cChunkInterface & a_ChunkInter cItems Pickups; Pickups.push_back(cItem(Block, 1)); - a_WorldInterface.SpawnItemPickups(Pickups, a_BlockX + 0.5, a_BlockY + 0.5, a_BlockZ + 0.5); + a_WorldInterface.SpawnItemPickups(Pickups, blockPos.x + 0.5, blockPos.y + 0.5, blockPos.z + 0.5); } } diff --git a/src/Blocks/BlockPiston.h b/src/Blocks/BlockPiston.h index 56f7f9951..e0066e8ab 100644 --- a/src/Blocks/BlockPiston.h +++ b/src/Blocks/BlockPiston.h @@ -3,6 +3,8 @@ #include "BlockHandler.h" +#include <unordered_set> + class cWorld; @@ -79,8 +81,11 @@ public: } } - static void ExtendPiston(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World); - static void RetractPiston(int a_BlockX, int a_BlockY, int a_BlockZ, cWorld * a_World); + /** Converts piston block's metadata into a unit vector representing the direction in which the piston will extend. */ + static Vector3i MetadataToOffset(NIBBLETYPE a_PistonMeta); + + static void ExtendPiston(Vector3i a_BlockPos, cWorld * a_World); + static void RetractPiston(Vector3i a_BlockPos, cWorld * a_World); virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override { @@ -89,6 +94,8 @@ public: } private: + + typedef std::unordered_set<Vector3i, VectorHasher<int>> Vector3iSet; /** Returns true if the piston (specified by blocktype) is a sticky piston */ static inline bool IsSticky(BLOCKTYPE a_BlockType) { return (a_BlockType == E_BLOCK_STICKY_PISTON); } @@ -141,19 +148,16 @@ private: return true; } - /** Returns true if the specified block can be pulled by a sticky piston */ - static inline bool CanPull(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) - { - if (cBlockInfo::IsPistonBreakable(a_BlockType)) - { - return false; // CanBreakPush returns true, but we need false to prevent pulling - } - - return CanPush(a_BlockType, a_BlockMeta); - } - - /** Returns how many blocks the piston has to push (where the first free space is); < 0 when unpushable */ - static int FirstPassthroughBlock(int a_PistonX, int a_PistonY, int a_PistonZ, NIBBLETYPE a_PistonMeta, cWorld * a_World); + /** Tries to push a block and increases the pushed blocks variable. Returns true if the block is pushable */ + static bool CanPushBlock( + const Vector3i & a_BlockPos, cWorld * a_World, bool a_RequirePushable, + Vector3iSet & a_BlocksPushed, const Vector3i & a_PushDir + ); + + /** Moves a list of blocks in a specific direction */ + static void PushBlocks(const Vector3iSet & a_BlocksToPush, + cWorld * a_World, const Vector3i & a_PushDir + ); } ; diff --git a/src/Blocks/BlockSlime.h b/src/Blocks/BlockSlime.h new file mode 100644 index 000000000..e785a6a13 --- /dev/null +++ b/src/Blocks/BlockSlime.h @@ -0,0 +1,32 @@ +#pragma once + +#include "BlockHandler.h" + + + + + +class cBlockSlimeHandler : + public cBlockHandler +{ +public: + cBlockSlimeHandler(BLOCKTYPE a_BlockType) + : cBlockHandler(a_BlockType) + { + } + + virtual void ConvertToPickups(cItems & a_Pickups, NIBBLETYPE a_BlockMeta) override + { + a_Pickups.push_back(cItem(m_BlockType, 1, 0)); + } + + virtual ColourID GetMapBaseColourID(NIBBLETYPE a_Meta) override + { + UNUSED(a_Meta); + return 1; + } +}; + + + + |