From 4d069f03014dff6404018340310ad509af0a6da3 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Thu, 2 Jan 2020 17:35:28 +0900 Subject: [PATCH] [ServerWorld] Small optimization. --- common/CMakeLists.txt | 2 +- common/include/world/Chunk.hpp | 1 - server/include/world/ServerWorld.hpp | 2 ++ server/source/world/ServerChunk.cpp | 8 +------- server/source/world/ServerWorld.cpp | 23 +++++++++++++++-------- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index fa182c7e..4489dd52 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -34,5 +34,5 @@ target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -DSOL_CHECK_ARGUMENT target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -std=c++14) # target_compile_features(${CMAKE_PROJECT_NAME}_common PRIVATE cxx_std_17) -# target_compile_options(${CMAKE_PROJECT_NAME}_server PRIVATE -pg) +# target_compile_options(${CMAKE_PROJECT_NAME}_common PRIVATE -pg) diff --git a/common/include/world/Chunk.hpp b/common/include/world/Chunk.hpp index 78e6d2df..cec1693e 100644 --- a/common/include/world/Chunk.hpp +++ b/common/include/world/Chunk.hpp @@ -91,7 +91,6 @@ class Chunk : public gk::NonCopyable { bool m_hasChanged = false; bool m_isInitialized = false; - u32 m_lastTick = 0; std::unordered_map m_tickingBlocks; std::unordered_map m_blockData; diff --git a/server/include/world/ServerWorld.hpp b/server/include/world/ServerWorld.hpp index 6a7df815..0e1e2b7a 100644 --- a/server/include/world/ServerWorld.hpp +++ b/server/include/world/ServerWorld.hpp @@ -46,6 +46,8 @@ class ServerWorld : public World { private: std::vector> m_chunks; + + u32 m_lastTick = 0; }; #endif // SERVERWORLD_HPP_ diff --git a/server/source/world/ServerChunk.cpp b/server/source/world/ServerChunk.cpp index 24474aea..b7e19aa6 100644 --- a/server/source/world/ServerChunk.cpp +++ b/server/source/world/ServerChunk.cpp @@ -11,10 +11,6 @@ * * ===================================================================================== */ -#include - -#include - #include "Player.hpp" #include "Server.hpp" #include "ServerBlock.hpp" @@ -39,9 +35,7 @@ void ServerChunk::generate() { } void ServerChunk::tick(std::unordered_map &players, World &world, Server &server) { - if (!m_tickingBlocks.empty() && m_lastTick < gk::GameClock::getTicks() / 50) { - m_lastTick = gk::GameClock::getTicks() / 50; - + if (!m_tickingBlocks.empty()) { for (auto &it : m_tickingBlocks) { int z = it.first / (width * height); int y = (it.first - z * width * height) / width; diff --git a/server/source/world/ServerWorld.cpp b/server/source/world/ServerWorld.cpp index 5e124282..e9592c40 100644 --- a/server/source/world/ServerWorld.cpp +++ b/server/source/world/ServerWorld.cpp @@ -11,6 +11,8 @@ * * ===================================================================================== */ +#include + #include "Config.hpp" #include "Network.hpp" #include "Server.hpp" @@ -49,15 +51,20 @@ ServerWorld::ServerWorld() { } void ServerWorld::update(Server &server, std::unordered_map &players) { - for (auto &it : m_chunks) { - it->tick(players, *this, server); - it->update(); + if (m_lastTick < gk::GameClock::getTicks() / 50) { + m_lastTick = gk::GameClock::getTicks() / 50; - if (it->isGenerated() && !it->isSent()) { - for (auto &client : server.info().clients()) - sendChunkData(client, it.get()); - // DEBUG("Chunk updated at", it->x(), it->y(), it->z()); - it->setSent(true); + for (auto &it : m_chunks) { + it->tick(players, *this, server); + + it->update(); + + if (it->isGenerated() && !it->isSent()) { + for (auto &client : server.info().clients()) + sendChunkData(client, it.get()); + // DEBUG("Chunk updated at", it->x(), it->y(), it->z()); + it->setSent(true); + } } } }