]> git.localhorst.tv Git - blank.git/blob - src/world/ChunkIndex.hpp
indexed iteration in ray/world collision tests
[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         // raw iteration access, may contain nullptrs
51         std::vector<Chunk *>::const_iterator begin() const noexcept { return chunks.begin(); }
52         std::vector<Chunk *>::const_iterator end() const noexcept { return chunks.end(); }
53
54         ExactLocation::Coarse CoordsBegin() const noexcept { return base - ExactLocation::Coarse(extent); }
55         ExactLocation::Coarse CoordsEnd() const noexcept { return base + ExactLocation::Coarse(extent + 1); }
56
57         void Register(Chunk &) noexcept;
58
59         int TotalChunks() const noexcept { return total_length; }
60         int IndexedChunks() const noexcept { return total_indexed; }
61         int MissingChunks() const noexcept { return total_length - total_indexed; }
62
63         ExactLocation::Coarse NextMissing() noexcept;
64
65         const ExactLocation::Coarse &Base() const noexcept { return base; }
66         void Rebase(const ExactLocation::Coarse &);
67
68 private:
69         int GetCol(int) const noexcept;
70
71         void Shift(Block::Face);
72
73         void Clear() noexcept;
74         void Scan() noexcept;
75
76         void Set(int index, Chunk &) noexcept;
77         void Unset(int index) noexcept;
78
79 private:
80         ChunkStore &store;
81         ExactLocation::Coarse base;
82         int extent;
83         int side_length;
84         int total_length;
85         int total_indexed;
86         int last_missing;
87         glm::ivec3 stride;
88         std::vector<Chunk *> chunks;
89
90 };
91
92 }
93
94 #endif