2019-01-12 14:18:36 +01:00
|
|
|
/*
|
|
|
|
* =====================================================================================
|
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* OpenMiner
|
2020-02-25 01:42:10 +09:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* Copyright (C) 2018-2020 Unarelith, Quentin Bazin <openminer@unarelith.net>
|
2020-02-25 01:42:10 +09:00
|
|
|
* Copyright (C) 2019-2020 the OpenMiner contributors (see CONTRIBUTORS.md)
|
|
|
|
*
|
|
|
|
* This file is part of OpenMiner.
|
2019-01-12 14:18:36 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is free software; you can redistribute it and/or
|
2020-02-08 18:34:26 +09:00
|
|
|
* 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.
|
2019-01-12 14:18:36 +01:00
|
|
|
*
|
2020-02-25 01:42:10 +09:00
|
|
|
* OpenMiner is distributed in the hope that it will be useful,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 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.
|
2019-01-12 14:18:36 +01:00
|
|
|
*
|
2020-02-08 18:34:26 +09:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2020-02-25 01:42:10 +09:00
|
|
|
* along with OpenMiner; if not, write to the Free Software Foundation, Inc.,
|
2020-02-08 18:34:26 +09:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2019-01-12 14:18:36 +01:00
|
|
|
*
|
|
|
|
* =====================================================================================
|
|
|
|
*/
|
2020-01-02 17:35:28 +09:00
|
|
|
#include <gk/core/GameClock.hpp>
|
|
|
|
|
2020-04-29 01:55:14 +02:00
|
|
|
#include "Dimension.hpp"
|
2020-02-15 22:48:56 +09:00
|
|
|
#include "EngineConfig.hpp"
|
2020-04-29 01:55:14 +02:00
|
|
|
#include "ItemDropFactory.hpp"
|
2019-01-12 14:18:36 +01:00
|
|
|
#include "Network.hpp"
|
2019-01-21 21:51:18 +01:00
|
|
|
#include "Server.hpp"
|
2020-02-11 15:00:03 +09:00
|
|
|
#include "ServerCommandHandler.hpp"
|
2020-04-29 01:55:14 +02:00
|
|
|
#include "ServerConfig.hpp"
|
2019-01-26 10:05:37 +01:00
|
|
|
#include "ServerPlayer.hpp"
|
2019-01-12 14:18:36 +01:00
|
|
|
#include "ServerWorld.hpp"
|
|
|
|
|
2020-03-08 15:28:46 +01:00
|
|
|
void ServerWorld::update() {
|
2020-03-15 19:48:18 +01:00
|
|
|
if (m_lastTick < m_clock.getTicks() / 50) {
|
|
|
|
m_lastTick = m_clock.getTicks() / 50;
|
2020-01-17 19:21:38 +09:00
|
|
|
|
|
|
|
for (auto &it : m_chunks) {
|
2020-03-08 15:28:46 +01:00
|
|
|
it.second->tick(*this, *m_server);
|
2020-01-17 19:21:38 +09:00
|
|
|
|
2020-01-21 13:10:33 +09:00
|
|
|
if (it.second->areAllNeighboursLoaded())
|
2020-01-19 18:22:50 +09:00
|
|
|
it.second->updateLights();
|
2020-01-17 19:21:38 +09:00
|
|
|
|
2020-01-20 13:40:07 +09:00
|
|
|
if (it.second->isInitialized() && !it.second->isSent()) {
|
2020-02-11 15:00:03 +09:00
|
|
|
for (auto &client : m_server->server().info().clients())
|
2020-03-10 17:30:22 +01:00
|
|
|
sendChunkData(client, *it.second.get());
|
2020-01-17 19:21:38 +09:00
|
|
|
|
2020-04-03 07:27:57 +02:00
|
|
|
// gkDebug() << "Chunk updated at" << it.second->x() << it.second->y() << it.second->z();
|
2020-01-17 19:21:38 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-05 18:58:25 +02:00
|
|
|
|
|
|
|
m_scene.update();
|
2019-01-12 18:53:25 +01:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:30:22 +01:00
|
|
|
void ServerWorld::createChunkNeighbours(ServerChunk &chunk) {
|
2020-01-20 13:40:07 +09:00
|
|
|
gk::Vector3i surroundingChunks[6] = {
|
2020-03-10 17:30:22 +01:00
|
|
|
{chunk.x() - 1, chunk.y(), chunk.z()},
|
|
|
|
{chunk.x() + 1, chunk.y(), chunk.z()},
|
|
|
|
{chunk.x(), chunk.y() - 1, chunk.z()},
|
|
|
|
{chunk.x(), chunk.y() + 1, chunk.z()},
|
|
|
|
{chunk.x(), chunk.y(), chunk.z() - 1},
|
|
|
|
{chunk.x(), chunk.y(), chunk.z() + 1},
|
2020-01-20 13:40:07 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
2020-01-16 10:07:01 +09:00
|
|
|
// Assign surrounding chunk pointers
|
2020-03-10 17:30:22 +01:00
|
|
|
chunk.setSurroundingChunk(i, neighbour);
|
|
|
|
neighbour->setSurroundingChunk((i % 2 == 0) ? i + 1 : i - 1, &chunk);
|
2020-01-16 10:07:01 +09:00
|
|
|
|
2020-01-20 13:40:07 +09:00
|
|
|
continue;
|
2020-01-16 10:07:01 +09:00
|
|
|
}
|
2020-01-17 14:39:29 +09:00
|
|
|
|
2020-01-20 13:40:07 +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
|
2020-01-20 13:40:07 +09:00
|
|
|
}
|
|
|
|
);
|
2020-01-19 18:22:50 +09:00
|
|
|
|
2020-01-20 13:40:07 +09:00
|
|
|
// Get the created neighbour
|
|
|
|
neighbour = it.first->second.get();
|
2020-01-17 14:39:29 +09:00
|
|
|
|
2020-01-20 13:40:07 +09:00
|
|
|
// Assign surrounding chunk pointers
|
2020-03-10 17:30:22 +01:00
|
|
|
chunk.setSurroundingChunk(i, neighbour);
|
|
|
|
neighbour->setSurroundingChunk((i % 2 == 0) ? i + 1 : i - 1, &chunk);
|
2020-01-17 18:32:40 +09:00
|
|
|
}
|
2020-01-16 10:07:01 +09:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:30:22 +01:00
|
|
|
void ServerWorld::sendChunkData(const ClientInfo &client, ServerChunk &chunk) {
|
2019-01-12 14:18:36 +01:00
|
|
|
sf::Packet packet;
|
|
|
|
packet << Network::Command::ChunkData;
|
2020-03-10 17:30:22 +01:00
|
|
|
packet << chunk.x() << chunk.y() << chunk.z();
|
2020-02-20 11:38:30 +01:00
|
|
|
for (u16 z = 0 ; z < CHUNK_HEIGHT ; ++z) {
|
|
|
|
for (u16 y = 0 ; y < CHUNK_DEPTH ; ++y) {
|
|
|
|
for (u16 x = 0 ; x < CHUNK_WIDTH ; ++x) {
|
2020-03-28 15:51:40 +01:00
|
|
|
packet << chunk.data(x, y, z);
|
2020-03-10 17:30:22 +01:00
|
|
|
packet << chunk.lightmap().getLightData(x, y, z);
|
2020-02-07 23:15:44 +09:00
|
|
|
|
2020-03-10 17:30:22 +01:00
|
|
|
BlockData *blockData = chunk.getBlockData(x, y, z);
|
2020-02-07 23:15:44 +09:00
|
|
|
if (blockData) {
|
2020-03-10 17:30:22 +01:00
|
|
|
s32 globalX = x + chunk.x() * CHUNK_WIDTH;
|
|
|
|
s32 globalY = y + chunk.y() * CHUNK_DEPTH;
|
|
|
|
s32 globalZ = z + chunk.z() * CHUNK_HEIGHT;
|
2020-02-08 14:36:22 +09:00
|
|
|
|
2020-02-11 15:00:03 +09:00
|
|
|
m_server->sendBlockDataUpdate(globalX, globalY, globalZ, blockData, &client);
|
|
|
|
m_server->sendBlockInvUpdate(globalX, globalY, globalZ, blockData->inventory, &client);
|
2020-02-07 23:15:44 +09:00
|
|
|
}
|
2019-01-12 16:45:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-01-12 14:18:36 +01:00
|
|
|
|
|
|
|
client.tcpSocket->send(packet);
|
2020-03-10 17:30:22 +01:00
|
|
|
chunk.setSent(true);
|
|
|
|
chunk.setChanged(false);
|
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-03-04 14:38:31 +01:00
|
|
|
void ServerWorld::sendRequestedData(ClientInfo &client, int cx, int cy, int cz) {
|
2020-03-10 17:30:22 +01:00
|
|
|
ServerChunk &chunk = createChunk(cx, cy, cz);
|
2020-01-20 13:40:07 +09:00
|
|
|
|
2020-01-21 12:07:44 +09:00
|
|
|
// Create our neighbours so that we can generate and process lights correctly
|
|
|
|
createChunkNeighbours(chunk);
|
|
|
|
|
2020-02-03 16:57:11 +09:00
|
|
|
// Generate our chunk
|
2020-03-10 17:30:22 +01:00
|
|
|
if (!chunk.isInitialized()) {
|
|
|
|
m_terrainGenerator.generate(chunk);
|
2020-02-03 16:57:11 +09:00
|
|
|
|
2020-03-10 17:30:22 +01:00
|
|
|
chunk.setInitialized(true);
|
2020-02-03 16:57:11 +09:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:30:22 +01:00
|
|
|
chunk.updateLights();
|
2020-01-20 13:40:07 +09:00
|
|
|
|
|
|
|
sendChunkData(client, chunk);
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:55:14 +02:00
|
|
|
void ServerWorld::onBlockDigged(int x, int y, int z, const Block &block, ServerPlayer &player) {
|
|
|
|
if (ServerConfig::useItemDrops) {
|
2020-04-29 19:09:37 +02:00
|
|
|
ItemDropFactory::create(m_scene.registry(), x + 0.5, y + 0.5, z + 0.5, m_dimension.id(), block.getItemDrop().item().stringID(), block.getItemDrop().amount());
|
2020-04-29 01:55:14 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
player.inventory().addStack(block.getItemDrop().item().stringID(), block.getItemDrop().amount());
|
|
|
|
m_server->sendPlayerInvUpdate(player.clientID(), &player.client());
|
|
|
|
}
|
2020-04-05 18:58:25 +02:00
|
|
|
}
|
|
|
|
|
2020-03-10 17:30:22 +01:00
|
|
|
ServerChunk &ServerWorld::createChunk(s32 cx, s32 cy, s32 cz) {
|
|
|
|
ServerChunk *chunk = (ServerChunk *)getChunk(cx, cy, cz);
|
|
|
|
if (!chunk) {
|
|
|
|
auto it = m_chunks.emplace(gk::Vector3i{cx, cy, cz}, new ServerChunk(cx, cy, cz, *this));
|
|
|
|
chunk = it.first->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return *chunk;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|