2019-01-12 14:18:36 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
|
|
|
* Filename: ServerWorld.cpp
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* Created: 12/01/2019 14:02:22
|
|
|
|
*
|
|
|
|
* Author: Quentin Bazin, <quent42340@gmail.com>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
2020-01-02 17:35:28 +09:00
|
|
|
#include <gk/core/GameClock.hpp>
|
|
|
|
|
2019-01-12 14:18:36 +01:00
|
|
|
#include "Config.hpp"
|
|
|
|
#include "Network.hpp"
|
2019-01-21 21:51:18 +01:00
|
|
|
#include "Server.hpp"
|
2019-01-26 10:05:37 +01:00
|
|
|
#include "ServerPlayer.hpp"
|
2019-01-12 14:18:36 +01:00
|
|
|
#include "ServerWorld.hpp"
|
|
|
|
|
2019-01-12 18:53:25 +01:00
|
|
|
ServerWorld::ServerWorld() {
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:29:19 +02:00
|
|
|
void ServerWorld::update(Server &server, std::unordered_map<u16, ServerPlayer> &players) {
|
2020-01-17 19:21:38 +09:00
|
|
|
if (m_lastTick < gk::GameClock::getTicks() / 50) {
|
|
|
|
m_lastTick = gk::GameClock::getTicks() / 50;
|
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
for (auto &it : players) {
|
|
|
|
sendChunks(it.second);
|
|
|
|
}
|
|
|
|
|
2020-01-17 19:21:38 +09:00
|
|
|
for (auto &it : m_chunks) {
|
|
|
|
it.second->tick(players, *this, server);
|
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
if (it.second->isLightGenerated())
|
|
|
|
it.second->updateLights();
|
2020-01-17 19:21:38 +09:00
|
|
|
|
|
|
|
if (it.second->isGenerated() && !it.second->isSent()) {
|
|
|
|
for (auto &client : server.info().clients())
|
|
|
|
sendChunkData(client, it.second.get());
|
|
|
|
|
|
|
|
DEBUG("Chunk updated at", it.second->x(), it.second->y(), it.second->z());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 18:53:25 +01:00
|
|
|
}
|
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
void ServerWorld::sendChunks(const ServerPlayer &player) {
|
2020-01-16 10:07:01 +09:00
|
|
|
// Player chunk pos
|
|
|
|
int pcx = std::floor(player.x() / CHUNK_WIDTH);
|
|
|
|
int pcy = std::floor(player.y() / CHUNK_HEIGHT);
|
|
|
|
int pcz = std::floor(player.z() / CHUNK_DEPTH);
|
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
std::queue<ServerChunk *> *chunkQueue;
|
2020-01-19 19:16:21 +09:00
|
|
|
u8 *currentDistance;
|
2020-01-19 18:22:50 +09:00
|
|
|
|
|
|
|
auto it = m_chunkQueues.find(player.clientID());
|
|
|
|
if (it == m_chunkQueues.end()) {
|
2020-01-19 19:16:21 +09:00
|
|
|
auto it = m_chunkQueues.emplace(player.clientID(), std::make_pair(std::queue<ServerChunk *>{}, 1));
|
|
|
|
chunkQueue = &it.first->second.first;
|
|
|
|
currentDistance = &it.first->second.second;
|
2020-01-19 18:22:50 +09:00
|
|
|
}
|
|
|
|
else {
|
2020-01-19 19:16:21 +09:00
|
|
|
chunkQueue = &it->second.first;
|
|
|
|
currentDistance = &it->second.second;
|
2020-01-19 18:22:50 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a chunk at the current player position if it doesn't exist
|
|
|
|
ServerChunk *chunk = (ServerChunk *)getChunk(pcx, pcy, pcz);
|
|
|
|
if (!chunk) {
|
|
|
|
auto it = m_chunks.emplace(gk::Vector3i{pcx, pcy, pcz}, new ServerChunk(pcx, pcy, pcz));
|
|
|
|
chunk = it.first->second.get();
|
|
|
|
|
|
|
|
// DEBUG("Creating chunk at", chunk->x(), chunk->y(), chunk->z());
|
|
|
|
}
|
2020-01-16 10:07:01 +09:00
|
|
|
|
2020-01-19 19:16:21 +09:00
|
|
|
// Add the chunk to the queue
|
|
|
|
chunkQueue->emplace(chunk);
|
2020-01-19 18:22:50 +09:00
|
|
|
|
|
|
|
// Load surrounding chunks, 4 at a time
|
|
|
|
u8 chunksSent = 0;
|
|
|
|
while (chunksSent < 4 && !chunkQueue->empty()) {
|
|
|
|
ServerChunk *chunk = chunkQueue->front();
|
|
|
|
chunkQueue->pop();
|
2020-01-16 10:07:01 +09:00
|
|
|
|
2020-01-17 19:21:38 +09:00
|
|
|
// If the chunk is already generated, update lights and send it
|
2020-01-17 18:32:40 +09:00
|
|
|
if (chunk->isGenerated()) {
|
2020-01-19 17:29:04 +09:00
|
|
|
// DEBUG("Updating lights in chunk at", chunk->x(), chunk->y(), chunk->z());
|
|
|
|
|
2020-01-17 18:32:40 +09:00
|
|
|
chunk->updateLights();
|
2020-01-19 18:22:50 +09:00
|
|
|
chunk->setLightGenerated(true);
|
2020-01-17 19:21:38 +09:00
|
|
|
|
2020-01-19 19:16:21 +09:00
|
|
|
sendChunkData(player.client(), chunk);
|
2020-01-17 18:32:40 +09:00
|
|
|
}
|
2020-01-16 10:41:47 +09:00
|
|
|
|
2020-01-17 18:32:40 +09:00
|
|
|
// DEBUG("Processing chunk in queue at", chunk->x(), chunk->y(), chunk->z());
|
2020-01-16 10:41:47 +09:00
|
|
|
|
2020-01-16 10:07:01 +09:00
|
|
|
gk::Vector3i surroundingChunks[6] = {
|
|
|
|
{chunk->x() - 1, chunk->y(), chunk->z()},
|
|
|
|
{chunk->x() + 1, chunk->y(), chunk->z()},
|
|
|
|
{chunk->x(), chunk->y(), chunk->z() - 1},
|
|
|
|
{chunk->x(), chunk->y(), chunk->z() + 1},
|
|
|
|
{chunk->x(), chunk->y() - 1, chunk->z()},
|
|
|
|
{chunk->x(), chunk->y() + 1, chunk->z()},
|
|
|
|
};
|
|
|
|
|
|
|
|
for (u8 i = 0 ; i < 6 ; ++i) {
|
2020-01-16 10:41:47 +09:00
|
|
|
// Check if this neighbour already exists, if yes then skip it
|
2020-01-17 14:39:29 +09:00
|
|
|
ServerChunk *neighbour = (ServerChunk *)getChunk(surroundingChunks[i].x, surroundingChunks[i].y, surroundingChunks[i].z);
|
2020-01-19 19:16:21 +09:00
|
|
|
if (!neighbour) {
|
|
|
|
// Create our neighbour
|
|
|
|
auto it = m_chunks.emplace(
|
|
|
|
gk::Vector3i{
|
|
|
|
surroundingChunks[i].x,
|
|
|
|
surroundingChunks[i].y,
|
|
|
|
surroundingChunks[i].z
|
|
|
|
},
|
|
|
|
new ServerChunk{
|
|
|
|
surroundingChunks[i].x,
|
|
|
|
surroundingChunks[i].y,
|
|
|
|
surroundingChunks[i].z
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Get the created neighbour
|
|
|
|
neighbour = it.first->second.get();
|
2020-01-17 14:39:29 +09:00
|
|
|
}
|
2020-01-16 10:41:47 +09:00
|
|
|
|
2020-01-17 19:21:38 +09:00
|
|
|
// DEBUG("Creating chunk at", neighbour->x(), neighbour->y(), neighbour->z());
|
2020-01-16 10:41:47 +09:00
|
|
|
|
2020-01-16 10:07:01 +09:00
|
|
|
// Assign surrounding chunk pointers
|
|
|
|
chunk->setSurroundingChunk(i, neighbour);
|
|
|
|
neighbour->setSurroundingChunk((i % 2 == 0) ? i + 1 : i - 1, chunk);
|
|
|
|
|
|
|
|
// Compute distance to player chunk
|
|
|
|
int dx = std::abs(surroundingChunks[i].x - pcx);
|
|
|
|
int dy = std::abs(surroundingChunks[i].y - pcy);
|
|
|
|
int dz = std::abs(surroundingChunks[i].z - pcz);
|
2020-01-17 14:39:29 +09:00
|
|
|
int distance = std::max(dx, std::max(dy, dz)); // FIXME
|
2020-01-16 10:07:01 +09:00
|
|
|
|
|
|
|
// If the chunk is close enough, add it to the queue
|
2020-01-19 19:16:21 +09:00
|
|
|
if (distance <= *currentDistance || (!chunk->isGenerated() && distance < Config::renderDistance))
|
2020-01-19 18:22:50 +09:00
|
|
|
chunkQueue->emplace(neighbour);
|
2020-01-19 19:16:21 +09:00
|
|
|
else {
|
|
|
|
++(*currentDistance);
|
|
|
|
if (*currentDistance >= Config::renderDistance)
|
|
|
|
*currentDistance = 1;
|
|
|
|
}
|
2020-01-16 10:07:01 +09:00
|
|
|
}
|
2020-01-17 14:39:29 +09:00
|
|
|
|
2020-01-17 19:21:38 +09:00
|
|
|
// DEBUG("Generating chunk at", chunk->x(), chunk->y(), chunk->z());
|
2020-01-17 14:39:29 +09:00
|
|
|
|
2020-01-17 19:21:38 +09:00
|
|
|
// All neighbours are created, so generate the chunk, propagate the light and send it
|
2020-01-19 19:16:21 +09:00
|
|
|
if (!chunk->isGenerated()) {
|
|
|
|
chunk->generate();
|
|
|
|
chunk->updateLights();
|
|
|
|
sendChunkData(player.client(), chunk);
|
2020-01-19 18:22:50 +09:00
|
|
|
|
2020-01-19 19:16:21 +09:00
|
|
|
chunkQueue->emplace(chunk);
|
|
|
|
}
|
2020-01-17 14:39:29 +09:00
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
++chunksSent;
|
2020-01-17 18:32:40 +09:00
|
|
|
}
|
2020-01-16 10:07:01 +09:00
|
|
|
}
|
|
|
|
|
2020-01-19 18:22:50 +09:00
|
|
|
void ServerWorld::sendChunkData(const Client &client, ServerChunk *chunk) {
|
2019-01-12 14:18:36 +01:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::ChunkData;
|
2019-01-12 18:56:23 +01:00
|
|
|
packet << chunk->x() << chunk->y() << chunk->z();
|
2020-01-17 14:39:29 +09:00
|
|
|
for (u16 x = 0 ; x < CHUNK_WIDTH ; ++x) {
|
2019-01-12 16:45:22 +01:00
|
|
|
for (u16 y = 0 ; y < CHUNK_HEIGHT ; ++y) {
|
2020-01-17 14:39:29 +09:00
|
|
|
for (u16 z = 0 ; z < CHUNK_DEPTH ; ++z) {
|
2019-01-12 18:53:25 +01:00
|
|
|
packet << u16(chunk->data()[x][y][z]);
|
2019-01-13 14:17:13 +01:00
|
|
|
packet << chunk->lightmap().getLightData(x, y, z);
|
2019-01-12 16:45:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 14:18:36 +01:00
|
|
|
|
|
|
|
client.tcpSocket->send(packet);
|
2019-01-21 21:51:18 +01:00
|
|
|
chunk->setSent(true);
|
2019-01-12 18:53:25 +01:00
|
|
|
|
2020-01-17 14:39:29 +09:00
|
|
|
// std::cout << "Chunk at (" << chunk->x() << ", " << chunk->y() << ", " << chunk->z() << ") sent to client" << std::endl;
|
2019-01-12 18:53:25 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 10:07:01 +09:00
|
|
|
Chunk *ServerWorld::getChunk(int cx, int cy, int cz) const {
|
2020-01-16 01:37:49 +09:00
|
|
|
auto it = m_chunks.find({cx, cy, cz});
|
|
|
|
if (it == m_chunks.end())
|
2019-01-12 18:53:25 +01:00
|
|
|
return nullptr;
|
|
|
|
|
2020-01-16 01:37:49 +09:00
|
|
|
return it->second.get();
|
2019-01-12 14:18:36 +01:00
|
|
|
}
|
|
|
|
|