From e4483eca01b48b943cd0461e24a74ae1a3139ed4 Mon Sep 17 00:00:00 2001 From: Nicolas James Date: Wed, 12 Feb 2025 21:57:46 +1100 Subject: Update to most recent version (old initial commit) --- src/server/world/chunk.cc | 26 ++++++++++++++++++++++++++ src/server/world/chunk.hh | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/server/world/chunk.cc create mode 100644 src/server/world/chunk.hh (limited to 'src/server/world') diff --git a/src/server/world/chunk.cc b/src/server/world/chunk.cc new file mode 100644 index 0000000..80a3da9 --- /dev/null +++ b/src/server/world/chunk.cc @@ -0,0 +1,26 @@ +#include "server/world/chunk.hh" + +namespace server { +namespace world { + +void chunk::update() noexcept { + if (!this->should_update) { + return; + } + this->packet.clear_chunk_packet(); + this->pack(packet.mutable_chunk_packet()); + this->should_update = false; +} + +void chunk::write() noexcept { + if (!this->should_write) { + return; + } + server::database::write_chunk(this->pos, this->packet.chunk_packet()); + this->should_write = false; +} + +chunk::~chunk() noexcept { this->write(); } + +} // namespace world +} // namespace server diff --git a/src/server/world/chunk.hh b/src/server/world/chunk.hh new file mode 100644 index 0000000..b8fc73c --- /dev/null +++ b/src/server/world/chunk.hh @@ -0,0 +1,46 @@ +#ifndef SERVER_WORLD_CHUNK_HH_ +#define SERVER_WORLD_CHUNK_HH_ + +#include + +#include "server/database.hh" +#include "server/shared.hh" +#include "shared/entity/player.hh" +#include "shared/math/math.hh" +#include "shared/net/net.hh" +#include "shared/net/proto.hh" +#include "shared/world/chunk.hh" + +namespace server { +namespace world { + +class chunk : public shared::world::chunk { +private: + bool should_write = false; + bool should_update = true; + +public: + proto::packet packet; // Packet ready for sending, updated in update(). + void arm_should_update() noexcept { + this->should_update = this->should_write = true; + } + +public: + template + chunk(Args&&... args) noexcept + : shared::world::chunk(std::forward(args)...) { + this->pack(packet.mutable_chunk_packet()); + } + virtual ~chunk() noexcept; + +public: + // Update the chunk_packet associated with the chunk if necessary. + void update() noexcept; + // calling .write before the destrutor will not result in a double write + void write() noexcept; +}; + +} // namespace world +} // namespace server + +#endif -- cgit v1.2.3