From e0bceb2db111c09bc7f7cd941ab67d16538ccdb0 Mon Sep 17 00:00:00 2001 From: yvt Date: Fri, 6 Dec 2013 15:51:14 +0900 Subject: [PATCH] Fixed #42 --- Sources/Client/GameMapWrapper.cpp | 45 +++++++++++++++++++++---------- Sources/Client/GameMapWrapper.h | 4 ++- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/Sources/Client/GameMapWrapper.cpp b/Sources/Client/GameMapWrapper.cpp index 296b5528..f4d1244d 100644 --- a/Sources/Client/GameMapWrapper.cpp +++ b/Sources/Client/GameMapWrapper.cpp @@ -205,6 +205,11 @@ namespace spades { } + template + static inline bool EqualTwoCond(T a, T b, T c, bool cond) { + return a == b || (cond && a == c); + } + std::vector GameMapWrapper::RemoveBlocks(const std::vector& cells) { SPADES_MARK_FUNCTION(); @@ -221,8 +226,13 @@ namespace spades { for(size_t i = 0; i < cells.size(); i++){ CellPos pos = cells[i]; m->Set(pos.x, pos.y, pos.z, false, 0); - if(GetLink(pos.x, pos.y, pos.z) == Invalid) - continue; + // if(GetLink(pos.x, pos.y, pos.z) == Invalid){ + // this block is already disconnected. + // } + + if(GetLink(pos.x, pos.y, pos.z) == Marked){ + continue; + } SPAssert(GetLink(pos.x, pos.y, pos.z) != Root); SetLink(pos.x, pos.y, pos.z, Invalid); @@ -237,34 +247,41 @@ namespace spades { // don't "continue;" when non-solid int x = pos.x, y = pos.y, z = pos.z; - if(x > 0 && GetLink(x-1,y,z) == PositiveX){ - SetLink(x-1, y, z, Invalid); + if(x > 0 && EqualTwoCond(GetLink(x-1,y,z), PositiveX, Invalid, m->IsSolid(x-1, y, z))){ + SetLink(x-1, y, z, Marked); queue.Push(CellPos(x-1, y, z)); } - if(x < width-1 && GetLink(x+1,y,z) == NegativeX){ - SetLink(x+1, y, z, Invalid); + if(x < width-1 && EqualTwoCond(GetLink(x+1,y,z), NegativeX, Invalid, m->IsSolid(x+1, y, z))){ + SetLink(x+1, y, z, Marked); queue.Push(CellPos(x+1, y, z)); } - if(y > 0 && GetLink(x,y-1,z) == PositiveY){ - SetLink(x, y-1, z, Invalid); + if(y > 0 && EqualTwoCond(GetLink(x,y-1,z), PositiveY, Invalid, m->IsSolid(x, y-1, z))){ + SetLink(x, y-1, z, Marked); queue.Push(CellPos(x, y-1, z)); } - if(y < height-1 && GetLink(x,y+1,z) == NegativeY){ - SetLink(x, y+1, z, Invalid); + if(y < height-1 && EqualTwoCond(GetLink(x,y+1,z), NegativeY, Invalid, m->IsSolid(x, y+1, z))){ + SetLink(x, y+1, z, Marked); queue.Push(CellPos(x, y+1, z)); } - if(z > 0 && GetLink(x,y,z-1) == PositiveZ){ - SetLink(x, y, z-1, Invalid); + if(z > 0 && EqualTwoCond(GetLink(x,y,z-1), PositiveZ, Invalid, m->IsSolid(x, y, z-1))){ + SetLink(x, y, z-1, Marked); queue.Push(CellPos(x, y, z-1)); } - if(z < depth-1 && GetLink(x,y,z+1) == NegativeZ){ - SetLink(x, y, z+1, Invalid); + if(z < depth-1 && EqualTwoCond(GetLink(x,y,z+1), NegativeZ, Invalid, m->IsSolid(x, y, z+1))){ + SetLink(x, y, z+1, Marked); queue.Push(CellPos(x, y, z+1)); } } } + // remove "visited" mark + for(size_t i = 0; i < unlinkedCells.size(); i++){ + const CellPos& pos = unlinkedCells[i]; + if(GetLink(pos.x, pos.y, pos.z) == Marked) + SetLink(pos.x, pos.y, pos.z, Invalid); + } + SPAssert(queue.IsEmpty()); // start relinking diff --git a/Sources/Client/GameMapWrapper.h b/Sources/Client/GameMapWrapper.h index 4777b5b8..2d5ab283 100644 --- a/Sources/Client/GameMapWrapper.h +++ b/Sources/Client/GameMapWrapper.h @@ -67,7 +67,9 @@ namespace spades { Invalid = 0, Root, NegativeX, PositiveX, NegativeY, PositiveY, - NegativeZ, PositiveZ + NegativeZ, PositiveZ, + + Marked };