[ServerWorld] Now using chunk references instead of chunk pointers.

This commit is contained in:
Quentin Bazin 2020-03-10 17:30:22 +01:00
parent 11c5cb92a2
commit 7ca62a6d98
6 changed files with 126 additions and 39 deletions

1
.gitignore vendored
View File

@ -55,6 +55,7 @@ server/openminer_server
*.dll
# Misc
*.dat
*.zip
test_atlas.png
config.lua

View File

@ -40,6 +40,8 @@ class Dimension : public ISerializable {
Dimension(u16 id, const std::string &stringID, const std::string &name)
: m_id(id), m_stringID(stringID), m_name(name) {}
u16 id() const { return m_id; }
void addBiome(const std::string &biome) { m_biomes.emplace_back(biome); }
void serialize(sf::Packet &packet) const override;

View File

@ -45,7 +45,7 @@ void ServerWorld::update() {
if (it.second->isInitialized() && !it.second->isSent()) {
for (auto &client : m_server->server().info().clients())
sendChunkData(client, it.second.get());
sendChunkData(client, *it.second.get());
// DEBUG("Chunk updated at", it.second->x(), it.second->y(), it.second->z());
}
@ -53,14 +53,14 @@ void ServerWorld::update() {
}
}
void ServerWorld::createChunkNeighbours(ServerChunk *chunk) {
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() - 1, chunk->z()},
{chunk->x(), chunk->y() + 1, chunk->z()},
{chunk->x(), chunk->y(), chunk->z() - 1},
{chunk->x(), chunk->y(), chunk->z() + 1},
{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},
};
for (u8 i = 0 ; i < 6 ; ++i) {
@ -68,8 +68,8 @@ void ServerWorld::createChunkNeighbours(ServerChunk *chunk) {
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);
chunk.setSurroundingChunk(i, neighbour);
neighbour->setSurroundingChunk((i % 2 == 0) ? i + 1 : i - 1, &chunk);
continue;
}
@ -93,26 +93,26 @@ void ServerWorld::createChunkNeighbours(ServerChunk *chunk) {
neighbour = it.first->second.get();
// Assign surrounding chunk pointers
chunk->setSurroundingChunk(i, neighbour);
neighbour->setSurroundingChunk((i % 2 == 0) ? i + 1 : i - 1, chunk);
chunk.setSurroundingChunk(i, neighbour);
neighbour->setSurroundingChunk((i % 2 == 0) ? i + 1 : i - 1, &chunk);
}
}
void ServerWorld::sendChunkData(const ClientInfo &client, ServerChunk *chunk) {
void ServerWorld::sendChunkData(const ClientInfo &client, ServerChunk &chunk) {
sf::Packet packet;
packet << Network::Command::ChunkData;
packet << chunk->x() << chunk->y() << chunk->z();
packet << chunk.x() << chunk.y() << chunk.z();
for (u16 z = 0 ; z < CHUNK_HEIGHT ; ++z) {
for (u16 y = 0 ; y < CHUNK_DEPTH ; ++y) {
for (u16 x = 0 ; x < CHUNK_WIDTH ; ++x) {
packet << chunk->data()[z][y][x];
packet << chunk->lightmap().getLightData(x, y, z);
packet << chunk.data()[z][y][x];
packet << chunk.lightmap().getLightData(x, y, z);
BlockData *blockData = chunk->getBlockData(x, y, z);
BlockData *blockData = chunk.getBlockData(x, y, z);
if (blockData) {
s32 globalX = x + chunk->x() * CHUNK_WIDTH;
s32 globalY = y + chunk->y() * CHUNK_DEPTH;
s32 globalZ = z + chunk->z() * CHUNK_HEIGHT;
s32 globalX = x + chunk.x() * CHUNK_WIDTH;
s32 globalY = y + chunk.y() * CHUNK_DEPTH;
s32 globalZ = z + chunk.z() * CHUNK_HEIGHT;
m_server->sendBlockDataUpdate(globalX, globalY, globalZ, blockData, &client);
m_server->sendBlockInvUpdate(globalX, globalY, globalZ, blockData->inventory, &client);
@ -122,32 +122,38 @@ void ServerWorld::sendChunkData(const ClientInfo &client, ServerChunk *chunk) {
}
client.tcpSocket->send(packet);
chunk->setSent(true);
chunk->setChanged(false);
chunk.setSent(true);
chunk.setChanged(false);
// std::cout << "Chunk at (" << chunk->x() << ", " << chunk->y() << ", " << chunk->z() << ") sent to client" << std::endl;
}
void ServerWorld::sendRequestedData(ClientInfo &client, int cx, int cy, int cz) {
ServerChunk &chunk = createChunk(cx, cy, cz);
// 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);
}
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();
}
// 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);
return *chunk;
}
Chunk *ServerWorld::getChunk(int cx, int cy, int cz) const {

View File

@ -47,14 +47,18 @@ class ServerWorld : public World {
void update();
void createChunkNeighbours(ServerChunk *chunk);
void sendChunkData(const ClientInfo &client, ServerChunk *chunk);
void createChunkNeighbours(ServerChunk &chunk);
void sendChunkData(const ClientInfo &client, ServerChunk &chunk);
void sendRequestedData(ClientInfo &client, s32 cx, s32 cy, s32 cz);
ServerChunk &createChunk(s32 cx, s32 cy, s32 cz);
Chunk *getChunk(int cx, int cy, int cz) const override;
const Dimension &dimension() const { return m_dimension; }
const ChunkMap &chunks() const { return m_chunks; }
TerrainGenerator &terrainGenerator() { return m_terrainGenerator; }
void setServer(ServerCommandHandler *server) { m_server = server; }

View File

@ -39,3 +39,78 @@ void WorldController::update() {
it.update();
}
// #include <fstream>
//
// void WorldController::load() {
// std::cout << "Loading..." << std::endl;
//
// std::ifstream file("save.dat", std::ofstream::binary);
//
// if (file.is_open()) {
// file.seekg(0, file.end);
// int length = file.tellg();
// file.seekg(0, file.beg);
//
// char *buffer = new char[length];
// file.read(buffer, length);
//
// sf::Packet save;
// save.append(buffer, length);
//
// delete[] buffer;
//
// while (!save.endOfPacket()) {
// for (auto &world : m_worldList) {
// int cx, cy, cz;
// save >> cx >> cy >> cz;
//
// ServerChunk &chunk = world.createChunk(cx, cy, cz);
// for (u8 z = 0 ; z < Chunk::height ; ++z) {
// for (u8 y = 0 ; y < Chunk::depth ; ++y) {
// for (u8 x = 0 ; x < Chunk::width ; ++x) {
// u32 data;
// u8 light;
// save >> data >> light;
//
// chunk.setBlockRaw(x, y, z, data & 0xffff);
// chunk.setData(x, y, z, data >> 16);
// chunk.lightmap().setLightData(x, y, z, light);
// }
// }
// }
//
// chunk.setInitialized(true);
// chunk.setSent(false);
// }
// }
// }
// }
//
// void WorldController::save() {
// std::cout << "Saving..." << std::endl;
//
// std::ofstream file("save.dat", std::ofstream::binary | std::ofstream::trunc);
//
// sf::Packet save;
// for (auto &world : m_worldList) {
// for (auto &chunk : world.chunks()) {
// if (!chunk.second->isInitialized()) continue;
//
// const gk::Vector3i &chunkpos = chunk.first;
// const Chunk::DataArray &data = chunk.second->data();
// save << chunkpos.x << chunkpos.y << chunkpos.z;
//
// for (u8 z = 0 ; z < Chunk::height ; ++z) {
// for (u8 y = 0 ; y < Chunk::depth ; ++y) {
// for (u8 x = 0 ; x < Chunk::width ; ++x) {
// save << u32(data[z][y][x])
// << u8(chunk.second->lightmap().getLightData(x, y, z));
// }
// }
// }
// }
// }
//
// file.write((const char *)save.getData(), save.getDataSize());
// }

View File

@ -35,8 +35,7 @@ class Registry;
class WorldController {
public:
WorldController(Registry &registry)
: m_registry(registry) {}
WorldController(Registry &registry) : m_registry(registry) {}
void init();