1 #ifndef BLANK_CHUNK_HPP_
2 #define BLANK_CHUNK_HPP_
5 #include "geometry.hpp"
10 #include <glm/glm.hpp>
15 /// cube of size 16 (256 tiles, 4096 blocks)
19 using Pos = glm::tvec3<int>;
22 explicit Chunk(const BlockTypeRegistry &);
25 Chunk &operator =(Chunk &&);
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(); }
33 static AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; }
35 static constexpr bool InBounds(const Block::Pos &pos) {
37 pos.x >= 0 && pos.x < Width() &&
38 pos.y >= 0 && pos.y < Height() &&
39 pos.z >= 0 && pos.z < Depth();
41 static constexpr bool InBounds(const Pos &pos) {
43 pos.x >= 0 && pos.x < Width() &&
44 pos.y >= 0 && pos.y < Height() &&
45 pos.z >= 0 && pos.z < Depth();
47 static constexpr int ToIndex(const Pos &pos) {
48 return pos.x + pos.y * Width() + pos.z * Width() * Height();
50 static constexpr bool InBounds(int idx) {
51 return idx >= 0 && idx < Size();
53 static Block::Pos ToCoords(int idx) {
55 0.5f + (idx % Width()),
56 0.5f + ((idx / Width()) % Height()),
57 0.5f + (idx / (Width() * Height()))
60 static Pos ToPos(int idx) {
63 ((idx / Width()) % Height()),
64 (idx / (Width() * Height()))
67 glm::mat4 ToTransform(int idx) const;
69 static constexpr bool IsBorder(int idx) {
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
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;
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();
91 // check if block at given index is completely enclosed (and therefore invisible)
92 bool Obstructed(int idx) const;
94 void Invalidate() { dirty = true; }
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); }
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)); }
104 const Block *FindNext(const Pos &pos, Block::Face face) const;
105 const Block *FindNext(const Block::Pos &pos, Block::Face face) const { return FindNext(Pos(pos), face); }
106 const Block *FindNext(int index, Block::Face face) const { return FindNext(ToPos(index), face); }
108 const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
110 void SetLight(int index, int level);
111 void SetLight(const Pos &pos, int level) { SetLight(ToIndex(pos), level); }
112 void SetLight(const Block::Pos &pos, int level) { SetLight(ToIndex(pos), level); }
114 int GetLight(int index) const;
115 int GetLight(const Pos &pos) const { return GetLight(ToIndex(pos)); }
116 int GetLight(const Block::Pos &pos) const { return GetLight(ToIndex(pos)); }
118 float GetVertexLight(int index, const BlockModel::Position &, const BlockModel::Normal &) const;
125 return blank::Intersection(ray, Bounds(), M, &dist);
133 glm::vec3 &normal) const;
135 void Position(const Pos &);
136 const Pos &Position() const { return position; }
137 glm::mat4 Transform(const Pos &offset) const;
146 const BlockTypeRegistry *types;
147 Chunk *neighbor[Block::FACE_COUNT];
148 Block blocks[16 * 16 * 16];
149 unsigned char light[16 * 16 * 16];
160 // resolve chunk/position from oob coordinates
161 BlockLookup(Chunk *c, const Chunk::Pos &p);
163 // resolve chunk/position from ib coordinates and direction
164 BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir);
166 // check if lookup was successful
167 operator bool() const { return chunk; }
169 // only valid if lookup was successful
170 Chunk &GetChunk() const { return *chunk; }
171 const Chunk::Pos &GetBlockPos() const { return pos; }
172 const Block &GetBlock() const { return GetChunk().BlockAt(GetBlockPos()); }
173 const BlockType &GetType() const { return GetChunk().Type(GetBlock()); }
174 int GetLight() const { return GetChunk().GetLight(GetBlockPos()); }
193 ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &);
195 void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
196 void GenerateSurrounding(const Chunk::Pos &);
198 std::list<Chunk> &Loaded() { return loaded; }
200 Chunk *Loaded(const Chunk::Pos &);
201 bool Queued(const Chunk::Pos &);
202 bool Known(const Chunk::Pos &);
203 Chunk &ForceLoad(const Chunk::Pos &);
205 void Rebase(const Chunk::Pos &);
209 Chunk &Generate(const Chunk::Pos &pos);
210 void Insert(Chunk &);
211 void Remove(Chunk &);
216 const BlockTypeRegistry ®
217 const Generator &gen;
219 std::list<Chunk> loaded;
220 std::list<Chunk::Pos> to_generate;
221 std::list<Chunk> to_free;