[ServerWorld] Chunks are now sent correctly.

This commit is contained in:
Quentin Bazin 2020-12-29 19:15:30 +01:00
parent 8bd9e860dc
commit 4fe4b60aea
3 changed files with 12 additions and 4 deletions

View File

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

View File

@ -60,6 +60,8 @@ class ServerPlayer : public Player {
public:
gk::Vector3i lastChunkUpdate{0, 0, 0}; // FIXME
std::unordered_set<gk::Vector3i> sentChunks; // Cleared every tick
private:
ClientInfo *m_client = nullptr;

View File

@ -51,6 +51,8 @@ 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) {
m_chunksToSend.emplace(std::make_pair(currentChunk, std::ref(it.second)));
@ -67,9 +69,11 @@ void ServerWorld::update(bool doTick) {
while (!m_chunksToSend.empty()) {
auto &[chunkPos, player] = m_chunksToSend.front();
if (!player.isChunkLoaded(chunkPos)) {
sendRequestedData(*player.client(), chunkPos.x, chunkPos.y, chunkPos.z);
player.addLoadedChunk(chunkPos);
if (player.sentChunks.find(chunkPos) == player.sentChunks.end()) {
if (!player.isChunkLoaded(chunkPos)) {
sendRequestedData(*player.client(), chunkPos.x, chunkPos.y, chunkPos.z);
player.addLoadedChunk(chunkPos);
}
gk::Vector3i playerChunkPos = player.getCurrentChunk();
if ((playerChunkPos - chunkPos).length() < ServerConfig::renderDistance) {
@ -80,6 +84,8 @@ void ServerWorld::update(bool doTick) {
addChunkToSend(chunkPos, 0, 0, 1, player);
addChunkToSend(chunkPos, 0, 0, -1, player);
}
player.sentChunks.emplace(chunkPos);
}
m_chunksToSend.pop();