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