Seems to be working, but some trees are incomplete.

This commit is contained in:
Quentin Bazin 2021-01-02 00:02:10 +01:00
parent b3b00038eb
commit b4f210de3a
8 changed files with 33 additions and 13 deletions

View File

@ -125,9 +125,9 @@ void ClientCommandHandler::sendItemActivated(const glm::ivec4 &selectedBlock) {
m_client.send(packet);
}
void ClientCommandHandler::sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ) {
void ClientCommandHandler::sendChunkUnload(s32 chunkX, s32 chunkY, s32 chunkZ) {
Network::Packet packet;
packet << Network::Command::ChunkRequest;
packet << Network::Command::ChunkUnload;
packet << chunkX << chunkY << chunkZ;
m_client.send(packet);
}

View File

@ -57,6 +57,7 @@ class ClientCommandHandler {
void sendBlockInvUpdate(Inventory &inventory);
void sendItemActivated(const glm::ivec4 &selectedBlock);
void sendChunkRequest(s32 chunkX, s32 chunkY, s32 chunkZ);
void sendChunkUnload(s32 chunkX, s32 chunkY, s32 chunkZ);
void sendChatMessage(const std::string &message);
void sendKeyPressed(u16 keyID);

View File

@ -49,6 +49,8 @@ void ClientWorld::update(bool allowWorldReload) {
for (auto it = m_chunks.begin() ; it != m_chunks.end() ;) {
// If chunk is too far, remove it
if (it->second->isTooFar() && (it->second->isInitialized() || it->second->areAllNeighboursTooFar())) {
// gkDebug() << "Chunk removed at" << it->second->x() << it->second->y() << it->second->z();
m_client->sendChunkUnload(it->second->x(), it->second->y(), it->second->z());
removeChunk(it);
}
// Otherwise, update the chunk
@ -284,7 +286,10 @@ void ClientWorld::draw(gk::RenderTarget &target, gk::RenderStates states) const
// Nope, too far, don't render it
if(glm::length(center) > (Config::renderDistance + 1) * CHUNK_WIDTH) {
it.second->setTooFar(true);
// If it is way too far, mark it for deletion
if(glm::length(center) > (Config::renderDistance + 3) * CHUNK_WIDTH)
it.second->setTooFar(true);
// gkDebug() << "Chunk at" << it.second->x() << it.second->y() << it.second->z() << "is too far:" << glm::length(center) << ">" << ((Config::renderDistance + 1) * CHUNK_WIDTH);
continue;
}

View File

@ -41,7 +41,7 @@ std::string Network::commandToString(Network::Command command) {
{Network::Command::ServerClosed, "ServerClosed"},
{Network::Command::ChunkData, "ChunkData"},
{Network::Command::ChunkRequest, "ChunkRequest"},
{Network::Command::ChunkUnload, "ChunkUnload"},
{Network::Command::PlayerPlaceBlock, "PlayerPlaceBlock"},
{Network::Command::PlayerDigBlock, "PlayerDigBlock"},

View File

@ -44,7 +44,7 @@ namespace Network {
ServerClosed = 0x11,
ChunkData = 0x20,
ChunkRequest = 0x21,
ChunkUnload = 0x21,
PlayerPlaceBlock = 0x30,
PlayerDigBlock = 0x31,

View File

@ -36,7 +36,7 @@ u8 ServerConfig::maxPlayers = 5;
u16 ServerConfig::maxItemStackSize = 64;
// World
u8 ServerConfig::renderDistance = 5;
u8 ServerConfig::renderDistance = 8;
// Mod-defined options
std::unordered_map<std::string, sol::object> ServerConfig::options;

View File

@ -277,11 +277,16 @@ void ServerCommandHandler::setupCallbacks() {
m_players.disconnectPlayer(client.playerName);
});
m_server.setCommandCallback(Network::Command::ChunkRequest, [this](ClientInfo &client, Network::Packet &packet) {
m_server.setCommandCallback(Network::Command::ChunkUnload, [this](ClientInfo &client, Network::Packet &packet) {
s32 cx, cy, cz;
packet >> cx >> cy >> cz;
// getWorldForClient(client.id).sendRequestedData(client, cx, cy, cz);
ServerPlayer *player = m_players.getPlayerFromClientID(client.id);
if (player) {
player->removeLoadedChunk(gk::Vector3i{cx, cy, cz});
}
else
gkError() << ("Failed to unload chunk for player " + std::to_string(client.id) + ": Player not found").c_str();
});
m_server.setCommandCallback(Network::Command::PlayerInvUpdate, [this](ClientInfo &client, Network::Packet &packet) {

View File

@ -51,13 +51,12 @@ void ServerWorld::update(bool doTick) {
{
for (auto &it : m_players) {
if (it.second.isReady() && it.second.dimension() == m_dimension.id()) {
it.second.sentChunks.clear();
gk::Vector3i currentChunk = it.second.getCurrentChunk();
if (!it.second.isChunkLoaded(currentChunk) || it.second.lastChunkUpdate != currentChunk) {
it.second.sentChunks.clear();
m_chunksToSend.emplace(std::make_pair(currentChunk, std::ref(it.second)));
it.second.lastChunkUpdate = currentChunk;
gkWarning() << "coucou";
gkWarning() << "Player changed chunk";
}
}
}
@ -75,8 +74,18 @@ void ServerWorld::update(bool doTick) {
player.addLoadedChunk(chunkPos);
}
gk::Vector3i playerChunkPos = player.getCurrentChunk();
if ((playerChunkPos - chunkPos).length() < ServerConfig::renderDistance) {
glm::dvec3 chunkWorldPos{
chunkPos.x * CHUNK_WIDTH + CHUNK_WIDTH / 2.f,
chunkPos.y * CHUNK_DEPTH + CHUNK_DEPTH / 2.f,
chunkPos.z * CHUNK_HEIGHT + CHUNK_HEIGHT / 2.f
};
glm::dvec3 playerPos{
player.x(),
player.y(),
player.z()
};
if (glm::length(playerPos - chunkWorldPos) < (ServerConfig::renderDistance + 1) * CHUNK_WIDTH) {
// gkDebug() << "OK for chunk" << chunkPos.x << chunkPos.y << chunkPos.z << ":" << glm::length(playerPos - chunkWorldPos) << "<" << (int)ServerConfig::renderDistance * CHUNK_WIDTH;
addChunkToSend(chunkPos, 1, 0, 0, player);
addChunkToSend(chunkPos, -1, 0, 0, player);
addChunkToSend(chunkPos, 0, 1, 0, player);