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-02 17:35:28 +09:00
|
|
|
if (m_lastTick < gk::GameClock::getTicks() / 50) {
|
|
|
|
m_lastTick = gk::GameClock::getTicks() / 50;
|
|
|
|
|
|
|
|
for (auto &it : m_chunks) {
|
2020-01-16 01:37:49 +09:00
|
|
|
it.second->tick(players, *this, server);
|
2019-01-21 21:51:18 +01:00
|
|
|
|
2020-01-16 01:37:49 +09:00
|
|
|
it.second->update();
|
2020-01-02 17:35:28 +09:00
|
|
|
|
2020-01-16 01:37:49 +09:00
|
|
|
if (it.second->isGenerated() && !it.second->isSent()) {
|
2020-01-02 17:35:28 +09:00
|
|
|
for (auto &client : server.info().clients())
|
2020-01-16 01:37:49 +09:00
|
|
|
sendChunkData(client, it.second.get());
|
2020-01-02 17:35:28 +09:00
|
|
|
// DEBUG("Chunk updated at", it->x(), it->y(), it->z());
|
2020-01-16 01:37:49 +09:00
|
|
|
it.second->setSent(true);
|
2019-01-18 01:59:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 18:53:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerWorld::sendChunkData(Client &client, ServerChunk *chunk) {
|
2020-01-16 01:37:49 +09:00
|
|
|
if (!chunk) return;
|
|
|
|
|
2019-01-18 01:59:26 +01:00
|
|
|
chunk->generate();
|
2019-01-16 22:24:57 +01:00
|
|
|
chunk->update();
|
2019-12-30 20:18:52 +09:00
|
|
|
chunk->setInitialized(true);
|
2019-01-12 16:45:22 +01:00
|
|
|
|
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();
|
|
|
|
for (u16 z = 0 ; z < CHUNK_DEPTH ; ++z) {
|
2019-01-12 16:45:22 +01:00
|
|
|
for (u16 y = 0 ; y < CHUNK_HEIGHT ; ++y) {
|
2019-01-12 18:56:23 +01:00
|
|
|
for (u16 x = 0 ; x < CHUNK_WIDTH ; ++x) {
|
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-16 01:37:49 +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
|
|
|
}
|
|
|
|
|
2019-01-18 01:59:26 +01:00
|
|
|
void ServerWorld::sendRequestedData(Client &client, int cx, int cy, int cz) {
|
2020-01-16 01:37:49 +09:00
|
|
|
std::cout << "Chunk at (" << cx << ", " << cy << ", " << cz << ") requested" << std::endl;
|
|
|
|
|
2019-01-18 01:59:26 +01:00
|
|
|
ServerChunk *chunk = getChunk(cx, cy, cz);
|
2020-01-16 01:37:49 +09:00
|
|
|
if (!chunk) {
|
|
|
|
auto it = m_chunks.emplace(gk::Vector3i(cx, cy, cz), new ServerChunk(cx, cy, cz));
|
|
|
|
chunk = it.first->second.get();
|
2019-01-18 01:59:26 +01:00
|
|
|
}
|
2020-01-16 01:37:49 +09:00
|
|
|
|
|
|
|
sendChunkData(client, chunk);
|
2019-01-18 01:59:26 +01:00
|
|
|
}
|
|
|
|
|
2019-01-12 18:53:25 +01:00
|
|
|
ServerChunk *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
|
|
|
}
|
|
|
|
|
2019-01-25 23:47:45 +01:00
|
|
|
BlockData *ServerWorld::getBlockData(int x, int y, int z) const {
|
2020-01-16 01:37:49 +09:00
|
|
|
Chunk *chunk = getChunk(x / CHUNK_WIDTH, y / CHUNK_HEIGHT, z / CHUNK_DEPTH);
|
2019-01-25 23:47:45 +01:00
|
|
|
if (chunk)
|
|
|
|
return chunk->getBlockData(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1));
|
2020-01-16 01:37:49 +09:00
|
|
|
|
|
|
|
return nullptr;
|
2019-01-25 23:47:45 +01:00
|
|
|
}
|
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
u16 ServerWorld::getBlock(int x, int y, int z) const {
|
2020-01-16 01:37:49 +09:00
|
|
|
Chunk *chunk = getChunk(x / CHUNK_WIDTH, y / CHUNK_HEIGHT, z / CHUNK_DEPTH);
|
2019-01-16 22:24:57 +01:00
|
|
|
if (chunk)
|
|
|
|
return chunk->getBlock(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1));
|
2020-01-16 01:37:49 +09:00
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-16 01:37:49 +09:00
|
|
|
void ServerWorld::setBlock(int x, int y, int z, u16 id) const {
|
|
|
|
Chunk *chunk = getChunk(x / CHUNK_WIDTH, y / CHUNK_HEIGHT, z / CHUNK_DEPTH);
|
2019-01-16 22:24:57 +01:00
|
|
|
if (chunk)
|
|
|
|
chunk->setBlock(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1), id);
|
|
|
|
}
|
|
|
|
|
2019-01-26 10:05:37 +01:00
|
|
|
u16 ServerWorld::getData(int x, int y, int z) const {
|
2020-01-16 01:37:49 +09:00
|
|
|
Chunk *chunk = getChunk(x / CHUNK_WIDTH, y / CHUNK_HEIGHT, z / CHUNK_DEPTH);
|
2019-01-26 10:05:37 +01:00
|
|
|
if (chunk)
|
|
|
|
return chunk->getData(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1));
|
2020-01-16 01:37:49 +09:00
|
|
|
|
2019-01-26 10:05:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-16 01:37:49 +09:00
|
|
|
void ServerWorld::setData(int x, int y, int z, u16 id) const {
|
|
|
|
Chunk *chunk = getChunk(x / CHUNK_WIDTH, y / CHUNK_HEIGHT, z / CHUNK_DEPTH);
|
2019-01-26 10:05:37 +01:00
|
|
|
if (chunk)
|
2020-01-16 01:37:49 +09:00
|
|
|
chunk->setBlock(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1), id);
|
2019-01-26 10:05:37 +01:00
|
|
|
}
|
|
|
|
|