OpenMiner/server/source/world/ServerWorld.cpp

166 lines
5.0 KiB
C++
Raw Normal View History

/*
* =====================================================================================
*
* OpenMiner
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* =====================================================================================
*/
2020-01-02 17:35:28 +09:00
#include <gk/core/GameClock.hpp>
#include "Config.hpp"
#include "Network.hpp"
#include "Server.hpp"
#include "ServerPlayer.hpp"
#include "ServerWorld.hpp"
ServerWorld::ServerWorld() {
}
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;
for (auto &it : m_chunks) {
it.second->tick(players, *this, server);
if (it.second->areAllNeighboursLoaded())
it.second->updateLights();
2020-01-17 19:21:38 +09:00
if (it.second->isInitialized() && !it.second->isSent()) {
2020-01-17 19:21:38 +09:00
for (auto &client : server.info().clients())
sendChunkData(client, it.second.get());
// DEBUG("Chunk updated at", it.second->x(), it.second->y(), it.second->z());
2020-01-17 19:21:38 +09:00
}
}
}
}
void ServerWorld::createChunkNeighbours(ServerChunk *chunk) {
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) {
// Check if this neighbour already exists, if yes then skip it
ServerChunk *neighbour = (ServerChunk *)getChunk(surroundingChunks[i].x, surroundingChunks[i].y, surroundingChunks[i].z);
if (neighbour) {
// Assign surrounding chunk pointers
chunk->setSurroundingChunk(i, neighbour);
neighbour->setSurroundingChunk((i % 2 == 0) ? i + 1 : i - 1, chunk);
continue;
}
2020-01-17 14:39:29 +09:00
// 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,
2020-02-07 23:15:44 +09:00
surroundingChunks[i].z,
*this
}
);
// Get the created neighbour
neighbour = it.first->second.get();
2020-01-17 14:39:29 +09:00
// Assign surrounding chunk pointers
chunk->setSurroundingChunk(i, neighbour);
neighbour->setSurroundingChunk((i % 2 == 0) ? i + 1 : i - 1, chunk);
}
}
void ServerWorld::sendChunkData(const Client &client, ServerChunk *chunk) {
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) {
for (u16 y = 0 ; y < CHUNK_HEIGHT ; ++y) {
2020-01-17 14:39:29 +09:00
for (u16 z = 0 ; z < CHUNK_DEPTH ; ++z) {
packet << u16(chunk->data()[x][y][z]);
packet << chunk->lightmap().getLightData(x, y, z);
2020-02-07 23:15:44 +09:00
BlockData *blockData = chunk->getBlockData(x, y, z);
if (blockData) {
s32 globalX = x + chunk->x() * CHUNK_WIDTH;
s32 globalY = y + chunk->y() * CHUNK_HEIGHT;
s32 globalZ = z + chunk->z() * CHUNK_DEPTH;
2020-02-07 23:15:44 +09:00
sf::Packet packet1;
packet1 << Network::Command::BlockDataUpdate << globalX << globalY << globalZ;
packet1 << blockData->meta << blockData->useAltTiles;
2020-02-07 23:15:44 +09:00
client.tcpSocket->send(packet1);
sf::Packet packet2;
packet2 << Network::Command::BlockInvUpdate;
packet2 << globalX << globalY << globalZ;
2020-02-07 23:15:44 +09:00
packet2 << blockData->inventory;
client.tcpSocket->send(packet2);
}
}
}
}
client.tcpSocket->send(packet);
chunk->setSent(true);
2020-01-17 14:39:29 +09:00
// std::cout << "Chunk at (" << chunk->x() << ", " << chunk->y() << ", " << chunk->z() << ") sent to client" << std::endl;
}
void ServerWorld::sendRequestedData(Client &client, int cx, int cy, int cz) {
ServerChunk *chunk = (ServerChunk *)getChunk(cx, cy, cz);
if (!chunk) {
2020-02-07 23:15:44 +09:00
auto it = m_chunks.emplace(gk::Vector3i{cx, cy, cz}, new ServerChunk(cx, cy, cz, *this));
chunk = it.first->second.get();
}
2020-01-21 12:07:44 +09:00
// Create our neighbours so that we can generate and process lights correctly
createChunkNeighbours(chunk);
// Generate our chunk
if (!chunk->isInitialized()) {
m_terrainGenerator.generate(*chunk);
chunk->setInitialized(true);
}
chunk->updateLights();
sendChunkData(client, chunk);
}
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())
return nullptr;
2020-01-16 01:37:49 +09:00
return it->second.get();
}