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