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>
|
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
|
|
|
#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() {
|
|
|
|
for(s32 z = 0 ; z < m_depth ; z++) {
|
|
|
|
for(s32 y = 0 ; y < m_height ; y++) {
|
|
|
|
for(s32 x = 0 ; x < m_width ; x++) {
|
|
|
|
m_chunks.emplace_back(new ServerChunk(x - m_width / 2,
|
|
|
|
y - m_height / 2,
|
|
|
|
z - m_depth / 2));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-31 19:55:41 +09:00
|
|
|
// FIXME: Duplicated with ClientWorld
|
|
|
|
for(s32 z = 0 ; z < m_depth ; ++z) {
|
|
|
|
for(s32 y = 0 ; y < m_height ; ++y) {
|
|
|
|
for(s32 x = 0 ; x < m_width ; ++x) {
|
|
|
|
s32 cx = x - m_width / 2;
|
|
|
|
s32 cy = y - m_height / 2;
|
|
|
|
s32 cz = z - m_depth / 2;
|
|
|
|
|
|
|
|
Chunk *chunk = getChunk(cx, cy, cz);
|
|
|
|
if(cx > -m_width / 2) chunk->setSurroundingChunk(Chunk::Left, getChunk(cx - 1, cy, cz));
|
|
|
|
if(cx < m_width / 2 - 1) chunk->setSurroundingChunk(Chunk::Right, getChunk(cx + 1, cy, cz));
|
|
|
|
if(cy > -m_height / 2) chunk->setSurroundingChunk(Chunk::Bottom, getChunk(cx, cy - 1, cz));
|
|
|
|
if(cy < m_height / 2 - 1) chunk->setSurroundingChunk(Chunk::Top, getChunk(cx, cy + 1, cz));
|
|
|
|
if(cz > -m_depth / 2) chunk->setSurroundingChunk(Chunk::Front, getChunk(cx, cy, cz - 1));
|
|
|
|
if(cz < m_depth / 2 - 1) chunk->setSurroundingChunk(Chunk::Back, getChunk(cx, cy, cz + 1));
|
2019-01-12 18:53:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:29:19 +02:00
|
|
|
void ServerWorld::update(Server &server, std::unordered_map<u16, ServerPlayer> &players) {
|
2019-01-16 22:24:57 +01:00
|
|
|
for (auto &it : m_chunks) {
|
2019-04-07 18:20:15 +02:00
|
|
|
it->tick(players, *this, server);
|
2019-01-16 22:24:57 +01:00
|
|
|
it->update();
|
2019-01-21 21:51:18 +01:00
|
|
|
|
|
|
|
if (it->isGenerated() && !it->isSent()) {
|
|
|
|
for (auto &client : server.info().clients())
|
|
|
|
sendChunkData(client, it.get());
|
|
|
|
// DEBUG("Chunk updated at", it->x(), it->y(), it->z());
|
|
|
|
it->setSent(true);
|
|
|
|
}
|
2019-01-16 22:24:57 +01:00
|
|
|
}
|
2019-01-12 18:53:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerWorld::sendWorldData(Client &client) {
|
2019-01-18 01:59:26 +01:00
|
|
|
for(s32 z = -m_depth / 8 ; z < m_depth / 8 ; z++) {
|
|
|
|
for(s32 y = -m_height / 8 ; y < m_height / 8 ; y++) {
|
|
|
|
for(s32 x = -m_width / 8 ; x < m_width / 8 ; x++) {
|
|
|
|
sendChunkData(client, getChunk(x, y, z));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 18:53:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServerWorld::sendChunkData(Client &client, ServerChunk *chunk) {
|
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
|
|
|
|
2019-12-31 19:55:41 +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) {
|
|
|
|
ServerChunk *chunk = getChunk(cx, cy, cz);
|
|
|
|
if (chunk) {
|
|
|
|
sendChunkData(client, chunk);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-12 18:53:25 +01:00
|
|
|
ServerChunk *ServerWorld::getChunk(int cx, int cy, int cz) const {
|
|
|
|
cx += m_width / 2;
|
|
|
|
cy += m_height / 2;
|
|
|
|
cz += m_depth / 2;
|
|
|
|
|
|
|
|
if (cx < 0 || cx >= m_width || cy < 0 || cy >= m_height || cz < 0 || cz >= m_depth)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return m_chunks.at(cx + cy * m_width + cz * m_width * m_height).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 {
|
|
|
|
int cx = (x + CHUNK_WIDTH * (m_width / 2)) / CHUNK_WIDTH;
|
|
|
|
int cy = (y + CHUNK_HEIGHT * (m_height / 2)) / CHUNK_HEIGHT;
|
|
|
|
int cz = (z + CHUNK_DEPTH * (m_depth / 2)) / CHUNK_DEPTH;
|
|
|
|
|
|
|
|
if (cx < 0 || cx >= m_width || cy < 0 || cy >= m_height || cz < 0 || cz >= m_depth)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
Chunk *chunk = m_chunks.at(cx + cy * m_width + cz * m_width * m_height).get();
|
|
|
|
if (chunk)
|
|
|
|
return chunk->getBlockData(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-16 22:24:57 +01:00
|
|
|
u16 ServerWorld::getBlock(int x, int y, int z) const {
|
|
|
|
int cx = (x + CHUNK_WIDTH * (m_width / 2)) / CHUNK_WIDTH;
|
|
|
|
int cy = (y + CHUNK_HEIGHT * (m_height / 2)) / CHUNK_HEIGHT;
|
|
|
|
int cz = (z + CHUNK_DEPTH * (m_depth / 2)) / CHUNK_DEPTH;
|
|
|
|
|
|
|
|
if (cx < 0 || cx >= m_width || cy < 0 || cy >= m_height || cz < 0 || cz >= m_depth)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ServerChunk *chunk = m_chunks.at(cx + cy * m_width + cz * m_width * m_height).get();
|
|
|
|
if (chunk)
|
|
|
|
return chunk->getBlock(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerWorld::setBlock(int x, int y, int z, u16 id) {
|
|
|
|
int cx = (x + CHUNK_WIDTH * (m_width / 2)) / CHUNK_WIDTH;
|
|
|
|
int cy = (y + CHUNK_HEIGHT * (m_height / 2)) / CHUNK_HEIGHT;
|
|
|
|
int cz = (z + CHUNK_DEPTH * (m_depth / 2)) / CHUNK_DEPTH;
|
|
|
|
|
|
|
|
if (cx < 0 || cx >= m_width || cy < 0 || cy >= m_height || cz < 0 || cz >= m_depth)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ServerChunk *chunk = m_chunks.at(cx + cy * m_width + cz * m_width * m_height).get();
|
|
|
|
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 {
|
|
|
|
int cx = (x + CHUNK_WIDTH * (m_width / 2)) / CHUNK_WIDTH;
|
|
|
|
int cy = (y + CHUNK_HEIGHT * (m_height / 2)) / CHUNK_HEIGHT;
|
|
|
|
int cz = (z + CHUNK_DEPTH * (m_depth / 2)) / CHUNK_DEPTH;
|
|
|
|
|
|
|
|
if (cx < 0 || cx >= m_width || cy < 0 || cy >= m_height || cz < 0 || cz >= m_depth)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ServerChunk *chunk = m_chunks.at(cx + cy * m_width + cz * m_width * m_height).get();
|
|
|
|
if (chunk)
|
|
|
|
return chunk->getData(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServerWorld::setData(int x, int y, int z, u16 id) {
|
|
|
|
int cx = (x + CHUNK_WIDTH * (m_width / 2)) / CHUNK_WIDTH;
|
|
|
|
int cy = (y + CHUNK_HEIGHT * (m_height / 2)) / CHUNK_HEIGHT;
|
|
|
|
int cz = (z + CHUNK_DEPTH * (m_depth / 2)) / CHUNK_DEPTH;
|
|
|
|
|
|
|
|
if (cx < 0 || cx >= m_width || cy < 0 || cy >= m_height || cz < 0 || cz >= m_depth)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ServerChunk *chunk = m_chunks.at(cx + cy * m_width + cz * m_width * m_height).get();
|
|
|
|
if (chunk)
|
|
|
|
chunk->setData(x & (CHUNK_WIDTH - 1), y & (CHUNK_HEIGHT - 1), z & (CHUNK_DEPTH - 1), id);
|
|
|
|
}
|
|
|
|
|