Skip to content

Commit

Permalink
[WorldController] '/save' only save modified chunks now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 18, 2020
1 parent 276efe5 commit b1ed51f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions source/common/world/Chunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ class Chunk : public gk::NonCopyable {

void setBlockRaw(int x, int y, int z, u16 block);

virtual void onBlockPlaced(int, int, int, const Block &) const {}
virtual void onBlockDestroyed(int, int, int, const Block &) const {}
virtual void onBlockPlaced(int, int, int, const Block &) {}
virtual void onBlockDestroyed(int, int, int, const Block &) {}

BlockData *getBlockData(int x, int y, int z) const;
BlockData *addBlockData(int x, int y, int z, int inventoryWidth = 0, int inventoryHeight = 0);
Expand Down
8 changes: 6 additions & 2 deletions source/server/world/ServerChunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ void ServerChunk::updateLights() {
}
}

void ServerChunk::onBlockPlaced(int x, int y, int z, const Block &block) const {
void ServerChunk::onBlockPlaced(int x, int y, int z, const Block &block) {
const ServerBlock &serverBlock = (ServerBlock &)block;
serverBlock.onBlockPlaced(glm::ivec3{x + m_x * CHUNK_WIDTH, y + m_y * CHUNK_DEPTH, z + m_z * CHUNK_HEIGHT}, m_world);

m_hasBeenModified = true;
}

void ServerChunk::onBlockDestroyed(int x, int y, int z, const Block &block) const {
void ServerChunk::onBlockDestroyed(int x, int y, int z, const Block &block) {
const ServerBlock &serverBlock = (ServerBlock &)block;
serverBlock.onBlockDestroyed(glm::ivec3{x + m_x * CHUNK_WIDTH, y + m_y * CHUNK_DEPTH, z + m_z * CHUNK_HEIGHT}, m_world);

m_hasBeenModified = true;
}

void ServerChunk::tick(World &world, ServerCommandHandler &server) {
Expand Down
7 changes: 5 additions & 2 deletions source/server/world/ServerChunk.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,19 @@ class ServerChunk : public Chunk {

void updateLights();

void onBlockPlaced(int x, int y, int z, const Block &block) const;
void onBlockDestroyed(int x, int y, int z, const Block &block) const;
void onBlockPlaced(int x, int y, int z, const Block &block) override;
void onBlockDestroyed(int x, int y, int z, const Block &block) override;

void tick(World &world, ServerCommandHandler &server);

bool isSent() const { return m_isSent; }
void setSent(bool isSent) { m_isSent = isSent; }

bool hasBeenModified() const { return m_hasBeenModified; }

private:
std::atomic_bool m_isSent{false};
std::atomic_bool m_hasBeenModified{false};
};

#endif // SERVERCHUNK_HPP_
10 changes: 5 additions & 5 deletions source/server/world/WorldController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ void WorldController::save(const std::string &name) {
for (auto &world : m_worldList) {
Network::Packet chunks;
unsigned int chunkCount = 0;
for (auto &chunk : world.chunks()) {
if (!chunk.second->isInitialized()) continue;
for (auto &it : world.chunks()) {
if (!it.second->isInitialized() || !it.second->hasBeenModified()) continue;

const gk::Vector3i &chunkpos = chunk.first;
const Chunk::DataArray &data = chunk.second->data();
const gk::Vector3i &chunkpos = it.first;
const Chunk::DataArray &data = it.second->data();
chunks << 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) {
chunks << u32(data[z][y][x])
<< u8(chunk.second->lightmap().getLightData(x, y, z));
<< u8(it.second->lightmap().getLightData(x, y, z));
}
}
}
Expand Down

0 comments on commit b1ed51f

Please sign in to comment.