]> git.localhorst.tv Git - blank.git/blob - src/chunk.hpp
made chunks aware of their neighbors
[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 Chunk::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 Chunk::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 Chunk::Pos ToPos(int idx) {
61                 return Chunk::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         void SetNeighbor(Chunk &);
80         bool HasNeighbor(Block::Face f) const { return neighbor[f]; }
81         Chunk &GetNeighbor(Block::Face f) { return *neighbor[f]; }
82         const Chunk &GetNeighbor(Block::Face f) const { return *neighbor[f]; }
83         void ClearNeighbors();
84         void Unlink();
85         void Relink();
86
87         // check if block at given index is completely enclosed (and therefore invisible)
88         bool Obstructed(int idx) const;
89
90         void Allocate();
91         void Invalidate() { dirty = true; }
92
93         Block &BlockAt(int index) { return blocks[index]; }
94         const Block &BlockAt(int index) const { return blocks[index]; }
95         Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); }
96         const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
97         Block &BlockAt(const Chunk::Pos &pos) { return BlockAt(ToIndex(pos)); }
98         const Block &BlockAt(const Chunk::Pos &pos) const { return BlockAt(ToIndex(pos)); }
99
100         const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
101
102         bool Intersection(
103                 const Ray &ray,
104                 const glm::mat4 &M,
105                 float &dist
106         ) const {
107                 return blank::Intersection(ray, Bounds(), M, &dist);
108         }
109
110         bool Intersection(
111                 const Ray &,
112                 const glm::mat4 &M,
113                 int &blkid,
114                 float &dist,
115                 glm::vec3 &normal) const;
116
117         void Position(const Pos &);
118         const Pos &Position() const { return position; }
119         glm::mat4 Transform(const Pos &offset) const;
120
121         void CheckUpdate();
122         void Draw();
123
124 private:
125         void Update();
126
127 private:
128         const BlockTypeRegistry *types;
129         Chunk *neighbor[Block::FACE_COUNT];
130         std::vector<Block> blocks;
131         Model model;
132         Pos position;
133         bool dirty;
134
135 };
136
137
138 class Generator;
139
140 class ChunkLoader {
141
142 public:
143         ChunkLoader(const BlockTypeRegistry &, const Generator &);
144
145         void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
146
147         std::list<Chunk> &Loaded() { return loaded; }
148
149         Chunk *Loaded(const Chunk::Pos &);
150         bool Queued(const Chunk::Pos &);
151         bool Known(const Chunk::Pos &);
152         Chunk &ForceLoad(const Chunk::Pos &);
153
154         void Rebase(const Chunk::Pos &);
155         void Update();
156
157 private:
158         Chunk &Generate(const Chunk::Pos &pos);
159         void Insert(Chunk &);
160         void Remove(Chunk &);
161
162 private:
163         Chunk::Pos base;
164
165         const BlockTypeRegistry &reg;
166         const Generator &gen;
167
168         std::list<Chunk> loaded;
169         std::list<Chunk::Pos> to_generate;
170         std::list<Chunk> to_free;
171
172         int load_dist;
173         int unload_dist;
174
175 };
176
177 }
178
179 #endif