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 glm::vec3 &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 int ToIndex(const glm::vec3 &pos) {
42 return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
44 static constexpr bool InBounds(int idx) {
45 return idx >= 0 && idx < Size();
47 static Block::Pos ToCoords(int idx) {
49 0.5f + (idx % Width()),
50 0.5f + ((idx / Width()) % Height()),
51 0.5f + (idx / (Width() * Height()))
54 glm::mat4 ToTransform(int idx) const;
56 static constexpr bool IsBorder(int idx) {
58 idx < Width() * Height() ||
59 (idx / Width()) % Height() == 0 ||
60 (idx / Width()) % Height() == Height() - 1 ||
61 (idx / (Width() * Height())) == Depth() - 1;
64 // check if block at given index is completely enclosed (and therefore invisible)
65 bool Obstructed(int idx) const;
68 void Invalidate() { dirty = true; }
70 Block &BlockAt(int index) { return blocks[index]; }
71 const Block &BlockAt(int index) const { return blocks[index]; }
72 Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); }
73 const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
75 const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
81 float *dist = nullptr,
82 glm::vec3 *normal = nullptr) const;
84 void Position(const Pos &);
85 const Pos &Position() const { return position; }
86 glm::mat4 Transform(const Pos &offset) const;
95 const BlockTypeRegistry *types;
96 std::vector<Block> blocks;
109 ChunkLoader(const BlockTypeRegistry &, const Generator &);
111 void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
113 std::list<Chunk> &Loaded() { return loaded; }
115 Chunk *Loaded(const Chunk::Pos &);
116 bool Queued(const Chunk::Pos &);
117 bool Known(const Chunk::Pos &);
118 Chunk &ForceLoad(const Chunk::Pos &);
120 void Rebase(const Chunk::Pos &);
126 const BlockTypeRegistry ®
127 const Generator &gen;
129 std::list<Chunk> loaded;
130 std::list<Chunk::Pos> to_generate;
131 std::list<Chunk> to_free;