]> git.localhorst.tv Git - blank.git/blob - src/chunk.hpp
d86f4170b8deff7e22d346299170bfece8860fc8
[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 = 16;
29         static constexpr int height = 16;
30         static constexpr int depth = 16;
31         static Pos Extent() noexcept { return { width, height, depth }; }
32         static constexpr int size = 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 Block::Pos ToCoords(const Pos &pos) noexcept {
62                 return Block::Pos(pos) + 0.5f;
63         }
64         static Pos ToPos(int idx) noexcept {
65                 return Pos(
66                         (idx % width),
67                         ((idx / width) % height),
68                         (idx / (width * height))
69                 );
70         }
71         glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
72
73         static constexpr bool IsBorder(int idx) noexcept {
74                 return
75                         idx < width * height ||                    // low Z plane
76                         idx % width == 0 ||                          // low X plane
77                         (idx / (width * height)) == depth - 1 || // high Z plane
78                         idx % width == width - 1 ||                // high X plane
79                         (idx / width) % height == 0 ||             // low Y plane
80                         (idx / width) % height == height - 1;    // high Y plane
81         }
82
83         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
84         bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
85         bool IsSurface(const Pos &pos) const noexcept;
86
87         void SetNeighbor(Chunk &) noexcept;
88         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
89         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
90         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
91         void ClearNeighbors() noexcept;
92         void Unlink() noexcept;
93         void Relink() noexcept;
94
95         // check which faces of a block at given index are obstructed (and therefore invisible)
96         Block::FaceSet Obstructed(const Pos &) const noexcept;
97
98         void Invalidate() noexcept { dirty = true; }
99
100         void SetBlock(int index, const Block &) noexcept;
101         void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
102         void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
103
104         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
105         const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
106         const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
107
108         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
109         const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
110
111         void SetLight(int index, int level) noexcept;
112         void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
113         void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
114
115         int GetLight(int index) const noexcept;
116         int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
117         int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
118
119         float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
120
121         bool Intersection(
122                 const Ray &ray,
123                 const glm::mat4 &M,
124                 float &dist
125         ) const noexcept {
126                 return blank::Intersection(ray, Bounds(), M, &dist);
127         }
128
129         bool Intersection(
130                 const Ray &,
131                 const glm::mat4 &M,
132                 int &blkid,
133                 float &dist,
134                 glm::vec3 &normal) const noexcept;
135
136         void Position(const Pos &pos) noexcept { position = pos; }
137         const Pos &Position() const noexcept { return position; }
138         glm::mat4 Transform(const Pos &offset) const noexcept {
139                 return glm::translate((position - offset) * Extent());
140         }
141
142         void CheckUpdate() noexcept;
143         void Draw() noexcept;
144
145 private:
146         void Update() noexcept;
147
148 private:
149         const BlockTypeRegistry *types;
150         Chunk *neighbor[Block::FACE_COUNT];
151         Block blocks[16 * 16 * 16];
152         unsigned char light[16 * 16 * 16];
153         BlockModel model;
154         Pos position;
155         bool dirty;
156
157 };
158
159
160 class BlockLookup {
161
162 public:
163         // resolve chunk/position from oob coordinates
164         BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept;
165
166         // resolve chunk/position from ib coordinates and direction
167         BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir) noexcept;
168
169         // check if lookup was successful
170         operator bool() const { return chunk; }
171
172         // only valid if lookup was successful
173         Chunk &GetChunk() const noexcept { return *chunk; }
174         const Chunk::Pos &GetBlockPos() const noexcept { return pos; }
175         const Block &GetBlock() const noexcept { return GetChunk().BlockAt(GetBlockPos()); }
176         const BlockType &GetType() const noexcept { return GetChunk().Type(GetBlock()); }
177         int GetLight() const noexcept { return GetChunk().GetLight(GetBlockPos()); }
178
179         // traverse in given direction
180         BlockLookup Next(Block::Face f) const { return BlockLookup(chunk, pos, f); }
181
182 private:
183         Chunk *chunk;
184         Chunk::Pos pos;
185
186 };
187
188
189 class Generator;
190
191 class ChunkLoader {
192
193 public:
194         struct Config {
195                 int load_dist = 6;
196                 int unload_dist = 8;
197         };
198
199         ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept;
200
201         void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
202         void GenerateSurrounding(const Chunk::Pos &);
203
204         std::list<Chunk> &Loaded() noexcept { return loaded; }
205
206         Chunk *Loaded(const Chunk::Pos &) noexcept;
207         bool Queued(const Chunk::Pos &) noexcept;
208         bool Known(const Chunk::Pos &) noexcept;
209         Chunk &ForceLoad(const Chunk::Pos &);
210
211         void Rebase(const Chunk::Pos &);
212         void Update();
213
214 private:
215         Chunk &Generate(const Chunk::Pos &pos);
216         void Insert(Chunk &) noexcept;
217         void Remove(Chunk &) noexcept;
218
219 private:
220         Chunk::Pos base;
221
222         const BlockTypeRegistry &reg;
223         const Generator &gen;
224
225         std::list<Chunk> loaded;
226         std::list<Chunk::Pos> to_generate;
227         std::list<Chunk> to_free;
228
229         int load_dist;
230         int unload_dist;
231
232 };
233
234 }
235
236 #endif