diff --git a/docs/network-protocol.md b/docs/network-protocol.md index 06e80732..8aeca97a 100644 --- a/docs/network-protocol.md +++ b/docs/network-protocol.md @@ -274,6 +274,14 @@ Packet sent from a client when it is ready to receive chunks. _This packet has no field._ +#### PlayerChunkPosUpdate + +| Field name | Field type | Notes | +| ------------- | ----------- | ---------------------------------------------------- | +| Chunk X | s32 | Chunk X coordinate | +| Chunk Y | s32 | Chunk Y coordinate | +| Chunk Z | s32 | Chunk Z coordinate | + #### BlockActivated | Field name | Field type | Notes | diff --git a/source/client/network/ClientCommandHandler.cpp b/source/client/network/ClientCommandHandler.cpp index a513a17b..3e9a63ad 100644 --- a/source/client/network/ClientCommandHandler.cpp +++ b/source/client/network/ClientCommandHandler.cpp @@ -97,6 +97,14 @@ void ClientCommandHandler::sendPlayerReady() { m_client.send(packet); } +void ClientCommandHandler::sendPlayerChunkPosUpdate() const { + Network::Packet packet; + const gk::Vector3i &chunkPos = m_player.getCurrentChunk(); + packet << Network::Command::PlayerChunkPosUpdate + << s32(chunkPos.x) << s32(chunkPos.y) << s32(chunkPos.z); + m_client.send(packet); +} + void ClientCommandHandler::sendBlockActivated(const glm::ivec4 &selectedBlock) { Network::Packet packet; packet << Network::Command::BlockActivated diff --git a/source/client/network/ClientCommandHandler.hpp b/source/client/network/ClientCommandHandler.hpp index 94d6fab1..fbc5c727 100644 --- a/source/client/network/ClientCommandHandler.hpp +++ b/source/client/network/ClientCommandHandler.hpp @@ -53,6 +53,7 @@ class ClientCommandHandler { void sendPlayerPlaceBlock(s32 x, s32 y, s32 z, u32 block); void sendPlayerHeldItemChanged(u8 hotbarSlot, u16 itemID); void sendPlayerReady(); + void sendPlayerChunkPosUpdate() const; void sendBlockActivated(const glm::ivec4 &selectedBlock); void sendBlockInvUpdate(Inventory &inventory); void sendItemActivated(const glm::ivec4 &selectedBlock); diff --git a/source/client/world/ClientPlayer.cpp b/source/client/world/ClientPlayer.cpp index 912df214..55d678cd 100644 --- a/source/client/world/ClientPlayer.cpp +++ b/source/client/world/ClientPlayer.cpp @@ -29,6 +29,7 @@ #include #include +#include "ClientCommandHandler.hpp" #include "ClientPlayer.hpp" #include "ClientWorld.hpp" #include "GameConfig.hpp" @@ -153,6 +154,12 @@ void ClientPlayer::updatePosition(const ClientWorld &world) { else { GameConfig::currentScreenEffect = 0; } + + // Sending PlayerChunkPosUpdate if needed + if (!m_lastChunkPos.has_value() || m_lastChunkPos.value() != getCurrentChunk()) { + world.client().sendPlayerChunkPosUpdate(); + m_lastChunkPos = getCurrentChunk(); + } } void ClientPlayer::setPosition(double x, double y, double z) { diff --git a/source/client/world/ClientPlayer.hpp b/source/client/world/ClientPlayer.hpp index 9789881f..4c1506e7 100644 --- a/source/client/world/ClientPlayer.hpp +++ b/source/client/world/ClientPlayer.hpp @@ -88,6 +88,8 @@ class ClientPlayer : public Player { bool m_isJumping = false; const float m_jumpSpeed = 0.06f; + + std::optional m_lastChunkPos; }; #endif // CLIENTPLAYER_HPP_ diff --git a/source/client/world/ClientWorld.hpp b/source/client/world/ClientWorld.hpp index 6f3002fa..310ce1c1 100644 --- a/source/client/world/ClientWorld.hpp +++ b/source/client/world/ClientWorld.hpp @@ -65,6 +65,8 @@ class ClientWorld : public World, public gk::Drawable { const ClientScene &scene() const { return m_scene; } ClientScene &scene() { return m_scene; } + const ClientCommandHandler &client() const { return *m_client; } + void setClient(ClientCommandHandler &client) { m_client = &client; } void setCamera(gk::Camera &camera) { m_camera = &camera; m_scene.setCamera(camera); } void setEventHandler(gk::EventHandler &eventHandler) { m_eventHandler = &eventHandler; } diff --git a/source/common/network/Network.cpp b/source/common/network/Network.cpp index 6919c4ed..311691f8 100644 --- a/source/common/network/Network.cpp +++ b/source/common/network/Network.cpp @@ -52,6 +52,7 @@ std::string Network::commandToString(Network::Command command) { {Network::Command::PlayerChangeDimension, "PlayerChangeDimension"}, {Network::Command::PlayerHeldItemChanged, "PlayerHeldItemChanged"}, {Network::Command::PlayerReady, "PlayerReady"}, + {Network::Command::PlayerChunkPosUpdate, "PlayerChunkPosUpdate"}, {Network::Command::BlockUpdate, "BlockUpdate"}, {Network::Command::BlockActivated, "BlockActivated"}, diff --git a/source/common/network/Network.hpp b/source/common/network/Network.hpp index 4403af5d..b5b999dc 100644 --- a/source/common/network/Network.hpp +++ b/source/common/network/Network.hpp @@ -55,6 +55,7 @@ namespace Network { PlayerChangeDimension = 0x36, PlayerHeldItemChanged = 0x37, PlayerReady = 0x38, + PlayerChunkPosUpdate = 0x39, BlockUpdate = 0x40, BlockActivated = 0x41,