From b2fd4e6b51ff3c6f515ce1a71c7f294656bd9759 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Sat, 15 Feb 2020 17:36:24 +0900 Subject: [PATCH] [ServerChunk] Incomplete tree issue fixed (see #41). --- client/source/world/ClientChunk.cpp | 3 ++- common/include/world/Chunk.hpp | 4 ++++ common/source/world/ChunkLightmap.cpp | 18 +++++++++--------- server/source/world/ServerChunk.cpp | 3 ++- server/source/world/ServerWorld.cpp | 1 + 5 files changed, 18 insertions(+), 11 deletions(-) diff --git a/client/source/world/ClientChunk.cpp b/client/source/world/ClientChunk.cpp index da14f42a..51fd6787 100644 --- a/client/source/world/ClientChunk.cpp +++ b/client/source/world/ClientChunk.cpp @@ -26,8 +26,9 @@ #include "TextureAtlas.hpp" void ClientChunk::update() { - if (m_lightmap.updateLights() || m_hasChanged) { + if (m_lightmap.updateLights() || m_hasChanged || m_hasLightChanged) { m_hasChanged = false; + m_hasLightChanged = false; m_verticesCount = m_builder.buildChunk(*this, m_vbo); } diff --git a/common/include/world/Chunk.hpp b/common/include/world/Chunk.hpp index ca67098b..a5906181 100644 --- a/common/include/world/Chunk.hpp +++ b/common/include/world/Chunk.hpp @@ -79,6 +79,9 @@ class Chunk : public gk::NonCopyable { bool hasChanged() const { return m_hasChanged; } void setChanged(bool hasChanged) { m_hasChanged = hasChanged; } + bool hasLightChanged() const { return m_hasLightChanged; } + void setLightChanged(bool hasLightChanged) { m_hasLightChanged = hasLightChanged; } + bool isInitialized() const { return m_isInitialized; } void setInitialized(bool isInitialized) { m_isInitialized = isInitialized; } @@ -106,6 +109,7 @@ class Chunk : public gk::NonCopyable { Chunk *m_surroundingChunks[6]{nullptr, nullptr, nullptr, nullptr, nullptr, nullptr}; bool m_hasChanged = false; + bool m_hasLightChanged = false; bool m_isInitialized = false; std::unordered_map m_tickingBlocks; diff --git a/common/source/world/ChunkLightmap.cpp b/common/source/world/ChunkLightmap.cpp index f4938f0e..b5e31e50 100644 --- a/common/source/world/ChunkLightmap.cpp +++ b/common/source/world/ChunkLightmap.cpp @@ -278,7 +278,7 @@ u8 ChunkLightmap::getTorchlight(int x, int y, int z) const { void ChunkLightmap::setLightData(int x, int y, int z, u8 val) { m_lightMap[x][y][z] = val; - m_chunk->setChanged(true); + m_chunk->setLightChanged(true); updateSurroundingChunks(x, y, z); } @@ -293,7 +293,7 @@ void ChunkLightmap::setSunlight(int x, int y, int z, u8 val) { m_lightMap[x][y][z] = (m_lightMap[x][y][z] & 0xf) | (val << 4); - m_chunk->setChanged(true); + m_chunk->setLightChanged(true); updateSurroundingChunks(x, y, z); }; @@ -308,17 +308,17 @@ void ChunkLightmap::setTorchlight(int x, int y, int z, u8 val) { m_lightMap[x][y][z] = (m_lightMap[x][y][z] & 0xf0) | val; - m_chunk->setChanged(true); + m_chunk->setLightChanged(true); updateSurroundingChunks(x, y, z); } void ChunkLightmap::updateSurroundingChunks(int x, int y, int z) { - if(x == 0 && m_chunk->getSurroundingChunk(Chunk::Left)) { m_chunk->getSurroundingChunk(Chunk::Left)->setChanged(true); } - if(x == CHUNK_WIDTH - 1 && m_chunk->getSurroundingChunk(Chunk::Right)) { m_chunk->getSurroundingChunk(Chunk::Right)->setChanged(true); } - if(y == 0 && m_chunk->getSurroundingChunk(Chunk::Bottom)) { m_chunk->getSurroundingChunk(Chunk::Bottom)->setChanged(true); } - if(y == CHUNK_HEIGHT - 1 && m_chunk->getSurroundingChunk(Chunk::Top)) { m_chunk->getSurroundingChunk(Chunk::Top)->setChanged(true); } - if(z == 0 && m_chunk->getSurroundingChunk(Chunk::Front)) { m_chunk->getSurroundingChunk(Chunk::Front)->setChanged(true); } - if(z == CHUNK_DEPTH - 1 && m_chunk->getSurroundingChunk(Chunk::Back)) { m_chunk->getSurroundingChunk(Chunk::Back)->setChanged(true); } + if(x == 0 && m_chunk->getSurroundingChunk(Chunk::Left)) { m_chunk->getSurroundingChunk(Chunk::Left)->setLightChanged(true); } + if(x == CHUNK_WIDTH - 1 && m_chunk->getSurroundingChunk(Chunk::Right)) { m_chunk->getSurroundingChunk(Chunk::Right)->setLightChanged(true); } + if(y == 0 && m_chunk->getSurroundingChunk(Chunk::Bottom)) { m_chunk->getSurroundingChunk(Chunk::Bottom)->setLightChanged(true); } + if(y == CHUNK_HEIGHT - 1 && m_chunk->getSurroundingChunk(Chunk::Top)) { m_chunk->getSurroundingChunk(Chunk::Top)->setLightChanged(true); } + if(z == 0 && m_chunk->getSurroundingChunk(Chunk::Front)) { m_chunk->getSurroundingChunk(Chunk::Front)->setLightChanged(true); } + if(z == CHUNK_DEPTH - 1 && m_chunk->getSurroundingChunk(Chunk::Back)) { m_chunk->getSurroundingChunk(Chunk::Back)->setLightChanged(true); } } diff --git a/server/source/world/ServerChunk.cpp b/server/source/world/ServerChunk.cpp index 31693957..64c9000c 100644 --- a/server/source/world/ServerChunk.cpp +++ b/server/source/world/ServerChunk.cpp @@ -27,8 +27,9 @@ #include "World.hpp" void ServerChunk::updateLights() { - if (m_lightmap.updateLights()) { + if (m_lightmap.updateLights() || m_hasChanged) { m_isSent = false; + m_hasChanged = false; } } diff --git a/server/source/world/ServerWorld.cpp b/server/source/world/ServerWorld.cpp index 423f8cf8..d74d9484 100644 --- a/server/source/world/ServerWorld.cpp +++ b/server/source/world/ServerWorld.cpp @@ -119,6 +119,7 @@ void ServerWorld::sendChunkData(const Client &client, ServerChunk *chunk) { client.tcpSocket->send(packet); chunk->setSent(true); + chunk->setChanged(false); // std::cout << "Chunk at (" << chunk->x() << ", " << chunk->y() << ", " << chunk->z() << ") sent to client" << std::endl; }