aboutsummaryrefslogtreecommitdiff
path: root/src/server/world.hh
blob: 134f63bfb17d5de6f65582434e0186828d1e77cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef SERVER_WORLD_HH_
#define SERVER_WORLD_HH_

#include <cstdint>

#include "server/database.hh"
#include "server/shared.hh"
#include "shared/math.hh"
#include "shared/net/net.hh"
#include "shared/net/proto.hh"
#include "shared/player.hh"
#include "shared/world.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;
    }
    bool get_should_update() noexcept { return this->should_update; }

private:
    proto::packet make_chunk_packet() const noexcept;

public:
    // Attempt to read the file using protobuf, otherwise create a new chunk.
    // chunk(const chunk&) = delete;
    chunk(const uint64_t& seed, const shared::math::coords& coords) noexcept;
    ~chunk() noexcept;

    // Update the chunk_packet associated with the chunk if necessary.
    void update() noexcept {
        if (!this->should_update) {
            return;
        }
        this->packet = make_chunk_packet();
        this->should_update = false;
    }
    // calling .write before the destrutor will not result in a double write
    void write() noexcept {
        if (!this->should_write) {
            return;
        }
        server::database::write_chunk(this->pos, this->packet.chunk_packet());
        this->should_write = false;
    }
};

} // namespace world
} // namespace server

#endif