]> git.localhorst.tv Git - blank.git/blob - src/world/ChunkIndex.hpp
transmit chunks from server to client
[blank.git] / src / world / ChunkIndex.hpp
1 #ifndef BLANK_WORLD_CHUNKINDEX_HPP_
2 #define BLANK_WORLD_CHUNKINDEX_HPP_
3
4 #include "Chunk.hpp"
5
6 #include <vector>
7
8
9 namespace blank {
10
11 class ChunkStore;
12
13 class ChunkIndex {
14
15 public:
16         ChunkIndex(ChunkStore &, const Chunk::Pos &base, int extent);
17         ~ChunkIndex();
18
19         ChunkIndex(const ChunkIndex &) = delete;
20         ChunkIndex &operator =(const ChunkIndex &) = delete;
21
22 public:
23         bool InRange(const Chunk::Pos &) const noexcept;
24         int IndexOf(const Chunk::Pos &) const noexcept;
25         Chunk::Pos PositionOf(int) const noexcept;
26         /// returns nullptr if given position is out of range or the chunk
27         /// is not loaded, so also works as a "has" function
28         Chunk *Get(const Chunk::Pos &) noexcept;
29         const Chunk *Get(const Chunk::Pos &) const noexcept;
30         Chunk *operator [](int i) noexcept { return chunks[i]; }
31         const Chunk *operator [](int i) const noexcept { return chunks[i]; }
32
33         int Extent() const noexcept { return extent; }
34
35         Chunk::Pos CoordsBegin() const noexcept { return base - Chunk::Pos(extent); }
36         Chunk::Pos CoordsEnd() const noexcept { return base + Chunk::Pos(extent + 1); }
37
38         void Register(Chunk &) noexcept;
39
40         int TotalChunks() const noexcept { return total_length; }
41         int IndexedChunks() const noexcept { return total_indexed; }
42         int MissingChunks() const noexcept { return total_length - total_indexed; }
43
44         Chunk::Pos NextMissing() noexcept;
45
46         const Chunk::Pos &Base() const noexcept { return base; }
47         void Rebase(const Chunk::Pos &);
48
49 private:
50         int GetCol(int) const noexcept;
51
52         void Shift(Block::Face);
53
54         void Clear() noexcept;
55         void Scan() noexcept;
56
57         void Set(int index, Chunk &) noexcept;
58         void Unset(int index) noexcept;
59
60 private:
61         ChunkStore &store;
62         Chunk::Pos base;
63         int extent;
64         int side_length;
65         int total_length;
66         int total_indexed;
67         int last_missing;
68         glm::ivec3 stride;
69         std::vector<Chunk *> chunks;
70
71 };
72
73 }
74
75 #endif