From fa194fd9e2b6ed4aa05d6d507668abe3bd95f3f3 Mon Sep 17 00:00:00 2001 From: Quentin Bazin Date: Thu, 20 May 2021 03:25:12 +0200 Subject: [PATCH] Fixed dimension change. --- mods/default/blocks.lua | 2 -- .../client/network/ClientCommandHandler.cpp | 3 +- source/client/world/ClientPlayer.cpp | 4 --- source/client/world/ClientWorld.cpp | 2 ++ source/common/world/World.cpp | 11 ++++++ source/common/world/World.hpp | 3 ++ .../server/network/ServerCommandHandler.cpp | 34 ++++++++++++------- source/server/world/ServerWorld.cpp | 7 ++-- 8 files changed, 45 insertions(+), 21 deletions(-) diff --git a/mods/default/blocks.lua b/mods/default/blocks.lua index 077e1093..522bf3ae 100644 --- a/mods/default/blocks.lua +++ b/mods/default/blocks.lua @@ -241,8 +241,6 @@ mod:block { } server:send_player_change_dimension(client.id, pos.x, pos.y, pos.z, dim, client) - player:set_dimension(dim) - -- player:set_position(pos.x, pos.y, pos.z) end, } diff --git a/source/client/network/ClientCommandHandler.cpp b/source/client/network/ClientCommandHandler.cpp index 3e9a63ad..a0ebc8fb 100644 --- a/source/client/network/ClientCommandHandler.cpp +++ b/source/client/network/ClientCommandHandler.cpp @@ -303,10 +303,11 @@ void ClientCommandHandler::setupCallbacks() { if (clientId == m_client.id()) { m_player.setDimension(dimension); - m_player.setPosition(x, y, z); + m_player.setPosition(x + 0.5, y + 0.5, z + 0.5); m_world.clear(); m_world.changeDimension(dimension); m_entityMap.clear(); + sendPlayerChunkPosUpdate(); } }); diff --git a/source/client/world/ClientPlayer.cpp b/source/client/world/ClientPlayer.cpp index 55d678cd..8aa71ea0 100644 --- a/source/client/world/ClientPlayer.cpp +++ b/source/client/world/ClientPlayer.cpp @@ -163,10 +163,6 @@ void ClientPlayer::updatePosition(const ClientWorld &world) { } void ClientPlayer::setPosition(double x, double y, double z) { - m_x = x; - m_y = y; - m_z = z; - Player::setPosition(x, y, z); gk::Vector3f camPos = m_cameraLocalPos; diff --git a/source/client/world/ClientWorld.cpp b/source/client/world/ClientWorld.cpp index f5011967..1de20b46 100644 --- a/source/client/world/ClientWorld.cpp +++ b/source/client/world/ClientWorld.cpp @@ -111,6 +111,8 @@ void ClientWorld::clear() { } void ClientWorld::changeDimension(u16 dimensionID) { + World::clearUpdateQueues(); + const Dimension &dimension = Registry::getInstance().getDimension(dimensionID); m_dimension = &dimension; diff --git a/source/common/world/World.cpp b/source/common/world/World.cpp index 0f903955..77f59f3e 100644 --- a/source/common/world/World.cpp +++ b/source/common/world/World.cpp @@ -116,6 +116,17 @@ void World::setData(int x, int y, int z, u16 data) const { chunk->setData(x & (CHUNK_WIDTH - 1), y & (CHUNK_DEPTH - 1), z & (CHUNK_HEIGHT - 1), data); } +void World::clearUpdateQueues() { + while (!m_chunkUpdateQueue.empty()) + m_chunkUpdateQueue.pop(); + + while (!m_chunkProcessQueue.empty()) + m_chunkProcessQueue.pop(); + + m_chunksToUpdate.clear(); + m_chunksToProcess.clear(); +} + // Please update 'docs/lua-api-cpp.md' if you change this void World::initUsertype(sol::state &lua) { lua.new_usertype("World", diff --git a/source/common/world/World.hpp b/source/common/world/World.hpp index 0dbf755b..3e085a72 100644 --- a/source/common/world/World.hpp +++ b/source/common/world/World.hpp @@ -72,6 +72,9 @@ class World { static bool isReloadRequested; + protected: + void clearUpdateQueues(); + private: std::set m_chunksToUpdate; std::queue m_chunkUpdateQueue; diff --git a/source/server/network/ServerCommandHandler.cpp b/source/server/network/ServerCommandHandler.cpp index d9f344b0..e3b9e991 100644 --- a/source/server/network/ServerCommandHandler.cpp +++ b/source/server/network/ServerCommandHandler.cpp @@ -140,20 +140,30 @@ void ServerCommandHandler::sendPlayerInvUpdate(u16 clientID, const ClientInfo *c } void ServerCommandHandler::sendPlayerChangeDimension(u16 clientID, s32 x, s32 y, s32 z, u16 dimension, const ClientInfo *client) const { - Network::Packet packet; - packet << Network::Command::PlayerChangeDimension; - packet << clientID << x << y << z << dimension; + ServerPlayer *player = m_players.getPlayerFromClientID(clientID); + if (player) { + Network::Packet packet; + packet << Network::Command::PlayerChangeDimension; + packet << clientID << x << y << z << dimension; - if (!client) - m_server.sendToAllClients(packet); + if (client) { + client->tcpSocket->send(packet); + + // FIXME: sendPlayerChangeDimension shouldn't be exposed to Lua + // Instead, there should be a changePlayerDimension function that sends + // the packet above + the entities (instead of doing that here) + // also, it should change player dimension and position accordingly + m_worldController.getWorld(dimension).scene().sendEntities(*client); + + player->setPosition(x + 0.5, y + 0.5, z + 0.5); + player->setDimension(dimension); + player->clearLoadedChunks(); + } + else + m_server.sendToAllClients(packet); + } else - client->tcpSocket->send(packet); - - // FIXME: sendPlayerChangeDimension shouldn't be exposed to Lua - // Instead, there should be a world.changePlayerDimension function that sends - // the packet above + the entities (instead of doing that here) - if (client) - m_worldController.getWorld(dimension).scene().sendEntities(*client); + gkError() << ("Failed to send dimension change for player " + std::to_string(clientID) + ": Player not found").c_str(); } void ServerCommandHandler::sendChatMessage(u16 clientID, const std::string &message, const ClientInfo *client) const { diff --git a/source/server/world/ServerWorld.cpp b/source/server/world/ServerWorld.cpp index 6c768aac..9b15ad6a 100644 --- a/source/server/world/ServerWorld.cpp +++ b/source/server/world/ServerWorld.cpp @@ -177,6 +177,9 @@ void ServerWorld::processSendRequests() { // u64 chunksTooOld = 0; // u64 startQueueSize = m_chunkSendRequestQueue.size(); + if (!m_chunkSendRequestQueue.empty()) + gkDebug() << "Processing send requests..."; + u64 start = gk::GameClock::getInstance().getTicks(true); u64 now = start; while (/* now - start < 100 && */!m_chunkSendRequestQueue.empty()) { @@ -251,8 +254,8 @@ void ServerWorld::processSendRequests() { now = gk::GameClock::getInstance().getTicks(true); } - if (m_dimension.id() == 0 && now - start > 0) { - gkDebug() << "Took" << now - start << "ms" + if (now - start > 0) { + gkDebug() << "Done in" << now - start << "ms for dim" << m_dimension.id() << "| Gen:" << chunksGenerated << "| Sent:" << chunksSent; // << "| BTQ:" << chunksBackToQueue