1 #ifndef BLANK_CHUNK_HPP_
2 #define BLANK_CHUNK_HPP_
5 #include "geometry.hpp"
10 #include <glm/glm.hpp>
11 #include <glm/gtx/transform.hpp>
16 /// cube of size 16 (256 tiles, 4096 blocks)
20 using Pos = glm::tvec3<int>;
23 explicit Chunk(const BlockTypeRegistry &) noexcept;
25 Chunk(Chunk &&) noexcept;
26 Chunk &operator =(Chunk &&) noexcept;
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(); }
34 static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
36 static constexpr bool InBounds(const Block::Pos &pos) noexcept {
38 pos.x >= 0 && pos.x < Width() &&
39 pos.y >= 0 && pos.y < Height() &&
40 pos.z >= 0 && pos.z < Depth();
42 static constexpr bool InBounds(const Pos &pos) noexcept {
44 pos.x >= 0 && pos.x < Width() &&
45 pos.y >= 0 && pos.y < Height() &&
46 pos.z >= 0 && pos.z < Depth();
48 static constexpr int ToIndex(const Pos &pos) noexcept {
49 return pos.x + pos.y * Width() + pos.z * Width() * Height();
51 static constexpr bool InBounds(int idx) noexcept {
52 return idx >= 0 && idx < Size();
54 static Block::Pos ToCoords(int idx) noexcept {
56 0.5f + (idx % Width()),
57 0.5f + ((idx / Width()) % Height()),
58 0.5f + (idx / (Width() * Height()))
61 static Pos ToPos(int idx) noexcept {
64 ((idx / Width()) % Height()),
65 (idx / (Width() * Height()))
68 glm::mat4 ToTransform(int idx) const noexcept;
70 static constexpr bool IsBorder(int idx) noexcept {
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
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;
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;
92 // check which faces of a block at given index are obstructed (and therefore invisible)
93 Block::FaceSet Obstructed(int idx) const noexcept;
95 void Invalidate() noexcept { dirty = true; }
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); }
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)); }
105 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
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); }
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)); }
115 float GetVertexLight(int index, const BlockModel::Position &, const Model::Normal &) const noexcept;
122 return blank::Intersection(ray, Bounds(), M, &dist);
130 glm::vec3 &normal) const noexcept;
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());
138 void CheckUpdate() noexcept;
139 void Draw() noexcept;
142 void Update() noexcept;
145 const BlockTypeRegistry *types;
146 Chunk *neighbor[Block::FACE_COUNT];
147 Block blocks[16 * 16 * 16];
148 unsigned char light[16 * 16 * 16];
159 // resolve chunk/position from oob coordinates
160 BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept;
162 // resolve chunk/position from ib coordinates and direction
163 BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir) noexcept;
165 // check if lookup was successful
166 operator bool() const { return chunk; }
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()); }
192 ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept;
194 void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
195 void GenerateSurrounding(const Chunk::Pos &);
197 std::list<Chunk> &Loaded() noexcept { return loaded; }
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 &);
204 void Rebase(const Chunk::Pos &);
208 Chunk &Generate(const Chunk::Pos &pos);
209 void Insert(Chunk &) noexcept;
210 void Remove(Chunk &) noexcept;
215 const BlockTypeRegistry ®
216 const Generator &gen;
218 std::list<Chunk> loaded;
219 std::list<Chunk::Pos> to_generate;
220 std::list<Chunk> to_free;