]> git.localhorst.tv Git - blank.git/blob - src/chunk.hpp
noexcept all the things
[blank.git] / src / chunk.hpp
1 #ifndef BLANK_CHUNK_HPP_
2 #define BLANK_CHUNK_HPP_
3
4 #include "block.hpp"
5 #include "geometry.hpp"
6 #include "model.hpp"
7
8 #include <list>
9 #include <vector>
10 #include <glm/glm.hpp>
11 #include <glm/gtx/transform.hpp>
12
13
14 namespace blank {
15
16 /// cube of size 16 (256 tiles, 4096 blocks)
17 class Chunk {
18
19 public:
20         using Pos = glm::tvec3<int>;
21
22 public:
23         explicit Chunk(const BlockTypeRegistry &) noexcept;
24
25         Chunk(Chunk &&) noexcept;
26         Chunk &operator =(Chunk &&) noexcept;
27
28         static constexpr int Width() { return 16; }
29         static constexpr int Height() { return 16; }
30         static constexpr int Depth() { return 16; }
31         static Pos Extent() noexcept { return { Width(), Height(), Depth() }; }
32         static constexpr int Size() { return Width() * Height() * Depth(); }
33
34         static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
35
36         static constexpr bool InBounds(const Block::Pos &pos) noexcept {
37                 return
38                         pos.x >= 0 && pos.x < Width() &&
39                         pos.y >= 0 && pos.y < Height() &&
40                         pos.z >= 0 && pos.z < Depth();
41         }
42         static constexpr bool InBounds(const Pos &pos) noexcept {
43                 return
44                         pos.x >= 0 && pos.x < Width() &&
45                         pos.y >= 0 && pos.y < Height() &&
46                         pos.z >= 0 && pos.z < Depth();
47         }
48         static constexpr int ToIndex(const Pos &pos) noexcept {
49                 return pos.x + pos.y * Width() + pos.z * Width() * Height();
50         }
51         static constexpr bool InBounds(int idx) noexcept {
52                 return idx >= 0 && idx < Size();
53         }
54         static Block::Pos ToCoords(int idx) noexcept {
55                 return Block::Pos(
56                         0.5f + (idx % Width()),
57                         0.5f + ((idx / Width()) % Height()),
58                         0.5f + (idx / (Width() * Height()))
59                 );
60         }
61         static Pos ToPos(int idx) noexcept {
62                 return Pos(
63                         (idx % Width()),
64                         ((idx / Width()) % Height()),
65                         (idx / (Width() * Height()))
66                 );
67         }
68         glm::mat4 ToTransform(int idx) const noexcept;
69
70         static constexpr bool IsBorder(int idx) noexcept {
71                 return
72                         idx < Width() * Height() ||                    // low Z plane
73                         idx % Width() == 0 ||                          // low X plane
74                         (idx / (Width() * Height())) == Depth() - 1 || // high Z plane
75                         idx % Width() == Width() - 1 ||                // high X plane
76                         (idx / Width()) % Height() == 0 ||             // low Y plane
77                         (idx / Width()) % Height() == Height() - 1;    // high Y plane
78         }
79
80         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
81         bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
82         bool IsSurface(const Pos &pos) const noexcept;
83
84         void SetNeighbor(Chunk &) noexcept;
85         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
86         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
87         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
88         void ClearNeighbors() noexcept;
89         void Unlink() noexcept;
90         void Relink() noexcept;
91
92         // check which faces of a block at given index are obstructed (and therefore invisible)
93         Block::FaceSet Obstructed(int idx) const noexcept;
94
95         void Invalidate() noexcept { dirty = true; }
96
97         void SetBlock(int index, const Block &) noexcept;
98         void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
99         void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
100
101         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
102         const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
103         const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
104
105         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
106
107         void SetLight(int index, int level) noexcept;
108         void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
109         void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
110
111         int GetLight(int index) const noexcept;
112         int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
113         int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
114
115         float GetVertexLight(int index, const BlockModel::Position &, const Model::Normal &) const noexcept;
116
117         bool Intersection(
118                 const Ray &ray,
119                 const glm::mat4 &M,
120                 float &dist
121         ) const noexcept {
122                 return blank::Intersection(ray, Bounds(), M, &dist);
123         }
124
125         bool Intersection(
126                 const Ray &,
127                 const glm::mat4 &M,
128                 int &blkid,
129                 float &dist,
130                 glm::vec3 &normal) const noexcept;
131
132         void Position(const Pos &pos) noexcept { position = pos; }
133         const Pos &Position() const noexcept { return position; }
134         glm::mat4 Transform(const Pos &offset) const noexcept {
135                 return glm::translate((position - offset) * Extent());
136         }
137
138         void CheckUpdate() noexcept;
139         void Draw() noexcept;
140
141 private:
142         void Update() noexcept;
143
144 private:
145         const BlockTypeRegistry *types;
146         Chunk *neighbor[Block::FACE_COUNT];
147         Block blocks[16 * 16 * 16];
148         unsigned char light[16 * 16 * 16];
149         BlockModel model;
150         Pos position;
151         bool dirty;
152
153 };
154
155
156 class BlockLookup {
157
158 public:
159         // resolve chunk/position from oob coordinates
160         BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept;
161
162         // resolve chunk/position from ib coordinates and direction
163         BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir) noexcept;
164
165         // check if lookup was successful
166         operator bool() const { return chunk; }
167
168         // only valid if lookup was successful
169         Chunk &GetChunk() const noexcept { return *chunk; }
170         const Chunk::Pos &GetBlockPos() const noexcept { return pos; }
171         const Block &GetBlock() const noexcept { return GetChunk().BlockAt(GetBlockPos()); }
172         const BlockType &GetType() const noexcept { return GetChunk().Type(GetBlock()); }
173         int GetLight() const noexcept { return GetChunk().GetLight(GetBlockPos()); }
174
175 private:
176         Chunk *chunk;
177         Chunk::Pos pos;
178
179 };
180
181
182 class Generator;
183
184 class ChunkLoader {
185
186 public:
187         struct Config {
188                 int load_dist = 6;
189                 int unload_dist = 8;
190         };
191
192         ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept;
193
194         void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
195         void GenerateSurrounding(const Chunk::Pos &);
196
197         std::list<Chunk> &Loaded() noexcept { return loaded; }
198
199         Chunk *Loaded(const Chunk::Pos &) noexcept;
200         bool Queued(const Chunk::Pos &) noexcept;
201         bool Known(const Chunk::Pos &) noexcept;
202         Chunk &ForceLoad(const Chunk::Pos &);
203
204         void Rebase(const Chunk::Pos &);
205         void Update();
206
207 private:
208         Chunk &Generate(const Chunk::Pos &pos);
209         void Insert(Chunk &) noexcept;
210         void Remove(Chunk &) noexcept;
211
212 private:
213         Chunk::Pos base;
214
215         const BlockTypeRegistry &reg;
216         const Generator &gen;
217
218         std::list<Chunk> loaded;
219         std::list<Chunk::Pos> to_generate;
220         std::list<Chunk> to_free;
221
222         int load_dist;
223         int unload_dist;
224
225 };
226
227 }
228
229 #endif