[ServerWorld] Small optimization.

This commit is contained in:
Quentin Bazin 2020-01-02 17:35:28 +09:00
parent 8e0774bb3a
commit 4d069f0301
5 changed files with 19 additions and 17 deletions

View File

@ -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)

View File

@ -91,7 +91,6 @@ class Chunk : public gk::NonCopyable {
bool m_hasChanged = false;
bool m_isInitialized = false;
u32 m_lastTick = 0;
std::unordered_map<std::size_t, const Block&> m_tickingBlocks;
std::unordered_map<gk::Vector3i, BlockData> m_blockData;

View File

@ -46,6 +46,8 @@ class ServerWorld : public World {
private:
std::vector<std::unique_ptr<ServerChunk>> m_chunks;
u32 m_lastTick = 0;
};
#endif // SERVERWORLD_HPP_

View File

@ -11,10 +11,6 @@
*
* =====================================================================================
*/
#include <cstring>
#include <gk/core/GameClock.hpp>
#include "Player.hpp"
#include "Server.hpp"
#include "ServerBlock.hpp"
@ -39,9 +35,7 @@ void ServerChunk::generate() {
}
void ServerChunk::tick(std::unordered_map<u16, ServerPlayer> &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;

View File

@ -11,6 +11,8 @@
*
* =====================================================================================
*/
#include <gk/core/GameClock.hpp>
#include "Config.hpp"
#include "Network.hpp"
#include "Server.hpp"
@ -49,15 +51,20 @@ ServerWorld::ServerWorld() {
}
void ServerWorld::update(Server &server, std::unordered_map<u16, ServerPlayer> &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);
}
}
}
}