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