]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
7eceb980cb62d6c66918ae9b3eb7fa587416c2c3
[blank.git] / src / world / Chunk.hpp
1 #ifndef BLANK_WORLD_CHUNK_HPP_
2 #define BLANK_WORLD_CHUNK_HPP_
3
4 #include "Block.hpp"
5 #include "BlockTypeRegistry.hpp"
6 #include "../geometry/Location.hpp"
7 #include "../geometry/primitive.hpp"
8
9 #include <set>
10 #include <vector>
11 #include <glm/glm.hpp>
12 #include <glm/gtx/transform.hpp>
13
14
15 namespace blank {
16
17 class BlockType;
18 class Entity;
19 class WorldCollision;
20
21 /// cube of size 16 (256 tiles, 4096 blocks)
22 class Chunk {
23
24 public:
25         explicit Chunk(const BlockTypeRegistry &) noexcept;
26
27         Chunk(Chunk &&) noexcept;
28         Chunk &operator =(Chunk &&) noexcept;
29
30         static constexpr int side = ExactLocation::scale;
31         static constexpr float fside = ExactLocation::fscale;
32         static constexpr int size = side * side * side;
33
34         static AABB Bounds() noexcept { return AABB{ { 0.0f, 0.0f, 0.0f }, ExactLocation::FExtent() }; }
35         static glm::vec3 Center() noexcept { return glm::vec3(8.0f); }
36         static float Radius() noexcept { return 27.71281292110203669632f; /* 16 * √3 */ }
37
38         static constexpr bool InBounds(const ExactLocation::Fine &pos) noexcept {
39                 return
40                         pos.x >= 0.0f && pos.x < fside &&
41                         pos.y >= 0.0f && pos.y < fside &&
42                         pos.z >= 0.0f && pos.z < fside;
43         }
44         static constexpr bool InBounds(const RoughLocation::Fine &pos) noexcept {
45                 return
46                         pos.x >= 0 && pos.x < side &&
47                         pos.y >= 0 && pos.y < side &&
48                         pos.z >= 0 && pos.z < side;
49         }
50         static constexpr int ToIndex(const RoughLocation::Fine &pos) noexcept {
51                 return pos.x + pos.y * side + pos.z * side * side;
52         }
53         static constexpr bool InBounds(int idx) noexcept {
54                 return idx >= 0 && idx < size;
55         }
56         static ExactLocation::Fine ToCoords(int idx) noexcept {
57                 return ExactLocation::Fine(
58                         0.5f + (idx % side),
59                         0.5f + ((idx / side) % side),
60                         0.5f + (idx / (side * side))
61                 );
62         }
63         static ExactLocation::Fine ToCoords(const RoughLocation::Fine &pos) noexcept {
64                 return ExactLocation::Fine(pos) + 0.5f;
65         }
66         static RoughLocation::Fine ToPos(int idx) noexcept {
67                 return RoughLocation::Fine(
68                         (idx % side),
69                         ((idx / side) % side),
70                         (idx / (side * side))
71                 );
72         }
73         /// get a chunk-local transform for block at given position and index
74         /// (position and index are redundant)
75         glm::mat4 ToTransform(const RoughLocation::Fine &position, int index) const noexcept;
76         /// same as above, but also apply offset to given reference
77         glm::mat4 ToTransform(const ExactLocation::Coarse &ref, const RoughLocation::Fine &pos, int idx) const noexcept;
78
79         ExactLocation::Fine ToSceneCoords(const ExactLocation::Coarse &base, const ExactLocation::Fine &pos) const noexcept {
80                 return ExactLocation::Fine((position - base) * ExactLocation::Extent()) + pos;
81         }
82
83         static bool IsBorder(const RoughLocation::Fine &pos) noexcept {
84                 return
85                         pos.x == 0 ||
86                         pos.x == side - 1 ||
87                         pos.y == 0 ||
88                         pos.y == side - 1 ||
89                         pos.z == 0 ||
90                         pos.z == side - 1;
91         }
92         static constexpr bool IsBorder(int idx) noexcept {
93                 return
94                         idx < side * side ||                 // low Z plane
95                         idx % side == 0 ||                   // low X plane
96                         (idx / (side * side)) == side - 1 || // high Z plane
97                         idx % side == side - 1 ||            // high X plane
98                         (idx / side) % side == 0 ||          // low Y plane
99                         (idx / side) % side == side - 1;     // high Y plane
100         }
101
102         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
103         bool IsSurface(const ExactLocation::Fine &pos) const noexcept { return IsSurface(RoughLocation::Fine(pos)); }
104         bool IsSurface(const RoughLocation::Fine &pos) const noexcept;
105
106         void SetNeighbor(Block::Face, Chunk &) noexcept;
107         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
108         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
109         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
110         void Unlink() noexcept;
111
112         // check which faces of a block at given index are obstructed (and therefore invisible)
113         Block::FaceSet Obstructed(const RoughLocation::Fine &) const noexcept;
114
115         void SetBlock(int index, const Block &) noexcept;
116         void SetBlock(const ExactLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
117         void SetBlock(const RoughLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
118
119         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
120         const Block &BlockAt(const ExactLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
121         const Block &BlockAt(const RoughLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
122
123         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
124         const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
125
126         void SetLight(int index, int level) noexcept;
127         void SetLight(const ExactLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
128         void SetLight(const RoughLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
129
130         int GetLight(int index) const noexcept;
131         int GetLight(const ExactLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
132         int GetLight(const RoughLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
133
134         float GetVertexLight(const RoughLocation::Fine &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept;
135
136         /// get gravity for one unit mass at given point
137         glm::vec3 GravityAt(const ExactLocation &) const noexcept;
138
139         bool Intersection(
140                 const Ray &ray,
141                 const ExactLocation::Coarse &reference,
142                 float &dist
143         ) const noexcept {
144                 return blank::Intersection(ray, Bounds(), Transform(reference), &dist);
145         }
146
147         /// check if given ray intersects any block of this chunk
148         /// given reference indicated the chunk offset of the ray in world space
149         bool Intersection(
150                 const Ray &,
151                 const ExactLocation::Coarse &reference,
152                 WorldCollision &) noexcept;
153
154         bool Intersection(
155                 const AABB &box,
156                 const glm::mat4 &Mbox,
157                 const glm::mat4 &Mchunk,
158                 std::vector<WorldCollision> &) noexcept;
159
160         bool Intersection(
161                 const Entity &entity,
162                 const glm::mat4 &Mentity,
163                 const glm::mat4 &Mchunk,
164                 std::vector<WorldCollision> &) noexcept;
165
166         void Position(const ExactLocation::Coarse &pos) noexcept { position = pos; }
167         const ExactLocation::Coarse &Position() const noexcept { return position; }
168
169         glm::mat4 Transform(const ExactLocation::Coarse &offset) const noexcept {
170                 return glm::translate((position - offset) * ExactLocation::Extent());
171         }
172
173         void *BlockData() noexcept { return &blocks[0]; }
174         const void *BlockData() const noexcept { return &blocks[0]; }
175         static constexpr std::size_t BlockSize() noexcept { return offsetof(Chunk, position) - offsetof(Chunk, blocks); }
176
177         bool Generated() const noexcept { return generated; }
178         void SetGenerated() noexcept { generated = true; }
179         bool Lighted() const noexcept { return lighted; }
180         void ScanLights();
181
182         /// check for active blocks, should be called after
183         /// block data was modified by means other than SetBlock()
184         void ScanActive();
185
186         void Ref() noexcept { ++ref_count; }
187         void UnRef() noexcept { --ref_count; }
188         bool Referenced() const noexcept { return ref_count > 0; }
189
190         void Invalidate() noexcept { dirty_mesh = dirty_save = true; }
191         void InvalidateMesh() noexcept { dirty_mesh = true; }
192         void ClearMesh() noexcept { dirty_mesh = false; }
193         void ClearSave() noexcept { dirty_save = false; }
194         bool ShouldUpdateMesh() const noexcept { return dirty_mesh; }
195         bool ShouldUpdateSave() const noexcept { return dirty_save; }
196
197         void Update(BlockMesh &) noexcept;
198
199 private:
200         const BlockTypeRegistry *types;
201         Chunk *neighbor[Block::FACE_COUNT];
202
203         std::set<int> gravity;
204
205         Block blocks[size];
206         unsigned char light[size];
207         bool generated;
208         bool lighted;
209
210         ExactLocation::Coarse position;
211         int ref_count;
212         bool dirty_mesh;
213         bool dirty_save;
214
215 };
216
217 }
218
219 #endif